References: bnc#678871

# HG changeset patch
# User George Dunlap <george.dunlap@eu.citrix.com>
# Date 1295274253 0
# Node ID 97ab84aca65cdcbce2ddccc51629fb24adb056cf
# Parent  d1631540bcc4d369d7e7ec1d87e54e1a8f5d5f78
PoD: Allow pod_set_cache_target hypercall to be preempted

For very large VMs, setting the cache target can take long enough that
dom0 complains of soft lockups.  Allow the hypercall to be preempted.

Signed-off-by: George Dunlap <george.dunlap@eu.citrix.com>
Acked-by: Tim Deegan <Tim.Deegan@citrix.com>

Index: xen-4.0.2-testing/xen/arch/x86/domain.c
===================================================================
--- xen-4.0.2-testing.orig/xen/arch/x86/domain.c
+++ xen-4.0.2-testing/xen/arch/x86/domain.c
@@ -1709,8 +1709,8 @@ int hypercall_xlat_continuation(unsigned
     unsigned long nval = 0;
     va_list args;
 
-    BUG_ON(*id > 5);
-    BUG_ON(mask & (1U << *id));
+    BUG_ON(id && *id > 5);
+    BUG_ON(id && (mask & (1U << *id)));
 
     va_start(args, mask);
 
Index: xen-4.0.2-testing/xen/arch/x86/mm.c
===================================================================
--- xen-4.0.2-testing.orig/xen/arch/x86/mm.c
+++ xen-4.0.2-testing/xen/arch/x86/mm.c
@@ -4547,14 +4547,22 @@ long arch_memory_op(int op, XEN_GUEST_HA
             rc = p2m_pod_set_mem_target(d, target.target_pages);
         }
 
-        target.tot_pages       = d->tot_pages;
-        target.pod_cache_pages = d->arch.p2m->pod.count;
-        target.pod_entries     = d->arch.p2m->pod.entry_count;
-
-        if ( copy_to_guest(arg, &target, 1) )
+        if ( rc == -EAGAIN )
+        {
+            rc = hypercall_create_continuation(
+                __HYPERVISOR_memory_op, "lh", op, arg);
+        }
+        else if ( rc >= 0 )
         {
-            rc= -EFAULT;
-            goto pod_target_out_unlock;
+            target.tot_pages       = d->tot_pages;
+            target.pod_cache_pages = d->arch.p2m->pod.count;
+            target.pod_entries     = d->arch.p2m->pod.entry_count;
+
+            if ( copy_to_guest(arg, &target, 1) )
+            {
+                rc= -EFAULT;
+                goto pod_target_out_unlock;
+            }
         }
         
     pod_target_out_unlock:
Index: xen-4.0.2-testing/xen/arch/x86/mm/p2m.c
===================================================================
--- xen-4.0.2-testing.orig/xen/arch/x86/mm/p2m.c
+++ xen-4.0.2-testing/xen/arch/x86/mm/p2m.c
@@ -383,7 +383,7 @@ static struct page_info * p2m_pod_cache_
 
 /* Set the size of the cache, allocating or freeing as necessary. */
 static int
-p2m_pod_set_cache_target(struct domain *d, unsigned long pod_target)
+p2m_pod_set_cache_target(struct domain *d, unsigned long pod_target, int preemptible)
 {
     struct p2m_domain *p2md = d->arch.p2m;
     int ret = 0;
@@ -416,6 +416,12 @@ p2m_pod_set_cache_target(struct domain *
         }
 
         p2m_pod_cache_add(d, page, order);
+
+        if ( hypercall_preempt_check() && preemptible )
+        {
+            ret = -EAGAIN;
+            goto out;
+        }
     }
 
     /* Decreasing the target */
@@ -460,6 +466,12 @@ p2m_pod_set_cache_target(struct domain *
                 put_page(page+i);
 
             put_page(page+i);
+
+            if ( hypercall_preempt_check() && preemptible )
+            {
+                ret = -EAGAIN;
+                goto out;
+            }
         }
     }
 
@@ -537,7 +549,7 @@ p2m_pod_set_mem_target(struct domain *d,
 
     ASSERT( pod_target >= p2md->pod.count );
 
-    ret = p2m_pod_set_cache_target(d, pod_target);
+    ret = p2m_pod_set_cache_target(d, pod_target, 1/*preemptible*/);
 
 out:
     p2m_unlock(p2md);
@@ -701,7 +713,7 @@ out_entry_check:
     /* If we've reduced our "liabilities" beyond our "assets", free some */
     if ( p2md->pod.entry_count < p2md->pod.count )
     {
-        p2m_pod_set_cache_target(d, p2md->pod.entry_count);
+        p2m_pod_set_cache_target(d, p2md->pod.entry_count, 0/*can't preempt*/);
     }
 
 out_unlock:
Index: xen-4.0.2-testing/xen/arch/x86/x86_64/compat/mm.c
===================================================================
--- xen-4.0.2-testing.orig/xen/arch/x86/x86_64/compat/mm.c
+++ xen-4.0.2-testing/xen/arch/x86/x86_64/compat/mm.c
@@ -127,6 +127,9 @@ int compat_arch_memory_op(int op, XEN_GU
         if ( rc < 0 )
             break;
 
+        if ( rc == __HYPERVISOR_memory_op )
+            hypercall_xlat_continuation(NULL, 0x2, nat, arg);
+
         XLAT_pod_target(&cmp, nat);
 
         if ( copy_to_guest(arg, &cmp, 1) )
