Skip to contentSkip to conversation

x86/vPMU: constrain MSR_IA32_DS_AREA loads

PATCH Superseded Jan Beulich 4 participants 11 messages Dec 17, 2015 – Dec 18, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/vPMU: constrain MSR_IA32_DS_AREA loads

For one, loading the MSR with a possibly non-canonical address was possible since the verification is conditional, while the MSR load wasn't. And then for PV guests we need to further limit the range of valid addresses to exclude the hypervisor range.

xen/arch/x86/cpu/vpmu_intel.c +6 -3
@@ -366,7 +366,8 @@ static inline void __core2_vpmu_load(str
     }
 
     wrmsrl(MSR_CORE_PERF_FIXED_CTR_CTRL, core2_vpmu_cxt->fixed_ctrl);
-    wrmsrl(MSR_IA32_DS_AREA, core2_vpmu_cxt->ds_area);
+    if ( vpmu_is_set(vcpu_vpmu(v), VPMU_CPU_HAS_DS) )
+        wrmsrl(MSR_IA32_DS_AREA, core2_vpmu_cxt->ds_area);
     wrmsrl(MSR_IA32_PEBS_ENABLE, core2_vpmu_cxt->pebs_enable);
 
     if ( !has_hvm_container_vcpu(v) )
@@ -415,8 +416,10 @@ static int core2_vpmu_verify(struct vcpu
             enabled_cntrs |= (1ULL << i);
     }
 
