linux/arch/loongarch/kernel
Lisa Robinson df7a49b328 LoongArch: Fix PMU counter allocation for mixed-type event groups
commit a91f86e270 upstream.

When validating a perf event group, validate_group() unconditionally
attempts to allocate hardware PMU counters for the leader, sibling
events and the new event being added.

This is incorrect for mixed-type groups. If a PERF_TYPE_SOFTWARE event
is part of the group, the current code still tries to allocate a hardware
PMU counter for it, which can wrongly consume hardware PMU resources and
cause spurious allocation failures.

Fix this by only allocating PMU counters for hardware events during group
validation, and skipping software events.

A trimmed down reproducer is as simple as this:

  #include <stdio.h>
  #include <assert.h>
  #include <unistd.h>
  #include <string.h>
  #include <sys/syscall.h>
  #include <linux/perf_event.h>

  int main (int argc, char *argv[])
  {
  	struct perf_event_attr attr = { 0 };
  	int fds[5];

  	attr.disabled = 1;
  	attr.exclude_kernel = 1;
  	attr.exclude_hv = 1;
  	attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  		PERF_FORMAT_TOTAL_TIME_RUNNING | PERF_FORMAT_ID | PERF_FORMAT_GROUP;
  	attr.size = sizeof (attr);

  	attr.type = PERF_TYPE_SOFTWARE;
  	attr.config = PERF_COUNT_SW_DUMMY;
  	fds[0] = syscall (SYS_perf_event_open, &attr, 0, -1, -1, 0);
  	assert (fds[0] >= 0);

  	attr.type = PERF_TYPE_HARDWARE;
  	attr.config = PERF_COUNT_HW_CPU_CYCLES;
  	fds[1] = syscall (SYS_perf_event_open, &attr, 0, -1, fds[0], 0);
  	assert (fds[1] >= 0);

  	attr.type = PERF_TYPE_HARDWARE;
  	attr.config = PERF_COUNT_HW_INSTRUCTIONS;
  	fds[2] = syscall (SYS_perf_event_open, &attr, 0, -1, fds[0], 0);
  	assert (fds[2] >= 0);

  	attr.type = PERF_TYPE_HARDWARE;
  	attr.config = PERF_COUNT_HW_BRANCH_MISSES;
  	fds[3] = syscall (SYS_perf_event_open, &attr, 0, -1, fds[0], 0);
  	assert (fds[3] >= 0);

  	attr.type = PERF_TYPE_HARDWARE;
  	attr.config = PERF_COUNT_HW_CACHE_REFERENCES;
  	fds[4] = syscall (SYS_perf_event_open, &attr, 0, -1, fds[0], 0);
  	assert (fds[4] >= 0);

  	printf ("PASSED\n");

  	return 0;
  }

Cc: stable@vger.kernel.org
Fixes: b37042b2bb ("LoongArch: Add perf events support")
Signed-off-by: Lisa Robinson <lisa@bytefly.space>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
History 2026-01-23 11:18:47 +01:00
..
.gitignore LoongArch: Add build infrastructure 2022-06-03 20:09:27 +08:00
access-helper.h LoongArch: Add exception/interrupt handling 2022-06-03 20:09:28 +08:00
acpi.c LoongArch: Eliminate superfluous get_numa_distances_cnt() 2025-04-25 10:48:05 +02:00
alternative.c LoongArch: Use common function sign_extend64() 2023-01-17 11:42:16 +08:00
asm-offsets.c arch: Add the macro COMPILE_OFFSETS to all the asm-offsets.c 2025-11-02 22:15:22 +09:00
cacheinfo.c LoongArch: Refactor cache probe and flush methods 2022-10-12 16:36:14 +08:00
cpu-probe.c LoongArch: Rework CPU feature probe from CPUCFG/IOCSR 2024-09-24 15:32:20 +08:00
crash_dump.c LoongArch: Add kdump support 2022-10-12 16:36:19 +08:00
dma.c dma-mapping: Add helpers for dma_range_map bounds 2024-04-26 12:07:24 +02:00
efi-header.S LoongArch: Calculate various sizes in the linker script 2023-06-29 20:58:42 +08:00
efi.c LoongArch: Fix reserving screen info memory for above-4G firmware 2025-01-02 10:34:18 +01:00
elf.c LoongArch: Fix and simplify fcsr initialization on execve() 2024-01-17 12:43:08 +08:00
entry.S LoongArch: Add ORC stack unwinder support 2024-03-11 22:23:47 +08:00
env.c LoongArch: Check the return value when creating kobj 2025-09-25 11:13:46 +02:00
fpu.S LoongArch: Handle fp, lsx, lasx and lbt assembly symbols 2025-05-02 07:59:07 +02:00
ftrace.c LoongArch/ftrace: Add basic support 2022-12-14 08:41:53 +08:00
ftrace_dyn.c kprobe/ftrace: bail out if ftrace was killed 2024-05-16 07:23:30 +09:00
genex.S LoongArch: Move __arch_cpu_idle() to .cpuidle.text section 2025-05-22 14:29:44 +02:00
head.S LoongArch: Add writecombine support for DMW-based ioremap() 2024-07-20 22:40:59 +08:00
hw_breakpoint.c LoongArch: Change 8 to 14 for LOONGARCH_MAX_{BRP,WRP} 2025-02-08 09:58:19 +01:00
idle.c LoongArch: Fix idle VS timer enqueue 2025-02-21 14:01:17 +01:00
image-vars.h efi/loongarch: Directly position the loaded image file 2023-12-19 11:16:37 +01:00
inst.c LoongArch: Handle new atomic instructions for probes 2025-11-13 15:34:34 -05:00
io.c LoongArch: Add misc common routines 2022-06-03 20:09:28 +08:00
irq.c Merge tag 'irq-core-2024-09-16' into loongarch-next 2024-09-17 22:20:12 +08:00
jump_label.c LoongArch: Add jump-label implementation 2023-06-29 20:58:44 +08:00
kfpu.c LoongArch: Prevent cond_resched() occurring within kernel-fpu 2025-05-22 14:29:44 +02:00
kgdb.c LoongArch: Rework the arch_kgdb_breakpoint() implementation 2025-04-10 14:39:27 +02:00
kprobes.c LoongArch: Make the users of larch_insn_gen_break() constant 2024-07-20 22:41:07 +08:00
lbt.S LoongArch: Handle fp, lsx, lasx and lbt assembly symbols 2025-05-02 07:59:07 +02:00
machine_kexec.c LoongArch: Add machine_kexec_mask_interrupts() implementation 2025-12-18 13:55:22 +01:00
Makefile Fix mis-uses of 'cc-option' for warning disablement 2025-05-29 11:03:25 +02:00
Makefile.syscalls syscalls: fix syscall macros for newfstat/newfstatat 2024-08-02 15:20:47 +02:00
mcount.S LoongArch: Replace #include <asm/export.h> with #include <linux/export.h> 2023-08-25 23:40:26 +08:00
mcount_dyn.S LoongArch: Refactor register restoration in ftrace_common_return 2026-01-08 10:14:51 +01:00
mem.c LoongArch: Set all reserved memblocks on Node#0 at initialization 2023-09-20 14:26:29 +08:00
module-sections.c LoongArch: Optimize module load time by optimizing PLT/GOT counting 2025-08-28 16:31:14 +02:00
module.c arch: make execmem setup available regardless of CONFIG_MODULES 2024-05-14 00:31:44 -07:00
numa.c arch, mm: pull out allocation of NODE_DATA to generic code 2024-09-03 21:15:28 -07:00
paravirt.c LoongArch: Fix AP booting issue in VM mode 2024-11-12 16:35:39 +08:00
perf_event.c LoongArch: Fix PMU counter allocation for mixed-type event groups 2026-01-23 11:18:47 +01:00
perf_regs.c LoongArch: Add perf events support 2022-10-12 16:36:14 +08:00
proc.c LoongArch: Rework CPU feature probe from CPUCFG/IOCSR 2024-09-24 15:32:20 +08:00
process.c LoongArch: Don't crash in stack_top() for tasks without vDSO 2024-10-21 22:11:19 +08:00
ptrace.c LoongArch: Extend the maximum number of watchpoints 2025-02-17 10:05:18 +01:00
relocate.c LoongArch: Use unsigned long for _end and _text 2026-01-08 10:14:47 +01:00
relocate_kernel.S LoongArch: Make relocate_new_kernel_size be a .quad value 2025-08-20 18:30:14 +02:00
reset.c LoongArch: Fix idle VS timer enqueue 2025-02-21 14:01:17 +01:00
rethook.c LoongArch: Replace kretprobe with rethook 2023-06-29 20:58:44 +08:00
rethook.h LoongArch: Replace kretprobe with rethook 2023-06-29 20:58:44 +08:00
rethook_trampoline.S LoongArch: Add ORC stack unwinder support 2024-03-11 22:23:47 +08:00
setup.c LoongArch: Correct the calculation logic of thread_count 2026-01-08 10:14:47 +01:00
signal.c LoongArch: Save LBT before FPU in setup_sigcontext() 2025-09-09 18:58:02 +02:00
smp.c LoongArch: Use polling play_dead() when resuming from hibernation 2025-03-13 13:01:50 +01:00
stacktrace.c LoongArch: Fix unreliable stack for live patching 2025-09-25 11:13:46 +02:00
switch.S LoongArch: Fix build errors for CONFIG_RANDSTRUCT 2026-01-08 10:14:47 +01:00
syscall.c LoongArch: Remove STACK_FRAME_NON_STANDARD(do_syscall) 2024-09-17 22:23:10 +08:00
sysrq.c tty: sysrq: switch sysrq handlers from int to u8 2023-07-25 19:21:03 +02:00
time.c LoongArch: Save and restore CSR.CNTC for hibernation 2025-05-22 14:29:44 +02:00
topology.c LoongArch: convert to use arch_cpu_is_hotpluggable() 2023-12-06 12:41:50 +09:00
traps.c LoongArch: Use physical addresses for CSR_MERRENTRY/CSR_TLBRENTRY 2025-11-24 10:36:02 +01:00
unaligned.c LoongArch: Delete unnecessary debugfs checking 2023-06-29 20:58:42 +08:00
unwind.c LoongArch: Set unwind stack type to unknown rather than set error flag 2023-12-09 15:49:15 +08:00
unwind_guess.c LoongArch: Strip guess unwinder out from prologue unwinder 2023-01-17 11:42:16 +08:00
unwind_orc.c LoongArch: Don't use %pK through printk() in unwinder 2025-08-20 18:30:14 +02:00
unwind_prologue.c LoongArch: Set unwind stack type to unknown rather than set error flag 2023-12-09 15:49:15 +08:00
uprobes.c LoongArch: uprobes: Remove redundant code about resume_era 2025-05-22 14:29:45 +02:00
vdso.c LoongArch: vDSO: Check kcalloc() result in init_vdso() 2025-09-25 11:13:46 +02:00
vmlinux.lds.S LoongArch: Add support for relocating the kernel with RELR relocation 2024-07-20 22:41:07 +08:00