Re: [PATCH 1/2] mm/huge_memory: set memcg_data before a split page leaves the folio
From: Andrew Morton
Date: Tue Dec 01 2026 - 00:25:02 EST
On Wed, 04 Nov 2026 11:33:17 -0500 "Zi Yan" <ziy@xxxxxxxxxx> wrote:
> On Wed Nov 4, 2026 at 4:59 AM EST, Daejun Park via B4 Relay wrote:
> > From: Daejun Park <daejun7.park@xxxxxxxxxxx>
> >
> > __split_folio_to_order() copies memcg_data to a new folio after
> > clear_compound_head(). Until then, memcg_data of the first tail page
> > holds the folio's _nr_pages. page_cgroup_ino() can see that page as a
> > folio in the window and take _nr_pages for an objcg pointer. Reading
> > /proc/kpagecgroup as root then oopses
OK, so it's an oopser, normally requiring root to trigger it.
> > +#ifdef CONFIG_MEMCG
> > + /*
> > + * In the first tail page, memcg_data still holds
> > + * folio->_nr_pages. Lockless PFN walkers (page_cgroup_ino(),
> > + * page_owner) read memcg_data once the page is not a tail.
> > + */
>
> How about? Because clear_compound_head() makes the new_folio be
> recognized as a folio by reference-free readers.
>
> Assign memcg data before clear_compound_head(), so lockless,
> reference-free readers get the right value instead of bogus data from
> the overlaid ->_nr_pages in the first tail page.
Thanks both, I queued this -fix, Looks suitable?
+++ a/mm/huge_memory.c
@@ -3860,9 +3860,10 @@ static void __split_folio_to_order(struc
#ifdef CONFIG_MEMCG
/*
- * In the first tail page, memcg_data still holds
- * folio->_nr_pages. Lockless PFN walkers (page_cgroup_ino(),
- * page_owner) read memcg_data once the page is not a tail.
+ * clear_compound_head() makes new_folio visible as a folio to
+ * lockless, reference-free PFN walkers. In the first tail page,
+ * memcg_data overlays folio->_nr_pages. Set it before clearing
+ * compound_head so readers cannot mistake _nr_pages for an objcg.
*/
new_folio->memcg_data = folio->memcg_data;
#endif
_