From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C34AD606A68F for ; Wed, 2 Dec 2026 02:21:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=izdZGa2l Received: by kanga.kvack.org (Postfix) id E735B6B05BE; Tue, 1 Dec 2026 21:21:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E4A646B05C8; Tue, 1 Dec 2026 21:21:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D39E66B05CD; Tue, 1 Dec 2026 21:21:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A767E6B05BE for ; Tue, 1 Dec 2026 21:21:31 -0500 (EST) Received: from smtpin10.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay02.hostedemail.com (Postfix) with ESMTP id BA6351201A3 for ; Wed, 2 Dec 2026 02:21:30 +0000 (UTC) X-FDA: 85497441540.10.722BF7F Received: from mta1.migadu.com (out-214.mta1.migadu.com [95.215.58.214]) by imf03.hostedemail.com (Postfix) with ESMTP id 5A70420009 for ; Wed, 2 Dec 2026 02:21:28 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=izdZGa2l; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf03.hostedemail.com: domain of muchun.song@linux.dev designates 95.215.58.214 as permitted sender) smtp.mailfrom=muchun.song@linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796178089; b=420/QTra3fcn44Oeo+N4gY6kV1eoxjRj1/VRqJpFcgXBdnt72cdwr5sUWp/S5oWglcLh0o ej+8J7K8M37ldX2oO+lQ45ODJEoSnwxFXXX8r6oAHYDp6wz7RLVSJm1hllfUXJT25mrpBu Ujdgp/NGJQqGveKQT2jVPyPpdYFkm7A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796178089; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=e4oXZJ4rL5pDUr98NtWl/WmftI3PswvowBibvthrjwE=; b=L7BtyoP3zOf3+0VE3NR17gAyZO2b0x8eCXModr3s8MpY0CZoquOOSuR1QvNI0RLjXlZnP5 GIayD6RQTjvt5g0l7OJgCyHp3vpqoM7oBkkKPgnE0nCGviyI6x7suIYZfg0yQQMve+wKa7 t04tkqqyXXbD0ICs4U7ssJH/x7xhT7A= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=izdZGa2l; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf03.hostedemail.com: domain of muchun.song@linux.dev designates 95.215.58.214 as permitted sender) smtp.mailfrom=muchun.song@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=35JOin9y9KnwdO+1pjFdUfLGrXBgOOIUaa2l1A2rUtw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796178086; v=1; x=1796782886; b=izdZGa2l7O67sKatVa/nYS/J0Foo3WhUF/vyO/A5ABan7i2K9NNAGqKo+uzdJLYavQ2b/yct WzDtax6s9W0ADM8jJg4Mp5JUqHUyjPDY2mEUj3aOaI2W4cUBvvVITG/Q2t+ZLo7t0gwpV+K2T4I Ig40FpAi5w4Vn9LvcaGF22T8= X-Envelope-To: linux-mm@kvack.org Received: by smtp.migadu.com with ESMTPS id 8077e24bce5f03f2; Wed, 02 Dec 2026 02:21:26 +0000 X-Mizu-Trace-ID: 8077e24bce5f03f2 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset=us-ascii X-Archived-By: Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3901.200.71.1.16\)) Subject: Re: [REGRESSION] 6.18.y: HugeTLB vmemmap restore leaves stale struct pages since v6.18.51 From: Muchun Song In-Reply-To: <20261201180905.2170289-1-jbaron@akamai.com> Date: Wed, 2 Dec 2026 10:21:06 +0800 Cc: Muchun Song , stable@vger.kernel.org, regressions@lists.linux.dev, Greg Kroah-Hartman , Sasha Levin , Kiryl Shutsemau , Muchun Song , Oscar Salvador , David Hildenbrand , Andrew Morton , linux-mm@kvack.org Content-Transfer-Encoding: quoted-printable Message-Id: <67F3ADCC-2F22-4447-9613-470F369B0FA7@linux.dev> References: <20261201180905.2170289-1-jbaron@akamai.com> To: Jason Baron X-Mailer: Apple Mail (2.3901.200.71.1.16) X-Stat-Signature: cdatycsongsobpn7wz7gsx6j1bao6jfj X-Rspamd-Queue-Id: 5A70420009 X-Rspam-User: X-Rspamd-Server: rspam05 X-Spam-Status: No, score=-5.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-HE-Tag: 1796178088-513119 X-HE-Meta: U2FsdGVkX18wwb1kthAnSFuuMVIPCMYCSA9XjiFyR95xaH7dfaKzXwpLf2l23qUvNhxNmghHa6kvQgnBgwMa61viHtp1DBHbnRsv9C2ZAmLmrFks0ScOZTomw5nsqq85iHzPJRGZlBN5/9+uUOGC4QruEuPyZGxjfqyE3qqHnxSvvjsk0FKgD4/jNJ0xbFNEWHoOlIzbeYO6FvfONPlRkElhsJiUnad8xhzcjQIeqhsmTgdOrAZLkdVn0LAeDtjlB8cHVPQ4NHZ1C69Kshlb3NSLwIsZry7OIxdgUxhTOb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8bRF4qKlzlu6P46RR30XL9PIFJVmBEr2Br1AdkptaB9i6Rwx20Qnz8jnIHbiyjjH3Kc9yBdL+Efe4PuYc6r1BFM8GCD+YZWGsR9RQkYpPEkXnh4umuc4GDI5EdbIZA595Q7w7ZSvF6B3WyR5zifH5hkVElhvUz/8e4= Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 > On Dec 2, 2026, at 02:09, Jason Baron wrote: >=20 > Hi, >=20 > 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. >=20 > #regzbot introduced: v6.18.50..v6.18.51 >=20 > 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=3Dy and HVO enabled > (CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON=3Dy or > hugetlb_free_vmemmap=3Don). hugetlb_free_vmemmap=3Doff 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. >=20 > 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 =3D 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. >=20 > 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.=20 >=20 > 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. >=20 > 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 =3D=3D 1 sees memcg_data =3D=3D 0x200, i.e. a page = charged to > a memcg at address 0x200, and oopses in the memcg code. >=20 > 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=3D1, or CONFIG_DEBUG_VM=3Dy). On v6.18.66 it = logs > "Bad page state" reports on the first run; it exits 1 when it sees = them. >=20 > Splat on vanilla v6.18.66 (QEMU VM, init_on_free=3D1, not tainted): >=20 > [ 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=3D0|zone=3D2|lastcpupid=3D0x3ffff) > [ 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] > [ 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 >=20 > (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. >=20 > 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. >=20 > Reverting these two commits on v6.18.66 fixes it: >=20 > git revert --no-commit d73f79d1a2c2 daeffb841bb9 >=20 > 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. >=20 > Happy to test patches. Hi, Does this [1] work? [1] = https://lore.kernel.org/all/20261130035130.2441486-1-songmuchun@bytedance.= com/ Thanks. >=20 > Thanks, >=20 > -Jason >=20 >=20 > = ------------------------------------------------------------------------ > 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=3D1 (or CONFIG_DEBUG_VM=3Dy) = 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 =3D=3D 1) oopses in > * __lruvec_stat_mod_folio() or uncharge_folio() with memcg =3D=3D = 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 > #include > #include > #include > #include > #include >=20 > #define NR_HUGEPAGES "/proc/sys/vm/nr_hugepages" > #define HVO_SYSCTL "/proc/sys/vm/hugetlb_optimize_vmemmap" >=20 > static long read_long(const char *path) > { > FILE *f =3D fopen(path, "r"); > long v =3D -1; >=20 > if (!f) > return -1; > if (fscanf(f, "%ld", &v) !=3D 1) > v =3D -1; > fclose(f); > return v; > } >=20 > static int write_long(const char *path, long v) > { > FILE *f =3D fopen(path, "w"); >=20 > if (!f) > return -1; > fprintf(f, "%ld\n", v); > return fclose(f); /* reports write errors, e.g. EINVAL */ > } >=20 > /* Count occurrences of @needle in the kernel log ring buffer. */ > static long count_klog(const char *needle) > { > int size =3D klogctl(10 /* SYSLOG_ACTION_SIZE_BUFFER */, NULL, 0); > char *buf, *p; > long n =3D 0; >=20 > if (size <=3D 0) > return -1; > buf =3D calloc(1, size + 1); > if (!buf) > return -1; > if (klogctl(3 /* SYSLOG_ACTION_READ_ALL */, buf, size) < 0) { > free(buf); > return -1; > } > for (p =3D buf; (p =3D strstr(p, needle)); p +=3D strlen(needle)) > n++; > free(buf); > return n; > } >=20 > int main(int argc, char **argv) > { > long want =3D argc > 1 ? atol(argv[1]) : 512; > long hvo, got, before, after; >=20 > if (geteuid()) { > fprintf(stderr, "run as root\n"); > return 2; > } >=20 > hvo =3D read_long(HVO_SYSCTL); > if (hvo < 0) { > fprintf(stderr, "%s missing: kernel has no HVO\n", HVO_SYSCTL); > return 2; > } > if (hvo !=3D 1 && write_long(HVO_SYSCTL, 1)) { > perror("enable " HVO_SYSCTL); > return 2; > } >=20 > before =3D count_klog("Bad page state"); > if (before < 0) { > perror("klogctl"); > return 2; > } >=20 > /* 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 =3D 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; > } >=20 > /* 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 */ >=20 > after =3D 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=3D1 or CONFIG_DEBUG_VM=3Dy).\n");= > return 0; > }