Skip to content

Commit

Permalink
perf/x86/intel/bts: Check if bts_ctx is allocated when calling BTS fu…
Browse files Browse the repository at this point in the history
…nctions

bts_ctx might not be allocated, for example if the CPU has X86_FEATURE_PTI,
but intel_bts_disable/enable_local() and intel_bts_interrupt() are called
unconditionally from intel_pmu_handle_irq() and crash on bts_ctx.

So check if bts_ctx is allocated when calling BTS functions.

Fixes: 3acfcef ("perf/x86/intel/bts: Allocate bts_ctx only if necessary")
Reported-by: Jiri Olsa <olsajiri@gmail.com>
Tested-by: Jiri Olsa <jolsa@kernel.org>
Suggested-by: Adrian Hunter <adrian.hunter@intel.com>
Suggested-by: Dave Hansen <dave.hansen@intel.com>
Signed-off-by: Li RongQing <lirongqing@baidu.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20250306051102.2642-1-lirongqing@baidu.com
  • Loading branch information
Li RongQing authored and Ingo Molnar committed Mar 6, 2025
1 parent fa6192a commit 7a310c6
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions arch/x86/events/intel/bts.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,14 @@ static void bts_event_stop(struct perf_event *event, int flags)

void intel_bts_enable_local(void)
{
struct bts_ctx *bts = this_cpu_ptr(bts_ctx);
int state = READ_ONCE(bts->state);
struct bts_ctx *bts;
int state;

if (!bts_ctx)
return;

bts = this_cpu_ptr(bts_ctx);
state = READ_ONCE(bts->state);
/*
* Here we transition from INACTIVE to ACTIVE;
* if we instead are STOPPED from the interrupt handler,
Expand All @@ -358,7 +363,12 @@ void intel_bts_enable_local(void)

void intel_bts_disable_local(void)
{
struct bts_ctx *bts = this_cpu_ptr(bts_ctx);
struct bts_ctx *bts;

if (!bts_ctx)
return;

bts = this_cpu_ptr(bts_ctx);

/*
* Here we transition from ACTIVE to INACTIVE;
Expand Down Expand Up @@ -450,12 +460,17 @@ bts_buffer_reset(struct bts_buffer *buf, struct perf_output_handle *handle)
int intel_bts_interrupt(void)
{
struct debug_store *ds = this_cpu_ptr(&cpu_hw_events)->ds;
struct bts_ctx *bts = this_cpu_ptr(bts_ctx);
struct perf_event *event = bts->handle.event;
struct bts_ctx *bts;
struct perf_event *event;
struct bts_buffer *buf;
s64 old_head;
int err = -ENOSPC, handled = 0;

if (!bts_ctx)
return 0;

bts = this_cpu_ptr(bts_ctx);
event = bts->handle.event;
/*
* The only surefire way of knowing if this NMI is ours is by checking
* the write ptr against the PMI threshold.
Expand Down

0 comments on commit 7a310c6

Please sign in to comment.