Skip to contentSkip to conversation

Mark hugepages to the hypervisor to speed up mapping (with signoff this time)

PATCH In review Dave McCracken 2 participants 3 messages Jun 9, 2010

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

PATCH 1/1 Mark hugepages to the hypervisor to speed up mapping (with signoff this time)

Two new hypercalls were recently added to the Xen hypervisor to mark and unmark hugepages as in use. Once a hugepage has been marked, actually mapping the hugepage is sped up by a significant amount (more than 20 times faster).

This patch marks hugepages when they are added to the hugepage pool and unmarks them when they are removed. It also checks to make sure each hugepage is contiguous at the machine level.

--------

/arch/x86/xen/mmu.c renamed from arch/x86/xen/mmu.c +48 -0
@@ -2531,3 +2531,51 @@ static int __init xen_mmu_debugfs(void)
 fs_initcall(xen_mmu_debugfs);
 
 #endif	/* CONFIG_XEN_DEBUG_FS */
+
+static int mark_ok = 1;
+
+int arch_prepare_hugepage(struct page *page)
+{
+	struct mmuext_op op;
+	unsigned long pfn, mfn, m;
+	int i;
+	int rc;
+
+	pfn = page_to_pfn(page);
+	mfn = pfn_to_mfn(pfn);
+	if (mfn & ((HPAGE_SIZE/PAGE_SIZE)-1)) {
+		printk("Guest pages are not properly aligned to use hugepages\n");
+		return 1;
+	}
+	for (i = 0, m = mfn; i < HPAGE_SIZE/PAGE_SIZE; i++, pfn++, m++) {
+		if (pfn_to_mfn(pfn) != m) {
+			printk("Guest pages are not properly aligned to use hugepages\n");
+			return 1;
+		}
+	}
+	/* It's ok if this fails.  We just fall back to the slow refcounting */
+	if (mark_ok) {
+		op.cmd = MMUEXT_MARK_SUPER;
+		op.arg1.mfn = mfn;
+		rc = HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF);
+		if (rc) {
+			if (rc == -ENOSYS) {
+				mark_ok = 0;
+				return 0;
+			}
+			return 1;
+		}
+	}
+	return 0;
+}
+
+void arch_release_hugepage(struct page *page)
+{
+	struct mmuext_op op;
+
+	if (mark_ok) {
+		op.cmd = MMUEXT_UNMARK_SUPER;
+		op.arg1.mfn = pfn_to_mfn(page_to_pfn(page));
+		HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF);
+	}
+}
/arch/x86/include/asm/hugetlb.h renamed from arch/x86/include/asm/hugetlb.h +5 -0
@@ -95,6 +95,10 @@ static inline int huge_ptep_set_access_f
 	return changed;
 }
 
+#ifdef CONFIG_XEN
+int arch_prepare_hugepage(struct page *page);
+void arch_release_hugepage(struct page *page);
+#else
 static inline int arch_prepare_hugepage(struct page *page)
 {
 	return 0;
@@ -103,5 +107,6 @@ static inline int arch_prepare_hugepage(
 static inline void arch_release_hugepage(struct page *page)
 {
 }
+#endif
 
 #endif /* _ASM_X86_HUGETLB_H */
/include/xen/interface/xen.h renamed from include/xen/interface/xen.h +17 -0
@@ -167,6 +167,19 @@
  * cmd: MMUEXT_SET_LDT
  * linear_addr: Linear address of LDT base (NB. must be page-aligned).
  * nr_ents: Number of entries in LDT.
+ *
+ * cmd: MMUEXT_CLEAR_PAGE
+ * mfn: Machine frame number to be cleared.
+ *
+ * cmd: MMUEXT_COPY_PAGE
+ * mfn: Machine frame number of the destination page.
+ * src_mfn: Machine frame number of the source page.
+ *
+ * cmd: MMUEXT_MARK_SUPER
+ * mfn: Machine frame number of head of superpage to be marked.
+ *
+ * cmd: MMUEXT_UNMARK_SUPER
+ * mfn: Machine frame number of head of superpage to be cleared.
  */
 #define MMUEXT_PIN_L1_TABLE      0
 #define MMUEXT_PIN_L2_TABLE      1
@@ -183,6 +196,10 @@
 #define MMUEXT_FLUSH_CACHE      12
 #define MMUEXT_SET_LDT          13
 #define MMUEXT_NEW_USER_BASEPTR 15
+#define MMUEXT_CLEAR_PAGE       16
+#define MMUEXT_COPY_PAGE        17
+#define MMUEXT_MARK_SUPER       19
+#define MMUEXT_UNMARK_SUPER     20
 
 #ifndef __ASSEMBLY__
 #include <linux/types.h>
#1 lore raw
Quoted 18 lines · Dave McCracken

Two new hypercalls were recently added to the Xen hypervisor to mark and unmark hugepages as in use. Once a hugepage has been marked, actually mapping the hugepage is sped up by a significant amount (more than 20 times faster).

This patch marks hugepages when they are added to the hugepage pool and unmarks them when they are removed. It also checks to make sure each hugepage is contiguous at the machine level.

--------

/arch/x86/xen/mmu.c static int __init xen_mmu_debugfs(void) · line 2535

Quoted 5 lines
/arch/x86/xen/mmu.c renamed from arch/x86/xen/mmu.c +2 -0
@@ -2531,3 +2531,51 @@ static int __init xen_mmu_debugfs(void)
 fs_initcall(xen_mmu_debugfs);
 
 #endif	/* CONFIG_XEN_DEBUG_FS */
+
+static int mark_ok = 1;

__read_mostly

/arch/x86/xen/mmu.c static int __init xen_mmu_debugfs(void) · line 2547

Quoted 13 lines
/arch/x86/xen/mmu.c renamed from arch/x86/xen/mmu.c +12 -0
+
+int arch_prepare_hugepage(struct page *page)
+{
+	struct mmuext_op op;
+	unsigned long pfn, mfn, m;
+	int i;
+	int rc;
+
+	pfn = page_to_pfn(page);
+	mfn = pfn_to_mfn(pfn);
+	if (mfn & ((HPAGE_SIZE/PAGE_SIZE)-1)) {
+		printk("Guest pages are not properly aligned to use hugepages\n");

Don't use printk to do this. Use WARN() to print a stack trace so you can work out who's doing this.

/arch/x86/xen/mmu.c static int __init xen_mmu_debugfs(void) · line 2552

Quoted 6 lines
/arch/x86/xen/mmu.c renamed from arch/x86/xen/mmu.c +5 -0
+		return 1;
+	}
+	for (i = 0, m = mfn; i < HPAGE_SIZE/PAGE_SIZE; i++, pfn++, m++) {
+		if (pfn_to_mfn(pfn) != m) {
+			printk("Guest pages are not properly aligned to use hugepages\n");

Isn't this a superset of the previous test?

Quoted 43 lines
/arch/x86/xen/mmu.c renamed from arch/x86/xen/mmu.c +29 -0
+			return 1;
+		}
+	}
+	/* It's ok if this fails.  We just fall back to the slow refcounting */
+	if (mark_ok) {
+		op.cmd = MMUEXT_MARK_SUPER;
+		op.arg1.mfn = mfn;
+		rc = HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF);
+		if (rc) {
+			if (rc == -ENOSYS) {
+				mark_ok = 0;
+				return 0;
+			}
+			return 1;
+		}
+	}
+	return 0;
+}
+
+void arch_release_hugepage(struct page *page)
+{
+	struct mmuext_op op;
+
+	if (mark_ok) {
+		op.cmd = MMUEXT_UNMARK_SUPER;
+		op.arg1.mfn = pfn_to_mfn(page_to_pfn(page));
+		HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF);
+	}
+}
/arch/x86/include/asm/hugetlb.h renamed from arch/x86/include/asm/hugetlb.h +4 -0
@@ -95,6 +95,10 @@ static inline int huge_ptep_set_access_f
 	return changed;
 }
 
