Skip to contentSkip to conversation

x86/memshr: fix preemption in relinquish_shared_pages()

PATCH Superseded Jan Beulich 4 participants 6 messages Dec 17, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/memshr: fix preemption in relinquish_shared_pages()

For one, should hypercall_preempt_check() return false the first time it gets called, it would never have got called again (because count, being checked for equality, didn't get reset to zero).

And then, if there were a huge range of unshared pages, with count not getting incremented at all in that case there would also not be any preemption.

Fix this by using a biased increment (ratio 1:16 for unshared vs shared pages), and check for certain bits in count to be clear rather than for a specific value (the alternative would be to do a greater-or-equal comparison and flush the value to zero in case of a "false" return from hypercall_preempt_check()).

xen/arch/x86/mm/mem_sharing.c +5 -3
@@ -1290,11 +1290,13 @@ int relinquish_shared_pages(struct domai
             set_rc = p2m->set_entry(p2m, gfn, _mfn(0), PAGE_ORDER_4K,
                                     p2m_invalid, p2m_access_rwx);
             ASSERT(set_rc != 0);
-            count++;
+            count += 0x10;
         }
+        else
+            ++count;
 
-        /* Preempt every 2MiB. Arbitrary */
-        if ( (count == 512) && hypercall_preempt_check() )
+        /* Preempt every 2MiB (shared) or 32MiB (unshared) - arbitrary. */
+        if ( !(count & 0x1ff0) && hypercall_preempt_check() )
         {
             p2m->next_shared_gfn_to_relinquish = gfn + 1;
             rc = -EAGAIN;

x86/memshr: fix preemption in relinquish_shared_pages()

For one, should hypercall_preempt_check() return false the first time it gets called, it would never have got called again (because count, being checked for equality, didn't get reset to zero).

And then, if there were a huge range of unshared pages, with count not getting incremented at all in that case there would also not be any preemption.

Fix this by using a biased increment (ratio 1:16 for unshared vs shared pages), and check for certain bits in count to be clear rather than for a specific value (the alternative would be to do a greater-or-equal comparison and flush the value to zero in case of a "false" return from hypercall_preempt_check()).

xen/arch/x86/mm/mem_sharing.c +5 -3
@@ -1290,11 +1290,13 @@ int relinquish_shared_pages(struct domai
             set_rc = p2m->set_entry(p2m, gfn, _mfn(0), PAGE_ORDER_4K,
                                     p2m_invalid, p2m_access_rwx);
             ASSERT(set_rc != 0);
-            count++;
+            count += 0x10;
         }
+        else
+            ++count;
 
-        /* Preempt every 2MiB. Arbitrary */
-        if ( (count == 512) && hypercall_preempt_check() )
+        /* Preempt every 2MiB (shared) or 32MiB (unshared) - arbitrary. */
+        if ( !(count & 0x1ff0) && hypercall_preempt_check() )
         {
             p2m->next_shared_gfn_to_relinquish = gfn + 1;
             rc = -EAGAIN;

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

Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Jan Beulich

For one, should hypercall_preempt_check() return false the first time it gets called, it would never have got called again (because count, being checked for equality, didn't get reset to zero).

And then, if there were a huge range of unshared pages, with count not getting incremented at all in that case there would also not be any preemption.

Quoted 7 lines

Fix this by using a biased increment (ratio 1:16 for unshared vs shared pages), and check for certain bits in count to be clear rather than for a specific value (the alternative would be to do a greater-or-equal comparison and flush the value to zero in case of a "false" return from hypercall_preempt_check()).

Quoted 25 lines
xen/arch/x86/mm/mem_sharing.c +5 -3
@@ -1290,11 +1290,13 @@ int relinquish_shared_pages(struct domai
             set_rc = p2m->set_entry(p2m, gfn, _mfn(0), PAGE_ORDER_4K,
                                     p2m_invalid, p2m_access_rwx);
             ASSERT(set_rc != 0);
-            count++;
+            count += 0x10;
         }
+        else
+            ++count;
 
-        /* Preempt every 2MiB. Arbitrary */
-        if ( (count == 512) && hypercall_preempt_check() )
+        /* Preempt every 2MiB (shared) or 32MiB (unshared) - arbitrary. */
+        if ( !(count & 0x1ff0) && hypercall_preempt_check() )
         {
             p2m->next_shared_gfn_to_relinquish = gfn + 1;
             rc = -EAGAIN;
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
In reply to Jan Beulich
Quoted 22 lines · Jan Beulich

For one, should hypercall_preempt_check() return false the first time it gets called, it would never have got called again (because count, being checked for equality, didn't get reset to zero).

And then, if there were a huge range of unshared pages, with count not getting incremented at all in that case there would also not be any preemption.

Fix this by using a biased increment (ratio 1:16 for unshared vs shared pages), and check for certain bits in count to be clear rather than for a specific value (the alternative would be to do a greater-or-equal comparison and flush the value to zero in case of a "false" return from hypercall_preempt_check()).

xen/arch/x86/mm/mem_sharing.c +0 -0
@@ -1290,11 +1290,13 @@ int relinquish_shared_pages(struct domai
             set_rc = p2m->set_entry(p2m, gfn, _mfn(0), PAGE_ORDER_4K,
                                     p2m_invalid, p2m_access_rwx);
             ASSERT(set_rc != 0);

xen/arch/x86/mm/mem_sharing.c int relinquish_shared_pages(struct domai · line 1299

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +5 -3
-            count++;
+            count += 0x10;
         }
+        else
+            ++count;
 
-        /* Preempt every 2MiB. Arbitrary */
-        if ( (count == 512) && hypercall_preempt_check() )
+        /* Preempt every 2MiB (shared) or 32MiB (unshared) - arbitrary. */
+        if ( !(count & 0x1ff0) && hypercall_preempt_check() )

This will check after each of for the first 15 gfns unless one of them is shared (and potentially again after each rollover). I think it would be clearer just to check for > 2000 and reset to 0.

Cheers,

Tim.

#3 lore raw

PATCH v2 1/1 x86/memshr: fix preemption in relinquish_shared_pages()

For one, should hypercall_preempt_check() return false the first time it gets called, it would never have got called again (because count, being checked for equality, didn't get reset to zero).

And then, if there were a huge range of unshared pages, with count not getting incremented at all in that case there would also not be any preemption.

Fix this by using a biased increment (ratio 1:16 for unshared vs shared pages), and flushing the count to zero in case of a "false" return from hypercall_preempt_check().

---
v2: Adjust continuation detection as per Tim's request.

xen/arch/x86/mm/mem_sharing.c +12 -6
@@ -1290,15 +1290,21 @@ int relinquish_shared_pages(struct domai
             set_rc = p2m->set_entry(p2m, gfn, _mfn(0), PAGE_ORDER_4K,
                                     p2m_invalid, p2m_access_rwx);
             ASSERT(set_rc != 0);
-            count++;
+            count += 0x10;
         }
+        else
+            ++count;
 
-        /* Preempt every 2MiB. Arbitrary */
-        if ( (count == 512) && hypercall_preempt_check() )
+        /* Preempt every 2MiB (shared) or 32MiB (unshared) - arbitrary. */
+        if ( count >= 0x2000 )
         {
-            p2m->next_shared_gfn_to_relinquish = gfn + 1;
-            rc = -EAGAIN;
-            break;
+            if ( hypercall_preempt_check() )
+            {
+                p2m->next_shared_gfn_to_relinquish = gfn + 1;
+                rc = -EAGAIN;
+                break;
+            }
+            count = 0;
         }
     }
 

x86/memshr: fix preemption in relinquish_shared_pages()

For one, should hypercall_preempt_check() return false the first time it gets called, it would never have got called again (because count, being checked for equality, didn't get reset to zero).

And then, if there were a huge range of unshared pages, with count not getting incremented at all in that case there would also not be any preemption.

Fix this by using a biased increment (ratio 1:16 for unshared vs shared pages), and flushing the count to zero in case of a "false" return from hypercall_preempt_check().

---
v2: Adjust continuation detection as per Tim's request.

xen/arch/x86/mm/mem_sharing.c +12 -6
@@ -1290,15 +1290,21 @@ int relinquish_shared_pages(struct domai
             set_rc = p2m->set_entry(p2m, gfn, _mfn(0), PAGE_ORDER_4K,
                                     p2m_invalid, p2m_access_rwx);
             ASSERT(set_rc != 0);
-            count++;
+            count += 0x10;
         }
+        else
+            ++count;
 
-        /* Preempt every 2MiB. Arbitrary */
-        if ( (count == 512) && hypercall_preempt_check() )
+        /* Preempt every 2MiB (shared) or 32MiB (unshared) - arbitrary. */
+        if ( count >= 0x2000 )
         {
-            p2m->next_shared_gfn_to_relinquish = gfn + 1;
-            rc = -EAGAIN;
-            break;
+            if ( hypercall_preempt_check() )
+            {
+                p2m->next_shared_gfn_to_relinquish = gfn + 1;
+                rc = -EAGAIN;
+                break;
+            }
+            count = 0;
         }
     }
 

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

Reviewed-by on v2 1/1
#4 lore raw
Quoted 8 lines · Jan Beulich

For one, should hypercall_preempt_check() return false the first time it gets called, it would never have got called again (because count, being checked for equality, didn't get reset to zero).

And then, if there were a huge range of unshared pages, with count not getting incremented at all in that case there would also not be any preemption.

Quoted 5 lines

Fix this by using a biased increment (ratio 1:16 for unshared vs shared pages), and flushing the count to zero in case of a "false" return from hypercall_preempt_check().

Quoted 33 lines

---
v2: Adjust continuation detection as per Tim's request.

xen/arch/x86/mm/mem_sharing.c +12 -6
@@ -1290,15 +1290,21 @@ int relinquish_shared_pages(struct domai
             set_rc = p2m->set_entry(p2m, gfn, _mfn(0), PAGE_ORDER_4K,
                                     p2m_invalid, p2m_access_rwx);
             ASSERT(set_rc != 0);
-            count++;
+            count += 0x10;
         }
+        else
+            ++count;
 
-        /* Preempt every 2MiB. Arbitrary */
-        if ( (count == 512) && hypercall_preempt_check() )
+        /* Preempt every 2MiB (shared) or 32MiB (unshared) - arbitrary. */
+        if ( count >= 0x2000 )
         {
-            p2m->next_shared_gfn_to_relinquish = gfn + 1;
-            rc = -EAGAIN;
-            break;
+            if ( hypercall_preempt_check() )
+            {
+                p2m->next_shared_gfn_to_relinquish = gfn + 1;
+                rc = -EAGAIN;
+                break;
+            }
+            count = 0;
         }
     }
 
Acked-by Andres Lagar-Cavilla
#5 lore raw
Quoted 50 lines · Tim Deegan
Quoted 8 lines · Jan Beulich

For one, should hypercall_preempt_check() return false the first time it gets called, it would never have got called again (because count, being checked for equality, didn't get reset to zero).

And then, if there were a huge range of unshared pages, with count not getting incremented at all in that case there would also not be any preemption.

Quoted 5 lines

Fix this by using a biased increment (ratio 1:16 for unshared vs shared pages), and flushing the count to zero in case of a "false" return from hypercall_preempt_check().

Quoted 32 lines

---
v2: Adjust continuation detection as per Tim's request.

xen/arch/x86/mm/mem_sharing.c +12 -6
@@ -1290,15 +1290,21 @@ int relinquish_shared_pages(struct domai
            set_rc = p2m->set_entry(p2m, gfn, _mfn(0), PAGE_ORDER_4K,
                                    p2m_invalid, p2m_access_rwx);
            ASSERT(set_rc != 0);
-            count++;
+            count += 0x10;
        }
+        else
+            ++count;
 
-        /* Preempt every 2MiB. Arbitrary */
-        if ( (count == 512) && hypercall_preempt_check() )
+        /* Preempt every 2MiB (shared) or 32MiB (unshared) - arbitrary. */
+        if ( count >= 0x2000 )
        {
-            p2m->next_shared_gfn_to_relinquish = gfn + 1;
-            rc = -EAGAIN;
-            break;
+            if ( hypercall_preempt_check() )
+            {
+                p2m->next_shared_gfn_to_relinquish = gfn + 1;
+                rc = -EAGAIN;
+                break;
+            }
+            count = 0;
        }
    }