ia64/xen-unstable

changeset 18574:9a7b46546e05

Eliminate code duplication with rcu_lock_domain_by_id().

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Oct 02 11:39:36 2008 +0100 (2008-10-02)
parents 8dc05a2b3beb
children 80365bc6df42
files xen/arch/x86/hvm/hvm.c xen/arch/x86/mm.c xen/common/domain.c xen/common/event_channel.c xen/common/memory.c xen/include/xen/sched.h
line diff
     1.1 --- a/xen/arch/x86/hvm/hvm.c	Thu Oct 02 11:32:08 2008 +0100
     1.2 +++ b/xen/arch/x86/hvm/hvm.c	Thu Oct 02 11:39:36 2008 +0100
     1.3 @@ -2339,21 +2339,9 @@ long do_hvm_op(unsigned long op, XEN_GUE
     1.4          if ( a.index >= HVM_NR_PARAMS )
     1.5              return -EINVAL;
     1.6  
     1.7 -        if ( a.domid == DOMID_SELF )
     1.8 -        {
     1.9 -            d = rcu_lock_current_domain();
    1.10 -        }
    1.11 -        else
    1.12 -        {
    1.13 -            if ( (d = rcu_lock_domain_by_id(a.domid)) == NULL )
    1.14 -                return -ESRCH;
    1.15 -            if ( !IS_PRIV_FOR(current->domain, d) )
    1.16 -            {
    1.17 -                rc = -EPERM;
    1.18 -                goto param_fail;
    1.19 -            }
    1.20 -        }
    1.21 -
    1.22 +        rc = rcu_lock_target_domain_by_id(a.domid, &d);
    1.23 +        if ( rc != 0 )
    1.24 +            return rc;
    1.25  
    1.26          rc = -EINVAL;
    1.27          if ( !is_hvm_domain(d) )
    1.28 @@ -2521,20 +2509,9 @@ long do_hvm_op(unsigned long op, XEN_GUE
    1.29          if ( copy_from_guest(&a, arg, 1) )
    1.30              return -EFAULT;
    1.31  
    1.32 -        if ( a.domid == DOMID_SELF )
    1.33 -        {
    1.34 -            d = rcu_lock_current_domain();
    1.35 -        }
    1.36 -        else
    1.37 -        {
    1.38 -            if ( (d = rcu_lock_domain_by_id(a.domid)) == NULL )
    1.39 -                return -ESRCH;
    1.40 -            if ( !IS_PRIV_FOR(current->domain, d) )
    1.41 -            {
    1.42 -                rc = -EPERM;
    1.43 -                goto param_fail2;
    1.44 -            }
    1.45 -        }
    1.46 +        rc = rcu_lock_target_domain_by_id(a.domid, &d);
    1.47 +        if ( rc != 0 )
    1.48 +            return rc;
    1.49  
    1.50          rc = -EINVAL;
    1.51          if ( !is_hvm_domain(d) )
    1.52 @@ -2570,20 +2547,9 @@ long do_hvm_op(unsigned long op, XEN_GUE
    1.53          if ( copy_from_guest(&a, arg, 1) )
    1.54              return -EFAULT;
    1.55  
    1.56 -        if ( a.domid == DOMID_SELF )
    1.57 -        {
    1.58 -            d = rcu_lock_current_domain();
    1.59 -        }
    1.60 -        else
    1.61 -        {
    1.62 -            if ( (d = rcu_lock_domain_by_id(a.domid)) == NULL )
    1.63 -                return -ESRCH;
    1.64 -            if ( !IS_PRIV_FOR(current->domain, d) )
    1.65 -            {
    1.66 -                rc = -EPERM;
    1.67 -                goto param_fail3;
    1.68 -            }
    1.69 -        }
    1.70 +        rc = rcu_lock_target_domain_by_id(a.domid, &d);
    1.71 +        if ( rc != 0 )
    1.72 +            return rc;
    1.73  
    1.74          rc = -EINVAL;
    1.75          if ( !is_hvm_domain(d) )
    1.76 @@ -2637,20 +2603,9 @@ long do_hvm_op(unsigned long op, XEN_GUE
    1.77          if ( copy_from_guest(&a, arg, 1) )
    1.78              return -EFAULT;
    1.79  
    1.80 -        if ( a.domid == DOMID_SELF )
    1.81 -        {
    1.82 -            d = rcu_lock_current_domain();
    1.83 -        }
    1.84 -        else
    1.85 -        {
    1.86 -            if ( (d = rcu_lock_domain_by_id(a.domid)) == NULL )
    1.87 -                return -ESRCH;
    1.88 -            if ( !IS_PRIV_FOR(current->domain, d) )
    1.89 -            {
    1.90 -                rc = -EPERM;
    1.91 -                goto param_fail4;
    1.92 -            }
    1.93 -        }
    1.94 +        rc = rcu_lock_target_domain_by_id(a.domid, &d);
    1.95 +        if ( rc != 0 )
    1.96 +            return rc;
    1.97  
    1.98          rc = -EINVAL;
    1.99          if ( !is_hvm_domain(d) )
     2.1 --- a/xen/arch/x86/mm.c	Thu Oct 02 11:32:08 2008 +0100
     2.2 +++ b/xen/arch/x86/mm.c	Thu Oct 02 11:39:36 2008 +0100
     2.3 @@ -3550,6 +3550,8 @@ DEFINE_XEN_GUEST_HANDLE(e820entry_t);
     2.4  long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
     2.5  {
     2.6      struct page_info *page = NULL;
     2.7 +    int rc;
     2.8 +
     2.9      switch ( op )
    2.10      {
    2.11      case XENMEM_add_to_physmap:
    2.12 @@ -3561,20 +3563,9 @@ long arch_memory_op(int op, XEN_GUEST_HA
    2.13          if ( copy_from_guest(&xatp, arg, 1) )
    2.14              return -EFAULT;
    2.15  
    2.16 -        if ( xatp.domid == DOMID_SELF )
    2.17 -        {
    2.18 -            d = rcu_lock_current_domain();
    2.19 -        }
    2.20 -        else
    2.21 -        {
    2.22 -            if ( (d = rcu_lock_domain_by_id(xatp.domid)) == NULL )
    2.23 -                return -ESRCH;
    2.24 -            if ( !IS_PRIV_FOR(current->domain, d) )
    2.25 -            {
    2.26 -                rcu_unlock_domain(d);
    2.27 -                return -EPERM;
    2.28 -            }
    2.29 -        }
    2.30 +        rc = rcu_lock_target_domain_by_id(xatp.domid, &d);
    2.31 +        if ( rc != 0 )
    2.32 +            return rc;
    2.33  
    2.34          if ( xsm_add_to_physmap(current->domain, d) )
    2.35          {
    2.36 @@ -3661,20 +3652,9 @@ long arch_memory_op(int op, XEN_GUEST_HA
    2.37          if ( copy_from_guest(&xrfp, arg, 1) )
    2.38              return -EFAULT;
    2.39  
    2.40 -        if ( xrfp.domid == DOMID_SELF )
    2.41 -        {
    2.42 -            d = rcu_lock_current_domain();
    2.43 -        }
    2.44 -        else
    2.45 -        {
    2.46 -            if ( (d = rcu_lock_domain_by_id(xrfp.domid)) == NULL )
    2.47 -                return -ESRCH;
    2.48 -            if ( !IS_PRIV_FOR(current->domain, d) )
    2.49 -            {
    2.50 -                rcu_unlock_domain(d);
    2.51 -                return -EPERM;
    2.52 -            }
    2.53 -        }
    2.54 +        rc = rcu_lock_target_domain_by_id(xrfp.domid, &d);
    2.55 +        if ( rc != 0 )
    2.56 +            return rc;
    2.57  
    2.58          if ( xsm_remove_from_physmap(current->domain, d) )
    2.59          {
    2.60 @@ -3708,20 +3688,9 @@ long arch_memory_op(int op, XEN_GUEST_HA
    2.61          if ( fmap.map.nr_entries > ARRAY_SIZE(d->arch.e820) )
    2.62              return -EINVAL;
    2.63  
    2.64 -        if ( fmap.domid == DOMID_SELF )
    2.65 -        {
    2.66 -            d = rcu_lock_current_domain();
    2.67 -        }
    2.68 -        else
    2.69 -        {
    2.70 -            if ( (d = rcu_lock_domain_by_id(fmap.domid)) == NULL )
    2.71 -                return -ESRCH;
    2.72 -            if ( !IS_PRIV_FOR(current->domain, d) )
    2.73 -            {
    2.74 -                rcu_unlock_domain(d);
    2.75 -                return -EPERM;
    2.76 -            }
    2.77 -        }
    2.78 +        rc = rcu_lock_target_domain_by_id(fmap.domid, &d);
    2.79 +        if ( rc != 0 )
    2.80 +            return rc;
    2.81  
    2.82          rc = xsm_domain_memory_map(d);
    2.83          if ( rc )
     3.1 --- a/xen/common/domain.c	Thu Oct 02 11:32:08 2008 +0100
     3.2 +++ b/xen/common/domain.c	Thu Oct 02 11:39:36 2008 +0100
     3.3 @@ -332,6 +332,25 @@ struct domain *rcu_lock_domain_by_id(dom
     3.4      return NULL;
     3.5  }
     3.6  
     3.7 +int rcu_lock_target_domain_by_id(domid_t dom, struct domain **d)
     3.8 +{
     3.9 +    if ( dom == DOMID_SELF )
    3.10 +    {
    3.11 +        *d = rcu_lock_current_domain();
    3.12 +        return 0;
    3.13 +    }
    3.14 +
    3.15 +    if ( (*d = rcu_lock_domain_by_id(dom)) == NULL )
    3.16 +        return -ESRCH;
    3.17 +
    3.18 +    if ( !IS_PRIV_FOR(current->domain, *d) )
    3.19 +    {
    3.20 +        rcu_unlock_domain(*d);
    3.21 +        return -EPERM;
    3.22 +    }
    3.23 +
    3.24 +    return 0;
    3.25 +}
    3.26  
    3.27  int domain_kill(struct domain *d)
    3.28  {
     4.1 --- a/xen/common/event_channel.c	Thu Oct 02 11:32:08 2008 +0100
     4.2 +++ b/xen/common/event_channel.c	Thu Oct 02 11:39:36 2008 +0100
     4.3 @@ -129,20 +129,9 @@ static long evtchn_alloc_unbound(evtchn_
     4.4      domid_t        dom = alloc->dom;
     4.5      long           rc;
     4.6  
     4.7 -    if ( dom == DOMID_SELF )
     4.8 -    {
     4.9 -        d = rcu_lock_current_domain();
    4.10 -    }
    4.11 -    else
    4.12 -    {
    4.13 -        if ( (d = rcu_lock_domain_by_id(dom)) == NULL )
    4.14 -            return -ESRCH;
    4.15 -        if ( !IS_PRIV_FOR(current->domain, d) )
    4.16 -        {
    4.17 -            rcu_unlock_domain(d);
    4.18 -            return -EPERM;
    4.19 -        }
    4.20 -    }
    4.21 +    rc = rcu_lock_target_domain_by_id(dom, &d);
    4.22 +    if ( rc )
    4.23 +        return rc;
    4.24  
    4.25      spin_lock(&d->evtchn_lock);
    4.26  
    4.27 @@ -663,20 +652,9 @@ static long evtchn_status(evtchn_status_
    4.28      struct evtchn   *chn;
    4.29      long             rc = 0;
    4.30  
    4.31 -    if ( dom == DOMID_SELF )
    4.32 -    {
    4.33 -        d = rcu_lock_current_domain();
    4.34 -    }
    4.35 -    else
    4.36 -    {
    4.37 -        if ( (d = rcu_lock_domain_by_id(dom)) == NULL )
    4.38 -            return -ESRCH;
    4.39 -        if ( !IS_PRIV_FOR(current->domain, d) )
    4.40 -        {
    4.41 -            rcu_unlock_domain(d);
    4.42 -            return -EPERM;
    4.43 -        }
    4.44 -    }
    4.45 +    rc = rcu_lock_target_domain_by_id(dom, &d);
    4.46 +    if ( rc )
    4.47 +        return rc;
    4.48  
    4.49      spin_lock(&d->evtchn_lock);
    4.50  
    4.51 @@ -824,20 +802,9 @@ static long evtchn_reset(evtchn_reset_t 
    4.52      struct domain *d;
    4.53      int i, rc;
    4.54  
    4.55 -    if ( dom == DOMID_SELF )
    4.56 -    {
    4.57 -        d = rcu_lock_current_domain();
    4.58 -    }
    4.59 -    else
    4.60 -    {
    4.61 -        if ( (d = rcu_lock_domain_by_id(dom)) == NULL )
    4.62 -            return -ESRCH;
    4.63 -        if ( !IS_PRIV_FOR(current->domain, d) )
    4.64 -        {
    4.65 -            rc = -EPERM;
    4.66 -            goto out;
    4.67 -        }
    4.68 -    }
    4.69 +    rc = rcu_lock_target_domain_by_id(dom, &d);
    4.70 +    if ( rc )
    4.71 +        return rc;
    4.72  
    4.73      rc = xsm_evtchn_reset(current->domain, d);
    4.74      if ( rc )
     5.1 --- a/xen/common/memory.c	Thu Oct 02 11:32:08 2008 +0100
     5.2 +++ b/xen/common/memory.c	Thu Oct 02 11:39:36 2008 +0100
     5.3 @@ -222,21 +222,9 @@ static long translate_gpfn_list(
     5.4           !guest_handle_subrange_okay(op.mfn_list, *progress, op.nr_gpfns-1) )
     5.5          return -EFAULT;
     5.6  
     5.7 -    if ( op.domid == DOMID_SELF )
     5.8 -    {
     5.9 -        d = rcu_lock_current_domain();
    5.10 -    }
    5.11 -    else
    5.12 -    {
    5.13 -        if ( (d = rcu_lock_domain_by_id(op.domid)) == NULL )
    5.14 -            return -ESRCH;
    5.15 -        if ( !IS_PRIV_FOR(current->domain, d) )
    5.16 -        {
    5.17 -            rcu_unlock_domain(d);
    5.18 -            return -EPERM;
    5.19 -        }
    5.20 -    }
    5.21 -
    5.22 +    rc = rcu_lock_target_domain_by_id(op.domid, &d);
    5.23 +    if ( rc )
    5.24 +        return rc;
    5.25  
    5.26      if ( !paging_mode_translate(d) )
    5.27      {
    5.28 @@ -595,20 +583,9 @@ long do_memory_op(unsigned long cmd, XEN
    5.29          if ( copy_from_guest(&domid, arg, 1) )
    5.30              return -EFAULT;
    5.31  
    5.32 -        if ( likely(domid == DOMID_SELF) )
    5.33 -        {
    5.34 -            d = rcu_lock_current_domain();
    5.35 -        }
    5.36 -        else
    5.37 -        {
    5.38 -            if ( (d = rcu_lock_domain_by_id(domid)) == NULL )
    5.39 -                return -ESRCH;
    5.40 -            if ( !IS_PRIV_FOR(current->domain, d) )
    5.41 -            {
    5.42 -                rcu_unlock_domain(d);
    5.43 -                return -EPERM;
    5.44 -            }
    5.45 -        }
    5.46 +        rc = rcu_lock_target_domain_by_id(domid, &d);
    5.47 +        if ( rc )
    5.48 +            return rc;
    5.49  
    5.50          rc = xsm_memory_stat_reservation(current->domain, d);
    5.51          if ( rc )
     6.1 --- a/xen/include/xen/sched.h	Thu Oct 02 11:32:08 2008 +0100
     6.2 +++ b/xen/include/xen/sched.h	Thu Oct 02 11:39:36 2008 +0100
     6.3 @@ -364,6 +364,13 @@ int construct_dom0(
     6.4   */
     6.5  struct domain *rcu_lock_domain_by_id(domid_t dom);
     6.6  
     6.7 +/*
     6.8 + * As above function, but accounts for current domain context:
     6.9 + *  - Translates target DOMID_SELF into caller's domain id; and
    6.10 + *  - Checks that caller has permission to act on the target domain.
    6.11 + */
    6.12 +int rcu_lock_target_domain_by_id(domid_t dom, struct domain **d);
    6.13 +
    6.14  /* Finish a RCU critical region started by rcu_lock_domain_by_id(). */
    6.15  static inline void rcu_unlock_domain(struct domain *d)
    6.16  {