perf/x86: Add update attribute groups
Subsystems (3)
Jiri Olsa
reviewercover perf/x86: Add update attribute groups
hi,
following up on [1], this patchset adds update attribute groups to pmu and gets rid of the 'creative' attribute handling code.
In x86 pmu we mainly add attributes into following directories:
events, format, caps
so it seems like we could have just 3 attribute groups, but most of the attributes presence depends on HW, which ends up with attributes merging and all that 'creative' code we have now.
Currently, we have 'struct pmu::attr_groups', which is in its final shape (merged) and it's added via sysfs_create_groups call when registering the pmu:
perf_pmu_register
pmu_dev_alloc
device_add
device_add_attrs
device_add_groups
sysfs_create_groups(pmu::attr_groups)
This interface does not provide a way to have multiple attribute groups, which could add files into same directory, all has to be prepared ahead.
This patchset adds 'update attribute group', which is added via new sysfs_update_groups function, after the initial set of attributes is created. The group's is_visible function will cover its HW dependency/visibility.
This will allow us to update "events" or "format" directories with attributes that depend on various HW config.
For example having group_format_extra group, that updates "format" directory, only if pmu version is 2 and higher:
static umode_t
lbr_is_visible(struct kobject *kobj, struct attribute *attr, int i)
{
return x86_pmu.lbr_nr ? attr->mode : 0;
}
static struct attribute_group group_caps_lbr = {
.name = "caps",
.attrs = lbr_attrs,
.is_visible = lbr_is_visible,
};Note that some of the groups are defined without 'attrs' set, which is assigned later based on the detected cpu model.
And finally we'll end up with just a single set of attribute groups:
static const struct attribute_group *attr_update[] = {
&group_events_td,
&group_events_mem,
&group_events_tsx,
&group_caps_gen,
&group_caps_lbr,
&group_format_extra,
&group_format_extra_skl,
&group_default,
NULL,
};that is passed to struct pmu.
Also available in:
git://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
perf/x86_attrs
thoughts? thanks,
jirka
[1] https://lore.kernel.org/lkml/[email protected]/
---
Jiri Olsa (8):
sysfs: Add sysfs_update_groups function
perf: Add attr_groups_update into struct pmu
perf/x86: Get rid of x86_pmu::event_attrs
perf/x86: Use the new pmu::update_attrs attribute group perf/x86: Add is_visible attribute_group callback for base events perf/x86: Use update attribute groups for caps perf/x86/intel: Use update attributes for skylake format perf/x86: Use update attribute groups for default attributes
arch/x86/events/core.c | 105 +++++++++++++++------------------------------------------------------------------------------------------ arch/x86/events/intel/core.c | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------- arch/x86/events/perf_event.h | 7 +------ fs/sysfs/group.c | 43 ++++++++++++++++++++++++++++--------------- include/linux/perf_event.h | 1 + include/linux/sysfs.h | 2 ++ kernel/events/core.c | 6 ++++++ 7 files changed, 145 insertions(+), 163 deletions(-)
Jiri Olsa
reviewerPATCH 1/8 sysfs: Add sysfs_update_groups function
Adding sysfs_update_groups function to update
multiple groups.
TODO:
I'm not sure how to handle error path in here,
currently it removes the whole updated group
together with already existing (not updated)
attributes.
--- fs/sysfs/group.c | 43 ++++++++++++++++++++++++++++--------------- include/linux/sysfs.h | 2 ++ 2 files changed, 30 insertions(+), 15 deletions(-)
fs/sysfs/group.c +28 -15
@@ -175,6 +175,26 @@ int sysfs_create_group(struct kobject *kobj, }EXPORT_SYMBOL_GPL (sysfs_create_group); +static int internal_create_groups (struct kobject *kobj,int update, +const struct attribute_group **groups) +{ +int error =0 ; +int i; + +if (!groups) +return 0 ; + +for (i =0 ; groups[i]; i++) { + error =internal_create_group (kobj, update, groups[i]); +if (error) { +while (--i >=0 ) +sysfs_remove_group (kobj, groups[i]); +break ; + } + } +return error; +} +/** * sysfs_create_groups - given a directory kobject, create a bunch of attribute groups * @kobj: The kobject to create the group on
@@ -191,24 +211,17 @@ EXPORT_SYMBOL_GPL(sysfs_create_group);int sysfs_create_groups (struct kobject *kobj,const struct attribute_group **groups) { -int error =0 ; -int i; - -if (!groups) -return 0 ; - -for (i =0 ; groups[i]; i++) { - error =sysfs_create_group (kobj, groups[i]); -if (error) { -while (--i >=0 ) -sysfs_remove_group (kobj, groups[i]); -break ; - } - } -return error; +return internal_create_groups (kobj,0 , groups); }EXPORT_SYMBOL_GPL (sysfs_create_groups); +int sysfs_update_groups (struct kobject *kobj, +const struct attribute_group **groups) +{ +return internal_create_groups (kobj,1 , groups); +} +EXPORT_SYMBOL_GPL (sysfs_update_groups); +/** * sysfs_update_group - given a directory kobject, update an attribute group * @kobj: The kobject to update the group on
include/linux/sysfs.h +2 -0
@@ -268,6 +268,8 @@ int __must_check sysfs_create_group(struct kobject *kobj, const struct attribute_group *grp);int __must_check sysfs_create_groups (struct kobject *kobj,const struct attribute_group **groups); +int __must_check sysfs_update_groups (struct kobject *kobj, +const struct attribute_group **groups);int sysfs_update_group (struct kobject *kobj,const struct attribute_group *grp); void sysfs_remove_group(struct kobject *kobj,
Signature · 1 line
2.20.1
Re: [PATCH 1/8] sysfs: Add sysfs_update_groups function
Quoted 9 lines · Jiri Olsa
Adding
sysfs_update_groupsfunction to update
multiple groups.TODO:
I'm not sure how to handle error path in here,
currently it removes the whole updated group
together with already existing (not updated)
attributes.
I think that should be fine. The chance of an error happening here is really slim.
Quoted 1 line
Signed-off-by Jiri Olsa <[email protected]>
Re: [PATCH 1/8] sysfs: Add sysfs_update_groups function
Quoted 11 lines · Greg Kroah-Hartman
Quoted 9 lines · Jiri Olsa
Adding
sysfs_update_groupsfunction to update
multiple groups.TODO:
I'm not sure how to handle error path in here,
currently it removes the whole updated group
together with already existing (not updated)
attributes.
Quoted 7 lines
I think that should be fine. The chance of an error happening here is really slim.
Quoted 1 line
Signed-off-by Jiri Olsa <[email protected]>Reviewed-by Greg Kroah-Hartman <[email protected]>
cool, I'll post version with proper changelogs
thanks,
jirka
Jiri Olsa
reviewerPATCH 2/8 perf: Add attr_groups_update into struct pmu
In reply to Jiri OlsaAdding attr_update attribute group into pmu, to allow having multiple attribute groups for same group name.
This will allow us to update "events" or "format" directories with attributes that depend on various HW conditions.
For example having group_format_extra group that updates "format" directory only if pmu version is 2 and higher:
static umode_t
exra_is_visible(struct kobject *kobj, struct attribute *attr, int i)
{
return x86_pmu.version >= 2 ? attr->mode : 0;
}
static struct attribute_group group_format_extra = {
.name = "format",
.is_visible = exra_is_visible,
};--- include/linux/perf_event.h | 1 + kernel/events/core.c | 6 ++++++ 2 files changed, 7 insertions(+)
include/linux/perf_event.h +1 -0
@@ -255,6 +255,7 @@ struct pmu {struct module *module;struct device *dev;const struct attribute_group **attr_groups; +const struct attribute_group **attr_update;const char *name;int type;
kernel/events/core.c +6 -0
@@ -9874,6 +9874,12 @@ static int pmu_dev_alloc(struct pmu *pmu)if (ret) goto del_dev; +if (pmu->attr_update) + ret =sysfs_update_groups (&pmu->dev->kobj, pmu->attr_update); + +if (ret) + goto del_dev; + out:return ret;
Signature · 1 line
2.20.1
Jiri Olsa
reviewerPATCH 3/8 perf/x86: Get rid of x86_pmu::event_attrs
In reply to Jiri OlsaNobody is using that.
--- arch/x86/events/core.c | 3 --- arch/x86/events/perf_event.h | 1 - 2 files changed, 4 deletions(-)
arch/x86/events/core.c +0 -3
@@ -1850,9 +1850,6 @@ static int __init init_hw_perf_events(void) x86_pmu_caps_group.attrs = tmp; } -if (x86_pmu.event_attrs) - x86_pmu_events_group.attrs = x86_pmu.event_attrs; -if (!x86_pmu.events_sysfs_show) x86_pmu_events_group.attrs = &empty_attrs;else
arch/x86/events/perf_event.h +0 -1
@@ -631,7 +631,6 @@ struct x86_pmu {int attr_rdpmc_broken;int attr_rdpmc;struct attribute **format_attrs; -struct attribute **event_attrs;struct attribute **caps_attrs;ssize_t (*events_sysfs_show)(char *page,u64 config);
Signature · 1 line
2.20.1
Jiri Olsa
reviewerPATCH 4/8 perf/x86: Use the new pmu::update_attrs attribute group
In reply to Jiri OlsaUsing the new pmu::update_attrs attribute group to create detected events for x86_pmu.
Moving the topdown/memory/tsx attributes to separate attribute groups with specific is_visible functions.
--- arch/x86/events/core.c | 10 +---- arch/x86/events/intel/core.c | 78 ++++++++++++++++++++---------------- arch/x86/events/perf_event.h | 2 +- 3 files changed, 47 insertions(+), 43 deletions(-)
arch/x86/events/core.c +2 -8
@@ -1855,14 +1855,6 @@ static int __init init_hw_perf_events(void)else filter_events (x86_pmu_events_group.attrs); -if (x86_pmu.cpu_events) { -struct attribute **tmp; - - tmp =merge_attr (x86_pmu_events_group.attrs, x86_pmu.cpu_events); -if (!WARN_ON (!tmp)) - x86_pmu_events_group.attrs = tmp; - } -if (x86_pmu.attrs) {struct attribute **tmp;
@@ -1871,6 +1863,8 @@ static int __init init_hw_perf_events(void) x86_pmu_attr_group.attrs = tmp; } + pmu.attr_update = x86_pmu.attr_update; +pr_info ("... version: %d\n" , x86_pmu.version);pr_info ("... bit width: %d\n" , x86_pmu.cntval_bits);pr_info ("... generic registers: %d\n" , x86_pmu.num_counters);
arch/x86/events/intel/core.c +44 -34
164 lines not on this page · raw message
arch/x86/events/perf_event.h +1 -1
@@ -634,7 +634,7 @@ struct x86_pmu {struct attribute **caps_attrs;ssize_t (*events_sysfs_show)(char *page,u64 config); -struct attribute **cpu_events; +const struct attribute_group **attr_update;unsigned long attr_freeze_on_smi;struct attribute **attrs;
Signature · 1 line
2.20.1
Jiri Olsa
reviewerPATCH 5/8 perf/x86: Add is_visible attribute_group callback for base events
In reply to Jiri OlsaWe dont need to pre-filter out unsupported base events, we can just use its group's is_visible function to do this.
--- arch/x86/events/core.c | 52 ++++++++++++------------------------------ 1 file changed, 14 insertions(+), 38 deletions(-)
arch/x86/events/core.c +14 -38
@@ -1618,42 +1618,6 @@ static struct attribute_group x86_pmu_format_group __ro_after_init = { .attrs = NULL, }; -/* -* Remove all undefined events (x86_pmu.event_map(id) == 0) -* out of events_attr attributes. -*/ -static void __init filter_events (struct attribute **attrs) -{ -struct device_attribute *d; -struct perf_pmu_events_attr *pmu_attr; -int offset =0 ; -int i, j; - -for (i =0 ; attrs[i]; i++) { - d = (struct device_attribute *)attrs[i]; - pmu_attr = container_of(d, struct perf_pmu_events_attr, attr); -/* str trumps id */ -if (pmu_attr->event_str) -continue ; -if (x86_pmu.event_map (i + offset)) -continue ; - -for (j = i; attrs[j]; j++) - attrs[j] = attrs[j +1 ]; - -/* Check the shifted attr. */ - i--; - -/* -* event_map() is index based, the attrs array is organized -* by increasing event index. If we shift the events, then -* we need to compensate for the event_map(), otherwise -* we are looking up the wrong event in the map -*/ - offset++; - } -} -/* Merge two pointer arrays */ __init struct attribute **merge_attr (struct attribute **a,struct attribute **b) {
@@ -1744,9 +1708,23 @@ static struct attribute *events_attr[] = { NULL, }; +/* +* Remove all undefined events (x86_pmu.event_map(id) == 0) +* out of events_attr attributes. +*/ +static umode_t +is_visible (struct kobject *kobj,struct attribute *attr,int idx) +{ +struct perf_pmu_events_attr *pmu_attr; + + pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr.attr); +return pmu_attr->event_str || x86_pmu.event_map (idx) ? attr->mode :0 ; +} +static struct attribute_group x86_pmu_events_group__ro_after_init = { .name ="events" , .attrs = events_attr, + .is_visible = is_visible, };ssize_t x86_event_sysfs_show (char *page,u64 config,u64 event)
@@ -1852,8 +1830,6 @@ static int __init init_hw_perf_events(void)if (!x86_pmu.events_sysfs_show) x86_pmu_events_group.attrs = &empty_attrs; -else -filter_events (x86_pmu_events_group.attrs);if (x86_pmu.attrs) {struct attribute **tmp;
Signature · 1 line
2.20.1
Jiri Olsa
reviewerPATCH 6/8 perf/x86: Use update attribute groups for caps
In reply to Jiri OlsaUsing the new pmu::update_attrs attribute group for "caps" directory.
--- arch/x86/events/core.c | 8 ------- arch/x86/events/intel/core.c | 44 ++++++++++++++++++++++++++++-------- arch/x86/events/perf_event.h | 1 - 3 files changed, 34 insertions(+), 19 deletions(-)
arch/x86/events/core.c +0 -8
@@ -1820,14 +1820,6 @@ static int __init init_hw_perf_events(void) x86_pmu_format_group.attrs = x86_pmu.format_attrs; -if (x86_pmu.caps_attrs) { -struct attribute **tmp; - - tmp =merge_attr (x86_pmu_caps_group.attrs, x86_pmu.caps_attrs); -if (!WARN_ON (!tmp)) - x86_pmu_caps_group.attrs = tmp; - } -if (!x86_pmu.events_sysfs_show) x86_pmu_events_group.attrs = &empty_attrs;
arch/x86/events/intel/core.c +34 -10
80 lines not on this page · raw message
arch/x86/events/perf_event.h +0 -1
@@ -631,7 +631,6 @@ struct x86_pmu {int attr_rdpmc_broken;int attr_rdpmc;struct attribute **format_attrs; -struct attribute **caps_attrs;ssize_t (*events_sysfs_show)(char *page,u64 config);const struct attribute_group **attr_update;
Signature · 1 line
2.20.1
Jiri Olsa
reviewerPATCH 7/8 perf/x86/intel: Use update attributes for skylake format
In reply to Jiri OlsaUsing the new pmu::update_attrs attribute group for skylake specific format attributes.
--- arch/x86/events/intel/core.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
arch/x86/events/intel/core.c +10 -5
@@ -4444,6 +4444,11 @@ static struct attribute_group group_format_extra = { .is_visible = exra_is_visible, }; +static struct attribute_group group_format_extra_skl = { + .name ="format" , + .is_visible = exra_is_visible, +}; +static const struct attribute_group *attr_update[] = { &group_events_td, &group_events_mem,
@@ -4451,17 +4456,18 @@ static const struct attribute_group *attr_update[] = { &group_caps_gen, &group_caps_lbr, &group_format_extra, + &group_format_extra_skl, NULL, };__init int intel_pmu_init (void ) { +struct attribute **extra_skl_attr =NULL ;struct attribute **extra_attr =NULL ;struct attribute **td_attr =NULL ;struct attribute **mem_attr =NULL ;struct attribute **tsx_attr =NULL ; -struct attribute **to_free =NULL ;union cpuid10_edx edx;union cpuid10_eax eax;union cpuid10_ebx ebx;
@@ -4949,8 +4955,7 @@ __init int intel_pmu_init(void) x86_pmu.get_event_constraints = hsw_get_event_constraints; extra_attr =boot_cpu_has (X86_FEATURE_RTM ) ? hsw_format_attr : nhm_format_attr; - extra_attr =merge_attr (extra_attr, skl_format_attr); - to_free = extra_attr; + extra_skl_attr = skl_format_attr; td_attr = hsw_events_attrs; mem_attr = hsw_mem_events_attrs; tsx_attr = hsw_tsx_events_attrs;
@@ -4988,7 +4993,7 @@ __init int intel_pmu_init(void) x86_pmu.get_event_constraints = icl_get_event_constraints; extra_attr =boot_cpu_has (X86_FEATURE_RTM ) ? hsw_format_attr : nhm_format_attr; - extra_attr =merge_attr (extra_attr, skl_format_attr); + extra_skl_attr = skl_format_attr; mem_attr = icl_events_attrs; tsx_attr = icl_tsx_events_attrs; x86_pmu.rtm_abort_event = X86_CONFIG(.event=0xca, .umask=0x02);
@@ -5023,6 +5028,7 @@ __init int intel_pmu_init(void) group_events_mem.attrs = mem_attr; group_events_tsx.attrs = tsx_attr; group_format_extra.attrs = extra_attr; + group_format_extra_skl.attrs = extra_skl_attr; x86_pmu.attr_update = attr_update;
@@ -5103,7 +5109,6 @@ __init int intel_pmu_init(void)if (x86_pmu.counter_freezing) x86_pmu.handle_irq = intel_pmu_handle_irq_v4; -kfree (to_free);return 0 ; }
Signature · 1 line
2.20.1
Jiri Olsa
reviewerPATCH 8/8 perf/x86: Use update attribute groups for default attributes
In reply to Jiri OlsaUsing the new pmu::update_attrs attribute group for default attributes - freeze_on_smi, allow_tsx_force_abort.
--- arch/x86/events/core.c | 34 ---------------------------------- arch/x86/events/intel/core.c | 9 +++++---- arch/x86/events/perf_event.h | 3 --- 3 files changed, 5 insertions(+), 41 deletions(-)
arch/x86/events/core.c +0 -34
@@ -1618,32 +1618,6 @@ static struct attribute_group x86_pmu_format_group __ro_after_init = { .attrs = NULL, }; -/* Merge two pointer arrays */ -__init struct attribute **merge_attr (struct attribute **a,struct attribute **b) -{ -struct attribute **new; -int j, i; - -for (j =0 ; a && a[j]; j++) - ; -for (i =0 ; b && b[i]; i++) - j++; - j++; - - new =kmalloc_array (j,sizeof (struct attribute *),GFP_KERNEL ); -if (!new) -return NULL ; - - j =0 ; -for (i =0 ; a && a[i]; i++) - new[j++] = a[i]; -for (i =0 ; b && b[i]; i++) - new[j++] = b[i]; - new[j] =NULL ; - -return new; -} -ssize_t events_sysfs_show (struct device *dev,struct device_attribute *attr,char *page) { struct perf_pmu_events_attr *pmu_attr = \
@@ -1823,14 +1797,6 @@ static int __init init_hw_perf_events(void)if (!x86_pmu.events_sysfs_show) x86_pmu_events_group.attrs = &empty_attrs; -if (x86_pmu.attrs) { -struct attribute **tmp; - - tmp =merge_attr (x86_pmu_attr_group.attrs, x86_pmu.attrs); -if (!WARN_ON (!tmp)) - x86_pmu_attr_group.attrs = tmp; - } - pmu.attr_update = x86_pmu.attr_update;pr_info ("... version: %d\n" , x86_pmu.version);
arch/x86/events/intel/core.c +5 -4
@@ -3888,8 +3888,6 @@ static __initconst const struct x86_pmu core_pmu = { .check_period = intel_pmu_check_period, }; -static struct attribute *intel_pmu_attrs[]; -static __initconst const struct x86_pmu intel_pmu = { .name ="Intel" , .handle_irq = intel_pmu_handle_irq,
@@ -3921,8 +3919,6 @@ static __initconst const struct x86_pmu intel_pmu = { .format_attrs = intel_arch3_formats_attr, .events_sysfs_show = intel_event_sysfs_show, - .attrs = intel_pmu_attrs, - .cpu_prepare = intel_pmu_cpu_prepare, .cpu_starting = intel_pmu_cpu_starting, .cpu_dying = intel_pmu_cpu_dying,
@@ -4449,6 +4445,10 @@ static struct attribute_group group_format_extra_skl = { .is_visible = exra_is_visible, }; +static struct attribute_group group_default = { + .attrs = intel_pmu_attrs, +}; +static const struct attribute_group *attr_update[] = { &group_events_td, &group_events_mem,
@@ -4457,6 +4457,7 @@ static const struct attribute_group *attr_update[] = { &group_caps_lbr, &group_format_extra, &group_format_extra_skl, + &group_default, NULL, };
arch/x86/events/perf_event.h +0 -3
@@ -636,7 +636,6 @@ struct x86_pmu {const struct attribute_group **attr_update;unsigned long attr_freeze_on_smi; -struct attribute **attrs;/* * CPU Hotplug hooks
@@ -903,8 +902,6 @@ static inline void set_linear_ip(struct pt_regs *regs, unsigned long ip)ssize_t x86_event_sysfs_show (char *page,u64 config,u64 event);ssize_t intel_event_sysfs_show (char *page,u64 config); -struct attribute **merge_attr (struct attribute **a,struct attribute **b); -ssize_t events_sysfs_show (struct device *dev,struct device_attribute *attr,char *page); ssize_t events_ht_sysfs_show(struct device *dev, struct device_attribute *attr,
Signature · 1 line
2.20.1
Greg Kroah-Hartman
maintainerRe: [PATCH 8/8] perf/x86: Use update attribute groups for default attributes
Quoted 2 lines · Jiri Olsa
Using the new
pmu::update_attrsattribute group for default attributes -freeze_on_smi,allow_tsx_force_abort.
"And delete the unused merge_attr() function"
Nice work with this series, it looks sane to me!
greg k-h