From: "Jan Beulich" Subject: [PATCH] adjust a few RCU domain locking calls Date: Fri, 07 Sep 2012 13:56:17 +0100 Message-ID: <504A0B110200007800099CCD@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part7E4F6AE1.0__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part7E4F6AE1.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline x86's do_physdev_op() had a case where the locking was entirely superfluous. Its physdev_map_pirq() further had a case where the lock was being obtained too early, needlessly complicating early exit paths. Grant table code had two open coded instances of rcu_lock_target_domain_by_id(), and a third code section could be consolidated by using the newly introduced helper function. The memory hypercall code had two more instances of open coding rcu_lock_target_domain_by_id(), but note that here this is not just cleanup, but also fixes an error return path in memory_exchange() to actually return an error. Signed-off-by: Jan Beulich --- a/xen/arch/x86/physdev.c +++ b/xen/arch/x86/physdev.c @@ -90,14 +90,10 @@ static int physdev_hvm_map_pirq( int physdev_map_pirq(domid_t domid, int type, int *index, int *pirq_p, struct msi_info *msi) { - struct domain *d; + struct domain *d =3D current->domain; int pirq, irq, ret =3D 0; void *map_data =3D NULL; =20 - ret =3D rcu_lock_target_domain_by_id(domid, &d); - if ( ret ) - return ret; - if ( domid =3D=3D DOMID_SELF && is_hvm_domain(d) ) { /* @@ -105,14 +101,15 @@ int physdev_map_pirq(domid_t domid, int=20 * calls back into itself and deadlocks on hvm_domain.irq_lock. */ if ( !is_hvm_pv_evtchn_domain(d) ) - { - ret =3D -EINVAL; - goto free_domain; - } - ret =3D physdev_hvm_map_pirq(d, type, index, pirq_p); - goto free_domain; + return -EINVAL; + + return physdev_hvm_map_pirq(d, type, index, pirq_p); } =20 + ret =3D rcu_lock_target_domain_by_id(domid, &d); + if ( ret ) + return ret; + if ( !IS_PRIV_FOR(current->domain, d) ) { ret =3D -EPERM; @@ -696,13 +693,12 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_H } case PHYSDEVOP_get_free_pirq: { struct physdev_get_free_pirq out; - struct domain *d; + struct domain *d =3D v->domain; =20 ret =3D -EFAULT; if ( copy_from_guest(&out, arg, 1) !=3D 0 ) break; =20 - d =3D rcu_lock_current_domain(); spin_lock(&d->event_lock); =20 ret =3D get_free_pirq(d, out.type); @@ -717,7 +713,6 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_H } =20 spin_unlock(&d->event_lock); - rcu_unlock_domain(d); =20 if ( ret >=3D 0 ) { --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -24,7 +24,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 = USA */ =20 -#include +#include #include #include #include @@ -195,6 +195,30 @@ double_gt_unlock(struct grant_table *lgt spin_unlock(&rgt->lock); } =20 +static struct domain *gt_lock_target_domain_by_id(domid_t dom) +{ + struct domain *d; + int rc =3D GNTST_general_error; + + switch ( rcu_lock_target_domain_by_id(dom, &d) ) + { + case 0: + return d; + + case -ESRCH: + gdprintk(XENLOG_INFO, "Bad domid %d.\n", dom); + rc =3D GNTST_bad_domain; + break; + + case -EPERM: + rc =3D GNTST_permission_denied; + break; + } + + ASSERT(rc < 0 && -rc <=3D MAX_ERRNO); + return ERR_PTR(rc); +} + static inline int __get_maptrack_handle( struct grant_table *t) @@ -1261,7 +1285,6 @@ gnttab_setup_table( struct grant_table *gt; int i; unsigned long gmfn; - domid_t dom; =20 if ( count !=3D 1 ) return -EINVAL; @@ -1281,25 +1304,11 @@ gnttab_setup_table( goto out1; } =20 - dom =3D op.dom; - if ( dom =3D=3D DOMID_SELF ) + d =3D gt_lock_target_domain_by_id(op.dom); + if ( IS_ERR(d) ) { - d =3D rcu_lock_current_domain(); - } - else - { - if ( unlikely((d =3D rcu_lock_domain_by_id(dom)) =3D=3D NULL) ) - { - gdprintk(XENLOG_INFO, "Bad domid %d.\n", dom); - op.status =3D GNTST_bad_domain; - goto out1; - } - - if ( unlikely(!IS_PRIV_FOR(current->domain, d)) ) - { - op.status =3D GNTST_permission_denied; - goto out2; - } + op.status =3D PTR_ERR(d); + goto out1; } =20 if ( xsm_grant_setup(current->domain, d) ) @@ -1352,7 +1361,6 @@ gnttab_query_size( { struct gnttab_query_size op; struct domain *d; - domid_t dom; int rc; =20 if ( count !=3D 1 ) @@ -1364,25 +1372,11 @@ gnttab_query_size( return -EFAULT; } =20 - dom =3D op.dom; - if ( dom =3D=3D DOMID_SELF ) - { - d =3D rcu_lock_current_domain(); - } - else + d =3D gt_lock_target_domain_by_id(op.dom); + if ( IS_ERR(d) ) { - if ( unlikely((d =3D rcu_lock_domain_by_id(dom)) =3D=3D NULL) ) - { - gdprintk(XENLOG_INFO, "Bad domid %d.\n", dom); - op.status =3D GNTST_bad_domain; - goto query_out; - } - - if ( unlikely(!IS_PRIV_FOR(current->domain, d)) ) - { - op.status =3D GNTST_permission_denied; - goto query_out_unlock; - } + op.status =3D PTR_ERR(d); + goto query_out; } =20 rc =3D xsm_grant_query_size(current->domain, d); @@ -2240,15 +2234,10 @@ gnttab_get_status_frames(XEN_GUEST_HANDL return -EFAULT; } =20 - rc =3D rcu_lock_target_domain_by_id(op.dom, &d); - if ( rc < 0 ) + d =3D gt_lock_target_domain_by_id(op.dom); + if ( IS_ERR(d) ) { - if ( rc =3D=3D -ESRCH ) - op.status =3D GNTST_bad_domain; - else if ( rc =3D=3D -EPERM ) - op.status =3D GNTST_permission_denied; - else - op.status =3D GNTST_general_error; + op.status =3D PTR_ERR(d); goto out1; } rc =3D xsm_grant_setup(current->domain, d); --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -329,22 +329,9 @@ static long memory_exchange(XEN_GUEST_HA out_chunk_order =3D exch.in.extent_order - exch.out.extent_order; } =20 - if ( likely(exch.in.domid =3D=3D DOMID_SELF) ) - { - d =3D rcu_lock_current_domain(); - } - else - { - if ( (d =3D rcu_lock_domain_by_id(exch.in.domid)) =3D=3D NULL ) - goto fail_early; - - if ( !IS_PRIV_FOR(current->domain, d) ) - { - rcu_unlock_domain(d); - rc =3D -EPERM; - goto fail_early; - } - } + rc =3D rcu_lock_target_domain_by_id(exch.in.domid, &d); + if ( rc ) + goto fail_early; =20 memflags |=3D MEMF_bits(domain_clamp_alloc_bitsize( d, @@ -583,20 +570,8 @@ long do_memory_op(unsigned long cmd, XEN && (reservation.mem_flags & XENMEMF_populate_on_demand) ) args.memflags |=3D MEMF_populate_on_demand; =20 - if ( likely(reservation.domid =3D=3D DOMID_SELF) ) - { - d =3D rcu_lock_current_domain(); - } - else - { - if ( (d =3D rcu_lock_domain_by_id(reservation.domid)) =3D=3D = NULL ) - return start_extent; - if ( !IS_PRIV_FOR(current->domain, d) ) - { - rcu_unlock_domain(d); - return start_extent; - } - } + if ( unlikely(rcu_lock_target_domain_by_id(reservation.domid, = &d)) ) + return start_extent; args.domain =3D d; =20 rc =3D xsm_memory_adjust_reservation(current->domain, d); --=__Part7E4F6AE1.0__= Content-Type: text/plain; name="adjust-rcu-lock-domain.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="adjust-rcu-lock-domain.patch" adjust a few RCU domain locking calls=0A=0Ax86's do_physdev_op() had a = case where the locking was entirely=0Asuperfluous. Its physdev_map_pirq() = further had a case where the lock=0Awas being obtained too early, = needlessly complicating early exit paths.=0A=0AGrant table code had two = open coded instances of=0Arcu_lock_target_domain_by_id(), and a third code = section could be=0Aconsolidated by using the newly introduced helper = function.=0A=0AThe memory hypercall code had two more instances of open = coding=0Arcu_lock_target_domain_by_id(), but note that here this is not = just=0Acleanup, but also fixes an error return path in memory_exchange() = to=0Aactually return an error.=0A=0ASigned-off-by: Jan Beulich =0A=0A--- a/xen/arch/x86/physdev.c=0A+++ b/xen/arch/x86/physdev.c=0A= @@ -90,14 +90,10 @@ static int physdev_hvm_map_pirq(=0A int physdev_map_pir= q(domid_t domid, int type, int *index, int *pirq_p,=0A = struct msi_info *msi)=0A {=0A- struct domain *d;=0A+ struct domain = *d =3D current->domain;=0A int pirq, irq, ret =3D 0;=0A void = *map_data =3D NULL;=0A =0A- ret =3D rcu_lock_target_domain_by_id(domid, = &d);=0A- if ( ret )=0A- return ret;=0A-=0A if ( domid =3D=3D = DOMID_SELF && is_hvm_domain(d) )=0A {=0A /*=0A@@ -105,14 = +101,15 @@ int physdev_map_pirq(domid_t domid, int =0A * calls = back into itself and deadlocks on hvm_domain.irq_lock.=0A */=0A = if ( !is_hvm_pv_evtchn_domain(d) )=0A- {=0A- ret = =3D -EINVAL;=0A- goto free_domain;=0A- }=0A- ret = =3D physdev_hvm_map_pirq(d, type, index, pirq_p);=0A- goto = free_domain;=0A+ return -EINVAL;=0A+=0A+ return = physdev_hvm_map_pirq(d, type, index, pirq_p);=0A }=0A =0A+ ret =3D = rcu_lock_target_domain_by_id(domid, &d);=0A+ if ( ret )=0A+ = return ret;=0A+=0A if ( !IS_PRIV_FOR(current->domain, d) )=0A {=0A = ret =3D -EPERM;=0A@@ -696,13 +693,12 @@ ret_t do_physdev_op(int = cmd, XEN_GUEST_H=0A }=0A case PHYSDEVOP_get_free_pirq: {=0A = struct physdev_get_free_pirq out;=0A- struct domain *d;=0A+ = struct domain *d =3D v->domain;=0A =0A ret =3D -EFAULT;=0A = if ( copy_from_guest(&out, arg, 1) !=3D 0 )=0A break;=0A =0A- = d =3D rcu_lock_current_domain();=0A spin_lock(&d->event_lock)= ;=0A =0A ret =3D get_free_pirq(d, out.type);=0A@@ -717,7 +713,6 @@ = ret_t do_physdev_op(int cmd, XEN_GUEST_H=0A }=0A =0A = spin_unlock(&d->event_lock);=0A- rcu_unlock_domain(d);=0A =0A = if ( ret >=3D 0 )=0A {=0A--- a/xen/common/grant_table.c=0A+++ = b/xen/common/grant_table.c=0A@@ -24,7 +24,7 @@=0A * Foundation, Inc., 59 = Temple Place, Suite 330, Boston, MA 02111-1307 USA=0A */=0A =0A-#include= =0A+#include =0A #include =0A = #include =0A #include =0A@@ -195,6 +195,30 @@ = double_gt_unlock(struct grant_table *lgt=0A spin_unlock(&rgt->lock)= ;=0A }=0A =0A+static struct domain *gt_lock_target_domain_by_id(domid_t = dom)=0A+{=0A+ struct domain *d;=0A+ int rc =3D GNTST_general_error;= =0A+=0A+ switch ( rcu_lock_target_domain_by_id(dom, &d) )=0A+ {=0A+ = case 0:=0A+ return d;=0A+=0A+ case -ESRCH:=0A+ = gdprintk(XENLOG_INFO, "Bad domid %d.\n", dom);=0A+ rc =3D GNTST_bad_= domain;=0A+ break;=0A+=0A+ case -EPERM:=0A+ rc =3D = GNTST_permission_denied;=0A+ break;=0A+ }=0A+=0A+ ASSERT(rc < = 0 && -rc <=3D MAX_ERRNO);=0A+ return ERR_PTR(rc);=0A+}=0A+=0A static = inline int=0A __get_maptrack_handle(=0A struct grant_table *t)=0A@@ = -1261,7 +1285,6 @@ gnttab_setup_table(=0A struct grant_table *gt;=0A = int i;=0A unsigned long gmfn;=0A- domid_t = dom;=0A =0A if ( count !=3D 1 )=0A return -EINVAL;=0A@@ = -1281,25 +1304,11 @@ gnttab_setup_table(=0A goto out1;=0A }=0A = =0A- dom =3D op.dom;=0A- if ( dom =3D=3D DOMID_SELF )=0A+ d =3D = gt_lock_target_domain_by_id(op.dom);=0A+ if ( IS_ERR(d) )=0A {=0A- = d =3D rcu_lock_current_domain();=0A- }=0A- else=0A- {=0A- = if ( unlikely((d =3D rcu_lock_domain_by_id(dom)) =3D=3D NULL) )=0A- = {=0A- gdprintk(XENLOG_INFO, "Bad domid %d.\n", dom);=0A- = op.status =3D GNTST_bad_domain;=0A- goto out1;=0A- = }=0A-=0A- if ( unlikely(!IS_PRIV_FOR(current->domain, d)) )=0A- = {=0A- op.status =3D GNTST_permission_denied;=0A- = goto out2;=0A- }=0A+ op.status =3D PTR_ERR(d);=0A+ = goto out1;=0A }=0A =0A if ( xsm_grant_setup(current->domain, d) = )=0A@@ -1352,7 +1361,6 @@ gnttab_query_size(=0A {=0A struct gnttab_quer= y_size op;=0A struct domain *d;=0A- domid_t dom;=0A int = rc;=0A =0A if ( count !=3D 1 )=0A@@ -1364,25 +1372,11 @@ gnttab_query_s= ize(=0A return -EFAULT;=0A }=0A =0A- dom =3D op.dom;=0A- = if ( dom =3D=3D DOMID_SELF )=0A- {=0A- d =3D rcu_lock_current_dom= ain();=0A- }=0A- else=0A+ d =3D gt_lock_target_domain_by_id(op.dom= );=0A+ if ( IS_ERR(d) )=0A {=0A- if ( unlikely((d =3D = rcu_lock_domain_by_id(dom)) =3D=3D NULL) )=0A- {=0A- = gdprintk(XENLOG_INFO, "Bad domid %d.\n", dom);=0A- op.status = =3D GNTST_bad_domain;=0A- goto query_out;=0A- }=0A-=0A- = if ( unlikely(!IS_PRIV_FOR(current->domain, d)) )=0A- {=0A- = op.status =3D GNTST_permission_denied;=0A- goto = query_out_unlock;=0A- }=0A+ op.status =3D PTR_ERR(d);=0A+ = goto query_out;=0A }=0A =0A rc =3D xsm_grant_query_size(current= ->domain, d);=0A@@ -2240,15 +2234,10 @@ gnttab_get_status_frames(XEN_GUEST_= HANDL=0A return -EFAULT;=0A }=0A =0A- rc =3D rcu_lock_target= _domain_by_id(op.dom, &d);=0A- if ( rc < 0 )=0A+ d =3D gt_lock_target= _domain_by_id(op.dom);=0A+ if ( IS_ERR(d) )=0A {=0A- if ( rc = =3D=3D -ESRCH )=0A- op.status =3D GNTST_bad_domain;=0A- = else if ( rc =3D=3D -EPERM )=0A- op.status =3D GNTST_permission_= denied;=0A- else=0A- op.status =3D GNTST_general_error;= =0A+ op.status =3D PTR_ERR(d);=0A goto out1;=0A }=0A = rc =3D xsm_grant_setup(current->domain, d);=0A--- a/xen/common/memory.c=0A= +++ b/xen/common/memory.c=0A@@ -329,22 +329,9 @@ static long memory_exchang= e(XEN_GUEST_HA=0A out_chunk_order =3D exch.in.extent_order - = exch.out.extent_order;=0A }=0A =0A- if ( likely(exch.in.domid = =3D=3D DOMID_SELF) )=0A- {=0A- d =3D rcu_lock_current_domain();= =0A- }=0A- else=0A- {=0A- if ( (d =3D rcu_lock_domain_by_id= (exch.in.domid)) =3D=3D NULL )=0A- goto fail_early;=0A-=0A- = if ( !IS_PRIV_FOR(current->domain, d) )=0A- {=0A- = rcu_unlock_domain(d);=0A- rc =3D -EPERM;=0A- goto = fail_early;=0A- }=0A- }=0A+ rc =3D rcu_lock_target_domain_by_i= d(exch.in.domid, &d);=0A+ if ( rc )=0A+ goto fail_early;=0A =0A = memflags |=3D MEMF_bits(domain_clamp_alloc_bitsize(=0A d,=0A@@ = -583,20 +570,8 @@ long do_memory_op(unsigned long cmd, XEN=0A = && (reservation.mem_flags & XENMEMF_populate_on_demand) )=0A = args.memflags |=3D MEMF_populate_on_demand;=0A =0A- if ( likely(rese= rvation.domid =3D=3D DOMID_SELF) )=0A- {=0A- d =3D = rcu_lock_current_domain();=0A- }=0A- else=0A- {=0A- = if ( (d =3D rcu_lock_domain_by_id(reservation.domid)) =3D=3D NULL = )=0A- return start_extent;=0A- if ( !IS_PRIV_FOR(= current->domain, d) )=0A- {=0A- rcu_unlock_domain= (d);=0A- return start_extent;=0A- }=0A- = }=0A+ if ( unlikely(rcu_lock_target_domain_by_id(reservation.domid, = &d)) )=0A+ return start_extent;=0A args.domain =3D = d;=0A =0A rc =3D xsm_memory_adjust_reservation(current->domain, = d);=0A --=__Part7E4F6AE1.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part7E4F6AE1.0__=--