Skip to contentSkip to conversation

linux/i386: relax highpte pinning

PATCH In review Jan Beulich 2 participants 3 messages Jan 16, 2007 – Jan 22, 2007

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux/i386: relax highpte pinning

Just like making lowmem page tables read-only as late as possible, defer the pinning of highmem page tables.
The use of PG_arch_1 for both PG_foreign and PG_pinned is no longer possible now, therefore the patch also splits off PG_foreign to use a separate bit. At once, the patch fixes a bug uncovered by the original highpte patch, in that using virt_to_page() or pte_offset_kernel() in the context of pte handling is inappropriate.
The modifications to pmd_populate() include quite a bit of cleanup, resulting in the new macro being only slightly larger than the old one (otherwise it would have about doubled in size).
Finally (and I'm not insisting on this part, but I think it is appropriate), it adds PG_foreign and PG_pinned to the set of flags checked for during page allocation and freeing.

arch/i386/mm/highmem-xen.c +3 -1
Index: sle10-sp1-2007-01-12/arch/i386/mm/highmem-xen.c
===================================================================
@@ -55,7 +55,9 @@ void *kmap_atomic(struct page *page, enu
 /* Same as kmap_atomic but with PAGE_KERNEL_RO page protection. */
 void *kmap_atomic_pte(struct page *page, enum km_type type)
 {
-	return __kmap_atomic(page, type, PAGE_KERNEL_RO);
+	return __kmap_atomic(page, type,
+	                     test_bit(PG_pinned, &page->flags)
+	                     ? PAGE_KERNEL_RO : kmap_prot);
 }
 
 void kunmap_atomic(void *kvaddr, enum km_type type)
arch/i386/mm/pgtable-xen.c +24 -18

90 lines not on this page · raw message

include/asm-i386/mach-xen/asm/page.h +5 -6
Index: sle10-sp1-2007-01-12/include/asm-i386/mach-xen/asm/page.h
===================================================================
@@ -29,13 +29,12 @@
 #include <asm/bug.h>
 #include <xen/interface/xen.h>
 #include <xen/features.h>
-#include <xen/foreign_page.h>
 
-#define arch_free_page(_page,_order)			\
-({	int foreign = PageForeign(_page);		\
-	if (foreign)					\
-		(PageForeignDestructor(_page))(_page);	\
-	foreign;					\
+#define arch_free_page(_page,_order)		\
+({	int foreign = PageForeign(_page);	\
+	if (foreign)				\
+		PageForeignDestructor(_page);	\
+	foreign;				\
 })
 #define HAVE_ARCH_FREE_PAGE
 
include/asm-i386/mach-xen/asm/pgalloc.h +16 -13
Index: sle10-sp1-2007-01-12/include/asm-i386/mach-xen/asm/pgalloc.h
===================================================================
@@ -6,28 +6,31 @@
 #include <linux/threads.h>
 #include <linux/mm.h>		/* for struct page */
 #include <asm/io.h>		/* for phys_to_virt and page_to_pseudophys */
-
-/* Is this pagetable pinned? */
-#define PG_pinned	PG_arch_1
+#include <asm/hypervisor.h>
 
 #define pmd_populate_kernel(mm, pmd, pte) \
 		set_pmd(pmd, __pmd(_PAGE_TABLE + __pa(pte)))
 
 #define pmd_populate(mm, pmd, pte) 					\
 do {									\
+	unsigned long pfn = page_to_pfn(pte);				\
 	if (test_bit(PG_pinned, &virt_to_page((mm)->pgd)->flags)) {	\
 		if (!PageHighMem(pte))					\
 			BUG_ON(HYPERVISOR_update_va_mapping(		\
-			  (unsigned long)__va(page_to_pfn(pte)<<PAGE_SHIFT),\
-			  pfn_pte(page_to_pfn(pte), PAGE_KERNEL_RO), 0));\
-		set_pmd(pmd, __pmd(_PAGE_TABLE +			\
-			((unsigned long long)page_to_pfn(pte) <<	\
-				(unsigned long long) PAGE_SHIFT)));	\
-	} else {							\
-		*(pmd) = __pmd(_PAGE_TABLE +				\
-			((unsigned long long)page_to_pfn(pte) <<	\
-				(unsigned long long) PAGE_SHIFT));	\
-	}								\
+			  (unsigned long)__va(pfn << PAGE_SHIFT),	\
+			  pfn_pte(pfn, PAGE_KERNEL_RO), 0));		\
+		else if (!test_and_set_bit(PG_pinned, &pte->flags)) {	\
+			struct mmuext_op op;				\
+			kmap_flush_unused();				\
+			op.cmd = MMUEXT_PIN_L1_TABLE;			\
+			op.arg1.mfn = pfn_to_mfn(pfn);			\
+			BUG_ON(HYPERVISOR_mmuext_op(&op, 1, NULL,	\
+			                            DOMID_SELF) < 0);	\
+		}							\
+		set_pmd(pmd,						\
+		        __pmd(_PAGE_TABLE + ((paddr_t)pfn << PAGE_SHIFT))); \
+	} else							\
+		*(pmd) = __pmd(_PAGE_TABLE + ((paddr_t)pfn << PAGE_SHIFT)); \
 } while (0)
 
 /*
include/asm-i386/mach-xen/asm/pgtable.h +3 -0
Index: sle10-sp1-2007-01-12/include/asm-i386/mach-xen/asm/pgtable.h
===================================================================
@@ -26,6 +26,9 @@
 #include <linux/list.h>
 #include <linux/spinlock.h>
 
+/* Is this pagetable pinned? */
+#define PG_pinned	PG_arch_1
+
 struct mm_struct;
 struct vm_area_struct;
 
include/asm-x86_64/mach-xen/asm/page.h +5 -6
Index: sle10-sp1-2007-01-12/include/asm-x86_64/mach-xen/asm/page.h
===================================================================
@@ -9,13 +9,12 @@
 #include <asm/bug.h>
 #endif
 #include <xen/interface/xen.h> 
-#include <xen/foreign_page.h>
 
-#define arch_free_page(_page,_order)			\
-({	int foreign = PageForeign(_page);		\
-	if (foreign)					\
-		(PageForeignDestructor(_page))(_page);	\
-	foreign;					\
+#define arch_free_page(_page,_order)		\
+({	int foreign = PageForeign(_page);	\
+	if (foreign)				\
+		PageForeignDestructor(_page);	\
+	foreign;				\
 })
 #define HAVE_ARCH_FREE_PAGE
 
include/linux/page-flags.h +14 -0
Index: sle10-sp1-2007-01-12/include/linux/page-flags.h
===================================================================
@@ -76,6 +76,8 @@
 #define PG_nosave_free		18	/* Free, should not be written */
 #define PG_uncached		19	/* Page has been mapped as uncached */
 
+#define PG_foreign		20	/* Page is owned by foreign allocator. */
+
 /*
  * Global page accounting.  One instance per CPU.  Only unsigned longs are
  * allowed.
@@ -344,6 +346,18 @@ extern void __mod_page_state_offset(unsi
 #define SetPageUncached(page)	set_bit(PG_uncached, &(page)->flags)
 #define ClearPageUncached(page)	clear_bit(PG_uncached, &(page)->flags)
 
+#define PageForeign(page)	test_bit(PG_foreign, &(page)->flags)
+#define SetPageForeign(page, dtor) do {		\
+	set_bit(PG_foreign, &(page)->flags);	\
+	(page)->mapping = (void *)dtor;		\
+} while (0)
+#define ClearPageForeign(page) do {		\
+	clear_bit(PG_foreign, &(page)->flags);	\
+	(page)->mapping = NULL;			\
+} while (0)
+#define PageForeignDestructor(page)		\
+	( (void (*) (struct page *)) (page)->mapping )(page)
+
 struct page;	/* forward declaration */
 
 int test_clear_page_dirty(struct page *page);
include/xen/foreign_page.h deleted +0 -30
Index: sle10-sp1-2007-01-12/include/xen/foreign_page.h
===================================================================
@@ -1,30 +0,0 @@
-/******************************************************************************
- * foreign_page.h
- * 
- * Provide a "foreign" page type, that is owned by a foreign allocator and 
- * not the normal buddy allocator in page_alloc.c
- * 
- * Copyright (c) 2004, K A Fraser
- */
-
-#ifndef __ASM_XEN_FOREIGN_PAGE_H__
-#define __ASM_XEN_FOREIGN_PAGE_H__
-
-#define PG_foreign		PG_arch_1
-
-#define PageForeign(page)	test_bit(PG_foreign, &(page)->flags)
-
-#define SetPageForeign(page, dtor) do {		\
-	set_bit(PG_foreign, &(page)->flags);	\
-	(page)->mapping = (void *)dtor;		\
-} while (0)
-
-#define ClearPageForeign(page) do {		\
-	clear_bit(PG_foreign, &(page)->flags);	\
-	(page)->mapping = NULL;			\
-} while (0)
-
-#define PageForeignDestructor(page)	\
-	( (void (*) (struct page *)) (page)->mapping )
-
-#endif /* __ASM_XEN_FOREIGN_PAGE_H__ */
mm/page_alloc.c +15 -3
Index: sle10-sp1-2007-01-12/mm/page_alloc.c
===================================================================
@@ -154,7 +154,11 @@ static void bad_page(struct page *page)
 			1 << PG_slab    |
 			1 << PG_swapcache |
 			1 << PG_writeback |
-			1 << PG_buddy );
+			1 << PG_buddy	|
+#ifdef CONFIG_X86_XEN
+			1 << PG_pinned	|
+#endif
+			1 << PG_foreign );
 	set_page_count(page, 0);
 	reset_page_mapcount(page);
 	page->mapping = NULL;
@@ -368,7 +372,11 @@ static inline int free_pages_check(struc
 			1 << PG_swapcache |
 			1 << PG_writeback |
 			1 << PG_reserved |
-			1 << PG_buddy ))))
+			1 << PG_buddy	|
+#ifdef CONFIG_X86_XEN
+			1 << PG_pinned	|
+#endif
+			1 << PG_foreign ))))
 		bad_page(page);
 	if (PageDirty(page))
 		__ClearPageDirty(page);
@@ -528,7 +536,11 @@ static int prep_new_page(struct page *pa
 			1 << PG_swapcache |
 			1 << PG_writeback |
 			1 << PG_reserved |
-			1 << PG_buddy ))))
+			1 << PG_buddy	|
+#ifdef CONFIG_X86_XEN
+			1 << PG_pinned	|
+#endif
+			1 << PG_foreign ))))
 		bad_page(page);
 
 	/*
#1 lore raw

I like the cleanups in this patch, including making PageForeign a generic page flag (which it does indeed have to become).

However I'm fundamentally confused about why you believe highptes have to be explicitly pinned. They will be pinned automatically by mm_pin() when it pins the pgdir. The act of populating a pmd entry of a pinned mm will cause the pte page to be pinned also -- you don't need to do it explicitly.

So I'm not sure if you're working around an issue I haven't foreseen or if there's simply some confusion around this issue.

-- Keir

Quoted 16 lines · Jan Beulich

Just like making lowmem page tables read-only as late as possible, defer the pinning of highmem page tables.
The use of PG_arch_1 for both PG_foreign and PG_pinned is no longer possible now, therefore the patch also splits off PG_foreign to use a separate bit. At once, the patch fixes a bug uncovered by the original highpte patch, in that
using virt_to_page() or pte_offset_kernel() in the context of pte handling is inappropriate.
The modifications to pmd_populate() include quite a bit of cleanup, resulting in
the new macro being only slightly larger than the old one (otherwise it would have about doubled in size).
Finally (and I'm not insisting on this part, but I think it is appropriate), it adds
PG_foreign and PG_pinned to the set of flags checked for during page allocation and freeing.

#2 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 17.01.07 15:51 >>>

I like the cleanups in this patch, including making PageForeign a generic page flag (which it does indeed have to become).

However I'm fundamentally confused about why you believe highptes have to be explicitly pinned. They will be pinned automatically by mm_pin() when it pins the pgdir. The act of populating a pmd entry of a pinned mm will cause the pte page to be pinned also -- you don't need to do it explicitly.

So I'm not sure if you're working around an issue I haven't foreseen or if there's simply some confusion around this issue.

Here's the updated patch, without any addition of explicit pinning.

arch/i386/mm/highmem-xen.c +3 -1
Index: sle10sp1-2007-01-22/arch/i386/mm/highmem-xen.c
===================================================================
@@ -55,7 +55,9 @@ void *kmap_atomic(struct page *page, enu
 /* Same as kmap_atomic but with PAGE_KERNEL_RO page protection. */
 void *kmap_atomic_pte(struct page *page, enum km_type type)
 {
-	return __kmap_atomic(page, type, PAGE_KERNEL_RO);
+	return __kmap_atomic(page, type,
+	                     test_bit(PG_pinned, &page->flags)
+	                     ? PAGE_KERNEL_RO : kmap_prot);
 }
 
 void kunmap_atomic(void *kvaddr, enum km_type type)
arch/i386/mm/pgtable-xen.c +22 -30

120 lines not on this page · raw message

include/asm-i386/mach-xen/asm/page.h +5 -6
Index: sle10sp1-2007-01-22/include/asm-i386/mach-xen/asm/page.h
===================================================================
@@ -28,13 +28,12 @@
 #include <asm/bug.h>
 #include <xen/interface/xen.h>
 #include <xen/features.h>
-#include <xen/foreign_page.h>
 
-#define arch_free_page(_page,_order)			\
-({	int foreign = PageForeign(_page);		\
-	if (foreign)					\
-		(PageForeignDestructor(_page))(_page);	\
-	foreign;					\
+#define arch_free_page(_page,_order)		\
+({	int foreign = PageForeign(_page);	\
+	if (foreign)				\
+		PageForeignDestructor(_page);	\
+	foreign;				\
 })
 #define HAVE_ARCH_FREE_PAGE
 
include/asm-i386/mach-xen/asm/pgalloc.h +9 -13
Index: sle10sp1-2007-01-22/include/asm-i386/mach-xen/asm/pgalloc.h
===================================================================
@@ -6,27 +6,23 @@
 #include <linux/mm.h>		/* for struct page */
 #include <asm/io.h>		/* for phys_to_virt and page_to_pseudophys */
 
-/* Is this pagetable pinned? */
-#define PG_pinned	PG_arch_1
-
 #define pmd_populate_kernel(mm, pmd, pte) \
 		set_pmd(pmd, __pmd(_PAGE_TABLE + __pa(pte)))
 
 #define pmd_populate(mm, pmd, pte) 					\
 do {									\
+	unsigned long pfn = page_to_pfn(pte);				\
 	if (test_bit(PG_pinned, &virt_to_page((mm)->pgd)->flags)) {	\
 		if (!PageHighMem(pte))					\
 			BUG_ON(HYPERVISOR_update_va_mapping(		\
-			  (unsigned long)__va(page_to_pfn(pte)<<PAGE_SHIFT),\
-			  pfn_pte(page_to_pfn(pte), PAGE_KERNEL_RO), 0));\
-		set_pmd(pmd, __pmd(_PAGE_TABLE +			\
-			((unsigned long long)page_to_pfn(pte) <<	\
-				(unsigned long long) PAGE_SHIFT)));	\
-	} else {							\
-		*(pmd) = __pmd(_PAGE_TABLE +				\
-			((unsigned long long)page_to_pfn(pte) <<	\
-				(unsigned long long) PAGE_SHIFT));	\
-	}								\
+			  (unsigned long)__va(pfn << PAGE_SHIFT),	\
+			  pfn_pte(pfn, PAGE_KERNEL_RO), 0));		\
+		else if (!test_and_set_bit(PG_pinned, &pte->flags))	\
+			kmap_flush_unused();				\
+		set_pmd(pmd,						\
+		        __pmd(_PAGE_TABLE + ((paddr_t)pfn << PAGE_SHIFT))); \
+	} else							\
+		*(pmd) = __pmd(_PAGE_TABLE + ((paddr_t)pfn << PAGE_SHIFT)); \
 } while (0)
 
 /*
include/asm-i386/mach-xen/asm/pgtable.h +3 -0
Index: sle10sp1-2007-01-22/include/asm-i386/mach-xen/asm/pgtable.h
===================================================================
@@ -25,6 +25,9 @@
 #include <linux/list.h>
 #include <linux/spinlock.h>
 
+/* Is this pagetable pinned? */
+#define PG_pinned	PG_arch_1
+
 struct mm_struct;
 struct vm_area_struct;
 
include/asm-x86_64/mach-xen/asm/page.h +5 -6
Index: sle10sp1-2007-01-22/include/asm-x86_64/mach-xen/asm/page.h
===================================================================
@@ -8,13 +8,12 @@
 #include <asm/bug.h>
 #endif
 #include <xen/interface/xen.h> 
-#include <xen/foreign_page.h>
 
-#define arch_free_page(_page,_order)			\
-({	int foreign = PageForeign(_page);		\
-	if (foreign)					\
-		(PageForeignDestructor(_page))(_page);	\
-	foreign;					\
+#define arch_free_page(_page,_order)		\
+({	int foreign = PageForeign(_page);	\
+	if (foreign)				\
+		PageForeignDestructor(_page);	\
+	foreign;				\
 })
 #define HAVE_ARCH_FREE_PAGE
 
include/linux/page-flags.h +14 -0
Index: sle10-sp1-2007-01-12/include/linux/page-flags.h
===================================================================
@@ -76,6 +76,8 @@
 #define PG_nosave_free		18	/* Free, should not be written */
 #define PG_uncached		19	/* Page has been mapped as uncached */
 
+#define PG_foreign		20	/* Page is owned by foreign allocator. */
+
 /*
  * Global page accounting.  One instance per CPU.  Only unsigned longs are
  * allowed.
@@ -344,6 +346,18 @@ extern void __mod_page_state_offset(unsi
 #define SetPageUncached(page)	set_bit(PG_uncached, &(page)->flags)
 #define ClearPageUncached(page)	clear_bit(PG_uncached, &(page)->flags)
 
+#define PageForeign(page)	test_bit(PG_foreign, &(page)->flags)
+#define SetPageForeign(page, dtor) do {		\
+	set_bit(PG_foreign, &(page)->flags);	\
+	(page)->mapping = (void *)dtor;		\
+} while (0)
+#define ClearPageForeign(page) do {		\
+	clear_bit(PG_foreign, &(page)->flags);	\
+	(page)->mapping = NULL;			\
+} while (0)
+#define PageForeignDestructor(page)		\
+	( (void (*) (struct page *)) (page)->mapping )(page)
+
 struct page;	/* forward declaration */
 
 int test_clear_page_dirty(struct page *page);
include/xen/foreign_page.h deleted +0 -30
Index: sle10-sp1-2007-01-12/include/xen/foreign_page.h
===================================================================
@@ -1,30 +0,0 @@
-/******************************************************************************
- * foreign_page.h
- * 
- * Provide a "foreign" page type, that is owned by a foreign allocator and 
- * not the normal buddy allocator in page_alloc.c
- * 
- * Copyright (c) 2004, K A Fraser
- */
-
-#ifndef __ASM_XEN_FOREIGN_PAGE_H__
-#define __ASM_XEN_FOREIGN_PAGE_H__
-
-#define PG_foreign		PG_arch_1
-
-#define PageForeign(page)	test_bit(PG_foreign, &(page)->flags)
-
-#define SetPageForeign(page, dtor) do {		\
-	set_bit(PG_foreign, &(page)->flags);	\
-	(page)->mapping = (void *)dtor;		\
-} while (0)
-
-#define ClearPageForeign(page) do {		\
-	clear_bit(PG_foreign, &(page)->flags);	\
-	(page)->mapping = NULL;			\
-} while (0)
-
-#define PageForeignDestructor(page)	\
-	( (void (*) (struct page *)) (page)->mapping )
-
-#endif /* __ASM_XEN_FOREIGN_PAGE_H__ */
mm/page_alloc.c +15 -3
Index: sle10-sp1-2007-01-12/mm/page_alloc.c
===================================================================
@@ -154,7 +154,11 @@ static void bad_page(struct page *page)
 			1 << PG_slab    |
 			1 << PG_swapcache |
 			1 << PG_writeback |
-			1 << PG_buddy );
+			1 << PG_buddy	|
+#ifdef CONFIG_X86_XEN
+			1 << PG_pinned	|
+#endif
+			1 << PG_foreign );
 	set_page_count(page, 0);
 	reset_page_mapcount(page);
 	page->mapping = NULL;
@@ -389,7 +393,11 @@ static inline int free_pages_check(struc
 			1 << PG_swapcache |
 			1 << PG_writeback |
 			1 << PG_reserved |
-			1 << PG_buddy ))))
+			1 << PG_buddy	|
+#ifdef CONFIG_X86_XEN
+			1 << PG_pinned	|
+#endif
+			1 << PG_foreign ))))
 		bad_page(page);
 	if (PageDirty(page))
 		__ClearPageDirty(page);
@@ -539,7 +547,11 @@ static int prep_new_page(struct page *pa
 			1 << PG_swapcache |
 			1 << PG_writeback |
 			1 << PG_reserved |
-			1 << PG_buddy ))))
+			1 << PG_buddy	|
+#ifdef CONFIG_X86_XEN
+			1 << PG_pinned	|
+#endif
+			1 << PG_foreign ))))
 		bad_page(page);
 
 	/*