Skip to contentSkip to conversation

sched: cgroup: Move task_can_attach() to cpuset.c

PATCH In review Steven Rostedt 7 participants 10 messages Oct 3, 2025 – Oct 6, 2025

Subsystems (3)
lore
Share & tools
lore

Steven Rostedt

author reviewer
#0 lore raw

PATCH 1/1 sched: cgroup: Move task_can_attach() to cpuset.c

From: Steven Rostedt <[email protected]>

At our monthly stable meeting, we were talking about documenting non static functions and randomly picked a function to look at. That was task_can_attach(). It was then noticed that it's only used by cgroup/cpuset.c and nothing else. It's a simple function that doesn't reference anything unique to sched/core.c, hence there's no reason that function should be there.

Move it to cgroup/cpuset.c as that's the only place it is used. Also make it a static inline as it is so small.

---
 include/linux/sched.h  |  1 -
 kernel/cgroup/cpuset.c | 19 +++++++++++++++++++
 kernel/sched/core.c    | 19 -------------------
 3 files changed, 19 insertions(+), 20 deletions(-)
include/linux/sched.h +0 -1
index e4ce0a76831e..4ee4fa973eda 100644
@@ -1849,7 +1849,6 @@ current_restore_flags(unsigned long orig_flags, unsigned long flags)
 }
 
 extern int cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
-extern int task_can_attach(struct task_struct *p);
 extern int dl_bw_alloc(int cpu, u64 dl_bw);
 extern void dl_bw_free(int cpu, u64 dl_bw);
 
kernel/cgroup/cpuset.c +19 -0
index 27adb04df675..21fe872803e8 100644
@@ -3009,6 +3009,25 @@ static void reset_migrate_dl_data(struct cpuset *cs)
 	cs->sum_migrate_dl_bw = 0;
 }
 
+static inline int task_can_attach(struct task_struct *p)
+{
+	int ret = 0;
+
+	/*
+	 * Kthreads which disallow setaffinity shouldn't be moved
+	 * to a new cpuset; we don't want to change their CPU
+	 * affinity and isolating such threads by their set of
+	 * allowed nodes is unnecessary.  Thus, cpusets are not
+	 * applicable for such threads.  This prevents checking for
+	 * success of set_cpus_allowed_ptr() on all attached tasks
+	 * before cpus_mask may be changed.
+	 */
+	if (p->flags & PF_NO_SETAFFINITY)
+		ret = -EINVAL;
+
+	return ret;
+}
+
 /* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */
 static int cpuset_can_attach(struct cgroup_taskset *tset)
 {
kernel/sched/core.c +0 -19
index ccba6fc3c3fe..a195c4b25475 100644
@@ -8070,25 +8070,6 @@ int cpuset_cpumask_can_shrink(const struct cpumask *cur,
 	return ret;
 }
 
-int task_can_attach(struct task_struct *p)
-{
-	int ret = 0;
-
-	/*
-	 * Kthreads which disallow setaffinity shouldn't be moved
-	 * to a new cpuset; we don't want to change their CPU
-	 * affinity and isolating such threads by their set of
-	 * allowed nodes is unnecessary.  Thus, cpusets are not
-	 * applicable for such threads.  This prevents checking for
-	 * success of set_cpus_allowed_ptr() on all attached tasks
-	 * before cpus_mask may be changed.
-	 */
-	if (p->flags & PF_NO_SETAFFINITY)
-		ret = -EINVAL;
-
-	return ret;
-}
-
 bool sched_smp_initialized __read_mostly;
 
 #ifdef CONFIG_NUMA_BALANCING
Signature · 1 line
2.50.1
#1 lore raw

Hi Steven,

Quoted 9 lines · Steven Rostedt

From: Steven Rostedt <[email protected]>

At our monthly stable meeting, we were talking about documenting non static functions and randomly picked a function to look at. That was task_can_attach(). It was then noticed that it's only used by cgroup/cpuset.c and nothing else. It's a simple function that doesn't reference anything unique to sched/core.c, hence there's no reason that function should be there.

Quoted 4 lines

Move it to cgroup/cpuset.c as that's the only place it is used. Also make it a static inline as it is so small.

Nice cleanup. :)

