Skip to contentSkip to conversation

x86: fix determination of bit count for struct domain allocations

PATCH In review Jan Beulich 3 participants 8 messages Mar 14, 2014 – Mar 24, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: fix determination of bit count for struct domain allocations

We can't just add in the hole shift value, as the hole may be at or above the 44-bit boundary. Instead we need to determine the total bit count until reaching 32 significant (not squashed out) bits in PFN representations.

xen/arch/x86/domain.c +17 -1
@@ -180,6 +180,19 @@ void dump_pageframe_info(struct domain *
     spin_unlock(&d->page_alloc_lock);
 }
 
+static unsigned int __init noinline _domain_struct_bits(void)
+{
+    unsigned int bits = 32 + PAGE_SHIFT;
+    unsigned int sig = hweight32(~pfn_hole_mask);
+    unsigned int mask = pfn_hole_mask >> 32;
+
+    for ( ; bits < BITS_PER_LONG && sig < 32; ++bits, mask >>= 1 )
+        if ( !(mask & 1) )
+            ++sig;
+
+    return bits;
+}
+
 struct domain *alloc_domain_struct(void)
 {
     struct domain *d;
@@ -187,7 +200,10 @@ struct domain *alloc_domain_struct(void)
      * We pack the PDX of the domain structure into a 32-bit field within
      * the page_info structure. Hence the MEMF_bits() restriction.
      */
-    unsigned int bits = 32 + PAGE_SHIFT + pfn_pdx_hole_shift;
+    static unsigned int __read_mostly bits;
+
+    if ( unlikely(!bits) )
+         bits = _domain_struct_bits();
 
     BUILD_BUG_ON(sizeof(*d) > PAGE_SIZE);
     d = alloc_xenheap_pages(0, MEMF_bits(bits));

x86: fix determination of bit count for struct domain allocations

We can't just add in the hole shift value, as the hole may be at or above the 44-bit boundary. Instead we need to determine the total bit count until reaching 32 significant (not squashed out) bits in PFN representations.

xen/arch/x86/domain.c +17 -1
@@ -180,6 +180,19 @@ void dump_pageframe_info(struct domain *
     spin_unlock(&d->page_alloc_lock);
 }
 
+static unsigned int __init noinline _domain_struct_bits(void)
+{
+    unsigned int bits = 32 + PAGE_SHIFT;
+    unsigned int sig = hweight32(~pfn_hole_mask);
+    unsigned int mask = pfn_hole_mask >> 32;
+
+    for ( ; bits < BITS_PER_LONG && sig < 32; ++bits, mask >>= 1 )
+        if ( !(mask & 1) )
+            ++sig;
+
+    return bits;
+}
+
 struct domain *alloc_domain_struct(void)
 {
     struct domain *d;
@@ -187,7 +200,10 @@ struct domain *alloc_domain_struct(void)
      * We pack the PDX of the domain structure into a 32-bit field within
      * the page_info structure. Hence the MEMF_bits() restriction.
      */
-    unsigned int bits = 32 + PAGE_SHIFT + pfn_pdx_hole_shift;
+    static unsigned int __read_mostly bits;
+
+    if ( unlikely(!bits) )
+         bits = _domain_struct_bits();
 
     BUILD_BUG_ON(sizeof(*d) > PAGE_SIZE);
     d = alloc_xenheap_pages(0, MEMF_bits(bits));

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#1 lore raw
Quoted 10 lines · Jan Beulich

We can't just add in the hole shift value, as the hole may be at or above the 44-bit boundary. Instead we need to determine the total bit count until reaching 32 significant (not squashed out) bits in PFN representations.

xen/arch/x86/domain.c void dump_pageframe_info(struct domain * · line 183

Quoted 4 lines
xen/arch/x86/domain.c +1 -0
@@ -180,6 +180,19 @@ void dump_pageframe_info(struct domain *
     spin_unlock(&d->page_alloc_lock);
 }
 
+static unsigned int __init noinline _domain_struct_bits(void)

noinline for debugging purposes?

~Andrew

Quoted 32 lines
xen/arch/x86/domain.c +16 -1
+{
+    unsigned int bits = 32 + PAGE_SHIFT;
+    unsigned int sig = hweight32(~pfn_hole_mask);
+    unsigned int mask = pfn_hole_mask >> 32;
+
+    for ( ; bits < BITS_PER_LONG && sig < 32; ++bits, mask >>= 1 )
+        if ( !(mask & 1) )
+            ++sig;
+
+    return bits;
+}
+
 struct domain *alloc_domain_struct(void)
 {
     struct domain *d;
@@ -187,7 +200,10 @@ struct domain *alloc_domain_struct(void)
      * We pack the PDX of the domain structure into a 32-bit field within
      * the page_info structure. Hence the MEMF_bits() restriction.
      */
-    unsigned int bits = 32 + PAGE_SHIFT + pfn_pdx_hole_shift;
+    static unsigned int __read_mostly bits;
+
+    if ( unlikely(!bits) )
+         bits = _domain_struct_bits();
 
     BUILD_BUG_ON(sizeof(*d) > PAGE_SIZE);
     d = alloc_xenheap_pages(0, MEMF_bits(bits));
Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#2 lore raw
Quoted 15 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 13 lines · Jan Beulich

We can't just add in the hole shift value, as the hole may be at or above the 44-bit boundary. Instead we need to determine the total bit count until reaching 32 significant (not squashed out) bits in PFN representations.

xen/arch/x86/domain.c +0 -0
@@ -180,6 +180,19 @@ void dump_pageframe_info(struct domain *
     spin_unlock(&d->page_alloc_lock);
 }
 
Quoted 3 lines

xen/arch/x86/domain.c void dump_pageframe_info(struct domain * · line 183

Quoted 1 line
xen/arch/x86/domain.c +1 -0
+static unsigned int __init noinline _domain_struct_bits(void)

noinline for debugging purposes?

No, for it to really end up in .init.text (as the caller is in .text).

Jan

#3 lore raw
Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 13 lines · Jan Beulich

We can't just add in the hole shift value, as the hole may be at or above the 44-bit boundary. Instead we need to determine the total bit count until reaching 32 significant (not squashed out) bits in PFN representations.

xen/arch/x86/domain.c +0 -0
@@ -180,6 +180,19 @@ void dump_pageframe_info(struct domain *
     spin_unlock(&d->page_alloc_lock);
 }
 
Quoted 6 lines
Quoted 2 lines

xen/arch/x86/domain.c void dump_pageframe_info(struct domain * · line 183

Quoted 1 line
xen/arch/x86/domain.c +1 -0
+static unsigned int __init noinline _domain_struct_bits(void)

noinline for debugging purposes?

No, for it to really end up in .init.text (as the caller is in .text).

Jan

In which case it should assert/bug if the function returns 0, to be sure we will never fall into the unlikely case again and try to call it from a non-init function. Also a comment to explain this.

~Andrew

#4 lore raw
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 13 lines · Jan Beulich

We can't just add in the hole shift value, as the hole may be at or above the 44-bit boundary. Instead we need to determine the total bit count until reaching 32 significant (not squashed out) bits in PFN representations.

xen/arch/x86/domain.c +0 -0
@@ -180,6 +180,19 @@ void dump_pageframe_info(struct domain *
     spin_unlock(&d->page_alloc_lock);
 }
 
Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

xen/arch/x86/domain.c void dump_pageframe_info(struct domain * · line 183

Quoted 1 line
xen/arch/x86/domain.c +1 -0
+static unsigned int __init noinline _domain_struct_bits(void)

noinline for debugging purposes?

No, for it to really end up in .init.text (as the caller is in .text).

In which case it should assert/bug if the function returns 0, to be sure we will never fall into the unlikely case again and try to call it from a non-init function. Also a comment to explain this.

I really dislike asserting the absolutely obvious: The function can be very easily proven to only return values in the range [44,64].

Jan

#5 lore raw
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 10 lines · Jan Beulich

We can't just add in the hole shift value, as the hole may be at or above the 44-bit boundary. Instead we need to determine the total bit count until reaching 32 significant (not squashed out) bits in PFN representations.

xen/arch/x86/domain.c void dump_pageframe_info(struct domain * · line 183

Quoted 4 lines
xen/arch/x86/domain.c +1 -0
@@ -180,6 +180,19 @@ void dump_pageframe_info(struct domain *
     spin_unlock(&d->page_alloc_lock);
 }
 
+static unsigned int __init noinline _domain_struct_bits(void)

noinline for debugging purposes?

Quoted 9 lines
Quoted 4 lines
Quoted 1 line

No, for it to really end up in .init.text (as the caller is in .text).

In which case it should assert/bug if the function returns 0, to be sure we will never fall into the unlikely case again and try to call it from a non-init function. Also a comment to explain this.

I really dislike asserting the absolutely obvious: The function can be very easily proven to only return values in the range [44,64].

Jan

Hmm ok, but at the very least this deserves a comment to that effect.

~Andrew

Acked-by on v1 1/1
#6 lore raw
In reply to Jan Beulich
Quoted 15 lines

Jan Beulich <mailto:[email protected]>
14 March 2014 15:23
We can't just add in the hole shift value, as the hole may be at or above the 44-bit boundary. Instead we need to determine the total bit count until reaching 32 significant (not squashed out) bits in PFN representations.

xen/arch/x86/domain.c +0 -0
@@ -180,6 +180,19 @@ void dump_pageframe_info(struct domain *
 spin_unlock(&d->page_alloc_lock);
 }

xen/arch/x86/domain.c void dump_pageframe_info(struct domain * · line 191

Quoted 10 lines
xen/arch/x86/domain.c +9 -0
 
+static unsigned int __init noinline _domain_struct_bits(void)
+{
+ unsigned int bits = 32 + PAGE_SHIFT;
+ unsigned int sig = hweight32(~pfn_hole_mask);
+ unsigned int mask = pfn_hole_mask >> 32;
+
+ for ( ; bits < BITS_PER_LONG && sig < 32; ++bits, mask >>= 1 )
+ if ( !(mask & 1) )
+ ++sig;

This logic took a few looks to be sure what it was doing, let alone correct. Without the patch comment I'd be even a bit more lost. I think the changeset comment, or similar, should be included as a code comment for this new function.

I'm sure given there is only one hole there is a simpler form of this based on checking whether bottom_shift > 44. But this is more general I suppose, so probably better despite being harder to understand.

Quoted 11 lines
xen/arch/x86/domain.c +3 -0
+ return bits;
+}
+
 struct domain *alloc_domain_struct(void)
 {
 struct domain *d;
@@ -187,7 +200,10 @@ struct domain *alloc_domain_struct(void)
 * We pack the PDX of the domain structure into a 32-bit field within

xen/arch/x86/domain.c struct domain *alloc_domain_struct(void) · line 209

Quoted 10 lines
xen/arch/x86/domain.c +4 -1
 * the page_info structure. Hence the MEMF_bits() restriction.
 */
- unsigned int bits = 32 + PAGE_SHIFT + pfn_pdx_hole_shift;
+ static unsigned int __read_mostly bits;
+
+ if ( unlikely(!bits) )
+ bits = _domain_struct_bits();
 
 BUILD_BUG_ON(sizeof(*d) > PAGE_SIZE);
 d = alloc_xenheap_pages(0, MEMF_bits(bits));

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#7 lore raw
Quoted 17 lines
Quoted 17 lines
Quoted 1 line · On 24.03.14 at 09:56, <[email protected]> wrote:

Jan Beulich <mailto:[email protected]>
14 March 2014 15:23
We can't just add in the hole shift value, as the hole may be at or above the 44-bit boundary. Instead we need to determine the total bit count until reaching 32 significant (not squashed out) bits in PFN representations.

xen/arch/x86/domain.c +0 -0
@@ -180,6 +180,19 @@ void dump_pageframe_info(struct domain *
 spin_unlock(&d->page_alloc_lock);
 }
 
Quoted 14 lines

xen/arch/x86/domain.c void dump_pageframe_info(struct domain * · line 191

Quoted 9 lines
xen/arch/x86/domain.c +9 -0
+static unsigned int __init noinline _domain_struct_bits(void)
+{
+ unsigned int bits = 32 + PAGE_SHIFT;
+ unsigned int sig = hweight32(~pfn_hole_mask);
+ unsigned int mask = pfn_hole_mask >> 32;
+
+ for ( ; bits < BITS_PER_LONG && sig < 32; ++bits, mask >>= 1 )
+ if ( !(mask & 1) )
+ ++sig;

This logic took a few looks to be sure what it was doing, let alone correct. Without the patch comment I'd be even a bit more lost. I think the changeset comment, or similar, should be included as a code comment for this new function.

In which case I'll probably also add something along the lines of what Andrew had asked for.

Quoted 3 lines

I'm sure given there is only one hole there is a simpler form of this based on checking whether bottom_shift > 44. But this is more general I suppose, so probably better despite being harder to understand.

Actually I noticed this being wrong in the context of generalizing the PFN compaction logic (pending people having access to suitable hardware to verify those changes), which among other things will allow for multiple holes.

Jan