runstate/time area registration by (guest) physical address
v2 cover runstate/time area registration by (guest) physical address
Since it was indicated that introducing specific new vCPU ops may be beneficial independent of the introduction of a fully physical- address-based ABI flavor, here we go. There continue to be a number of open questions throughout the series, resolving of which is one of the main goals of this v2 posting.
1: domain: GADDR based shared guest area registration alternative - cleanup 3: domain: update GADDR based runstate guest area
4: x86: update GADDR based secondary time area
5: x86/mem-sharing: copy GADDR based shared guest areas
6: domain: map/unmap GADDR based shared guest areas
7: domain: introduce GADDR based runstate area registration alternative 8: x86: introduce GADDR based secondary time area registration alternative 9: common: convert vCPU info area registration
Jan
RFC PATCH v2 1/8 domain: GADDR based shared guest area registration alternative - teardown
In preparation of the introduction of new vCPU operations allowing to register the respective areas (one of the two is x86-specific) by guest-physical address, add the necessary domain cleanup hooks.
---
RFC: Zapping the areas in pv_shim_shutdown() may not be strictly
necessary: Aiui unmap_vcpu_info() is called only because the vCPU info area cannot be re-registered. Beyond that I guess the assumption is that the areas would only be re-registered as they were before. If that's not the case I wonder whether the guest handles for both areas shouldn't also be zapped.
---
v2: Add assertion in unmap_guest_area().
xen/arch/x86/domain.c +5 -0
@@ -1014,7 +1014,10 @@ int arch_domain_soft_reset(struct domain } for_each_vcpu ( d, v ) + {set_xen_guest_handle (v->arch.time_info_guest,NULL ); +unmap_guest_area (v, &v->arch.time_guest_area); + } exit_put_gfn:put_gfn (d,gfn_x (gfn));
@@ -2329,6 +2332,8 @@ int domain_relinquish_resources(struct dif ( ret )return ret; +unmap_guest_area (v, &v->arch.time_guest_area); +vpmu_destroy (v); }
xen/arch/x86/include/asm/domain.h +1 -0
@@ -658,6 +658,7 @@ struct arch_vcpu/* A secondary copy of the vcpu time info. */ XEN_GUEST_HANDLE (vcpu_time_info_t) time_info_guest; +struct guest_area time_guest_area;struct arch_vm_event *vm_event;
xen/arch/x86/pv/shim.c +3 -1
@@ -394,8 +394,10 @@ int pv_shim_shutdown(uint8_t reason) for_each_vcpu ( d, v ) { -/* Unmap guest vcpu_info pages. */ +/* Unmap guest vcpu_info page and runstate/time areas. */ unmap_vcpu_info (v); +unmap_guest_area (v, &v->runstate_guest_area); +unmap_guest_area (v, &v->arch.time_guest_area);/* Reset the periodic timer to the default value. */ vcpu_set_periodic_timer (v,MILLISECS (10 ));
xen/common/domain.c +17 -0
@@ -963,7 +963,10 @@ int domain_kill(struct domain *d)if (cpupool_move_domain (d, cpupool0) )return -ERESTART ; for_each_vcpu ( d, v ) + {unmap_vcpu_info (v); +unmap_guest_area (v, &v->runstate_guest_area); + } d->is_dying = DOMDYING_dead;/* Mem event cleanup has to go here because the rings * have to be put before we call put_domain. */
@@ -1417,6 +1420,7 @@ int domain_soft_reset(struct domain *d, {set_xen_guest_handle (runstate_guest (v),NULL );unmap_vcpu_info (v); +unmap_guest_area (v, &v->runstate_guest_area); } rc =arch_domain_soft_reset (d);
@@ -1568,6 +1572,19 @@ void unmap_vcpu_info(struct vcpu *v)put_page_and_type (mfn_to_page (mfn)); } +/* +* This is only intended to be used for domain cleanup (or more generally only +* with at least the respective vCPU, if it's not the current one, reliably +* paused). +*/ +void unmap_guest_area (struct vcpu *v,struct guest_area *area) +{ +struct domain *d = v->domain; + +if ( v != current ) +ASSERT (atomic_read (&v->pause_count) |atomic_read (&d->pause_count)); +} +int default_initialise_vcpu (struct vcpu *v,XEN_GUEST_HANDLE_PARAM (void ) arg) {struct vcpu_guest_context *ctxt;
xen/include/xen/domain.h +11 -0
@@ -5,6 +5,12 @@#include <xen/types.h> #include <public/xen.h> + +struct guest_area { +struct page_info *pg; +void *map; +}; +#include <asm/domain.h> #include <asm/numa.h>
@@ -76,6 +82,11 @@ void arch_vcpu_destroy(struct vcpu *v);int map_vcpu_info (struct vcpu *v,unsigned long gfn,unsigned int offset);void unmap_vcpu_info (struct vcpu *v); +int map_guest_area (struct vcpu *v,paddr_t gaddr,unsigned int size, +struct guest_area *area, +void (*populate)(void *dst,struct vcpu *v)); +void unmap_guest_area (struct vcpu *v,struct guest_area *area); +int arch_domain_create (struct domain *d,struct xen_domctl_createdomain *config,unsigned int flags);
xen/include/xen/sched.h +1 -0
@@ -202,6 +202,7 @@ struct vcpu XEN_GUEST_HANDLE(vcpu_runstate_info_compat_t) compat; } runstate_guest;/* guest address */ #endif +struct guest_area runstate_guest_area;unsigned int new_state;/* Has the FPU been initialised? */
RFC PATCH v2 2/8 domain: update GADDR based runstate guest area
In reply to Jan BeulichBefore adding a new vCPU operation to register the runstate area by guest-physical address, add code to actually keep such areas up-to-date.
Note that updating of the area will be done exclusively following the model enabled by VMASST_TYPE_runstate_update_flag for virtual-address based registered areas.
Note further that pages aren't marked dirty when written to (matching the handling of space mapped by map_vcpu_info()), on the basis that the registrations are lost anyway across migration (or would need re- populating at the target for transparent migration). Plus the contents of the areas in question have to be deemed volatile in the first place (so saving a "most recent" value is pretty meaningless even for e.g. snapshotting).
---
RFC: HVM guests (on x86) can change bitness and hence layout (and size!
and alignment) of the runstate area. I don't think it is an option to require 32-bit code to pass a range such that even the 64-bit layout wouldn't cross a page boundary (and be suitably aligned). I also don't see any other good solution, so for now a crude approach with an extra boolean is used (using has_32bit_shinfo() isn't race free and could hence lead to overrunning the mapped space).
---
v2: Drop VM-assist conditionals.
xen/common/domain.c +41 -2
@@ -1616,14 +1616,53 @@ bool update_runstate_area(struct vcpu *vstruct guest_memory_policy policy = { };void __user *guest_handle =NULL ;struct vcpu_runstate_info runstate; +struct vcpu_runstate_info *map = v->runstate_guest_area.map; + +memcpy (&runstate, &v->runstate,sizeof (runstate)); + +if ( map ) + { +uint64_t *pset; +#ifdef CONFIG_COMPAT +struct compat_vcpu_runstate_info *cmap =NULL ; + +if ( v->runstate_guest_area_compat ) + cmap = (void *)map; +#endif + +/* +* NB: No VM_ASSIST(v->domain, runstate_update_flag) check here. +* Always using that updating model. +*/ +#ifdef CONFIG_COMPAT +if ( cmap ) + pset = &cmap->state_entry_time; + else +#endif + pset = &map->state_entry_time; + runstate.state_entry_time |=XEN_RUNSTATE_UPDATE ; +write_atomic (pset, runstate.state_entry_time); +smp_wmb (); + +#ifdef CONFIG_COMPAT +if ( cmap ) +XLAT_vcpu_runstate_info (cmap, &runstate); + else +#endif + *map = runstate; + +smp_wmb (); + runstate.state_entry_time &= ~XEN_RUNSTATE_UPDATE ; +write_atomic (pset, runstate.state_entry_time); + +return true; + }if (guest_handle_is_null (runstate_guest (v)) )return true;update_guest_memory_policy (v, &policy); -memcpy (&runstate, &v->runstate,sizeof (runstate)); -if (VM_ASSIST (v->domain, runstate_update_flag) ) {#ifdef CONFIG_COMPAT
xen/include/xen/sched.h +2 -0
@@ -231,6 +231,8 @@ struct vcpu#ifdef CONFIG_COMPAT /* A hypercall is using the compat ABI? */ bool hcall_compat; +/* Physical runstate area registered via compat ABI? */ +bool runstate_guest_area_compat;#endif #ifdef CONFIG_IOREQ_SERVER
PATCH v2 3/8 x86: update GADDR based secondary time area
In reply to Jan BeulichBefore adding a new vCPU operation to register the secondary time area by guest-physical address, add code to actually keep such areas up-to- date.
Note that pages aren't marked dirty when written to (matching the handling of space mapped by map_vcpu_info()), on the basis that the registrations are lost anyway across migration (or would need re- populating at the target for transparent migration). Plus the contents of the areas in question have to be deemed volatile in the first place (so saving a "most recent" value is pretty meaningless even for e.g. snapshotting).
xen/arch/x86/time.c +22 -0
@@ -1462,12 +1462,34 @@ static void __update_vcpu_system_time(st v->arch.pv.pending_system_time = _u; } +static void write_time_guest_area (struct vcpu_time_info *map, +const struct vcpu_time_info *src) +{ +/* 1. Update userspace version. */ +write_atomic (&map->version, src->version); +smp_wmb (); + +/* 2. Update all other userspace fields. */ + *map = *src; + +/* 3. Update userspace version again. */ +smp_wmb (); +write_atomic (&map->version,version_update_end (src->version)); +} +bool update_secondary_system_time (struct vcpu *v,struct vcpu_time_info *u) {XEN_GUEST_HANDLE (vcpu_time_info_t) user_u = v->arch.time_info_guest; +struct vcpu_time_info *map = v->arch.time_guest_area.map;struct guest_memory_policy policy = { .nested_guest_mode = false }; +if ( map ) + { +write_time_guest_area (map, u); +return true; + } +if (guest_handle_is_null (user_u) )return true;
PATCH v2 4/8 x86/mem-sharing: copy GADDR based shared guest areas
In reply to Jan BeulichIn preparation of the introduction of new vCPU operations allowing to register the respective areas (one of the two is x86-specific) by guest-physical address, add the necessary fork handling (with the backing function yet to be filled in).
xen/arch/x86/mm/mem_sharing.c +72 -0
@@ -1653,6 +1653,65 @@ static void copy_vcpu_nonreg_state(struchvm_set_nonreg_state (cd_vcpu, &nrs); } +static int copy_guest_area (struct guest_area *cd_area, +const struct guest_area *d_area, +struct vcpu *cd_vcpu, +const struct domain *d) +{ +mfn_t d_mfn, cd_mfn; + +if ( !d_area->pg ) +return 0 ; + + d_mfn =page_to_mfn (d_area->pg); + +/* Allocate & map a page for the area if it hasn't been already. */ +if ( !cd_area->pg ) + { +gfn_t gfn =mfn_to_gfn (d, d_mfn); +struct p2m_domain *p2m =p2m_get_hostp2m (cd_vcpu->domain); +p2m_type_t p2mt; +p2m_access_t p2ma; +unsigned int offset; +int ret; + + cd_mfn = p2m->get_entry (p2m, gfn, &p2mt, &p2ma,0 ,NULL ,NULL ); +if (mfn_eq (cd_mfn,INVALID_MFN ) ) + { +struct page_info *pg =alloc_domheap_page (cd_vcpu->domain,0 ); + +if ( !pg ) +return -ENOMEM ; + + cd_mfn =page_to_mfn (pg); +set_gpfn_from_mfn (mfn_x (cd_mfn),gfn_x (gfn)); + + ret = p2m->set_entry (p2m, gfn, cd_mfn,PAGE_ORDER_4K , p2m_ram_rw, + p2m->default_access,-1 ); +if ( ret ) +return ret; + } +else if ( p2mt != p2m_ram_rw ) +return -EBUSY ; + +/* +* Simply specify the entire range up to the end of the page. All the +* function uses it for is a check for not crossing page boundaries. +*/ + offset =PAGE_OFFSET (d_area->map); + ret =map_guest_area (cd_vcpu,gfn_to_gaddr (gfn) + offset, +PAGE_SIZE - offset, cd_area,NULL ); +if ( ret ) +return ret; + } +else + cd_mfn =page_to_mfn (cd_area->pg); + +copy_domain_page (cd_mfn, d_mfn); + +return 0 ; +} +static int copy_vpmu (struct vcpu *d_vcpu,struct vcpu *cd_vcpu) {struct vpmu_struct *d_vpmu =vcpu_vpmu (d_vcpu);
@@ -1745,6 +1804,16 @@ static int copy_vcpu_settings(struct domcopy_domain_page (new_vcpu_info_mfn, vcpu_info_mfn); } +/* Same for the (physically registered) runstate and time info areas. */ + ret =copy_guest_area (&cd_vcpu->runstate_guest_area, + &d_vcpu->runstate_guest_area, cd_vcpu, d); +if ( ret ) +return ret; + ret =copy_guest_area (&cd_vcpu->arch.time_guest_area, + &d_vcpu->arch.time_guest_area, cd_vcpu, d); +if ( ret ) +return ret; + ret =copy_vpmu (d_vcpu, cd_vcpu);if ( ret )return ret;
@@ -1987,7 +2056,10 @@ int mem_sharing_fork_reset(struct domain state:if ( reset_state ) + { rc =copy_settings (d, pd); +/* TBD: What to do here with -ERESTART? */ + }domain_unpause (d);
xen/common/domain.c +7 -0
@@ -1572,6 +1572,13 @@ void unmap_vcpu_info(struct vcpu *v)put_page_and_type (mfn_to_page (mfn)); } +int map_guest_area (struct vcpu *v,paddr_t gaddr,unsigned int size, +struct guest_area *area, +void (*populate)(void *dst,struct vcpu *v)) +{ +return -EOPNOTSUPP ; +} +/* * This is only intended to be used for domain cleanup (or more generally only * with at least the respective vCPU, if it's not the current one, reliably
Re: [PATCH v2 4/8] x86/mem-sharing: copy GADDR based shared guest areas
Quoted 30 lines · Jan Beulich
In preparation of the introduction of new vCPU operations allowing to register the respective areas (one of the two is x86-specific) by guest-physical address, add the necessary fork handling (with the backing function yet to be filled in).
Signed-off-by Jan Beulich <[email protected]>xen/arch/x86/mm/mem_sharing.c +16
-0@@ -1653,6 +1653,65 @@ static void copy_vcpu_nonreg_state(struchvm_set_nonreg_state (cd_vcpu, &nrs); } +static int copy_guest_area (struct guest_area *cd_area, +const struct guest_area *d_area, +struct vcpu *cd_vcpu, +const struct domain *d) +{ +mfn_t d_mfn, cd_mfn; + +if ( !d_area->pg ) +return 0 ; + + d_mfn =page_to_mfn (d_area->pg); + +/* Allocate & map a page for the area if it hasn't been already. */ +if ( !cd_area->pg ) + { +gfn_t gfn =mfn_to_gfn (d, d_mfn);
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1681
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10
-0+struct p2m_domain *p2m =p2m_get_hostp2m (cd_vcpu->domain); +p2m_type_t p2mt; +p2m_access_t p2ma; +unsigned int offset; +int ret; + + cd_mfn = p2m->get_entry (p2m, gfn, &p2mt, &p2ma,0 ,NULL ,NULL ); +if (mfn_eq (cd_mfn,INVALID_MFN ) ) + { + struct page_info *pg = alloc_domheap_page(cd_vcpu->domain,
0);
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8
-0+ +if ( !pg ) +return -ENOMEM ; + + cd_mfn =page_to_mfn (pg); +set_gpfn_from_mfn (mfn_x (cd_mfn),gfn_x (gfn)); + + ret = p2m->set_entry(p2m, gfn, cd_mfn, PAGE_ORDER_4K,
p2m_ram_rw,
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+ p2m->default_access, -1); +if ( ret ) +return ret; + } +else if ( p2mt != p2m_ram_rw ) +return -EBUSY ; + +/* +* Simply specify the entire range up to the end of the page.
All the
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1699
Quoted 1 line
xen/arch/x86/mm/mem_sharing.c +1
-0+* function uses it for is a check for not crossing page
boundaries.
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1708
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+*/ + offset =PAGE_OFFSET (d_area->map); + ret =map_guest_area (cd_vcpu,gfn_to_gaddr (gfn) + offset, +PAGE_SIZE - offset, cd_area,NULL ); +if ( ret ) +return ret; + } +else + cd_mfn =page_to_mfn (cd_area->pg);
Everything to this point seems to be non mem-sharing/forking related. Could these live somewhere else? There must be some other place where allocating these areas happens already for non-fork VMs so it would make sense to just refactor that code to be callable from here.
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +6
-0+ +copy_domain_page (cd_mfn, d_mfn); + +return 0 ; +} +static int copy_vpmu (struct vcpu *d_vcpu,struct vcpu *cd_vcpu) {struct vpmu_struct *d_vpmu =vcpu_vpmu (d_vcpu);
xen/arch/x86/mm/mem_sharing.c static int copy_vcpu_settings(struct dom · line 1807
Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +1
-0@@ -1745,6 +1804,16 @@ static int copy_vcpu_settings(struct domcopy_domain_page (new_vcpu_info_mfn, vcpu_info_mfn); } +/* Same for the (physically registered) runstate and time info
areas. */
Quoted 13 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+ ret =copy_guest_area (&cd_vcpu->runstate_guest_area, + &d_vcpu->runstate_guest_area, cd_vcpu, d); +if ( ret ) +return ret; + ret =copy_guest_area (&cd_vcpu->arch.time_guest_area, + &d_vcpu->arch.time_guest_area, cd_vcpu, d); +if ( ret ) +return ret; + ret =copy_vpmu (d_vcpu, cd_vcpu);if ( ret )return ret;
xen/arch/x86/mm/mem_sharing.c int mem_sharing_fork_reset(struct domain · line 2061
Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c +2
-0@@ -1987,7 +2056,10 @@ int mem_sharing_fork_reset(struct domain state:if ( reset_state ) + { rc =copy_settings (d, pd); +/* TBD: What to do here with -ERESTART? */
Where does ERESTART coming from?
Re: [PATCH v2 4/8] x86/mem-sharing: copy GADDR based shared guest areas
Quoted 65 lines · Tamas K Lengyel
Quoted 22 lines · Jan Beulich
xen/arch/x86/mm/mem_sharing.c +16
-0@@ -1653,6 +1653,65 @@ static void copy_vcpu_nonreg_state(struchvm_set_nonreg_state (cd_vcpu, &nrs); } +static int copy_guest_area (struct guest_area *cd_area, +const struct guest_area *d_area, +struct vcpu *cd_vcpu, +const struct domain *d) +{ +mfn_t d_mfn, cd_mfn; + +if ( !d_area->pg ) +return 0 ; + + d_mfn =page_to_mfn (d_area->pg); + +/* Allocate & map a page for the area if it hasn't been already. */ +if ( !cd_area->pg ) + { +gfn_t gfn =mfn_to_gfn (d, d_mfn);xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1681
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10
-0+struct p2m_domain *p2m =p2m_get_hostp2m (cd_vcpu->domain); +p2m_type_t p2mt; +p2m_access_t p2ma; +unsigned int offset; +int ret; + + cd_mfn = p2m->get_entry (p2m, gfn, &p2mt, &p2ma,0 ,NULL ,NULL ); +if (mfn_eq (cd_mfn,INVALID_MFN ) ) + { + struct page_info *pg = alloc_domheap_page(cd_vcpu->domain,0);
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8
-0+ +if ( !pg ) +return -ENOMEM ; + + cd_mfn =page_to_mfn (pg); +set_gpfn_from_mfn (mfn_x (cd_mfn),gfn_x (gfn)); + + ret = p2m->set_entry(p2m, gfn, cd_mfn, PAGE_ORDER_4K,
p2m_ram_rw,xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+ p2m->default_access, -1); +if ( ret ) +return ret; + } +else if ( p2mt != p2m_ram_rw ) +return -EBUSY ; + +/* +* Simply specify the entire range up to the end of the page. All the
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1699
Quoted 1 line
xen/arch/x86/mm/mem_sharing.c +1
-0+* function uses it for is a check for not crossing page boundaries.
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1708
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+*/ + offset =PAGE_OFFSET (d_area->map); + ret =map_guest_area (cd_vcpu,gfn_to_gaddr (gfn) + offset, +PAGE_SIZE - offset, cd_area,NULL ); +if ( ret ) +return ret; + } +else + cd_mfn =page_to_mfn (cd_area->pg);
Quoted 4 lines
Everything to this point seems to be non mem-sharing/forking related. Could these live somewhere else? There must be some other place where allocating these areas happens already for non-fork VMs so it would make sense to just refactor that code to be callable from here.
It is the "copy" aspect with makes this mem-sharing (or really fork) specific. Plus in the end this is no different from what you have there right now for copying the vCPU info area. In the final patch that other code gets removed by re-using the code here.
I also haven't been able to spot anything that could be factored out (and one might expect that if there was something, then the vCPU info area copying should also already have used it). map_guest_area() is all that is used for other purposes as well.
Quoted 29 lines
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +6
-0+ +copy_domain_page (cd_mfn, d_mfn); + +return 0 ; +} +static int copy_vpmu (struct vcpu *d_vcpu,struct vcpu *cd_vcpu) {struct vpmu_struct *d_vpmu =vcpu_vpmu (d_vcpu);xen/arch/x86/mm/mem_sharing.c static int copy_vcpu_settings(struct dom · line 1807
Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +1
-0@@ -1745,6 +1804,16 @@ static int copy_vcpu_settings(struct domcopy_domain_page (new_vcpu_info_mfn, vcpu_info_mfn); } +/* Same for the (physically registered) runstate and time info areas. */
Quoted 14 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+ ret =copy_guest_area (&cd_vcpu->runstate_guest_area, + &d_vcpu->runstate_guest_area, cd_vcpu, d); +if ( ret ) +return ret; + ret =copy_guest_area (&cd_vcpu->arch.time_guest_area, + &d_vcpu->arch.time_guest_area, cd_vcpu, d); +if ( ret ) +return ret; + ret =copy_vpmu (d_vcpu, cd_vcpu);if ( ret )return ret;@@ -1987,7 +2056,10 @@ int mem_sharing_fork_reset(struct domain
Quoted 7 lines
xen/arch/x86/mm/mem_sharing.c int mem_sharing_fork_reset(struct domain · line 2061
Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c +2
-0state:if ( reset_state ) + { rc =copy_settings (d, pd); +/* TBD: What to do here with -ERESTART? */ Where does ERESTART coming from?
From map_guest_area()'s attempt to acquire the hypercall deadlock mutex, in order to then pause the subject vCPU. I suppose that in the forking case it may already be paused, but then there's no way map_guest_area() could know. Looking at the pause count is fragile, as there's no guarantee that the vCPU may be unpaused while we're still doing work on it. Hence I view such checks as only suitable for assertions.
Jan
Re: [PATCH v2 4/8] x86/mem-sharing: copy GADDR based shared guest areas
Quoted 9 lines · Jan Beulich
Quoted 7 lines · Tamas K Lengyel
Quoted 6 lines · Jan Beulich
xen/arch/x86/mm/mem_sharing.c +0
-0@@ -1653,6 +1653,65 @@ static void copy_vcpu_nonreg_state(struchvm_set_nonreg_state (cd_vcpu, &nrs); }
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c +5
-0+static int copy_guest_area (struct guest_area *cd_area, +const struct guest_area *d_area, +struct vcpu *cd_vcpu, +const struct domain *d) +{Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8
-0+mfn_t d_mfn, cd_mfn; + +if ( !d_area->pg ) +return 0 ; + + d_mfn =page_to_mfn (d_area->pg); + +/* Allocate & map a page for the area if it hasn't been already.
*/
Quoted 10 lines
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10
-0+if ( !cd_area->pg ) + { +gfn_t gfn =mfn_to_gfn (d, d_mfn); +struct p2m_domain *p2m =p2m_get_hostp2m (cd_vcpu->domain); +p2m_type_t p2mt; +p2m_access_t p2ma; +unsigned int offset; +int ret; + + cd_mfn = p2m->get_entry(p2m, gfn, &p2mt, &p2ma, 0, NULL,
NULL);
Quoted 35 lines
Quoted 35 lines
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 3 lines
xen/arch/x86/mm/mem_sharing.c +3
-0+if (mfn_eq (cd_mfn,INVALID_MFN ) ) + { + struct page_info *pg = alloc_domheap_page(cd_vcpu->domain,0);
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8
-0+ +if ( !pg ) +return -ENOMEM ; + + cd_mfn =page_to_mfn (pg); +set_gpfn_from_mfn (mfn_x (cd_mfn),gfn_x (gfn)); + + ret = p2m->set_entry(p2m, gfn, cd_mfn, PAGE_ORDER_4K,
p2m_ram_rw,xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+ p2m->default_access, -1); +if ( ret ) +return ret; + } +else if ( p2mt != p2m_ram_rw ) +return -EBUSY ; + +/* +* Simply specify the entire range up to the end of the page. All the
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1699
Quoted 1 line
xen/arch/x86/mm/mem_sharing.c +1
-0+* function uses it for is a check for not crossing page boundaries.
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+*/ + offset =PAGE_OFFSET (d_area->map); + ret =map_guest_area (cd_vcpu,gfn_to_gaddr (gfn) + offset, +PAGE_SIZE - offset, cd_area,NULL ); +if ( ret ) +return ret; + } +else + cd_mfn =page_to_mfn (cd_area->pg);
Quoted 1 line
Quoted 1 line
Everything to this point seems to be non mem-sharing/forking related.
Could
Quoted 1 line
Quoted 1 line
these live somewhere else? There must be some other place where
allocating
Quoted 1 line
Quoted 1 line
these areas happens already for non-fork VMs so it would make sense to
just
Quoted 6 lines
Quoted 1 line
refactor that code to be callable from here.
It is the "copy" aspect with makes this mem-sharing (or really fork) specific. Plus in the end this is no different from what you have there right now for copying the vCPU info area. In the final patch that other code gets removed by re-using the code here.
Yes, the copy part is fork-specific. Arguably if there was a way to do the allocation of the page for vcpu_info I would prefer that being elsewhere, but while the only requirement is allocate-page and copy from parent I'm OK with that logic being in here because it's really straight forward. But now you also do extra sanity checks here which are harder to comprehend in this context alone. What if extra sanity checks will be needed in the future? Or the sanity checks in the future diverge from where this happens for normal VMs because someone overlooks this needing to be synched here too?
Quoted 4 lines
I also haven't been able to spot anything that could be factored out (and one might expect that if there was something, then the vCPU info area copying should also already have used it).
map_guest_area()is all that is used for other purposes as well.
Well, there must be a location where all this happens for normal VMs as well, no? Why not factor that code so that it can be called from here, so that we don't have to track sanity check requirements in two different locations? Or for normal VMs that sanity checking bit isn't required? If so, why?
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
xen/arch/x86/mm/mem_sharing.c +6
-0+ +copy_domain_page (cd_mfn, d_mfn); + +return 0 ; +} +static int copy_vpmu (struct vcpu *d_vcpu,struct vcpu *cd_vcpu) {struct vpmu_struct *d_vpmu =vcpu_vpmu (d_vcpu);@@ -1745,6 +1804,16 @@ static int copy_vcpu_settings(struct domcopy_domain_page (new_vcpu_info_mfn, vcpu_info_mfn); }
Quoted 4 lines
Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c static int copy_vcpu_settings(struct dom · line 1807
Quoted 1 line
xen/arch/x86/mm/mem_sharing.c +1
-0+/* Same for the (physically registered) runstate and time info areas. */
Quoted 2 lines
xen/arch/x86/mm/mem_sharing.c +2
-0+ ret = copy_guest_area(&cd_vcpu->runstate_guest_area, + &d_vcpu->runstate_guest_area, cd_vcpu,
d);
Quoted 4 lines
Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c static int copy_vcpu_settings(struct dom
Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +4
-0+if ( ret ) +return ret; + ret = copy_guest_area(&cd_vcpu->arch.time_guest_area, + &d_vcpu->arch.time_guest_area, cd_vcpu,
d);
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +3
-0+if ( ret ) +return ret; + ret =copy_vpmu (d_vcpu, cd_vcpu);if ( ret )return ret;@@ -1987,7 +2056,10 @@ int mem_sharing_fork_reset(struct domainQuoted 7 lines
xen/arch/x86/mm/mem_sharing.c int mem_sharing_fork_reset(struct domain · line 2061
Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c +2
-0state:if ( reset_state ) + { rc =copy_settings (d, pd); +/* TBD: What to do here with -ERESTART? */ Where does ERESTART coming from?
Quoted 6 lines
From map_guest_area()'s attempt to acquire the hypercall deadlock mutex, in order to then pause the subject vCPU. I suppose that in the forking case it may already be paused, but then there's no way
map_guest_area()could know. Looking at the pause count is fragile, as there's no guarantee that the vCPU may be unpaused while we're still doing work on it. Hence I view such checks as only suitable for assertions.
Since map_guest_area is only used to sanity check, and it only happens when the page is being setup for the fork, why can't the sanity check be done on the parent? The parent is guaranteed to be paused when forks are active so there is no ERESTART concern and from the looks of it if there is a concern the sanity check is looking for it would be visible on the parent just as well as the fork. But I would like to understand why that sanity checking is required in the first place.
Thanks,
Tamas
Re: [PATCH v2 4/8] x86/mem-sharing: copy GADDR based shared guest areas
Quoted 82 lines · Tamas K Lengyel
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Tamas K Lengyel
Quoted 6 lines · Jan Beulich
xen/arch/x86/mm/mem_sharing.c +0
-0@@ -1653,6 +1653,65 @@ static void copy_vcpu_nonreg_state(struchvm_set_nonreg_state (cd_vcpu, &nrs); }Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c +5
-0+static int copy_guest_area (struct guest_area *cd_area, +const struct guest_area *d_area, +struct vcpu *cd_vcpu, +const struct domain *d) +{Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8
-0+mfn_t d_mfn, cd_mfn; + +if ( !d_area->pg ) +return 0 ; + + d_mfn =page_to_mfn (d_area->pg); + +/* Allocate & map a page for the area if it hasn't been already. */
Quoted 10 lines
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10
-0+if ( !cd_area->pg ) + { +gfn_t gfn =mfn_to_gfn (d, d_mfn); +struct p2m_domain *p2m =p2m_get_hostp2m (cd_vcpu->domain); +p2m_type_t p2mt; +p2m_access_t p2ma; +unsigned int offset; +int ret; + + cd_mfn = p2m->get_entry(p2m, gfn, &p2mt, &p2ma, 0, NULL,NULL);
Quoted 35 lines
Quoted 35 lines
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 3 lines
xen/arch/x86/mm/mem_sharing.c +3
-0+if (mfn_eq (cd_mfn,INVALID_MFN ) ) + { + struct page_info *pg = alloc_domheap_page(cd_vcpu->domain,0);
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8
-0+ +if ( !pg ) +return -ENOMEM ; + + cd_mfn =page_to_mfn (pg); +set_gpfn_from_mfn (mfn_x (cd_mfn),gfn_x (gfn)); + + ret = p2m->set_entry(p2m, gfn, cd_mfn, PAGE_ORDER_4K,
p2m_ram_rw,xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+ p2m->default_access, -1); +if ( ret ) +return ret; + } +else if ( p2mt != p2m_ram_rw ) +return -EBUSY ; + +/* +* Simply specify the entire range up to the end of the page. All the
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1699
Quoted 1 line
xen/arch/x86/mm/mem_sharing.c +1
-0+* function uses it for is a check for not crossing page boundaries.
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+*/ + offset =PAGE_OFFSET (d_area->map); + ret =map_guest_area (cd_vcpu,gfn_to_gaddr (gfn) + offset, +PAGE_SIZE - offset, cd_area,NULL ); +if ( ret ) +return ret; + } +else + cd_mfn =page_to_mfn (cd_area->pg);Quoted 1 line
Quoted 1 line
Everything to this point seems to be non mem-sharing/forking related.
Could
Quoted 1 line
Quoted 1 line
these live somewhere else? There must be some other place where
allocating
Quoted 1 line
Quoted 1 line
these areas happens already for non-fork VMs so it would make sense to
just
Quoted 6 lines
Quoted 1 line
refactor that code to be callable from here.
It is the "copy" aspect with makes this mem-sharing (or really fork) specific. Plus in the end this is no different from what you have there right now for copying the vCPU info area. In the final patch that other code gets removed by re-using the code here.
Quoted 6 lines
Yes, the copy part is fork-specific. Arguably if there was a way to do the allocation of the page for
vcpu_infoI would prefer that being elsewhere, but while the only requirement is allocate-page and copy from parent I'm OK with that logic being in here because it's really straight forward. But now you also do extra sanity checks here which are harder to comprehend in this context alone.
What sanity checks are you talking about (also below, where you claim map_guest_area() would be used only to sanity check)?
Quoted 11 lines
What if extra sanity checks will be needed in the future? Or the sanity checks in the future diverge from where this happens for normal VMs because someone overlooks this needing to be synched here too?
Quoted 4 lines
I also haven't been able to spot anything that could be factored out (and one might expect that if there was something, then the vCPU info area copying should also already have used it).
map_guest_area()is all that is used for other purposes as well.Well, there must be a location where all this happens for normal VMs as well, no?
That's map_guest_area(). What is needed here but not elsewhere is the populating of the GFN underlying the to-be-mapped area. That's the code being added here, mirroring what you need to do for the vCPU info page. Similar code isn't needed elsewhere because the guest invoked operation is purely a "map" - the underlying pages are already expected to be populated (which of course we check, or else we wouldn't know what page to actually map).
Quoted 4 lines
Why not factor that code so that it can be called from here, so that we don't have to track sanity check requirements in two different locations? Or for normal VMs that sanity checking bit isn't required? If so, why?
As per above, I'm afraid that I'm lost with these questions. I simply don't know what you're talking about.
Quoted 46 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
xen/arch/x86/mm/mem_sharing.c +6
-0+ +copy_domain_page (cd_mfn, d_mfn); + +return 0 ; +} +static int copy_vpmu (struct vcpu *d_vcpu,struct vcpu *cd_vcpu) {struct vpmu_struct *d_vpmu =vcpu_vpmu (d_vcpu);@@ -1745,6 +1804,16 @@ static int copy_vcpu_settings(struct domcopy_domain_page (new_vcpu_info_mfn, vcpu_info_mfn); }Quoted 4 lines
Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c static int copy_vcpu_settings(struct dom · line 1807
Quoted 1 line
xen/arch/x86/mm/mem_sharing.c +1
-0+/* Same for the (physically registered) runstate and time info areas. */
Quoted 2 lines
xen/arch/x86/mm/mem_sharing.c +2
-0+ ret = copy_guest_area(&cd_vcpu->runstate_guest_area, + &d_vcpu->runstate_guest_area, cd_vcpu,d);
Quoted 4 lines
Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c static int copy_vcpu_settings(struct dom
Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +4
-0+if ( ret ) +return ret; + ret = copy_guest_area(&cd_vcpu->arch.time_guest_area, + &d_vcpu->arch.time_guest_area, cd_vcpu,d);
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +3
-0+if ( ret ) +return ret; + ret =copy_vpmu (d_vcpu, cd_vcpu);if ( ret )return ret;@@ -1987,7 +2056,10 @@ int mem_sharing_fork_reset(struct domainQuoted 7 lines
xen/arch/x86/mm/mem_sharing.c int mem_sharing_fork_reset(struct domain · line 2061
Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c +2
-0state:if ( reset_state ) + { rc =copy_settings (d, pd); +/* TBD: What to do here with -ERESTART? */ Where does ERESTART coming from?
Quoted 6 lines
From map_guest_area()'s attempt to acquire the hypercall deadlock mutex, in order to then pause the subject vCPU. I suppose that in the forking case it may already be paused, but then there's no way
map_guest_area()could know. Looking at the pause count is fragile, as there's no guarantee that the vCPU may be unpaused while we're still doing work on it. Hence I view such checks as only suitable for assertions.
Quoted 3 lines
Since
map_guest_areais only used to sanity check, and it only happens when the page is being setup for the fork, why can't the sanity check be done on the parent?
As above, I'm afraid I simply don't understand what you're asking.
Quoted 4 lines
The parent is guaranteed to be paused when forks are active so there is no ERESTART concern and from the looks of it if there is a concern the sanity check is looking for it would be visible on the parent just as well as the fork.
The parent being paused isn't of interest to map_guest_area(). It's the subject vcpu (i.e. in the forked instance) where we require this. Thinking of it - the forked domain wasn't started yet, was it? We could then avoid the pausing (and the acquiring of the hypercall deadlock mutex) based on ->creation_finished still being "false", or even simply based on v->domain != current->domain. Then there wouldn't be any chance anymore of -ERESTART making it here.
Quoted 2 lines
But I would like to understand why that sanity checking is required in the first place.
See further up.
Jan
Re: [PATCH v2 4/8] x86/mem-sharing: copy GADDR based shared guest areas
Quoted 35 lines · Jan Beulich
Quoted 33 lines · Tamas K Lengyel
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Tamas K Lengyel
Quoted 6 lines · Jan Beulich
xen/arch/x86/mm/mem_sharing.c +0
-0@@ -1653,6 +1653,65 @@ static void copy_vcpu_nonreg_state(struchvm_set_nonreg_state (cd_vcpu, &nrs); }Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c +5
-0+static int copy_guest_area (struct guest_area *cd_area, +const struct guest_area *d_area, +struct vcpu *cd_vcpu, +const struct domain *d) +{Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8
-0+mfn_t d_mfn, cd_mfn; + +if ( !d_area->pg ) +return 0 ; + + d_mfn =page_to_mfn (d_area->pg); + +/* Allocate & map a page for the area if it hasn't been already. */
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10
-0+if ( !cd_area->pg ) + { +gfn_t gfn =mfn_to_gfn (d, d_mfn); +struct p2m_domain *p2m =p2m_get_hostp2m (cd_vcpu->domain); +p2m_type_t p2mt; +p2m_access_t p2ma; +unsigned int offset; +int ret; + + cd_mfn = p2m->get_entry(p2m, gfn, &p2mt, &p2ma, 0, NULL,
Quoted 4 lines
Quoted 4 lines
NULL);
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
xen/arch/x86/mm/mem_sharing.c +3
-0+if (mfn_eq (cd_mfn,INVALID_MFN ) ) + { + struct page_info *pg =
alloc_domheap_page(cd_vcpu->domain,
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
0);
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8
-0+ +if ( !pg ) +return -ENOMEM ; + + cd_mfn =page_to_mfn (pg); +set_gpfn_from_mfn (mfn_x (cd_mfn),gfn_x (gfn)); + + ret = p2m->set_entry(p2m, gfn, cd_mfn, PAGE_ORDER_4K,
p2m_ram_rw,
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+ p2m->default_access, -1); +if ( ret ) +return ret; + } +else if ( p2mt != p2m_ram_rw ) +return -EBUSY ; + +/* +* Simply specify the entire range up to the end of the
page.
Quoted 19 lines
Quoted 19 lines
Quoted 13 lines
Quoted 13 lines
All the
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1699
Quoted 1 line
xen/arch/x86/mm/mem_sharing.c +1
-0+* function uses it for is a check for not crossing page boundaries.
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+*/ + offset =PAGE_OFFSET (d_area->map); + ret =map_guest_area (cd_vcpu,gfn_to_gaddr (gfn) + offset, +PAGE_SIZE - offset, cd_area,NULL ); +if ( ret ) +return ret; + } +else + cd_mfn =page_to_mfn (cd_area->pg);Quoted 1 line
Quoted 1 line
Everything to this point seems to be non mem-sharing/forking related.
Could
Quoted 1 line
Quoted 1 line
these live somewhere else? There must be some other place where
allocating
Quoted 1 line
Quoted 1 line
these areas happens already for non-fork VMs so it would make sense to
just
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
refactor that code to be callable from here.
It is the "copy" aspect with makes this mem-sharing (or really fork) specific. Plus in the end this is no different from what you have there right now for copying the vCPU info area. In the final patch that other code gets removed by re-using the code here.
Yes, the copy part is fork-specific. Arguably if there was a way to do
the
Quoted 1 line
Quoted 1 line
allocation of the page for
vcpu_infoI would prefer that being
elsewhere,
Quoted 1 line
Quoted 1 line
but while the only requirement is allocate-page and copy from parent
I'm OK
Quoted 1 line
Quoted 1 line
with that logic being in here because it's really straight forward. But
now
Quoted 1 line
Quoted 1 line
you also do extra sanity checks here which are harder to comprehend in
this
Quoted 4 lines
Quoted 1 line
context alone.
What sanity checks are you talking about (also below, where you claim
map_guest_area()would be used only to sanity check)?
Did I misread your comment above "All the function uses it for is a check for not crossing page boundaries"? That sounds to me like a simple sanity check, unclear why it matters though and why only for forks.
Quoted 3 lines
Quoted 3 lines
What if extra sanity checks will be needed in the future? Or the sanity checks in the future diverge from where this happens for
normal
Quoted 10 lines
Quoted 9 lines
VMs because someone overlooks this needing to be synched here too?
Quoted 4 lines
I also haven't been able to spot anything that could be factored out (and one might expect that if there was something, then the vCPU info area copying should also already have used it).
map_guest_area()is all that is used for other purposes as well.Well, there must be a location where all this happens for normal VMs as well, no?
Quoted 7 lines
That's
map_guest_area(). What is needed here but not elsewhere is the populating of the GFN underlying the to-be-mapped area. That's the code being added here, mirroring what you need to do for the vCPU info page. Similar code isn't needed elsewhere because the guest invoked operation is purely a "map" - the underlying pages are already expected to be populated (which of course we check, or else we wouldn't know what page to actually map).
Populated by what and when?
Quoted 8 lines
Quoted 5 lines
Why not factor that code so that it can be called from here, so that we don't have to track sanity check requirements in two different locations? Or for normal VMs that sanity checking bit isn't required? If so, why?
As per above, I'm afraid that I'm lost with these questions. I simply don't know what you're talking about.
You are adding code here that allocates memory and copies the content of similarly allocated memory from the parent. You perform extra sanity checks for unknown reasons that seem to be only needed here. It is unclear why you are doing that and why can't the same code-paths that allocate this memory for the parent be simply reused so the only thing left to do here would be to copy the content from the parent?
Tamas
Re: [PATCH v2 4/8] x86/mem-sharing: copy GADDR based shared guest areas
Quoted 97 lines · Tamas K Lengyel
Quoted 35 lines · Jan Beulich
Quoted 33 lines · Tamas K Lengyel
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Tamas K Lengyel
Quoted 6 lines · Jan Beulich
xen/arch/x86/mm/mem_sharing.c +0
-0@@ -1653,6 +1653,65 @@ static void copy_vcpu_nonreg_state(struchvm_set_nonreg_state (cd_vcpu, &nrs); }Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c +5
-0+static int copy_guest_area (struct guest_area *cd_area, +const struct guest_area *d_area, +struct vcpu *cd_vcpu, +const struct domain *d) +{Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8
-0+mfn_t d_mfn, cd_mfn; + +if ( !d_area->pg ) +return 0 ; + + d_mfn =page_to_mfn (d_area->pg); + +/* Allocate & map a page for the area if it hasn't been already. */
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10
-0+if ( !cd_area->pg ) + { +gfn_t gfn =mfn_to_gfn (d, d_mfn); +struct p2m_domain *p2m =p2m_get_hostp2m (cd_vcpu->domain); +p2m_type_t p2mt; +p2m_access_t p2ma; +unsigned int offset; +int ret; + + cd_mfn = p2m->get_entry(p2m, gfn, &p2mt, &p2ma, 0, NULL,Quoted 4 lines
Quoted 4 lines
NULL);
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
xen/arch/x86/mm/mem_sharing.c +3
-0+if (mfn_eq (cd_mfn,INVALID_MFN ) ) + { + struct page_info *pg =alloc_domheap_page(cd_vcpu->domain,
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
0);
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8
-0+ +if ( !pg ) +return -ENOMEM ; + + cd_mfn =page_to_mfn (pg); +set_gpfn_from_mfn (mfn_x (cd_mfn),gfn_x (gfn)); + + ret = p2m->set_entry(p2m, gfn, cd_mfn, PAGE_ORDER_4K,
p2m_ram_rw,Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+ p2m->default_access, -1); +if ( ret ) +return ret; + } +else if ( p2mt != p2m_ram_rw ) +return -EBUSY ; + +/* +* Simply specify the entire range up to the end of the page.
Quoted 19 lines
Quoted 19 lines
Quoted 13 lines
Quoted 13 lines
All the
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1699
Quoted 1 line
xen/arch/x86/mm/mem_sharing.c +1
-0+* function uses it for is a check for not crossing page boundaries.
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+*/ + offset =PAGE_OFFSET (d_area->map); + ret =map_guest_area (cd_vcpu,gfn_to_gaddr (gfn) + offset, +PAGE_SIZE - offset, cd_area,NULL ); +if ( ret ) +return ret; + } +else + cd_mfn =page_to_mfn (cd_area->pg);Quoted 1 line
Quoted 1 line
Everything to this point seems to be non mem-sharing/forking related.
Could
Quoted 1 line
Quoted 1 line
these live somewhere else? There must be some other place where
allocating
Quoted 1 line
Quoted 1 line
these areas happens already for non-fork VMs so it would make sense to
just
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
refactor that code to be callable from here.
It is the "copy" aspect with makes this mem-sharing (or really fork) specific. Plus in the end this is no different from what you have there right now for copying the vCPU info area. In the final patch that other code gets removed by re-using the code here.
Yes, the copy part is fork-specific. Arguably if there was a way to do
the
Quoted 1 line
Quoted 1 line
allocation of the page for
vcpu_infoI would prefer that beingelsewhere,
Quoted 1 line
Quoted 1 line
but while the only requirement is allocate-page and copy from parent
I'm OK
Quoted 1 line
Quoted 1 line
with that logic being in here because it's really straight forward. But
now
Quoted 1 line
Quoted 1 line
you also do extra sanity checks here which are harder to comprehend in
this
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
context alone.
What sanity checks are you talking about (also below, where you claim
map_guest_area()would be used only to sanity check)?Did I misread your comment above "All the function uses it for is a check for not crossing page boundaries"? That sounds to me like a simple sanity check, unclear why it matters though and why only for forks.
The comment is about the function's use of the range it is being passed. It doesn't say in any way that the function is doing only sanity checking. If the comment wording is ambiguous or unclear, I'm happy to take improvement suggestions.
Quoted 21 lines
Quoted 2 lines
Quoted 2 lines
What if extra sanity checks will be needed in the future? Or the sanity checks in the future diverge from where this happens for
normal
Quoted 10 lines
Quoted 9 lines
VMs because someone overlooks this needing to be synched here too?
Quoted 4 lines
I also haven't been able to spot anything that could be factored out (and one might expect that if there was something, then the vCPU info area copying should also already have used it).
map_guest_area()is all that is used for other purposes as well.Well, there must be a location where all this happens for normal VMs as well, no?
Quoted 7 lines
That's
map_guest_area(). What is needed here but not elsewhere is the populating of the GFN underlying the to-be-mapped area. That's the code being added here, mirroring what you need to do for the vCPU info page. Similar code isn't needed elsewhere because the guest invoked operation is purely a "map" - the underlying pages are already expected to be populated (which of course we check, or else we wouldn't know what page to actually map).
Quoted 1 line
Populated by what and when?
Population happens either at domain creation or when the guest is moving pages around (e.g. during ballooning). What is happening here (also in the pre-existing code to deal with the vCPU info page) is the minimal amount of "populate at creation" to meet the prereq for the mapping operation(s).
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Why not factor that code so that it can be called from here, so that we don't have to track sanity check requirements in two different locations? Or for normal VMs that sanity checking bit isn't required? If so, why?
As per above, I'm afraid that I'm lost with these questions. I simply don't know what you're talking about.
Quoted 6 lines
You are adding code here that allocates memory and copies the content of similarly allocated memory from the parent. You perform extra sanity checks for unknown reasons that seem to be only needed here. It is unclear why you are doing that and why can't the same code-paths that allocate this memory for the parent be simply reused so the only thing left to do here would be to copy the content from the parent?
No, I'm not "adding code" in the sense that I read your comments so far. Such code was already there (and, as pointed out somewhere, in slightly broken form). Yes, I'm introducing a 2nd instance of this, but just to then (in the last patch) remove the original (slightly broken) instance. So across the entire series this is merely code movement (with adjustments).
Jan
Re: [PATCH v2 4/8] x86/mem-sharing: copy GADDR based shared guest areas
Quoted 6 lines · Jan Beulich
Quoted 4 lines · Tamas K Lengyel
Quoted 3 lines · Jan Beulich
Quoted 1 line · Tamas K Lengyel
On Mon, Jan 23, 2023 at 11:24 AM Jan Beulich <[email protected]>
Quoted 2 lines · wrote:
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines · Tamas K Lengyel
Quoted 1 line
On Mon, Jan 23, 2023 at 9:55 AM Jan Beulich <[email protected]>
Quoted 6 lines · wrote:
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c +0
-0@@ -1653,6 +1653,65 @@ static void copy_vcpu_nonreg_state(struchvm_set_nonreg_state (cd_vcpu, &nrs); }
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c +5
-0+static int copy_guest_area (struct guest_area *cd_area, +const struct guest_area *d_area, +struct vcpu *cd_vcpu, +const struct domain *d) +{Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1668
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8
-0+mfn_t d_mfn, cd_mfn; + +if ( !d_area->pg ) +return 0 ; + + d_mfn =page_to_mfn (d_area->pg); + +/* Allocate & map a page for the area if it hasn't been
already.
Quoted 49 lines
Quoted 49 lines
Quoted 11 lines
Quoted 11 lines
*/
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10
-0+if ( !cd_area->pg ) + { +gfn_t gfn =mfn_to_gfn (d, d_mfn); +struct p2m_domain *p2m =p2m_get_hostp2m (cd_vcpu->domain); +p2m_type_t p2mt; +p2m_access_t p2ma; +unsigned int offset; +int ret; + + cd_mfn = p2m->get_entry(p2m, gfn, &p2mt, &p2ma, 0, NULL,Quoted 4 lines
Quoted 4 lines
NULL);
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
xen/arch/x86/mm/mem_sharing.c +3
-0+if (mfn_eq (cd_mfn,INVALID_MFN ) ) + { + struct page_info *pg =alloc_domheap_page(cd_vcpu->domain,
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
0);
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1689
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8
-0+ +if ( !pg ) +return -ENOMEM ; + + cd_mfn =page_to_mfn (pg); +set_gpfn_from_mfn (mfn_x (cd_mfn),gfn_x (gfn)); + + ret = p2m->set_entry(p2m, gfn, cd_mfn, PAGE_ORDER_4K,
p2m_ram_rw,Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1698
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+ p2m->default_access, -1); +if ( ret ) +return ret; + } +else if ( p2mt != p2m_ram_rw ) +return -EBUSY ; + +/* +* Simply specify the entire range up to the end of the page.
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
All the
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1699
Quoted 1 line
xen/arch/x86/mm/mem_sharing.c +1
-0+* function uses it for is a check for not crossing page boundaries.
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+*/ + offset =PAGE_OFFSET (d_area->map); + ret =map_guest_area (cd_vcpu,gfn_to_gaddr (gfn) + offset, +PAGE_SIZE - offset, cd_area,NULL ); +if ( ret ) +return ret; + } +else + cd_mfn =page_to_mfn (cd_area->pg);
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Everything to this point seems to be non mem-sharing/forking
related.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Could
Quoted 1 line
Quoted 1 line
these live somewhere else? There must be some other place where
allocating
Quoted 1 line
Quoted 1 line
these areas happens already for non-fork VMs so it would make sense
to
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
just
Quoted 6 lines
Quoted 1 line
refactor that code to be callable from here.
It is the "copy" aspect with makes this mem-sharing (or really fork) specific. Plus in the end this is no different from what you have there right now for copying the vCPU info area. In the final patch that other code gets removed by re-using the code here.
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Yes, the copy part is fork-specific. Arguably if there was a way to do
the
Quoted 1 line
Quoted 1 line
allocation of the page for
vcpu_infoI would prefer that beingelsewhere,
Quoted 1 line
Quoted 1 line
but while the only requirement is allocate-page and copy from parent
I'm OK
Quoted 1 line
Quoted 1 line
with that logic being in here because it's really straight forward.
But
Quoted 9 lines
Quoted 9 lines
now
Quoted 1 line
Quoted 1 line
you also do extra sanity checks here which are harder to comprehend in
this
Quoted 4 lines
Quoted 1 line
context alone.
What sanity checks are you talking about (also below, where you claim
map_guest_area()would be used only to sanity check)?Did I misread your comment above "All the function uses it for is a
check
Quoted 1 line
Quoted 1 line
for not crossing page boundaries"? That sounds to me like a simple
sanity
Quoted 6 lines
Quoted 1 line
check, unclear why it matters though and why only for forks.
The comment is about the function's use of the range it is being passed. It doesn't say in any way that the function is doing only sanity checking. If the comment wording is ambiguous or unclear, I'm happy to take improvement suggestions.
Yes, please do, it definitely was confusing while reviewing the patch.
Thanks,
Tamas
Re: [PATCH v2 4/8] x86/mem-sharing: copy GADDR based shared guest areas
Quoted 115 lines · Tamas K Lengyel
Quoted 6 lines · Jan Beulich
Quoted 4 lines · Tamas K Lengyel
Quoted 3 lines · Jan Beulich
Quoted 1 line · Tamas K Lengyel
On Mon, Jan 23, 2023 at 11:24 AM Jan Beulich <[email protected]>
Quoted 2 lines · wrote:
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines · Tamas K Lengyel
Quoted 1 line
On Mon, Jan 23, 2023 at 9:55 AM Jan Beulich <[email protected]>
Quoted 6 lines · wrote:
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c +0
-0@@ -1653,6 +1653,65 @@ static void copy_vcpu_nonreg_state(struchvm_set_nonreg_state (cd_vcpu, &nrs); }Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c +5
-0+static int copy_guest_area (struct guest_area *cd_area, +const struct guest_area *d_area, +struct vcpu *cd_vcpu, +const struct domain *d) +{Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1668
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8
-0+mfn_t d_mfn, cd_mfn; + +if ( !d_area->pg ) +return 0 ; + + d_mfn =page_to_mfn (d_area->pg); + +/* Allocate & map a page for the area if it hasn't been already.
Quoted 49 lines
Quoted 49 lines
Quoted 11 lines
Quoted 11 lines
*/
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +10
-0+if ( !cd_area->pg ) + { +gfn_t gfn =mfn_to_gfn (d, d_mfn); +struct p2m_domain *p2m =p2m_get_hostp2m (cd_vcpu->domain); +p2m_type_t p2mt; +p2m_access_t p2ma; +unsigned int offset; +int ret; + + cd_mfn = p2m->get_entry(p2m, gfn, &p2mt, &p2ma, 0, NULL,Quoted 4 lines
Quoted 4 lines
NULL);
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
xen/arch/x86/mm/mem_sharing.c +3
-0+if (mfn_eq (cd_mfn,INVALID_MFN ) ) + { + struct page_info *pg =alloc_domheap_page(cd_vcpu->domain,
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
0);
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1689
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8
-0+ +if ( !pg ) +return -ENOMEM ; + + cd_mfn =page_to_mfn (pg); +set_gpfn_from_mfn (mfn_x (cd_mfn),gfn_x (gfn)); + + ret = p2m->set_entry(p2m, gfn, cd_mfn, PAGE_ORDER_4K,
p2m_ram_rw,Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1698
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+ p2m->default_access, -1); +if ( ret ) +return ret; + } +else if ( p2mt != p2m_ram_rw ) +return -EBUSY ; + +/* +* Simply specify the entire range up to the end of the page.
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
All the
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1699
Quoted 1 line
xen/arch/x86/mm/mem_sharing.c +1
-0+* function uses it for is a check for not crossing page boundaries.
Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+*/ + offset =PAGE_OFFSET (d_area->map); + ret =map_guest_area (cd_vcpu,gfn_to_gaddr (gfn) + offset, +PAGE_SIZE - offset, cd_area,NULL ); +if ( ret ) +return ret; + } +else + cd_mfn =page_to_mfn (cd_area->pg);Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Everything to this point seems to be non mem-sharing/forking
related.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Could
Quoted 1 line
Quoted 1 line
these live somewhere else? There must be some other place where
allocating
Quoted 1 line
Quoted 1 line
these areas happens already for non-fork VMs so it would make sense
to
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
just
Quoted 6 lines
Quoted 1 line
refactor that code to be callable from here.
It is the "copy" aspect with makes this mem-sharing (or really fork) specific. Plus in the end this is no different from what you have there right now for copying the vCPU info area. In the final patch that other code gets removed by re-using the code here.
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Yes, the copy part is fork-specific. Arguably if there was a way to do
the
Quoted 1 line
Quoted 1 line
allocation of the page for
vcpu_infoI would prefer that beingelsewhere,
Quoted 1 line
Quoted 1 line
but while the only requirement is allocate-page and copy from parent
I'm OK
Quoted 1 line
Quoted 1 line
with that logic being in here because it's really straight forward.
But
Quoted 9 lines
Quoted 9 lines
now
Quoted 1 line
Quoted 1 line
you also do extra sanity checks here which are harder to comprehend in
this
Quoted 4 lines
Quoted 1 line
context alone.
What sanity checks are you talking about (also below, where you claim
map_guest_area()would be used only to sanity check)?Did I misread your comment above "All the function uses it for is a
check
Quoted 1 line
Quoted 1 line
for not crossing page boundaries"? That sounds to me like a simple
sanity
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
check, unclear why it matters though and why only for forks.
The comment is about the function's use of the range it is being passed. It doesn't say in any way that the function is doing only sanity checking. If the comment wording is ambiguous or unclear, I'm happy to take improvement suggestions.
Yes, please do, it definitely was confusing while reviewing the patch.
I'm sorry, but what does "please do" mean when I asked for improvement suggestions? I continue to think the comment is quite clear as is, so if anything needs adjusting, I'd need to know pretty precisely what it is that needs adding and/or re-writing. I can't, after all, guess what your misunderstanding resulted from.
Jan
Re: [PATCH v2 4/8] x86/mem-sharing: copy GADDR based shared guest areas
Quoted 33 lines · Jan Beulich
Quoted 31 lines · Tamas K Lengyel
Quoted 6 lines · Jan Beulich
Quoted 4 lines · Tamas K Lengyel
Quoted 3 lines · Jan Beulich
Quoted 1 line · Tamas K Lengyel
On Mon, Jan 23, 2023 at 11:24 AM Jan Beulich <[email protected]>
Quoted 2 lines · wrote:
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines · Tamas K Lengyel
Quoted 1 line
On Mon, Jan 23, 2023 at 9:55 AM Jan Beulich <[email protected]>
Quoted 6 lines · wrote:
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c +0
-0@@ -1653,6 +1653,65 @@ static void copy_vcpu_nonreg_state(struchvm_set_nonreg_state (cd_vcpu, &nrs); }Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
xen/arch/x86/mm/mem_sharing.c +5
-0+static int copy_guest_area (struct guest_area *cd_area, +const struct guest_area *d_area, +struct vcpu *cd_vcpu, +const struct domain *d) +{Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1668
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8
-0+mfn_t d_mfn, cd_mfn; + +if ( !d_area->pg ) +return 0 ; + + d_mfn =page_to_mfn (d_area->pg); + +/* Allocate & map a page for the area if it hasn't been already.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
*/
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
xen/arch/x86/mm/mem_sharing.c +4
-0+if ( !cd_area->pg ) + { +gfn_t gfn =mfn_to_gfn (d, d_mfn); + struct p2m_domain *p2m =
p2m_get_hostp2m(cd_vcpu->domain);
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 10 lines
Quoted 10 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1678
Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c +6
-0+p2m_type_t p2mt; +p2m_access_t p2ma; +unsigned int offset; +int ret; + + cd_mfn = p2m->get_entry(p2m, gfn, &p2mt, &p2ma, 0, NULL,NULL);
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
xen/arch/x86/mm/mem_sharing.c +3
-0+if (mfn_eq (cd_mfn,INVALID_MFN ) ) + { + struct page_info *pg =alloc_domheap_page(cd_vcpu->domain,
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
0);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1689
Quoted 8 lines
xen/arch/x86/mm/mem_sharing.c +8
-0+ +if ( !pg ) +return -ENOMEM ; + + cd_mfn =page_to_mfn (pg); +set_gpfn_from_mfn (mfn_x (cd_mfn),gfn_x (gfn)); + + ret = p2m->set_entry(p2m, gfn, cd_mfn,
PAGE_ORDER_4K,
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
p2m_ram_rw,Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +9
-0+ p2m->default_access, -1); +if ( ret ) +return ret; + } +else if ( p2mt != p2m_ram_rw ) +return -EBUSY ; + +/* +* Simply specify the entire range up to the end of the
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
page.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
All the
xen/arch/x86/mm/mem_sharing.c static void copy_vcpu_nonreg_state(struc · line 1699
Quoted 1 line
xen/arch/x86/mm/mem_sharing.c +1
-0+* function uses it for is a check for not crossing page boundaries.
Quoted 3 lines
xen/arch/x86/mm/mem_sharing.c +3
-0+*/ + offset =PAGE_OFFSET (d_area->map); + ret = map_guest_area(cd_vcpu, gfn_to_gaddr(gfn) +
offset,
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
xen/arch/x86/mm/mem_sharing.c +6
-0+ PAGE_SIZE - offset, cd_area, NULL); +if ( ret ) +return ret; + } +else + cd_mfn =page_to_mfn (cd_area->pg);
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Everything to this point seems to be non mem-sharing/forking
related.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Could
Quoted 1 line
Quoted 1 line
these live somewhere else? There must be some other place where
allocating
Quoted 1 line
Quoted 1 line
these areas happens already for non-fork VMs so it would make
sense
Quoted 5 lines
Quoted 5 lines
to
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
just
Quoted 3 lines
Quoted 1 line
refactor that code to be callable from here.
It is the "copy" aspect with makes this mem-sharing (or really
fork)
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
specific. Plus in the end this is no different from what you have there right now for copying the vCPU info area. In the final patch that other code gets removed by re-using the code here.
Yes, the copy part is fork-specific. Arguably if there was a way to
do
Quoted 9 lines
Quoted 9 lines
Quoted 6 lines
Quoted 6 lines
the
Quoted 1 line
Quoted 1 line
allocation of the page for
vcpu_infoI would prefer that beingelsewhere,
Quoted 1 line
Quoted 1 line
but while the only requirement is allocate-page and copy from parent
I'm OK
Quoted 1 line
Quoted 1 line
with that logic being in here because it's really straight forward.
But
Quoted 2 lines
Quoted 2 lines
now
Quoted 1 line
Quoted 1 line
you also do extra sanity checks here which are harder to comprehend
in
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
this
Quoted 4 lines
Quoted 1 line
context alone.
What sanity checks are you talking about (also below, where you claim
map_guest_area()would be used only to sanity check)?
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Did I misread your comment above "All the function uses it for is a
check
Quoted 1 line
Quoted 1 line
for not crossing page boundaries"? That sounds to me like a simple
sanity
Quoted 3 lines
Quoted 1 line
check, unclear why it matters though and why only for forks.
The comment is about the function's use of the range it is being
passed.
Quoted 1 line
Quoted 1 line
Quoted 1 line
It doesn't say in any way that the function is doing only sanity
checking.
Quoted 10 lines
Quoted 4 lines
Quoted 2 lines
If the comment wording is ambiguous or unclear, I'm happy to take improvement suggestions.
Yes, please do, it definitely was confusing while reviewing the patch.
I'm sorry, but what does "please do" mean when I asked for improvement suggestions? I continue to think the comment is quite clear as is, so if anything needs adjusting, I'd need to know pretty precisely what it is that needs adding and/or re-writing. I can't, after all, guess what your misunderstanding resulted from.
I meant please do add the extra information you just spelled out in your previous email. "Map the page into the guest. We pass the entire range to map_guest_area so that it can check that no cross-page mapping is taking place (in which case it will fail). If no such issue is present the page is being mapped and made accessible to the guest."
If that's not what the function is doing, please explain clearly what it will do.
Tamas
PATCH v2 5/8 domain: map/unmap GADDR based shared guest areas
In reply to Jan BeulichThe registration by virtual/linear address has downsides: At least on x86 the access is expensive for HVM/PVH domains. Furthermore for 64-bit PV domains the areas are inaccessible (and hence cannot be updated by Xen) when in guest-user mode, and for HVM guests they may be inaccessible when Meltdown mitigations are in place. (There are yet more issues.)
In preparation of the introduction of new vCPU operations allowing to register the respective areas (one of the two is x86-specific) by guest-physical address, flesh out the map/unmap functions.
Noteworthy differences from map_vcpu_info():
- areas can be registered more than once (and de-registered),
- remote vCPU-s are paused rather than checked for being down (which in principle can change right after the check),
- the domain lock is taken for a much smaller region.
---
RFC: By using global domain page mappings the demand on the underlying
VA range may increase significantly. I did consider to use per- domain mappings instead, but they exist for x86 only. Of course we could have arch_{,un}map_guest_area() aliasing global domain page mapping functions on Arm and using per-domain mappings on x86. Yet then again map_vcpu_info() doesn't (and can't) do so.
RFC: In map_guest_area() I'm not checking the P2M type, instead - just
like map_vcpu_info() - solely relying on the type ref acquisition. Checking for p2m_ram_rw alone would be wrong, as at least p2m_ram_logdirty ought to also be okay to use here (and in similar cases, e.g. in Argo's find_ring_mfn()). p2m_is_pageable() could be used here (like altp2m_vcpu_enable_ve() does) as well as in map_vcpu_info(), yet then again the P2M type is stale by the time it is being looked at anyway without the P2M lock held.
---
v2: currd -> d, to cover mem-sharing's copy_guest_area(). Re-base over
change(s) earlier in the series. Use ~0 as "unmap" request indicator.
xen/common/domain.c +91 -1
@@ -1576,7 +1576,82 @@ int map_guest_area(struct vcpu *v, paddrstruct guest_area *area, void (*populate)(void *dst, struct vcpu *v)) { -return -EOPNOTSUPP ; + struct domain *d = v->domain; +void *map =NULL ; +struct page_info *pg =NULL ; +int rc =0 ; + +if ( ~gaddr ) + { +unsigned long gfn =PFN_DOWN (gaddr); +unsigned int align; +p2m_type_t p2mt; + +if ( gfn !=PFN_DOWN (gaddr + size -1 ) ) +return -ENXIO ; + +#ifdef CONFIG_COMPAT +if (has_32bit_shinfo (d) ) + align = alignof(compat_ulong_t ); + else +#endif + align = alignof(xen_ulong_t ); +if ( gaddr & (align -1 ) ) +return -ENXIO ; + + rc =check_get_page_from_gfn (d,_gfn (gfn), false, &p2mt, &pg); +if ( rc ) +return rc; + +if ( !get_page_type (pg, PGT_writable_page) ) + { +put_page (pg); +return -EACCES ; + } + + map =__map_domain_page_global (pg); +if ( !map ) + { +put_page_and_type (pg); +return -ENOMEM ; + } + map +=PAGE_OFFSET (gaddr); + } + +if ( v != current ) + { +if ( !spin_trylock (&d->hypercall_deadlock_mutex) ) + { + rc = -ERESTART ; + goto unmap; + } + +vcpu_pause (v); + +spin_unlock (&d->hypercall_deadlock_mutex); + } + +domain_lock (d); + +if ( map ) +populate (map, v); + +SWAP (area->pg, pg); +SWAP (area->map, map); + +domain_unlock (d); + +if ( v != current ) +vcpu_unpause (v); + + unmap: +if ( pg ) + { +unmap_domain_page_global (map); +put_page_and_type (pg); + } + +return rc; }/*
@@ -1587,9 +1662,24 @@ int map_guest_area(struct vcpu *v, paddrvoid unmap_guest_area (struct vcpu *v,struct guest_area *area) {struct domain *d = v->domain; +void *map; +struct page_info *pg;if ( v != current )ASSERT (atomic_read (&v->pause_count) |atomic_read (&d->pause_count)); + +domain_lock (d); + map = area->map; + area->map =NULL ; + pg = area->pg; + area->pg =NULL ; +domain_unlock (d); + +if ( pg ) + { +unmap_domain_page_global (map); +put_page_and_type (pg); + } }int default_initialise_vcpu (struct vcpu *v,XEN_GUEST_HANDLE_PARAM (void ) arg)
PATCH v2 6/8 domain: introduce GADDR based runstate area registration alternative
In reply to Jan BeulichThe registration by virtual/linear address has downsides: At least on x86 the access is expensive for HVM/PVH domains. Furthermore for 64-bit PV domains the area is inaccessible (and hence cannot be updated by Xen) when in guest-user mode.
Introduce a new vCPU operation allowing to register the runstate area by guest-physical address.
An at least theoretical downside to using physically registered areas is that PV then won't see dirty (and perhaps also accessed) bits set in its respective page table entries.
---
v2: Extend comment in public header.
xen/arch/x86/x86_64/domain.c +35 -0
@@ -12,6 +12,22 @@ CHECK_vcpu_get_physid;#undef xen_vcpu_get_physid +static void cf_check +runstate_area_populate (void *map,struct vcpu *v) +{ +if (is_pv_vcpu (v) ) + v->arch.pv.need_update_runstate_area = false; + + v->runstate_guest_area_compat = true; + +if ( v == current ) + { +struct compat_vcpu_runstate_info *info = map; + +XLAT_vcpu_runstate_info (info, &v->runstate); + } +} +int compat_vcpu_op (int cmd,unsigned int vcpuid,XEN_GUEST_HANDLE_PARAM (void ) arg) {
@@ -57,6 +73,25 @@ compat_vcpu_op(int cmd, unsigned int vcpbreak ; } + +case VCPUOP_register_runstate_phys_area: + { +struct compat_vcpu_register_runstate_memory_area area; + + rc = -EFAULT ; +if (copy_from_guest (&area.addr.p, arg,1 ) ) +break ; + + rc =map_guest_area (v, area.addr.p, +sizeof (struct compat_vcpu_runstate_info ), + &v->runstate_guest_area, + runstate_area_populate); +if ( rc == -ERESTART ) + rc =hypercall_create_continuation (__HYPERVISOR_vcpu_op,"iih" , + cmd, vcpuid, arg); + +break ; + }case VCPUOP_register_vcpu_time_memory_area: {
xen/common/domain.c +39 -0
@@ -1803,6 +1803,26 @@ bool update_runstate_area(struct vcpu *vreturn rc; } +static void cf_check +runstate_area_populate (void *map,struct vcpu *v) +{ +#ifdef CONFIG_PV +if (is_pv_vcpu (v) ) + v->arch.pv.need_update_runstate_area = false; +#endif + +#ifdef CONFIG_COMPAT + v->runstate_guest_area_compat = false; +#endif + +if ( v == current ) + { +struct vcpu_runstate_info *info = map; + + *info = v->runstate; + } +} +long common_vcpu_op (int cmd,struct vcpu *v,XEN_GUEST_HANDLE_PARAM (void ) arg) {long rc =0 ;
@@ -1977,6 +1997,25 @@ long common_vcpu_op(int cmd, struct vcpubreak ; } + +case VCPUOP_register_runstate_phys_area: + { +struct vcpu_register_runstate_memory_area area; + + rc = -EFAULT ; +if (copy_from_guest (&area.addr.p, arg,1 ) ) +break ; + + rc =map_guest_area (v, area.addr.p, +sizeof (struct vcpu_runstate_info ), + &v->runstate_guest_area, + runstate_area_populate); +if ( rc == -ERESTART ) + rc =hypercall_create_continuation (__HYPERVISOR_vcpu_op,"iih" , + cmd, vcpuid, arg); + +break ; + }default : rc = -ENOSYS ;
xen/include/public/vcpu.h +13 -0
@@ -218,6 +218,19 @@ struct vcpu_register_time_memory_area {typedef struct vcpu_register_time_memory_area vcpu_register_time_memory_area_t ;DEFINE_XEN_GUEST_HANDLE (vcpu_register_time_memory_area_t); +/* +* Like the respective VCPUOP_register_*_memory_area, just using the "addr.p" +* field of the supplied struct as a guest physical address (i.e. in GFN space). +* The respective area may not cross a page boundary. Pass ~0 to unregister an +* area. Note that as long as an area is registered by physical address, the +* linear address based area will not be serviced (updated) by the hypervisor. +* +* Note that the area registered via VCPUOP_register_runstate_memory_area will +* be updated in the same manner as the one registered via virtual address PLUS +* VMASST_TYPE_runstate_update_flag engaged by the domain. +*/ +#define VCPUOP_register_runstate_phys_area 14 +#endif /* __XEN_PUBLIC_VCPU_H__ */ /*
PATCH v2 7/8 x86: introduce GADDR based secondary time area registration alternative
In reply to Jan BeulichThe registration by virtual/linear address has downsides: The access is expensive for HVM/PVH domains. Furthermore for 64-bit PV domains the area is inaccessible (and hence cannot be updated by Xen) when in guest-user mode.
Introduce a new vCPU operation allowing to register the secondary time area by guest-physical address.
An at least theoretical downside to using physically registered areas is that PV then won't see dirty (and perhaps also accessed) bits set in its respective page table entries.
---
v2: Forge version in force_update_secondary_system_time().
xen/arch/x86/domain.c +28 -0
@@ -1499,6 +1499,15 @@ int arch_vcpu_reset(struct vcpu *v)return 0 ; } +static void cf_check +time_area_populate (void *map,struct vcpu *v) +{ +if (is_pv_vcpu (v) ) + v->arch.pv.pending_system_time.version =0 ; + +force_update_secondary_system_time (v, map); +} +long do_vcpu_op (int cmd,unsigned int vcpuid,XEN_GUEST_HANDLE_PARAM (void ) arg) {long rc =0 ;
@@ -1536,6 +1545,25 @@ long do_vcpu_op(int cmd, unsigned int vcbreak ; } + +case VCPUOP_register_vcpu_time_phys_area: + { +struct vcpu_register_time_memory_area area; + + rc = -EFAULT ; +if (copy_from_guest (&area.addr.p, arg,1 ) ) +break ; + + rc =map_guest_area (v, area.addr.p, +sizeof (vcpu_time_info_t), + &v->arch.time_guest_area, + time_area_populate); +if ( rc == -ERESTART ) + rc =hypercall_create_continuation (__HYPERVISOR_vcpu_op,"iih" , + cmd, vcpuid, arg); + +break ; + }case VCPUOP_get_physid: {
xen/arch/x86/include/asm/domain.h +2 -0
@@ -681,6 +681,8 @@ void domain_cpu_policy_changed(struct dobool update_secondary_system_time (struct vcpu *,struct vcpu_time_info *); +void force_update_secondary_system_time (struct vcpu *, +struct vcpu_time_info *);void vcpu_show_execution_state (struct vcpu *);void vcpu_show_registers (const struct vcpu *);
xen/arch/x86/time.c +10 -0
@@ -1524,6 +1524,16 @@ void force_update_vcpu_system_time(struc__update_vcpu_system_time (v,1 ); } +void force_update_secondary_system_time (struct vcpu *v, +struct vcpu_time_info *map) +{ +struct vcpu_time_info u; + +collect_time_info (v, &u); + u.version =-1 ;/* Compensate for version_update_end(). */ +write_time_guest_area (map, &u); +} +static void update_domain_rtc (void ) {struct domain *d;
xen/arch/x86/x86_64/domain.c +1 -0
@@ -115,6 +115,7 @@ compat_vcpu_op(int cmd, unsigned int vcpcase VCPUOP_send_nmi:case VCPUOP_get_physid: +case VCPUOP_register_vcpu_time_phys_area: rc =do_vcpu_op (cmd, vcpuid, arg);break ;
xen/include/public/vcpu.h +1 -0
@@ -230,6 +230,7 @@ DEFINE_XEN_GUEST_HANDLE(vcpu_register_ti* VMASST_TYPE_runstate_update_flag engaged by the domain. */ #define VCPUOP_register_runstate_phys_area 14 +#define VCPUOP_register_vcpu_time_phys_area 15#endif /* __XEN_PUBLIC_VCPU_H__ */
PATCH v2 8/8 common: convert vCPU info area registration
In reply to Jan BeulichSwitch to using map_guest_area(). Noteworthy differences from map_vcpu_info():
- remote vCPU-s are paused rather than checked for being down (which in principle can change right after the check),
- the domain lock is taken for a much smaller region,
- the error code for an attempt to re-register the area is now -EBUSY,
- we could in principle permit de-registration when no area was previously registered (which would permit "probing", if necessary for anything).
Note that this eliminates a bug in copy_vcpu_settings(): The function did allocate a new page regardless of the GFN already having a mapping, thus in particular breaking the case of two vCPU-s having their info areas on the same page.
---
RFC: I'm not really certain whether the preliminary check (ahead of
calling map_guest_area()) is worthwhile to have.
---
v2: Re-base over changes earlier in the series. Properly enforce no re-
registration. Avoid several casts by introducing local variables.
xen/arch/x86/include/asm/shared.h +13 -6
@@ -26,17 +26,20 @@ static inline void arch_set_##field(stru#define GET_SET_VCPU (type, field) \ static inline type arch_get_##field(const struct vcpu *v) \ { \ + const vcpu_info_t *vi = v->vcpu_info_area.map; \ + \ return !has_32bit_shinfo(v->domain) ? \ - v->vcpu_info->native.arch.field : \ - v->vcpu_info->compat.arch.field; \ + vi->native.arch.field : vi->compat.arch.field; \ } \ static inline void arch_set_##field(struct vcpu *v, \ type val) \ { \ + vcpu_info_t *vi = v->vcpu_info_area.map; \ + \ if ( !has_32bit_shinfo(v->domain) ) \ - v->vcpu_info->native.arch.field = val; \ + vi->native.arch.field = val; \ else \ - v->vcpu_info->compat.arch.field = val; \ + vi->compat.arch.field = val; \ }#else
@@ -57,12 +60,16 @@ static inline void arch_set_##field(stru#define GET_SET_VCPU (type, field) \ static inline type arch_get_##field(const struct vcpu *v) \ { \ - return v->vcpu_info->arch.field; \ + const vcpu_info_t *vi = v->vcpu_info_area.map; \ + \ + return vi->arch.field; \ } \ static inline void arch_set_##field(struct vcpu *v, \ type val) \ { \ - v->vcpu_info->arch.field = val; \ + vcpu_info_t *vi = v->vcpu_info_area.map; \ + \ + vi->arch.field = val; \ }#endif
xen/arch/x86/mm/mem_sharing.c +8 -37
61 lines not on this page · raw message
xen/arch/x86/pv/shim.c +1 -1
@@ -395,7 +395,7 @@ int pv_shim_shutdown(uint8_t reason) for_each_vcpu ( d, v ) {/* Unmap guest vcpu_info page and runstate/time areas. */ -unmap_vcpu_info (v); +unmap_guest_area (v, &v->vcpu_info_area);unmap_guest_area (v, &v->runstate_guest_area);unmap_guest_area (v, &v->arch.time_guest_area);
xen/arch/x86/time.c +1 -1
@@ -1438,7 +1438,7 @@ static void __update_vcpu_system_time(ststruct vcpu_time_info *u = &vcpu_info (v, time), _u;const struct domain *d = v->domain; -if ( v->vcpu_info ==NULL ) +if ( !v->vcpu_info_area.map )return ;collect_time_info (v, &_u);
xen/arch/x86/x86_64/asm-offsets.c +1 -1
@@ -53,7 +53,7 @@ void __dummy__(void) OFFSET(VCPU_processor, struct vcpu, processor); OFFSET(VCPU_domain, struct vcpu, domain); - OFFSET(VCPU_vcpu_info, struct vcpu, vcpu_info); + OFFSET(VCPU_vcpu_info, struct vcpu, vcpu_info_area.map); OFFSET(VCPU_trap_bounce, struct vcpu, arch.pv.trap_bounce); OFFSET(VCPU_thread_flags, struct vcpu, arch.flags); OFFSET(VCPU_event_addr, struct vcpu, arch.pv.event_callback_eip);
xen/arch/x86/x86_64/traps.c +1 -1
@@ -97,7 +97,7 @@ static void _show_registers(if ( context == CTXT_hypervisor )printk (" %pS" ,_p (regs->rip));printk ("\nRFLAGS: %016lx " , regs->rflags); -if ( (context == CTXT_pv_guest) && v && v->vcpu_info ) +if ( (context == CTXT_pv_guest) && v && v->vcpu_info_area.map )printk ("EM: %d " , !!vcpu_info (v, evtchn_upcall_mask));printk ("CONTEXT: %s" , context_names[context]);if ( v && !is_idle_vcpu (v) )
xen/common/compat/domain.c +1 -1
@@ -49,7 +49,7 @@ int compat_common_vcpu_op(int cmd, struc {case VCPUOP_initialise: { -if ( v->vcpu_info == &dummy_vcpu_info ) +if ( v->vcpu_info_area.map == &dummy_vcpu_info )return -EINVAL ;#ifdef CONFIG_HVM
xen/common/domain.c +83 -121
268 lines not on this page · raw message
xen/include/xen/domain.h +0 -3
@@ -79,9 +79,6 @@ void cf_check free_pirq_struct(void *);int arch_vcpu_create (struct vcpu *v);void arch_vcpu_destroy (struct vcpu *v); -int map_vcpu_info (struct vcpu *v,unsigned long gfn,unsigned int offset); -void unmap_vcpu_info (struct vcpu *v); -int map_guest_area (struct vcpu *v,paddr_t gaddr,unsigned int size,struct guest_area *area,void (*populate)(void *dst,struct vcpu *v));
xen/include/xen/sched.h +1 -4
@@ -175,7 +175,7 @@ struct vcpuint processor; -vcpu_info_t *vcpu_info; +struct guest_area vcpu_info_area;struct domain *domain;
@@ -288,9 +288,6 @@ struct vcpustruct waitqueue_vcpu *waitqueue_vcpu; -/* Guest-specified relocation of vcpu_info. */ -mfn_t vcpu_info_mfn; -struct evtchn_fifo_vcpu *evtchn_fifo;/* vPCI per-vCPU area, used to store data for long running operations. */
xen/include/xen/shared.h +2 -1
@@ -44,6 +44,7 @@ typedef struct vcpu_info vcpu_info_t;extern vcpu_info_t dummy_vcpu_info;#define shared_info (d, field) __shared_info(d, (d)->shared_info, field) -#define vcpu_info (v, field) __vcpu_info(v, (v)->vcpu_info, field) +#define vcpu_info (v, field) \ + __vcpu_info(v, (vcpu_info_t *)(v)->vcpu_info_area.map, field)#endif /* __XEN_SHARED_H__ */