Skip to contentSkip to conversation

mm: infrastructure for correctly handling foreign pages on Xen

PATCH Superseded David Vrabel 3 participants 5 messages Jan 12, 2015

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover mm: infrastructure for correctly handling foreign pages on Xen

These two patches are the common parts of a larger Xen series[1] fixing several long-standing bugs the handling of foreign[2] pages in Xen guests.

The first patch is required to fix get_user_pages[_fast]() with userspace space mappings of such foreign pages. Basically, pte_page() doesn't work so an alternate mechanism is needed to get the page from a VMA and address. By requiring mappings needing this method are 'special' this should not have an impact on the common use cases.

The second patch isn't essential but helps with readability of the resulting user of the page flag.

For further background reading see:

http://xenbits.xen.org/people/dvrabel/grant-improvements-C.pdf

Changes in v2:

- Add a find_page VMA op instead of the pages field so: a) the size of struct vm_area_struct does not increase; and b) the common code need not handling splitting the pages area.

David

[1] http://lists.xen.org/archives/html/xen-devel/2015-01/msg00979.html

[2] Another guest's page temporarily granted to this guest.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#1 lore raw

PATCH v2 1/2 mm: provide a find_page vma operation

The optional find_page VMA operation is used to lookup the pages backing a VMA. This is useful in cases where the normal mechanisms for finding the page don't work. This is only called if the PTE is special.

One use case is a Xen PV guest mapping foreign pages into userspace.

In a Xen PV guest, the PTEs contain MFNs so get_user_pages() (for example) must do an MFN to PFN (M2P) lookup before it can get the page. For foreign pages (those owned by another guest) the M2P lookup returns the PFN as seen by the foreign guest (which would be completely the wrong page for the local guest).

This cannot be fixed up improving the M2P lookup since one MFN may be mapped onto two or more pages so getting the right page is impossible given just the MFN.

---
 include/linux/mm.h |    3 +++
 mm/memory.c        |    2 ++
 2 files changed, 5 insertions(+)
include/linux/mm.h +3 -0
index 80fc92a..1306643 100644
@@ -290,6 +290,9 @@ struct vm_operations_struct {
 	/* called by sys_remap_file_pages() to populate non-linear mapping */
 	int (*remap_pages)(struct vm_area_struct *vma, unsigned long addr,
 			   unsigned long size, pgoff_t pgoff);
+
+	struct page * (*find_page)(struct vm_area_struct *vma,
+				   unsigned long addr);
 };
 
 struct mmu_gather;
mm/memory.c +2 -0
index c6565f0..f23a862 100644
@@ -754,6 +754,8 @@ struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
 	if (HAVE_PTE_SPECIAL) {
 		if (likely(!pte_special(pte)))
 			goto check_pfn;
+		if (vma->vm_ops && vma->vm_ops->find_page)
+			return vma->vm_ops->find_page(vma, addr);
 		if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
 			return NULL;
 		if (!is_zero_pfn(pfn))
Signature · 7 lines
1.7.10.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#2 lore raw

Re: [PATCH 1/2] mm: provide a find_page vma operation

Quoted 13 lines · David Vrabel

The optional find_page VMA operation is used to lookup the pages backing a VMA. This is useful in cases where the normal mechanisms for finding the page don't work. This is only called if the PTE is special.

One use case is a Xen PV guest mapping foreign pages into userspace.

In a Xen PV guest, the PTEs contain MFNs so get_user_pages() (for example) must do an MFN to PFN (M2P) lookup before it can get the page. For foreign pages (those owned by another guest) the M2P lookup returns the PFN as seen by the foreign guest (which would be completely the wrong page for the local guest).

Quoted 5 lines

This cannot be fixed up improving the M2P lookup since one MFN may be mapped onto two or more pages so getting the right page is impossible given just the MFN.

Looks much better to me, thanks.

Quoted 10 lines
---
 include/linux/mm.h |    3 +++
 mm/memory.c        |    2 ++
 2 files changed, 5 insertions(+)
include/linux/mm.h +0 -0
index 80fc92a..1306643 100644

include/linux/mm.h struct vm_operations_struct { · line 295

Quoted 6 lines
include/linux/mm.h +3 -0
@@ -290,6 +290,9 @@ struct vm_operations_struct {
 	/* called by sys_remap_file_pages() to populate non-linear mapping */
 	int (*remap_pages)(struct vm_area_struct *vma, unsigned long addr,
 			   unsigned long size, pgoff_t pgoff);
+
+	struct page * (*find_page)(struct vm_area_struct *vma,
+				   unsigned long addr);

Could you please add a comment what that method is used for?

It would probably also be useful if the name reflected that this only applies to special ptes. find_special_page()? lookup_special_pte()? pte_special_page()?

Thanks

include/linux/mm.h struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, · line 761

Quoted 9 lines
include/linux/mm.h +2 -0
@@ -754,6 +754,8 @@ struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
 	if (HAVE_PTE_SPECIAL) {
 		if (likely(!pte_special(pte)))
 			goto check_pfn;
+		if (vma->vm_ops && vma->vm_ops->find_page)
+			return vma->vm_ops->find_page(vma, addr);
 		if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
 			return NULL;
 		if (!is_zero_pfn(pfn))

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw

PATCH v2 2/2 mm: add 'foreign' alias for the 'pinned' page flag

In reply to David Vrabel

From: Jenny Herbert <[email protected]>

The foreign page flag will be used by Xen guests to mark pages that have grant mappings of frames from other (foreign) guests.

The foreign flag is an alias for the existing (Xen-specific) pinned flag. This is safe because pinned is only used on pages used for page tables and these cannot also be foreign.

---
 include/linux/page-flags.h |    2 ++
 1 file changed, 2 insertions(+)
include/linux/page-flags.h +2 -0
index e1f5fcd..7734cc8 100644
@@ -123,6 +123,7 @@ enum pageflags {
 	/* XEN */
 	PG_pinned = PG_owner_priv_1,
 	PG_savepinned = PG_dirty,
+	PG_foreign = PG_owner_priv_1,
 
 	/* SLOB */
 	PG_slob_free = PG_private,
@@ -215,6 +216,7 @@ __PAGEFLAG(Slab, slab)
 PAGEFLAG(Checked, checked)		/* Used by some filesystems */
 PAGEFLAG(Pinned, pinned) TESTSCFLAG(Pinned, pinned)	/* Xen */
 PAGEFLAG(SavePinned, savepinned);			/* Xen */
+PAGEFLAG(Foreign, foreign);				/* Xen */
 PAGEFLAG(Reserved, reserved) __CLEARPAGEFLAG(Reserved, reserved)
 PAGEFLAG(SwapBacked, swapbacked) __CLEARPAGEFLAG(SwapBacked, swapbacked)
 	__SETPAGEFLAG(SwapBacked, swapbacked)
Signature · 7 lines
1.7.10.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#4 lore raw
In reply to David Vrabel
Quoted 10 lines · On Mon, 12 Jan 2015 15:53:11 +0000 David Vrabel <[email protected]> wrote:

These two patches are the common parts of a larger Xen series[1] fixing several long-standing bugs the handling of foreign[2] pages in Xen guests.

The first patch is required to fix get_user_pages[_fast]() with userspace space mappings of such foreign pages. Basically, pte_page() doesn't work so an alternate mechanism is needed to get the page from a VMA and address. By requiring mappings needing this method are 'special' this should not have an impact on the common use cases.

Quoted 7 lines

The second patch isn't essential but helps with readability of the resulting user of the page flag.

For further background reading see:

http://xenbits.xen.org/people/dvrabel/grant-improvements-C.pdf

Looks OK to me. I can merge them if you like, but it's probably more convenient for you to include them in the Xen tree.

It would be nice if PG_foreign (and PG_everythingelse) was properly documented at the definition site.