FAILED: patch "[PATCH] perf/core: Run sched_task() for PMUs with only CPU-wide" failed to apply to 6.6-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 6.6-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.6.y
git checkout FETCH_HEAD
git cherry-pick -x 3d8d74100954a3b17e5c5e37adfe14e16b1db103
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026092954-reviving-drearily-7882@gregkh' --subject-prefix 'PATCH 6.6.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 3d8d74100954a3b17e5c5e37adfe14e16b1db103 Mon Sep 17 00:00:00 2001
From: Puranjay Mohan <[email protected]>
Date: Mon, 10 Aug 2026 06:35:35 -0700
Subject: [PATCH] perf/core: Run sched_task() for PMUs with only CPU-wide
events
perf_pmu_sched_task() returns early when cpuctx->task_ctx is set and leaves the work to perf_ctx_sched_task_cb(), which only walks ctx->pmu_ctx_list. A PMU whose events are all CPU-wide is not on that list, so nothing calls its sched_task(). With
perf record -b -e cycles -a -- ls
armv8pmu_sched_task() is skipped on every switch to a task that has a perf context but no event on that PMU, and BRBE records leak across the task boundary. intel_pmu_lbr_add() calls perf_sched_cb_inc() unconditionally too, so LBR records leak the same way on x86.
Drop the early return and skip only the CPCs that perf_ctx_sched_task_cb() handles. That one needs a gate of its own to make the split exact: it tests cpc->sched_cb_usage, which perf_sched_cb_inc() sets per CPU for every branch stack user, so a task with an event for that PMU pinned to another CPU would be handled twice. On x86 the second __intel_pmu_lbr_restore() finds lbr_stack_state == LBR_NONE and calls intel_pmu_lbr_reset(), throwing away the callstack the first one restored.
cpc->task_epc is set only while a task context is scheduled in, and there is one epc per PMU on ctx->pmu_ctx_list, so the two gates are inverses.
For the CPCs perf_pmu_sched_task() picks up, the callback now runs outside the perf_ctx_disable() and perf_ctx_enable() pair in perf_event_context_sched_in(). __perf_pmu_sched_task() disables the PMU around the call itself.
kernel/events/core.c +9 -4
@@ -3764,6 +3764,9 @@ static void perf_ctx_sched_task_cb(struct perf_event_context *ctx,list_for_each_entry (pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { cpc =this_cpc (pmu_ctx->pmu); +if (cpc->task_epc != pmu_ctx) +continue ; +if (cpc->sched_cb_usage && pmu_ctx->pmu->sched_task) pmu_ctx->pmu->sched_task (pmu_ctx, task, sched_in); }
@@ -3924,15 +3927,17 @@ static void perf_pmu_sched_task(struct task_struct *prev, struct task_struct *next, bool sched_in) { -struct perf_cpu_context *cpuctx =this_cpu_ptr (&perf_cpu_context);struct perf_cpu_pmu_context *cpc, *cpc2; -/* cpuctx->task_ctx will be handled in perf_event_context_sched_in/out */ -if (prev == next || cpuctx->task_ctx) +if (prev == next)return ; -list_for_each_entry_safe (cpc, cpc2,this_cpu_ptr (&sched_cb_list), sched_cb_entry) +list_for_each_entry_safe (cpc, cpc2,this_cpu_ptr (&sched_cb_list), sched_cb_entry) { +if (cpc->task_epc) +continue ; +__perf_pmu_sched_task (cpc, sched_in ? next : prev, sched_in); + } } static void perf_event_switch(struct task_struct *task,
Sasha Levin
maintainerPATCH 1/2 perf: Supply task information to sched_task()
From: Kan Liang <[email protected]>
[ Upstream commit d57e94f5b891925e4f2796266eba31edd5a01903 ]
To save/restore LBR call stack data in system-wide mode, the task_struct information is required.
Extend the parameters of sched_task() to supply task_struct information.
When schedule in, the LBR call stack data for new task will be restored. When schedule out, the LBR call stack data for old task will be saved. Only need to pass the required task_struct information.
[Stable backport: keep the two-argument task_ctx_sched_out() interface. Use list_for_each_entry_safe() in perf_pmu_sched_task(), as in upstream 59e63416f5153, so callbacks may remove their list entry and the follow-up 3d8d74100954 can apply its per-PMU scheduling checks.]
Stable-dep-of: 3d8d74100954 ("perf/core: Run sched_task() for PMUs with only CPU-wide events")
--- arch/powerpc/perf/core-book3s.c | 8 ++++++-- arch/s390/kernel/perf_pai_crypto.c | 3 ++- arch/s390/kernel/perf_pai_ext.c | 3 ++- arch/x86/events/amd/brs.c | 3 ++- arch/x86/events/amd/lbr.c | 3 ++- arch/x86/events/core.c | 5 +++-- arch/x86/events/intel/core.c | 4 ++-- arch/x86/events/intel/lbr.c | 3 ++- arch/x86/events/perf_event.h | 14 +++++++++----- include/linux/perf_event.h | 2 +- kernel/events/core.c | 24 +++++++++++++----------- 11 files changed, 44 insertions(+), 28 deletions(-)
arch/powerpc/perf/core-book3s.c +6 -2
@@ -132,7 +132,10 @@ static unsigned long ebb_switch_in(bool ebb, struct cpu_hw_events *cpuhw)static inline void power_pmu_bhrb_enable (struct perf_event *event) {}static inline void power_pmu_bhrb_disable (struct perf_event *event) {} -static void power_pmu_sched_task (struct perf_event_pmu_context *pmu_ctx,bool sched_in) {} +static void power_pmu_sched_task (struct perf_event_pmu_context *pmu_ctx, +struct task_struct *task,bool sched_in) +{ +}static inline void power_pmu_bhrb_read (struct perf_event *event,struct cpu_hw_events *cpuhw) {}static void pmao_restore_workaround (bool ebb) { }#endif /* CONFIG_PPC32 */
@@ -451,7 +454,8 @@ static void power_pmu_bhrb_disable(struct perf_event *event)/* Called from ctxsw to prevent one process's branch entries to * mingle with the other process's entries during context switch. */ -static void power_pmu_sched_task (struct perf_event_pmu_context *pmu_ctx,bool sched_in) +static void power_pmu_sched_task (struct perf_event_pmu_context *pmu_ctx, +struct task_struct *task,bool sched_in) {if (!ppmu->bhrb_nr)return ;
arch/s390/kernel/perf_pai_crypto.c +2 -1
@@ -378,7 +378,8 @@ static int paicrypt_push_sample(void)/* Called on schedule-in and schedule-out. No access to event structure, * but for sampling only event CRYPTO_ALL is allowed. */ -static void paicrypt_sched_task (struct perf_event_pmu_context *pmu_ctx,bool sched_in) +static void paicrypt_sched_task (struct perf_event_pmu_context *pmu_ctx, +struct task_struct *task,bool sched_in) {/* We started with a clean page on event installation. So read out * results on schedule_out and if page was dirty, clear values.
arch/s390/kernel/perf_pai_ext.c +2 -1
@@ -467,7 +467,8 @@ static int paiext_push_sample(void)/* Called on schedule-in and schedule-out. No access to event structure, * but for sampling only event NNPA_ALL is allowed. */ -static void paiext_sched_task (struct perf_event_pmu_context *pmu_ctx,bool sched_in) +static void paiext_sched_task (struct perf_event_pmu_context *pmu_ctx, +struct task_struct *task,bool sched_in) {/* We started with a clean page on event installation. So read out * results on schedule_out and if page was dirty, clear values.
arch/x86/events/amd/brs.c +2 -1
@@ -381,7 +381,8 @@ static void amd_brs_poison_buffer(void)* On ctxswin, sched_in = true, called after the PMU has started * On ctxswout, sched_in = false, called before the PMU is stopped */ -void amd_pmu_brs_sched_task (struct perf_event_pmu_context *pmu_ctx,bool sched_in) +void amd_pmu_brs_sched_task (struct perf_event_pmu_context *pmu_ctx, +struct task_struct *task,bool sched_in) {struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events);
arch/x86/events/amd/lbr.c +2 -1
@@ -376,7 +376,8 @@ void amd_pmu_lbr_del(struct perf_event *event)perf_sched_cb_dec (event->pmu); } -void amd_pmu_lbr_sched_task (struct perf_event_pmu_context *pmu_ctx,bool sched_in) +void amd_pmu_lbr_sched_task (struct perf_event_pmu_context *pmu_ctx, +struct task_struct *task,bool sched_in) {struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events);
arch/x86/events/core.c +3 -2
@@ -2626,9 +2626,10 @@ static const struct attribute_group *x86_pmu_attr_groups[] = { NULL, }; -static void x86_pmu_sched_task (struct perf_event_pmu_context *pmu_ctx,bool sched_in) +static void x86_pmu_sched_task (struct perf_event_pmu_context *pmu_ctx, +struct task_struct *task,bool sched_in) { -static_call_cond (x86_pmu_sched_task)(pmu_ctx, sched_in); +static_call_cond (x86_pmu_sched_task)(pmu_ctx, task, sched_in); } static void x86_pmu_swap_task_ctx(struct perf_event_pmu_context *prev_epc,
arch/x86/events/intel/core.c +2 -2
@@ -4933,10 +4933,10 @@ static void intel_pmu_cpu_dead(int cpu) }static void intel_pmu_sched_task (struct perf_event_pmu_context *pmu_ctx, -bool sched_in) +struct task_struct *task,bool sched_in) {intel_pmu_pebs_sched_task (pmu_ctx, sched_in); -intel_pmu_lbr_sched_task (pmu_ctx, sched_in); +intel_pmu_lbr_sched_task (pmu_ctx, task, sched_in); } static void intel_pmu_swap_task_ctx(struct perf_event_pmu_context *prev_epc,
arch/x86/events/intel/lbr.c +2 -1
@@ -538,7 +538,8 @@ void intel_pmu_lbr_swap_task_ctx(struct perf_event_pmu_context *prev_epc, task_context_opt(next_ctx_data)->lbr_callstack_users); } -void intel_pmu_lbr_sched_task (struct perf_event_pmu_context *pmu_ctx,bool sched_in) +void intel_pmu_lbr_sched_task (struct perf_event_pmu_context *pmu_ctx, +struct task_struct *task,bool sched_in) {struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events);void *task_ctx;
arch/x86/events/perf_event.h +9 -5
@@ -816,7 +816,7 @@ struct x86_pmu {void (*check_microcode)(void );void (*sched_task)(struct perf_event_pmu_context *pmu_ctx, -bool sched_in); +struct task_struct *task,bool sched_in);/* * Intel Arch Perfmon v2+
@@ -1310,7 +1310,8 @@ void amd_pmu_lbr_reset(void);void amd_pmu_lbr_read (void );void amd_pmu_lbr_add (struct perf_event *event);void amd_pmu_lbr_del (struct perf_event *event); -void amd_pmu_lbr_sched_task (struct perf_event_pmu_context *pmu_ctx,bool sched_in); +void amd_pmu_lbr_sched_task (struct perf_event_pmu_context *pmu_ctx, +struct task_struct *task,bool sched_in);void amd_pmu_lbr_enable_all (void );void amd_pmu_lbr_disable_all (void );int amd_pmu_lbr_hw_config (struct perf_event *event);
@@ -1351,7 +1352,8 @@ static inline void amd_pmu_brs_del(struct perf_event *event) perf_sched_cb_dec(event->pmu); } -void amd_pmu_brs_sched_task (struct perf_event_pmu_context *pmu_ctx,bool sched_in); +void amd_pmu_brs_sched_task (struct perf_event_pmu_context *pmu_ctx, +struct task_struct *task,bool sched_in);#else static inline int amd_brs_init (void ) {
@@ -1376,7 +1378,8 @@ static inline void amd_pmu_brs_del(struct perf_event *event) { } -static inline void amd_pmu_brs_sched_task (struct perf_event_pmu_context *pmu_ctx,bool sched_in) +static inline void amd_pmu_brs_sched_task (struct perf_event_pmu_context *pmu_ctx, +struct task_struct *task,bool sched_in) { }
@@ -1558,7 +1561,8 @@ void intel_ds_init(void);void intel_pmu_lbr_swap_task_ctx (struct perf_event_pmu_context *prev_epc,struct perf_event_pmu_context *next_epc); -void intel_pmu_lbr_sched_task (struct perf_event_pmu_context *pmu_ctx,bool sched_in); +void intel_pmu_lbr_sched_task (struct perf_event_pmu_context *pmu_ctx, +struct task_struct *task,bool sched_in);u64 lbr_from_signext_quirk_wr (u64 val);
include/linux/perf_event.h +1 -1
@@ -453,7 +453,7 @@ struct pmu {* context-switches callback */ void (*sched_task) (struct perf_event_pmu_context *pmu_ctx, -bool sched_in); +struct task_struct *task,bool sched_in);/* * Kmem cache of PMU specific data
kernel/events/core.c +13 -11
@@ -3524,7 +3524,8 @@ static void perf_event_swap_task_ctx_data(struct perf_event_context *prev_ctx, } } -static void perf_ctx_sched_task_cb (struct perf_event_context *ctx,bool sched_in) +static void perf_ctx_sched_task_cb (struct perf_event_context *ctx, +struct task_struct *task,bool sched_in) {struct perf_event_pmu_context *pmu_ctx;struct perf_cpu_pmu_context *cpc;
@@ -3533,7 +3534,7 @@ static void perf_ctx_sched_task_cb(struct perf_event_context *ctx, bool sched_in cpc =this_cpu_ptr (pmu_ctx->pmu->cpu_pmu_context);if (cpc->sched_cb_usage && pmu_ctx->pmu->sched_task) - pmu_ctx->pmu->sched_task (pmu_ctx, sched_in); + pmu_ctx->pmu->sched_task (pmu_ctx, task, sched_in); } }
@@ -3591,7 +3592,7 @@ perf_event_context_sched_out(struct task_struct *task, struct task_struct *next)WRITE_ONCE (ctx->task, next);WRITE_ONCE (next_ctx->task, task); -perf_ctx_sched_task_cb (ctx, false); +perf_ctx_sched_task_cb (ctx, task, false);perf_event_swap_task_ctx_data (ctx, next_ctx);perf_ctx_enable (ctx, false);
@@ -3621,7 +3622,7 @@ perf_event_context_sched_out(struct task_struct *task, struct task_struct *next)perf_ctx_disable (ctx, false); inside_switch: -perf_ctx_sched_task_cb (ctx, false); +perf_ctx_sched_task_cb (ctx, task, false);task_ctx_sched_out (ctx,EVENT_ALL );perf_ctx_enable (ctx, false);
@@ -3663,7 +3664,8 @@ void perf_sched_cb_inc(struct pmu *pmu)* PEBS requires this to provide PID/TID information. This requires we flush * all queued PEBS records before we context switch to a new task. */ -static void __perf_pmu_sched_task (struct perf_cpu_pmu_context *cpc,bool sched_in) +static void __perf_pmu_sched_task (struct perf_cpu_pmu_context *cpc, +struct task_struct *task,bool sched_in) {struct perf_cpu_context *cpuctx =this_cpu_ptr (&perf_cpu_context);struct pmu *pmu;
@@ -3677,7 +3679,7 @@ static void __perf_pmu_sched_task(struct perf_cpu_pmu_context *cpc, bool sched_iperf_ctx_lock (cpuctx, cpuctx->task_ctx);perf_pmu_disable (pmu); - pmu->sched_task (cpc->task_epc, sched_in); + pmu->sched_task (cpc->task_epc, task, sched_in);perf_pmu_enable (pmu);perf_ctx_unlock (cpuctx, cpuctx->task_ctx);
@@ -3688,14 +3690,14 @@ static void perf_pmu_sched_task(struct task_struct *prev, bool sched_in) {struct perf_cpu_context *cpuctx =this_cpu_ptr (&perf_cpu_context); -struct perf_cpu_pmu_context *cpc; +struct perf_cpu_pmu_context *cpc, *cpc2;/* cpuctx->task_ctx will be handled in perf_event_context_sched_in/out */ if (prev == next || cpuctx->task_ctx)return ; -list_for_each_entry (cpc,this_cpu_ptr (&sched_cb_list), sched_cb_entry) -__perf_pmu_sched_task (cpc, sched_in); +list_for_each_entry_safe (cpc, cpc2,this_cpu_ptr (&sched_cb_list), sched_cb_entry) +__perf_pmu_sched_task (cpc, sched_in ? next : prev, sched_in); } static void perf_event_switch(struct task_struct *task,
@@ -3999,7 +4001,7 @@ static void perf_event_context_sched_in(struct task_struct *task)perf_ctx_lock (cpuctx, ctx);perf_ctx_disable (ctx, false); -perf_ctx_sched_task_cb (ctx, true); +perf_ctx_sched_task_cb (ctx, task, true);perf_ctx_enable (ctx, false);perf_ctx_unlock (cpuctx, ctx);
@@ -4030,7 +4032,7 @@ static void perf_event_context_sched_in(struct task_struct *task)perf_event_sched_in (cpuctx, ctx); -perf_ctx_sched_task_cb (cpuctx->task_ctx, true); +perf_ctx_sched_task_cb (cpuctx->task_ctx, task, true);if (!RB_EMPTY_ROOT (&ctx->pinned_groups.tree))perf_ctx_enable (&cpuctx->ctx, false);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/2 perf/core: Run sched_task() for PMUs with only CPU-wide events
From: Puranjay Mohan <[email protected]>
[ Upstream commit 3d8d74100954a3b17e5c5e37adfe14e16b1db103 ]
perf_pmu_sched_task() returns early when cpuctx->task_ctx is set and leaves the work to perf_ctx_sched_task_cb(), which only walks ctx->pmu_ctx_list. A PMU whose events are all CPU-wide is not on that list, so nothing calls its sched_task(). With
perf record -b -e cycles -a -- ls
armv8pmu_sched_task() is skipped on every switch to a task that has a perf context but no event on that PMU, and BRBE records leak across the task boundary. intel_pmu_lbr_add() calls perf_sched_cb_inc() unconditionally too, so LBR records leak the same way on x86.
Drop the early return and skip only the CPCs that perf_ctx_sched_task_cb() handles. That one needs a gate of its own to make the split exact: it tests cpc->sched_cb_usage, which perf_sched_cb_inc() sets per CPU for every branch stack user, so a task with an event for that PMU pinned to another CPU would be handled twice. On x86 the second __intel_pmu_lbr_restore() finds lbr_stack_state == LBR_NONE and calls intel_pmu_lbr_reset(), throwing away the callstack the first one restored.
cpc->task_epc is set only while a task context is scheduled in, and there is one epc per PMU on ctx->pmu_ctx_list, so the two gates are inverses.
For the CPCs perf_pmu_sched_task() picks up, the callback now runs outside the perf_ctx_disable() and perf_ctx_enable() pair in perf_event_context_sched_in(). __perf_pmu_sched_task() disables the PMU around the call itself.
--- kernel/events/core.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
kernel/events/core.c +9 -4
@@ -3533,6 +3533,9 @@ static void perf_ctx_sched_task_cb(struct perf_event_context *ctx,list_for_each_entry (pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { cpc =this_cpu_ptr (pmu_ctx->pmu->cpu_pmu_context); +if (cpc->task_epc != pmu_ctx) +continue ; +if (cpc->sched_cb_usage && pmu_ctx->pmu->sched_task) pmu_ctx->pmu->sched_task (pmu_ctx, task, sched_in); }
@@ -3689,15 +3692,17 @@ static void perf_pmu_sched_task(struct task_struct *prev, struct task_struct *next, bool sched_in) { -struct perf_cpu_context *cpuctx =this_cpu_ptr (&perf_cpu_context);struct perf_cpu_pmu_context *cpc, *cpc2; -/* cpuctx->task_ctx will be handled in perf_event_context_sched_in/out */ -if (prev == next || cpuctx->task_ctx) +if (prev == next)return ; -list_for_each_entry_safe (cpc, cpc2,this_cpu_ptr (&sched_cb_list), sched_cb_entry) +list_for_each_entry_safe (cpc, cpc2,this_cpu_ptr (&sched_cb_list), sched_cb_entry) { +if (cpc->task_epc) +continue ; +__perf_pmu_sched_task (cpc, sched_in ? next : prev, sched_in); + } } static void perf_event_switch(struct task_struct *task,
Signature · 1 line
2.53.0