2022-10-07 14:42:35

by Jiaxun Yang

[permalink] [raw]
Subject: [PATCH v5] MIPS: Expose prid and globalnumber to sysfs

Some application would like to know precise model and rev of processor
to do errata workaround or optimization.

Expose them in sysfs as:
/sys/devices/system/cpu/cpuX/regs/identification/prid
/sys/devices/system/cpu/cpuX/regs/identification/globalnumber

Reusing AArch64 CPU registers directory.

Signed-off-by: Jiaxun Yang <[email protected]>
---
v2: Drop static qualifier for kobj (gregkh)
v3: Use kzalloc to allocate struct cpuregs.
note: When Greg mentioned about static I was thinking about
static qualifier of percpu variable. After reading documents
again it turns out kobjs should be allocated at runtime. Arm64's
cpuinfo kobj is also on a percpu variable... I guess that was a
intentional use?
v4: Properly handle err of kobj creation. (gregkh)
v5: Drop invalid kfree
---
.../ABI/testing/sysfs-devices-system-cpu | 11 +++
arch/mips/kernel/topology.c | 99 +++++++++++++++++++
2 files changed, 110 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-devices-system-cpu b/Documentation/ABI/testing/sysfs-devices-system-cpu
index 5bf61881f012..9fdfe2de0f76 100644
--- a/Documentation/ABI/testing/sysfs-devices-system-cpu
+++ b/Documentation/ABI/testing/sysfs-devices-system-cpu
@@ -512,6 +512,17 @@ Description: information about CPUs heterogeneity.

cpu_capacity: capacity of cpuX.

+What: /sys/devices/system/cpu/cpuX/regs/
+ /sys/devices/system/cpu/cpuX/regs/identification/
+ /sys/devices/system/cpu/cpuX/regs/identification/prid
+ /sys/devices/system/cpu/cpuX/regs/identification/globalnumber
+Date: October 2022
+Contact: Linux MIPS Kernel Mailing list <[email protected]>
+Description: MIPS CPU registers
+
+ 'identification' directory exposes the Processor ID and Global Number
+ registers for identifying model and revision of the CPU.
+
What: /sys/devices/system/cpu/vulnerabilities
/sys/devices/system/cpu/vulnerabilities/meltdown
/sys/devices/system/cpu/vulnerabilities/spectre_v1
diff --git a/arch/mips/kernel/topology.c b/arch/mips/kernel/topology.c
index 9429d85a4703..80aaaca3cfbc 100644
--- a/arch/mips/kernel/topology.c
+++ b/arch/mips/kernel/topology.c
@@ -5,6 +5,8 @@
#include <linux/node.h>
#include <linux/nodemask.h>
#include <linux/percpu.h>
+#include <linux/seq_file.h>
+#include <linux/smp.h>

static DEFINE_PER_CPU(struct cpu, cpu_devices);

@@ -26,3 +28,100 @@ static int __init topology_init(void)
}

