From: Stefano Stabellini Subject: [PATCH 3/3] xen/m2p: use GNTTABOP_unmap_and_replace to replace foreign grants with balloon_trade_page Date: Sun, 21 Jul 2013 18:32:31 +0100 Message-ID: <1374427951-24126-3-git-send-email-stefano.stabellini@eu.citrix.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xensource.com Cc: Ian.Campbell@citrix.com, Stefano Stabellini , david.vrabel@citrix.com, alex@alex.org.uk, dcrisan@flexiant.com List-Id: xen-devel@lists.xenproject.org GNTTABOP_unmap_grant_ref unmaps a grant and replaces it with a 0 mapping instead of reinstating the original mapping. Doing so separately would be racy. To unmap a grant and reinstate the original mapping atomically we use GNTTABOP_unmap_and_replace. GNTTABOP_unmap_and_replace doesn't work with GNTMAP_contains_pte, so don't use it for kmaps. Note: the old GNTTABOP_unmap_and_replace_legacy zeros the mapping passed as "new_addr". This can cause race conditions when another cpu tries to use the mapping before it has been restored. Signed-off-by: Stefano Stabellini CC: alex@alex.org.uk CC: dcrisan@flexiant.com --- arch/x86/xen/p2m.c | 25 ++++++++++++++++++------- drivers/xen/gntdev.c | 11 ++--------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/arch/x86/xen/p2m.c b/arch/x86/xen/p2m.c index 95fb2aa..20d7056 100644 --- a/arch/x86/xen/p2m.c +++ b/arch/x86/xen/p2m.c @@ -161,6 +161,7 @@ #include #include #include +#include #include #include "multicalls.h" @@ -967,7 +968,9 @@ int m2p_remove_override(struct page *page, if (kmap_op != NULL) { if (!PageHighMem(page)) { struct multicall_space mcs; - struct gnttab_unmap_grant_ref *unmap_op; + struct gnttab_unmap_and_replace *unmap_op; + unsigned long trade_page_address = (unsigned long) + __va(page_to_pfn(balloon_trade_page) << PAGE_SHIFT); /* * It might be that we queued all the m2p grant table @@ -990,20 +993,28 @@ int m2p_remove_override(struct page *page, } mcs = xen_mc_entry( - sizeof(struct gnttab_unmap_grant_ref)); + sizeof(struct gnttab_unmap_and_replace)); unmap_op = mcs.args; unmap_op->host_addr = kmap_op->host_addr; + unmap_op->new_addr = trade_page_address; unmap_op->handle = kmap_op->handle; - unmap_op->dev_bus_addr = 0; MULTI_grant_table_op(mcs.mc, - GNTTABOP_unmap_grant_ref, unmap_op, 1); + gnttabop_unmap_and_replace, unmap_op, 1); xen_mc_issue(PARAVIRT_LAZY_MMU); - set_pte_at(&init_mm, address, ptep, - pfn_pte(pfn, PAGE_KERNEL)); - __flush_tlb_single(address); + /* this version of the hypercall is racy, let's try to limit + * the damage */ + if (unlikely(gnttabop_unmap_and_replace == + GNTTABOP_unmap_and_replace_legacy)) { + mcs = __xen_mc_entry(0); + + MULTI_update_va_mapping(mcs.mc, trade_page_address, + pfn_pte(page_to_pfn(balloon_trade_page), PAGE_KERNEL_RO), + 0); + xen_mc_issue(PARAVIRT_LAZY_MMU); + } kmap_op->host_addr = 0; } } diff --git a/drivers/xen/gntdev.c b/drivers/xen/gntdev.c index 3c8803f..51f4c95 100644 --- a/drivers/xen/gntdev.c +++ b/drivers/xen/gntdev.c @@ -270,19 +270,12 @@ static int map_grant_pages(struct grant_map *map) * with find_grant_ptes. */ for (i = 0; i < map->count; i++) { - unsigned level; unsigned long address = (unsigned long) pfn_to_kaddr(page_to_pfn(map->pages[i])); - pte_t *ptep; - u64 pte_maddr = 0; BUG_ON(PageHighMem(map->pages[i])); - ptep = lookup_address(address, &level); - pte_maddr = arbitrary_virt_to_machine(ptep).maddr; - gnttab_set_map_op(&map->kmap_ops[i], pte_maddr, - map->flags | - GNTMAP_host_map | - GNTMAP_contains_pte, + gnttab_set_map_op(&map->kmap_ops[i], address, + map->flags | GNTMAP_host_map, map->grants[i].ref, map->grants[i].domid); } -- 1.7.2.5