x86: improve handling of sparse physical memory maps
cover x86: improve handling of sparse physical memory maps
Significant holes in the physical memory map weren't handled gracefully so far in Xen: The overhead of setting up a fully linear 1:1 mapping as well as a fully linear frame table could so high that Xen wouldn't boot on extreme systems.
The following adjustments are being made in each patch of this series:
1) For x86-64, the virtual range reserved for the 1:1 mapping gets extended to cover 5Tb, and the virtual size of the frame table gets changed to match whatever the 1:1 table can cover.
2) The largest range of contiguous bits which are zero for all valid MFNs gets removed from the MFN representation used to index into the 1:1 mapping and frame table, thereby cutting the virtual range these tables must cover approximately by half with each bit removed.
3) The reduced in size frame table gets mapped sparsely, i.e. holes of MFN ranges spanning a sufficiently large range will no longer have struct page_info instances allocated for them.
4) The non-reducible (in virtual size) M2P table gets allocated sparsely in the same manner (just requiring larger holes for a table chunk to be omitted).
This ends the portion of the patch series that deal with overhead reduction. On a partner system with suitable memory layout, this patch series has been shown to reduce Xen memory overhead from 14.2% of total memory to 1.8%, thus getting reasonably close to the theoretically minimal overhead of 1.3% (counting just frame table, 1:1 mapping, and M->P tables).
Since the more complex calculations resulted in not insignificant code bloat, the following non-essential patches represent possible attempts to reduce the code size overhead, and should be considered RFC:
5) Introduce a variant of map_domain_page() directly getting passed a struct page_info * argument, based on the observation that in many places the argument to this function so far simply was the result of page_to_mfn(). This is meaningful for the x86-64 case where map_domain_page() really just is an invocation of mfn_to_virt(), and hence the combined mfn_to_virt(page_to_mfn()) now represents a needless roundtrip conversion compressed -> uncompressed -> compressed of the MFN representation.
6) The grant table code had some particularly frequent repetitions of mfn_to_page() on each time the same input arguments. To help the compiler (which can do only a limited job on CSE), this adds explicit caching of the transformation result in a few places.
7) This one is likely the most questionable part: Realizing that the compiler can do a much better job in terms of CSE when the variables needed are constant, this introduces and extension to the read-mostly variables, allowing variables to be marked as being written just during boot initialization.
In the event of transmission problems with the patches (our IS&T folks, while now being aware of the patch corruption issues seen in the past, refuse to address the root cause of the problem for the time being), the full set of patches is attached here.
Extend the virtual range reserved for the 1:1 mapping to cover 5Tb, and make the virtual size of the frame table gets match whatever the 1:1 table can cover.
xen/arch/x86/boot/head.S +4 -4
@@ -33,7 +33,7 @@ ENTRY(start) /* Checksum: must be the negated sum of the first two fields. */ .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) - .section .init.text + .section .init.text, "ax" .Lbad_cpu_msg: .asciz "ERR: Not a 64-bit CPU!" .Lbad_ldr_msg: .asciz "ERR: Not a Multiboot bootloader!"
@@ -128,13 +128,13 @@ __start: loop 1b /* Initialise L3 xen-map page directory entry. */ mov $(sym_phys(l2_xenmap)+7),%eax - mov %eax,sym_phys(l3_xenmap) + (50*8) + mov %eax,sym_phys(l3_xenmap) + l3_table_offset(XEN_VIRT_START)*8 /* Hook identity-map and xen-map L3 tables into PML4. */ mov $(sym_phys(l3_identmap)+7),%eax mov %eax,sym_phys(idle_pg_table) + ( 0*8) /* PML4[ 0]: 1:1 map */ - mov %eax,sym_phys(idle_pg_table) + (262*8) /* PML4[262]: 1:1 map */ + mov %eax,sym_phys(idle_pg_table) + l4_table_offset(DIRECTMAP_VIRT_START)*8 mov $(sym_phys(l3_xenmap)+7),%eax - mov %eax,sym_phys(idle_pg_table) + (261*8) /* PML4[261]: xen map */ + mov %eax,sym_phys(idle_pg_table) + l4_table_offset(XEN_VIRT_START)*8 #else /* Initialize low and high mappings of memory with 2MB pages */ mov $sym_phys(idle_pg_table_l2),%edi
xen/arch/x86/e820.c +5 -1
@@ -500,15 +500,19 @@ static void __init machine_specific_memo "can be accessed by Xen in 32-bit mode.");#else { -unsigned long limit, mpt_limit, pft_limit; +unsigned long limit, mpt_limit, ro_mpt_limit, pft_limit; limit =DIRECTMAP_VIRT_END -DIRECTMAP_VIRT_START ; mpt_limit = ((RDWR_MPT_VIRT_END -RDWR_MPT_VIRT_START ) /sizeof (unsigned long )) <<PAGE_SHIFT ; + ro_mpt_limit = ((RO_MPT_VIRT_END -RO_MPT_VIRT_START ) + /sizeof (unsigned long )) <<PAGE_SHIFT ; pft_limit = ((FRAMETABLE_VIRT_END -FRAMETABLE_VIRT_START ) /sizeof (struct page_info )) <<PAGE_SHIFT ;if ( limit > mpt_limit ) limit = mpt_limit; +if ( limit > ro_mpt_limit ) + limit = ro_mpt_limit;if ( limit > pft_limit ) limit = pft_limit; clip_to_limit(limit,
xen/arch/x86/mm.c +7 -0
@@ -166,6 +166,13 @@ void __init init_frametable(void) {unsigned long nr_pages, page_step, i, mfn; +#ifdef __x86_64__ +BUILD_BUG_ON (FRAMETABLE_VIRT_START & ((1UL <<L3_PAGETABLE_SHIFT ) -1 )); +BUILD_BUG_ON (XEN_VIRT_END >FRAMETABLE_VIRT_END ); +#else +BUILD_BUG_ON (FRAMETABLE_VIRT_START & ((1UL <<L2_PAGETABLE_SHIFT ) -1 )); +#endif + nr_pages =PFN_UP (max_page *sizeof (*frame_table)); page_step =1 << (cpu_has_page1gb ?L3_PAGETABLE_SHIFT -PAGE_SHIFT :L2_PAGETABLE_SHIFT -PAGE_SHIFT );
xen/include/asm-x86/config.h +16 -15
@@ -148,21 +148,19 @@ extern unsigned int video_mode, video_fl* 0xffff828000000000 - 0xffff8283ffffffff [16GB, 2^34 bytes, PML4:261] * Machine-to-phys translation table. * 0xffff828400000000 - 0xffff8287ffffffff [16GB, 2^34 bytes, PML4:261] -* Page-frame information array. -* 0xffff828800000000 - 0xffff828bffffffff [16GB, 2^34 bytes, PML4:261] * ioremap()/fixmap area. -* 0xffff828c00000000 - 0xffff828c3fffffff [1GB, 2^30 bytes, PML4:261] +* 0xffff828800000000 - 0xffff82883fffffff [1GB, 2^30 bytes, PML4:261] * Compatibility machine-to-phys translation table. -* 0xffff828c40000000 - 0xffff828c7fffffff [1GB, 2^30 bytes, PML4:261] +* 0xffff828840000000 - 0xffff82887fffffff [1GB, 2^30 bytes, PML4:261] * High read-only compatibility machine-to-phys translation table. -* 0xffff828c80000000 - 0xffff828cbfffffff [1GB, 2^30 bytes, PML4:261] +* 0xffff828880000000 - 0xffff8288bfffffff [1GB, 2^30 bytes, PML4:261] * Xen text, static data, bss. -* 0xffff828cc0000000 - 0xffff82ffffffffff [461GB, PML4:261] +* 0xffff8288c0000000 - 0xffff82f5ffffffff [437GB, PML4:261] * Reserved for future use. -* 0xffff830000000000 - 0xffff83ffffffffff [1TB, 2^40 bytes, PML4:262-263] +* 0xffff82f600000000 - 0xffff82ffffffffff [40GB, 2^38 bytes, PML4:261] +* Page-frame information array. +* 0xffff830000000000 - 0xffff87ffffffffff [5TB, 5*2^40 bytes, PML4:262-271] * 1:1 direct mapping of all physical memory. -* 0xffff840000000000 - 0xffff87ffffffffff [4TB, 2^42 bytes, PML4:264-271] -* Reserved for future use. * 0xffff880000000000 - 0xffffffffffffffff [120TB, PML4:272-511] * Guest-defined use. *
@@ -210,11 +208,8 @@ extern unsigned int video_mode, video_fl/* Slot 261: machine-to-phys conversion table (16GB). */ #define RDWR_MPT_VIRT_START (PML4_ADDR(261))#define RDWR_MPT_VIRT_END (RDWR_MPT_VIRT_START + GB(16)) -/* Slot 261: page-frame information array (16GB). */ -#define FRAMETABLE_VIRT_START (RDWR_MPT_VIRT_END) -#define FRAMETABLE_VIRT_END (FRAMETABLE_VIRT_START + GB(16))/* Slot 261: ioremap()/fixmap area (16GB). */ -#define IOREMAP_VIRT_START (FRAMETABLE_VIRT_END) +#define IOREMAP_VIRT_START RDWR_MPT_VIRT_END#define IOREMAP_VIRT_END (IOREMAP_VIRT_START + GB(16))/* Slot 261: compatibility machine-to-phys conversion table (1GB). */ #define RDWR_COMPAT_MPT_VIRT_START IOREMAP_VIRT_END
@@ -225,9 +220,15 @@ 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 262-263: A direct 1:1 mapping of all of physical memory. */ +/* Slot 261: page-frame information array (40GB). */ +#define FRAMETABLE_VIRT_END DIRECTMAP_VIRT_START +#define FRAMETABLE_SIZE ((DIRECTMAP_SIZE >> PAGE_SHIFT) * \ + sizeof(struct page_info)) +#define FRAMETABLE_VIRT_START (FRAMETABLE_VIRT_END - FRAMETABLE_SIZE) +/* Slot 262-271: A direct 1:1 mapping of all of physical memory. */ #define DIRECTMAP_VIRT_START (PML4_ADDR(262)) -#define DIRECTMAP_VIRT_END (DIRECTMAP_VIRT_START + PML4_ENTRY_BYTES*2) +#define DIRECTMAP_SIZE (PML4_ENTRY_BYTES*10) +#define DIRECTMAP_VIRT_END (DIRECTMAP_VIRT_START + DIRECTMAP_SIZE)#ifndef __ASSEMBLY__
xen/include/asm-x86/x86_64/page.h +2 -2
@@ -16,8 +16,8 @@#define L4_PAGETABLE_ENTRIES (1<<PAGETABLE_ORDER)#define ROOT_PAGETABLE_ENTRIES L4_PAGETABLE_ENTRIES -#define __PAGE_OFFSET (0xFFFF830000000000) -#define __XEN_VIRT_START (0xFFFF828C80000000) +#define __PAGE_OFFSET DIRECTMAP_VIRT_START +#define __XEN_VIRT_START XEN_VIRT_START/* These are architectural limits. Current CPUs support only 40-bit phys. */ #define PADDR_BITS 52
Introduces a virtual space conserving transformation on the MFN thus far used to index 1:1 mapping and frame table, removing the largest range of contiguous bits (below the most significant one) which are zero for all valid MFNs from the MFN representation, to be used to index into those arrays, thereby cutting the virtual range these tables must cover approximately by half with each bit removed.
Since this should account for hotpluggable memory (in order to not requiring a re-write when that gets supported), the determination of which bits are candidates for removal must not be based on the E820 information, but instead has to use the SRAT. That in turn requires a change to the ordering of steps done during early boot.
As a consequence of the now possibly much larger range of memory coverable by raw MFNs, all places where MFNs get stored in 32-bit structure members (struct page_info has several of these), the compressed representation must be used there. The shadow code part of the changes would certainly deserve some close looking at by one of those who originally wrote it.
Likewise, the much larger coverable address range requires the writable M2P table to be sized more generously, in order for it to not become a limiting factor. Read-only (user-viosible) and writable copies of this table are now the same size.
xen/arch/x86/e820.c +4 -11
@@ -500,22 +500,15 @@ static void __init machine_specific_memo "can be accessed by Xen in 32-bit mode.");#else { -unsigned long limit, mpt_limit, ro_mpt_limit, pft_limit; +unsigned long mpt_limit, ro_mpt_limit; - limit =DIRECTMAP_VIRT_END -DIRECTMAP_VIRT_START ; mpt_limit = ((RDWR_MPT_VIRT_END -RDWR_MPT_VIRT_START ) /sizeof (unsigned long )) <<PAGE_SHIFT ; ro_mpt_limit = ((RO_MPT_VIRT_END -RO_MPT_VIRT_START ) /sizeof (unsigned long )) <<PAGE_SHIFT ; - pft_limit = ((FRAMETABLE_VIRT_END -FRAMETABLE_VIRT_START ) - /sizeof (struct page_info )) <<PAGE_SHIFT ; -if ( limit > mpt_limit ) - limit = mpt_limit; -if ( limit > ro_mpt_limit ) - limit = ro_mpt_limit; -if ( limit > pft_limit ) - limit = pft_limit; -clip_to_limit (limit, +if ( mpt_limit > ro_mpt_limit ) + mpt_limit = ro_mpt_limit; +clip_to_limit (mpt_limit,"Only the first %lu GB of the physical " "memory map can be accessed by Xen." ); }
xen/arch/x86/mm.c +6 -4
@@ -173,7 +173,7 @@ void __init init_frametable(void)BUILD_BUG_ON (FRAMETABLE_VIRT_START & ((1UL <<L2_PAGETABLE_SHIFT ) -1 ));#endif - nr_pages =PFN_UP (max_page *sizeof (*frame_table)); + nr_pages =PFN_UP (max_pdx *sizeof (*frame_table)); page_step =1 << (cpu_has_page1gb ?L3_PAGETABLE_SHIFT -PAGE_SHIFT :L2_PAGETABLE_SHIFT -PAGE_SHIFT );
@@ -248,10 +248,11 @@ void __init arch_init_memory(void)* the statically-initialised 1-16MB mapping area. */ iostart_pfn = max_t(unsigned long, pfn, 1UL << (20 - PAGE_SHIFT)); - ioend_pfn = rstart_pfn;#if defined(CONFIG_X86_32 ) - ioend_pfn = min_t(unsigned long, ioend_pfn, + ioend_pfn = min_t(unsigned long, rstart_pfn,DIRECTMAP_MBYTES << (20 -PAGE_SHIFT )); +#else + ioend_pfn =min (rstart_pfn,16UL << (20 -PAGE_SHIFT ));#endif if ( iostart_pfn < ioend_pfn ) destroy_xen_mappings((unsigned long)mfn_to_virt(iostart_pfn),
@@ -260,7 +261,8 @@ void __init arch_init_memory(void)/* Mark as I/O up to next RAM region. */ for ( ; pfn < rstart_pfn; pfn++ ) { -BUG_ON (!mfn_valid (pfn)); +if ( !mfn_valid (pfn) ) +continue ;share_xen_page_with_guest (mfn_to_page (pfn), dom_io, XENSHARE_writable); }
xen/arch/x86/mm/shadow/common.c +22 -11
@@ -1340,7 +1340,7 @@ static inline void trace_shadow_prealloc/* Convert smfn to gfn */ unsigned long gfn;ASSERT (mfn_valid (smfn)); - gfn =mfn_to_gfn (d,_mfn (mfn_to_page (smfn)->v.sh.back)); + gfn =mfn_to_gfn (d,backpointer (mfn_to_page (smfn)));__trace_var (TRC_SHADOW_PREALLOC_UNPIN ,0 /*!tsc*/ ,sizeof (gfn), (unsigned char *)&gfn); }
@@ -1502,13 +1502,13 @@ __initcall(shadow_blow_tables_keyhandlerstatic inline struct page_info *next_shadow (const struct page_info *sp) { -return sp->next_shadow ?mfn_to_page (_mfn (sp->next_shadow)) :NULL ; +return sp->next_shadow ?pdx_to_page (sp->next_shadow) :NULL ; }static inline void set_next_shadow (struct page_info *sp,struct page_info *next) { - sp->next_shadow = next ?mfn_x (page_to_mfn (next)) :0 ; + sp->next_shadow = next ?page_to_pdx (next) :0 ; }/* Allocate another shadow's worth of (contiguous, aligned) pages,
@@ -1554,6 +1554,17 @@ mfn_t shadow_alloc(struct domain *d, } d->arch.paging.shadow.free_pages -=1 << order; +switch (shadow_type) + { +case SH_type_fl1_32_shadow: +case SH_type_fl1_pae_shadow: +case SH_type_fl1_64_shadow: +break ; +default : + backpointer =pfn_to_pdx (backpointer); +break ; + } +/* Init page info fields and clear the pages */ for ( i =0 ; i <1 <<order ; i++ ) {
@@ -1911,7 +1922,7 @@ static void sh_hash_audit_bucket(structBUG_ON ( sp->u.sh.type ==0 );BUG_ON ( sp->u.sh.type > SH_type_max_shadow );/* Wrong bucket? */ -BUG_ON (sh_hash (sp->v.sh.back, sp->u.sh.type) != bucket ); +BUG_ON (sh_hash (__backpointer (sp), sp->u.sh.type) != bucket );/* Duplicate entry? */ for ( x =next_shadow (sp); x; x =next_shadow (x) ) BUG_ON( x->v.sh.back == sp->v.sh.back &&
@@ -1921,7 +1932,7 @@ static void sh_hash_audit_bucket(struct && sp->u.sh.type != SH_type_fl1_pae_shadow && sp->u.sh.type != SH_type_fl1_64_shadow ) { -struct page_info *gpg =mfn_to_page (_mfn (sp->v.sh.back)); +struct page_info *gpg =mfn_to_page (backpointer (sp));/* Bad shadow flags on guest page? */ BUG_ON ( !(gpg->shadow_flags & (1 <<sp->u.sh.type)) );/* Bad type count on guest page? */
@@ -1935,9 +1946,9 @@ static void sh_hash_audit_bucket(struct {if ( !page_is_out_of_sync (gpg) ) { -SHADOW_ERROR ("MFN %#" PRpgmfn" shadowed (by %#" PRI_mfn")" +SHADOW_ERROR ("MFN %#" PRI_mfn" shadowed (by %#" PRI_mfn")" " and not OOS but has typecount %#lx\n" , - sp->v.sh.back, +__backpointer (sp),mfn_x (page_to_mfn (sp)), gpg->u.inuse.type_info);BUG ();
@@ -1949,9 +1960,9 @@ static void sh_hash_audit_bucket(structif ( (gpg->u.inuse.type_info & PGT_type_mask) == PGT_writable_page && (gpg->u.inuse.type_info & PGT_count_mask) !=0 ) { -SHADOW_ERROR ("MFN %#" PRpgmfn" shadowed (by %#" PRI_mfn")" +SHADOW_ERROR ("MFN %#" PRI_mfn" shadowed (by %#" PRI_mfn")" " but has typecount %#lx\n" , - sp->v.sh.back,mfn_x (page_to_mfn (sp)), +__backpointer (sp),mfn_x (page_to_mfn (sp)), gpg->u.inuse.type_info);BUG (); }
@@ -2037,7 +2048,7 @@ mfn_t shadow_hash_lookup(struct vcpu *v, prev =NULL ;while (sp) { -if ( sp->v.sh.back == n && sp->u.sh.type == t ) +if (__backpointer (sp) == n && sp->u.sh.type == t ) {/* Pull-to-front if 'sp' isn't already the head item */ if (unlikely (sp != d->arch.paging.shadow.hash_table[key]) )
@@ -2204,7 +2215,7 @@ void sh_destroy_shadow(struct vcpu *v, m t == SH_type_fl1_64_shadow || t == SH_type_monitor_table || (is_pv_32on64_vcpu (v) && t == SH_type_l4_64_shadow) || - (page_get_owner (mfn_to_page (_mfn (sp->v.sh.back))) + (page_get_owner (mfn_to_page (backpointer (sp))) == v->domain));/* The down-shifts here are so that the switch statement is on nice
xen/arch/x86/mm/shadow/multi.c +14 -14
@@ -1010,7 +1010,7 @@ static int shadow_set_l2e(struct vcpu *v#if (SHADOW_OPTIMIZATIONS &SHOPT_OUT_OF_SYNC ) {struct page_info *sp =mfn_to_page (sl1mfn); -mfn_t gl1mfn =_mfn (sp->v.sh.back); +mfn_t gl1mfn =backpointer (sp);/* If the shadow is a fl1 then the backpointer contains the GFN instead of the GMFN, and it's definitely not
@@ -1974,7 +1974,7 @@ void sh_destroy_l4_shadow(struct vcpu *vASSERT (t == SH_type_l4_shadow);/* Record that the guest page isn't shadowed any more (in this type) */ - gmfn =_mfn (mfn_to_page (smfn)->v.sh.back); + gmfn =backpointer (mfn_to_page (smfn));delete_shadow_status (v, gmfn, t, smfn);shadow_demote (v, gmfn, t);/* Decrement refcounts of all the old entries */
@@ -2003,7 +2003,7 @@ void sh_destroy_l3_shadow(struct vcpu *vASSERT (t == SH_type_l3_shadow);/* Record that the guest page isn't shadowed any more (in this type) */ - gmfn =_mfn (mfn_to_page (smfn)->v.sh.back); + gmfn =backpointer (mfn_to_page (smfn));delete_shadow_status (v, gmfn, t, smfn);shadow_demote (v, gmfn, t);
@@ -2038,7 +2038,7 @@ void sh_destroy_l2_shadow(struct vcpu *v#endif /* Record that the guest page isn't shadowed any more (in this type) */ - gmfn =_mfn (mfn_to_page (smfn)->v.sh.back); + gmfn =backpointer (mfn_to_page (smfn));delete_shadow_status (v, gmfn, t, smfn);shadow_demote (v, gmfn, t);
@@ -2073,7 +2073,7 @@ void sh_destroy_l1_shadow(struct vcpu *v }else { -mfn_t gmfn =_mfn (mfn_to_page (smfn)->v.sh.back); +mfn_t gmfn =backpointer (mfn_to_page (smfn));delete_shadow_status (v, gmfn, t, smfn);shadow_demote (v, gmfn, t); }
@@ -2397,7 +2397,7 @@ static int validate_gl1e(struct vcpu *v, result |=shadow_set_l1e (v, sl1p, new_sl1e, p2mt, sl1mfn);#if (SHADOW_OPTIMIZATIONS &SHOPT_OUT_OF_SYNC ) - gl1mfn =_mfn (mfn_to_page (sl1mfn)->v.sh.back); + gl1mfn =backpointer (mfn_to_page (sl1mfn));if (mfn_valid (gl1mfn) &&mfn_is_out_of_sync (gl1mfn) ) {
@@ -3006,8 +3006,8 @@ static int sh_page_fault(struct vcpu *v, + shadow_l2_linear_offset(va)), sizeof(sl2e)) != 0) || !(shadow_l2e_get_flags(sl2e) & _PAGE_PRESENT) - || !mfn_valid(gl1mfn = _mfn(mfn_to_page( - shadow_l2e_get_mfn(sl2e))->v.sh.back)) + || !mfn_valid(gl1mfn = backpointer(mfn_to_page( + shadow_l2e_get_mfn(sl2e)))) || unlikely(mfn_is_out_of_sync(gl1mfn)) ) {/* Hit the slow path as if there had been no
@@ -3582,7 +3582,7 @@ sh_invlpg(struct vcpu *v, unsigned long#if (SHADOW_OPTIMIZATIONS &SHOPT_OUT_OF_SYNC )/* Check to see if the SL1 is out of sync. */ { -mfn_t gl1mfn =_mfn (mfn_to_page (sl1mfn)->v.sh.back); +mfn_t gl1mfn =backpointer (mfn_to_page (sl1mfn));struct page_info *pg =mfn_to_page (gl1mfn);if (mfn_valid (gl1mfn) &&page_is_out_of_sync (pg) )
@@ -3612,7 +3612,7 @@ sh_invlpg(struct vcpu *v, unsigned long } sl1mfn =shadow_l2e_get_mfn (sl2e); - gl1mfn =_mfn (mfn_to_page (sl1mfn)->v.sh.back); + gl1mfn =backpointer (mfn_to_page (sl1mfn)); pg =mfn_to_page (gl1mfn); if ( likely(sh_mfn_is_a_page_table(gl1mfn)
@@ -4949,7 +4949,7 @@ int sh_audit_l1_table(struct vcpu *v, mfint done =0 ;/* Follow the backpointer */ - gl1mfn =_mfn (mfn_to_page (sl1mfn)->v.sh.back); + gl1mfn =backpointer (mfn_to_page (sl1mfn));#if (SHADOW_OPTIMIZATIONS &SHOPT_OUT_OF_SYNC )/* Out-of-sync l1 shadows can contain anything: just check the OOS hash */
@@ -5042,7 +5042,7 @@ int sh_audit_l2_table(struct vcpu *v, mfint done =0 ;/* Follow the backpointer */ - gl2mfn =_mfn (mfn_to_page (sl2mfn)->v.sh.back); + gl2mfn =backpointer (mfn_to_page (sl2mfn));#if (SHADOW_OPTIMIZATIONS &SHOPT_OUT_OF_SYNC )/* Only L1's may be out of sync. */
@@ -5091,7 +5091,7 @@ int sh_audit_l3_table(struct vcpu *v, mfint done =0 ;/* Follow the backpointer */ - gl3mfn =_mfn (mfn_to_page (sl3mfn)->v.sh.back); + gl3mfn =backpointer (mfn_to_page (sl3mfn));#if (SHADOW_OPTIMIZATIONS &SHOPT_OUT_OF_SYNC )/* Only L1's may be out of sync. */
@@ -5138,7 +5138,7 @@ int sh_audit_l4_table(struct vcpu *v, mfint done =0 ;/* Follow the backpointer */ - gl4mfn =_mfn (mfn_to_page (sl4mfn)->v.sh.back); + gl4mfn =backpointer (mfn_to_page (sl4mfn));#if (SHADOW_OPTIMIZATIONS &SHOPT_OUT_OF_SYNC )/* Only L1's may be out of sync. */
xen/arch/x86/mm/shadow/private.h +15 -2
@@ -488,6 +488,19 @@ mfn_t oos_snapshot_lookup(struct vcpu *v#undef pagetable_from_page#define pagetable_from_page (pg) pagetable_from_mfn(page_to_mfn(pg)) +#define backpointer (sp) _mfn(pdx_to_pfn((unsigned long)(sp)->v.sh.back)) +static inline unsigned long __backpointer (const struct page_info *sp) +{ +switch (sp->u.sh.type) + { +case SH_type_fl1_32_shadow: +case SH_type_fl1_pae_shadow: +case SH_type_fl1_64_shadow: +return sp->v.sh.back; + } +return pdx_to_pfn (sp->v.sh.back); +} +static inline int sh_mfn_is_a_page_table (mfn_t gmfn) {
@@ -610,8 +623,8 @@ static inline int sh_get_ref(struct vcpuif (unlikely (nx >=1U <<26 ) ) { -SHADOW_PRINTK ("shadow ref overflow, gmfn=%" PRpgmfn" smfn=%lx\n" , - sp->v.sh.back,mfn_x (smfn)); +SHADOW_PRINTK ("shadow ref overflow, gmfn=%lx smfn=%lx\n" , +__backpointer (sp),mfn_x (smfn));return 0 ; }
xen/arch/x86/setup.c +61 -6
111 lines not on this page · raw message
xen/arch/x86/srat.c +64 -0
70 lines not on this page · raw message
xen/arch/x86/tboot.c +7 -1
@@ -175,6 +175,9 @@ static void update_pagetable_mac(vmac_ctfor ( mfn =0 ; mfn < max_page; mfn++ ) {struct page_info *page =mfn_to_page (mfn); + +if ( !mfn_valid (mfn) ) +continue ;if (is_page_in_use (page) && !is_xen_heap_page (page) ) {if ( page->count_info & PGC_page_table ) {void *pg =map_domain_page (mfn);
@@ -237,6 +240,9 @@ static void tboot_gen_xenheap_integrity(for ( mfn =0 ; mfn < max_page; mfn++ ) {struct page_info *page =__mfn_to_page (mfn); + +if ( !mfn_valid (mfn) ) +continue ;if (is_page_in_use (page) &&is_xen_heap_page (page) ) {void *pg =mfn_to_virt (mfn);vmac_update ((uint8_t *)pg,PAGE_SIZE , &ctx);
@@ -258,7 +264,7 @@ static void tboot_gen_frametable_integrivmac_set_key ((uint8_t *)key, &ctx); *mac =vmac ((uint8_t *)frame_table, -PFN_UP (max_page *sizeof (*frame_table)), nonce,NULL , &ctx); +PFN_UP (max_pdx *sizeof (*frame_table)), nonce,NULL , &ctx);printk ("MAC for frametable is: 0x%08" PRIx64"\n" , *mac);
xen/arch/x86/x86_64/mm.c +39 -0
@@ -33,6 +33,15 @@#include <asm/msr.h> #include <public/memory.h> +/* Parameters for PFN/MADDR compression. */ +unsigned long __read_mostly max_pdx; +unsigned long __read_mostly pfn_pdx_bottom_mask = ~0UL ; +unsigned long __read_mostly ma_va_bottom_mask = ~0UL ; +unsigned long __read_mostly pfn_top_mask =0 ; +unsigned long __read_mostly ma_top_mask =0 ; +unsigned long __read_mostly pfn_hole_mask =0 ; +unsigned int __read_mostly pfn_pdx_hole_shift =0 ; +#ifdef CONFIG_COMPAT unsigned int m2p_compat_vstart = __HYPERVISOR_COMPAT_VIRT_START;#endif
@@ -144,6 +153,36 @@ void *do_page_walk(struct vcpu *v, unsigreturn mfn_to_virt (mfn) + (addr & ~PAGE_MASK ); } +void __init pfn_pdx_hole_setup (unsigned long mask) +{ +unsigned int i, j, bottom_shift, hole_shift; + +for ( hole_shift = bottom_shift = j =0 ; ; ) + { + i =find_next_zero_bit (&mask,BITS_PER_LONG , j); + j =find_next_bit (&mask,BITS_PER_LONG , i); +if ( j >=BITS_PER_LONG ) +break ; +if ( j - i > hole_shift ) + { + hole_shift = j - i; + bottom_shift = i; + } + } +if ( !hole_shift ) +return ; + +printk (KERN_INFO "PFN compression on bits %u...%u\n" , + bottom_shift, bottom_shift + hole_shift -1 ); + + pfn_pdx_hole_shift = hole_shift; + pfn_pdx_bottom_mask = (1UL << bottom_shift) -1 ; + ma_va_bottom_mask = (PAGE_SIZE << bottom_shift) -1 ; + pfn_hole_mask = ((1UL << hole_shift) -1 ) << bottom_shift; + pfn_top_mask = ~(pfn_pdx_bottom_mask | pfn_hole_mask); + ma_top_mask = pfn_top_mask <<PAGE_SHIFT ; +} +void __init paging_init (void ) {unsigned long i, mpt_size, va;
xen/drivers/acpi/numa.c +1 -1
@@ -141,7 +141,7 @@ acpi_parse_memory_affinity(struct acpi_sreturn 0 ; } -static int __init acpi_parse_srat (struct acpi_table_header *table) +int __init acpi_parse_srat (struct acpi_table_header *table) {if (!table)return -EINVAL ;
xen/include/asm-x86/config.h +10 -9
@@ -145,17 +145,17 @@ extern unsigned int video_mode, video_fl* Shadow linear page table. * 0xffff820000000000 - 0xffff827fffffffff [512GB, 2^39 bytes, PML4:260] * Per-domain mappings (e.g., GDT, LDT). -* 0xffff828000000000 - 0xffff8283ffffffff [16GB, 2^34 bytes, PML4:261] +* 0xffff828000000000 - 0xffff82bfffffffff [256GB, 2^38 bytes, PML4:261] * Machine-to-phys translation table. -* 0xffff828400000000 - 0xffff8287ffffffff [16GB, 2^34 bytes, PML4:261] +* 0xffff82c000000000 - 0xffff82c3ffffffff [16GB, 2^34 bytes, PML4:261] * ioremap()/fixmap area. -* 0xffff828800000000 - 0xffff82883fffffff [1GB, 2^30 bytes, PML4:261] +* 0xffff82c400000000 - 0xffff82c43fffffff [1GB, 2^30 bytes, PML4:261] * Compatibility machine-to-phys translation table. -* 0xffff828840000000 - 0xffff82887fffffff [1GB, 2^30 bytes, PML4:261] +* 0xffff82c440000000 - 0xffff82c47fffffff [1GB, 2^30 bytes, PML4:261] * High read-only compatibility machine-to-phys translation table. -* 0xffff828880000000 - 0xffff8288bfffffff [1GB, 2^30 bytes, PML4:261] +* 0xffff82c480000000 - 0xffff82c4bfffffff [1GB, 2^30 bytes, PML4:261] * Xen text, static data, bss. -* 0xffff8288c0000000 - 0xffff82f5ffffffff [437GB, PML4:261] +* 0xffff82c4c0000000 - 0xffff82f5ffffffff [197GB, PML4:261] * Reserved for future use. * 0xffff82f600000000 - 0xffff82ffffffffff [40GB, 2^38 bytes, PML4:261] * Page-frame information array.
@@ -188,7 +188,8 @@ extern unsigned int video_mode, video_fl#define HYPERVISOR_VIRT_END (HYPERVISOR_VIRT_START + PML4_ENTRY_BYTES*16)/* Slot 256: read-only guest-accessible machine-to-phys translation table. */ #define RO_MPT_VIRT_START (PML4_ADDR(256)) -#define RO_MPT_VIRT_END (RO_MPT_VIRT_START + PML4_ENTRY_BYTES/2) +#define MPT_VIRT_SIZE (PML4_ENTRY_BYTES / 2) +#define RO_MPT_VIRT_END (RO_MPT_VIRT_START + MPT_VIRT_SIZE)/* Slot 257: ioremap for PCI mmconfig space for 2048 segments (512GB) * - full 16-bit segment support needs 44 bits * - since PML4 slot has 39 bits, we limit segments to 2048 (11-bits)
@@ -205,9 +206,9 @@ extern unsigned int video_mode, video_fl#define PERDOMAIN_VIRT_START (PML4_ADDR(260))#define PERDOMAIN_VIRT_END (PERDOMAIN_VIRT_START + (PERDOMAIN_MBYTES<<20))#define PERDOMAIN_MBYTES (PML4_ENTRY_BYTES >> (20 + PAGETABLE_ORDER)) -/* Slot 261: machine-to-phys conversion table (16GB). */ +/* Slot 261: machine-to-phys conversion table (256GB). */ #define RDWR_MPT_VIRT_START (PML4_ADDR(261)) -#define RDWR_MPT_VIRT_END (RDWR_MPT_VIRT_START + GB(16)) +#define RDWR_MPT_VIRT_END (RDWR_MPT_VIRT_START + MPT_VIRT_SIZE)/* Slot 261: ioremap()/fixmap area (16GB). */ #define IOREMAP_VIRT_START RDWR_MPT_VIRT_END#define IOREMAP_VIRT_END (IOREMAP_VIRT_START + GB(16))
xen/include/asm-x86/mm.h +36 -10
89 lines not on this page · raw message
xen/include/asm-x86/numa.h +1 -0
@@ -75,5 +75,6 @@ static inline __attribute__((pure)) int#define clear_node_cpumask (cpu) do {} while (0)#endif +void srat_parse_regions (u64 addr);#endif
xen/include/asm-x86/page.h +6 -10
@@ -220,8 +220,6 @@ void copy_page_sse2(void *, const void * copy_page_sse2(_t, _f) : \ (void)memcpy(_t, _f, PAGE_SIZE)) -#define __mfn_valid (mfn) ((mfn) < max_page) -/* Convert between Xen-heap virtual addresses and machine addresses. */ #define __pa (x) (virt_to_maddr(x))#define __va (x) (maddr_to_virt(x))
@@ -231,16 +229,12 @@ void copy_page_sse2(void *, const void *#define __mfn_to_virt (mfn) (maddr_to_virt((paddr_t)(mfn) << PAGE_SHIFT))/* Convert between machine frame numbers and page-info structures. */ -#define __mfn_to_page (mfn) (frame_table + (mfn)) -#define __page_to_mfn (pg) ((unsigned long)((pg) - frame_table)) +#define __mfn_to_page (mfn) (frame_table + pfn_to_pdx(mfn)) +#define __page_to_mfn (pg) pdx_to_pfn((unsigned long)((pg) - frame_table))/* Convert between machine addresses and page-info structures. */ -#define __maddr_to_page (ma) (frame_table + ((ma) >> PAGE_SHIFT)) -#define __page_to_maddr (pg) ((paddr_t)((pg) - frame_table) << PAGE_SHIFT) - -/* Convert between Xen-heap virtual addresses and page-info structures. */ -#define __virt_to_page (va) (frame_table + (__pa(va) >> PAGE_SHIFT)) -#define __page_to_virt (pg) (maddr_to_virt(page_to_maddr(pg))) +#define __maddr_to_page (ma) __mfn_to_page((ma) >> PAGE_SHIFT) +#define __page_to_maddr (pg) ((paddr_t)__page_to_mfn(pg) << PAGE_SHIFT)/* Convert between frame number and address formats. */ #define __pfn_to_paddr (pfn) ((paddr_t)(pfn) << PAGE_SHIFT)
@@ -253,6 +247,8 @@ void copy_page_sse2(void *, const void *#define mfn_valid (mfn) __mfn_valid(mfn)#define virt_to_mfn (va) __virt_to_mfn(va)#define mfn_to_virt (mfn) __mfn_to_virt(mfn) +#define virt_to_maddr (va) __virt_to_maddr((unsigned long)(va)) +#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 maddr_to_page (ma) __maddr_to_page(ma)
xen/include/asm-x86/x86_32/page.h +8 -4
@@ -41,21 +41,25 @@#include <xen/config.h> #include <asm/types.h> +#define __mfn_valid (mfn) ((mfn) < max_page) + +#define max_pdx max_page +#define pfn_to_pdx (pfn) (pfn) +#define pdx_to_pfn (pdx) (pdx) +#define virt_to_pdx (va) virt_to_mfn(va) +#define pdx_to_virt (pdx) mfn_to_virt(pdx) +static inline unsigned long __virt_to_maddr (unsigned long va) {ASSERT (va >=DIRECTMAP_VIRT_START && va <DIRECTMAP_VIRT_END );return va -DIRECTMAP_VIRT_START ; } -#define virt_to_maddr (va) \ - (__virt_to_maddr((unsigned long)(va)))static inline void *__maddr_to_virt (unsigned long ma) {ASSERT (ma <DIRECTMAP_VIRT_END -DIRECTMAP_VIRT_START );return (void *)(ma +DIRECTMAP_VIRT_START ); } -#define maddr_to_virt (ma) \ - (__maddr_to_virt((unsigned long)(ma)))/* read access (should only be used for debug printk's) */ typedef u64 intpte_t ;
xen/include/asm-x86/x86_64/page.h +46 -8
71 lines not on this page · raw message
xen/include/xen/acpi.h +3 -0
@@ -269,6 +269,9 @@ int acpi_table_parse(char *id, acpi_tablint acpi_table_parse_entries (char *id,unsigned long table_size,int entry_id,acpi_table_entry_handler handler,unsigned int max_entries);int acpi_table_parse_madt (enum acpi_madt_type id,acpi_table_entry_handler handler,unsigned int max_entries); +int acpi_table_parse_srat (enum acpi_srat_entry_id id, +acpi_madt_entry_handler handler,unsigned int max_entries); +int acpi_parse_srat (struct acpi_table_header *);void acpi_table_print (struct acpi_table_header *header,unsigned long phys_addr);void acpi_table_print_madt_entry (struct acpi_subtable_header *madt);void acpi_table_print_srat_entry (struct acpi_subtable_header *srat);
xen/include/xen/mm.h +23 -13
@@ -103,6 +103,16 @@ struct page_list_head#define PAGE_LIST_NULL (~0) +# if !defined(pdx_to_page) && !defined(page_to_pdx) +# if defined(__page_to_mfn) || defined(__mfn_to_page) +# define page_to_pdx __page_to_mfn +# define pdx_to_page __mfn_to_page +# else +# define page_to_pdx page_to_mfn +# define pdx_to_page mfn_to_page +# endif +# endif +# define PAGE_LIST_HEAD_INIT (name) { NULL, NULL }# define PAGE_LIST_HEAD (name) \ struct page_list_head name = PAGE_LIST_HEAD_INIT(name)
@@ -123,21 +133,21 @@ static inline struct page_info * page_list_next(const struct page_info *page, const struct page_list_head *head) { - return page != head->tail ? mfn_to_page(page->list.next) : NULL; + return page != head->tail ? pdx_to_page(page->list.next) : NULL; }static inline struct page_info *page_list_prev (const struct page_info *page,const struct page_list_head *head) { -return page != head->next ?mfn_to_page (page->list.prev) :NULL ; +return page != head->next ?pdx_to_page (page->list.prev) :NULL ; }static inline void page_list_add (struct page_info *page,struct page_list_head *head) {if ( head->next ) { - page->list.next =page_to_mfn (head->next); - head->next->list.prev =page_to_mfn (page); + page->list.next =page_to_pdx (head->next); + head->next->list.prev =page_to_pdx (page); }else {
@@ -153,8 +163,8 @@ page_list_add_tail(struct page_info *pag page->list.next =PAGE_LIST_NULL ;if ( head->next ) { - page->list.prev =page_to_mfn (head->tail); - head->tail->list.next =page_to_mfn (page); + page->list.prev =page_to_pdx (head->tail); + head->tail->list.next =page_to_pdx (page); }else {
@@ -191,8 +201,8 @@ __page_list_del_head(struct page_info *pstatic inline void page_list_del (struct page_info *page,struct page_list_head *head) { -struct page_info *next =mfn_to_page (page->list.next); -struct page_info *prev =mfn_to_page (page->list.prev); +struct page_info *next =pdx_to_page (page->list.next); +struct page_info *prev =pdx_to_page (page->list.prev);if ( !__page_list_del_head (page, head, next, prev) ) {
@@ -204,8 +214,8 @@ static inline void page_list_del2(struct page_info *page, struct page_list_head *head1, struct page_list_head *head2) { - struct page_info *next = mfn_to_page(page->list.next); -struct page_info *prev =mfn_to_page (page->list.prev); + struct page_info *next = pdx_to_page(page->list.next); +struct page_info *prev =pdx_to_page (page->list.prev);if ( !__page_list_del_head (page, head1, next, prev) && !__page_list_del_head (page, head2, next, prev) )
@@ -252,11 +262,11 @@ page_list_splice(struct page_list_head * last = list->tail; at = head->next; - first->list.prev =page_to_mfn (head->next); + first->list.prev =page_to_pdx (head->next); head->next = first; - last->list.next =page_to_mfn (at); - at->list.prev =page_to_mfn (last); + last->list.next =page_to_pdx (at); + at->list.prev =page_to_pdx (last); }#define page_list_for_each (pos, head) \
Avoid backing frame table holes with memory, when those holes are large enough to cover an exact multiple of large pages. This is based on the introduction of a bit map, where each bit represents one such range, thus allowing mfn_valid() checks to easily filter out those MFNs that now shouldn't be used to index the frame table.
This allows for saving a couple of 2M pages even on "normal" systems.
xen/arch/x86/mm.c +47 -21
89 lines not on this page · raw message
xen/arch/x86/setup.c +14 -0
@@ -312,6 +312,16 @@ static void __init setup_max_pdx(void)#endif } +static void __init set_pdx_range (unsigned long smfn,unsigned long emfn) +{ +unsigned long idx, eidx; + + idx =pfn_to_pdx (smfn) /PDX_GROUP_COUNT ; + eidx = (pfn_to_pdx (emfn -1 ) +PDX_GROUP_COUNT ) /PDX_GROUP_COUNT ; +for ( ; idx < eidx; ++idx ) +__set_bit (idx, pdx_group_valid); +} +/* A temporary copy of the e820 map that we can mess with during bootstrap. */ static struct e820map __initdata boot_e820;
@@ -657,6 +667,8 @@ void __init __start_xen(unsigned long mbif ( (boot_e820.map[i].type !=E820_RAM ) || (s >= e) )continue ; +set_pdx_range (s >>PAGE_SHIFT , e >>PAGE_SHIFT ); +/* Map the chunk. No memory will need to be allocated to do this. */ map_pages_to_xen( (unsigned long)maddr_to_bootstrap_virt(s),
@@ -853,6 +865,8 @@ void __init __start_xen(unsigned long mb }#endif +set_pdx_range (s >>PAGE_SHIFT , e >>PAGE_SHIFT ); +/* Need to create mappings above 16MB. */ map_s =max_t (uint64_t, s,16 <<20 ); map_e = e;
xen/arch/x86/x86_64/mm.c +8 -0
@@ -64,6 +64,14 @@ l3_pgentry_t __attribute__ ((__section__l2_pgentry_t __attribute__ ((__section__ (".bss.page_aligned" ))) l2_xenmap[L2_PAGETABLE_ENTRIES ]; +int __mfn_valid (unsigned long mfn) +{ +return likely (mfn < max_page) && +likely (!(mfn & pfn_hole_mask)) && +likely (test_bit (pfn_to_pdx (mfn) /PDX_GROUP_COUNT , + pdx_group_valid)); +} +void *alloc_xen_pagetable (void ) {extern int early_boot;
xen/include/asm-x86/config.h +2 -1
@@ -314,7 +314,8 @@ extern unsigned int video_mode, video_fl#define RDWR_MPT_VIRT_END LINEAR_PT_VIRT_START#define RDWR_MPT_VIRT_START (RDWR_MPT_VIRT_END - (MACHPHYS_MBYTES<<20))#define FRAMETABLE_VIRT_END RDWR_MPT_VIRT_START -#define FRAMETABLE_VIRT_START (FRAMETABLE_VIRT_END - (FRAMETABLE_MBYTES<<20)) +#define FRAMETABLE_SIZE (FRAMETABLE_MBYTES<<20) +#define FRAMETABLE_VIRT_START (FRAMETABLE_VIRT_END - FRAMETABLE_SIZE)#define RO_MPT_VIRT_END FRAMETABLE_VIRT_START#define RO_MPT_VIRT_START (RO_MPT_VIRT_END - (MACHPHYS_MBYTES<<20))
xen/include/asm-x86/mm.h +4 -0
@@ -263,6 +263,10 @@ extern unsigned long max_page;extern unsigned long total_pages;void init_frametable (void ); +#define PDX_GROUP_COUNT ((1 << L2_PAGETABLE_SHIFT) / \ + (sizeof(*frame_table) & -sizeof(*frame_table))) +extern unsigned long pdx_group_valid[]; +/* Convert between Xen-heap virtual addresses and page-info structures. */ static inline struct page_info *__virt_to_page (const void *v) {
xen/include/asm-x86/x86_32/page.h +5 -1
@@ -41,7 +41,11 @@#include <xen/config.h> #include <asm/types.h> -#define __mfn_valid (mfn) ((mfn) < max_page) +#define __mfn_valid (mfn) ({ \ + unsigned long __m_f_n = (mfn); \ + likely(__m_f_n < max_page) && \ + likely(test_bit(pfn_to_pdx(__m_f_n) / PDX_GROUP_COUNT, pdx_group_valid)); \ +})#define max_pdx max_page#define pfn_to_pdx (pfn) (pfn)
xen/include/asm-x86/x86_64/page.h +2 -5
@@ -35,7 +35,7 @@/* Physical address where Xen was relocated to. */ extern unsigned long xen_phys_start; -extern unsigned long max_page, max_pdx; +extern unsigned long max_pdx;extern unsigned long pfn_pdx_bottom_mask, ma_va_bottom_mask;extern unsigned int pfn_pdx_hole_shift;extern unsigned long pfn_hole_mask;
@@ -53,10 +53,7 @@ extern void pfn_pdx_hole_setup(unsigned#define pdx_to_virt (pdx) ((void *)(DIRECTMAP_VIRT_START + \ ((unsigned long)(pdx) << PAGE_SHIFT))) -static inline int __mfn_valid (unsigned long mfn) -{ -return mfn < max_page && !(mfn & pfn_hole_mask); -} +extern int __mfn_valid (unsigned long mfn);static inline unsigned long pfn_to_pdx (unsigned long pfn) {
Avoid backing M2P table holes with memory, when those holes are large enough to cover an exact multiple of large pages.
For the sake of saving and migrating guests, XENMEM_machphys_mfn_list fills the holes in the array it returns with the MFN for the previous range returned (thanks to Keir pointing out that it really doesn't matter *what* MFN gets returned for invalid ranges). Using the most rescently encountered MFN (rather than e.g. always the first one) represents an attempt to cut down on the number of references these pages will get when they get mapped into a privileged domain's address space.
This also allows for saving a couple of 2M pages even on certain "normal" systems.
xen/arch/x86/x86_32/mm.c +32 -11
92 lines not on this page · raw message
xen/arch/x86/x86_64/compat/mm.c +18 -5
@@ -153,19 +153,31 @@ int compat_arch_memory_op(int op, XEN_GU }case XENMEM_machphys_mfn_list: + { +unsigned long limit; +compat_pfn_t last_mfn; +if (copy_from_guest (&xmml, arg,1 ) )return -EFAULT ; -for ( i =0 , v =RDWR_COMPAT_MPT_VIRT_START ; - (i != xmml.max_extents) && (v !=RDWR_COMPAT_MPT_VIRT_END ); + limit = (unsigned long )(compat_machine_to_phys_mapping + + min_t(unsigned long, max_page, +MACH2PHYS_COMPAT_NR_ENTRIES (current->domain))); +if ( limit >RDWR_COMPAT_MPT_VIRT_END ) + limit =RDWR_COMPAT_MPT_VIRT_END ; +for ( i =0 , v =RDWR_COMPAT_MPT_VIRT_START , last_mfn =0 ; + (i != xmml.max_extents) && (v < limit); i++, v +=1 <<L2_PAGETABLE_SHIFT ) { l2e = compat_idle_pg_table_l2[l2_table_offset (v)]; -if ( !(l2e_get_flags (l2e) & _PAGE_PRESENT) ) -break ; - mfn =l2e_get_pfn (l2e) +l1_table_offset (v); +if (l2e_get_flags (l2e) & _PAGE_PRESENT ) + mfn =l2e_get_pfn (l2e); +else + mfn = last_mfn; +ASSERT (mfn);if (copy_to_compat_offset (xmml.extent_start, i, &mfn,1 ) )return -EFAULT ; + last_mfn = mfn; } xmml.nr_extents = i;
@@ -173,6 +185,7 @@ int compat_arch_memory_op(int op, XEN_GU rc = -EFAULT ;break ; + }default : rc = -ENOSYS ;
xen/arch/x86/x86_64/mm.c +87 -39
203 lines not on this page · raw message
Introduce a variant of map_domain_page() directly getting passed a struct page_info * argument, based on the observation that in many places the argument to this function so far simply was the result of page_to_mfn(). This is meaningful for the x86-64 case where map_domain_page() really just is an invocation of mfn_to_virt(), and hence the combined mfn_to_virt(page_to_mfn()) now represents a needless roundtrip conversion compressed -> uncompressed -> compressed of the MFN representation.
While it might seem more logical to rename map_domain_page() to e.g. map_domain_mfn(), this would require touching a lot more code.
xen/arch/x86/hvm/stdvga.c +3 -3
@@ -85,14 +85,14 @@ const uint8_t gr_mask[9] = {static uint8_t *vram_getb (struct hvm_hw_stdvga *s,unsigned int a) {struct page_info *pg = s->vram_page[(a >>12 ) &0x3f ]; -uint8_t *p =map_domain_page (page_to_mfn (pg)); +uint8_t *p =__map_domain_page (pg);return &p[a &0xfff ]; }static uint32_t *vram_getl (struct hvm_hw_stdvga *s,unsigned int a) {struct page_info *pg = s->vram_page[(a >>10 ) &0x3f ]; -uint32_t *p =map_domain_page (page_to_mfn (pg)); +uint32_t *p =__map_domain_page (pg);return &p[a &0x3ff ]; }
@@ -601,7 +601,7 @@ void stdvga_init(struct domain *d)if ( pg ==NULL )break ; s->vram_page[i] = pg; - p =map_domain_page (page_to_mfn (pg)); + p =__map_domain_page (pg);clear_page (p);unmap_domain_page (p); }
xen/arch/x86/hvm/vlapic.c +1 -1
@@ -978,7 +978,7 @@ int vlapic_init(struct vcpu *v) }if (vlapic->regs ==NULL ) { - vlapic->regs =map_domain_page_global (page_to_mfn (vlapic->regs_page)); + vlapic->regs =__map_domain_page_global (vlapic->regs_page);if ( vlapic->regs ==NULL ) { dprintk(XENLOG_ERR, "map vlapic regs error: %d/%d\n",
xen/arch/x86/mm.c +1 -1
@@ -543,7 +543,7 @@ static int alloc_segdesc_page(struct pagstruct desc_struct *descs;int i; - descs =map_domain_page (page_to_mfn (page)); + descs =__map_domain_page (page);for ( i =0 ; i <512 ; i++ )if (unlikely (!check_descriptor (page_get_owner (page), &descs[i])) )
xen/arch/x86/mm/hap/hap.c +2 -2
@@ -255,7 +255,7 @@ static struct page_info *hap_alloc(struc d->arch.paging.hap.free_pages--; - p =hap_map_domain_page (page_to_mfn (pg)); + p =__map_domain_page (pg);ASSERT (p !=NULL );clear_page (p);hap_unmap_domain_page (p);
@@ -293,7 +293,7 @@ static struct page_info *hap_alloc_p2m_p NULL, MEMF_bits(32) | MEMF_node(domain_to_node(d)));if (likely (pg !=NULL ) ) { -void *p =hap_map_domain_page (page_to_mfn (pg)); +void *p =__map_domain_page (pg);clear_page (p);hap_unmap_domain_page (p); }
xen/arch/x86/mm/p2m.c +1 -1
@@ -200,7 +200,7 @@ p2m_next_level(struct domain *d, mfn_t *else flags &= ~_PAGE_PSE;/* Clear _PAGE_PSE (== _PAGE_PAT) */ - l1_entry =map_domain_page (mfn_x (page_to_mfn (pg))); + l1_entry =__map_domain_page (pg);for ( i =0 ; i <L1_PAGETABLE_ENTRIES ; i++ ) { new_entry =l1e_from_pfn (pfn + i, flags);
xen/arch/x86/mm/paging.c +2 -4
@@ -99,7 +99,6 @@static mfn_t paging_new_log_dirty_page (struct domain *d,void **mapping_p) { -mfn_t mfn;struct page_info *page; page =alloc_domheap_page (NULL ,MEMF_node (domain_to_node (d)));
@@ -110,10 +109,9 @@ static mfn_t paging_new_log_dirty_page(s } d->arch.paging.log_dirty.allocs++; - mfn =page_to_mfn (page); - *mapping_p =map_domain_page (mfn_x (mfn)); + *mapping_p =__map_domain_page (page); -return mfn; +return page_to_mfn (page); } static mfn_t paging_new_log_dirty_leaf(
xen/arch/x86/mm/shadow/common.c +2 -2
@@ -1578,7 +1578,7 @@ mfn_t shadow_alloc(struct domain *d,flush_tlb_mask (&mask); }/* Now safe to clear the page for reuse */ - p =sh_map_domain_page (page_to_mfn (sp+i)); + p =__map_domain_page (sp+i);ASSERT (p !=NULL );clear_page (p);sh_unmap_domain_page (p);
@@ -3130,7 +3130,7 @@ int shadow_enable(struct domain *d, u32 }/* Fill it with 32-bit, non-PAE superpage entries, each mapping 4MB * of virtual address space onto the same physical address range */ - e =sh_map_domain_page (page_to_mfn (pg)); + e =__map_domain_page (pg);for ( i =0 ; i <PAGE_SIZE /sizeof (*e); i++ ) e[i] = ((0x400000U * i) | _PAGE_PRESENT | _PAGE_RW | _PAGE_USER
xen/arch/x86/tboot.c +1 -2
@@ -205,8 +205,7 @@ static void tboot_gen_domain_integrity(cpage_list_for_each (page, &d->page_list) { -void *pg; - pg =map_domain_page (page_to_mfn (page)); +void *pg =__map_domain_page (page);vmac_update (pg,PAGE_SIZE , &ctx);unmap_domain_page (pg); }
xen/common/grant_table.c +1 -1
@@ -1195,7 +1195,7 @@ gnttab_transfer( } sp =map_domain_page (mfn); - dp =map_domain_page (page_to_mfn (new_page)); + dp =__map_domain_page (new_page);memcpy (dp, sp,PAGE_SIZE );unmap_domain_page (dp);unmap_domain_page (sp);
xen/common/page_alloc.c +1 -1
@@ -1235,7 +1235,7 @@ __initcall(pagealloc_keyhandler_init);void scrub_one_page (struct page_info *pg) { -void *p =map_domain_page (page_to_mfn (pg)); +void *p =__map_domain_page (pg);#ifndef NDEBUG /* Avoid callers relying on allocations returning zeroed pages. */
xen/common/tmem_xen.c +1 -1
@@ -268,7 +268,7 @@ static void *tmh_persistent_pool_page_geif ( (pi =_tmh_alloc_page_thispool (d)) ==NULL )return NULL ;ASSERT (IS_VALID_PAGE (pi)); -return map_domain_page (page_to_mfn (pi)); +return __map_domain_page (pi); }static void tmh_persistent_pool_page_put (void *page_va)
xen/drivers/passthrough/amd/iommu_map.c +1 -1
@@ -430,7 +430,7 @@ static u64 iommu_l2e_from_pfn(struct pag (level - IOMMU_PAGING_MODE_LEVEL_1))); offset &= ~PTE_PER_TABLE_MASK ; - table_vaddr =map_domain_page (page_to_mfn (table)); + table_vaddr =__map_domain_page (table); pde = table_vaddr + (offset *IOMMU_PAGE_TABLE_ENTRY_SIZE ); next_table_maddr =amd_iommu_get_next_table_from_pte (pde);
xen/drivers/passthrough/amd/pci_amd_iommu.c +1 -1
@@ -335,7 +335,7 @@ static void deallocate_next_page_table(su64 next_table_maddr;int index; - table_vaddr =map_domain_page (page_to_mfn (pg)); + table_vaddr =__map_domain_page (pg);if ( level >1 ) {
xen/drivers/passthrough/vtd/iommu.c +1 -1
@@ -154,7 +154,7 @@ u64 alloc_pgtable_maddr(struct acpi_drhd (node == -1 ) ? 0 : MEMF_node(node));if ( !pg )return 0 ; - vaddr =map_domain_page (page_to_mfn (pg)); + vaddr =__map_domain_page (pg);memset (vaddr,0 ,PAGE_SIZE * npages);iommu_flush_cache_page (vaddr, npages);
xen/include/asm-x86/hvm/svm/amd-iommu-proto.h +1 -1
@@ -131,7 +131,7 @@ static inline struct page_info* alloc_am pg =alloc_domheap_page (NULL ,0 );if ( pg ==NULL )return 0 ; - vaddr =map_domain_page (page_to_mfn (pg)); + vaddr =__map_domain_page (pg);memset (vaddr,0 ,PAGE_SIZE );unmap_domain_page (vaddr);return pg;
xen/include/xen/domain_page.h +5 -0
@@ -34,6 +34,9 @@ void unmap_domain_page(const void *va);void *map_domain_page_global (unsigned long mfn);void unmap_domain_page_global (const void *va); +#define __map_domain_page (pg) map_domain_page(__page_to_mfn(pg)) +#define __map_domain_page_global (pg) map_domain_page_global(__page_to_mfn(pg)) +#define DMCACHE_ENTRY_VALID 1U#define DMCACHE_ENTRY_HELD 2U
@@ -97,9 +100,11 @@ domain_mmap_cache_destroy(struct domain_#else /* !CONFIG_DOMAIN_PAGE */ #define map_domain_page (mfn) mfn_to_virt(mfn) +#define __map_domain_page (pg) page_to_virt(pg)#define unmap_domain_page (va) ((void)(va))#define map_domain_page_global (mfn) mfn_to_virt(mfn) +#define __map_domain_page_global (pg) page_to_virt(pg)#define unmap_domain_page_global (va) ((void)(va)) struct domain_mmap_cache {
The grant table code had some particularly frequent repetitions of mfn_to_page() on each time the same input arguments. To help the compiler (which can do only a limited job on CSE), this adds explicit caching of the transformation result in a few places.
xen/common/grant_table.c +28 -21
152 lines not on this page · raw message
This one is likely the most questionable part: Realizing that the compiler can do a much better job in terms of CSE when the variables needed are constant, this introduces and extension to the read-mostly variables, allowing variables to be marked as being written just during boot initialization.
There is, however, quite a bit of care required, as it must be ensured that the compiler will never mix uses of the "real" and the "aliased" variable within a function, and that the compiler never cache the contents of the read-only representation across those function calls that lead to initialization through the writable representation.
The concept therefore is to have two mechanisms for initialization:
Either through marking a whole translation unit as being initialization only (and hence only ever seeing the writable representation), or by making sure that in a particular initialization function it is only the writable representation that gets accessed (even when wanting to read its value). For that latter method, I wasn't able to think of a mechanism which would enforce this, which is the reason for the need to be really careful.
The benefit of the whole contruct is that for some of the converted variables the number of accesses to these variables goes down by over 50%, including cases where entire inline function expansions (accessing several of these variables) can be eliminated by CSE.
xen/arch/x86/mm.c +4 -4
@@ -138,12 +138,12 @@ l1_pgentry_t __attribute__ ((__section__struct domain *dom_xen, *dom_io;/* Frame table size in pages. */ -unsigned long max_page; -unsigned long total_pages; +DEFINE_WRITE_ONCE(unsigned long, max_page, 0); +DEFINE_WRITE_ONCE(unsigned long, total_pages, 0); -unsigned long __read_mostly pdx_group_valid[BITS_TO_LONGS ( +DEFINE_WRITE_ONCE(unsigned long[BITS_TO_LONGS( (FRAMETABLE_SIZE / sizeof(*frame_table) + PDX_GROUP_COUNT - 1) - /PDX_GROUP_COUNT )] = { [0 ] =1 }; + / PDX_GROUP_COUNT)], pdx_group_valid, { [0] = 1 });#define PAGE_CACHE_ATTRS (_PAGE_PAT|_PAGE_PCD|_PAGE_PWT)
xen/arch/x86/setup.c +1 -0
@@ -1,3 +1,4 @@ +#include <xen/no-write-once.h> #include <xen/config.h> #include <xen/init.h> #include <xen/lib.h>
xen/arch/x86/x86_64/mm.c +15 -13
@@ -34,13 +34,13 @@#include <public/memory.h> /* Parameters for PFN/MADDR compression. */ -unsigned long __read_mostly max_pdx; -unsigned long __read_mostly pfn_pdx_bottom_mask = ~0UL ; -unsigned long __read_mostly ma_va_bottom_mask = ~0UL ; -unsigned long __read_mostly pfn_top_mask =0 ; -unsigned long __read_mostly ma_top_mask =0 ; -unsigned long __read_mostly pfn_hole_mask =0 ; -unsigned int __read_mostly pfn_pdx_hole_shift =0 ; +DEFINE_WRITE_ONCE(unsigned long, max_pdx, 0); +DEFINE_WRITE_ONCE(unsigned long, pfn_pdx_bottom_mask, ~0UL); +DEFINE_WRITE_ONCE(unsigned long, ma_va_bottom_mask, ~0UL); +DEFINE_WRITE_ONCE(unsigned long, pfn_top_mask, 0); +DEFINE_WRITE_ONCE(unsigned long, ma_top_mask, 0); +DEFINE_WRITE_ONCE(unsigned long, pfn_hole_mask, 0); +DEFINE_WRITE_ONCE(unsigned int, pfn_pdx_hole_shift, 0);#ifdef CONFIG_COMPAT unsigned int m2p_compat_vstart = __HYPERVISOR_COMPAT_VIRT_START;
@@ -183,12 +183,14 @@ void __init pfn_pdx_hole_setup(unsignedprintk (KERN_INFO "PFN compression on bits %u...%u\n" , bottom_shift, bottom_shift + hole_shift -1 ); - pfn_pdx_hole_shift = hole_shift; - pfn_pdx_bottom_mask = (1UL << bottom_shift) -1 ; - ma_va_bottom_mask = (PAGE_SIZE << bottom_shift) -1 ; - pfn_hole_mask = ((1UL << hole_shift) -1 ) << bottom_shift; - pfn_top_mask = ~(pfn_pdx_bottom_mask | pfn_hole_mask); - ma_top_mask = pfn_top_mask <<PAGE_SHIFT ; +write_once (pfn_pdx_hole_shift) = hole_shift; +write_once (pfn_pdx_bottom_mask) = (1UL << bottom_shift) -1 ; +write_once (ma_va_bottom_mask) = (PAGE_SIZE << bottom_shift) -1 ; +write_once (pfn_hole_mask) = ((1UL << hole_shift) -1 ) + << bottom_shift; +write_once (pfn_top_mask) = ~(write_once (pfn_pdx_bottom_mask) + |write_once (pfn_hole_mask)); +write_once (ma_top_mask) =write_once (pfn_top_mask) <<PAGE_SHIFT ; }void __init paging_init (void )
xen/arch/x86/xen.lds.S +1 -0
@@ -59,6 +59,7 @@ SECTIONS . = ALIGN(128); .data.read_mostly : { + *(.data.write_once) *(.data.read_mostly) } :text
xen/include/asm-x86/mm.h +4 -3
@@ -4,6 +4,7 @@#include <xen/config.h> #include <xen/list.h> +#include <xen/write-once.h> #include <asm/io.h> #include <asm/uaccess.h>
@@ -259,13 +260,13 @@ extern void share_xen_page_with_privileg struct page_info *page, int readonly);#define frame_table ((struct page_info *)FRAMETABLE_VIRT_START) -extern unsigned long max_page; -extern unsigned long total_pages; +extern DECLARE_WRITE_ONCE(unsigned long, max_page); +extern DECLARE_WRITE_ONCE(unsigned long, total_pages);void init_frametable (void );#define PDX_GROUP_COUNT ((1 << L2_PAGETABLE_SHIFT) / \ (sizeof(*frame_table) & -sizeof(*frame_table))) -extern unsigned long pdx_group_valid[]; +extern DECLARE_WRITE_ONCE(unsigned long[], pdx_group_valid);/* Convert between Xen-heap virtual addresses and page-info structures. */ static inline struct page_info *__virt_to_page (const void *v)
xen/include/asm-x86/x86_64/page.h +9 -6
@@ -30,16 +30,19 @@#ifndef __ASSEMBLY__#include <xen/config.h> +#include <xen/write-once.h> #include <asm/types.h> /* Physical address where Xen was relocated to. */ extern unsigned long xen_phys_start; -extern unsigned long max_pdx; -extern unsigned long pfn_pdx_bottom_mask, ma_va_bottom_mask; -extern unsigned int pfn_pdx_hole_shift; -extern unsigned long pfn_hole_mask; -extern unsigned long pfn_top_mask, ma_top_mask; +extern DECLARE_WRITE_ONCE(unsigned long, max_pdx); +extern DECLARE_WRITE_ONCE(unsigned long, pfn_pdx_bottom_mask); +extern DECLARE_WRITE_ONCE(unsigned long, ma_va_bottom_mask); +extern DECLARE_WRITE_ONCE(unsigned int, pfn_pdx_hole_shift); +extern DECLARE_WRITE_ONCE(unsigned long, pfn_hole_mask); +extern DECLARE_WRITE_ONCE(unsigned long, pfn_top_mask); +extern DECLARE_WRITE_ONCE(unsigned long, ma_top_mask);extern void pfn_pdx_hole_setup (unsigned long );#define page_to_pdx (pg) ((pg) - frame_table)
@@ -53,7 +56,7 @@ extern void pfn_pdx_hole_setup(unsigned#define pdx_to_virt (pdx) ((void *)(DIRECTMAP_VIRT_START + \ ((unsigned long)(pdx) << PAGE_SHIFT))) -extern int __mfn_valid (unsigned long mfn); +extern __attribute_write_once__ int __mfn_valid(unsigned long mfn);static inline unsigned long pfn_to_pdx (unsigned long pfn) {
xen/include/xen/no-write-once.h new file +11 -0
@@ -0,0 +1,11 @@ +#ifdef __XEN_WRITE_ONCE_H__ +#error Must not include xen/no-write-once.h after xen/write-once.h. +#endif +#ifndef __XEN_NO_WRITE_ONCE_H__ +#define __XEN_NO_WRITE_ONCE_H__ + +#define DECLARE_WRITE_ONCE (type, name) __typeof__(type) name + +#define __attribute_write_once__ + +#endif /* __XEN_NO_WRITE_ONCE_H__ */
xen/include/xen/write-once.h new file +15 -0
@@ -0,0 +1,15 @@ +#if !defined(__XEN_WRITE_ONCE_H__) && !defined(__XEN_NO_WRITE_ONCE_H__) +#define __XEN_WRITE_ONCE_H__ + +#define DECLARE_WRITE_ONCE (type, name) const __typeof__(type) name + +#define DEFINE_WRITE_ONCE (type, name, ...) static __typeof__(type) \ + __attribute_used__ __attribute__((__section__(".data.write_once"))) \ + __write_once_##name __asm__(#name) = __VA_ARGS__; \ + __asm__(".globl " #name) + +#define write_once (name) __write_once_##name + +#define __attribute_write_once__ __attribute_const__ + +#endif /* __XEN_WRITE_ONCE_H__ */
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel