From 5d7ddd4e59f6cc242618b758467424dc4ee882bf Mon Sep 17 00:00:00 2001 From: mxiao-js <138932298+mxiao-js@users.noreply.github.com> Date: Tue, 8 Jul 2025 17:56:39 -0400 Subject: [PATCH 1/4] register tsc_freq_khz device property in arch/x86/kernel/setup.c --- arch/x86/kernel/setup.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index fb27be6971286d..0af815e9d2c8c8 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -1311,3 +1312,34 @@ bool arch_cpu_is_hotpluggable(int cpu) return cpu > 0; } #endif /* CONFIG_HOTPLUG_CPU */ + +/* bodge: overwrite arch_register_cpu to add a tsc sysfs entry post-init of the cpu. + * i doubt this is the right spot for it to live, but it's at least justifiable as + * it's next to arch_cpu_is_hotpluggable. + */ +static ssize_t tsc_freq_khz_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + return sysfs_emit(buf, "%lu\n", tsc_khz); +} + +static DEVICE_ATTR_RO(tsc_freq_khz); + +int arch_register_cpu(int cpu) +{ + struct cpu *c = per_cpu_ptr(cpu_devices, cpu); + int register_result; + + c->hotpluggable = arch_cpu_is_hotpluggable(cpu); + + register_result = register_cpu(c, cpu); + + if (!register_result) { + device_create_file(&c->dev, &dev_attr_tsc_freq_khz); + } + + return register_result; +} + + From d1fa69b1add15c93d3c1615c64c95d64a42f85b3 Mon Sep 17 00:00:00 2001 From: mxiao-js <138932298+mxiao-js@users.noreply.github.com> Date: Tue, 8 Jul 2025 17:58:44 -0400 Subject: [PATCH 2/4] also include device.h for DEVICE_ATTR_RO / other structs not sure if necessary, add it just in case. --- arch/x86/kernel/setup.c | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index 0af815e9d2c8c8..f3fd5179c0a5e9 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include From 7c2bb9796941254322ce4a6556f2df0eb908dc08 Mon Sep 17 00:00:00 2001 From: mxiao-js <138932298+mxiao-js@users.noreply.github.com> Date: Tue, 8 Jul 2025 18:04:38 -0400 Subject: [PATCH 3/4] add unregister handler fix leak from hotpluggable cpus --- arch/x86/kernel/setup.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index f3fd5179c0a5e9..d376984bb66f46 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -1343,4 +1343,13 @@ int arch_register_cpu(int cpu) return register_result; } +#ifdef CONFIG_HOTPLUG_CPU +void arch_unregister_cpu(int cpu) +{ + struct cpu *c = per_cpu_ptr(cpu_devices, cpu); + device_remove_file(&c->dev, &dev_attr_tsc_freq_khz); + + unregister_cpu(); +} +#endif /* CONFIG_HOTPLUG_CPU */ From e469eb46fe806586af7927b99da14a821f656849 Mon Sep 17 00:00:00 2001 From: mxiao-js <138932298+mxiao-js@users.noreply.github.com> Date: Wed, 9 Jul 2025 00:17:07 -0400 Subject: [PATCH 4/4] Make code compile (and work) --- arch/x86/kernel/setup.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index d376984bb66f46..ca80189c075d7f 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -1329,7 +1329,7 @@ static DEVICE_ATTR_RO(tsc_freq_khz); int arch_register_cpu(int cpu) { - struct cpu *c = per_cpu_ptr(cpu_devices, cpu); + struct cpu *c = &per_cpu(cpu_devices, cpu); int register_result; c->hotpluggable = arch_cpu_is_hotpluggable(cpu); @@ -1346,10 +1346,10 @@ int arch_register_cpu(int cpu) #ifdef CONFIG_HOTPLUG_CPU void arch_unregister_cpu(int cpu) { - struct cpu *c = per_cpu_ptr(cpu_devices, cpu); + struct cpu *c = &per_cpu(cpu_devices, cpu); device_remove_file(&c->dev, &dev_attr_tsc_freq_khz); - unregister_cpu(); + unregister_cpu(c); } #endif /* CONFIG_HOTPLUG_CPU */