Public bug reported:

[Summary]
On linux 6.8.0-151.151 (noble-updates / noble-security), with HugeTLB vmemmap 
optimization enabled
(hugetlb_free_vmemmap=on), shrinking a hugepage pool at runtime corrupts the 
freed pages' struct pages:

* 2 MiB pages: every freed hugepage is reported as "BUG: Bad page state ... 
corrupted mapping in tail page" and
  the memory is NOT returned. Freeing 1024 x 2 MiB pages leaves MemFree 
unchanged (2 GiB lost until reboot).
* 1 GiB pages: one 4 KiB page in every 64 is reported as "BUG: Bad page state 
... nonzero mapcount"
  (page_type hugetlb) and leaked. About 1/64 of the memory is lost, and, worse, 
the freed range can never form a
  free block above order 5 (256 KiB) again, so THP and any other high-order 
allocation in that range fails for
  good. Compaction does not help. Only a reboot clears it.

6.8.0-149.149 does not have the problem. 6.8.0-151 with 
hugetlb_free_vmemmap=off does not have it either.
6.8.0-150.150 was not tested, but it is the version whose changelog adds the 
patches named below.

[Reproducer]
Any machine or VM; no workload needed. Shown here in a QEMU guest (8 GiB, 2 
vCPU, -kernel/-initrd with a busybox
init; the two scripts are attached: hvo-2m.sh for 2 MiB pages, hvo-test.sh for 
1 GiB pages):

  kernel command line: hugetlb_free_vmemmap=on hugepages=1024
  echo 0 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages

or, for 1 GiB pages:

  kernel command line: hugetlb_free_vmemmap=on default_hugepagesz=1G 
hugepagesz=1G hugepages=2
  echo 0 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages

Then read dmesg, /proc/sys/kernel/tainted, /proc/meminfo and
/proc/buddyinfo.

[Results]
                                      6.8.0-149 hvo=on    6.8.0-151 hvo=off    
6.8.0-151 hvo=on
2 MiB x1024 freed: MemFree before     5987084 kB          (similar)            
5986812 kB
                   MemFree after      7913472 kB          8061712 kB           
5954808 kB   <- nothing came back
                   Bad page reports   0                   0                    
60 (rate limit), tainted 32
1 GiB x2 freed:    Bad page reports   0                   0                    
60 (rate limit), tainted 32
   buddyinfo Normal after (order 0..10):
     6.8.0-149 on :    22    14    10     4     3     1     3     2     2     5 
 1218
     6.8.0-151 off:    23    15     6     3     2     4     1     4     1     3 
 1229
     6.8.0-151 on :  7944  7942  7941  7941  7936  7936     8     9     6     6 
  705

First report, 1 GiB case (they repeat at pfn +0x40, +0x80, ...):

  BUG: Bad page state in process init  pfn:180040
  page:... refcount:0 mapcount:1 mapping:0000000000000000 index:0x0 pfn:0x180040
  head:... order:16 entire_mapcount:1 nr_pages_mapped:0 pincount:0
  flags: 0x17ffffc0000040(head|node=0|zone=2|lastcpupid=0x1fffff)
  page_type: 0xfffff7ff(hugetlb)
  page dumped because: nonzero mapcount
  CPU: 0 PID: 1 Comm: init Not tainted 6.8.0-151-generic #151-Ubuntu
  Call Trace:
   bad_page+0x76/0x120
   free_page_is_bad_report+0x86/0xa0
   free_unref_page_prepare+0x2d9/0x4c0
   free_unref_page+0x34/0x1c0
   __free_pages+0x123/0x140
   free_contig_range+0x52/0xc0
   __update_and_free_hugetlb_folio+0x8f/0x140
   update_and_free_hugetlb_folio+0x3f/0x80
   update_and_free_pages_bulk+0x82/0x110
   set_max_huge_pages+0x3ad/0x5c0
   __nr_hugepages_store_common+0x5b/0xf0
   nr_hugepages_store+0x92/0xa0

2 MiB case: "page dumped because: corrupted mapping in tail page", from
free_tail_page_prepare().

[Likely cause]
The 6.8.0-150.150 changelog lists, under "Noble update: upstream stable 
patchset 2026-11-03" (LP: #2172887):

  - mm/hugetlb: refactor code around vmemmap_walk
  - mm/hugetlb_vmemmap: fix incorrect vmemmap restore in rollback
  - mm/hugetlb: initialize gigantic bootmem hugepage struct pages earlier

In mm/hugetlb_vmemmap.c of 6.8.0-151, vmemmap_restore_pte() now does a plain 
copy_page() from the page the PTE
currently maps, and reset_struct_pages() is gone. But 
hugetlb_vmemmap_optimize_folio() sets

  vmemmap_tail = vmemmap_head;      /* a copy of the folio's first
vmemmap page */

so the page every tail PTE maps starts with the folio's HEAD struct page. On 
restore, each re-allocated vmemmap
page therefore begins with a copy of the head struct page (PG_head, hugetlb 
page_type, mapcount) and of the first
tail pages' special fields, which is exactly what the reports show: one bad 
struct page per 4 KiB of vmemmap, i.e.
every 64th pfn. Upstream this code restores from a dedicated shared tail page 
that 6.8 does not have; the old
6.8 code relied on reset_struct_pages() to scrub the first three struct pages 
after the copy. So the refactor
looks incomplete as backported to 6.8. (Read from the source diff, not 
bisected.)

The same patches came from v6.6.160 / v6.12.114 per LP: #2172887; those
trees were not tested here.

[Impact]
Any Noble host that uses hugetlb_free_vmemmap=on and ever shrinks a hugepage 
pool (nr_hugepages lowered, pages
freed after a VM stops with a dynamic pool, boot-time reservation released) 
silently loses memory or loses all
large contiguous free memory until reboot. We met it on KVM hosts that reserved 
1 GiB pages at boot and released
them later: guest memory backed by shmem THP fell from 60-99 % huge-page backed 
to 3-4 %, thp_file_fallback climbed
continuously, compaction stalled without success, and VM workloads ran about 10 
% slower until the hosts were
rebooted with hugepages=0.

[Workaround]
Do not free HugeTLB pages at runtime on this kernel with 
hugetlb_free_vmemmap=on: either boot without the option,
or set the final pool size on the kernel command line. A reboot clears an 
affected machine.

[Tested]
linux-image-6.8.0-149-generic 6.8.0-149.149, linux-image-6.8.0-151-generic 
6.8.0-151.151, amd64,
QEMU 8.2.2 (Ubuntu 24.04.5 host), plus bare-metal AMD hosts on 6.8.0-151 (1 GiB 
case).

Kernel: linux-image-6.8.0-151-generic 6.8.0-151.151 from the Ubuntu
archive (kernel log: "Linux version 6.8.0-151-generic
(buildd@lcy02-amd64-009) ... #151-Ubuntu SMP PREEMPT"). No hardware is
involved (QEMU guest, no devices), so lspci and apport data are not
relevant; the full guest console output of each run is attached instead.

[Attachments]
hvo-2m.sh, hvo-test.sh: the reproducer scripts (boot a throwaway VM from the 
unpacked linux-image .deb)
2M-6.8.0-149-hvo-on.txt, 2M-6.8.0-151-hvo-off.txt, 2M-6.8.0-151-hvo-on.txt: 2 
MiB runs
1G-6.8.0-149-hvo-on.txt, 1G-6.8.0-151-hvo-off.txt, 1G-6.8.0-151-hvo-on.txt: 1 
GiB runs

** Affects: linux (Ubuntu)
     Importance: Undecided
         Status: New

-- 
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/2179422

Title:
  Noble 6.8.0-151 (since -150?): freeing HugeTLB pages with
  hugetlb_free_vmemmap=on gives "BUG: Bad page state" and leaks memory

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/linux/+bug/2179422/+subscriptions


-- 
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to