Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Muchun Song <muchun.song@linux.dev>
To: Jason Baron <jbaron@akamai.com>
Cc: Muchun Song <muchun.song@linux.dev>,
	stable@vger.kernel.org, regressions@lists.linux.dev,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Sasha Levin <sashal@kernel.org>, Kiryl Shutsemau <kas@kernel.org>,
	Muchun Song <songmuchun@bytedance.com>,
	Oscar Salvador <osalvador@suse.de>,
	David Hildenbrand <david@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	linux-mm@kvack.org
Subject: Re: [REGRESSION] 6.18.y: HugeTLB vmemmap restore leaves stale struct pages since v6.18.51
Date: Wed, 2 Dec 2026 10:21:06 +0800	[thread overview]
Message-ID: <67F3ADCC-2F22-4447-9613-470F369B0FA7@linux.dev> (raw)
In-Reply-To: <20261201180905.2170289-1-jbaron@akamai.com>



> On Dec 2, 2026, at 02:09, Jason Baron <jbaron@akamai.com> wrote:
> 
> Hi,
> 
> Freeing a hugetlb page that had its vmemmap optimized (HVO) corrupts
> struct pages on stable 6.18.y starting with v6.18.51.  The stale data is
> later read as a bogus memcg pointer and the kernel oopses.  v6.18.50 is
> fine.  I can reproduce this on a vanilla v6.18.66 build in a VM with the
> short program at the end of this mail.
> 
> #regzbot introduced: v6.18.50..v6.18.51
> 
> Affected
> --------
> - 6.18.y from v6.18.51 through v6.18.66 (reproduced on v6.18.66; also
>   seen on a 6.18.60-based kernel).  v6.18.50 and earlier still have
>   reset_struct_pages() and are not affected.
> - x86-64 with CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP=y and HVO enabled
>   (CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON=y or
>   hugetlb_free_vmemmap=on).  hugetlb_free_vmemmap=off avoids the problem.
> - Trigger: any free of an HVO-optimized hugetlb page to the buddy
>   allocator, e.g. shrinking /proc/sys/vm/nr_hugepages, or surplus
>   hugetlb pages being released when their users exit.
> 
> What goes wrong
> ---------------
> vmemmap_restore_pte() in mm/hugetlb_vmemmap.c restores the vmemmap pages
> that HVO had remapped to one shared read-only page.  In 6.18.y that
> shared page is a copy of the head's vmemmap page
> (vmemmap_tail = vmemmap_head in hugetlb_vmemmap_optimize_folio()), so
> entries 0-3 of it hold the head page and the folio's metadata pages.
> Entry 1 contains _nr_pages (512 for a 2 MB folio) at offset 0x38, which
> overlays memcg_data of an ordinary page.
> 
> Up to v6.18.50 the restore did copy_page() followed by
> reset_struct_pages(), which turns entries 0-3 of every restored page into
> ordinary tail pages. 
> 
> That is correct upstream because 622026e87c40 ("mm/hugetlb: remove fake
> head pages") makes the shared tail page a properly initialised tail
> template.  That commit, and the rest of that series, is not in 6.18.y,
> so the shared page here is still the head copy.  Every restored vmemmap
> page therefore starts with head metadata: PG_head and a hugetlb
> page_type at entry 0, and _nr_pages at +0x38 of entry 1.
> 
> Those struct pages go back to the buddy allocator stale.  With page
> sanity checks enabled the problem is reported when the hugepage is freed
> (splat below).  Without the checks, the next user of a page whose pfn
> satisfies pfn % 64 == 1 sees memcg_data == 0x200, i.e. a page charged to
> a memcg at address 0x200, and oopses in the memcg code.
> 
> Reproducer
> ----------
> The program at the end of this mail enables HVO, grows the hugepage pool
> and shrinks it back to 0.  Run it as root on a kernel with page checks on
> (boot with init_on_free=1, or CONFIG_DEBUG_VM=y).  On v6.18.66 it logs
> "Bad page state" reports on the first run; it exits 1 when it sees them.
> 
> Splat on vanilla v6.18.66 (QEMU VM, init_on_free=1, not tainted):
> 
> [   51.936217] BUG: Bad page state in process hvo-restore-rep  pfn:381040
> [   51.938599] page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x381040
> [   51.938602] head: order:9 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0
> [   51.938603] flags: 0x17fffe000000040(head|node=0|zone=2|lastcpupid=0x3ffff)
> [   51.938607] page_type: f4(hugetlb)
> [   51.938609] raw: 017fffe000000040 ffffcdd380bebc98 ffffcdd380bebc98 0000000000000000
> [   51.938611] raw: 0000000000000000 0000000000000010 00000000f4000000 0000000000000000
> [   51.938612] head: 017fffe000000040 ffffcdd380bebc98 ffffcdd380bebc98 0000000000000000
> [   51.938613] head: 0000000000000000 0000000000000010 00000000f4000000 0000000000000000
> [   51.938614] head: 017fffe000000009 fffff7664e040001 00000000ffffffff 00000000ffffffff
> [   51.938615] head: ffffffffffffffff 0000000000000000 00000000ffffffff 0000000000000200
> [   51.938616] page dumped because: corrupted mapping in tail page
> [   51.938652] CPU: 21 UID: 0 PID: 3199 Comm: hvo-restore-rep Not tainted 6.18.66 #1 PREEMPT(none)
> [   51.938656] Call Trace:
> [   51.938669]  <TASK>
> [   51.938669]  dump_stack_lvl+0x51/0x70
> [   51.938674]  bad_page+0x6c/0x100
> [   51.938678]  free_tail_page_prepare+0x65/0x240
> [   51.938679]  __free_frozen_pages+0x543/0x6e0
> [   51.938682]  update_and_free_pages_bulk+0x1c8/0x240
> [   51.938686]  set_max_huge_pages+0x2e3/0x3d0
> [   51.938688]  hugetlb_sysctl_handler_common+0xc4/0xf0
> [   51.938692]  proc_sys_call_handler+0x183/0x280
> [   51.938696]  vfs_write+0x2e1/0x3f0
> [   51.938700]  ksys_write+0x52/0xd0
> [   51.938702]  do_syscall_64+0x6c/0x1230
> [   51.938725] RIP: 0033:0x7fb0d7b14a87
> 
> (Module list and remaining frames trimmed.)  The pfn:0x381040 page is
> entry 0 of the second vmemmap page of the hugepage and carries the head's
> flags and page_type; the 0x200 in the last word of the last "head:" line
> is _nr_pages in page 1.
> 
> This can lead to an oops later when the pages are allocated. In every
> oops the faulting folio was the second struct page (index 1) of
> a 64-entry vmemmap page.
> 
> Reverting these two commits on v6.18.66 fixes it:
> 
>   git revert --no-commit d73f79d1a2c2 daeffb841bb9
> 
>  daeffb841bb9 mm/hugetlb: refactor code around vmemmap_walk
>               (upstream c0b495b91a47) dropped reset_struct_pages(), and
>  d73f79d1a2c2 mm/hugetlb_vmemmap: fix incorrect vmemmap restore in
>               rollback (upstream c7bde43f6daf) then made
>               vmemmap_restore_pte() copy_page() whatever page the PTE
>               currently maps.
> 
> Happy to test patches.

Hi,

Does this [1] work?

[1] https://lore.kernel.org/all/20261130035130.2441486-1-songmuchun@bytedance.com/

Thanks.

> 
> Thanks,
> 
> -Jason
> 
> 
> ------------------------------------------------------------------------
> hvo-restore-repro.c
> ------------------------------------------------------------------------
> // SPDX-License-Identifier: GPL-2.0
> /*
> * hvo-restore-repro.c - HugeTLB vmemmap optimization (HVO) restore leaves
> * stale struct pages behind when hugepages are freed.
> *
> * Affected: stable 6.18.y from v6.18.51 (HVO refactor c0b495b91a47 and its
> * "rollback" fix c7bde43f6daf are present without 622026e87c40, "mm/hugetlb:
> * remove fake head pages").  There the shared vmemmap tail page is a copy of
> * the head's vmemmap page, and vmemmap_restore_pte() copies it into every
> * restored vmemmap page without fixing up its first four struct pages.
> * Entry 1 of each restored page keeps _nr_pages (512 for 2 MB), which
> * overlays memcg_data, so the page returns to the buddy allocator looking
> * charged to a memcg at address 0x200.
> *
> * What this does: enable HVO, grow the hugepage pool, then shrink it to 0 so
> * the optimized hugepages are freed through the vmemmap restore path.
> *
> * How to see it: boot with init_on_free=1 (or CONFIG_DEBUG_VM=y) so page
> * sanity checks are enabled.  The shrink then logs
> *   BUG: Bad page state ... corrupted mapping in tail page
> *   page does not match folio ... memcg:200
> * from free_tail_page_prepare().  Without those checks the stale value is not
> * reported; a later user of such a page (pfn % 64 == 1) oopses in
> * __lruvec_stat_mod_folio() or uncharge_folio() with memcg == 0x200.
> *
> * Build: gcc -O2 -Wall -o hvo-restore-repro hvo-restore-repro.c
> * Run (root):  ./hvo-restore-repro [nr_hugepages, default 512]
> * Exit: 0 no splat seen, 1 bug reproduced, 2 setup error.
> */
> #define _GNU_SOURCE
> #include <errno.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <sys/klog.h>
> #include <unistd.h>
> 
> #define NR_HUGEPAGES "/proc/sys/vm/nr_hugepages"
> #define HVO_SYSCTL "/proc/sys/vm/hugetlb_optimize_vmemmap"
> 
> static long read_long(const char *path)
> {
> FILE *f = fopen(path, "r");
> long v = -1;
> 
> if (!f)
> return -1;
> if (fscanf(f, "%ld", &v) != 1)
> v = -1;
> fclose(f);
> return v;
> }
> 
> static int write_long(const char *path, long v)
> {
> FILE *f = fopen(path, "w");
> 
> if (!f)
> return -1;
> fprintf(f, "%ld\n", v);
> return fclose(f); /* reports write errors, e.g. EINVAL */
> }
> 
> /* Count occurrences of @needle in the kernel log ring buffer. */
> static long count_klog(const char *needle)
> {
> int size = klogctl(10 /* SYSLOG_ACTION_SIZE_BUFFER */, NULL, 0);
> char *buf, *p;
> long n = 0;
> 
> if (size <= 0)
> return -1;
> buf = calloc(1, size + 1);
> if (!buf)
> return -1;
> if (klogctl(3 /* SYSLOG_ACTION_READ_ALL */, buf, size) < 0) {
> free(buf);
> return -1;
> }
> for (p = buf; (p = strstr(p, needle)); p += strlen(needle))
> n++;
> free(buf);
> return n;
> }
> 
> int main(int argc, char **argv)
> {
> long want = argc > 1 ? atol(argv[1]) : 512;
> long hvo, got, before, after;
> 
> if (geteuid()) {
> fprintf(stderr, "run as root\n");
> return 2;
> }
> 
> hvo = read_long(HVO_SYSCTL);
> if (hvo < 0) {
> fprintf(stderr, "%s missing: kernel has no HVO\n", HVO_SYSCTL);
> return 2;
> }
> if (hvo != 1 && write_long(HVO_SYSCTL, 1)) {
> perror("enable " HVO_SYSCTL);
> return 2;
> }
> 
> before = count_klog("Bad page state");
> if (before < 0) {
> perror("klogctl");
> return 2;
> }
> 
> /* New pool pages are HVO-optimized as they are added to the pool. */
> if (write_long(NR_HUGEPAGES, want)) {
> perror("grow " NR_HUGEPAGES);
> return 2;
> }
> got = read_long(NR_HUGEPAGES);
> printf("hugepage pool grown to %ld (requested %ld)\n", got, want);
> if (got < 1) {
> fprintf(stderr, "could not allocate any hugepages\n");
> return 2;
> }
> 
> /* Freeing optimized hugepages restores their vmemmap: the buggy path. */
> if (write_long(NR_HUGEPAGES, 0)) {
> perror("shrink " NR_HUGEPAGES);
> return 2;
> }
> sleep(2); /* let any deferred frees run */
> 
> after = count_klog("Bad page state");
> if (after > before) {
> printf("BUG REPRODUCED: %ld new \"Bad page state\" report(s)\n",
>       after - before);
> return 1;
> }
> printf("no bad-page report. Either the kernel is fixed, or page checks\n"
>       "are off (boot with init_on_free=1 or CONFIG_DEBUG_VM=y).\n");
> return 0;
> }



  reply	other threads:[~2026-12-02  2:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 18:09 [REGRESSION] 6.18.y: HugeTLB vmemmap restore leaves stale struct pages since v6.18.51 Jason Baron
2026-12-02  2:21 ` Muchun Song [this message]
2026-12-02  3:00   ` Muchun Song

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=67F3ADCC-2F22-4447-9613-470F369B0FA7@linux.dev \
    --to=muchun.song@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jbaron@akamai.com \
    --cc=kas@kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=osalvador@suse.de \
    --cc=regressions@lists.linux.dev \
    --cc=sashal@kernel.org \
    --cc=songmuchun@bytedance.com \
    --cc=stable@vger.kernel.org \
    /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