Apparently it became that small with commit 2ef269ef1ac00 ("cgroup/cpuset:
Free DL BW in case can_attach() fails"), maybe we can mention that in the commit message?

Quoted 33 lines
---
 include/linux/sched.h  |  1 -
 kernel/cgroup/cpuset.c | 19 +++++++++++++++++++
 kernel/sched/core.c    | 19 -------------------
 3 files changed, 19 insertions(+), 20 deletions(-)
include/linux/sched.h +0 -1
index e4ce0a76831e..4ee4fa973eda 100644
@@ -1849,7 +1849,6 @@ current_restore_flags(unsigned long orig_flags, unsigned long flags)
 }
 
 extern int cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
-extern int task_can_attach(struct task_struct *p);
 extern int dl_bw_alloc(int cpu, u64 dl_bw);
 extern void dl_bw_free(int cpu, u64 dl_bw);
 
kernel/cgroup/cpuset.c +7 -0
index 27adb04df675..21fe872803e8 100644
@@ -3009,6 +3009,25 @@ static void reset_migrate_dl_data(struct cpuset *cs)
 	cs->sum_migrate_dl_bw = 0;
 }
 
+static inline int task_can_attach(struct task_struct *p)
+{
+	int ret = 0;
+
+	/*
+	 * Kthreads which disallow setaffinity shouldn't be moved
+	 * to a new cpuset; we don't want to change their CPU

kernel/cgroup/cpuset.c static void reset_migrate_dl_data(struct cpuset *cs) · line 3028

Quoted 10 lines
kernel/cgroup/cpuset.c +10 -0
+	 * affinity and isolating such threads by their set of
+	 * allowed nodes is unnecessary.  Thus, cpusets are not
+	 * applicable for such threads.  This prevents checking for
+	 * success of set_cpus_allowed_ptr() on all attached tasks
+	 * before cpus_mask may be changed.
+	 */
+	if (p->flags & PF_NO_SETAFFINITY)
+		ret = -EINVAL;
+
+	return ret;

As we're cleaning up, we could just return -EINVAL and 0 directly and get rid of that ret variable.

Quoted 38 lines
kernel/cgroup/cpuset.c +2 -0
+}
+
 /* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */
 static int cpuset_can_attach(struct cgroup_taskset *tset)
 {
kernel/sched/core.c +0 -19
index ccba6fc3c3fe..a195c4b25475 100644
@@ -8070,25 +8070,6 @@ int cpuset_cpumask_can_shrink(const struct cpumask *cur,
 	return ret;
 }
 
-int task_can_attach(struct task_struct *p)
-{
-	int ret = 0;
-
-	/*
-	 * Kthreads which disallow setaffinity shouldn't be moved
-	 * to a new cpuset; we don't want to change their CPU
-	 * affinity and isolating such threads by their set of
-	 * allowed nodes is unnecessary.  Thus, cpusets are not
-	 * applicable for such threads.  This prevents checking for
-	 * success of set_cpus_allowed_ptr() on all attached tasks
-	 * before cpus_mask may be changed.
-	 */
-	if (p->flags & PF_NO_SETAFFINITY)
-		ret = -EINVAL;
-
-	return ret;
-}
-
 bool sched_smp_initialized __read_mostly;
 
 #ifdef CONFIG_NUMA_BALANCING
Signature · 1 line
2.50.1

Thanks,
-Andrea

author reviewer
#2 lore raw
Quoted 12 lines · On Fri, 3 Oct 2025 18:36:24 +0200 Andrea Righi <[email protected]> wrote:

Hi Steven,

Quoted 9 lines · Steven Rostedt

From: Steven Rostedt <[email protected]>

At our monthly stable meeting, we were talking about documenting non static functions and randomly picked a function to look at. That was task_can_attach(). It was then noticed that it's only used by cgroup/cpuset.c and nothing else. It's a simple function that doesn't reference anything unique to sched/core.c, hence there's no reason that function should be there.

Quoted 6 lines
Quoted 4 lines

Move it to cgroup/cpuset.c as that's the only place it is used. Also make it a static inline as it is so small.

Nice cleanup. :)

