perf: Add wrappers for invoking guest callbacks
authorSean Christopherson <seanjc@google.com>
Thu, 11 Nov 2021 02:07:28 +0000 (02:07 +0000)
committerPeter Zijlstra <peterz@infradead.org>
Wed, 17 Nov 2021 13:49:08 +0000 (14:49 +0100)
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.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Link: https://lore.kernel.org/r/20211111020738.2512932-8-seanjc@google.com
arch/arm64/kernel/perf_callchain.c
arch/x86/events/core.c
arch/x86/events/intel/core.c
include/linux/perf_event.h

index 274dc3e11b6d3dce5fc9b7cf1e1d60dcd41a6434..db04a55cee7e031443c9393817314397bd1dea38 100644 (file)
@@ -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) {
index e29312a1003a4465d074be0b8d87d0e13b8261dc..6203473980270ac46ff81ef58b694adf8448804c 100644 (file)
@@ -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 *ent
 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 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) {
index 7ff24d1ecdb79b86256a8c897cec4761dfa4f8e1..f7af8029664f36046cbb5661bd7114f383362756 100644 (file)
@@ -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();
        }
 
index 5e6b346d62a792554098475023471b78528cd82f..346d5aff580482975f6613a815ffd5b571cc2add 100644 (file)
@@ -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);