Skip to contentSkip to conversation

xen/page_alloc: Keep away MFN 0 from the buddy allocator

PATCH In review Julien Grall 4 participants 9 messages Aug 9, 2019 – Aug 12, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/page_alloc: Keep away MFN 0 from the buddy allocator

Combining of buddies happens only such that the resulting larger buddy is still order-aligned. To cross a zone boundary while merging, the implication is that both the buddy [0, 2^n-1] and the buddy [2^n, 2^(n+1)] are free.

Ideally we want to fix the allocator, but for now we can just prevent adding the MFN 0 in the allocator.

On x86, the MFN 0 is already kept away from the buddy allocator. So the bug can only happen on Arm platform where the first memory bank is starting at 0.

As this is a specific to the allocator, the MFN 0 is removed in the common code to cater all the architectures (current and future).

---
Cc: [email protected]
Cc: [email protected]

I am not sure I fully understood the exact problem when MFN 0 is given to the allocator. Feel free to suggest a better explanation.

Can anyone able to reproduce the bug test where the patch effectively solve the crash?

---
 xen/common/page_alloc.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
xen/common/page_alloc.c +12 -0
index 453b303b5b..42b8a8ce23 100644
@@ -1759,6 +1759,18 @@ static void init_heap_pages(
     bool idle_scrub = false;
 
     /*
+     * Keep MFN 0 away from the buddy allocator to avoid crossing zone
+     * boundary when merging two buddies.
+     */
+    if ( !mfn_x(page_to_mfn(pg)) )
+    {
+        if ( nr_pages-- <= 1 )
+            return;
+        pg++;
+    }
+
+
+    /*
      * Some pages may not go through the boot allocator (e.g reserved
      * memory at boot but released just after --- kernel, initramfs,
      * etc.).
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Julien Grall

Combining of buddies happens only such that the resulting larger buddy is still order-aligned. To cross a zone boundary while merging, the implication is that both the buddy [0, 2^n-1] and the buddy [2^n, 2^(n+1)] are free.

[2^n, 2^(n+1)-1]

You may want to add that merging across zone boundaries is what we need to prevent.

Quoted 7 lines

Ideally we want to fix the allocator, but for now we can just prevent adding the MFN 0 in the allocator.

On x86, the MFN 0 is already kept away from the buddy allocator. So the bug can only happen on Arm platform where the first memory bank is starting at 0.

Quoted 5 lines

As this is a specific to the allocator, the MFN 0 is removed in the common code to cater all the architectures (current and future).

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 18 lines · Jan Beulich
Quoted 4 lines · Julien Grall

Combining of buddies happens only such that the resulting larger buddy is still order-aligned. To cross a zone boundary while merging, the implication is that both the buddy [0, 2^n-1] and the buddy [2^n, 2^(n+1)] are free.

[2^n, 2^(n+1)-1]

You may want to add that merging across zone boundaries is what we need to prevent.

Quoted 7 lines

Ideally we want to fix the allocator, but for now we can just prevent adding the MFN 0 in the allocator.

On x86, the MFN 0 is already kept away from the buddy allocator. So the bug can only happen on Arm platform where the first memory bank is starting at 0.

Quoted 7 lines
Quoted 5 lines

As this is a specific to the allocator, the MFN 0 is removed in the common code to cater all the architectures (current and future).

Here is Jeff's initial patch for the issue.

From: Jeff Kubascik <[email protected]>
Date: Mon, 4 Mar 2019 14:14:05 -0500
Subject: [PATCH] Check zone before merging adjacent blocks in heap

The Xen heap is split up into nodes and zones. Each node + zone is managed as a separate pool of memory.

When returning pages to the heap, free_heap_pages will check adjacent blocks to see if they can be combined into a larger block. However, the zone of the adjacent block is not checked. This results in blocks that migrate from one zone to another.

When a block migrates to the adjacent zone, the avail counters for the old and new node + zone is not updated accordingly. The avail counter is used when allocating pages to determine whether to skip over a zone. With this behavior, it is possible for free pages to collect in a zone with the avail counter smaller than the actual page count, resulting in free pages that are not allocable.

This commit adds a check to compare the adjacent block's zone with the current zone before merging them.

---
 xen/common/page_alloc.c | 2 ++
 1 file changed, 2 insertions(+)
xen/common/page_alloc.c +2 -0
index 482f0988f7..a92268cc67 100644
@@ -1419,6 +1419,7 @@ static void free_heap_pages(
             if ( !mfn_valid(page_to_mfn(predecessor)) ||
                  !page_state_is(predecessor, free) ||
                  (PFN_ORDER(predecessor) != order) ||
+                 (page_to_zone(pg-mask) != zone) ||
                  (phys_to_nid(page_to_maddr(predecessor)) != node) )
                 break;
 
@@ -1442,6 +1443,7 @@ static void free_heap_pages(
             if ( !mfn_valid(page_to_mfn(successor)) ||
                  !page_state_is(successor, free) ||
                  (PFN_ORDER(successor) != order) ||
+                 (page_to_zone(pg+mask) != zone) ||
                  (phys_to_nid(page_to_maddr(successor)) != node) )
                 break;
 
Signature · 6 lines
2.22.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#3 lore raw
Quoted 22 lines · Stewart Hildebrand
Quoted 21 lines · Jan Beulich
Quoted 4 lines · Julien Grall

Combining of buddies happens only such that the resulting larger buddy is still order-aligned. To cross a zone boundary while merging, the implication is that both the buddy [0, 2^n-1] and the buddy [2^n, 2^(n+1)] are free.

[2^n, 2^(n+1)-1]

You may want to add that merging across zone boundaries is what we need to prevent.

Quoted 10 lines

Ideally we want to fix the allocator, but for now we can just prevent adding the MFN 0 in the allocator.

On x86, the MFN 0 is already kept away from the buddy allocator. So the bug can only happen on Arm platform where the first memory bank is starting at 0.

As this is a specific to the allocator, the MFN 0 is removed in the common code to cater all the architectures (current and future).

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Here is Jeff's initial patch for the issue.

I committed Julien's patch for now, but if we need to make any changes or decide for a better alternative, we can always revert it.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw

On Friday, August 9, 2019 2:24 PM, Stefano Stabellini <[email protected]>

Quoted 4 lines · Stewart Hildebrand
Quoted 1 line

Here is Jeff's initial patch for the issue.

I committed Julien's patch for now,

Great! Thanks!

Quoted 2 lines

but if we need to make any changes
or decide for a better alternative, we can always revert it.

Can we entertain committing both patches?
To paraphrase George from an earlier discussion: Removing MFN 0 fixes the issue by relying on side effects. Adding an explicit check in the merging logic directly fixes the issue.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw

Hi Stewart,

Quoted 4 lines · Stewart Hildebrand

On Friday, August 9, 2019 2:24 PM, Stefano Stabellini <[email protected]>

Quoted 3 lines · Stewart Hildebrand
Quoted 1 line

Here is Jeff's initial patch for the issue.

Quoted 8 lines
Quoted 1 line

I committed Julien's patch for now,

Great! Thanks!

Quoted 2 lines

but if we need to make any changes
or decide for a better alternative, we can always revert it.

Can we entertain committing both patches?

That's you to post formally the patch on the ML ;). The way you posted is likely going to be unnoticed as you hijack the discussion (subject title as not be changed).

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#6 lore raw
In reply to Stewart Hildebrand
Quoted 6 lines · Stewart Hildebrand

On Friday, August 9, 2019 2:24 PM, Stefano Stabellini <[email protected]>

Quoted 4 lines · Stewart Hildebrand
Quoted 1 line

Here is Jeff's initial patch for the issue.

I committed Julien's patch for now,

Quoted 8 lines

Great! Thanks!

Quoted 2 lines

but if we need to make any changes
or decide for a better alternative, we can always revert it.

Can we entertain committing both patches?
To paraphrase George from an earlier discussion: Removing MFN 0 fixes the issue by relying on side effects. Adding an explicit check in the merging logic directly fixes the issue.

I thought previous discussion (when you had first posted you variant of the fix) had clarified that there are objections to you modifying an often executed code path when the same effect can be achieved by modifying an infrequently executed one.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw
In reply to Stewart Hildebrand
Quoted 22 lines · Stewart Hildebrand
Quoted 21 lines · Jan Beulich
Quoted 4 lines · Julien Grall

Combining of buddies happens only such that the resulting larger buddy is still order-aligned. To cross a zone boundary while merging, the implication is that both the buddy [0, 2^n-1] and the buddy [2^n, 2^(n+1)] are free.

[2^n, 2^(n+1)-1]

You may want to add that merging across zone boundaries is what we need to prevent.

Quoted 10 lines

Ideally we want to fix the allocator, but for now we can just prevent adding the MFN 0 in the allocator.

On x86, the MFN 0 is already kept away from the buddy allocator. So the bug can only happen on Arm platform where the first memory bank is starting at 0.

As this is a specific to the allocator, the MFN 0 is removed in the common code to cater all the architectures (current and future).

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Here is Jeff's initial patch for the issue.

To be honest, it would have been nice if you had clarified _why_ you sent this in reply here.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Acked-by Tested-by on v1 1/1
#8 lore raw
In reply to Julien Grall
Quoted 12 lines · Julien Grall

Combining of buddies happens only such that the resulting larger buddy is still order-aligned. To cross a zone boundary while merging, the implication is that both the buddy [0, 2^n-1] and the buddy [2^n, 2^(n+1)] are free.

Ideally we want to fix the allocator, but for now we can just prevent adding the MFN 0 in the allocator.

On x86, the MFN 0 is already kept away from the buddy allocator. So the bug can only happen on Arm platform where the first memory bank is starting at 0.

Quoted 5 lines

As this is a specific to the allocator, the MFN 0 is removed in the common code to cater all the architectures (current and future).

I could reproduce the problem and I confirm that this patch fixes it:

Quoted 39 lines

---
Cc: [email protected]
Cc: [email protected]

I am not sure I fully understood the exact problem when MFN 0 is given to the allocator. Feel free to suggest a better explanation.

Can anyone able to reproduce the bug test where the patch effectively solve the crash?

---
 xen/common/page_alloc.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
xen/common/page_alloc.c +12 -0
index 453b303b5b..42b8a8ce23 100644
@@ -1759,6 +1759,18 @@ static void init_heap_pages(
     bool idle_scrub = false;
 
     /*
+     * Keep MFN 0 away from the buddy allocator to avoid crossing zone
+     * boundary when merging two buddies.
+     */
+    if ( !mfn_x(page_to_mfn(pg)) )
+    {
+        if ( nr_pages-- <= 1 )
+            return;
+        pg++;
+    }
+
+
+    /*
      * Some pages may not go through the boot allocator (e.g reserved
      * memory at boot but released just after --- kernel, initramfs,
      * etc.).
Signature · 1 line
2.11.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel