Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.209 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796180449; b=HkfsridN6D2EXi4l6KB8yPbcUR1/FfontYvidEinUsFGEzpyjaWo+PMIN+271xMN00nz YAMRF5Ja+fUjGyhci9YczcdRLMV0kmdgxo8WZdBSC+CPqFRF99k1P1To8agdFLsw/d3g0 8rI0r6VTgjKQ5awUUgDuaV6kPK/oSDP/1tYiLiQ5w/gXEgLpf/5kev0hvnugNPhdWbhAE fxXWy8oFbp48+1MNrDiTWgTh8Zv3T+zurpK6YTTDrdSXbJETEgpq/0v3+fmrtY5vVlWFR x28nGhmwDW4TuewngAq7CgIAXquscGazRluFvKb/2RSwOszcAK0MoxLHnTH8fMOU72w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796180449; 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=AX3fsHuSZjKv1fDPvtTqJzdW1dAT4KMLVoHaikXC6VA=; b=Q8IZLPkjd0/7FnL3t7qeWkOgfTdBsJw/YaJByE7GJBrKv9dPV6YczDoiluWTD6l+5JlE 3OEusue1rfDvM1n1PQgB+hp8hMIkboFA7T96qFAZYTapwmZokFqQWIsMeGMfha9jebgTs GaZf3r+CAOMnQSuIJJVZdv3m9Lmx6lyCvzlrWQV4GIwMhGj26d3VTxZLlg2Nb/5PMA0EF Req3re+6Q4p3YrBEEIG/UM1A23sLjv97eRvw445/3WrUqH9Nj17lcNgUsgdaujnsZQwY1 vEHRZCm2fkfhjuxQxhlKEUjAIx1dM3+weDscV2raNZzLohjHUNx/yTcMDTaJ2WOWgVQ== 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=OXEp2jlC; arc=none smtp.remote-ip=91.218.175.209 Received: from mta0.migadu.com (out-209.mta0.migadu.com [91.218.175.209]) (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 A48B9606A31A for ; Wed, 2 Dec 2026 03:00:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A48B9606A31A 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=OXEp2jlC X-Envelope-To: stable@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=8vUvrypcJTvOuO11ANQrYPTs79r45wV5vFRsavkubh0=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796180446; v=1; x=1796785246; b=OXEp2jlCvAAWRNf0GSFpYVnRX1gjxk4k8Ag0pax0WVmMrT5g35x7JuaiTnKDB+ssc/oI01MC dZ1/iUc8XPGxG49NxXAdQFgLnWE6Z3z4EpJkdsT71Rngeor2v4Wps8CJhlhulxTWpexC81W0KzA rlc2WRrpcHgZkI6PdlNSN8JM= X-Envelope-To: stable@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 04c11007b8557aee; Wed, 02 Dec 2026 03:00:25 +0000 X-Mizu-Trace-ID: 04c11007b8557aee X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset=utf-8 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: <67F3ADCC-2F22-4447-9613-470F369B0FA7@linux.dev> Date: Wed, 2 Dec 2026 11:00:12 +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: References: <20261201180905.2170289-1-jbaron@akamai.com> <67F3ADCC-2F22-4447-9613-470F369B0FA7@linux.dev> To: Jason Baron X-Mailer: Apple Mail (2.3901.200.71.1.16) > On Dec 2, 2026, at 10:21, Muchun Song wrote: >=20 >=20 >=20 >> 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. Of course. I think reworking those two commits is a good option as well. I found that daeffb841bb9 was backported because b1b7c045e808 depends on it. The fix for b1b7c045e808 should be relatively simple, so it's not worth us making such a large change=E2=80=94especially in the case of a = single patch within a series, which is very prone to problems. >>=20 >> Happy to test patches. >=20 > Hi, >=20 > Does this [1] work? >=20 > [1] = https://lore.kernel.org/all/20261130035130.2441486-1-songmuchun@bytedance.= com/ Therefore, I'd rather revert these two commits than merge this fix. >=20 > Thanks.