Speed up superpage mapping, take 3
PATCH 1/1 Speed up superpage mapping, take 3
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.
Fast superpage mapping is only supported on 64 bit hypervisors. For 32 bit hyperviors, superpage mapping is supported but will be extremely slow.
--------
/xen/include/asm-x86/mm.h renamed from xen/include/asm-x86/mm.h +23 -1
@@ -214,6 +214,23 @@ struct page_info#define PGC_count_width PG_shift(9)#define PGC_count_mask ((1UL<<PGC_count_width)-1) +#ifdef __x86_64__ +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) +#endif +#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 +279,9 @@ extern void share_xen_page_with_privileg struct page_info *page, int readonly);#define frame_table ((struct page_info *)FRAMETABLE_VIRT_START) +#ifdef __x86_64__ +#define spage_table ((struct spage_info *)SPAGETABLE_VIRT_START) +#endif extern unsigned long max_page;extern unsigned long total_pages;void init_frametable (void );
@@ -305,6 +325,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 +392,7 @@ pae_copy_root(struct vcpu *v, l3_pgentryint 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 +5 -0
@@ -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) * \
/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 +5 -0
@@ -1739,6 +1739,11 @@ static int relinquish_memory(BUG (); } +#ifdef __x86_64__ +if (opt_allow_superpage) +clear_superpage_mark (page); +#endif +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 +261 -40
361 lines not on this page · raw message