Skip to content

Commit 0930bba

Browse files
Stefano Stabellinikonradwilk
authored andcommitted
xen: modify kernel mappings corresponding to granted pages
If we want to use granted pages for AIO, changing the mappings of a user vma and the corresponding p2m is not enough, we also need to update the kernel mappings accordingly. Currently this is only needed for pages that are created for user usages through /dev/xen/gntdev. As in, pages that have been in use by the kernel and use the P2M will not need this special mapping. However there are no guarantees that in the future the kernel won't start accessing pages through the 1:1 even for internal usage. In order to avoid the complexity of dealing with highmem, we allocated the pages lowmem. We issue a HYPERVISOR_grant_table_op right away in m2p_add_override and we remove the mappings using another HYPERVISOR_grant_table_op in m2p_remove_override. Considering that m2p_add_override and m2p_remove_override are called once per page we use multicalls and hypercall batching. Use the kmap_op pointer directly as argument to do the mapping as it is guaranteed to be present up until the unmapping is done. Before issuing any unmapping multicalls, we need to make sure that the mapping has already being done, because we need the kmap->handle to be set correctly. Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> [v1: Removed GRANT_FRAME_BIT usage] Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
1 parent 693394b commit 0930bba

6 files changed

Lines changed: 104 additions & 16 deletions

File tree

‎arch/x86/include/asm/xen/page.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <asm/pgtable.h>
1313

1414
#include <xen/interface/xen.h>
15+
#include <xen/grant_table.h>
1516
#include <xen/features.h>
1617

1718
/* Xen machine address */
@@ -48,7 +49,7 @@ extern unsigned long set_phys_range_identity(unsigned long pfn_s,
4849
unsigned long pfn_e);
4950

5051
extern int m2p_add_override(unsigned long mfn, struct page *page,
51-
bool clear_pte);
52+
struct gnttab_map_grant_ref *kmap_op);
5253
extern int m2p_remove_override(struct page *page, bool clear_pte);
5354
extern struct page *m2p_find_override(unsigned long mfn);
5455
extern unsigned long m2p_find_override_pfn(unsigned long mfn, unsigned long pfn);

‎arch/x86/xen/p2m.c‎

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@
161161
#include <asm/xen/page.h>
162162
#include <asm/xen/hypercall.h>
163163
#include <asm/xen/hypervisor.h>
164+
#include <xen/grant_table.h>
164165

166+
#include "multicalls.h"
165167
#include "xen-ops.h"
166168

167169
static void __init m2p_override_init(void);
@@ -676,7 +678,8 @@ static unsigned long mfn_hash(unsigned long mfn)
676678
}
677679

