Skip to contentSkip to conversation

Crash on x86_32 for: mm: page_alloc: avoid merging non-fallbackable pageblocks with others

BUG Steven Rostedt 4 participants 13 messages Mar 30, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

I started testing new patches and it crashed when doing the x86-32 test on boot up.

Initializing HighMem for node 0 (000375fe:0021ee00)

BUG: kernel NULL pointer dereference, address: 00000878
#PF: supervisor read access in kernel mode
#PF: error_code(0x0000) - not-present page
*pdpt = 0000000000000000 *pde = f0000000f000eef3 
Oops: 0000 [#1] PREEMPT SMP PTI
CPU: 0 PID: 0 Comm: swapper Not tainted 5.17.0-test+ #469
Hardware name: MSI MS-7823/CSM-H87M-G43 (MS-7823), BIOS V1.6 02/22/2014
EIP: get_pfnblock_flags_mask+0x2c/0x36
Code: 6d ea ff 55 89 e5 56 89 ce 53 8b 18 89 d8 c1 eb 1e e8 f7 fb ff ff 69 db c0 02 00 00 89 c1 89 c2 c1 ea 05 8b 83 7c d7 79 c1 5b <8b> 04 90 d3 e8 21 f0 5e 5d c3 55 89 e5 57 56 89 d6 53 89 c3 64 a1
EAX: 00000000 EBX: f75f6000 ECX: 000043dc EDX: 0000021e
ESI: 00000007 EDI: 00000000 EBP: c15d9e34 ESP: c15d9e30
DS: 007b ES: 007b FS: 00d8 GS: 0000 SS: 0068 EFLAGS: 00210007
CR0: 80050033 CR2: 00000878 CR3: 01a48000 CR4: 000406b0
Call Trace:
 __free_one_page+0x168/0x22a
 free_pcppages_bulk+0xf0/0x1b9
 free_unref_page_commit+0xe4/0xed
 free_unref_page+0x77/0x9b
 free_the_page+0x16/0x18
 __free_pages+0x22/0x51
 add_highpages_with_active_regions+0xbb/0xea
 set_highmem_pages_init+0x69/0x7a
 mem_init+0x2d/0x141
 start_kernel+0x353/0x5f4
 ? set_intr_gate+0x47/0x5a
 ? early_idt_handler_common+0x44/0x44
 i386_start_kernel+0x48/0x4a
 startup_32_smp+0x161/0x164
Modules linked in:
CR2: 0000000000000878
---[ end trace 0000000000000000 ]---

I bisected it down to:

1dd214b8f21ca46d5431be9b2db8513c59e07a26
mm: page_alloc: avoid merging non-fallbackable pageblocks with others

To confirm, I went back to Linus's master branch (from last night), to verify that it crashes. Then reverted this commit, recompiled and it booted up fine without it.

Attached is the config that crashed.

-- Steve

#1 lore raw
Quoted 5 lines · Steven Rostedt

I started testing new patches and it crashed when doing the x86-32 test on boot up.

Initializing HighMem for node 0 (000375fe:0021ee00)

Quoted 9 lines
BUG: kernel NULL pointer dereference, address: 00000878
#PF: supervisor read access in kernel mode
#PF: error_code(0x0000) - not-present page
*pdpt = 0000000000000000 *pde = f0000000f000eef3
Oops: 0000 [#1] PREEMPT SMP PTI
CPU: 0 PID: 0 Comm: swapper Not tainted 5.17.0-test+ #469
Hardware name: MSI MS-7823/CSM-H87M-G43 (MS-7823), BIOS V1.6 02/22/2014
EIP: get_pfnblock_flags_mask+0x2c/0x36
Code: 6d ea ff 55 89 e5 56 89 ce 53 8b 18 89 d8 c1 eb 1e e8 f7 fb ff ff 69 db c0 02 00 00 89 c1 89 c2 c1 ea 05 8b 83 7c d7 79 c1 5b <8b> 04 90 d3 e8 21 f0 5e 5d c3 55 89 e5 57 56 89 d6 53 89 c3 64 a1

The whole function is in that Code: thing, and it decodes to:

0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 56 push %esi
4: 89 ce mov %ecx,%esi
6: 53 push %ebx

   7: 8b 18                mov    (%eax),%ebx
   9: 89 d8                mov    %ebx,%eax
   b: c1 eb 1e              shr    $0x1e,%ebx
   e: e8 f7 fb ff ff        call   0xfffffc0a
  13: 69 db c0 02 00 00    imul   $0x2c0,%ebx,%ebx
  19: 89 c1                mov    %eax,%ecx
  1b: 89 c2                mov    %eax,%edx
  1d: c1 ea 05              shr    $0x5,%edx
  20: 8b 83 7c d7 79 c1    mov    -0x3e862884(%ebx),%eax
  26: 5b                    pop    %ebx
  27:* 8b 04 90              mov    (%eax,%edx,4),%eax <-- trapping instruction
  2a: d3 e8                shr    %cl,%eax
  2c: 21 f0                and    %esi,%eax
  2e: 5e                    pop    %esi
  2f: 5d                    pop    %ebp
  30: c3                    ret

with '%eax' being NULL, and %edx being 0x21e.

(The call seems to be to 'pfn_to_bitidx().isra.0' if my compiler does similar code generation, so it's out-of-lined part of pfn_to_bitidx() despite being marked inline)

So that oops is that

        word = bitmap[word_bitidx];

line, with 'bitmap' being NULL (and %edx contains 'word_bitidx').

Looking around, your 'config-bad' doesn't even have CONFIG_MEMORY_ISOLATION enabled, and so I suspect the culprit is this part of the change:

- if (unlikely(has_isolate_pageblock(zone))) {

which used to always be false for that config, and now the code is suddenly enabled.

Alternatively, that code just can't deal with highmem properly.

But I didn't really analyze things, I'm mainly doing pattern matching here.

Zi Yan - and all the people who ack'ed and reviewed this - please take a deeper look..

Linus

reviewer
#2 lore raw
Quoted 61 lines · Linus Torvalds
Quoted 5 lines · Steven Rostedt

I started testing new patches and it crashed when doing the x86-32 test on boot up.

Initializing HighMem for node 0 (000375fe:0021ee00)

Quoted 9 lines
BUG: kernel NULL pointer dereference, address: 00000878
#PF: supervisor read access in kernel mode
#PF: error_code(0x0000) - not-present page
*pdpt = 0000000000000000 *pde = f0000000f000eef3
Oops: 0000 [#1] PREEMPT SMP PTI
CPU: 0 PID: 0 Comm: swapper Not tainted 5.17.0-test+ #469
Hardware name: MSI MS-7823/CSM-H87M-G43 (MS-7823), BIOS V1.6 02/22/2014
EIP: get_pfnblock_flags_mask+0x2c/0x36
Code: 6d ea ff 55 89 e5 56 89 ce 53 8b 18 89 d8 c1 eb 1e e8 f7 fb ff ff 69 db c0 02 00 00 89 c1 89 c2 c1 ea 05 8b 83 7c d7 79 c1 5b <8b> 04 90 d3 e8 21 f0 5e 5d c3 55 89 e5 57 56 89 d6 53 89 c3 64 a1

The whole function is in that Code: thing, and it decodes to:

0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 56 push %esi
4: 89 ce mov %ecx,%esi
6: 53 push %ebx

   7: 8b 18                mov    (%eax),%ebx
   9: 89 d8                mov    %ebx,%eax
   b: c1 eb 1e              shr    $0x1e,%ebx
   e: e8 f7 fb ff ff        call   0xfffffc0a
  13: 69 db c0 02 00 00    imul   $0x2c0,%ebx,%ebx
  19: 89 c1                mov    %eax,%ecx
  1b: 89 c2                mov    %eax,%edx
  1d: c1 ea 05              shr    $0x5,%edx
  20: 8b 83 7c d7 79 c1    mov    -0x3e862884(%ebx),%eax
  26: 5b                    pop    %ebx
  27:* 8b 04 90              mov    (%eax,%edx,4),%eax <-- trapping instruction
  2a: d3 e8                shr    %cl,%eax
  2c: 21 f0                and    %esi,%eax
  2e: 5e                    pop    %esi
  2f: 5d                    pop    %ebp
  30: c3                    ret

with '%eax' being NULL, and %edx being 0x21e.

(The call seems to be to 'pfn_to_bitidx().isra.0' if my compiler does similar code generation, so it's out-of-lined part of pfn_to_bitidx() despite being marked inline)

So that oops is that

        word = bitmap[word_bitidx];

line, with 'bitmap' being NULL (and %edx contains 'word_bitidx').

Looking around, your 'config-bad' doesn't even have CONFIG_MEMORY_ISOLATION enabled, and so I suspect the culprit is this part of the change:

- if (unlikely(has_isolate_pageblock(zone))) {

which used to always be false for that config, and now the code is suddenly enabled.

Quoted 7 lines

Alternatively, that code just can't deal with highmem properly.

But I didn't really analyze things, I'm mainly doing pattern matching here.

Zi Yan - and all the people who ack'ed and reviewed this - please take a deeper look..

In the original code, it will jump back to continue_merging and still tries to find the buddy. The crash means the found buddy is not valid, since its pageblock migratetype is NULL. That seems to suggest the physical memory range is not aligned to MAX_ORDER_NR_PAGES, which should not be the case. But if (!page_is_buddy(page, buddy, order)) prevents further buddy merging. I must be missing something.

Hi Steven,

Can you try the patch below to see if it fixes the crash? Thanks.

mm/page_alloc.c +2 -0
index bdc8f60ae462..83a90e2973b7 100644
@@ -1108,6 +1108,8 @@ static inline void __free_one_page(struct page *page,
 
                buddy_pfn = __find_buddy_pfn(pfn, order);
                buddy = page + (buddy_pfn - pfn);
+               if (!page_is_buddy(page, buddy, order))
+                       goto done_merging;
                buddy_mt = get_pageblock_migratetype(buddy);
 
                if (migratetype != buddy_mt

--
Best Regards,
Yan, Zi

#3 lore raw
Quoted 6 lines · On Wed, 30 Mar 2022 16:29:28 -0400 Zi Yan <[email protected]> wrote:
mm/page_alloc.c +0 -0
index bdc8f60ae462..83a90e2973b7 100644

mm/page_alloc.c static inline void __free_one_page(struct page *page, · line 1115

Quoted 8 lines
mm/page_alloc.c +2 -0
@@ -1108,6 +1108,8 @@ static inline void __free_one_page(struct page *page,
 
                buddy_pfn = __find_buddy_pfn(pfn, order);
                buddy = page + (buddy_pfn - pfn);
+               if (!page_is_buddy(page, buddy, order))
+                       goto done_merging;
                buddy_mt = get_pageblock_migratetype(buddy);
 
                if (migratetype != buddy_mt

The above did not apply to Linus's tree, nor even the problem commit (before or after), but I found where the code is, and added it manually.

It does appear to allow the machine to boot.

-- Steve

reviewer
#4 lore raw
Quoted 15 lines · Steven Rostedt
Quoted 12 lines · On Wed, 30 Mar 2022 16:29:28 -0400 Zi Yan <[email protected]> wrote:
mm/page_alloc.c +2 -0
index bdc8f60ae462..83a90e2973b7 100644
@@ -1108,6 +1108,8 @@ static inline void __free_one_page(struct page *page,
 
                buddy_pfn = __find_buddy_pfn(pfn, order);
                buddy = page + (buddy_pfn - pfn);
+               if (!page_is_buddy(page, buddy, order))
+                       goto done_merging;
                buddy_mt = get_pageblock_migratetype(buddy);
 
Quoted 8 lines

mm/page_alloc.c static inline void __free_one_page(struct page *page, · line 1115

Quoted 2 lines
mm/page_alloc.c +0 -0
                if (migratetype != buddy_mt

The above did not apply to Linus's tree, nor even the problem commit (before or after), but I found where the code is, and added it manually.

It does appear to allow the machine to boot.

I just pulled Linus’s tree and grabbed the diff. Anyway, thanks.

I would like to get more understanding of the issue before blindly sending this as a fix.

Merge the other thread:

Quoted 6 lines

Not sure if this matters or not, but my kernel command line has:

crashkernel=256M

Could that have caused this to break?

Unlikely, 256MB is MAX_ORDER_NR_PAGES aligned (MAX_ORDER is 11 here). __find_buddy_pfn() will not get any buddy_pfn from crashkernel memory region, since that would cross MAX_ORDER_NR_PAGES boundary.

page_is_buddy() checks page_is_guard(buddy), PageBuddy(buddy), buddy_order(buddy), and page_zone_id(buddy), where page_is_guard(buddy) is always false since CONFIG_DEBUG_PAGEALLOC is not set in your config. So either PageBuddy(buddy) is false, buddy_order(buddy) != order, or page_zone_id(buddy) is not the same as page_zone_id(page).

Do you mind adding the following code right before my fix code above and provide a complete boot log? I would like to understand what went wrong. Thanks.

pr_info("buddy_pfn: %lx, PageBuddy: %d, buddy_order: %d (vs %d), page_zone_id: %d (vs %d)\n",
buddy_pfn, PageBuddy(buddy), buddy_order(buddy), order, page_zone_id(buddy), page_zone_id(page));

--
Best Regards,
Yan, Zi

reviewer
#5 lore raw
Quoted 48 lines · Zi Yan
Quoted 15 lines · Steven Rostedt
Quoted 12 lines · On Wed, 30 Mar 2022 16:29:28 -0400 Zi Yan <[email protected]> wrote:
mm/page_alloc.c +2 -0
index bdc8f60ae462..83a90e2973b7 100644
@@ -1108,6 +1108,8 @@ static inline void __free_one_page(struct page *page,
 
                buddy_pfn = __find_buddy_pfn(pfn, order);
                buddy = page + (buddy_pfn - pfn);
+               if (!page_is_buddy(page, buddy, order))
+                       goto done_merging;
                buddy_mt = get_pageblock_migratetype(buddy);
 
Quoted 8 lines

mm/page_alloc.c static inline void __free_one_page(struct page *page, · line 1115

Quoted 2 lines
mm/page_alloc.c +0 -0
                if (migratetype != buddy_mt

The above did not apply to Linus's tree, nor even the problem commit (before or after), but I found where the code is, and added it manually.

It does appear to allow the machine to boot.

I just pulled Linus’s tree and grabbed the diff. Anyway, thanks.

I would like to get more understanding of the issue before blindly sending this as a fix.

Merge the other thread:

Quoted 6 lines

Not sure if this matters or not, but my kernel command line has:

crashkernel=256M

Could that have caused this to break?

Unlikely, 256MB is MAX_ORDER_NR_PAGES aligned (MAX_ORDER is 11 here). __find_buddy_pfn() will not get any buddy_pfn from crashkernel memory region, since that would cross MAX_ORDER_NR_PAGES boundary.

page_is_buddy() checks page_is_guard(buddy), PageBuddy(buddy), buddy_order(buddy), and page_zone_id(buddy), where page_is_guard(buddy) is always false since CONFIG_DEBUG_PAGEALLOC is not set in your config. So either PageBuddy(buddy) is false, buddy_order(buddy) != order, or page_zone_id(buddy) is not the same as page_zone_id(page).

Quoted 9 lines

Do you mind adding the following code right before my fix code above and provide a complete boot log? I would like to understand what went wrong. Thanks.

pr_info("buddy_pfn: %lx, PageBuddy: %d, buddy_order: %d (vs %d), page_zone_id: %d (vs %d)\n",
buddy_pfn, PageBuddy(buddy), buddy_order(buddy), order, page_zone_id(buddy), page_zone_id(page));

This seems to be a bug in the original code too.
But "if (unlikely(has_isolate_pageblock(zone)))" is too rare to trigger it. I do not see how having isolated pageblocks in a zone could get us away from checking page_is_buddy().

--
Best Regards,
Yan, Zi

#6 lore raw
Quoted 60 lines · Zi Yan
Quoted 48 lines · Zi Yan
Quoted 15 lines · Steven Rostedt
Quoted 12 lines · On Wed, 30 Mar 2022 16:29:28 -0400 Zi Yan <[email protected]> wrote:
mm/page_alloc.c +2 -0
index bdc8f60ae462..83a90e2973b7 100644
@@ -1108,6 +1108,8 @@ static inline void __free_one_page(struct page *page,
 
                buddy_pfn = __find_buddy_pfn(pfn, order);
                buddy = page + (buddy_pfn - pfn);
+               if (!page_is_buddy(page, buddy, order))
+                       goto done_merging;
                buddy_mt = get_pageblock_migratetype(buddy);
 
Quoted 8 lines

mm/page_alloc.c static inline void __free_one_page(struct page *page, · line 1115

Quoted 2 lines
mm/page_alloc.c +0 -0
                if (migratetype != buddy_mt

The above did not apply to Linus's tree, nor even the problem commit (before or after), but I found where the code is, and added it manually.

It does appear to allow the machine to boot.

I just pulled Linus’s tree and grabbed the diff. Anyway, thanks.

I would like to get more understanding of the issue before blindly sending this as a fix.

Merge the other thread:

Quoted 6 lines

Not sure if this matters or not, but my kernel command line has:

crashkernel=256M

Could that have caused this to break?

Unlikely, 256MB is MAX_ORDER_NR_PAGES aligned (MAX_ORDER is 11 here). __find_buddy_pfn() will not get any buddy_pfn from crashkernel memory region, since that would cross MAX_ORDER_NR_PAGES boundary.

page_is_buddy() checks page_is_guard(buddy), PageBuddy(buddy), buddy_order(buddy), and page_zone_id(buddy), where page_is_guard(buddy) is always false since CONFIG_DEBUG_PAGEALLOC is not set in your config. So either PageBuddy(buddy) is false, buddy_order(buddy) != order, or page_zone_id(buddy) is not the same as page_zone_id(page).

Quoted 9 lines

Do you mind adding the following code right before my fix code above and provide a complete boot log? I would like to understand what went wrong. Thanks.

pr_info("buddy_pfn: %lx, PageBuddy: %d, buddy_order: %d (vs %d), page_zone_id: %d (vs %d)\n",
buddy_pfn, PageBuddy(buddy), buddy_order(buddy), order, page_zone_id(buddy), page_zone_id(page));

Quoted 4 lines

This seems to be a bug in the original code too.
But "if (unlikely(has_isolate_pageblock(zone)))" is too rare to trigger it. I do not see how having isolated pageblocks in a zone could get us away from checking page_is_buddy().

IIRC the assumption was that pageblock bitmaps would always exist withing MAX_ORDER blocks. But here we are still under mem_init() where has_isolate_pageblock() couldn't happen. And the assumption could have been silently broken by subsequent memory init changes.

Quoted 3 lines

--
Best Regards,
Yan, Zi

#7 lore raw
In reply to Zi Yan
Quoted 3 lines · On Wed, 30 Mar 2022 17:25:28 -0400 Zi Yan <[email protected]> wrote:

pr_info("buddy_pfn: %lx, PageBuddy: %d, buddy_order: %d (vs %d), page_zone_id: %d (vs %d)\n",
buddy_pfn, PageBuddy(buddy), buddy_order(buddy), order, page_zone_id(buddy), page_zone_id(page));

Attached (came from ktest log, not actual dmesg).

-- Steve

#8 lore raw
In reply to Zi Yan
Quoted 6 lines · On Wed, 30 Mar 2022 16:29:28 -0400 Zi Yan <[email protected]> wrote:

In the original code, it will jump back to continue_merging and still tries to find the buddy. The crash means the found buddy is not valid, since its pageblock migratetype is NULL. That seems to suggest the physical memory range is not aligned to MAX_ORDER_NR_PAGES, which should not be the case. But if (!page_is_buddy(page, buddy, order)) prevents further buddy merging. I must be missing something.

Not sure if this matters or not, but my kernel command line has:

crashkernel=256M

Could that have caused this to break?

-- Steve

#9 lore raw
In reply to Linus Torvalds
Quoted 52 lines · Linus Torvalds
Quoted 5 lines · Steven Rostedt

I started testing new patches and it crashed when doing the x86-32 test on boot up.

Initializing HighMem for node 0 (000375fe:0021ee00)

Quoted 9 lines
BUG: kernel NULL pointer dereference, address: 00000878
#PF: supervisor read access in kernel mode
#PF: error_code(0x0000) - not-present page
*pdpt = 0000000000000000 *pde = f0000000f000eef3
Oops: 0000 [#1] PREEMPT SMP PTI
CPU: 0 PID: 0 Comm: swapper Not tainted 5.17.0-test+ #469
Hardware name: MSI MS-7823/CSM-H87M-G43 (MS-7823), BIOS V1.6 02/22/2014
EIP: get_pfnblock_flags_mask+0x2c/0x36
Code: 6d ea ff 55 89 e5 56 89 ce 53 8b 18 89 d8 c1 eb 1e e8 f7 fb ff ff 69 db c0 02 00 00 89 c1 89 c2 c1 ea 05 8b 83 7c d7 79 c1 5b <8b> 04 90 d3 e8 21 f0 5e 5d c3 55 89 e5 57 56 89 d6 53 89 c3 64 a1

The whole function is in that Code: thing, and it decodes to:

0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 56 push %esi
4: 89 ce mov %ecx,%esi
6: 53 push %ebx

   7: 8b 18                mov    (%eax),%ebx
   9: 89 d8                mov    %ebx,%eax
   b: c1 eb 1e              shr    $0x1e,%ebx
   e: e8 f7 fb ff ff        call   0xfffffc0a
  13: 69 db c0 02 00 00    imul   $0x2c0,%ebx,%ebx
  19: 89 c1                mov    %eax,%ecx
  1b: 89 c2                mov    %eax,%edx
  1d: c1 ea 05              shr    $0x5,%edx
  20: 8b 83 7c d7 79 c1    mov    -0x3e862884(%ebx),%eax
  26: 5b                    pop    %ebx
  27:* 8b 04 90              mov    (%eax,%edx,4),%eax <-- trapping instruction
  2a: d3 e8                shr    %cl,%eax
  2c: 21 f0                and    %esi,%eax
  2e: 5e                    pop    %esi
  2f: 5d                    pop    %ebp
  30: c3                    ret

with '%eax' being NULL, and %edx being 0x21e.

(The call seems to be to 'pfn_to_bitidx().isra.0' if my compiler does similar code generation, so it's out-of-lined part of pfn_to_bitidx() despite being marked inline)

So that oops is that

        word = bitmap[word_bitidx];

line, with 'bitmap' being NULL (and %edx contains 'word_bitidx').

Quoted 8 lines

Looking around, your 'config-bad' doesn't even have CONFIG_MEMORY_ISOLATION enabled, and so I suspect the culprit is this part of the change:

- if (unlikely(has_isolate_pageblock(zone))) {

which used to always be false for that config, and now the code is suddenly enabled.

If CONFIG_MEMORY_ISOLATION was enabled then the zone layout would be the same, so I think it's not simply that. I think it's the timing - has_isolate_pageblock(zone) would only be possible to become true later in runtime when some isolation is ongoing, but here it seems we are still in the early boot. Probably at a boundary of highmem with another zone that doesn't have the pageblock bitmap yet initialized? While later it would have, and all would be fine.

As Zi Yan said, the usual merging code will, through page_is_buddy() find safely enough the buddy is not applicable, so I agree with his patch direction. Seems this also shows the code tried to become too smart and for the next merge window we should try just move the migratetype checks into the main while loop (under something like "if (order >= max_order)") and simplify the function a lot, hopefully with negligible perf impact.

Quoted 8 lines

Alternatively, that code just can't deal with highmem properly.

But I didn't really analyze things, I'm mainly doing pattern matching here.

Zi Yan - and all the people who ack'ed and reviewed this - please take a deeper look..

Linus

reviewer
#10 lore raw
Quoted 62 lines · Vlastimil Babka
Quoted 52 lines · Linus Torvalds
Quoted 5 lines · Steven Rostedt

I started testing new patches and it crashed when doing the x86-32 test on boot up.

Initializing HighMem for node 0 (000375fe:0021ee00)

Quoted 9 lines
BUG: kernel NULL pointer dereference, address: 00000878
#PF: supervisor read access in kernel mode
#PF: error_code(0x0000) - not-present page
*pdpt = 0000000000000000 *pde = f0000000f000eef3
Oops: 0000 [#1] PREEMPT SMP PTI
CPU: 0 PID: 0 Comm: swapper Not tainted 5.17.0-test+ #469
Hardware name: MSI MS-7823/CSM-H87M-G43 (MS-7823), BIOS V1.6 02/22/2014
EIP: get_pfnblock_flags_mask+0x2c/0x36
Code: 6d ea ff 55 89 e5 56 89 ce 53 8b 18 89 d8 c1 eb 1e e8 f7 fb ff ff 69 db c0 02 00 00 89 c1 89 c2 c1 ea 05 8b 83 7c d7 79 c1 5b <8b> 04 90 d3 e8 21 f0 5e 5d c3 55 89 e5 57 56 89 d6 53 89 c3 64 a1

The whole function is in that Code: thing, and it decodes to:

0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 56 push %esi
4: 89 ce mov %ecx,%esi
6: 53 push %ebx

   7: 8b 18                mov    (%eax),%ebx
   9: 89 d8                mov    %ebx,%eax
   b: c1 eb 1e              shr    $0x1e,%ebx
   e: e8 f7 fb ff ff        call   0xfffffc0a
  13: 69 db c0 02 00 00    imul   $0x2c0,%ebx,%ebx
  19: 89 c1                mov    %eax,%ecx
  1b: 89 c2                mov    %eax,%edx
  1d: c1 ea 05              shr    $0x5,%edx
  20: 8b 83 7c d7 79 c1    mov    -0x3e862884(%ebx),%eax
  26: 5b                    pop    %ebx
  27:* 8b 04 90              mov    (%eax,%edx,4),%eax <-- trapping instruction
  2a: d3 e8                shr    %cl,%eax
  2c: 21 f0                and    %esi,%eax
  2e: 5e                    pop    %esi
  2f: 5d                    pop    %ebp
  30: c3                    ret

with '%eax' being NULL, and %edx being 0x21e.

(The call seems to be to 'pfn_to_bitidx().isra.0' if my compiler does similar code generation, so it's out-of-lined part of pfn_to_bitidx() despite being marked inline)

So that oops is that

        word = bitmap[word_bitidx];

line, with 'bitmap' being NULL (and %edx contains 'word_bitidx').

Quoted 8 lines

Looking around, your 'config-bad' doesn't even have CONFIG_MEMORY_ISOLATION enabled, and so I suspect the culprit is this part of the change:

- if (unlikely(has_isolate_pageblock(zone))) {

which used to always be false for that config, and now the code is suddenly enabled.

Quoted 7 lines

If CONFIG_MEMORY_ISOLATION was enabled then the zone layout would be the same, so I think it's not simply that. I think it's the timing - has_isolate_pageblock(zone) would only be possible to become true later in runtime when some isolation is ongoing, but here it seems we are still in the early boot. Probably at a boundary of highmem with another zone that doesn't have the pageblock bitmap yet initialized? While later it would have, and all would be fine.

It seems so based on the boot log from Steven.

Quoted 8 lines

As Zi Yan said, the usual merging code will, through page_is_buddy() find safely enough the buddy is not applicable, so I agree with his patch direction. Seems this also shows the code tried to become too smart and for the next merge window we should try just move the migratetype checks into the main while loop (under something like "if (order >= max_order)") and simplify the function a lot, hopefully with negligible perf impact.

Something like this?

mm/page_alloc.c +22 -29
index 6c6af8658775..568ecaf5700d 100644
@@ -1053,7 +1053,6 @@ static inline void __free_one_page(struct page *page,
                int migratetype, fpi_t fpi_flags)
 {
        struct capture_control *capc = task_capc(zone);
-       unsigned int max_order = pageblock_order;
        unsigned long buddy_pfn;
        unsigned long combined_pfn;
        struct page *buddy;
@@ -1069,8 +1068,7 @@ static inline void __free_one_page(struct page *page,
        VM_BUG_ON_PAGE(pfn & ((1 << order) - 1), page);
        VM_BUG_ON_PAGE(bad_range(zone, page), page);
 
-continue_merging:
-       while (order < max_order) {
+       while (order < MAX_ORDER - 1) {
                if (compaction_capture(capc, page, order, migratetype)) {
                        __mod_zone_freepage_state(zone, -(1 << order),
                                                                migratetype);
@@ -1081,6 +1079,27 @@ static inline void __free_one_page(struct page *page,
 
                if (!page_is_buddy(page, buddy, order))
                        goto done_merging;
+
+               if (order >= pageblock_order) {
+                       /* If we are here, it means order is >= pageblock_order.
+                        * We want to prevent merge between freepages on pageblock
+                        * without fallbacks and normal pageblock. Without this,
+                        * pageblock isolation could cause incorrect freepage or CMA
+                        * accounting or HIGHATOMIC accounting.
+                        *
+                        * We don't want to hit this code for the more frequent
+                        * low-order merging.
+                        */
+                       int buddy_mt;
+
+                       buddy_mt = get_pageblock_migratetype(buddy);
+
+                       if (migratetype != buddy_mt
+                                       && (!migratetype_is_mergeable(migratetype) ||
+                                               !migratetype_is_mergeable(buddy_mt)))
+                               goto done_merging;
+               }
+
                /*
                 * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page,
                 * merge with it and move up one order.
@@ -1094,32 +1113,6 @@ static inline void __free_one_page(struct page *page,
                pfn = combined_pfn;
                order++;
        }
-       if (order < MAX_ORDER - 1) {
-               /* If we are here, it means order is >= pageblock_order.
-                * We want to prevent merge between freepages on pageblock
-                * without fallbacks and normal pageblock. Without this,
-                * pageblock isolation could cause incorrect freepage or CMA
-                * accounting or HIGHATOMIC accounting.
-                *
-                * We don't want to hit this code for the more frequent
-                * low-order merging.
-                */
-               int buddy_mt;
-
-               buddy_pfn = __find_buddy_pfn(pfn, order);
-               buddy = page + (buddy_pfn - pfn);
-
-               if (!page_is_buddy(page, buddy, order))
-                       goto done_merging;
-               buddy_mt = get_pageblock_migratetype(buddy);
-
-               if (migratetype != buddy_mt
-                               && (!migratetype_is_mergeable(migratetype) ||
-                                       !migratetype_is_mergeable(buddy_mt)))
-                       goto done_merging;
-               max_order = order + 1;
-               goto continue_merging;
-       }
 
 done_merging:
        set_buddy_order(page, order);

--
Best Regards,
Yan, Zi

#11 lore raw
Quoted 83 lines · Zi Yan
Quoted 62 lines · Vlastimil Babka
Quoted 52 lines · Linus Torvalds
Quoted 5 lines · Steven Rostedt

I started testing new patches and it crashed when doing the x86-32 test on boot up.

Initializing HighMem for node 0 (000375fe:0021ee00)

Quoted 9 lines
BUG: kernel NULL pointer dereference, address: 00000878
#PF: supervisor read access in kernel mode
#PF: error_code(0x0000) - not-present page
*pdpt = 0000000000000000 *pde = f0000000f000eef3
Oops: 0000 [#1] PREEMPT SMP PTI
CPU: 0 PID: 0 Comm: swapper Not tainted 5.17.0-test+ #469
Hardware name: MSI MS-7823/CSM-H87M-G43 (MS-7823), BIOS V1.6 02/22/2014
EIP: get_pfnblock_flags_mask+0x2c/0x36
Code: 6d ea ff 55 89 e5 56 89 ce 53 8b 18 89 d8 c1 eb 1e e8 f7 fb ff ff 69 db c0 02 00 00 89 c1 89 c2 c1 ea 05 8b 83 7c d7 79 c1 5b <8b> 04 90 d3 e8 21 f0 5e 5d c3 55 89 e5 57 56 89 d6 53 89 c3 64 a1

The whole function is in that Code: thing, and it decodes to:

0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 56 push %esi
4: 89 ce mov %ecx,%esi
6: 53 push %ebx

   7: 8b 18                mov    (%eax),%ebx
   9: 89 d8                mov    %ebx,%eax
   b: c1 eb 1e              shr    $0x1e,%ebx
   e: e8 f7 fb ff ff        call   0xfffffc0a
  13: 69 db c0 02 00 00    imul   $0x2c0,%ebx,%ebx
  19: 89 c1                mov    %eax,%ecx
  1b: 89 c2                mov    %eax,%edx
  1d: c1 ea 05              shr    $0x5,%edx
  20: 8b 83 7c d7 79 c1    mov    -0x3e862884(%ebx),%eax
  26: 5b                    pop    %ebx
  27:* 8b 04 90              mov    (%eax,%edx,4),%eax <-- trapping instruction
  2a: d3 e8                shr    %cl,%eax
  2c: 21 f0                and    %esi,%eax
  2e: 5e                    pop    %esi
  2f: 5d                    pop    %ebp
  30: c3                    ret

with '%eax' being NULL, and %edx being 0x21e.

(The call seems to be to 'pfn_to_bitidx().isra.0' if my compiler does similar code generation, so it's out-of-lined part of pfn_to_bitidx() despite being marked inline)

So that oops is that

        word = bitmap[word_bitidx];

line, with 'bitmap' being NULL (and %edx contains 'word_bitidx').

Quoted 8 lines

Looking around, your 'config-bad' doesn't even have CONFIG_MEMORY_ISOLATION enabled, and so I suspect the culprit is this part of the change:

- if (unlikely(has_isolate_pageblock(zone))) {

which used to always be false for that config, and now the code is suddenly enabled.

Quoted 7 lines

If CONFIG_MEMORY_ISOLATION was enabled then the zone layout would be the same, so I think it's not simply that. I think it's the timing - has_isolate_pageblock(zone) would only be possible to become true later in runtime when some isolation is ongoing, but here it seems we are still in the early boot. Probably at a boundary of highmem with another zone that doesn't have the pageblock bitmap yet initialized? While later it would have, and all would be fine.

It seems so based on the boot log from Steven.

Quoted 8 lines

As Zi Yan said, the usual merging code will, through page_is_buddy() find safely enough the buddy is not applicable, so I agree with his patch direction. Seems this also shows the code tried to become too smart and for the next merge window we should try just move the migratetype checks into the main while loop (under something like "if (order >= max_order)") and simplify the function a lot, hopefully with negligible perf impact.

Quoted 1 line

Something like this?

Exactly (with some nits).

Quoted 23 lines
mm/page_alloc.c +1 -3
index 6c6af8658775..568ecaf5700d 100644
@@ -1053,7 +1053,6 @@ static inline void __free_one_page(struct page *page,
                int migratetype, fpi_t fpi_flags)
 {
        struct capture_control *capc = task_capc(zone);
-       unsigned int max_order = pageblock_order;
        unsigned long buddy_pfn;
        unsigned long combined_pfn;
        struct page *buddy;
@@ -1069,8 +1068,7 @@ static inline void __free_one_page(struct page *page,
        VM_BUG_ON_PAGE(pfn & ((1 << order) - 1), page);
        VM_BUG_ON_PAGE(bad_range(zone, page), page);
 
-continue_merging:
-       while (order < max_order) {
+       while (order < MAX_ORDER - 1) {
                if (compaction_capture(capc, page, order, migratetype)) {
                        __mod_zone_freepage_state(zone, -(1 << order),
                                                                migratetype);

mm/page_alloc.c static inline void __free_one_page(struct page *page, · line 1083

Quoted 5 lines
mm/page_alloc.c +2 -0
@@ -1081,6 +1079,27 @@ static inline void __free_one_page(struct page *page,
 
                if (!page_is_buddy(page, buddy, order))
                        goto done_merging;
+
+               if (order >= pageblock_order) {

Would use unlikely() here.

mm/page_alloc.c static inline void __free_one_page(struct page *page, · line 1084

Quoted 1 line
mm/page_alloc.c +1 -0
+                       /* If we are here, it means order is >= pageblock_order.

Can drop this like as it became quite obvious now :)

mm/page_alloc.c static inline void __free_one_page(struct page *page, · line 1091

Quoted 7 lines
mm/page_alloc.c +7 -0
+                        * We want to prevent merge between freepages on pageblock
+                        * without fallbacks and normal pageblock. Without this,
+                        * pageblock isolation could cause incorrect freepage or CMA
+                        * accounting or HIGHATOMIC accounting.
+                        *
+                        * We don't want to hit this code for the more frequent
+                        * low-order merging.

The last two lines should be also dropped now.

mm/page_alloc.c static inline void __free_one_page(struct page *page, · line 1095

Quoted 4 lines
mm/page_alloc.c +4 -0
+                        */
+                       int buddy_mt;
+
+                       buddy_mt = get_pageblock_migratetype(buddy);

Can be now single line.

Quoted 47 lines
mm/page_alloc.c +7 -27
+
+                       if (migratetype != buddy_mt
+                                       && (!migratetype_is_mergeable(migratetype) ||
+                                               !migratetype_is_mergeable(buddy_mt)))
+                               goto done_merging;
+               }
+
                /*
                 * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page,
                 * merge with it and move up one order.
@@ -1094,32 +1113,6 @@ static inline void __free_one_page(struct page *page,
                pfn = combined_pfn;
                order++;
        }
-       if (order < MAX_ORDER - 1) {
-               /* If we are here, it means order is >= pageblock_order.
-                * We want to prevent merge between freepages on pageblock
-                * without fallbacks and normal pageblock. Without this,
-                * pageblock isolation could cause incorrect freepage or CMA
-                * accounting or HIGHATOMIC accounting.
-                *
-                * We don't want to hit this code for the more frequent
-                * low-order merging.
-                */
-               int buddy_mt;
-
-               buddy_pfn = __find_buddy_pfn(pfn, order);
-               buddy = page + (buddy_pfn - pfn);
-
-               if (!page_is_buddy(page, buddy, order))
-                       goto done_merging;
-               buddy_mt = get_pageblock_migratetype(buddy);
-
-               if (migratetype != buddy_mt
-                               && (!migratetype_is_mergeable(migratetype) ||
-                                       !migratetype_is_mergeable(buddy_mt)))
-                       goto done_merging;
-               max_order = order + 1;
-               goto continue_merging;
-       }
 
 done_merging:
        set_buddy_order(page, order);
 
--

Best Regards,
Yan, Zi

reviewer
#12 lore raw
Quoted 84 lines · Vlastimil Babka
Quoted 83 lines · Zi Yan
Quoted 62 lines · Vlastimil Babka
Quoted 52 lines · Linus Torvalds
Quoted 5 lines · Steven Rostedt

I started testing new patches and it crashed when doing the x86-32 test on boot up.

Initializing HighMem for node 0 (000375fe:0021ee00)

Quoted 9 lines
BUG: kernel NULL pointer dereference, address: 00000878
#PF: supervisor read access in kernel mode
#PF: error_code(0x0000) - not-present page
*pdpt = 0000000000000000 *pde = f0000000f000eef3
Oops: 0000 [#1] PREEMPT SMP PTI
CPU: 0 PID: 0 Comm: swapper Not tainted 5.17.0-test+ #469
Hardware name: MSI MS-7823/CSM-H87M-G43 (MS-7823), BIOS V1.6 02/22/2014
EIP: get_pfnblock_flags_mask+0x2c/0x36
Code: 6d ea ff 55 89 e5 56 89 ce 53 8b 18 89 d8 c1 eb 1e e8 f7 fb ff ff 69 db c0 02 00 00 89 c1 89 c2 c1 ea 05 8b 83 7c d7 79 c1 5b <8b> 04 90 d3 e8 21 f0 5e 5d c3 55 89 e5 57 56 89 d6 53 89 c3 64 a1

The whole function is in that Code: thing, and it decodes to:

0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 56 push %esi
4: 89 ce mov %ecx,%esi
6: 53 push %ebx

   7: 8b 18                mov    (%eax),%ebx
   9: 89 d8                mov    %ebx,%eax
   b: c1 eb 1e              shr    $0x1e,%ebx
   e: e8 f7 fb ff ff        call   0xfffffc0a
  13: 69 db c0 02 00 00    imul   $0x2c0,%ebx,%ebx
  19: 89 c1                mov    %eax,%ecx
  1b: 89 c2                mov    %eax,%edx
  1d: c1 ea 05              shr    $0x5,%edx
  20: 8b 83 7c d7 79 c1    mov    -0x3e862884(%ebx),%eax
  26: 5b                    pop    %ebx
  27:* 8b 04 90              mov    (%eax,%edx,4),%eax <-- trapping instruction
  2a: d3 e8                shr    %cl,%eax
  2c: 21 f0                and    %esi,%eax
  2e: 5e                    pop    %esi
  2f: 5d                    pop    %ebp
  30: c3                    ret

with '%eax' being NULL, and %edx being 0x21e.

(The call seems to be to 'pfn_to_bitidx().isra.0' if my compiler does similar code generation, so it's out-of-lined part of pfn_to_bitidx() despite being marked inline)

So that oops is that

        word = bitmap[word_bitidx];

line, with 'bitmap' being NULL (and %edx contains 'word_bitidx').

Quoted 8 lines

Looking around, your 'config-bad' doesn't even have CONFIG_MEMORY_ISOLATION enabled, and so I suspect the culprit is this part of the change:

- if (unlikely(has_isolate_pageblock(zone))) {

which used to always be false for that config, and now the code is suddenly enabled.

Quoted 7 lines

If CONFIG_MEMORY_ISOLATION was enabled then the zone layout would be the same, so I think it's not simply that. I think it's the timing - has_isolate_pageblock(zone) would only be possible to become true later in runtime when some isolation is ongoing, but here it seems we are still in the early boot. Probably at a boundary of highmem with another zone that doesn't have the pageblock bitmap yet initialized? While later it would have, and all would be fine.

It seems so based on the boot log from Steven.

Quoted 8 lines

As Zi Yan said, the usual merging code will, through page_is_buddy() find safely enough the buddy is not applicable, so I agree with his patch direction. Seems this also shows the code tried to become too smart and for the next merge window we should try just move the migratetype checks into the main while loop (under something like "if (order >= max_order)") and simplify the function a lot, hopefully with negligible perf impact.

Quoted 3 lines
Quoted 1 line

Something like this?

Exactly (with some nits).

Thanks for the comment. Should I send the patch below with nits fix to supersede the patch I just sent?

Quoted 94 lines
Quoted 24 lines
mm/page_alloc.c +1 -3
index 6c6af8658775..568ecaf5700d 100644
@@ -1053,7 +1053,6 @@ static inline void __free_one_page(struct page *page,
                int migratetype, fpi_t fpi_flags)
 {
        struct capture_control *capc = task_capc(zone);
-       unsigned int max_order = pageblock_order;
        unsigned long buddy_pfn;
        unsigned long combined_pfn;
        struct page *buddy;
@@ -1069,8 +1068,7 @@ static inline void __free_one_page(struct page *page,
        VM_BUG_ON_PAGE(pfn & ((1 << order) - 1), page);
        VM_BUG_ON_PAGE(bad_range(zone, page), page);
 
-continue_merging:
-       while (order < max_order) {
+       while (order < MAX_ORDER - 1) {
                if (compaction_capture(capc, page, order, migratetype)) {
                        __mod_zone_freepage_state(zone, -(1 << order),
                                                                migratetype);

mm/page_alloc.c static inline void __free_one_page(struct page *page, · line 1083

Quoted 5 lines
mm/page_alloc.c +2 -0
@@ -1081,6 +1079,27 @@ static inline void __free_one_page(struct page *page,
 
                if (!page_is_buddy(page, buddy, order))
                        goto done_merging;
+
+               if (order >= pageblock_order) {

Would use unlikely() here.

mm/page_alloc.c static inline void __free_one_page(struct page *page, · line 1084

Quoted 1 line
mm/page_alloc.c +1 -0
+                       /* If we are here, it means order is >= pageblock_order.

Can drop this like as it became quite obvious now :)

mm/page_alloc.c static inline void __free_one_page(struct page *page, · line 1091

Quoted 7 lines
mm/page_alloc.c +7 -0
+                        * We want to prevent merge between freepages on pageblock
+                        * without fallbacks and normal pageblock. Without this,
+                        * pageblock isolation could cause incorrect freepage or CMA
+                        * accounting or HIGHATOMIC accounting.
+                        *
+                        * We don't want to hit this code for the more frequent
+                        * low-order merging.

The last two lines should be also dropped now.

mm/page_alloc.c static inline void __free_one_page(struct page *page, · line 1095

Quoted 4 lines
mm/page_alloc.c +4 -0
+                        */
+                       int buddy_mt;
+
+                       buddy_mt = get_pageblock_migratetype(buddy);

Can be now single line.

Quoted 41 lines
mm/page_alloc.c +7 -26
+
+                       if (migratetype != buddy_mt
+                                       && (!migratetype_is_mergeable(migratetype) ||
+                                               !migratetype_is_mergeable(buddy_mt)))
+                               goto done_merging;
+               }
+
                /*
                 * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page,
                 * merge with it and move up one order.
@@ -1094,32 +1113,6 @@ static inline void __free_one_page(struct page *page,
                pfn = combined_pfn;
                order++;
        }
-       if (order < MAX_ORDER - 1) {
-               /* If we are here, it means order is >= pageblock_order.
-                * We want to prevent merge between freepages on pageblock
-                * without fallbacks and normal pageblock. Without this,
-                * pageblock isolation could cause incorrect freepage or CMA
-                * accounting or HIGHATOMIC accounting.
-                *
-                * We don't want to hit this code for the more frequent
-                * low-order merging.
-                */
-               int buddy_mt;
-
-               buddy_pfn = __find_buddy_pfn(pfn, order);
-               buddy = page + (buddy_pfn - pfn);
-
-               if (!page_is_buddy(page, buddy, order))
-                       goto done_merging;
-               buddy_mt = get_pageblock_migratetype(buddy);
-
-               if (migratetype != buddy_mt
-                               && (!migratetype_is_mergeable(migratetype) ||
-                                       !migratetype_is_mergeable(buddy_mt)))
-                       goto done_merging;
-               max_order = order + 1;
-               goto continue_merging;
-       }
 
Quoted 6 lines
Quoted 6 lines
mm/page_alloc.c +0 -1
 done_merging:
        set_buddy_order(page, order);
 
--

Best Regards,
Yan, Zi

--
Best Regards,
Yan, Zi