Commit f528260b authored by Vishal Moola (Oracle)'s avatar Vishal Moola (Oracle) Committed by Andrew Morton

mm/khugepaged: fix invalid page access in release_pte_pages()

release_pte_pages() converts from a pfn to a folio by using pfn_folio(). 
If the pte is not mapped, pfn_folio() will result in undefined behavior
which ends up causing a kernel panic[1].

Only call pfn_folio() once we have validated that the pte is both valid
and mapped to fix the issue.

[1] https://lore.kernel.org/linux-mm/ff300770-afe9-908d-23ed-d23e0796e899@samsung.com/

Link: https://lkml.kernel.org/r/20230213214324.34215-1-vishal.moola@gmail.comSigned-off-by: default avatarVishal Moola (Oracle) <vishal.moola@gmail.com>
Fixes: 9bdfeea4 ("mm/khugepaged: convert release_pte_pages() to use folios")
Reported-by: default avatarMarek Szyprowski <m.szyprowski@samsung.com>
Tested-by: default avatarMarek Szyprowski <m.szyprowski@samsung.com>
Debugged-by: default avatarAlexandre Ghiti <alex@ghiti.fr>
Cc: Matthew Wilcox <willy@infradead.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent f67d6b26
...@@ -511,11 +511,17 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte, ...@@ -511,11 +511,17 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte,
while (--_pte >= pte) { while (--_pte >= pte) {
pte_t pteval = *_pte; pte_t pteval = *_pte;
unsigned long pfn;
folio = pfn_folio(pte_pfn(pteval)); if (pte_none(pteval))
if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)) && continue;
!folio_test_large(folio)) pfn = pte_pfn(pteval);
release_pte_folio(folio); if (is_zero_pfn(pfn))
continue;
folio = pfn_folio(pfn);
if (folio_test_large(folio))
continue;
release_pte_folio(folio);
} }
list_for_each_entry_safe(folio, tmp, compound_pagelist, lru) { list_for_each_entry_safe(folio, tmp, compound_pagelist, lru) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment