Merge tag 'perf-urgent-2026-11-08' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull perf events fixes from Ingo Molnar:

 - Fix sideband event output when only requesting attr.defer_output
   (Ian Rogers)

 - Clamp deferred callchain entry count to fit u16 header.size
   (Ian Rogers)

 - Various AMD IBS fixes (Ravi Bangoria):
    - Reset CPU0 IBS state on syscore resume
    - Fix kernel address leak via REGS_USER and REGS_INTR
    - Simplify/fix event state handling with IBS_CAPS_DIS

 - Revert AUX page allocation order default to be continuous (Leo Yan)

 - Don't send SIGTRAP after exec removed the event (Danish Khateeb)

* tag 'perf-urgent-2026-11-08' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip:
  perf/core: Don't send SIGTRAP after exec removed the event
  Revert "perf/aux: Allocate non-contiguous AUX pages by default"
  perf/amd/ibs: Simplify event state handling with IBS_CAPS_DIS
  perf/amd/ibs: Fix kernel address leak via REGS_USER and REGS_INTR
  perf/amd/ibs: Reset CPU0 IBS state on syscore resume
  perf/core: Clamp deferred callchain entry count to fit u16 header.size
  perf/core: Include perf_event_attr::defer_output in is_sb_event() check
This commit is contained in:
Linus Torvalds
2026-11-08 08:54:01 -08:00
5 changed files with 217 additions and 61 deletions
+174 -25
View File
@@ -28,6 +28,7 @@ static u32 ibs_caps;
#include <linux/hardirq.h>
#include <asm/nmi.h>
#include <asm/delay.h>
#include <asm/amd/ibs.h>
/* attr.config2 */
@@ -73,7 +74,12 @@ static u32 ibs_caps;
*
* XXX: we could probably be using !atomic bitops for all this.
*/
/*
* NOTE: IBS_CAPS_DIS eliminates the RMW race on the IBS_{FETCH|OP}_CTL, making
* IBS_STARTED and IBS_STOPPED states unnecessary. We use only IBS_ENABLED and
* IBS_STOPPING states when IBS_CAPS_DIS is present. Semantics of IBS_STOPPING
* changes a bit as well.
*/
enum ibs_states {
IBS_ENABLED = 0,
IBS_STARTED = 1,
@@ -557,7 +563,9 @@ static void perf_ibs_start(struct perf_event *event, int flags)
struct hw_perf_event *hwc = &event->hw;
struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
unsigned int delay = 50;
u64 period, config = 0;
u64 ctl;
if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
return;
@@ -575,25 +583,89 @@ static void perf_ibs_start(struct perf_event *event, int flags)
}
config |= period >> 4;
/*
* Reset the IBS_{FETCH|OP}_CTL MSR before updating pcpu->state.
* Doing so prevents a race condition in which an NMI due to other
* source might accidentally activate the event before we enable
* it ourselves.
*/
perf_ibs_disable_event(perf_ibs, hwc, 0);
/*
* Set STARTED before enabling the hardware, such that a subsequent NMI
* must observe it.
*/
set_bit(IBS_STARTED, pcpu->state);
clear_bit(IBS_STOPPING, pcpu->state);
if (ibs_caps & IBS_CAPS_DIS) {
/*
* IBS HW SW
*
* o Capture sample o ->stop()
* o Set VAL bit CTL2[Dis] = 1
* o Raise NMI --. o ...
* \ o ->start()
* \ wrmsr(IBS_{FETCH|OP}_CTL, new value);
* \ (wrmsr clears the VAL bit)
* `--> o Receives delayed NMI
* (Unknown NMI since VAL bit is cleared)
*
* Handle this with induced delay.
*/
rdmsrq(hwc->config_base, ctl);
while ((ctl & perf_ibs->valid_mask) && delay--) {
udelay(1);
rdmsrq(hwc->config_base, ctl);
}
} else {
/*
* Reset the IBS_{FETCH|OP}_CTL MSR before updating pcpu->state.
* Doing so prevents a race condition in which an NMI due to
* other source might accidentally activate the event before we
* enable it ourselves.
*/
perf_ibs_disable_event(perf_ibs, hwc, 0);
/*
* Set STARTED before enabling the hardware, such that a
* subsequent NMI must observe it.
*/
set_bit(IBS_STARTED, pcpu->state);
clear_bit(IBS_STOPPING, pcpu->state);
}
perf_ibs_enable_event(perf_ibs, hwc, config);
perf_event_update_userpage(event);
}
/* ->stop() for platforms with additional CTL2[Dis] bit. */
static void perf_ibs_stop_dis(struct perf_event *event, int flags)
{
struct hw_perf_event *hwc = &event->hw;
struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
struct cpu_perf_ibs *pcpu = this_cpu_ptr(perf_ibs->pcpu);
u64 config;
u64 ctl2;
WARN_ON_ONCE(test_bit(IBS_STOPPING, pcpu->state));
set_bit(IBS_STOPPING, pcpu->state);
rdmsrq(hwc->extra_reg.reg, ctl2);
if (ctl2 & perf_ibs->disable_mask)
goto reset_state;
wrmsrq(hwc->extra_reg.reg, perf_ibs->disable_mask);
WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
hwc->state |= PERF_HES_STOPPED;
if (hwc->state & PERF_HES_UPTODATE)
goto reset_state;
rdmsrq(hwc->config_base, config);
config &= ~perf_ibs->valid_mask;
/*
* Clear the Val bit otherwise ->start() will unnecessarily
* wait for 50us.
*/
if (in_nmi())
wrmsrq(hwc->config_base, config);
perf_ibs_event_update(perf_ibs, event, &config);
hwc->state |= PERF_HES_UPTODATE;
reset_state:
clear_bit(IBS_STOPPING, pcpu->state);
}
static void perf_ibs_stop(struct perf_event *event, int flags)
{
struct hw_perf_event *hwc = &event->hw;
@@ -602,6 +674,9 @@ static void perf_ibs_stop(struct perf_event *event, int flags)
u64 config;
int stopping;
if (ibs_caps & IBS_CAPS_DIS)
return perf_ibs_stop_dis(event, flags);
if (test_and_set_bit(IBS_STOPPING, pcpu->state))
return;
@@ -1416,19 +1491,43 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
unsigned int msr;
u64 *buf, *config, period, new_config = 0;
int br_target_idx = -1;
u64 ctl2, ctl;
if (!test_bit(IBS_STARTED, pcpu->state)) {
fail:
if (ibs_caps & IBS_CAPS_DIS) {
/*
* Catch spurious interrupts after stopping IBS: After
* disabling IBS there could be still incoming NMIs
* with samples that even have the valid bit cleared.
* Mark all this NMIs as handled.
* ->stop() {
* set_bit(IBS_STOPPING);
* <-- NMI (race 1)
* CTL2[Dis]=1;
* ... <-- NMI (race 2)
* }
*/
if (test_and_clear_bit(IBS_STOPPED, pcpu->state))
return 1;
rdmsrq(perf_ibs->msr2, ctl2);
if (test_bit(IBS_STOPPING, pcpu->state) /* race 1 */ ||
(ctl2 & perf_ibs->disable_mask) /* race 2 */) {
rdmsrq(perf_ibs->msr, ctl);
if (ctl & perf_ibs->valid_mask) {
ctl &= ~(perf_ibs->enable_mask |
perf_ibs->valid_mask);
wrmsrq(perf_ibs->msr, ctl);
return 1;
}
return 0;
}
} else {
if (!test_bit(IBS_STARTED, pcpu->state)) {
fail:
/*
* Catch spurious interrupts after stopping IBS: After
* disabling IBS there could be still incoming NMIs
* with samples that even have the valid bit cleared.
* Mark all this NMIs as handled.
*/
if (test_and_clear_bit(IBS_STOPPED, pcpu->state))
return 1;
return 0;
return 0;
}
}
if (WARN_ON_ONCE(!event))
@@ -1533,6 +1632,56 @@ fail:
throttle = perf_event_account_interrupt(event);
goto out;
}
/*
* IBS captured RIP is out of sync with NMI pt_regs due to NMI
* delivery latency. Use NMI pt_regs for PERF_SAMPLE_REGS_USER.
*
* Note: Valid user regs are required for PERF_SAMPLE_STACK_USER.
*/
if (event->attr.sample_type & (PERF_SAMPLE_REGS_USER |
PERF_SAMPLE_STACK_USER)) {
perf_sample_regs_user(&data.regs_user, iregs);
/*
* Don't update dyn_size if user has not requested for
* PERF_SAMPLE_REGS_USER.
*/
if (event->attr.sample_type & PERF_SAMPLE_REGS_USER) {
/* regs dump ABI info */
int size = sizeof(u64);
if (data.regs_user.regs) {
u64 mask = event->attr.sample_regs_user;
size += hweight64(mask) * sizeof(u64);
}
data.dyn_size += size;
}
data.sample_flags |= PERF_SAMPLE_REGS_USER;
}
/*
* IBS captured RIP is out of sync with NMI pt_regs due to NMI
* delivery latency. Use NMI pt_regs for PERF_SAMPLE_REGS_INTR.
*/
if (event->attr.sample_type & PERF_SAMPLE_REGS_INTR) {
/* regs dump ABI info */
int size = sizeof(u64);
perf_sample_regs_intr(&data.regs_intr, iregs,
event->attr.exclude_kernel);
if (data.regs_intr.regs) {
u64 mask = event->attr.sample_regs_intr;
size += hweight64(mask) * sizeof(u64);
}
data.dyn_size += size;
data.sample_flags |= PERF_SAMPLE_REGS_INTR;
}
/*
* Prevent leaking physical addresses to unprivileged users. Skip
* PERF_SAMPLE_PHYS_ADDR check since generic code prevents it for
@@ -1948,7 +2097,7 @@ static int perf_ibs_suspend(void *data)
static void perf_ibs_resume(void *data)
{
ibs_eilvt_setup();
setup_APIC_ibs();
x86_pmu_amd_ibs_starting_cpu(smp_processor_id());
}
static const struct syscore_ops perf_ibs_syscore_ops = {
-2
View File
@@ -1896,8 +1896,6 @@ static __init int pt_init(void)
if (!intel_pt_validate_hw_cap(PT_CAP_topa_multiple_entries))
pt_pmu.pmu.capabilities = PERF_PMU_CAP_AUX_NO_SG;
else
pt_pmu.pmu.capabilities = PERF_PMU_CAP_AUX_PREFER_LARGE;
pt_pmu.pmu.capabilities |= PERF_PMU_CAP_EXCLUSIVE |
PERF_PMU_CAP_ITRACE |
+8 -3
View File
@@ -304,9 +304,8 @@ struct perf_event_pmu_context;
#define PERF_PMU_CAP_AUX_OUTPUT 0x0080
#define PERF_PMU_CAP_EXTENDED_HW_TYPE 0x0100
#define PERF_PMU_CAP_AUX_PAUSE 0x0200
#define PERF_PMU_CAP_AUX_PREFER_LARGE 0x0400
#define PERF_PMU_CAP_MEDIATED_VPMU 0x0800
#define PERF_PMU_CAP_SIMD_REGS 0x1000
#define PERF_PMU_CAP_MEDIATED_VPMU 0x0400
#define PERF_PMU_CAP_SIMD_REGS 0x0800
/**
* pmu::scope
@@ -1814,6 +1813,12 @@ extern unsigned long perf_misc_flags(struct perf_event *event, struct pt_regs *r
extern unsigned long perf_instruction_pointer(struct perf_event *event,
struct pt_regs *regs);
extern void perf_sample_regs_user(struct perf_regs *regs_user,
struct pt_regs *regs);
extern void perf_sample_regs_intr(struct perf_regs *regs_intr,
struct pt_regs *regs,
bool exclude_kernel);
#ifndef perf_arch_misc_flags
# define perf_arch_misc_flags(regs) \
(user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
+26 -11
View File
@@ -5356,7 +5356,7 @@ static bool is_sb_event(struct perf_event *event)
attr->comm || attr->comm_exec ||
attr->task || attr->ksymbol ||
attr->context_switch || attr->text_poke ||
attr->bpf_event)
attr->bpf_event || attr->defer_output)
return true;
return false;
@@ -7646,6 +7646,15 @@ static void perf_sigtrap(struct perf_event *event)
if (event->ctx->task == TASK_TOMBSTONE)
return;
/*
* The event was removed after this signal was queued, e.g. by exec()
* (remove_on_exec), which may also have closed its fd (close-on-exec).
* The new program has default signal handlers, so a SIGTRAP would
* kill it.
*/
if (READ_ONCE(event->state) <= PERF_EVENT_STATE_EXIT)
return;
/*
* We'd expect this to only occur if the irq_work is delayed and either
* ctx->task or current has changed in the meantime. This can be the
@@ -7919,8 +7928,8 @@ perf_output_sample_simd_regs(struct perf_output_handle *handle,
}
}
static void perf_sample_regs_user(struct perf_regs *regs_user,
struct pt_regs *regs)
void perf_sample_regs_user(struct perf_regs *regs_user,
struct pt_regs *regs)
{
if (user_mode(regs)) {
regs_user->abi = perf_reg_abi(current);
@@ -7933,9 +7942,9 @@ static void perf_sample_regs_user(struct perf_regs *regs_user,
}
}
static void perf_sample_regs_intr(struct perf_regs *regs_intr,
struct pt_regs *regs,
bool exclude_kernel)
void perf_sample_regs_intr(struct perf_regs *regs_intr,
struct pt_regs *regs,
bool exclude_kernel)
{
/*
* Hardware skid can lead to a scenario where a PMI is
@@ -10514,8 +10523,6 @@ struct perf_callchain_deferred_event {
struct {
struct perf_event_header header;
u64 cookie;
u64 nr;
u64 ips[];
} event;
};
@@ -10524,12 +10531,19 @@ static void perf_callchain_deferred_output(struct perf_event *event, void *data)
struct perf_callchain_deferred_event *deferred_event = data;
struct perf_output_handle handle;
struct perf_sample_data sample;
u64 nr = deferred_event->trace->nr;
u16 size, id_size, max_payload;
int ret;
u16 size = sizeof(deferred_event->event) + (deferred_event->trace->nr * sizeof(u64));
if (!event->attr.defer_output)
return;
id_size = event->attr.sample_id_all ? event->id_header_size : 0;
max_payload = round_down(U16_MAX - sizeof(deferred_event->event) -
sizeof(u64) - id_size, sizeof(u64));
nr = min_t(u64, nr, max_payload / sizeof(u64));
size = sizeof(deferred_event->event) + sizeof(u64) + nr * sizeof(u64);
/* XXX do we really need sample_id_all for this ??? */
perf_event_header__init(&deferred_event->event.header, &sample,
PERF_RECORD_CALLCHAIN_DEFERRED,
@@ -10543,8 +10557,10 @@ static void perf_callchain_deferred_output(struct perf_event *event, void *data)
return;
perf_output_put(&handle, deferred_event->event);
for (int i = 0; i < deferred_event->trace->nr; i++) {
perf_output_put(&handle, nr);
for (u64 i = 0; i < nr; i++) {
u64 entry = deferred_event->trace->entries[i];
perf_output_put(&handle, entry);
}
perf_event__output_id_sample(event, &handle, &sample);
@@ -10559,7 +10575,6 @@ static void perf_unwind_deferred_callback(struct unwind_work *work,
.trace = trace,
.event = {
.cookie = cookie,
.nr = trace->nr,
},
};
+9 -20
View File
@@ -684,15 +684,7 @@ int rb_alloc_aux(struct perf_buffer *rb, struct perf_event *event,
{
bool overwrite = !(flags & RING_BUFFER_WRITABLE);
int node = (event->cpu == -1) ? -1 : cpu_to_node(event->cpu);
bool use_contiguous_pages = event->pmu->capabilities & (
PERF_PMU_CAP_AUX_NO_SG | PERF_PMU_CAP_AUX_PREFER_LARGE);
/*
* Initialize max_order to 0 for page allocation. This allocates single
* pages to minimize memory fragmentation. This is overridden if the
* PMU needs or prefers contiguous pages (use_contiguous_pages = true).
*/
int max_order = 0;
int ret = -ENOMEM;
int ret = -ENOMEM, max_order;
if (!has_aux(event))
return -EOPNOTSUPP;
@@ -702,8 +694,8 @@ int rb_alloc_aux(struct perf_buffer *rb, struct perf_event *event,
if (!overwrite) {
/*
* Watermark defaults to half the buffer, to aid PMU drivers
* in double buffering.
* Watermark defaults to half the buffer, and so does the
* max_order, to aid PMU drivers in double buffering.
*/
if (!watermark)
watermark = min_t(unsigned long,
@@ -711,19 +703,16 @@ int rb_alloc_aux(struct perf_buffer *rb, struct perf_event *event,
(unsigned long)nr_pages << (PAGE_SHIFT - 1));
/*
* If using contiguous pages, use aux_watermark as the basis
* for chunking to help PMU drivers honor the watermark.
* Use aux_watermark as the basis for chunking to
* help PMU drivers honor the watermark.
*/
if (use_contiguous_pages)
max_order = get_order(watermark);
max_order = get_order(watermark);
} else {
/*
* If using contiguous pages, we need to start with the
* max_order that fits in nr_pages, not the other way around,
* hence ilog2() and not get_order.
* We need to start with the max_order that fits in nr_pages,
* not the other way around, hence ilog2() and not get_order.
*/
if (use_contiguous_pages)
max_order = ilog2(nr_pages);
max_order = ilog2(nr_pages);
watermark = 0;
}