678680
/* Add an MFN override for a particular page */
679-
int m2p_add_override(unsigned long mfn, struct page *page, bool clear_pte)
681+
int m2p_add_override(unsigned long mfn, struct page *page,
682+
struct gnttab_map_grant_ref *kmap_op)
680683
{
681684
unsigned long flags;
682685
unsigned long pfn;
@@ -700,9 +703,20 @@ int m2p_add_override(unsigned long mfn, struct page *page, bool clear_pte)
700703
if (unlikely(!set_phys_to_machine(pfn, FOREIGN_FRAME(mfn))))
701704
return -ENOMEM;
702705

703-
if (clear_pte && !PageHighMem(page))
704-
/* Just zap old mapping for now */
705-
pte_clear(&init_mm, address, ptep);
706+
if (kmap_op != NULL) {
707+
if (!PageHighMem(page)) {
708+
struct multicall_space mcs =
709+
xen_mc_entry(sizeof(*kmap_op));
710+
711+
MULTI_grant_table_op(mcs.mc,
712+
GNTTABOP_map_grant_ref, kmap_op, 1);
713+
714+
xen_mc_issue(PARAVIRT_LAZY_MMU);
715+
}
716+
/* let's use dev_bus_addr to record the old mfn instead */
717+
kmap_op->dev_bus_addr = page->index;
718+
page->index = (unsigned long) kmap_op;
719+
}
706720
spin_lock_irqsave(&m2p_override_lock, flags);
707721
list_add(&page->lru, &m2p_overrides[mfn_hash(mfn)]);
708722
spin_unlock_irqrestore(&m2p_override_lock, flags);
@@ -736,14 +750,56 @@ int m2p_remove_override(struct page *page, bool clear_pte)
736750
spin_lock_irqsave(&m2p_override_lock, flags);
737751
list_del(&page->lru);
738752
spin_unlock_irqrestore(&m2p_override_lock, flags);
739-
set_phys_to_machine(pfn, page->index);
740753
WARN_ON(!PagePrivate(page));
741754
ClearPagePrivate(page);
742-
if (clear_pte && !PageHighMem(page))
743-
set_pte_at(&init_mm, address, ptep,
744-
pfn_pte(pfn, PAGE_KERNEL));
745-
/* No tlb flush necessary because the caller already
746-
* left the pte unmapped. */
755+
756+
if (clear_pte) {
757+
struct gnttab_map_grant_ref *map_op =
758+
(struct gnttab_map_grant_ref *) page->index;
759+
set_phys_to_machine(pfn, map_op->dev_bus_addr);
760+
if (!PageHighMem(page)) {
761+
struct multicall_space mcs;
762+
struct gnttab_unmap_grant_ref *unmap_op;
763+
764+
/*
765+
* It might be that we queued all the m2p grant table
766+
* hypercalls in a multicall, then m2p_remove_override
767+
* get called before the multicall has actually been
768+
* issued. In this case handle is going to -1 because
769+
* it hasn't been modified yet.
770+
*/
771+
if (map_op->handle == -1)
772+
xen_mc_flush();
773+
/*
774+
* Now if map_op->handle is negative it means that the
775+
* hypercall actually returned an error.
776+
*/
777+
if (map_op->handle == GNTST_general_error) {
778+
printk(KERN_WARNING "m2p_remove_override: "
779+
"pfn %lx mfn %lx, failed to modify kernel mappings",
780+
pfn, mfn);
781+
return -1;
782+
}
783+
784+
mcs = xen_mc_entry(
785+
sizeof(struct gnttab_unmap_grant_ref));
786+
unmap_op = mcs.args;
787+
unmap_op->host_addr = map_op->host_addr;
788+
unmap_op->handle = map_op->handle;
789+
unmap_op->dev_bus_addr = 0;
790+
791+
MULTI_grant_table_op(mcs.mc,
792+
GNTTABOP_unmap_grant_ref, unmap_op, 1);
793+
794+
xen_mc_issue(PARAVIRT_LAZY_MMU);
795+
796+
set_pte_at(&init_mm, address, ptep,
797+
pfn_pte(pfn, PAGE_KERNEL));
798+
__flush_tlb_single(address);
799+
map_op->host_addr = 0;
800+
}
801+
} else
802+
set_phys_to_machine(pfn, page->index);
747803

748804
return 0;
749805
}

