MIPS: SiByte: Add HW PMU support
Subsystems (4)
cover MIPS: SiByte: Add HW PMU support
This patch series resurrects the SiByte HW PMU support which used to be supported with oprofile but was later removed.
Florian Fainelli (3):
MIPS: SiByte: Wire up the CPU interrupt controller
MIPS: perf: Add SB1 userspace callchain unwinding
MIPS: perf: Add SB1 and SB1A performance counter support
Documentation/arch/mips/index.rst | 1 + Documentation/arch/mips/sb1-pmu.rst | 162 ++++++++++ arch/mips/include/asm/mach-sibyte/irq.h | 10 + arch/mips/kernel/perf_event.c | 106 ++++++- arch/mips/kernel/perf_event_mipsxx.c | 373 +++++++++++++++++++++++- arch/mips/sibyte/bcm1480/irq.c | 7 +- arch/mips/sibyte/sb1250/irq.c | 3 + include/linux/cpuhotplug.h | 1 + 8 files changed, 647 insertions(+), 16 deletions(-) create mode 100644 Documentation/arch/mips/sb1-pmu.rst create mode 100644 arch/mips/include/asm/mach-sibyte/irq.h
Signature · 1 line
2.53.0
PATCH 1/3 MIPS: SiByte: Wire up the CPU interrupt controller
Initialize the MIPS CPU interrupt controller on SB1250 and BCM1480 so CPU-local interrupts have a configured irq_chip and handler. Reserve IRQs 128 through 135 above both interrupt mapper ranges, and dispatch BCM1480 IP7 through the CPU controller as SB1250 already does.
This provides the CPU-local IP7 interrupt needed by the SB1 performance counters without overlapping the SiByte interrupt mapper IRQ numbers.
--- arch/mips/include/asm/mach-sibyte/irq.h | 10 ++++++++++ arch/mips/sibyte/bcm1480/irq.c | 7 ++++++- arch/mips/sibyte/sb1250/irq.c | 3 +++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 arch/mips/include/asm/mach-sibyte/irq.h
arch/mips/include/asm/mach-sibyte/irq.h new file +10 -0
@@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __ASM_MACH_SIBYTE_IRQ_H +#define __ASM_MACH_SIBYTE_IRQ_H + +/* Keep CPU-local interrupts above both SiByte interrupt mapper ranges. */ +#define MIPS_CPU_IRQ_BASE 128 + +#include <asm/mach-generic/irq.h> + +#endif
arch/mips/sibyte/bcm1480/irq.c +6 -1
@@ -15,6 +15,7 @@#include <asm/irq_regs.h> #include <asm/signal.h> #include <asm/io.h> +#include <asm/irq_cpu.h> #include <asm/sibyte/bcm1480_regs.h> #include <asm/sibyte/bcm1480_int.h>
@@ -242,6 +243,8 @@ void __init arch_init_irq(void)unsigned int imask =STATUSF_IP4 |STATUSF_IP3 |STATUSF_IP2 |STATUSF_IP1 |STATUSF_IP0 ; +mips_cpu_irq_init (); +/* Default everything to IP2 */ /* Start with _high registers which has no bit 0 interrupt source */ for (i =1 ; i <BCM1480_NR_IRQS_HALF ; i++) {/* was I0 */
@@ -337,7 +340,9 @@ asmlinkage void plat_irq_dispatch(void) pending =read_c0_cause () &read_c0_status (); -if (pending &CAUSEF_IP4 ) +if (pending &CAUSEF_IP7 ) +do_IRQ (MIPS_CPU_IRQ_BASE +7 ); +else if (pending &CAUSEF_IP4 )do_IRQ (K_BCM1480_INT_TIMER_0 + cpu);#ifdef CONFIG_SMP else if (pending & CAUSEF_IP3)
arch/mips/sibyte/sb1250/irq.c +3 -0
@@ -15,6 +15,7 @@#include <asm/signal.h> #include <asm/time.h> #include <asm/io.h> +#include <asm/irq_cpu.h> #include <asm/sibyte/sb1250_regs.h> #include <asm/sibyte/sb1250_int.h>
@@ -225,6 +226,8 @@ void __init arch_init_irq(void)unsigned int imask =STATUSF_IP4 |STATUSF_IP3 |STATUSF_IP2 |STATUSF_IP1 |STATUSF_IP0 ; +mips_cpu_irq_init (); +/* Default everything to IP2 */ for (i =0 ; i <SB1250_NR_IRQS ; i++) {/* was I0 */ __raw_writeq(IMR_IP2_VAL,
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 1/3] MIPS: SiByte: Wire up the CPU interrupt controller
Sashiko has reviewed this patch and found no issues.
Quoted 9 lines
Initialize the MIPS CPU interrupt controller on SB1250 and BCM1480 so CPU-local interrupts have a configured
irq_chipand handler. Reserve IRQs 128 through 135 above both interrupt mapper ranges, and dispatch BCM1480 IP7 through the CPU controller as SB1250 already does.This provides the CPU-local IP7 interrupt needed by the SB1 performance counters without overlapping the SiByte interrupt mapper IRQ numbers.
Signed-off-by Florian Fainelli <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH 2/3 MIPS: perf: Add SB1 userspace callchain unwinding
In reply to Florian FainelliRestore the userspace callchain capability formerly available through MIPS OProfile, exposing it through perf for SB1 and SB1A. Commit 5f307491f3a0 ("MIPS: oprofile: Add callgraph support") introduced an in-kernel userspace prologue scanner, which disappeared with the removal of MIPS OProfile support in e258958945c6 ("arch: mips: Remove CONFIG_OPROFILE support"). The MIPS perf userspace callchain hook remained unimplemented.
Follow the former OProfile approach of examining instructions before the sampled PC to find standard MIPS stack adjustments and saved return addresses. Bound each scan to 128 instructions, use nofault user reads, validate frame data and stack progress, and stop at the perf stack limit. Handle o32, n32 and n64 address widths and the actual sw/sd return-address slot widths, rather than relying on the kernel's unsigned long size. Allow the sampled leaf to use the live return-address register, and permit repeated return PCs when the stack advances so recursion is not truncated.
This complements the existing MIPS DWARF callchain mechanism, enabled by 1ddc96bd42da ("MIPS: kernel: Support extracting off-line stack traces from user-space with perf") and b5f184fbdb03 ("perf tools: Support MIPS unwinding and dwarf-regs"). With --call-graph dwarf, the kernel records user registers and a stack snapshot for the perf tool to unwind using DWARF information. The new walker instead produces PERF_SAMPLE_CALLCHAIN entries in the kernel for ordinary perf -g/--call-graph fp sampling, without requiring DWARF unwind information or a userspace unwinding library. It does not change or replace the DWARF path, which can handle frames beyond the standard prologues recognized by this best-effort scanner.
Limit enablement to SB1 and SB1A, whose standard instruction encoding is handled here and which have neither MIPS16 nor microMIPS. Other MIPS CPU types retain their existing userspace callchain behavior. Nonstandard prologues, dynamic stack adjustments and signal trampolines can truncate chains.
--- arch/mips/kernel/perf_event.c | 106 ++++++++++++++++++++++++++++++++-- 1 file changed, 102 insertions(+), 4 deletions(-)
arch/mips/kernel/perf_event.c +102 -4
@@ -13,15 +13,113 @@#include <linux/perf_event.h> #include <linux/sched/task_stack.h> +#include <linux/uaccess.h> +#include <asm/cpu-type.h> +#include <asm/inst.h> #include <asm/stacktrace.h> /* Callchain handling code. */ -/* -* Leave userspace callchain empty for now. When we find a way to trace -* the user stack callchains, we will add it here. -*/ +struct sb1_user_frame { +unsigned long pc; +unsigned long sp; +unsigned long ra; +}; + +static unsigned long sb1_user_address (unsigned long addr) +{ +if (IS_ENABLED (CONFIG_64BIT ) &&test_thread_flag (TIF_32BIT_ADDR )) +return (u32 )addr; +return addr; +} + +/* Best-effort prologue unwinding, as provided by the former MIPS OProfile. */ +static bool sb1_unwind_user (struct sb1_user_frame *frame) +{ +unsigned long pc = frame->pc, caller = frame->ra; +unsigned int frame_size =0 , ra_size =0 , ra_offset =0 , n; +union mips_instruction insn; + +/* SB1 has neither MIPS16 nor microMIPS instructions. */ +if ((pc &3 ) || (frame->sp &3 )) +return false; + +for (n =0 ; n <128 && pc >=sizeof (insn); n++) { + pc -=sizeof (insn); +if (copy_from_user_nofault (&insn, (void __user *)pc,sizeof (insn))) +break ; +if (insn.i_format.rs ==29 && insn.i_format.rt ==29 && + (insn.i_format.opcode == addiu_op || + insn.i_format.opcode == daddiu_op)) { +/* A positive adjustment belongs to an epilogue. */ +if (insn.i_format.simmediate >=0 ) +break ; + frame_size = -insn.i_format.simmediate; +break ; + } +if (insn.i_format.rs ==29 && insn.i_format.rt ==31 && + insn.i_format.simmediate >=0 ) { +if (insn.i_format.opcode == sw_op) + ra_size =sizeof (u32); +else if (insn.i_format.opcode == sd_op) + ra_size =sizeof (u64); +else +continue ; + ra_offset = insn.i_format.simmediate; + } +/* Do not scan into the preceding function through a return. */ +if (insn.r_format.opcode == spec_op && + insn.r_format.func == jr_op) +break ; + } + +if (ra_size) { +u64 ra64; +u32 ra32; +void __user *slot = (void __user *)(frame->sp + ra_offset); + +if (!frame_size || ra_offset + ra_size > frame_size) +return false; +if (ra_size ==sizeof (ra32)) { +if (copy_from_user_nofault (&ra32, slot,sizeof (ra32))) +return false; + caller = ra32; + }else { +if (copy_from_user_nofault (&ra64, slot,sizeof (ra64))) +return false; + caller = ra64; + } + } + + caller =sb1_user_address (caller); +if (!caller || (caller == frame->pc && !frame_size) || (caller &3 ) || + !access_ok ((void __user *)caller,sizeof (insn)) || + frame->sp + frame_size < frame->sp) +return false; + frame->sp += frame_size; + frame->pc = caller; +/* Only the sampled leaf may use the live return-address register. */ + frame->ra =0 ; +return true; +} + +void perf_callchain_user (struct perf_callchain_entry_ctx *entry, +struct pt_regs *regs) +{ +struct sb1_user_frame frame = { + .pc =sb1_user_address (regs->cp0_epc), + .sp =sb1_user_address (regs->regs[29 ]), + .ra =sb1_user_address (regs->regs[31 ]), + }; + +if (current_cpu_type () !=CPU_SB1 &¤t_cpu_type () !=CPU_SB1A ) +return ; +do { +if (perf_callchain_store (entry, frame.pc)) +break ; + }while (sb1_unwind_user (&frame)); +}static void save_raw_perf_callchain (struct perf_callchain_entry_ctx *entry,unsigned long reg29)
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 2/3] MIPS: perf: Add SB1 userspace callchain unwinding
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] MIPS: perf: perf_callchain_user() clobbers current->thread.cp0_baduaddr --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] MIPS: perf: perf_callchain_user() clobbers current->thread.cp0_baduaddr
--
commit d44d716d25b2c4b55ac95b94fe0bbc43afc39a94
Author: Florian Fainelli <[email protected]>
MIPS: perf: Add SB1 userspace callchain unwinding
This commit adds in-kernel userspace callchain unwinding for SB1 and SB1A CPUs by scanning function prologues prior to the sampled PC.
Quoted 5 lines
arch/mips/kernel/perf_event.c +0
-0@@ -13,15 +13,113 @@
[ ... ]
Quoted 15 lines
arch/mips/kernel/perf_event.c +15
-0+/* Best-effort prologue unwinding, as provided by the former MIPS OProfile. */ +static bool sb1_unwind_user (struct sb1_user_frame *frame) +{ +unsigned long pc = frame->pc, caller = frame->ra; +unsigned int frame_size =0 , ra_size =0 , ra_offset =0 , n; +union mips_instruction insn; + +/* SB1 has neither MIPS16 nor microMIPS instructions. */ +if ((pc &3 ) || (frame->sp &3 )) +return false; + +for (n =0 ; n <128 && pc >=sizeof (insn); n++) { + pc -=sizeof (insn); +if (copy_from_user_nofault (&insn, (void __user *)pc,sizeof (insn))) +break ;
[ ... ]
Quoted 6 lines
arch/mips/kernel/perf_event.c +6
-0+void perf_callchain_user (struct perf_callchain_entry_ctx *entry, +struct pt_regs *regs) +{ +struct sb1_user_frame frame = { + .pc =sb1_user_address (regs->cp0_epc), + .sp =sb1_user_address (regs->regs[29 ]),
arch/mips/kernel/perf_event.c line 122
Quoted 10 lines
arch/mips/kernel/perf_event.c +10
-0+ .ra = sb1_user_address(regs->regs[31]), + }; + +if (current_cpu_type () !=CPU_SB1 &¤t_cpu_type () !=CPU_SB1A ) +return ; +do { +if (perf_callchain_store (entry, frame.pc)) +break ; + }while (sb1_unwind_user (&frame)); +}
[Severity: Medium]
Can perf_callchain_user() clobber current->thread.cp0_baduaddr while an interrupted process-context usercopy is recovering from a fault?
On MIPS, raw_copy_from_user(), raw_copy_to_user() and __clear_user() run with hardware interrupts enabled. When a kernel-mode user access faults, __do_page_fault() (or do_ade()) runs fixup_exception() and stores the faulting address in current->thread.cp0_baduaddr:
arch/mips/mm/fault.c:__do_page_fault() {
...
no_context:
/* Are we prepared to handle this kernel fault? */
if (fixup_exception(regs)) {
current->thread.cp0_baduaddr = address;
return;
}
...
}If a perf interrupt fires before the usercopy fixup handler reads THREAD_BUADDR (current->thread.cp0_baduaddr) and get_perf_callchain() walks the user stack of the interrupted user task, a fault in copy_from_user_nofault() inside sb1_unwind_user() overwrites cp0_baduaddr:
raw_copy_from_user()
<page fault on user address>
__do_page_fault()
fixup_exception(regs)
current->thread.cp0_baduaddr = address;
<hardirq>
...
get_perf_callchain()
perf_callchain_user()
sb1_unwind_user()
copy_from_user_nofault()
<page fault on unmapped user page>
__do_page_fault()
fixup_exception(regs)
current->thread.cp0_baduaddr = address; .Ll_exc:
LOADK t0, THREAD_BUADDR(t0) <-- reads clobbered cp0_baduaddr SUB len, AT, t0
In _inline_copy_from_user(), the bogus residual byte count returned by raw_copy_from_user() is then passed to memset(to + (n - res), 0, res), corrupting kernel memory out of bounds or leaving uncopied bytes unzeroed.
Saving and restoring current->thread.cp0_baduaddr across perf_callchain_user() would avoid corrupting an in-flight usercopy fixup.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH 3/3 MIPS: perf: Add SB1 and SB1A performance counter support
In reply to Florian FainelliSupport the four CPU-local SB1 performance counters through the MIPS PMU driver. Use 64-bit CP0 accesses for the 40 count bits and overflow bit, validate the documented layout at initialization, and handle sampling on CPU-local IP7. Keep the interrupt masked while no events are scheduled and clear the unused Count/Compare source sharing IP7 before enabling it and in the handler. Reset counters through CPU hotplug callbacks.
Expose the historical OProfile event catalog through raw events and 48 sysfs aliases, plus generic cycles, instructions, branches and branch misses. Preserve the catalog's duplicate encodings and disambiguate its duplicate names. Leave generic cache events unmapped where the historical descriptions do not establish a reliable mapping.
Add explicit physical counter selection and independent exclusions for EXL and ordinary kernel execution. These exclusions only remove modes already permitted by perf attributes. Enforce counter 0's cycles-only restriction and reject reserved configurations. Reassign flexible events when fixed-counter requests require it, preserving counts and sampling state, and support groups led by software events. Honor sampling throttling.
Document the ABI, hardware assumptions, usage and validation limits.
--- Documentation/arch/mips/index.rst | 1 + Documentation/arch/mips/sb1-pmu.rst | 162 ++++++++++++ arch/mips/kernel/perf_event_mipsxx.c | 373 ++++++++++++++++++++++++++- include/linux/cpuhotplug.h | 1 + 4 files changed, 526 insertions(+), 11 deletions(-) create mode 100644 Documentation/arch/mips/sb1-pmu.rst
Documentation/arch/mips/index.rst +1 -0
@@ -10,5 +10,6 @@ MIPS-specific Documentation booting ingenic-tcu + sb1-pmu features
Documentation/arch/mips/sb1-pmu.rst new file +162 -0
162 lines not on this page · raw message
arch/mips/kernel/perf_event_mipsxx.c +362 -11
514 lines not on this page · raw message
include/linux/cpuhotplug.h +1 -0
@@ -151,6 +151,7 @@ enum cpuhp_state {CPUHP_AP_PERF_X86_STARTING ,CPUHP_AP_PERF_X86_AMD_IBS_STARTING ,CPUHP_AP_PERF_XTENSA_STARTING , +CPUHP_AP_PERF_MIPS_SB1_STARTING ,CPUHP_AP_ARM_VFP_STARTING ,CPUHP_AP_ARM64_DEBUG_MONITORS_STARTING , CPUHP_AP_PERF_ARM_HW_BREAKPOINT_STARTING,
Signature · 1 line
2.53.0
Randy Dunlap
reviewerRe: [PATCH 3/3] MIPS: perf: Add SB1 and SB1A performance counter support
Hi,
This has one 'make htmldocs' warning (see below).
Quoted 15 lines · Florian Fainelli
--- Documentation/arch/mips/index.rst | 1 + Documentation/arch/mips/sb1-pmu.rst | 162 ++++++++++++ arch/mips/kernel/perf_event_mipsxx.c | 373 ++++++++++++++++++++++++++- include/linux/cpuhotplug.h | 1 + 4 files changed, 526 insertions(+), 11 deletions(-) create mode 100644 Documentation/arch/mips/sb1-pmu.rstDocumentation/arch/mips/sb1-pmu.rst new file +0
-0
Documentation/arch/mips/sb1-pmu.rst line 57
Quoted 3 lines
Documentation/arch/mips/sb1-pmu.rst new file +2
-0@@ -0,0 +1,162 @@ +Counter selection and mode filtering +-----------------------------------
Documentation/arch/mips/sb1-pmu.rst:57: WARNING: Title underline too short.
Counter selection and mode filtering
----------------------------------- [docutils]
Quoted 6 lines
Documentation/arch/mips/sb1-pmu.rst new file +6
-0+ +The ``counter`` format field selects a physical counter: 0 means automatic, +and values 1 through 4 select physical counters 0 through 3. A non-cycle +event requesting physical counter 0 is rejected. Automatic events may move +to another eligible counter when a fixed-counter event is added, preserving +their accumulated counts and sample periods.
Signature · 1 line
~Randy
[email protected]
AI reviewRe: [PATCH 3/3] MIPS: perf: Add SB1 and SB1A performance counter support
In reply to Florian FainelliThank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] MIPS: perf: double hw_perf_event_destroy() call when validate_group() fails - [Medium] MIPS: perf…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] MIPS: perf: double hw_perf_event_destroy() call when validate_group() fails
- [Medium] MIPS: perf: IP7 left unmasked after irq_work_run() disables last SB1 event
- [Medium] MIPS: perf: mipspmu_event_init() returns -EOPNOTSUPP before checking attr.type
- [Low] docs: mips: sb1-pmu: heading underline too short for section title
--
commit 4595e65c6c56d05599f7d396b72896c6da74a174
Author: Florian Fainelli <[email protected]>
MIPS: perf: Add SB1 and SB1A performance counter support
This commit adds MIPS PMU support for the four CPU-local performance counters on SB1 and SB1A processors, along with sysfs event aliases and documentation.
Quoted 6 lines
Documentation/arch/mips/sb1-pmu.rst new file +0
-0@@ -0,0 +1,162 @@
[ ... ]
Quoted 2 lines
+Counter selection and mode filtering
+-----------------------------------
[Severity: Low]
The hyphen underline here is 35 characters for a 36-character heading, which triggers a Sphinx "Title underline too short" warning during htmldocs builds.
Quoted 4 lines
arch/mips/kernel/perf_event_mipsxx.c +0
-0
[ ... ]
Quoted 16 lines
arch/mips/kernel/perf_event_mipsxx.c +8
-0@@ -564,6 +627,14 @@ static void mipspmu_enable(struct pmu *pmu)write_unlock (&pmuint_rwlock);#endif resume_local_counters (); +if (is_sb1_pmu () && + !bitmap_empty (this_cpu_ptr (&cpu_hw_events)->used_mask, + mipspmu.num_counters) && + !irq_percpu_is_enabled (mipspmu.irq)) { +/* Count/Compare shares IP7, but SiByte uses a separate timer. */ +write_c0_compare (read_c0_count () -1 ); +enable_percpu_irq (mipspmu.irq,IRQ_TYPE_NONE ); + } }/*
arch/mips/kernel/perf_event_mipsxx.c static void mipspmu_enable(struct pmu *pmu) · line 657
Quoted 8 lines
arch/mips/kernel/perf_event_mipsxx.c +2
-0@@ -579,6 +650,8 @@ static void mipspmu_enable(struct pmu *pmu)*/ static void mipspmu_disable (struct pmu *pmu) { +if (is_sb1_pmu () &&irq_percpu_is_enabled (mipspmu.irq)) +disable_percpu_irq (mipspmu.irq);pause_local_counters ();#ifdef CONFIG_MIPS_PERF_SHARED_TC_COUNTERS write_lock (&pmuint_rwlock);
[Severity: Medium]
Can this leave IP7 unmasked in hardware when the last event on a CPU is disabled from inside the PMU interrupt handler?
handle_percpu_devid_irq()
chip->irq_ack() /* mask_mips_irq() masks IP7 */
mipsxx_pmu_handle_irq()
mipsxx_pmu_handle_shared_irq()
handle_associated_event()
perf_event_overflow() /* queues pending_disable_irq */
irq_work_run()
perf_pending_disable()
...
mipspmu_disable()
disable_percpu_irq() /* clears percpu_enabled, masks IP7 */
mipspmu_del() /* clears last bit in used_mask */
mipspmu_enable() /* used_mask empty, skips enable */
chip->irq_eoi()
unmask_mips_irq() <-- unmasks IP7 in CP0 Status
When a sampling event with a refresh limit (PERF_EVENT_IOC_REFRESH) overflows and auto-disables via irq_work_run(), disable_percpu_irq() clears the CPU in desc->percpu_enabled, but handle_percpu_devid_irq() then runs chip->irq_eoi() (unmask_mips_irq()), which sets STATUSF_IP7 again. That leaves IP7 unmasked with no events scheduled while irq_percpu_is_enabled() returns false, so spurious interrupts fire whenever CP0 Count wraps past Compare and sb1_pmu_cpu_offline() will also skip masking IP7.
[ ... ]
arch/mips/kernel/perf_event_mipsxx.c static int mipspmu_event_init(struct perf_event *event) · line 746
Quoted 11 lines
arch/mips/kernel/perf_event_mipsxx.c +4
-0@@ -657,6 +737,10 @@ static int mipspmu_event_init(struct perf_event *event)if (has_branch_stack (event))return -EOPNOTSUPP ; +if (is_sb1_pmu () && (event->attr.precise_ip || event->attr.config1 || + event->attr.config2)) +return -EOPNOTSUPP ; +switch (event->attr.type) {case PERF_TYPE_RAW :case PERF_TYPE_HARDWARE :
[Severity: Medium]
Should the precise_ip, config1 and config2 check come after the event->attr.type switch?
Checking those fields first makes mipspmu_event_init() return -EOPNOTSUPP instead of -ENOENT for events that do not target this PMU (for example, PERF_TYPE_BREAKPOINT where bp_addr and bp_len union with config1 and config2, or any PMU registered after mipspmu), causing perf_init_event() to abort its PMU list walk early.
arch/mips/kernel/perf_event_mipsxx.c static int mipspmu_event_init(struct perf_event *event) · line 774
Quoted 10 lines
arch/mips/kernel/perf_event_mipsxx.c +4
-1@@ -683,7 +767,10 @@ static int mipspmu_event_init(struct perf_event *event)if (err)return err; -return __hw_perf_event_init (event); + err =__hw_perf_event_init (event); +if (err &&is_sb1_pmu () && !event->destroy) +hw_perf_event_destroy (event); +return err; }
[Severity: High]
Does a failure in validate_group() cause hw_perf_event_destroy() to run twice for the same event?
arch/mips/kernel/perf_event_mipsxx.c:__hw_perf_event_init() {
...
if (event->group_leader != event)
err = validate_group(event);
event->destroy = hw_perf_event_destroy;
if (err)
event->destroy(event);
return err;
}When validate_group() fails (such as when a fourth non-cycle event is added to a group), __hw_perf_event_init() calls event->destroy(event) and returns err with event->destroy still set to hw_perf_event_destroy. Because perf_try_init_event() skips clearing event->destroy on event_init() failure, perf_event_alloc()'s __free(__free_event) cleanup calls event->destroy(event) a second time. That decrements active_events twice for one increment, dropping active_events to zero while the group's earlier events are still open and resetting hardware counters across all CPUs via on_each_cpu(reset_counters, ...).
Also, gating the !event->destroy cleanup on is_sb1_pmu() leaves active_events leaked on non-SB1 MIPS PMUs when __hw_perf_event_init() fails before setting event->destroy. If __hw_perf_event_init() only sets event->destroy after validate_group() succeeds (or clears it after calling it), mipspmu_event_init() can handle the error cleanup in one place.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3