Commit ccc9d90a authored by Wei Liu's avatar Wei Liu Committed by David Vrabel

xenbus_client: Extend interface to support 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
* scsifront/back
* vtpmfront

The interface is documented, as before, in xenbus_client.c.
Signed-off-by: default avatarWei Liu <wei.liu2@citrix.com>
Signed-off-by: default avatarPaul Durrant <paul.durrant@citrix.com>
Signed-off-by: default avatarBob Liu <bob.liu@oracle.com>
Cc: Konrad Wilk <konrad.wilk@oracle.com>
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Signed-off-by: default avatarDavid Vrabel <david.vrabel@citrix.com>
parent 278edfc0
...@@ -193,7 +193,7 @@ static struct xen_blkif *xen_blkif_alloc(domid_t domid) ...@@ -193,7 +193,7 @@ static struct xen_blkif *xen_blkif_alloc(domid_t domid)
return ERR_PTR(-ENOMEM); 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) unsigned int evtchn)
{ {
int err; int err;
...@@ -202,7 +202,8 @@ static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page, ...@@ -202,7 +202,8 @@ static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page,
if (blkif->irq) if (blkif->irq)
return 0; 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) if (err < 0)
return err; return err;
......
...@@ -1245,6 +1245,7 @@ static int setup_blkring(struct xenbus_device *dev, ...@@ -1245,6 +1245,7 @@ static int setup_blkring(struct xenbus_device *dev,
struct blkfront_info *info) struct blkfront_info *info)
{ {
struct blkif_sring *sring; struct blkif_sring *sring;
grant_ref_t gref;
int err; int err;
info->ring_ref = GRANT_INVALID_REF; info->ring_ref = GRANT_INVALID_REF;
...@@ -1257,13 +1258,13 @@ static int setup_blkring(struct xenbus_device *dev, ...@@ -1257,13 +1258,13 @@ static int setup_blkring(struct xenbus_device *dev,
SHARED_RING_INIT(sring); SHARED_RING_INIT(sring);
FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE); 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) { if (err < 0) {
free_page((unsigned long)sring); free_page((unsigned long)sring);
info->ring.sring = NULL; info->ring.sring = NULL;
goto fail; goto fail;
} }
info->ring_ref = err; info->ring_ref = gref;
err = xenbus_alloc_evtchn(dev, &info->evtchn); err = xenbus_alloc_evtchn(dev, &info->evtchn);
if (err) if (err)
......
...@@ -193,6 +193,7 @@ static int setup_ring(struct xenbus_device *dev, struct tpm_private *priv) ...@@ -193,6 +193,7 @@ static int setup_ring(struct xenbus_device *dev, struct tpm_private *priv)
struct xenbus_transaction xbt; struct xenbus_transaction xbt;
const char *message = NULL; const char *message = NULL;
int rv; int rv;
grant_ref_t gref;
priv->shr = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO); priv->shr = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
if (!priv->shr) { if (!priv->shr) {
...@@ -200,11 +201,11 @@ static int setup_ring(struct xenbus_device *dev, struct tpm_private *priv) ...@@ -200,11 +201,11 @@ static int setup_ring(struct xenbus_device *dev, struct tpm_private *priv)
return -ENOMEM; return -ENOMEM;
} }
rv = xenbus_grant_ring(dev, virt_to_mfn(priv->shr)); rv = xenbus_grant_ring(dev, &priv->shr, 1, &gref);
if (rv < 0) if (rv < 0)
return rv; return rv;
priv->ring_ref = rv; priv->ring_ref = gref;
rv = xenbus_alloc_evtchn(dev, &priv->evtchn); rv = xenbus_alloc_evtchn(dev, &priv->evtchn);
if (rv) if (rv)
......
...@@ -1781,7 +1781,7 @@ int xenvif_map_frontend_rings(struct xenvif_queue *queue, ...@@ -1781,7 +1781,7 @@ int xenvif_map_frontend_rings(struct xenvif_queue *queue,
int err = -ENOMEM; int err = -ENOMEM;
err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif), err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
tx_ring_ref, &addr); &tx_ring_ref, 1, &addr);
if (err) if (err)
goto err; goto err;
...@@ -1789,7 +1789,7 @@ int xenvif_map_frontend_rings(struct xenvif_queue *queue, ...@@ -1789,7 +1789,7 @@ int xenvif_map_frontend_rings(struct xenvif_queue *queue,
BACK_RING_INIT(&queue->tx, txs, PAGE_SIZE); BACK_RING_INIT(&queue->tx, txs, PAGE_SIZE);
err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif), err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
rx_ring_ref, &addr); &rx_ring_ref, 1, &addr);
if (err) if (err)
goto err; goto err;
......
...@@ -1486,6 +1486,7 @@ static int setup_netfront(struct xenbus_device *dev, ...@@ -1486,6 +1486,7 @@ static int setup_netfront(struct xenbus_device *dev,
{ {
struct xen_netif_tx_sring *txs; struct xen_netif_tx_sring *txs;
struct xen_netif_rx_sring *rxs; struct xen_netif_rx_sring *rxs;
grant_ref_t gref;
int err; int err;
queue->tx_ring_ref = GRANT_INVALID_REF; queue->tx_ring_ref = GRANT_INVALID_REF;
...@@ -1502,10 +1503,10 @@ static int setup_netfront(struct xenbus_device *dev, ...@@ -1502,10 +1503,10 @@ static int setup_netfront(struct xenbus_device *dev,
SHARED_RING_INIT(txs); SHARED_RING_INIT(txs);
FRONT_RING_INIT(&queue->tx, txs, PAGE_SIZE); FRONT_RING_INIT(&queue->tx, txs, PAGE_SIZE);
err = xenbus_grant_ring(dev, virt_to_mfn(txs)); err = xenbus_grant_ring(dev, txs, 1, &gref);
if (err < 0) if (err < 0)
goto grant_tx_ring_fail; goto grant_tx_ring_fail;
queue->tx_ring_ref = err; queue->tx_ring_ref = gref;
rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH); rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
if (!rxs) { if (!rxs) {
...@@ -1516,10 +1517,10 @@ static int setup_netfront(struct xenbus_device *dev, ...@@ -1516,10 +1517,10 @@ static int setup_netfront(struct xenbus_device *dev,
SHARED_RING_INIT(rxs); SHARED_RING_INIT(rxs);
FRONT_RING_INIT(&queue->rx, rxs, PAGE_SIZE); FRONT_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
err = xenbus_grant_ring(dev, virt_to_mfn(rxs)); err = xenbus_grant_ring(dev, rxs, 1, &gref);
if (err < 0) if (err < 0)
goto grant_rx_ring_fail; goto grant_rx_ring_fail;
queue->rx_ring_ref = err; queue->rx_ring_ref = gref;
if (feature_split_evtchn) if (feature_split_evtchn)
err = setup_netfront_split(queue); err = setup_netfront_split(queue);
......
...@@ -777,12 +777,13 @@ static int pcifront_publish_info(struct pcifront_device *pdev) ...@@ -777,12 +777,13 @@ static int pcifront_publish_info(struct pcifront_device *pdev)
{ {
int err = 0; int err = 0;
struct xenbus_transaction trans; 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) if (err < 0)
goto out; goto out;
pdev->gnt_ref = err; pdev->gnt_ref = gref;
err = xenbus_alloc_evtchn(pdev->xdev, &pdev->evtchn); err = xenbus_alloc_evtchn(pdev->xdev, &pdev->evtchn);
if (err) if (err)
......
...@@ -714,6 +714,7 @@ static int scsifront_alloc_ring(struct vscsifrnt_info *info) ...@@ -714,6 +714,7 @@ static int scsifront_alloc_ring(struct vscsifrnt_info *info)
{ {
struct xenbus_device *dev = info->dev; struct xenbus_device *dev = info->dev;
struct vscsiif_sring *sring; struct vscsiif_sring *sring;
grant_ref_t gref;
int err = -ENOMEM; int err = -ENOMEM;
/***** Frontend to Backend ring start *****/ /***** Frontend to Backend ring start *****/
...@@ -726,14 +727,14 @@ static int scsifront_alloc_ring(struct vscsifrnt_info *info) ...@@ -726,14 +727,14 @@ static int scsifront_alloc_ring(struct vscsifrnt_info *info)
SHARED_RING_INIT(sring); SHARED_RING_INIT(sring);
FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE); FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
err = xenbus_grant_ring(dev, virt_to_mfn(sring)); err = xenbus_grant_ring(dev, sring, 1, &gref);
if (err < 0) { if (err < 0) {
free_page((unsigned long)sring); free_page((unsigned long)sring);
xenbus_dev_fatal(dev, err, xenbus_dev_fatal(dev, err,
"fail to grant shared ring (Front to Back)"); "fail to grant shared ring (Front to Back)");
return err; return err;
} }
info->ring_ref = err; info->ring_ref = gref;
err = xenbus_alloc_evtchn(dev, &info->evtchn); err = xenbus_alloc_evtchn(dev, &info->evtchn);
if (err) { if (err) {
......
...@@ -113,7 +113,7 @@ static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref, ...@@ -113,7 +113,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", "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
gnt_ref, remote_evtchn); 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) { if (err < 0) {
xenbus_dev_fatal(pdev->xdev, err, xenbus_dev_fatal(pdev->xdev, err,
"Error mapping other domain page in ours."); "Error mapping other domain page in ours.");
......
...@@ -809,7 +809,7 @@ static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref, ...@@ -809,7 +809,7 @@ static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref,
if (info->irq) if (info->irq)
return -1; return -1;
err = xenbus_map_ring_valloc(info->dev, ring_ref, &area); err = xenbus_map_ring_valloc(info->dev, &ring_ref, 1, &area);
if (err) if (err)
return err; return err;
......
...@@ -52,17 +52,25 @@ ...@@ -52,17 +52,25 @@
struct xenbus_map_node { struct xenbus_map_node {
struct list_head next; struct list_head next;
union { union {
struct vm_struct *area; /* PV */ struct {
struct page *page; /* HVM */ struct vm_struct *area;
} pv;
struct {
struct page *pages[XENBUS_MAX_RING_PAGES];
void *addr;
} hvm;
}; };
grant_handle_t handle; grant_handle_t handles[XENBUS_MAX_RING_PAGES];
unsigned int nr_handles;
}; };
static DEFINE_SPINLOCK(xenbus_valloc_lock); static DEFINE_SPINLOCK(xenbus_valloc_lock);
static LIST_HEAD(xenbus_valloc_pages); static LIST_HEAD(xenbus_valloc_pages);
struct xenbus_ring_ops { struct xenbus_ring_ops {
int (*map)(struct xenbus_device *dev, int gnt, void **vaddr); int (*map)(struct xenbus_device *dev,
grant_ref_t *gnt_refs, unsigned int nr_grefs,
void **vaddr);
int (*unmap)(struct xenbus_device *dev, void *vaddr); int (*unmap)(struct xenbus_device *dev, void *vaddr);
}; };
...@@ -355,17 +363,39 @@ static void xenbus_switch_fatal(struct xenbus_device *dev, int depth, int err, ...@@ -355,17 +363,39 @@ static void xenbus_switch_fatal(struct xenbus_device *dev, int depth, int err,
/** /**
* xenbus_grant_ring * xenbus_grant_ring
* @dev: xenbus device * @dev: xenbus device
* @ring_mfn: mfn of ring to grant * @vaddr: starting virtual address of the ring
* @nr_pages: number of pages to be granted
* Grant access to the given @ring_mfn to the peer of the given device. Return * @grefs: grant reference array to be filled in
* a grant reference on success, or -errno on error. On error, the device will *
* switch to XenbusStateClosing, and the error will be saved in the store. * 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); int err;
if (err < 0) int i, j;
xenbus_dev_fatal(dev, err, "granting access to ring page");
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;
}
return 0;
fail:
for (j = 0; j < i; j++)
gnttab_end_foreign_access_ref(grefs[j], 0);
return err; return err;
} }
EXPORT_SYMBOL_GPL(xenbus_grant_ring); EXPORT_SYMBOL_GPL(xenbus_grant_ring);
...@@ -419,62 +449,130 @@ EXPORT_SYMBOL_GPL(xenbus_free_evtchn); ...@@ -419,62 +449,130 @@ EXPORT_SYMBOL_GPL(xenbus_free_evtchn);
/** /**
* xenbus_map_ring_valloc * xenbus_map_ring_valloc
* @dev: xenbus device * @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 * @vaddr: pointer to address to be filled out by mapping
* *
* Based on Rusty Russell's skeleton driver's map_page. * Map @nr_grefs pages of memory into this domain from another
* Map a page of memory into this domain from another domain's grant table. * domain's grant table. xenbus_map_ring_valloc allocates @nr_grefs
* xenbus_map_ring_valloc allocates a page of virtual address space, maps the * pages of virtual address space, maps the pages to that address, and
* page to that address, and sets *vaddr to that address. * sets *vaddr to that address. Returns 0 on success, and GNTST_*
* Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h) * (see xen/include/interface/grant_table.h) or -ENOMEM / -EINVAL on
* or -ENOMEM on error. If an error is returned, device will switch to * error. If an error is returned, device will switch to
* XenbusStateClosing and the error message will be saved in XenStore. * 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); 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);
goto fail;
} else
handles[i] = map[i].handle;
}
return GNTST_okay;
fail:
for (i = j = 0; i < nr_grefs; i++) {
if (handles[i] != INVALID_GRANT_HANDLE) {
memset(&unmap[j], 0, sizeof(unmap[j]));
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();
*leaked = false;
for (i = 0; i < j; i++) {
if (unmap[i].status != GNTST_okay) {
*leaked = true;
break;
}
}
return err;
}
static int xenbus_map_ring_valloc_pv(struct xenbus_device *dev, static int xenbus_map_ring_valloc_pv(struct xenbus_device *dev,
int gnt_ref, void **vaddr) grant_ref_t *gnt_refs,
unsigned int nr_grefs,
void **vaddr)
{ {
struct gnttab_map_grant_ref op = {
.flags = GNTMAP_host_map | GNTMAP_contains_pte,
.ref = gnt_ref,
.dom = dev->otherend_id,
};
struct xenbus_map_node *node; struct xenbus_map_node *node;
struct vm_struct *area; struct vm_struct *area;
pte_t *pte; pte_t *ptes[XENBUS_MAX_RING_PAGES];
phys_addr_t phys_addrs[XENBUS_MAX_RING_PAGES];
int err = GNTST_okay;
int i;
bool leaked;
*vaddr = NULL; *vaddr = NULL;
if (nr_grefs > XENBUS_MAX_RING_PAGES)
return -EINVAL;
node = kzalloc(sizeof(*node), GFP_KERNEL); node = kzalloc(sizeof(*node), GFP_KERNEL);
if (!node) if (!node)
return -ENOMEM; return -ENOMEM;
area = alloc_vm_area(PAGE_SIZE, &pte); area = alloc_vm_area(PAGE_SIZE * nr_grefs, ptes);
if (!area) { if (!area) {
kfree(node); kfree(node);
return -ENOMEM; return -ENOMEM;
} }
op.host_addr = arbitrary_virt_to_machine(pte).maddr; for (i = 0; i < nr_grefs; i++)
phys_addrs[i] = arbitrary_virt_to_machine(ptes[i]).maddr;
gnttab_batch_map(&op, 1);
if (op.status != GNTST_okay) { err = __xenbus_map_ring(dev, gnt_refs, nr_grefs, node->handles,
free_vm_area(area); phys_addrs,
kfree(node); GNTMAP_host_map | GNTMAP_contains_pte,
xenbus_dev_fatal(dev, op.status, &leaked);
"mapping in shared page %d from domain %d", if (err)
gnt_ref, dev->otherend_id); goto failed;
return op.status;
}
node->handle = op.handle; node->nr_handles = nr_grefs;
node->area = area; node->pv.area = area;
spin_lock(&xenbus_valloc_lock); spin_lock(&xenbus_valloc_lock);
list_add(&node->next, &xenbus_valloc_pages); list_add(&node->next, &xenbus_valloc_pages);
...@@ -482,14 +580,33 @@ static int xenbus_map_ring_valloc_pv(struct xenbus_device *dev, ...@@ -482,14 +580,33 @@ static int xenbus_map_ring_valloc_pv(struct xenbus_device *dev,
*vaddr = area->addr; *vaddr = area->addr;
return 0; return 0;
failed:
if (!leaked)
free_vm_area(area);
else
pr_alert("leaking VM area %p size %u page(s)", area, nr_grefs);
kfree(node);
return err;
} }
static int xenbus_map_ring_valloc_hvm(struct xenbus_device *dev, static int xenbus_map_ring_valloc_hvm(struct xenbus_device *dev,
int gnt_ref, void **vaddr) grant_ref_t *gnt_ref,
unsigned int nr_grefs,
void **vaddr)
{ {
struct xenbus_map_node *node; struct xenbus_map_node *node;
int i;
int err; int err;
void *addr; void *addr;
bool leaked = false;
/* Why do we need two arrays? See comment of __xenbus_map_ring */
phys_addr_t phys_addrs[XENBUS_MAX_RING_PAGES];
unsigned long addrs[XENBUS_MAX_RING_PAGES];
if (nr_grefs > XENBUS_MAX_RING_PAGES)
return -EINVAL;
*vaddr = NULL; *vaddr = NULL;
...@@ -497,15 +614,32 @@ static int xenbus_map_ring_valloc_hvm(struct xenbus_device *dev, ...@@ -497,15 +614,32 @@ static int xenbus_map_ring_valloc_hvm(struct xenbus_device *dev,
if (!node) if (!node)
return -ENOMEM; return -ENOMEM;
err = alloc_xenballooned_pages(1, &node->page, false /* lowmem */); err = alloc_xenballooned_pages(nr_grefs, node->hvm.pages,
false /* lowmem */);
if (err) if (err)
goto out_err; goto out_err;
addr = pfn_to_kaddr(page_to_pfn(node->page)); for (i = 0; i < nr_grefs; i++) {
unsigned long pfn = page_to_pfn(node->hvm.pages[i]);
phys_addrs[i] = (unsigned long)pfn_to_kaddr(pfn);
addrs[i] = (unsigned long)pfn_to_kaddr(pfn);
}
err = __xenbus_map_ring(dev, gnt_ref, nr_grefs, node->handles,
phys_addrs, GNTMAP_host_map, &leaked);
node->nr_handles = nr_grefs;
err = xenbus_map_ring(dev, gnt_ref, &node->handle, addr);
if (err) if (err)
goto out_err_free_ballooned_pages; goto out_free_ballooned_pages;
addr = vmap(node->hvm.pages, nr_grefs, VM_MAP | VM_IOREMAP,
PAGE_KERNEL);
if (!addr) {
err = -ENOMEM;
goto out_xenbus_unmap_ring;
}
node->hvm.addr = addr;
spin_lock(&xenbus_valloc_lock); spin_lock(&xenbus_valloc_lock);
list_add(&node->next, &xenbus_valloc_pages); list_add(&node->next, &xenbus_valloc_pages);
...@@ -514,8 +648,16 @@ static int xenbus_map_ring_valloc_hvm(struct xenbus_device *dev, ...@@ -514,8 +648,16 @@ static int xenbus_map_ring_valloc_hvm(struct xenbus_device *dev,
*vaddr = addr; *vaddr = addr;
return 0; return 0;
out_err_free_ballooned_pages: out_xenbus_unmap_ring:
free_xenballooned_pages(1, &node->page); if (!leaked)
xenbus_unmap_ring(dev, node->handles, node->nr_handles,
addrs);
else
pr_alert("leaking %p size %u page(s)",
addr, nr_grefs);
out_free_ballooned_pages:
if (!leaked)
free_xenballooned_pages(nr_grefs, node->hvm.pages);
out_err: out_err:
kfree(node); kfree(node);
return err; return err;
...@@ -525,35 +667,37 @@ static int xenbus_map_ring_valloc_hvm(struct xenbus_device *dev, ...@@ -525,35 +667,37 @@ static int xenbus_map_ring_valloc_hvm(struct xenbus_device *dev,
/** /**
* xenbus_map_ring * xenbus_map_ring
* @dev: xenbus device * @dev: xenbus device
* @gnt_ref: grant reference * @gnt_refs: grant reference array
* @handle: pointer to grant handle to be filled * @nr_grefs: number of grant reference
* @vaddr: address to be mapped to * @handles: pointer to grant handle to be filled
* @vaddrs: addresses to be mapped to
* @leaked: fail to clean up a failed map, caller should not free vaddr
* *
* Map a page of memory into this domain from another domain's grant table. * Map pages of memory into this domain from another domain's grant table.
* xenbus_map_ring does not allocate the virtual address space (you must do * xenbus_map_ring does not allocate the virtual address space (you must do
* this yourself!). It only maps in the page to the specified address. * this yourself!). It only maps in the pages to the specified address.
* Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h) * 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 * or -ENOMEM / -EINVAL on error. If an error is returned, device will switch to
* XenbusStateClosing and the error message will be saved in XenStore. * XenbusStateClosing and the first error message will be saved in XenStore.
* Further more if we fail to map the ring, caller should check @leaked.
* If @leaked is not zero it means xenbus_map_ring fails to clean up, caller
* should not free the address space of @vaddr.
*/ */
int xenbus_map_ring(struct xenbus_device *dev, int gnt_ref, int xenbus_map_ring(struct xenbus_device *dev, grant_ref_t *gnt_refs,
grant_handle_t *handle, void *vaddr) unsigned int nr_grefs, grant_handle_t *handles,
unsigned long *vaddrs, bool *leaked)
{ {
struct gnttab_map_grant_ref op; phys_addr_t phys_addrs[XENBUS_MAX_RING_PAGES];
int i;
gnttab_set_map_op(&op, (unsigned long)vaddr, GNTMAP_host_map, gnt_ref,
dev->otherend_id);
gnttab_batch_map(&op, 1); if (nr_grefs > XENBUS_MAX_RING_PAGES)
return -EINVAL;
if (op.status != GNTST_okay) { for (i = 0; i < nr_grefs; i++)
xenbus_dev_fatal(dev, op.status, phys_addrs[i] = (unsigned long)vaddrs[i];
"mapping in shared page %d from domain %d",
gnt_ref, dev->otherend_id);
} else
*handle = op.handle;
return op.status; return __xenbus_map_ring(dev, gnt_refs, nr_grefs, handles,
phys_addrs, GNTMAP_host_map, leaked);
} }
EXPORT_SYMBOL_GPL(xenbus_map_ring); EXPORT_SYMBOL_GPL(xenbus_map_ring);
...@@ -579,14 +723,15 @@ EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree); ...@@ -579,14 +723,15 @@ EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree);
static int xenbus_unmap_ring_vfree_pv(struct xenbus_device *dev, void *vaddr) static int xenbus_unmap_ring_vfree_pv(struct xenbus_device *dev, void *vaddr)
{ {
struct xenbus_map_node *node; struct xenbus_map_node *node;
struct gnttab_unmap_grant_ref op = { struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_PAGES];
.host_addr = (unsigned long)vaddr,
};
unsigned int level; unsigned int level;
int i;
bool leaked = false;
int err;
spin_lock(&xenbus_valloc_lock); spin_lock(&xenbus_valloc_lock);
list_for_each_entry(node, &xenbus_valloc_pages, next) { list_for_each_entry(node, &xenbus_valloc_pages, next) {
if (node->area->addr == vaddr) { if (node->pv.area->addr == vaddr) {
list_del(&node->next); list_del(&node->next);
goto found; goto found;
} }
...@@ -601,22 +746,41 @@ static int xenbus_unmap_ring_vfree_pv(struct xenbus_device *dev, void *vaddr) ...@@ -601,22 +746,41 @@ static int xenbus_unmap_ring_vfree_pv(struct xenbus_device *dev, void *vaddr)
return GNTST_bad_virt_addr; return GNTST_bad_virt_addr;
} }
op.handle = node->handle; for (i = 0; i < node->nr_handles; i++) {
op.host_addr = arbitrary_virt_to_machine( unsigned long addr;
lookup_address((unsigned long)vaddr, &level)).maddr;
if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1)) memset(&unmap[i], 0, sizeof(unmap[i]));
addr = (unsigned long)vaddr + (PAGE_SIZE * i);
unmap[i].host_addr = arbitrary_virt_to_machine(
lookup_address(addr, &level)).maddr;
unmap[i].dev_bus_addr = 0;
unmap[i].handle = node->handles[i];
}
if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap, i))
BUG(); BUG();
if (op.status == GNTST_okay) err = GNTST_okay;
free_vm_area(node->area); leaked = false;
else for (i = 0; i < node->nr_handles; i++) {
xenbus_dev_error(dev, op.status, if (unmap[i].status != GNTST_okay) {
leaked = true;
xenbus_dev_error(dev, unmap[i].status,
"unmapping page at handle %d error %d", "unmapping page at handle %d error %d",
node->handle, op.status); node->handles[i], unmap[i].status);
err = unmap[i].status;
break;
}
}
if (!leaked)
free_vm_area(node->pv.area);
else
pr_alert("leaking VM area %p size %u page(s)",
node->pv.area, node->nr_handles);
kfree(node); kfree(node);
return op.status; return err;
} }
static int xenbus_unmap_ring_vfree_hvm(struct xenbus_device *dev, void *vaddr) static int xenbus_unmap_ring_vfree_hvm(struct xenbus_device *dev, void *vaddr)
...@@ -624,10 +788,12 @@ static int xenbus_unmap_ring_vfree_hvm(struct xenbus_device *dev, void *vaddr) ...@@ -624,10 +788,12 @@ static int xenbus_unmap_ring_vfree_hvm(struct xenbus_device *dev, void *vaddr)
int rv; int rv;
struct xenbus_map_node *node; struct xenbus_map_node *node;
void *addr; void *addr;
unsigned long addrs[XENBUS_MAX_RING_PAGES];
int i;
spin_lock(&xenbus_valloc_lock); spin_lock(&xenbus_valloc_lock);
list_for_each_entry(node, &xenbus_valloc_pages, next) { list_for_each_entry(node, &xenbus_valloc_pages, next) {
addr = pfn_to_kaddr(page_to_pfn(node->page)); addr = node->hvm.addr;
if (addr == vaddr) { if (addr == vaddr) {
list_del(&node->next); list_del(&node->next);
goto found; goto found;
...@@ -643,12 +809,16 @@ static int xenbus_unmap_ring_vfree_hvm(struct xenbus_device *dev, void *vaddr) ...@@ -643,12 +809,16 @@ static int xenbus_unmap_ring_vfree_hvm(struct xenbus_device *dev, void *vaddr)
return GNTST_bad_virt_addr; return GNTST_bad_virt_addr;
} }
rv = xenbus_unmap_ring(dev, node->handle, addr); for (i = 0; i < node->nr_handles; i++)
addrs[i] = (unsigned long)pfn_to_kaddr(page_to_pfn(node->hvm.pages[i]));
rv = xenbus_unmap_ring(dev, node->handles, node->nr_handles,
addrs);
if (!rv) if (!rv)
free_xenballooned_pages(1, &node->page); vunmap(vaddr);
else else
WARN(1, "Leaking %p\n", vaddr); WARN(1, "Leaking %p, size %u page(s)\n", vaddr,
node->nr_handles);
kfree(node); kfree(node);
return rv; return rv;
...@@ -657,29 +827,44 @@ static int xenbus_unmap_ring_vfree_hvm(struct xenbus_device *dev, void *vaddr) ...@@ -657,29 +827,44 @@ static int xenbus_unmap_ring_vfree_hvm(struct xenbus_device *dev, void *vaddr)
/** /**
* xenbus_unmap_ring * xenbus_unmap_ring
* @dev: xenbus device * @dev: xenbus device
* @handle: grant handle * @handles: grant handle array
* @vaddr: addr to unmap * @nr_handles: number of handles in the array
* @vaddrs: addresses to unmap
* *
* Unmap a page of memory in this domain that was imported from another domain. * Unmap memory in this domain that was imported from another domain.
* Returns 0 on success and returns GNTST_* on error * Returns 0 on success and returns GNTST_* on error
* (see xen/include/interface/grant_table.h). * (see xen/include/interface/grant_table.h).
*/ */
int xenbus_unmap_ring(struct xenbus_device *dev, 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)
{ {
struct gnttab_unmap_grant_ref op; struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_PAGES];
int i;
int err;
if (nr_handles > XENBUS_MAX_RING_PAGES)
return -EINVAL;
gnttab_set_unmap_op(&op, (unsigned long)vaddr, GNTMAP_host_map, handle); for (i = 0; i < nr_handles; i++)
gnttab_set_unmap_op(&unmap[i], vaddrs[i],
GNTMAP_host_map, handles[i]);
if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1)) if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap, i))
BUG(); BUG();
if (op.status != GNTST_okay) err = GNTST_okay;
xenbus_dev_error(dev, op.status, for (i = 0; i < nr_handles; i++) {
if (unmap[i].status != GNTST_okay) {
xenbus_dev_error(dev, unmap[i].status,
"unmapping page at handle %d error %d", "unmapping page at handle %d error %d",
handle, op.status); handles[i], unmap[i].status);
err = unmap[i].status;
break;
}
}
return op.status; return err;
} }
EXPORT_SYMBOL_GPL(xenbus_unmap_ring); EXPORT_SYMBOL_GPL(xenbus_unmap_ring);
......
...@@ -46,6 +46,10 @@ ...@@ -46,6 +46,10 @@
#include <xen/interface/io/xenbus.h> #include <xen/interface/io/xenbus.h>
#include <xen/interface/io/xs_wire.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. */ /* Register callback to watch this node. */
struct xenbus_watch struct xenbus_watch
{ {
...@@ -199,15 +203,19 @@ int xenbus_watch_pathfmt(struct xenbus_device *dev, struct xenbus_watch *watch, ...@@ -199,15 +203,19 @@ int xenbus_watch_pathfmt(struct xenbus_device *dev, struct xenbus_watch *watch,
const char *pathfmt, ...); const char *pathfmt, ...);
int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state new_state); 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_grant_ring(struct xenbus_device *dev, void *vaddr,
int xenbus_map_ring_valloc(struct xenbus_device *dev, unsigned int nr_pages, grant_ref_t *grefs);
int gnt_ref, void **vaddr); int xenbus_map_ring_valloc(struct xenbus_device *dev, grant_ref_t *gnt_refs,
int xenbus_map_ring(struct xenbus_device *dev, int gnt_ref, unsigned int nr_grefs, void **vaddr);
grant_handle_t *handle, 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_vfree(struct xenbus_device *dev, void *vaddr);
int xenbus_unmap_ring(struct xenbus_device *dev, 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_alloc_evtchn(struct xenbus_device *dev, int *port);
int xenbus_free_evtchn(struct xenbus_device *dev, int port); int xenbus_free_evtchn(struct xenbus_device *dev, int port);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment