[PATCH 1/3] topology/sysfs: export die attributes only if an architectures has support

From: Heiko Carstens
Date: Sun Nov 28 2021 - 16:24:50 EST


The die_id and die_cpus topology sysfs attributes have been added with
commit 0e344d8c709f ("cpu/topology: Export die_id") and commit
2e4c54dac7b3 ("topology: Create core_cpus and die_cpus sysfs attributes").

While they are currently only used and useful for x86 they are still
present with bogus default values for all architectures. Instead of
enforcing such new sysfs attributes to all architectures, make them
only optional visible if an architecture opts in by defining both the
topology_die_id and topology_die_cpumask attributes.

This is similar to what was done when the book and drawer topology
levels were introduced: avoid useless and therefore confusing sysfs
attributes for architectures which cannot make use of them.

This should not break any existing applications, since this is a
rather new interface and applications should be able to handle also
older kernel versions without such attributes - besides that they
contain only useful information for x86.

Signed-off-by: Heiko Carstens <hca@xxxxxxxxxxxxx>
---
Documentation/admin-guide/cputopology.rst | 3 +++
drivers/base/topology.c | 8 ++++++++
include/linux/topology.h | 2 ++
3 files changed, 13 insertions(+)

diff --git a/Documentation/admin-guide/cputopology.rst b/Documentation/admin-guide/cputopology.rst
index 6b62e182baf4..c68d07533c45 100644
--- a/Documentation/admin-guide/cputopology.rst
+++ b/Documentation/admin-guide/cputopology.rst
@@ -11,6 +11,9 @@ Architecture-neutral, drivers/base/topology.c, exports these attributes.
However, the book and drawer related sysfs files will only be created if
CONFIG_SCHED_BOOK and CONFIG_SCHED_DRAWER are selected, respectively.

+The die hierarchy related sysfs files will only be created if an architecture
+provides the related macros as described below.
+
CONFIG_SCHED_BOOK and CONFIG_SCHED_DRAWER are currently only used on s390,
where they reflect the cpu and cache hierarchy.

diff --git a/drivers/base/topology.c b/drivers/base/topology.c
index 8f2b641d0b8c..7fa5a7fbef25 100644
--- a/drivers/base/topology.c
+++ b/drivers/base/topology.c
@@ -45,8 +45,10 @@ static ssize_t name##_list_read(struct file *file, struct kobject *kobj, \
define_id_show_func(physical_package_id);
static DEVICE_ATTR_RO(physical_package_id);

+#ifndef topology_die_no_sysfs
define_id_show_func(die_id);
static DEVICE_ATTR_RO(die_id);
+#endif

define_id_show_func(cluster_id);
static DEVICE_ATTR_RO(cluster_id);
@@ -70,9 +72,11 @@ define_siblings_read_func(cluster_cpus, cluster_cpumask);
static BIN_ATTR_RO(cluster_cpus, 0);
static BIN_ATTR_RO(cluster_cpus_list, 0);

+#ifndef topology_die_no_sysfs
define_siblings_read_func(die_cpus, die_cpumask);
static BIN_ATTR_RO(die_cpus, 0);
static BIN_ATTR_RO(die_cpus_list, 0);
+#endif

define_siblings_read_func(package_cpus, core_cpumask);
static BIN_ATTR_RO(package_cpus, 0);
@@ -103,8 +107,10 @@ static struct bin_attribute *bin_attrs[] = {
&bin_attr_core_siblings_list,
&bin_attr_cluster_cpus,
&bin_attr_cluster_cpus_list,
+#ifndef topology_die_no_sysfs
&bin_attr_die_cpus,
&bin_attr_die_cpus_list,
+#endif
&bin_attr_package_cpus,
&bin_attr_package_cpus_list,
#ifdef CONFIG_SCHED_BOOK
@@ -120,7 +126,9 @@ static struct bin_attribute *bin_attrs[] = {

static struct attribute *default_attrs[] = {
&dev_attr_physical_package_id.attr,
+#ifndef topology_die_no_sysfs
&dev_attr_die_id.attr,
+#endif
&dev_attr_cluster_id.attr,
&dev_attr_core_id.attr,
#ifdef CONFIG_SCHED_BOOK
diff --git a/include/linux/topology.h b/include/linux/topology.h
index 0b3704ad13c8..1541d210be24 100644
--- a/include/linux/topology.h
+++ b/include/linux/topology.h
@@ -185,6 +185,7 @@ static inline int cpu_to_mem(int cpu)
#endif
#ifndef topology_die_id
#define topology_die_id(cpu) ((void)(cpu), -1)
+#define topology_die_no_sysfs 1
#endif
#ifndef topology_cluster_id
#define topology_cluster_id(cpu) ((void)(cpu), -1)
@@ -203,6 +204,7 @@ static inline int cpu_to_mem(int cpu)
#endif
#ifndef topology_die_cpumask
#define topology_die_cpumask(cpu) cpumask_of(cpu)
+#define topology_die_no_sysfs 1
#endif

#if defined(CONFIG_SCHED_SMT) && !defined(cpu_smt_mask)
--
2.32.0