Add a per-CPU sysfs attribute at: /sys/devices/system/cpu/cpuN/cpuidle/latency_limit_ns This allows privileged userspace to set a governor-respected upper bound on the exit latency for idle state selection on a given CPU. When set (non-zero), cpuidle_governor_latency_req() returns the minimum of the existing PM QoS constraints and latency_limit_ns, so all governors (menu, TEO, haltpoll) automatically respect it without per-governor modifications. Use case: cloud operators running mixed workloads can cap idle depth on CPUs pinned to latency-sensitive VMs while allowing other CPUs to enter deep C-states for energy savings. The interface is latency-based (nanoseconds) rather than C-state-index-based, making it portable across Intel/AMD/ARM without uarch-specific tuning. Signed-off-by: Anthony Harivel --- drivers/cpuidle/governor.c | 10 +++++++++- drivers/cpuidle/sysfs.c | 36 ++++++++++++++++++++++++++++++++++++ include/linux/cpuidle.h | 1 + 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/drivers/cpuidle/governor.c b/drivers/cpuidle/governor.c index 5d0e7f78c6c5..4c6f77ce2028 100644 --- a/drivers/cpuidle/governor.c +++ b/drivers/cpuidle/governor.c @@ -112,6 +112,8 @@ s64 cpuidle_governor_latency_req(unsigned int cpu) int device_req = dev_pm_qos_raw_resume_latency(device); int global_req = cpu_latency_qos_limit(); int global_wake_req = cpu_wakeup_latency_qos_limit(); + struct cpuidle_device *dev; + s64 result; if (global_req > global_wake_req) global_req = global_wake_req; @@ -119,5 +121,11 @@ s64 cpuidle_governor_latency_req(unsigned int cpu) if (device_req > global_req) device_req = global_req; - return (s64)device_req * NSEC_PER_USEC; + result = (s64)device_req * NSEC_PER_USEC; + + dev = per_cpu(cpuidle_devices, cpu); + if (dev && dev->latency_limit_ns && dev->latency_limit_ns < result) + result = dev->latency_limit_ns; + + return result; } diff --git a/drivers/cpuidle/sysfs.c b/drivers/cpuidle/sysfs.c index b81d22479234..d060a4b7facc 100644 --- a/drivers/cpuidle/sysfs.c +++ b/drivers/cpuidle/sysfs.c @@ -207,8 +207,44 @@ static void cpuidle_sysfs_release(struct kobject *kobj) complete(&kdev->kobj_unregister); } +static ssize_t show_latency_limit_ns(struct cpuidle_device *dev, char *buf) +{ + return sysfs_emit(buf, "%llu\n", dev->latency_limit_ns); +} + +static ssize_t store_latency_limit_ns(struct cpuidle_device *dev, + const char *buf, size_t count) +{ + u64 value; + int err; + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + + err = kstrtou64(buf, 0, &value); + if (err) + return err; + + dev->latency_limit_ns = value; + + return count; +} + +static struct cpuidle_attr attr_latency_limit_ns = { + .attr = { .name = "latency_limit_ns", .mode = 0644 }, + .show = show_latency_limit_ns, + .store = store_latency_limit_ns, +}; + +static struct attribute *cpuidle_device_default_attrs[] = { + &attr_latency_limit_ns.attr, + NULL, +}; +ATTRIBUTE_GROUPS(cpuidle_device_default); + static const struct kobj_type ktype_cpuidle = { .sysfs_ops = &cpuidle_sysfs_ops, + .default_groups = cpuidle_device_default_groups, .release = cpuidle_sysfs_release, }; diff --git a/include/linux/cpuidle.h b/include/linux/cpuidle.h index a2485348def3..f70d6288e13d 100644 --- a/include/linux/cpuidle.h +++ b/include/linux/cpuidle.h @@ -101,6 +101,7 @@ struct cpuidle_device { u64 last_residency_ns; u64 poll_limit_ns; u64 forced_idle_latency_limit_ns; + u64 latency_limit_ns; struct cpuidle_state_usage states_usage[CPUIDLE_STATE_MAX]; struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX]; struct cpuidle_driver_kobj *kobj_driver; -- 2.55.0