xenbus_client: extend interface to suppurt multi-page ring
Subsystems (8)
PATCH v4 1/1 xenbus_client: extend interface to suppurt multi-page ring
Originally Xen PV drivers only use single-page ring to pass along information. This might limit the throughput between frontend and backend.
The patch extends Xenbus driver to support multi-page ring, which in general should improve throughput if ring is the bottleneck. Changes to various frontend / backend to adapt to the new interface are also included.
Affected Xen drivers:
* blkfront/back
* netfront/back
* pcifront/back
* tpmfront
The interface is documented, as before, in xenbus_client.c.
Change in V2:
* allow ring has arbitrary number of pages <= XENBUS_MAX_RING_PAGES
Change in V3:
* update function prototypes
* carefully deal with types of different sizes
Change in V4:
* use PAGE_KERNEL instead of PAGE_KERNEL_IO to avoid breakage on Arm
--- drivers/block/xen-blkback/xenbus.c | 5 +- drivers/block/xen-blkfront.c | 5 +- drivers/char/tpm/xen-tpmfront.c | 5 +- drivers/net/xen-netback/netback.c | 4 +- drivers/net/xen-netfront.c | 9 +- drivers/pci/xen-pcifront.c | 5 +- drivers/xen/xen-pciback/xenbus.c | 2 +- drivers/xen/xenbus/xenbus_client.c | 389 ++++++++++++++++++++++++++---------- include/xen/xenbus.h | 20 +- 9 files changed, 321 insertions(+), 123 deletions(-)
drivers/block/xen-blkback/xenbus.c +3 -2
@@ -176,7 +176,7 @@ fail:return ERR_PTR (-ENOMEM ); } -static int xen_blkif_map (struct xen_blkif *blkif,unsigned long shared_page, +static int xen_blkif_map (struct xen_blkif *blkif,grant_ref_t gref,unsigned int evtchn) {int err;
@@ -185,7 +185,8 @@ static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page,if (blkif->irq)return 0 ; - err =xenbus_map_ring_valloc (blkif->be->dev, shared_page, &blkif->blk_ring); + err =xenbus_map_ring_valloc (blkif->be->dev, &gref,1 , + &blkif->blk_ring);if (err <0 )return err;
drivers/block/xen-blkfront.c +3 -2
@@ -1144,6 +1144,7 @@ static int setup_blkring(struct xenbus_device *dev, {struct blkif_sring *sring;int err; +grant_ref_t gref; info->ring_ref =GRANT_INVALID_REF ;
@@ -1155,13 +1156,13 @@ static int setup_blkring(struct xenbus_device *dev,SHARED_RING_INIT (sring);FRONT_RING_INIT (&info->ring, sring,PAGE_SIZE ); - err =xenbus_grant_ring (dev,virt_to_mfn (info->ring.sring)); + err =xenbus_grant_ring (dev, info->ring.sring,1 , &gref);if (err <0 ) {free_page ((unsigned long )sring); info->ring.sring =NULL ; goto fail; } - info->ring_ref = err; + info->ring_ref = gref; err =xenbus_alloc_evtchn (dev, &info->evtchn);if (err)
drivers/char/tpm/xen-tpmfront.c +3 -2
@@ -235,6 +235,7 @@ static int setup_ring(struct xenbus_device *dev, struct tpm_private *priv)struct xenbus_transaction xbt;const char *message =NULL ;int rv; +grant_ref_t gref; priv->shr = (void *)__get_free_page (GFP_KERNEL |__GFP_ZERO);if (!priv->shr) {
@@ -242,11 +243,11 @@ static int setup_ring(struct xenbus_device *dev, struct tpm_private *priv)return -ENOMEM ; } - rv =xenbus_grant_ring (dev,virt_to_mfn (priv->shr)); + rv =xenbus_grant_ring (dev, &priv->shr,1 , &gref);if (rv <0 )return rv; - priv->ring_ref = rv; + priv->ring_ref = gref; rv =xenbus_alloc_evtchn (dev, &priv->evtchn);if (rv)
drivers/net/xen-netback/netback.c +2 -2
@@ -1610,7 +1610,7 @@ int xenvif_map_frontend_rings(struct xenvif *vif,int err = -ENOMEM ; err =xenbus_map_ring_valloc (xenvif_to_xenbus_device (vif), - tx_ring_ref, &addr); + &tx_ring_ref,1 , &addr);if (err) goto err;
@@ -1618,7 +1618,7 @@ int xenvif_map_frontend_rings(struct xenvif *vif,BACK_RING_INIT (&vif->tx, txs,PAGE_SIZE ); err =xenbus_map_ring_valloc (xenvif_to_xenbus_device (vif), - rx_ring_ref, &addr); + &rx_ring_ref,1 , &addr);if (err) goto err;
drivers/net/xen-netfront.c +5 -4
@@ -1593,6 +1593,7 @@ static int setup_netfront(struct xenbus_device *dev, struct netfront_info *info)int err;struct net_device *netdev = info->netdev;unsigned int feature_split_evtchn; +grant_ref_t gref; info->tx_ring_ref =GRANT_INVALID_REF ; info->rx_ring_ref =GRANT_INVALID_REF ;
@@ -1621,11 +1622,11 @@ static int setup_netfront(struct xenbus_device *dev, struct netfront_info *info)SHARED_RING_INIT (txs);FRONT_RING_INIT (&info->tx, txs,PAGE_SIZE ); - err =xenbus_grant_ring (dev,virt_to_mfn (txs)); + err =xenbus_grant_ring (dev, txs,1 , &gref);if (err <0 ) goto grant_tx_ring_fail; - info->tx_ring_ref = err; + info->tx_ring_ref = gref; rxs = (struct xen_netif_rx_sring *)get_zeroed_page (GFP_NOIO | __GFP_HIGH);if (!rxs) { err = -ENOMEM ;
@@ -1635,10 +1636,10 @@ static int setup_netfront(struct xenbus_device *dev, struct netfront_info *info)SHARED_RING_INIT (rxs);FRONT_RING_INIT (&info->rx, rxs,PAGE_SIZE ); - err =xenbus_grant_ring (dev,virt_to_mfn (rxs)); + err =xenbus_grant_ring (dev, rxs,1 , &gref);if (err <0 ) goto grant_rx_ring_fail; - info->rx_ring_ref = err; + info->rx_ring_ref = gref;if (feature_split_evtchn) err =setup_netfront_split (info);
drivers/pci/xen-pcifront.c +3 -2
@@ -771,12 +771,13 @@ static int pcifront_publish_info(struct pcifront_device *pdev) {int err =0 ;struct xenbus_transaction trans; +grant_ref_t gref; - err =xenbus_grant_ring (pdev->xdev,virt_to_mfn (pdev->sh_info)); + err =xenbus_grant_ring (pdev->xdev, pdev->sh_info,1 , &gref);if (err <0 ) goto out; - pdev->gnt_ref = err; + pdev->gnt_ref = gref; err =xenbus_alloc_evtchn (pdev->xdev, &pdev->evtchn);if (err)
drivers/xen/xen-pciback/xenbus.c +1 -1
@@ -111,7 +111,7 @@ static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,"Attaching to frontend resources - gnt_ref=%d evtchn=%d\n" , gnt_ref, remote_evtchn); - err =xenbus_map_ring_valloc (pdev->xdev, gnt_ref, &vaddr); + err =xenbus_map_ring_valloc (pdev->xdev, &gnt_ref,1 , &vaddr);if (err <0 ) {xenbus_dev_fatal (pdev->xdev, err,"Error mapping other domain page in ours." );
drivers/xen/xenbus/xenbus_client.c +287 -102
550 lines not on this page · raw message
include/xen/xenbus.h +14 -6
@@ -46,6 +46,10 @@#include <xen/interface/io/xenbus.h> #include <xen/interface/io/xs_wire.h> +#define XENBUS_MAX_RING_PAGE_ORDER 4 +#define XENBUS_MAX_RING_PAGES (1U << XENBUS_MAX_RING_PAGE_ORDER) +#define INVALID_GRANT_HANDLE (~0U) +/* Register callback to watch this node. */ struct xenbus_watch {
@@ -196,15 +200,19 @@ int xenbus_watch_pathfmt(struct xenbus_device *dev, struct xenbus_watch *watch, const char *pathfmt, ...);int xenbus_switch_state (struct xenbus_device *dev,enum xenbus_state new_state); -int xenbus_grant_ring (struct xenbus_device *dev,unsigned long ring_mfn); -int xenbus_map_ring_valloc (struct xenbus_device *dev, -int gnt_ref,void **vaddr); -int xenbus_map_ring (struct xenbus_device *dev,int gnt_ref, -grant_handle_t *handle,void *vaddr); +int xenbus_grant_ring (struct xenbus_device *dev,void *vaddr, +unsigned int nr_pages,grant_ref_t *grefs); +int xenbus_map_ring_valloc (struct xenbus_device *dev,grant_ref_t *gnt_refs, +unsigned int nr_grefs,void **vaddr); +int xenbus_map_ring (struct xenbus_device *dev, +grant_ref_t *gnt_refs,unsigned int nr_grefs, +grant_handle_t *handles,unsigned long *vaddrs, +bool *leaked);int xenbus_unmap_ring_vfree (struct xenbus_device *dev,void *vaddr);int xenbus_unmap_ring (struct xenbus_device *dev, -grant_handle_t handle,void *vaddr); +grant_handle_t *handles,unsigned int nr_handles, +unsigned long *vaddrs);int xenbus_alloc_evtchn (struct xenbus_device *dev,int *port);int xenbus_bind_evtchn (struct xenbus_device *dev,int remote_port,int *port);
Signature · 1 line
1.7.10.4
Quoted 34 lines · Wei Liu
drivers/xen/xenbus/xenbus_client.c +19
-8@@ -357,17 +365,39 @@ static void xenbus_switch_fatal(struct xenbus_device *dev, int depth, int err,/** * xenbus_grant_ring * @dev: xenbus device -* @ring_mfn: mfn of ring to grant - -* Grant access to the given @ring_mfn to the peer of the given device. Return -* 0 on success, or -errno on error. On error, the device will switch to +* @vaddr: starting virtual address of the ring +* @nr_pages: number of pages to be granted +* @grefs: grant reference array to be filled in +* +* Grant access to the given @vaddr to the peer of the given device. +* Then fill in @grefs with grant references. Return 0 on success, or +* -errno on error. On error, the device will switch to * XenbusStateClosing, and the error will be saved in the store. */ -int xenbus_grant_ring (struct xenbus_device *dev,unsigned long ring_mfn) +int xenbus_grant_ring (struct xenbus_device *dev,void *vaddr, +unsigned int nr_pages,grant_ref_t *grefs) { -int err =gnttab_grant_foreign_access (dev->otherend_id, ring_mfn,0 ); -if (err <0 ) -xenbus_dev_fatal (dev, err,"granting access to ring page" ); +int err; +int i; + +for (i =0 ; i < nr_pages; i++) { +unsigned long addr = (unsigned long )vaddr + + (PAGE_SIZE * i); + err =gnttab_grant_foreign_access (dev->otherend_id, +virt_to_mfn (addr),0 ); +if (err <0 ) { + xenbus_dev_fatal(dev, err,
drivers/xen/xenbus/xenbus_client.c static void xenbus_switch_fatal(struct xenbus_device *dev, int depth, int err, · line 399
Quoted 10 lines
drivers/xen/xenbus/xenbus_client.c +10
-0+ "granting access to ring page"); + goto fail; + } + grefs[i] = err; + } + +return 0 ; + +fail: +for (; i >=0 ; i--)
There's an off-by-one here, causing gnttab_end_foreign_access_ref to be called on the first uninitialized (at least in this function) gref.
Quoted 70 lines
drivers/xen/xenbus/xenbus_client.c +46
-9+gnttab_end_foreign_access_ref (grefs[i],0 );return err; }EXPORT_SYMBOL_GPL (xenbus_grant_ring);@@ -448,62 +478,131 @@ EXPORT_SYMBOL_GPL(xenbus_free_evtchn);/** * xenbus_map_ring_valloc * @dev: xenbus device -* @gnt_ref: grant reference +* @gnt_refs: grant reference array +* @nr_grefs: number of grant references * @vaddr: pointer to address to be filled out by mapping * -* Based on Rusty Russell's skeleton driver's map_page. -* Map a page of memory into this domain from another domain's grant table. -* xenbus_map_ring_valloc allocates a page of virtual address space, maps the -* page to that address, and sets *vaddr to that address. -* Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h) -* or -ENOMEM on error. If an error is returned, device will switch to +* Map @nr_grefs pages of memory into this domain from another +* domain's grant table. xenbus_map_ring_valloc allocates @nr_grefs +* pages of virtual address space, maps the pages to that address, and +* sets *vaddr to that address. Returns 0 on success, and GNTST_* +* (see xen/include/interface/grant_table.h) or -ENOMEM / -EINVAL on +* error. If an error is returned, device will switch to * XenbusStateClosing and the error message will be saved in XenStore. */ -int xenbus_map_ring_valloc (struct xenbus_device *dev,int gnt_ref,void **vaddr) +int xenbus_map_ring_valloc (struct xenbus_device *dev,grant_ref_t *gnt_refs, +unsigned int nr_grefs,void **vaddr) { -return ring_ops->map (dev, gnt_ref, vaddr); +return ring_ops->map (dev, gnt_refs, nr_grefs, vaddr); }EXPORT_SYMBOL_GPL (xenbus_map_ring_valloc); +/* N.B. sizeof(phys_addr_t) doesn't always equal to sizeof(unsigned +* long), e.g. 32-on-64. Caller is responsible for preparing the +* right array to feed into this function */ +static int __xenbus_map_ring (struct xenbus_device *dev, +grant_ref_t *gnt_refs, +unsigned int nr_grefs, +grant_handle_t *handles, +phys_addr_t *addrs, +unsigned int flags, +bool *leaked) +{ +struct gnttab_map_grant_ref map[XENBUS_MAX_RING_PAGES ]; +struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_PAGES ]; +int i, j; +int err = GNTST_okay; + +if (nr_grefs >XENBUS_MAX_RING_PAGES ) +return -EINVAL ; + +for (i =0 ; i < nr_grefs; i++) { +memset (&map[i],0 ,sizeof (map[i])); +gnttab_set_map_op (&map[i], addrs[i], flags, gnt_refs[i], + dev->otherend_id); + handles[i] =INVALID_GRANT_HANDLE ; + } + +gnttab_batch_map (map, i); + +for (i =0 ; i < nr_grefs; i++) { +if (map[i].status != GNTST_okay) { + err = map[i].status; +xenbus_dev_fatal (dev, map[i].status, +"mapping in shared page %d from domain %d" , + gnt_refs[i], dev->otherend_id);
drivers/xen/xenbus/xenbus_client.c EXPORT_SYMBOL_GPL(xenbus_free_evtchn); · line 543
Quoted 10 lines
drivers/xen/xenbus/xenbus_client.c +10
-0+ goto fail; + }else + handles[i] = map[i].handle; + } + +return GNTST_okay; + + fail: +for (i = j =0 ; i < nr_grefs; i++) { +memset (&unmap[i],0 ,sizeof (unmap[i]));
I think you mean unmap[j], not unmap[i] (both times)? If so, you might as well only do this in the following if block, not out here unconditionally.
Quoted 10 lines
drivers/xen/xenbus/xenbus_client.c +10
-0+if (handles[i] !=INVALID_GRANT_HANDLE ) { +gnttab_set_unmap_op (&unmap[j], (phys_addr_t )addrs[i], + GNTMAP_host_map, handles[i]); + j++; + } + } + +if (HYPERVISOR_grant_table_op (GNTTABOP_unmap_grant_ref, unmap, j)) +BUG (); +
drivers/xen/xenbus/xenbus_client.c EXPORT_SYMBOL_GPL(xenbus_free_evtchn); · line 563
Quoted 10 lines
drivers/xen/xenbus/xenbus_client.c +10
-0+ *leaked = false; +for (i =0 ; i < j; i++) { +if (unmap[i].status != GNTST_okay) { + *leaked = true; +break ; + } + } + +return err; +
Extra blank line?
- Matthew
Quoted 2 lines
(quoted diff) +2
-0+} +
On Sun, Nov 10, 2013 at 11:22:53AM +1300, Matthew Daley wrote:
[...]
Quoted 22 lines
Quoted 22 lines
drivers/xen/xenbus/xenbus_client.c +17
-4-int xenbus_grant_ring (struct xenbus_device *dev,unsigned long ring_mfn) +int xenbus_grant_ring (struct xenbus_device *dev,void *vaddr, +unsigned int nr_pages,grant_ref_t *grefs) { -int err =gnttab_grant_foreign_access (dev->otherend_id, ring_mfn,0 ); -if (err <0 ) -xenbus_dev_fatal (dev, err,"granting access to ring page" ); +int err; +int i; + +for (i =0 ; i < nr_pages; i++) { +unsigned long addr = (unsigned long )vaddr + + (PAGE_SIZE * i); + err =gnttab_grant_foreign_access (dev->otherend_id, +virt_to_mfn (addr),0 ); +if (err <0 ) { +xenbus_dev_fatal (dev, err, +"granting access to ring page" ); + goto fail; + } + grefs[i] = err; + }
Quoted 9 lines
drivers/xen/xenbus/xenbus_client.c static void xenbus_switch_fatal(struct xenbus_device *dev, int depth, int err, · line 399
Quoted 5 lines
drivers/xen/xenbus/xenbus_client.c +5
-0+ +return 0 ; + +fail: +for (; i >=0 ; i--)There's an off-by-one here, causing
gnttab_end_foreign_access_refto be called on the first uninitialized (at least in this function) gref.
Good catch.
Quoted 2 lines
Quoted 2 lines
+ gnttab_end_foreign_access_ref(grefs[i], 0); return err;
[...]
Quoted 9 lines
drivers/xen/xenbus/xenbus_client.c EXPORT_SYMBOL_GPL(xenbus_free_evtchn); · line 543
Quoted 4 lines
drivers/xen/xenbus/xenbus_client.c +4
-0+ + fail: +for (i = j =0 ; i < nr_grefs; i++) { +memset (&unmap[i],0 ,sizeof (unmap[i]));I think you mean unmap[j], not unmap[i] (both times)? If so, you might as well only do this in the following if block, not out here unconditionally.
Indeed, will move this inside following if block.
Thanks for revewing. :-)
Wei.