Skip to contentSkip to conversation

Speed up superpage mapping, take 2

PATCH In review Dave McCracken 3 participants 4 messages May 25, 2010 – May 26, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Speed up superpage mapping, take 2

Second try: actually make it build for 32 bit hypervisor.

The current version of superpage mapping takes a PGT_writable reference to every page in a superpage each time it is mapped. This is extremely slow, so slow that applications become unusable.

My solution for this is to introduce a superpage table in the hypervisor, similar to the frametable structure for pages. Currently this table only has a type_info element. There are three types a superpage can have, SGT_mark, SGT_dynamic, or SGT_none.

In normal operation, the first time a superpage is mapped, a PGT_writable reference is taken to each page in the superpage, and the superpage is set to type SGT_dynamic and the superpage typecount is incremented. On subsequent mappings and unmappings, only the superpage typecount changes. On the last unmap, the PGT_writable reference on each page is removed.

The SGT_mark type is set and cleared through two new MMUEXT hypercalls, mark_super and unmark_super. When the hypercall is made, the superpage's type is set to SGT_mark and a PGT_writable reference is taken to its pages. On unmark, the type is cleared and the reference removed.

If a page is already set to SGT_dynamic when mark_super is called, the type is changed to SGT_mark and no additional PGT_writable reference is taken. If there are still outstanding mappings of this superpage when unmark_super is called, the type is set to SGT_dynamic and the PGT_writable reference is not removed.

--------

/xen/include/public/arch-x86/xen-x86_32.h renamed from xen/include/public/arch-x86/xen-x86_32.h +3 -3
@@ -64,9 +64,9 @@
 #define FLAT_USER_DS    FLAT_RING3_DS
 #define FLAT_USER_SS    FLAT_RING3_SS
 
-#define __HYPERVISOR_VIRT_START_PAE    0xF5800000
-#define __MACH2PHYS_VIRT_START_PAE     0xF5800000
-#define __MACH2PHYS_VIRT_END_PAE       0xF6800000
+#define __HYPERVISOR_VIRT_START_PAE    0xF5600000
+#define __MACH2PHYS_VIRT_START_PAE     0xF5600000
+#define __MACH2PHYS_VIRT_END_PAE       0xF6600000
 #define HYPERVISOR_VIRT_START_PAE      \
     mk_unsigned_long(__HYPERVISOR_VIRT_START_PAE)
 #define MACH2PHYS_VIRT_START_PAE       \
/xen/include/asm-x86/mm.h renamed from xen/include/asm-x86/mm.h +19 -1
@@ -214,6 +214,21 @@ struct page_info
 #define PGC_count_width   PG_shift(9)
 #define PGC_count_mask    ((1UL<<PGC_count_width)-1)
 
+struct spage_info
+{
+	unsigned long type_info;
+};
+
+ /* The following page types are MUTUALLY EXCLUSIVE. */
+#define SGT_none          PG_mask(0, 2)  /* superpage not in use */
+#define SGT_mark          PG_mask(1, 2)  /* Marked as a superpage */
+#define SGT_dynamic       PG_mask(2, 2)  /* has been dynamically mapped as a superpage */
+#define SGT_type_mask     PG_mask(3, 2)  /* Bits 30-31 or 62-63. */
+
+ /* Count of uses of this superpage as its current type. */
+#define SGT_count_width   PG_shift(3)
+#define SGT_count_mask    ((1UL<<SGT_count_width)-1)
+
 #if defined(__i386__)
 #define is_xen_heap_page(page) is_xen_heap_mfn(page_to_mfn(page))
 #define is_xen_heap_mfn(mfn) ({                         \
@@ -262,6 +277,7 @@ extern void share_xen_page_with_privileg
     struct page_info *page, int readonly);
 
 #define frame_table ((struct page_info *)FRAMETABLE_VIRT_START)
+#define spage_table ((struct spage_info *)SPAGETABLE_VIRT_START)
 extern unsigned long max_page;
 extern unsigned long total_pages;
 void init_frametable(void);
