Skip to content

Commit 08ece5b

Browse files
Zoltan Kisskonradwilk
authored andcommitted
xen/grant-table: Avoid m2p_override during mapping
The grant mapping API does m2p_override unnecessarily: only gntdev needs it, for blkback and future netback patches it just cause a lock contention, as those pages never go to userspace. Therefore this series does the following: - the original functions were renamed to __gnttab_[un]map_refs, with a new parameter m2p_override - based on m2p_override either they follow the original behaviour, or just set the private flag and call set_phys_to_machine - gnttab_[un]map_refs are now a wrapper to call __gnttab_[un]map_refs with m2p_override false - a new function gnttab_[un]map_refs_userspace provides the old behaviour It also removes a stray space from page.h and change ret to 0 if XENFEAT_auto_translated_physmap, as that is the only possible return value there. v2: - move the storing of the old mfn in page->index to gnttab_map_refs - move the function header update to a separate patch v3: - a new approach to retain old behaviour where it needed - squash the patches into one v4: - move out the common bits from m2p* functions, and pass pfn/mfn as parameter - clear page->private before doing anything with the page, so m2p_find_override won't race with this v5: - change return value handling in __gnttab_[un]map_refs - remove a stray space in page.h - add detail why ret = 0 now at some places v6: - don't pass pfn to m2p* functions, just get it locally Signed-off-by: Zoltan Kiss <zoltan.kiss@citrix.com> Suggested-by: David Vrabel <david.vrabel@citrix.com> Acked-by: David Vrabel <david.vrabel@citrix.com> Acked-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com> Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
1 parent 47c5420 commit 08ece5b

6 files changed

Lines changed: 101 additions & 46 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ extern unsigned long set_phys_range_identity(unsigned long pfn_s,
5252
extern int m2p_add_override(unsigned long mfn, struct page *page,
5353
struct gnttab_map_grant_ref *kmap_op);
5454
extern int m2p_remove_override(struct page *page,
55-
struct gnttab_map_grant_ref *kmap_op);
55+
struct gnttab_map_grant_ref *kmap_op,
56+
unsigned long mfn);
5657
extern struct page *m2p_find_override(unsigned long mfn);
5758
extern unsigned long m2p_find_override_pfn(unsigned long mfn, unsigned long pfn);
5859

@@ -121,7 +122,7 @@ static inline unsigned long mfn_to_pfn(unsigned long mfn)
121122
pfn = m2p_find_override_pfn(mfn, ~0);
122123
}
123124