-    if ( vpmu_is_set(vcpu_vpmu(v), VPMU_CPU_HAS_DS) &&
-         !is_canonical_address(core2_vpmu_cxt->ds_area) )
+    if ( vpmu_is_set(vpmu, VPMU_CPU_HAS_DS) &&
+         !(has_hvm_container_vcpu(v)
+           ? is_canonical_address(core2_vpmu_cxt->ds_area)
+           : __addr_ok(core2_vpmu_cxt->ds_area)) )
         return -EINVAL;
 
     if ( (core2_vpmu_cxt->global_ctrl & enabled_cntrs) ||

x86/vPMU: constrain MSR_IA32_DS_AREA loads

For one, loading the MSR with a possibly non-canonical address was possible since the verification is conditional, while the MSR load wasn't. And then for PV guests we need to further limit the range of valid addresses to exclude the hypervisor range.

xen/arch/x86/cpu/vpmu_intel.c +6 -3
@@ -366,7 +366,8 @@ static inline void __core2_vpmu_load(str
     }
 
     wrmsrl(MSR_CORE_PERF_FIXED_CTR_CTRL, core2_vpmu_cxt->fixed_ctrl);
-    wrmsrl(MSR_IA32_DS_AREA, core2_vpmu_cxt->ds_area);
+    if ( vpmu_is_set(vcpu_vpmu(v), VPMU_CPU_HAS_DS) )
+        wrmsrl(MSR_IA32_DS_AREA, core2_vpmu_cxt->ds_area);
     wrmsrl(MSR_IA32_PEBS_ENABLE, core2_vpmu_cxt->pebs_enable);
 
     if ( !has_hvm_container_vcpu(v) )
@@ -415,8 +416,10 @@ static int core2_vpmu_verify(struct vcpu
             enabled_cntrs |= (1ULL << i);
     }
 
-    if ( vpmu_is_set(vcpu_vpmu(v), VPMU_CPU_HAS_DS) &&
-         !is_canonical_address(core2_vpmu_cxt->ds_area) )
+    if ( vpmu_is_set(vpmu, VPMU_CPU_HAS_DS) &&
+         !(has_hvm_container_vcpu(v)
+           ? is_canonical_address(core2_vpmu_cxt->ds_area)
+           : __addr_ok(core2_vpmu_cxt->ds_area)) )
         return -EINVAL;
 
     if ( (core2_vpmu_cxt->global_ctrl & enabled_cntrs) ||

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Jan Beulich

For one, loading the MSR with a possibly non-canonical address was possible since the verification is conditional, while the MSR load wasn't. And then for PV guests we need to further limit the range of valid addresses to exclude the hypervisor range.

#2 lore raw
In reply to Jan Beulich
Quoted 20 lines · Jan Beulich

For one, loading the MSR with a possibly non-canonical address was possible since the verification is conditional, while the MSR load wasn't. And then for PV guests we need to further limit the range of valid addresses to exclude the hypervisor range.

xen/arch/x86/cpu/vpmu_intel.c +2 -1
@@ -366,7 +366,8 @@ static inline void __core2_vpmu_load(str
      }
 
      wrmsrl(MSR_CORE_PERF_FIXED_CTR_CTRL, core2_vpmu_cxt->fixed_ctrl);
-    wrmsrl(MSR_IA32_DS_AREA, core2_vpmu_cxt->ds_area);
+    if ( vpmu_is_set(vcpu_vpmu(v), VPMU_CPU_HAS_DS) )
+        wrmsrl(MSR_IA32_DS_AREA, core2_vpmu_cxt->ds_area);
      wrmsrl(MSR_IA32_PEBS_ENABLE, core2_vpmu_cxt->pebs_enable);
 
      if ( !has_hvm_container_vcpu(v) )

xen/arch/x86/cpu/vpmu_intel.c static int core2_vpmu_verify(struct vcpu · line 422

Quoted 9 lines
xen/arch/x86/cpu/vpmu_intel.c +4 -2
@@ -415,8 +416,10 @@ static int core2_vpmu_verify(struct vcpu
              enabled_cntrs |= (1ULL << i);
      }
 
-    if ( vpmu_is_set(vcpu_vpmu(v), VPMU_CPU_HAS_DS) &&
-         !is_canonical_address(core2_vpmu_cxt->ds_area) )
+    if ( vpmu_is_set(vpmu, VPMU_CPU_HAS_DS) &&
+         !(has_hvm_container_vcpu(v)
+           ? is_canonical_address(core2_vpmu_cxt->ds_area)
+           : __addr_ok(core2_vpmu_cxt->ds_area)) )

Should we instead of (or in addition to) this also make the same change in core2_vpmu_do_wrmsr()?

-boris

Quoted 6 lines
xen/arch/x86/cpu/vpmu_intel.c +0 -0
          return -EINVAL;
 
      if ( (core2_vpmu_cxt->global_ctrl & enabled_cntrs) ||
#3 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 17.12.15 at 15:12, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
xen/arch/x86/cpu/vpmu_intel.c +0 -0
@@ -415,8 +416,10 @@ static int core2_vpmu_verify(struct vcpu
              enabled_cntrs |= (1ULL << i);
      }
 
Quoted 9 lines

xen/arch/x86/cpu/vpmu_intel.c static int core2_vpmu_verify(struct vcpu · line 422

Quoted 6 lines
xen/arch/x86/cpu/vpmu_intel.c +4 -2
-    if ( vpmu_is_set(vcpu_vpmu(v), VPMU_CPU_HAS_DS) &&
-         !is_canonical_address(core2_vpmu_cxt->ds_area) )
+    if ( vpmu_is_set(vpmu, VPMU_CPU_HAS_DS) &&
+         !(has_hvm_container_vcpu(v)
+           ? is_canonical_address(core2_vpmu_cxt->ds_area)
+           : __addr_ok(core2_vpmu_cxt->ds_area)) )

Should we instead of (or in addition to) this also make the same change in core2_vpmu_do_wrmsr()?

Currently there's no need for this since - afaict - PV guests can't write this MSR directly (it's not among the white listed set in traps.c).

Jan

#4 lore raw
Quoted 14 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 17.12.15 at 15:12, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
xen/arch/x86/cpu/vpmu_intel.c +0 -0
@@ -415,8 +416,10 @@ static int core2_vpmu_verify(struct vcpu
               enabled_cntrs |= (1ULL << i);
       }
 
Quoted 8 lines

xen/arch/x86/cpu/vpmu_intel.c static int core2_vpmu_verify(struct vcpu · line 422

Quoted 6 lines
xen/arch/x86/cpu/vpmu_intel.c +4 -2
-    if ( vpmu_is_set(vcpu_vpmu(v), VPMU_CPU_HAS_DS) &&
-         !is_canonical_address(core2_vpmu_cxt->ds_area) )
+    if ( vpmu_is_set(vpmu, VPMU_CPU_HAS_DS) &&
+         !(has_hvm_container_vcpu(v)
+           ? is_canonical_address(core2_vpmu_cxt->ds_area)
+           : __addr_ok(core2_vpmu_cxt->ds_area)) )

Should we instead of (or in addition to) this also make the same change in core2_vpmu_do_wrmsr()?

Quoted 3 lines

Currently there's no need for this since - afaict - PV guests can't write this MSR directly (it's not among the white listed set in traps.c).

Then we probably shouldn't set VPMU_CPU_HAS_DS for PV guests.

-boris

#5 lore raw
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 17.12.15 at 15:26, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 17.12.15 at 15:12, <[email protected]> wrote:

xen/arch/x86/cpu/vpmu_intel.c static int core2_vpmu_verify(struct vcpu · line 422

Quoted 10 lines · Jan Beulich
xen/arch/x86/cpu/vpmu_intel.c +4 -2
@@ -415,8 +416,10 @@ static int core2_vpmu_verify(struct vcpu
               enabled_cntrs |= (1ULL << i);
       }
 
-    if ( vpmu_is_set(vcpu_vpmu(v), VPMU_CPU_HAS_DS) &&
-         !is_canonical_address(core2_vpmu_cxt->ds_area) )
+    if ( vpmu_is_set(vpmu, VPMU_CPU_HAS_DS) &&
+         !(has_hvm_container_vcpu(v)
+           ? is_canonical_address(core2_vpmu_cxt->ds_area)
+           : __addr_ok(core2_vpmu_cxt->ds_area)) )

Should we instead of (or in addition to) this also make the same change in core2_vpmu_do_wrmsr()?

Quoted 5 lines
Quoted 3 lines

Currently there's no need for this since - afaict - PV guests can't write this MSR directly (it's not among the white listed set in traps.c).

Then we probably shouldn't set VPMU_CPU_HAS_DS for PV guests.

Or add the MSR to the permitted set. You know better than I what the best route here is.

Jan

#6 lore raw
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 17.12.15 at 15:26, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 17.12.15 at 15:12, <[email protected]> wrote:

xen/arch/x86/cpu/vpmu_intel.c static int core2_vpmu_verify(struct vcpu · line 422

Quoted 10 lines · Jan Beulich
xen/arch/x86/cpu/vpmu_intel.c +4 -2
@@ -415,8 +416,10 @@ static int core2_vpmu_verify(struct vcpu
                enabled_cntrs |= (1ULL << i);
        }
 
-    if ( vpmu_is_set(vcpu_vpmu(v), VPMU_CPU_HAS_DS) &&
-         !is_canonical_address(core2_vpmu_cxt->ds_area) )
+    if ( vpmu_is_set(vpmu, VPMU_CPU_HAS_DS) &&
+         !(has_hvm_container_vcpu(v)
+           ? is_canonical_address(core2_vpmu_cxt->ds_area)
+           : __addr_ok(core2_vpmu_cxt->ds_area)) )

Should we instead of (or in addition to) this also make the same change in core2_vpmu_do_wrmsr()?

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

Currently there's no need for this since - afaict - PV guests can't write this MSR directly (it's not among the white listed set in traps.c).

Then we probably shouldn't set VPMU_CPU_HAS_DS for PV guests.

Or add the MSR to the permitted set. You know better than I what the best route here is.

I vaguely recall a conversation where we weren't sure whether BTS (which needs DS area) will work for PV. Something to do with DS address being in the right context (guest or host). I'd need to find that conversation (or test BTS on PV).

-boris

#7 lore raw
Quoted 23 lines

From: Boris Ostrovsky [mailto:[email protected]]
Sent: Thursday, December 17, 2015 10:42 PM

Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 17.12.15 at 15:26, <[email protected]> wrote:
Quoted 17 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 17.12.15 at 15:12, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
xen/arch/x86/cpu/vpmu_intel.c +0 -0
@@ -415,8 +416,10 @@ static int core2_vpmu_verify(struct vcpu
                enabled_cntrs |= (1ULL << i);
        }
 
Quoted 8 lines

xen/arch/x86/cpu/vpmu_intel.c static int core2_vpmu_verify(struct vcpu · line 422

Quoted 6 lines
xen/arch/x86/cpu/vpmu_intel.c +4 -2
-    if ( vpmu_is_set(vcpu_vpmu(v), VPMU_CPU_HAS_DS) &&
-         !is_canonical_address(core2_vpmu_cxt->ds_area) )
+    if ( vpmu_is_set(vpmu, VPMU_CPU_HAS_DS) &&
+         !(has_hvm_container_vcpu(v)
+           ? is_canonical_address(core2_vpmu_cxt->ds_area)
+           : __addr_ok(core2_vpmu_cxt->ds_area)) )

Should we instead of (or in addition to) this also make the same change in core2_vpmu_do_wrmsr()?

Currently there's no need for this since - afaict - PV guests can't write this MSR directly (it's not among the white listed set in traps.c).

Quoted 9 lines
Quoted 3 lines
Quoted 1 line

Then we probably shouldn't set VPMU_CPU_HAS_DS for PV guests.

Or add the MSR to the permitted set. You know better than I what the best route here is.

I vaguely recall a conversation where we weren't sure whether BTS (which needs DS area) will work for PV. Something to do with DS address being in the right context (guest or host). I'd need to find that conversation (or test BTS on PV).

I guess I don't need to review current patch until you have a conclusion, right? :-)

Thanks
Kevin

#8 lore raw
Quoted 24 lines · Kevin
Quoted 24 lines

From: Boris Ostrovsky [mailto:[email protected]]
Sent: Thursday, December 17, 2015 10:42 PM

Quoted 20 lines · Jan Beulich
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 17.12.15 at 15:26, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 17.12.15 at 15:12, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
(quoted diff) +0 -0
@@ -415,8 +416,10 @@ static int core2_vpmu_verify(struct vcpu
                 enabled_cntrs |= (1ULL << i);
         }
 
Quoted 8 lines

Review static int core2_vpmu_verify(struct vcpu · line 422

Quoted 6 lines
(quoted diff) +4 -2
-    if ( vpmu_is_set(vcpu_vpmu(v), VPMU_CPU_HAS_DS) &&
-         !is_canonical_address(core2_vpmu_cxt->ds_area) )
+    if ( vpmu_is_set(vpmu, VPMU_CPU_HAS_DS) &&
+         !(has_hvm_container_vcpu(v)
+           ? is_canonical_address(core2_vpmu_cxt->ds_area)
+           : __addr_ok(core2_vpmu_cxt->ds_area)) )

Should we instead of (or in addition to) this also make the same change in core2_vpmu_do_wrmsr()?

Quoted 3 lines

Currently there's no need for this since - afaict - PV guests can't write this MSR directly (it's not among the white listed set in traps.c).

Then we probably shouldn't set VPMU_CPU_HAS_DS for PV guests.

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

Or add the MSR to the permitted set. You know better than I what the best route here is.

I vaguely recall a conversation where we weren't sure whether BTS (which needs DS area) will work for PV. Something to do with DS address being in the right context (guest or host). I'd need to find that conversation (or test BTS on PV).

I guess I don't need to review current patch until you have a conclusion, right? :-)

All I can say that is BTS does not work on PV (at least as far as perf is concerned, which is the only tool I know that could use it). Which is not surprising given that we can't access DS_AREA MSR.

-boris

#9 lore raw
Quoted 33 lines
Quoted 1 line
Quoted 1 line · On 18.12.15 at 16:12, <[email protected]> wrote:
Quoted 31 lines · Kevin
Quoted 31 lines

From: Boris Ostrovsky [mailto:[email protected]]
Sent: Thursday, December 17, 2015 10:42 PM

Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 17.12.15 at 15:26, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 17.12.15 at 15:12, <[email protected]> wrote:

Review static int core2_vpmu_verify(struct vcpu · line 422

Quoted 10 lines · Jan Beulich
(quoted diff) +4 -2
@@ -415,8 +416,10 @@ static int core2_vpmu_verify(struct vcpu
                 enabled_cntrs |= (1ULL << i);
         }
 
-    if ( vpmu_is_set(vcpu_vpmu(v), VPMU_CPU_HAS_DS) &&
-         !is_canonical_address(core2_vpmu_cxt->ds_area) )
+    if ( vpmu_is_set(vpmu, VPMU_CPU_HAS_DS) &&
+         !(has_hvm_container_vcpu(v)
+           ? is_canonical_address(core2_vpmu_cxt->ds_area)
+           : __addr_ok(core2_vpmu_cxt->ds_area)) )

Should we instead of (or in addition to) this also make the same change in core2_vpmu_do_wrmsr()?

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

Currently there's no need for this since - afaict - PV guests can't write this MSR directly (it's not among the white listed set in traps.c).

Then we probably shouldn't set VPMU_CPU_HAS_DS for PV guests.

Or add the MSR to the permitted set. You know better than I what the best route here is.

I vaguely recall a conversation where we weren't sure whether BTS (which needs DS area) will work for PV. Something to do with DS address being in the right context (guest or host). I'd need to find that conversation (or test BTS on PV).

Quoted 6 lines
Quoted 1 line

I guess I don't need to review current patch until you have a conclusion,

right? :-)

All I can say that is BTS does not work on PV (at least as far as perf is concerned, which is the only tool I know that could use it). Which is not surprising given that we can't access DS_AREA MSR.

But in the context of this patch (and what direction a v2 might need to go) we'd need to assume that MSR can be accessed.

Jan

#10 lore raw
Quoted 33 lines · Jan Beulich
Quoted 33 lines
Quoted 1 line
Quoted 1 line · On 18.12.15 at 16:12, <[email protected]> wrote:
Quoted 31 lines · Kevin
Quoted 31 lines

From: Boris Ostrovsky [mailto:[email protected]]
Sent: Thursday, December 17, 2015 10:42 PM

Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 17.12.15 at 15:26, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 17.12.15 at 15:12, <[email protected]> wrote:

Review static int core2_vpmu_verify(struct vcpu · line 422

Quoted 10 lines · Jan Beulich
(quoted diff) +4 -2
@@ -415,8 +416,10 @@ static int core2_vpmu_verify(struct vcpu
                  enabled_cntrs |= (1ULL << i);
          }
 
-    if ( vpmu_is_set(vcpu_vpmu(v), VPMU_CPU_HAS_DS) &&
-         !is_canonical_address(core2_vpmu_cxt->ds_area) )
+    if ( vpmu_is_set(vpmu, VPMU_CPU_HAS_DS) &&
+         !(has_hvm_container_vcpu(v)
+           ? is_canonical_address(core2_vpmu_cxt->ds_area)
+           : __addr_ok(core2_vpmu_cxt->ds_area)) )

Should we instead of (or in addition to) this also make the same change in core2_vpmu_do_wrmsr()?

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

Currently there's no need for this since - afaict - PV guests can't write this MSR directly (it's not among the white listed set in traps.c).

Then we probably shouldn't set VPMU_CPU_HAS_DS for PV guests.

Or add the MSR to the permitted set. You know better than I what the best route here is.

I vaguely recall a conversation where we weren't sure whether BTS (which needs DS area) will work for PV. Something to do with DS address being in the right context (guest or host). I'd need to find that conversation (or test BTS on PV).

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

I guess I don't need to review current patch until you have a conclusion,

right? :-)

All I can say that is BTS does not work on PV (at least as far as perf is concerned, which is the only tool I know that could use it). Which is not surprising given that we can't access DS_AREA MSR.

But in the context of this patch (and what direction a v2 might need to go) we'd need to assume that MSR can be accessed.

In which case I think we need to add the same test to core2_vpmu_do_wrmsr() as well.

My earlier statement that we perform this test there *instead of* doing in core2_vpmu_verify() was incorrect --- we need to do it in both places since the value can come from the guest in the vpmu_context that we are loading thus bypassing core2_vpmu_do_wrmsr() path.

-boris