perf: Relex privilege restriction on AMD IBS
Subsystems (3)
Namhyung Kim
maintainerv6 cover perf: Relex privilege restriction on AMD IBS
Hello,
This is v6 to allow AMD IBS to regular users on the default settings where kernel-level profiling is disabled (perf_event_paranoid=2). Currently AMD IBS doesn't allow any kind of exclusion in the event attribute. But users needs to set attr.exclude_kernel to open an event on such an environment.
v6 changes)
* add Reviewed-and-tested-by from Ravi
v5) https://lore.kernel.org/lkml/[email protected]
* drop PERF_FORMAT_DROPPED support for now
* add Acked-by from Thomas for s390
v4) https://lore.kernel.org/lkml/[email protected]
* remove RFC tag
* fix sysfs attribute for ibs_fetch/format (Ravi)
* handle exclude_hv as well, so ":u" modifier would work for IBS
* add Acked and Reviewed-by from Kyle and Madhavan
v3) https://lore.kernel.org/lkml/[email protected]
* fix build on s390
* add swfilt format for attr.config2
* count powerpc core-book3s dropped samples
v2) https://lore.kernel.org/lkml/[email protected]/
* add PERF_FORMAT_DROPPED
* account dropped sw events and from BPF handler
* use precise RIP from IBS record
v1) https://lore.kernel.org/lkml/[email protected]/
While IBS doesn't support hardware level privilege filters, the kernel can allow the event and drop samples belongs to the kernel like in the software events. This is limited but it still contains precise samples which is important for various analysis like data type profiling.
This version added format/swfilt file in sysfs to expose the software filtering by setting the attribute config2 value. I think it's easier to add a new config rather than adding a new PMU in order to handle event multiplexing across IBS PMU. Users can use the perf tool to enable this feature manually like below. Probably the perf tool can handle this automatically in the future.
$ perf record -e ibs_op/swfilt=1/u $PROG
Let me know what you think.
Thanks,
Namhyung
Namhyung Kim (2):
perf/core: Export perf_exclude_event() perf/x86: Relax privilege filter restriction on AMD IBS
arch/s390/kernel/perf_cpum_sf.c | 6 ++-- arch/x86/events/amd/ibs.c | 59 +++++++++++++++++++++++---------- include/linux/perf_event.h | 6 ++++ kernel/events/core.c | 3 +- 4 files changed, 51 insertions(+), 23 deletions(-)
Signature · 1 line
2.47.0.338.g60cca15819-goog
Namhyung Kim
maintainerPATCH v6 1/2 perf/core: Export perf_exclude_event()
While at it, rename the same function in s390 cpum_sf PMU.
--- arch/s390/kernel/perf_cpum_sf.c | 6 +++--- include/linux/perf_event.h | 6 ++++++ kernel/events/core.c | 3 +-- 3 files changed, 10 insertions(+), 5 deletions(-)
arch/s390/kernel/perf_cpum_sf.c +3 -3
@@ -981,7 +981,7 @@ static void cpumsf_pmu_disable(struct pmu *pmu) cpuhw->flags &= ~PMU_F_ENABLED ; } -/* perf_exclude_event() - Filter event +/* perf_event_exclude() - Filter event * @event: The perf event * @regs: pt_regs structure * @sde_regs: Sample-data-entry (sde) regs structure
@@ -990,7 +990,7 @@ static void cpumsf_pmu_disable(struct pmu *pmu)* * Return non-zero if the event shall be excluded. */ -static int perf_exclude_event (struct perf_event *event,struct pt_regs *regs, +static int perf_event_exclude (struct perf_event *event,struct pt_regs *regs,struct perf_sf_sde_regs *sde_regs) {if (event->attr.exclude_user &&user_mode (regs))
@@ -1073,7 +1073,7 @@ static int perf_push_sample(struct perf_event *event, data.tid_entry.pid = basic->hpp &LPP_PID_MASK ; overflow =0 ; -if (perf_exclude_event (event, ®s, sde_regs)) +if (perf_event_exclude (event, ®s, sde_regs)) goto out;if (perf_event_overflow (event, &data, ®s)) { overflow =1 ;
include/linux/perf_event.h +6 -0
@@ -1690,6 +1690,8 @@ static inline int perf_allow_tracepoint(struct perf_event_attr *attr)return security_perf_event_open (attr,PERF_SECURITY_TRACEPOINT ); } +extern int perf_exclude_event (struct perf_event *event,struct pt_regs *regs); +extern void perf_event_init (void ); extern void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size, struct pt_regs *regs,
@@ -1895,6 +1897,10 @@ static inline u64 perf_event_pause(struct perf_event *event, bool reset) {return 0 ; } +static inline int perf_exclude_event (struct perf_event *event,struct pt_regs *regs) +{ +return 0 ; +}#endif #if defined(CONFIG_PERF_EVENTS ) && defined(CONFIG_CPU_SUP_INTEL )
kernel/events/core.c +1 -2
@@ -10081,8 +10081,7 @@ static void perf_swevent_event(struct perf_event *event, u64 nr,perf_swevent_overflow (event,0 , data, regs); } -static int perf_exclude_event (struct perf_event *event, -struct pt_regs *regs) +int perf_exclude_event (struct perf_event *event,struct pt_regs *regs) {if (event->hw.state &PERF_HES_STOPPED )return 1 ;
Signature · 1 line
2.47.0.338.g60cca15819-goog
[tip: perf/core] perf/core: Export perf_exclude_event()
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 370514034cc0a38270c1717eda7f598422fd5c4c Gitweb: https://git.kernel.org/tip/370514034cc0a38270c1717eda7f598422fd5c4c Author: Namhyung Kim <[email protected]> AuthorDate: Tue, 03 Dec 2024 10:04:40 -08:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Fri, 06 Dec 2024 10:32:44 +01:00
perf/core: Export perf_exclude_event()
While at it, rename the same function in s390 cpum_sf PMU.
--- arch/s390/kernel/perf_cpum_sf.c | 6 +++--- include/linux/perf_event.h | 6 ++++++ kernel/events/core.c | 3 +-- 3 files changed, 10 insertions(+), 5 deletions(-)
arch/s390/kernel/perf_cpum_sf.c +3 -3
@@ -981,7 +981,7 @@ static void cpumsf_pmu_disable(struct pmu *pmu) cpuhw->flags &= ~PMU_F_ENABLED ; } -/* perf_exclude_event() - Filter event +/* perf_event_exclude() - Filter event * @event: The perf event * @regs: pt_regs structure * @sde_regs: Sample-data-entry (sde) regs structure
@@ -990,7 +990,7 @@ static void cpumsf_pmu_disable(struct pmu *pmu)* * Return non-zero if the event shall be excluded. */ -static int perf_exclude_event (struct perf_event *event,struct pt_regs *regs, +static int perf_event_exclude (struct perf_event *event,struct pt_regs *regs,struct perf_sf_sde_regs *sde_regs) {if (event->attr.exclude_user &&user_mode (regs))
@@ -1073,7 +1073,7 @@ static int perf_push_sample(struct perf_event *event, data.tid_entry.pid = basic->hpp &LPP_PID_MASK ; overflow =0 ; -if (perf_exclude_event (event, ®s, sde_regs)) +if (perf_event_exclude (event, ®s, sde_regs)) goto out;if (perf_event_overflow (event, &data, ®s)) { overflow =1 ;
include/linux/perf_event.h +6 -0
@@ -1690,6 +1690,8 @@ static inline int perf_allow_tracepoint(struct perf_event_attr *attr)return security_perf_event_open (attr,PERF_SECURITY_TRACEPOINT ); } +extern int perf_exclude_event (struct perf_event *event,struct pt_regs *regs); +extern void perf_event_init (void ); extern void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size, struct pt_regs *regs,
@@ -1895,6 +1897,10 @@ static inline u64 perf_event_pause(struct perf_event *event, bool reset) {return 0 ; } +static inline int perf_exclude_event (struct perf_event *event,struct pt_regs *regs) +{ +return 0 ; +}#endif #if defined(CONFIG_PERF_EVENTS ) && defined(CONFIG_CPU_SUP_INTEL )
kernel/events/core.c +1 -2
@@ -10081,8 +10081,7 @@ static void perf_swevent_event(struct perf_event *event, u64 nr,perf_swevent_overflow (event,0 , data, regs); } -static int perf_exclude_event (struct perf_event *event, -struct pt_regs *regs) +int perf_exclude_event (struct perf_event *event,struct pt_regs *regs) {if (event->hw.state &PERF_HES_STOPPED )return 1 ;
[tip: perf/core] perf/core: Export perf_exclude_event()
In reply to Namhyung KimThe following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 3807d5c928f46b27de80b67de4aae15699d19bec Gitweb: https://git.kernel.org/tip/3807d5c928f46b27de80b67de4aae15699d19bec Author: Namhyung Kim <[email protected]> AuthorDate: Tue, 03 Dec 2024 10:04:40 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Mon, 09 Dec 2024 11:18:08 +01:00
perf/core: Export perf_exclude_event()
While at it, rename the same function in s390 cpum_sf PMU.
--- arch/s390/kernel/perf_cpum_sf.c | 6 +++--- include/linux/perf_event.h | 6 ++++++ kernel/events/core.c | 3 +-- 3 files changed, 10 insertions(+), 5 deletions(-)
arch/s390/kernel/perf_cpum_sf.c +3 -3
@@ -981,7 +981,7 @@ static void cpumsf_pmu_disable(struct pmu *pmu) cpuhw->flags &= ~PMU_F_ENABLED ; } -/* perf_exclude_event() - Filter event +/* perf_event_exclude() - Filter event * @event: The perf event * @regs: pt_regs structure * @sde_regs: Sample-data-entry (sde) regs structure
@@ -990,7 +990,7 @@ static void cpumsf_pmu_disable(struct pmu *pmu)* * Return non-zero if the event shall be excluded. */ -static int perf_exclude_event (struct perf_event *event,struct pt_regs *regs, +static int perf_event_exclude (struct perf_event *event,struct pt_regs *regs,struct perf_sf_sde_regs *sde_regs) {if (event->attr.exclude_user &&user_mode (regs))
@@ -1073,7 +1073,7 @@ static int perf_push_sample(struct perf_event *event, data.tid_entry.pid = basic->hpp &LPP_PID_MASK ; overflow =0 ; -if (perf_exclude_event (event, ®s, sde_regs)) +if (perf_event_exclude (event, ®s, sde_regs)) goto out;if (perf_event_overflow (event, &data, ®s)) { overflow =1 ;
include/linux/perf_event.h +6 -0
@@ -1690,6 +1690,8 @@ static inline int perf_allow_tracepoint(struct perf_event_attr *attr)return security_perf_event_open (attr,PERF_SECURITY_TRACEPOINT ); } +extern int perf_exclude_event (struct perf_event *event,struct pt_regs *regs); +extern void perf_event_init (void ); extern void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size, struct pt_regs *regs,
@@ -1895,6 +1897,10 @@ static inline u64 perf_event_pause(struct perf_event *event, bool reset) {return 0 ; } +static inline int perf_exclude_event (struct perf_event *event,struct pt_regs *regs) +{ +return 0 ; +}#endif #if defined(CONFIG_PERF_EVENTS ) && defined(CONFIG_CPU_SUP_INTEL )
kernel/events/core.c +1 -2
@@ -10039,8 +10039,7 @@ static void perf_swevent_event(struct perf_event *event, u64 nr,perf_swevent_overflow (event,0 , data, regs); } -static int perf_exclude_event (struct perf_event *event, -struct pt_regs *regs) +int perf_exclude_event (struct perf_event *event,struct pt_regs *regs) {if (event->hw.state &PERF_HES_STOPPED )return 1 ;
[tip: perf/core] perf/core: Export perf_exclude_event()
In reply to Namhyung KimThe following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 6057b90ecc84f232dd32a047a086a4c4c271765f Gitweb: https://git.kernel.org/tip/6057b90ecc84f232dd32a047a086a4c4c271765f Author: Namhyung Kim <[email protected]> AuthorDate: Tue, 03 Dec 2024 10:04:40 -08:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Mon, 09 Dec 2024 15:50:31 +01:00
perf/core: Export perf_exclude_event()
While at it, rename the same function in s390 cpum_sf PMU.
--- arch/s390/kernel/perf_cpum_sf.c | 6 +++--- include/linux/perf_event.h | 6 ++++++ kernel/events/core.c | 3 +-- 3 files changed, 10 insertions(+), 5 deletions(-)
arch/s390/kernel/perf_cpum_sf.c +3 -3
@@ -981,7 +981,7 @@ static void cpumsf_pmu_disable(struct pmu *pmu) cpuhw->flags &= ~PMU_F_ENABLED ; } -/* perf_exclude_event() - Filter event +/* perf_event_exclude() - Filter event * @event: The perf event * @regs: pt_regs structure * @sde_regs: Sample-data-entry (sde) regs structure
@@ -990,7 +990,7 @@ static void cpumsf_pmu_disable(struct pmu *pmu)* * Return non-zero if the event shall be excluded. */ -static int perf_exclude_event (struct perf_event *event,struct pt_regs *regs, +static int perf_event_exclude (struct perf_event *event,struct pt_regs *regs,struct perf_sf_sde_regs *sde_regs) {if (event->attr.exclude_user &&user_mode (regs))
@@ -1073,7 +1073,7 @@ static int perf_push_sample(struct perf_event *event, data.tid_entry.pid = basic->hpp &LPP_PID_MASK ; overflow =0 ; -if (perf_exclude_event (event, ®s, sde_regs)) +if (perf_event_exclude (event, ®s, sde_regs)) goto out;if (perf_event_overflow (event, &data, ®s)) { overflow =1 ;
include/linux/perf_event.h +6 -0
@@ -1690,6 +1690,8 @@ static inline int perf_allow_tracepoint(struct perf_event_attr *attr)return security_perf_event_open (attr,PERF_SECURITY_TRACEPOINT ); } +extern int perf_exclude_event (struct perf_event *event,struct pt_regs *regs); +extern void perf_event_init (void ); extern void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size, struct pt_regs *regs,
@@ -1895,6 +1897,10 @@ static inline u64 perf_event_pause(struct perf_event *event, bool reset) {return 0 ; } +static inline int perf_exclude_event (struct perf_event *event,struct pt_regs *regs) +{ +return 0 ; +}#endif #if defined(CONFIG_PERF_EVENTS ) && defined(CONFIG_CPU_SUP_INTEL )
kernel/events/core.c +1 -2
@@ -10039,8 +10039,7 @@ static void perf_swevent_event(struct perf_event *event, u64 nr,perf_swevent_overflow (event,0 , data, regs); } -static int perf_exclude_event (struct perf_event *event, -struct pt_regs *regs) +int perf_exclude_event (struct perf_event *event,struct pt_regs *regs) {if (event->hw.state &PERF_HES_STOPPED )return 1 ;
Namhyung Kim
maintainerPATCH v6 2/2 perf/x86: Relax privilege filter restriction on AMD IBS
In reply to Namhyung KimWhile IBS is available for per-thread profiling, still regular users cannot open an event due to the default paranoid setting (2) which doesn't allow unprivileged users to get kernel samples. That means it needs to set exclude_kernel bit in the attribute but IBS driver would reject it since it has PERF_PMU_CAP_NO_EXCLUDE. This is not what we want and I've been getting requests to fix this issue.
This should be done in the hardware, but until we get the HW fix we may allow exclude_{kernel,user,hv} in the attribute and silently drop the samples in the PMU IRQ handler. It won't guarantee the sampling frequency or even it'd miss some with fixed period too. Not ideal, but that'd still be helpful to regular users.
To minimize the confusion, let's add 'swfilt' bit to attr.config2 which is exposed in the sysfs format directory so that users can figure out if the kernel support the privilege filters by software.
$ perf record -e ibs_op/swfilt=1/u true
This uses perf_exclude_event() which checks regs->cs. But it should be fine because set_linear_ip() also updates the CS according to the RIP provided by IBS.
--- arch/x86/events/amd/ibs.c | 59 +++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 18 deletions(-)
arch/x86/events/amd/ibs.c +41 -18
@@ -31,6 +31,8 @@ static u32 ibs_caps;#define IBS_FETCH_CONFIG_MASK (IBS_FETCH_RAND_EN | IBS_FETCH_MAX_CNT)#define IBS_OP_CONFIG_MASK IBS_OP_MAX_CNT +/* attr.config2 */ +#define IBS_SW_FILTER_MASK 1/* * IBS states:
@@ -290,6 +292,16 @@ static int perf_ibs_init(struct perf_event *event)if (has_branch_stack (event))return -EOPNOTSUPP ; +/* handle exclude_{user,kernel} in the IRQ handler */ +if (event->attr.exclude_host || event->attr.exclude_guest || + event->attr.exclude_idle) +return -EINVAL ; + +if (!(event->attr.config2 &IBS_SW_FILTER_MASK ) && + (event->attr.exclude_kernel || event->attr.exclude_user || + event->attr.exclude_hv)) +return -EINVAL ; + ret =validate_group (event);if (ret)return ret;
@@ -550,24 +562,14 @@ static struct attribute *attrs_empty[] = { NULL, }; -static struct attribute_group empty_format_group = { - .name ="format" , - .attrs = attrs_empty, -}; -static struct attribute_group empty_caps_group = { .name ="caps" , .attrs = attrs_empty, }; -static const struct attribute_group *empty_attr_groups[] = { - &empty_format_group, - &empty_caps_group, -NULL , -}; -PMU_FORMAT_ATTR (rand_en,"config:57" );PMU_FORMAT_ATTR (cnt_ctl,"config:19" ); +PMU_FORMAT_ATTR (swfilt,"config2:0" );PMU_EVENT_ATTR_STRING (l3missonly, fetch_l3missonly,"config:59" );PMU_EVENT_ATTR_STRING (l3missonly, op_l3missonly,"config:16" );PMU_EVENT_ATTR_STRING (zen4_ibs_extensions, zen4_ibs_extensions,"1" );
@@ -578,8 +580,9 @@ zen4_ibs_extensions_is_visible(struct kobject *kobj, struct attribute *attr, intreturn ibs_caps &IBS_CAPS_ZEN4 ? attr->mode :0 ; } -static struct attribute *rand_en_attrs[] = { +static struct attribute *fetch_attrs[] = { &format_attr_rand_en.attr, + &format_attr_swfilt.attr,NULL , };
@@ -593,9 +596,9 @@ static struct attribute *zen4_ibs_extensions_attrs[] = { NULL, }; -static struct attribute_group group_rand_en = { +static struct attribute_group group_fetch_formats = { .name ="format" , - .attrs = rand_en_attrs, + .attrs = fetch_attrs, };static struct attribute_group group_fetch_l3missonly = {
@@ -611,7 +614,7 @@ static struct attribute_group group_zen4_ibs_extensions = { };static const struct attribute_group *fetch_attr_groups[] = { - &group_rand_en, + &group_fetch_formats, &empty_caps_group,NULL , };
@@ -628,6 +631,11 @@ cnt_ctl_is_visible(struct kobject *kobj, struct attribute *attr, int i)return ibs_caps &IBS_CAPS_OPCNT ? attr->mode :0 ; } +static struct attribute *op_attrs[] = { + &format_attr_swfilt.attr, +NULL , +}; +static struct attribute *cnt_ctl_attrs[] = { &format_attr_cnt_ctl.attr,NULL ,
@@ -638,6 +646,11 @@ static struct attribute *op_l3missonly_attrs[] = { NULL, }; +static struct attribute_group group_op_formats = { + .name ="format" , + .attrs = op_attrs, +}; +static struct attribute_group group_cnt_ctl = { .name ="format" , .attrs = cnt_ctl_attrs,
@@ -650,6 +663,12 @@ static struct attribute_group group_op_l3missonly = { .is_visible = zen4_ibs_extensions_is_visible, }; +static const struct attribute_group *op_attr_groups[] = { + &group_op_formats, + &empty_caps_group, +NULL , +}; +static const struct attribute_group *op_attr_update[] = { &group_cnt_ctl, &group_op_l3missonly,
@@ -667,7 +686,6 @@ static struct perf_ibs perf_ibs_fetch = { .start = perf_ibs_start, .stop = perf_ibs_stop, .read = perf_ibs_read, - .capabilities = PERF_PMU_CAP_NO_EXCLUDE, }, .msr = MSR_AMD64_IBSFETCHCTL, .config_mask = IBS_FETCH_CONFIG_MASK,
@@ -691,7 +709,6 @@ static struct perf_ibs perf_ibs_op = { .start = perf_ibs_start, .stop = perf_ibs_stop, .read = perf_ibs_read, - .capabilities = PERF_PMU_CAP_NO_EXCLUDE, }, .msr = MSR_AMD64_IBSOPCTL, .config_mask = IBS_OP_CONFIG_MASK,
@@ -1111,6 +1128,12 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs) regs.flags |=PERF_EFLAGS_EXACT ; } +if ((event->attr.config2 &IBS_SW_FILTER_MASK ) && +perf_exclude_event (event, ®s)) { + throttle =perf_event_account_interrupt (event); + goto out; + } +if (event->attr.sample_type &PERF_SAMPLE_RAW ) { raw = (struct perf_raw_record ){ .frag = {
@@ -1227,7 +1250,7 @@ static __init int perf_ibs_op_init(void)if (ibs_caps &IBS_CAPS_ZEN4 ) perf_ibs_op.config_mask |=IBS_OP_L3MISSONLY ; - perf_ibs_op.pmu.attr_groups = empty_attr_groups; + perf_ibs_op.pmu.attr_groups = op_attr_groups; perf_ibs_op.pmu.attr_update = op_attr_update;return perf_ibs_pmu_init (&perf_ibs_op,"ibs_op" );
Signature · 1 line
2.47.0.338.g60cca15819-goog
[tip: perf/core] perf/x86: Relax privilege filter restriction on AMD IBS
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: c8bec3b6fd0088c326985b9119c9cd876c227b4c Gitweb: https://git.kernel.org/tip/c8bec3b6fd0088c326985b9119c9cd876c227b4c Author: Namhyung Kim <[email protected]> AuthorDate: Tue, 03 Dec 2024 10:04:41 -08:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Fri, 06 Dec 2024 10:32:44 +01:00
perf/x86: Relax privilege filter restriction on AMD IBS
While IBS is available for per-thread profiling, still regular users cannot open an event due to the default paranoid setting (2) which doesn't allow unprivileged users to get kernel samples. That means it needs to set exclude_kernel bit in the attribute but IBS driver would reject it since it has PERF_PMU_CAP_NO_EXCLUDE. This is not what we want and I've been getting requests to fix this issue.
This should be done in the hardware, but until we get the HW fix we may allow exclude_{kernel,user,hv} in the attribute and silently drop the samples in the PMU IRQ handler. It won't guarantee the sampling frequency or even it'd miss some with fixed period too. Not ideal, but that'd still be helpful to regular users.
To minimize the confusion, let's add 'swfilt' bit to attr.config2 which is exposed in the sysfs format directory so that users can figure out if the kernel support the privilege filters by software.
$ perf record -e ibs_op/swfilt=1/u true
This uses perf_exclude_event() which checks regs->cs. But it should be fine because set_linear_ip() also updates the CS according to the RIP provided by IBS.
--- arch/x86/events/amd/ibs.c | 59 ++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 18 deletions(-)
arch/x86/events/amd/ibs.c +41 -18
@@ -31,6 +31,8 @@ static u32 ibs_caps;#define IBS_FETCH_CONFIG_MASK (IBS_FETCH_RAND_EN | IBS_FETCH_MAX_CNT)#define IBS_OP_CONFIG_MASK IBS_OP_MAX_CNT +/* attr.config2 */ +#define IBS_SW_FILTER_MASK 1/* * IBS states:
@@ -290,6 +292,16 @@ static int perf_ibs_init(struct perf_event *event)if (has_branch_stack (event))return -EOPNOTSUPP ; +/* handle exclude_{user,kernel} in the IRQ handler */ +if (event->attr.exclude_host || event->attr.exclude_guest || + event->attr.exclude_idle) +return -EINVAL ; + +if (!(event->attr.config2 &IBS_SW_FILTER_MASK ) && + (event->attr.exclude_kernel || event->attr.exclude_user || + event->attr.exclude_hv)) +return -EINVAL ; + ret =validate_group (event);if (ret)return ret;
@@ -550,24 +562,14 @@ static struct attribute *attrs_empty[] = { NULL, }; -static struct attribute_group empty_format_group = { - .name ="format" , - .attrs = attrs_empty, -}; -static struct attribute_group empty_caps_group = { .name ="caps" , .attrs = attrs_empty, }; -static const struct attribute_group *empty_attr_groups[] = { - &empty_format_group, - &empty_caps_group, -NULL , -}; -PMU_FORMAT_ATTR (rand_en,"config:57" );PMU_FORMAT_ATTR (cnt_ctl,"config:19" ); +PMU_FORMAT_ATTR (swfilt,"config2:0" );PMU_EVENT_ATTR_STRING (l3missonly, fetch_l3missonly,"config:59" );PMU_EVENT_ATTR_STRING (l3missonly, op_l3missonly,"config:16" );PMU_EVENT_ATTR_STRING (zen4_ibs_extensions, zen4_ibs_extensions,"1" );
@@ -578,8 +580,9 @@ zen4_ibs_extensions_is_visible(struct kobject *kobj, struct attribute *attr, intreturn ibs_caps &IBS_CAPS_ZEN4 ? attr->mode :0 ; } -static struct attribute *rand_en_attrs[] = { +static struct attribute *fetch_attrs[] = { &format_attr_rand_en.attr, + &format_attr_swfilt.attr,NULL , };
@@ -593,9 +596,9 @@ static struct attribute *zen4_ibs_extensions_attrs[] = { NULL, }; -static struct attribute_group group_rand_en = { +static struct attribute_group group_fetch_formats = { .name ="format" , - .attrs = rand_en_attrs, + .attrs = fetch_attrs, };static struct attribute_group group_fetch_l3missonly = {
@@ -611,7 +614,7 @@ static struct attribute_group group_zen4_ibs_extensions = { };static const struct attribute_group *fetch_attr_groups[] = { - &group_rand_en, + &group_fetch_formats, &empty_caps_group,NULL , };
@@ -628,6 +631,11 @@ cnt_ctl_is_visible(struct kobject *kobj, struct attribute *attr, int i)return ibs_caps &IBS_CAPS_OPCNT ? attr->mode :0 ; } +static struct attribute *op_attrs[] = { + &format_attr_swfilt.attr, +NULL , +}; +static struct attribute *cnt_ctl_attrs[] = { &format_attr_cnt_ctl.attr,NULL ,
@@ -638,6 +646,11 @@ static struct attribute *op_l3missonly_attrs[] = { NULL, }; +static struct attribute_group group_op_formats = { + .name ="format" , + .attrs = op_attrs, +}; +static struct attribute_group group_cnt_ctl = { .name ="format" , .attrs = cnt_ctl_attrs,
@@ -650,6 +663,12 @@ static struct attribute_group group_op_l3missonly = { .is_visible = zen4_ibs_extensions_is_visible, }; +static const struct attribute_group *op_attr_groups[] = { + &group_op_formats, + &empty_caps_group, +NULL , +}; +static const struct attribute_group *op_attr_update[] = { &group_cnt_ctl, &group_op_l3missonly,
@@ -667,7 +686,6 @@ static struct perf_ibs perf_ibs_fetch = { .start = perf_ibs_start, .stop = perf_ibs_stop, .read = perf_ibs_read, - .capabilities = PERF_PMU_CAP_NO_EXCLUDE, }, .msr = MSR_AMD64_IBSFETCHCTL, .config_mask = IBS_FETCH_CONFIG_MASK,
@@ -691,7 +709,6 @@ static struct perf_ibs perf_ibs_op = { .start = perf_ibs_start, .stop = perf_ibs_stop, .read = perf_ibs_read, - .capabilities = PERF_PMU_CAP_NO_EXCLUDE, }, .msr = MSR_AMD64_IBSOPCTL, .config_mask = IBS_OP_CONFIG_MASK,
@@ -1111,6 +1128,12 @@ fail: regs.flags |=PERF_EFLAGS_EXACT ; } +if ((event->attr.config2 &IBS_SW_FILTER_MASK ) && +perf_exclude_event (event, ®s)) { + throttle =perf_event_account_interrupt (event); + goto out; + } +if (event->attr.sample_type &PERF_SAMPLE_RAW ) { raw = (struct perf_raw_record ){ .frag = {
@@ -1227,7 +1250,7 @@ static __init int perf_ibs_op_init(void)if (ibs_caps &IBS_CAPS_ZEN4 ) perf_ibs_op.config_mask |=IBS_OP_L3MISSONLY ; - perf_ibs_op.pmu.attr_groups = empty_attr_groups; + perf_ibs_op.pmu.attr_groups = op_attr_groups; perf_ibs_op.pmu.attr_update = op_attr_update;return perf_ibs_pmu_init (&perf_ibs_op,"ibs_op" );
[tip: perf/core] perf/x86: Relax privilege filter restriction on AMD IBS
In reply to Namhyung KimThe following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 9290c1b27402e7eee1692c1bb010ca7055dff116 Gitweb: https://git.kernel.org/tip/9290c1b27402e7eee1692c1bb010ca7055dff116 Author: Namhyung Kim <[email protected]> AuthorDate: Tue, 03 Dec 2024 10:04:41 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Mon, 09 Dec 2024 11:18:08 +01:00
perf/x86: Relax privilege filter restriction on AMD IBS
While IBS is available for per-thread profiling, still regular users cannot open an event due to the default paranoid setting (2) which doesn't allow unprivileged users to get kernel samples. That means it needs to set exclude_kernel bit in the attribute but IBS driver would reject it since it has PERF_PMU_CAP_NO_EXCLUDE. This is not what we want and I've been getting requests to fix this issue.
This should be done in the hardware, but until we get the HW fix we may allow exclude_{kernel,user,hv} in the attribute and silently drop the samples in the PMU IRQ handler. It won't guarantee the sampling frequency or even it'd miss some with fixed period too. Not ideal, but that'd still be helpful to regular users.
To minimize the confusion, let's add 'swfilt' bit to attr.config2 which is exposed in the sysfs format directory so that users can figure out if the kernel support the privilege filters by software.
$ perf record -e ibs_op/swfilt=1/u true
This uses perf_exclude_event() which checks regs->cs. But it should be fine because set_linear_ip() also updates the CS according to the RIP provided by IBS.
--- arch/x86/events/amd/ibs.c | 59 ++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 18 deletions(-)
arch/x86/events/amd/ibs.c +41 -18
@@ -31,6 +31,8 @@ static u32 ibs_caps;#define IBS_FETCH_CONFIG_MASK (IBS_FETCH_RAND_EN | IBS_FETCH_MAX_CNT)#define IBS_OP_CONFIG_MASK IBS_OP_MAX_CNT +/* attr.config2 */ +#define IBS_SW_FILTER_MASK 1/* * IBS states:
@@ -290,6 +292,16 @@ static int perf_ibs_init(struct perf_event *event)if (has_branch_stack (event))return -EOPNOTSUPP ; +/* handle exclude_{user,kernel} in the IRQ handler */ +if (event->attr.exclude_host || event->attr.exclude_guest || + event->attr.exclude_idle) +return -EINVAL ; + +if (!(event->attr.config2 &IBS_SW_FILTER_MASK ) && + (event->attr.exclude_kernel || event->attr.exclude_user || + event->attr.exclude_hv)) +return -EINVAL ; + ret =validate_group (event);if (ret)return ret;
@@ -550,24 +562,14 @@ static struct attribute *attrs_empty[] = { NULL, }; -static struct attribute_group empty_format_group = { - .name ="format" , - .attrs = attrs_empty, -}; -static struct attribute_group empty_caps_group = { .name ="caps" , .attrs = attrs_empty, }; -static const struct attribute_group *empty_attr_groups[] = { - &empty_format_group, - &empty_caps_group, -NULL , -}; -PMU_FORMAT_ATTR (rand_en,"config:57" );PMU_FORMAT_ATTR (cnt_ctl,"config:19" ); +PMU_FORMAT_ATTR (swfilt,"config2:0" );PMU_EVENT_ATTR_STRING (l3missonly, fetch_l3missonly,"config:59" );PMU_EVENT_ATTR_STRING (l3missonly, op_l3missonly,"config:16" );PMU_EVENT_ATTR_STRING (zen4_ibs_extensions, zen4_ibs_extensions,"1" );
@@ -578,8 +580,9 @@ zen4_ibs_extensions_is_visible(struct kobject *kobj, struct attribute *attr, intreturn ibs_caps &IBS_CAPS_ZEN4 ? attr->mode :0 ; } -static struct attribute *rand_en_attrs[] = { +static struct attribute *fetch_attrs[] = { &format_attr_rand_en.attr, + &format_attr_swfilt.attr,NULL , };
@@ -593,9 +596,9 @@ static struct attribute *zen4_ibs_extensions_attrs[] = { NULL, }; -static struct attribute_group group_rand_en = { +static struct attribute_group group_fetch_formats = { .name ="format" , - .attrs = rand_en_attrs, + .attrs = fetch_attrs, };static struct attribute_group group_fetch_l3missonly = {
@@ -611,7 +614,7 @@ static struct attribute_group group_zen4_ibs_extensions = { };static const struct attribute_group *fetch_attr_groups[] = { - &group_rand_en, + &group_fetch_formats, &empty_caps_group,NULL , };
@@ -628,6 +631,11 @@ cnt_ctl_is_visible(struct kobject *kobj, struct attribute *attr, int i)return ibs_caps &IBS_CAPS_OPCNT ? attr->mode :0 ; } +static struct attribute *op_attrs[] = { + &format_attr_swfilt.attr, +NULL , +}; +static struct attribute *cnt_ctl_attrs[] = { &format_attr_cnt_ctl.attr,NULL ,
@@ -638,6 +646,11 @@ static struct attribute *op_l3missonly_attrs[] = { NULL, }; +static struct attribute_group group_op_formats = { + .name ="format" , + .attrs = op_attrs, +}; +static struct attribute_group group_cnt_ctl = { .name ="format" , .attrs = cnt_ctl_attrs,
@@ -650,6 +663,12 @@ static struct attribute_group group_op_l3missonly = { .is_visible = zen4_ibs_extensions_is_visible, }; +static const struct attribute_group *op_attr_groups[] = { + &group_op_formats, + &empty_caps_group, +NULL , +}; +static const struct attribute_group *op_attr_update[] = { &group_cnt_ctl, &group_op_l3missonly,
@@ -667,7 +686,6 @@ static struct perf_ibs perf_ibs_fetch = { .start = perf_ibs_start, .stop = perf_ibs_stop, .read = perf_ibs_read, - .capabilities = PERF_PMU_CAP_NO_EXCLUDE, }, .msr = MSR_AMD64_IBSFETCHCTL, .config_mask = IBS_FETCH_CONFIG_MASK,
@@ -691,7 +709,6 @@ static struct perf_ibs perf_ibs_op = { .start = perf_ibs_start, .stop = perf_ibs_stop, .read = perf_ibs_read, - .capabilities = PERF_PMU_CAP_NO_EXCLUDE, }, .msr = MSR_AMD64_IBSOPCTL, .config_mask = IBS_OP_CONFIG_MASK,
@@ -1111,6 +1128,12 @@ fail: regs.flags |=PERF_EFLAGS_EXACT ; } +if ((event->attr.config2 &IBS_SW_FILTER_MASK ) && +perf_exclude_event (event, ®s)) { + throttle =perf_event_account_interrupt (event); + goto out; + } +if (event->attr.sample_type &PERF_SAMPLE_RAW ) { raw = (struct perf_raw_record ){ .frag = {
@@ -1227,7 +1250,7 @@ static __init int perf_ibs_op_init(void)if (ibs_caps &IBS_CAPS_ZEN4 ) perf_ibs_op.config_mask |=IBS_OP_L3MISSONLY ; - perf_ibs_op.pmu.attr_groups = empty_attr_groups; + perf_ibs_op.pmu.attr_groups = op_attr_groups; perf_ibs_op.pmu.attr_update = op_attr_update;return perf_ibs_pmu_init (&perf_ibs_op,"ibs_op" );
[tip: perf/core] perf/x86: Relax privilege filter restriction on AMD IBS
In reply to Namhyung KimThe following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: d29e744c71673a71da8f8522799ee02744cad6c9 Gitweb: https://git.kernel.org/tip/d29e744c71673a71da8f8522799ee02744cad6c9 Author: Namhyung Kim <[email protected]> AuthorDate: Tue, 03 Dec 2024 10:04:41 -08:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Mon, 09 Dec 2024 15:50:32 +01:00
perf/x86: Relax privilege filter restriction on AMD IBS
While IBS is available for per-thread profiling, still regular users cannot open an event due to the default paranoid setting (2) which doesn't allow unprivileged users to get kernel samples. That means it needs to set exclude_kernel bit in the attribute but IBS driver would reject it since it has PERF_PMU_CAP_NO_EXCLUDE. This is not what we want and I've been getting requests to fix this issue.
This should be done in the hardware, but until we get the HW fix we may allow exclude_{kernel,user,hv} in the attribute and silently drop the samples in the PMU IRQ handler. It won't guarantee the sampling frequency or even it'd miss some with fixed period too. Not ideal, but that'd still be helpful to regular users.
To minimize the confusion, let's add 'swfilt' bit to attr.config2 which is exposed in the sysfs format directory so that users can figure out if the kernel support the privilege filters by software.
$ perf record -e ibs_op/swfilt=1/u true
This uses perf_exclude_event() which checks regs->cs. But it should be fine because set_linear_ip() also updates the CS according to the RIP provided by IBS.
--- arch/x86/events/amd/ibs.c | 59 ++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 18 deletions(-)
arch/x86/events/amd/ibs.c +41 -18
@@ -31,6 +31,8 @@ static u32 ibs_caps;#define IBS_FETCH_CONFIG_MASK (IBS_FETCH_RAND_EN | IBS_FETCH_MAX_CNT)#define IBS_OP_CONFIG_MASK IBS_OP_MAX_CNT +/* attr.config2 */ +#define IBS_SW_FILTER_MASK 1/* * IBS states:
@@ -290,6 +292,16 @@ static int perf_ibs_init(struct perf_event *event)if (has_branch_stack (event))return -EOPNOTSUPP ; +/* handle exclude_{user,kernel} in the IRQ handler */ +if (event->attr.exclude_host || event->attr.exclude_guest || + event->attr.exclude_idle) +return -EINVAL ; + +if (!(event->attr.config2 &IBS_SW_FILTER_MASK ) && + (event->attr.exclude_kernel || event->attr.exclude_user || + event->attr.exclude_hv)) +return -EINVAL ; + ret =validate_group (event);if (ret)return ret;
@@ -550,24 +562,14 @@ static struct attribute *attrs_empty[] = { NULL, }; -static struct attribute_group empty_format_group = { - .name ="format" , - .attrs = attrs_empty, -}; -static struct attribute_group empty_caps_group = { .name ="caps" , .attrs = attrs_empty, }; -static const struct attribute_group *empty_attr_groups[] = { - &empty_format_group, - &empty_caps_group, -NULL , -}; -PMU_FORMAT_ATTR (rand_en,"config:57" );PMU_FORMAT_ATTR (cnt_ctl,"config:19" ); +PMU_FORMAT_ATTR (swfilt,"config2:0" );PMU_EVENT_ATTR_STRING (l3missonly, fetch_l3missonly,"config:59" );PMU_EVENT_ATTR_STRING (l3missonly, op_l3missonly,"config:16" );PMU_EVENT_ATTR_STRING (zen4_ibs_extensions, zen4_ibs_extensions,"1" );
@@ -578,8 +580,9 @@ zen4_ibs_extensions_is_visible(struct kobject *kobj, struct attribute *attr, intreturn ibs_caps &IBS_CAPS_ZEN4 ? attr->mode :0 ; } -static struct attribute *rand_en_attrs[] = { +static struct attribute *fetch_attrs[] = { &format_attr_rand_en.attr, + &format_attr_swfilt.attr,NULL , };
@@ -593,9 +596,9 @@ static struct attribute *zen4_ibs_extensions_attrs[] = { NULL, }; -static struct attribute_group group_rand_en = { +static struct attribute_group group_fetch_formats = { .name ="format" , - .attrs = rand_en_attrs, + .attrs = fetch_attrs, };static struct attribute_group group_fetch_l3missonly = {
@@ -611,7 +614,7 @@ static struct attribute_group group_zen4_ibs_extensions = { };static const struct attribute_group *fetch_attr_groups[] = { - &group_rand_en, + &group_fetch_formats, &empty_caps_group,NULL , };
@@ -628,6 +631,11 @@ cnt_ctl_is_visible(struct kobject *kobj, struct attribute *attr, int i)return ibs_caps &IBS_CAPS_OPCNT ? attr->mode :0 ; } +static struct attribute *op_attrs[] = { + &format_attr_swfilt.attr, +NULL , +}; +static struct attribute *cnt_ctl_attrs[] = { &format_attr_cnt_ctl.attr,NULL ,
@@ -638,6 +646,11 @@ static struct attribute *op_l3missonly_attrs[] = { NULL, }; +static struct attribute_group group_op_formats = { + .name ="format" , + .attrs = op_attrs, +}; +static struct attribute_group group_cnt_ctl = { .name ="format" , .attrs = cnt_ctl_attrs,
@@ -650,6 +663,12 @@ static struct attribute_group group_op_l3missonly = { .is_visible = zen4_ibs_extensions_is_visible, }; +static const struct attribute_group *op_attr_groups[] = { + &group_op_formats, + &empty_caps_group, +NULL , +}; +static const struct attribute_group *op_attr_update[] = { &group_cnt_ctl, &group_op_l3missonly,
@@ -667,7 +686,6 @@ static struct perf_ibs perf_ibs_fetch = { .start = perf_ibs_start, .stop = perf_ibs_stop, .read = perf_ibs_read, - .capabilities = PERF_PMU_CAP_NO_EXCLUDE, }, .msr = MSR_AMD64_IBSFETCHCTL, .config_mask = IBS_FETCH_CONFIG_MASK,
@@ -691,7 +709,6 @@ static struct perf_ibs perf_ibs_op = { .start = perf_ibs_start, .stop = perf_ibs_stop, .read = perf_ibs_read, - .capabilities = PERF_PMU_CAP_NO_EXCLUDE, }, .msr = MSR_AMD64_IBSOPCTL, .config_mask = IBS_OP_CONFIG_MASK,
@@ -1111,6 +1128,12 @@ fail: regs.flags |=PERF_EFLAGS_EXACT ; } +if ((event->attr.config2 &IBS_SW_FILTER_MASK ) && +perf_exclude_event (event, ®s)) { + throttle =perf_event_account_interrupt (event); + goto out; + } +if (event->attr.sample_type &PERF_SAMPLE_RAW ) { raw = (struct perf_raw_record ){ .frag = {
@@ -1227,7 +1250,7 @@ static __init int perf_ibs_op_init(void)if (ibs_caps &IBS_CAPS_ZEN4 ) perf_ibs_op.config_mask |=IBS_OP_L3MISSONLY ; - perf_ibs_op.pmu.attr_groups = empty_attr_groups; + perf_ibs_op.pmu.attr_groups = op_attr_groups; perf_ibs_op.pmu.attr_update = op_attr_update;return perf_ibs_pmu_init (&perf_ibs_op,"ibs_op" );