All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Vrabel <david.vrabel@citrix.com>
To: xen-devel@lists.xenproject.org
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>,
	Jennifer Herbert <jennifer.herbert@citrix.com>,
	David Vrabel <david.vrabel@citrix.com>
Subject: [PATCHv4 07/14] xen: mark grant mapped pages as foreign
Date: Mon, 26 Jan 2015 17:01:20 +0000	[thread overview]
Message-ID: <1422291687-7398-8-git-send-email-david.vrabel@citrix.com> (raw)
In-Reply-To: <1422291687-7398-1-git-send-email-david.vrabel@citrix.com>

From: Jennifer Herbert <jennifer.herbert@citrix.com>

Use the "foreign" page flag to mark pages that have a grant map.  Use
page->private to store information of the grant (the granting domain
and the grant reference).

Signed-off-by: Jennifer Herbert <jennifer.herbert@citrix.com>
Signed-off-by: David Vrabel <david.vrabel@citrix.com>
---
 arch/x86/xen/p2m.c        |    7 -------
 drivers/xen/grant-table.c |   43 +++++++++++++++++++++++++++++++++++++++++--
 include/xen/grant_table.h |   20 ++++++++++++++++++++
 3 files changed, 61 insertions(+), 9 deletions(-)

diff --git a/arch/x86/xen/p2m.c b/arch/x86/xen/p2m.c
index a8691cb..f18fd1d 100644
--- a/arch/x86/xen/p2m.c
+++ b/arch/x86/xen/p2m.c
@@ -681,12 +681,8 @@ int set_foreign_p2m_mapping(struct gnttab_map_grant_ref *map_ops,
 		}
 		pfn = page_to_pfn(pages[i]);
 
-		WARN_ON(PagePrivate(pages[i]));
 		WARN(pfn_to_mfn(pfn) != INVALID_P2M_ENTRY, "page must be ballooned");
 
-		SetPagePrivate(pages[i]);
-		set_page_private(pages[i], mfn);
-
 		if (unlikely(!set_phys_to_machine(pfn, FOREIGN_FRAME(mfn)))) {
 			ret = -ENOMEM;
 			goto out;
@@ -716,9 +712,6 @@ int clear_foreign_p2m_mapping(struct gnttab_unmap_grant_ref *unmap_ops,
 			goto out;
 		}
 
-		set_page_private(pages[i], INVALID_P2M_ENTRY);
-		WARN_ON(!PagePrivate(pages[i]));
-		ClearPagePrivate(pages[i]);
 		set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
 	}
 	if (kunmap_ops)
diff --git a/drivers/xen/grant-table.c b/drivers/xen/grant-table.c
index b4f93c4..89dcca4 100644
--- a/drivers/xen/grant-table.c
+++ b/drivers/xen/grant-table.c
@@ -679,12 +679,27 @@ EXPORT_SYMBOL_GPL(gnttab_free_auto_xlat_frames);
  */
 int gnttab_alloc_pages(int nr_pages, struct page **pages)
 {
+	int i;
 	int ret;
 
 	ret = alloc_xenballooned_pages(nr_pages, pages, false);
 	if (ret < 0)
 		return ret;
 
+	for (i = 0; i < nr_pages; i++) {
+#if BITS_PER_LONG < 64
+		struct xen_page_foreign *foreign;
+
+		foreign = kzalloc(sizeof(*foreign), GFP_KERNEL);
+		if (!foreign) {
+			gnttab_free_pages(nr_pages, pages);
+			return -ENOMEM;
+		}
+		set_page_private(pages[i], (unsigned long)foreign);
+#endif
+		SetPagePrivate(pages[i]);
+	}
+
 	return 0;
 }
 EXPORT_SYMBOL(gnttab_alloc_pages);
@@ -696,6 +711,16 @@ EXPORT_SYMBOL(gnttab_alloc_pages);
  */
 void gnttab_free_pages(int nr_pages, struct page **pages)
 {
+	int i;
+
+	for (i = 0; i < nr_pages; i++) {
+		if (PagePrivate(pages[i])) {
+#if BITS_PER_LONG < 64
+			kfree((void *)page_private(pages[i]));
+#endif
+			ClearPagePrivate(pages[i]);
+		}
+	}
 	free_xenballooned_pages(nr_pages, pages);
 }
 EXPORT_SYMBOL(gnttab_free_pages);
@@ -756,12 +781,22 @@ int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
 	if (ret)
 		return ret;
 
-	/* Retry eagain maps */
-	for (i = 0; i < count; i++)
+	for (i = 0; i < count; i++) {
+		/* Retry eagain maps */
 		if (map_ops[i].status == GNTST_eagain)
 			gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, map_ops + i,
 						&map_ops[i].status, __func__);
 
+		if (map_ops[i].status == GNTST_okay) {
+			struct xen_page_foreign *foreign;
+
+			SetPageForeign(pages[i]);
+			foreign = xen_page_foreign(pages[i]);
+			foreign->domid = map_ops[i].dom;
+			foreign->gref = map_ops[i].ref;
+		}
+	}
+
 	return set_foreign_p2m_mapping(map_ops, kmap_ops, pages, count);
 }
 EXPORT_SYMBOL_GPL(gnttab_map_refs);
