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

[Xen-changelog] [xen-unstable] Eliminate code duplication with rcu_lock_domain_by_id().



# HG changeset patch
# User Keir Fraser <keir.fraser@xxxxxxxxxx>
# Date 1222943976 -3600
# Node ID 9a7b46546e05ca452cfcc43f6bd4515b3ee710dc
# Parent  8dc05a2b3beb62738cceebffa1371bcf654bff52
Eliminate code duplication with rcu_lock_domain_by_id().

Signed-off-by: Isaku Yamahata <yamahata@xxxxxxxxxxxxx>
---
 xen/arch/x86/hvm/hvm.c     |   69 +++++++--------------------------------------
 xen/arch/x86/mm.c          |   53 +++++++---------------------------
 xen/common/domain.c        |   19 ++++++++++++
 xen/common/event_channel.c |   51 +++++----------------------------
 xen/common/memory.c        |   35 +++-------------------
 xen/include/xen/sched.h    |    7 ++++
 6 files changed, 64 insertions(+), 170 deletions(-)

diff -r 8dc05a2b3beb -r 9a7b46546e05 xen/arch/x86/hvm/hvm.c
--- a/xen/arch/x86/hvm/hvm.c    Thu Oct 02 11:32:08 2008 +0100
+++ b/xen/arch/x86/hvm/hvm.c    Thu Oct 02 11:39:36 2008 +0100
@@ -2339,21 +2339,9 @@ long do_hvm_op(unsigned long op, XEN_GUE
         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 @@ long do_hvm_op(unsigned long op, XEN_GUE
         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 @@ long do_hvm_op(unsigned long op, XEN_GUE
         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 @@ long do_hvm_op(unsigned long op, XEN_GUE
         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 8dc05a2b3beb -r 9a7b46546e05 xen/arch/x86/mm.c
--- a/xen/arch/x86/mm.c Thu Oct 02 11:32:08 2008 +0100
+++ b/xen/arch/x86/mm.c Thu Oct 02 11:39:36 2008 +0100
@@ -3550,6 +3550,8 @@ long arch_memory_op(int op, XEN_GUEST_HA
 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 +3563,9 @@ long arch_memory_op(int op, XEN_GUEST_HA
         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 +3652,9 @@ long arch_memory_op(int op, XEN_GUEST_HA
         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 +3688,9 @@ long arch_memory_op(int op, XEN_GUEST_HA
         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 8dc05a2b3beb -r 9a7b46546e05 xen/common/domain.c
--- a/xen/common/domain.c       Thu Oct 02 11:32:08 2008 +0100
+++ b/xen/common/domain.c       Thu Oct 02 11:39:36 2008 +0100
@@ -332,6 +332,25 @@ struct domain *rcu_lock_domain_by_id(dom
     return NULL;
 }
 
+int rcu_lock_target_domain_by_id(domid_t dom, struct domain **d)
+{
+    if ( dom == DOMID_SELF )
+    {
+        *d = rcu_lock_current_domain();
+        return 0;
+    }
+
+    if ( (*d = rcu_lock_domain_by_id(dom)) == NULL )
+        return -ESRCH;
+
+    if ( !IS_PRIV_FOR(current->domain, *d) )
+    {
+        rcu_unlock_domain(*d);
+        return -EPERM;
+    }
+
+    return 0;
+}
 
 int domain_kill(struct domain *d)
 {
diff -r 8dc05a2b3beb -r 9a7b46546e05 xen/common/event_channel.c
--- a/xen/common/event_channel.c        Thu Oct 02 11:32:08 2008 +0100
+++ b/xen/common/event_channel.c        Thu Oct 02 11:39:36 2008 +0100
@@ -129,20 +129,9 @@ static long evtchn_alloc_unbound(evtchn_
     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 @@ static long evtchn_status(evtchn_status_
     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 @@ static long evtchn_reset(evtchn_reset_t 
     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 8dc05a2b3beb -r 9a7b46546e05 xen/common/memory.c
--- a/xen/common/memory.c       Thu Oct 02 11:32:08 2008 +0100
+++ b/xen/common/memory.c       Thu Oct 02 11:39:36 2008 +0100
@@ -222,21 +222,9 @@ static long translate_gpfn_list(
          !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 @@ long do_memory_op(unsigned long cmd, XEN
         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 8dc05a2b3beb -r 9a7b46546e05 xen/include/xen/sched.h
--- a/xen/include/xen/sched.h   Thu Oct 02 11:32:08 2008 +0100
+++ b/xen/include/xen/sched.h   Thu Oct 02 11:39:36 2008 +0100
@@ -364,6 +364,13 @@ int construct_dom0(
  */
 struct domain *rcu_lock_domain_by_id(domid_t dom);
 
+/*
+ * As above function, but accounts for current domain context:
+ *  - Translates target DOMID_SELF into caller's domain id; and
+ *  - Checks that caller has permission to act on the target domain.
+ */
+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)
 {

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


 


Rackspace

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