Thanks, since you also were the first to notice it is only used in one spot.

Quoted 4 lines

Apparently it became that small with commit 2ef269ef1ac00 ("cgroup/cpuset:
Free DL BW in case can_attach() fails"), maybe we can mention that in the commit message?

Sure.

Quoted 39 lines
Quoted 39 lines
---
 include/linux/sched.h  |  1 -
 kernel/cgroup/cpuset.c | 19 +++++++++++++++++++
 kernel/sched/core.c    | 19 -------------------
 3 files changed, 19 insertions(+), 20 deletions(-)
include/linux/sched.h +0 -1
index e4ce0a76831e..4ee4fa973eda 100644
@@ -1849,7 +1849,6 @@ current_restore_flags(unsigned long orig_flags, unsigned long flags)
 }
 
 extern int cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
-extern int task_can_attach(struct task_struct *p);
 extern int dl_bw_alloc(int cpu, u64 dl_bw);
 extern void dl_bw_free(int cpu, u64 dl_bw);
 
kernel/cgroup/cpuset.c +12 -0
index 27adb04df675..21fe872803e8 100644
@@ -3009,6 +3009,25 @@ static void reset_migrate_dl_data(struct cpuset *cs)
 	cs->sum_migrate_dl_bw = 0;
 }
 
+static inline int task_can_attach(struct task_struct *p)
+{
+	int ret = 0;
+
+	/*
+	 * Kthreads which disallow setaffinity shouldn't be moved
+	 * to a new cpuset; we don't want to change their CPU
+	 * affinity and isolating such threads by their set of
+	 * allowed nodes is unnecessary.  Thus, cpusets are not
+	 * applicable for such threads.  This prevents checking for
+	 * success of set_cpus_allowed_ptr() on all attached tasks
+	 * before cpus_mask may be changed.
Quoted 8 lines

kernel/cgroup/cpuset.c static void reset_migrate_dl_data(struct cpuset *cs) · line 3028

Quoted 5 lines
kernel/cgroup/cpuset.c +5 -0
+	 */
+	if (p->flags & PF_NO_SETAFFINITY)
+		ret = -EINVAL;
+
+	return ret;  

As we're cleaning up, we could just return -EINVAL and 0 directly and get rid of that ret variable.

That should be a separate patch. Moves should really not do much else. I even wondered about making it a static inline too, but figured that wasn't touching the logic, and the function was going to become static anyway.

-- Steve

Quoted 42 lines
Quoted 39 lines
kernel/cgroup/cpuset.c +2 -0
+}
+
 /* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */
 static int cpuset_can_attach(struct cgroup_taskset *tset)
 {
kernel/sched/core.c +0 -19
index ccba6fc3c3fe..a195c4b25475 100644
@@ -8070,25 +8070,6 @@ int cpuset_cpumask_can_shrink(const struct cpumask *cur,
 	return ret;
 }
 
-int task_can_attach(struct task_struct *p)
-{
-	int ret = 0;
-
-	/*
-	 * Kthreads which disallow setaffinity shouldn't be moved
-	 * to a new cpuset; we don't want to change their CPU
-	 * affinity and isolating such threads by their set of
-	 * allowed nodes is unnecessary.  Thus, cpusets are not
-	 * applicable for such threads.  This prevents checking for
-	 * success of set_cpus_allowed_ptr() on all attached tasks
-	 * before cpus_mask may be changed.
-	 */
-	if (p->flags & PF_NO_SETAFFINITY)
-		ret = -EINVAL;
-
-	return ret;
-}
-
 bool sched_smp_initialized __read_mostly;
 
 #ifdef CONFIG_NUMA_BALANCING
Signature · 1 line
2.50.1

Thanks,
-Andrea

Tested-by
#3 lore raw

On Fri, Oct 03, 2025 at 12:43:00PM -0400, Steven Rostedt wrote:
...

Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
kernel/cgroup/cpuset.c +12 -0
+static inline int task_can_attach(struct task_struct *p)
+{
+	int ret = 0;
+
+	/*
+	 * Kthreads which disallow setaffinity shouldn't be moved
+	 * to a new cpuset; we don't want to change their CPU
+	 * affinity and isolating such threads by their set of
+	 * allowed nodes is unnecessary.  Thus, cpusets are not
+	 * applicable for such threads.  This prevents checking for
+	 * success of set_cpus_allowed_ptr() on all attached tasks
+	 * before cpus_mask may be changed.
Quoted 8 lines

kernel/cgroup/cpuset.c static void reset_migrate_dl_data(struct cpuset *cs) · line 3028

Quoted 5 lines
kernel/cgroup/cpuset.c +5 -0
+	 */
+	if (p->flags & PF_NO_SETAFFINITY)
+		ret = -EINVAL;
+
+	return ret;  

As we're cleaning up, we could just return -EINVAL and 0 directly and get rid of that ret variable.

Quoted 3 lines

That should be a separate patch. Moves should really not do much else. I even wondered about making it a static inline too, but figured that wasn't touching the logic, and the function was going to become static anyway.

Oh yes, good point. In the meantime, FWIW:

Thanks,
-Andrea

Acked-by on v1 1/1
#4 lore raw
In reply to Steven Rostedt
Quoted 81 lines · Steven Rostedt

From: Steven Rostedt <[email protected]>

At our monthly stable meeting, we were talking about documenting non static functions and randomly picked a function to look at. That was task_can_attach(). It was then noticed that it's only used by cgroup/cpuset.c and nothing else. It's a simple function that doesn't reference anything unique to sched/core.c, hence there's no reason that function should be there.

Move it to cgroup/cpuset.c as that's the only place it is used. Also make it a static inline as it is so small.

---
  include/linux/sched.h  |  1 -
  kernel/cgroup/cpuset.c | 19 +++++++++++++++++++
  kernel/sched/core.c    | 19 -------------------
  3 files changed, 19 insertions(+), 20 deletions(-)
include/linux/sched.h +0 -1
index e4ce0a76831e..4ee4fa973eda 100644
@@ -1849,7 +1849,6 @@ current_restore_flags(unsigned long orig_flags, unsigned long flags)
  }
 
  extern int cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
-extern int task_can_attach(struct task_struct *p);
  extern int dl_bw_alloc(int cpu, u64 dl_bw);
  extern void dl_bw_free(int cpu, u64 dl_bw);
 
kernel/cgroup/cpuset.c +19 -0
index 27adb04df675..21fe872803e8 100644
@@ -3009,6 +3009,25 @@ static void reset_migrate_dl_data(struct cpuset *cs)
  	cs->sum_migrate_dl_bw = 0;
  }
 