@@ -770,12 +805,16 @@ int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
 		      struct gnttab_unmap_grant_ref *kunmap_ops,
 		      struct page **pages, unsigned int count)
 {
+	unsigned int i;
 	int ret;
 
 	ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap_ops, count);
 	if (ret)
 		return ret;
 
+	for (i = 0; i < count; i++)
+		ClearPageForeign(pages[i]);
+
 	return clear_foreign_p2m_mapping(unmap_ops, kunmap_ops, pages, count);
 }
 EXPORT_SYMBOL_GPL(gnttab_unmap_refs);
diff --git a/include/xen/grant_table.h b/include/xen/grant_table.h
index 949803e..d3bef56 100644
--- a/include/xen/grant_table.h
+++ b/include/xen/grant_table.h
@@ -45,6 +45,8 @@
 #include <asm/xen/hypervisor.h>
 
 #include <xen/features.h>
+#include <linux/mm_types.h>
+#include <linux/page-flags.h>
 
 #define GNTTAB_RESERVED_XENSTORE 1
 
@@ -185,4 +187,22 @@ int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
 void gnttab_batch_map(struct gnttab_map_grant_ref *batch, unsigned count);
 void gnttab_batch_copy(struct gnttab_copy *batch, unsigned count);
 
+
+struct xen_page_foreign {
+	domid_t domid;
+	grant_ref_t gref;
+};
+
+static inline struct xen_page_foreign *xen_page_foreign(struct page *page)
+{
+	if (!PageForeign(page))
+		return NULL;
+#if BITS_PER_LONG < 64
+	return (struct xen_page_foreign *)page->private;
+#else
+	BUILD_BUG_ON(sizeof(struct xen_page_foreign) > BITS_PER_LONG);
+	return (struct xen_page_foreign *)&page->private;
+#endif
+}
+
 #endif /* __ASM_GNTTAB_H__ */
-- 
1.7.10.4

  parent reply	other threads:[~2015-01-26 17:01 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-26 17:01 [PATCHv4 00/14] xen: fix many long-standing grant mapping bugs David Vrabel
2015-01-26 17:01 ` [PATCHv4 01/14] mm: provide a find_special_page vma operation David Vrabel
2015-01-26 17:01 ` [PATCHv4 02/14] mm: add 'foreign' alias for the 'pinned' page flag David Vrabel
2015-01-26 17:01 ` [PATCHv4 03/14] xen/grant-table: pre-populate kernel unmap ops for xen_gnttab_unmap_refs() David Vrabel
2015-01-26 18:31   ` Stefano Stabellini
2015-01-27 11:55     ` David Vrabel
2015-01-26 17:01 ` [PATCHv4 04/14] xen: remove scratch frames for ballooned pages and m2p override David Vrabel
2015-01-27 10:57   ` Stefano Stabellini
2015-01-27 11:00     ` David Vrabel
2015-01-27 11:10       ` Stefano Stabellini
2015-01-27 11:14         ` David Vrabel
2015-01-27 11:17           ` Stefano Stabellini
2015-01-26 17:01 ` [PATCHv4 05/14] x86/xen: require ballooned pages for grant maps David Vrabel
2015-01-27 11:08   ` Stefano Stabellini
2015-01-26 17:01 ` [PATCHv4 06/14] xen/grant-table: add helpers for allocating pages David Vrabel
2015-01-27 11:13   ` Stefano Stabellini
2015-01-26 17:01 ` David Vrabel [this message]
2015-01-27 11:32   ` [PATCHv4 07/14] xen: mark grant mapped pages as foreign Stefano Stabellini
2015-01-26 17:01 ` [PATCHv4 08/14] xen-netback: use foreign page information from the pages themselves David Vrabel
2015-01-26 17:01 ` [PATCHv4 09/14] xen/grant-table: add a mechanism to safely unmap pages that are in use David Vrabel
2015-01-26 19:14   ` Stefano Stabellini
2015-01-26 17:01 ` [PATCHv4 10/14] xen/gntdev: convert priv->lock to a mutex David Vrabel
2015-01-26 18:57   ` Stefano Stabellini
2015-01-26 19:17     ` David Vrabel
2015-01-26 21:07       ` Stefano Stabellini
2015-01-27 10:00         ` David Vrabel
2015-01-27 10:20           ` Stefano Stabellini
2015-01-26 17:01 ` [PATCHv4 11/14] xen/gntdev: safely unmap grants in case they are still in use David Vrabel
2015-01-27 11:37   ` Stefano Stabellini
2015-01-26 17:01 ` [PATCHv4 12/14] xen-blkback: " David Vrabel
2015-01-26 19:21   ` Roger Pau Monné
2015-01-27 11:34   ` Stefano Stabellini
2015-01-26 17:01 ` [PATCHv4 13/14] xen/gntdev: mark userspace PTEs as special on x86 PV guests David Vrabel
2015-01-27 11:42   ` Stefano Stabellini
2015-01-26 17:01 ` [PATCHv4 14/14] xen/gntdev: provide find_special_page VMA operation David Vrabel
2015-01-27 11:45   ` Stefano Stabellini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1422291687-7398-8-git-send-email-david.vrabel@citrix.com \
    --to=david.vrabel@citrix.com \
    --cc=boris.ostrovsky@oracle.com \
    --cc=jennifer.herbert@citrix.com \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.