Skip to contentSkip to conversation

cgroup/cpuset: Extend kthread_is_per_cpu() check to all PF_NO_SETAFFINITY tasks

PATCH Applied Waiman Long 4 participants 6 messages May 8, 2025 – May 9, 2025

Subsystems (2)
lore
Share & tools
lore

Waiman Long

author maintainer
#0 lore raw

PATCH v2 1/1 cgroup/cpuset: Extend kthread_is_per_cpu() check to all PF_NO_SETAFFINITY tasks

Commit ec5fbdfb99d1 ("cgroup/cpuset: Enable update_tasks_cpumask() on top_cpuset") enabled us to pull CPUs dedicated to child partitions from tasks in top_cpuset by ignoring per cpu kthreads. However, there can be other kthreads that are not per cpu but have PF_NO_SETAFFINITY flag set to indicate that we shouldn't mess with their CPU affinity. For other kthreads, their affinity will be changed to skip CPUs dedicated to child partitions whether it is an isolating or a scheduling one.

As all the per cpu kthreads have PF_NO_SETAFFINITY set, the PF_NO_SETAFFINITY tasks are essentially a superset of per cpu kthreads. Fix this issue by dropping the kthread_is_per_cpu() check and checking the PF_NO_SETAFFINITY flag instead.

Fixes ec5fbdfb99d1 ("cgroup/cpuset: Enable update_tasks_cpumask() on top_cpuset")
---
 kernel/cgroup/cpuset.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/cgroup/cpuset.c +4 -2
index d0143b3dce47..967603300ee3 100644
@@ -1130,9 +1130,11 @@ void cpuset_update_tasks_cpumask(struct cpuset *cs, struct cpumask *new_cpus)
 
 		if (top_cs) {
 			/*
-			 * Percpu kthreads in top_cpuset are ignored
+			 * PF_NO_SETAFFINITY tasks are ignored.
+			 * All per cpu kthreads should have PF_NO_SETAFFINITY
+			 * flag set, see kthread_set_per_cpu().
 			 */
-			if (kthread_is_per_cpu(task))
+			if (task->flags & PF_NO_SETAFFINITY)
 				continue;
 			cpumask_andnot(new_cpus, possible_mask, subpartitions_cpus);
 		} else {
Signature · 1 line
2.49.0
Acked-by on v2 1/1
#1 lore raw
Quoted 26 lines · Waiman Long

Commit ec5fbdfb99d1 ("cgroup/cpuset: Enable update_tasks_cpumask() on top_cpuset") enabled us to pull CPUs dedicated to child partitions from tasks in top_cpuset by ignoring per cpu kthreads. However, there can be other kthreads that are not per cpu but have PF_NO_SETAFFINITY flag set to indicate that we shouldn't mess with their CPU affinity. For other kthreads, their affinity will be changed to skip CPUs dedicated to child partitions whether it is an isolating or a scheduling one.

As all the per cpu kthreads have PF_NO_SETAFFINITY set, the PF_NO_SETAFFINITY tasks are essentially a superset of per cpu kthreads. Fix this issue by dropping the kthread_is_per_cpu() check and checking the PF_NO_SETAFFINITY flag instead.

Fixes ec5fbdfb99d1 ("cgroup/cpuset: Enable update_tasks_cpumask() on top_cpuset")
---
 kernel/cgroup/cpuset.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/cgroup/cpuset.c +0 -0
index d0143b3dce47..967603300ee3 100644
@@ -1130,9 +1130,11 @@ void cpuset_update_tasks_cpumask(struct cpuset *cs, struct cpumask *new_cpus)
 
 		if (top_cs) {

kernel/cgroup/cpuset.c void cpuset_update_tasks_cpumask(struct cpuset *cs, struct cpumask *new_cpus) · line 1139

Quoted 10 lines
kernel/cgroup/cpuset.c +4 -2
 			/*
-			 * Percpu kthreads in top_cpuset are ignored
+			 * PF_NO_SETAFFINITY tasks are ignored.
+			 * All per cpu kthreads should have PF_NO_SETAFFINITY
+			 * flag set, see kthread_set_per_cpu().
 			 */
-			if (kthread_is_per_cpu(task))
+			if (task->flags & PF_NO_SETAFFINITY)
 				continue;
 			cpumask_andnot(new_cpus, possible_mask, subpartitions_cpus);

But this makes me realize I overlooked that when I introduced the unbound kthreads centralized affinity.

cpuset_update_tasks_cpumask() seem to blindly affine to subpartitions_cpus while unbound kthreads might have their preferences (per-nodes or random cpumasks).

So I need to make that pass through kthread API.

It seems that subpartition_cpus doesn't contain nohz_full= CPUs. But it excludes isolcpus=. And it's usually sane to assume that nohz_full= CPUs are isolated.

I think I can just rename update_unbound_workqueue_cpumask() to update_unbound_kthreads_cpumask() and then handle unbound kthreads from there along with workqueues. And then completely ignore kthreads from cpuset_update_tasks_cpumask().

Let me think about it (but feel free to apply the current patch meanwhile).

Thanks.

Signature · 2 lines
Frederic Weisbecker
SUSE Labs
#2 lore raw
Quoted 39 lines · Frederic Weisbecker
Quoted 26 lines · Waiman Long

Commit ec5fbdfb99d1 ("cgroup/cpuset: Enable update_tasks_cpumask() on top_cpuset") enabled us to pull CPUs dedicated to child partitions from tasks in top_cpuset by ignoring per cpu kthreads. However, there can be other kthreads that are not per cpu but have PF_NO_SETAFFINITY flag set to indicate that we shouldn't mess with their CPU affinity. For other kthreads, their affinity will be changed to skip CPUs dedicated to child partitions whether it is an isolating or a scheduling one.

As all the per cpu kthreads have PF_NO_SETAFFINITY set, the PF_NO_SETAFFINITY tasks are essentially a superset of per cpu kthreads. Fix this issue by dropping the kthread_is_per_cpu() check and checking the PF_NO_SETAFFINITY flag instead.

Fixes ec5fbdfb99d1 ("cgroup/cpuset: Enable update_tasks_cpumask() on top_cpuset")
---
  kernel/cgroup/cpuset.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
kernel/cgroup/cpuset.c +0 -0
index d0143b3dce47..967603300ee3 100644
@@ -1130,9 +1130,11 @@ void cpuset_update_tasks_cpumask(struct cpuset *cs, struct cpumask *new_cpus)
 
  		if (top_cs) {

kernel/cgroup/cpuset.c void cpuset_update_tasks_cpumask(struct cpuset *cs, struct cpumask *new_cpus) · line 1139

Quoted 10 lines
kernel/cgroup/cpuset.c +4 -2
  			/*
-			 * Percpu kthreads in top_cpuset are ignored
+			 * PF_NO_SETAFFINITY tasks are ignored.
+			 * All per cpu kthreads should have PF_NO_SETAFFINITY
+			 * flag set, see kthread_set_per_cpu().
  			 */
-			if (kthread_is_per_cpu(task))
+			if (task->flags & PF_NO_SETAFFINITY)
  				continue;
  			cpumask_andnot(new_cpus, possible_mask, subpartitions_cpus);
Quoted 7 lines

But this makes me realize I overlooked that when I introduced the unbound kthreads centralized affinity.

cpuset_update_tasks_cpumask() seem to blindly affine to subpartitions_cpus while unbound kthreads might have their preferences (per-nodes or random cpumasks).

So I need to make that pass through kthread API.

AFAIU, the kthread_bind_mask() or the kthread_bin_cpu() functions will set PF_NO_SETAFFINITY.

Quoted 4 lines

It seems that subpartition_cpus doesn't contain nohz_full= CPUs. But it excludes isolcpus=. And it's usually sane to assume that nohz_full= CPUs are isolated.

Most users that want isolated CPUs will set both isolcpus and nohz_full to the same set of CPUs. I do see that RH OpenShift can set nohz_full for a collection of CPUs that may be dynamically isolated later on via cpuset partition.

Quoted 5 lines

I think I can just rename update_unbound_workqueue_cpumask() to update_unbound_kthreads_cpumask() and then handle unbound kthreads from there along with workqueues. And then completely ignore kthreads from cpuset_update_tasks_cpumask().

I guess we can do that. Right now, update_unbound_workqueue_cpumask() is only called to excluded isolated CPUs. The cpuset_update_tasks_cpumasks() will updated affinity for both isolated and scheduling partitions. I agree that there is code duplication here. To suit Xi Wang use case, we may have to add a sysctl parameter, for instance, to decide if we have to update unbound kthreads in the scheduling partition case.

Cheers,
Longman

Quoted 4 lines

Let me think about it (but feel free to apply the current patch meanwhile).

Thanks.

maintainer
#3 lore raw
In reply to Frederic Weisbecker

Hello,

On Fri, May 09, 2025 at 03:18:17PM +0200, Frederic Weisbecker wrote:
...

Quoted 7 lines

But this makes me realize I overlooked that when I introduced the unbound kthreads centralized affinity.

cpuset_update_tasks_cpumask() seem to blindly affine to subpartitions_cpus while unbound kthreads might have their preferences (per-nodes or random cpumasks).

So I need to make that pass through kthread API.

I wonder whether it'd be cleaner if all kthread affinity restrictions go through housekeeping instead of cpuset modifying the cpumasks directly so that housekeeping keeps track of where different classes of kthreads can run and tell e.g. workqueue what to do.

Thanks.

Signature · 1 line
tejun
#4 lore raw
Quoted 10 lines · Tejun Heo

Hello,

On Fri, May 09, 2025 at 03:18:17PM +0200, Frederic Weisbecker wrote:
...

Quoted 6 lines

But this makes me realize I overlooked that when I introduced the unbound kthreads centralized affinity.

cpuset_update_tasks_cpumask() seem to blindly affine to subpartitions_cpus while unbound kthreads might have their preferences (per-nodes or random cpumasks).

Quoted 6 lines
Quoted 1 line

So I need to make that pass through kthread API.

I wonder whether it'd be cleaner if all kthread affinity restrictions go through housekeeping instead of cpuset modifying the cpumasks directly so that housekeeping keeps track of where different classes of kthreads can run and tell e.g. workqueue what to do.

Good suggestion. "isolated_cpus" should indeed be handled by housekeeping itself. More precisely housekeeping_cpu(HK_TYPE_DOMAIN) should be updated through some housekeeping_update() function to union the boot 'isolcpus=' and the isolated mask of cpusets partition. Waiman tried that at some point. This will require some synchronization against the readers of HK_TYPE_DOMAIN.

It's beyond the scope of the kthreads affinity issue but yes that's all planned within the cpusets integration of nohz_full.

Thanks.

Quoted 5 lines

Thanks.

Signature · 1 line
tejun
Signature · 2 lines
Frederic Weisbecker
SUSE Labs
maintainer Applied
#5 lore raw
In reply to Waiman Long
Quoted 8 lines · Waiman Long

Commit ec5fbdfb99d1 ("cgroup/cpuset: Enable update_tasks_cpumask() on top_cpuset") enabled us to pull CPUs dedicated to child partitions from tasks in top_cpuset by ignoring per cpu kthreads. However, there can be other kthreads that are not per cpu but have PF_NO_SETAFFINITY flag set to indicate that we shouldn't mess with their CPU affinity. For other kthreads, their affinity will be changed to skip CPUs dedicated to child partitions whether it is an isolating or a scheduling one.

Quoted 7 lines

As all the per cpu kthreads have PF_NO_SETAFFINITY set, the PF_NO_SETAFFINITY tasks are essentially a superset of per cpu kthreads. Fix this issue by dropping the kthread_is_per_cpu() check and checking the PF_NO_SETAFFINITY flag instead.

Fixes ec5fbdfb99d1 ("cgroup/cpuset: Enable update_tasks_cpumask() on top_cpuset")

Applied to cgroup/for-6.15-fixes.

Thanks.

Signature · 1 line
tejun