+static inline int task_can_attach(struct task_struct *p)
+{
+	int ret = 0;
+
+	/*
+	 * Kthreads which disallow setaffinity shouldn't be moved
+	 * to a new cpuset; we don't want to change their CPU
+	 * affinity and isolating such threads by their set of
+	 * allowed nodes is unnecessary.  Thus, cpusets are not
+	 * applicable for such threads.  This prevents checking for
+	 * success of set_cpus_allowed_ptr() on all attached tasks
+	 * before cpus_mask may be changed.
+	 */
+	if (p->flags & PF_NO_SETAFFINITY)
+		ret = -EINVAL;
+
+	return ret;
+}
+
  /* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */
  static int cpuset_can_attach(struct cgroup_taskset *tset)
  {
kernel/sched/core.c +0 -12
index ccba6fc3c3fe..a195c4b25475 100644
@@ -8070,25 +8070,6 @@ int cpuset_cpumask_can_shrink(const struct cpumask *cur,
  	return ret;
  }
 
-int task_can_attach(struct task_struct *p)
-{
-	int ret = 0;
-
-	/*
-	 * Kthreads which disallow setaffinity shouldn't be moved
-	 * to a new cpuset; we don't want to change their CPU
-	 * affinity and isolating such threads by their set of
-	 * allowed nodes is unnecessary.  Thus, cpusets are not
-	 * applicable for such threads.  This prevents checking for
-	 * success of set_cpus_allowed_ptr() on all attached tasks
-	 * before cpus_mask may be changed.

kernel/sched/core.c int cpuset_cpumask_can_shrink(const struct cpumask *cur, · line 8075

Quoted 10 lines
kernel/sched/core.c +0 -7
-	 */
-	if (p->flags & PF_NO_SETAFFINITY)
-		ret = -EINVAL;
-
-	return ret;
-}
-
  bool sched_smp_initialized __read_mostly;
 
  #ifdef CONFIG_NUMA_BALANCING
Tested-by on v1 1/1
#5 lore raw
In reply to Steven Rostedt
Quoted 12 lines · Steven Rostedt

From: Steven Rostedt <[email protected]>

At our monthly stable meeting, we were talking about documenting non static functions and randomly picked a function to look at. That was task_can_attach(). It was then noticed that it's only used by cgroup/cpuset.c and nothing else. It's a simple function that doesn't reference anything unique to sched/core.c, hence there's no reason that function should be there.

Move it to cgroup/cpuset.c as that's the only place it is used. Also make it a static inline as it is so small.

Quoted 6 lines
---
 include/linux/sched.h  |  1 -
 kernel/cgroup/cpuset.c | 19 +++++++++++++++++++
 kernel/sched/core.c    | 19 -------------------
 3 files changed, 19 insertions(+), 20 deletions(-)

That was a fun exercise. :)

Reviewed-by on v1 1/1
#6 lore raw

Re: [External] : [PATCH] sched: cgroup: Move task_can_attach() to cpuset.c

In reply to Steven Rostedt
Quoted 81 lines · Steven Rostedt

From: Steven Rostedt <[email protected]>

At our monthly stable meeting, we were talking about documenting non static functions and randomly picked a function to look at. That was task_can_attach(). It was then noticed that it's only used by cgroup/cpuset.c and nothing else. It's a simple function that doesn't reference anything unique to sched/core.c, hence there's no reason that function should be there.

Move it to cgroup/cpuset.c as that's the only place it is used. Also make it a static inline as it is so small.

---
  include/linux/sched.h  |  1 -
  kernel/cgroup/cpuset.c | 19 +++++++++++++++++++
  kernel/sched/core.c    | 19 -------------------
  3 files changed, 19 insertions(+), 20 deletions(-)
include/linux/sched.h +0 -1
index e4ce0a76831e..4ee4fa973eda 100644
@@ -1849,7 +1849,6 @@ current_restore_flags(unsigned long orig_flags, unsigned long flags)
  }
 
  extern int cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
-extern int task_can_attach(struct task_struct *p);
  extern int dl_bw_alloc(int cpu, u64 dl_bw);
  extern void dl_bw_free(int cpu, u64 dl_bw);
 
kernel/cgroup/cpuset.c +19 -0
index 27adb04df675..21fe872803e8 100644
@@ -3009,6 +3009,25 @@ static void reset_migrate_dl_data(struct cpuset *cs)
  	cs->sum_migrate_dl_bw = 0;
  }
 