+#ifdef CONFIG_XEN
+int arch_prepare_hugepage(struct page *page);
+void arch_release_hugepage(struct page *page);
+#else
 static inline int arch_prepare_hugepage(struct page *page)
 {
 	return 0;

/arch/x86/include/asm/hugetlb.h static inline int arch_prepare_hugepage( · line 111

Quoted 5 lines
/arch/x86/include/asm/hugetlb.h renamed from arch/x86/include/asm/hugetlb.h +1 -0
@@ -103,5 +107,6 @@ static inline int arch_prepare_hugepage(
 static inline void arch_release_hugepage(struct page *page)
 {
 }
+#endif
 

That won't work. What if you boot a CONFIG_XEN kernel native?

Something like:

#ifdef CONFIG_XEN
int xen_prepare_hugepage(struct page *page); #else
static inline int xen_prepare_hugepage(struct page *page)

{
	return 0;
}

#endif

static inline int arch_prepare_hugepage(struct page *page)

{
	if (xen_pv_domain())
		return xen_prepare_hugepage(page);
	return 0;
}

might work better.

J

Quoted 37 lines
/arch/x86/include/asm/hugetlb.h renamed from arch/x86/include/asm/hugetlb.h +0 -0
 #endif /* _ASM_X86_HUGETLB_H */
/include/xen/interface/xen.h renamed from include/xen/interface/xen.h +17 -0
@@ -167,6 +167,19 @@
  * cmd: MMUEXT_SET_LDT
  * linear_addr: Linear address of LDT base (NB. must be page-aligned).
  * nr_ents: Number of entries in LDT.
+ *
+ * cmd: MMUEXT_CLEAR_PAGE
+ * mfn: Machine frame number to be cleared.
+ *
+ * cmd: MMUEXT_COPY_PAGE
+ * mfn: Machine frame number of the destination page.
+ * src_mfn: Machine frame number of the source page.
+ *
+ * cmd: MMUEXT_MARK_SUPER
+ * mfn: Machine frame number of head of superpage to be marked.
+ *
+ * cmd: MMUEXT_UNMARK_SUPER
+ * mfn: Machine frame number of head of superpage to be cleared.
  */
 #define MMUEXT_PIN_L1_TABLE      0
 #define MMUEXT_PIN_L2_TABLE      1
@@ -183,6 +196,10 @@
 #define MMUEXT_FLUSH_CACHE      12
 #define MMUEXT_SET_LDT          13
 #define MMUEXT_NEW_USER_BASEPTR 15
+#define MMUEXT_CLEAR_PAGE       16
+#define MMUEXT_COPY_PAGE        17
+#define MMUEXT_MARK_SUPER       19
+#define MMUEXT_UNMARK_SUPER     20
 
 #ifndef __ASSEMBLY__
 #include <linux/types.h>
#2 lore raw
Quoted 10 lines · Jeremy Fitzhardinge
Quoted 8 lines
/arch/x86/xen/mmu.c renamed from arch/x86/xen/mmu.c +5 -0
+             return 1;
+     }
+     for (i = 0, m = mfn; i < HPAGE_SIZE/PAGE_SIZE; i++, pfn++, m++) {
+             if (pfn_to_mfn(pfn) != m) {
+                     printk("Guest pages are not properly aligned to use

hugepages\n");

Isn't this a superset of the previous test?

The code is doublechecking to make sure this hugepage is actually contiguous at the machine level.

Dave McCracken
Oracle Corp.