Skip to content

Commit

Permalink
mm/khugepaged: fix invalid page access in release_pte_pages()
Browse files Browse the repository at this point in the history
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.com
Signed-off-by: Vishal Moola (Oracle) <vishal.moola@gmail.com>
Fixes: 9bdfeea ("mm/khugepaged: convert release_pte_pages() to use folios")
Reported-by: Marek Szyprowski <m.szyprowski@samsung.com>
Tested-by: Marek Szyprowski <m.szyprowski@samsung.com>
Debugged-by: Alexandre Ghiti <alex@ghiti.fr>
Cc: Matthew Wilcox <willy@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
  • Loading branch information
Vishal Moola (Oracle) authored and Andrew Morton committed Feb 13, 2023
1 parent f67d6b2 commit f528260
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions mm/khugepaged.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,17 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte,

while (--_pte >= pte) {
pte_t pteval = *_pte;
unsigned long pfn;

folio = pfn_folio(pte_pfn(pteval));
if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)) &&
!folio_test_large(folio))
release_pte_folio(folio);
if (pte_none(pteval))
continue;
pfn = pte_pfn(pteval);
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) {
Expand Down

0 comments on commit f528260

Please sign in to comment.