@@ -305,6 +321,8 @@ void cleanup_page_cacheattr(struct page_
 
 int is_iomem_page(unsigned long mfn);
 
+void clear_superpage_mark(struct page_info *page);
+
 struct domain *page_get_owner_and_reference(struct page_info *page);
 void put_page(struct page_info *page);
 int  get_page(struct page_info *page, struct domain *domain);
@@ -370,7 +388,7 @@ pae_copy_root(struct vcpu *v, l3_pgentry
 
 int check_descriptor(const struct domain *, struct desc_struct *d);
 
-extern int opt_allow_hugepage;
+extern int opt_allow_superpage;
 extern int mem_hotplug;
 
 /******************************************************************************
/xen/include/asm-x86/guest_pt.h renamed from xen/include/asm-x86/guest_pt.h +1 -1
@@ -187,7 +187,7 @@ guest_supports_superpages(struct vcpu *v
      * CR4.PSE is set or the guest is in PAE or long mode. 
      * It's also used in the dummy PT for vcpus with CR4.PG cleared. */
     return (!is_hvm_vcpu(v)
-            ? opt_allow_hugepage
+            ? opt_allow_superpage
             : (GUEST_PAGING_LEVELS != 2 
                || !hvm_paging_enabled(v)
                || (v->arch.hvm_vcpu.guest_cr[4] & X86_CR4_PSE)));
/xen/include/asm-x86/x86_32/page.h renamed from xen/include/asm-x86/x86_32/page.h +5 -0
@@ -6,6 +6,7 @@
 #define L2_PAGETABLE_SHIFT      21
 #define L3_PAGETABLE_SHIFT      30
 #define PAGE_SHIFT              L1_PAGETABLE_SHIFT
+#define SUPERPAGE_SHIFT         L2_PAGETABLE_SHIFT
 #define ROOT_PAGETABLE_SHIFT    L3_PAGETABLE_SHIFT
 
 #define PAGETABLE_ORDER         9
@@ -13,6 +14,7 @@
 #define L2_PAGETABLE_ENTRIES    (1<<PAGETABLE_ORDER)
 #define L3_PAGETABLE_ENTRIES    4
 #define ROOT_PAGETABLE_ENTRIES  L3_PAGETABLE_ENTRIES
+#define SUPERPAGE_ORDER         PAGETABLE_ORDER
 
 /*
  * Architecturally, physical addresses may be up to 52 bits. However, the
@@ -53,6 +55,9 @@
 #define virt_to_pdx(va)         virt_to_mfn(va)
 #define pdx_to_virt(pdx)        mfn_to_virt(pdx)
 
+#define pfn_to_sdx(pfn)         ((pfn)>>(SUPERPAGE_SHIFT-PAGE_SHIFT))
+#define sdx_to_pfn(sdx)         ((sdx)<<(SUPERPAGE_SHIFT-PAGE_SHIFT))
+
 static inline unsigned long __virt_to_maddr(unsigned long va)
 {
     ASSERT(va >= DIRECTMAP_VIRT_START && va < DIRECTMAP_VIRT_END);
/xen/include/asm-x86/config.h renamed from xen/include/asm-x86/config.h +11 -2
@@ -225,6 +225,11 @@ extern unsigned int video_mode, video_fl
 /* Slot 261: xen text, static data and bss (1GB). */
 #define XEN_VIRT_START          (HIRO_COMPAT_MPT_VIRT_END)
 #define XEN_VIRT_END            (XEN_VIRT_START + GB(1))
+/* Slot 261: superpage information array (20MB). */
+#define SPAGETABLE_VIRT_END     FRAMETABLE_VIRT_START
+#define SPAGETABLE_SIZE         ((DIRECTMAP_SIZE >> SUPERPAGE_SHIFT) * \
+                                 sizeof(struct spage_info))
+#define SPAGETABLE_VIRT_START   (SPAGETABLE_VIRT_END - SPAGETABLE_SIZE)
 /* Slot 261: page-frame information array (40GB). */
 #define FRAMETABLE_VIRT_END     DIRECTMAP_VIRT_START
 #define FRAMETABLE_SIZE         ((DIRECTMAP_SIZE >> PAGE_SHIFT) * \
@@ -289,6 +294,7 @@ extern unsigned int video_mode, video_fl
  *  Guest linear pagetable                               ( 8MB)
  *  Machine-to-physical translation table [writable]     (16MB)
  *  Frame-info table                                     (96MB)
+ *  Spage-info table                                     ( 2MB)
  *   * Start of guest inaccessible area
  *  Machine-to-physical translation table [read-only]    (16MB)
  *   * Start of guest unmodifiable area
@@ -302,6 +308,7 @@ extern unsigned int video_mode, video_fl
 #define LINEARPT_MBYTES          8
 #define MACHPHYS_MBYTES         16 /* 1 MB needed per 1 GB memory */
 #define FRAMETABLE_MBYTES       (MACHPHYS_MBYTES * 6)
+#define	SPAGETABLE_MBYTES	2
 
 #define IOREMAP_VIRT_END	0UL
 #define IOREMAP_VIRT_START	(IOREMAP_VIRT_END - (IOREMAP_MBYTES<<20))
@@ -320,7 +327,9 @@ extern unsigned int video_mode, video_fl
 #define FRAMETABLE_VIRT_END	RDWR_MPT_VIRT_START
 #define FRAMETABLE_SIZE         (FRAMETABLE_MBYTES<<20)
 #define FRAMETABLE_VIRT_START	(FRAMETABLE_VIRT_END - FRAMETABLE_SIZE)
-#define RO_MPT_VIRT_END		FRAMETABLE_VIRT_START
+#define	SPAGETABLE_VIRT_END	FRAMETABLE_VIRT_START
+#define	SPAGETABLE_VIRT_START	(SPAGETABLE_VIRT_END - (SPAGETABLE_MBYTES<<20))
+#define RO_MPT_VIRT_END		SPAGETABLE_VIRT_START
 #define RO_MPT_VIRT_START	(RO_MPT_VIRT_END - (MACHPHYS_MBYTES<<20))
 
 #define DIRECTMAP_PHYS_END	(DIRECTMAP_MBYTES<<20)
@@ -329,7 +338,7 @@ extern unsigned int video_mode, video_fl
 #define GUEST_SEGMENT_MAX_ADDR  RO_MPT_VIRT_END
 
 /* Hypervisor owns top 168MB of virtual address space. */
-#define HYPERVISOR_VIRT_START   mk_unsigned_long(0xF5800000)
+#define HYPERVISOR_VIRT_START   mk_unsigned_long(0xF5600000)
 
 #define L2_PAGETABLE_FIRST_XEN_SLOT \
     (HYPERVISOR_VIRT_START >> L2_PAGETABLE_SHIFT)
/xen/include/asm-x86/page.h renamed from xen/include/asm-x86/page.h +10 -0
@@ -240,6 +240,14 @@ void copy_page_sse2(void *, const void *
 #define __pfn_to_paddr(pfn) ((paddr_t)(pfn) << PAGE_SHIFT)
 #define __paddr_to_pfn(pa)  ((unsigned long)((pa) >> PAGE_SHIFT))
 
+/* Convert between machine frame numbers and spage-info structures. */
+#define __mfn_to_spage(mfn)  (spage_table + pfn_to_sdx(mfn))
+#define __spage_to_mfn(pg)   sdx_to_pfn((unsigned long)((pg) - spage_table))
+
+/* Convert between page-info structures and spage-info structures. */
+#define page_to_spage(page)  (spage_table+(((page)-frame_table)>>(SUPERPAGE_SHIFT-PAGE_SHIFT)))
+#define spage_to_page(spage)  (frame_table+(((spage)-spage_table)<<(SUPERPAGE_SHIFT-PAGE_SHIFT)))
+
 /*
  * We define non-underscored wrappers for above conversion functions. These are
  * overridden in various source files while underscored versions remain intact.
@@ -251,6 +259,8 @@ void copy_page_sse2(void *, const void *
 #define maddr_to_virt(ma)   __maddr_to_virt((unsigned long)(ma))
 #define mfn_to_page(mfn)    __mfn_to_page(mfn)
 #define page_to_mfn(pg)     __page_to_mfn(pg)
+#define mfn_to_spage(mfn)    __mfn_to_spage(mfn)
+#define spage_to_mfn(pg)     __spage_to_mfn(pg)
 #define maddr_to_page(ma)   __maddr_to_page(ma)
 #define page_to_maddr(pg)   __page_to_maddr(pg)
 #define virt_to_page(va)    __virt_to_page(va)
/xen/include/asm-x86/x86_64/page.h renamed from xen/include/asm-x86/x86_64/page.h +14 -0
@@ -7,6 +7,7 @@
 #define L3_PAGETABLE_SHIFT      30
 #define L4_PAGETABLE_SHIFT      39
 #define PAGE_SHIFT              L1_PAGETABLE_SHIFT
+#define SUPERPAGE_SHIFT         L2_PAGETABLE_SHIFT
 #define ROOT_PAGETABLE_SHIFT    L4_PAGETABLE_SHIFT
 
 #define PAGETABLE_ORDER         9
@@ -15,6 +16,7 @@
 #define L3_PAGETABLE_ENTRIES    (1<<PAGETABLE_ORDER)
 #define L4_PAGETABLE_ENTRIES    (1<<PAGETABLE_ORDER)
 #define ROOT_PAGETABLE_ENTRIES  L4_PAGETABLE_ENTRIES
+#define SUPERPAGE_ORDER         PAGETABLE_ORDER
 
 #define __PAGE_OFFSET           DIRECTMAP_VIRT_START
 #define __XEN_VIRT_START        XEN_VIRT_START
@@ -41,6 +43,8 @@ extern void pfn_pdx_hole_setup(unsigned
 
 #define page_to_pdx(pg)  ((pg) - frame_table)
 #define pdx_to_page(pdx) (frame_table + (pdx))
+#define spage_to_pdx(spg) ((spg>>(SUPERPAGE_SHIFT-PAGE_SHIFT)) - spage_table)
+#define pdx_to_spage(pdx) (spage_table + ((pdx)<<(SUPERPAGE_SHIFT-PAGE_SHIFT)))
 /*
  * Note: These are solely for the use by page_{get,set}_owner(), and
  *       therefore don't need to handle the XEN_VIRT_{START,END} range.
@@ -64,6 +68,16 @@ static inline unsigned long pdx_to_pfn(u
            ((pdx << pfn_pdx_hole_shift) & pfn_top_mask);
 }
 
+static inline unsigned long pfn_to_sdx(unsigned long pfn)
+{
+    return pfn_to_pdx(pfn) >> (SUPERPAGE_SHIFT-PAGE_SHIFT);
+}
+
+static inline unsigned long sdx_to_pfn(unsigned long sdx)
+{
+    return pdx_to_pfn(sdx << (SUPERPAGE_SHIFT-PAGE_SHIFT));
+}
+
 static inline unsigned long __virt_to_maddr(unsigned long va)
 {
     ASSERT(va >= XEN_VIRT_START);
/xen/arch/x86/domain.c renamed from xen/arch/x86/domain.c +3 -0
@@ -1739,6 +1739,9 @@ static int relinquish_memory(
             BUG();
         }
 
+        if (opt_allow_superpage)
+            clear_superpage_mark(page);
+
         if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
             put_page(page);
 
/xen/arch/x86/mm.c renamed from xen/arch/x86/mm.c +243 -40

343 lines not on this page · raw message

#1 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line · Dave McCracken

I didn't look in any great detail at other aspects of the patch, but this

Quoted 5 lines
/xen/include/public/arch-x86/xen-x86_32.h renamed from xen/include/public/arch-x86/xen-x86_32.h +0 -0
@@ -64,9 +64,9 @@
 #define FLAT_USER_DS    FLAT_RING3_DS
 #define FLAT_USER_SS    FLAT_RING3_SS

/xen/include/public/arch-x86/xen-x86_32.h line 72

Quoted 10 lines
/xen/include/public/arch-x86/xen-x86_32.h renamed from xen/include/public/arch-x86/xen-x86_32.h +3 -3
 
-#define __HYPERVISOR_VIRT_START_PAE    0xF5800000
-#define __MACH2PHYS_VIRT_START_PAE     0xF5800000
-#define __MACH2PHYS_VIRT_END_PAE       0xF6800000
+#define __HYPERVISOR_VIRT_START_PAE    0xF5600000
+#define __MACH2PHYS_VIRT_START_PAE     0xF5600000
+#define __MACH2PHYS_VIRT_END_PAE       0xF6600000
 #define HYPERVISOR_VIRT_START_PAE      \
     mk_unsigned_long(__HYPERVISOR_VIRT_START_PAE)
 #define MACH2PHYS_VIRT_START_PAE       \

is absolutely forbidden: You cannot lower the start address of the hypervisor address space hole - no kernel can be expected to be able to deal with that. I think you will need to make the feature 64-bit specific, unless you can find a 2Mb range within the existing hole (but I doubt there is any room left), or unless you map the table dynamically as needed for 32-bits.

Jan

#2 lore raw
Quoted 8 lines · Jan Beulich
Quoted 8 lines
/xen/include/public/arch-x86/xen-x86_32.h renamed from xen/include/public/arch-x86/xen-x86_32.h +3 -3
-#define __HYPERVISOR_VIRT_START_PAE    0xF5800000
-#define __MACH2PHYS_VIRT_START_PAE     0xF5800000
-#define __MACH2PHYS_VIRT_END_PAE       0xF6800000
+#define __HYPERVISOR_VIRT_START_PAE    0xF5600000
+#define __MACH2PHYS_VIRT_START_PAE     0xF5600000
+#define __MACH2PHYS_VIRT_END_PAE       0xF6600000
 #define HYPERVISOR_VIRT_START_PAE      \
     mk_unsigned_long(__HYPERVISOR_VIRT_START_PAE)
Quoted 8 lines
Quoted 1 line

#define MACH2PHYS_VIRT_START_PAE \

is absolutely forbidden: You cannot lower the start address of the hypervisor address space hole - no kernel can be expected to be able to deal with that. I think you will need to make the feature 64-bit specific, unless you can find a 2Mb range within the existing hole (but I doubt there is any room left), or unless you map the table dynamically as needed for 32-bits.

Sixty-four-bit-only is *fine* by me. But yes, the patch will need another rev for this.

-- Keir

#3 lore raw
Quoted 21 lines · Keir Fraser
Quoted 11 lines · Jan Beulich
Quoted 11 lines
/xen/include/public/arch-x86/xen-x86_32.h renamed from xen/include/public/arch-x86/xen-x86_32.h +3 -3
-#define __HYPERVISOR_VIRT_START_PAE    0xF5800000
-#define __MACH2PHYS_VIRT_START_PAE     0xF5800000
-#define __MACH2PHYS_VIRT_END_PAE       0xF6800000
+#define __HYPERVISOR_VIRT_START_PAE    0xF5600000
+#define __MACH2PHYS_VIRT_START_PAE     0xF5600000
+#define __MACH2PHYS_VIRT_END_PAE       0xF6600000
 
 #define HYPERVISOR_VIRT_START_PAE      \
 
     mk_unsigned_long(__HYPERVISOR_VIRT_START_PAE)
Quoted 8 lines
Quoted 1 line

#define MACH2PHYS_VIRT_START_PAE \

is absolutely forbidden: You cannot lower the start address of the hypervisor address space hole - no kernel can be expected to be able to deal with that. I think you will need to make the feature 64-bit specific, unless you can find a 2Mb range within the existing hole (but I doubt there is any room left), or unless you map the table dynamically as needed for 32-bits.

Quoted 2 lines

Sixty-four-bit-only is *fine* by me. But yes, the patch will need another rev for this.

Sorry, I should have picked up on this. 64 bit only is fine by me too. I'll respin the patch.

Dave McCracken