gnttab: limit mapcount() looping 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; } -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 = 0; - *wrc = *rdc = 0; - - for ( handle = 0; handle < lgt->maptrack_limit; handle++ ) + for ( handle = 0; !(kind & MAPKIND_WRITE) && + handle < lgt->maptrack_limit; handle++ ) { map = &maptrack_entry(lgt, handle); if ( !(map->flags & (GNTMAP_device_map|GNTMAP_host_map)) || map->domid != rd->domain_id ) continue; if ( active_entry(rd->grant_table, map->ref).frame == mfn ) - (map->flags & GNTMAP_readonly) ? (*rdc)++ : (*wrc)++; + kind |= map->flags & GNTMAP_readonly ? + MAPKIND_READ : MAPKIND_WRITE; } + + return kind; } /* @@ -782,21 +786,21 @@ __gnttab_map_grant_ref( if ( gnttab_need_iommu_mapping(ld) ) { - unsigned int wrc, rdc; + unsigned int kind = mapkind(lgt, rd, frame); int err = 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 == 0 ) + if ( !(kind & MAPKIND_WRITE) ) err = iommu_map_page(ld, frame, frame, IOMMUF_readable|IOMMUF_writable); } else if ( act_pin && !old_pin ) { - if ( (wrc + rdc) == 0 ) + if ( !kind ) err = iommu_map_page(ld, frame, frame, IOMMUF_readable); } if ( err ) @@ -990,12 +994,12 @@ __gnttab_unmap_common( if ( gnttab_need_iommu_mapping(ld) ) { - unsigned int wrc, rdc; + unsigned int kind = mapkind(lgt, rd, op->frame); int err = 0; - mapcount(lgt, rd, op->frame, &wrc, &rdc); - if ( (wrc + rdc) == 0 ) + + if ( !kind ) err = iommu_unmap_page(ld, op->frame); - else if ( wrc == 0 ) + else if ( !(kind & MAPKIND_WRITE) ) err = iommu_map_page(ld, op->frame, op->frame, IOMMUF_readable); if ( err ) {