124-
/*
125+
/*
125126
* pfn is ~0 if there are no entries in the m2p for mfn or if the
126127
* entry doesn't map back to the mfn and m2p_override doesn't have a
127128
* valid entry for it.

‎arch/x86/xen/p2m.c‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -899,13 +899,6 @@ int m2p_add_override(unsigned long mfn, struct page *page,
899899
"m2p_add_override: pfn %lx not mapped", pfn))
900900
return -EINVAL;
901901
}
902-
WARN_ON(PagePrivate(page));
903-
SetPagePrivate(page);
904-
set_page_private(page, mfn);
905-
page->index = pfn_to_mfn(pfn);
906-
907-
if (unlikely(!set_phys_to_machine(pfn, FOREIGN_FRAME(mfn))))
908-
return -ENOMEM;
909902

910903
if (kmap_op != NULL) {
911904
if (!PageHighMem(page)) {
@@ -944,19 +937,16 @@ int m2p_add_override(unsigned long mfn, struct page *page,
944937
}
945938
EXPORT_SYMBOL_GPL(m2p_add_override);
946939
int m2p_remove_override(struct page *page,
947-
struct gnttab_map_grant_ref *kmap_op)
940+
struct gnttab_map_grant_ref *kmap_op,
941+
unsigned long mfn)
948942
{
949943
unsigned long flags;
950-
unsigned long mfn;
951944
unsigned long pfn;
952945
unsigned long uninitialized_var(address);
953946
unsigned level;
954947
pte_t *ptep = NULL;
955948

956949
pfn = page_to_pfn(page);
957-
mfn = get_phys_to_machine(pfn);
958-
if (mfn == INVALID_P2M_ENTRY || !(mfn & FOREIGN_FRAME_BIT))
959-
return -EINVAL;
960950

961951
if (!PageHighMem(page)) {
962952
address = (unsigned long)__va(pfn << PAGE_SHIFT);
@@ -970,10 +960,7 @@ int m2p_remove_override(struct page *page,
970960
spin_lock_irqsave(&m2p_override_lock, flags);
971961
list_del(&page->lru);
972962
spin_unlock_irqrestore(&m2p_override_lock, flags);
973-
WARN_ON(!PagePrivate(page));
974-
ClearPagePrivate(page);
975963

976-
set_phys_to_machine(pfn, page->index);
977964
if (kmap_op != NULL) {
978965
if (!PageHighMem(page)) {
979966
struct multicall_space mcs;

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,7 @@ static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
285285

286286
if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST ||
287287
!rb_next(&persistent_gnt->node)) {
288-
ret = gnttab_unmap_refs(unmap, NULL, pages,
289-
segs_to_unmap);
288+
ret = gnttab_unmap_refs(unmap, pages, segs_to_unmap);
290289
BUG_ON(ret);
291290
put_free_pages(blkif, pages, segs_to_unmap);
292291
segs_to_unmap = 0;
@@ -321,16 +320,15 @@ static void unmap_purged_grants(struct work_struct *work)
321320
pages[segs_to_unmap] = persistent_gnt->page;
322321

323322
if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
324-
ret = gnttab_unmap_refs(unmap, NULL, pages,
325-
segs_to_unmap);
323+
ret = gnttab_unmap_refs(unmap, pages, segs_to_unmap);
326324
BUG_ON(ret);
327325
put_free_pages(blkif, pages, segs_to_unmap);
328326
segs_to_unmap = 0;
329327
}
330328
kfree(persistent_gnt);
331329
}
332330
if (segs_to_unmap > 0) {
333-
ret = gnttab_unmap_refs(unmap, NULL, pages, segs_to_unmap);
331+
ret = gnttab_unmap_refs(unmap, pages, segs_to_unmap);
334332
BUG_ON(ret);
335333
put_free_pages(blkif, pages, segs_to_unmap);
336334
}
@@ -670,15 +668,14 @@ static void xen_blkbk_unmap(struct xen_blkif *blkif,
670668
GNTMAP_host_map, pages[i]->handle);
671669
pages[i]->handle = BLKBACK_INVALID_HANDLE;
672670
if (++invcount == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
673-
ret = gnttab_unmap_refs(unmap, NULL, unmap_pages,
674-
invcount);
671+
ret = gnttab_unmap_refs(unmap, unmap_pages, invcount);
675672
BUG_ON(ret);
676673
put_free_pages(blkif, unmap_pages, invcount);
677674
invcount = 0;
678675
}
679676
}
680677
if (invcount) {
681-
ret = gnttab_unmap_refs(unmap, NULL, unmap_pages, invcount);
678+
ret = gnttab_unmap_refs(unmap, unmap_pages, invcount);
682679
BUG_ON(ret);
683680
put_free_pages(blkif, unmap_pages, invcount);
684681
}
@@ -740,7 +737,7 @@ static int xen_blkbk_map(struct xen_blkif *blkif,
740737
}
741738

742739
if (segs_to_map) {
743-
ret = gnttab_map_refs(map, NULL, pages_to_gnt, segs_to_map);
740+
ret = gnttab_map_refs(map, pages_to_gnt, segs_to_map);
744741
BUG_ON(ret);
745742
}
746743

‎drivers/xen/gntdev.c‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,10 @@ static int map_grant_pages(struct grant_map *map)
284284
}
285285

286286
pr_debug("map %d+%d\n", map->index, map->count);
287-
err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
288-
map->pages, map->count);
287+
err = gnttab_map_refs_userspace(map->map_ops,
288+
use_ptemod ? map->kmap_ops : NULL,
289+
map->pages,
290+
map->count);
289291
if (err)
290292
return err;
291293

@@ -315,9 +317,10 @@ static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
315317
}
316318
}
317319

318-
err = gnttab_unmap_refs(map->unmap_ops + offset,
319-
use_ptemod ? map->kmap_ops + offset : NULL, map->pages + offset,
320-
pages);
320+
err = gnttab_unmap_refs_userspace(map->unmap_ops + offset,
321+
use_ptemod ? map->kmap_ops + offset : NULL,
322+
map->pages + offset,
323+
pages);
321324
if (err)
322325
return err;
323326

‎drivers/xen/grant-table.c‎

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -928,15 +928,17 @@ void gnttab_batch_copy(struct gnttab_copy *batch, unsigned count)
928928
}
929929
EXPORT_SYMBOL_GPL(gnttab_batch_copy);
930930

931-
int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
931+
int __gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
932932
struct gnttab_map_grant_ref *kmap_ops,
933-
struct page **pages, unsigned int count)
933+
struct page **pages, unsigned int count,
934+
bool m2p_override)
934935
{
935936
int i, ret;
936937
bool lazy = false;
937938
pte_t *pte;
938-
unsigned long mfn;
939+
unsigned long mfn, pfn;
939940

941+
BUG_ON(kmap_ops && !m2p_override);
940942
ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map_ops, count);
941943
if (ret)
942944
return ret;
@@ -955,10 +957,12 @@ int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
955957
set_phys_to_machine(map_ops[i].host_addr >> PAGE_SHIFT,
956958
map_ops[i].dev_bus_addr >> PAGE_SHIFT);
957959
}
958-
return ret;
960+
return 0;
959961
}
960962

961-
if (!in_interrupt() && paravirt_get_lazy_mode() == PARAVIRT_LAZY_NONE) {
963+
if (m2p_override &&
964+
!in_interrupt() &&
965+
paravirt_get_lazy_mode() == PARAVIRT_LAZY_NONE) {
962966
arch_enter_lazy_mmu_mode();
963967
lazy = true;
964968
}
@@ -975,8 +979,20 @@ int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
975979
} else {
976980
mfn = PFN_DOWN(map_ops[i].dev_bus_addr);
977981
}
978-
ret = m2p_add_override(mfn, pages[i], kmap_ops ?
979-
&kmap_ops[i] : NULL);
982+
pfn = page_to_pfn(pages[i]);
983+
984+
WARN_ON(PagePrivate(pages[i]));
985+
SetPagePrivate(pages[i]);
986+
set_page_private(pages[i], mfn);
987+
988+
pages[i]->index = pfn_to_mfn(pfn);
989+
if (unlikely(!set_phys_to_machine(pfn, FOREIGN_FRAME(mfn)))) {
990+
ret = -ENOMEM;
991+
goto out;
992+
}
993+
if (m2p_override)
994+
ret = m2p_add_override(mfn, pages[i], kmap_ops ?
995+
&kmap_ops[i] : NULL);
980996
if (ret)
981997
goto out;
982998
}
@@ -987,15 +1003,32 @@ int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
9871003

9881004
return ret;
9891005
}
1006+
1007+
int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
1008+
struct page **pages, unsigned int count)
1009+
{
1010+
return __gnttab_map_refs(map_ops, NULL, pages, count, false);
1011+
}
9901012
EXPORT_SYMBOL_GPL(gnttab_map_refs);
9911013

992-
int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
1014+
int gnttab_map_refs_userspace(struct gnttab_map_grant_ref *map_ops,
1015+
struct gnttab_map_grant_ref *kmap_ops,
1016+
struct page **pages, unsigned int count)
1017+
{
1018+
return __gnttab_map_refs(map_ops, kmap_ops, pages, count, true);
1019+
}
1020+
EXPORT_SYMBOL_GPL(gnttab_map_refs_userspace);
1021+
1022+
int __gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
9931023
struct gnttab_map_grant_ref *kmap_ops,
994-
struct page **pages, unsigned int count)
1024+
struct page **pages, unsigned int count,
1025+
bool m2p_override)
9951026
{
9961027
int i, ret;
9971028
bool lazy = false;
1029+
unsigned long pfn, mfn;
9981030

1031+
BUG_ON(kmap_ops && !m2p_override);
9991032
ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap_ops, count);
10001033
if (ret)
10011034
return ret;
@@ -1006,17 +1039,33 @@ int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
10061039
set_phys_to_machine(unmap_ops[i].host_addr >> PAGE_SHIFT,
10071040
INVALID_P2M_ENTRY);
10081041
}
1009-
return ret;
1042+
return 0;
10101043
}
10111044

1012-
if (!in_interrupt() && paravirt_get_lazy_mode() == PARAVIRT_LAZY_NONE) {
1045+
if (m2p_override &&
1046+
!in_interrupt() &&
1047+
paravirt_get_lazy_mode() == PARAVIRT_LAZY_NONE) {
10131048
arch_enter_lazy_mmu_mode();
10141049
lazy = true;
10151050
}
10161051

10171052
for (i = 0; i < count; i++) {
1018-
ret = m2p_remove_override(pages[i], kmap_ops ?
1019-
&kmap_ops[i] : NULL);
1053+
pfn = page_to_pfn(pages[i]);
1054+
mfn = get_phys_to_machine(pfn);
1055+
if (mfn == INVALID_P2M_ENTRY || !(mfn & FOREIGN_FRAME_BIT)) {
1056+
ret = -EINVAL;
1057+
goto out;
1058+
}
1059+
1060+
set_page_private(pages[i], INVALID_P2M_ENTRY);
1061+
WARN_ON(!PagePrivate(pages[i]));
1062+
ClearPagePrivate(pages[i]);
1063+
set_phys_to_machine(pfn, pages[i]->index);
1064+
if (m2p_override)
1065+
ret = m2p_remove_override(pages[i],
1066+
kmap_ops ?
1067+
&kmap_ops[i] : NULL,
1068+
mfn);
10201069
if (ret)
10211070
goto out;
10221071
}
@@ -1027,8 +1076,22 @@ int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
10271076

10281077
return ret;
10291078
}
1079+
1080+
int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *map_ops,
1081+
struct page **pages, unsigned int count)
1082+
{
1083+
return __gnttab_unmap_refs(map_ops, NULL, pages, count, false);
1084+
}
10301085
EXPORT_SYMBOL_GPL(gnttab_unmap_refs);
10311086

1087+
int gnttab_unmap_refs_userspace(struct gnttab_unmap_grant_ref *map_ops,
1088+
struct gnttab_map_grant_ref *kmap_ops,
1089+
struct page **pages, unsigned int count)
1090+
{
1091+
return __gnttab_unmap_refs(map_ops, kmap_ops, pages, count, true);
1092+
}
1093+
EXPORT_SYMBOL_GPL(gnttab_unmap_refs_userspace);
1094+
10321095
static unsigned nr_status_frames(unsigned nr_grant_frames)
10331096
{
10341097
BUG_ON(grefs_per_grant_frame == 0);

‎include/xen/grant_table.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,15 @@ void gnttab_free_auto_xlat_frames(void);
191191
#define gnttab_map_vaddr(map) ((void *)(map.host_virt_addr))
192192

193193
int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
194-
struct gnttab_map_grant_ref *kmap_ops,
195194
struct page **pages, unsigned int count);
195+
int gnttab_map_refs_userspace(struct gnttab_map_grant_ref *map_ops,
196+
struct gnttab_map_grant_ref *kmap_ops,
197+
struct page **pages, unsigned int count);
196198
int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
197-
struct gnttab_map_grant_ref *kunmap_ops,
198199
struct page **pages, unsigned int count);
200+
int gnttab_unmap_refs_userspace(struct gnttab_unmap_grant_ref *unmap_ops,
201+
struct gnttab_map_grant_ref *kunmap_ops,
202+
struct page **pages, unsigned int count);
199203

200204
/* Perform a batch of grant map/copy operations. Retry every batch slot
201205
* for which the hypervisor returns GNTST_eagain. This is typically due

0 commit comments

Comments
 (0)