x86: M2P maintenance adjustments (step 1)
cover x86: M2P maintenance adjustments (step 1)
As pointed out by Andrew, maintaining the compat M2P when !PV32 (or when "pv=no-32" was given) is a pointless waste of memory. Do away with this, with a possible plan to subsequently use the compat instance instead of the native one in shim mode with a 32-bit guest (requiring more intrusive rework, in particular to delay setting up the M2P until the bitness of the client domain is known).
1: slightly re-arrange 32-bit handling in dom0_construct_pv() 2: don't maintain compat M2P when !PV32
3: don't override INVALID_M2P_ENTRY with SHARED_M2P_ENTRY
Jan
PATCH 1/3 x86: slightly re-arrange 32-bit handling in dom0_construct_pv()
Add #ifdef-s (the 2nd one will be needed in particular, to guard the uses of m2p_compat_vstart and HYPERVISOR_COMPAT_VIRT_START()) and fold duplicate uses of elf_32bit().
Also adjust what gets logged: Avoid "compat32" when support isn't built in, and don't assume ELF class <> ELFCLASS64 means ELFCLASS32.
xen/arch/x86/pv/dom0_build.c +42 -29
@@ -300,7 +300,6 @@ int __init dom0_construct_pv(struct domastruct page_info *page =NULL ;start_info_t *si;struct vcpu *v = d->vcpu[0 ]; -unsigned long long value;void *image_base =bootstrap_map (image);unsigned long image_len = image->mod_end;void *image_start = image_base + image_headroom;
@@ -357,27 +356,36 @@ int __init dom0_construct_pv(struct doma goto out;/* compatibility check */ +printk (" Xen kernel: 64-bit, lsb%s\n" , +IS_ENABLED (CONFIG_PV32 ) ?", compat32" :"" ); compatible =0 ; machine =elf_uval (&elf, elf.ehdr, e_machine); -printk (" Xen kernel: 64-bit, lsb, compat32\n" ); -if (elf_32bit (&elf) && parms.pae ==XEN_PAE_BIMODAL ) - parms.pae =XEN_PAE_EXTCR3 ; -if (elf_32bit (&elf) && parms.pae && machine ==EM_386 ) + +#ifdef CONFIG_PV32 +if (elf_32bit (&elf) ) { -if (unlikely (rc =switch_compat (d)) ) +if ( parms.pae ==XEN_PAE_BIMODAL ) + parms.pae =XEN_PAE_EXTCR3 ; +if ( parms.pae && machine ==EM_386 ) { -printk ("Dom0 failed to switch to compat: %d\n" , rc); -return rc; - } +if (unlikely (rc =switch_compat (d)) ) + { +printk ("Dom0 failed to switch to compat: %d\n" , rc); +return rc; + } - compatible =1 ; + compatible =1 ; + } } -if (elf_64bit (&elf) && machine ==EM_X86_64 ) +#endif + +if (elf_64bit (&elf) && machine ==EM_X86_64 ) compatible =1 ; -printk (" Dom0 kernel: %s%s, %s, paddr %#" PRIx64" -> %#" PRIx64"\n" , -elf_64bit (&elf) ?"64-bit" :"32-bit" , - parms.pae ?", PAE" :"" , -elf_msb (&elf) ?"msb" :"lsb" , + +printk (" Dom0 kernel: %s-bit%s, %s, paddr %#" PRIx64" -> %#" PRIx64"\n" , +elf_64bit (&elf) ?"64" :elf_32bit (&elf) ?"32" :"??" , + parms.pae ?", PAE" :"" , +elf_msb (&elf) ?"msb" :"lsb" , elf.pstart, elf.pend);if ( elf.bsd_symtab_pstart ) printk(" Dom0 symbol map %#" PRIx64 " -> %#" PRIx64 "\n",
@@ -405,23 +413,28 @@ int __init dom0_construct_pv(struct domaif ( parms.pae ==XEN_PAE_EXTCR3 )set_bit (VMASST_TYPE_pae_extended_cr3, &d->vm_assist); -if ( !pv_shim && (parms.virt_hv_start_low !=UNSET_ADDR ) && -elf_32bit (&elf) ) +#ifdef CONFIG_PV32 +if (elf_32bit (&elf) ) { -unsigned long mask = (1UL <<L2_PAGETABLE_SHIFT ) -1 ; - value = (parms.virt_hv_start_low + mask) & ~mask; -BUG_ON (!is_pv_32bit_domain (d)); -if ( value > __HYPERVISOR_COMPAT_VIRT_START ) -panic ("Domain 0 expects too high a hypervisor start address\n" ); -HYPERVISOR_COMPAT_VIRT_START (d) = - max_t(unsigned int, m2p_compat_vstart, value); - } +if ( !pv_shim && (parms.virt_hv_start_low !=UNSET_ADDR ) ) + { +unsigned long mask = (1UL <<L2_PAGETABLE_SHIFT ) -1 ; +unsigned long value = (parms.virt_hv_start_low + mask) & ~mask; -if ( (parms.p2m_base !=UNSET_ADDR ) &&elf_32bit (&elf) ) - { -printk (XENLOG_WARNING "P2M table base ignored\n" ); - parms.p2m_base =UNSET_ADDR ; +BUG_ON (!is_pv_32bit_domain (d)); +if ( value > __HYPERVISOR_COMPAT_VIRT_START ) +panic ("Domain 0 expects too high a hypervisor start address\n" ); +HYPERVISOR_COMPAT_VIRT_START (d) = + max_t(unsigned int, m2p_compat_vstart, value); + } + +if ( parms.p2m_base !=UNSET_ADDR ) + { +printk (XENLOG_WARNING "P2M table base ignored\n" ); + parms.p2m_base =UNSET_ADDR ; + } } +#endif /* * Why do we need this? The number of page-table frames depends on the
Re: [PATCH 1/3] x86: slightly re-arrange 32-bit handling in dom0_construct_pv()
Quoted 8 lines · Jan Beulich
Add #ifdef-s (the 2nd one will be needed in particular, to guard the uses of
m2p_compat_vstartandHYPERVISOR_COMPAT_VIRT_START()) and fold duplicate uses ofelf_32bit().Also adjust what gets logged: Avoid "compat32" when support isn't built in, and don't assume ELF class <> ELFCLASS64 means ELFCLASS32.
Signed-off-by Jan Beulich <[email protected]>
further suggestions.
Quoted 12 lines
xen/arch/x86/pv/dom0_build.c +0
-1@@ -300,7 +300,6 @@ int __init dom0_construct_pv(struct domastruct page_info *page =NULL ;start_info_t *si;struct vcpu *v = d->vcpu[0 ]; -unsigned long long value;void *image_base =bootstrap_map (image);unsigned long image_len = image->mod_end;void *image_start = image_base + image_headroom;
xen/arch/x86/pv/dom0_build.c int __init dom0_construct_pv(struct doma · line 360
Quoted 5 lines
xen/arch/x86/pv/dom0_build.c +2
-0@@ -357,27 +356,36 @@ int __init dom0_construct_pv(struct doma goto out;/* compatibility check */ +printk (" Xen kernel: 64-bit, lsb%s\n" , +IS_ENABLED (CONFIG_PV32 ) ?", compat32" :"" );
Here, and below, we print out lsb/msb for the ELF file. However, we don't use or check that it is actually lsb, and blindly assume that it is.
Why bother printing it then?
Quoted 56 lines
xen/arch/x86/pv/dom0_build.c +23
-18compatible =0 ; machine =elf_uval (&elf, elf.ehdr, e_machine); -printk (" Xen kernel: 64-bit, lsb, compat32\n" ); -if (elf_32bit (&elf) && parms.pae ==XEN_PAE_BIMODAL ) - parms.pae =XEN_PAE_EXTCR3 ; -if (elf_32bit (&elf) && parms.pae && machine ==EM_386 ) + +#ifdef CONFIG_PV32 +if (elf_32bit (&elf) ) { -if (unlikely (rc =switch_compat (d)) ) +if ( parms.pae ==XEN_PAE_BIMODAL ) + parms.pae =XEN_PAE_EXTCR3 ; +if ( parms.pae && machine ==EM_386 ) { -printk ("Dom0 failed to switch to compat: %d\n" , rc); -return rc; - } +if (unlikely (rc =switch_compat (d)) ) + { +printk ("Dom0 failed to switch to compat: %d\n" , rc); +return rc; + } - compatible =1 ; + compatible =1 ; + } } -if (elf_64bit (&elf) && machine ==EM_X86_64 ) +#endif + +if (elf_64bit (&elf) && machine ==EM_X86_64 ) compatible =1 ; -printk (" Dom0 kernel: %s%s, %s, paddr %#" PRIx64" -> %#" PRIx64"\n" , -elf_64bit (&elf) ?"64-bit" :"32-bit" , - parms.pae ?", PAE" :"" , -elf_msb (&elf) ?"msb" :"lsb" , + +printk (" Dom0 kernel: %s-bit%s, %s, paddr %#" PRIx64" -> %#" PRIx64"\n" , +elf_64bit (&elf) ?"64" :elf_32bit (&elf) ?"32" :"??" , + parms.pae ?", PAE" :"" , +elf_msb (&elf) ?"msb" :"lsb" , elf.pstart, elf.pend);if ( elf.bsd_symtab_pstart ) printk(" Dom0 symbol map %#" PRIx64 " -> %#" PRIx64 "\n",@@ -405,23 +413,28 @@ int __init dom0_construct_pv(struct domaif ( parms.pae ==XEN_PAE_EXTCR3 )set_bit (VMASST_TYPE_pae_extended_cr3, &d->vm_assist); -if ( !pv_shim && (parms.virt_hv_start_low !=UNSET_ADDR ) && -elf_32bit (&elf) ) +#ifdef CONFIG_PV32 +if (elf_32bit (&elf) ) { -unsigned long mask = (1UL <<L2_PAGETABLE_SHIFT ) -1 ; - value = (parms.virt_hv_start_low + mask) & ~mask;
xen/arch/x86/pv/dom0_build.c int __init dom0_construct_pv(struct doma · line 422
Quoted 10 lines
xen/arch/x86/pv/dom0_build.c +4
-6-BUG_ON (!is_pv_32bit_domain (d)); -if ( value > __HYPERVISOR_COMPAT_VIRT_START ) -panic ("Domain 0 expects too high a hypervisor start address\n" ); -HYPERVISOR_COMPAT_VIRT_START (d) = - max_t(unsigned int, m2p_compat_vstart, value); - } +if ( !pv_shim && (parms.virt_hv_start_low !=UNSET_ADDR ) ) + { +unsigned long mask = (1UL <<L2_PAGETABLE_SHIFT ) -1 ; +unsigned long value = (parms.virt_hv_start_low + mask) & ~mask;
ROUNDUP() instead of opencoding it?
xen/arch/x86/pv/dom0_build.c int __init dom0_construct_pv(struct doma · line 424
Quoted 6 lines
xen/arch/x86/pv/dom0_build.c +1
-4-if ( (parms.p2m_base !=UNSET_ADDR ) &&elf_32bit (&elf) ) - { -printk (XENLOG_WARNING "P2M table base ignored\n" ); - parms.p2m_base =UNSET_ADDR ; +BUG_ON (!is_pv_32bit_domain (d));
This BUG_ON() is useless. I suspect it is a vestigial safety measure from when the switch to compat was opencoded rather than using switch_compat() directly.
xen/arch/x86/pv/dom0_build.c int __init dom0_construct_pv(struct doma · line 426
Quoted 2 lines
xen/arch/x86/pv/dom0_build.c +2
-0+if ( value > __HYPERVISOR_COMPAT_VIRT_START ) +panic ("Domain 0 expects too high a hypervisor start address\n" );
It would be better to printk() and return -EINVAL, to be consistent with how other fatal errors are reported to the user.
~Andrew
Quoted 15 lines
xen/arch/x86/pv/dom0_build.c +10
-0+HYPERVISOR_COMPAT_VIRT_START (d) = + max_t(unsigned int, m2p_compat_vstart, value); + } + +if ( parms.p2m_base !=UNSET_ADDR ) + { +printk (XENLOG_WARNING "P2M table base ignored\n" ); + parms.p2m_base =UNSET_ADDR ; + } } +#endif /* * Why do we need this? The number of page-table frames depends on the
Re: [PATCH 1/3] x86: slightly re-arrange 32-bit handling in dom0_construct_pv()
Quoted 5 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich
Add #ifdef-s (the 2nd one will be needed in particular, to guard the uses of
m2p_compat_vstartandHYPERVISOR_COMPAT_VIRT_START()) and fold duplicate uses ofelf_32bit().
Quoted 7 lines
Quoted 4 lines
Also adjust what gets logged: Avoid "compat32" when support isn't built in, and don't assume ELF class <> ELFCLASS64 means ELFCLASS32.
Signed-off-by Jan Beulich <[email protected]>further suggestions.
Thanks.
Quoted 11 lines
xen/arch/x86/pv/dom0_build.c int __init dom0_construct_pv(struct doma · line 360
Quoted 6 lines
xen/arch/x86/pv/dom0_build.c +2
-0@@ -357,27 +356,36 @@ int __init dom0_construct_pv(struct doma goto out;/* compatibility check */ +printk (" Xen kernel: 64-bit, lsb%s\n" , +IS_ENABLED (CONFIG_PV32 ) ?", compat32" :"" );Here, and below, we print out lsb/msb for the ELF file. However, we don't use or check that it is actually lsb, and blindly assume that it is.
Why bother printing it then?
To be honest, I'd rather add a check than drop the printing. However unlikely it may be to encounter an MSB ELF binary ... This particular one I'd like to do in a separate, follow-on patch though.
I've addressed the other comments in what I intend to commit.
Jan
PATCH 2/3 x86: don't maintain compat M2P when !PV32
In reply to Jan BeulichNote that opt_pv32's declaration / #define need to be moved due to other header dependencies; in particular can asm-x86/mm.h not include asm-x86/pv/domain.h.
While touching their definitions anyway, also adjust section placement of m2p_compat_vstart and compat_idle_pg_table_l2. Similarly, while putting init_xen_pae_l2_slots() inside #ifdef, also move it to a PV-only source file.
---
An alternative place for opt_pv32.h would seem to be asm-x86/config.h.
xen/arch/x86/domain.c +2 -0
@@ -597,8 +597,10 @@ int arch_domain_create(struct domain *d, } d->arch.emulation_flags = emflags; +#ifdef CONFIG_PV32 HYPERVISOR_COMPAT_VIRT_START (d) =is_pv_domain (d) ? __HYPERVISOR_COMPAT_VIRT_START : ~0u ; +#endif if ( (rc =paging_domain_init (d)) !=0 ) goto fail;
xen/arch/x86/mm.c +0 -7
@@ -1427,13 +1427,6 @@ static bool pae_xen_mappings_check(constreturn true; } -void init_xen_pae_l2_slots (l2_pgentry_t *l2t,const struct domain *d) -{ -memcpy (&l2t[COMPAT_L2_PAGETABLE_FIRST_XEN_SLOT (d)], - compat_idle_pg_table_l2, -COMPAT_L2_PAGETABLE_XEN_SLOTS (d) *sizeof (*l2t)); -} -static int promote_l2_table (struct page_info *page,unsigned long type) {struct domain *d =page_get_owner (page);
xen/arch/x86/pv/mm.c +9 -0
@@ -128,6 +128,15 @@ bool pv_map_ldt_shadow_page(unsigned intreturn true; } +#ifdef CONFIG_PV32 +void init_xen_pae_l2_slots (l2_pgentry_t *l2t,const struct domain *d) +{ +memcpy (&l2t[COMPAT_L2_PAGETABLE_FIRST_XEN_SLOT (d)], + compat_idle_pg_table_l2, +COMPAT_L2_PAGETABLE_XEN_SLOTS (d) *sizeof (*l2t)); +} +#endif +/* * Local variables: * mode: C
xen/arch/x86/x86_64/compat/mm.c +6 -0
@@ -103,6 +103,9 @@ int compat_arch_memory_op(unsigned long .max_mfn = MACH2PHYS_COMPAT_NR_ENTRIES(d) - 1 }; +if ( !opt_pv32 ) +return -EOPNOTSUPP ; +if (copy_to_guest (arg, &mapping,1 ) ) rc = -EFAULT ;
@@ -115,6 +118,9 @@ int compat_arch_memory_op(unsigned longunsigned long limit;compat_pfn_t last_mfn; +if ( !opt_pv32 ) +return -EOPNOTSUPP ; +if (copy_from_guest (&xmml, arg,1 ) )return -EFAULT ;
xen/arch/x86/x86_64/mm.c +48 -32
133 lines not on this page · raw message
xen/include/asm-x86/config.h +10 -1
@@ -142,7 +142,7 @@ extern unsigned char boot_edid_info[128]* 0xffff82c000000000 - 0xffff82cfffffffff [64GB, 2^36 bytes, PML4:261] * vmap()/ioremap()/fixmap area. * 0xffff82d000000000 - 0xffff82d03fffffff [1GB, 2^30 bytes, PML4:261] -* Compatibility machine-to-phys translation table. +* Compatibility machine-to-phys translation table (CONFIG_PV32). * 0xffff82d040000000 - 0xffff82d07fffffff [1GB, 2^30 bytes, PML4:261] * Xen text, static data, bss. #ifndef CONFIG_BIGMEM
@@ -246,9 +246,18 @@ extern unsigned char boot_edid_info[128]#ifndef __ASSEMBLY__ +#ifdef CONFIG_PV32 +/* This is not a fixed value, just a lower limit. */ #define __HYPERVISOR_COMPAT_VIRT_START 0xF5800000#define HYPERVISOR_COMPAT_VIRT_START (d) ((d)->arch.hv_compat_vstart) + +#else + +#define HYPERVISOR_COMPAT_VIRT_START (d) ((void)(d), 0) + +#endif +#define MACH2PHYS_COMPAT_VIRT_START HYPERVISOR_COMPAT_VIRT_START#define MACH2PHYS_COMPAT_VIRT_END 0xFFE00000#define MACH2PHYS_COMPAT_NR_ENTRIES (d) \
xen/include/asm-x86/domain.h +2 -0
@@ -307,7 +307,9 @@ struct arch_domain {struct page_info *perdomain_l3_pg; +#ifdef CONFIG_PV32 unsigned int hv_compat_vstart; +#endif /* Maximum physical-address bitwidth supported by this guest. */ unsigned int physaddr_bitsize;
xen/include/asm-x86/mm.h +33 -5
@@ -42,8 +42,12 @@#define _PGT_validated PG_shift(6)#define PGT_validated PG_mask(1, 6)/* PAE only: is this an L2 page directory containing Xen-private mappings? */ +#ifdef CONFIG_PV32 #define _PGT_pae_xen_l2 PG_shift(7)#define PGT_pae_xen_l2 PG_mask(1, 7) +#else +#define PGT_pae_xen_l2 0 +#endif /* Has this page been *partially* validated for use as its current type? */ #define _PGT_partial PG_shift(8)#define PGT_partial PG_mask(1, 8)
@@ -494,15 +498,39 @@ extern paddr_t mem_hotplug;#define SHARED_M2P_ENTRY (~0UL - 1UL)#define SHARED_M2P (_e) ((_e) == SHARED_M2P_ENTRY) -#define compat_machine_to_phys_mapping ((unsigned int *)RDWR_COMPAT_MPT_VIRT_START) +#ifdef CONFIG_PV32 + +extern int8_t opt_pv32; + +# define compat_machine_to_phys_mapping ((unsigned int *)RDWR_COMPAT_MPT_VIRT_START) + +# define set_compat_m2p (mfn, entry) \ + ((void)(!opt_pv32 || \ + (mfn) >= (RDWR_COMPAT_MPT_VIRT_END - RDWR_COMPAT_MPT_VIRT_START) / 4 || \ + (compat_machine_to_phys_mapping[mfn] = (entry)))) + +#else /* !CONFIG_PV32 */ + +# define opt_pv32 false + +/* +* Declare the symbol such that (dead) code referencing it can be built +* without a lot of #ifdef-ary, but mark it fully const and don't define +* this symbol anywhere (relying on DCE by the compiler). +*/ +extern const unsigned int *const compat_machine_to_phys_mapping; + +# define set_compat_m2p (mfn, entry) + +#endif /* CONFIG_PV32 */ +#define _set_gpfn_from_mfn (mfn, pfn) ({ \ struct domain *d = page_get_owner(mfn_to_page(_mfn(mfn))); \ unsigned long entry = (d && (d == dom_cow)) ? \ SHARED_M2P_ENTRY : (pfn); \ - ((void)((mfn) >= (RDWR_COMPAT_MPT_VIRT_END - RDWR_COMPAT_MPT_VIRT_START) / 4 || \ - (compat_machine_to_phys_mapping[(mfn)] = (unsigned int)(entry))), \ - machine_to_phys_mapping[(mfn)] = (entry)); \ - }) + set_compat_m2p(mfn, (unsigned int)(entry)); \ + machine_to_phys_mapping[mfn] = (entry); \ +})/* * Disable some users of set_gpfn_from_mfn() (e.g., free_heap_pages()) until
xen/include/asm-x86/pv/domain.h +0 -6
@@ -23,12 +23,6 @@#include <xen/sched.h> -#ifdef CONFIG_PV32 -extern int8_t opt_pv32; -#else -# define opt_pv32 false -#endif -/* * PCID values for the address spaces of 64-bit pv domains: *
Re: [PATCH 2/3] x86: don't maintain compat M2P when !PV32
Quoted 3 lines · Jan Beulich
Note that opt_pv32's declaration / #define need to be moved due to other header dependencies; in particular can
asm-x86/mm.hnot includeasm-x86/pv/domain.h.
While I do appreciate that our headers are a complete tangle, I can't help but feel that this is making the problem worse.
mm.h isn't a better place for opt_pv32 to live. config.h perhaps, seeing as its effects are wider than both the domain support itself, or the memory management support ?
Quoted 7 lines
While touching their definitions anyway, also adjust section placement of
m2p_compat_vstartandcompat_idle_pg_table_l2. Similarly, while puttinginit_xen_pae_l2_slots()inside #ifdef, also move it to a PV-only source file.Suggested-by Andrew Cooper <[email protected]>Signed-off-by Jan Beulich <[email protected]>
So interestingly, this is done out of the order which I was expecting to do things. Its not a problem, but I'd like to double check that we aren't creating future problems.
The goal of this suggestion was actually for PV-Shim, to have only the regular or compat M2P, as they're fairly large structures and adversely affect VM density.
This of course requires the kernel elf file to be parsed earlier during boot, but that isn't a problem. (It also allows for a PV/PVH dom0 usability fix, whereby the Xen command line has to match the ELF image provided, rather than auto-selecting the default when only one option is available.)
The other aspect would be to teach Xen to run on only the compat M2P, which is fine for any shim smaller than 16T. (Honestly, if it weren't an ABI with guests, Shim ought to run exclusively on the compat M2P to reduce the memory overhead.)
Then during boot, the Shim path would chose to construct only the regular or compat M2P, based on bitness of the provided kernel.
Quoted 2 lines
---
An alternative place foropt_pv32.hwould seem to beasm-x86/config.h.
Oh - yes please. I think that would be better overall.
xen/arch/x86/domain.c int arch_domain_create(struct domain *d, · line 603
Quoted 11 lines
xen/arch/x86/domain.c +2
-0@@ -597,8 +597,10 @@ int arch_domain_create(struct domain *d, } d->arch.emulation_flags = emflags; +#ifdef CONFIG_PV32 HYPERVISOR_COMPAT_VIRT_START (d) =is_pv_domain (d) ? __HYPERVISOR_COMPAT_VIRT_START : ~0u ; +#endif
Can we drop HYPERVISOR_COMPAT_VIRT_START() ?
Its use here as an lvalue in particular makes logic especually hard to follow, but all it is actually doing is wrapping the shorter d->arch.hv_compat_vstart
In particular, it would remove the need to conditionally stub HYPERVISOR_COMPAT_VIRT_START() later.
xen/arch/x86/x86_64/mm.c static void destroy_m2p_mapping(struct m · line 321
Quoted 11 lines
xen/arch/x86/x86_64/mm.c +1
-1@@ -315,10 +318,10 @@ static void destroy_m2p_mapping(struct m*/ static int setup_compat_m2p_table (struct mem_hotadd_info *info) { +int err =0 ;unsigned long i, smap, emap, epfn = info->epfn;mfn_t mfn;unsigned int n; -int err =0 ;
Remnants of an earlier change?
xen/include/asm-x86/mm.h line 50
Quoted 12 lines
xen/include/asm-x86/mm.h +4
-0@@ -42,8 +42,12 @@#define _PGT_validated PG_shift(6)#define PGT_validated PG_mask(1, 6)/* PAE only: is this an L2 page directory containing Xen-private mappings? */ +#ifdef CONFIG_PV32 #define _PGT_pae_xen_l2 PG_shift(7)#define PGT_pae_xen_l2 PG_mask(1, 7) +#else +#define PGT_pae_xen_l2 0 +#endif
Hmm - this is going to irritate Coverity and Clang some more. I still need to figure out an effective way to make Coverity not object to this type of short circuiting like this.
I've looked through the users and I think that they're all safe. I do however wonder whether is_guest_l2_slot() can be simplified and have its is_pv_32bit_domain() clause dropped, seeing as it is expensive with its lfences, and the logic ought to only care about PGT_pae_xen_l2 vs PGT_l2_page_table.
Quoted 8 lines
xen/include/asm-x86/mm.h +0
-1/* Has this page been *partially* validated for use as its current type? */ #define _PGT_partial PG_shift(8)#define PGT_partial PG_mask(1, 8)@@ -494,15 +498,39 @@ extern paddr_t mem_hotplug;#define SHARED_M2P_ENTRY (~0UL - 1UL)#define SHARED_M2P (_e) ((_e) == SHARED_M2P_ENTRY) -#define compat_machine_to_phys_mapping ((unsigned int *)RDWR_COMPAT_MPT_VIRT_START)
xen/include/asm-x86/mm.h extern paddr_t mem_hotplug; · line 510
Quoted 10 lines
xen/include/asm-x86/mm.h +10
-0+#ifdef CONFIG_PV32 + +extern int8_t opt_pv32; + +# define compat_machine_to_phys_mapping ((unsigned int *)RDWR_COMPAT_MPT_VIRT_START) + +# define set_compat_m2p (mfn, entry) \ + ((void)(!opt_pv32 || \ + (mfn) >= (RDWR_COMPAT_MPT_VIRT_END - RDWR_COMPAT_MPT_VIRT_START) / 4 || \ + (compat_machine_to_phys_mapping[mfn] = (entry))))
I know this is extracting previous logic, but "entry" would probably be better if it were named "val" or similar.
However, see my reply to patch 3 which I think will simplify this substantially.
~Andrew
Re: [PATCH 2/3] x86: don't maintain compat M2P when !PV32
Quoted 10 lines · Andrew Cooper
Quoted 9 lines · Jan Beulich
Note that opt_pv32's declaration / #define need to be moved due to other header dependencies; in particular can
asm-x86/mm.hnot includeasm-x86/pv/domain.h.While touching their definitions anyway, also adjust section placement of
m2p_compat_vstartandcompat_idle_pg_table_l2. Similarly, while puttinginit_xen_pae_l2_slots()inside #ifdef, also move it to a PV-only source file.
Quoted 6 lines
Quoted 2 lines
Suggested-by Andrew Cooper <[email protected]>Signed-off-by Jan Beulich <[email protected]>So interestingly, this is done out of the order which I was expecting to do things. Its not a problem, but I'd like to double check that we aren't creating future problems.
I've thought about this for quite some time, and didn't see how it would cause problems. And the change here clearly is the more low hanging fruit.
Quoted 3 lines
The goal of this suggestion was actually for PV-Shim, to have only the regular or compat M2P, as they're fairly large structures and adversely affect VM density.
But in particular for {INVALID,SHARED}_M2P_ENTRY there'll need to be some, well, hacks if we want to use the compat one as a replacement for the native one. This will require some more careful thought (at least on my side).
Quoted 5 lines
This of course requires the kernel elf file to be parsed earlier during boot, but that isn't a problem. (It also allows for a PV/PVH dom0 usability fix, whereby the Xen command line has to match the ELF image provided, rather than auto-selecting the default when only one option is available.)
Hmm, no, that's not my current plan, see the cover letter. I've already checked that there are no set_gpfn_from_mfn() uses (except with INVALID_M2P_ENTRY) ahead of Dom0 creation. So instead of moving the parsing earlier, I'm intending to move the setting up of the (right) M2P later. My current take on this is that it'll mainly involve breaking out existing code into its own functions.
Quoted 4 lines
The other aspect would be to teach Xen to run on only the compat M2P, which is fine for any shim smaller than 16T. (Honestly, if it weren't an ABI with guests, Shim ought to run exclusively on the compat M2P to reduce the memory overhead.)
You've covered the shim aspect above, I thought, and the ABI aspect precludes not maintaining the native M2P when there's a 64-bit guest. So I'm not sure what you're trying to suggest here that we may be able to gain.
Quoted 2 lines
Then during boot, the Shim path would chose to construct only the regular or compat M2P, based on bitness of the provided kernel.
That's the plan, yes, but covered higher up.
Quoted 4 lines
Quoted 2 lines
---
An alternative place foropt_pv32.hwould seem to beasm-x86/config.h.Oh - yes please. I think that would be better overall.
Done.
Quoted 13 lines
xen/arch/x86/domain.c int arch_domain_create(struct domain *d, · line 603
Quoted 10 lines
xen/arch/x86/domain.c +2
-0@@ -597,8 +597,10 @@ int arch_domain_create(struct domain *d, } d->arch.emulation_flags = emflags; +#ifdef CONFIG_PV32 HYPERVISOR_COMPAT_VIRT_START (d) =is_pv_domain (d) ? __HYPERVISOR_COMPAT_VIRT_START : ~0u ; +#endif Can we drop
HYPERVISOR_COMPAT_VIRT_START()?
Quoted 6 lines
Its use here as an lvalue in particular makes logic especually hard to follow, but all it is actually doing is wrapping the shorter
d->arch.hv_compat_vstartIn particular, it would remove the need to conditionally stub
HYPERVISOR_COMPAT_VIRT_START()later.
I can do this as a prereq patch, sure, but I'm not convinced as the avoiding of the stub will mean a few new #ifdef-s afaict. Please confirm that you're convinced this will yield the overall better result.
Quoted 5 lines
Quoted 5 lines
xen/arch/x86/x86_64/mm.c +0
-0@@ -315,10 +318,10 @@ static void destroy_m2p_mapping(struct m*/ static int setup_compat_m2p_table (struct mem_hotadd_info *info)
Quoted 8 lines
xen/arch/x86/x86_64/mm.c static void destroy_m2p_mapping(struct m · line 321
Quoted 6 lines
xen/arch/x86/x86_64/mm.c +1
-1{ +int err =0 ;unsigned long i, smap, emap, epfn = info->epfn;mfn_t mfn;unsigned int n; -int err =0 ;Remnants of an earlier change?
Oops.
Quoted 8 lines
Quoted 8 lines
xen/include/asm-x86/mm.h +1
-0@@ -42,8 +42,12 @@#define _PGT_validated PG_shift(6)#define PGT_validated PG_mask(1, 6)/* PAE only: is this an L2 page directory containing Xen-private mappings? */ +#ifdef CONFIG_PV32 #define _PGT_pae_xen_l2 PG_shift(7)
Quoted 8 lines
xen/include/asm-x86/mm.h line 50
Quoted 4 lines
xen/include/asm-x86/mm.h +3
-0#define PGT_pae_xen_l2 PG_mask(1, 7) +#else +#define PGT_pae_xen_l2 0 +#endif Hmm - this is going to irritate Coverity and Clang some more. I still need to figure out an effective way to make Coverity not object to this type of short circuiting like this.
I assume this is just a remark, not implying any action on my part?
Quoted 1 line
I've looked through the users and I think that they're all safe.
I wouldn't have dared make the change without first checking.
Quoted 5 lines
I do
however wonder whetheris_guest_l2_slot()can be simplified and have itsis_pv_32bit_domain()clause dropped, seeing as it is expensive with its lfences, and the logic ought to only care aboutPGT_pae_xen_l2vsPGT_l2_page_table.
Good idea, yes, but that'll be a separate patch.
Quoted 4 lines
Quoted 4 lines
xen/include/asm-x86/mm.h +0
-0@@ -494,15 +498,39 @@ extern paddr_t mem_hotplug;#define SHARED_M2P_ENTRY (~0UL - 1UL)#define SHARED_M2P (_e) ((_e) == SHARED_M2P_ENTRY)
Quoted 14 lines
xen/include/asm-x86/mm.h extern paddr_t mem_hotplug; · line 510
Quoted 11 lines
xen/include/asm-x86/mm.h +10
-1-#define compat_machine_to_phys_mapping ((unsigned int *)RDWR_COMPAT_MPT_VIRT_START) +#ifdef CONFIG_PV32 + +extern int8_t opt_pv32; + +# define compat_machine_to_phys_mapping ((unsigned int *)RDWR_COMPAT_MPT_VIRT_START) + +# define set_compat_m2p (mfn, entry) \ + ((void)(!opt_pv32 || \ + (mfn) >= (RDWR_COMPAT_MPT_VIRT_END - RDWR_COMPAT_MPT_VIRT_START) / 4 || \ + (compat_machine_to_phys_mapping[mfn] = (entry))))I know this is extracting previous logic, but "entry" would probably be better if it were named "val" or similar.
I was wondering myself, but didn't consider val or alike meaningfully better. As it looks you do, I'll switch.
Quoted 2 lines
However, see my reply to patch 3 which I think will simplify this substantially.
Neither my inbox nor the list archives have such a reply, so I can only assume this is yet to be sent.
Jan
Re: [PATCH 2/3] x86: don't maintain compat M2P when !PV32
Quoted 7 lines · Jan Beulich
Quoted 4 lines · Andrew Cooper
Quoted 1 line · Jan Beulich
An alternative place for
opt_pv32.hwould seem to beasm-x86/config.h.Oh - yes please. I think that would be better overall.
Done.
Now that I'm trying to build the result: There's an immediate downside - we can't use int8_t in config.h, so I'll have to switch to using signed char instead.
Jan
Re: [PATCH 2/3] x86: don't maintain compat M2P when !PV32
In reply to Jan BeulichQuoted 22 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper
Quoted 9 lines · Jan Beulich
Note that opt_pv32's declaration / #define need to be moved due to other header dependencies; in particular can
asm-x86/mm.hnot includeasm-x86/pv/domain.h.While touching their definitions anyway, also adjust section placement of
m2p_compat_vstartandcompat_idle_pg_table_l2. Similarly, while puttinginit_xen_pae_l2_slots()inside #ifdef, also move it to a PV-only source file.Quoted 6 lines
Quoted 2 lines
Suggested-by Andrew Cooper <[email protected]>Signed-off-by Jan Beulich <[email protected]>So interestingly, this is done out of the order which I was expecting to do things. Its not a problem, but I'd like to double check that we aren't creating future problems.
I've thought about this for quite some time, and didn't see how it would cause problems. And the change here clearly is the more low hanging fruit.
Quoted 8 lines
Quoted 3 lines
The goal of this suggestion was actually for PV-Shim, to have only the regular or compat M2P, as they're fairly large structures and adversely affect VM density.
But in particular for {INVALID,SHARED}_M2P_ENTRY there'll need to be some, well, hacks if we want to use the compat one as a replacement for the native one. This will require some more careful thought (at least on my side).
Having looked into this some more, I'm still unsure whether this is a viable thing to do. While we do have VALID_M2P() (checking the top bit of the entry), its use is rather limited. The most noteworthy place (but by far not the only one) where it's _not_ used is perhaps the handling of MMU_MACHPHYS_UPDATE. Additionally there's no similar checking of bit 31 for 32-bit guests at all. Hence at a first approximation both (uint32_t)INVALID_P2M_ENTRY and (uint32_t)SHARED_P2M_ENTRY are to be considered valid GFNs (albeit they wouldn't have been on a 32-bit Xen, but those were similarly lacking enforcement of this restriction anyway).
Jan
PATCH 3/3 x86: don't override INVALID_M2P_ENTRY with SHARED_M2P_ENTRY
In reply to Jan BeulichWhile in most cases code ahead of the invocation of set_gpfn_from_mfn() deals with shared pages, at least in set_typed_p2m_entry() I can't spot such handling (it's entirely possible there's code missing there). Let's try to play safe and add an extra check.
xen/include/asm-x86/mm.h +8 -3
@@ -525,9 +525,14 @@ extern const unsigned int *const compat_#endif /* CONFIG_PV32 */ #define _set_gpfn_from_mfn (mfn, pfn) ({ \ - struct domain *d = page_get_owner(mfn_to_page(_mfn(mfn))); \ - unsigned long entry = (d && (d == dom_cow)) ? \ - SHARED_M2P_ENTRY : (pfn); \ + unsigned long entry = (pfn); \ + if ( entry != INVALID_M2P_ENTRY ) \ + { \ + const struct domain *d; \ + d = page_get_owner(mfn_to_page(_mfn(mfn))); \ + if ( d && (d == dom_cow) ) \ + entry = SHARED_M2P_ENTRY; \ + } \ set_compat_m2p(mfn, (unsigned int)(entry)); \ machine_to_phys_mapping[mfn] = (entry); \ })
Re: [PATCH 3/3] x86: don't override INVALID_M2P_ENTRY with SHARED_M2P_ENTRY
Quoted 18 lines · Jan Beulich
While in most cases code ahead of the invocation of
set_gpfn_from_mfn()deals with shared pages, at least inset_typed_p2m_entry()I can't spot such handling (it's entirely possible there's code missing there). Let's try to play safe and add an extra check.Signed-off-by Jan Beulich <[email protected]>xen/include/asm-x86/mm.h +1
-3@@ -525,9 +525,14 @@ extern const unsigned int *const compat_#endif /* CONFIG_PV32 */ #define _set_gpfn_from_mfn (mfn, pfn) ({ \ - struct domain *d = page_get_owner(mfn_to_page(_mfn(mfn))); \ - unsigned long entry = (d && (d == dom_cow)) ? \ - SHARED_M2P_ENTRY : (pfn); \ + unsigned long entry = (pfn); \
xen/include/asm-x86/mm.h extern const unsigned int *const compat_ · line 538
Quoted 10 lines
xen/include/asm-x86/mm.h +7
-0+if ( entry !=INVALID_M2P_ENTRY ) \ + { \ +const struct domain *d; \ + d =page_get_owner (mfn_to_page (_mfn (mfn))); \ +if ( d && (d == dom_cow) ) \ + entry =SHARED_M2P_ENTRY ; \ + } \set_compat_m2p (mfn, (unsigned int )(entry)); \ machine_to_phys_mapping[mfn] = (entry); \ })
Hmm - we already have a lot of callers, and this is already too complicated to be a define.
We have x86 which uses M2P, and ARM which doesn't. We have two more architectures on the way which probably won't want M2P, and certainly won't in the beginning.
Can we introduce CONFIG_M2P which is selected by x86, rename this infrastructure to set_m2p() or something, provide a no-op fallback in common code, and move this implementation into x86/mm.c ?
In particular, silently clobbering pfn to SHARED_M2P_ENTRY is rude behaviour. It would be better to ASSERT() the right one is passed in, which also simplifies release builds.
~Andrew
Re: [PATCH 3/3] x86: don't override INVALID_M2P_ENTRY with SHARED_M2P_ENTRY
Quoted 24 lines · Andrew Cooper
Quoted 23 lines · Jan Beulich
While in most cases code ahead of the invocation of
set_gpfn_from_mfn()deals with shared pages, at least inset_typed_p2m_entry()I can't spot such handling (it's entirely possible there's code missing there). Let's try to play safe and add an extra check.Signed-off-by Jan Beulich <[email protected]>xen/include/asm-x86/mm.h +7
-3@@ -525,9 +525,14 @@ extern const unsigned int *const compat_#endif /* CONFIG_PV32 */ #define _set_gpfn_from_mfn (mfn, pfn) ({ \ - struct domain *d = page_get_owner(mfn_to_page(_mfn(mfn))); \ - unsigned long entry = (d && (d == dom_cow)) ? \ - SHARED_M2P_ENTRY : (pfn); \ + unsigned long entry = (pfn); \ + if ( entry != INVALID_M2P_ENTRY ) \ + { \ + const struct domain *d; \ + d = page_get_owner(mfn_to_page(_mfn(mfn))); \ + if ( d && (d == dom_cow) ) \ + entry = SHARED_M2P_ENTRY; \
Quoted 8 lines
xen/include/asm-x86/mm.h extern const unsigned int *const compat_ · line 538
Quoted 5 lines
xen/include/asm-x86/mm.h +1
-0+ } \set_compat_m2p (mfn, (unsigned int )(entry)); \ machine_to_phys_mapping[mfn] = (entry); \ })Hmm - we already have a lot of callers, and this is already too complicated to be a define.
I did consider moving this into an out-of-line function, yes.
Quoted 7 lines
We have x86 which uses M2P, and ARM which doesn't. We have two more architectures on the way which probably won't want M2P, and certainly won't in the beginning.
Can we introduce
CONFIG_M2Pwhich is selected by x86, rename this infrastructure toset_m2p()or something, provide a no-op fallback in common code, and move this implementation intox86/mm.c?
We can, sure. Question is whether this isn't more scope creep than is acceptable considering the purpose of this change.
Quoted 3 lines
In particular, silently clobbering pfn to
SHARED_M2P_ENTRYis rude behaviour. It would be better toASSERT()the right one is passed in, which also simplifies release builds.
Now this is, irrespective of me agreeing with the point you make, a change I'm not going to make: There's no way I could guarantee I wouldn't break mem-sharing. A change like this can imo only possibly be done by someone actively working on and with mem-sharing.
Jan