From: "Jan Beulich" Subject: [PATCH 2/6] gnttab: limit mapcount() looping Date: Fri, 12 Jun 2015 11:35:56 +0100 Message-ID: <557AD22C02000078000841DE@mail.emea.novell.com> References: <557AD03E02000078000841B4@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartEDD9CF1C.1__=" Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1Z3MJb-0003gq-5a for xen-devel@lists.xenproject.org; Fri, 12 Jun 2015 10:35:59 +0000 In-Reply-To: <557AD03E02000078000841B4@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. --=__PartEDD9CF1C.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline The function also doesn't need to return counts; 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 @@ -541,24 +541,28 @@ 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( + const struct grant_table *lgt, const struct domain *rd, unsigned long = mfn) { struct grant_mapping *map; grant_handle_t handle; + unsigned int kind =3D 0; =20 - *wrc =3D *rdc =3D 0; - - 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 /* @@ -782,21 +786,21 @@ __gnttab_map_grant_ref( =20 if ( gnttab_need_iommu_mapping(ld) ) { - unsigned int wrc, rdc; + unsigned int kind =3D mapkind(lgt, rd, frame); int err =3D 0; + /* 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); 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 ) @@ -990,12 +994,12 @@ __gnttab_unmap_common( =20 if ( gnttab_need_iommu_mapping(ld) ) { - unsigned int wrc, rdc; + unsigned int kind =3D mapkind(lgt, rd, op->frame); int err =3D 0; - mapcount(lgt, rd, op->frame, &wrc, &rdc); - if ( (wrc + rdc) =3D=3D 0 ) + + 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); if ( err ) { --=__PartEDD9CF1C.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 also doesn't need to = return counts; all its callers are=0Aafter is whether at least one entry = of a certain kind exists. With that=0Athere's no point for that loop to = continue once the looked for=0Acondition was found to be met by one entry. = Rename the function to=0Amatch 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@@ -541,24 +541,28 @@ static int grant_map_exi= sts(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+ const = struct grant_table *lgt, const struct domain *rd, unsigned long mfn)=0A = {=0A struct grant_mapping *map;=0A grant_handle_t handle;=0A+ = unsigned int kind =3D 0;=0A =0A- *wrc =3D *rdc =3D 0;=0A-=0A- for ( = handle =3D 0; handle < lgt->maptrack_limit; 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_hos= t_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@@ -782,21 +786,21 @@ __gnttab_map_grant_ref(=0A = =0A if ( gnttab_need_iommu_mapping(ld) )=0A {=0A- unsigned = int wrc, rdc;=0A+ unsigned int kind =3D mapkind(lgt, rd, frame);=0A = int err =3D 0;=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 = 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@@ -990,12 +994,12 @@ __gnttab_unmap_common(=0A =0A = if ( gnttab_need_iommu_mapping(ld) )=0A {=0A- unsigned int = wrc, rdc;=0A+ unsigned int kind =3D mapkind(lgt, rd, op->frame);=0A = int err =3D 0;=0A- mapcount(lgt, rd, op->frame, &wrc, = &rdc);=0A- if ( (wrc + rdc) =3D=3D 0 )=0A+=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 if ( err )=0A {=0A --=__PartEDD9CF1C.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 --=__PartEDD9CF1C.1__=--