[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH] eliminate code duplication with rcu_lock_domain_by_id().



note: I did only compile test.



eliminate code duplication with rcu_lock_domain_by_id().

This patch eliminates code duplication.

Signed-off-by: Isaku Yamahata <yamahata@xxxxxxxxxxxxx>

diff -r ab19284c5070 xen/arch/x86/hvm/hvm.c
--- a/xen/arch/x86/hvm/hvm.c    Wed Oct 01 14:07:17 2008 +0100
+++ b/xen/arch/x86/hvm/hvm.c    Thu Oct 02 18:55:24 2008 +0900
@@ -2339,21 +2339,9 @@
         if ( a.index >= HVM_NR_PARAMS )
             return -EINVAL;
 
-        if ( a.domid == DOMID_SELF )
-        {
-            d = rcu_lock_current_domain();
-        }
-        else
-        {
-            if ( (d = rcu_lock_domain_by_id(a.domid)) == NULL )
-                return -ESRCH;
-            if ( !IS_PRIV_FOR(current->domain, d) )
-            {
-                rc = -EPERM;
-                goto param_fail;
-            }
-        }
-
+        rc = rcu_lock_target_domain_by_id(a.domid, &d);
+        if ( rc != 0 )
+            return rc;
 
         rc = -EINVAL;
         if ( !is_hvm_domain(d) )
@@ -2521,20 +2509,9 @@
         if ( copy_from_guest(&a, arg, 1) )
             return -EFAULT;
 
-        if ( a.domid == DOMID_SELF )
-        {
-            d = rcu_lock_current_domain();
-        }
-        else
-        {
-            if ( (d = rcu_lock_domain_by_id(a.domid)) == NULL )
-                return -ESRCH;
-            if ( !IS_PRIV_FOR(current->domain, d) )
-            {
-                rc = -EPERM;
-                goto param_fail2;
-            }
-        }
+        rc = rcu_lock_target_domain_by_id(a.domid, &d);
+        if ( rc != 0 )
+            return rc;
 
         rc = -EINVAL;
         if ( !is_hvm_domain(d) )
@@ -2570,20 +2547,9 @@
         if ( copy_from_guest(&a, arg, 1) )
             return -EFAULT;
 
-        if ( a.domid == DOMID_SELF )
-        {
-            d = rcu_lock_current_domain();
-        }
-        else
-        {
-            if ( (d = rcu_lock_domain_by_id(a.domid)) == NULL )
-                return -ESRCH;
-            if ( !IS_PRIV_FOR(current->domain, d) )
-            {
-                rc = -EPERM;
-                goto param_fail3;
-            }
-        }
+        rc = rcu_lock_target_domain_by_id(a.domid, &d);
+        if ( rc != 0 )
+            return rc;
 
         rc = -EINVAL;
         if ( !is_hvm_domain(d) )
@@ -2637,20 +2603,9 @@
         if ( copy_from_guest(&a, arg, 1) )
             return -EFAULT;
 
-        if ( a.domid == DOMID_SELF )
-        {
-            d = rcu_lock_current_domain();
-        }
-        else
-        {
-            if ( (d = rcu_lock_domain_by_id(a.domid)) == NULL )
-                return -ESRCH;
-            if ( !IS_PRIV_FOR(current->domain, d) )
-            {
-                rc = -EPERM;
-                goto param_fail4;
-            }
-        }
+        rc = rcu_lock_target_domain_by_id(a.domid, &d);
+        if ( rc != 0 )
+            return rc;
 
         rc = -EINVAL;
         if ( !is_hvm_domain(d) )