subsys_initcall(topology_init);
+
+static struct kobj_type cpuregs_kobj_type = {
+ .sysfs_ops = &kobj_sysfs_ops,
+};
+
+struct cpureg {
+ struct kobject kobj;
+ struct cpuinfo_mips *info;
+};
+static DEFINE_PER_CPU(struct cpureg *, cpuregs);
+
+#define kobj_to_cpureg(kobj) container_of(kobj, struct cpureg, kobj)
+#define CPUREGS_ATTR_RO(_name, _field) \
+ static ssize_t _name##_show(struct kobject *kobj, \
+ struct kobj_attribute *attr, char *buf) \
+ { \
+ struct cpuinfo_mips *info = kobj_to_cpureg(kobj)->info; \
+ \
+ return sprintf(buf, "0x%08x\n", info->_field); \
+ } \
+ static struct kobj_attribute cpuregs_attr_##_name = __ATTR_RO(_name)
+
+CPUREGS_ATTR_RO(prid, processor_id);
+CPUREGS_ATTR_RO(globalnumber, globalnumber);
+
+static struct attribute *cpuregs_id_attrs[] = {
+ &cpuregs_attr_prid.attr,
+ &cpuregs_attr_globalnumber.attr,
+ NULL
+};
+
+static const struct attribute_group cpuregs_attr_group = {
+ .attrs = cpuregs_id_attrs,
+ .name = "identification"
+};
+
+static int cpuregs_cpu_online(unsigned int cpu)
+{
+ int rc;
+ struct device *dev;
+ struct cpureg *reg;
+
+ dev = get_cpu_device(cpu);
+ if (!dev) {
+ rc = -ENODEV;
+ goto out;
+ }
+ reg = kzalloc(sizeof(struct cpureg), GFP_KERNEL);
+ if (!reg) {
+ rc = -ENOMEM;
+ goto out;
+ }
+ rc = kobject_init_and_add(&reg->kobj, &cpuregs_kobj_type,
+ &dev->kobj, "regs");
+ if (rc)
+ goto out_kobj;
+ rc = sysfs_create_group(&reg->kobj, &cpuregs_attr_group);
+ if (rc)
+ goto out_kobj;
+
+ return 0;
+out_kobj:
+ kobject_put(&reg->kobj);
+out:
+ return rc;
+}
+
+static int cpuregs_cpu_offline(unsigned int cpu)
+{
+ struct device *dev;
+ struct cpureg *reg = per_cpu(cpuregs, cpu);
+
+ dev = get_cpu_device(cpu);
+ if (!dev || !reg)
+ return -ENODEV;
+ if (reg->kobj.parent) {
+ sysfs_remove_group(&reg->kobj, &cpuregs_attr_group);
+ kobject_put(&reg->kobj);
+ }
+
+ return 0;
+}
+
+static int __init cpuinfo_regs_init(void)
+{
+ int ret;
+
+ ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "mips/topology:online",
+ cpuregs_cpu_online, cpuregs_cpu_offline);
+ if (ret < 0) {
+ pr_err("cpuinfo: failed to register hotplug callbacks.\n");
+ return ret;
+ }
+ return 0;
+}
+
+device_initcall(cpuinfo_regs_init);
--
2.37.0 (Apple Git-136)


2022-10-07 15:21:36

by Greg KH

[permalink] [raw]
Subject: Re: [PATCH v5] MIPS: Expose prid and globalnumber to sysfs

