Skip to contentSkip to conversation

x86/PoD: tighten conditions for checking super page

PATCH Superseded Jan Beulich 4 participants 7 messages Oct 30, 2015 – Nov 9, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/PoD: tighten conditions for checking super page

Since calling the function isn't cheap, try to avoid the call when we know up front it won't help; see the code comment for details on those conditions.

xen/arch/x86/mm/p2m-pod.c +14 -10
@@ -522,7 +522,6 @@ p2m_pod_decrease_reservation(struct doma
     if ( unlikely(d->is_dying) )
         goto out_unlock;
 
-recount:
     pod = nonpod = ram = 0;
 
     /* Figure out if we need to steal some freed memory for our cache */
@@ -562,15 +561,20 @@ recount:
         goto out_entry_check;
     }
 
-    /* Try to grab entire superpages if possible.  Since the common case is for drivers
-     * to pass back singleton pages, see if we can take the whole page back and mark the
-     * rest PoD. */
-    if ( steal_for_cache
-         && p2m_pod_zero_check_superpage(p2m, gpfn & ~(SUPERPAGE_PAGES-1)))
-    {
-        /* Since order may be arbitrary, we may have taken more or less
-         * than we were actually asked to; so just re-count from scratch */
-        goto recount;
+    /*
+     * Try to grab entire superpages if possible.  Since the common case is for
+     * drivers to pass back singleton pages, see if we can take the whole page
+     * back and mark the rest PoD.
+     * No need to do this though if
+     * - order >= SUPERPAGE_ORDER (the loop below will take care of this)
+     * - not all of the pages were RAM (now knowing order < SUPERPAGE_ORDER)
+     */
+    if ( steal_for_cache && order < SUPERPAGE_ORDER && (ram >> order) &&
+         p2m_pod_zero_check_superpage(p2m, gpfn & ~(SUPERPAGE_PAGES - 1)) )
+    {
+        pod += ram;
+        nonpod -= ram;
+        ram = 0;
     }
 
     /* Process as long as:

x86/PoD: tighten conditions for checking super page

Since calling the function isn't cheap, try to avoid the call when we know up front it won't help; see the code comment for details on those conditions.

xen/arch/x86/mm/p2m-pod.c +14 -10
@@ -522,7 +522,6 @@ p2m_pod_decrease_reservation(struct doma
     if ( unlikely(d->is_dying) )
         goto out_unlock;
 
-recount:
     pod = nonpod = ram = 0;
 
     /* Figure out if we need to steal some freed memory for our cache */
@@ -562,15 +561,20 @@ recount:
         goto out_entry_check;
     }
 
-    /* Try to grab entire superpages if possible.  Since the common case is for drivers
-     * to pass back singleton pages, see if we can take the whole page back and mark the
-     * rest PoD. */
-    if ( steal_for_cache
-         && p2m_pod_zero_check_superpage(p2m, gpfn & ~(SUPERPAGE_PAGES-1)))
-    {
-        /* Since order may be arbitrary, we may have taken more or less
-         * than we were actually asked to; so just re-count from scratch */
-        goto recount;
+    /*
+     * Try to grab entire superpages if possible.  Since the common case is for
+     * drivers to pass back singleton pages, see if we can take the whole page
+     * back and mark the rest PoD.
+     * No need to do this though if
+     * - order >= SUPERPAGE_ORDER (the loop below will take care of this)
+     * - not all of the pages were RAM (now knowing order < SUPERPAGE_ORDER)
+     */
+    if ( steal_for_cache && order < SUPERPAGE_ORDER && (ram >> order) &&
+         p2m_pod_zero_check_superpage(p2m, gpfn & ~(SUPERPAGE_PAGES - 1)) )
+    {
+        pod += ram;
+        nonpod -= ram;
+        ram = 0;
     }
 
     /* Process as long as:

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

#2 lore raw
In reply to Jan Beulich
Quoted 33 lines · Jan Beulich

Since calling the function isn't cheap, try to avoid the call when we know up front it won't help; see the code comment for details on those conditions.

xen/arch/x86/mm/p2m-pod.c +4 -10
@@ -522,7 +522,6 @@ p2m_pod_decrease_reservation(struct doma
     if ( unlikely(d->is_dying) )
         goto out_unlock;
 
-recount:
     pod = nonpod = ram = 0;
 
     /* Figure out if we need to steal some freed memory for our cache */
@@ -562,15 +561,20 @@ recount:
         goto out_entry_check;
     }
 
