Skip to contentSkip to conversation

sched/core: Show user_cpus_ptr & allow its reset

PATCH New Waiman Long 1 participant 3 messages Oct 29, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover sched/core: Show user_cpus_ptr & allow its reset

v2: Make minor change in patch 2 to show the new status fields will be
empty if user_cpus_ptr not defined.

As the presence of user_cpus_ptr will affect how the CPU affinity of a process will be defined, it makes sense to enable users to see if the user_cpus_ptr of a process is set as well as enabling the clearing of user_cpus_ptr if it is not needed.

Waiman Long (2):
sched/core: Enable full cpumask to clear user cpumask in
sched_setaffinity()
fs/proc: Show the content of task->user_cpus_ptr in /proc/<pid>/status

 Documentation/filesystems/proc.rst |  3 +++
 fs/proc/array.c                    |  9 +++++++++
 kernel/sched/syscalls.c            | 20 ++++++++++++++------
 3 files changed, 26 insertions(+), 6 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH v2 1/2 sched/core: Enable full cpumask to clear user cpumask in sched_setaffinity()

Since commit 8f9ea86fdf99 ("sched: Always preserve the user requested cpumask"), user provided CPU affinity via sched_setaffinity(2) is perserved even if the task is being moved to a different cpuset. However, that affinity is also being inherited by any subsequently created child processes which may not want or be aware of that affinity.

One way to solve this problem is to provide a way to back off from that user provided CPU affinity. This patch implements such a scheme by using a full cpumask (a cpumask with all bits set) to signal the clearing of the user cpumask to follow the default as allowed by the current cpuset. In fact, with a full cpumask in user_cpus_ptr, the task behavior should be the same as with a NULL user_cpus_ptr. This patch just formalizes it without causing any incompatibility and discard an otherwise useless cpumask.

---
 kernel/sched/syscalls.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
kernel/sched/syscalls.c +14 -6
index 77ae87f36e84..d68c7a4ee525 100644
@@ -1229,14 +1229,22 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
 		return retval;
 
 	/*
-	 * With non-SMP configs, user_cpus_ptr/user_mask isn't used and
-	 * alloc_user_cpus_ptr() returns NULL.
+	 * If a full cpumask is passed in, clear user_cpus_ptr and reset the
+	 * current cpu affinity to the default for the current cpuset.
 	 */
-	user_mask = alloc_user_cpus_ptr(NUMA_NO_NODE);
-	if (user_mask) {
-		cpumask_copy(user_mask, in_mask);
+	if (cpumask_full(in_mask)) {
+		user_mask = NULL;
 	} else {
-		return -ENOMEM;
+		/*
+		 * With non-SMP configs, user_cpus_ptr/user_mask isn't used and
+		 * alloc_user_cpus_ptr() returns NULL.
+		 */
+		user_mask = alloc_user_cpus_ptr(NUMA_NO_NODE);
+		if (user_mask) {
+			cpumask_copy(user_mask, in_mask);
+		} else {
+			return -ENOMEM;
+		}
 	}
 
 	ac = (struct affinity_context){
Signature · 1 line
2.51.0
#2 lore raw

PATCH v2 2/2 fs/proc: Show the content of task->user_cpus_ptr in /proc/<pid>/status

In reply to Waiman Long

The task->user_cpus_ptr was introduced by commit b90ca8badbd1 ("sched:
Introduce task_struct::user_cpus_ptr to track requested affinity") to keep track of user-requested CPU affinity. With commit da019032819a ("sched: Enforce user requested affinity"), user_cpus_ptr will persistently affect how cpus_allowed will be set.

As child processes inherit the user_cpus_ptr setting from its parent, they may not know that their user_cpus_ptr may be set changing their CPU affinity in an unexpected way. Currently there is not an easy way to figure out if their user_cpus_ptr is set even if they have never called sched_setaffinity(2) themselves. So it makes sense to enable users to see the presence of a previously set user_cpus_ptr so they can do something about it without getting a surprise.

Add new "Cpus_user" and "Cpus_user_list" fields to /proc/<pid>/status output via task_cpus_allowed() as the presence of user_cpus_ptr will affect their cpus_allowed cpumask. The new fields will be empty if user_cpus_ptr isn't set.

---
 Documentation/filesystems/proc.rst | 3 +++
 fs/proc/array.c                    | 9 +++++++++
 2 files changed, 12 insertions(+)
Documentation/filesystems/proc.rst +3 -0
index 0b86a8022fa1..4317c79a530f 100644
@@ -291,6 +291,9 @@ It's slow but very precise.
  SpeculationIndirectBranch   indirect branch speculation mode
  Cpus_allowed                mask of CPUs on which this process may run
  Cpus_allowed_list           Same as previous, but in "list format"
+ Cpus_user                   mask of user requested CPUs from
+                             sched_setaffinity(2), empty if not defined
+ Cpus_user_list              Same as previous, but in "list format"
  Mems_allowed                mask of memory nodes allowed to this process
  Mems_allowed_list           Same as previous, but in "list format"
  voluntary_ctxt_switches     number of voluntary context switches
fs/proc/array.c +9 -0
index 2ae63189091e..17e700556daa 100644
@@ -403,10 +403,19 @@ static inline void task_context_switch_counts(struct seq_file *m,
 
 static void task_cpus_allowed(struct seq_file *m, struct task_struct *task)
 {
+	cpumask_t *user_cpus = task->user_cpus_ptr;
+
 	seq_printf(m, "Cpus_allowed:\t%*pb\n",
 		   cpumask_pr_args(&task->cpus_mask));
 	seq_printf(m, "Cpus_allowed_list:\t%*pbl\n",
 		   cpumask_pr_args(&task->cpus_mask));
+
+	if (user_cpus) {
+		seq_printf(m, "Cpus_user:\t%*pb\n", cpumask_pr_args(user_cpus));
+		seq_printf(m, "Cpus_user_list:\t%*pbl\n", cpumask_pr_args(user_cpus));
+	} else {
+		seq_puts(m, "Cpus_user:\nCpus_user_list:\n");
+	}
 }
 
 static inline void task_core_dumping(struct seq_file *m, struct task_struct *task)
Signature · 1 line
2.51.0