x86: map M2P table sparsely
PATCH 4/7 x86: map M2P table sparsely
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 recently 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
@@ -72,7 +72,7 @@ void __init paging_init(void) {unsigned long v;struct page_info *pg; -int i; +unsigned int i, n;if ( cpu_has_pge ) {
@@ -96,8 +96,18 @@ void __init paging_init(void)*/ mpt_size = (max_page *BYTES_PER_LONG ) + (1UL <<L2_PAGETABLE_SHIFT ) -1 ; mpt_size &= ~((1UL <<L2_PAGETABLE_SHIFT ) -1UL ); +#define MFN (x) (((x) << L2_PAGETABLE_SHIFT) / sizeof(unsigned long)) +#define CNT ((sizeof(*frame_table) & -sizeof(*frame_table)) / \ + sizeof(*machine_to_phys_mapping)) +BUILD_BUG_ON ((sizeof (*frame_table) & ~sizeof (*frame_table)) % \ +sizeof (*machine_to_phys_mapping));for ( i =0 ; i < (mpt_size >>L2_PAGETABLE_SHIFT ); i++ ) { +for ( n =0 ; n <CNT ; ++n) +if (mfn_valid (MFN (i) + n *PDX_GROUP_COUNT ) ) +break ; +if ( n ==CNT ) +continue ;if ( (pg =alloc_domheap_pages (NULL ,PAGETABLE_ORDER ,0 )) ==NULL )panic ("Not enough memory to bootstrap Xen.\n" ); l2e_write(&idle_pg_table_l2[l2_linear_offset(RDWR_MPT_VIRT_START) + i],
@@ -106,11 +116,12 @@ void __init paging_init(void)l2e_write (&idle_pg_table_l2[l2_linear_offset (RO_MPT_VIRT_START ) + i],l2e_from_page ( pg, (__PAGE_HYPERVISOR | _PAGE_PSE) & ~_PAGE_RW)); +/* Fill with an obvious debug pattern. */ +memset ((void *)(RDWR_MPT_VIRT_START + (i <<L2_PAGETABLE_SHIFT )),0x55 , +1UL <<L2_PAGETABLE_SHIFT ); } - -/* Fill with an obvious debug pattern. */ -for ( i =0 ; i < (mpt_size /BYTES_PER_LONG ); i++) -set_gpfn_from_mfn (i,0x55555555 ); +#undef CNT +#undef MFN/* Create page tables for ioremap()/map_domain_page_global(). */ for ( i =0 ; i < (IOREMAP_MBYTES >> (L2_PAGETABLE_SHIFT -20 )); i++ )
@@ -163,14 +174,17 @@ void __init subarch_init_memory(void) {unsigned long m2p_start_mfn;unsigned int i, j; +l2_pgentry_t l2e;BUILD_BUG_ON (sizeof (struct page_info ) !=24 );/* M2P table is mappable read-only by privileged domains. */ for ( i =0 ; i < (mpt_size >>L2_PAGETABLE_SHIFT ); i++ ) { - m2p_start_mfn =l2e_get_pfn ( - idle_pg_table_l2[l2_linear_offset (RDWR_MPT_VIRT_START ) + i]); + l2e = idle_pg_table_l2[l2_linear_offset (RDWR_MPT_VIRT_START ) + i]; +if ( !(l2e_get_flags (l2e) & _PAGE_PRESENT) ) +continue ; + m2p_start_mfn =l2e_get_pfn (l2e);for ( j =0 ; j <L2_PAGETABLE_ENTRIES ; j++ ) {struct page_info *page =mfn_to_page (m2p_start_mfn + j);
@@ -191,8 +205,9 @@ void __init subarch_init_memory(void)long subarch_memory_op (int op,XEN_GUEST_HANDLE (void ) arg) {struct xen_machphys_mfn_list xmml; -unsigned long mfn; +unsigned long mfn, last_mfn;unsigned int i, max; +l2_pgentry_t l2e;long rc =0 ; switch ( op )
@@ -203,12 +218,18 @@ long subarch_memory_op(int op, XEN_GUEST max = min_t(unsigned int, xmml.max_extents, mpt_size >> 21); -for ( i =0 ; i < max; i++ ) +for ( i =0 , last_mfn =0 ; i < max; i++ ) { - mfn =l2e_get_pfn (idle_pg_table_l2[l2_linear_offset ( -RDWR_MPT_VIRT_START + (i <<21 ))]) +l1_table_offset (i <<21 ); + l2e = idle_pg_table_l2[l2_linear_offset ( +RDWR_MPT_VIRT_START + (i <<21 ))]; +if (l2e_get_flags (l2e) & _PAGE_PRESENT ) + mfn =l2e_get_pfn (l2e); +else + mfn = last_mfn; +ASSERT (mfn);if (copy_to_guest_offset (xmml.extent_start, i, &mfn,1 ) )return -EFAULT ; + last_mfn = mfn; } xmml.nr_extents = i;
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