From: "Jan Beulich" Subject: [PATCH v2 2/6] gnttab: limit mapcount() looping Date: Mon, 15 Jun 2015 13:26:09 +0100 Message-ID: <557EE0810200007800084CB8@mail.emea.novell.com> References: <557EDDDD0200007800084C94@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartF6C2D071.1__=" Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1Z4TSz-0001wp-E4 for xen-devel@lists.xenproject.org; Mon, 15 Jun 2015 12:26:17 +0000 In-Reply-To: <557EDDDD0200007800084C94@mail.emea.novell.com> 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 Cc: Ian Campbell , Keir Fraser , Ian Jackson , Tim Deegan 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. --=__PartF6C2D071.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline The function doesn't need to return counts in the first place; all its callers are after is whether at least one entry of a certain kind exists. With that there's no point for that loop to continue once the looked for condition was found to be met by one entry. Rename the function to match the changed behavior. Signed-off-by: Jan Beulich --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -253,7 +253,7 @@ static inline void double_gt_lock(struct grant_table *lgt, struct grant_table *rgt) { /* - * See mapcount() for why the write lock is also required for the + * See mapkind() for why the write lock is also required for the * remote domain. */ if ( lgt < rgt ) @@ -566,14 +566,14 @@ static int grant_map_exists(const struct return -EINVAL; } =20 -static void mapcount( - struct grant_table *lgt, struct domain *rd, unsigned long mfn, - unsigned int *wrc, unsigned int *rdc) +#define MAPKIND_READ 1 +#define MAPKIND_WRITE 2 +static unsigned int mapkind( + struct grant_table *lgt, const struct domain *rd, unsigned long mfn) { struct grant_mapping *map; grant_handle_t handle; - - *wrc =3D *rdc =3D 0; + unsigned int kind =3D 0; =20 /* * Must have the local domain's grant table write lock when @@ -586,15 +586,19 @@ static void mapcount( */ ASSERT(rw_is_write_locked(&rd->grant_table->lock)); =20 - for ( handle =3D 0; handle < lgt->maptrack_limit; handle++ ) + for ( handle =3D 0; !(kind & MAPKIND_WRITE) && + handle < lgt->maptrack_limit; handle++ ) { map =3D &maptrack_entry(lgt, handle); if ( !(map->flags & (GNTMAP_device_map|GNTMAP_host_map)) || map->domid !=3D rd->domain_id ) continue; if ( _active_entry(rd->grant_table, map->ref).frame =3D=3D mfn ) - (map->flags & GNTMAP_readonly) ? (*rdc)++ : (*wrc)++; + kind |=3D map->flags & GNTMAP_readonly ? + MAPKIND_READ : MAPKIND_WRITE; } + + return kind; } =20 /* @@ -819,24 +823,24 @@ __gnttab_map_grant_ref( need_iommu =3D gnttab_need_iommu_mapping(ld); if ( need_iommu ) { - unsigned int wrc, rdc; + unsigned int kind; int err =3D 0; =20 double_gt_lock(lgt, rgt); =20 /* We're not translated, so we know that gmfns and mfns are the same things, so the IOMMU entry is always 1-to-1. */ - mapcount(lgt, rd, frame, &wrc, &rdc); + kind =3D mapkind(lgt, rd, frame); if ( (act_pin & (GNTPIN_hstw_mask|GNTPIN_devw_mask)) && !(old_pin & (GNTPIN_hstw_mask|GNTPIN_devw_mask)) ) { - if ( wrc =3D=3D 0 ) + if ( !(kind & MAPKIND_WRITE) ) err =3D iommu_map_page(ld, frame, frame, IOMMUF_readable|IOMMUF_writable); } else if ( act_pin && !old_pin ) { - if ( (wrc + rdc) =3D=3D 0 ) + if ( !kind ) err =3D iommu_map_page(ld, frame, frame, IOMMUF_readable);= } if ( err ) @@ -1050,15 +1054,15 @@ __gnttab_unmap_common( =20 if ( rc =3D=3D GNTST_okay && gnttab_need_iommu_mapping(ld) ) { - unsigned int wrc, rdc; + unsigned int kind; int err =3D 0; =20 double_gt_lock(lgt, rgt); =20 - mapcount(lgt, rd, op->frame, &wrc, &rdc); - if ( (wrc + rdc) =3D=3D 0 ) + kind =3D mapkind(lgt, rd, op->frame); + if ( !kind ) err =3D iommu_unmap_page(ld, op->frame); - else if ( wrc =3D=3D 0 ) + else if ( !(kind & MAPKIND_WRITE) ) err =3D iommu_map_page(ld, op->frame, op->frame, IOMMUF_readab= le); =20 double_gt_unlock(lgt, rgt); --=__PartF6C2D071.1__= Content-Type: text/plain; name="gnttab-limit-mapcount-looping.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="gnttab-limit-mapcount-looping.patch" gnttab: limit mapcount() looping=0A=0AThe function doesn't need to return = counts in the first place; all its=0Acallers are after is whether at least = one entry of a certain kind=0Aexists. With that there's no point for that = loop to continue once the=0Alooked for condition was found to be met by = one entry. Rename the=0Afunction to match the changed behavior.=0A=0ASigned= -off-by: Jan Beulich =0A=0A--- a/xen/common/grant_table.= c=0A+++ b/xen/common/grant_table.c=0A@@ -253,7 +253,7 @@ static inline = void=0A double_gt_lock(struct grant_table *lgt, struct grant_table = *rgt)=0A {=0A /*=0A- * See mapcount() for why the write lock is = also required for the=0A+ * See mapkind() for why the write lock is = also required for the=0A * remote domain.=0A */=0A if ( lgt = < rgt )=0A@@ -566,14 +566,14 @@ static int grant_map_exists(const = struct=0A return -EINVAL;=0A }=0A =0A-static void mapcount(=0A- = struct grant_table *lgt, struct domain *rd, unsigned long mfn,=0A- = unsigned int *wrc, unsigned int *rdc)=0A+#define MAPKIND_READ 1=0A+#define = MAPKIND_WRITE 2=0A+static unsigned int mapkind(=0A+ struct grant_table = *lgt, const struct domain *rd, unsigned long mfn)=0A {=0A struct = grant_mapping *map;=0A grant_handle_t handle;=0A-=0A- *wrc =3D *rdc = =3D 0;=0A+ unsigned int kind =3D 0;=0A =0A /*=0A * Must have = the local domain's grant table write lock when=0A@@ -586,15 +586,19 @@ = static void mapcount(=0A */=0A ASSERT(rw_is_write_locked(&rd->gran= t_table->lock));=0A =0A- for ( handle =3D 0; handle < lgt->maptrack_limi= t; handle++ )=0A+ for ( handle =3D 0; !(kind & MAPKIND_WRITE) &&=0A+ = handle < lgt->maptrack_limit; handle++ )=0A {=0A = map =3D &maptrack_entry(lgt, handle);=0A if ( !(map->flags & = (GNTMAP_device_map|GNTMAP_host_map)) ||=0A map->domid !=3D = rd->domain_id )=0A continue;=0A if ( _active_entry(rd->= grant_table, map->ref).frame =3D=3D mfn )=0A- (map->flags & = GNTMAP_readonly) ? (*rdc)++ : (*wrc)++;=0A+ kind |=3D map->flags= & GNTMAP_readonly ?=0A+ MAPKIND_READ : MAPKIND_WRITE;= =0A }=0A+=0A+ return kind;=0A }=0A =0A /*=0A@@ -819,24 +823,24 @@ = __gnttab_map_grant_ref(=0A need_iommu =3D gnttab_need_iommu_mapping(ld)= ;=0A if ( need_iommu )=0A {=0A- unsigned int wrc, rdc;=0A+ = unsigned int kind;=0A int err =3D 0;=0A =0A = double_gt_lock(lgt, rgt);=0A =0A /* We're not translated, so we = know that gmfns and mfns are=0A the same things, so the IOMMU = entry is always 1-to-1. */=0A- mapcount(lgt, rd, frame, &wrc, = &rdc);=0A+ kind =3D mapkind(lgt, rd, frame);=0A if ( = (act_pin & (GNTPIN_hstw_mask|GNTPIN_devw_mask)) &&=0A = !(old_pin & (GNTPIN_hstw_mask|GNTPIN_devw_mask)) )=0A {=0A- = if ( wrc =3D=3D 0 )=0A+ if ( !(kind & MAPKIND_WRITE) )=0A = err =3D iommu_map_page(ld, frame, frame,=0A = IOMMUF_readable|IOMMUF_writable);=0A }=0A = else if ( act_pin && !old_pin )=0A {=0A- if ( (wrc + = rdc) =3D=3D 0 )=0A+ if ( !kind )=0A err =3D = iommu_map_page(ld, frame, frame, IOMMUF_readable);=0A }=0A = if ( err )=0A@@ -1050,15 +1054,15 @@ __gnttab_unmap_common(=0A =0A if = ( rc =3D=3D GNTST_okay && gnttab_need_iommu_mapping(ld) )=0A {=0A- = unsigned int wrc, rdc;=0A+ unsigned int kind;=0A int err = =3D 0;=0A =0A double_gt_lock(lgt, rgt);=0A =0A- mapcount(lgt= , rd, op->frame, &wrc, &rdc);=0A- if ( (wrc + rdc) =3D=3D 0 )=0A+ = kind =3D mapkind(lgt, rd, op->frame);=0A+ if ( !kind )=0A = err =3D iommu_unmap_page(ld, op->frame);=0A- else if ( wrc = =3D=3D 0 )=0A+ else if ( !(kind & MAPKIND_WRITE) )=0A = err =3D iommu_map_page(ld, op->frame, op->frame, IOMMUF_readable);=0A =0A = double_gt_unlock(lgt, rgt);=0A --=__PartF6C2D071.1__= 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 --=__PartF6C2D071.1__=--