From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.119 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796178089; b=2RregE0/BXCJAPviH8UjELCO1jRM9Spcmjkk+vstiUhaeXA8EpTWg/cGKpGwt3wiJjHf n68/pCQhOxhsvORGJBohlZrzrubAY9lmapqiTJd5kTOpSxLBPaVKx8480A4NSlCUJfQJb Fwh5rrioDsgp0GmWcUk0MDe3+FB1bIJdvPrKo9t2x9Xw/TipEiX7o3kP8iUzqXAqiQslt etJuvHgewUn5YS6DbJC+lT0bGFZ028PcGS/+XDUgLmgWZNIcxqzsAQW4oBC7W/gmyd41G V6Mk/6xuEhyn4b7xqsPIS5qwehPtz1qiGzPcJMB9UuSu/8eRhGt18+qnaCbYfowu5CA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796178089; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Content-Type:Mime-Version:Subject:From: In-Reply-To:Date:Cc:Content-Transfer-Encoding:Message-Id:References: To:X-Mailer; bh=e4oXZJ4rL5pDUr98NtWl/WmftI3PswvowBibvthrjwE=; b=vCc0rbh2yPgio4X05bAXB43RkzuWYjlO54HdJurCJ9jrZEhQVqANCNKmxlKmVTQN4r2A vgqBL4yaHs688Lryv2z3ivv09euS+j2MsevTTvCH5eYmjJHtEMc9Dk1Ny0TputM5H/utB 5V3iaeBSm0quwCtlnLdJwgRUaetIXX7XboDXF4r1nEIgVpQtxLhkk03xBMSZO/lfsm1Gy RRDzAuOtFGp8ZrPs590zLq3DbvDIau/b77J66bDcMqp6W9B/cMqib86h4oMiInQgla4Td OHhlR7xraj68b/cifZLhEMxzuH3M1hyz7OBvmmLBEXhtBilmJapS3rNp6LFgVQzvLVA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=jRCa35vC; arc=none smtp.remote-ip=91.218.175.119 Received: from mta0.migadu.com (out-119.mta0.migadu.com [91.218.175.119]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E0ECA606A699 for ; Wed, 2 Dec 2026 02:21:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E0ECA606A699 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev 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=jRCa35vC X-Envelope-To: stable@vger.kernel.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=1796178087; v=1; x=1796782887; b=jRCa35vC0FVFyVDevGIrh5kZ1RBbQggMW1WroItPE/muuHeta8XdIbQaOkF2hVAWkb7uPf8/ NsY3q7w4YWMoGAvpEHUxmcO1NuqwGNMISXrX7SMSTkYFf953L23LkC7KYd9ib6eUQDPozQrnzVa 3XjWOs19iitQW10KgRdlYtDY= X-Envelope-To: stable@vger.kernel.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-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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) > 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; > }