Skip to contentSkip to conversation

x86/vPMU: adjustements to vendor hooks treatment

PATCH In review Jan Beulich 3 participants 15 messages Nov 29, 2021 – Dec 1, 2021

lore
Share & tools
lore
#0 lore raw

cover x86/vPMU: adjustements to vendor hooks treatment

In the context of [1] the 1st patch will be of interest, whereas the other changes are a result of observations while doing that conversion.

1: convert vendor hook invocations to altcall
2: invoke <vendor>_vpmu_initialise() through a hook as well 3: move vpmu_ops to .init.data

Jan

[1] https://lists.xen.org/archives/html/xen-devel/2021-11/msg01822.html

#1 lore raw

PATCH 1/3 x86/vPMU: convert vendor hook invocations to altcall

At least some vPMU functions will be invoked (and hence can further be speculated into) even in the vPMU-disabled case. Convert vpmu_ops to the standard single-instance model being a prerequisite to engaging the alternative_call() machinery, and convert all respective calls. Note that this requires vpmu_init() to become a pre-SMP initcall.

This change then also helps performance.

To replace a few vpmu->arch_vpmu_ops NULL checks, introduce a new VPMU_INITIALIZED state, such that in the absence of any other suitable vmpu_is_set() checks this state can be checked for.

While adding the inclusion of xen/err.h, also prune other xen/*.h inclusions.

xen/arch/x86/cpu/vpmu.c +34 -37
@@ -17,12 +17,12 @@
  *
  * Author: Haitao Shan <[email protected]>
  */
-#include <xen/sched.h>
-#include <xen/xenoprof.h>
-#include <xen/event.h>
-#include <xen/guest_access.h>
 #include <xen/cpu.h>
+#include <xen/err.h>
 #include <xen/param.h>
+#include <xen/event.h>
+#include <xen/guest_access.h>
+#include <xen/sched.h>
 #include <asm/regs.h>
 #include <asm/types.h>
 #include <asm/msr.h>
@@ -49,6 +49,7 @@ CHECK_pmu_params;
 static unsigned int __read_mostly opt_vpmu_enabled;
 unsigned int __read_mostly vpmu_mode = XENPMU_MODE_OFF;
 unsigned int __read_mostly vpmu_features = 0;
+static struct arch_vpmu_ops __read_mostly vpmu_ops;
 
 static DEFINE_SPINLOCK(vpmu_lock);
 static unsigned vpmu_count;
@@ -120,7 +121,6 @@ int vpmu_do_msr(unsigned int msr, uint64
 {
     struct vcpu *curr = current;
     struct vpmu_struct *vpmu;
-    const struct arch_vpmu_ops *ops;
     int ret = 0;
 
     /*
@@ -133,14 +133,13 @@ int vpmu_do_msr(unsigned int msr, uint64
          goto nop;
 
     vpmu = vcpu_vpmu(curr);
-    ops = vpmu->arch_vpmu_ops;
-    if ( !ops )
+    if ( !vpmu_is_set(vpmu, VPMU_INITIALIZED) )
         goto nop;
 
-    if ( is_write && ops->do_wrmsr )
-        ret = ops->do_wrmsr(msr, *msr_content, supported);
-    else if ( !is_write && ops->do_rdmsr )
-        ret = ops->do_rdmsr(msr, msr_content);
+    if ( is_write && vpmu_ops.do_wrmsr )
+        ret = alternative_call(vpmu_ops.do_wrmsr, msr, *msr_content, supported);
+    else if ( !is_write && vpmu_ops.do_rdmsr )
+        ret = alternative_call(vpmu_ops.do_rdmsr, msr, msr_content);
     else
         goto nop;
 
@@ -153,7 +152,7 @@ int vpmu_do_msr(unsigned int msr, uint64
         vpmu_is_set(vpmu, VPMU_CACHED) )
     {
         vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
-        ops->arch_vpmu_save(curr, 0);
+        alternative_vcall(vpmu_ops.arch_vpmu_save, curr, 0);
         vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
     }
 
@@ -202,7 +201,7 @@ void vpmu_do_interrupt(struct cpu_user_r
         sampling = sampled;
 
     vpmu = vcpu_vpmu(sampling);
-    if ( !vpmu->arch_vpmu_ops )
+    if ( !vpmu_is_set(vpmu, VPMU_INITIALIZED) )
         return;
 
     /* PV(H) guest */
@@ -220,7 +219,7 @@ void vpmu_do_interrupt(struct cpu_user_r
 
         /* PV guest will be reading PMU MSRs from xenpmu_data */
         vpmu_set(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
-        vpmu->arch_vpmu_ops->arch_vpmu_save(sampling, 1);
+        alternative_vcall(vpmu_ops.arch_vpmu_save, sampling, 1);
         vpmu_reset(vpmu, VPMU_CONTEXT_SAVE | VPMU_CONTEXT_LOADED);
 
         if ( is_hvm_vcpu(sampled) )
@@ -321,7 +320,7 @@ void vpmu_do_interrupt(struct cpu_user_r
     /* We don't support (yet) HVM dom0 */
     ASSERT(sampling == sampled);
 
-    if ( !vpmu->arch_vpmu_ops->do_interrupt(regs) ||
+    if ( !alternative_call(vpmu_ops.do_interrupt, regs) ||
          !is_vlapic_lvtpc_enabled(vlapic) )
         return;
 
@@ -349,8 +348,7 @@ static void vpmu_save_force(void *arg)
 
     vpmu_set(vpmu, VPMU_CONTEXT_SAVE);
 
-    if ( vpmu->arch_vpmu_ops )
-        (void)vpmu->arch_vpmu_ops->arch_vpmu_save(v, 0);
+    alternative_vcall(vpmu_ops.arch_vpmu_save, v, 0);
 
     vpmu_reset(vpmu, VPMU_CONTEXT_SAVE);
 
@@ -368,9 +366,8 @@ void vpmu_save(struct vcpu *v)
     vpmu->last_pcpu = pcpu;
     per_cpu(last_vcpu, pcpu) = v;
 
-    if ( vpmu->arch_vpmu_ops )
-        if ( vpmu->arch_vpmu_ops->arch_vpmu_save(v, 0) )
-            vpmu_reset(vpmu, VPMU_CONTEXT_LOADED);
+    if ( alternative_call(vpmu_ops.arch_vpmu_save, v, 0) )
+        vpmu_reset(vpmu, VPMU_CONTEXT_LOADED);
 
     apic_write(APIC_LVTPC, PMU_APIC_VECTOR | APIC_LVT_MASKED);
 }
@@ -426,13 +423,13 @@ int vpmu_load(struct vcpu *v, bool_t fro
          vpmu_is_set(vpmu, VPMU_CACHED)) )
         return 0;
 
-    if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->arch_vpmu_load )
+    if ( vpmu_ops.arch_vpmu_load )
     {
         int ret;
 
         apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
         /* Arch code needs to set VPMU_CONTEXT_LOADED */
-        ret = vpmu->arch_vpmu_ops->arch_vpmu_load(v, from_guest);
+        ret = alternative_call(vpmu_ops.arch_vpmu_load, v, from_guest);
         if ( ret )
         {
             apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc | APIC_LVT_MASKED);
@@ -572,7 +569,7 @@ static void vpmu_arch_destroy(struct vcp
         on_selected_cpus(cpumask_of(vpmu->last_pcpu),
                          vpmu_clear_last, v, 1);
 
-    if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->arch_vpmu_destroy )
+    if ( vpmu_ops.arch_vpmu_destroy )
     {
         /*
          * Unload VPMU first if VPMU_CONTEXT_LOADED being set.
@@ -582,7 +579,7 @@ static void vpmu_arch_destroy(struct vcp
             on_selected_cpus(cpumask_of(vcpu_vpmu(v)->last_pcpu),
                              vpmu_save_force, v, 1);
 
-         vpmu->arch_vpmu_ops->arch_vpmu_destroy(v);
+         alternative_vcall(vpmu_ops.arch_vpmu_destroy, v);
     }
 
     vpmu_reset(vpmu, VPMU_CONTEXT_ALLOCATED);
@@ -689,10 +686,9 @@ static void pvpmu_finish(struct domain *
 /* Dump some vpmu information to console. Used in keyhandler dump_domains(). */
 void vpmu_dump(struct vcpu *v)
 {
-    struct vpmu_struct *vpmu = vcpu_vpmu(v);
-
-    if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->arch_vpmu_dump )
-        vpmu->arch_vpmu_ops->arch_vpmu_dump(v);
+    if ( vpmu_is_set(vcpu_vpmu(v), VPMU_INITIALIZED) &&
+         vpmu_ops.arch_vpmu_dump )
+        alternative_vcall(vpmu_ops.arch_vpmu_dump, v);
 }
 
 long do_xenpmu_op(unsigned int op, XEN_GUEST_HANDLE_PARAM(xen_pmu_params_t) arg)
@@ -870,6 +866,7 @@ static struct notifier_block cpu_nfb = {
 static int __init vpmu_init(void)
 {
     int vendor = current_cpu_data.x86_vendor;
+    const struct arch_vpmu_ops *ops = NULL;
 
     if ( !opt_vpmu_enabled )
         return 0;
@@ -886,36 +883,36 @@ static int __init vpmu_init(void)
     switch ( vendor )
     {
     case X86_VENDOR_AMD:
-        if ( amd_vpmu_init() )
-           vpmu_mode = XENPMU_MODE_OFF;
+        ops = amd_vpmu_init();
         break;
 
     case X86_VENDOR_HYGON:
-        if ( hygon_vpmu_init() )
-           vpmu_mode = XENPMU_MODE_OFF;
+        ops = hygon_vpmu_init();
         break;
 
     case X86_VENDOR_INTEL:
-        if ( core2_vpmu_init() )
-           vpmu_mode = XENPMU_MODE_OFF;
+        ops = core2_vpmu_init();
         break;
 
     default:
         printk(XENLOG_WARNING "VPMU: Unknown CPU vendor: %d. "
                "Turning VPMU off.\n", vendor);
-        vpmu_mode = XENPMU_MODE_OFF;
         break;
     }
 
-    if ( vpmu_mode != XENPMU_MODE_OFF )
+    if ( !IS_ERR_OR_NULL(ops) )
     {
+        vpmu_ops = *ops;
         register_cpu_notifier(&cpu_nfb);
         printk(XENLOG_INFO "VPMU: version " __stringify(XENPMU_VER_MAJ) "."
                __stringify(XENPMU_VER_MIN) "\n");
     }
     else
+    {
+        vpmu_mode = XENPMU_MODE_OFF;
         opt_vpmu_enabled = 0;
+    }
 
     return 0;
 }
-__initcall(vpmu_init);
+presmp_initcall(vpmu_init);
xen/arch/x86/cpu/vpmu_amd.c +10 -11
@@ -21,9 +21,9 @@
  *
  */
 
-#include <xen/xenoprof.h>
+#include <xen/err.h>
 #include <xen/sched.h>
-#include <xen/irq.h>
+#include <xen/xenoprof.h>
 #include <asm/apic.h>
 #include <asm/vpmu.h>
 #include <asm/hvm/save.h>
@@ -483,7 +483,7 @@ static void amd_vpmu_dump(const struct v
     }
 }
 
-static const struct arch_vpmu_ops amd_vpmu_ops = {
+static const struct arch_vpmu_ops __initconstrel amd_vpmu_ops = {
     .do_wrmsr = amd_vpmu_do_wrmsr,
     .do_rdmsr = amd_vpmu_do_rdmsr,
     .do_interrupt = amd_vpmu_do_interrupt,
@@ -529,13 +529,12 @@ int svm_vpmu_initialise(struct vcpu *v)
                offsetof(struct xen_pmu_amd_ctxt, regs));
     }
 
-    vpmu->arch_vpmu_ops = &amd_vpmu_ops;
+    vpmu_set(vpmu, VPMU_INITIALIZED | VPMU_CONTEXT_ALLOCATED);
 
-    vpmu_set(vpmu, VPMU_CONTEXT_ALLOCATED);
     return 0;
 }
 
-static int __init common_init(void)
+static const struct arch_vpmu_ops *__init common_init(void)
 {
     unsigned int i;
 
@@ -543,7 +542,7 @@ static int __init common_init(void)
     {
         printk(XENLOG_WARNING "VPMU: Unsupported CPU family %#x\n",
                current_cpu_data.x86);
-        return -EINVAL;
+        return ERR_PTR(-EINVAL);
     }
 
     if ( sizeof(struct xen_pmu_data) +
@@ -553,7 +552,7 @@ static int __init common_init(void)
                "VPMU: Register bank does not fit into VPMU shared page\n");
         counters = ctrls = NULL;
         num_counters = 0;
-        return -ENOSPC;
+        return ERR_PTR(-ENOSPC);
     }
 
     for ( i = 0; i < num_counters; i++ )
@@ -562,10 +561,10 @@ static int __init common_init(void)
         ctrl_rsvd[i] &= CTRL_RSVD_MASK;
     }
 
-    return 0;
+    return &amd_vpmu_ops;
 }
 
-int __init amd_vpmu_init(void)
+const struct arch_vpmu_ops *__init amd_vpmu_init(void)
 {
     switch ( current_cpu_data.x86 )
     {
@@ -592,7 +591,7 @@ int __init amd_vpmu_init(void)
     return common_init();
 }
 
-int __init hygon_vpmu_init(void)
+const struct arch_vpmu_ops *__init hygon_vpmu_init(void)
 {
     switch ( current_cpu_data.x86 )
     {
xen/arch/x86/cpu/vpmu_intel.c +8 -8
@@ -18,9 +18,9 @@
  * Author: Haitao Shan <[email protected]>
  */
 
+#include <xen/err.h>
 #include <xen/sched.h>
 #include <xen/xenoprof.h>
-#include <xen/irq.h>
 #include <asm/system.h>
 #include <asm/regs.h>
 #include <asm/types.h>
@@ -819,7 +819,7 @@ static void core2_vpmu_destroy(struct vc
     vpmu_clear(vpmu);
 }
 
-static const struct arch_vpmu_ops core2_vpmu_ops = {
+static const struct arch_vpmu_ops __initconstrel core2_vpmu_ops = {
     .do_wrmsr = core2_vpmu_do_wrmsr,
     .do_rdmsr = core2_vpmu_do_rdmsr,
     .do_interrupt = core2_vpmu_do_interrupt,
@@ -893,12 +893,12 @@ int vmx_vpmu_initialise(struct vcpu *v)
     if ( is_pv_vcpu(v) && !core2_vpmu_alloc_resource(v) )
         return -EIO;
 
-    vpmu->arch_vpmu_ops = &core2_vpmu_ops;
+    vpmu_set(vpmu, VPMU_INITIALIZED);
 
     return 0;
 }
 
-int __init core2_vpmu_init(void)
+const struct arch_vpmu_ops *__init core2_vpmu_init(void)
 {
     unsigned int version = 0;
     unsigned int i;
@@ -921,13 +921,13 @@ int __init core2_vpmu_init(void)
     default:
         printk(XENLOG_WARNING "VPMU: PMU version %u is not supported\n",
                version);
-        return -EINVAL;
+        return ERR_PTR(-EINVAL);
     }
 
     if ( current_cpu_data.x86 != 6 )
     {
         printk(XENLOG_WARNING "VPMU: only family 6 is supported\n");
-        return -EINVAL;
+        return ERR_PTR(-EINVAL);
     }
 
     arch_pmc_cnt = core2_get_arch_pmc_count();
@@ -972,9 +972,9 @@ int __init core2_vpmu_init(void)
         printk(XENLOG_WARNING
                "VPMU: Register bank does not fit into VPMU share page\n");
         arch_pmc_cnt = fixed_pmc_cnt = 0;
-        return -ENOSPC;
+        return ERR_PTR(-ENOSPC);
     }
 
-    return 0;
+    return &core2_vpmu_ops;
 }
 
xen/include/asm-x86/vpmu.h +14 -14
@@ -49,10 +49,10 @@ struct arch_vpmu_ops {
     void (*arch_vpmu_dump)(const struct vcpu *);
 };
 
-int core2_vpmu_init(void);
+const struct arch_vpmu_ops *core2_vpmu_init(void);
 int vmx_vpmu_initialise(struct vcpu *);
-int amd_vpmu_init(void);
-int hygon_vpmu_init(void);
+const struct arch_vpmu_ops *amd_vpmu_init(void);
+const struct arch_vpmu_ops *hygon_vpmu_init(void);
 int svm_vpmu_initialise(struct vcpu *);
 
 struct vpmu_struct {
@@ -61,25 +61,25 @@ struct vpmu_struct {
     u32 hw_lapic_lvtpc;
     void *context;      /* May be shared with PV guest */
     void *priv_context; /* hypervisor-only */
-    const struct arch_vpmu_ops *arch_vpmu_ops;
     struct xen_pmu_data *xenpmu_data;
     spinlock_t vpmu_lock;
 };
 
 /* VPMU states */
-#define VPMU_CONTEXT_ALLOCATED              0x1
-#define VPMU_CONTEXT_LOADED                 0x2
-#define VPMU_RUNNING                        0x4
-#define VPMU_CONTEXT_SAVE                   0x8   /* Force context save */
-#define VPMU_FROZEN                         0x10  /* Stop counters while VCPU is not running */
-#define VPMU_PASSIVE_DOMAIN_ALLOCATED       0x20
+#define VPMU_INITIALIZED                    0x1
+#define VPMU_CONTEXT_ALLOCATED              0x2
+#define VPMU_CONTEXT_LOADED                 0x4
+#define VPMU_RUNNING                        0x8
+#define VPMU_CONTEXT_SAVE                   0x10  /* Force context save */
+#define VPMU_FROZEN                         0x20  /* Stop counters while VCPU is not running */
+#define VPMU_PASSIVE_DOMAIN_ALLOCATED       0x40
 /* PV(H) guests: VPMU registers are accessed by guest from shared page */
-#define VPMU_CACHED                         0x40
-#define VPMU_AVAILABLE                      0x80
+#define VPMU_CACHED                         0x80
+#define VPMU_AVAILABLE                      0x100
 
 /* Intel-specific VPMU features */
-#define VPMU_CPU_HAS_DS                     0x100 /* Has Debug Store */
-#define VPMU_CPU_HAS_BTS                    0x200 /* Has Branch Trace Store */
+#define VPMU_CPU_HAS_DS                     0x1000 /* Has Debug Store */
+#define VPMU_CPU_HAS_BTS                    0x2000 /* Has Branch Trace Store */
 
 static inline void vpmu_set(struct vpmu_struct *vpmu, const u32 mask)
 {
#2 lore raw

Re: [PATCH 1/3] x86/vPMU: convert vendor hook invocations to altcall

Quoted 20 lines · Jan Beulich
xen/arch/x86/cpu/vpmu.c +4 -4
@@ -17,12 +17,12 @@
  *
  * Author: Haitao Shan <[email protected]>
  */
-#include <xen/sched.h>
-#include <xen/xenoprof.h>
-#include <xen/event.h>
-#include <xen/guest_access.h>
 #include <xen/cpu.h>
+#include <xen/err.h>
 #include <xen/param.h>
+#include <xen/event.h>
+#include <xen/guest_access.h>
+#include <xen/sched.h>
 #include <asm/regs.h>
 #include <asm/types.h>
 #include <asm/msr.h>

xen/arch/x86/cpu/vpmu.c CHECK_pmu_params; · line 52

Quoted 4 lines
xen/arch/x86/cpu/vpmu.c +1 -0
@@ -49,6 +49,7 @@ CHECK_pmu_params;
 static unsigned int __read_mostly opt_vpmu_enabled;
 unsigned int __read_mostly vpmu_mode = XENPMU_MODE_OFF;
 unsigned int __read_mostly vpmu_features = 0;
+static struct arch_vpmu_ops __read_mostly vpmu_ops;

Thoughts on renaming to just struct vpmu_ops ?  The arch_ really is quite wrong, and you touch every impacted line in this patch, other than the main struct name itself.

[edit] there are other misuses of arch_.  Perhaps best to defer this to a later change.

Quoted 7 lines
xen/arch/x86/cpu/vpmu.c +1 -2
@@ -133,14 +133,13 @@ int vpmu_do_msr(unsigned int msr, uint64
          goto nop;
 
     vpmu = vcpu_vpmu(curr);
-    ops = vpmu->arch_vpmu_ops;
-    if ( !ops )
+    if ( !vpmu_is_set(vpmu, VPMU_INITIALIZED) )

xen/arch/x86/cpu/vpmu.c int vpmu_do_msr(unsigned int msr, uint64 · line 142

Quoted 10 lines
xen/arch/x86/cpu/vpmu.c +4 -4
         goto nop;
 
-    if ( is_write && ops->do_wrmsr )
-        ret = ops->do_wrmsr(msr, *msr_content, supported);
-    else if ( !is_write && ops->do_rdmsr )
-        ret = ops->do_rdmsr(msr, msr_content);
+    if ( is_write && vpmu_ops.do_wrmsr )
+        ret = alternative_call(vpmu_ops.do_wrmsr, msr, *msr_content, supported);
+    else if ( !is_write && vpmu_ops.do_rdmsr )
+        ret = alternative_call(vpmu_ops.do_rdmsr, msr, msr_content);

Elsewhere, you've dropped the function pointer NULL checks.  Why not here?

Quoted 26 lines
xen/include/asm-x86/vpmu.h +7 -7
@@ -61,25 +61,25 @@ struct vpmu_struct {
     u32 hw_lapic_lvtpc;
     void *context;      /* May be shared with PV guest */
     void *priv_context; /* hypervisor-only */
-    const struct arch_vpmu_ops *arch_vpmu_ops;
     struct xen_pmu_data *xenpmu_data;
     spinlock_t vpmu_lock;
 };
 
 /* VPMU states */
-#define VPMU_CONTEXT_ALLOCATED              0x1
-#define VPMU_CONTEXT_LOADED                 0x2
-#define VPMU_RUNNING                        0x4
-#define VPMU_CONTEXT_SAVE                   0x8   /* Force context save */
-#define VPMU_FROZEN                         0x10  /* Stop counters while VCPU is not running */
-#define VPMU_PASSIVE_DOMAIN_ALLOCATED       0x20
+#define VPMU_INITIALIZED                    0x1
+#define VPMU_CONTEXT_ALLOCATED              0x2
+#define VPMU_CONTEXT_LOADED                 0x4
+#define VPMU_RUNNING                        0x8
+#define VPMU_CONTEXT_SAVE                   0x10  /* Force context save */
+#define VPMU_FROZEN                         0x20  /* Stop counters while VCPU is not running */
+#define VPMU_PASSIVE_DOMAIN_ALLOCATED       0x40
 /* PV(H) guests: VPMU registers are accessed by guest from shared page */

xen/include/asm-x86/vpmu.h struct vpmu_struct { · line 82

Quoted 10 lines
xen/include/asm-x86/vpmu.h +4 -4
-#define VPMU_CACHED                         0x40
-#define VPMU_AVAILABLE                      0x80
+#define VPMU_CACHED                         0x80
+#define VPMU_AVAILABLE                      0x100
 
 /* Intel-specific VPMU features */
-#define VPMU_CPU_HAS_DS                     0x100 /* Has Debug Store */
-#define VPMU_CPU_HAS_BTS                    0x200 /* Has Branch Trace Store */
+#define VPMU_CPU_HAS_DS                     0x1000 /* Has Debug Store */
+#define VPMU_CPU_HAS_BTS                    0x2000 /* Has Branch Trace Store */

Seeing as you're shuffling each of these, how about adding some leading 0's for alignment?

~Andrew

#3 lore raw

Re: [PATCH 1/3] x86/vPMU: convert vendor hook invocations to altcall

Quoted 10 lines · Andrew Cooper
Quoted 9 lines · Jan Beulich
xen/arch/x86/cpu/vpmu.c +1 -2
@@ -133,14 +133,13 @@ int vpmu_do_msr(unsigned int msr, uint64
          goto nop;
 
     vpmu = vcpu_vpmu(curr);
-    ops = vpmu->arch_vpmu_ops;
-    if ( !ops )
+    if ( !vpmu_is_set(vpmu, VPMU_INITIALIZED) )
         goto nop;
 
Quoted 10 lines

xen/arch/x86/cpu/vpmu.c int vpmu_do_msr(unsigned int msr, uint64 · line 142

Quoted 8 lines
xen/arch/x86/cpu/vpmu.c +4 -4
-    if ( is_write && ops->do_wrmsr )
-        ret = ops->do_wrmsr(msr, *msr_content, supported);
-    else if ( !is_write && ops->do_rdmsr )
-        ret = ops->do_rdmsr(msr, msr_content);
+    if ( is_write && vpmu_ops.do_wrmsr )
+        ret = alternative_call(vpmu_ops.do_wrmsr, msr, *msr_content, supported);
+    else if ( !is_write && vpmu_ops.do_rdmsr )
+        ret = alternative_call(vpmu_ops.do_rdmsr, msr, msr_content);

Elsewhere, you've dropped the function pointer NULL checks.  Why not here?

No, I'm not dropping any function pointer checks here; all I drop is checks of the ops pointer being NULL. These checks all get dropped in patch 3.

Quoted 31 lines
Quoted 31 lines
xen/include/asm-x86/vpmu.h +9 -9
@@ -61,25 +61,25 @@ struct vpmu_struct {
     u32 hw_lapic_lvtpc;
     void *context;      /* May be shared with PV guest */
     void *priv_context; /* hypervisor-only */
-    const struct arch_vpmu_ops *arch_vpmu_ops;
     struct xen_pmu_data *xenpmu_data;
     spinlock_t vpmu_lock;
 };
 
 /* VPMU states */
-#define VPMU_CONTEXT_ALLOCATED              0x1
-#define VPMU_CONTEXT_LOADED                 0x2
-#define VPMU_RUNNING                        0x4
-#define VPMU_CONTEXT_SAVE                   0x8   /* Force context save */
-#define VPMU_FROZEN                         0x10  /* Stop counters while VCPU is not running */
-#define VPMU_PASSIVE_DOMAIN_ALLOCATED       0x20
+#define VPMU_INITIALIZED                    0x1
+#define VPMU_CONTEXT_ALLOCATED              0x2
+#define VPMU_CONTEXT_LOADED                 0x4
+#define VPMU_RUNNING                        0x8
+#define VPMU_CONTEXT_SAVE                   0x10  /* Force context save */
+#define VPMU_FROZEN                         0x20  /* Stop counters while VCPU is not running */
+#define VPMU_PASSIVE_DOMAIN_ALLOCATED       0x40
 /* PV(H) guests: VPMU registers are accessed by guest from shared page */
-#define VPMU_CACHED                         0x40
-#define VPMU_AVAILABLE                      0x80
+#define VPMU_CACHED                         0x80
+#define VPMU_AVAILABLE                      0x100
 
Quoted 8 lines

xen/include/asm-x86/vpmu.h struct vpmu_struct { · line 82

Quoted 5 lines
xen/include/asm-x86/vpmu.h +2 -2
 /* Intel-specific VPMU features */
-#define VPMU_CPU_HAS_DS                     0x100 /* Has Debug Store */
-#define VPMU_CPU_HAS_BTS                    0x200 /* Has Branch Trace Store */
+#define VPMU_CPU_HAS_DS                     0x1000 /* Has Debug Store */
+#define VPMU_CPU_HAS_BTS                    0x2000 /* Has Branch Trace Store */

Seeing as you're shuffling each of these, how about adding some leading 0's for alignment?

Fine with me; I did consider it at the time of writing the patch, but decided that such a change of non-mandatory style may not be justified here (or even in general), as there are also downsides to such padding: Once adding a constant with more significant digits, all pre-existing ones need touching to insert yet another zero.

Jan

Reviewed-by
#4 lore raw

Re: [PATCH 1/3] x86/vPMU: convert vendor hook invocations to altcall

Quoted 20 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper
Quoted 9 lines · Jan Beulich
xen/arch/x86/cpu/vpmu.c +1 -2
@@ -133,14 +133,13 @@ int vpmu_do_msr(unsigned int msr, uint64
          goto nop;
 
     vpmu = vcpu_vpmu(curr);
-    ops = vpmu->arch_vpmu_ops;
-    if ( !ops )
+    if ( !vpmu_is_set(vpmu, VPMU_INITIALIZED) )
         goto nop;
 
Quoted 9 lines

xen/arch/x86/cpu/vpmu.c int vpmu_do_msr(unsigned int msr, uint64 · line 142

Quoted 8 lines
xen/arch/x86/cpu/vpmu.c +4 -4
-    if ( is_write && ops->do_wrmsr )
-        ret = ops->do_wrmsr(msr, *msr_content, supported);
-    else if ( !is_write && ops->do_rdmsr )
-        ret = ops->do_rdmsr(msr, msr_content);
+    if ( is_write && vpmu_ops.do_wrmsr )
+        ret = alternative_call(vpmu_ops.do_wrmsr, msr, *msr_content, supported);
+    else if ( !is_write && vpmu_ops.do_rdmsr )
+        ret = alternative_call(vpmu_ops.do_rdmsr, msr, msr_content);

Elsewhere, you've dropped the function pointer NULL checks.  Why not here?

Quoted 3 lines

No, I'm not dropping any function pointer checks here; all I drop is checks of the ops pointer being NULL. These checks all get dropped in patch 3.

Oh ok.

Quoted 39 lines
Quoted 32 lines
Quoted 32 lines
xen/include/asm-x86/vpmu.h +9 -9
@@ -61,25 +61,25 @@ struct vpmu_struct {
     u32 hw_lapic_lvtpc;
     void *context;      /* May be shared with PV guest */
     void *priv_context; /* hypervisor-only */
-    const struct arch_vpmu_ops *arch_vpmu_ops;
     struct xen_pmu_data *xenpmu_data;
     spinlock_t vpmu_lock;
 };
 
 /* VPMU states */
-#define VPMU_CONTEXT_ALLOCATED              0x1
-#define VPMU_CONTEXT_LOADED                 0x2
-#define VPMU_RUNNING                        0x4
-#define VPMU_CONTEXT_SAVE                   0x8   /* Force context save */
-#define VPMU_FROZEN                         0x10  /* Stop counters while VCPU is not running */
-#define VPMU_PASSIVE_DOMAIN_ALLOCATED       0x20
+#define VPMU_INITIALIZED                    0x1
+#define VPMU_CONTEXT_ALLOCATED              0x2
+#define VPMU_CONTEXT_LOADED                 0x4
+#define VPMU_RUNNING                        0x8
+#define VPMU_CONTEXT_SAVE                   0x10  /* Force context save */
+#define VPMU_FROZEN                         0x20  /* Stop counters while VCPU is not running */
+#define VPMU_PASSIVE_DOMAIN_ALLOCATED       0x40
 /* PV(H) guests: VPMU registers are accessed by guest from shared page */
-#define VPMU_CACHED                         0x40
-#define VPMU_AVAILABLE                      0x80
+#define VPMU_CACHED                         0x80
+#define VPMU_AVAILABLE                      0x100
 
Quoted 7 lines

xen/include/asm-x86/vpmu.h struct vpmu_struct { · line 82

Quoted 5 lines
xen/include/asm-x86/vpmu.h +2 -2
 /* Intel-specific VPMU features */
-#define VPMU_CPU_HAS_DS                     0x100 /* Has Debug Store */
-#define VPMU_CPU_HAS_BTS                    0x200 /* Has Branch Trace Store */
+#define VPMU_CPU_HAS_DS                     0x1000 /* Has Debug Store */
+#define VPMU_CPU_HAS_BTS                    0x2000 /* Has Branch Trace Store */

Seeing as you're shuffling each of these, how about adding some leading 0's for alignment?

Quoted 6 lines

Fine with me; I did consider it at the time of writing the patch, but decided that such a change of non-mandatory style may not be justified here (or even in general), as there are also downsides to such padding: Once adding a constant with more significant digits, all pre-existing ones need touching to insert yet another zero.

I don't mind specifically if it gets left as-is, but having a pile of constants like this tabulated correct makes a massive improvement to code clarity.

That said, this whole flags infrastructure is almost exclusively obfuscation, and I've got a good mind to replace it all with a bitfield.  I'll save taking some shears to this code for another time.

~Andrew

#5 lore raw

PATCH 2/3 x86/vPMU: invoke <vendor>_vpmu_initialise() through a hook as well

In reply to Jan Beulich

I see little point in having an open-coded switch() statement to achieve the same; like other vendor-specific operations the function can be supplied in the respective ops structure instances.

xen/arch/x86/cpu/vpmu.c +10 -15
@@ -455,21 +455,11 @@ static int vpmu_arch_initialise(struct v
 
     ASSERT(!(vpmu->flags & ~VPMU_AVAILABLE) && !vpmu->context);
 
-    if ( !vpmu_available(v) )
+    if ( !vpmu_available(v) || vpmu_mode == XENPMU_MODE_OFF )
         return 0;
 
-    switch ( vendor )
+    if ( !vpmu_ops.initialise )
     {
-    case X86_VENDOR_AMD:
-    case X86_VENDOR_HYGON:
-        ret = svm_vpmu_initialise(v);
-        break;
-
-    case X86_VENDOR_INTEL:
-        ret = vmx_vpmu_initialise(v);
-        break;
-
-    default:
         if ( vpmu_mode != XENPMU_MODE_OFF )
         {
             printk(XENLOG_G_WARNING "VPMU: Unknown CPU vendor %d. "
@@ -480,12 +470,17 @@ static int vpmu_arch_initialise(struct v
         return -EINVAL;
     }
 
-    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
-
+    ret = alternative_call(vpmu_ops.initialise, v);
     if ( ret )
+    {
         printk(XENLOG_G_WARNING "VPMU: Initialization failed for %pv\n", v);
+        return ret;
+    }
+
+    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
+    vpmu_set(vpmu, VPMU_INITIALIZED);
 
-    return ret;
+    return 0;
 }
 
 static void get_vpmu(struct vcpu *v)
xen/arch/x86/cpu/vpmu_amd.c +13 -15
@@ -483,24 +483,11 @@ static void amd_vpmu_dump(const struct v
     }
 }
 
-static const struct arch_vpmu_ops __initconstrel amd_vpmu_ops = {
-    .do_wrmsr = amd_vpmu_do_wrmsr,
-    .do_rdmsr = amd_vpmu_do_rdmsr,
-    .do_interrupt = amd_vpmu_do_interrupt,
-    .arch_vpmu_destroy = amd_vpmu_destroy,
-    .arch_vpmu_save = amd_vpmu_save,
-    .arch_vpmu_load = amd_vpmu_load,
-    .arch_vpmu_dump = amd_vpmu_dump
-};
-
-int svm_vpmu_initialise(struct vcpu *v)
+static int svm_vpmu_initialise(struct vcpu *v)
 {
     struct xen_pmu_amd_ctxt *ctxt;
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
 
-    if ( vpmu_mode == XENPMU_MODE_OFF )
-        return 0;
-
     if ( !counters )
         return -EINVAL;
 
@@ -529,11 +516,22 @@ int svm_vpmu_initialise(struct vcpu *v)
                offsetof(struct xen_pmu_amd_ctxt, regs));
     }
 
-    vpmu_set(vpmu, VPMU_INITIALIZED | VPMU_CONTEXT_ALLOCATED);
+    vpmu_set(vpmu, VPMU_CONTEXT_ALLOCATED);
 
     return 0;
 }
 
+static const struct arch_vpmu_ops __initconstrel amd_vpmu_ops = {
+    .initialise = svm_vpmu_initialise,
+    .do_wrmsr = amd_vpmu_do_wrmsr,
+    .do_rdmsr = amd_vpmu_do_rdmsr,
+    .do_interrupt = amd_vpmu_do_interrupt,
+    .arch_vpmu_destroy = amd_vpmu_destroy,
+    .arch_vpmu_save = amd_vpmu_save,
+    .arch_vpmu_load = amd_vpmu_load,
+    .arch_vpmu_dump = amd_vpmu_dump
+};
+
 static const struct arch_vpmu_ops *__init common_init(void)
 {
     unsigned int i;
xen/arch/x86/cpu/vpmu_intel.c +12 -16
@@ -819,25 +819,12 @@ static void core2_vpmu_destroy(struct vc
     vpmu_clear(vpmu);
 }
 
-static const struct arch_vpmu_ops __initconstrel core2_vpmu_ops = {
-    .do_wrmsr = core2_vpmu_do_wrmsr,
-    .do_rdmsr = core2_vpmu_do_rdmsr,
-    .do_interrupt = core2_vpmu_do_interrupt,
-    .arch_vpmu_destroy = core2_vpmu_destroy,
-    .arch_vpmu_save = core2_vpmu_save,
-    .arch_vpmu_load = core2_vpmu_load,
-    .arch_vpmu_dump = core2_vpmu_dump
-};
-
-int vmx_vpmu_initialise(struct vcpu *v)
+static int vmx_vpmu_initialise(struct vcpu *v)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
     u64 msr_content;
     static bool_t ds_warned;
 
-    if ( vpmu_mode == XENPMU_MODE_OFF )
-        return 0;
-
     if ( v->domain->arch.cpuid->basic.pmu_version <= 1 ||
          v->domain->arch.cpuid->basic.pmu_version >= 6 )
         return -EINVAL;
@@ -893,11 +880,20 @@ int vmx_vpmu_initialise(struct vcpu *v)
     if ( is_pv_vcpu(v) && !core2_vpmu_alloc_resource(v) )
         return -EIO;
 
-    vpmu_set(vpmu, VPMU_INITIALIZED);
-
     return 0;
 }
 
+static const struct arch_vpmu_ops __initconstrel core2_vpmu_ops = {
+    .initialise = vmx_vpmu_initialise,
+    .do_wrmsr = core2_vpmu_do_wrmsr,
+    .do_rdmsr = core2_vpmu_do_rdmsr,
+    .do_interrupt = core2_vpmu_do_interrupt,
+    .arch_vpmu_destroy = core2_vpmu_destroy,
+    .arch_vpmu_save = core2_vpmu_save,
+    .arch_vpmu_load = core2_vpmu_load,
+    .arch_vpmu_dump = core2_vpmu_dump
+};
+
 const struct arch_vpmu_ops *__init core2_vpmu_init(void)
 {
     unsigned int version = 0;
xen/include/asm-x86/vpmu.h +1 -2
@@ -39,6 +39,7 @@
 
 /* Arch specific operations shared by all vpmus */
 struct arch_vpmu_ops {
+    int (*initialise)(struct vcpu *v);
     int (*do_wrmsr)(unsigned int msr, uint64_t msr_content,
                     uint64_t supported);
     int (*do_rdmsr)(unsigned int msr, uint64_t *msr_content);
@@ -50,10 +51,8 @@ struct arch_vpmu_ops {
 };
 
 const struct arch_vpmu_ops *core2_vpmu_init(void);
-int vmx_vpmu_initialise(struct vcpu *);
 const struct arch_vpmu_ops *amd_vpmu_init(void);
 const struct arch_vpmu_ops *hygon_vpmu_init(void);
-int svm_vpmu_initialise(struct vcpu *);
 
 struct vpmu_struct {
     u32 flags;
Reviewed-by on v1 2/3
#6 lore raw

Re: [PATCH 2/3] x86/vPMU: invoke <vendor>_vpmu_initialise() through a hook as well

Quoted 5 lines · Jan Beulich

I see little point in having an open-coded switch() statement to achieve the same; like other vendor-specific operations the function can be supplied in the respective ops structure instances.

Yeah, that logic was just bizarre.

Now that the ARM folks have added XEN_DOMCTL_CDF_vpmu, there is a huge quantity of simplification which can be done around vpmu_available()/etc, but that's definitely future work.

I think it would probably be good to get a position where we can enable hwdom vpmu by default.  There is a spec on how to share perfcounters with firmware, which we ought to investigate to let the watchdog coexists beside vPMU.

Quoted 7 lines
xen/arch/x86/cpu/vpmu.c +0 -1
@@ -480,12 +470,17 @@ static int vpmu_arch_initialise(struct v
         return -EINVAL;
     }
 
-    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;

xen/arch/x86/cpu/vpmu.c static int vpmu_arch_initialise(struct v · line 481

Quoted 10 lines
xen/arch/x86/cpu/vpmu.c +7 -1
-
+    ret = alternative_call(vpmu_ops.initialise, v);
     if ( ret )
+    {
         printk(XENLOG_G_WARNING "VPMU: Initialization failed for %pv\n", v);
+        return ret;
+    }
+
+    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
+    vpmu_set(vpmu, VPMU_INITIALIZED);

It occurs to me that if, in previous patch, you do:

    if ( ret )
        printk(XENLOG_G_WARNING "VPMU: Initialization failed for %pv\n", v); +    else
+        vpmu_set(vpmu, VPMU_INITIALIZED);

then you don't need to undo the adjustments in
{svm,vmx}_vpmu_initialise() in this patch.

Quoted 17 lines
xen/arch/x86/cpu/vpmu.c +1 -1
-    return ret;
+    return 0;
 }
 
 static void get_vpmu(struct vcpu *v)
xen/arch/x86/cpu/vpmu_amd.c +1 -1
@@ -529,11 +516,22 @@ int svm_vpmu_initialise(struct vcpu *v)
                offsetof(struct xen_pmu_amd_ctxt, regs));
     }
 
-    vpmu_set(vpmu, VPMU_INITIALIZED | VPMU_CONTEXT_ALLOCATED);
+    vpmu_set(vpmu, VPMU_CONTEXT_ALLOCATED);
 
     return 0;
 }

xen/arch/x86/cpu/vpmu_amd.c int svm_vpmu_initialise(struct vcpu *v) · line 532

Quoted 10 lines
xen/arch/x86/cpu/vpmu_amd.c +9 -0
 
+static const struct arch_vpmu_ops __initconstrel amd_vpmu_ops = {
+    .initialise = svm_vpmu_initialise,
+    .do_wrmsr = amd_vpmu_do_wrmsr,
+    .do_rdmsr = amd_vpmu_do_rdmsr,
+    .do_interrupt = amd_vpmu_do_interrupt,
+    .arch_vpmu_destroy = amd_vpmu_destroy,
+    .arch_vpmu_save = amd_vpmu_save,
+    .arch_vpmu_load = amd_vpmu_load,
+    .arch_vpmu_dump = amd_vpmu_dump

As you're moving everything, trailing comma please.

Quoted 10 lines
xen/arch/x86/cpu/vpmu_intel.c +0 -2
@@ -893,11 +880,20 @@ int vmx_vpmu_initialise(struct vcpu *v)
     if ( is_pv_vcpu(v) && !core2_vpmu_alloc_resource(v) )
         return -EIO;
 
-    vpmu_set(vpmu, VPMU_INITIALIZED);
-
     return 0;
 }

xen/arch/x86/cpu/vpmu_intel.c int vmx_vpmu_initialise(struct vcpu *v) · line 894

Quoted 10 lines
xen/arch/x86/cpu/vpmu_intel.c +9 -0
 
+static const struct arch_vpmu_ops __initconstrel core2_vpmu_ops = {
+    .initialise = vmx_vpmu_initialise,
+    .do_wrmsr = core2_vpmu_do_wrmsr,
+    .do_rdmsr = core2_vpmu_do_rdmsr,
+    .do_interrupt = core2_vpmu_do_interrupt,
+    .arch_vpmu_destroy = core2_vpmu_destroy,
+    .arch_vpmu_save = core2_vpmu_save,
+    .arch_vpmu_load = core2_vpmu_load,
+    .arch_vpmu_dump = core2_vpmu_dump

And here.

preferably with the VPMU_INITIALIZED adjustment.

~Andrew

#7 lore raw

Re: [PATCH 2/3] x86/vPMU: invoke <vendor>_vpmu_initialise() through a hook as well

Quoted 21 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
xen/arch/x86/cpu/vpmu.c +0 -1
@@ -480,12 +470,17 @@ static int vpmu_arch_initialise(struct v
         return -EINVAL;
     }
 
-    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;

xen/arch/x86/cpu/vpmu.c static int vpmu_arch_initialise(struct v · line 481

Quoted 10 lines
xen/arch/x86/cpu/vpmu.c +7 -1
-
+    ret = alternative_call(vpmu_ops.initialise, v);
     if ( ret )
+    {
         printk(XENLOG_G_WARNING "VPMU: Initialization failed for %pv\n", v);
+        return ret;
+    }
+
+    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
+    vpmu_set(vpmu, VPMU_INITIALIZED);

It occurs to me that if, in previous patch, you do:

Quoted 7 lines

    if ( ret )
        printk(XENLOG_G_WARNING "VPMU: Initialization failed for %pv\n", v); +    else
+        vpmu_set(vpmu, VPMU_INITIALIZED);

then you don't need to undo the adjustments in
{svm,vmx}_vpmu_initialise() in this patch.

I actually had it that way first, until noticing it was wrong. It can be done only here because it if only here where the XENPMU_MODE_OFF checks move from the vendor functions into here.

Quoted 2 lines

preferably with the VPMU_INITIALIZED adjustment.

Thanks; as said, that adjustment can't be done in patch 1 just yet. But I did add the missing trailing commas.

Jan

#8 lore raw

Re: [PATCH 2/3] x86/vPMU: invoke <vendor>_vpmu_initialise() through a hook as well

Quoted 26 lines · Jan Beulich
Quoted 25 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
xen/arch/x86/cpu/vpmu.c +0 -1
@@ -480,12 +470,17 @@ static int vpmu_arch_initialise(struct v
         return -EINVAL;
     }
 
-    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;

xen/arch/x86/cpu/vpmu.c static int vpmu_arch_initialise(struct v · line 481

Quoted 10 lines
xen/arch/x86/cpu/vpmu.c +7 -1
-
+    ret = alternative_call(vpmu_ops.initialise, v);
     if ( ret )
+    {
         printk(XENLOG_G_WARNING "VPMU: Initialization failed for %pv\n", v);
+        return ret;
+    }
+
+    vpmu->hw_lapic_lvtpc = PMU_APIC_VECTOR | APIC_LVT_MASKED;
+    vpmu_set(vpmu, VPMU_INITIALIZED);

It occurs to me that if, in previous patch, you do:

    if ( ret )
        printk(XENLOG_G_WARNING "VPMU: Initialization failed for %pv\n", v); +    else
+        vpmu_set(vpmu, VPMU_INITIALIZED);

Quoted 5 lines
Quoted 2 lines

then you don't need to undo the adjustments in
{svm,vmx}_vpmu_initialise() in this patch.

I actually had it that way first, until noticing it was wrong. It can be done only here because it if only here where the XENPMU_MODE_OFF checks move from the vendor functions into here.

Oh ok.  Never mind then.

Quoted 4 lines
Quoted 2 lines

preferably with the VPMU_INITIALIZED adjustment.

Thanks; as said, that adjustment can't be done in patch 1 just yet. But I did add the missing trailing commas.

Thanks.

~Andrew

#9 lore raw

PATCH 3/3 x86/vPMU: move vpmu_ops to .init.data

In reply to Jan Beulich

Both vendors' code populates all hooks, so there's no need to have any NULL checks before invoking the hook functions. With that the only remaining uses of the object are in alternative_{,v}call(), i.e. none after alternatives patching.

In vpmu_arch_initialise() the check gets replaced by an opt_vpmu_enabled one, as I couldn't convince myself that the pre-existing checks would be sufficient to cover all possible cases.

xen/arch/x86/cpu/vpmu.c +20 -34
@@ -49,7 +49,7 @@ CHECK_pmu_params;
 static unsigned int __read_mostly opt_vpmu_enabled;
 unsigned int __read_mostly vpmu_mode = XENPMU_MODE_OFF;
 unsigned int __read_mostly vpmu_features = 0;
-static struct arch_vpmu_ops __read_mostly vpmu_ops;
+static struct arch_vpmu_ops __initdata vpmu_ops;
 
 static DEFINE_SPINLOCK(vpmu_lock);
 static unsigned vpmu_count;
@@ -136,12 +136,10 @@ int vpmu_do_msr(unsigned int msr, uint64
     if ( !vpmu_is_set(vpmu, VPMU_INITIALIZED) )
         goto nop;
 
-    if ( is_write && vpmu_ops.do_wrmsr )
+    if ( is_write )
         ret = alternative_call(vpmu_ops.do_wrmsr, msr, *msr_content, supported);
-    else if ( !is_write && vpmu_ops.do_rdmsr )
-        ret = alternative_call(vpmu_ops.do_rdmsr, msr, msr_content);
     else
-        goto nop;
+        ret = alternative_call(vpmu_ops.do_rdmsr, msr, msr_content);
 
     /*
      * We may have received a PMU interrupt while handling MSR access
@@ -375,7 +373,7 @@ void vpmu_save(struct vcpu *v)
 int vpmu_load(struct vcpu *v, bool_t from_guest)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
-    int pcpu = smp_processor_id();
+    int pcpu = smp_processor_id(), ret;
     struct vcpu *prev = NULL;
 
     if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
@@ -423,21 +421,13 @@ int vpmu_load(struct vcpu *v, bool_t fro
          vpmu_is_set(vpmu, VPMU_CACHED)) )
         return 0;
 
-    if ( vpmu_ops.arch_vpmu_load )
-    {
-        int ret;
-
-        apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
-        /* Arch code needs to set VPMU_CONTEXT_LOADED */
-        ret = alternative_call(vpmu_ops.arch_vpmu_load, v, from_guest);
-        if ( ret )
-        {
-            apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc | APIC_LVT_MASKED);
-            return ret;
-        }
-    }
+    apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
+    /* Arch code needs to set VPMU_CONTEXT_LOADED */
+    ret = alternative_call(vpmu_ops.arch_vpmu_load, v, from_guest);
+    if ( ret )
+        apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc | APIC_LVT_MASKED);
 
-    return 0;
+    return ret;
 }
 
 static int vpmu_arch_initialise(struct vcpu *v)
@@ -458,7 +448,7 @@ static int vpmu_arch_initialise(struct v
     if ( !vpmu_available(v) || vpmu_mode == XENPMU_MODE_OFF )
         return 0;
 
-    if ( !vpmu_ops.initialise )
+    if ( !opt_vpmu_enabled )
     {
         if ( vpmu_mode != XENPMU_MODE_OFF )
         {
@@ -564,18 +554,15 @@ static void vpmu_arch_destroy(struct vcp
         on_selected_cpus(cpumask_of(vpmu->last_pcpu),
                          vpmu_clear_last, v, 1);
 
-    if ( vpmu_ops.arch_vpmu_destroy )
-    {
-        /*
-         * Unload VPMU first if VPMU_CONTEXT_LOADED being set.
-         * This will stop counters.
-         */
-        if ( vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
-            on_selected_cpus(cpumask_of(vcpu_vpmu(v)->last_pcpu),
-                             vpmu_save_force, v, 1);
+    /*
+     * Unload VPMU first if VPMU_CONTEXT_LOADED being set.
+     * This will stop counters.
+     */
+    if ( vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )
+        on_selected_cpus(cpumask_of(vcpu_vpmu(v)->last_pcpu),
+                         vpmu_save_force, v, 1);
 
-         alternative_vcall(vpmu_ops.arch_vpmu_destroy, v);
-    }
+    alternative_vcall(vpmu_ops.arch_vpmu_destroy, v);
 
     vpmu_reset(vpmu, VPMU_CONTEXT_ALLOCATED);
 }
@@ -681,8 +668,7 @@ static void pvpmu_finish(struct domain *
 /* Dump some vpmu information to console. Used in keyhandler dump_domains(). */
 void vpmu_dump(struct vcpu *v)
 {
-    if ( vpmu_is_set(vcpu_vpmu(v), VPMU_INITIALIZED) &&
-         vpmu_ops.arch_vpmu_dump )
+    if ( vpmu_is_set(vcpu_vpmu(v), VPMU_INITIALIZED) )
         alternative_vcall(vpmu_ops.arch_vpmu_dump, v);
 }
 
Reviewed-by on v1 3/3
#10 lore raw

Re: [PATCH 3/3] x86/vPMU: move vpmu_ops to .init.data

Quoted 8 lines · Jan Beulich

Both vendors' code populates all hooks, so there's no need to have any NULL checks before invoking the hook functions. With that the only remaining uses of the object are in alternative_{,v}call(), i.e. none after alternatives patching.

In vpmu_arch_initialise() the check gets replaced by an opt_vpmu_enabled one, as I couldn't convince myself that the pre-existing checks would be sufficient to cover all possible cases.

Yeah - that sounds safe.  As I said on the previous patch, there is a massive quantity of clean-up and simplification to do now with XEN_DOMCTL_CDF_vpmu.

Quoted 1 line
#11 lore raw

[PATCH 4/3] x86/vPMU: Drop supported parameter from the wrmsr path

In reply to Jan Beulich

The supported parameter was added in 2d9b91f1aeaa ("VMX/vPMU: fix DebugCtl MSR handling"), it laid the groundwork for XSA-269, and in the meantime 2a8a8e99feb9 ("x86/vtx: Fix the checking for unknown/invalid MSR_DEBUGCTL bits") totally rewrote MSR_DEBUGCTL handling.

Strip out the parameter again.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/cpu/vpmu.c        |  5 ++---
 xen/arch/x86/cpu/vpmu_amd.c    |  5 +----
 xen/arch/x86/cpu/vpmu_intel.c  |  5 +----
 xen/arch/x86/hvm/svm/svm.c     |  2 +-
 xen/arch/x86/hvm/vmx/vmx.c     |  2 +-
 xen/arch/x86/pv/emul-priv-op.c |  2 +-
 xen/include/asm-x86/vpmu.h     | 13 +++++--------
 7 files changed, 12 insertions(+), 22 deletions(-)
xen/arch/x86/cpu/vpmu.c +2 -3
index 7fd69a2a58e3..8ec4547bedf4 100644
@@ -116,8 +116,7 @@ void vpmu_lvtpc_update(uint32_t val)
         apic_write(APIC_LVTPC, vpmu->hw_lapic_lvtpc);
 }
 
-int vpmu_do_msr(unsigned int msr, uint64_t *msr_content,
-                uint64_t supported, bool_t is_write)
+int vpmu_do_msr(unsigned int msr, uint64_t *msr_content, bool is_write)
 {
     struct vcpu *curr = current;
     struct vpmu_struct *vpmu;
@@ -137,7 +136,7 @@ int vpmu_do_msr(unsigned int msr, uint64_t *msr_content,
         goto nop;
 
     if ( is_write )
-        ret = alternative_call(vpmu_ops.do_wrmsr, msr, *msr_content, supported);
+        ret = alternative_call(vpmu_ops.do_wrmsr, msr, *msr_content);
     else
         ret = alternative_call(vpmu_ops.do_rdmsr, msr, msr_content);
 
xen/arch/x86/cpu/vpmu_amd.c +1 -4
index b28c78b81109..903fe1887ef0 100644
@@ -348,16 +348,13 @@ static void context_update(unsigned int msr, u64 msr_content)
     }
 }
 
-static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content,
-                             uint64_t supported)
+static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
 {
     struct vcpu *v = current;
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
     unsigned int idx = 0;
     int type = get_pmu_reg_type(msr, &idx);
 
-    ASSERT(!supported);
-
     if ( (type == MSR_TYPE_CTRL ) &&
          ((msr_content & CTRL_RSVD_MASK) != ctrl_rsvd[idx]) )
         return -EINVAL;
xen/arch/x86/cpu/vpmu_intel.c +1 -4
index 63e798b9edac..076882c615f4 100644
@@ -522,8 +522,7 @@ static int core2_vpmu_msr_common_check(u32 msr_index, int *type, int *index)
     return 1;
 }
 
-static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content,
-                               uint64_t supported)
+static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
 {
     int i, tmp;
     int type = -1, index = -1;
@@ -535,8 +534,6 @@ static int core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content,
     if ( !core2_vpmu_msr_common_check(msr, &type, &index) )
         return -EINVAL;
 
-    ASSERT(!supported);
-
     if ( (type == MSR_TYPE_COUNTER) && (msr_content & fixed_counters_mask) )
         /* Writing unsupported bits to a fixed counter */
         return -EINVAL;
xen/arch/x86/hvm/svm/svm.c +1 -1
index f0e10dec046e..fae39c4b4cbd 100644
@@ -2113,7 +2113,7 @@ static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content)
     case MSR_AMD_FAM15H_EVNTSEL3:
     case MSR_AMD_FAM15H_EVNTSEL4:
     case MSR_AMD_FAM15H_EVNTSEL5:
-        if ( vpmu_do_wrmsr(msr, msr_content, 0) )
+        if ( vpmu_do_wrmsr(msr, msr_content) )
             goto gpf;
         break;
 
xen/arch/x86/hvm/vmx/vmx.c +1 -1
index 37c31c08b984..a7a0d662342a 100644
@@ -3487,7 +3487,7 @@ static int vmx_msr_write_intercept(unsigned int msr, uint64_t msr_content)
     case MSR_CORE_PERF_FIXED_CTR_CTRL...MSR_CORE_PERF_GLOBAL_OVF_CTRL:
     case MSR_IA32_PEBS_ENABLE:
     case MSR_IA32_DS_AREA:
-         if ( vpmu_do_wrmsr(msr, msr_content, 0) )
+         if ( vpmu_do_wrmsr(msr, msr_content) )
             goto gp_fault;
         break;
 
xen/arch/x86/pv/emul-priv-op.c +1 -1
index 7f4279a0517b..8ba65178e98f 100644
@@ -1163,7 +1163,7 @@ static int write_msr(unsigned int reg, uint64_t val,
                      !is_hardware_domain(currd) )
                     return X86EMUL_OKAY;
 
-                if ( vpmu_do_wrmsr(reg, val, 0) )
+                if ( vpmu_do_wrmsr(reg, val) )
                     break;
                 return X86EMUL_OKAY;
             }
xen/include/asm-x86/vpmu.h +5 -8
index ccf5a0cf90b9..80656891e5a3 100644
@@ -40,8 +40,7 @@
 /* Arch specific operations shared by all vpmus */
 struct arch_vpmu_ops {
     int (*initialise)(struct vcpu *v);
-    int (*do_wrmsr)(unsigned int msr, uint64_t msr_content,
-                    uint64_t supported);
+    int (*do_wrmsr)(unsigned int msr, uint64_t msr_content);
     int (*do_rdmsr)(unsigned int msr, uint64_t *msr_content);
     int (*do_interrupt)(struct cpu_user_regs *regs);
     void (*arch_vpmu_destroy)(struct vcpu *v);
@@ -104,8 +103,7 @@ static inline bool_t vpmu_are_all_set(const struct vpmu_struct *vpmu,
 }
 
 void vpmu_lvtpc_update(uint32_t val);
-int vpmu_do_msr(unsigned int msr, uint64_t *msr_content,
-                uint64_t supported, bool_t is_write);
+int vpmu_do_msr(unsigned int msr, uint64_t *msr_content, bool is_write);
 void vpmu_do_interrupt(struct cpu_user_regs *regs);
 void vpmu_initialise(struct vcpu *v);
 void vpmu_destroy(struct vcpu *v);
@@ -113,14 +111,13 @@ void vpmu_save(struct vcpu *v);
 int vpmu_load(struct vcpu *v, bool_t from_guest);
 void vpmu_dump(struct vcpu *v);
 
-static inline int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content,
-                                uint64_t supported)
+static inline int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
 {
-    return vpmu_do_msr(msr, &msr_content, supported, 1);
+    return vpmu_do_msr(msr, &msr_content, 1);
 }
 static inline int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
 {
-    return vpmu_do_msr(msr, msr_content, 0, 0);
+    return vpmu_do_msr(msr, msr_content, 0);
 }
 
 extern unsigned int vpmu_mode;
Signature · 1 line
2.11.0
author Reviewed-by
#12 lore raw

Re: [PATCH 4/3] x86/vPMU: Drop supported parameter from the wrmsr path

Quoted 8 lines · Andrew Cooper

The supported parameter was added in 2d9b91f1aeaa ("VMX/vPMU: fix DebugCtl MSR handling"), it laid the groundwork for XSA-269, and in the meantime 2a8a8e99feb9 ("x86/vtx: Fix the checking for unknown/invalid MSR_DEBUGCTL bits") totally rewrote MSR_DEBUGCTL handling.

Strip out the parameter again.

albeit please with ...

xen/include/asm-x86/vpmu.h void vpmu_save(struct vcpu *v); · line 116

Quoted 10 lines
xen/include/asm-x86/vpmu.h +2 -3
@@ -113,14 +111,13 @@ void vpmu_save(struct vcpu *v);
 int vpmu_load(struct vcpu *v, bool_t from_guest);
 void vpmu_dump(struct vcpu *v);
 
-static inline int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content,
-                                uint64_t supported)
+static inline int vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
 {
-    return vpmu_do_msr(msr, &msr_content, supported, 1);
+    return vpmu_do_msr(msr, &msr_content, 1);

... "true" and ...

xen/include/asm-x86/vpmu.h void vpmu_save(struct vcpu *v); · line 120

Quoted 5 lines
xen/include/asm-x86/vpmu.h +1 -1
 }
 static inline int vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
 {
-    return vpmu_do_msr(msr, msr_content, 0, 0);
+    return vpmu_do_msr(msr, msr_content, 0);

... "false" used respectively, as you're touching this anyway.

Jan

#13 lore raw

[PATCH 5/3] x86/vPMU: Harden indirect branches

In reply to Jan Beulich

As all function pointer calls are resoved to direct calls on boot, clobber the endbr64 instructions too to make life harder for an attacker which has managed to hijack a function pointer.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/cpu/vpmu_amd.c   | 2 +-
 xen/arch/x86/cpu/vpmu_intel.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/cpu/vpmu_amd.c +1 -1
index 903fe1887ef0..e26f4f584e88 100644
@@ -518,7 +518,7 @@ static int svm_vpmu_initialise(struct vcpu *v)
     return 0;
 }
 
-static const struct arch_vpmu_ops __initconstrel amd_vpmu_ops = {
+static struct arch_vpmu_ops __initdata_cf_clobber amd_vpmu_ops = {
     .initialise = svm_vpmu_initialise,
     .do_wrmsr = amd_vpmu_do_wrmsr,
     .do_rdmsr = amd_vpmu_do_rdmsr,
xen/arch/x86/cpu/vpmu_intel.c +1 -1
index 076882c615f4..98a93d1f3c41 100644
@@ -880,7 +880,7 @@ static int vmx_vpmu_initialise(struct vcpu *v)
     return 0;
 }
 
-static const struct arch_vpmu_ops __initconstrel core2_vpmu_ops = {
+static struct arch_vpmu_ops __initdata_cf_clobber core2_vpmu_ops = {
     .initialise = vmx_vpmu_initialise,
     .do_wrmsr = core2_vpmu_do_wrmsr,
     .do_rdmsr = core2_vpmu_do_rdmsr,
Signature · 1 line
2.11.0
#14 lore raw

Re: [PATCH 5/3] x86/vPMU: Harden indirect branches

xen/arch/x86/cpu/vpmu_amd.c static int svm_vpmu_initialise(struct vcpu *v) · line 521

Quoted 8 lines · Andrew Cooper
xen/arch/x86/cpu/vpmu_amd.c +1 -1
@@ -518,7 +518,7 @@ static int svm_vpmu_initialise(struct vcpu *v)
     return 0;
 }
 
-static const struct arch_vpmu_ops __initconstrel amd_vpmu_ops = {
+static struct arch_vpmu_ops __initdata_cf_clobber amd_vpmu_ops = {

This depends on an uncommitted patch (introducing the annotation) and hence is a little difficult to review without a pointer to that patch (which doesn't look to have "cf_" in its subject, and I didn't recall anything else to search for in my mailbox). The main question I see here is whether it's warranted to drop the const: I'd like to retain it if at all possible, just to document that the structure doesn't get modified at runtime (read: initialization time).

Later... I've spotted it in my to-be-reviewed folder; it's "x86/altcall: Optimise away endbr64 instruction where possible". There's no discussion there either about const. Iirc the reason we need __initconstrel alongside __initconst is for older tool chains complaining about section conflicts. If that's right, for CET-IBT we need relatively new tool chains anyway. Hence the mixing of const and non-const within a section may not be an issue. IOW with newer tool chains all could go in a single section; we'd need two separate annotations only for older tool chains (falling back to __initdata or __initconstrel). Of course at that point it might be easier to have both .init.data.cf_clobber and .init.rodata.cf_clobber in the first place, grouped together by the linker script.

Jan