‎drivers/block/xen-blkback/blkback.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ static int xen_blkbk_map(struct blkif_request *req,
396396
continue;
397397

398398
ret = m2p_add_override(PFN_DOWN(map[i].dev_bus_addr),
399-
blkbk->pending_page(pending_req, i), false);
399+
blkbk->pending_page(pending_req, i), NULL);
400400
if (ret) {
401401
pr_alert(DRV_PFX "Failed to install M2P override for %lx (ret: %d)\n",
402402
(unsigned long)map[i].dev_bus_addr, ret);

‎drivers/xen/gntdev.c‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ struct grant_map {
8383
struct ioctl_gntdev_grant_ref *grants;
8484
struct gnttab_map_grant_ref *map_ops;
8585
struct gnttab_unmap_grant_ref *unmap_ops;
86+
struct gnttab_map_grant_ref *kmap_ops;
8687
struct page **pages;
8788
};
8889

@@ -116,10 +117,12 @@ static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
116117
add->grants = kzalloc(sizeof(add->grants[0]) * count, GFP_KERNEL);
117118
add->map_ops = kzalloc(sizeof(add->map_ops[0]) * count, GFP_KERNEL);
118119
add->unmap_ops = kzalloc(sizeof(add->unmap_ops[0]) * count, GFP_KERNEL);
120+
add->kmap_ops = kzalloc(sizeof(add->kmap_ops[0]) * count, GFP_KERNEL);
119121
add->pages = kzalloc(sizeof(add->pages[0]) * count, GFP_KERNEL);
120122
if (NULL == add->grants ||
121123
NULL == add->map_ops ||
122124
NULL == add->unmap_ops ||
125+
NULL == add->kmap_ops ||
123126
NULL == add->pages)
124127
goto err;
125128

@@ -129,6 +132,7 @@ static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
129132
for (i = 0; i < count; i++) {
130133
add->map_ops[i].handle = -1;
131134
add->unmap_ops[i].handle = -1;
135+
add->kmap_ops[i].handle = -1;
132136
}
133137

134138
add->index = 0;
@@ -142,6 +146,7 @@ static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
142146
kfree(add->grants);
143147
kfree(add->map_ops);
144148
kfree(add->unmap_ops);
149+
kfree(add->kmap_ops);
145150
kfree(add);
146151
return NULL;
147152
}
@@ -243,10 +248,35 @@ static int map_grant_pages(struct grant_map *map)
243248
gnttab_set_unmap_op(&map->unmap_ops[i], addr,
244249
map->flags, -1 /* handle */);
245250
}
251+
} else {
252+
/*
253+
* Setup the map_ops corresponding to the pte entries pointing
254+
* to the kernel linear addresses of the struct pages.
255+
* These ptes are completely different from the user ptes dealt
256+
* with find_grant_ptes.
257+
*/
258+
for (i = 0; i < map->count; i++) {
259+
unsigned level;
260+
unsigned long address = (unsigned long)
261+
pfn_to_kaddr(page_to_pfn(map->pages[i]));
262+
pte_t *ptep;
263+
u64 pte_maddr = 0;
264+
BUG_ON(PageHighMem(map->pages[i]));
265+
266+
ptep = lookup_address(address, &level);
267+
pte_maddr = arbitrary_virt_to_machine(ptep).maddr;
268+
gnttab_set_map_op(&map->kmap_ops[i], pte_maddr,
269+
map->flags |
270+
GNTMAP_host_map |
271+
GNTMAP_contains_pte,
272+
map->grants[i].ref,
273+
map->grants[i].domid);
274+
}
246275
}
247276

248277
pr_debug("map %d+%d\n", map->index, map->count);
249-
err = gnttab_map_refs(map->map_ops, map->pages, map->count);
278+
err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
279+
map->pages, map->count);
250280
if (err)
251281
return err;
252282

‎drivers/xen/grant-table.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,8 @@ unsigned int gnttab_max_grant_frames(void)
448448
EXPORT_SYMBOL_GPL(gnttab_max_grant_frames);
449449

450450
int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
451-
struct page **pages, unsigned int count)
451+
struct gnttab_map_grant_ref *kmap_ops,
452+
struct page **pages, unsigned int count)
452453
{
453454
int i, ret;
454455
pte_t *pte;
@@ -488,8 +489,7 @@ int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
488489
*/
489490
return -EOPNOTSUPP;
490491
}
491-
ret = m2p_add_override(mfn, pages[i],
492-
map_ops[i].flags & GNTMAP_contains_pte);
492+
ret = m2p_add_override(mfn, pages[i], &kmap_ops[i]);
493493
if (ret)
494494
return ret;
495495
}

‎include/xen/grant_table.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ unsigned int gnttab_max_grant_frames(void);
156156
#define gnttab_map_vaddr(map) ((void *)(map.host_virt_addr))
157157

158158
int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
159+
struct gnttab_map_grant_ref *kmap_ops,
159160
struct page **pages, unsigned int count);
160161
int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
161162
struct page **pages, unsigned int count);

0 commit comments

Comments
 (0)