[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH 1/6 v2] xenbus: Support HVM backends



Add HVM implementations of xenbus_(map,unmap)_ring_v(alloc,free) so
that ring mappings can be done without using GNTMAP_contains_pte which
is not supported on HVM. This also removes the need to use vmlist_lock
on PV by tracking the allocated xenbus rings.

Signed-off-by: Daniel De Graaf <dgdegra@xxxxxxxxxxxxx>
---
 drivers/xen/xenbus/xenbus_client.c |  209 +++++++++++++++++++++++++++---------
 1 files changed, 160 insertions(+), 49 deletions(-)

diff --git a/drivers/xen/xenbus/xenbus_client.c 
b/drivers/xen/xenbus/xenbus_client.c
index 1906125..367decf 100644
--- a/drivers/xen/xenbus/xenbus_client.c
+++ b/drivers/xen/xenbus/xenbus_client.c
@@ -32,16 +32,30 @@
 
 #include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/spinlock.h>
 #include <linux/vmalloc.h>
 #include <linux/export.h>
 #include <asm/xen/hypervisor.h>
 #include <asm/xen/page.h>
 #include <xen/interface/xen.h>
 #include <xen/interface/event_channel.h>
+#include <xen/balloon.h>
 #include <xen/events.h>
 #include <xen/grant_table.h>
 #include <xen/xenbus.h>
 
+struct xenbus_map_node {
+       struct list_head next;
+       union {
+               struct vm_struct *area; /* PV */
+               struct page *page;     /* HVM */
+       };
+       grant_handle_t handle;
+};
+
+static DEFINE_SPINLOCK(xenbus_valloc_lock);
+static LIST_HEAD(xenbus_valloc_pages);
+
 const char *xenbus_strstate(enum xenbus_state state)
 {
        static const char *const name[] = {
@@ -420,35 +434,29 @@ int xenbus_free_evtchn(struct xenbus_device *dev, int 
port)
 EXPORT_SYMBOL_GPL(xenbus_free_evtchn);
 
 
-/**
- * xenbus_map_ring_valloc
- * @dev: xenbus device
- * @gnt_ref: grant reference
- * @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
- * XenbusStateClosing and the error message will be saved in XenStore.
- */
-int xenbus_map_ring_valloc(struct xenbus_device *dev, int gnt_ref, void 
**vaddr)
+static int xenbus_map_ring_valloc_pv(struct xenbus_device *dev,
+                                    int gnt_ref, 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 vm_struct *area;
        pte_t *pte;
 
        *vaddr = NULL;
 
+       node = kzalloc(sizeof(*node), GFP_KERNEL);
+       if (!node)
+               return -ENOMEM;
+
        area = alloc_vm_area(PAGE_SIZE, &pte);
-       if (!area)
+       if (!area) {
+               kfree(node);
                return -ENOMEM;
+       }
 
        op.host_addr = arbitrary_virt_to_machine(pte).maddr;
 
@@ -457,18 +465,81 @@ int xenbus_map_ring_valloc(struct xenbus_device *dev, int 
gnt_ref, void **vaddr)
 
        if (op.status != GNTST_okay) {
                free_vm_area(area);
+               kfree(node);
                xenbus_dev_fatal(dev, op.status,
                                 "mapping in shared page %d from domain %d",
                                 gnt_ref, dev->otherend_id);
                return op.status;
        }
 
-       /* Stuff the handle in an unused field */
-       area->phys_addr = (unsigned long)op.handle;
+       node->handle = op.handle;
+       node->area = area;
+
+       spin_lock(&xenbus_valloc_lock);
+       list_add(&node->next, &xenbus_valloc_pages);
+       spin_unlock(&xenbus_valloc_lock);
 
        *vaddr = area->addr;
        return 0;
 }
+
+static int xenbus_map_ring_valloc_hvm(struct xenbus_device *dev,
+                                     int gnt_ref, void **vaddr)
+{
+       struct xenbus_map_node *node;
+       int err;
+       void *addr;
+
+       *vaddr = NULL;
+
+       node = kzalloc(sizeof(*node), GFP_KERNEL);
+       if (!node)
+               return -ENOMEM;
+
+       err = alloc_xenballooned_pages(1, &node->page, false /* lowmem */);
+       if (err)
+               goto out_err;
+
+       addr = pfn_to_kaddr(page_to_pfn(node->page));
+
+       err = xenbus_map_ring(dev, gnt_ref, &node->handle, addr);
+       if (err)
+               goto out_err;
+
+       spin_lock(&xenbus_valloc_lock);
+       list_add(&node->next, &xenbus_valloc_pages);
+       spin_unlock(&xenbus_valloc_lock);
+
+       *vaddr = addr;
+       return 0;
+
+ out_err:
+       free_xenballooned_pages(1, &node->page);
+       kfree(node);
+       return err;
+}
+
+/**
+ * xenbus_map_ring_valloc
+ * @dev: xenbus device
+ * @gnt_ref: grant reference
+ * @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
+ * XenbusStateClosing and the error message will be saved in XenStore.
+ */
+int xenbus_map_ring_valloc(struct xenbus_device *dev, int gnt_ref, void 
**vaddr)
+{
+       if (xen_pv_domain())
+               return xenbus_map_ring_valloc_pv(dev, gnt_ref, vaddr);
+       else
+               return xenbus_map_ring_valloc_hvm(dev, gnt_ref, vaddr);
+}
 EXPORT_SYMBOL_GPL(xenbus_map_ring_valloc);
 
 
@@ -510,47 +581,32 @@ int xenbus_map_ring(struct xenbus_device *dev, int 
gnt_ref,
 }
 EXPORT_SYMBOL_GPL(xenbus_map_ring);
 
-
-/**
- * xenbus_unmap_ring_vfree
- * @dev: xenbus device
- * @vaddr: addr to unmap
- *
- * Based on Rusty Russell's skeleton driver's unmap_page.
- * Unmap a page of memory in this domain that was imported from another domain.
- * Use xenbus_unmap_ring_vfree if you mapped in your memory with
- * xenbus_map_ring_valloc (it will free the virtual address space).
- * Returns 0 on success and returns GNTST_* on error
- * (see xen/include/interface/grant_table.h).
- */
-int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr)
+static int xenbus_unmap_ring_vfree_pv(struct xenbus_device *dev, void *vaddr)
 {
-       struct vm_struct *area;
+       struct xenbus_map_node *node;
        struct gnttab_unmap_grant_ref op = {
                .host_addr = (unsigned long)vaddr,
        };
        unsigned int level;
 
-       /* It'd be nice if linux/vmalloc.h provided a find_vm_area(void *addr)
-        * method so that we don't have to muck with vmalloc internals here.
-        * We could force the user to hang on to their struct vm_struct from
-        * xenbus_map_ring_valloc, but these 6 lines considerably simplify
-        * this API.
-        */
-       read_lock(&vmlist_lock);
-       for (area = vmlist; area != NULL; area = area->next) {
-               if (area->addr == vaddr)
-                       break;
+       spin_lock(&xenbus_valloc_lock);
+       list_for_each_entry(node, &xenbus_valloc_pages, next) {
+               if (node->area->addr == vaddr) {
+                       list_del(&node->next);
+                       goto found;
+               }
        }
-       read_unlock(&vmlist_lock);
+       node = NULL;
+ found:
+       spin_unlock(&xenbus_valloc_lock);
 
-       if (!area) {
+       if (!node) {
                xenbus_dev_error(dev, -ENOENT,
                                 "can't find mapped virtual address %p", vaddr);
                return GNTST_bad_virt_addr;
        }
 
-       op.handle = (grant_handle_t)area->phys_addr;
+       op.handle = node->handle;
        op.host_addr = arbitrary_virt_to_machine(
                lookup_address((unsigned long)vaddr, &level)).maddr;
 
@@ -558,16 +614,71 @@ int xenbus_unmap_ring_vfree(struct xenbus_device *dev, 
void *vaddr)
                BUG();
 
        if (op.status == GNTST_okay)
-               free_vm_area(area);
+               free_vm_area(node->area);
        else
                xenbus_dev_error(dev, op.status,
                                 "unmapping page at handle %d error %d",
-                                (int16_t)area->phys_addr, op.status);
+                                node->handle, op.status);
 
+       kfree(node);
        return op.status;
 }
-EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree);
 
+static int xenbus_unmap_ring_vfree_hvm(struct xenbus_device *dev, void *vaddr)
+{
+       int rv;
+       struct xenbus_map_node *node;
+       void *addr;
+
+       spin_lock(&xenbus_valloc_lock);
+       list_for_each_entry(node, &xenbus_valloc_pages, next) {
+               addr = pfn_to_kaddr(page_to_pfn(node->page));
+               if (addr == vaddr) {
+                       list_del(&node->next);
+                       goto found;
+               }
+       }
+       node = NULL;
+ found:
+       spin_unlock(&xenbus_valloc_lock);
+
+       if (!node) {
+               xenbus_dev_error(dev, -ENOENT,
+                                "can't find mapped virtual address %p", vaddr);
+               return GNTST_bad_virt_addr;
+       }
+
+       rv = xenbus_unmap_ring(dev, node->handle, addr);
+
+       if (!rv)
+               free_xenballooned_pages(1, &node->page);
+       else
+               WARN(1, "Leaking %p\n", vaddr);
+
+       kfree(node);
+       return rv;
+}
+
+/**
+ * xenbus_unmap_ring_vfree
+ * @dev: xenbus device
+ * @vaddr: addr to unmap
+ *
+ * Based on Rusty Russell's skeleton driver's unmap_page.
+ * Unmap a page of memory in this domain that was imported from another domain.
+ * Use xenbus_unmap_ring_vfree if you mapped in your memory with
+ * xenbus_map_ring_valloc (it will free the virtual address space).
+ * Returns 0 on success and returns GNTST_* on error
+ * (see xen/include/interface/grant_table.h).
+ */
+int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr)
+{
+       if (xen_pv_domain())
+               return xenbus_unmap_ring_vfree_pv(dev, vaddr);
+       else
+               return xenbus_unmap_ring_vfree_hvm(dev, vaddr);
+}
+EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree);
 
 /**
  * xenbus_unmap_ring
-- 
1.7.7.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.