6.18.y: HugeTLB vmemmap restore leaves stale struct pages since v6.18.51
Subsystems (3)
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.
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;
}Quoted 103 lines · Jason Baron
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 havereset_struct_pages()and are not affected.
- x86-64 withCONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP=yand HVO enabled (CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON=yor 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()inmm/hugetlb_vmemmap.crestores 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_headinhugetlb_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 offset0x38, which overlaysmemcg_dataof an ordinary page.Up to v6.18.50 the restore did
copy_page()followed byreset_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_headand a hugetlbpage_typeat entry 0, and_nr_pagesat +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 address0x200, 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, orCONFIG_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; the0x200in the last word of the last "head:" line is_nr_pagesin 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
Quoted 8 lines
daeffb841bb9mm/hugetlb: refactor code aroundvmemmap_walk(upstream c0b495b91a47) dropped reset_struct_pages(), and
d73f79d1a2c2mm/hugetlb_vmemmap: fix incorrect vmemmap restore in
rollback (upstreamc7bde43f6daf) then madevmemmap_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/[email protected]/
Thanks.
Quoted 149 lines
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 refactorc0b495b91a47and its
* "rollback" fixc7bde43f6dafare present without622026e87c40, "mm/hugetlb:
* remove fake head pages"). There the shared vmemmap tail page is a copy of
* the head's vmemmap page, andvmemmap_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
* overlaysmemcg_data, so the page returns to the buddy allocator looking
* charged to a memcg at address0x200.
*
* 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 (orCONFIG_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
* fromfree_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()oruncharge_folio()with memcg ==0x200. *
* Build: gcc -O2 -Wall -o hvo-restore-reprohvo-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" #defineHVO_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;
}
Quoted 105 lines · Muchun Song
Quoted 102 lines · Jason Baron
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 havereset_struct_pages()and are not affected.
- x86-64 withCONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP=yand HVO enabled (CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON=yor 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()inmm/hugetlb_vmemmap.crestores 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_headinhugetlb_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 offset0x38, which overlaysmemcg_dataof an ordinary page.Up to v6.18.50 the restore did
copy_page()followed byreset_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_headand a hugetlbpage_typeat entry 0, and_nr_pagesat +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 address0x200, 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, orCONFIG_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; the0x200in the last word of the last "head:" line is_nr_pagesin 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:
Quoted 8 lines
Quoted 8 lines
git revert --no-commit
d73f79d1a2c2daeffb841bb9
daeffb841bb9mm/hugetlb: refactor code aroundvmemmap_walk(upstream c0b495b91a47) dropped reset_struct_pages(), and
d73f79d1a2c2mm/hugetlb_vmemmap: fix incorrect vmemmap restore in
rollback (upstreamc7bde43f6daf) then madevmemmap_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—especially in the case of a single patch within a series, which is very prone to problems.
Quoted 8 lines
Quoted 2 lines
Happy to test patches.
Hi,
Does this [1] work?
Therefore, I'd rather revert these two commits than merge this fix.
Quoted 2 lines
Thanks.
Sasha Levin
maintainerQuoted 3 lines
Reverting these two commits on v6.18.66 fixes it:
git revert --no-commit d73f79d1a2c2 daeffb841bb9
The revert of the c7bde43f6daf ("mm/hugetlb_vmemmap: fix incorrect vmemmap restore in rollback") backport is already queued for 6.18 and 6.12. Could you test 6.18 with only that revert, keeping the c0b495b91a47 ("mm/hugetlb:
refactor code around vmemmap_walk") backport?
Once you confirm, Muchun's adapted fix <[email protected]> will be queued for 6.18 and 6.12.
Signature · 2 lines
Thanks, Sasha
Quoted 9 lines · Sasha Levin
!-------------------------------------------------------------------|
This Message Is From an External Sender
This message came from outside your organization.
|-------------------------------------------------------------------!Quoted 3 lines
Reverting these two commits on v6.18.66 fixes it:
git revert --no-commit d73f79d1a2c2 daeffb841bb9
Quoted 9 lines
The revert of the
c7bde43f6daf("mm/hugetlb_vmemmap: fix incorrect vmemmap restore in rollback") backport is already queued for 6.18 and 6.12. Could you test 6.18 with only that revert, keeping thec0b495b91a47("mm/hugetlb:
refactor code aroundvmemmap_walk") backport?Once you confirm, Muchun's adapted fix <[email protected]> will be queued for 6.18 and 6.12.
Hi,
I've tested v6.18.66 with only d73f79d1a2c2 reverted (git revert d73f79d1a2c2), x86-64, CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP=y with HVO enabled, booted with init_on_free=1 so the page sanity checks are on.
The reproducer from my first mail now exits 0 and dmesg has no "Bad page state" reports. Unmodified v6.18.66 reports on the first run.
I only exercised the normal restore path (shrinking nr_hugepages). I
did not test the vmemmap_remap_free() rollback path that d73f79d1a2c2 was written to fix.
Thanks,
-Jason
Sasha Levin
maintainerQuoted 6 lines
I've tested v6.18.66 with only
d73f79d1a2c2reverted (git revertd73f79d1a2c2), x86-64,CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP=ywith HVO enabled, booted with init_on_free=1 so the page sanity checks are on.The reproducer from my first mail now exits 0 and dmesg has no "Bad page state" reports. Unmodified v6.18.66 reports on the first run.
Thanks for testing, Jason. The revert is queued for the next 6.18 and 6.12 releases.
Muchun, given that the revert alone fixes this, do you still prefer reverting the 6.18 and 6.12 backports of c0b495b91a47 ("mm/hugetlb:
refactor code around vmemmap_walk") as well? If so, could you send the fix that b1b7c045e808 ("mm/hugetlb: initialize gigantic bootmem hugepage struct pages earlier") then needs? Otherwise I'll queue your adapted c7bde43f6daf ("mm/hugetlb_vmemmap: fix incorrect vmemmap restore in rollback") on top of the revert.
Signature · 2 lines
Thanks, Sasha