drivers/perf: arm_spe: Use perf_allow_kernel() for permissions
Subsystems (2)
James Clark
reviewerPATCH v2 1/1 drivers/perf: arm_spe: Use perf_allow_kernel() for permissions
For other PMUs, PERF_SAMPLE_PHYS_ADDR requires perf_allow_kernel() rather than just perfmon_capable(). Because PMSCR_EL1_PA is another form of physical address, make it consistent and use perf_allow_kernel() for SPE as well. PMSCR_EL1_PCT and PMSCR_EL1_CX also get the same change.
This improves consistency and 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 ...
---
Changes since v1:
* Export perf_allow_kernel() instead of sysctl_perf_event_paranoid
drivers/perf/arm_spe_pmu.c | 9 ++++----- include/linux/perf_event.h | 8 +------- kernel/events/core.c | 9 +++++++++ 3 files changed, 14 insertions(+), 12 deletions(-)
drivers/perf/arm_spe_pmu.c +4 -5
@@ -41,7 +41,7 @@/* * Cache if the event is allowed to trace Context information. -* This allows us to perform the check, i.e, perfmon_capable(), +* This allows us to perform the check, i.e, perf_allow_kernel(), * in the context of the event owner, once, during the event_init(). */ #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_Cstatic void set_spe_event_has_cx (struct perf_event *event) { -if (IS_ENABLED (CONFIG_PID_IN_CONTEXTIDR ) &&perfmon_capable ()) +if (IS_ENABLED (CONFIG_PID_IN_CONTEXTIDR ) && !perf_allow_kernel (&event->attr)) event->hw.flags |=SPE_PMU_HW_FLAGS_CX ; }
@@ -745,9 +745,8 @@ static int arm_spe_pmu_event_init(struct perf_event *event)set_spe_event_has_cx (event); reg =arm_spe_event_to_pmscr (event); -if (!perfmon_capable () && - (reg & (PMSCR_EL1_PA |PMSCR_EL1_PCT ))) -return -EACCES ; +if (reg & (PMSCR_EL1_PA |PMSCR_EL1_PCT )) +return perf_allow_kernel (&event->attr);return 0 ; }
include/linux/perf_event.h +1 -7
@@ -1602,13 +1602,7 @@ static inline int perf_is_paranoid(void)return sysctl_perf_event_paranoid >-1 ; } -static inline int perf_allow_kernel (struct perf_event_attr *attr) -{ -if (sysctl_perf_event_paranoid >1 && !perfmon_capable ()) -return -EACCES ; - -return security_perf_event_open (attr,PERF_SECURITY_KERNEL ); -} +int perf_allow_kernel (struct perf_event_attr *attr);static inline int perf_allow_cpu (struct perf_event_attr *attr) {
kernel/events/core.c +9 -0
@@ -13351,6 +13351,15 @@ const struct perf_event_attr *perf_event_attrs(struct perf_event *event)return &event->attr; } +int perf_allow_kernel (struct perf_event_attr *attr) +{ +if (sysctl_perf_event_paranoid >1 && !perfmon_capable ()) +return -EACCES ; + +return security_perf_event_open (attr,PERF_SECURITY_KERNEL ); +} +EXPORT_SYMBOL_GPL (perf_allow_kernel); +/* * Inherit an event from parent task to child task. *
Signature · 1 line
2.34.1
Will Deacon
maintainerQuoted 42 lines · James Clark
For other PMUs,
PERF_SAMPLE_PHYS_ADDRrequiresperf_allow_kernel()rather than justperfmon_capable(). BecausePMSCR_EL1_PAis another form of physical address, make it consistent and useperf_allow_kernel()for SPE as well.PMSCR_EL1_PCTandPMSCR_EL1_CXalso get the same change.This improves consistency and indirectly fixes the following error message which is misleading because
perf_event_paranoidis not taken into account byperfmon_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_paranoidsetting ...Suggested-by Al Grant <[email protected]>Signed-off-by James Clark <[email protected]>---
Changes since v1:* Export
perf_allow_kernel()instead ofsysctl_perf_event_paranoiddrivers/perf/arm_spe_pmu.c | 9 ++++----- include/linux/perf_event.h | 8 +------- kernel/events/core.c | 9 +++++++++ 3 files changed, 14 insertions(+), 12 deletions(-)drivers/perf/arm_spe_pmu.c +1
-1@@ -41,7 +41,7 @@/* * Cache if the event is allowed to trace Context information. -* This allows us to perform the check, i.e, perfmon_capable(), +* This allows us to perform the check, i.e, perf_allow_kernel(), * in the context of the event owner, once, during the event_init(). */ #define SPE_PMU_HW_FLAGS_CX 0x00001
drivers/perf/arm_spe_pmu.c static_assert((PERF_EVENT_FLAG_ARCH & SPE_PMU_HW_FLAGS_CX) == SPE_PMU_HW_FLAGS_C · line 54
Quoted 6 lines
drivers/perf/arm_spe_pmu.c +1
-1@@ -50,7 +50,7 @@ static_assert((PERF_EVENT_FLAG_ARCH & SPE_PMU_HW_FLAGS_CX) == SPE_PMU_HW_FLAGS_Cstatic void set_spe_event_has_cx (struct perf_event *event) { -if (IS_ENABLED (CONFIG_PID_IN_CONTEXTIDR ) &&perfmon_capable ()) +if (IS_ENABLED (CONFIG_PID_IN_CONTEXTIDR ) && !perf_allow_kernel (&event->attr)) event->hw.flags |=SPE_PMU_HW_FLAGS_CX ;
The rationale for this change in the commit message is because other drivers gate PERF_SAMPLE_PHYS_ADDR on perf_allow_kernel(). However, putting the PID in contextidr doesn't seem to have anything to do with that...
drivers/perf/arm_spe_pmu.c static int arm_spe_pmu_event_init(struct perf_event *event) · line 748
Quoted 10 lines
drivers/perf/arm_spe_pmu.c +1
-3}@@ -745,9 +745,8 @@ static int arm_spe_pmu_event_init(struct perf_event *event)set_spe_event_has_cx (event); reg =arm_spe_event_to_pmscr (event); -if (!perfmon_capable () && - (reg & (PMSCR_EL1_PA |PMSCR_EL1_PCT ))) -return -EACCES ; +if (reg & (PMSCR_EL1_PA |PMSCR_EL1_PCT ))
Similarly here. What does the physical counter have to do with physical address sampling other than sharing the word "physical"?
Will
James Clark
reviewerQuoted 44 lines · Will Deacon
Quoted 43 lines · James Clark
For other PMUs,
PERF_SAMPLE_PHYS_ADDRrequiresperf_allow_kernel()rather than justperfmon_capable(). BecausePMSCR_EL1_PAis another form of physical address, make it consistent and useperf_allow_kernel()for SPE as well.PMSCR_EL1_PCTandPMSCR_EL1_CXalso get the same change.This improves consistency and indirectly fixes the following error message which is misleading because
perf_event_paranoidis not taken into account byperfmon_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_paranoidsetting ...Suggested-by Al Grant <[email protected]>Signed-off-by James Clark <[email protected]>---
Changes since v1:* Export
perf_allow_kernel()instead ofsysctl_perf_event_paranoiddrivers/perf/arm_spe_pmu.c | 9 ++++----- include/linux/perf_event.h | 8 +------- kernel/events/core.c | 9 +++++++++ 3 files changed, 14 insertions(+), 12 deletions(-)drivers/perf/arm_spe_pmu.c +1
-1@@ -41,7 +41,7 @@/* * Cache if the event is allowed to trace Context information. -* This allows us to perform the check, i.e, perfmon_capable(), +* This allows us to perform the check, i.e, perf_allow_kernel(), * in the context of the event owner, once, during the event_init(). */ #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
Quoted 11 lines
drivers/perf/arm_spe_pmu.c static_assert((PERF_EVENT_FLAG_ARCH & SPE_PMU_HW_FLAGS_CX) == SPE_PMU_HW_FLAGS_C · line 54
Quoted 5 lines
drivers/perf/arm_spe_pmu.c +1
-1static void set_spe_event_has_cx (struct perf_event *event) { -if (IS_ENABLED (CONFIG_PID_IN_CONTEXTIDR ) &&perfmon_capable ()) +if (IS_ENABLED (CONFIG_PID_IN_CONTEXTIDR ) && !perf_allow_kernel (&event->attr)) event->hw.flags |=SPE_PMU_HW_FLAGS_CX ;The rationale for this change in the commit message is because other drivers gate
PERF_SAMPLE_PHYS_ADDRonperf_allow_kernel(). However, putting the PID in contextidr doesn't seem to have anything to do with that...
That is true, I suppose I was thinking of two reasons to do it this way that I didn't really elaborate on:
#1 because context IDs and physical timestamps didn't seem to be any more sensitive than physical addresses, so it wouldn't make sense for them to have a stricter permissions model than addresses.
#2 (although this is indirect and not really related to the driver) but Perf will still print the misleading warning when physical timestamps are requested. So some other fix would eventually have to be made for that.
I'm not sure if you are objecting to the permissions change for the other two things, or it's just a lack of reasoning in the commit message?
IMO if we think the other two can't be changed, I would actually rather drop the change than only target PERF_SAMPLE_PHYS_ADDR. Because that seems like it unnecessarily complicates the permissions and might be quite surprising to a user. And then maybe some attempt of a fix could be made in Perf instead. Although that could be difficult because of the lack of a specific error code from the driver.
Quoted 15 lines
drivers/perf/arm_spe_pmu.c static int arm_spe_pmu_event_init(struct perf_event *event) · line 748
Quoted 10 lines
drivers/perf/arm_spe_pmu.c +1
-3}@@ -745,9 +745,8 @@ static int arm_spe_pmu_event_init(struct perf_event *event)set_spe_event_has_cx (event); reg =arm_spe_event_to_pmscr (event); -if (!perfmon_capable () && - (reg & (PMSCR_EL1_PA |PMSCR_EL1_PCT ))) -return -EACCES ; +if (reg & (PMSCR_EL1_PA |PMSCR_EL1_PCT ))Similarly here. What does the physical counter have to do with physical address sampling other than sharing the word "physical"?
Will
Will Deacon
maintainerQuoted 64 lines · James Clark
Quoted 44 lines · Will Deacon
Quoted 43 lines · James Clark
For other PMUs,
PERF_SAMPLE_PHYS_ADDRrequiresperf_allow_kernel()rather than justperfmon_capable(). BecausePMSCR_EL1_PAis another form of physical address, make it consistent and useperf_allow_kernel()for SPE as well.PMSCR_EL1_PCTandPMSCR_EL1_CXalso get the same change.This improves consistency and indirectly fixes the following error message which is misleading because
perf_event_paranoidis not taken into account byperfmon_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_paranoidsetting ...Suggested-by Al Grant <[email protected]>Signed-off-by James Clark <[email protected]>---
Changes since v1:* Export
perf_allow_kernel()instead ofsysctl_perf_event_paranoiddrivers/perf/arm_spe_pmu.c | 9 ++++----- include/linux/perf_event.h | 8 +------- kernel/events/core.c | 9 +++++++++ 3 files changed, 14 insertions(+), 12 deletions(-)drivers/perf/arm_spe_pmu.c +1
-1@@ -41,7 +41,7 @@/* * Cache if the event is allowed to trace Context information. -* This allows us to perform the check, i.e, perfmon_capable(), +* This allows us to perform the check, i.e, perf_allow_kernel(), * in the context of the event owner, once, during the event_init(). */ #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_Cstatic void set_spe_event_has_cx (struct perf_event *event) {Quoted 9 lines
drivers/perf/arm_spe_pmu.c static_assert((PERF_EVENT_FLAG_ARCH & SPE_PMU_HW_FLAGS_CX) == SPE_PMU_HW_FLAGS_C · line 54
Quoted 3 lines
drivers/perf/arm_spe_pmu.c +1
-1-if (IS_ENABLED (CONFIG_PID_IN_CONTEXTIDR ) &&perfmon_capable ()) +if (IS_ENABLED (CONFIG_PID_IN_CONTEXTIDR ) && !perf_allow_kernel (&event->attr)) event->hw.flags |=SPE_PMU_HW_FLAGS_CX ;The rationale for this change in the commit message is because other drivers gate
PERF_SAMPLE_PHYS_ADDRonperf_allow_kernel(). However, putting the PID in contextidr doesn't seem to have anything to do with that...That is true, I suppose I was thinking of two reasons to do it this way that I didn't really elaborate on:
#1 because context IDs and physical timestamps didn't seem to be any more sensitive than physical addresses, so it wouldn't make sense for them to have a stricter permissions model than addresses.
Quoted 6 lines
#2 (although this is indirect and not really related to the driver) but Perf will still print the misleading warning when physical timestamps are requested. So some other fix would eventually have to be made for that.
I'm not sure if you are objecting to the permissions change for the other two things, or it's just a lack of reasoning in the commit message?
I'm just objecting to the rationale in the commit message not being applicable to some of the changes made in the code. A much better rationale to use perf_allow_kernel() is because of its integration with the LSM hooks.
Will