From: "David Hildenbrand (Arm)" <david@kernel.org>
To: daejun7.park@samsung.com, Andrew Morton <akpm@linux-foundation.org>
Cc: Tao Yi <escape@deepseek.com>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Hocko <mhocko@kernel.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Shakeel Butt <shakeel.butt@linux.dev>,
Muchun Song <muchun.song@linux.dev>, Zi Yan <ziy@nvidia.com>,
Matthew Wilcox <willy@infradead.org>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
"Liam R. Howlett" <liam@infradead.org>,
Nico Pache <nico.pache@linux.dev>,
Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
Barry Song <baohua@kernel.org>, Lance Yang <lance.yang@linux.dev>,
Usama Arif <usama.arif@linux.dev>,
Kiryl Shutsemau <kas@kernel.org>,
Vlastimil Babka <vbabka@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Brendan Jackman <brendan.jackman@linux.dev>,
linux-mm@kvack.org, cgroups@vger.kernel.org,
linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH 1/2] mm/huge_memory: set memcg_data before a split page leaves the folio
Date: Tue, 1 Dec 2026 19:03:13 +0100 [thread overview]
Message-ID: <f53ea242-be39-4031-b860-f7c4f7b1c11c@kernel.org> (raw)
In-Reply-To: <20261104-kpagecgroup-split-v1-1-f70e947fec85@samsung.com>
On 11/4/26 10:59, Daejun Park via B4 Relay wrote:
> From: Daejun Park <daejun7.park@samsung.com>
>
> __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 while folios are split to
> order 0.
>
> Copy memcg_data before the smp_wmb(), as before commit fa23a338de93
> ("mm: separate folio_split_memcg_refs() from split_page_memcg()"). Add
> the paired smp_rmb() to page_cgroup_ino().
>
> Fixes: fa23a338de93 ("mm: separate folio_split_memcg_refs() from split_page_memcg()")
> Reported-by: Tao Yi <escape@deepseek.com>
> Closes: https://lore.kernel.org/all/20260907124001.1544655-1-escape@deepseek.com/
> Cc: stable@vger.kernel.org
> Signed-off-by: Daejun Park <daejun7.park@samsung.com>
> ---
> Tested on x86_64 with KASAN (7.3-rc7 and mm-hotfixes-unstable) in an
> 8-vCPU QEMU guest: six threads read /proc/kpagecgroup for page 1 of 64
> THPs that debugfs split_huge_pages splits to order 0. Base oopsed in 6
> of 6 runs, 4 of them within 1.5 s. With patch 1, 8 runs of 300 s had
> no oops.
>
> Before 7.4, page_cgroup_ino() and folio_memcg_check() take non-const
> pointers, so a stable backport needs "struct folio *folio" there. The
> hunk applies to those trees without a conflict, and the build then
> fails with CONFIG_WERROR=y (-Wdiscarded-qualifiers).
> ---
> mm/huge_memory.c | 17 +++++++++++++----
> mm/memcontrol.c | 9 ++++++++-
> 2 files changed, 21 insertions(+), 5 deletions(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 14599d0fff..adc5a47ee7 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -3768,7 +3768,19 @@ static void __split_folio_to_order(struct folio *folio, int old_order,
> if (folio_test_swapcache(folio))
> new_folio->swap.val = folio->swap.val + i;
>
> - /* Page flags must be visible before we make the page non-compound. */
> +#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.
> + */
> + new_folio->memcg_data = folio->memcg_data;
> +#endif
> +
> + /*
> + * Page flags and memcg_data must be visible before we make the
> + * page non-compound.
> + */
> smp_wmb();
>
> /*
> @@ -3803,9 +3815,6 @@ static void __split_folio_to_order(struct folio *folio, int old_order,
> folio_set_young(new_folio);
> if (folio_test_idle(folio))
> folio_set_idle(new_folio);
> -#ifdef CONFIG_MEMCG
> - new_folio->memcg_data = folio->memcg_data;
> -#endif
>
> folio_xchg_last_cpupid(new_folio, folio_last_cpupid(folio));
> }
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 84f4a8fd4d..c244d56446 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -375,12 +375,19 @@ struct cgroup_subsys_state *get_mem_cgroup_css_from_folio(const struct folio *fo
> */
> ino_t page_cgroup_ino(const struct page *page)
> {
> + const struct folio *folio;
> struct mem_cgroup *memcg;
> unsigned long ino = 0;
>
> rcu_read_lock();
> /* page_folio() is racy here, but the entire function is racy anyway */
> - memcg = folio_memcg_check(page_folio(page));
> + folio = page_folio(page);
> + /*
> + * Order the memcg_data load after the compound_info load in
> + * page_folio(). Pairs with smp_wmb() in __split_folio_to_order().
> + */
> + smp_rmb();
> + memcg = folio_memcg_check(folio);
>
> while (memcg && !css_is_online(&memcg->css))
> memcg = parent_mem_cgroup(memcg);
>
Looking at sashiko reports, we have something related (although less relevant
because /proc/kpagecgroup I assume):
In page_cgroup_ino() in mm/memcontrol.c, locklessly inspecting a struct
page via /proc/kpagecgroup races with concurrent compound page preparation
of order >= 3, causing folio_memcg_check() to misinterpret the compound
page count as an obj_cgroup pointer:
CPU 0 (/proc/kpagecgroup read) CPU 1 (compound page allocation)
------------------------------ --------------------------------
kpagecgroup_read()
page_cgroup_ino(page) // page = head + 1
folio = page_folio(page);
// sees non-tail page, returns (struct folio *)page
prep_compound_page(head, order)
prep_compound_tail(head + 1, ...)
prep_compound_head(head, order)
folio_set_order(head_folio, order)
head_folio->_nr_pages = 1U << order;
folio_memcg_check(folio)
memcg_data = READ_ONCE(folio->memcg_data);
// reads 1U << order from overlaid _nr_pages
objcg = (void *)(memcg_data & ~OBJEXTS_FLAGS_MASK);
obj_cgroup_memcg(objcg)
return objcg->memcg; // invalid pointer dereference
When page_cgroup_ino() inspects an unreferenced page at PFN head + 1
before prep_compound_tail() marks it as a tail page (or when prep_compound_tail()
and folio_set_order() are reordered), page_folio() returns the page itself
as a folio. If that page is concurrently initialized as the first tail
page of a compound folio via folio_set_order(), writing head_folio->_nr_pages
(1U << order) overwrites the memory overlaying folio->memcg_data in the
first tail page. Because page_cgroup_ino() does not revalidate that the
page is not a tail page after reading folio->memcg_data, and the low 3
flag bits (OBJEXTS_FLAGS_MASK) of 1U << order are zero for orders >= 3,
folio_memcg_check() casts the page count to a struct obj_cgroup pointer
and passes it to obj_cgroup_memcg(), resulting in a kernel oops when
dereferencing objcg->memcg.
Daejun,can you take care of this as well?
--
Cheers,
David
next prev parent reply other threads:[~2026-12-01 18:03 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-04 9:59 [PATCH 0/2] mm: fix memcg_data reads during a THP split Daejun Park via B4 Relay
2026-11-04 9:59 ` [PATCH 1/2] mm/huge_memory: set memcg_data before a split page leaves the folio Daejun Park via B4 Relay
2026-11-04 16:33 ` Zi Yan
2026-12-01 5:24 ` Andrew Morton
[not found] ` <CGME20261104163327epcas2p1f056cd220dc0d3e40e6fcea082f01b74@epcms2p6>
2026-11-05 1:06 ` Daejun Park
2026-12-01 18:03 ` David Hildenbrand (Arm) [this message]
2026-11-04 9:59 ` [PATCH 2/2] mm/page_owner: also check PageTail before reading memcg_data Daejun Park via B4 Relay
2026-11-04 16:38 ` Zi Yan
2026-11-06 8:57 ` [PATCH 0/2] mm: fix memcg_data reads during a THP split Tao Yi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f53ea242-be39-4031-b860-f7c4f7b1c11c@kernel.org \
--to=david@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=brendan.jackman@linux.dev \
--cc=cgroups@vger.kernel.org \
--cc=daejun7.park@samsung.com \
--cc=dev.jain@arm.com \
--cc=escape@deepseek.com \
--cc=hannes@cmpxchg.org \
--cc=kas@kernel.org \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=muchun.song@linux.dev \
--cc=nico.pache@linux.dev \
--cc=roman.gushchin@linux.dev \
--cc=ryan.roberts@arm.com \
--cc=shakeel.butt@linux.dev \
--cc=stable@vger.kernel.org \
--cc=surenb@google.com \
--cc=usama.arif@linux.dev \
--cc=vbabka@kernel.org \
--cc=willy@infradead.org \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®