xen: don't call vmalloc_sync_all() when mapping foreign pages
Subsystems (9)
cover xen: don't call vmalloc_sync_all() when mapping foreign pages
This set of pages avoids the need to call vmalloc_sync_all() when mapping foreign pages. Two new functions are adding for mapping foreign pages onto RAM pages instead of vmalloc space.
This does waste a page of RAM for each mapped page. In the future a ballooned page could be used instead (once the API for getting a ballooned page with the right GFP flags is available).
David
PATCH 1/6 xen: add functions for mapping foreign pages over pages
From: David Vrabel <[email protected]>
Add xenbus_map_ring_page() and xenbus_unmap_ring_page() which map and unmap foreign pages over a page instead of mapping them into vmalloc address space. This avoids having to do an expensive vmalloc_sync_all().
--- drivers/xen/xenbus/xenbus_client.c | 68 ++++++++++++++++++++++++++++++++++++ include/xen/xenbus.h | 3 ++ 2 files changed, 71 insertions(+), 0 deletions(-)
drivers/xen/xenbus/xenbus_client.c +68 -0
@@ -32,6 +32,7 @@#include <linux/slab.h> #include <linux/types.h> +#include <linux/mm.h> #include <linux/vmalloc.h> #include <asm/xen/hypervisor.h> #include <xen/interface/xen.h>
@@ -39,6 +40,7 @@#include <xen/events.h> #include <xen/grant_table.h> #include <xen/xenbus.h> +#include <xen/page.h> const char *xenbus_strstate (enum xenbus_state state) {
@@ -509,6 +511,44 @@ EXPORT_SYMBOL_GPL(xenbus_map_ring);/** +* xenbus_map_ring_page - map a foreign page into a kernel page +* @dev: xenbus device +* @gnt_ref: grant reference +* @page: return the page the foreign page has been mapped to +* +* Map a foreign page from another domain's grant table into a newly +* allocated page in this domain. The page must be unmapped with +* xenbus_unmap_ring_page(). +* +* Returns 0 on success, and -ENOMEM or GNTST_* (see +* include/xen/interface/grant_table.h) on error. +*/ +int xenbus_map_ring_page (struct xenbus_device *dev,int gnt_ref, +struct page **page) +{ +struct page *new_page; +grant_ref_t handle; +int ret; + + new_page =alloc_page (GFP_KERNEL ); +if (!new_page) +return -ENOMEM ; + + ret =xenbus_map_ring (dev, gnt_ref, &handle,page_address (new_page)); +if (ret <0 ) + goto err; + + new_page->private = handle; + *page = new_page; +return 0 ; + +err: +__free_page (new_page); +return ret; +} +EXPORT_SYMBOL_GPL (xenbus_map_ring_page); + +/** * xenbus_unmap_ring_vfree * @dev: xenbus device * @vaddr: addr to unmap
@@ -593,6 +633,34 @@ int xenbus_unmap_ring(struct xenbus_device *dev, }EXPORT_SYMBOL_GPL (xenbus_unmap_ring); +/** +* xenbus_unmap_ring_page - unmap an foreign page from a kernel page +* @dev: xenbus device +* @page: page the foreign page was mapped to +* +* Unmap a foreign page previously mapped with xenbus_map_ring_page(). +* The page is freed. +*/ +void xenbus_unmap_ring_page (struct xenbus_device *dev,struct page *page) +{ +int ret; + + ret =xenbus_unmap_ring (dev, page->private,page_address (page)); +if (ret <0 ) +return ; + +/* +* Restore the original PTE of this page before freeing it. +*/ + ret =HYPERVISOR_update_va_mapping ( + (unsigned long )page_address (page), +mfn_pte (get_phys_to_machine (page_to_pfn (page)),PAGE_KERNEL ), +0 ); +BUG_ON (ret); + +__free_page (page); +} +EXPORT_SYMBOL_GPL (xenbus_unmap_ring_page);/** * xenbus_read_driver_state
include/xen/xenbus.h +3 -0
@@ -212,10 +212,13 @@ 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_map_ring_page (struct xenbus_device *dev,int gnt_ref, +struct page **page);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); +void xenbus_unmap_ring_page (struct xenbus_device *dev,struct page *page);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.2.5
PATCH 2/6 block: xen-blkback: use API provided by xenbus module to map rings
In reply to David VrabelFrom: David Vrabel <[email protected]>
The xenbus module provides xenbus_map_ring_page() and xenbus_map_ring_page(). Use these to map the ring pages granted by the frontend.
--- drivers/block/xen-blkback/common.h | 5 +-- drivers/block/xen-blkback/xenbus.c | 55 +++++------------------------------ 2 files changed, 9 insertions(+), 51 deletions(-)
drivers/block/xen-blkback/common.h +1 -4
@@ -139,7 +139,7 @@ struct xen_blkif {/* Comms information. */ enum blkif_protocol blk_protocol;union blkif_back_rings blk_rings; -struct vm_struct *blk_ring_area; +struct page *blk_ring_page;/* The VBD attached to this interface. */ struct xen_vbd vbd;/* Back pointer to the backend_info. */
@@ -163,9 +163,6 @@ struct xen_blkif {int st_wr_sect;wait_queue_head_t waiting_to_free; - -grant_handle_t shmem_handle; -grant_ref_t shmem_ref; };
drivers/block/xen-blkback/xenbus.c +8 -47
97 lines not on this page · raw message
Signature · 1 line
1.7.2.5
PATCH 3/6 net: xen-netback: use API provided by xenbus module to map rings
In reply to David VrabelFrom: David Vrabel <[email protected]>
The xenbus module provides xenbus_map_ring_page() and xenbus_map_ring_page(). Use these to map the ring pages granted by the frontend.
--- drivers/net/xen-netback/common.h | 13 +++--- drivers/net/xen-netback/netback.c | 79 +++++++----------------------------- 2 files changed, 23 insertions(+), 69 deletions(-)
drivers/net/xen-netback/common.h +7 -6
@@ -58,10 +58,6 @@ struct xenvif {u8 fe_dev_addr[6 ];/* Physical parameters of the comms window. */ -grant_handle_t tx_shmem_handle; -grant_ref_t tx_shmem_ref; -grant_handle_t rx_shmem_handle; -grant_ref_t rx_shmem_ref;unsigned int irq;/* List of frontends to notify after a batch of frames sent. */
@@ -70,8 +66,8 @@ struct xenvif {/* The shared rings and indexes. */ struct xen_netif_tx_back_ring tx;struct xen_netif_rx_back_ring rx; -struct vm_struct *tx_comms_area; -struct vm_struct *rx_comms_area; +struct page *tx_ring_page; +struct page *rx_ring_page;/* Frontend feature information. */ u8 can_sg:1;
@@ -106,6 +102,11 @@ struct xenvif {wait_queue_head_t waiting_to_free; }; +static inline struct xenbus_device *xenvif_to_xenbus_device (struct xenvif *vif) +{ +return to_xenbus_device (vif->dev->dev.parent); +} +#define XEN_NETIF_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, PAGE_SIZE)#define XEN_NETIF_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, PAGE_SIZE)
drivers/net/xen-netback/netback.c +16 -63
104 lines not on this page · raw message
Signature · 1 line
1.7.2.5
PATCH 4/6 xen: xen-pciback: use xenbus_map_ring_page() to map rings
In reply to David VrabelFrom: David Vrabel <[email protected]>
xenbus_map_ring_valloc() is deprecated in favour of xenbus_map_ring_page().
--- drivers/xen/xen-pciback/pciback.h | 1 + drivers/xen/xen-pciback/xenbus.c | 7 +++---- 2 files changed, 4 insertions(+), 4 deletions(-)
drivers/xen/xen-pciback/pciback.h +1 -0
@@ -32,6 +32,7 @@ struct xen_pcibk_device {struct xenbus_watch be_watch;u8 be_watching;int evtchn_irq; +struct page *ring_page;struct xen_pci_sharedinfo *sh_info;unsigned long flags;struct work_struct op_work;
drivers/xen/xen-pciback/xenbus.c +3 -4
@@ -79,7 +79,7 @@ static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)spin_lock (&pdev->dev_lock);if (pdev->sh_info !=NULL ) { -xenbus_unmap_ring_vfree (pdev->xdev, pdev->sh_info); +xenbus_unmap_ring_page (pdev->xdev, pdev->ring_page); pdev->sh_info =NULL ; }spin_unlock (&pdev->dev_lock);
@@ -107,13 +107,12 @@ static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref, int remote_evtchn) {int err =0 ; -void *vaddr;dev_dbg (&pdev->xdev->dev,"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_page (pdev->xdev, gnt_ref, &pdev->ring_page);if (err <0 ) {xenbus_dev_fatal (pdev->xdev, err,"Error mapping other domain page in ours." );
@@ -121,7 +120,7 @@ static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref, }spin_lock (&pdev->dev_lock); - pdev->sh_info = vaddr; + pdev->sh_info =page_address (pdev->ring_page);spin_unlock (&pdev->dev_lock); err = bind_interdomain_evtchn_to_irqhandler(
Signature · 1 line
1.7.2.5
PATCH 5/6 xen: xenbus: remove xenbus_map_ring_valloc() and xenbus_map_ring_vfree()
In reply to David VrabelFrom: David Vrabel <[email protected]>
xenbus_map_ring_valloc() and xenbus_map_ring_vfree() are no longer used. Drivers should use xenbus_map_ring_page() instead.
xenbus_map_ring() and xenbus_unmap_ring also have no users outside of the file and are made static.
--- arch/ia64/xen/grant-table.c | 2 +- drivers/xen/xenbus/xenbus_client.c | 119 ++---------------------------------- include/xen/xenbus.h | 9 +--- 3 files changed, 7 insertions(+), 123 deletions(-)
arch/ia64/xen/grant-table.c +1 -1
@@ -54,7 +54,7 @@ struct vm_struct *xen_alloc_vm_area(unsigned long size) area->size = size; area->pages =NULL ; area->nr_pages = nr_pages; - area->phys_addr =0 ;/* xenbus_map_ring_valloc uses this field! */ + area->phys_addr =0 ;return area;
drivers/xen/xenbus/xenbus_client.c +5 -114
160 lines not on this page · raw message
include/xen/xenbus.h +1 -8
@@ -208,16 +208,9 @@ int xenbus_watch_pathfmt(struct xenbus_device *dev, struct xenbus_watch *watch,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_map_ring_page (struct xenbus_device *dev,int gnt_ref,struct page **page); - -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);void xenbus_unmap_ring_page (struct xenbus_device *dev,struct page *page);int xenbus_alloc_evtchn (struct xenbus_device *dev,int *port);
Signature · 1 line
1.7.2.5
PATCH 6/6 mm: remove vmalloc_sync_all() from alloc_vm_area()
In reply to David VrabelFrom: David Vrabel <[email protected]>
The address space allocated by the remaining user of alloc_vm_area() is not accessed during a hypercall. Therefore, the normal mechanism of populating the vmalloc page tables during a fault works so the vmalloc_sync_all() is not required and it can be removed.
--- mm/vmalloc.c | 12 +----------- 1 files changed, 1 insertions(+), 11 deletions(-)
mm/vmalloc.c +1 -11
@@ -2117,9 +2117,7 @@ static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)* * This function reserves a range of kernel address space, and * allocates pagetables to map that range. No actual mappings -* are created. If the kernel address space is not shared -* between processes, it syncs the pagetable across all -* processes. +* are created. */ struct vm_struct *alloc_vm_area (size_t size) {
@@ -2140,14 +2138,6 @@ struct vm_struct *alloc_vm_area(size_t size)return NULL ; } -/* -* If the allocated address space is passed to a hypercall -* before being used then we cannot rely on a page fault to -* trigger an update of the page tables. So sync all the page -* tables here. -*/ -vmalloc_sync_all (); -return area; }EXPORT_SYMBOL_GPL (alloc_vm_area);
Signature · 1 line
1.7.2.5
Quoted 7 lines · David Vrabel
This set of pages avoids the need to call
vmalloc_sync_all()when mapping foreign pages. Two new functions are adding for mapping foreign pages onto RAM pages instead of vmalloc space.This does waste a page of RAM for each mapped page. In the future a ballooned page could be used instead (once the API for getting a ballooned page with the right GFP flags is available).
You can allocate a pfn, free ("balloon out") the underlying mfn, and replace it with a granted page; it doesn't need any particular new infrastructure.
But that said, if you want to allocate virtual addresses for mapping granted pages, then alloc_vm_area() is the right thing to use. You need to make sure you touch the pages from within the kernel before passing those addresses to a hypercall to make sure the mappings are established within the current task (possibly within a no-preempt region to guarantee that nothing odd happens). Or alternatively, you could switch the current pagetable to init_mm for the hypercall (if it isn't already)
- since that's the reference pagetable - assuming you're not passing usermode virtual addresses to the hypercall.
This series is relying on regular ram mappings are already synced to all tasks, but I'm not sure that's necessarily guaranteed (for example, if you hotplug new memory into the domain, the new pages won't be mapped into every mm unless they're synced).
J
Quoted 5 lines · Jeremy Fitzhardinge
Quoted 4 lines · David Vrabel
This set of pages avoids the need to call
vmalloc_sync_all()when mapping foreign pages. Two new functions are adding for mapping foreign pages onto RAM pages instead of vmalloc space.
Quoted 7 lines
Quoted 3 lines
This does waste a page of RAM for each mapped page. In the future a ballooned page could be used instead (once the API for getting a ballooned page with the right GFP flags is available).
You can allocate a pfn, free ("balloon out") the underlying mfn, and replace it with a granted page; it doesn't need any particular new infrastructure.
David, give a look at drivers/xen/balloon.c:alloc_xenballooned_pages, in particular after my changes to it:
http://marc.info/?l=linux-kernel&m=131552369305998&w=2
it allows the caller to allocate a lowmem page and balloon it out right away.
Quoted 9 lines
But that said, if you want to allocate virtual addresses for mapping granted pages, then
alloc_vm_area()is the right thing to use. You need to make sure you touch the pages from within the kernel before passing those addresses to a hypercall to make sure the mappings are established within the current task (possibly within a no-preempt region to guarantee that nothing odd happens). Or alternatively, you could switch the current pagetable toinit_mmfor the hypercall (if it isn't already)
- since that's the reference pagetable - assuming you're not passing usermode virtual addresses to the hypercall.
This problem is not very different from the one we are trying to solve with alloc_xenballooned_pages: in case we want a page that is mapped in kernel space we just use allocate a lowmem page, balloon it out, change the p2m and add the page to the m2p_override.
Why should we solve that issue differently?
Quoted 4 lines
This series is relying on regular ram mappings are already synced to all tasks, but I'm not sure that's necessarily guaranteed (for example, if you hotplug new memory into the domain, the new pages won't be mapped into every mm unless they're synced).
the series is using GFP_KERNEL, so this problem shouldn't occur, right?
Quoted 6 lines · Stefano Stabellini
Quoted 4 lines · Jeremy Fitzhardinge
This series is relying on regular ram mappings are already synced to all tasks, but I'm not sure that's necessarily guaranteed (for example, if you hotplug new memory into the domain, the new pages won't be mapped into every mm unless they're synced).
the series is using
GFP_KERNEL, so this problem shouldn't occur, right?
What properties do you think GFP_KERNEL guarantees?
J
Quoted 9 lines · Jeremy Fitzhardinge
Quoted 6 lines · Stefano Stabellini
Quoted 4 lines · Jeremy Fitzhardinge
This series is relying on regular ram mappings are already synced to all tasks, but I'm not sure that's necessarily guaranteed (for example, if you hotplug new memory into the domain, the new pages won't be mapped into every mm unless they're synced).
the series is using
GFP_KERNEL, so this problem shouldn't occur, right?What properties do you think
GFP_KERNELguarantees?
That the memory is below 4G and always mapped in the kernel 1:1 region.
Regarding memory hotplug it looks like that x86_32 is mapping new memory ZONE_HIGHMEM, therefore avoiding any problems with GFP_KERNEL allocations. On the other hand x86_64 is mapping the memory ZONE_NORMAL and calling init_memory_mapping on the new range right away. AFAICT changes to the 1:1 mapping in init_mm are automatically synced across all mm's because the pgd is shared?
Quoted 10 lines · Stefano Stabellini
Quoted 8 lines · Jeremy Fitzhardinge
Quoted 6 lines · Stefano Stabellini
Quoted 4 lines · Jeremy Fitzhardinge
This series is relying on regular ram mappings are already synced to all tasks, but I'm not sure that's necessarily guaranteed (for example, if you hotplug new memory into the domain, the new pages won't be mapped into every mm unless they're synced).
the series is using
GFP_KERNEL, so this problem shouldn't occur, right?What properties do you think
GFP_KERNELguarantees?That the memory is below 4G and always mapped in the kernel 1:1 region.
Hm, but that's not quite the same thing as "mapped into every pagetable". Lowmem pages always have a kernel virtual address, and its always OK to touch them at any point in kernel code[*] because one can rely on the fault handler to create mappings as needed - but that doesn't mean they're necessarily mapped by present ptes in the current pagetable.
[*] - except NMI handlers
Quoted 6 lines
Regarding memory hotplug it looks like that
x86_32is mapping new memoryZONE_HIGHMEM, therefore avoiding any problems withGFP_KERNELallocations. On the other handx86_64is mapping the memoryZONE_NORMALand callinginit_memory_mappingon the new range right away. AFAICT changes to the 1:1 mapping ininit_mmare automatically synced across all mm's because the pgd is shared?
TBH I'm not sure. vmalloc_sync_one/all does seem to do *something* on 64-bit, but I was never completely sure what regions of the address space were already shared. I *think* it might be that the pgd and pud are not shared, but the pmd down is, so if you add a new pmd you need to sync it into all the puds (and puds into pgds if you add a new one of those).
But I'd be happier pretending that vmalloc_sync_* just doesn't exist, and deal with it at the hypercall level - in the short term, by just making sure that the callers touch all those pages before passing them into the hypercall.
J
Quoted 12 lines · Jeremy Fitzhardinge
Quoted 10 lines · Stefano Stabellini
Quoted 8 lines · Jeremy Fitzhardinge
Quoted 6 lines · Stefano Stabellini
Quoted 4 lines · Jeremy Fitzhardinge
This series is relying on regular ram mappings are already synced to all tasks, but I'm not sure that's necessarily guaranteed (for example, if you hotplug new memory into the domain, the new pages won't be mapped into every mm unless they're synced).
the series is using
GFP_KERNEL, so this problem shouldn't occur, right?What properties do you think
GFP_KERNELguarantees?That the memory is below 4G and always mapped in the kernel 1:1 region.
Quoted 8 lines
Hm, but that's not quite the same thing as "mapped into every pagetable". Lowmem pages always have a kernel virtual address, and its always OK to touch them at any point in kernel code[*] because one can rely on the fault handler to create mappings as needed - but that doesn't mean they're necessarily mapped by present ptes in the current pagetable.
[*] - except NMI handlers
Is that really true?
I quickly went through the fault handler and I couldn't see anything related to the kernel 1:1 region.
Quoted 14 lines
Quoted 6 lines
Regarding memory hotplug it looks like that
x86_32is mapping new memoryZONE_HIGHMEM, therefore avoiding any problems withGFP_KERNELallocations. On the other handx86_64is mapping the memoryZONE_NORMALand callinginit_memory_mappingon the new range right away. AFAICT changes to the 1:1 mapping ininit_mmare automatically synced across all mm's because the pgd is shared?TBH I'm not sure. vmalloc_sync_one/all does seem to do *something* on 64-bit, but I was never completely sure what regions of the address space were already shared. I *think* it might be that the pgd and pud are not shared, but the pmd down is, so if you add a new pmd you need to sync it into all the puds (and puds into pgds if you add a new one of those).
Quoted 4 lines
But I'd be happier pretending that vmalloc_sync_* just doesn't exist, and deal with it at the hypercall level - in the short term, by just making sure that the callers touch all those pages before passing them into the hypercall.
That would certainly be an improvement over what we have now.
However I am worried about the gntdev stuff: if I am right and the 1:1 mapping is guaranteed to be sync'ed, then it is OK and we can use alloc_xenballooned_pages everywhere, otherwise we should fix or remove alloc_xenballooned_pages from gntdev too.
Quoted 5 lines · Jeremy Fitzhardinge
But I'd be happier pretending that vmalloc_sync_* just doesn't exist, and deal with it at the hypercall level - in the short term, by just making sure that the callers touch all those pages before passing them into the hypercall.
I don't think you can access the pages at the addresses allocated with alloc_vm_area() because the pages don't exist yet and there's a check of pte_present() in vmalloc_fault() and I think an Oops will be reported.
David
Quoted 7 lines · Jeremy Fitzhardinge
Quoted 5 lines · David Vrabel
This set of pages avoids the need to call
vmalloc_sync_all()when mapping foreign pages. Two new functions are adding for mapping foreign pages onto RAM pages instead of vmalloc space.[...]
Quoted 9 lines
But that said, if you want to allocate virtual addresses for mapping granted pages, then
alloc_vm_area()is the right thing to use. You need to make sure you touch the pages from within the kernel before passing those addresses to a hypercall to make sure the mappings are established within the current task (possibly within a no-preempt region to guarantee that nothing odd happens). Or alternatively, you could switch the current pagetable toinit_mmfor the hypercall (if it isn't already)
- since that's the reference pagetable - assuming you're not passing usermode virtual addresses to the hypercall.
Making alloc_vm_area() fill in a array of pte_t *'s and passing the pointer to the PTE (using the GNTMAP_contains_pte flag) in the hypercall should allow Xen to update the PTEs in init_mm directly.
However, I'm not sure what to do about ia64 where GNTMAP_contains_pte is not supported. Any ideas?
Here's the untested patch I have so far.
arch/x86/xen/grant-table.c +1 -1
@@ -71,7 +71,7 @@ int arch_gnttab_map_shared(unsigned long *frames, unsigned long nr_gframes,if (shared ==NULL ) {struct vm_struct *area = -alloc_vm_area (PAGE_SIZE * max_nr_gframes); +alloc_vm_area (PAGE_SIZE * max_nr_gframes,NULL );BUG_ON (area ==NULL ); shared = area->addr; *__shared = shared;
drivers/xen/xenbus/xenbus_client.c +4 -3
@@ -435,19 +435,20 @@ EXPORT_SYMBOL_GPL(xenbus_free_evtchn);int xenbus_map_ring_valloc (struct xenbus_device *dev,int gnt_ref,void **vaddr) {struct gnttab_map_grant_ref op = { - .flags = GNTMAP_host_map, + .flags = GNTMAP_host_map | GNTMAP_contains_pte, .ref = gnt_ref, .dom = dev->otherend_id, };struct vm_struct *area; +pte_t *pte; *vaddr =NULL ; - area =alloc_vm_area (PAGE_SIZE ); + area =alloc_vm_area (PAGE_SIZE , &pte);if (!area)return -ENOMEM ; - op.host_addr = (unsigned long )area->addr; + op.host_addr = (unsigned long )pte;if (HYPERVISOR_grant_table_op (GNTTABOP_map_grant_ref, &op,1 ))BUG ();
include/linux/vmalloc.h +1 -1
@@ -118,7 +118,7 @@ unmap_kernel_range(unsigned long addr, unsigned long size)#endif /* Allocate/destroy a 'vmalloc' VM area. */ -extern struct vm_struct *alloc_vm_area (size_t size); +extern struct vm_struct *alloc_vm_area (size_t size,pte_t **ptes);extern void free_vm_area (struct vm_struct *area);/* for /dev/kmem */
mm/vmalloc.c +13 -14
@@ -2105,23 +2105,30 @@ void __attribute__((weak)) vmalloc_sync_all(void)static int f (pte_t *pte,pgtable_t table,unsigned long addr,void *data) { -/* apply_to_page_range() does all the hard work. */ +pte_t ***p = data; + +if (p) { + *(*p) = pte; + (*p)++; + }return 0 ; }/** * alloc_vm_area - allocate a range of kernel address space * @size: size of the area +* @ptes: returns the PTEs for the address space * * Returns: NULL on failure, vm_struct on success * * This function reserves a range of kernel address space, and * allocates pagetables to map that range. No actual mappings -* are created. If the kernel address space is not shared -* between processes, it syncs the pagetable across all -* processes. +* are created. +* +* If @ptes is non-NULL, pointers to the PTEs (in init_mm) +* allocated for the VM area are returned. */ -struct vm_struct *alloc_vm_area (size_t size) +struct vm_struct *alloc_vm_area (size_t size,pte_t **ptes) {struct vm_struct *area;
@@ -2135,19 +2142,11 @@ struct vm_struct *alloc_vm_area(size_t size)* of kernel virtual address space and mapped into init_mm. */ if (apply_to_page_range (&init_mm, (unsigned long )area->addr, - area->size, f,NULL )) { + area->size, f, ptes ? &ptes :NULL )) {free_vm_area (area);return NULL ; } -/* -* If the allocated address space is passed to a hypercall -* before being used then we cannot rely on a page fault to -* trigger an update of the page tables. So sync all the page -* tables here. -*/ -vmalloc_sync_all (); -return area; }EXPORT_SYMBOL_GPL (alloc_vm_area);
David
Quoted 18 lines · David Vrabel
Quoted 7 lines · Jeremy Fitzhardinge
Quoted 5 lines · David Vrabel
This set of pages avoids the need to call
vmalloc_sync_all()when mapping foreign pages. Two new functions are adding for mapping foreign pages onto RAM pages instead of vmalloc space.[...]
Quoted 9 lines
But that said, if you want to allocate virtual addresses for mapping granted pages, then
alloc_vm_area()is the right thing to use. You need to make sure you touch the pages from within the kernel before passing those addresses to a hypercall to make sure the mappings are established within the current task (possibly within a no-preempt region to guarantee that nothing odd happens). Or alternatively, you could switch the current pagetable toinit_mmfor the hypercall (if it isn't already)
- since that's the reference pagetable - assuming you're not passing usermode virtual addresses to the hypercall.
Quoted 8 lines
Making
alloc_vm_area()fill in a array ofpte_t*'s and passing the pointer to the PTE (using theGNTMAP_contains_pteflag) in the hypercall should allow Xen to update the PTEs ininit_mmdirectly.However, I'm not sure what to do about ia64 where
GNTMAP_contains_pteis not supported. Any ideas?Here's the untested patch I have so far.
This (sort of) works with some hacks/changes (described below).
Quoted 18 lines
arch/x86/xen/grant-table.c +1
-1@@ -71,7 +71,7 @@ int arch_gnttab_map_shared(unsigned long *frames, unsigned long nr_gframes,if (shared ==NULL ) {struct vm_struct *area = -alloc_vm_area (PAGE_SIZE * max_nr_gframes); +alloc_vm_area (PAGE_SIZE * max_nr_gframes,NULL );BUG_ON (area ==NULL ); shared = area->addr; *__shared = shared;drivers/xen/xenbus/xenbus_client.c +0
-0
drivers/xen/xenbus/xenbus_client.c EXPORT_SYMBOL_GPL(xenbus_free_evtchn); · line 443
Quoted 10 lines
drivers/xen/xenbus/xenbus_client.c +2
-1@@ -435,19 +435,20 @@ EXPORT_SYMBOL_GPL(xenbus_free_evtchn);int xenbus_map_ring_valloc (struct xenbus_device *dev,int gnt_ref,void **vaddr) {struct gnttab_map_grant_ref op = { - .flags = GNTMAP_host_map, + .flags = GNTMAP_host_map | GNTMAP_contains_pte, .ref = gnt_ref, .dom = dev->otherend_id, };struct vm_struct *area; +pte_t *pte;
alloc_vm_area() is allocating two pages instead of the one we ask for. I hacked this with
pte_t *pte[2];
drivers/xen/xenbus/xenbus_client.c EXPORT_SYMBOL_GPL(xenbus_free_evtchn); · line 451
Quoted 9 lines
drivers/xen/xenbus/xenbus_client.c +2
-2*vaddr =NULL ; - area =alloc_vm_area (PAGE_SIZE ); + area =alloc_vm_area (PAGE_SIZE , &pte);if (!area)return -ENOMEM ; - op.host_addr = (unsigned long )area->addr; + op.host_addr = (unsigned long )pte;
With the GNTMAP_contains_pte flags this needs a machine address here so:
op.host_addr = arbitrary_virt_to_machine(pte).maddr;
drivers/xen/xenbus/xenbus_client.c EXPORT_SYMBOL_GPL(xenbus_free_evtchn); · line 454
Quoted 2 lines
drivers/xen/xenbus/xenbus_client.c +0
-0if (HYPERVISOR_grant_table_op (GNTTABOP_map_grant_ref, &op,1 ))BUG ();
Also need to do something with xenbus_map_ring_vfree() which doesn't work.
Quoted 74 lines
include/linux/vmalloc.h +1
-1@@ -118,7 +118,7 @@ unmap_kernel_range(unsigned long addr, unsigned long size)#endif /* Allocate/destroy a 'vmalloc' VM area. */ -extern struct vm_struct *alloc_vm_area (size_t size); +extern struct vm_struct *alloc_vm_area (size_t size,pte_t **ptes);extern void free_vm_area (struct vm_struct *area);/* for /dev/kmem */ mm/vmalloc.c +13
-14@@ -2105,23 +2105,30 @@ void __attribute__((weak)) vmalloc_sync_all(void)static int f (pte_t *pte,pgtable_t table,unsigned long addr,void *data) { -/* apply_to_page_range() does all the hard work. */ +pte_t ***p = data; + +if (p) { + *(*p) = pte; + (*p)++; + }return 0 ; }/** * alloc_vm_area - allocate a range of kernel address space * @size: size of the area +* @ptes: returns the PTEs for the address space * * Returns: NULL on failure, vm_struct on success * * This function reserves a range of kernel address space, and * allocates pagetables to map that range. No actual mappings -* are created. If the kernel address space is not shared -* between processes, it syncs the pagetable across all -* processes. +* are created. +* +* If @ptes is non-NULL, pointers to the PTEs (in init_mm) +* allocated for the VM area are returned. */ -struct vm_struct *alloc_vm_area (size_t size) +struct vm_struct *alloc_vm_area (size_t size,pte_t **ptes) {struct vm_struct *area;@@ -2135,19 +2142,11 @@ struct vm_struct *alloc_vm_area(size_t size)* of kernel virtual address space and mapped into init_mm. */ if (apply_to_page_range (&init_mm, (unsigned long )area->addr, - area->size, f,NULL )) { + area->size, f, ptes ? &ptes :NULL )) {free_vm_area (area);return NULL ; } -/* -* If the allocated address space is passed to a hypercall -* before being used then we cannot rely on a page fault to -* trigger an update of the page tables. So sync all the page -* tables here. -*/ -vmalloc_sync_all (); -return area; }EXPORT_SYMBOL_GPL (alloc_vm_area);