perf: KVM: Fix, optimize, and clean up callbacks
Subsystems (14)
Sean Christopherson
maintainerv4 cover perf: KVM: Fix, optimize, and clean up callbacks
This is a combination of ~2 series to fix bugs in the perf+KVM callbacks, optimize the callbacks by employing static_call, and do a variety of cleanup in both perf and KVM.
For the non-perf patches, I think everything except patch 13 (Paolo) and patches 15 and 16 (Marc) has the appropriate acks.
Patch 1 fixes a set of mostly-theoretical bugs by protecting the guest callbacks pointer with RCU.
Patches 2 and 3 fix an Intel PT handling bug where KVM incorrectly eats PT interrupts when PT is supposed to be owned entirely by the host.
Patches 4-9 clean up perf's callback infrastructure and switch to static_call for arm64 and x86 (the only survivors).
Patches 10-17 clean up related KVM code and unify the arm64/x86 callbacks.
Based on Linus' tree, commit cb690f5238d7 ("Merge tag 'for-5.16/drivers...).
v4:
- Rebase.
- Collect acks and reviews.
- Fully protect perf_guest_cbs with RCU. [Paolo].
- Add patch to hide arm64's kvm_arm_pmu_available behind CONFIG_HW_PERF_EVENTS=y.
v3:
- https://lore.kernel.org/all/[email protected]/
- Add wrappers for guest callbacks to that stubs can be provided when GUEST_PERF_EVENTS=n.
- s/HAVE_GUEST_PERF_EVENTS/GUEST_PERF_EVENTS and select it from KVM and XEN_PV instead of from top-level arm64/x86. [Paolo]
- Drop an unnecessary synchronize_rcu() when registering callbacks. [Peter]
- Retain a WARN_ON_ONCE() when unregistering callbacks if the caller didn't provide the correct pointer. [Peter]
- Rework the static_call patch to move it all to common perf.
- Add a patch to drop the (un)register stubs, made possible after having KVM+XEN_PV select GUEST_PERF_EVENTS.
- Split dropping guest callback "support" for arm, csky, etc... to a separate patch, to make introducing GUEST_PERF_EVENTS cleaner.
v2 (relative to static_call v10):
- Split the patch into the semantic change (multiplexed ->state) and introduction of static_call.
- Don't use '0' for "not a guest RIP".
- Handle unregister path.
- Drop changes for architectures that can be culled entirely.
v2 (relative to v1):
- https://lkml.kernel.org/r/[email protected]
- Drop per-cpu approach. [Peter]
- Fix mostly-theoretical reload and use-after-free with READ_ONCE(), WRITE_ONCE(), and synchronize_rcu(). [Peter]
- Avoid new exports like the plague. [Peter]
v1:
- https://lkml.kernel.org/r/[email protected]
v10 static_call:
- https://lkml.kernel.org/r/[email protected]
Like Xu (1):
perf/core: Rework guest callbacks to prepare for static_call support
Sean Christopherson (16):
perf: Protect perf_guest_cbs with RCU
KVM: x86: Register perf callbacks after calling vendor's
hardware_setup()
KVM: x86: Register Processor Trace interrupt hook iff PT enabled in
guest
perf: Stop pretending that perf can handle multiple guest callbacks
perf: Drop dead and useless guest "support" from arm, csky, nds32 and
riscv
perf: Add wrappers for invoking guest callbacks
perf: Force architectures to opt-in to guest callbacks perf/core: Use static_call to optimize perf_guest_info_callbacks
KVM: x86: Drop current_vcpu for kvm_running_vcpu + kvm_arch_vcpu
variable
KVM: x86: More precisely identify NMI from guest when handling PMI
KVM: Move x86's perf guest info callbacks to generic KVM
KVM: x86: Move Intel Processor Trace interrupt handler to vmx.c
KVM: arm64: Convert to the generic perf callbacks
KVM: arm64: Hide kvm_arm_pmu_available behind CONFIG_HW_PERF_EVENTS=y
KVM: arm64: Drop perf.c and fold its tiny bits of code into arm.c
perf: Drop guest callback (un)register stubs
arch/arm/kernel/perf_callchain.c | 28 ++------------ arch/arm64/include/asm/kvm_host.h | 11 +++++- arch/arm64/kernel/image-vars.h | 2 + arch/arm64/kernel/perf_callchain.c | 13 ++++--- arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arm.c | 10 ++++- arch/arm64/kvm/perf.c | 59 ------------------------------ arch/arm64/kvm/pmu-emul.c | 2 + arch/csky/kernel/perf_callchain.c | 10 ----- arch/nds32/kernel/perf_event_cpu.c | 29 ++------------- arch/riscv/kernel/perf_callchain.c | 10 ----- arch/x86/events/core.c | 13 ++++--- arch/x86/events/intel/core.c | 5 +-- arch/x86/include/asm/kvm_host.h | 7 +++- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/pmu.c | 2 +- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 25 ++++++++++++- arch/x86/kvm/x86.c | 58 +++++------------------------ arch/x86/kvm/x86.h | 17 +++++++-- arch/x86/xen/Kconfig | 1 + arch/x86/xen/pmu.c | 32 +++++++--------- include/kvm/arm_pmu.h | 19 ++++++---- include/linux/kvm_host.h | 10 +++++ include/linux/perf_event.h | 44 ++++++++++++++++------ init/Kconfig | 4 ++ kernel/events/core.c | 41 +++++++++++++++------ virt/kvm/kvm_main.c | 44 ++++++++++++++++++++++ 29 files changed, 246 insertions(+), 256 deletions(-) delete mode 100644 arch/arm64/kvm/perf.c
Signature · 1 line
2.34.0.rc0.344.g81b53c2807-goog
Sean Christopherson
maintainerPATCH v4 1/17 perf: Protect perf_guest_cbs with RCU
Protect perf_guest_cbs with RCU to fix multiple possible errors. Luckily, all paths that read perf_guest_cbs already require RCU protection, e.g. to protect the callback chains, so only the direct perf_guest_cbs touchpoints need to be modified.
Bug #1 is a simple lack of WRITE_ONCE/READ_ONCE behavior to ensure perf_guest_cbs isn't reloaded between a !NULL check and a dereference. Fixed via the READ_ONCE() in rcu_dereference().
Bug #2 is that on weakly-ordered architectures, updates to the callbacks themselves are not guaranteed to be visible before the pointer is made visible to readers. Fixed by the smp_store_release() in rcu_assign_pointer() when the new pointer is non-NULL.
Bug #3 is that, because the callbacks are global, it's possible for readers to run in parallel with an unregisters, and thus a module implementing the callbacks can be unloaded while readers are in flight, resulting in a use-after-free. Fixed by a synchronize_rcu() call when unregistering callbacks.
Bug #1 escaped notice because it's extremely unlikely a compiler will reload perf_guest_cbs in this sequence. perf_guest_cbs does get reloaded for future derefs, e.g. for ->is_user_mode(), but the ->is_in_guest() guard all but guarantees the consumer will win the race, e.g. to nullify perf_guest_cbs, KVM has to completely exit the guest and teardown down all VMs before KVM start its module unload / unregister sequence. This also makes it all but impossible to encounter bug #3.
Bug #2 has not been a problem because all architectures that register callbacks are strongly ordered and/or have a static set of callbacks.
But with help, unloading kvm_intel can trigger bug #1 e.g. wrapping perf_guest_cbs with READ_ONCE in perf_misc_flags() while spamming kvm_intel module load/unload leads to:
BUG: kernel NULL pointer dereference, address: 0000000000000000 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page PGD 0 P4D 0 Oops: 0000 [#1] PREEMPT SMP CPU: 6 PID: 1825 Comm: stress Not tainted 5.14.0-rc2+ #459 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 RIP: 0010:perf_misc_flags+0x1c/0x70 Call Trace: perf_prepare_sample+0x53/0x6b0 perf_event_output_forward+0x67/0x160 __perf_event_overflow+0x52/0xf0 handle_pmi_common+0x207/0x300 intel_pmu_handle_irq+0xcf/0x410 perf_event_nmi_handler+0x28/0x50 nmi_handle+0xc7/0x260 default_do_nmi+0x6b/0x170 exc_nmi+0x103/0x130 asm_exc_nmi+0x76/0xbf
--- arch/arm/kernel/perf_callchain.c | 17 +++++++++++------ arch/arm64/kernel/perf_callchain.c | 18 ++++++++++++------ arch/csky/kernel/perf_callchain.c | 6 ++++-- arch/nds32/kernel/perf_event_cpu.c | 17 +++++++++++------ arch/riscv/kernel/perf_callchain.c | 7 +++++-- arch/x86/events/core.c | 17 +++++++++++------ arch/x86/events/intel/core.c | 9 ++++++--- include/linux/perf_event.h | 13 ++++++++++++- kernel/events/core.c | 13 ++++++++++--- 9 files changed, 82 insertions(+), 35 deletions(-)
arch/arm/kernel/perf_callchain.c +11 -6
@@ -62,9 +62,10 @@ user_backtrace(struct frame_tail __user *tail,void perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct frame_tail __user *tail; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->is_in_guest ()) {/* We don't support guest os callchain now */ return ; }
@@ -98,9 +99,10 @@ callchain_trace(struct stackframe *fr,void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct stackframe fr; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->is_in_guest ()) {/* We don't support guest os callchain now */ return ; }
@@ -111,18 +113,21 @@ perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *reunsigned long perf_instruction_pointer (struct pt_regs *regs) { -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) -return perf_guest_cbs->get_guest_ip (); +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); + +if (guest_cbs && guest_cbs->is_in_guest ()) +return guest_cbs->get_guest_ip ();return instruction_pointer (regs); }unsigned long perf_misc_flags (struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();int misc =0 ; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { -if (perf_guest_cbs->is_user_mode ()) +if (guest_cbs && guest_cbs->is_in_guest ()) { +if (guest_cbs->is_user_mode ()) misc |=PERF_RECORD_MISC_GUEST_USER ;else misc |=PERF_RECORD_MISC_GUEST_KERNEL ;
arch/arm64/kernel/perf_callchain.c +12 -6
@@ -102,7 +102,9 @@ compat_user_backtrace(struct compat_frame_tail __user *tail,void perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); + +if (guest_cbs && guest_cbs->is_in_guest ()) {/* We don't support guest os callchain now */ return ; }
@@ -147,9 +149,10 @@ static bool callchain_trace(void *data, unsigned long pc)void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct stackframe frame; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->is_in_guest ()) {/* We don't support guest os callchain now */ return ; }
@@ -160,18 +163,21 @@ void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,unsigned long perf_instruction_pointer (struct pt_regs *regs) { -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) -return perf_guest_cbs->get_guest_ip (); +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); + +if (guest_cbs && guest_cbs->is_in_guest ()) +return guest_cbs->get_guest_ip ();return instruction_pointer (regs); }unsigned long perf_misc_flags (struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();int misc =0 ; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { -if (perf_guest_cbs->is_user_mode ()) +if (guest_cbs && guest_cbs->is_in_guest ()) { +if (guest_cbs->is_user_mode ()) misc |=PERF_RECORD_MISC_GUEST_USER ;else misc |=PERF_RECORD_MISC_GUEST_KERNEL ;
arch/csky/kernel/perf_callchain.c +4 -2
@@ -86,10 +86,11 @@ static unsigned long user_backtrace(struct perf_callchain_entry_ctx *entry,void perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();unsigned long fp =0 ;/* C-SKY does not support virtualization. */ -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) +if (guest_cbs && guest_cbs->is_in_guest ())return ; fp = regs->regs[4 ];
@@ -110,10 +111,11 @@ void perf_callchain_user(struct perf_callchain_entry_ctx *entry,void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct stackframe fr;/* C-SKY does not support virtualization. */ -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->is_in_guest ()) {pr_warn ("C-SKY does not support perf in guest mode!" );return ; }
arch/nds32/kernel/perf_event_cpu.c +11 -6
@@ -1363,6 +1363,7 @@ void perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) { + struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();unsigned long fp =0 ;unsigned long gp =0 ;unsigned long lp =0 ;
@@ -1371,7 +1372,7 @@ perf_callchain_user(struct perf_callchain_entry_ctx *entry, leaf_fp =0 ; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->is_in_guest ()) {/* We don't support guest os callchain now */ return ; }
@@ -1479,9 +1480,10 @@ void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) { + struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();struct stackframe fr; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->is_in_guest ()) {/* We don't support guest os callchain now */ return ; }
@@ -1493,20 +1495,23 @@ perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,unsigned long perf_instruction_pointer (struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); +/* However, NDS32 does not support virtualization */ -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) -return perf_guest_cbs->get_guest_ip (); +if (guest_cbs && guest_cbs->is_in_guest ()) +return guest_cbs->get_guest_ip ();return instruction_pointer (regs); }unsigned long perf_misc_flags (struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();int misc =0 ;/* However, NDS32 does not support virtualization */ -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { -if (perf_guest_cbs->is_user_mode ()) +if (guest_cbs && guest_cbs->is_in_guest ()) { +if (guest_cbs->is_user_mode ()) misc |=PERF_RECORD_MISC_GUEST_USER ;else misc |=PERF_RECORD_MISC_GUEST_KERNEL ;
arch/riscv/kernel/perf_callchain.c +5 -2
@@ -56,10 +56,11 @@ static unsigned long user_backtrace(struct perf_callchain_entry_ctx *entry,void perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();unsigned long fp =0 ;/* RISC-V does not support perf in guest mode. */ -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) +if (guest_cbs && guest_cbs->is_in_guest ())return ; fp = regs->s0;
@@ -78,8 +79,10 @@ static bool fill_callchain(void *entry, unsigned long pc)void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); +/* RISC-V does not support perf in guest mode. */ -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->is_in_guest ()) {pr_warn ("RISC-V does not support perf in guest mode!" );return ; }
arch/x86/events/core.c +11 -6
@@ -2768,10 +2768,11 @@ static bool perf_hw_regs(struct pt_regs *regs)void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct unwind_state state;unsigned long addr; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->is_in_guest ()) {/* TODO: We don't support guest os callchain now */ return ; }
@@ -2871,10 +2872,11 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry_ctx *entvoid perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct stack_frame frame;const struct stack_frame __user *fp; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->is_in_guest ()) {/* TODO: We don't support guest os callchain now */ return ; }
@@ -2951,18 +2953,21 @@ static unsigned long code_segment_base(struct pt_regs *regs)unsigned long perf_instruction_pointer (struct pt_regs *regs) { -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) -return perf_guest_cbs->get_guest_ip (); +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); + +if (guest_cbs && guest_cbs->is_in_guest ()) +return guest_cbs->get_guest_ip ();return regs->ip +code_segment_base (regs); }unsigned long perf_misc_flags (struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();int misc =0 ; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { -if (perf_guest_cbs->is_user_mode ()) +if (guest_cbs && guest_cbs->is_in_guest ()) { +if (guest_cbs->is_user_mode ()) misc |=PERF_RECORD_MISC_GUEST_USER ;else misc |=PERF_RECORD_MISC_GUEST_KERNEL ;
arch/x86/events/intel/core.c +6 -3
@@ -2837,6 +2837,7 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status) {struct perf_sample_data data;struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events); +struct perf_guest_info_callbacks *guest_cbs;int bit;int handled =0 ;u64 intel_ctrl =hybrid (cpuc->pmu, intel_ctrl);
@@ -2903,9 +2904,11 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status)*/ if (__test_and_clear_bit (GLOBAL_STATUS_TRACE_TOPAPMI_BIT , (unsigned long *)&status)) { handled++; -if (unlikely (perf_guest_cbs && perf_guest_cbs->is_in_guest () && - perf_guest_cbs->handle_intel_pt_intr)) - perf_guest_cbs->handle_intel_pt_intr (); + + guest_cbs =perf_get_guest_cbs (); +if (unlikely (guest_cbs && guest_cbs->is_in_guest () && + guest_cbs->handle_intel_pt_intr)) + guest_cbs->handle_intel_pt_intr ();else intel_pt_interrupt (); }
include/linux/perf_event.h +12 -1
@@ -1240,7 +1240,18 @@ extern void perf_event_bpf_event(struct bpf_prog *prog,enum perf_bpf_event_type type, u16 flags); -extern struct perf_guest_info_callbacks *perf_guest_cbs; +extern struct perf_guest_info_callbacks __rcu *perf_guest_cbs; +static inline struct perf_guest_info_callbacks *perf_get_guest_cbs (void ) +{ +/* +* Callbacks are RCU-protected and must be READ_ONCE to avoid reloading +* the callbacks between a !NULL check and dereferences, to ensure +* pending stores/changes to the callback pointers are visible before a +* non-NULL perf_guest_cbs is visible to readers, and to prevent a +* module from unloading callbacks while readers are active. +*/ +return rcu_dereference (perf_guest_cbs); +}extern int perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *callbacks);extern int perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *callbacks);
kernel/events/core.c +10 -3
@@ -6526,18 +6526,25 @@ static void perf_pending_event(struct irq_work *entry)* Later on, we might change it to a list if there is * another virtualization implementation supporting the callbacks. */ -struct perf_guest_info_callbacks *perf_guest_cbs; +struct perf_guest_info_callbacks __rcu *perf_guest_cbs;int perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs) { - perf_guest_cbs = cbs; +if (WARN_ON_ONCE (rcu_access_pointer (perf_guest_cbs))) +return -EBUSY ; + +rcu_assign_pointer (perf_guest_cbs, cbs);return 0 ; }EXPORT_SYMBOL_GPL (perf_register_guest_info_callbacks);int perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *cbs) { - perf_guest_cbs =NULL ; +if (WARN_ON_ONCE (rcu_access_pointer (perf_guest_cbs) != cbs)) +return -EINVAL ; + +rcu_assign_pointer (perf_guest_cbs,NULL ); +synchronize_rcu ();return 0 ; }EXPORT_SYMBOL_GPL (perf_unregister_guest_info_callbacks);
Signature · 1 line
2.34.0.rc0.344.g81b53c2807-goog
Re: [PATCH v4 01/17] perf: Protect perf_guest_cbs with RCU
Quoted 55 lines · Sean Christopherson
Protect
perf_guest_cbswith RCU to fix multiple possible errors. Luckily, all paths that readperf_guest_cbsalready require RCU protection, e.g. to protect the callback chains, so only the directperf_guest_cbstouchpoints need to be modified.Bug #1 is a simple lack of WRITE_ONCE/READ_ONCE behavior to ensure
perf_guest_cbsisn't reloaded between a !NULL check and a dereference. Fixed via theREAD_ONCE()inrcu_dereference().Bug #2 is that on weakly-ordered architectures, updates to the callbacks themselves are not guaranteed to be visible before the pointer is made visible to readers. Fixed by the
smp_store_release()inrcu_assign_pointer()when the new pointer is non-NULL.Bug #3 is that, because the callbacks are global, it's possible for readers to run in parallel with an unregisters, and thus a module implementing the callbacks can be unloaded while readers are in flight, resulting in a use-after-free. Fixed by a
synchronize_rcu()call when unregistering callbacks.Bug #1 escaped notice because it's extremely unlikely a compiler will reload
perf_guest_cbsin this sequence.perf_guest_cbsdoes get reloaded for future derefs, e.g. for ->is_user_mode(), but the ->is_in_guest() guard all but guarantees the consumer will win the race, e.g. to nullifyperf_guest_cbs, KVM has to completely exit the guest and teardown down all VMs before KVM start its module unload / unregister sequence. This also makes it all but impossible to encounter bug #3.Bug #2 has not been a problem because all architectures that register callbacks are strongly ordered and/or have a static set of callbacks.
But with help, unloading
kvm_intelcan trigger bug #1 e.g. wrappingperf_guest_cbswithREAD_ONCEinperf_misc_flags()while spammingkvm_intelmodule load/unload leads to:BUG: kernel NULL pointer dereference, address: 0000000000000000 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page PGD 0 P4D 0 Oops: 0000 [#1] PREEMPT SMP CPU: 6 PID: 1825 Comm: stress Not tainted 5.14.0-rc2+ #459 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 RIP: 0010:perf_misc_flags+0x1c/0x70 Call Trace: perf_prepare_sample+0x53/0x6b0 perf_event_output_forward+0x67/0x160 __perf_event_overflow+0x52/0xf0 handle_pmi_common+0x207/0x300 intel_pmu_handle_irq+0xcf/0x410 perf_event_nmi_handler+0x28/0x50 nmi_handle+0xc7/0x260 default_do_nmi+0x6b/0x170 exc_nmi+0x103/0x130 asm_exc_nmi+0x76/0xbf
Quoted 4 lines
Fixes 39447b386c84 ("perf: Enhance perf to allow for guest statistic collection from host")Signed-off-by Sean Christopherson <[email protected]>---
One nit:
kernel/events/core.c static void perf_pending_event(struct irq_work *entry) · line 6547
Quoted 10 lines
EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) {kernel/events/core.c +5
-1- perf_guest_cbs =NULL ; +if (WARN_ON_ONCE (rcu_access_pointer (perf_guest_cbs) != cbs)) +return -EINVAL ; + +rcu_assign_pointer (perf_guest_cbs,NULL ); +synchronize_rcu ();
This technically could be RCU_INIT_POINTER but it's not worth a respin. There are dozens of other occurrences, and if somebody wanted they could use Coccinelle to fix all of them.
Paolo
Peter Zijlstra
maintainerRe: [PATCH v4 01/17] perf: Protect perf_guest_cbs with RCU
Quoted 14 lines · Paolo Bonzini
kernel/events/core.c static void perf_pending_event(struct irq_work *entry) · line 6547
Quoted 9 lines · Sean Christopherson
EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) {kernel/events/core.c +5
-1- perf_guest_cbs =NULL ; +if (WARN_ON_ONCE (rcu_access_pointer (perf_guest_cbs) != cbs)) +return -EINVAL ; + +rcu_assign_pointer (perf_guest_cbs,NULL ); +synchronize_rcu ();This technically could be
RCU_INIT_POINTERbut it's not worth a respin. There are dozens of other occurrences, and if somebody wanted they could use Coccinelle to fix all of them.
I've been pushing the other way, trying to get rid of RCU_INIT_POINTER() since rcu_assign_pointer(, NULL) actualy DTRT per __builtin_constant_p() etc.
There's a very few sites where we use RCU_INIT_POINTER() with a !NULL argument, and those are 'special'.
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Paolo Bonzini
maintainerRe: [PATCH v4 01/17] perf: Protect perf_guest_cbs with RCU
Quoted 6 lines · Peter Zijlstra
Quoted 3 lines
This technically could be
RCU_INIT_POINTERbut it's not worth a respin. There are dozens of other occurrences, and if somebody wanted they could use Coccinelle to fix all of them.I've been pushing the other way, trying to get rid of
RCU_INIT_POINTER()sincercu_assign_pointer(, NULL)actualy DTRT per__builtin_constant_p()etc.
Oh, that's pretty cool to know, thanks!
Paolo
Quoted 3 lines
There's a very few sites where we use
RCU_INIT_POINTER()with a !NULL argument, and those are 'special'.
[tip: perf/core] perf: Protect perf_guest_cbs with RCU
In reply to Sean ChristophersonThe following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: ff083a2d972f56bebfd82409ca62e5dfce950961 Gitweb: https://git.kernel.org/tip/ff083a2d972f56bebfd82409ca62e5dfce950961 Author: Sean Christopherson <[email protected]> AuthorDate: Thu, 11 Nov 2021 02:07:22 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Nov 2021 14:49:06 +01:00
perf: Protect perf_guest_cbs with RCU
Protect perf_guest_cbs with RCU to fix multiple possible errors. Luckily, all paths that read perf_guest_cbs already require RCU protection, e.g. to protect the callback chains, so only the direct perf_guest_cbs touchpoints need to be modified.
Bug #1 is a simple lack of WRITE_ONCE/READ_ONCE behavior to ensure perf_guest_cbs isn't reloaded between a !NULL check and a dereference. Fixed via the READ_ONCE() in rcu_dereference().
Bug #2 is that on weakly-ordered architectures, updates to the callbacks themselves are not guaranteed to be visible before the pointer is made visible to readers. Fixed by the smp_store_release() in rcu_assign_pointer() when the new pointer is non-NULL.
Bug #3 is that, because the callbacks are global, it's possible for readers to run in parallel with an unregisters, and thus a module implementing the callbacks can be unloaded while readers are in flight, resulting in a use-after-free. Fixed by a synchronize_rcu() call when unregistering callbacks.
Bug #1 escaped notice because it's extremely unlikely a compiler will reload perf_guest_cbs in this sequence. perf_guest_cbs does get reloaded for future derefs, e.g. for ->is_user_mode(), but the ->is_in_guest() guard all but guarantees the consumer will win the race, e.g. to nullify perf_guest_cbs, KVM has to completely exit the guest and teardown down all VMs before KVM start its module unload / unregister sequence. This also makes it all but impossible to encounter bug #3.
Bug #2 has not been a problem because all architectures that register callbacks are strongly ordered and/or have a static set of callbacks.
But with help, unloading kvm_intel can trigger bug #1 e.g. wrapping perf_guest_cbs with READ_ONCE in perf_misc_flags() while spamming kvm_intel module load/unload leads to:
BUG: kernel NULL pointer dereference, address: 0000000000000000 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page PGD 0 P4D 0 Oops: 0000 [#1] PREEMPT SMP CPU: 6 PID: 1825 Comm: stress Not tainted 5.14.0-rc2+ #459 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 RIP: 0010:perf_misc_flags+0x1c/0x70 Call Trace: perf_prepare_sample+0x53/0x6b0 perf_event_output_forward+0x67/0x160 __perf_event_overflow+0x52/0xf0 handle_pmi_common+0x207/0x300 intel_pmu_handle_irq+0xcf/0x410 perf_event_nmi_handler+0x28/0x50 nmi_handle+0xc7/0x260 default_do_nmi+0x6b/0x170 exc_nmi+0x103/0x130 asm_exc_nmi+0x76/0xbf
--- arch/arm/kernel/perf_callchain.c | 17 +++++++++++------ arch/arm64/kernel/perf_callchain.c | 18 ++++++++++++------ arch/csky/kernel/perf_callchain.c | 6 ++++-- arch/nds32/kernel/perf_event_cpu.c | 17 +++++++++++------ arch/riscv/kernel/perf_callchain.c | 7 +++++-- arch/x86/events/core.c | 17 +++++++++++------ arch/x86/events/intel/core.c | 9 ++++++--- include/linux/perf_event.h | 13 ++++++++++++- kernel/events/core.c | 13 ++++++++++--- 9 files changed, 82 insertions(+), 35 deletions(-)
arch/arm/kernel/perf_callchain.c +11 -6
@@ -62,9 +62,10 @@ user_backtrace(struct frame_tail __user *tail,void perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct frame_tail __user *tail; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->is_in_guest ()) {/* We don't support guest os callchain now */ return ; }
@@ -98,9 +99,10 @@ callchain_trace(struct stackframe *fr,void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct stackframe fr; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->is_in_guest ()) {/* We don't support guest os callchain now */ return ; }
@@ -111,18 +113,21 @@ perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *reunsigned long perf_instruction_pointer (struct pt_regs *regs) { -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) -return perf_guest_cbs->get_guest_ip (); +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); + +if (guest_cbs && guest_cbs->is_in_guest ()) +return guest_cbs->get_guest_ip ();return instruction_pointer (regs); }unsigned long perf_misc_flags (struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();int misc =0 ; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { -if (perf_guest_cbs->is_user_mode ()) +if (guest_cbs && guest_cbs->is_in_guest ()) { +if (guest_cbs->is_user_mode ()) misc |=PERF_RECORD_MISC_GUEST_USER ;else misc |=PERF_RECORD_MISC_GUEST_KERNEL ;
arch/arm64/kernel/perf_callchain.c +12 -6
@@ -102,7 +102,9 @@ compat_user_backtrace(struct compat_frame_tail __user *tail,void perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); + +if (guest_cbs && guest_cbs->is_in_guest ()) {/* We don't support guest os callchain now */ return ; }
@@ -147,9 +149,10 @@ static bool callchain_trace(void *data, unsigned long pc)void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct stackframe frame; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->is_in_guest ()) {/* We don't support guest os callchain now */ return ; }
@@ -160,18 +163,21 @@ void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,unsigned long perf_instruction_pointer (struct pt_regs *regs) { -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) -return perf_guest_cbs->get_guest_ip (); +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); + +if (guest_cbs && guest_cbs->is_in_guest ()) +return guest_cbs->get_guest_ip ();return instruction_pointer (regs); }unsigned long perf_misc_flags (struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();int misc =0 ; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { -if (perf_guest_cbs->is_user_mode ()) +if (guest_cbs && guest_cbs->is_in_guest ()) { +if (guest_cbs->is_user_mode ()) misc |=PERF_RECORD_MISC_GUEST_USER ;else misc |=PERF_RECORD_MISC_GUEST_KERNEL ;
arch/csky/kernel/perf_callchain.c +4 -2
@@ -86,10 +86,11 @@ static unsigned long user_backtrace(struct perf_callchain_entry_ctx *entry,void perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();unsigned long fp =0 ;/* C-SKY does not support virtualization. */ -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) +if (guest_cbs && guest_cbs->is_in_guest ())return ; fp = regs->regs[4 ];
@@ -110,10 +111,11 @@ void perf_callchain_user(struct perf_callchain_entry_ctx *entry,void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct stackframe fr;/* C-SKY does not support virtualization. */ -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->is_in_guest ()) {pr_warn ("C-SKY does not support perf in guest mode!" );return ; }
arch/nds32/kernel/perf_event_cpu.c +11 -6
@@ -1363,6 +1363,7 @@ void perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) { + struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();unsigned long fp =0 ;unsigned long gp =0 ;unsigned long lp =0 ;
@@ -1371,7 +1372,7 @@ perf_callchain_user(struct perf_callchain_entry_ctx *entry, leaf_fp =0 ; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->is_in_guest ()) {/* We don't support guest os callchain now */ return ; }
@@ -1479,9 +1480,10 @@ void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) { + struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();struct stackframe fr; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->is_in_guest ()) {/* We don't support guest os callchain now */ return ; }
@@ -1493,20 +1495,23 @@ perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,unsigned long perf_instruction_pointer (struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); +/* However, NDS32 does not support virtualization */ -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) -return perf_guest_cbs->get_guest_ip (); +if (guest_cbs && guest_cbs->is_in_guest ()) +return guest_cbs->get_guest_ip ();return instruction_pointer (regs); }unsigned long perf_misc_flags (struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();int misc =0 ;/* However, NDS32 does not support virtualization */ -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { -if (perf_guest_cbs->is_user_mode ()) +if (guest_cbs && guest_cbs->is_in_guest ()) { +if (guest_cbs->is_user_mode ()) misc |=PERF_RECORD_MISC_GUEST_USER ;else misc |=PERF_RECORD_MISC_GUEST_KERNEL ;
arch/riscv/kernel/perf_callchain.c +5 -2
@@ -56,10 +56,11 @@ static unsigned long user_backtrace(struct perf_callchain_entry_ctx *entry,void perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();unsigned long fp =0 ;/* RISC-V does not support perf in guest mode. */ -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) +if (guest_cbs && guest_cbs->is_in_guest ())return ; fp = regs->s0;
@@ -78,8 +79,10 @@ static bool fill_callchain(void *entry, unsigned long pc)void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); +/* RISC-V does not support perf in guest mode. */ -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->is_in_guest ()) {pr_warn ("RISC-V does not support perf in guest mode!" );return ; }
arch/x86/events/core.c +11 -6
@@ -2768,10 +2768,11 @@ static bool perf_hw_regs(struct pt_regs *regs)void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct unwind_state state;unsigned long addr; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->is_in_guest ()) {/* TODO: We don't support guest os callchain now */ return ; }
@@ -2871,10 +2872,11 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry_ctx *entvoid perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct stack_frame frame;const struct stack_frame __user *fp; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->is_in_guest ()) {/* TODO: We don't support guest os callchain now */ return ; }
@@ -2951,18 +2953,21 @@ static unsigned long code_segment_base(struct pt_regs *regs)unsigned long perf_instruction_pointer (struct pt_regs *regs) { -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) -return perf_guest_cbs->get_guest_ip (); +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); + +if (guest_cbs && guest_cbs->is_in_guest ()) +return guest_cbs->get_guest_ip ();return regs->ip +code_segment_base (regs); }unsigned long perf_misc_flags (struct pt_regs *regs) { +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();int misc =0 ; -if (perf_guest_cbs && perf_guest_cbs->is_in_guest ()) { -if (perf_guest_cbs->is_user_mode ()) +if (guest_cbs && guest_cbs->is_in_guest ()) { +if (guest_cbs->is_user_mode ()) misc |=PERF_RECORD_MISC_GUEST_USER ;else misc |=PERF_RECORD_MISC_GUEST_KERNEL ;
arch/x86/events/intel/core.c +6 -3
@@ -2837,6 +2837,7 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status) {struct perf_sample_data data;struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events); +struct perf_guest_info_callbacks *guest_cbs;int bit;int handled =0 ;u64 intel_ctrl =hybrid (cpuc->pmu, intel_ctrl);
@@ -2903,9 +2904,11 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status)*/ if (__test_and_clear_bit (GLOBAL_STATUS_TRACE_TOPAPMI_BIT , (unsigned long *)&status)) { handled++; -if (unlikely (perf_guest_cbs && perf_guest_cbs->is_in_guest () && - perf_guest_cbs->handle_intel_pt_intr)) - perf_guest_cbs->handle_intel_pt_intr (); + + guest_cbs =perf_get_guest_cbs (); +if (unlikely (guest_cbs && guest_cbs->is_in_guest () && + guest_cbs->handle_intel_pt_intr)) + guest_cbs->handle_intel_pt_intr ();else intel_pt_interrupt (); }
include/linux/perf_event.h +12 -1
@@ -1240,7 +1240,18 @@ extern void perf_event_bpf_event(struct bpf_prog *prog,enum perf_bpf_event_type type, u16 flags); -extern struct perf_guest_info_callbacks *perf_guest_cbs; +extern struct perf_guest_info_callbacks __rcu *perf_guest_cbs; +static inline struct perf_guest_info_callbacks *perf_get_guest_cbs (void ) +{ +/* +* Callbacks are RCU-protected and must be READ_ONCE to avoid reloading +* the callbacks between a !NULL check and dereferences, to ensure +* pending stores/changes to the callback pointers are visible before a +* non-NULL perf_guest_cbs is visible to readers, and to prevent a +* module from unloading callbacks while readers are active. +*/ +return rcu_dereference (perf_guest_cbs); +}extern int perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *callbacks);extern int perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *callbacks);
kernel/events/core.c +10 -3
@@ -6526,18 +6526,25 @@ static void perf_pending_event(struct irq_work *entry)* Later on, we might change it to a list if there is * another virtualization implementation supporting the callbacks. */ -struct perf_guest_info_callbacks *perf_guest_cbs; +struct perf_guest_info_callbacks __rcu *perf_guest_cbs;int perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs) { - perf_guest_cbs = cbs; +if (WARN_ON_ONCE (rcu_access_pointer (perf_guest_cbs))) +return -EBUSY ; + +rcu_assign_pointer (perf_guest_cbs, cbs);return 0 ; }EXPORT_SYMBOL_GPL (perf_register_guest_info_callbacks);int perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *cbs) { - perf_guest_cbs =NULL ; +if (WARN_ON_ONCE (rcu_access_pointer (perf_guest_cbs) != cbs)) +return -EINVAL ; + +rcu_assign_pointer (perf_guest_cbs,NULL ); +synchronize_rcu ();return 0 ; }EXPORT_SYMBOL_GPL (perf_unregister_guest_info_callbacks);
Sean Christopherson
maintainerPATCH v4 2/17 KVM: x86: Register perf callbacks after calling vendor's hardware_setup()
In reply to Sean ChristophersonWait to register perf callbacks until after doing vendor hardaware setup. VMX's hardware_setup() configures Intel Processor Trace (PT) mode, and a future fix to register the Intel PT guest interrupt hook if and only if Intel PT is exposed to the guest will consume the configured PT mode.
Delaying registration to hardware setup is effectively a nop as KVM's perf hooks all pivot on the per-CPU current_vcpu, which is non-NULL only when KVM is handling an IRQ/NMI in a VM-Exit path. I.e. current_vcpu will be NULL throughout both kvm_arch_init() and kvm_arch_hardware_setup().
--- arch/x86/kvm/x86.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +4 -3
@@ -8567,8 +8567,6 @@ int kvm_arch_init(void *opaque)kvm_timer_init (); -perf_register_guest_info_callbacks (&kvm_guest_cbs); -if (boot_cpu_has (X86_FEATURE_XSAVE )) { host_xcr0 =xgetbv (XCR_XFEATURE_ENABLED_MASK ); supported_xcr0 = host_xcr0 &KVM_SUPPORTED_XCR0 ;
@@ -8600,7 +8598,6 @@ void kvm_arch_exit(void)clear_hv_tscchange_cb ();#endif kvm_lapic_exit (); -perf_unregister_guest_info_callbacks (&kvm_guest_cbs);if (!boot_cpu_has (X86_FEATURE_CONSTANT_TSC )) cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
@@ -11149,6 +11146,8 @@ int kvm_arch_hardware_setup(void *opaque)memcpy (&kvm_x86_ops, ops->runtime_ops,sizeof (kvm_x86_ops));kvm_ops_static_call_update (); +perf_register_guest_info_callbacks (&kvm_guest_cbs); +if (!kvm_cpu_cap_has (X86_FEATURE_XSAVES )) supported_xss =0 ;
@@ -11176,6 +11175,8 @@ int kvm_arch_hardware_setup(void *opaque)void kvm_arch_hardware_unsetup (void ) { +perf_unregister_guest_info_callbacks (&kvm_guest_cbs); +static_call (kvm_x86_hardware_unsetup)(); }
Signature · 1 line
2.34.0.rc0.344.g81b53c2807-goog
[tip: perf/core] KVM: x86: Register perf callbacks after calling vendor's hardware_setup()
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 5c7df80e2ce4c954c80eb4ecf5fa002a5ff5d2d6 Gitweb: https://git.kernel.org/tip/5c7df80e2ce4c954c80eb4ecf5fa002a5ff5d2d6 Author: Sean Christopherson <[email protected]> AuthorDate: Thu, 11 Nov 2021 02:07:23 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Nov 2021 14:49:06 +01:00
KVM: x86: Register perf callbacks after calling vendor's hardware_setup()
Wait to register perf callbacks until after doing vendor hardaware setup. VMX's hardware_setup() configures Intel Processor Trace (PT) mode, and a future fix to register the Intel PT guest interrupt hook if and only if Intel PT is exposed to the guest will consume the configured PT mode.
Delaying registration to hardware setup is effectively a nop as KVM's perf hooks all pivot on the per-CPU current_vcpu, which is non-NULL only when KVM is handling an IRQ/NMI in a VM-Exit path. I.e. current_vcpu will be NULL throughout both kvm_arch_init() and kvm_arch_hardware_setup().
--- arch/x86/kvm/x86.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +4 -3
@@ -8626,8 +8626,6 @@ int kvm_arch_init(void *opaque)kvm_timer_init (); -perf_register_guest_info_callbacks (&kvm_guest_cbs); -if (boot_cpu_has (X86_FEATURE_XSAVE )) { host_xcr0 =xgetbv (XCR_XFEATURE_ENABLED_MASK ); supported_xcr0 = host_xcr0 &KVM_SUPPORTED_XCR0 ;
@@ -8659,7 +8657,6 @@ void kvm_arch_exit(void)clear_hv_tscchange_cb ();#endif kvm_lapic_exit (); -perf_unregister_guest_info_callbacks (&kvm_guest_cbs);if (!boot_cpu_has (X86_FEATURE_CONSTANT_TSC )) cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
@@ -11225,6 +11222,8 @@ int kvm_arch_hardware_setup(void *opaque)memcpy (&kvm_x86_ops, ops->runtime_ops,sizeof (kvm_x86_ops));kvm_ops_static_call_update (); +perf_register_guest_info_callbacks (&kvm_guest_cbs); +if (!kvm_cpu_cap_has (X86_FEATURE_XSAVES )) supported_xss =0 ;
@@ -11252,6 +11251,8 @@ int kvm_arch_hardware_setup(void *opaque)void kvm_arch_hardware_unsetup (void ) { +perf_unregister_guest_info_callbacks (&kvm_guest_cbs); +static_call (kvm_x86_hardware_unsetup)(); }
Sean Christopherson
maintainerPATCH v4 3/17 KVM: x86: Register Processor Trace interrupt hook iff PT enabled in guest
In reply to Sean ChristophersonOverride the Processor Trace (PT) interrupt handler for guest mode if and only if PT is configured for host+guest mode, i.e. is being used independently by both host and guest. If PT is configured for system mode, the host fully controls PT and must handle all events.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx/vmx.c | 1 + arch/x86/kvm/x86.c | 5 ++++- 3 files changed, 6 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1514,6 +1514,7 @@ struct kvm_x86_init_ops {int (*disabled_by_bios)(void );int (*check_processor_compatibility)(void );int (*hardware_setup)(void ); +bool (*intel_pt_intr_in_guest)(void );struct kvm_x86_ops *runtime_ops; };
arch/x86/kvm/vmx/vmx.c +1 -0
@@ -7918,6 +7918,7 @@ static struct kvm_x86_init_ops vmx_init_ops __initdata = { .disabled_by_bios = vmx_disabled_by_bios, .check_processor_compatibility = vmx_check_processor_compat, .hardware_setup = hardware_setup, + .intel_pt_intr_in_guest = vmx_pt_mode_is_host_guest, .runtime_ops = &vmx_x86_ops, };
arch/x86/kvm/x86.c +4 -1
@@ -8451,7 +8451,7 @@ static struct perf_guest_info_callbacks kvm_guest_cbs = { .is_in_guest = kvm_is_in_guest, .is_user_mode = kvm_is_user_mode, .get_guest_ip = kvm_get_guest_ip, - .handle_intel_pt_intr = kvm_handle_intel_pt_intr, + .handle_intel_pt_intr = NULL, };#ifdef CONFIG_X86_64
@@ -11146,6 +11146,8 @@ int kvm_arch_hardware_setup(void *opaque)memcpy (&kvm_x86_ops, ops->runtime_ops,sizeof (kvm_x86_ops));kvm_ops_static_call_update (); +if (ops->intel_pt_intr_in_guest && ops->intel_pt_intr_in_guest ()) + kvm_guest_cbs.handle_intel_pt_intr = kvm_handle_intel_pt_intr;perf_register_guest_info_callbacks (&kvm_guest_cbs);if (!kvm_cpu_cap_has (X86_FEATURE_XSAVES ))
@@ -11176,6 +11178,7 @@ int kvm_arch_hardware_setup(void *opaque)void kvm_arch_hardware_unsetup (void ) {perf_unregister_guest_info_callbacks (&kvm_guest_cbs); + kvm_guest_cbs.handle_intel_pt_intr =NULL ;static_call (kvm_x86_hardware_unsetup)(); }
Signature · 6 lines
2.34.0.rc0.344.g81b53c2807-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
[tip: perf/core] KVM: x86: Register Processor Trace interrupt hook iff PT enabled in guest
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: f4b027c5c8199abd4fb6f00d67d380548dbfdfa8 Gitweb: https://git.kernel.org/tip/f4b027c5c8199abd4fb6f00d67d380548dbfdfa8 Author: Sean Christopherson <[email protected]> AuthorDate: Thu, 11 Nov 2021 02:07:24 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Nov 2021 14:49:06 +01:00
KVM: x86: Register Processor Trace interrupt hook iff PT enabled in guest
Override the Processor Trace (PT) interrupt handler for guest mode if and only if PT is configured for host+guest mode, i.e. is being used independently by both host and guest. If PT is configured for system mode, the host fully controls PT and must handle all events.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx/vmx.c | 1 + arch/x86/kvm/x86.c | 5 ++++- 3 files changed, 6 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1516,6 +1516,7 @@ struct kvm_x86_init_ops {int (*disabled_by_bios)(void );int (*check_processor_compatibility)(void );int (*hardware_setup)(void ); +bool (*intel_pt_intr_in_guest)(void );struct kvm_x86_ops *runtime_ops; };
arch/x86/kvm/vmx/vmx.c +1 -0
@@ -7865,6 +7865,7 @@ static struct kvm_x86_init_ops vmx_init_ops __initdata = { .disabled_by_bios = vmx_disabled_by_bios, .check_processor_compatibility = vmx_check_processor_compat, .hardware_setup = hardware_setup, + .intel_pt_intr_in_guest = vmx_pt_mode_is_host_guest, .runtime_ops = &vmx_x86_ops, };
arch/x86/kvm/x86.c +4 -1
@@ -8510,7 +8510,7 @@ static struct perf_guest_info_callbacks kvm_guest_cbs = { .is_in_guest = kvm_is_in_guest, .is_user_mode = kvm_is_user_mode, .get_guest_ip = kvm_get_guest_ip, - .handle_intel_pt_intr = kvm_handle_intel_pt_intr, + .handle_intel_pt_intr = NULL, };#ifdef CONFIG_X86_64
@@ -11222,6 +11222,8 @@ int kvm_arch_hardware_setup(void *opaque)memcpy (&kvm_x86_ops, ops->runtime_ops,sizeof (kvm_x86_ops));kvm_ops_static_call_update (); +if (ops->intel_pt_intr_in_guest && ops->intel_pt_intr_in_guest ()) + kvm_guest_cbs.handle_intel_pt_intr = kvm_handle_intel_pt_intr;perf_register_guest_info_callbacks (&kvm_guest_cbs);if (!kvm_cpu_cap_has (X86_FEATURE_XSAVES ))
@@ -11252,6 +11254,7 @@ int kvm_arch_hardware_setup(void *opaque)void kvm_arch_hardware_unsetup (void ) {perf_unregister_guest_info_callbacks (&kvm_guest_cbs); + kvm_guest_cbs.handle_intel_pt_intr =NULL ;static_call (kvm_x86_hardware_unsetup)(); }
Sean Christopherson
maintainerPATCH v4 4/17 perf: Stop pretending that perf can handle multiple guest callbacks
In reply to Sean ChristophersonDrop the 'int' return value from the perf (un)register callbacks helpers and stop pretending perf can support multiple callbacks. The 'int' returns are not future proofing anything as none of the callers take action on an error. It's also not obvious that there will ever be co-tenant hypervisors, and if there are, that allowing multiple callbacks to be registered is desirable or even correct.
Opportunistically rename callbacks=>cbs in the affected declarations to match their definitions.
No functional change intended.
--- arch/arm64/include/asm/kvm_host.h | 4 ++-- arch/arm64/kvm/perf.c | 8 ++++---- include/linux/perf_event.h | 12 ++++++------ kernel/events/core.c | 15 ++++----------- 4 files changed, 16 insertions(+), 23 deletions(-)
arch/arm64/include/asm/kvm_host.h +2 -2
@@ -675,8 +675,8 @@ unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len);int kvm_handle_mmio_return (struct kvm_vcpu *vcpu);int io_mem_abort (struct kvm_vcpu *vcpu,phys_addr_t fault_ipa); -int kvm_perf_init (void ); -int kvm_perf_teardown (void ); +void kvm_perf_init (void ); +void kvm_perf_teardown (void );long kvm_hypercall_pv_features (struct kvm_vcpu *vcpu);gpa_t kvm_init_stolen_time (struct kvm_vcpu *vcpu);
arch/arm64/kvm/perf.c +4 -4
@@ -48,12 +48,12 @@ static struct perf_guest_info_callbacks kvm_guest_cbs = { .get_guest_ip = kvm_get_guest_ip, }; -int kvm_perf_init (void ) +void kvm_perf_init (void ) { -return perf_register_guest_info_callbacks (&kvm_guest_cbs); +perf_register_guest_info_callbacks (&kvm_guest_cbs); } -int kvm_perf_teardown (void ) +void kvm_perf_teardown (void ) { -return perf_unregister_guest_info_callbacks (&kvm_guest_cbs); +perf_unregister_guest_info_callbacks (&kvm_guest_cbs); }
include/linux/perf_event.h +6 -6
@@ -1252,8 +1252,8 @@ static inline struct perf_guest_info_callbacks *perf_get_guest_cbs(void)*/ return rcu_dereference (perf_guest_cbs); } -extern int perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *callbacks); -extern int perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *callbacks); +extern void perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs); +extern void perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *cbs);extern void perf_event_exec (void );extern void perf_event_comm (struct task_struct *tsk,bool exec);
@@ -1497,10 +1497,10 @@ perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }static inline void perf_bp_event (struct perf_event *event,void *data) { } -static inline int perf_register_guest_info_callbacks -(struct perf_guest_info_callbacks *callbacks) {return 0 ; } -static inline int perf_unregister_guest_info_callbacks -(struct perf_guest_info_callbacks *callbacks) {return 0 ; } +static inline void perf_register_guest_info_callbacks +(struct perf_guest_info_callbacks *cbs) { } +static inline void perf_unregister_guest_info_callbacks +(struct perf_guest_info_callbacks *cbs) { }static inline void perf_event_mmap (struct vm_area_struct *vma) { }
kernel/events/core.c +4 -11
@@ -6521,31 +6521,24 @@ static void perf_pending_event(struct irq_work *entry)perf_swevent_put_recursion_context (rctx); } -/* -* We assume there is only KVM supporting the callbacks. -* Later on, we might change it to a list if there is -* another virtualization implementation supporting the callbacks. -*/ struct perf_guest_info_callbacks __rcu *perf_guest_cbs; -int perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs) +void perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs) {if (WARN_ON_ONCE (rcu_access_pointer (perf_guest_cbs))) -return -EBUSY ; +return ;rcu_assign_pointer (perf_guest_cbs, cbs); -return 0 ; }EXPORT_SYMBOL_GPL (perf_register_guest_info_callbacks); -int perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *cbs) +void perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *cbs) {if (WARN_ON_ONCE (rcu_access_pointer (perf_guest_cbs) != cbs)) -return -EINVAL ; +return ;rcu_assign_pointer (perf_guest_cbs,NULL );synchronize_rcu (); -return 0 ; }EXPORT_SYMBOL_GPL (perf_unregister_guest_info_callbacks);
Signature · 6 lines
2.34.0.rc0.344.g81b53c2807-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
[tip: perf/core] perf: Stop pretending that perf can handle multiple guest callbacks
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 2934e3d09350c1a7ca2433fbeabfcd831e48a575 Gitweb: https://git.kernel.org/tip/2934e3d09350c1a7ca2433fbeabfcd831e48a575 Author: Sean Christopherson <[email protected]> AuthorDate: Thu, 11 Nov 2021 02:07:25 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Nov 2021 14:49:07 +01:00
perf: Stop pretending that perf can handle multiple guest callbacks
Drop the 'int' return value from the perf (un)register callbacks helpers and stop pretending perf can support multiple callbacks. The 'int' returns are not future proofing anything as none of the callers take action on an error. It's also not obvious that there will ever be co-tenant hypervisors, and if there are, that allowing multiple callbacks to be registered is desirable or even correct.
Opportunistically rename callbacks=>cbs in the affected declarations to match their definitions.
No functional change intended.
--- arch/arm64/include/asm/kvm_host.h | 4 ++-- arch/arm64/kvm/perf.c | 8 ++++---- include/linux/perf_event.h | 12 ++++++------ kernel/events/core.c | 15 ++++----------- 4 files changed, 16 insertions(+), 23 deletions(-)
arch/arm64/include/asm/kvm_host.h +2 -2
@@ -675,8 +675,8 @@ unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len);int kvm_handle_mmio_return (struct kvm_vcpu *vcpu);int io_mem_abort (struct kvm_vcpu *vcpu,phys_addr_t fault_ipa); -int kvm_perf_init (void ); -int kvm_perf_teardown (void ); +void kvm_perf_init (void ); +void kvm_perf_teardown (void );long kvm_hypercall_pv_features (struct kvm_vcpu *vcpu);gpa_t kvm_init_stolen_time (struct kvm_vcpu *vcpu);
arch/arm64/kvm/perf.c +4 -4
@@ -48,12 +48,12 @@ static struct perf_guest_info_callbacks kvm_guest_cbs = { .get_guest_ip = kvm_get_guest_ip, }; -int kvm_perf_init (void ) +void kvm_perf_init (void ) { -return perf_register_guest_info_callbacks (&kvm_guest_cbs); +perf_register_guest_info_callbacks (&kvm_guest_cbs); } -int kvm_perf_teardown (void ) +void kvm_perf_teardown (void ) { -return perf_unregister_guest_info_callbacks (&kvm_guest_cbs); +perf_unregister_guest_info_callbacks (&kvm_guest_cbs); }
include/linux/perf_event.h +6 -6
@@ -1252,8 +1252,8 @@ static inline struct perf_guest_info_callbacks *perf_get_guest_cbs(void)*/ return rcu_dereference (perf_guest_cbs); } -extern int perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *callbacks); -extern int perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *callbacks); +extern void perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs); +extern void perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *cbs);extern void perf_event_exec (void );extern void perf_event_comm (struct task_struct *tsk,bool exec);
@@ -1497,10 +1497,10 @@ perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }static inline void perf_bp_event (struct perf_event *event,void *data) { } -static inline int perf_register_guest_info_callbacks -(struct perf_guest_info_callbacks *callbacks) {return 0 ; } -static inline int perf_unregister_guest_info_callbacks -(struct perf_guest_info_callbacks *callbacks) {return 0 ; } +static inline void perf_register_guest_info_callbacks +(struct perf_guest_info_callbacks *cbs) { } +static inline void perf_unregister_guest_info_callbacks +(struct perf_guest_info_callbacks *cbs) { }static inline void perf_event_mmap (struct vm_area_struct *vma) { }
kernel/events/core.c +4 -11
@@ -6521,31 +6521,24 @@ static void perf_pending_event(struct irq_work *entry)perf_swevent_put_recursion_context (rctx); } -/* -* We assume there is only KVM supporting the callbacks. -* Later on, we might change it to a list if there is -* another virtualization implementation supporting the callbacks. -*/ struct perf_guest_info_callbacks __rcu *perf_guest_cbs; -int perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs) +void perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs) {if (WARN_ON_ONCE (rcu_access_pointer (perf_guest_cbs))) -return -EBUSY ; +return ;rcu_assign_pointer (perf_guest_cbs, cbs); -return 0 ; }EXPORT_SYMBOL_GPL (perf_register_guest_info_callbacks); -int perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *cbs) +void perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *cbs) {if (WARN_ON_ONCE (rcu_access_pointer (perf_guest_cbs) != cbs)) -return -EINVAL ; +return ;rcu_assign_pointer (perf_guest_cbs,NULL );synchronize_rcu (); -return 0 ; }EXPORT_SYMBOL_GPL (perf_unregister_guest_info_callbacks);
Sean Christopherson
maintainerPATCH v4 5/17 perf: Drop dead and useless guest "support" from arm, csky, nds32 and riscv
In reply to Sean ChristophersonDrop "support" for guest callbacks from architectures that don't implement the guest callbacks. Future patches will convert the callbacks to static_call; rather than churn a bunch of arch code (that was presumably copy+pasted from x86), remove it wholesale as it's useless and at best wasting cycles.
A future patch will also add a Kconfig to force architcture to opt into the callbacks to make it more difficult for uses "support" to sneak in in the future.
No functional change intended.
--- arch/arm/kernel/perf_callchain.c | 33 ++++------------------------- arch/csky/kernel/perf_callchain.c | 12 ----------- arch/nds32/kernel/perf_event_cpu.c | 34 ++++-------------------------- arch/riscv/kernel/perf_callchain.c | 13 ------------ 4 files changed, 8 insertions(+), 84 deletions(-)
arch/arm/kernel/perf_callchain.c +4 -29
@@ -62,14 +62,8 @@ user_backtrace(struct frame_tail __user *tail,void perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct frame_tail __user *tail; -if (guest_cbs && guest_cbs->is_in_guest ()) { -/* We don't support guest os callchain now */ -return ; - } -perf_callchain_store (entry, regs->ARM_pc);if (!current->mm)
@@ -99,44 +93,25 @@ callchain_trace(struct stackframe *fr,void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct stackframe fr; -if (guest_cbs && guest_cbs->is_in_guest ()) { -/* We don't support guest os callchain now */ -return ; - } -arm_get_current_stackframe (regs, &fr);walk_stackframe (&fr, callchain_trace, entry); }unsigned long perf_instruction_pointer (struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); - -if (guest_cbs && guest_cbs->is_in_guest ()) -return guest_cbs->get_guest_ip (); -return instruction_pointer (regs); }unsigned long perf_misc_flags (struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();int misc =0 ; -if (guest_cbs && guest_cbs->is_in_guest ()) { -if (guest_cbs->is_user_mode ()) - misc |=PERF_RECORD_MISC_GUEST_USER ; -else - misc |=PERF_RECORD_MISC_GUEST_KERNEL ; - }else { -if (user_mode (regs)) - misc |=PERF_RECORD_MISC_USER ; -else - misc |=PERF_RECORD_MISC_KERNEL ; - } +if (user_mode (regs)) + misc |=PERF_RECORD_MISC_USER ; +else + misc |=PERF_RECORD_MISC_KERNEL ;return misc; }
arch/csky/kernel/perf_callchain.c +0 -12
@@ -86,13 +86,8 @@ static unsigned long user_backtrace(struct perf_callchain_entry_ctx *entry,void perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();unsigned long fp =0 ; -/* C-SKY does not support virtualization. */ -if (guest_cbs && guest_cbs->is_in_guest ()) -return ; - fp = regs->regs[4 ];perf_callchain_store (entry, regs->pc);
@@ -111,15 +106,8 @@ void perf_callchain_user(struct perf_callchain_entry_ctx *entry,void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct stackframe fr; -/* C-SKY does not support virtualization. */ -if (guest_cbs && guest_cbs->is_in_guest ()) { -pr_warn ("C-SKY does not support perf in guest mode!" ); -return ; - } - fr.fp = regs->regs[4 ]; fr.lr = regs->lr;walk_stackframe (&fr, entry);
arch/nds32/kernel/perf_event_cpu.c +4 -30
@@ -1363,7 +1363,6 @@ void perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) { - struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs(); unsigned long fp = 0;unsigned long gp =0 ;unsigned long lp =0 ;
@@ -1372,11 +1371,6 @@ perf_callchain_user(struct perf_callchain_entry_ctx *entry, leaf_fp =0 ; -if (guest_cbs && guest_cbs->is_in_guest ()) { -/* We don't support guest os callchain now */ -return ; - } -perf_callchain_store (entry, regs->ipc); fp = regs->fp; gp = regs->gp;
@@ -1480,13 +1474,8 @@ void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) { - struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs(); struct stackframe fr; -if (guest_cbs && guest_cbs->is_in_guest ()) { -/* We don't support guest os callchain now */ -return ; - } fr.fp = regs->fp; fr.lp = regs->lp; fr.sp = regs->sp;
@@ -1495,32 +1484,17 @@ perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,unsigned long perf_instruction_pointer (struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); - -/* However, NDS32 does not support virtualization */ -if (guest_cbs && guest_cbs->is_in_guest ()) -return guest_cbs->get_guest_ip (); -return instruction_pointer (regs); }unsigned long perf_misc_flags (struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();int misc =0 ; -/* However, NDS32 does not support virtualization */ -if (guest_cbs && guest_cbs->is_in_guest ()) { -if (guest_cbs->is_user_mode ()) - misc |=PERF_RECORD_MISC_GUEST_USER ; -else - misc |=PERF_RECORD_MISC_GUEST_KERNEL ; - }else { -if (user_mode (regs)) - misc |=PERF_RECORD_MISC_USER ; -else - misc |=PERF_RECORD_MISC_KERNEL ; - } +if (user_mode (regs)) + misc |=PERF_RECORD_MISC_USER ; +else + misc |=PERF_RECORD_MISC_KERNEL ;return misc; }
arch/riscv/kernel/perf_callchain.c +0 -13
@@ -56,13 +56,8 @@ static unsigned long user_backtrace(struct perf_callchain_entry_ctx *entry,void perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();unsigned long fp =0 ; -/* RISC-V does not support perf in guest mode. */ -if (guest_cbs && guest_cbs->is_in_guest ()) -return ; - fp = regs->s0;perf_callchain_store (entry, regs->epc);
@@ -79,13 +74,5 @@ static bool fill_callchain(void *entry, unsigned long pc)void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); - -/* RISC-V does not support perf in guest mode. */ -if (guest_cbs && guest_cbs->is_in_guest ()) { -pr_warn ("RISC-V does not support perf in guest mode!" ); -return ; - } -walk_stackframe (NULL , regs, fill_callchain, entry); }
Signature · 6 lines
2.34.0.rc0.344.g81b53c2807-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
[tip: perf/core] perf: Drop dead and useless guest "support" from arm, csky, nds32 and riscv
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 84af21d850ee1ccc990df37dd47c13fdfe93be75 Gitweb: https://git.kernel.org/tip/84af21d850ee1ccc990df37dd47c13fdfe93be75 Author: Sean Christopherson <[email protected]> AuthorDate: Thu, 11 Nov 2021 02:07:26 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Nov 2021 14:49:07 +01:00
perf: Drop dead and useless guest "support" from arm, csky, nds32 and riscv
Drop "support" for guest callbacks from architectures that don't implement the guest callbacks. Future patches will convert the callbacks to static_call; rather than churn a bunch of arch code (that was presumably copy+pasted from x86), remove it wholesale as it's useless and at best wasting cycles.
A future patch will also add a Kconfig to force architcture to opt into the callbacks to make it more difficult for uses "support" to sneak in in the future.
No functional change intended.
--- arch/arm/kernel/perf_callchain.c | 33 +++------------------------- arch/csky/kernel/perf_callchain.c | 12 +---------- arch/nds32/kernel/perf_event_cpu.c | 34 +++-------------------------- arch/riscv/kernel/perf_callchain.c | 13 +----------- 4 files changed, 8 insertions(+), 84 deletions(-)
arch/arm/kernel/perf_callchain.c +4 -29
@@ -62,14 +62,8 @@ user_backtrace(struct frame_tail __user *tail,void perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct frame_tail __user *tail; -if (guest_cbs && guest_cbs->is_in_guest ()) { -/* We don't support guest os callchain now */ -return ; - } -perf_callchain_store (entry, regs->ARM_pc);if (!current->mm)
@@ -99,44 +93,25 @@ callchain_trace(struct stackframe *fr,void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct stackframe fr; -if (guest_cbs && guest_cbs->is_in_guest ()) { -/* We don't support guest os callchain now */ -return ; - } -arm_get_current_stackframe (regs, &fr);walk_stackframe (&fr, callchain_trace, entry); }unsigned long perf_instruction_pointer (struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); - -if (guest_cbs && guest_cbs->is_in_guest ()) -return guest_cbs->get_guest_ip (); -return instruction_pointer (regs); }unsigned long perf_misc_flags (struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();int misc =0 ; -if (guest_cbs && guest_cbs->is_in_guest ()) { -if (guest_cbs->is_user_mode ()) - misc |=PERF_RECORD_MISC_GUEST_USER ; -else - misc |=PERF_RECORD_MISC_GUEST_KERNEL ; - }else { -if (user_mode (regs)) - misc |=PERF_RECORD_MISC_USER ; -else - misc |=PERF_RECORD_MISC_KERNEL ; - } +if (user_mode (regs)) + misc |=PERF_RECORD_MISC_USER ; +else + misc |=PERF_RECORD_MISC_KERNEL ;return misc; }
arch/csky/kernel/perf_callchain.c +0 -12
@@ -86,13 +86,8 @@ static unsigned long user_backtrace(struct perf_callchain_entry_ctx *entry,void perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();unsigned long fp =0 ; -/* C-SKY does not support virtualization. */ -if (guest_cbs && guest_cbs->is_in_guest ()) -return ; - fp = regs->regs[4 ];perf_callchain_store (entry, regs->pc);
@@ -111,15 +106,8 @@ void perf_callchain_user(struct perf_callchain_entry_ctx *entry,void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct stackframe fr; -/* C-SKY does not support virtualization. */ -if (guest_cbs && guest_cbs->is_in_guest ()) { -pr_warn ("C-SKY does not support perf in guest mode!" ); -return ; - } - fr.fp = regs->regs[4 ]; fr.lr = regs->lr;walk_stackframe (&fr, entry);
arch/nds32/kernel/perf_event_cpu.c +4 -30
@@ -1363,7 +1363,6 @@ void perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) { - struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs(); unsigned long fp = 0;unsigned long gp =0 ;unsigned long lp =0 ;
@@ -1372,11 +1371,6 @@ perf_callchain_user(struct perf_callchain_entry_ctx *entry, leaf_fp =0 ; -if (guest_cbs && guest_cbs->is_in_guest ()) { -/* We don't support guest os callchain now */ -return ; - } -perf_callchain_store (entry, regs->ipc); fp = regs->fp; gp = regs->gp;
@@ -1480,13 +1474,8 @@ void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) { - struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs(); struct stackframe fr; -if (guest_cbs && guest_cbs->is_in_guest ()) { -/* We don't support guest os callchain now */ -return ; - } fr.fp = regs->fp; fr.lp = regs->lp; fr.sp = regs->sp;
@@ -1495,32 +1484,17 @@ perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,unsigned long perf_instruction_pointer (struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); - -/* However, NDS32 does not support virtualization */ -if (guest_cbs && guest_cbs->is_in_guest ()) -return guest_cbs->get_guest_ip (); -return instruction_pointer (regs); }unsigned long perf_misc_flags (struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();int misc =0 ; -/* However, NDS32 does not support virtualization */ -if (guest_cbs && guest_cbs->is_in_guest ()) { -if (guest_cbs->is_user_mode ()) - misc |=PERF_RECORD_MISC_GUEST_USER ; -else - misc |=PERF_RECORD_MISC_GUEST_KERNEL ; - }else { -if (user_mode (regs)) - misc |=PERF_RECORD_MISC_USER ; -else - misc |=PERF_RECORD_MISC_KERNEL ; - } +if (user_mode (regs)) + misc |=PERF_RECORD_MISC_USER ; +else + misc |=PERF_RECORD_MISC_KERNEL ;return misc; }
arch/riscv/kernel/perf_callchain.c +0 -13
@@ -56,13 +56,8 @@ static unsigned long user_backtrace(struct perf_callchain_entry_ctx *entry,void perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();unsigned long fp =0 ; -/* RISC-V does not support perf in guest mode. */ -if (guest_cbs && guest_cbs->is_in_guest ()) -return ; - fp = regs->s0;perf_callchain_store (entry, regs->epc);
@@ -79,13 +74,5 @@ static bool fill_callchain(void *entry, unsigned long pc)void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); - -/* RISC-V does not support perf in guest mode. */ -if (guest_cbs && guest_cbs->is_in_guest ()) { -pr_warn ("RISC-V does not support perf in guest mode!" ); -return ; - } -walk_stackframe (NULL , regs, fill_callchain, entry); }
Sean Christopherson
maintainerPATCH v4 6/17 perf/core: Rework guest callbacks to prepare for static_call support
In reply to Sean ChristophersonFrom: Like Xu <[email protected]>
To prepare for using static_calls to optimize perf's guest callbacks, replace ->is_in_guest and ->is_user_mode with a new multiplexed hook ->state, tweak ->handle_intel_pt_intr to play nice with being called when there is no active guest, and drop "guest" from ->get_guest_ip.
Return '0' from ->state and ->handle_intel_pt_intr to indicate "not in guest" so that DEFINE_STATIC_CALL_RET0 can be used to define the static calls, i.e. no callback == !guest.
--- arch/arm64/kernel/perf_callchain.c | 13 +++++----- arch/arm64/kvm/perf.c | 35 +++++++++++--------------- arch/x86/events/core.c | 13 +++++----- arch/x86/events/intel/core.c | 5 +--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/pmu.c | 2 +- arch/x86/kvm/x86.c | 40 ++++++++++++++++-------------- arch/x86/xen/pmu.c | 32 ++++++++++-------------- include/linux/perf_event.h | 10 +++++--- 9 files changed, 73 insertions(+), 79 deletions(-)
arch/arm64/kernel/perf_callchain.c +7 -6
@@ -104,7 +104,7 @@ void perf_callchain_user(struct perf_callchain_entry_ctx *entry, {struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); -if (guest_cbs && guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->state ()) {/* We don't support guest os callchain now */ return ; }
@@ -152,7 +152,7 @@ void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct stackframe frame; -if (guest_cbs && guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->state ()) {/* We don't support guest os callchain now */ return ; }
@@ -165,8 +165,8 @@ unsigned long perf_instruction_pointer(struct pt_regs *regs) {struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); -if (guest_cbs && guest_cbs->is_in_guest ()) -return guest_cbs->get_guest_ip (); +if (guest_cbs && guest_cbs->state ()) +return guest_cbs->get_ip ();return instruction_pointer (regs); }
@@ -174,10 +174,11 @@ unsigned long perf_instruction_pointer(struct pt_regs *regs)unsigned long perf_misc_flags (struct pt_regs *regs) {struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); +unsigned int guest_state = guest_cbs ? guest_cbs->state () :0 ;int misc =0 ; -if (guest_cbs && guest_cbs->is_in_guest ()) { -if (guest_cbs->is_user_mode ()) +if (guest_state) { +if (guest_state &PERF_GUEST_USER ) misc |=PERF_RECORD_MISC_GUEST_USER ;else misc |=PERF_RECORD_MISC_GUEST_KERNEL ;
arch/arm64/kvm/perf.c +15 -20
@@ -13,39 +13,34 @@DEFINE_STATIC_KEY_FALSE (kvm_arm_pmu_available); -static int kvm_is_in_guest (void ) +static unsigned int kvm_guest_state (void ) { -return kvm_get_running_vcpu () !=NULL ; -} +struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); +unsigned int state; -static int kvm_is_user_mode (void ) -{ -struct kvm_vcpu *vcpu; - - vcpu =kvm_get_running_vcpu (); +if (!vcpu) +return 0 ; -if (vcpu) -return !vcpu_mode_priv (vcpu); + state =PERF_GUEST_ACTIVE ; +if (!vcpu_mode_priv (vcpu)) + state |=PERF_GUEST_USER ; -return 0 ; +return state; }static unsigned long kvm_get_guest_ip (void ) { -struct kvm_vcpu *vcpu; +struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); - vcpu =kvm_get_running_vcpu (); +if (WARN_ON_ONCE (!vcpu)) +return 0 ; -if (vcpu) -return *vcpu_pc (vcpu); - -return 0 ; +return *vcpu_pc (vcpu); }static struct perf_guest_info_callbacks kvm_guest_cbs = { - .is_in_guest = kvm_is_in_guest, - .is_user_mode = kvm_is_user_mode, - .get_guest_ip = kvm_get_guest_ip, + .state = kvm_guest_state, + .get_ip = kvm_get_guest_ip, };void kvm_perf_init (void )
arch/x86/events/core.c +7 -6
@@ -2772,7 +2772,7 @@ perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *restruct unwind_state state;unsigned long addr; -if (guest_cbs && guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->state ()) {/* TODO: We don't support guest os callchain now */ return ; }
@@ -2876,7 +2876,7 @@ perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regsstruct stack_frame frame;const struct stack_frame __user *fp; -if (guest_cbs && guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->state ()) {/* TODO: We don't support guest os callchain now */ return ; }
@@ -2955,8 +2955,8 @@ unsigned long perf_instruction_pointer(struct pt_regs *regs) {struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); -if (guest_cbs && guest_cbs->is_in_guest ()) -return guest_cbs->get_guest_ip (); +if (guest_cbs && guest_cbs->state ()) +return guest_cbs->get_ip ();return regs->ip +code_segment_base (regs); }
@@ -2964,10 +2964,11 @@ unsigned long perf_instruction_pointer(struct pt_regs *regs)unsigned long perf_misc_flags (struct pt_regs *regs) {struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); +unsigned int guest_state = guest_cbs ? guest_cbs->state () :0 ;int misc =0 ; -if (guest_cbs && guest_cbs->is_in_guest ()) { -if (guest_cbs->is_user_mode ()) +if (guest_state) { +if (guest_state &PERF_GUEST_USER ) misc |=PERF_RECORD_MISC_GUEST_USER ;else misc |=PERF_RECORD_MISC_GUEST_KERNEL ;
arch/x86/events/intel/core.c +1 -4
@@ -2906,10 +2906,7 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status) handled++; guest_cbs =perf_get_guest_cbs (); -if (unlikely (guest_cbs && guest_cbs->is_in_guest () && - guest_cbs->handle_intel_pt_intr)) - guest_cbs->handle_intel_pt_intr (); -else +if (likely (!guest_cbs || !guest_cbs->handle_intel_pt_intr ()))intel_pt_interrupt (); }
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1893,7 +1893,7 @@ int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu);int kvm_complete_insn_gp (struct kvm_vcpu *vcpu,int err);void __kvm_request_immediate_exit (struct kvm_vcpu *vcpu); -int kvm_is_in_guest (void ); +unsigned int kvm_guest_state (void );void __user *__x86_set_memory_region (struct kvm *kvm,int id,gpa_t gpa,u32 size);
arch/x86/kvm/pmu.c +1 -1
@@ -87,7 +87,7 @@ static void kvm_perf_overflow_intr(struct perf_event *perf_event,* woken up. So we should wake it, but this is impossible from * NMI context. Do it from irq work instead. */ -if (!kvm_is_in_guest ()) +if (!kvm_guest_state ())irq_work_queue (&pmc_to_pmu (pmc)->irq_work);else kvm_make_request (KVM_REQ_PMI , pmc->vcpu);
arch/x86/kvm/x86.c +22 -18
@@ -8413,44 +8413,48 @@ static void kvm_timer_init(void) DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);EXPORT_PER_CPU_SYMBOL_GPL (current_vcpu); -int kvm_is_in_guest (void ) +unsigned int kvm_guest_state (void ) { -return __this_cpu_read (current_vcpu) !=NULL ; -} +struct kvm_vcpu *vcpu =__this_cpu_read (current_vcpu); +unsigned int state; -static int kvm_is_user_mode (void ) -{ -int user_mode =3 ; +if (!vcpu) +return 0 ; -if (__this_cpu_read (current_vcpu)) - user_mode =static_call (kvm_x86_get_cpl)(__this_cpu_read (current_vcpu)); + state =PERF_GUEST_ACTIVE ; +if (static_call (kvm_x86_get_cpl)(vcpu)) + state |=PERF_GUEST_USER ; -return user_mode !=0 ; +return state; } -static unsigned long kvm_get_guest_ip (void ) +static unsigned long kvm_guest_get_ip (void ) { -unsigned long ip =0 ; +struct kvm_vcpu *vcpu =__this_cpu_read (current_vcpu); -if (__this_cpu_read (current_vcpu)) - ip =kvm_rip_read (__this_cpu_read (current_vcpu)); +if (WARN_ON_ONCE (!vcpu)) +return 0 ; -return ip; +return kvm_rip_read (vcpu); } -static void kvm_handle_intel_pt_intr (void ) +static unsigned int kvm_handle_intel_pt_intr (void ) {struct kvm_vcpu *vcpu =__this_cpu_read (current_vcpu); +/* '0' on failure so that the !PT case can use a RET0 static call. */ +if (!vcpu) +return 0 ; +kvm_make_request (KVM_REQ_PMI , vcpu);__set_bit (MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT , (unsigned long *)&vcpu->arch.pmu.global_status); +return 1 ; }static struct perf_guest_info_callbacks kvm_guest_cbs = { - .is_in_guest = kvm_is_in_guest, - .is_user_mode = kvm_is_user_mode, - .get_guest_ip = kvm_get_guest_ip, + .state = kvm_guest_state, + .get_ip = kvm_guest_get_ip, .handle_intel_pt_intr =NULL , };
arch/x86/xen/pmu.c +13 -19
@@ -413,34 +413,29 @@ int pmu_apic_update(uint32_t val) }/* perf callbacks */ -static int xen_is_in_guest (void ) +static unsigned int xen_guest_state (void ) {const struct xen_pmu_data *xenpmu_data =get_xenpmu_data (); +unsigned int state =0 ;if (!xenpmu_data) {pr_warn_once ("%s: pmudata not initialized\n" , __func__); -return 0 ; +return state; }if (!xen_initial_domain () || (xenpmu_data->domain_id >=DOMID_SELF )) -return 0 ; +return state; -return 1 ; -} + state |=PERF_GUEST_ACTIVE ; -static int xen_is_user_mode (void ) -{ -const struct xen_pmu_data *xenpmu_data =get_xenpmu_data (); - -if (!xenpmu_data) { -pr_warn_once ("%s: pmudata not initialized\n" , __func__); -return 0 ; +if (xenpmu_data->pmu.pmu_flags &PMU_SAMPLE_PV ) { +if (xenpmu_data->pmu.pmu_flags &PMU_SAMPLE_USER ) + state |=PERF_GUEST_USER ; + }else if (xenpmu_data->pmu.r.regs.cpl &3 ) { + state |=PERF_GUEST_USER ; } -if (xenpmu_data->pmu.pmu_flags &PMU_SAMPLE_PV ) -return (xenpmu_data->pmu.pmu_flags &PMU_SAMPLE_USER ); -else -return !!(xenpmu_data->pmu.r.regs.cpl &3 ); +return state; }static unsigned long xen_get_guest_ip (void )
@@ -456,9 +451,8 @@ static unsigned long xen_get_guest_ip(void) }static struct perf_guest_info_callbacks xen_guest_cbs = { - .is_in_guest = xen_is_in_guest, - .is_user_mode = xen_is_user_mode, - .get_guest_ip = xen_get_guest_ip, + .state = xen_guest_state, + .get_ip = xen_get_guest_ip, };/* Convert registers from Xen's format to Linux' */
include/linux/perf_event.h +6 -4
@@ -26,11 +26,13 @@# include <asm/local64.h> #endif +#define PERF_GUEST_ACTIVE 0x01 +#define PERF_GUEST_USER 0x02 +struct perf_guest_info_callbacks { -int (*is_in_guest)(void ); -int (*is_user_mode)(void ); -unsigned long (*get_guest_ip)(void ); -void (*handle_intel_pt_intr)(void ); +unsigned int (*state)(void ); +unsigned long (*get_ip)(void ); +unsigned int (*handle_intel_pt_intr)(void ); };#ifdef CONFIG_HAVE_HW_BREAKPOINT
Signature · 6 lines
2.34.0.rc0.344.g81b53c2807-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
[tip: perf/core] perf/core: Rework guest callbacks to prepare for static_call support
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: b9f5621c9547dd787900f005a9e1c3d5712de512 Gitweb: https://git.kernel.org/tip/b9f5621c9547dd787900f005a9e1c3d5712de512 Author: Like Xu <[email protected]> AuthorDate: Thu, 11 Nov 2021 02:07:27 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Nov 2021 14:49:07 +01:00
perf/core: Rework guest callbacks to prepare for static_call support
To prepare for using static_calls to optimize perf's guest callbacks, replace ->is_in_guest and ->is_user_mode with a new multiplexed hook ->state, tweak ->handle_intel_pt_intr to play nice with being called when there is no active guest, and drop "guest" from ->get_guest_ip.
Return '0' from ->state and ->handle_intel_pt_intr to indicate "not in guest" so that DEFINE_STATIC_CALL_RET0 can be used to define the static calls, i.e. no callback == !guest.
--- arch/arm64/kernel/perf_callchain.c | 13 ++++----- arch/arm64/kvm/perf.c | 35 ++++++++++--------------- arch/x86/events/core.c | 13 ++++----- arch/x86/events/intel/core.c | 5 +---- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/pmu.c | 2 +- arch/x86/kvm/x86.c | 40 +++++++++++++++-------------- arch/x86/xen/pmu.c | 32 +++++++++-------------- include/linux/perf_event.h | 10 ++++--- 9 files changed, 73 insertions(+), 79 deletions(-)
arch/arm64/kernel/perf_callchain.c +7 -6
@@ -104,7 +104,7 @@ void perf_callchain_user(struct perf_callchain_entry_ctx *entry, {struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); -if (guest_cbs && guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->state ()) {/* We don't support guest os callchain now */ return ; }
@@ -152,7 +152,7 @@ void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct stackframe frame; -if (guest_cbs && guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->state ()) {/* We don't support guest os callchain now */ return ; }
@@ -165,8 +165,8 @@ unsigned long perf_instruction_pointer(struct pt_regs *regs) {struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); -if (guest_cbs && guest_cbs->is_in_guest ()) -return guest_cbs->get_guest_ip (); +if (guest_cbs && guest_cbs->state ()) +return guest_cbs->get_ip ();return instruction_pointer (regs); }
@@ -174,10 +174,11 @@ unsigned long perf_instruction_pointer(struct pt_regs *regs)unsigned long perf_misc_flags (struct pt_regs *regs) {struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); +unsigned int guest_state = guest_cbs ? guest_cbs->state () :0 ;int misc =0 ; -if (guest_cbs && guest_cbs->is_in_guest ()) { -if (guest_cbs->is_user_mode ()) +if (guest_state) { +if (guest_state &PERF_GUEST_USER ) misc |=PERF_RECORD_MISC_GUEST_USER ;else misc |=PERF_RECORD_MISC_GUEST_KERNEL ;
arch/arm64/kvm/perf.c +15 -20
@@ -13,39 +13,34 @@DEFINE_STATIC_KEY_FALSE (kvm_arm_pmu_available); -static int kvm_is_in_guest (void ) +static unsigned int kvm_guest_state (void ) { -return kvm_get_running_vcpu () !=NULL ; -} - -static int kvm_is_user_mode (void ) -{ -struct kvm_vcpu *vcpu; +struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); +unsigned int state; - vcpu =kvm_get_running_vcpu (); +if (!vcpu) +return 0 ; -if (vcpu) -return !vcpu_mode_priv (vcpu); + state =PERF_GUEST_ACTIVE ; +if (!vcpu_mode_priv (vcpu)) + state |=PERF_GUEST_USER ; -return 0 ; +return state; }static unsigned long kvm_get_guest_ip (void ) { -struct kvm_vcpu *vcpu; - - vcpu =kvm_get_running_vcpu (); +struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); -if (vcpu) -return *vcpu_pc (vcpu); +if (WARN_ON_ONCE (!vcpu)) +return 0 ; -return 0 ; +return *vcpu_pc (vcpu); }static struct perf_guest_info_callbacks kvm_guest_cbs = { - .is_in_guest = kvm_is_in_guest, - .is_user_mode = kvm_is_user_mode, - .get_guest_ip = kvm_get_guest_ip, + .state = kvm_guest_state, + .get_ip = kvm_get_guest_ip, };void kvm_perf_init (void )
arch/x86/events/core.c +7 -6
@@ -2772,7 +2772,7 @@ perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *restruct unwind_state state;unsigned long addr; -if (guest_cbs && guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->state ()) {/* TODO: We don't support guest os callchain now */ return ; }
@@ -2876,7 +2876,7 @@ perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regsstruct stack_frame frame;const struct stack_frame __user *fp; -if (guest_cbs && guest_cbs->is_in_guest ()) { +if (guest_cbs && guest_cbs->state ()) {/* TODO: We don't support guest os callchain now */ return ; }
@@ -2955,8 +2955,8 @@ unsigned long perf_instruction_pointer(struct pt_regs *regs) {struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); -if (guest_cbs && guest_cbs->is_in_guest ()) -return guest_cbs->get_guest_ip (); +if (guest_cbs && guest_cbs->state ()) +return guest_cbs->get_ip ();return regs->ip +code_segment_base (regs); }
@@ -2964,10 +2964,11 @@ unsigned long perf_instruction_pointer(struct pt_regs *regs)unsigned long perf_misc_flags (struct pt_regs *regs) {struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); +unsigned int guest_state = guest_cbs ? guest_cbs->state () :0 ;int misc =0 ; -if (guest_cbs && guest_cbs->is_in_guest ()) { -if (guest_cbs->is_user_mode ()) +if (guest_state) { +if (guest_state &PERF_GUEST_USER ) misc |=PERF_RECORD_MISC_GUEST_USER ;else misc |=PERF_RECORD_MISC_GUEST_KERNEL ;
arch/x86/events/intel/core.c +1 -4
@@ -2906,10 +2906,7 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status) handled++; guest_cbs =perf_get_guest_cbs (); -if (unlikely (guest_cbs && guest_cbs->is_in_guest () && - guest_cbs->handle_intel_pt_intr)) - guest_cbs->handle_intel_pt_intr (); -else +if (likely (!guest_cbs || !guest_cbs->handle_intel_pt_intr ()))intel_pt_interrupt (); }
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1895,7 +1895,7 @@ int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu);int kvm_complete_insn_gp (struct kvm_vcpu *vcpu,int err);void __kvm_request_immediate_exit (struct kvm_vcpu *vcpu); -int kvm_is_in_guest (void ); +unsigned int kvm_guest_state (void );void __user *__x86_set_memory_region (struct kvm *kvm,int id,gpa_t gpa,u32 size);
arch/x86/kvm/pmu.c +1 -1
@@ -87,7 +87,7 @@ static void kvm_perf_overflow_intr(struct perf_event *perf_event,* woken up. So we should wake it, but this is impossible from * NMI context. Do it from irq work instead. */ -if (!kvm_is_in_guest ()) +if (!kvm_guest_state ())irq_work_queue (&pmc_to_pmu (pmc)->irq_work);else kvm_make_request (KVM_REQ_PMI , pmc->vcpu);
arch/x86/kvm/x86.c +22 -18
@@ -8472,44 +8472,48 @@ static void kvm_timer_init(void) DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);EXPORT_PER_CPU_SYMBOL_GPL (current_vcpu); -int kvm_is_in_guest (void ) +unsigned int kvm_guest_state (void ) { -return __this_cpu_read (current_vcpu) !=NULL ; -} +struct kvm_vcpu *vcpu =__this_cpu_read (current_vcpu); +unsigned int state; -static int kvm_is_user_mode (void ) -{ -int user_mode =3 ; +if (!vcpu) +return 0 ; -if (__this_cpu_read (current_vcpu)) - user_mode =static_call (kvm_x86_get_cpl)(__this_cpu_read (current_vcpu)); + state =PERF_GUEST_ACTIVE ; +if (static_call (kvm_x86_get_cpl)(vcpu)) + state |=PERF_GUEST_USER ; -return user_mode !=0 ; +return state; } -static unsigned long kvm_get_guest_ip (void ) +static unsigned long kvm_guest_get_ip (void ) { -unsigned long ip =0 ; +struct kvm_vcpu *vcpu =__this_cpu_read (current_vcpu); -if (__this_cpu_read (current_vcpu)) - ip =kvm_rip_read (__this_cpu_read (current_vcpu)); +if (WARN_ON_ONCE (!vcpu)) +return 0 ; -return ip; +return kvm_rip_read (vcpu); } -static void kvm_handle_intel_pt_intr (void ) +static unsigned int kvm_handle_intel_pt_intr (void ) {struct kvm_vcpu *vcpu =__this_cpu_read (current_vcpu); +/* '0' on failure so that the !PT case can use a RET0 static call. */ +if (!vcpu) +return 0 ; +kvm_make_request (KVM_REQ_PMI , vcpu);__set_bit (MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT , (unsigned long *)&vcpu->arch.pmu.global_status); +return 1 ; }static struct perf_guest_info_callbacks kvm_guest_cbs = { - .is_in_guest = kvm_is_in_guest, - .is_user_mode = kvm_is_user_mode, - .get_guest_ip = kvm_get_guest_ip, + .state = kvm_guest_state, + .get_ip = kvm_guest_get_ip, .handle_intel_pt_intr =NULL , };
arch/x86/xen/pmu.c +13 -19
@@ -413,34 +413,29 @@ int pmu_apic_update(uint32_t val) }/* perf callbacks */ -static int xen_is_in_guest (void ) +static unsigned int xen_guest_state (void ) {const struct xen_pmu_data *xenpmu_data =get_xenpmu_data (); +unsigned int state =0 ;if (!xenpmu_data) {pr_warn_once ("%s: pmudata not initialized\n" , __func__); -return 0 ; +return state; }if (!xen_initial_domain () || (xenpmu_data->domain_id >=DOMID_SELF )) -return 0 ; +return state; -return 1 ; -} - -static int xen_is_user_mode (void ) -{ -const struct xen_pmu_data *xenpmu_data =get_xenpmu_data (); + state |=PERF_GUEST_ACTIVE ; -if (!xenpmu_data) { -pr_warn_once ("%s: pmudata not initialized\n" , __func__); -return 0 ; +if (xenpmu_data->pmu.pmu_flags &PMU_SAMPLE_PV ) { +if (xenpmu_data->pmu.pmu_flags &PMU_SAMPLE_USER ) + state |=PERF_GUEST_USER ; + }else if (xenpmu_data->pmu.r.regs.cpl &3 ) { + state |=PERF_GUEST_USER ; } -if (xenpmu_data->pmu.pmu_flags &PMU_SAMPLE_PV ) -return (xenpmu_data->pmu.pmu_flags &PMU_SAMPLE_USER ); -else -return !!(xenpmu_data->pmu.r.regs.cpl &3 ); +return state; }static unsigned long xen_get_guest_ip (void )
@@ -456,9 +451,8 @@ static unsigned long xen_get_guest_ip(void) }static struct perf_guest_info_callbacks xen_guest_cbs = { - .is_in_guest = xen_is_in_guest, - .is_user_mode = xen_is_user_mode, - .get_guest_ip = xen_get_guest_ip, + .state = xen_guest_state, + .get_ip = xen_get_guest_ip, };/* Convert registers from Xen's format to Linux' */
include/linux/perf_event.h +6 -4
@@ -26,11 +26,13 @@# include <asm/local64.h> #endif +#define PERF_GUEST_ACTIVE 0x01 +#define PERF_GUEST_USER 0x02 +struct perf_guest_info_callbacks { -int (*is_in_guest)(void ); -int (*is_user_mode)(void ); -unsigned long (*get_guest_ip)(void ); -void (*handle_intel_pt_intr)(void ); +unsigned int (*state)(void ); +unsigned long (*get_ip)(void ); +unsigned int (*handle_intel_pt_intr)(void ); };#ifdef CONFIG_HAVE_HW_BREAKPOINT
Sean Christopherson
maintainerPATCH v4 7/17 perf: Add wrappers for invoking guest callbacks
In reply to Sean ChristophersonAdd helpers for the guest callbacks to prepare for burying the callbacks behind a Kconfig (it's a lot easier to provide a few stubs than to #ifdef piles of code), and also to prepare for converting the callbacks to static_call(). perf_instruction_pointer() in particular will have subtle semantics with static_call(), as the "no callbacks" case will return 0 if the callbacks are unregistered between querying guest state and getting the IP. Implement the change now to avoid a functional change when adding static_call() support, and because the new helper needs to return _something_ in this case.
--- arch/arm64/kernel/perf_callchain.c | 16 +++++----------- arch/x86/events/core.c | 15 +++++---------- arch/x86/events/intel/core.c | 5 +---- include/linux/perf_event.h | 24 ++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 25 deletions(-)
arch/arm64/kernel/perf_callchain.c +5 -11
@@ -102,9 +102,7 @@ compat_user_backtrace(struct compat_frame_tail __user *tail,void perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); - -if (guest_cbs && guest_cbs->state ()) { +if (perf_guest_state ()) {/* We don't support guest os callchain now */ return ; }
@@ -149,10 +147,9 @@ static bool callchain_trace(void *data, unsigned long pc)void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct stackframe frame; -if (guest_cbs && guest_cbs->state ()) { +if (perf_guest_state ()) {/* We don't support guest os callchain now */ return ; }
@@ -163,18 +160,15 @@ void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,unsigned long perf_instruction_pointer (struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); - -if (guest_cbs && guest_cbs->state ()) -return guest_cbs->get_ip (); +if (perf_guest_state ()) +return perf_guest_get_ip ();return instruction_pointer (regs); }unsigned long perf_misc_flags (struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); -unsigned int guest_state = guest_cbs ? guest_cbs->state () :0 ; +unsigned int guest_state =perf_guest_state ();int misc =0 ;if (guest_state) {
arch/x86/events/core.c +5 -10
@@ -2768,11 +2768,10 @@ static bool perf_hw_regs(struct pt_regs *regs)void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct unwind_state state;unsigned long addr; -if (guest_cbs && guest_cbs->state ()) { +if (perf_guest_state ()) {/* TODO: We don't support guest os callchain now */ return ; }
@@ -2872,11 +2871,10 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry_ctx *entvoid perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct stack_frame frame;const struct stack_frame __user *fp; -if (guest_cbs && guest_cbs->state ()) { +if (perf_guest_state ()) {/* TODO: We don't support guest os callchain now */ return ; }
@@ -2953,18 +2951,15 @@ static unsigned long code_segment_base(struct pt_regs *regs)unsigned long perf_instruction_pointer (struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); - -if (guest_cbs && guest_cbs->state ()) -return guest_cbs->get_ip (); +if (perf_guest_state ()) +return perf_guest_get_ip ();return regs->ip +code_segment_base (regs); }unsigned long perf_misc_flags (struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); -unsigned int guest_state = guest_cbs ? guest_cbs->state () :0 ; +unsigned int guest_state =perf_guest_state ();int misc =0 ;if (guest_state) {
arch/x86/events/intel/core.c +1 -4
@@ -2837,7 +2837,6 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status) {struct perf_sample_data data;struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events); -struct perf_guest_info_callbacks *guest_cbs;int bit;int handled =0 ;u64 intel_ctrl =hybrid (cpuc->pmu, intel_ctrl);
@@ -2904,9 +2903,7 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status)*/ if (__test_and_clear_bit (GLOBAL_STATUS_TRACE_TOPAPMI_BIT , (unsigned long *)&status)) { handled++; - - guest_cbs =perf_get_guest_cbs (); -if (likely (!guest_cbs || !guest_cbs->handle_intel_pt_intr ())) +if (!perf_guest_handle_intel_pt_intr ())intel_pt_interrupt (); }
include/linux/perf_event.h +24 -0
@@ -1254,6 +1254,30 @@ static inline struct perf_guest_info_callbacks *perf_get_guest_cbs(void)*/ return rcu_dereference (perf_guest_cbs); } +static inline unsigned int perf_guest_state (void ) +{ +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); + +return guest_cbs ? guest_cbs->state () :0 ; +} +static inline unsigned long perf_guest_get_ip (void ) +{ +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); + +/* +* Arbitrarily return '0' in the unlikely scenario that the callbacks +* are unregistered between checking guest state and getting the IP. +*/ +return guest_cbs ? guest_cbs->get_ip () :0 ; +} +static inline unsigned int perf_guest_handle_intel_pt_intr (void ) +{ +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); + +if (guest_cbs && guest_cbs->handle_intel_pt_intr) +return guest_cbs->handle_intel_pt_intr (); +return 0 ; +}extern void perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs);extern void perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *cbs);
Signature · 6 lines
2.34.0.rc0.344.g81b53c2807-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
[tip: perf/core] perf: Add wrappers for invoking guest callbacks
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 1c3430516b0732d923de9fd3bfb3e2e537eeb235 Gitweb: https://git.kernel.org/tip/1c3430516b0732d923de9fd3bfb3e2e537eeb235 Author: Sean Christopherson <[email protected]> AuthorDate: Thu, 11 Nov 2021 02:07:28 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Nov 2021 14:49:08 +01:00
perf: Add wrappers for invoking guest callbacks
Add helpers for the guest callbacks to prepare for burying the callbacks behind a Kconfig (it's a lot easier to provide a few stubs than to #ifdef piles of code), and also to prepare for converting the callbacks to static_call(). perf_instruction_pointer() in particular will have subtle semantics with static_call(), as the "no callbacks" case will return 0 if the callbacks are unregistered between querying guest state and getting the IP. Implement the change now to avoid a functional change when adding static_call() support, and because the new helper needs to return _something_ in this case.
--- arch/arm64/kernel/perf_callchain.c | 16 +++++----------- arch/x86/events/core.c | 15 +++++---------- arch/x86/events/intel/core.c | 5 +---- include/linux/perf_event.h | 24 ++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 25 deletions(-)
arch/arm64/kernel/perf_callchain.c +5 -11
@@ -102,9 +102,7 @@ compat_user_backtrace(struct compat_frame_tail __user *tail,void perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); - -if (guest_cbs && guest_cbs->state ()) { +if (perf_guest_state ()) {/* We don't support guest os callchain now */ return ; }
@@ -149,10 +147,9 @@ static bool callchain_trace(void *data, unsigned long pc)void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct stackframe frame; -if (guest_cbs && guest_cbs->state ()) { +if (perf_guest_state ()) {/* We don't support guest os callchain now */ return ; }
@@ -163,18 +160,15 @@ void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,unsigned long perf_instruction_pointer (struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); - -if (guest_cbs && guest_cbs->state ()) -return guest_cbs->get_ip (); +if (perf_guest_state ()) +return perf_guest_get_ip ();return instruction_pointer (regs); }unsigned long perf_misc_flags (struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); -unsigned int guest_state = guest_cbs ? guest_cbs->state () :0 ; +unsigned int guest_state =perf_guest_state ();int misc =0 ;if (guest_state) {
arch/x86/events/core.c +5 -10
@@ -2768,11 +2768,10 @@ static bool perf_hw_regs(struct pt_regs *regs)void perf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct unwind_state state;unsigned long addr; -if (guest_cbs && guest_cbs->state ()) { +if (perf_guest_state ()) {/* TODO: We don't support guest os callchain now */ return ; }
@@ -2872,11 +2871,10 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry_ctx *entvoid perf_callchain_user (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs ();struct stack_frame frame;const struct stack_frame __user *fp; -if (guest_cbs && guest_cbs->state ()) { +if (perf_guest_state ()) {/* TODO: We don't support guest os callchain now */ return ; }
@@ -2953,18 +2951,15 @@ static unsigned long code_segment_base(struct pt_regs *regs)unsigned long perf_instruction_pointer (struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); - -if (guest_cbs && guest_cbs->state ()) -return guest_cbs->get_ip (); +if (perf_guest_state ()) +return perf_guest_get_ip ();return regs->ip +code_segment_base (regs); }unsigned long perf_misc_flags (struct pt_regs *regs) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); -unsigned int guest_state = guest_cbs ? guest_cbs->state () :0 ; +unsigned int guest_state =perf_guest_state ();int misc =0 ;if (guest_state) {
arch/x86/events/intel/core.c +1 -4
@@ -2837,7 +2837,6 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status) {struct perf_sample_data data;struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events); -struct perf_guest_info_callbacks *guest_cbs;int bit;int handled =0 ;u64 intel_ctrl =hybrid (cpuc->pmu, intel_ctrl);
@@ -2904,9 +2903,7 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status)*/ if (__test_and_clear_bit (GLOBAL_STATUS_TRACE_TOPAPMI_BIT , (unsigned long *)&status)) { handled++; - - guest_cbs =perf_get_guest_cbs (); -if (likely (!guest_cbs || !guest_cbs->handle_intel_pt_intr ())) +if (!perf_guest_handle_intel_pt_intr ())intel_pt_interrupt (); }
include/linux/perf_event.h +24 -0
@@ -1254,6 +1254,30 @@ static inline struct perf_guest_info_callbacks *perf_get_guest_cbs(void)*/ return rcu_dereference (perf_guest_cbs); } +static inline unsigned int perf_guest_state (void ) +{ +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); + +return guest_cbs ? guest_cbs->state () :0 ; +} +static inline unsigned long perf_guest_get_ip (void ) +{ +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); + +/* +* Arbitrarily return '0' in the unlikely scenario that the callbacks +* are unregistered between checking guest state and getting the IP. +*/ +return guest_cbs ? guest_cbs->get_ip () :0 ; +} +static inline unsigned int perf_guest_handle_intel_pt_intr (void ) +{ +struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); + +if (guest_cbs && guest_cbs->handle_intel_pt_intr) +return guest_cbs->handle_intel_pt_intr (); +return 0 ; +}extern void perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs);extern void perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *cbs);
Sean Christopherson
maintainerPATCH v4 8/17 perf: Force architectures to opt-in to guest callbacks
In reply to Sean ChristophersonIntroduce GUEST_PERF_EVENTS and require architectures to select it to allow registering and using guest callbacks in perf. This will hopefully make it more difficult for new architectures to add useless "support" for guest callbacks, e.g. via copy+paste.
Stubbing out the helpers has the happy bonus of avoiding a load of perf_guest_cbs when GUEST_PERF_EVENTS=n on arm64/x86.
--- arch/arm64/kvm/Kconfig | 1 + arch/x86/kvm/Kconfig | 1 + arch/x86/xen/Kconfig | 1 + include/linux/perf_event.h | 6 ++++++ init/Kconfig | 4 ++++ kernel/events/core.c | 2 ++ 6 files changed, 15 insertions(+)
arch/arm64/kvm/Kconfig +1 -0
@@ -39,6 +39,7 @@ menuconfig KVM select HAVE_KVM_IRQ_BYPASS select HAVE_KVM_VCPU_RUN_PID_CHANGE select SCHED_INFO + select GUEST_PERF_EVENTS if PERF_EVENTS help Support hosting virtualized guest machines.
arch/x86/kvm/Kconfig +1 -0
@@ -36,6 +36,7 @@ config KVM select KVM_MMIO select SCHED_INFO select PERF_EVENTS + select GUEST_PERF_EVENTS select HAVE_KVM_MSI select HAVE_KVM_CPU_RELAX_INTERCEPT select HAVE_KVM_NO_POLL
arch/x86/xen/Kconfig +1 -0
@@ -23,6 +23,7 @@ config XEN_PV select PARAVIRT_XXL select XEN_HAVE_PVMMU select XEN_HAVE_VPMU + select GUEST_PERF_EVENTS help Support running as a Xen PV guest.
include/linux/perf_event.h +6 -0
@@ -1242,6 +1242,7 @@ extern void perf_event_bpf_event(struct bpf_prog *prog,enum perf_bpf_event_type type, u16 flags); +#ifdef CONFIG_GUEST_PERF_EVENTS extern struct perf_guest_info_callbacks __rcu *perf_guest_cbs;static inline struct perf_guest_info_callbacks *perf_get_guest_cbs (void ) {
@@ -1280,6 +1281,11 @@ static inline unsigned int perf_guest_handle_intel_pt_intr(void) }extern void perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs);extern void perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *cbs); +#else +static inline unsigned int perf_guest_state (void ) {return 0 ; } +static inline unsigned long perf_guest_get_ip (void ) {return 0 ; } +static inline unsigned int perf_guest_handle_intel_pt_intr (void ) {return 0 ; } +#endif /* CONFIG_GUEST_PERF_EVENTS */ extern void perf_event_exec (void );extern void perf_event_comm (struct task_struct *tsk,bool exec);
init/Kconfig +4 -0
@@ -1799,6 +1799,10 @@ config HAVE_PERF_EVENTS help See tools/perf/design.txt for details. +config GUEST_PERF_EVENTS + bool + depends on HAVE_PERF_EVENTS + config PERF_USE_VMALLOC bool help
kernel/events/core.c +2 -0
@@ -6521,6 +6521,7 @@ static void perf_pending_event(struct irq_work *entry)perf_swevent_put_recursion_context (rctx); } +#ifdef CONFIG_GUEST_PERF_EVENTS struct perf_guest_info_callbacks __rcu *perf_guest_cbs;void perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs)
@@ -6541,6 +6542,7 @@ void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)synchronize_rcu (); }EXPORT_SYMBOL_GPL (perf_unregister_guest_info_callbacks); +#endif static void perf_output_sample_regs(struct perf_output_handle *handle,
Signature · 6 lines
2.34.0.rc0.344.g81b53c2807-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
[tip: perf/core] perf: Force architectures to opt-in to guest callbacks
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 2aef6f306b39bbe74e2287d6e2ee07c4867d87d0 Gitweb: https://git.kernel.org/tip/2aef6f306b39bbe74e2287d6e2ee07c4867d87d0 Author: Sean Christopherson <[email protected]> AuthorDate: Thu, 11 Nov 2021 02:07:29 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Nov 2021 14:49:08 +01:00
perf: Force architectures to opt-in to guest callbacks
Introduce GUEST_PERF_EVENTS and require architectures to select it to allow registering and using guest callbacks in perf. This will hopefully make it more difficult for new architectures to add useless "support" for guest callbacks, e.g. via copy+paste.
Stubbing out the helpers has the happy bonus of avoiding a load of perf_guest_cbs when GUEST_PERF_EVENTS=n on arm64/x86.
--- arch/arm64/kvm/Kconfig | 1 + arch/x86/kvm/Kconfig | 1 + arch/x86/xen/Kconfig | 1 + include/linux/perf_event.h | 6 ++++++ init/Kconfig | 4 ++++ kernel/events/core.c | 2 ++ 6 files changed, 15 insertions(+)
arch/arm64/kvm/Kconfig +1 -0
@@ -39,6 +39,7 @@ menuconfig KVM select HAVE_KVM_IRQ_BYPASS select HAVE_KVM_VCPU_RUN_PID_CHANGE select SCHED_INFO + select GUEST_PERF_EVENTS if PERF_EVENTS help Support hosting virtualized guest machines.
arch/x86/kvm/Kconfig +1 -0
@@ -36,6 +36,7 @@ config KVM select KVM_MMIO select SCHED_INFO select PERF_EVENTS + select GUEST_PERF_EVENTS select HAVE_KVM_MSI select HAVE_KVM_CPU_RELAX_INTERCEPT select HAVE_KVM_NO_POLL
arch/x86/xen/Kconfig +1 -0
@@ -23,6 +23,7 @@ config XEN_PV select PARAVIRT_XXL select XEN_HAVE_PVMMU select XEN_HAVE_VPMU + select GUEST_PERF_EVENTS help Support running as a Xen PV guest.
include/linux/perf_event.h +6 -0
@@ -1242,6 +1242,7 @@ extern void perf_event_bpf_event(struct bpf_prog *prog,enum perf_bpf_event_type type, u16 flags); +#ifdef CONFIG_GUEST_PERF_EVENTS extern struct perf_guest_info_callbacks __rcu *perf_guest_cbs;static inline struct perf_guest_info_callbacks *perf_get_guest_cbs (void ) {
@@ -1280,6 +1281,11 @@ static inline unsigned int perf_guest_handle_intel_pt_intr(void) }extern void perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs);extern void perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *cbs); +#else +static inline unsigned int perf_guest_state (void ) {return 0 ; } +static inline unsigned long perf_guest_get_ip (void ) {return 0 ; } +static inline unsigned int perf_guest_handle_intel_pt_intr (void ) {return 0 ; } +#endif /* CONFIG_GUEST_PERF_EVENTS */ extern void perf_event_exec (void );extern void perf_event_comm (struct task_struct *tsk,bool exec);
init/Kconfig +4 -0
@@ -1804,6 +1804,10 @@ config HAVE_PERF_EVENTS help See tools/perf/design.txt for details. +config GUEST_PERF_EVENTS + bool + depends on HAVE_PERF_EVENTS + config PERF_USE_VMALLOC bool help
kernel/events/core.c +2 -0
@@ -6521,6 +6521,7 @@ static void perf_pending_event(struct irq_work *entry)perf_swevent_put_recursion_context (rctx); } +#ifdef CONFIG_GUEST_PERF_EVENTS struct perf_guest_info_callbacks __rcu *perf_guest_cbs;void perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs)
@@ -6541,6 +6542,7 @@ void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)synchronize_rcu (); }EXPORT_SYMBOL_GPL (perf_unregister_guest_info_callbacks); +#endif static void perf_output_sample_regs(struct perf_output_handle *handle,
Sean Christopherson
maintainerPATCH v4 9/17 perf/core: Use static_call to optimize perf_guest_info_callbacks
In reply to Sean ChristophersonUse static_call to optimize perf's guest callbacks on arm64 and x86, which are now the only architectures that define the callbacks. Use DEFINE_STATIC_CALL_RET0 as the default/NULL for all guest callbacks, as the callback semantics are that a return value '0' means "not in guest".
static_call obviously avoids the overhead of CONFIG_RETPOLINE=y, but is also advantageous versus other solutions, e.g. per-cpu callbacks, in that a per-cpu memory load is not needed to detect the !guest case.
Based on code from Peter and Like.
--- include/linux/perf_event.h | 34 ++++++++-------------------------- kernel/events/core.c | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 26 deletions(-)
include/linux/perf_event.h +8 -26
@@ -1244,40 +1244,22 @@ extern void perf_event_bpf_event(struct bpf_prog *prog,#ifdef CONFIG_GUEST_PERF_EVENTS extern struct perf_guest_info_callbacks __rcu *perf_guest_cbs; -static inline struct perf_guest_info_callbacks *perf_get_guest_cbs (void ) -{ -/* -* Callbacks are RCU-protected and must be READ_ONCE to avoid reloading -* the callbacks between a !NULL check and dereferences, to ensure -* pending stores/changes to the callback pointers are visible before a -* non-NULL perf_guest_cbs is visible to readers, and to prevent a -* module from unloading callbacks while readers are active. -*/ -return rcu_dereference (perf_guest_cbs); -} + +DECLARE_STATIC_CALL (__perf_guest_state, *perf_guest_cbs->state); +DECLARE_STATIC_CALL (__perf_guest_get_ip, *perf_guest_cbs->get_ip); +DECLARE_STATIC_CALL (__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr); +static inline unsigned int perf_guest_state (void ) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); - -return guest_cbs ? guest_cbs->state () :0 ; +return static_call (__perf_guest_state)(); }static inline unsigned long perf_guest_get_ip (void ) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); - -/* -* Arbitrarily return '0' in the unlikely scenario that the callbacks -* are unregistered between checking guest state and getting the IP. -*/ -return guest_cbs ? guest_cbs->get_ip () :0 ; +return static_call (__perf_guest_get_ip)(); }static inline unsigned int perf_guest_handle_intel_pt_intr (void ) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); - -if (guest_cbs && guest_cbs->handle_intel_pt_intr) -return guest_cbs->handle_intel_pt_intr (); -return 0 ; +return static_call (__perf_guest_handle_intel_pt_intr)(); }extern void perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs);extern void perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *cbs);
kernel/events/core.c +15 -0
@@ -6524,12 +6524,23 @@ static void perf_pending_event(struct irq_work *entry)#ifdef CONFIG_GUEST_PERF_EVENTS struct perf_guest_info_callbacks __rcu *perf_guest_cbs; +DEFINE_STATIC_CALL_RET0 (__perf_guest_state, *perf_guest_cbs->state); +DEFINE_STATIC_CALL_RET0 (__perf_guest_get_ip, *perf_guest_cbs->get_ip); +DEFINE_STATIC_CALL_RET0 (__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr); +void perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs) {if (WARN_ON_ONCE (rcu_access_pointer (perf_guest_cbs)))return ;rcu_assign_pointer (perf_guest_cbs, cbs); +static_call_update (__perf_guest_state, cbs->state); +static_call_update (__perf_guest_get_ip, cbs->get_ip); + +/* Implementing ->handle_intel_pt_intr is optional. */ +if (cbs->handle_intel_pt_intr) +static_call_update (__perf_guest_handle_intel_pt_intr, + cbs->handle_intel_pt_intr); }EXPORT_SYMBOL_GPL (perf_register_guest_info_callbacks);
@@ -6539,6 +6550,10 @@ void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)return ;rcu_assign_pointer (perf_guest_cbs,NULL ); +static_call_update (__perf_guest_state, (void *)&__static_call_return0); +static_call_update (__perf_guest_get_ip, (void *)&__static_call_return0); +static_call_update (__perf_guest_handle_intel_pt_intr, + (void *)&__static_call_return0);synchronize_rcu (); }EXPORT_SYMBOL_GPL (perf_unregister_guest_info_callbacks);
Signature · 6 lines
2.34.0.rc0.344.g81b53c2807-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
[tip: perf/core] perf/core: Use static_call to optimize perf_guest_info_callbacks
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 87b940a0675e25261f022ac3e53e0dfff9cdb995 Gitweb: https://git.kernel.org/tip/87b940a0675e25261f022ac3e53e0dfff9cdb995 Author: Sean Christopherson <[email protected]> AuthorDate: Thu, 11 Nov 2021 02:07:30 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Nov 2021 14:49:09 +01:00
perf/core: Use static_call to optimize perf_guest_info_callbacks
Use static_call to optimize perf's guest callbacks on arm64 and x86, which are now the only architectures that define the callbacks. Use DEFINE_STATIC_CALL_RET0 as the default/NULL for all guest callbacks, as the callback semantics are that a return value '0' means "not in guest".
static_call obviously avoids the overhead of CONFIG_RETPOLINE=y, but is also advantageous versus other solutions, e.g. per-cpu callbacks, in that a per-cpu memory load is not needed to detect the !guest case.
Based on code from Peter and Like.
--- include/linux/perf_event.h | 34 ++++++++-------------------------- kernel/events/core.c | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 26 deletions(-)
include/linux/perf_event.h +8 -26
@@ -1244,40 +1244,22 @@ extern void perf_event_bpf_event(struct bpf_prog *prog,#ifdef CONFIG_GUEST_PERF_EVENTS extern struct perf_guest_info_callbacks __rcu *perf_guest_cbs; -static inline struct perf_guest_info_callbacks *perf_get_guest_cbs (void ) -{ -/* -* Callbacks are RCU-protected and must be READ_ONCE to avoid reloading -* the callbacks between a !NULL check and dereferences, to ensure -* pending stores/changes to the callback pointers are visible before a -* non-NULL perf_guest_cbs is visible to readers, and to prevent a -* module from unloading callbacks while readers are active. -*/ -return rcu_dereference (perf_guest_cbs); -} + +DECLARE_STATIC_CALL (__perf_guest_state, *perf_guest_cbs->state); +DECLARE_STATIC_CALL (__perf_guest_get_ip, *perf_guest_cbs->get_ip); +DECLARE_STATIC_CALL (__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr); +static inline unsigned int perf_guest_state (void ) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); - -return guest_cbs ? guest_cbs->state () :0 ; +return static_call (__perf_guest_state)(); }static inline unsigned long perf_guest_get_ip (void ) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); - -/* -* Arbitrarily return '0' in the unlikely scenario that the callbacks -* are unregistered between checking guest state and getting the IP. -*/ -return guest_cbs ? guest_cbs->get_ip () :0 ; +return static_call (__perf_guest_get_ip)(); }static inline unsigned int perf_guest_handle_intel_pt_intr (void ) { -struct perf_guest_info_callbacks *guest_cbs =perf_get_guest_cbs (); - -if (guest_cbs && guest_cbs->handle_intel_pt_intr) -return guest_cbs->handle_intel_pt_intr (); -return 0 ; +return static_call (__perf_guest_handle_intel_pt_intr)(); }extern void perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs);extern void perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *cbs);
kernel/events/core.c +15 -0
@@ -6524,12 +6524,23 @@ static void perf_pending_event(struct irq_work *entry)#ifdef CONFIG_GUEST_PERF_EVENTS struct perf_guest_info_callbacks __rcu *perf_guest_cbs; +DEFINE_STATIC_CALL_RET0 (__perf_guest_state, *perf_guest_cbs->state); +DEFINE_STATIC_CALL_RET0 (__perf_guest_get_ip, *perf_guest_cbs->get_ip); +DEFINE_STATIC_CALL_RET0 (__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr); +void perf_register_guest_info_callbacks (struct perf_guest_info_callbacks *cbs) {if (WARN_ON_ONCE (rcu_access_pointer (perf_guest_cbs)))return ;rcu_assign_pointer (perf_guest_cbs, cbs); +static_call_update (__perf_guest_state, cbs->state); +static_call_update (__perf_guest_get_ip, cbs->get_ip); + +/* Implementing ->handle_intel_pt_intr is optional. */ +if (cbs->handle_intel_pt_intr) +static_call_update (__perf_guest_handle_intel_pt_intr, + cbs->handle_intel_pt_intr); }EXPORT_SYMBOL_GPL (perf_register_guest_info_callbacks);
@@ -6539,6 +6550,10 @@ void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)return ;rcu_assign_pointer (perf_guest_cbs,NULL ); +static_call_update (__perf_guest_state, (void *)&__static_call_return0); +static_call_update (__perf_guest_get_ip, (void *)&__static_call_return0); +static_call_update (__perf_guest_handle_intel_pt_intr, + (void *)&__static_call_return0);synchronize_rcu (); }EXPORT_SYMBOL_GPL (perf_unregister_guest_info_callbacks);
Sean Christopherson
maintainerRe: [PATCH v4 09/17] perf/core: Use static_call to optimize perf_guest_info_callbacks
In reply to Sean Christopherson+Will and Sami, -most everyone else
Quoted 16 lines · Sean Christopherson
Use
static_callto optimize perf's guest callbacks on arm64 and x86, which are now the only architectures that define the callbacks. UseDEFINE_STATIC_CALL_RET0as the default/NULL for all guest callbacks, as the callback semantics are that a return value '0' means "not in guest".
static_callobviously avoids the overhead ofCONFIG_RETPOLINE=y, but is also advantageous versus other solutions, e.g. per-cpu callbacks, in that a per-cpu memory load is not needed to detect the !guest case.Based on code from Peter and Like.
Suggested-by Peter Zijlstra (Intel) <[email protected]>Cc Like Xu <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>Signed-off-by Sean Christopherson <[email protected]>---
...
kernel/events/core.c static void perf_pending_event(struct irq_work *entry) · line 6529
Quoted 11 lines
kernel/events/core.c +3
-0@@ -6524,12 +6524,23 @@ static void perf_pending_event(struct irq_work *entry)#ifdef CONFIG_GUEST_PERF_EVENTS struct perf_guest_info_callbacks __rcu *perf_guest_cbs; +DEFINE_STATIC_CALL_RET0 (__perf_guest_state, *perf_guest_cbs->state); +DEFINE_STATIC_CALL_RET0 (__perf_guest_get_ip, *perf_guest_cbs->get_ip); +DEFINE_STATIC_CALL_RET0 (__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr);
Using __static_call_return0() makes clang's CFI sad on arm64 due to the resulting function prototype mistmatch, which IIUC, is verified by clang's __cfi_check() for indirect calls, i.e. architectures without CONFIG_HAVE_STATIC_CALL.
We could fudge around the issue by using stubs, massaging prototypes, etc..., but that means doing that for every arch-agnostic user of __static_call_return0().
Any clever ideas? Can we do something like generate a unique function for every DEFINE_STATIC_CALL_RET0 for CONFIG_HAVE_STATIC_CALL=n, e.g. using typeof() to get the prototype?
Kernel panic - not syncing: CFI failure (target: __static_call_return0+0x0/0x8) CPU: 0 PID: 1625 Comm: batterystats-wo Tainted: G W OE 5.16.0-mainline #1$ Hardware name: Raven EVT 1.1 (DT)$ Call trace:$ dump_backtrace+0xf0/0x130$ show_stack+0x1c/0x2c$ dump_stack_lvl+0x68/0x98$ panic+0x168/0x420$ __cfi_check_fail+0x58/0x5c$ __cfi_slowpath_diag+0x150/0x1a4$ perf_misc_flags+0x74/0xa4$ perf_prepare_sample+0x50/0x44c$ perf_event_output_forward+0x5c/0xcc$ __perf_event_overflow+0xc8/0x188$ perf_swevent_event+0x7c/0x10c$ perf_tp_event+0x168/0x298$ perf_trace_run_bpf_submit+0x8c/0xdc$ perf_trace_sched_switch+0x180/0x1cc$ __schedule+0x850/0x924$ schedule+0x98/0xe0$ binder_wait_for_work+0x158/0x368$ binder_thread_read+0x278/0x243c$ binder_ioctl_write_read+0x120/0x45c$ binder_ioctl+0x1ac/0xc34$ __arm64_sys_ioctl+0xa8/0x118$ invoke_syscall+0x64/0x178$ el0_svc_common+0x8c/0x100$ do_el0_svc+0x28/0xa0$ el0_svc+0x24/0x84$ el0t_64_sync_handler+0x88/0xec$ el0t_64_sync+0x1b4/0x1b8$
Re: [PATCH v4 09/17] perf/core: Use static_call to optimize perf_guest_info_callbacks
Quoted 8 lines · Sean Christopherson
kernel/events/core.c static void perf_pending_event(struct irq_work *entry) · line 6529
Quoted 3 lines
kernel/events/core.c +3
-0+DEFINE_STATIC_CALL_RET0 (__perf_guest_state, *perf_guest_cbs->state); +DEFINE_STATIC_CALL_RET0 (__perf_guest_get_ip, *perf_guest_cbs->get_ip); +DEFINE_STATIC_CALL_RET0 (__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr);Using
__static_call_return0()makes clang's CFI sad on arm64 due to the resulting function prototype mistmatch, which IIUC, is verified by clang's__cfi_check()for indirect calls, i.e. architectures withoutCONFIG_HAVE_STATIC_CALL.
Quoted 6 lines
We could fudge around the issue by using stubs, massaging prototypes, etc..., but that means doing that for every arch-agnostic user of
__static_call_return0().Any clever ideas? Can we do something like generate a unique function for every
DEFINE_STATIC_CALL_RET0forCONFIG_HAVE_STATIC_CALL=n, e.g. usingtypeof()to get the prototype?
I'm not sure there's a clever fix for this. On architectures without HAVE_STATIC_CALL, this is an indirect call to a function with a mismatching type, which CFI is intended to catch.
The obvious way to solve the problem would be to use a stub function with the correct type, which I agree, isn't going to scale. You can alternatively check if .func points to __static_call_return0 and not make the indirect call if it does. If neither of these options are feasible, you can disable CFI checking in the functions that have these static calls using the __nocfi attribute.
Kees, any thoughts?
Sami
Peter Zijlstra
maintainerRe: [PATCH v4 09/17] perf/core: Use static_call to optimize perf_guest_info_callbacks
Quoted 20 lines · Sami Tolvanen
Quoted 8 lines · Sean Christopherson
kernel/events/core.c static void perf_pending_event(struct irq_work *entry) · line 6529
Quoted 3 lines
kernel/events/core.c +3
-0+DEFINE_STATIC_CALL_RET0 (__perf_guest_state, *perf_guest_cbs->state); +DEFINE_STATIC_CALL_RET0 (__perf_guest_get_ip, *perf_guest_cbs->get_ip); +DEFINE_STATIC_CALL_RET0 (__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr);Using
__static_call_return0()makes clang's CFI sad on arm64 due to the resulting function prototype mistmatch, which IIUC, is verified by clang's__cfi_check()for indirect calls, i.e. architectures withoutCONFIG_HAVE_STATIC_CALL.Quoted 6 lines
We could fudge around the issue by using stubs, massaging prototypes, etc..., but that means doing that for every arch-agnostic user of
__static_call_return0().Any clever ideas? Can we do something like generate a unique function for every
DEFINE_STATIC_CALL_RET0forCONFIG_HAVE_STATIC_CALL=n, e.g. usingtypeof()to get the prototype?I'm not sure there's a clever fix for this. On architectures without
HAVE_STATIC_CALL, this is an indirect call to a function with a mismatching type, which CFI is intended to catch.
Quoted 6 lines
The obvious way to solve the problem would be to use a stub function with the correct type, which I agree, isn't going to scale. You can alternatively check if .func points to
__static_call_return0and not make the indirect call if it does. If neither of these options are feasible, you can disable CFI checking in the functions that have these static calls using the__nocfiattribute.
There's also the tp_stub_func() thing that does basically the same thing.
Re: [PATCH v4 09/17] perf/core: Use static_call to optimize perf_guest_info_callbacks
In reply to Sami TolvanenQuoted 20 lines · Sami Tolvanen
Quoted 8 lines · Sean Christopherson
kernel/events/core.c static void perf_pending_event(struct irq_work *entry) · line 6529
Quoted 3 lines
kernel/events/core.c +3
-0+DEFINE_STATIC_CALL_RET0 (__perf_guest_state, *perf_guest_cbs->state); +DEFINE_STATIC_CALL_RET0 (__perf_guest_get_ip, *perf_guest_cbs->get_ip); +DEFINE_STATIC_CALL_RET0 (__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr);Using
__static_call_return0()makes clang's CFI sad on arm64 due to the resulting function prototype mistmatch, which IIUC, is verified by clang's__cfi_check()for indirect calls, i.e. architectures withoutCONFIG_HAVE_STATIC_CALL.Quoted 6 lines
We could fudge around the issue by using stubs, massaging prototypes, etc..., but that means doing that for every arch-agnostic user of
__static_call_return0().Any clever ideas? Can we do something like generate a unique function for every
DEFINE_STATIC_CALL_RET0forCONFIG_HAVE_STATIC_CALL=n, e.g. usingtypeof()to get the prototype?I'm not sure there's a clever fix for this. On architectures without
HAVE_STATIC_CALL, this is an indirect call to a function with a mismatching type, which CFI is intended to catch.
Quoted 8 lines
The obvious way to solve the problem would be to use a stub function with the correct type, which I agree, isn't going to scale. You can alternatively check if .func points to
__static_call_return0and not make the indirect call if it does. If neither of these options are feasible, you can disable CFI checking in the functions that have these static calls using the__nocfiattribute.Kees, any thoughts?
I'm digging through the macros to sort this out, but IIUC, an example of the problem is:
perf_guest_cbs->handle_intel_pt_intr is:
unsigned int (*handle_intel_pt_intr)(void);
The declaration for this starts with:
DECLARE_STATIC_CALL(__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr);
which produces:
extern struct static_call_key STATIC_CALL_KEY(__perf_guest_handle_intel_pt_intr);
extern typeof(*perf_guest_cbs->handle_intel_pt_intr) STATIC_CALL_TRAMP(__perf_guest_handle_intel_pt_intr);
and the last line becomes:
extern unsigned int (*__SCT____perf_guest_handle_intel_pt_intr)(void);
with !HAVE_STATIC_CALL, when perf_guest_handle_intel_pt_intr() does:
return static_call(__perf_guest_handle_intel_pt_intr)();
it is resolving static_call() into:
((typeof(STATIC_CALL_TRAMP(name))*)(STATIC_CALL_KEY(name).func))
so the caller is expecting "unsigned int (*)(void)" but the prototype of __static_call_return0 is "long (*)(void)":
long __static_call_return0(void);
Could we simply declare a type-matched ret0 trampoline too?
#define STATIC_CALL_TRAMP_RET0_PREFIX __SCT__ret0__
#define STATIC_CALL_TRAMP_RET0(name) __PASTE(STATIC_CALL_TRAMP_RET0_PREFIX, name)
#define DEFINE_STATIC_CALL_RET0(name, _func) \
static typeof(_func) STATIC_CALL_TRAMP_RET0(name) { return 0; } \
__DEFINE_STATIC_CALL(name, _func, STATIC_CALL_TRAMP_RET0(name));static_call_update(__perf_guest_handle_intel_pt_intr,
(void *)&STATIC_CALL_TRAMP_RET0(__perf_guest_handle_intel_pt_intr))
Signature · 5 lines
Kees Cook _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Peter Zijlstra
maintainerRe: [PATCH v4 09/17] perf/core: Use static_call to optimize perf_guest_info_callbacks
Quoted 10 lines · Kees Cook
I'm digging through the macros to sort this out, but IIUC, an example of the problem is:
so the caller is expecting "unsigned int (*)(void)" but the prototype of
__static_call_return0is "long (*)(void)":long
__static_call_return0(void);Could we simply declare a type-matched ret0 trampoline too?
That'll work for this case, but the next case the function will have arguments we'll need even more nonsense...
And as stated in that other email, there's tb_stub_func() having the exact same problem as well.
The x86_64 CFI patches had a work-around for this, that could trivially be lifted I suppose.
Re: [PATCH v4 09/17] perf/core: Use static_call to optimize perf_guest_info_callbacks
Quoted 9 lines · Peter Zijlstra
Quoted 7 lines · Kees Cook
I'm digging through the macros to sort this out, but IIUC, an example of the problem is:
so the caller is expecting "unsigned int (*)(void)" but the prototype of
__static_call_return0is "long (*)(void)":
Quoted 6 lines
Quoted 3 lines
long
__static_call_return0(void);Could we simply declare a type-matched ret0 trampoline too?
That'll work for this case, but the next case the function will have arguments we'll need even more nonsense...
Shouldn't the typeof() work there too, though? I.e. as long as the return value can hold a "0", it'd work.
Quoted 2 lines
And as stated in that other email, there's
tb_stub_func()having the exact same problem as well.
Yeah, I'd need to go look at that again.
Quoted 2 lines
The
x86_64CFI patches had a work-around for this, that could trivially be lifted I suppose.
Yeah, I think it'd be similar. I haven't had a chance to go look at that again...
Signature · 5 lines
Kees Cook _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v4 09/17] perf/core: Use static_call to optimize perf_guest_info_callbacks
Quoted 26 lines · Kees Cook
Quoted 9 lines · Peter Zijlstra
Quoted 7 lines · Kees Cook
I'm digging through the macros to sort this out, but IIUC, an example of the problem is:
so the caller is expecting "unsigned int (*)(void)" but the prototype of
__static_call_return0is "long (*)(void)":Quoted 6 lines
Quoted 3 lines
long
__static_call_return0(void);Could we simply declare a type-matched ret0 trampoline too?
That'll work for this case, but the next case the function will have arguments we'll need even more nonsense...
Shouldn't the
typeof()work there too, though? I.e. as long as the return value can hold a "0", it'd work.Quoted 2 lines
And as stated in that other email, there's
tb_stub_func()having the exact same problem as well.Yeah, I'd need to go look at that again.
Quoted 8 lines
Quoted 2 lines
The
x86_64CFI patches had a work-around for this, that could trivially be lifted I suppose.Yeah, I think it'd be similar. I haven't had a chance to go look at that again...
--
Kees Cook
Hi All,
I noticed that this thread kind of died out. Does anyone have any action items that need to be followed up to help resolve this issue? The offending patch is breaking the ARM64 Android Common Kernels (Pixel 6 in particular) and I imagine it might also have an impact on other ARM64 platforms upstream that just haven't hit this perf_trace case yet.
If anyone needs help verifying any fixes, I'm happy to test it out.
Thanks,
Will
Sean Christopherson
maintainerRe: [PATCH v4 09/17] perf/core: Use static_call to optimize perf_guest_info_callbacks
Quoted 25 lines · Will McVicker
Quoted 24 lines · Kees Cook
Quoted 9 lines · Peter Zijlstra
Quoted 7 lines · Kees Cook
I'm digging through the macros to sort this out, but IIUC, an example of the problem is:
so the caller is expecting "unsigned int (*)(void)" but the prototype of
__static_call_return0is "long (*)(void)":Quoted 6 lines
Quoted 3 lines
long
__static_call_return0(void);Could we simply declare a type-matched ret0 trampoline too?
That'll work for this case, but the next case the function will have arguments we'll need even more nonsense...
Shouldn't the
typeof()work there too, though? I.e. as long as the return value can hold a "0", it'd work.Quoted 2 lines
And as stated in that other email, there's
tb_stub_func()having the exact same problem as well.
Quoted 7 lines
Quoted 7 lines
Yeah, I'd need to go look at that again.
Quoted 2 lines
The
x86_64CFI patches had a work-around for this, that could trivially be lifted I suppose.Yeah, I think it'd be similar. I haven't had a chance to go look at that again...
Peter and/or Kees, can you provide a pointer to the patches that could potentially be used as a basis for fixing ARM CFI? Or even better, send a patch to actually fix this? :-)
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v4 09/17] perf/core: Use static_call to optimize perf_guest_info_callbacks
In reply to Kees CookQuoted 12 lines · Kees Cook
Quoted 11 lines · Peter Zijlstra
Quoted 9 lines · Kees Cook
I'm digging through the macros to sort this out, but IIUC, an example of the problem is:
so the caller is expecting "unsigned int (*)(void)" but the prototype of
__static_call_return0is "long (*)(void)":long
__static_call_return0(void);
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Could we simply declare a type-matched ret0 trampoline too?
That'll work for this case, but the next case the function will have arguments we'll need even more nonsense...
Shouldn't the
typeof()work there too, though? I.e. as long as the return value can hold a "0", it'd work.
I gave this a shot but then hit a wall with the arguments indeed:
typedef int (perf_snapshot_branch_stack_t)(struct perf_branch_entry *entries,
unsigned int cnt);
[...]DEFINE_STATIC_CALL_RET0(perf_snapshot_branch_stack, perf_snapshot_branch_stack_t);
I can generate a stub with the matching return type using typeof() but the arguments have to be fixed e.g. to (void):
#define DEFINE_STATIC_CALL_RET0(name, _func) \
static inline typeof(((typeof(_func)*)0)()) \
__static_call_ret0_##name(void) { return 0; } \
__DEFINE_STATIC_CALL(name, _func, __static_call_ret0_##name)I believe this would work for most perf callbacks cases except the one above because the arguments would generate a different hash for CFI.
Quoted 5 lines
Quoted 3 lines
And as stated in that other email, there's
tb_stub_func()having the exact same problem as well.Yeah, I'd need to go look at that again.
Is this testing for "_func == __static_call_return0" in static_call()? Ok, but I don't understand how to handle the arguments here either. The call sites do "static_call(name)(...)", and I don't see a way to handle this using macro magic.
Quoted 7 lines
Quoted 3 lines
The
x86_64CFI patches had a work-around for this, that could trivially be lifted I suppose.Yeah, I think it'd be similar. I haven't had a chance to go look at that again...
What is this work-around for x86?
Downstream I had to resolve this my providing individual stubs for each DEFINE_STATIC_CALL_RET0() :( If you care to see my hack:
https://android-review.googlesource.com/c/kernel/common/+/3980171
I don't have a clue on how to fix this properly though. Any ideas?
--
Carlos Llamas
Re: [PATCH v4 09/17] perf/core: Use static_call to optimize perf_guest_info_callbacks
Hi Carlos,
Quoted 31 lines · Carlos Llamas
Quoted 12 lines · Kees Cook
Quoted 11 lines · Peter Zijlstra
Quoted 9 lines · Kees Cook
I'm digging through the macros to sort this out, but IIUC, an example of the problem is:
so the caller is expecting "unsigned int (*)(void)" but the prototype of
__static_call_return0is "long (*)(void)":long
__static_call_return0(void);Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Could we simply declare a type-matched ret0 trampoline too?
That'll work for this case, but the next case the function will have arguments we'll need even more nonsense...
Shouldn't the
typeof()work there too, though? I.e. as long as the return value can hold a "0", it'd work.I gave this a shot but then hit a wall with the arguments indeed:
typedef int (perf_snapshot_branch_stack_t)(struct
perf_branch_entry*entries,unsigned int cnt);[...]
DEFINE_STATIC_CALL_RET0(perf_snapshot_branch_stack, perf_snapshot_branch_stack_t);I can generate a stub with the matching return type using
typeof()but the arguments have to be fixed e.g. to (void):
Quoted 7 lines
#define DEFINE_STATIC_CALL_RET0(name, _func) \ static inline typeof(((typeof(_func)*)0)()) \ __static_call_ret0_##name(void) { return 0; } \ __DEFINE_STATIC_CALL(name, _func, __static_call_ret0_##name)I believe this would work for most perf callbacks cases except the one above because the arguments would generate a different hash for CFI.
If you need a stub with a matching CFI type for an arbitrary function, perhaps you can do something like this (arm64, untested):
#define DEFINE_TYPED_STUB_RET0(name, reffunc) \ typeof(reffunc) name; \ __ADDRESSABLE(name); \ asm( \ " " __ALIGN_STR " \n" \ " .4byte __kcfi_typeid_" #name " \n" \ #name ": \n" \ " bti c \n" \ " mov x0, xzr \n" \ " ret " \ )
For the !CONFIG_CFI case, or architectures that implement static calls, you can probably just point the stub to __static_call_return0:
#define DEFINE_TYPED_STUB_RET0(name, reffunc) \
typeof(reffunc) name __asm__("__static_call_return0")Sami
Re: [PATCH v4 09/17] perf/core: Use static_call to optimize perf_guest_info_callbacks
Quoted 17 lines · Sami Tolvanen
Hi Carlos,
If you need a stub with a matching CFI type for an arbitrary function, perhaps you can do something like this (arm64, untested):
#define DEFINE_TYPED_STUB_RET0(name, reffunc) \ typeof(reffunc) name; \ __ADDRESSABLE(name); \ asm( \ " " __ALIGN_STR " \n" \ " .4byte __kcfi_typeid_" #name " \n" \ #name ": \n" \ " bti c \n" \ " mov x0, xzr \n" \ " ret " \ )
Quoted 7 lines
For the !CONFIG_CFI case, or architectures that implement static calls, you can probably just point the stub to
__static_call_return0:#define DEFINE_TYPED_STUB_RET0(name, reffunc) \ typeof(reffunc) name __asm__("__static_call_return0")Sami
Oh that is pretty cool it worked for me. This is the patch I tested with. Still needs a bit more plumbing but no more CFI issues.
---
arch/arm64/include/asm/linkage.h +3 -1
@@ -5,8 +5,10 @@#include <asm/assembler.h> #endif +#include <linux/stringify.h> +#define __ALIGN .balign CONFIG_FUNCTION_ALIGNMENT -#define __ALIGN_STR ".balign " #CONFIG_FUNCTION_ALIGNMENT +#define __ALIGN_STR __stringify(__ALIGN)/* * When using in-kernel BTI we need to ensure that PCS-conformant
include/linux/static_call.h +24 -1
@@ -184,6 +184,9 @@ extern int static_call_text_reserved(void *start, void *end);extern long __static_call_return0 (void ); +#define static_call_update_stub (name) \ + static_call_update(name, __static_call_return0) +#define DEFINE_STATIC_CALL (name, _func) \ DECLARE_STATIC_CALL(name, _func); \ struct static_call_key STATIC_CALL_KEY(name) = { \
@@ -270,6 +273,9 @@ static inline int static_call_text_reserved(void *start, void *end)extern long __static_call_return0 (void ); +#define static_call_update_stub (name) \ + static_call_update(name, __static_call_return0) +#define EXPORT_STATIC_CALL (name) \ EXPORT_SYMBOL(STATIC_CALL_KEY(name)); \ EXPORT_SYMBOL(STATIC_CALL_TRAMP(name))
@@ -306,8 +312,25 @@ static inline long __static_call_return0(void)#define DEFINE_STATIC_CALL_NULL (name, _func) \ __DEFINE_STATIC_CALL(name, _func, NULL) +/* Move somewhere under arch/arm64/... */ +#define ARCH_DEFINE_TYPED_STUB_RET0 (name, reffunc) \ + typeof(reffunc) name; \ + __ADDRESSABLE(name); \ + asm( \ + " " __ALIGN_STR "\n" \ + " .4byte __kcfi_typeid_" #name "\n" \ + #name ":\n" \ + " bti c\n" \ + " mov x0, xzr\n" \ + " ret" \ + ); +#define DEFINE_STATIC_CALL_RET0 (name, _func) \ - __DEFINE_STATIC_CALL(name, _func, __static_call_return0) + ARCH_DEFINE_TYPED_STUB_RET0(__static_call_##name, _func) \ + __DEFINE_STATIC_CALL(name, _func, __static_call_##name) + +#define static_call_update_stub (name) \ + static_call_update(name, __static_call_##name)static inline void __static_call_nop (void ) { }
kernel/events/core.c +4 -4
@@ -7701,10 +7701,10 @@ void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)return ;rcu_assign_pointer (perf_guest_cbs,NULL ); -static_call_update (__perf_guest_state, (void *)&__static_call_return0); -static_call_update (__perf_guest_get_ip, (void *)&__static_call_return0); -static_call_update (__perf_guest_handle_intel_pt_intr, (void *)&__static_call_return0); -static_call_update (__perf_guest_handle_mediated_pmi, (void *)&__static_call_return0); +static_call_update_stub (__perf_guest_state); +static_call_update_stub (__perf_guest_get_ip); +static_call_update_stub (__perf_guest_handle_intel_pt_intr); +static_call_update_stub (__perf_guest_handle_mediated_pmi);synchronize_rcu (); }EXPORT_SYMBOL_GPL (perf_unregister_guest_info_callbacks);
PATCH 1/1 static_call: use CFI-compliant return0 stubs
In reply to Sami TolvanenArchitectures with !HAVE_STATIC_CALL (such as arm64) rely on the generic static_call implementation via indirect calls. In particular, users of DEFINE_STATIC_CALL_RET0, default to the generic __static_call_return0 stub to optimize the unset path.
However, __static_call_return0 has a fixed signature of "long (*)(void)" which may not match the expected prototype at callsites. This triggers CFI failures when CONFIG_CFI is enabled. A trivial linux-perf command does it:
$ perf record -a sleep 1 CFI failure at perf_prepare_sample+0x98/0x7f8 (target: __static_call_return0+0x0/0x10; expected type: 0x837de525) Internal error: Oops - CFI: 00000000f2008228 [#1] SMP Modules linked in: CPU: 0 UID: 0 PID: 638 Comm: perf Not tainted 7.0.0-rc3 #25 PREEMPT Hardware name: linux,dummy-virt (DT) pstate: 900000c5 (NzcV daIF -PAN -UAO -TCO -DIT -SSBS BTYPE=--) pc : perf_prepare_sample+0x98/0x7f8 lr : perf_prepare_sample+0x70/0x7f8 sp : ffff80008289bc20 x29: ffff80008289bc30 x28: 000000000000001f x27: 0000000000000018 x26: 0000000000000100 x25: ffffffffffffffff x24: 0000000000000000 x23: 0000000000010187 x22: ffff8000851eba40 x21: 0000000000010087 x20: ffff0000098c9ea0 x19: ffff80008289bdc0 x18: 0000000000000000 x17: 00000000837de525 x16: 0000000072923c8f x15: 7fffffffffffffff x14: 00007fffffffffff x13: 00000000ffffffea x12: 0000000000000000 x11: 0000000000000015 x10: 0000000000000000 x9 : ffff8000822f2240 x8 : ffff800080276e4c x7 : 0000000000000000 x6 : 0000000000000000 x5 : 0000000000000000 x4 : ffff8000851eba10 x3 : ffff8000851eba40 x2 : ffff8000822f2240 x1 : 0000000000000000 x0 : 00000009d377c3a0 Call trace: perf_prepare_sample+0x98/0x7f8 (P) perf_event_output_forward+0x5c/0x17c __perf_event_overflow+0x2fc/0x460 perf_event_overflow+0x1c/0x28 armv8pmu_handle_irq+0x134/0x210 [...]
To fix this, let architectures provide an ARCH_DEFINE_TYPED_STUB_RET0 implementation that generates individual signature-matching stubs for users of DEFINE_STATIC_CALL_RET0. This ensures the CFI hash of the target call matches that of the callsite.
--- arch/Kconfig | 4 ++++ arch/arm64/Kconfig | 1 + arch/arm64/include/asm/linkage.h | 3 ++- arch/arm64/include/asm/static_call.h | 23 +++++++++++++++++++++++ include/linux/static_call.h | 19 ++++++++++++++++++- kernel/events/core.c | 11 +++++++---- kernel/sched/core.c | 4 ++-- 7 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 arch/arm64/include/asm/static_call.h
arch/Kconfig +4 -0
@@ -1678,6 +1678,10 @@ config HAVE_STATIC_CALL_INLINE depends on HAVE_STATIC_CALL select OBJTOOL +config HAVE_STATIC_CALL_TYPED_STUBS + bool + depends on !HAVE_STATIC_CALL + config HAVE_PREEMPT_DYNAMIC bool
arch/arm64/Kconfig +1 -0
@@ -252,6 +252,7 @@ config ARM64 select HAVE_RSEQ select HAVE_RUST if RUSTC_SUPPORTS_ARM64 select HAVE_STACKPROTECTOR + select HAVE_STATIC_CALL_TYPED_STUBS if CFI select HAVE_SYSCALL_TRACEPOINTS select HAVE_KPROBES select HAVE_KRETPROBES
arch/arm64/include/asm/linkage.h +2 -1
@@ -4,9 +4,10 @@#ifdef __ASSEMBLER__#include <asm/assembler.h> #endif +#include <linux/stringify.h> #define __ALIGN .balign CONFIG_FUNCTION_ALIGNMENT -#define __ALIGN_STR ".balign " #CONFIG_FUNCTION_ALIGNMENT +#define __ALIGN_STR __stringify(__ALIGN)/* * When using in-kernel BTI we need to ensure that PCS-conformant
arch/arm64/include/asm/static_call.h new file +23 -0
@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_ARM64_STATIC_CALL_H +#define _ASM_ARM64_STATIC_CALL_H + +#include <linux/compiler.h> +#include <asm/linkage.h> + +/* Generates a CFI-compliant "return 0" stub matching @reffunc signature */ +#define __ARCH_DEFINE_TYPED_STUB_RET0 (name, reffunc) \ + typeof(reffunc) name; \ + __ADDRESSABLE(name); \ + asm( \ + " " __ALIGN_STR " \n" \ + " .4byte __kcfi_typeid_" #name "\n" \ + #name ": \n" \ + " bti c \n" \ + " mov x0, xzr \n" \ + " ret" \ + ); +#define ARCH_DEFINE_TYPED_STUB_RET0 (name, reffunc) \ + __ARCH_DEFINE_TYPED_STUB_RET0(name, reffunc) + +#endif /* _ASM_ARM64_STATIC_CALL_H */
include/linux/static_call.h +18 -1
@@ -184,6 +184,8 @@ extern int static_call_text_reserved(void *start, void *end);extern long __static_call_return0 (void ); +#define STATIC_CALL_STUB_RET0 (...) ((void *)&__static_call_return0) +#define DEFINE_STATIC_CALL (name, _func) \ DECLARE_STATIC_CALL(name, _func); \ struct static_call_key STATIC_CALL_KEY(name) = { \
@@ -270,6 +272,8 @@ static inline int static_call_text_reserved(void *start, void *end)extern long __static_call_return0 (void ); +#define STATIC_CALL_STUB_RET0 (...) ((void *)&__static_call_return0) +#define EXPORT_STATIC_CALL (name) \ EXPORT_SYMBOL(STATIC_CALL_KEY(name)); \ EXPORT_SYMBOL(STATIC_CALL_TRAMP(name))
@@ -294,6 +298,18 @@ static inline long __static_call_return0(void)return 0 ; } +#ifdef CONFIG_HAVE_STATIC_CALL_TYPED_STUBS +#include <asm/static_call.h> + +#define STATIC_CALL_STUB_RET0 (name) __static_call_##name +#define DEFINE_STATIC_CALL_STUB_RET0 (name, _func) \ + ARCH_DEFINE_TYPED_STUB_RET0(STATIC_CALL_STUB_RET0(name), _func) +#else +/* Fall back to the generic __static_call_return0 stub */ +#define STATIC_CALL_STUB_RET0 (...) ((void *)&__static_call_return0) +#define DEFINE_STATIC_CALL_STUB_RET0 (...) +#endif +#define __DEFINE_STATIC_CALL (name, _func, _func_init) \ DECLARE_STATIC_CALL(name, _func); \ struct static_call_key STATIC_CALL_KEY(name) = { \
@@ -307,7 +323,8 @@ static inline long __static_call_return0(void) __DEFINE_STATIC_CALL(name, _func, NULL)#define DEFINE_STATIC_CALL_RET0 (name, _func) \ - __DEFINE_STATIC_CALL(name, _func, __static_call_return0) + DEFINE_STATIC_CALL_STUB_RET0(name, _func) \ + __DEFINE_STATIC_CALL(name, _func, STATIC_CALL_STUB_RET0(name))static inline void __static_call_nop (void ) { }
kernel/events/core.c +7 -4
@@ -7695,16 +7695,19 @@ void perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) }EXPORT_SYMBOL_GPL (perf_register_guest_info_callbacks); +#define static_call_disable (name) \ + static_call_update(name, STATIC_CALL_STUB_RET0(name)) +void perf_unregister_guest_info_callbacks (struct perf_guest_info_callbacks *cbs) {if (WARN_ON_ONCE (rcu_access_pointer (perf_guest_cbs) != cbs))return ;rcu_assign_pointer (perf_guest_cbs,NULL ); -static_call_update (__perf_guest_state, (void *)&__static_call_return0); -static_call_update (__perf_guest_get_ip, (void *)&__static_call_return0); -static_call_update (__perf_guest_handle_intel_pt_intr, (void *)&__static_call_return0); -static_call_update (__perf_guest_handle_mediated_pmi, (void *)&__static_call_return0); +static_call_disable (__perf_guest_state); +static_call_disable (__perf_guest_get_ip); +static_call_disable (__perf_guest_handle_intel_pt_intr); +static_call_disable (__perf_guest_handle_mediated_pmi);synchronize_rcu (); }EXPORT_SYMBOL_GPL (perf_unregister_guest_info_callbacks);
kernel/sched/core.c +2 -2
@@ -7443,12 +7443,12 @@ EXPORT_SYMBOL(__cond_resched);#ifdef CONFIG_PREEMPT_DYNAMIC # ifdef CONFIG_HAVE_PREEMPT_DYNAMIC_CALL # define cond_resched_dynamic_enabled __cond_resched -# define cond_resched_dynamic_disabled ((void *)&__static_call_return0) +# define cond_resched_dynamic_disabled STATIC_CALL_STUB_RET0(cond_resched)DEFINE_STATIC_CALL_RET0 (cond_resched, __cond_resched);EXPORT_STATIC_CALL_TRAMP (cond_resched);# define might_resched_dynamic_enabled __cond_resched -# define might_resched_dynamic_disabled ((void *)&__static_call_return0) +# define might_resched_dynamic_disabled STATIC_CALL_STUB_RET0(might_resched)DEFINE_STATIC_CALL_RET0 (might_resched, __cond_resched);EXPORT_STATIC_CALL_TRAMP (might_resched);# elif defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY )
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
Peter Zijlstra
maintainerRe: [PATCH] static_call: use CFI-compliant return0 stubs
Quoted 9 lines · Carlos Llamas
Architectures with !HAVE_STATIC_CALL (such as arm64) rely on the generic
static_callimplementation via indirect calls. In particular, users ofDEFINE_STATIC_CALL_RET0, default to the generic__static_call_return0stub to optimize the unset path.However,
__static_call_return0has a fixed signature of "long (*)(void)" which may not match the expected prototype at callsites. This triggers CFI failures whenCONFIG_CFIis enabled. A trivial linux-perf command does it:
*sigh*...
And ARM64 can't really do the inline thing because its immediate range is too small and it all turns into a mess constructing the address in a register and doing an indirect call anyway, right?
I'll stare at it in more detail tomorrow.
Re: [PATCH] static_call: use CFI-compliant return0 stubs
Quoted 11 lines · Peter Zijlstra
Quoted 9 lines · Carlos Llamas
Architectures with !HAVE_STATIC_CALL (such as arm64) rely on the generic
static_callimplementation via indirect calls. In particular, users ofDEFINE_STATIC_CALL_RET0, default to the generic__static_call_return0stub to optimize the unset path.However,
__static_call_return0has a fixed signature of "long (*)(void)" which may not match the expected prototype at callsites. This triggers CFI failures whenCONFIG_CFIis enabled. A trivial linux-perf command does it:
Quoted 6 lines
*sigh*...
And ARM64 can't really do the inline thing because its immediate range is too small and it all turns into a mess constructing the address in a register and doing an indirect call anyway, right?
Right, the range for the jump is very limited. I _think_ tracepoints have managed to implement the trampoline work-around:
arch/arm64/kernel/ftrace.c
So it looks do-able I think but a much complex route.
--
Carlos Llamas
Ard Biesheuvel
reviewerRe: [PATCH] static_call: use CFI-compliant return0 stubs
Hi Carlos,
You've cc'ed around 50 people on this patch, which is a bit excessive. Better to take get_maintainer.pl with a grain of salt if it proposes a cc list like that.
Quoted 19 lines · Carlos Llamas
Quoted 11 lines · Peter Zijlstra
Quoted 9 lines · Carlos Llamas
Architectures with !HAVE_STATIC_CALL (such as arm64) rely on the generic
static_callimplementation via indirect calls. In particular, users ofDEFINE_STATIC_CALL_RET0, default to the generic__static_call_return0stub to optimize the unset path.However,
__static_call_return0has a fixed signature of "long (*)(void)" which may not match the expected prototype at callsites. This triggers CFI failures whenCONFIG_CFIis enabled. A trivial linux-perf command does it:Quoted 6 lines
*sigh*...
And ARM64 can't really do the inline thing because its immediate range is too small and it all turns into a mess constructing the address in a register and doing an indirect call anyway, right?
Quoted 6 lines
Right, the range for the jump is very limited. I _think_ tracepoints have managed to implement the trampoline work-around:
arch/arm64/kernel/ftrace.cSo it looks do-able I think but a much complex route.
So far, we have managed to avoid the blessings of objtool on arm64, and the complexity associated with the inline patching is not really justified, given that on arm64, there is not really a need to avoid indirect calls (and as Peter says, we might end up with them anyway)
A while ago, I had a stab at implementing the out-of-line variety [0], but nobody cared enough to even respond. It is rather concise, and localised to arm64, so it is something we might consider for CONFIG_CFI builds. It is essentially the same sequence that arm64 uses for trampolines between modules and the kernel if they are out of direct branching range, with some .rodata patching to change the target. (arm64 basically only permits code patching without stopping the machine when it involves patching branch opcodes into NOPS or vice versa).
Doing so for only CONFIG_CFI makes sense because it removes the CFI overhead for all static calls, although it adds back some overhead for the trampoline. But there is currently no need to do this unconditionally.
[0] https://lore.kernel.org/linux-arm-kernel/[email protected]/
Peter Zijlstra
maintainerRe: [PATCH] static_call: use CFI-compliant return0 stubs
Quoted 15 lines · Ard Biesheuvel
So far, we have managed to avoid the blessings of objtool on arm64, and the complexity associated with the inline patching is not really justified, given that on arm64, there is not really a need to avoid indirect calls (and as Peter says, we might end up with them anyway)
A while ago, I had a stab at implementing the out-of-line variety [0], but nobody cared enough to even respond. It is rather concise, and localised to arm64, so it is something we might consider for
CONFIG_CFIbuilds. It is essentially the same sequence that arm64 uses for trampolines between modules and the kernel if they are out of direct branching range, with some .rodata patching to change the target. (arm64 basically only permits code patching without stopping the machine when it involves patching branch opcodes into NOPS or vice versa).
Quoted 4 lines
Doing so for only
CONFIG_CFImakes sense because it removes the CFI overhead for all static calls, although it adds back some overhead for the trampoline. But there is currently no need to do this unconditionally.
Right, so your v3 is very simple and straight forward, and should work as an end run around the CFI issue, by effectively doing that indirect tail call in the trampoline outside of the compiler generated software cfi things.
And I think I like your thing better because it handles all possible cases, not just the ret0 oddity and isn't in fact much larger.
Re: [PATCH] static_call: use CFI-compliant return0 stubs
Quoted 6 lines · Peter Zijlstra
Quoted 5 lines · Ard Biesheuvel
So far, we have managed to avoid the blessings of objtool on arm64, and the complexity associated with the inline patching is not really justified, given that on arm64, there is not really a need to avoid indirect calls (and as Peter says, we might end up with them anyway)
Quoted 9 lines
Quoted 9 lines
A while ago, I had a stab at implementing the out-of-line variety [0], but nobody cared enough to even respond. It is rather concise, and localised to arm64, so it is something we might consider for
CONFIG_CFIbuilds. It is essentially the same sequence that arm64 uses for trampolines between modules and the kernel if they are out of direct branching range, with some .rodata patching to change the target. (arm64 basically only permits code patching without stopping the machine when it involves patching branch opcodes into NOPS or vice versa).
Great! I'll go read your implementation then.
Quoted 5 lines
Quoted 4 lines
Doing so for only
CONFIG_CFImakes sense because it removes the CFI overhead for all static calls, although it adds back some overhead for the trampoline. But there is currently no need to do this unconditionally.
Quoted 7 lines
Right, so your v3 is very simple and straight forward, and should work as an end run around the CFI issue, by effectively doing that indirect tail call in the trampoline outside of the compiler generated software cfi things.
And I think I like your thing better because it handles all possible cases, not just the ret0 oddity and isn't in fact much larger.
SGTM, I'll switch over to testing Ard's patch.
Thanks,
Carlos Llamas
Re: [PATCH v4 09/17] perf/core: Use static_call to optimize perf_guest_info_callbacks
In reply to Sami TolvanenQuoted 17 lines · Sami Tolvanen
Hi Carlos,
If you need a stub with a matching CFI type for an arbitrary function, perhaps you can do something like this (arm64, untested):
#define DEFINE_TYPED_STUB_RET0(name, reffunc) \ typeof(reffunc) name; \ __ADDRESSABLE(name); \ asm( \ " " __ALIGN_STR " \n" \ " .4byte __kcfi_typeid_" #name " \n" \ #name ": \n" \ " bti c \n" \ " mov x0, xzr \n" \ " ret " \ )
Quoted 7 lines
For the !CONFIG_CFI case, or architectures that implement static calls, you can probably just point the stub to
__static_call_return0:#define DEFINE_TYPED_STUB_RET0(name, reffunc) \ typeof(reffunc) name __asm__("__static_call_return0")Sami
Alright, I've sent out a patch for this here:
https://lore.kernel.org/all/[email protected]/
It might seem "incomplete" because it only handles the RET0 case and it doesn't export the stubs either. However, I didn't find any static_calls for which the lack of these things would be an issue.
I can address such things in a v2 if really needed though.
Cheers,
--
Carlos Llamas
Sean Christopherson
maintainerPATCH v4 10/17 KVM: x86: Drop current_vcpu for kvm_running_vcpu + kvm_arch_vcpu variable
In reply to Sean ChristophersonUse the generic kvm_running_vcpu plus a new 'handling_intr_from_guest' variable in kvm_arch_vcpu instead of the semi-redundant current_vcpu. kvm_before/after_interrupt() must be called while the vCPU is loaded, (which protects against preemption), thus kvm_running_vcpu is guaranteed to be non-NULL when handling_intr_from_guest is non-zero.
Switching to kvm_get_running_vcpu() will allows moving KVM's perf callbacks to generic code, and the new flag will be used in a future patch to more precisely identify the "NMI from guest" case.
--- arch/x86/include/asm/kvm_host.h | 3 +-- arch/x86/kvm/pmu.c | 2 +- arch/x86/kvm/x86.c | 21 ++++++++++++--------- arch/x86/kvm/x86.h | 10 ++++++---- 4 files changed, 20 insertions(+), 16 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -2
@@ -773,6 +773,7 @@ struct kvm_vcpu_arch {unsigned nmi_pending;/* NMI queued after currently running handler */ bool nmi_injected;/* Trying to inject an NMI this entry */ bool smi_pending;/* SMI queued after currently running handler */ +u8 handling_intr_from_guest;struct kvm_mtrr mtrr_state;u64 pat;
@@ -1893,8 +1894,6 @@ int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu);int kvm_complete_insn_gp (struct kvm_vcpu *vcpu,int err);void __kvm_request_immediate_exit (struct kvm_vcpu *vcpu); -unsigned int kvm_guest_state (void ); -void __user *__x86_set_memory_region (struct kvm *kvm,int id,gpa_t gpa,u32 size);bool kvm_vcpu_is_reset_bsp (struct kvm_vcpu *vcpu);
arch/x86/kvm/pmu.c +1 -1
@@ -87,7 +87,7 @@ static void kvm_perf_overflow_intr(struct perf_event *perf_event,* woken up. So we should wake it, but this is impossible from * NMI context. Do it from irq work instead. */ -if (!kvm_guest_state ()) +if (!kvm_handling_nmi_from_guest (pmc->vcpu))irq_work_queue (&pmc_to_pmu (pmc)->irq_work);else kvm_make_request (KVM_REQ_PMI , pmc->vcpu);
arch/x86/kvm/x86.c +12 -9
@@ -8410,15 +8410,17 @@ static void kvm_timer_init(void) kvmclock_cpu_online, kvmclock_cpu_down_prep); } -DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu); -EXPORT_PER_CPU_SYMBOL_GPL (current_vcpu); +static inline bool kvm_pmi_in_guest (struct kvm_vcpu *vcpu) +{ +return vcpu && vcpu->arch.handling_intr_from_guest; +} -unsigned int kvm_guest_state (void ) +static unsigned int kvm_guest_state (void ) { -struct kvm_vcpu *vcpu =__this_cpu_read (current_vcpu); +struct kvm_vcpu *vcpu =kvm_get_running_vcpu ();unsigned int state; -if (!vcpu) +if (!kvm_pmi_in_guest (vcpu))return 0 ; state =PERF_GUEST_ACTIVE ;
@@ -8430,9 +8432,10 @@ unsigned int kvm_guest_state(void)static unsigned long kvm_guest_get_ip (void ) { -struct kvm_vcpu *vcpu =__this_cpu_read (current_vcpu); +struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); -if (WARN_ON_ONCE (!vcpu)) +/* Retrieving the IP must be guarded by a call to kvm_guest_state(). */ +if (WARN_ON_ONCE (!kvm_pmi_in_guest (vcpu)))return 0 ;return kvm_rip_read (vcpu);
@@ -8440,10 +8443,10 @@ static unsigned long kvm_guest_get_ip(void)static unsigned int kvm_handle_intel_pt_intr (void ) { -struct kvm_vcpu *vcpu =__this_cpu_read (current_vcpu); +struct kvm_vcpu *vcpu =kvm_get_running_vcpu ();/* '0' on failure so that the !PT case can use a RET0 static call. */ -if (!vcpu) +if (!kvm_pmi_in_guest (vcpu))return 0 ;kvm_make_request (KVM_REQ_PMI , vcpu);
arch/x86/kvm/x86.h +6 -4
@@ -385,18 +385,20 @@ static inline bool kvm_cstate_in_guest(struct kvm *kvm)return kvm->arch.cstate_in_guest; } -DECLARE_PER_CPU(struct kvm_vcpu *, current_vcpu); -static inline void kvm_before_interrupt (struct kvm_vcpu *vcpu) { -__this_cpu_write (current_vcpu, vcpu); +WRITE_ONCE (vcpu->arch.handling_intr_from_guest,1 ); }static inline void kvm_after_interrupt (struct kvm_vcpu *vcpu) { -__this_cpu_write (current_vcpu,NULL ); +WRITE_ONCE (vcpu->arch.handling_intr_from_guest,0 ); } +static inline bool kvm_handling_nmi_from_guest (struct kvm_vcpu *vcpu) +{ +return !!vcpu->arch.handling_intr_from_guest; +}static inline bool kvm_pat_valid (u64 data) {
Signature · 6 lines
2.34.0.rc0.344.g81b53c2807-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
[tip: perf/core] KVM: x86: Drop current_vcpu for kvm_running_vcpu + kvm_arch_vcpu variable
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 73cd107b9685c5308e864061772e4a78a629e4a0 Gitweb: https://git.kernel.org/tip/73cd107b9685c5308e864061772e4a78a629e4a0 Author: Sean Christopherson <[email protected]> AuthorDate: Thu, 11 Nov 2021 02:07:31 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Nov 2021 14:49:09 +01:00
KVM: x86: Drop current_vcpu for kvm_running_vcpu + kvm_arch_vcpu variable
Use the generic kvm_running_vcpu plus a new 'handling_intr_from_guest' variable in kvm_arch_vcpu instead of the semi-redundant current_vcpu. kvm_before/after_interrupt() must be called while the vCPU is loaded, (which protects against preemption), thus kvm_running_vcpu is guaranteed to be non-NULL when handling_intr_from_guest is non-zero.
Switching to kvm_get_running_vcpu() will allows moving KVM's perf callbacks to generic code, and the new flag will be used in a future patch to more precisely identify the "NMI from guest" case.
--- arch/x86/include/asm/kvm_host.h | 3 +-- arch/x86/kvm/pmu.c | 2 +- arch/x86/kvm/x86.c | 21 ++++++++++++--------- arch/x86/kvm/x86.h | 10 ++++++---- 4 files changed, 20 insertions(+), 16 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -2
@@ -773,6 +773,7 @@ struct kvm_vcpu_arch {unsigned nmi_pending;/* NMI queued after currently running handler */ bool nmi_injected;/* Trying to inject an NMI this entry */ bool smi_pending;/* SMI queued after currently running handler */ +u8 handling_intr_from_guest;struct kvm_mtrr mtrr_state;u64 pat;
@@ -1895,8 +1896,6 @@ int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu);int kvm_complete_insn_gp (struct kvm_vcpu *vcpu,int err);void __kvm_request_immediate_exit (struct kvm_vcpu *vcpu); -unsigned int kvm_guest_state (void ); -void __user *__x86_set_memory_region (struct kvm *kvm,int id,gpa_t gpa,u32 size);bool kvm_vcpu_is_reset_bsp (struct kvm_vcpu *vcpu);
arch/x86/kvm/pmu.c +1 -1
@@ -87,7 +87,7 @@ static void kvm_perf_overflow_intr(struct perf_event *perf_event,* woken up. So we should wake it, but this is impossible from * NMI context. Do it from irq work instead. */ -if (!kvm_guest_state ()) +if (!kvm_handling_nmi_from_guest (pmc->vcpu))irq_work_queue (&pmc_to_pmu (pmc)->irq_work);else kvm_make_request (KVM_REQ_PMI , pmc->vcpu);
arch/x86/kvm/x86.c +12 -9
@@ -8469,15 +8469,17 @@ static void kvm_timer_init(void) kvmclock_cpu_online, kvmclock_cpu_down_prep); } -DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu); -EXPORT_PER_CPU_SYMBOL_GPL (current_vcpu); +static inline bool kvm_pmi_in_guest (struct kvm_vcpu *vcpu) +{ +return vcpu && vcpu->arch.handling_intr_from_guest; +} -unsigned int kvm_guest_state (void ) +static unsigned int kvm_guest_state (void ) { -struct kvm_vcpu *vcpu =__this_cpu_read (current_vcpu); +struct kvm_vcpu *vcpu =kvm_get_running_vcpu ();unsigned int state; -if (!vcpu) +if (!kvm_pmi_in_guest (vcpu))return 0 ; state =PERF_GUEST_ACTIVE ;
@@ -8489,9 +8491,10 @@ unsigned int kvm_guest_state(void)static unsigned long kvm_guest_get_ip (void ) { -struct kvm_vcpu *vcpu =__this_cpu_read (current_vcpu); +struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); -if (WARN_ON_ONCE (!vcpu)) +/* Retrieving the IP must be guarded by a call to kvm_guest_state(). */ +if (WARN_ON_ONCE (!kvm_pmi_in_guest (vcpu)))return 0 ;return kvm_rip_read (vcpu);
@@ -8499,10 +8502,10 @@ static unsigned long kvm_guest_get_ip(void)static unsigned int kvm_handle_intel_pt_intr (void ) { -struct kvm_vcpu *vcpu =__this_cpu_read (current_vcpu); +struct kvm_vcpu *vcpu =kvm_get_running_vcpu ();/* '0' on failure so that the !PT case can use a RET0 static call. */ -if (!vcpu) +if (!kvm_pmi_in_guest (vcpu))return 0 ;kvm_make_request (KVM_REQ_PMI , vcpu);
arch/x86/kvm/x86.h +6 -4
@@ -385,18 +385,20 @@ static inline bool kvm_cstate_in_guest(struct kvm *kvm)return kvm->arch.cstate_in_guest; } -DECLARE_PER_CPU(struct kvm_vcpu *, current_vcpu); -static inline void kvm_before_interrupt (struct kvm_vcpu *vcpu) { -__this_cpu_write (current_vcpu, vcpu); +WRITE_ONCE (vcpu->arch.handling_intr_from_guest,1 ); }static inline void kvm_after_interrupt (struct kvm_vcpu *vcpu) { -__this_cpu_write (current_vcpu,NULL ); +WRITE_ONCE (vcpu->arch.handling_intr_from_guest,0 ); } +static inline bool kvm_handling_nmi_from_guest (struct kvm_vcpu *vcpu) +{ +return !!vcpu->arch.handling_intr_from_guest; +}static inline bool kvm_pat_valid (u64 data) {
Sean Christopherson
maintainerPATCH v4 11/17 KVM: x86: More precisely identify NMI from guest when handling PMI
In reply to Sean ChristophersonDifferentiate between IRQ and NMI for KVM's PMC overflow callback, which was originally invoked in response to an NMI that arrived while the guest was running, but was inadvertantly changed to fire on IRQs as well when support for perf without PMU/NMI was added to KVM. In practice, this should be a nop as the PMC overflow callback shouldn't be reached, but it's a cheap and easy fix that also better documents the situation.
Note, this also doesn't completely prevent false positives if perf somehow ends up calling into KVM, e.g. an NMI can arrive in host after KVM sets its flag.
--- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 4 +++- arch/x86/kvm/x86.c | 2 +- arch/x86/kvm/x86.h | 13 ++++++++++--- 4 files changed, 15 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/svm.c +1 -1
@@ -3936,7 +3936,7 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu) }if (unlikely (svm->vmcb->control.exit_code ==SVM_EXIT_NMI )) -kvm_before_interrupt (vcpu); +kvm_before_interrupt (vcpu,KVM_HANDLING_NMI );kvm_load_host_xsave_state (vcpu);stgi ();
arch/x86/kvm/vmx/vmx.c +3 -1
@@ -6371,7 +6371,9 @@ void vmx_do_interrupt_nmi_irqoff(unsigned long entry);static void handle_interrupt_nmi_irqoff (struct kvm_vcpu *vcpu,unsigned long entry) { -kvm_before_interrupt (vcpu); +bool is_nmi = entry == (unsigned long )asm_exc_nmi_noist; + +kvm_before_interrupt (vcpu, is_nmi ?KVM_HANDLING_NMI :KVM_HANDLING_IRQ );vmx_do_interrupt_nmi_irqoff (entry);kvm_after_interrupt (vcpu); }
arch/x86/kvm/x86.c +1 -1
@@ -9837,7 +9837,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)* interrupts on processors that implement an interrupt shadow, the * stat.exits increment will do nicely. */ -kvm_before_interrupt (vcpu); +kvm_before_interrupt (vcpu,KVM_HANDLING_IRQ );local_irq_enable (); ++vcpu->stat.exits;local_irq_disable ();
arch/x86/kvm/x86.h +10 -3
@@ -385,9 +385,16 @@ static inline bool kvm_cstate_in_guest(struct kvm *kvm)return kvm->arch.cstate_in_guest; } -static inline void kvm_before_interrupt (struct kvm_vcpu *vcpu) +enum kvm_intr_type { +/* Values are arbitrary, but must be non-zero. */ +KVM_HANDLING_IRQ =1 , +KVM_HANDLING_NMI , +}; + +static inline void kvm_before_interrupt (struct kvm_vcpu *vcpu, +enum kvm_intr_type intr) { -WRITE_ONCE (vcpu->arch.handling_intr_from_guest,1 ); +WRITE_ONCE (vcpu->arch.handling_intr_from_guest, (u8 )intr); }static inline void kvm_after_interrupt (struct kvm_vcpu *vcpu)
@@ -397,7 +404,7 @@ static inline void kvm_after_interrupt(struct kvm_vcpu *vcpu)static inline bool kvm_handling_nmi_from_guest (struct kvm_vcpu *vcpu) { -return !!vcpu->arch.handling_intr_from_guest; +return vcpu->arch.handling_intr_from_guest ==KVM_HANDLING_NMI ; }static inline bool kvm_pat_valid (u64 data)
Signature · 6 lines
2.34.0.rc0.344.g81b53c2807-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
[tip: perf/core] KVM: x86: More precisely identify NMI from guest when handling PMI
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: db215756ae5970aec8ad50257d2eb1678b552b91 Gitweb: https://git.kernel.org/tip/db215756ae5970aec8ad50257d2eb1678b552b91 Author: Sean Christopherson <[email protected]> AuthorDate: Thu, 11 Nov 2021 02:07:32 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Nov 2021 14:49:09 +01:00
KVM: x86: More precisely identify NMI from guest when handling PMI
Differentiate between IRQ and NMI for KVM's PMC overflow callback, which was originally invoked in response to an NMI that arrived while the guest was running, but was inadvertantly changed to fire on IRQs as well when support for perf without PMU/NMI was added to KVM. In practice, this should be a nop as the PMC overflow callback shouldn't be reached, but it's a cheap and easy fix that also better documents the situation.
Note, this also doesn't completely prevent false positives if perf somehow ends up calling into KVM, e.g. an NMI can arrive in host after KVM sets its flag.
--- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 4 +++- arch/x86/kvm/x86.c | 2 +- arch/x86/kvm/x86.h | 13 ++++++++++--- 4 files changed, 15 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/svm.c +1 -1
@@ -3931,7 +3931,7 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu) }if (unlikely (svm->vmcb->control.exit_code ==SVM_EXIT_NMI )) -kvm_before_interrupt (vcpu); +kvm_before_interrupt (vcpu,KVM_HANDLING_NMI );kvm_load_host_xsave_state (vcpu);stgi ();
arch/x86/kvm/vmx/vmx.c +3 -1
@@ -6317,7 +6317,9 @@ void vmx_do_interrupt_nmi_irqoff(unsigned long entry);static void handle_interrupt_nmi_irqoff (struct kvm_vcpu *vcpu,unsigned long entry) { -kvm_before_interrupt (vcpu); +bool is_nmi = entry == (unsigned long )asm_exc_nmi_noist; + +kvm_before_interrupt (vcpu, is_nmi ?KVM_HANDLING_NMI :KVM_HANDLING_IRQ );vmx_do_interrupt_nmi_irqoff (entry);kvm_after_interrupt (vcpu); }
arch/x86/kvm/x86.c +1 -1
@@ -9896,7 +9896,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)* interrupts on processors that implement an interrupt shadow, the * stat.exits increment will do nicely. */ -kvm_before_interrupt (vcpu); +kvm_before_interrupt (vcpu,KVM_HANDLING_IRQ );local_irq_enable (); ++vcpu->stat.exits;local_irq_disable ();
arch/x86/kvm/x86.h +10 -3
@@ -385,9 +385,16 @@ static inline bool kvm_cstate_in_guest(struct kvm *kvm)return kvm->arch.cstate_in_guest; } -static inline void kvm_before_interrupt (struct kvm_vcpu *vcpu) +enum kvm_intr_type { +/* Values are arbitrary, but must be non-zero. */ +KVM_HANDLING_IRQ =1 , +KVM_HANDLING_NMI , +}; + +static inline void kvm_before_interrupt (struct kvm_vcpu *vcpu, +enum kvm_intr_type intr) { -WRITE_ONCE (vcpu->arch.handling_intr_from_guest,1 ); +WRITE_ONCE (vcpu->arch.handling_intr_from_guest, (u8 )intr); }static inline void kvm_after_interrupt (struct kvm_vcpu *vcpu)
@@ -397,7 +404,7 @@ static inline void kvm_after_interrupt(struct kvm_vcpu *vcpu)static inline bool kvm_handling_nmi_from_guest (struct kvm_vcpu *vcpu) { -return !!vcpu->arch.handling_intr_from_guest; +return vcpu->arch.handling_intr_from_guest ==KVM_HANDLING_NMI ; }static inline bool kvm_pat_valid (u64 data)
Sean Christopherson
maintainerPATCH v4 12/17 KVM: Move x86's perf guest info callbacks to generic KVM
In reply to Sean ChristophersonMove x86's perf guest callbacks into common KVM, as they are semantically identical to arm64's callbacks (the only other such KVM callbacks). arm64 will convert to the common versions in a future patch.
Implement the necessary arm64 arch hooks now to avoid having to provide stubs or a temporary #define (from x86) to avoid arm64 compilation errors when CONFIG_GUEST_PERF_EVENTS=y.
--- arch/arm64/include/asm/kvm_host.h | 10 ++++++ arch/arm64/kvm/arm.c | 5 +++ arch/x86/include/asm/kvm_host.h | 3 ++ arch/x86/kvm/x86.c | 53 +++++++------------------------ include/linux/kvm_host.h | 10 ++++++ virt/kvm/kvm_main.c | 44 +++++++++++++++++++++++++ 6 files changed, 83 insertions(+), 42 deletions(-)
arch/arm64/include/asm/kvm_host.h +10 -0
@@ -678,6 +678,16 @@ int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa);void kvm_perf_init (void );void kvm_perf_teardown (void ); +/* +* Returns true if a Performance Monitoring Interrupt (PMI), a.k.a. perf event, +* arrived in guest context. For arm64, any event that arrives while a vCPU is +* loaded is considered to be "in guest". +*/ +static inline bool kvm_arch_pmi_in_guest (struct kvm_vcpu *vcpu) +{ +return IS_ENABLED (CONFIG_GUEST_PERF_EVENTS ) && !!vcpu; +} +long kvm_hypercall_pv_features (struct kvm_vcpu *vcpu);gpa_t kvm_init_stolen_time (struct kvm_vcpu *vcpu);void kvm_update_stolen_time (struct kvm_vcpu *vcpu);
arch/arm64/kvm/arm.c +5 -0
@@ -496,6 +496,11 @@ bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)return vcpu_mode_priv (vcpu); } +unsigned long kvm_arch_vcpu_get_ip (struct kvm_vcpu *vcpu) +{ +return *vcpu_pc (vcpu); +} +/* Just ensure a guest exit from a particular CPU */ static void exit_vm_noop (void *info) {
arch/x86/include/asm/kvm_host.h +3 -0
@@ -1565,6 +1565,9 @@ static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm)return -ENOTSUPP ; } +#define kvm_arch_pmi_in_guest (vcpu) \ + ((vcpu) && (vcpu)->arch.handling_intr_from_guest) +int kvm_mmu_module_init (void );void kvm_mmu_module_exit (void );
arch/x86/kvm/x86.c +11 -42
89 lines not on this page · raw message
include/linux/kvm_host.h +10 -0
@@ -1162,6 +1162,16 @@ static inline bool kvm_arch_intc_initialized(struct kvm *kvm) }#endif +#ifdef CONFIG_GUEST_PERF_EVENTS +unsigned long kvm_arch_vcpu_get_ip (struct kvm_vcpu *vcpu); + +void kvm_register_perf_callbacks (unsigned int (*pt_intr_handler)(void )); +void kvm_unregister_perf_callbacks (void ); +#else +static inline void kvm_register_perf_callbacks (void *ign) {} +static inline void kvm_unregister_perf_callbacks (void ) {} +#endif /* CONFIG_GUEST_PERF_EVENTS */ +int kvm_arch_init_vm (struct kvm *kvm,unsigned long type);void kvm_arch_destroy_vm (struct kvm *kvm);void kvm_arch_sync_events (struct kvm *kvm);
virt/kvm/kvm_main.c +44 -0
50 lines not on this page · raw message
Signature · 6 lines
2.34.0.rc0.344.g81b53c2807-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
[tip: perf/core] KVM: Move x86's perf guest info callbacks to generic KVM
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: e1bfc24577cc65c95dc519d7621a9c985b97e567 Gitweb: https://git.kernel.org/tip/e1bfc24577cc65c95dc519d7621a9c985b97e567 Author: Sean Christopherson <[email protected]> AuthorDate: Thu, 11 Nov 2021 02:07:33 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Nov 2021 14:49:10 +01:00
KVM: Move x86's perf guest info callbacks to generic KVM
Move x86's perf guest callbacks into common KVM, as they are semantically identical to arm64's callbacks (the only other such KVM callbacks). arm64 will convert to the common versions in a future patch.
Implement the necessary arm64 arch hooks now to avoid having to provide stubs or a temporary #define (from x86) to avoid arm64 compilation errors when CONFIG_GUEST_PERF_EVENTS=y.
--- arch/arm64/include/asm/kvm_host.h | 10 ++++++- arch/arm64/kvm/arm.c | 5 +++- arch/x86/include/asm/kvm_host.h | 3 ++- arch/x86/kvm/x86.c | 53 ++++++------------------------ include/linux/kvm_host.h | 10 ++++++- virt/kvm/kvm_main.c | 44 +++++++++++++++++++++++++- 6 files changed, 83 insertions(+), 42 deletions(-)
arch/arm64/include/asm/kvm_host.h +10 -0
@@ -678,6 +678,16 @@ int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa);void kvm_perf_init (void );void kvm_perf_teardown (void ); +/* +* Returns true if a Performance Monitoring Interrupt (PMI), a.k.a. perf event, +* arrived in guest context. For arm64, any event that arrives while a vCPU is +* loaded is considered to be "in guest". +*/ +static inline bool kvm_arch_pmi_in_guest (struct kvm_vcpu *vcpu) +{ +return IS_ENABLED (CONFIG_GUEST_PERF_EVENTS ) && !!vcpu; +} +long kvm_hypercall_pv_features (struct kvm_vcpu *vcpu);gpa_t kvm_init_stolen_time (struct kvm_vcpu *vcpu);void kvm_update_stolen_time (struct kvm_vcpu *vcpu);
arch/arm64/kvm/arm.c +5 -0
@@ -496,6 +496,11 @@ bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)return vcpu_mode_priv (vcpu); } +unsigned long kvm_arch_vcpu_get_ip (struct kvm_vcpu *vcpu) +{ +return *vcpu_pc (vcpu); +} +/* Just ensure a guest exit from a particular CPU */ static void exit_vm_noop (void *info) {
arch/x86/include/asm/kvm_host.h +3 -0
@@ -1567,6 +1567,9 @@ static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm)return -ENOTSUPP ; } +#define kvm_arch_pmi_in_guest (vcpu) \ + ((vcpu) && (vcpu)->arch.handling_intr_from_guest) +int kvm_mmu_module_init (void );void kvm_mmu_module_exit (void );
arch/x86/kvm/x86.c +11 -42
89 lines not on this page · raw message
include/linux/kvm_host.h +10 -0
@@ -1170,6 +1170,16 @@ static inline bool kvm_arch_intc_initialized(struct kvm *kvm) }#endif +#ifdef CONFIG_GUEST_PERF_EVENTS +unsigned long kvm_arch_vcpu_get_ip (struct kvm_vcpu *vcpu); + +void kvm_register_perf_callbacks (unsigned int (*pt_intr_handler)(void )); +void kvm_unregister_perf_callbacks (void ); +#else +static inline void kvm_register_perf_callbacks (void *ign) {} +static inline void kvm_unregister_perf_callbacks (void ) {} +#endif /* CONFIG_GUEST_PERF_EVENTS */ +int kvm_arch_init_vm (struct kvm *kvm,unsigned long type);void kvm_arch_destroy_vm (struct kvm *kvm);void kvm_arch_sync_events (struct kvm *kvm);
virt/kvm/kvm_main.c +44 -0
50 lines not on this page · raw message
Sean Christopherson
maintainerPATCH v4 13/17 KVM: x86: Move Intel Processor Trace interrupt handler to vmx.c
In reply to Sean ChristophersonNow that all state needed for VMX's PT interrupt handler is exposed to vmx.c (specifically the currently running vCPU), move the handler into vmx.c where it belongs.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/vmx/vmx.c | 22 +++++++++++++++++++++- arch/x86/kvm/x86.c | 20 +------------------- 3 files changed, 23 insertions(+), 21 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1515,7 +1515,7 @@ struct kvm_x86_init_ops {int (*disabled_by_bios)(void );int (*check_processor_compatibility)(void );int (*hardware_setup)(void ); -bool (*intel_pt_intr_in_guest)(void ); +unsigned int (*handle_intel_pt_intr)(void );struct kvm_x86_ops *runtime_ops; };
arch/x86/kvm/vmx/vmx.c +21 -1
@@ -7708,6 +7708,20 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector, }; +static unsigned int vmx_handle_intel_pt_intr (void ) +{ +struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); + +/* '0' on failure so that the !PT case can use a RET0 static call. */ +if (!kvm_arch_pmi_in_guest (vcpu)) +return 0 ; + +kvm_make_request (KVM_REQ_PMI , vcpu); +__set_bit (MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT , + (unsigned long *)&vcpu->arch.pmu.global_status); +return 1 ; +} +static __init void vmx_setup_user_return_msrs (void ) {
@@ -7734,6 +7748,8 @@ static __init void vmx_setup_user_return_msrs(void)kvm_add_user_return_msr (vmx_uret_msrs_list[i]); } +static struct kvm_x86_init_ops vmx_init_ops__initdata ; +static __init int hardware_setup (void ) {unsigned long host_bndcfgs;
@@ -7892,6 +7908,10 @@ static __init int hardware_setup(void)return -EINVAL ;if (!enable_ept || !cpu_has_vmx_intel_pt ()) pt_mode =PT_MODE_SYSTEM ; +if (pt_mode ==PT_MODE_HOST_GUEST ) + vmx_init_ops.handle_intel_pt_intr = vmx_handle_intel_pt_intr; +else + vmx_init_ops.handle_intel_pt_intr =NULL ;setup_default_sgx_lepubkeyhash ();
@@ -7920,7 +7940,7 @@ static struct kvm_x86_init_ops vmx_init_ops __initdata = { .disabled_by_bios = vmx_disabled_by_bios, .check_processor_compatibility = vmx_check_processor_compat, .hardware_setup = hardware_setup, - .intel_pt_intr_in_guest = vmx_pt_mode_is_host_guest, + .handle_intel_pt_intr = NULL, .runtime_ops = &vmx_x86_ops, };
arch/x86/kvm/x86.c +1 -19
@@ -8410,20 +8410,6 @@ static void kvm_timer_init(void) kvmclock_cpu_online, kvmclock_cpu_down_prep); } -static unsigned int kvm_handle_intel_pt_intr (void ) -{ -struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); - -/* '0' on failure so that the !PT case can use a RET0 static call. */ -if (!kvm_arch_pmi_in_guest (vcpu)) -return 0 ; - -kvm_make_request (KVM_REQ_PMI , vcpu); -__set_bit (MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT , - (unsigned long *)&vcpu->arch.pmu.global_status); -return 1 ; -} -#ifdef CONFIG_X86_64 static void pvclock_gtod_update_fn (struct work_struct *work) {
@@ -11116,11 +11102,7 @@ int kvm_arch_hardware_setup(void *opaque)memcpy (&kvm_x86_ops, ops->runtime_ops,sizeof (kvm_x86_ops));kvm_ops_static_call_update (); -/* Temporary ugliness. */ -if (ops->intel_pt_intr_in_guest && ops->intel_pt_intr_in_guest ()) -kvm_register_perf_callbacks (kvm_handle_intel_pt_intr); -else -kvm_register_perf_callbacks (NULL ); +kvm_register_perf_callbacks (ops->handle_intel_pt_intr);if (!kvm_cpu_cap_has (X86_FEATURE_XSAVES )) supported_xss =0 ;
Signature · 6 lines
2.34.0.rc0.344.g81b53c2807-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
[tip: perf/core] KVM: x86: Move Intel Processor Trace interrupt handler to vmx.c
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 33271a9e2b52e07e278a67c900d2d2afb5c55bd5 Gitweb: https://git.kernel.org/tip/33271a9e2b52e07e278a67c900d2d2afb5c55bd5 Author: Sean Christopherson <[email protected]> AuthorDate: Thu, 11 Nov 2021 02:07:34 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Nov 2021 14:49:10 +01:00
KVM: x86: Move Intel Processor Trace interrupt handler to vmx.c
Now that all state needed for VMX's PT interrupt handler is exposed to vmx.c (specifically the currently running vCPU), move the handler into vmx.c where it belongs.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/vmx/vmx.c | 22 +++++++++++++++++++++- arch/x86/kvm/x86.c | 20 +------------------- 3 files changed, 23 insertions(+), 21 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1517,7 +1517,7 @@ struct kvm_x86_init_ops {int (*disabled_by_bios)(void );int (*check_processor_compatibility)(void );int (*hardware_setup)(void ); -bool (*intel_pt_intr_in_guest)(void ); +unsigned int (*handle_intel_pt_intr)(void );struct kvm_x86_ops *runtime_ops; };
arch/x86/kvm/vmx/vmx.c +21 -1
@@ -7655,6 +7655,20 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector, }; +static unsigned int vmx_handle_intel_pt_intr (void ) +{ +struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); + +/* '0' on failure so that the !PT case can use a RET0 static call. */ +if (!kvm_arch_pmi_in_guest (vcpu)) +return 0 ; + +kvm_make_request (KVM_REQ_PMI , vcpu); +__set_bit (MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT , + (unsigned long *)&vcpu->arch.pmu.global_status); +return 1 ; +} +static __init void vmx_setup_user_return_msrs (void ) {
@@ -7681,6 +7695,8 @@ static __init void vmx_setup_user_return_msrs(void)kvm_add_user_return_msr (vmx_uret_msrs_list[i]); } +static struct kvm_x86_init_ops vmx_init_ops__initdata ; +static __init int hardware_setup (void ) {unsigned long host_bndcfgs;
@@ -7839,6 +7855,10 @@ static __init int hardware_setup(void)return -EINVAL ;if (!enable_ept || !cpu_has_vmx_intel_pt ()) pt_mode =PT_MODE_SYSTEM ; +if (pt_mode ==PT_MODE_HOST_GUEST ) + vmx_init_ops.handle_intel_pt_intr = vmx_handle_intel_pt_intr; +else + vmx_init_ops.handle_intel_pt_intr =NULL ;setup_default_sgx_lepubkeyhash ();
@@ -7867,7 +7887,7 @@ static struct kvm_x86_init_ops vmx_init_ops __initdata = { .disabled_by_bios = vmx_disabled_by_bios, .check_processor_compatibility = vmx_check_processor_compat, .hardware_setup = hardware_setup, - .intel_pt_intr_in_guest = vmx_pt_mode_is_host_guest, + .handle_intel_pt_intr = NULL, .runtime_ops = &vmx_x86_ops, };
arch/x86/kvm/x86.c +1 -19
@@ -8469,20 +8469,6 @@ static void kvm_timer_init(void) kvmclock_cpu_online, kvmclock_cpu_down_prep); } -static unsigned int kvm_handle_intel_pt_intr (void ) -{ -struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); - -/* '0' on failure so that the !PT case can use a RET0 static call. */ -if (!kvm_arch_pmi_in_guest (vcpu)) -return 0 ; - -kvm_make_request (KVM_REQ_PMI , vcpu); -__set_bit (MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT , - (unsigned long *)&vcpu->arch.pmu.global_status); -return 1 ; -} -#ifdef CONFIG_X86_64 static void pvclock_gtod_update_fn (struct work_struct *work) {
@@ -11192,11 +11178,7 @@ int kvm_arch_hardware_setup(void *opaque)memcpy (&kvm_x86_ops, ops->runtime_ops,sizeof (kvm_x86_ops));kvm_ops_static_call_update (); -/* Temporary ugliness. */ -if (ops->intel_pt_intr_in_guest && ops->intel_pt_intr_in_guest ()) -kvm_register_perf_callbacks (kvm_handle_intel_pt_intr); -else -kvm_register_perf_callbacks (NULL ); +kvm_register_perf_callbacks (ops->handle_intel_pt_intr);if (!kvm_cpu_cap_has (X86_FEATURE_XSAVES )) supported_xss =0 ;
Sean Christopherson
maintainerPATCH v4 14/17 KVM: arm64: Convert to the generic perf callbacks
In reply to Sean ChristophersonDrop arm64's version of the callbacks in favor of the callbacks provided by generic KVM, which are semantically identical.
--- arch/arm64/kvm/perf.c | 34 ++-------------------------------- 1 file changed, 2 insertions(+), 32 deletions(-)
arch/arm64/kvm/perf.c +2 -32
@@ -13,42 +13,12 @@DEFINE_STATIC_KEY_FALSE (kvm_arm_pmu_available); -static unsigned int kvm_guest_state (void ) -{ -struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); -unsigned int state; - -if (!vcpu) -return 0 ; - - state =PERF_GUEST_ACTIVE ; -if (!vcpu_mode_priv (vcpu)) - state |=PERF_GUEST_USER ; - -return state; -} - -static unsigned long kvm_get_guest_ip (void ) -{ -struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); - -if (WARN_ON_ONCE (!vcpu)) -return 0 ; - -return *vcpu_pc (vcpu); -} - -static struct perf_guest_info_callbacks kvm_guest_cbs = { - .state = kvm_guest_state, - .get_ip = kvm_get_guest_ip, -}; -void kvm_perf_init (void ) { -perf_register_guest_info_callbacks (&kvm_guest_cbs); +kvm_register_perf_callbacks (NULL ); }void kvm_perf_teardown (void ) { -perf_unregister_guest_info_callbacks (&kvm_guest_cbs); +kvm_unregister_perf_callbacks (); }
Signature · 6 lines
2.34.0.rc0.344.g81b53c2807-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
[tip: perf/core] KVM: arm64: Convert to the generic perf callbacks
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 7b517831a1c6aceb0821860edb9c7bc7d4f803a2 Gitweb: https://git.kernel.org/tip/7b517831a1c6aceb0821860edb9c7bc7d4f803a2 Author: Sean Christopherson <[email protected]> AuthorDate: Thu, 11 Nov 2021 02:07:35 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Nov 2021 14:49:11 +01:00
KVM: arm64: Convert to the generic perf callbacks
Drop arm64's version of the callbacks in favor of the callbacks provided by generic KVM, which are semantically identical.
--- arch/arm64/kvm/perf.c | 34 ++-------------------------------- 1 file changed, 2 insertions(+), 32 deletions(-)
arch/arm64/kvm/perf.c +2 -32
@@ -13,42 +13,12 @@DEFINE_STATIC_KEY_FALSE (kvm_arm_pmu_available); -static unsigned int kvm_guest_state (void ) -{ -struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); -unsigned int state; - -if (!vcpu) -return 0 ; - - state =PERF_GUEST_ACTIVE ; -if (!vcpu_mode_priv (vcpu)) - state |=PERF_GUEST_USER ; - -return state; -} - -static unsigned long kvm_get_guest_ip (void ) -{ -struct kvm_vcpu *vcpu =kvm_get_running_vcpu (); - -if (WARN_ON_ONCE (!vcpu)) -return 0 ; - -return *vcpu_pc (vcpu); -} - -static struct perf_guest_info_callbacks kvm_guest_cbs = { - .state = kvm_guest_state, - .get_ip = kvm_get_guest_ip, -}; -void kvm_perf_init (void ) { -perf_register_guest_info_callbacks (&kvm_guest_cbs); +kvm_register_perf_callbacks (NULL ); }void kvm_perf_teardown (void ) { -perf_unregister_guest_info_callbacks (&kvm_guest_cbs); +kvm_unregister_perf_callbacks (); }
Sean Christopherson
maintainerPATCH v4 15/17 KVM: arm64: Hide kvm_arm_pmu_available behind CONFIG_HW_PERF_EVENTS=y
In reply to Sean ChristophersonMove the definition of kvm_arm_pmu_available to pmu-emul.c and, out of "necessity", hide it behind CONFIG_HW_PERF_EVENTS. Provide a stub for the key's wrapper, kvm_arm_support_pmu_v3(). Moving the key's definition out of perf.c will allow a future commit to delete perf.c entirely.
--- arch/arm64/kernel/image-vars.h | 2 ++ arch/arm64/kvm/perf.c | 2 -- arch/arm64/kvm/pmu-emul.c | 2 ++ include/kvm/arm_pmu.h | 19 ++++++++++++------- 4 files changed, 16 insertions(+), 9 deletions(-)
arch/arm64/kernel/image-vars.h +2 -0
@@ -102,7 +102,9 @@ KVM_NVHE_ALIAS(__stop___kvm_ex_table);KVM_NVHE_ALIAS (kvm_arm_hyp_percpu_base);/* PMU available static key */ +#ifdef CONFIG_HW_PERF_EVENTS KVM_NVHE_ALIAS (kvm_arm_pmu_available); +#endif /* Position-independent library routines */ KVM_NVHE_ALIAS_HYP (clear_page, __pi_clear_page);
arch/arm64/kvm/perf.c +0 -2
@@ -11,8 +11,6 @@#include <asm/kvm_emulate.h> -DEFINE_STATIC_KEY_FALSE (kvm_arm_pmu_available); -void kvm_perf_init (void ) {kvm_register_perf_callbacks (NULL );
arch/arm64/kvm/pmu-emul.c +2 -0
@@ -14,6 +14,8 @@#include <kvm/arm_pmu.h> #include <kvm/arm_vgic.h> +DEFINE_STATIC_KEY_FALSE (kvm_arm_pmu_available); +static void kvm_pmu_create_perf_event (struct kvm_vcpu *vcpu,u64 select_idx);static void kvm_pmu_update_pmc_chained (struct kvm_vcpu *vcpu,u64 select_idx);static void kvm_pmu_stop_counter (struct kvm_vcpu *vcpu,struct kvm_pmc *pmc);
include/kvm/arm_pmu.h +12 -7
@@ -13,13 +13,6 @@#define ARMV8_PMU_CYCLE_IDX (ARMV8_PMU_MAX_COUNTERS - 1)#define ARMV8_PMU_MAX_COUNTER_PAIRS ((ARMV8_PMU_MAX_COUNTERS + 1) >> 1) -DECLARE_STATIC_KEY_FALSE (kvm_arm_pmu_available); - -static __always_inline bool kvm_arm_support_pmu_v3(void) -{ -return static_branch_likely (&kvm_arm_pmu_available); -} -#ifdef CONFIG_HW_PERF_EVENTS struct kvm_pmc {
@@ -36,6 +29,13 @@ struct kvm_pmu {struct irq_work overflow_work; }; +DECLARE_STATIC_KEY_FALSE (kvm_arm_pmu_available); + +static __always_inline bool kvm_arm_support_pmu_v3(void) +{ +return static_branch_likely (&kvm_arm_pmu_available); +} +#define kvm_arm_pmu_irq_initialized (v) ((v)->arch.pmu.irq_num >= VGIC_NR_SGIS)u64 kvm_pmu_get_counter_value (struct kvm_vcpu *vcpu,u64 select_idx);void kvm_pmu_set_counter_value (struct kvm_vcpu *vcpu,u64 select_idx,u64 val);
@@ -65,6 +65,11 @@ int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu);struct kvm_pmu { }; +static inline bool kvm_arm_support_pmu_v3 (void ) +{ +return false; +} +#define kvm_arm_pmu_irq_initialized (v) (false)static inline u64 kvm_pmu_get_counter_value (struct kvm_vcpu *vcpu,u64 select_idx)
Signature · 6 lines
2.34.0.rc0.344.g81b53c2807-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v4 15/17] KVM: arm64: Hide kvm_arm_pmu_available behind CONFIG_HW_PERF_EVENTS=y
Quoted 7 lines · Sean Christopherson
Move the definition of
kvm_arm_pmu_availabletopmu-emul.cand, out of "necessity", hide it behindCONFIG_HW_PERF_EVENTS. Provide a stub for the key's wrapper,kvm_arm_support_pmu_v3(). Moving the key's definition out ofperf.cwill allow a future commit to deleteperf.centirely.Signed-off-by Sean Christopherson <[email protected]>
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
[tip: perf/core] KVM: arm64: Hide kvm_arm_pmu_available behind CONFIG_HW_PERF_EVENTS=y
In reply to Sean ChristophersonThe following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: be399d824b432a85f8df86b566d2e5994fdf58b0 Gitweb: https://git.kernel.org/tip/be399d824b432a85f8df86b566d2e5994fdf58b0 Author: Sean Christopherson <[email protected]> AuthorDate: Thu, 11 Nov 2021 02:07:36 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Nov 2021 14:49:11 +01:00
KVM: arm64: Hide kvm_arm_pmu_available behind CONFIG_HW_PERF_EVENTS=y
Move the definition of kvm_arm_pmu_available to pmu-emul.c and, out of "necessity", hide it behind CONFIG_HW_PERF_EVENTS. Provide a stub for the key's wrapper, kvm_arm_support_pmu_v3(). Moving the key's definition out of perf.c will allow a future commit to delete perf.c entirely.
--- arch/arm64/kernel/image-vars.h | 2 ++ arch/arm64/kvm/perf.c | 2 -- arch/arm64/kvm/pmu-emul.c | 2 ++ include/kvm/arm_pmu.h | 19 ++++++++++++------- 4 files changed, 16 insertions(+), 9 deletions(-)
arch/arm64/kernel/image-vars.h +2 -0
@@ -102,7 +102,9 @@ KVM_NVHE_ALIAS(__stop___kvm_ex_table);KVM_NVHE_ALIAS (kvm_arm_hyp_percpu_base);/* PMU available static key */ +#ifdef CONFIG_HW_PERF_EVENTS KVM_NVHE_ALIAS (kvm_arm_pmu_available); +#endif /* Position-independent library routines */ KVM_NVHE_ALIAS_HYP (clear_page, __pi_clear_page);
arch/arm64/kvm/perf.c +0 -2
@@ -11,8 +11,6 @@#include <asm/kvm_emulate.h> -DEFINE_STATIC_KEY_FALSE (kvm_arm_pmu_available); -void kvm_perf_init (void ) {kvm_register_perf_callbacks (NULL );
arch/arm64/kvm/pmu-emul.c +2 -0
@@ -14,6 +14,8 @@#include <kvm/arm_pmu.h> #include <kvm/arm_vgic.h> +DEFINE_STATIC_KEY_FALSE (kvm_arm_pmu_available); +static void kvm_pmu_create_perf_event (struct kvm_vcpu *vcpu,u64 select_idx);static void kvm_pmu_update_pmc_chained (struct kvm_vcpu *vcpu,u64 select_idx);static void kvm_pmu_stop_counter (struct kvm_vcpu *vcpu,struct kvm_pmc *pmc);
include/kvm/arm_pmu.h +12 -7
@@ -13,13 +13,6 @@#define ARMV8_PMU_CYCLE_IDX (ARMV8_PMU_MAX_COUNTERS - 1)#define ARMV8_PMU_MAX_COUNTER_PAIRS ((ARMV8_PMU_MAX_COUNTERS + 1) >> 1) -DECLARE_STATIC_KEY_FALSE (kvm_arm_pmu_available); - -static __always_inline bool kvm_arm_support_pmu_v3(void) -{ -return static_branch_likely (&kvm_arm_pmu_available); -} -#ifdef CONFIG_HW_PERF_EVENTS struct kvm_pmc {
@@ -36,6 +29,13 @@ struct kvm_pmu {struct irq_work overflow_work; }; +DECLARE_STATIC_KEY_FALSE (kvm_arm_pmu_available); + +static __always_inline bool kvm_arm_support_pmu_v3(void) +{ +return static_branch_likely (&kvm_arm_pmu_available); +} +#define kvm_arm_pmu_irq_initialized (v) ((v)->arch.pmu.irq_num >= VGIC_NR_SGIS)u64 kvm_pmu_get_counter_value (struct kvm_vcpu *vcpu,u64 select_idx);void kvm_pmu_set_counter_value (struct kvm_vcpu *vcpu,u64 select_idx,u64 val);
@@ -65,6 +65,11 @@ int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu);struct kvm_pmu { }; +static inline bool kvm_arm_support_pmu_v3 (void ) +{ +return false; +} +#define kvm_arm_pmu_irq_initialized (v) (false)static inline u64 kvm_pmu_get_counter_value (struct kvm_vcpu *vcpu,u64 select_idx)
Sean Christopherson
maintainerPATCH v4 16/17 KVM: arm64: Drop perf.c and fold its tiny bits of code into arm.c
In reply to Sean ChristophersonCall KVM's (un)register perf callbacks helpers directly from arm.c and delete perf.c
No functional change intended.
--- arch/arm64/include/asm/kvm_host.h | 3 --- arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arm.c | 5 +++-- arch/arm64/kvm/perf.c | 22 ---------------------- 4 files changed, 4 insertions(+), 28 deletions(-) delete mode 100644 arch/arm64/kvm/perf.c
arch/arm64/include/asm/kvm_host.h +0 -3
@@ -675,9 +675,6 @@ unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len);int kvm_handle_mmio_return (struct kvm_vcpu *vcpu);int io_mem_abort (struct kvm_vcpu *vcpu,phys_addr_t fault_ipa); -void kvm_perf_init (void ); -void kvm_perf_teardown (void ); -/* * Returns true if a Performance Monitoring Interrupt (PMI), a.k.a. perf event, * arrived in guest context. For arm64, any event that arrives while a vCPU is
arch/arm64/kvm/Makefile +1 -1
@@ -12,7 +12,7 @@ obj-$(CONFIG_KVM) += hyp/ kvm-y := $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o $(KVM)/eventfd.o \ $(KVM)/vfio.o $(KVM)/irqchip.o $(KVM)/binary_stats.o \ - arm.o mmu.o mmio.o psci.o perf.o hypercalls.o pvtime.o \ + arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ inject_fault.o va_layout.o handle_exit.o \ guest.o debug.o reset.o sys_regs.o \ vgic-sys-reg-v3.o fpsimd.o pmu.o \
arch/arm64/kvm/arm.c +3 -2
@@ -1776,7 +1776,8 @@ static int init_subsystems(void)if (err) goto out; -kvm_perf_init (); +kvm_register_perf_callbacks (NULL ); +kvm_sys_reg_table_init (); out:
@@ -2164,7 +2165,7 @@ int kvm_arch_init(void *opaque)/* NOP: Compiling as a module not supported */ void kvm_arch_exit (void ) { -kvm_perf_teardown (); +kvm_unregister_perf_callbacks (); }static int __init early_kvm_mode_cfg (char *arg)
arch/arm64/kvm/perf.c deleted +0 -22
@@ -1,22 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-only -/* -* Based on the x86 implementation. -* -* Copyright (C) 2012 ARM Ltd. -* Author: Marc Zyngier <[email protected]> -*/ - -#include <linux/perf_event.h> -#include <linux/kvm_host.h> - -#include <asm/kvm_emulate.h> - -void kvm_perf_init (void ) -{ -kvm_register_perf_callbacks (NULL ); -} - -void kvm_perf_teardown (void ) -{ -kvm_unregister_perf_callbacks (); -}
Signature · 6 lines
2.34.0.rc0.344.g81b53c2807-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v4 16/17] KVM: arm64: Drop perf.c and fold its tiny bits of code into arm.c
Quoted 7 lines · Sean Christopherson
Call KVM's (un)register perf callbacks helpers directly from
arm.cand deleteperf.cNo functional change intended.
Signed-off-by Sean Christopherson <[email protected]>
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
[tip: perf/core] KVM: arm64: Drop perf.c and fold its tiny bits of code into arm.c
In reply to Sean ChristophersonThe following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 17ed14eba22b3a86e82fb6df28af00fb4cadfd77 Gitweb: https://git.kernel.org/tip/17ed14eba22b3a86e82fb6df28af00fb4cadfd77 Author: Sean Christopherson <[email protected]> AuthorDate: Thu, 11 Nov 2021 02:07:37 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Nov 2021 14:49:11 +01:00
KVM: arm64: Drop perf.c and fold its tiny bits of code into arm.c
Call KVM's (un)register perf callbacks helpers directly from arm.c and delete perf.c
No functional change intended.
--- arch/arm64/include/asm/kvm_host.h | 3 --- arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arm.c | 5 +++-- arch/arm64/kvm/perf.c | 22 ---------------------- 4 files changed, 4 insertions(+), 28 deletions(-) delete mode 100644 arch/arm64/kvm/perf.c
arch/arm64/include/asm/kvm_host.h +0 -3
@@ -675,9 +675,6 @@ unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len);int kvm_handle_mmio_return (struct kvm_vcpu *vcpu);int io_mem_abort (struct kvm_vcpu *vcpu,phys_addr_t fault_ipa); -void kvm_perf_init (void ); -void kvm_perf_teardown (void ); -/* * Returns true if a Performance Monitoring Interrupt (PMI), a.k.a. perf event, * arrived in guest context. For arm64, any event that arrives while a vCPU is
arch/arm64/kvm/Makefile +1 -1
@@ -12,7 +12,7 @@ obj-$(CONFIG_KVM) += hyp/ kvm-y := $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o $(KVM)/eventfd.o \ $(KVM)/vfio.o $(KVM)/irqchip.o $(KVM)/binary_stats.o \ - arm.o mmu.o mmio.o psci.o perf.o hypercalls.o pvtime.o \ + arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ inject_fault.o va_layout.o handle_exit.o \ guest.o debug.o reset.o sys_regs.o \ vgic-sys-reg-v3.o fpsimd.o pmu.o \
arch/arm64/kvm/arm.c +3 -2
@@ -1773,7 +1773,8 @@ static int init_subsystems(void)if (err) goto out; -kvm_perf_init (); +kvm_register_perf_callbacks (NULL ); +kvm_sys_reg_table_init (); out:
@@ -2161,7 +2162,7 @@ out_err:/* NOP: Compiling as a module not supported */ void kvm_arch_exit (void ) { -kvm_perf_teardown (); +kvm_unregister_perf_callbacks (); }static int __init early_kvm_mode_cfg (char *arg)
arch/arm64/kvm/perf.c deleted +0 -22
@@ -1,22 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-only -/* -* Based on the x86 implementation. -* -* Copyright (C) 2012 ARM Ltd. -* Author: Marc Zyngier <[email protected]> -*/ - -#include <linux/perf_event.h> -#include <linux/kvm_host.h> - -#include <asm/kvm_emulate.h> - -void kvm_perf_init (void ) -{ -kvm_register_perf_callbacks (NULL ); -} - -void kvm_perf_teardown (void ) -{ -kvm_unregister_perf_callbacks (); -}
Sean Christopherson
maintainerPATCH v4 17/17 perf: Drop guest callback (un)register stubs
In reply to Sean ChristophersonDrop perf's stubs for (un)registering guest callbacks now that KVM registration of callbacks is hidden behind GUEST_PERF_EVENTS=y. The only other user is x86 XEN_PV, and x86 unconditionally selects PERF_EVENTS.
No functional change intended.
--- include/linux/perf_event.h | 5 ----- 1 file changed, 5 deletions(-)
include/linux/perf_event.h +0 -5
@@ -1511,11 +1511,6 @@ perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }static inline void perf_bp_event (struct perf_event *event,void *data) { } -static inline void perf_register_guest_info_callbacks -(struct perf_guest_info_callbacks *cbs) { } -static inline void perf_unregister_guest_info_callbacks -(struct perf_guest_info_callbacks *cbs) { } -static inline void perf_event_mmap (struct vm_area_struct *vma) { }typedef int (perf_ksymbol_get_name_f )(char *name,int name_len,void *data);
Signature · 6 lines
2.34.0.rc0.344.g81b53c2807-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
[tip: perf/core] perf: Drop guest callback (un)register stubs
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: a9f4a6e92b3b319296fb078da2615f618f6cd80c Gitweb: https://git.kernel.org/tip/a9f4a6e92b3b319296fb078da2615f618f6cd80c Author: Sean Christopherson <[email protected]> AuthorDate: Thu, 11 Nov 2021 02:07:38 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 17 Nov 2021 14:49:12 +01:00
perf: Drop guest callback (un)register stubs
Drop perf's stubs for (un)registering guest callbacks now that KVM registration of callbacks is hidden behind GUEST_PERF_EVENTS=y. The only other user is x86 XEN_PV, and x86 unconditionally selects PERF_EVENTS.
No functional change intended.
--- include/linux/perf_event.h | 5 ----- 1 file changed, 5 deletions(-)
include/linux/perf_event.h +0 -5
@@ -1511,11 +1511,6 @@ perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }static inline void perf_bp_event (struct perf_event *event,void *data) { } -static inline void perf_register_guest_info_callbacks -(struct perf_guest_info_callbacks *cbs) { } -static inline void perf_unregister_guest_info_callbacks -(struct perf_guest_info_callbacks *cbs) { } -static inline void perf_event_mmap (struct vm_area_struct *vma) { }typedef int (perf_ksymbol_get_name_f )(char *name,int name_len,void *data);
Peter Zijlstra
maintainerQuoted 16 lines · Sean Christopherson
Like Xu (1):
perf/core: Rework guest callbacks to prepare forstatic_callsupportSean Christopherson (16):
perf: Protectperf_guest_cbswith RCU
KVM: x86: Register perf callbacks after calling vendor's
hardware_setup()
KVM: x86: Register Processor Trace interrupt hook iff PT enabled in
guest
perf: Stop pretending that perf can handle multiple guest callbacks
perf: Drop dead and useless guest "support" from arm, csky, nds32 and
riscv
perf: Add wrappers for invoking guest callbacks
perf: Force architectures to opt-in to guest callbacks perf/core: Usestatic_callto optimizeperf_guest_info_callbacks
KVM: x86: Dropcurrent_vcpuforkvm_running_vcpu+kvm_arch_vcpu
Quoted 8 lines
variable
KVM: x86: More precisely identify NMI from guest when handling PMI
KVM: Move x86's perf guest info callbacks to generic KVM
KVM: x86: Move Intel Processor Trace interrupt handler tovmx.c
KVM: arm64: Convert to the generic perf callbacks
KVM: arm64: Hidekvm_arm_pmu_availablebehindCONFIG_HW_PERF_EVENTS=y
KVM: arm64: Dropperf.cand fold its tiny bits of code intoarm.c
perf: Drop guest callback (un)register stubs
Thanks!, I'll queue them up and push them into tip/perf/core once -rc1 happens.
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv