Skip to contentSkip to conversation

refactor sqthread cpu binding logic

PATCH In review Hao Xu 2 participants 4 messages Sep 1, 2021 1 message not in the archive

Subsystems (4)
lore
Share & tools
lore

Hao Xu

author
#0 lore raw

v3 cover refactor sqthread cpu binding logic

This patchset is to enhance sqthread cpu binding logic, we didn't consider cgroup setting before. In container environment, theoretically sqthread is in its container's task group, it shouldn't occupy cpu out of its container. Otherwise it may cause some problems, for example:
resource balancer may controll cpu resource allocation by a container's current cpu usage, here sqthread should be counted in.

v1-->v2
- add helper to do cpu in current-cpuset test

v2-->v3
- split it to 2 patches, first as prep one, second as comsumer
- remove warnning, since cpuset may change anytime, we cannot catch all cases, so make the behaviour consistent.

Hao Xu (2):
cpuset: add a helper to check if cpu in cpuset of current task
io_uring: consider cgroup setting when binding sqpoll cpu

 fs/io_uring.c          | 19 ++++++++++++++-----
 include/linux/cpuset.h |  7 +++++++
 kernel/cgroup/cpuset.c | 11 +++++++++++
 3 files changed, 32 insertions(+), 5 deletions(-)
Signature · 1 line
2.24.4
#1 lore raw

PATCH 1/2 cpuset: add a helper to check if cpu in cpuset of current task

Check if a cpu is in the cpuset of current task, not guaranteed that cpu is online.

---
 include/linux/cpuset.h |  7 +++++++
 kernel/cgroup/cpuset.c | 11 +++++++++++
 2 files changed, 18 insertions(+)
include/linux/cpuset.h +7 -0
index 04c20de66afc..fad77c91bc1f 100644
@@ -116,6 +116,8 @@ static inline int cpuset_do_slab_mem_spread(void)
 
 extern bool current_cpuset_is_being_rebound(void);
 
+extern bool test_cpu_in_current_cpuset(int cpu);
+
 extern void rebuild_sched_domains(void);
 
 extern void cpuset_print_current_mems_allowed(void);
@@ -257,6 +259,11 @@ static inline bool current_cpuset_is_being_rebound(void)
 	return false;
 }
 
+static inline bool test_cpu_in_current_cpuset(int cpu)
+{
+	return false;
+}
+
 static inline void rebuild_sched_domains(void)
 {
 	partition_sched_domains(1, NULL, NULL);
kernel/cgroup/cpuset.c +11 -0
index adb5190c4429..a63c27e9430e 100644
@@ -1849,6 +1849,17 @@ bool current_cpuset_is_being_rebound(void)
 	return ret;
 }
 
+bool test_cpu_in_current_cpuset(int cpu)
+{
+	bool ret;
+
+	rcu_read_lock();
+	ret = cpumask_test_cpu(cpu, task_cs(current)->effective_cpus);
+	rcu_read_unlock();
+
+	return ret;
+}
+
 static int update_relax_domain_level(struct cpuset *cs, s64 val)
 {
 #ifdef CONFIG_SMP
Signature · 1 line
2.24.4

A message that is not in the archive (sent off-list or not yet received): 20210901101833.69535-1-haoxu-KPsoFbNs7GizrGE5bRqYAgC/[email protected]

author
#3 lore raw

PATCH v3 2/2 io_uring: consider cgroup setting when binding sqpoll cpu

Since sqthread is userspace like thread now, it should respect cgroup setting, thus we should consider current allowed cpuset when doing cpu binding for sqthread.

---
 fs/io_uring.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/io_uring.c +14 -5
index 7cc458e0b636..414dfedf79a7 100644
@@ -79,6 +79,7 @@
 #include <linux/pagemap.h>
 #include <linux/io_uring.h>
 #include <linux/tracehook.h>
+#include <linux/cpuset.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/io_uring.h>
@@ -7102,6 +7103,14 @@ static bool io_sqd_handle_event(struct io_sq_data *sqd)
 	return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
 }
 
+static inline int io_sq_bind_cpu(int cpu)
+{
+	if (test_cpu_in_current_cpuset(cpu))
+		set_cpus_allowed_ptr(current, cpumask_of(cpu));
+
+	return 0;
+}
+
 static int io_sq_thread(void *data)
 {
 	struct io_sq_data *sqd = data;
@@ -7112,11 +7121,9 @@ static int io_sq_thread(void *data)
 
 	snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
 	set_task_comm(current, buf);
-
 	if (sqd->sq_cpu != -1)
-		set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
-	else
-		set_cpus_allowed_ptr(current, cpu_online_mask);
+		io_sq_bind_cpu(sqd->sq_cpu);
+
 	current->flags |= PF_NO_SETAFFINITY;
 
 	mutex_lock(&sqd->lock);
@@ -8310,8 +8317,10 @@ static int io_sq_offload_create(struct io_ring_ctx *ctx,
 			int cpu = p->sq_thread_cpu;
 
 			ret = -EINVAL;
-			if (cpu >= nr_cpu_ids || !cpu_online(cpu))
+			if (cpu >= nr_cpu_ids || !cpu_online(cpu) ||
+			    !test_cpu_in_current_cpuset(cpu))
 				goto err_sqpoll;
+
 			sqd->sq_cpu = cpu;
 		} else {
 			sqd->sq_cpu = -1;
Signature · 1 line
2.24.4
#4 lore raw

Re: [PATCH 2/2] io_uring: consider cgroup setting when binding sqpoll cpu

Quoted 22 lines · Hao Xu

Since sqthread is userspace like thread now, it should respect cgroup setting, thus we should consider current allowed cpuset when doing cpu binding for sqthread.

---
  fs/io_uring.c | 19 ++++++++++++++-----
  1 file changed, 14 insertions(+), 5 deletions(-)
fs/io_uring.c +1 -0
index 7cc458e0b636..414dfedf79a7 100644
@@ -79,6 +79,7 @@
  #include <linux/pagemap.h>
  #include <linux/io_uring.h>
  #include <linux/tracehook.h>
+#include <linux/cpuset.h>
 
  #define CREATE_TRACE_POINTS
  #include <trace/events/io_uring.h>

fs/io_uring.c static bool io_sqd_handle_event(struct io_sq_data *sqd) · line 7111

Quoted 9 lines
fs/io_uring.c +6 -0
@@ -7102,6 +7103,14 @@ static bool io_sqd_handle_event(struct io_sq_data *sqd)
  	return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
  }
 
+static inline int io_sq_bind_cpu(int cpu)
+{
+	if (test_cpu_in_current_cpuset(cpu))
+		set_cpus_allowed_ptr(current, cpumask_of(cpu));
+
+	return 0;

Ah, no need to return value anymore, even no need to have this function here. I'll resend a new version.

Quoted 32 lines
fs/io_uring.c +7 -5
+}
+
  static int io_sq_thread(void *data)
  {
  	struct io_sq_data *sqd = data;
@@ -7112,11 +7121,9 @@ static int io_sq_thread(void *data)
 
  	snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
  	set_task_comm(current, buf);
-
  	if (sqd->sq_cpu != -1)
-		set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
-	else
-		set_cpus_allowed_ptr(current, cpu_online_mask);
+		io_sq_bind_cpu(sqd->sq_cpu);
+
  	current->flags |= PF_NO_SETAFFINITY;
 
  	mutex_lock(&sqd->lock);
@@ -8310,8 +8317,10 @@ static int io_sq_offload_create(struct io_ring_ctx *ctx,
  			int cpu = p->sq_thread_cpu;
 
  			ret = -EINVAL;
-			if (cpu >= nr_cpu_ids || !cpu_online(cpu))
+			if (cpu >= nr_cpu_ids || !cpu_online(cpu) ||
+			    !test_cpu_in_current_cpuset(cpu))
  				goto err_sqpoll;
+
  			sqd->sq_cpu = cpu;
  		} else {
  			sqd->sq_cpu = -1;