diff -r ab19284c5070 xen/arch/x86/mm.c
--- a/xen/arch/x86/mm.c Wed Oct 01 14:07:17 2008 +0100
+++ b/xen/arch/x86/mm.c Thu Oct 02 18:55:24 2008 +0900
@@ -3550,6 +3550,7 @@
 long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
 {
     struct page_info *page = NULL;
+    int rc;
     switch ( op )
     {
     case XENMEM_add_to_physmap:
@@ -3561,20 +3562,9 @@
         if ( copy_from_guest(&xatp, arg, 1) )
             return -EFAULT;
 
-        if ( xatp.domid == DOMID_SELF )
-        {
-            d = rcu_lock_current_domain();
-        }
-        else
-        {
-            if ( (d = rcu_lock_domain_by_id(xatp.domid)) == NULL )
-                return -ESRCH;
-            if ( !IS_PRIV_FOR(current->domain, d) )
-            {
-                rcu_unlock_domain(d);
-                return -EPERM;
-            }
-        }
+        rc = rcu_lock_target_domain_by_id(xatp.domid, &d);
+        if ( rc != 0 )
+            return rc;
 
         if ( xsm_add_to_physmap(current->domain, d) )
         {
@@ -3661,20 +3651,9 @@
         if ( copy_from_guest(&xrfp, arg, 1) )
             return -EFAULT;
 
-        if ( xrfp.domid == DOMID_SELF )
-        {
-            d = rcu_lock_current_domain();
-        }
-        else
-        {
-            if ( (d = rcu_lock_domain_by_id(xrfp.domid)) == NULL )
-                return -ESRCH;
-            if ( !IS_PRIV_FOR(current->domain, d) )
-            {
-                rcu_unlock_domain(d);
-                return -EPERM;
-            }
-        }
+        rc = rcu_lock_target_domain_by_id(xrfp.domid, &d);
+        if ( rc != 0 )
+            return rc;
 
         if ( xsm_remove_from_physmap(current->domain, d) )
         {
@@ -3708,20 +3687,9 @@
         if ( fmap.map.nr_entries > ARRAY_SIZE(d->arch.e820) )
             return -EINVAL;
 
-        if ( fmap.domid == DOMID_SELF )
-        {
-            d = rcu_lock_current_domain();
-        }
-        else
-        {
-            if ( (d = rcu_lock_domain_by_id(fmap.domid)) == NULL )
-                return -ESRCH;
-            if ( !IS_PRIV_FOR(current->domain, d) )
-            {
-                rcu_unlock_domain(d);
-                return -EPERM;
-            }
-        }
+        rc = rcu_lock_target_domain_by_id(fmap.domid, &d);
+        if ( rc != 0 )
+            return rc;
 
         rc = xsm_domain_memory_map(d);
         if ( rc )
diff -r ab19284c5070 xen/common/domain.c
--- a/xen/common/domain.c       Wed Oct 01 14:07:17 2008 +0100
+++ b/xen/common/domain.c       Thu Oct 02 18:55:24 2008 +0900
@@ -332,6 +332,24 @@
     return NULL;
 }
 
+int rcu_lock_target_domain_by_id(domid_t dom, struct domain **d)
+{
+    if ( dom == DOMID_SELF )
+        *d = rcu_lock_current_domain();
+    else
+    {
+        *d = rcu_lock_domain_by_id(dom);
+        if ( *d == NULL )
+            return -ESRCH;
+        if ( !IS_PRIV_FOR(current->domain, *d) )
+        {
+            rcu_lock_domain(*d);
+            return -EPERM;
+        }
+    }
+
+    return 0;
+}
 
 int domain_kill(struct domain *d)
 {
diff -r ab19284c5070 xen/common/event_channel.c
--- a/xen/common/event_channel.c        Wed Oct 01 14:07:17 2008 +0100
+++ b/xen/common/event_channel.c        Thu Oct 02 18:55:24 2008 +0900
@@ -129,20 +129,9 @@
     domid_t        dom = alloc->dom;
     long           rc;
 
-    if ( dom == DOMID_SELF )
-    {
-        d = rcu_lock_current_domain();
-    }
-    else
-    {
-        if ( (d = rcu_lock_domain_by_id(dom)) == NULL )
-            return -ESRCH;
-        if ( !IS_PRIV_FOR(current->domain, d) )
-        {
-            rcu_unlock_domain(d);
-            return -EPERM;
-        }
-    }
+    rc = rcu_lock_target_domain_by_id(dom, &d);
+    if ( rc )
+        return rc;
 
     spin_lock(&d->evtchn_lock);
 
@@ -663,20 +652,9 @@
     struct evtchn   *chn;
     long             rc = 0;
 
-    if ( dom == DOMID_SELF )
-    {
-        d = rcu_lock_current_domain();
-    }
-    else
-    {
-        if ( (d = rcu_lock_domain_by_id(dom)) == NULL )
-            return -ESRCH;
-        if ( !IS_PRIV_FOR(current->domain, d) )
-        {
-            rcu_unlock_domain(d);
-            return -EPERM;
-        }
-    }
+    rc = rcu_lock_target_domain_by_id(dom, &d);
+    if ( rc )
+        return rc;
 
     spin_lock(&d->evtchn_lock);
 
@@ -824,20 +802,9 @@
     struct domain *d;
     int i, rc;
 
-    if ( dom == DOMID_SELF )
-    {
-        d = rcu_lock_current_domain();
-    }
-    else
-    {
-        if ( (d = rcu_lock_domain_by_id(dom)) == NULL )
-            return -ESRCH;
-        if ( !IS_PRIV_FOR(current->domain, d) )
-        {
-            rc = -EPERM;
-            goto out;
-        }
-    }
+    rc = rcu_lock_target_domain_by_id(dom, &d);
+    if ( rc )
+        return rc;
 
     rc = xsm_evtchn_reset(current->domain, d);
     if ( rc )
diff -r ab19284c5070 xen/common/memory.c
--- a/xen/common/memory.c       Wed Oct 01 14:07:17 2008 +0100
+++ b/xen/common/memory.c       Thu Oct 02 18:55:24 2008 +0900
@@ -222,21 +222,9 @@
          !guest_handle_subrange_okay(op.mfn_list, *progress, op.nr_gpfns-1) )
         return -EFAULT;
 
-    if ( op.domid == DOMID_SELF )
-    {
-        d = rcu_lock_current_domain();
-    }
-    else
-    {
-        if ( (d = rcu_lock_domain_by_id(op.domid)) == NULL )
-            return -ESRCH;
-        if ( !IS_PRIV_FOR(current->domain, d) )
-        {
-            rcu_unlock_domain(d);
-            return -EPERM;
-        }
-    }
-
+    rc = rcu_lock_target_domain_by_id(op.domid, &d);
+    if ( rc )
+        return rc;
 
     if ( !paging_mode_translate(d) )
     {
@@ -595,20 +583,9 @@
         if ( copy_from_guest(&domid, arg, 1) )
             return -EFAULT;
 
-        if ( likely(domid == DOMID_SELF) )
-        {
-            d = rcu_lock_current_domain();
-        }
-        else
-        {
-            if ( (d = rcu_lock_domain_by_id(domid)) == NULL )
-                return -ESRCH;
-            if ( !IS_PRIV_FOR(current->domain, d) )
-            {
-                rcu_unlock_domain(d);
-                return -EPERM;
-            }
-        }
+        rc = rcu_lock_target_domain_by_id(domid, &d);
+        if ( rc )
+            return rc;
 
         rc = xsm_memory_stat_reservation(current->domain, d);
         if ( rc )
diff -r ab19284c5070 xen/include/xen/sched.h
--- a/xen/include/xen/sched.h   Wed Oct 01 14:07:17 2008 +0100
+++ b/xen/include/xen/sched.h   Thu Oct 02 18:55:24 2008 +0900
@@ -363,6 +363,7 @@
  * The returned domain reference must be discarded using rcu_unlock_domain().
  */
 struct domain *rcu_lock_domain_by_id(domid_t dom);
+int rcu_lock_target_domain_by_id(domid_t dom, struct domain **d);
 
 /* Finish a RCU critical region started by rcu_lock_domain_by_id(). */
 static inline void rcu_unlock_domain(struct domain *d)

-- 
yamahata

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.