x86/altp2m: fix HVMOP_altp2m_set_domain_state race
PATCH v2 1/1 x86/altp2m: fix HVMOP_altp2m_set_domain_state race
HVMOP_altp2m_set_domain_state does not domain_pause(), presumably on purpose (as it was originally supposed to cater to a in-guest agent, and a domain pausing itself is not a good idea).
This can lead to domain crashes in the vmx_vmexit_handler() code that checks if the guest has the ability to switch EPTP without an exit. That code can __vmread() the host p2m's EPT_POINTER (before HVMOP_altp2m_set_domain_state "for_each_vcpu()" has a chance to run altp2m_vcpu_initialise(), but after d->arch.altp2m_active is set).
This patch reorganizes the code so that d->arch.altp2m_active is set to true only after all the init work has been done, and to false before the uninit work begins. This required adding a new bool parameter altp2m_vcpu_update_p2m(), which relied on d->arch.altp2m_active being set before it's called.
While at it, I've changed a couple of bool_t's to bool's.
--- xen/arch/x86/hvm/hvm.c | 17 ++++++++++++++--- xen/arch/x86/hvm/vmx/vmx.c | 4 ++-- xen/arch/x86/mm/altp2m.c | 4 ++-- xen/arch/x86/mm/p2m.c | 4 ++-- xen/include/asm-x86/altp2m.h | 2 +- xen/include/asm-x86/domain.h | 2 +- xen/include/asm-x86/hvm/hvm.h | 6 +++--- 7 files changed, 25 insertions(+), 14 deletions(-)
xen/arch/x86/hvm/hvm.c +14 -3
@@ -4525,7 +4525,7 @@ static int do_altp2m_op(case HVMOP_altp2m_set_domain_state: {struct vcpu *v; -bool_t ostate; +bool ostate, nstate;if (nestedhvm_enabled (d) ) {
@@ -4534,12 +4534,16 @@ static int do_altp2m_op( } ostate = d->arch.altp2m_active; - d->arch.altp2m_active = !!a.u.domain_state.state; + nstate = !!a.u.domain_state.state;/* If the alternate p2m state has changed, handle appropriately */ -if ( d->arch.altp2m_active != ostate && +if ( nstate != ostate && (ostate || !(rc =p2m_init_altp2m_by_id (d,0 ))) ) { +/* First mark altp2m as disabled, then altp2m_vcpu_destroy(). */ +if ( ostate ) + d->arch.altp2m_active = false; +for_each_vcpu ( d, v ) {if ( !ostate )
@@ -4550,7 +4554,14 @@ static int do_altp2m_op(if ( ostate )p2m_flush_altp2m (d); +else +/* +* Wait until altp2m_vcpu_initialise() is done before marking +* altp2m as being enabled for the domain. +*/ + d->arch.altp2m_active = true; } +break ; }
xen/arch/x86/hvm/vmx/vmx.c +2 -2
@@ -2150,13 +2150,13 @@ static bool_t vmx_is_singlestep_supported(void)return !!cpu_has_monitor_trap_flag; } -static void vmx_vcpu_update_eptp (struct vcpu *v) +static void vmx_vcpu_update_eptp (struct vcpu *v,bool altp2m_enabled) {struct domain *d = v->domain;struct p2m_domain *p2m =NULL ;struct ept_data *ept; -if (altp2m_active (d) ) +if ( altp2m_enabled ) p2m =p2m_get_altp2m (v);if ( !p2m ) p2m =p2m_get_hostp2m (d);
xen/arch/x86/mm/altp2m.c +2 -2
@@ -39,7 +39,7 @@ altp2m_vcpu_initialise(struct vcpu *v)vcpu_altp2m (v).p2midx =0 ;atomic_inc (&p2m_get_altp2m (v)->active_vcpus); -altp2m_vcpu_update_p2m (v); +altp2m_vcpu_update_p2m (v, true);if ( v != current )vcpu_unpause (v);
@@ -58,7 +58,7 @@ altp2m_vcpu_destroy(struct vcpu *v)altp2m_vcpu_reset (v); -altp2m_vcpu_update_p2m (v); +altp2m_vcpu_update_p2m (v, false);altp2m_vcpu_update_vmfunc_ve (v);if ( v != current )
xen/arch/x86/mm/p2m.c +2 -2
@@ -2332,7 +2332,7 @@ bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, unsigned int idx)atomic_dec (&p2m_get_altp2m (v)->active_vcpus);vcpu_altp2m (v).p2midx = idx;atomic_inc (&p2m_get_altp2m (v)->active_vcpus); -altp2m_vcpu_update_p2m (v); +altp2m_vcpu_update_p2m (v,altp2m_active (d)); } rc =1 ; }
@@ -2573,7 +2573,7 @@ int p2m_switch_domain_altp2m_by_id(struct domain *d, unsigned int idx)atomic_dec (&p2m_get_altp2m (v)->active_vcpus);vcpu_altp2m (v).p2midx = idx;atomic_inc (&p2m_get_altp2m (v)->active_vcpus); -altp2m_vcpu_update_p2m (v); +altp2m_vcpu_update_p2m (v,altp2m_active (d)); } rc =0 ;
xen/include/asm-x86/altp2m.h +1 -1
@@ -33,7 +33,7 @@ static inline bool altp2m_active(const struct domain *d)/* Alternate p2m VCPU */ void altp2m_vcpu_initialise (struct vcpu *v);void altp2m_vcpu_destroy (struct vcpu *v); -void altp2m_vcpu_reset (struct vcpu *v); +void altp2m_vcpu_disable_ve (struct vcpu *v);static inline uint16_t altp2m_vcpu_idx (const struct vcpu *v) {
xen/include/asm-x86/domain.h +1 -1
@@ -337,7 +337,7 @@ struct arch_domainmm_lock_t nested_p2m_lock;/* altp2m: allow multiple copies of host p2m */ -bool_t altp2m_active; +bool altp2m_active;struct p2m_domain *altp2m_p2m[MAX_ALTP2M ];mm_lock_t altp2m_list_lock;uint64_t *altp2m_eptp;
xen/include/asm-x86/hvm/hvm.h +3 -3
@@ -214,7 +214,7 @@ struct hvm_function_table {bool_t (*is_singlestep_supported)(void);/* Alternate p2m */ -void (*altp2m_vcpu_update_p2m)(struct vcpu *v); +void (*altp2m_vcpu_update_p2m)(struct vcpu *v,bool altp2m_enabled);void (*altp2m_vcpu_update_vmfunc_ve)(struct vcpu *v);bool_t (*altp2m_vcpu_emulate_ve)(struct vcpu *v);int (*altp2m_vcpu_emulate_vmfunc)(const struct cpu_user_regs *regs);
@@ -639,10 +639,10 @@ static inline bool hvm_altp2m_supported(void) }/* updates the current hardware p2m */ -static inline void altp2m_vcpu_update_p2m (struct vcpu *v) +static inline void altp2m_vcpu_update_p2m (struct vcpu *v,bool altp2m_enabled) {if ( hvm_funcs.altp2m_vcpu_update_p2m ) - hvm_funcs.altp2m_vcpu_update_p2m (v); + hvm_funcs.altp2m_vcpu_update_p2m (v, altp2m_enabled); }/* updates VMCS fields related to VMFUNC and #VE */
Signature · 7 lines
2.7.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 146 lines · Razvan Cojocaru
HVMOP_altp2m_set_domain_statedoes notdomain_pause(), presumably on purpose (as it was originally supposed to cater to a in-guest agent, and a domain pausing itself is not a good idea).This can lead to domain crashes in the
vmx_vmexit_handler()code that checks if the guest has the ability to switch EPTP without an exit. That code can__vmread()the host p2m'sEPT_POINTER(beforeHVMOP_altp2m_set_domain_state"for_each_vcpu()" has a chance to runaltp2m_vcpu_initialise(), but afterd->arch.altp2m_activeis set).This patch reorganizes the code so that
d->arch.altp2m_activeis set to true only after all the init work has been done, and to false before the uninit work begins. This required adding a new bool parameteraltp2m_vcpu_update_p2m(), which relied ond->arch.altp2m_activebeing set before it's called.While at it, I've changed a couple of bool_t's to bool's.
Signed-off-by Razvan Cojocaru <[email protected]>--- xen/arch/x86/hvm/hvm.c | 17 ++++++++++++++--- xen/arch/x86/hvm/vmx/vmx.c | 4 ++-- xen/arch/x86/mm/altp2m.c | 4 ++-- xen/arch/x86/mm/p2m.c | 4 ++-- xen/include/asm-x86/altp2m.h | 2 +- xen/include/asm-x86/domain.h | 2 +- xen/include/asm-x86/hvm/hvm.h | 6 +++--- 7 files changed, 25 insertions(+), 14 deletions(-)xen/arch/x86/hvm/hvm.c +14
-3@@ -4525,7 +4525,7 @@ static int do_altp2m_op(case HVMOP_altp2m_set_domain_state: {struct vcpu *v; -bool_t ostate; +bool ostate, nstate;if (nestedhvm_enabled (d) ) {@@ -4534,12 +4534,16 @@ static int do_altp2m_op( } ostate = d->arch.altp2m_active; - d->arch.altp2m_active = !!a.u.domain_state.state; + nstate = !!a.u.domain_state.state;/* If the alternate p2m state has changed, handle appropriately */ -if ( d->arch.altp2m_active != ostate && +if ( nstate != ostate && (ostate || !(rc =p2m_init_altp2m_by_id (d,0 ))) ) { +/* First mark altp2m as disabled, then altp2m_vcpu_destroy(). */ +if ( ostate ) + d->arch.altp2m_active = false; +for_each_vcpu ( d, v ) {if ( !ostate )@@ -4550,7 +4554,14 @@ static int do_altp2m_op(if ( ostate )p2m_flush_altp2m (d); +else +/* +* Wait until altp2m_vcpu_initialise() is done before marking +* altp2m as being enabled for the domain. +*/ + d->arch.altp2m_active = true; } +break ; }xen/arch/x86/hvm/vmx/vmx.c +2
-2@@ -2150,13 +2150,13 @@ static bool_t vmx_is_singlestep_supported(void)return !!cpu_has_monitor_trap_flag; } -static void vmx_vcpu_update_eptp (struct vcpu *v) +static void vmx_vcpu_update_eptp (struct vcpu *v,bool altp2m_enabled) {struct domain *d = v->domain;struct p2m_domain *p2m =NULL ;struct ept_data *ept; -if (altp2m_active (d) ) +if ( altp2m_enabled ) p2m =p2m_get_altp2m (v);if ( !p2m ) p2m =p2m_get_hostp2m (d);xen/arch/x86/mm/altp2m.c +2
-2@@ -39,7 +39,7 @@ altp2m_vcpu_initialise(struct vcpu *v)vcpu_altp2m (v).p2midx =0 ;atomic_inc (&p2m_get_altp2m (v)->active_vcpus); -altp2m_vcpu_update_p2m (v); +altp2m_vcpu_update_p2m (v, true);if ( v != current )vcpu_unpause (v);@@ -58,7 +58,7 @@ altp2m_vcpu_destroy(struct vcpu *v)altp2m_vcpu_reset (v); -altp2m_vcpu_update_p2m (v); +altp2m_vcpu_update_p2m (v, false);altp2m_vcpu_update_vmfunc_ve (v);if ( v != current )xen/arch/x86/mm/p2m.c +2
-2@@ -2332,7 +2332,7 @@ bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, unsigned int idx)atomic_dec (&p2m_get_altp2m (v)->active_vcpus);vcpu_altp2m (v).p2midx = idx;atomic_inc (&p2m_get_altp2m (v)->active_vcpus); -altp2m_vcpu_update_p2m (v); +altp2m_vcpu_update_p2m (v,altp2m_active (d)); } rc =1 ; }@@ -2573,7 +2573,7 @@ int p2m_switch_domain_altp2m_by_id(struct domain *d, unsigned int idx)atomic_dec (&p2m_get_altp2m (v)->active_vcpus);vcpu_altp2m (v).p2midx = idx;atomic_inc (&p2m_get_altp2m (v)->active_vcpus); -altp2m_vcpu_update_p2m (v); +altp2m_vcpu_update_p2m (v,altp2m_active (d)); } rc =0 ;xen/include/asm-x86/altp2m.h +0
-0
xen/include/asm-x86/altp2m.h static inline bool altp2m_active(const struct domain *d) · line 36
Quoted 5 lines
xen/include/asm-x86/altp2m.h +1
-1@@ -33,7 +33,7 @@ static inline bool altp2m_active(const struct domain *d)/* Alternate p2m VCPU */ void altp2m_vcpu_initialise (struct vcpu *v);void altp2m_vcpu_destroy (struct vcpu *v); -void altp2m_vcpu_reset (struct vcpu *v); +void altp2m_vcpu_disable_ve (struct vcpu *v);
Sorry, this is a leftover, unrelated change. I'll remove it in the next version, first I'll wait for other comments.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 08.02.19 at 15:00, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +1
-1@@ -4525,7 +4525,7 @@ static int do_altp2m_op(case HVMOP_altp2m_set_domain_state: {struct vcpu *v; -bool_t ostate; +bool ostate, nstate;if (nestedhvm_enabled (d) ) {
xen/arch/x86/hvm/hvm.c static int do_altp2m_op( · line 4537
Quoted 5 lines
xen/arch/x86/hvm/hvm.c +1
-1@@ -4534,12 +4534,16 @@ static int do_altp2m_op( } ostate = d->arch.altp2m_active; - d->arch.altp2m_active = !!a.u.domain_state.state; + nstate = !!a.u.domain_state.state;
No need for !! here.
xen/arch/x86/hvm/hvm.c static int do_altp2m_op( · line 4545
Quoted 8 lines
xen/arch/x86/hvm/hvm.c +4
-1/* If the alternate p2m state has changed, handle appropriately */ -if ( d->arch.altp2m_active != ostate && +if ( nstate != ostate && (ostate || !(rc =p2m_init_altp2m_by_id (d,0 ))) ) { +/* First mark altp2m as disabled, then altp2m_vcpu_destroy(). */ +if ( ostate ) + d->arch.altp2m_active = false;
Why the if()? In the opposite case you'd simply write false into what already holds false.
xen/arch/x86/hvm/hvm.c static int do_altp2m_op( · line 4562
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +6
-0@@ -4550,7 +4554,14 @@ static int do_altp2m_op(if ( ostate )p2m_flush_altp2m (d); +else +/* +* Wait until altp2m_vcpu_initialise() is done before marking +* altp2m as being enabled for the domain. +*/ + d->arch.altp2m_active = true;
Similarly here you could omit the "else" and simply store "nstate" afaict.
Quoted 8 lines
xen/arch/x86/hvm/vmx/vmx.c +1
-1@@ -2150,13 +2150,13 @@ static bool_t vmx_is_singlestep_supported(void)return !!cpu_has_monitor_trap_flag; } -static void vmx_vcpu_update_eptp (struct vcpu *v) +static void vmx_vcpu_update_eptp (struct vcpu *v,bool altp2m_enabled)
xen/arch/x86/hvm/vmx/vmx.c static bool_t vmx_is_singlestep_supported(void) · line 2162
Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +1
-1{struct domain *d = v->domain;struct p2m_domain *p2m =NULL ;struct ept_data *ept; -if (altp2m_active (d) ) +if ( altp2m_enabled ) p2m =p2m_get_altp2m (v);if ( !p2m ) p2m =p2m_get_hostp2m (d);
Is this an appropriate transformation? That is, can there not be any domains for which altp2m_active() returns false despite altp2m_enabled being true? (Looking at p2m_get_altp2m() I can't really judge whether index would always be INVALID_ALTP2M in such a case.)
Quoted 12 lines
xen/arch/x86/mm/p2m.c +1
-1@@ -2332,7 +2332,7 @@ bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, unsigned int idx)atomic_dec (&p2m_get_altp2m (v)->active_vcpus);vcpu_altp2m (v).p2midx = idx;atomic_inc (&p2m_get_altp2m (v)->active_vcpus); -altp2m_vcpu_update_p2m (v); +altp2m_vcpu_update_p2m (v,altp2m_active (d)); } rc =1 ; }
xen/arch/x86/mm/p2m.c int p2m_switch_domain_altp2m_by_id(struct domain *d, unsigned int idx) · line 2577
Quoted 6 lines
xen/arch/x86/mm/p2m.c +1
-1@@ -2573,7 +2573,7 @@ int p2m_switch_domain_altp2m_by_id(struct domain *d, unsigned int idx)atomic_dec (&p2m_get_altp2m (v)->active_vcpus);vcpu_altp2m (v).p2midx = idx;atomic_inc (&p2m_get_altp2m (v)->active_vcpus); -altp2m_vcpu_update_p2m (v); +altp2m_vcpu_update_p2m (v,altp2m_active (d)); }
In both cases, isn't altp2m_active() going to return true anyway when we get there (related to the question above)?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 08.02.19 at 15:00, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +1
-1@@ -4525,7 +4525,7 @@ static int do_altp2m_op(case HVMOP_altp2m_set_domain_state: {struct vcpu *v; -bool_t ostate; +bool ostate, nstate;if (nestedhvm_enabled (d) ) {@@ -4534,12 +4534,16 @@ static int do_altp2m_op( }
Quoted 5 lines
xen/arch/x86/hvm/hvm.c static int do_altp2m_op( · line 4537
Quoted 3 lines
xen/arch/x86/hvm/hvm.c +1
-1ostate = d->arch.altp2m_active; - d->arch.altp2m_active = !!a.u.domain_state.state; + nstate = !!a.u.domain_state.state;No need for !! here.
I'll remove it.
Quoted 11 lines
xen/arch/x86/hvm/hvm.c static int do_altp2m_op( · line 4545
Quoted 8 lines
xen/arch/x86/hvm/hvm.c +4
-1/* If the alternate p2m state has changed, handle appropriately */ -if ( d->arch.altp2m_active != ostate && +if ( nstate != ostate && (ostate || !(rc =p2m_init_altp2m_by_id (d,0 ))) ) { +/* First mark altp2m as disabled, then altp2m_vcpu_destroy(). */ +if ( ostate ) + d->arch.altp2m_active = false;Why the
if()? In the opposite case you'd simply write false into what already holds false.
The value written into d->arch.altp2m_active is not the point here. The point is that if ( ostate ), then we are disabling altp2m (because the if above this one makes sure ostate != nstate).
So in the disable case, first I wanted to set d->arch.altp2m_active to false (which immediately causes altp2m_active(d) to return false as well), and then actually perform the work.
Quoted 12 lines
xen/arch/x86/hvm/hvm.c static int do_altp2m_op( · line 4562
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +6
-0@@ -4550,7 +4554,14 @@ static int do_altp2m_op(if ( ostate )p2m_flush_altp2m (d); +else +/* +* Wait until altp2m_vcpu_initialise() is done before marking +* altp2m as being enabled for the domain. +*/ + d->arch.altp2m_active = true;Similarly here you could omit the "else" and simply store "nstate" afaict.
As above, in the enable case I wanted to first setup altp2m on all VCPUs with altp2m_vcpu_initialise(), and only after that set d->arch.altp2m_active = true.
In summary, if ( ostate ) we want to set d->arch.altp2m_active before the for (we're disabling altp2m), and if ( !ostate ) (which is the else above) we want to set d->arch.altp2m_active after said for.
We can indeed store nstate. I just thought things look clearer with "true" and "false", but if you prefer there's no problem assigning nstate.
Quoted 13 lines
Quoted 13 lines
xen/arch/x86/hvm/vmx/vmx.c +1
-1@@ -2150,13 +2150,13 @@ static bool_t vmx_is_singlestep_supported(void)return !!cpu_has_monitor_trap_flag; } -static void vmx_vcpu_update_eptp (struct vcpu *v) +static void vmx_vcpu_update_eptp (struct vcpu *v,bool altp2m_enabled) {struct domain *d = v->domain;struct p2m_domain *p2m =NULL ;struct ept_data *ept;
Quoted 11 lines
xen/arch/x86/hvm/vmx/vmx.c static bool_t vmx_is_singlestep_supported(void) · line 2162
Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +1
-1-if (altp2m_active (d) ) +if ( altp2m_enabled ) p2m =p2m_get_altp2m (v);if ( !p2m ) p2m =p2m_get_hostp2m (d);Is this an appropriate transformation? That is, can there not be any domains for which
altp2m_active()returns false despitealtp2m_enabledbeing true? (Looking atp2m_get_altp2m()I can't really judge whether index would always beINVALID_ALTP2Min such a case.)
Yes, it should be completely safe (please see below for details).
Quoted 13 lines
Quoted 13 lines
xen/arch/x86/mm/p2m.c +1
-1@@ -2332,7 +2332,7 @@ bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, unsigned int idx)atomic_dec (&p2m_get_altp2m (v)->active_vcpus);vcpu_altp2m (v).p2midx = idx;atomic_inc (&p2m_get_altp2m (v)->active_vcpus); -altp2m_vcpu_update_p2m (v); +altp2m_vcpu_update_p2m (v,altp2m_active (d)); } rc =1 ; }@@ -2573,7 +2573,7 @@ int p2m_switch_domain_altp2m_by_id(struct domain *d, unsigned int idx)atomic_dec (&p2m_get_altp2m (v)->active_vcpus);
Quoted 8 lines
xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, unsigned int idx) · line 2336
Quoted 5 lines
xen/arch/x86/mm/p2m.c +1
-1vcpu_altp2m (v).p2midx = idx;atomic_inc (&p2m_get_altp2m (v)->active_vcpus); -altp2m_vcpu_update_p2m (v); +altp2m_vcpu_update_p2m (v,altp2m_active (d)); }In both cases, isn't
altp2m_active()going to return true anyway when we get there (related to the question above)?
Yes, it will return true. In order for it to return false, altp2m_vcpu_destroy() would have had to run on that VCPU, which (among other things) calls altp2m_vcpu_reset(), which does v->p2midx = INVALID_ALTP2M.
There's an if() above (not shown in your reply) that says "if ( idx != vcpu_altp2m(v).p2midx )", so, indeed, by the time we end up here we can reasonably assume that altp2m_active(d) will return true.
I've just put in "altp2m_active(d)" to make sure there's absolutely no functional change here, but AFAICT it can be safely replaced with "true".
Thanks,
Razvan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 08.02.19 at 17:30, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
xen/arch/x86/hvm/hvm.c static int do_altp2m_op( · line 4545
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 08.02.19 at 15:00, <[email protected]> wrote:
/* If the alternate p2m state has changed, handle appropriately */xen/arch/x86/hvm/hvm.c +4
-1-if ( d->arch.altp2m_active != ostate && +if ( nstate != ostate && (ostate || !(rc =p2m_init_altp2m_by_id (d,0 ))) ) { +/* First mark altp2m as disabled, then altp2m_vcpu_destroy(). */ +if ( ostate ) + d->arch.altp2m_active = false;Quoted 2 lines
Why the
if()? In the opposite case you'd simply write false into what already holds false.
Quoted 7 lines
The value written into
d->arch.altp2m_activeis not the point here. The point is that if ( ostate ), then we are disabling altp2m (because the if above this one makes sure ostate != nstate).So in the disable case, first I wanted to set
d->arch.altp2m_activeto false (which immediately causesaltp2m_active(d)to return false as well), and then actually perform the work.
Sure - I'm not asking to remove everything above, just the if() (and keep what is now the body). That is because
- in the enable case the value is false and writing false into it is benign,
- in the disable case you want to actively change from true to false.
Quoted 17 lines
Quoted 12 lines
xen/arch/x86/hvm/hvm.c static int do_altp2m_op( · line 4562
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +6
-0@@ -4550,7 +4554,14 @@ static int do_altp2m_op(if ( ostate )p2m_flush_altp2m (d); +else +/* +* Wait until altp2m_vcpu_initialise() is done before marking +* altp2m as being enabled for the domain. +*/ + d->arch.altp2m_active = true;Similarly here you could omit the "else" and simply store "nstate" afaict.
As above, in the enable case I wanted to first setup altp2m on all VCPUs with
altp2m_vcpu_initialise(), and only after that setd->arch.altp2m_active= true.
Quoted 6 lines
In summary, if ( ostate ) we want to set
d->arch.altp2m_activebefore the for (we're disabling altp2m), and if ( !ostate ) (which is the else above) we want to setd->arch.altp2m_activeafter said for.We can indeed store nstate. I just thought things look clearer with "true" and "false", but if you prefer there's no problem assigning nstate.
Well, as always with mm and altp2m code, I'm not the maintainer, so I don't have the final say. It's just that I think unnecessary conditionals would better be avoided, even if they're not on a performance critical path.
Quoted 49 lines
Quoted 13 lines
Quoted 13 lines
xen/arch/x86/hvm/vmx/vmx.c +1
-1@@ -2150,13 +2150,13 @@ static bool_t vmx_is_singlestep_supported(void)return !!cpu_has_monitor_trap_flag; } -static void vmx_vcpu_update_eptp (struct vcpu *v) +static void vmx_vcpu_update_eptp (struct vcpu *v,bool altp2m_enabled) {struct domain *d = v->domain;struct p2m_domain *p2m =NULL ;struct ept_data *ept;Quoted 11 lines
xen/arch/x86/hvm/vmx/vmx.c static bool_t vmx_is_singlestep_supported(void) · line 2162
Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +1
-1-if (altp2m_active (d) ) +if ( altp2m_enabled ) p2m =p2m_get_altp2m (v);if ( !p2m ) p2m =p2m_get_hostp2m (d);Is this an appropriate transformation? That is, can there not be any domains for which
altp2m_active()returns false despitealtp2m_enabledbeing true? (Looking atp2m_get_altp2m()I can't really judge whether index would always beINVALID_ALTP2Min such a case.)Yes, it should be completely safe (please see below for details).
Quoted 13 lines
Quoted 13 lines
xen/arch/x86/mm/p2m.c +1
-1@@ -2332,7 +2332,7 @@ bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, unsigned int idx)atomic_dec (&p2m_get_altp2m (v)->active_vcpus);vcpu_altp2m (v).p2midx = idx;atomic_inc (&p2m_get_altp2m (v)->active_vcpus); -altp2m_vcpu_update_p2m (v); +altp2m_vcpu_update_p2m (v,altp2m_active (d)); } rc =1 ; }@@ -2573,7 +2573,7 @@ int p2m_switch_domain_altp2m_by_id(struct domain *d, unsigned int idx)atomic_dec (&p2m_get_altp2m (v)->active_vcpus);Quoted 8 lines
xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, unsigned int idx) · line 2336
Quoted 5 lines
xen/arch/x86/mm/p2m.c +1
-1vcpu_altp2m (v).p2midx = idx;atomic_inc (&p2m_get_altp2m (v)->active_vcpus); -altp2m_vcpu_update_p2m (v); +altp2m_vcpu_update_p2m (v,altp2m_active (d)); }In both cases, isn't
altp2m_active()going to return true anyway when we get there (related to the question above)?
Quoted 4 lines
Yes, it will return true. In order for it to return false,
altp2m_vcpu_destroy()would have had to run on that VCPU, which (among other things) callsaltp2m_vcpu_reset(), which doesv->p2midx=INVALID_ALTP2M.
Hmm, according to the change further up in this patch, altp2m_active() returns false before v->p2midx can be set to INVALID_ALTP2M (you don't change this property). So (related to the change further up) there's a period of time during which p2m_get_altp2m() will return non-NULL despite altp2m_active() returning false. Afaict, that is.
Jan
Quoted 10 lines
There's an
if()above (not shown in your reply) that says "if ( idx != vcpu_altp2m(v).p2midx )", so, indeed, by the time we end up here we can reasonably assume thataltp2m_active(d)will return true.I've just put in "
altp2m_active(d)" to make sure there's absolutely no functional change here, but AFAICT it can be safely replaced with "true".Thanks,
Razvan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 113 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 08.02.19 at 17:30, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
xen/arch/x86/hvm/hvm.c static int do_altp2m_op( · line 4545
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 08.02.19 at 15:00, <[email protected]> wrote:
/* If the alternate p2m state has changed, handle appropriately */xen/arch/x86/hvm/hvm.c +4
-1-if ( d->arch.altp2m_active != ostate && +if ( nstate != ostate && (ostate || !(rc =p2m_init_altp2m_by_id (d,0 ))) ) { +/* First mark altp2m as disabled, then altp2m_vcpu_destroy(). */ +if ( ostate ) + d->arch.altp2m_active = false;Quoted 2 lines
Why the
if()? In the opposite case you'd simply write false into what already holds false.Quoted 7 lines
The value written into
d->arch.altp2m_activeis not the point here. The point is that if ( ostate ), then we are disabling altp2m (because the if above this one makes sure ostate != nstate).So in the disable case, first I wanted to set
d->arch.altp2m_activeto false (which immediately causesaltp2m_active(d)to return false as well), and then actually perform the work.Sure - I'm not asking to remove everything above, just the
if()(and keep what is now the body). That is because
- in the enable case the value is false and writing false into it is benign,
- in the disable case you want to actively change from true to false.Quoted 17 lines
Quoted 12 lines
xen/arch/x86/hvm/hvm.c static int do_altp2m_op( · line 4562
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +6
-0@@ -4550,7 +4554,14 @@ static int do_altp2m_op(if ( ostate )p2m_flush_altp2m (d); +else +/* +* Wait until altp2m_vcpu_initialise() is done before marking +* altp2m as being enabled for the domain. +*/ + d->arch.altp2m_active = true;Similarly here you could omit the "else" and simply store "nstate" afaict.
As above, in the enable case I wanted to first setup altp2m on all VCPUs with
altp2m_vcpu_initialise(), and only after that setd->arch.altp2m_active= true.Quoted 6 lines
In summary, if ( ostate ) we want to set
d->arch.altp2m_activebefore the for (we're disabling altp2m), and if ( !ostate ) (which is the else above) we want to setd->arch.altp2m_activeafter said for.We can indeed store nstate. I just thought things look clearer with "true" and "false", but if you prefer there's no problem assigning nstate.
Well, as always with mm and altp2m code, I'm not the maintainer, so I don't have the final say. It's just that I think unnecessary conditionals would better be avoided, even if they're not on a performance critical path.
Quoted 49 lines
Quoted 13 lines
Quoted 13 lines
xen/arch/x86/hvm/vmx/vmx.c +1
-1@@ -2150,13 +2150,13 @@ static bool_t vmx_is_singlestep_supported(void)return !!cpu_has_monitor_trap_flag; } -static void vmx_vcpu_update_eptp (struct vcpu *v) +static void vmx_vcpu_update_eptp (struct vcpu *v,bool altp2m_enabled) {struct domain *d = v->domain;struct p2m_domain *p2m =NULL ;struct ept_data *ept;Quoted 11 lines
xen/arch/x86/hvm/vmx/vmx.c static bool_t vmx_is_singlestep_supported(void) · line 2162
Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +1
-1-if (altp2m_active (d) ) +if ( altp2m_enabled ) p2m =p2m_get_altp2m (v);if ( !p2m ) p2m =p2m_get_hostp2m (d);Is this an appropriate transformation? That is, can there not be any domains for which
altp2m_active()returns false despitealtp2m_enabledbeing true? (Looking atp2m_get_altp2m()I can't really judge whether index would always beINVALID_ALTP2Min such a case.)Yes, it should be completely safe (please see below for details).
Quoted 13 lines
Quoted 13 lines
xen/arch/x86/mm/p2m.c +1
-1@@ -2332,7 +2332,7 @@ bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, unsigned int idx)atomic_dec (&p2m_get_altp2m (v)->active_vcpus);vcpu_altp2m (v).p2midx = idx;atomic_inc (&p2m_get_altp2m (v)->active_vcpus); -altp2m_vcpu_update_p2m (v); +altp2m_vcpu_update_p2m (v,altp2m_active (d)); } rc =1 ; }@@ -2573,7 +2573,7 @@ int p2m_switch_domain_altp2m_by_id(struct domain *d, unsigned int idx)atomic_dec (&p2m_get_altp2m (v)->active_vcpus);Quoted 8 lines
xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, unsigned int idx) · line 2336
Quoted 5 lines
xen/arch/x86/mm/p2m.c +1
-1vcpu_altp2m (v).p2midx = idx;atomic_inc (&p2m_get_altp2m (v)->active_vcpus); -altp2m_vcpu_update_p2m (v); +altp2m_vcpu_update_p2m (v,altp2m_active (d)); }In both cases, isn't
altp2m_active()going to return true anyway when we get there (related to the question above)?Quoted 4 lines
Yes, it will return true. In order for it to return false,
altp2m_vcpu_destroy()would have had to run on that VCPU, which (among other things) callsaltp2m_vcpu_reset(), which doesv->p2midx=INVALID_ALTP2M.
Quoted 5 lines
Hmm, according to the change further up in this patch,
altp2m_active()returns false beforev->p2midxcan be set toINVALID_ALTP2M(you don't change this property). So (related to the change further up) there's a period of time during whichp2m_get_altp2m()will return non-NULL despitealtp2m_active()returning false. Afaict, that is.
Right, I now understand what you meant by removing "if ( ostate )" - you'd like d->arch.altp2m_active to only be set _after_ the for, for the enable, as well as for the disable case.
However, I wanted to keep both if()s to avoid another problem with this code:
3655 /*
3656 * If the guest has the ability to switch EPTP without an exit, 3657 * figure out whether it has done so and update the altp2m data. 3658 */
3659 if ( altp2m_active(v->domain) &&
3660 (v->arch.hvm.vmx.secondary_exec_control &
3661 SECONDARY_EXEC_ENABLE_VM_FUNCTIONS) )
3662 {
3663 unsigned long idx;
3664
3665 if ( v->arch.hvm.vmx.secondary_exec_control &
3666 SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS )
3667 __vmread(EPTP_INDEX, &idx);
3668 else
3669 {
3670 unsigned long eptp;
3671
3672 __vmread(EPT_POINTER, &eptp);
3673
3674 if ( (idx = p2m_find_altp2m_by_eptp(v->domain, eptp)) ==
3675 INVALID_ALTP2M )
3676 {
3677 gdprintk(XENLOG_ERR, "EPTP not found in alternate
p2m list\n");
3678 domain_crash(v->domain);
3679
3680 return;
3681 }
3682 }
So for the disable case, I wanted altp2m_active(v->domain) to return false immediately so that this code won't be triggered. Otherwise, assuming the following scenario:
1. altp2m_vcpu_destroy() is called for a VCPU _after_ the if ( altp2m_active(v->domain) ) check is performed, but before __vmread(EPT_POINTER, &eptp).
2. The code now __vmread()s the host EPTP and crashes.
Now, you're right that p2m_get_altp2m() may hand over a pointer to an altp2m between the moment where d->arch.altp2m_active is false and the point where v->p2midx actually becomes INVALID_ALTP2M with my code; but I think it can be argued that I should rather fix p2m_get_altp2m(), to return NULL if altp2m_active() == false and keep the if()s (which I've hopefully been more eloquent about now).
Is that OK?
Thanks,
Razvan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 69 lines
Quoted 1 line
Quoted 1 line · On 08.02.19 at 18:02, <[email protected]> wrote:
Quoted 59 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 08.02.19 at 17:30, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
xen/arch/x86/hvm/hvm.c static int do_altp2m_op( · line 4545
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 08.02.19 at 15:00, <[email protected]> wrote:
/* If the alternate p2m state has changed, handle appropriately */xen/arch/x86/hvm/hvm.c +4
-1-if ( d->arch.altp2m_active != ostate && +if ( nstate != ostate && (ostate || !(rc =p2m_init_altp2m_by_id (d,0 ))) ) { +/* First mark altp2m as disabled, then altp2m_vcpu_destroy(). */ +if ( ostate ) + d->arch.altp2m_active = false;Quoted 2 lines
Why the
if()? In the opposite case you'd simply write false into what already holds false.Quoted 7 lines
The value written into
d->arch.altp2m_activeis not the point here. The point is that if ( ostate ), then we are disabling altp2m (because the if above this one makes sure ostate != nstate).So in the disable case, first I wanted to set
d->arch.altp2m_activeto false (which immediately causesaltp2m_active(d)to return false as well), and then actually perform the work.Sure - I'm not asking to remove everything above, just the
if()(and keep what is now the body). That is because
- in the enable case the value is false and writing false into it is benign,
- in the disable case you want to actively change from true to false.Quoted 17 lines
Quoted 12 lines
xen/arch/x86/hvm/hvm.c static int do_altp2m_op( · line 4562
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +6
-0@@ -4550,7 +4554,14 @@ static int do_altp2m_op(if ( ostate )p2m_flush_altp2m (d); +else +/* +* Wait until altp2m_vcpu_initialise() is done before marking +* altp2m as being enabled for the domain. +*/ + d->arch.altp2m_active = true;Similarly here you could omit the "else" and simply store "nstate" afaict.
As above, in the enable case I wanted to first setup altp2m on all VCPUs with
altp2m_vcpu_initialise(), and only after that setd->arch.altp2m_active= true.Quoted 6 lines
In summary, if ( ostate ) we want to set
d->arch.altp2m_activebefore the for (we're disabling altp2m), and if ( !ostate ) (which is the else above) we want to setd->arch.altp2m_activeafter said for.We can indeed store nstate. I just thought things look clearer with "true" and "false", but if you prefer there's no problem assigning nstate.
Well, as always with mm and altp2m code, I'm not the maintainer, so I don't have the final say. It's just that I think unnecessary conditionals would better be avoided, even if they're not on a performance critical path.
[...]
Quoted 6 lines
Hmm, according to the change further up in this patch,
altp2m_active()returns false beforev->p2midxcan be set toINVALID_ALTP2M(you don't change this property). So (related to the change further up) there's a period of time during whichp2m_get_altp2m()will return non-NULL despitealtp2m_active()returning false. Afaict, that is.
Quoted 3 lines
Right, I now understand what you meant by removing "if ( ostate )" - you'd like
d->arch.altp2m_activeto only be set _after_ the for, for the enable, as well as for the disable case.
No, certainly not. Exactly because of ...
Quoted 6 lines
However, I wanted to keep both if()s to avoid another problem with this code:
[...]
So for the disable case, I wantedaltp2m_active(v->domain)to return false immediately so that this code won't be triggered. Otherwise, assuming the following scenario:
... this. Apparently "and keep what is now the body" was still not clear enough.
Taking your original code, I mean
if ( nstate != ostate &&
(ostate || !(rc = p2m_init_altp2m_by_id(d, 0))) )
{
/* First mark altp2m as disabled, then altp2m_vcpu_destroy(). */
d->arch.altp2m_active = false;
for_each_vcpu( d, v )
[...]
if ( ostate )
p2m_flush_altp2m(d);
/*
* Wait until altp2m_vcpu_initialise() is done before marking
* altp2m as being enabled for the domain.
*/
d->arch.altp2m_active = nstate;
}Quoted 6 lines
Now, you're right that
p2m_get_altp2m()may hand over a pointer to an altp2m between the moment whered->arch.altp2m_activeis false and the point wherev->p2midxactually becomesINVALID_ALTP2Mwith my code; but I think it can be argued that I should rather fixp2m_get_altp2m(), to return NULL ifaltp2m_active()== false and keep the if()s (which I've hopefully been more eloquent about now).
Yes, that looks to be an option, provided it doesn't violate assumptions elsewhere.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 29 lines
Quoted 5 lines
Right, I now understand what you meant by removing "if ( ostate )" - you'd like
d->arch.altp2m_activeto only be set _after_ the for, for the enable, as well as for the disable case.No, certainly not. Exactly because of ...
Quoted 6 lines
However, I wanted to keep both if()s to avoid another problem with this code:
[...]
So for the disable case, I wantedaltp2m_active(v->domain)to return false immediately so that this code won't be triggered. Otherwise, assuming the following scenario:... this. Apparently "and keep what is now the body" was still not clear enough.
Taking your original code, I mean
if ( nstate != ostate && (ostate || !(rc = p2m_init_altp2m_by_id(d, 0))) ) { /* First mark altp2m as disabled, then altp2m_vcpu_destroy(). */ d->arch.altp2m_active = false; for_each_vcpu( d, v ) [...]
Quoted 8 lines
if ( ostate )
p2m_flush_altp2m(d);
/*
* Wait until altp2m_vcpu_initialise() is done before marking
* altp2m as being enabled for the domain.
*/
d->arch.altp2m_active = nstate;
}Ah! Thanks for the clarification.
Quoted 9 lines
Quoted 6 lines
Now, you're right that
p2m_get_altp2m()may hand over a pointer to an altp2m between the moment whered->arch.altp2m_activeis false and the point wherev->p2midxactually becomesINVALID_ALTP2Mwith my code; but I think it can be argued that I should rather fixp2m_get_altp2m(), to return NULL ifaltp2m_active()== false and keep the if()s (which I've hopefully been more eloquent about now).Yes, that looks to be an option, provided it doesn't violate assumptions elsewhere.
It doesn't, AFAICT. Additionally, it can be argued that if code relies on p2m_get_altp2m() returning not-NULL when !altp2m_active(), said code is broken.
Thanks,
Razvan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel