x86/cacheattr: fix mtrr_pat_not_equal
PATCH 1/1 x86/cacheattr: fix mtrr_pat_not_equal
The function is supposed to return whether the MTRR and PAT state of two CPUs match. Currently this is not properly done because the test for the deftype and the enable bits required both the deftype and the enable bits to be different, while just one of those fields being different can already cause the MTRR states on the vCPU to not match.
Fix this by changing the AND into an OR instead, so that either the deftype or the enabled bits being different will cause the function to return mismatching state.
---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Juergen Gross <[email protected]>
---
This is a bugfix and should go into 4.11
--- xen/arch/x86/hvm/mtrr.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/mtrr.c +2 -3
@@ -492,9 +492,8 @@ bool_t mtrr_pat_not_equal(struct vcpu *vd, struct vcpu *vs)if ( res )return 1 ; -/* Test default type MSR. */ -if ( (md->def_type != ms->def_type) - && (md->enabled != ms->enabled) ) +/* Test default type MSR and the enable bits. */ +if ( (md->def_type != ms->def_type) || (md->enabled != ms->enabled) )return 1 ;/* Test PAT. */
Signature · 7 lines
2.17.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 11 lines · Roger Pau Monne
The function is supposed to return whether the MTRR and PAT state of two CPUs match. Currently this is not properly done because the test for the deftype and the enable bits required both the deftype and the enable bits to be different, while just one of those fields being different can already cause the MTRR states on the vCPU to not match.
Fix this by changing the AND into an OR instead, so that either the deftype or the enabled bits being different will cause the function to return mismatching state.
Signed-off-by Roger Pau Monné <[email protected]>
Juergen
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 17.05.18 at 11:48, <[email protected]> wrote:
The function is supposed to return whether the MTRR and PAT state of two CPUs match. Currently this is not properly done because the test for the deftype and the enable bits required both the deftype and the enable bits to be different, while just one of those fields being different can already cause the MTRR states on the vCPU to not match.
Fix this by changing the AND into an OR instead, so that either the deftype or the enabled bits being different will cause the function to return mismatching state.
This is by far not enough, but I didn't view the function as critical enough to warrant sending out the patch I have right away.
Jan
x86/HVM: correct mtrr_pat_not_equal()
The two vCPU-s differring in MTRR-enabled state means MTRR settings are not equal. Both vCPU-s having MTRRs disabled means only PAT needs to be compared. Along those lines for fixed range MTRRs. Differring variable range counts likewise mean settings are different overall.
Constify types and convert bool_t to bool.
xen/arch/x86/hvm/mtrr.c +31 -26
@@ -476,35 +476,40 @@ bool_t mtrr_var_range_msr_set(return 1 ; } -bool_t mtrr_pat_not_equal (struct vcpu *vd,struct vcpu *vs) +bool mtrr_pat_not_equal (const struct vcpu *vd,const struct vcpu *vs) { -struct mtrr_state *md = &vd->arch.hvm_vcpu.mtrr; -struct mtrr_state *ms = &vs->arch.hvm_vcpu.mtrr; -int32_t res; -uint8_t num_var_ranges = (uint8_t )md->mtrr_cap; - -/* Test fixed ranges. */ - res =memcmp (md->fixed_ranges, ms->fixed_ranges, -NUM_FIXED_RANGES *sizeof (mtrr_type)); -if ( res ) -return 1 ; - -/* Test var ranges. */ - res =memcmp (md->var_ranges, ms->var_ranges, - num_var_ranges*sizeof (struct mtrr_var_range )); -if ( res ) -return 1 ; - -/* Test default type MSR. */ -if ( (md->def_type != ms->def_type) - && (md->enabled != ms->enabled) ) -return 1 ; +const struct mtrr_state *md = &vd->arch.hvm_vcpu.mtrr; +const struct mtrr_state *ms = &vs->arch.hvm_vcpu.mtrr; -/* Test PAT. */ -if ( vd->arch.hvm_vcpu.pat_cr != vs->arch.hvm_vcpu.pat_cr ) -return 1 ; +if ( (md->enabled ^ ms->enabled) &2 ) +return true; + +if ( md->enabled &2 ) + { +unsigned int num_var_ranges = (uint8_t )md->mtrr_cap; + +/* Test default type MSR. */ +if ( md->def_type != ms->def_type ) +return true; + +/* Test fixed ranges. */ +if ( (md->enabled ^ ms->enabled) &1 ) +return true; + +if ( (md->enabled &1 ) && +memcmp (md->fixed_ranges, ms->fixed_ranges, +sizeof (md->fixed_ranges)) ) +return true; + +/* Test variable ranges. */ +if ( num_var_ranges != (uint8_t )ms->mtrr_cap || +memcmp (md->var_ranges, ms->var_ranges, + num_var_ranges *sizeof (*md->var_ranges)) ) +return true; + } -return 0 ; +/* Test PAT. */ +return vd->arch.hvm_vcpu.pat_cr != vs->arch.hvm_vcpu.pat_cr; }struct hvm_mem_pinned_cacheattr_range {
xen/include/asm-x86/mtrr.h +1 -1
@@ -92,6 +92,6 @@ extern void memory_type_changed(struct dextern bool_t pat_msr_set (uint64_t *pat,uint64_t msr);bool_t is_var_mtrr_overlapped (const struct mtrr_state *m); -bool_t mtrr_pat_not_equal (struct vcpu *vd,struct vcpu *vs); +bool mtrr_pat_not_equal (const struct vcpu *vd,const struct vcpu *vs);#endif /* __ASM_X86_MTRR_H__ */
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Roger Pau Monné
Reviewed-byQuoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 17.05.18 at 11:48, <[email protected]> wrote:
The function is supposed to return whether the MTRR and PAT state of two CPUs match. Currently this is not properly done because the test for the deftype and the enable bits required both the deftype and the enable bits to be different, while just one of those fields being different can already cause the MTRR states on the vCPU to not match.
Quoted 6 lines
Quoted 3 lines
Fix this by changing the AND into an OR instead, so that either the deftype or the enabled bits being different will cause the function to return mismatching state.
This is by far not enough, but I didn't view the function as critical enough to warrant sending out the patch I have right away.
I've also realized that the logic there is wonky and would return true in cases where the states are equal (ie: for example if fixed MTRRs contents are different but FE is disabled).
Just wanted to do a minimal change that prevents wrongly reporting that the state is equal when it's not (I think the other way around is not that critical).
You change LGTM, and fixes some obvious cases where the current code would return true even if the cache state is the same.
Quoted 11 lines
Jan
x86/HVM: correctmtrr_pat_not_equal()The two vCPU-s differring in MTRR-enabled state means MTRR settings are not equal. Both vCPU-s having MTRRs disabled means only PAT needs to be compared. Along those lines for fixed range MTRRs. Differring variable range counts likewise mean settings are different overall.
Constify types and convert
bool_tto bool.Signed-off-by Jan Beulich <[email protected]>
Quoted 48 lines
xen/arch/x86/hvm/mtrr.c +15
-25@@ -476,35 +476,40 @@ bool_t mtrr_var_range_msr_set(return 1 ; } -bool_t mtrr_pat_not_equal (struct vcpu *vd,struct vcpu *vs) +bool mtrr_pat_not_equal (const struct vcpu *vd,const struct vcpu *vs) { -struct mtrr_state *md = &vd->arch.hvm_vcpu.mtrr; -struct mtrr_state *ms = &vs->arch.hvm_vcpu.mtrr; -int32_t res; -uint8_t num_var_ranges = (uint8_t )md->mtrr_cap; - -/* Test fixed ranges. */ - res =memcmp (md->fixed_ranges, ms->fixed_ranges, -NUM_FIXED_RANGES *sizeof (mtrr_type)); -if ( res ) -return 1 ; - -/* Test var ranges. */ - res =memcmp (md->var_ranges, ms->var_ranges, - num_var_ranges*sizeof (struct mtrr_var_range )); -if ( res ) -return 1 ; - -/* Test default type MSR. */ -if ( (md->def_type != ms->def_type) - && (md->enabled != ms->enabled) ) -return 1 ; +const struct mtrr_state *md = &vd->arch.hvm_vcpu.mtrr; +const struct mtrr_state *ms = &vs->arch.hvm_vcpu.mtrr; -/* Test PAT. */ -if ( vd->arch.hvm_vcpu.pat_cr != vs->arch.hvm_vcpu.pat_cr ) -return 1 ; +if ( (md->enabled ^ ms->enabled) &2 ) +return true; + +if ( md->enabled &2 ) + { +unsigned int num_var_ranges = (uint8_t )md->mtrr_cap; + +/* Test default type MSR. */ +if ( md->def_type != ms->def_type ) +return true; + +/* Test fixed ranges. */
xen/arch/x86/hvm/mtrr.c bool_t mtrr_var_range_msr_set( · line 505
Quoted 10 lines
xen/arch/x86/hvm/mtrr.c +10
-0+if ( (md->enabled ^ ms->enabled) &1 ) +return true; + +if ( (md->enabled &1 ) && +memcmp (md->fixed_ranges, ms->fixed_ranges, +sizeof (md->fixed_ranges)) ) +return true; + +/* Test variable ranges. */ + if ( num_var_ranges != (uint8_t)ms->mtrr_cap ||
Is it really possible to have two vCPUs on the same domain with a different number of variable ranges?
Quoted 18 lines
xen/arch/x86/hvm/mtrr.c +6
-1+ memcmp(md->var_ranges, ms->var_ranges, + num_var_ranges * sizeof(*md->var_ranges)) ) +return true; + } -return 0 ; +/* Test PAT. */ +return vd->arch.hvm_vcpu.pat_cr != vs->arch.hvm_vcpu.pat_cr; }struct hvm_mem_pinned_cacheattr_range {
xen/include/asm-x86/mtrr.h extern void memory_type_changed(struct d · line 97
Quoted 7 lines
xen/include/asm-x86/mtrr.h +1
-1@@ -92,6 +92,6 @@ extern void memory_type_changed(struct dextern bool_t pat_msr_set (uint64_t *pat,uint64_t msr);bool_t is_var_mtrr_overlapped (const struct mtrr_state *m); -bool_t mtrr_pat_not_equal (struct vcpu *vd,struct vcpu *vs); +bool mtrr_pat_not_equal (const struct vcpu *vd,const struct vcpu *vs);#endif /* __ASM_X86_MTRR_H__ */
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 17.05.18 at 13:10, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 17.05.18 at 11:48, <[email protected]> wrote:
The function is supposed to return whether the MTRR and PAT state of two CPUs match. Currently this is not properly done because the test for the deftype and the enable bits required both the deftype and the enable bits to be different, while just one of those fields being different can already cause the MTRR states on the vCPU to not match.
Quoted 6 lines
Quoted 3 lines
Fix this by changing the AND into an OR instead, so that either the deftype or the enabled bits being different will cause the function to return mismatching state.
This is by far not enough, but I didn't view the function as critical enough to warrant sending out the patch I have right away.
I've also realized that the logic there is wonky and would return true in cases where the states are equal (ie: for example if fixed MTRRs contents are different but FE is disabled).
Just wanted to do a minimal change that prevents wrongly reporting that the state is equal when it's not (I think the other way around is not that critical).
You change LGTM, and fixes some obvious cases where the current code would return true even if the cache state is the same.
Quoted 8 lines
Jan
x86/HVM: correctmtrr_pat_not_equal()The two vCPU-s differring in MTRR-enabled state means MTRR settings are not equal. Both vCPU-s having MTRRs disabled means only PAT needs to be compared. Along those lines for fixed range MTRRs. Differring variable range counts likewise mean settings are different overall.
Quoted 5 lines
Quoted 3 lines
Constify types and convert
bool_tto bool.Signed-off-by Jan Beulich <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>
Thanks.
Quoted 53 lines
Quoted 53 lines
xen/arch/x86/hvm/mtrr.c +20
-25@@ -476,35 +476,40 @@ bool_t mtrr_var_range_msr_set( return 1; } -bool_t mtrr_pat_not_equal (struct vcpu *vd,struct vcpu *vs) +bool mtrr_pat_not_equal(const struct vcpu *vd, const struct vcpu *vs) { -struct mtrr_state *md = &vd->arch.hvm_vcpu.mtrr; -struct mtrr_state *ms = &vs->arch.hvm_vcpu.mtrr; -int32_t res; -uint8_t num_var_ranges = (uint8_t )md->mtrr_cap; - -/* Test fixed ranges. */ - res =memcmp (md->fixed_ranges, ms->fixed_ranges, -NUM_FIXED_RANGES *sizeof (mtrr_type)); -if ( res ) -return 1 ; - -/* Test var ranges. */ - res =memcmp (md->var_ranges, ms->var_ranges, - num_var_ranges*sizeof (struct mtrr_var_range )); -if ( res ) -return 1 ; - -/* Test default type MSR. */ -if ( (md->def_type != ms->def_type) - && (md->enabled != ms->enabled) ) -return 1 ; + const struct mtrr_state *md = &vd->arch.hvm_vcpu.mtrr; + const struct mtrr_state *ms = &vs->arch.hvm_vcpu.mtrr; -/* Test PAT. */ -if ( vd->arch.hvm_vcpu.pat_cr != vs->arch.hvm_vcpu.pat_cr ) -return 1 ; + if ( (md->enabled ^ ms->enabled) & 2 ) + return true; + + if ( md->enabled & 2 ) + { + unsigned int num_var_ranges = (uint8_t)md->mtrr_cap; + + /* Test default type MSR. */ + if ( md->def_type != ms->def_type ) + return true; + + /* Test fixed ranges. */ + if ( (md->enabled ^ ms->enabled) & 1 ) + return true; + + if ( (md->enabled & 1) && + memcmp(md->fixed_ranges, ms->fixed_ranges,
Quoted 8 lines
xen/arch/x86/hvm/mtrr.c bool_t mtrr_var_range_msr_set( · line 505
Quoted 5 lines
xen/arch/x86/hvm/mtrr.c +5
-0+ sizeof(md->fixed_ranges)) ) +return true; + +/* Test variable ranges. */ + if ( num_var_ranges != (uint8_t)ms->mtrr_cap ||Is it really possible to have two vCPUs on the same domain with a different number of variable ranges?
Right now this is more for cosmetic reasons than for functionality. In theory it is possible, and it'll become possible in practice once we allow the number to be controlled through the load operation (see one of the other patches we've discussed yesterday).
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 99 lines · Jan Beulich
Quoted 33 lines
Quoted 1 line
Quoted 1 line · On 17.05.18 at 13:10, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 17.05.18 at 11:48, <[email protected]> wrote:
The function is supposed to return whether the MTRR and PAT state of two CPUs match. Currently this is not properly done because the test for the deftype and the enable bits required both the deftype and the enable bits to be different, while just one of those fields being different can already cause the MTRR states on the vCPU to not match.
Quoted 5 lines
Quoted 3 lines
Fix this by changing the AND into an OR instead, so that either the deftype or the enabled bits being different will cause the function to return mismatching state.
This is by far not enough, but I didn't view the function as critical enough to warrant sending out the patch I have right away.
I've also realized that the logic there is wonky and would return true in cases where the states are equal (ie: for example if fixed MTRRs contents are different but FE is disabled).
Just wanted to do a minimal change that prevents wrongly reporting that the state is equal when it's not (I think the other way around is not that critical).
You change LGTM, and fixes some obvious cases where the current code would return true even if the cache state is the same.
Quoted 8 lines
Jan
x86/HVM: correctmtrr_pat_not_equal()The two vCPU-s differring in MTRR-enabled state means MTRR settings are not equal. Both vCPU-s having MTRRs disabled means only PAT needs to be compared. Along those lines for fixed range MTRRs. Differring variable range counts likewise mean settings are different overall.
Quoted 4 lines
Quoted 3 lines
Constify types and convert
bool_tto bool.Signed-off-by Jan Beulich <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>Thanks.
Quoted 52 lines
Quoted 52 lines
xen/arch/x86/hvm/mtrr.c +19
-25@@ -476,35 +476,40 @@ bool_t mtrr_var_range_msr_set(return 1 ; } -bool_t mtrr_pat_not_equal (struct vcpu *vd,struct vcpu *vs) +bool mtrr_pat_not_equal (const struct vcpu *vd,const struct vcpu *vs) { -struct mtrr_state *md = &vd->arch.hvm_vcpu.mtrr; -struct mtrr_state *ms = &vs->arch.hvm_vcpu.mtrr; -int32_t res; -uint8_t num_var_ranges = (uint8_t )md->mtrr_cap; - -/* Test fixed ranges. */ - res =memcmp (md->fixed_ranges, ms->fixed_ranges, -NUM_FIXED_RANGES *sizeof (mtrr_type)); -if ( res ) -return 1 ; - -/* Test var ranges. */ - res =memcmp (md->var_ranges, ms->var_ranges, - num_var_ranges*sizeof (struct mtrr_var_range )); -if ( res ) -return 1 ; - -/* Test default type MSR. */ -if ( (md->def_type != ms->def_type) - && (md->enabled != ms->enabled) ) -return 1 ; +const struct mtrr_state *md = &vd->arch.hvm_vcpu.mtrr; +const struct mtrr_state *ms = &vs->arch.hvm_vcpu.mtrr; -/* Test PAT. */ -if ( vd->arch.hvm_vcpu.pat_cr != vs->arch.hvm_vcpu.pat_cr ) -return 1 ; +if ( (md->enabled ^ ms->enabled) &2 ) +return true; + +if ( md->enabled &2 ) + { +unsigned int num_var_ranges = (uint8_t )md->mtrr_cap; + +/* Test default type MSR. */ +if ( md->def_type != ms->def_type ) +return true; + +/* Test fixed ranges. */ +if ( (md->enabled ^ ms->enabled) &1 ) +return true; + + if ( (md->enabled & 1) &&Quoted 8 lines
xen/arch/x86/hvm/mtrr.c bool_t mtrr_var_range_msr_set( · line 505
Quoted 6 lines
xen/arch/x86/hvm/mtrr.c +6
-0+ memcmp(md->fixed_ranges, ms->fixed_ranges, + sizeof(md->fixed_ranges)) ) +return true; + +/* Test variable ranges. */ + if ( num_var_ranges != (uint8_t)ms->mtrr_cap ||Is it really possible to have two vCPUs on the same domain with a different number of variable ranges?
Quoted 4 lines
Right now this is more for cosmetic reasons than for functionality. In theory it is possible, and it'll become possible in practice once we allow the number to be controlled through the load operation (see one of the other patches we've discussed yesterday).
MTRR MSRs are yet another todo item on the grand "move to a usable MSR infrastructure" list.
MTRRcap is a read-only MSR, so will live in the domain policy and be a single value across the entire domain.
Nothing good will come of trying to formally support different MSR capabilities on different vcpus, because you won't find any hardware where you can do this in practice.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 17.05.18 at 13:46, <[email protected]> wrote:
Nothing good will come of trying to formally support different MSR capabilities on different vcpus, because you won't find any hardware where you can do this in practice.
I agree, but it's not really clear to me how you want to enforce identical values yet at the same time allow save (or really load) records to define the number of ranges (which currently is broken). Perhaps we could refuse to launch/unpause a guest when there are inconsistencies.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 17.05.18 at 13:46, <[email protected]> wrote:
Nothing good will come of trying to formally support different MSR capabilities on different vcpus, because you won't find any hardware where you can do this in practice.
Thinking of it - allowing this would be a nice way to allow testing OS code when meaning it to cope with asymmetries.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 17 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 17.05.18 at 11:48, <[email protected]> wrote:
The function is supposed to return whether the MTRR and PAT state of two CPUs match. Currently this is not properly done because the test for the deftype and the enable bits required both the deftype and the enable bits to be different, while just one of those fields being different can already cause the MTRR states on the vCPU to not match.
Fix this by changing the AND into an OR instead, so that either the deftype or the enabled bits being different will cause the function to return mismatching state.
This is by far not enough, but I didn't view the function as critical enough to warrant sending out the patch I have right away.
Jan
x86/HVM: correctmtrr_pat_not_equal()
Quoted 8 lines
The two vCPU-s differring in MTRR-enabled state means MTRR settings are not equal. Both vCPU-s having MTRRs disabled means only PAT needs to be compared. Along those lines for fixed range MTRRs. Differring variable range counts likewise mean settings are different overall.
Constify types and convert
bool_tto bool.Signed-off-by Jan Beulich <[email protected]>
My Rab tag for Roger's patch is applicable here, too:
Juergen
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 17.05.18 at 13:49, <[email protected]> wrote:
Quoted 22 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 17.05.18 at 11:48, <[email protected]> wrote:
The function is supposed to return whether the MTRR and PAT state of two CPUs match. Currently this is not properly done because the test for the deftype and the enable bits required both the deftype and the enable bits to be different, while just one of those fields being different can already cause the MTRR states on the vCPU to not match.
Fix this by changing the AND into an OR instead, so that either the deftype or the enabled bits being different will cause the function to return mismatching state.
This is by far not enough, but I didn't view the function as critical enough to warrant sending out the patch I have right away.
Jan
x86/HVM: correctmtrr_pat_not_equal()The two vCPU-s differring in MTRR-enabled state means MTRR settings are not equal. Both vCPU-s having MTRRs disabled means only PAT needs to be compared. Along those lines for fixed range MTRRs. Differring variable range counts likewise mean settings are different overall.
Quoted 7 lines
Quoted 3 lines
Constify types and convert
bool_tto bool.Signed-off-by Jan Beulich <[email protected]>My Rab tag for Roger's patch is applicable here, too:
Release-acked-by Juergen Gross <[email protected]>
Thanks, but that depends on whether Andrew is willing to ack the patch (with or without minor modifications).
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel