Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf: RISC-V: Limit the number of counters returned from SBI

Perf gets the number of supported counters from SBI. If it happens that
the number of returned counters more than RISCV_MAX_COUNTERS the code
trusts it. It does not lead to an immediate problem but can potentially
lead to it. Prevent getting more than RISCV_MAX_COUNTERS from SBI.

Signed-off-by: Viacheslav Mitrofanov <v.v.mitrofanov@yadro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Link: https://lore.kernel.org/r/20230505072058.1049732-1-v.v.mitrofanov@yadro.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>

authored by

Viacheslav Mitrofanov and committed by
Palmer Dabbelt
ee95b88d 4681daca

+6
+6
drivers/perf/riscv_pmu_sbi.c
··· 855 855 goto out_free; 856 856 } 857 857 858 + /* It is possible to get from SBI more than max number of counters */ 859 + if (num_counters > RISCV_MAX_COUNTERS) { 860 + num_counters = RISCV_MAX_COUNTERS; 861 + pr_info("SBI returned more than maximum number of counters. Limiting the number of counters to %d\n", num_counters); 862 + } 863 + 858 864 /* cache all the information about counters now */ 859 865 if (pmu_sbi_get_ctrinfo(num_counters, &cmask)) 860 866 goto out_free;