Skip to content

Commit cd12909

Browse files
David Vrabelkonradwilk
authored andcommitted
xen: map foreign pages for shared rings by updating the PTEs directly
When mapping a foreign page with xenbus_map_ring_valloc() with the GNTTABOP_map_grant_ref hypercall, set the GNTMAP_contains_pte flag and pass a pointer to the PTE (in init_mm). After the page is mapped, the usual fault mechanism can be used to update additional MMs. This allows the vmalloc_sync_all() to be removed from alloc_vm_area(). Signed-off-by: David Vrabel <david.vrabel@citrix.com> Acked-by: Andrew Morton <akpm@linux-foundation.org> [v1: Squashed fix by Michal for no-mmu case] Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com> Signed-off-by: Michal Simek <monstr@monstr.eu>
1 parent 1ea6b8f commit cd12909

5 files changed

Lines changed: 24 additions & 20 deletions

File tree

‎arch/x86/xen/grant-table.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ int arch_gnttab_map_shared(unsigned long *frames, unsigned long nr_gframes,
7171

7272
if (shared == NULL) {
7373
struct vm_struct *area =
74-
alloc_vm_area(PAGE_SIZE * max_nr_gframes);
74+
alloc_vm_area(PAGE_SIZE * max_nr_gframes, NULL);
7575
BUG_ON(area == NULL);
7676
shared = area->addr;
7777
*__shared = shared;

‎drivers/xen/xenbus/xenbus_client.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <linux/vmalloc.h>
3636
#include <linux/export.h>
3737
#include <asm/xen/hypervisor.h>
38+
#include <asm/xen/page.h>
3839
#include <xen/interface/xen.h>
3940
#include <xen/interface/event_channel.h>
4041
#include <xen/events.h>
@@ -436,19 +437,20 @@ EXPORT_SYMBOL_GPL(xenbus_free_evtchn);
436437
int xenbus_map_ring_valloc(struct xenbus_device *dev, int gnt_ref, void **vaddr)
437438
{
438439
struct gnttab_map_grant_ref op = {
439-
.flags = GNTMAP_host_map,
440+
.flags = GNTMAP_host_map | GNTMAP_contains_pte,
440441
.ref = gnt_ref,
441442
.dom = dev->otherend_id,
442443
};
443444
struct vm_struct *area;
445+
pte_t *pte;
444446

445447
*vaddr = NULL;
446448

447-
area = alloc_vm_area(PAGE_SIZE);
449+
area = alloc_vm_area(PAGE_SIZE, &pte);
448450
if (!area)
449451
return -ENOMEM;
450452

451-
op.host_addr = (unsigned long)area->addr;
453+
op.host_addr = arbitrary_virt_to_machine(pte).maddr;
452454

453455
if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
454456
BUG();
@@ -527,6 +529,7 @@ int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr)
527529
struct gnttab_unmap_grant_ref op = {
528530
.host_addr = (unsigned long)vaddr,
529531
};
532+
unsigned int level;
530533

531534
/* It'd be nice if linux/vmalloc.h provided a find_vm_area(void *addr)
532535
* method so that we don't have to muck with vmalloc internals here.
@@ -548,6 +551,8 @@ int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr)
548551
}
549552

550553
op.handle = (grant_handle_t)area->phys_addr;
554+
op.host_addr = arbitrary_virt_to_machine(
555+
lookup_address((unsigned long)vaddr, &level)).maddr;
551556

552557
if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
553558
BUG();

‎include/linux/vmalloc.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ unmap_kernel_range(unsigned long addr, unsigned long size)
119119
#endif
120120

121121
/* Allocate/destroy a 'vmalloc' VM area. */
122-
extern struct vm_struct *alloc_vm_area(size_t size);
122+
extern struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes);
123123
extern void free_vm_area(struct vm_struct *area);
124124

125125
/* for /dev/kmem */

‎mm/nommu.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ void __attribute__((weak)) vmalloc_sync_all(void)
454454
* between processes, it syncs the pagetable across all
455455
* processes.
456456
*/
457-
struct vm_struct *alloc_vm_area(size_t size)
457+
struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
458458
{
459459
BUG();
460460
return NULL;

‎mm/vmalloc.c‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,23 +2141,30 @@ void __attribute__((weak)) vmalloc_sync_all(void)
21412141

21422142
static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
21432143
{
2144-
/* apply_to_page_range() does all the hard work. */
2144+
pte_t ***p = data;
2145+
2146+
if (p) {
2147+
*(*p) = pte;
2148+
(*p)++;
2149+
}
21452150
return 0;
21462151
}
21472152

21482153
/**
21492154
* alloc_vm_area - allocate a range of kernel address space
21502155
* @size: size of the area
2156+
* @ptes: returns the PTEs for the address space
21512157
*
21522158
* Returns: NULL on failure, vm_struct on success
21532159
*
21542160
* This function reserves a range of kernel address space, and
21552161
* allocates pagetables to map that range. No actual mappings
2156-
* are created. If the kernel address space is not shared
2157-
* between processes, it syncs the pagetable across all
2158-
* processes.
2162+
* are created.
2163+
*
2164+
* If @ptes is non-NULL, pointers to the PTEs (in init_mm)
2165+
* allocated for the VM area are returned.
21592166
*/
2160-
struct vm_struct *alloc_vm_area(size_t size)
2167+
struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
21612168
{
21622169
struct vm_struct *area;
21632170

@@ -2171,19 +2178,11 @@ struct vm_struct *alloc_vm_area(size_t size)
21712178
* of kernel virtual address space and mapped into init_mm.
21722179
*/
21732180
if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
2174-
area->size, f, NULL)) {
2181+
size, f, ptes ? &ptes : NULL)) {
21752182
free_vm_area(area);
21762183
return NULL;
21772184
}
21782185

2179-
/*
2180-
* If the allocated address space is passed to a hypercall
2181-
* before being used then we cannot rely on a page fault to
2182-
* trigger an update of the page tables. So sync all the page
2183-
* tables here.
2184-
*/
2185-
vmalloc_sync_all();
2186-
21872186
return area;
21882187
}
21892188
EXPORT_SYMBOL_GPL(alloc_vm_area);

0 commit comments

Comments
 (0)