+static inline int task_can_attach(struct task_struct *p)
+{
+	int ret = 0;
+
+	/*
+	 * Kthreads which disallow setaffinity shouldn't be moved
+	 * to a new cpuset; we don't want to change their CPU
+	 * affinity and isolating such threads by their set of
+	 * allowed nodes is unnecessary.  Thus, cpusets are not
+	 * applicable for such threads.  This prevents checking for
+	 * success of set_cpus_allowed_ptr() on all attached tasks
+	 * before cpus_mask may be changed.
+	 */
+	if (p->flags & PF_NO_SETAFFINITY)
+		ret = -EINVAL;
+
+	return ret;
+}
+
  /* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */
  static int cpuset_can_attach(struct cgroup_taskset *tset)
  {
kernel/sched/core.c +0 -12
index ccba6fc3c3fe..a195c4b25475 100644
@@ -8070,25 +8070,6 @@ int cpuset_cpumask_can_shrink(const struct cpumask *cur,
  	return ret;
  }
 
-int task_can_attach(struct task_struct *p)
-{
-	int ret = 0;
-
-	/*
-	 * Kthreads which disallow setaffinity shouldn't be moved
-	 * to a new cpuset; we don't want to change their CPU
-	 * affinity and isolating such threads by their set of
-	 * allowed nodes is unnecessary.  Thus, cpusets are not
-	 * applicable for such threads.  This prevents checking for
-	 * success of set_cpus_allowed_ptr() on all attached tasks
-	 * before cpus_mask may be changed.

kernel/sched/core.c int cpuset_cpumask_can_shrink(const struct cpumask *cur, · line 8075

Quoted 10 lines
kernel/sched/core.c +0 -7
-	 */
-	if (p->flags & PF_NO_SETAFFINITY)
-		ret = -EINVAL;
-
-	return ret;
-}
-
  bool sched_smp_initialized __read_mostly;
 
  #ifdef CONFIG_NUMA_BALANCING

Nice beneficial refactoring!

maintainer
#7 lore raw
In reply to Steven Rostedt
Quoted 9 lines · Steven Rostedt

From: Steven Rostedt <[email protected]>

At our monthly stable meeting, we were talking about documenting non static functions and randomly picked a function to look at. That was task_can_attach(). It was then noticed that it's only used by cgroup/cpuset.c and nothing else. It's a simple function that doesn't reference anything unique to sched/core.c, hence there's no reason that function should be there.

Quoted 4 lines

Move it to cgroup/cpuset.c as that's the only place it is used. Also make it a static inline as it is so small.

Peter, if it looks good to you, I can route it through cgroup tree.

Thanks.

Signature · 1 line
tejun
maintainer
#8 lore raw
Quoted 10 lines · Tejun Heo
Quoted 9 lines · Steven Rostedt

From: Steven Rostedt <[email protected]>

At our monthly stable meeting, we were talking about documenting non static functions and randomly picked a function to look at. That was task_can_attach(). It was then noticed that it's only used by cgroup/cpuset.c and nothing else. It's a simple function that doesn't reference anything unique to sched/core.c, hence there's no reason that function should be there.

Quoted 6 lines
Quoted 4 lines

Move it to cgroup/cpuset.c as that's the only place it is used. Also make it a static inline as it is so small.

Peter, if it looks good to you, I can route it through cgroup tree.

Yeah, I suppose so. But there were suggested changes to the Changelog and actual patch so perhaps wait for v2?

maintainer
#9 lore raw
Quoted 14 lines · Peter Zijlstra
Quoted 13 lines · Tejun Heo
Quoted 12 lines · Steven Rostedt

From: Steven Rostedt <[email protected]>

At our monthly stable meeting, we were talking about documenting non static functions and randomly picked a function to look at. That was task_can_attach(). It was then noticed that it's only used by cgroup/cpuset.c and nothing else. It's a simple function that doesn't reference anything unique to sched/core.c, hence there's no reason that function should be there.

Move it to cgroup/cpuset.c as that's the only place it is used. Also make it a static inline as it is so small.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Peter, if it looks good to you, I can route it through cgroup tree.

Yeah, I suppose so. But there were suggested changes to the Changelog and actual patch so perhaps wait for v2?

Yeah, will wait for v2 and then apply it.

Thanks.

Signature · 1 line
tejun