diff mbox series

PM: QoS: Add support for CPU affinity mask-based CPUs latency QoS

Message ID 20250424095228.1112558-1-quic_zhonhan@quicinc.com
State New
Headers show
Series PM: QoS: Add support for CPU affinity mask-based CPUs latency QoS | expand

Commit Message

Zhongqiu Han April 24, 2025, 9:52 a.m. UTC
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@quicinc.com>
---
 include/linux/pm_qos.h |  35 +++++++++
 kernel/power/qos.c     | 158 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 193 insertions(+)

Comments

kernel test robot April 25, 2025, 11:10 a.m. UTC | #1
Hi Zhongqiu,

kernel test robot noticed the following build warnings:

[auto build test WARNING on rafael-pm/linux-next]
[also build test WARNING on rafael-pm/bleeding-edge amd-pstate/linux-next amd-pstate/bleeding-edge linus/master v6.15-rc3 next-20250424]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Zhongqiu-Han/PM-QoS-Add-support-for-CPU-affinity-mask-based-CPUs-latency-QoS/20250424-175559
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git linux-next
patch link:    https://lore.kernel.org/r/20250424095228.1112558-1-quic_zhonhan%40quicinc.com
patch subject: [PATCH] PM: QoS: Add support for CPU affinity mask-based CPUs latency QoS
config: s390-randconfig-001-20250425 (https://download.01.org/0day-ci/archive/20250425/202504251845.LAwzxYtY-lkp@intel.com/config)
compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250425/202504251845.LAwzxYtY-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202504251845.LAwzxYtY-lkp@intel.com/

All warnings (new ones prefixed by >>):

   In file included from kernel/cpu.c:41:
   In file included from include/trace/events/power.h:8:
   In file included from include/linux/cpufreq.h:19:
>> include/linux/pm_qos.h:335:5: warning: no previous prototype for function 'cpu_latency_qos_affinity_add' [-Wmissing-prototypes]
     335 | int cpu_latency_qos_affinity_add(struct cpu_affinity_qos_req *pm_req,
         |     ^
   include/linux/pm_qos.h:335:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
     335 | int cpu_latency_qos_affinity_add(struct cpu_affinity_qos_req *pm_req,
         | ^
         | static 
>> include/linux/pm_qos.h:340:5: warning: no previous prototype for function 'cpu_latency_qos_affinity_update' [-Wmissing-prototypes]
     340 | int cpu_latency_qos_affinity_update(struct cpu_affinity_qos_req *pm_req,
         |     ^
   include/linux/pm_qos.h:340:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
     340 | int cpu_latency_qos_affinity_update(struct cpu_affinity_qos_req *pm_req,
         | ^
         | static 
>> include/linux/pm_qos.h:345:5: warning: no previous prototype for function 'cpu_latency_qos_affinity_remove' [-Wmissing-prototypes]
     345 | int cpu_latency_qos_affinity_remove(struct cpu_affinity_qos_req *pm_req)
         |     ^
   include/linux/pm_qos.h:345:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
     345 | int cpu_latency_qos_affinity_remove(struct cpu_affinity_qos_req *pm_req)
         | ^
         | static 
>> include/linux/pm_qos.h:349:5: warning: no previous prototype for function 'cpu_latency_qos_affinity_release' [-Wmissing-prototypes]
     349 | int cpu_latency_qos_affinity_release(struct list_head *pm_reqs)
         |     ^
   include/linux/pm_qos.h:349:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
     349 | int cpu_latency_qos_affinity_release(struct list_head *pm_reqs)
         | ^
         | static 
>> include/linux/pm_qos.h:353:6: warning: no previous prototype for function 'wakeup_qos_affinity_idle_cpu' [-Wmissing-prototypes]
     353 | void wakeup_qos_affinity_idle_cpu(int cpu) {}
         |      ^
   include/linux/pm_qos.h:353:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
     353 | void wakeup_qos_affinity_idle_cpu(int cpu) {}
         | ^
         | static 
   In file included from kernel/cpu.c:41:
   In file included from include/trace/events/power.h:10:
   include/linux/pm_qos.h:335:5: error: redefinition of 'cpu_latency_qos_affinity_add'
     335 | int cpu_latency_qos_affinity_add(struct cpu_affinity_qos_req *pm_req,
         |     ^
   include/linux/pm_qos.h:335:5: note: previous definition is here
     335 | int cpu_latency_qos_affinity_add(struct cpu_affinity_qos_req *pm_req,
         |     ^
   In file included from kernel/cpu.c:41:
   In file included from include/trace/events/power.h:10:
   include/linux/pm_qos.h:340:5: error: redefinition of 'cpu_latency_qos_affinity_update'
     340 | int cpu_latency_qos_affinity_update(struct cpu_affinity_qos_req *pm_req,
         |     ^
   include/linux/pm_qos.h:340:5: note: previous definition is here
     340 | int cpu_latency_qos_affinity_update(struct cpu_affinity_qos_req *pm_req,
         |     ^
   In file included from kernel/cpu.c:41:
   In file included from include/trace/events/power.h:10:
   include/linux/pm_qos.h:345:5: error: redefinition of 'cpu_latency_qos_affinity_remove'
     345 | int cpu_latency_qos_affinity_remove(struct cpu_affinity_qos_req *pm_req)
         |     ^
   include/linux/pm_qos.h:345:5: note: previous definition is here
     345 | int cpu_latency_qos_affinity_remove(struct cpu_affinity_qos_req *pm_req)
         |     ^
   In file included from kernel/cpu.c:41:
   In file included from include/trace/events/power.h:10:
   include/linux/pm_qos.h:349:5: error: redefinition of 'cpu_latency_qos_affinity_release'
     349 | int cpu_latency_qos_affinity_release(struct list_head *pm_reqs)
         |     ^
   include/linux/pm_qos.h:349:5: note: previous definition is here
     349 | int cpu_latency_qos_affinity_release(struct list_head *pm_reqs)
         |     ^
   In file included from kernel/cpu.c:41:
   In file included from include/trace/events/power.h:10:
   include/linux/pm_qos.h:353:6: error: redefinition of 'wakeup_qos_affinity_idle_cpu'
     353 | void wakeup_qos_affinity_idle_cpu(int cpu) {}
         |      ^
   include/linux/pm_qos.h:353:6: note: previous definition is here
     353 | void wakeup_qos_affinity_idle_cpu(int cpu) {}
         |      ^
   5 warnings and 5 errors generated.
--
   In file included from kernel/sched/core.c:88:
   In file included from kernel/sched/sched.h:31:
   In file included from include/linux/cpufreq.h:19:
>> include/linux/pm_qos.h:335:5: warning: no previous prototype for function 'cpu_latency_qos_affinity_add' [-Wmissing-prototypes]
     335 | int cpu_latency_qos_affinity_add(struct cpu_affinity_qos_req *pm_req,
         |     ^
   include/linux/pm_qos.h:335:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
     335 | int cpu_latency_qos_affinity_add(struct cpu_affinity_qos_req *pm_req,
         | ^
         | static 
>> include/linux/pm_qos.h:340:5: warning: no previous prototype for function 'cpu_latency_qos_affinity_update' [-Wmissing-prototypes]
     340 | int cpu_latency_qos_affinity_update(struct cpu_affinity_qos_req *pm_req,
         |     ^
   include/linux/pm_qos.h:340:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
     340 | int cpu_latency_qos_affinity_update(struct cpu_affinity_qos_req *pm_req,
         | ^
         | static 
>> include/linux/pm_qos.h:345:5: warning: no previous prototype for function 'cpu_latency_qos_affinity_remove' [-Wmissing-prototypes]
     345 | int cpu_latency_qos_affinity_remove(struct cpu_affinity_qos_req *pm_req)
         |     ^
   include/linux/pm_qos.h:345:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
     345 | int cpu_latency_qos_affinity_remove(struct cpu_affinity_qos_req *pm_req)
         | ^
         | static 
>> include/linux/pm_qos.h:349:5: warning: no previous prototype for function 'cpu_latency_qos_affinity_release' [-Wmissing-prototypes]
     349 | int cpu_latency_qos_affinity_release(struct list_head *pm_reqs)
         |     ^
   include/linux/pm_qos.h:349:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
     349 | int cpu_latency_qos_affinity_release(struct list_head *pm_reqs)
         | ^
         | static 
>> include/linux/pm_qos.h:353:6: warning: no previous prototype for function 'wakeup_qos_affinity_idle_cpu' [-Wmissing-prototypes]
     353 | void wakeup_qos_affinity_idle_cpu(int cpu) {}
         |      ^
   include/linux/pm_qos.h:353:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
     353 | void wakeup_qos_affinity_idle_cpu(int cpu) {}
         | ^
         | static 
   In file included from kernel/sched/core.c:88:
   In file included from kernel/sched/sched.h:73:
   In file included from include/trace/events/power.h:10:
   include/linux/pm_qos.h:335:5: error: redefinition of 'cpu_latency_qos_affinity_add'
     335 | int cpu_latency_qos_affinity_add(struct cpu_affinity_qos_req *pm_req,
         |     ^
   include/linux/pm_qos.h:335:5: note: previous definition is here
     335 | int cpu_latency_qos_affinity_add(struct cpu_affinity_qos_req *pm_req,
         |     ^
   In file included from kernel/sched/core.c:88:
   In file included from kernel/sched/sched.h:73:
   In file included from include/trace/events/power.h:10:
   include/linux/pm_qos.h:340:5: error: redefinition of 'cpu_latency_qos_affinity_update'
     340 | int cpu_latency_qos_affinity_update(struct cpu_affinity_qos_req *pm_req,
         |     ^
   include/linux/pm_qos.h:340:5: note: previous definition is here
     340 | int cpu_latency_qos_affinity_update(struct cpu_affinity_qos_req *pm_req,
         |     ^
   In file included from kernel/sched/core.c:88:
   In file included from kernel/sched/sched.h:73:
   In file included from include/trace/events/power.h:10:
   include/linux/pm_qos.h:345:5: error: redefinition of 'cpu_latency_qos_affinity_remove'
     345 | int cpu_latency_qos_affinity_remove(struct cpu_affinity_qos_req *pm_req)
         |     ^
   include/linux/pm_qos.h:345:5: note: previous definition is here
     345 | int cpu_latency_qos_affinity_remove(struct cpu_affinity_qos_req *pm_req)
         |     ^
   In file included from kernel/sched/core.c:88:
   In file included from kernel/sched/sched.h:73:
   In file included from include/trace/events/power.h:10:
   include/linux/pm_qos.h:349:5: error: redefinition of 'cpu_latency_qos_affinity_release'
     349 | int cpu_latency_qos_affinity_release(struct list_head *pm_reqs)
         |     ^
   include/linux/pm_qos.h:349:5: note: previous definition is here
     349 | int cpu_latency_qos_affinity_release(struct list_head *pm_reqs)
         |     ^
   In file included from kernel/sched/core.c:88:
   In file included from kernel/sched/sched.h:73:
   In file included from include/trace/events/power.h:10:
   include/linux/pm_qos.h:353:6: error: redefinition of 'wakeup_qos_affinity_idle_cpu'
     353 | void wakeup_qos_affinity_idle_cpu(int cpu) {}
         |      ^
   include/linux/pm_qos.h:353:6: note: previous definition is here
     353 | void wakeup_qos_affinity_idle_cpu(int cpu) {}
         |      ^
   kernel/sched/core.c:7686:12: warning: array index -1 is before the beginning of the array [-Warray-bounds]
    7686 |                                        preempt_modes[preempt_dynamic_mode] : "undef",
         |                                        ^             ~~~~~~~~~~~~~~~~~~~~
   kernel/sched/core.c:7661:1: note: array 'preempt_modes' declared here
    7661 | const char *preempt_modes[] = {
         | ^
   6 warnings and 5 errors generated.


vim +/cpu_latency_qos_affinity_add +335 include/linux/pm_qos.h

   325	
   326	#if defined(CONFIG_CPU_IDLE) && defined(CONFIG_PM)
   327	int cpu_latency_qos_affinity_add(struct cpu_affinity_qos_req *pm_req,
   328				const cpumask_t *affinity_mask, s32 latency_value);
   329	int cpu_latency_qos_affinity_update(struct cpu_affinity_qos_req *pm_req,
   330				s32 new_value);
   331	int cpu_latency_qos_affinity_remove(struct cpu_affinity_qos_req *pm_req);
   332	int cpu_latency_qos_affinity_release(struct list_head *pm_reqs);
   333	void wakeup_qos_affinity_idle_cpu(int cpu);
   334	#else
 > 335	int cpu_latency_qos_affinity_add(struct cpu_affinity_qos_req *pm_req,
   336				const cpumask_t *affinity_mask, s32 latency_value)
   337	{
   338		return 0;
   339	}
 > 340	int cpu_latency_qos_affinity_update(struct cpu_affinity_qos_req *pm_req,
   341				s32 new_value)
   342	{
   343		return 0;
   344	}
 > 345	int cpu_latency_qos_affinity_remove(struct cpu_affinity_qos_req *pm_req)
   346	{
   347		return 0;
   348	}
 > 349	int cpu_latency_qos_affinity_release(struct list_head *pm_reqs)
   350	{
   351		return 0;
   352	}
 > 353	void wakeup_qos_affinity_idle_cpu(int cpu) {}
diff mbox series

Patch

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)