Skip to content

Commit 682dc13

Browse files
vdsh-scpalmer-dabbelt
authored andcommitted
drivers: perf: ctr_get_width function for legacy is not defined
With parameters CONFIG_RISCV_PMU_LEGACY=y and CONFIG_RISCV_PMU_SBI=n linux kernel crashes when you try perf record: $ perf record ls [ 46.749286] Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000 [ 46.750199] Oops [#1] [ 46.750342] Modules linked in: [ 46.750608] CPU: 0 PID: 107 Comm: perf-exec Not tainted 6.6.0 #2 [ 46.750906] Hardware name: riscv-virtio,qemu (DT) [ 46.751184] epc : 0x0 [ 46.751430] ra : arch_perf_update_userpage+0x54/0x13e [ 46.751680] epc : 0000000000000000 ra : ffffffff8072ee52 sp : ff2000000022b8f0 [ 46.751958] gp : ffffffff81505988 tp : ff6000000290d400 t0 : ff2000000022b9c0 [ 46.752229] t1 : 0000000000000001 t2 : 0000000000000003 s0 : ff2000000022b930 [ 46.752451] s1 : ff600000028fb000 a0 : 0000000000000000 a1 : ff600000028fb000 [ 46.752673] a2 : 0000000ae2751268 a3 : 00000000004fb708 a4 : 0000000000000004 [ 46.752895] a5 : 0000000000000000 a6 : 000000000017ffe3 a7 : 00000000000000d2 [ 46.753117] s2 : ff600000028fb000 s3 : 0000000ae2751268 s4 : 0000000000000000 [ 46.753338] s5 : ffffffff8153e290 s6 : ff600000863b9000 s7 : ff60000002961078 [ 46.753562] s8 : ff60000002961048 s9 : ff60000002961058 s10: 0000000000000001 [ 46.753783] s11: 0000000000000018 t3 : ffffffffffffffff t4 : ffffffffffffffff [ 46.754005] t5 : ff6000000292270c t6 : ff2000000022bb30 [ 46.754179] status: 0000000200000100 badaddr: 0000000000000000 cause: 000000000000000c [ 46.754653] Code: Unable to access instruction at 0xffffffffffffffec. [ 46.754939] ---[ end trace 0000000000000000 ]--- [ 46.755131] note: perf-exec[107] exited with irqs disabled [ 46.755546] note: perf-exec[107] exited with preempt_count 4 This happens because in the legacy case the ctr_get_width function was not defined, but it is used in arch_perf_update_userpage. Also remove extra check in riscv_pmu_ctr_get_width_mask Signed-off-by: Vadim Shakirov <vadim.shakirov@syntacore.com> Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com> Reviewed-by: Atish Patra <atishp@rivosinc.com> Fixes: cc4c07c ("drivers: perf: Implement perf event mmap support in the SBI backend") Link: https://lore.kernel.org/r/20240227170002.188671-3-vadim.shakirov@syntacore.com Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent 65730fe commit 682dc13

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

drivers/perf/riscv_pmu.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,19 +150,11 @@ u64 riscv_pmu_ctr_get_width_mask(struct perf_event *event)
150150
struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
151151
struct hw_perf_event *hwc = &event->hw;
152152

153-
if (!rvpmu->ctr_get_width)
154-
/**
155-
* If the pmu driver doesn't support counter width, set it to default
156-
* maximum allowed by the specification.
157-
*/
158-
cwidth = 63;
159-
else {
160-
if (hwc->idx == -1)
161-
/* Handle init case where idx is not initialized yet */
162-
cwidth = rvpmu->ctr_get_width(0);
163-
else
164-
cwidth = rvpmu->ctr_get_width(hwc->idx);
165-
}
153+
if (hwc->idx == -1)
154+
/* Handle init case where idx is not initialized yet */
155+
cwidth = rvpmu->ctr_get_width(0);
156+
else
157+
cwidth = rvpmu->ctr_get_width(hwc->idx);
166158

167159
return GENMASK_ULL(cwidth, 0);
168160
}

drivers/perf/riscv_pmu_legacy.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ static int pmu_legacy_event_map(struct perf_event *event, u64 *config)
3737
return pmu_legacy_ctr_get_idx(event);
3838
}
3939

40+
/* cycle & instret are always 64 bit, one bit less according to SBI spec */
41+
static int pmu_legacy_ctr_get_width(int idx)
42+
{
43+
return 63;
44+
}
45+
4046
static u64 pmu_legacy_read_ctr(struct perf_event *event)
4147
{
4248
struct hw_perf_event *hwc = &event->hw;
@@ -111,7 +117,7 @@ static void pmu_legacy_init(struct riscv_pmu *pmu)
111117
pmu->ctr_stop = NULL;
112118
pmu->event_map = pmu_legacy_event_map;
113119
pmu->ctr_get_idx = pmu_legacy_ctr_get_idx;
114-
pmu->ctr_get_width = NULL;
120+
pmu->ctr_get_width = pmu_legacy_ctr_get_width;
115121
pmu->ctr_clear_idx = NULL;
116122
pmu->ctr_read = pmu_legacy_read_ctr;
117123
pmu->event_mapped = pmu_legacy_event_mapped;

0 commit comments

Comments
 (0)