On Fri, Oct 07, 2022 at 03:12:07PM +0100, Jiaxun Yang wrote:
> Some application would like to know precise model and rev of processor
> to do errata workaround or optimization.
>
> Expose them in sysfs as:
> /sys/devices/system/cpu/cpuX/regs/identification/prid
> /sys/devices/system/cpu/cpuX/regs/identification/globalnumber
>
> Reusing AArch64 CPU registers directory.
>
> Signed-off-by: Jiaxun Yang <[email protected]>
> ---
> v2: Drop static qualifier for kobj (gregkh)
> v3: Use kzalloc to allocate struct cpuregs.
> note: When Greg mentioned about static I was thinking about
> static qualifier of percpu variable. After reading documents
> again it turns out kobjs should be allocated at runtime. Arm64's
> cpuinfo kobj is also on a percpu variable... I guess that was a
> intentional use?
> v4: Properly handle err of kobj creation. (gregkh)
> v5: Drop invalid kfree
> ---
> .../ABI/testing/sysfs-devices-system-cpu | 11 +++
> arch/mips/kernel/topology.c | 99 +++++++++++++++++++
> 2 files changed, 110 insertions(+)
>
> diff --git a/Documentation/ABI/testing/sysfs-devices-system-cpu b/Documentation/ABI/testing/sysfs-devices-system-cpu
> index 5bf61881f012..9fdfe2de0f76 100644
> --- a/Documentation/ABI/testing/sysfs-devices-system-cpu
> +++ b/Documentation/ABI/testing/sysfs-devices-system-cpu
> @@ -512,6 +512,17 @@ Description: information about CPUs heterogeneity.
>
> cpu_capacity: capacity of cpuX.
>
> +What: /sys/devices/system/cpu/cpuX/regs/
> + /sys/devices/system/cpu/cpuX/regs/identification/
> + /sys/devices/system/cpu/cpuX/regs/identification/prid
> + /sys/devices/system/cpu/cpuX/regs/identification/globalnumber
> +Date: October 2022
> +Contact: Linux MIPS Kernel Mailing list <[email protected]>
> +Description: MIPS CPU registers
> +
> + 'identification' directory exposes the Processor ID and Global Number
> + registers for identifying model and revision of the CPU.
> +
> What: /sys/devices/system/cpu/vulnerabilities
> /sys/devices/system/cpu/vulnerabilities/meltdown
> /sys/devices/system/cpu/vulnerabilities/spectre_v1
> diff --git a/arch/mips/kernel/topology.c b/arch/mips/kernel/topology.c
> index 9429d85a4703..80aaaca3cfbc 100644
> --- a/arch/mips/kernel/topology.c
> +++ b/arch/mips/kernel/topology.c
> @@ -5,6 +5,8 @@
> #include <linux/node.h>
> #include <linux/nodemask.h>
> #include <linux/percpu.h>
> +#include <linux/seq_file.h>
> +#include <linux/smp.h>
>
> static DEFINE_PER_CPU(struct cpu, cpu_devices);
>
> @@ -26,3 +28,100 @@ static int __init topology_init(void)
> }
>
> subsys_initcall(topology_init);
> +
> +static struct kobj_type cpuregs_kobj_type = {
> + .sysfs_ops = &kobj_sysfs_ops,
> +};
> +
> +struct cpureg {
> + struct kobject kobj;
> + struct cpuinfo_mips *info;
> +};
> +static DEFINE_PER_CPU(struct cpureg *, cpuregs);
> +
> +#define kobj_to_cpureg(kobj) container_of(kobj, struct cpureg, kobj)
> +#define CPUREGS_ATTR_RO(_name, _field) \
> + static ssize_t _name##_show(struct kobject *kobj, \
> + struct kobj_attribute *attr, char *buf) \
> + { \
> + struct cpuinfo_mips *info = kobj_to_cpureg(kobj)->info; \
> + \
> + return sprintf(buf, "0x%08x\n", info->_field); \

sysfs_emit() please.


> + } \
> + static struct kobj_attribute cpuregs_attr_##_name = __ATTR_RO(_name)
> +
> +CPUREGS_ATTR_RO(prid, processor_id);
> +CPUREGS_ATTR_RO(globalnumber, globalnumber);
> +
> +static struct attribute *cpuregs_id_attrs[] = {
> + &cpuregs_attr_prid.attr,
> + &cpuregs_attr_globalnumber.attr,
> + NULL
> +};
> +
> +static const struct attribute_group cpuregs_attr_group = {
> + .attrs = cpuregs_id_attrs,
> + .name = "identification"
> +};
> +
> +static int cpuregs_cpu_online(unsigned int cpu)
> +{
> + int rc;
> + struct device *dev;
> + struct cpureg *reg;
> +
> + dev = get_cpu_device(cpu);
> + if (!dev) {
> + rc = -ENODEV;
> + goto out;
> + }
> + reg = kzalloc(sizeof(struct cpureg), GFP_KERNEL);
> + if (!reg) {
> + rc = -ENOMEM;
> + goto out;
> + }
> + rc = kobject_init_and_add(&reg->kobj, &cpuregs_kobj_type,
> + &dev->kobj, "regs");
> + if (rc)
> + goto out_kobj;
> + rc = sysfs_create_group(&reg->kobj, &cpuregs_attr_group);
> + if (rc)
> + goto out_kobj;
> +
> + return 0;
> +out_kobj:
> + kobject_put(&reg->kobj);
> +out:
> + return rc;
> +}
> +
> +static int cpuregs_cpu_offline(unsigned int cpu)
> +{
> + struct device *dev;
> + struct cpureg *reg = per_cpu(cpuregs, cpu);
> +
> + dev = get_cpu_device(cpu);
> + if (!dev || !reg)
> + return -ENODEV;
> + if (reg->kobj.parent) {

Why are you looking at the parent of a kobject? Why not just always
remove the kobject if you have a reference to it now? How does the
parent matter?

thanks,

greg k-h

2022-10-07 16:59:58

by Greg KH

[permalink] [raw]
Subject: Re: [PATCH v5] MIPS: Expose prid and globalnumber to sysfs

On Fri, Oct 07, 2022 at 05:37:34PM +0100, Jiaxun Yang wrote:
>
>
> 在2022年10月7日十月 下午3:52,Greg KH写道:
> [...]
> >> +
> >> +static int cpuregs_cpu_offline(unsigned int cpu)
> >> +{
> >> + struct device *dev;
> >> + struct cpureg *reg = per_cpu(cpuregs, cpu);
> >> +
> >> + dev = get_cpu_device(cpu);
> >> + if (!dev || !reg)
> >> + return -ENODEV;
> >> + if (reg->kobj.parent) {
> >
> > Why are you looking at the parent of a kobject? Why not just always
> > remove the kobject if you have a reference to it now? How does the
> > parent matter?
>
> Another dummy copy from Arm64 code... kobject_put should be enough here?

Why would it not be enough?

2022-10-07 17:13:33

by Jiaxun Yang

[permalink] [raw]
Subject: Re: [PATCH v5] MIPS: Expose prid and globalnumber to sysfs



在2022年10月7日十月 下午3:52,Greg KH写道:
[...]
>> +
>> +static int cpuregs_cpu_offline(unsigned int cpu)
>> +{
>> + struct device *dev;
>> + struct cpureg *reg = per_cpu(cpuregs, cpu);
>> +
>> + dev = get_cpu_device(cpu);
>> + if (!dev || !reg)
>> + return -ENODEV;
>> + if (reg->kobj.parent) {
>
> Why are you looking at the parent of a kobject? Why not just always
> remove the kobject if you have a reference to it now? How does the
> parent matter?

Another dummy copy from Arm64 code... kobject_put should be enough here?

Thanks

>
> thanks,
>
> greg k-h

--
- Jiaxun

2022-10-08 08:14:33

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v5] MIPS: Expose prid and globalnumber to sysfs

Hi Jiaxun,

I love your patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v6.0 next-20221007]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Jiaxun-Yang/MIPS-Expose-prid-and-globalnumber-to-sysfs/20221007-221349
base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 4c86114194e644b6da9107d75910635c9e87179e
reproduce:
# https://github.com/intel-lab-lkp/linux/commit/630e2c4b72708ec85e8abb5a045bcec6e15ec1f0
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Jiaxun-Yang/MIPS-Expose-prid-and-globalnumber-to-sysfs/20221007-221349
git checkout 630e2c4b72708ec85e8abb5a045bcec6e15ec1f0
make menuconfig
# enable CONFIG_COMPILE_TEST, CONFIG_WARN_MISSING_DOCUMENTS, CONFIG_WARN_ABI_ERRORS
make htmldocs

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>

All warnings (new ones prefixed by >>):

>> Warning: /sys/devices/system/cpu/cpuX/regs/ is defined 2 times: ./Documentation/ABI/testing/sysfs-devices-system-cpu:486 ./Documentation/ABI/testing/sysfs-devices-system-cpu:514

--
0-DAY CI Kernel Test Service
https://01.org/lkp


Attachments:
(No filename) (1.49 kB)
config (39.37 kB)
Download all attachments