Received: from eggs.gnu.org ([2001:4830:134:3::10]:36436) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XowSO-0002az-Vo for qemu-devel@nongnu.org; Thu, 13 Nov 2014 10:37:19 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XowSK-0006iM-J2 for qemu-devel@nongnu.org; Thu, 13 Nov 2014 10:37:12 -0500 Received: from smtp02.citrix.com ([66.165.176.63]:40004) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XowSK-0006i4-9r for qemu-devel@nongnu.org; Thu, 13 Nov 2014 10:37:08 -0500 Date: Thu, 13 Nov 2014 15:36:08 +0000 From: Stefano Stabellini In-Reply-To: <1415890703-10147-1-git-send-email-roger.pau@citrix.com> Message-ID: References: <1415890703-10147-1-git-send-email-roger.pau@citrix.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="1342847746-1213237974-1415891636=:26318" Content-ID: Subject: Re: [Qemu-devel] [PATCH v2 for-xen-4.5] xen_disk: fix unmapping of persistent grants List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Roger Pau Monne Cc: Kevin Wolf , Stefano Stabellini , George Dunlap , Konrad Rzeszutek Wilk , qemu-devel@nongnu.org, Stefan Hajnoczi , xen-devel@lists.xenproject.org --1342847746-1213237974-1415891636=:26318 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: QUOTED-PRINTABLE Content-ID: On Thu, 13 Nov 2014, Roger Pau Monne wrote: > This patch fixes two issues with persistent grants and the disk PV backen= d > (Qdisk): >=20 > - Keep track of memory regions where persistent grants have been mapped > since we need to unmap them as a whole. It is not possible to unmap a > single grant if it has been batch-mapped. > - Unmap persistent grants before switching to the closed state, so the > frontend can also free them. >=20 > Signed-off-by: Roger Pau Monn=C3=A9 > Reported-by: George Dunlap > Cc: Stefano Stabellini > Cc: Kevin Wolf > Cc: Stefan Hajnoczi > Cc: George Dunlap > Cc: Konrad Rzeszutek Wilk > --- > Xen release exception: this is obviously an important bug-fix that should= be > included in 4.5. Without this fix, trying to do a block-detach of a Qdisk > from a running guest can lead to guest crash depending on the blkfront > implementation. > --- > Changes since v1: > - Instead of disabling batch mappings when using persistent grants, keep > track of the persistently mapped regions and unmap them on disconnecti= on. > This prevents unmapping single persistent grants, but the current > implementation doesn't require it. >=20 > This new version is slightly faster than the previous one, the following > test results are in IOPS from 20 runs of a block-attach, fio run, > block-detach test loop: >=20 > x batch > + no_batch > +----------------------------------------------------------------------+ > | + + x x + + + x xx x | > |+ + x x+ *+++ * +x* +++x* x xx x *| > | |_____________A_____M______|| > | |________________A_____M___________| | > +----------------------------------------------------------------------+ > N Min Max Median Avg Stdd= ev > x 20 52941 63822 62396 61180.15 2673.64= 97 > + 20 49967 63849 60322 59116.9 3456.34= 55 > Difference at 95.0% confidence > =09-2063.25 +/- 1977.66 > =09-3.37242% +/- 3.23252% > =09(Student's t, pooled s =3D 3089.88) Not too bad! Thanks! > hw/block/xen_disk.c | 77 ++++++++++++++++++++++++++++++++++++++++++++---= ------ > 1 file changed, 64 insertions(+), 13 deletions(-) >=20 > diff --git a/hw/block/xen_disk.c b/hw/block/xen_disk.c > index 231e9a7..75bdc16 100644 > --- a/hw/block/xen_disk.c > +++ b/hw/block/xen_disk.c > @@ -59,6 +59,13 @@ struct PersistentGrant { > =20 > typedef struct PersistentGrant PersistentGrant; > =20 > +struct PersistentRegion { > + void *addr; > + int num; > +}; > + > +typedef struct PersistentRegion PersistentRegion; > + > struct ioreq { > blkif_request_t req; > int16_t status; > @@ -118,6 +125,7 @@ struct XenBlkDev { > gboolean feature_discard; > gboolean feature_persistent; > GTree *persistent_gnts; > + GSList *persistent_regions; > unsigned int persistent_gnt_count; > unsigned int max_grants; > =20 > @@ -164,19 +172,41 @@ static gint int_cmp(gconstpointer a, gconstpointer = b, gpointer user_data) > static void destroy_grant(gpointer pgnt) > { > PersistentGrant *grant =3D pgnt; > - XenGnttab gnt =3D grant->blkdev->xendev.gnttabdev; > =20 > - if (xc_gnttab_munmap(gnt, grant->page, 1) !=3D 0) { > - xen_be_printf(&grant->blkdev->xendev, 0, > - "xc_gnttab_munmap failed: %s\n", > - strerror(errno)); > - } > grant->blkdev->persistent_gnt_count--; > xen_be_printf(&grant->blkdev->xendev, 3, > - "unmapped grant %p\n", grant->page); > + "removed grant %p\n", grant->page); > g_free(grant); > } > =20 > +static void add_persistent_region(struct XenBlkDev *blkdev, void *addr, = int num) > +{ > + PersistentRegion *region; > + > + region =3D g_malloc0(sizeof(*region)); > + region->addr =3D addr; > + region->num =3D num; > + blkdev->persistent_regions =3D g_slist_append(blkdev->persistent_reg= ions, > + region); > +} > + > +static void remove_persistent_region(gpointer data, gpointer dev) > +{ > + PersistentRegion *region =3D data; > + struct XenBlkDev *blkdev =3D dev; > + XenGnttab gnt =3D blkdev->xendev.gnttabdev; > + > + if (xc_gnttab_munmap(gnt, region->addr, region->num) !=3D 0) { > + xen_be_printf(&blkdev->xendev, 0, > + "xc_gnttab_munmap region %p failed: %s\n", > + region->addr, strerror(errno)); > + } > + xen_be_printf(&blkdev->xendev, 3, > + "unmapped grant region %p with %d pages\n", > + region->addr, region->num); > + g_free(region); > +} > + > static struct ioreq *ioreq_start(struct XenBlkDev *blkdev) > { > struct ioreq *ioreq =3D NULL; > @@ -421,7 +451,17 @@ static int ioreq_map(struct ioreq *ioreq) > } > } > } > - if (ioreq->blkdev->feature_persistent) { > + if (ioreq->blkdev->feature_persistent && new_maps && > + (!batch_maps || (ioreq->blkdev->persistent_gnt_count + new_maps = <=3D > + ioreq->blkdev->max_grants))) { Do we really need this change? If so, could you please write something about it in the commit message? > + /* > + * If we are using persistent grants and batch mappings only > + * add the new maps to the list of persistent grants if the whol= e > + * area can be persistently mapped. > + */ Is this the reason for the change above? > + if (batch_maps && new_maps) { > + add_persistent_region(ioreq->blkdev, ioreq->pages, new_maps)= ; > + } > > while ((ioreq->blkdev->persistent_gnt_count < ioreq->blkdev->max= _grants) > && new_maps) { > /* Go through the list of newly mapped grants and add as man= y > @@ -437,6 +477,7 @@ static int ioreq_map(struct ioreq *ioreq) > grant->page =3D ioreq->pages + (new_maps) * XC_PAGE_SIZE= ; > } else { > grant->page =3D ioreq->page[new_maps]; > + add_persistent_region(ioreq->blkdev, ioreq->page[new_map= s], 1); Basically in the !batch_maps case we are duplicating persistent_gnts into persistent_regions. Couldn't we just avoid calling add_persistent_region at all in that case and rely on the old destroy_grant function? In fact I think we could just pass NULL as GDestroyNotify function when creating persistent_gnts if batch_maps and the old unmodified destroy_grant if !batch_maps. > } > grant->blkdev =3D ioreq->blkdev; > xen_be_printf(&ioreq->blkdev->xendev, 3, > @@ -447,6 +488,7 @@ static int ioreq_map(struct ioreq *ioreq) > grant); > ioreq->blkdev->persistent_gnt_count++; > } > + assert(!batch_maps || new_maps =3D=3D 0); Shouldn't new_maps be =3D=3D 0 even in the !batch_maps case? > } > for (i =3D 0; i < ioreq->v.niov; i++) { > ioreq->v.iov[i].iov_base +=3D (uintptr_t)page[i]; > @@ -972,6 +1014,7 @@ static int blk_connect(struct XenDevice *xendev) > blkdev->persistent_gnts =3D g_tree_new_full((GCompareDataFunc)in= t_cmp, > NULL, NULL, > (GDestroyNotify)destroy_gra= nt); > + blkdev->persistent_regions =3D NULL; > blkdev->persistent_gnt_count =3D 0; > } > =20 > @@ -1000,6 +1043,19 @@ static void blk_disconnect(struct XenDevice *xende= v) > blkdev->cnt_map--; > blkdev->sring =3D NULL; > } > + > + /* > + * Unmap persistent grants before switching to the closed state > + * so the frontend can free them. > + */ > + if (blkdev->feature_persistent) { > + g_tree_destroy(blkdev->persistent_gnts); > + assert(blkdev->persistent_gnt_count =3D=3D 0); > + g_slist_foreach(blkdev->persistent_regions, > + (GFunc)remove_persistent_region, blkdev); > + g_slist_free(blkdev->persistent_regions); > + blkdev->feature_persistent =3D false; > + } > } > =20 > static int blk_free(struct XenDevice *xendev) > @@ -1011,11 +1067,6 @@ static int blk_free(struct XenDevice *xendev) > blk_disconnect(xendev); > } > =20 > - /* Free persistent grants */ > - if (blkdev->feature_persistent) { > - g_tree_destroy(blkdev->persistent_gnts); > - } > - > while (!QLIST_EMPTY(&blkdev->freelist)) { > ioreq =3D QLIST_FIRST(&blkdev->freelist); > QLIST_REMOVE(ioreq, list); > --=20 > 1.9.3 (Apple Git-50) >=20 --1342847746-1213237974-1415891636=:26318--