Skip to content

Commit 5e9629d

Browse files
james-c-linarowilldeacon
authored andcommitted
drivers/perf: arm_spe: Use perf_allow_kernel() for permissions
Use perf_allow_kernel() for 'pa_enable' (physical addresses), 'pct_enable' (physical timestamps) and context IDs. This means that perf_event_paranoid is now taken into account and LSM hooks can be used, which is more consistent with other perf_event_open calls. For example PERF_SAMPLE_PHYS_ADDR uses perf_allow_kernel() rather than just perfmon_capable(). This also indirectly fixes the following error message which is misleading because perf_event_paranoid is not taken into account by perfmon_capable(): $ perf record -e arm_spe/pa_enable/ Error: Access to performance monitoring and observability operations is limited. Consider adjusting /proc/sys/kernel/perf_event_paranoid setting ... Suggested-by: Al Grant <al.grant@arm.com> Signed-off-by: James Clark <james.clark@linaro.org> Link: https://lore.kernel.org/r/20240827145113.1224604-1-james.clark@linaro.org Link: https://lore.kernel.org/all/20240807120039.GD37996@noisy.programming.kicks-ass.net/ Signed-off-by: Will Deacon <will@kernel.org>
1 parent db9e7a8 commit 5e9629d

3 files changed

Lines changed: 14 additions & 12 deletions

File tree

drivers/perf/arm_spe_pmu.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
/*
4343
* Cache if the event is allowed to trace Context information.
44-
* This allows us to perform the check, i.e, perfmon_capable(),
44+
* This allows us to perform the check, i.e, perf_allow_kernel(),
4545
* in the context of the event owner, once, during the event_init().
4646
*/
4747
#define SPE_PMU_HW_FLAGS_CX 0x00001
@@ -50,7 +50,7 @@ static_assert((PERF_EVENT_FLAG_ARCH & SPE_PMU_HW_FLAGS_CX) == SPE_PMU_HW_FLAGS_C
5050

5151
static void set_spe_event_has_cx(struct perf_event *event)
5252
{
53-
if (IS_ENABLED(CONFIG_PID_IN_CONTEXTIDR) && perfmon_capable())
53+
if (IS_ENABLED(CONFIG_PID_IN_CONTEXTIDR) && !perf_allow_kernel(&event->attr))
5454
event->hw.flags |= SPE_PMU_HW_FLAGS_CX;
5555
}
5656

@@ -745,9 +745,8 @@ static int arm_spe_pmu_event_init(struct perf_event *event)
745745

746746
set_spe_event_has_cx(event);
747747
reg = arm_spe_event_to_pmscr(event);
748-
if (!perfmon_capable() &&
749-
(reg & (PMSCR_EL1_PA | PMSCR_EL1_PCT)))
750-
return -EACCES;
748+
if (reg & (PMSCR_EL1_PA | PMSCR_EL1_PCT))
749+
return perf_allow_kernel(&event->attr);
751750

752751
return 0;
753752
}

include/linux/perf_event.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,13 +1602,7 @@ static inline int perf_is_paranoid(void)
16021602
return sysctl_perf_event_paranoid > -1;
16031603
}
16041604

1605-
static inline int perf_allow_kernel(struct perf_event_attr *attr)
1606-
{
1607-
if (sysctl_perf_event_paranoid > 1 && !perfmon_capable())
1608-
return -EACCES;
1609-
1610-
return security_perf_event_open(attr, PERF_SECURITY_KERNEL);
1611-
}
1605+
int perf_allow_kernel(struct perf_event_attr *attr);
16121606

16131607
static inline int perf_allow_cpu(struct perf_event_attr *attr)
16141608
{

kernel/events/core.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13351,6 +13351,15 @@ const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
1335113351
return &event->attr;
1335213352
}
1335313353

13354+
int perf_allow_kernel(struct perf_event_attr *attr)
13355+
{
13356+
if (sysctl_perf_event_paranoid > 1 && !perfmon_capable())
13357+
return -EACCES;
13358+
13359+
return security_perf_event_open(attr, PERF_SECURITY_KERNEL);
13360+
}
13361+
EXPORT_SYMBOL_GPL(perf_allow_kernel);
13362+
1335413363
/*
1335513364
* Inherit an event from parent task to child task.
1335613365
*

0 commit comments

Comments
 (0)