-    /* Try to grab entire superpages if possible.  Since the common case is for drivers
-     * to pass back singleton pages, see if we can take the whole page back and mark the
-     * rest PoD. */
-    if ( steal_for_cache
-         && p2m_pod_zero_check_superpage(p2m, gpfn & ~(SUPERPAGE_PAGES-1)))
-    {
-        /* Since order may be arbitrary, we may have taken more or less
-         * than we were actually asked to; so just re-count from scratch */
-        goto recount;
+    /*
+     * Try to grab entire superpages if possible.  Since the common case is for
+     * drivers to pass back singleton pages, see if we can take the whole page
+     * back and mark the rest PoD.

xen/arch/x86/mm/p2m-pod.c recount: · line 577

Quoted 10 lines
xen/arch/x86/mm/p2m-pod.c +10 -0
+     * No need to do this though if
+     * - order >= SUPERPAGE_ORDER (the loop below will take care of this)
+     * - not all of the pages were RAM (now knowing order < SUPERPAGE_ORDER)
+     */
+    if ( steal_for_cache && order < SUPERPAGE_ORDER && (ram >> order) &&
+         p2m_pod_zero_check_superpage(p2m, gpfn & ~(SUPERPAGE_PAGES - 1)) )
+    {
+        pod += ram;
+        nonpod -= ram;
+        ram = 0;

+1 for the idea; a couple of comments:

* I think it would be clearer to use "(ram == 1 << order)" instead of "ram >> order". I understand (ram >> order) will be non-zero only if ram == 1 << order, but why make people spend brain cycles trying to figure that out?

* If we're going to assume that "ram >> order" implies "all the entries are ram", and furthermore that a positive return value implies "all ram was changed to pod", wouldn't it be better to do something like the following?

pod = 1 << order
nonpod = ram = 0

This would be more clearly correct if we change the comparison to ram == 1 << order.

* steal_for_cache may now be wrong. I realize that since now ram == 0 that all the subsequent "steal_for_cache" expressions will end up as "false" anyway, but leaving invariants in an invalid state is sort of asking for trouble.

I'd prefer you just update steal_for_cache; but if not, at least leave a comment there saying that it may be wrong and why it doesn't matter.

Thanks,
-George

#3 lore raw
Quoted 50 lines
Quoted 1 line
Quoted 1 line · On 02.11.15 at 17:29, <[email protected]> wrote:
Quoted 17 lines · Jan Beulich

Since calling the function isn't cheap, try to avoid the call when we know up front it won't help; see the code comment for details on those conditions.

xen/arch/x86/mm/p2m-pod.c +0 -1
@@ -522,7 +522,6 @@ p2m_pod_decrease_reservation(struct doma
     if ( unlikely(d->is_dying) )
         goto out_unlock;
 
-recount:
     pod = nonpod = ram = 0;
 
     /* Figure out if we need to steal some freed memory for our cache */

xen/arch/x86/mm/p2m-pod.c recount: · line 565

Quoted 4 lines
xen/arch/x86/mm/p2m-pod.c +0 -1
@@ -562,15 +561,20 @@ recount:
         goto out_entry_check;
     }
 
-    /* Try to grab entire superpages if possible.  Since the common case is 

for drivers

xen/arch/x86/mm/p2m-pod.c recount: · line 566

Quoted 1 line
xen/arch/x86/mm/p2m-pod.c +0 -1
-     * to pass back singleton pages, see if we can take the whole page back 

and mark the

xen/arch/x86/mm/p2m-pod.c recount:

Quoted 9 lines
xen/arch/x86/mm/p2m-pod.c +2 -7
-     * rest PoD. */
-    if ( steal_for_cache
-         && p2m_pod_zero_check_superpage(p2m, gpfn & ~(SUPERPAGE_PAGES-1)))
-    {
-        /* Since order may be arbitrary, we may have taken more or less
-         * than we were actually asked to; so just re-count from scratch */
-        goto recount;
+    /*
+     * Try to grab entire superpages if possible.  Since the common case is 

for

xen/arch/x86/mm/p2m-pod.c recount: · line 566

Quoted 1 line
xen/arch/x86/mm/p2m-pod.c +1 -0
+     * drivers to pass back singleton pages, see if we can take the whole 

page

xen/arch/x86/mm/p2m-pod.c recount: · line 577

Quoted 11 lines
xen/arch/x86/mm/p2m-pod.c +11 -0
+     * back and mark the rest PoD.
+     * No need to do this though if
+     * - order >= SUPERPAGE_ORDER (the loop below will take care of this)
+     * - not all of the pages were RAM (now knowing order < SUPERPAGE_ORDER)
+     */
+    if ( steal_for_cache && order < SUPERPAGE_ORDER && (ram >> order) &&
+         p2m_pod_zero_check_superpage(p2m, gpfn & ~(SUPERPAGE_PAGES - 1)) )
+    {
+        pod += ram;
+        nonpod -= ram;
+        ram = 0;
Quoted 6 lines

+1 for the idea; a couple of comments:

* I think it would be clearer to use "(ram == 1 << order)" instead of "ram >> order". I understand (ram >> order) will be non-zero only if ram == 1 << order, but why make people spend brain cycles trying to figure that out?

Because I originally thought it makes the CPU spend less cycles on the calculation. But that I think about it again, I guess I was wrong (I would have been right only when order > 0 and the compiler would be able to prove this and it would actually make use of the knowledge using the status flags from the shift instead of doing a subsequent test to get those flags set).

So - yes, will do.

Quoted 10 lines

* If we're going to assume that "ram >> order" implies "all the entries are ram", and furthermore that a positive return value implies "all ram was changed to pod", wouldn't it be better to do something like the following?

pod = 1 << order
nonpod = ram = 0

This would be more clearly correct if we change the comparison to ram == 1 << order.

Well, yes, I can do that too. Here I really tried to avoid establishing an unnecessary dependency between the if() condition and the body (i.e. with how it is, the condition could change quite a bit without the calculations getting wrong, whereas with what you want the restrictions would be more tight).

Quoted 7 lines

* steal_for_cache may now be wrong. I realize that since now ram == 0 that all the subsequent "steal_for_cache" expressions will end up as "false" anyway, but leaving invariants in an invalid state is sort of asking for trouble.

I'd prefer you just update steal_for_cache; but if not, at least leave a comment there saying that it may be wrong and why it doesn't matter.

I'll see whether updating is reasonably straightforward.

Jan

#4 lore raw
Quoted 74 lines · Jan Beulich
Quoted 50 lines
Quoted 1 line
Quoted 1 line · On 02.11.15 at 17:29, <[email protected]> wrote:
Quoted 17 lines · Jan Beulich

Since calling the function isn't cheap, try to avoid the call when we know up front it won't help; see the code comment for details on those conditions.

xen/arch/x86/mm/p2m-pod.c +0 -1
@@ -522,7 +522,6 @@ p2m_pod_decrease_reservation(struct doma
     if ( unlikely(d->is_dying) )
         goto out_unlock;
 
-recount:
     pod = nonpod = ram = 0;
 
     /* Figure out if we need to steal some freed memory for our cache */

xen/arch/x86/mm/p2m-pod.c recount: · line 565

Quoted 4 lines
xen/arch/x86/mm/p2m-pod.c +0 -1
@@ -562,15 +561,20 @@ recount:
         goto out_entry_check;
     }
 
-    /* Try to grab entire superpages if possible.  Since the common case is 

for drivers

xen/arch/x86/mm/p2m-pod.c recount: · line 566

Quoted 1 line
xen/arch/x86/mm/p2m-pod.c +0 -1
-     * to pass back singleton pages, see if we can take the whole page back 

and mark the

xen/arch/x86/mm/p2m-pod.c recount:

Quoted 9 lines
xen/arch/x86/mm/p2m-pod.c +2 -7
-     * rest PoD. */
-    if ( steal_for_cache
-         && p2m_pod_zero_check_superpage(p2m, gpfn & ~(SUPERPAGE_PAGES-1)))
-    {
-        /* Since order may be arbitrary, we may have taken more or less
-         * than we were actually asked to; so just re-count from scratch */
-        goto recount;
+    /*
+     * Try to grab entire superpages if possible.  Since the common case is 

for

xen/arch/x86/mm/p2m-pod.c recount: · line 566

Quoted 1 line
xen/arch/x86/mm/p2m-pod.c +1 -0
+     * drivers to pass back singleton pages, see if we can take the whole 

page

xen/arch/x86/mm/p2m-pod.c recount: · line 577

Quoted 11 lines
xen/arch/x86/mm/p2m-pod.c +11 -0
+     * back and mark the rest PoD.
+     * No need to do this though if
+     * - order >= SUPERPAGE_ORDER (the loop below will take care of this)
+     * - not all of the pages were RAM (now knowing order < SUPERPAGE_ORDER)
+     */
+    if ( steal_for_cache && order < SUPERPAGE_ORDER && (ram >> order) &&
+         p2m_pod_zero_check_superpage(p2m, gpfn & ~(SUPERPAGE_PAGES - 1)) )
+    {
+        pod += ram;
+        nonpod -= ram;
+        ram = 0;
Quoted 6 lines

+1 for the idea; a couple of comments:

* I think it would be clearer to use "(ram == 1 << order)" instead of "ram >> order". I understand (ram >> order) will be non-zero only if ram == 1 << order, but why make people spend brain cycles trying to figure that out?

Because I originally thought it makes the CPU spend less cycles on the calculation. But that I think about it again, I guess I was wrong (I would have been right only when order > 0 and the compiler would be able to prove this and it would actually make use of the knowledge using the status flags from the shift instead of doing a subsequent test to get those flags set).

So - yes, will do.

Quoted 8 lines

* If we're going to assume that "ram >> order" implies "all the entries are ram", and furthermore that a positive return value implies "all ram was changed to pod", wouldn't it be better to do something like the following?

pod = 1 << order
nonpod = ram = 0

Quoted 8 lines
Quoted 2 lines

This would be more clearly correct if we change the comparison to ram == 1 << order.

Well, yes, I can do that too. Here I really tried to avoid establishing an unnecessary dependency between the if() condition and the body (i.e. with how it is, the condition could change quite a bit without the calculations getting wrong, whereas with what you want the restrictions would be more tight).

Well in fact, one of the reasons I made my suggestion is because there *is* a dependency between the if() condition and the body. If you take out (order < SUPERPAGE_ORDER), then (ram >> order) isn't a correct check any more; and (for example) if order == SUPERPAGE_ORDER+1, then subtracting ram is incorrect. I wanted to make it more obvious.

Thanks,
-George

#5 lore raw
In reply to George Dunlap
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 02.11.15 at 17:29, <[email protected]> wrote:

* steal_for_cache may now be wrong. I realize that since now ram == 0 that all the subsequent "steal_for_cache" expressions will end up as "false" anyway, but leaving invariants in an invalid state is sort of asking for trouble.

I'd prefer you just update steal_for_cache; but if not, at least leave a comment there saying that it may be wrong and why it doesn't matter.

I've just done the other things, but I don't think steal_for_cache can have changed at this point: p2m_pod_cache_add() increments p2m->pod.count by the same value by which p2m_pod_zero_check_superpage() bumps p2m->pod.entry_count right after having called p2m_pod_cache_add(). I could leave a comment of ASSERT() to that effect, unless I'm overlooking something.

Jan

#6 lore raw
Quoted 9 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 02.11.15 at 17:29, <[email protected]> wrote:

* steal_for_cache may now be wrong. I realize that since now ram == 0 that all the subsequent "steal_for_cache" expressions will end up as "false" anyway, but leaving invariants in an invalid state is sort of asking for trouble.

I'd prefer you just update steal_for_cache; but if not, at least leave a comment there saying that it may be wrong and why it doesn't matter.

Quoted 7 lines

I've just done the other things, but I don't think steal_for_cache can have changed at this point: p2m_pod_cache_add() increments p2m->pod.count by the same value by which p2m_pod_zero_check_superpage() bumps p2m->pod.entry_count right after having called p2m_pod_cache_add(). I could leave a comment of ASSERT() to that effect, unless I'm overlooking something.

Ah, yes of course.

-George