[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250424095228.1112558-1-quic_zhonhan@quicinc.com>
Date: Thu, 24 Apr 2025 17:52:28 +0800
From: Zhongqiu Han <quic_zhonhan@...cinc.com>
To: <rafael@...nel.org>, <pavel@...nel.org>, <len.brown@...el.com>
CC: <linux-pm@...r.kernel.org>, <linux-kernel@...r.kernel.org>,
<quic_zhonhan@...cinc.com>
Subject: [PATCH] PM: QoS: Add support for CPU affinity mask-based CPUs latency QoS
Currently, the PM QoS framework supports global CPU latency QoS and
per-device CPU latency QoS requests. An example of using global CPU
latency QoS is a commit 2777e73fc154 ("scsi: ufs: core: Add CPU latency
QoS support for UFS driver") that improved random io performance by 15%
for ufs on specific device platform.
However, this prevents all CPUs in the system from entering C states.
Typically, some threads or drivers know which specific CPUs they are
interested in. For example, drivers with IRQ affinity only want interrupts
to wake up and be handled on specific CPUs. Similarly, kernel thread bound
to specific CPUs through affinity only care about the latency of those
particular CPUs.
This patch introduces support for partial CPUs PM QoS using a CPU affinity
mask, allowing flexible and more precise latency QoS settings for specific
CPUs. This can help save power, especially on heterogeneous platforms with
big and little cores, as well as some power-conscious embedded systems for
example:
driver A rt kthread B module C
QoS cpu mask: 0-3 2-5 6-7
target latency: 20 30 50
| | |
v v v
+---------------------------------+
| PM QoS Framework |
+---------------------------------+
| | |
v v v
cpu mask: 0-3 2-3,4-5 6-7
actual latency: 20 20, 30 50
Implement this support based on per-device CPU latency PM QoS.
Signed-off-by: Zhongqiu Han <quic_zhonhan@...cinc.com>
---
include/linux/pm_qos.h | 35 +++++++++
kernel/power/qos.c | 158 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 193 insertions(+)
diff --git a/include/linux/pm_qos.h b/include/linux/pm_qos.h
index 4a69d4af3ff8..59139e5a0a30 100644
--- a/include/linux/pm_qos.h
+++ b/include/linux/pm_qos.h
@@ -131,6 +131,11 @@ enum pm_qos_req_action {
PM_QOS_REMOVE_REQ /* Remove an existing request */
};
+struct cpu_affinity_qos_req {
+ struct list_head list;
+ struct dev_pm_qos_request req;
+};
+
static inline int dev_pm_qos_request_active(struct dev_pm_qos_request *req)
{
return req->dev != NULL;
@@ -317,3 +322,33 @@ int freq_qos_remove_notifier(struct freq_constraints *qos,
struct notifier_block *notifier);
#endif
+
+#if defined(CONFIG_CPU_IDLE) && defined(CONFIG_PM)
+int cpu_latency_qos_affinity_add(struct cpu_affinity_qos_req *pm_req,
+ const cpumask_t *affinity_mask, s32 latency_value);
+int cpu_latency_qos_affinity_update(struct cpu_affinity_qos_req *pm_req,
+ s32 new_value);
+int cpu_latency_qos_affinity_remove(struct cpu_affinity_qos_req *pm_req);
+int cpu_latency_qos_affinity_release(struct list_head *pm_reqs);
+void wakeup_qos_affinity_idle_cpu(int cpu);
+#else
+int cpu_latency_qos_affinity_add(struct cpu_affinity_qos_req *pm_req,
+ const cpumask_t *affinity_mask, s32 latency_value)
+{
+ return 0;
+}
+int cpu_latency_qos_affinity_update(struct cpu_affinity_qos_req *pm_req,
+ s32 new_value)
+{
+ return 0;
+}
+int cpu_latency_qos_affinity_remove(struct cpu_affinity_qos_req *pm_req)
+{
+ return 0;
+}
+int cpu_latency_qos_affinity_release(struct list_head *pm_reqs)
+{
+ return 0;
+}
+void wakeup_qos_affinity_idle_cpu(int cpu) {}
+#endif
diff --git a/kernel/power/qos.c b/kernel/power/qos.c
index 4244b069442e..b9b814ee670f 100644
--- a/kernel/power/qos.c
+++ b/kernel/power/qos.c
@@ -335,6 +335,164 @@ void cpu_latency_qos_remove_request(struct pm_qos_request *req)
}
EXPORT_SYMBOL_GPL(cpu_latency_qos_remove_request);
+#ifdef CONFIG_PM
+
+/**
+ * wakeup_qos_affinity_idle_cpu - break one specific cpu out of idle.
+ */
+void wakeup_qos_affinity_idle_cpu(int cpu)
+{
+ preempt_disable();
+ if (cpu != smp_processor_id() && cpu_online(cpu))
+ wake_up_if_idle(cpu);
+ preempt_enable();
+}
+
+/**
+ * cpu_latency_qos_affinity_add - Add new CPU affinity latency QoS request.
+ * @pm_req : Pointer to a preallocated handle.
+ * @affinity_mask: Mask to determine which CPUs need latency QoS.
+ * @new_value: New requested constraint value.
+ *
+ * Use @latency_value to initialize the request handle pointed to by @pm_req,
+ * insert it as a new entry to the CPU latency QoS list and recompute the
+ * effective QoS constraint for that list, @affinity_mask determine which CPUs
+ * need the latency QoS.
+ *
+ * Callers need to save the handle for later use in updates and removal of the
+ * QoS request represented by it.
+ */
+int cpu_latency_qos_affinity_add(struct cpu_affinity_qos_req *pm_req,
+ const cpumask_t *affinity_mask,
+ s32 latency_value)
+{
+ int cpu;
+ cpumask_t actual_mask;
+ struct cpu_affinity_qos_req *cpu_pm_req;
+ int ret = 0;
+
+ if (!pm_req)
+ pr_err("%s: invalid PM Qos request\n", __func__);
+
+ INIT_LIST_HEAD(&pm_req->list);
+
+ if (!affinity_mask || cpumask_empty(affinity_mask) ||
+ latency_value < 0) {
+ pr_err("%s: invalid PM Qos request value\n", __func__);
+ return -EINVAL;
+ }
+
+ for_each_cpu(cpu, affinity_mask) {
+ cpu_pm_req = kzalloc(sizeof(struct cpu_affinity_qos_req),
+ GFP_KERNEL);
+ if (!cpu_pm_req) {
+ ret = -ENOMEM;
+ goto out_err;
+ }
+ ret = dev_pm_qos_add_request(get_cpu_device(cpu),
+ &cpu_pm_req->req,
+ DEV_PM_QOS_RESUME_LATENCY,
+ latency_value);
+ if (ret < 0) {
+ pr_err("failed to add latency req for cpu%d", cpu);
+ kfree(cpu_pm_req);
+ goto out_err;
+ } else if (ret > 0) {
+ wakeup_qos_affinity_idle_cpu(cpu);
+ }
+
+ cpumask_set_cpu(cpu, &actual_mask);
+ list_add(&cpu_pm_req->list, &pm_req->list);
+ }
+
+ pr_info("PM Qos latency: %d added on cpus %*pb\n", latency_value,
+ cpumask_pr_args(&actual_mask));
+
+ return ret;
+
+out_err:
+ cpu_latency_qos_affinity_release(&pm_req->list);
+ pr_err("failed to add PM QoS latency req, removed all added requests\n");
+ return ret;
+}
+EXPORT_SYMBOL_GPL(cpu_latency_qos_affinity_add);
+
+/**
+ * cpu_latency_qos_affinity_update - Modify existing CPU affinity latency QoS.
+ * @pm_req : QoS request to update for CPUs with affinity masks.
+ * @new_value: New requested constraint value.
+ *
+ * Use @new_value to update the QoS request represented by @pm_req in the CPU
+ * latency QoS list along with updating the effective constraint value for that
+ * list.
+ */
+int cpu_latency_qos_affinity_update(struct cpu_affinity_qos_req *pm_req,
+ s32 new_value)
+{
+ struct cpu_affinity_qos_req *cpu_pm_req, *next;
+ int ret = 0;
+
+ if (!pm_req || new_value < 0 || list_empty(&pm_req->list)) {
+ pr_err("%s: invalid PM Qos request value\n", __func__);
+ return -EINVAL;
+ }
+
+ list_for_each_entry_safe(cpu_pm_req, next, &pm_req->list, list) {
+ ret = dev_pm_qos_update_request(&cpu_pm_req->req, new_value);
+ if (ret < 0) {
+ pr_err("PM QoS qos update failed for %s\n",
+ dev_name(cpu_pm_req->req.dev));
+ } else if (ret > 0) {
+ wakeup_qos_affinity_idle_cpu(cpu_pm_req->req.dev->id);
+ }
+ }
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(cpu_latency_qos_affinity_update);
+
+/**
+ * cpu_latency_qos_affinity_remove - Remove existing CPU affinity latency QoS.
+ * @pm_req: QoS request to update for CPUs with affinity masks.
+ *
+ * Remove the CPU latency QoS request represented by @pm_req from the CPU latency
+ * QoS list.
+ */
+int cpu_latency_qos_affinity_remove(struct cpu_affinity_qos_req *pm_req)
+{
+ if (!pm_req || list_empty(&pm_req->list)) {
+ pr_err("%s: invalid PM Qos request value\n", __func__);
+ return -EINVAL;
+ }
+
+ return cpu_latency_qos_affinity_release(&pm_req->list);
+}
+EXPORT_SYMBOL_GPL(cpu_latency_qos_affinity_remove);
+
+/**
+ * cpu_latency_qos_affinity_release - Release pm_reqs latency QoS resource.
+ * @pm_req: QoS request to remove.
+ *
+ * Release pm_reqs managed CPU affinity latency QoS resource.
+ */
+int cpu_latency_qos_affinity_release(struct list_head *pm_reqs)
+{
+ int ret = 0;
+ struct cpu_affinity_qos_req *cpu_pm_req, *next;
+
+ list_for_each_entry_safe(cpu_pm_req, next, pm_reqs, list) {
+ ret = dev_pm_qos_remove_request(&cpu_pm_req->req);
+ if (ret < 0)
+ pr_err("failed to remove qos request for %s\n",
+ dev_name(cpu_pm_req->req.dev));
+ list_del(&cpu_pm_req->list);
+ kfree(cpu_pm_req);
+ }
+
+ return ret;
+}
+#endif /* CONFIG_PM */
+
/* User space interface to the CPU latency QoS via misc device. */
static int cpu_latency_qos_open(struct inode *inode, struct file *filp)
--
2.25.1
Powered by blists - more mailing lists