Re: [PATCH v3 2/3] mm: khugepaged: fix folio is used after pte_unmap_unlock()

From: Vernon Yang

Date: Tue Aug 25 2026 - 22:46:28 EST


On Mon, Aug 24, 2026 at 01:57:15PM +0200, David Hildenbrand (Arm) wrote:
> On 8/24/26 11:29, Vernon Yang wrote:
> > From: Vernon Yang <yanglincheng@xxxxxxxxxx>
> >
> > After the page table lock has dropped, the folio can be freed
> > concurrently. The trace_mm_khugepaged_scan_pmd() is left with
> > a dangling folio pointer.
> >
> > So using the folio_pfn() before dropping the page table lock,
> > closing use-after-free window.
> >
> > Fixes: 7d2eba0557c1 ("mm: add tracepoint for scanning pages")
> > Cc: stable@xxxxxxxxxxxxxxx
> > Signed-off-by: Vernon Yang <yanglincheng@xxxxxxxxxx>
> > ---
> > include/trace/events/huge_memory.h | 6 +++---
> > mm/khugepaged.c | 5 ++++-
> > 2 files changed, 7 insertions(+), 4 deletions(-)
> >
> > diff --git a/include/trace/events/huge_memory.h b/include/trace/events/huge_memory.h
> > index 7b526528f85b..fa828967e1fb 100644
> > --- a/include/trace/events/huge_memory.h
> > +++ b/include/trace/events/huge_memory.h
> > @@ -55,10 +55,10 @@ SCAN_STATUS
> >
> > TRACE_EVENT(mm_khugepaged_scan_pmd,
> >
> > - TP_PROTO(struct mm_struct *mm, struct folio *folio,
> > + TP_PROTO(struct mm_struct *mm, unsigned long pfn,
> > int referenced, int none_or_zero, int status, int unmapped),
> >
> > - TP_ARGS(mm, folio, referenced, none_or_zero, status, unmapped),
> > + TP_ARGS(mm, pfn, referenced, none_or_zero, status, unmapped),
> >
> > TP_STRUCT__entry(
> > __field(struct mm_struct *, mm)
> > @@ -71,7 +71,7 @@ TRACE_EVENT(mm_khugepaged_scan_pmd,
> >
> > TP_fast_assign(
> > __entry->mm = mm;
> > - __entry->pfn = folio ? folio_pfn(folio) : -1;
> > + __entry->pfn = pfn;
> > __entry->referenced = referenced;
> > __entry->none_or_zero = none_or_zero;
> > __entry->status = status;
> > diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> > index 00337405c0e0..4e0fca5942dd 100644
> > --- a/mm/khugepaged.c
> > +++ b/mm/khugepaged.c
> > @@ -1618,6 +1618,7 @@ static enum scan_result collapse_scan_pmd(struct mm_struct *mm,
> > enum scan_result result = SCAN_FAIL;
> > struct page *page = NULL;
> > struct folio *folio = NULL;
> > + unsigned long pfn = -1;
> > unsigned long addr;
> > unsigned long enabled_orders;
> > spinlock_t *ptl;
> > @@ -1780,6 +1781,8 @@ static enum scan_result collapse_scan_pmd(struct mm_struct *mm,
> > result = SCAN_SUCCEED;
> > }
> > out_unmap:
> > + if (folio)
> > + pfn = folio_pfn(folio);
>
> Should we reset the folio to NULL at the beginning of the loop? Then we really
> only trace the PFN if it really was problematic.

Yes, this is a pre-existing bug, and I'll fix it together. Thanks!

But it is not at the beginning of the loop, it is at the __ending__ of
the loop, for the same reason as PATCH#1.

--
Cheers,
Vernon