Add "preferred" file in /sys/devices/system/cpu

This would help
- Users to quickly check which CPUs are marked as preferred.
- Userspace daemons such as irqbalance to use this mask to
  send irq into preferred CPUs.

For example:
cat /sys/devices/system/cpu/online
0-719
cat /sys/devices/system/cpu/preferred
0-599        <<< Implies 0-599 are preferred for workloads and 600-719
                 should be avoided at this moment.

cat /sys/devices/system/cpu/preferred
0-719        <<< All CPUs are usable. There is no preference.

Signed-off-by: Shrikanth Hegde <[email protected]>
---
 Documentation/ABI/testing/sysfs-devices-system-cpu | 14 ++++++++++++++
 drivers/base/cpu.c                                 | 12 ++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-devices-system-cpu 
b/Documentation/ABI/testing/sysfs-devices-system-cpu
index 82d10d556cc8..676412c46b2a 100644
--- a/Documentation/ABI/testing/sysfs-devices-system-cpu
+++ b/Documentation/ABI/testing/sysfs-devices-system-cpu
@@ -806,3 +806,17 @@ Date:              Nov 2022
 Contact:       Linux kernel mailing list <[email protected]>
 Description:
                (RO) the list of CPUs that can be brought online.
+
+What:          /sys/devices/system/cpu/preferred
+Date:          July 2026
+Contact:       Linux kernel mailing list <[email protected]>
+Description:
+               (RO) the list of preferred CPUs applicable in
+               paravirtualized environments.
+
+               The steal governor driver dynamically adjusts this mask
+               based on observed steal time. Scheduling tasks on
+               CPUs outside of this list may lead to performance
+               degradations due to underlying physical CPU contention.
+
+               See Documentation/scheduler/sched-arch.rst for more details.
diff --git a/drivers/base/cpu.c b/drivers/base/cpu.c
index 19d288a3c80c..5da2a96fb37e 100644
--- a/drivers/base/cpu.c
+++ b/drivers/base/cpu.c
@@ -391,6 +391,15 @@ static int cpu_uevent(const struct device *dev, struct 
kobj_uevent_env *env)
 }
 #endif
 
+#ifdef CONFIG_PREFERRED_CPU
+static ssize_t preferred_show(struct device *dev,
+                             struct device_attribute *attr, char *buf)
+{
+       return sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(cpu_preferred_mask));
+}
+static DEVICE_ATTR_RO(preferred);
+#endif
+
 const struct bus_type cpu_subsys = {
        .name = "cpu",
        .dev_name = "cpu",
@@ -531,6 +540,9 @@ static struct attribute *cpu_root_attrs[] = {
 #endif
 #ifdef CONFIG_GENERIC_CPU_AUTOPROBE
        &dev_attr_modalias.attr,
+#endif
+#ifdef CONFIG_PREFERRED_CPU
+       &dev_attr_preferred.attr,
 #endif
        NULL
 };
-- 
2.47.3


Reply via email to