Skip to contentSkip to conversation

sched, cgroup/cpuset: Keep user set cpus affinity

PATCH Superseded Waiman Long 3 participants 7 messages Aug 1, 2022 – Aug 9, 2022

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Waiman Long

author maintainer
#0 lore raw

v2 cover sched, cgroup/cpuset: Keep user set cpus affinity

v2:
- Rework the v1 patch by extending the semantics of user_cpus_ptr to store user set cpus affinity and keeping to it as much as possible.

The user_cpus_ptr field is added by commit b90ca8badbd1 ("sched:
Introduce task_struct::user_cpus_ptr to track requested affinity") which uses it narrowly to allow keeping cpus affinity intact with asymmetric cpu setup.

This patchset extends user_cpus_ptr to store user set cpus affinity via sched_setaffinity() API. With that information avaiable, it will enable cpuset to keep cpus afinity as close to what the user wants as possible within the cpu list constraint of the current cpuset.

Waiman Long (2):
sched: Use user_cpus_ptr for saving user provided cpumask in
sched_setaffinity()
cgroup/cpuset: Keep user set cpus affinity

 include/linux/sched.h  |  1 +
 kernel/cgroup/cpuset.c | 24 ++++++++++++--
 kernel/sched/core.c    | 71 ++++++++++++++++++++++++++++++------------
 kernel/sched/sched.h   |  1 -
 4 files changed, 74 insertions(+), 23 deletions(-)
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH v2 1/2 sched: Use user_cpus_ptr for saving user provided cpumask in sched_setaffinity()

The user_cpus_ptr field is added by commit b90ca8badbd1 ("sched:
Introduce task_struct::user_cpus_ptr to track requested affinity"). It is currently used only by arm64 arch due to possible asymmetric cpu setup. This patch extends its usage to save user provided cpumask when sched_setaffinity() is called for all arches.

To preserve the existing arm64 use case, a new cpus_affinity_set flag is added to differentiate if user_cpus_ptr is set up by sched_setaffinity() or by force_compatible_cpus_allowed_ptr(). user_cpus_ptr
set by sched_setaffinity() has priority and won't be overwritten by force_compatible_cpus_allowed_ptr() or relax_compatible_cpus_allowed_ptr().

As a call to sched_setaffinity() will no longer clear user_cpus_ptr but set it instead, the SCA_USER flag is no longer necessary and can be removed.

---
 include/linux/sched.h |  1 +
 kernel/sched/core.c   | 71 +++++++++++++++++++++++++++++++------------
 kernel/sched/sched.h  |  1 -
 3 files changed, 52 insertions(+), 21 deletions(-)
include/linux/sched.h +1 -0
index c46f3a63b758..60ae022fa842 100644
@@ -815,6 +815,7 @@ struct task_struct {
 
 	unsigned int			policy;
 	int				nr_cpus_allowed;
+	int				cpus_affinity_set;
 	const cpumask_t			*cpus_ptr;
 	cpumask_t			*user_cpus_ptr;
 	cpumask_t			cpus_mask;
kernel/sched/core.c +51 -20

144 lines not on this page · raw message

kernel/sched/sched.h +0 -1
index 47b89a0fc6e5..c9e9731a1a17 100644
@@ -2242,7 +2242,6 @@ extern struct task_struct *pick_next_task_idle(struct rq *rq);
 #define SCA_CHECK		0x01
 #define SCA_MIGRATE_DISABLE	0x02
 #define SCA_MIGRATE_ENABLE	0x04
-#define SCA_USER		0x08
 
 #ifdef CONFIG_SMP
 
Signature · 1 line
2.31.1
author maintainer
#4 lore raw

PATCH v2 2/2 cgroup/cpuset: Keep user set cpus affinity

In reply to Waiman Long

It was found that any change to the current cpuset hierarchy may reset the cpumask of the tasks in the affected cpusets to the default cpuset value even if those tasks have cpus affinity explicitly set by the users before. That is especially easy to trigger under a cgroup v2 environment where writing "+cpuset" to the root cgroup's cgroup.subtree_control file will reset the cpus affinity of all the processes in the system.

That is problematic in a nohz_full environment where the tasks running in the nohz_full CPUs usually have their cpus affinity explicitly set and will behave incorrectly if cpus affinity changes.

Fix this problem by looking at user_cpus_ptr which will be set if cpus affinity have been explicitly set before and use it to restrcit the given cpumask unless there is no overlap. In that case, it will fallback to the given one.

With that change in place, it was verified that tasks that have its cpus affinity explicitly set will not be affected by changes made to the v2 cgroup.subtree_control files.

---
 kernel/cgroup/cpuset.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
kernel/cgroup/cpuset.c +22 -2
index 71a418858a5e..2e3af93bed03 100644
@@ -704,6 +704,26 @@ static int validate_change(struct cpuset *cur, struct cpuset *trial)
 	return ret;
 }
 
+/*
+ * Preserve user provided cpumask if set unless there is no overlap.
+ */
+static int cpuset_set_cpus_allowed_ptr(struct task_struct *p,
+				       const struct cpumask *mask)
+{
+	if (p->user_cpus_ptr && cpumask_intersects(p->user_cpus_ptr, mask)) {
+		cpumask_var_t new_mask;
+		int ret;
+
+		alloc_cpumask_var(&new_mask, GFP_KERNEL);
+		cpumask_and(new_mask, p->user_cpus_ptr, mask);
+		ret = set_cpus_allowed_ptr(p, new_mask);
+		free_cpumask_var(new_mask);
+		return ret;
+	}
+
+	return set_cpus_allowed_ptr(p, mask);
+}
+
 #ifdef CONFIG_SMP
 /*
  * Helper routine for generate_sched_domains().
@@ -1130,7 +1150,7 @@ static void update_tasks_cpumask(struct cpuset *cs)
 
 	css_task_iter_start(&cs->css, 0, &it);
 	while ((task = css_task_iter_next(&it)))
-		set_cpus_allowed_ptr(task, cs->effective_cpus);
+		cpuset_set_cpus_allowed_ptr(task, cs->effective_cpus);
 	css_task_iter_end(&it);
 }
 
@@ -2303,7 +2323,7 @@ static void cpuset_attach(struct cgroup_taskset *tset)
 		 * can_attach beforehand should guarantee that this doesn't
 		 * fail.  TODO: have a better way to handle failure here
 		 */
-		WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach));
+		WARN_ON_ONCE(cpuset_set_cpus_allowed_ptr(task, cpus_attach));
 
 		cpuset_change_task_nodemask(task, &cpuset_attach_nodemask_to);
 		cpuset_update_task_spread_flag(cs, task);
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH v2 1/2] sched: Use user_cpus_ptr for saving user provided cpumask in sched_setaffinity()

In reply to Waiman Long
Quoted 41 lines · Waiman Long

The user_cpus_ptr field is added by commit b90ca8badbd1 ("sched:
Introduce task_struct::user_cpus_ptr to track requested affinity"). It is currently used only by arm64 arch due to possible asymmetric cpu setup. This patch extends its usage to save user provided cpumask when sched_setaffinity() is called for all arches.

To preserve the existing arm64 use case, a new cpus_affinity_set flag is added to differentiate if user_cpus_ptr is set up by sched_setaffinity() or by force_compatible_cpus_allowed_ptr(). user_cpus_ptr
set by sched_setaffinity() has priority and won't be overwritten by force_compatible_cpus_allowed_ptr() or relax_compatible_cpus_allowed_ptr().

As a call to sched_setaffinity() will no longer clear user_cpus_ptr but set it instead, the SCA_USER flag is no longer necessary and can be removed.

---
 include/linux/sched.h |  1 +
 kernel/sched/core.c   | 71 +++++++++++++++++++++++++++++++------------
 kernel/sched/sched.h  |  1 -
 3 files changed, 52 insertions(+), 21 deletions(-)
include/linux/sched.h +1 -0
index c46f3a63b758..60ae022fa842 100644
@@ -815,6 +815,7 @@ struct task_struct {
 
 	unsigned int			policy;
 	int				nr_cpus_allowed;
+	int				cpus_affinity_set;
 	const cpumask_t			*cpus_ptr;
 	cpumask_t			*user_cpus_ptr;
 	cpumask_t			cpus_mask;
kernel/sched/core.c +0 -0
index da0bf6fe9ecd..7757828c7422 100644

kernel/sched/core.c int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src, · line 2610

Quoted 4 lines
kernel/sched/core.c +1 -0
@@ -2607,6 +2607,7 @@ int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src,
 		return -ENOMEM;
 
 	cpumask_copy(dst->user_cpus_ptr, src->user_cpus_ptr);
+	dst->cpus_affinity_set = src->cpus_affinity_set;

I haven't been through this thorougly, but it looks a bit suspicious to me to inherit this field directly across fork(). If a 64-bit task with this flag set forks and then exec's a 32-bit program, arm64 will be in trouble if we're not able to override the affinity forcefully.

Will

author maintainer
#3 lore raw

Re: [PATCH v2 1/2] sched: Use user_cpus_ptr for saving user provided cpumask in sched_setaffinity()

Quoted 44 lines · Will Deacon
Quoted 43 lines · Waiman Long

The user_cpus_ptr field is added by commit b90ca8badbd1 ("sched:
Introduce task_struct::user_cpus_ptr to track requested affinity"). It is currently used only by arm64 arch due to possible asymmetric cpu setup. This patch extends its usage to save user provided cpumask when sched_setaffinity() is called for all arches.

To preserve the existing arm64 use case, a new cpus_affinity_set flag is added to differentiate if user_cpus_ptr is set up by sched_setaffinity() or by force_compatible_cpus_allowed_ptr(). user_cpus_ptr
set by sched_setaffinity() has priority and won't be overwritten by force_compatible_cpus_allowed_ptr() or relax_compatible_cpus_allowed_ptr().

As a call to sched_setaffinity() will no longer clear user_cpus_ptr but set it instead, the SCA_USER flag is no longer necessary and can be removed.

---
  include/linux/sched.h |  1 +
  kernel/sched/core.c   | 71 +++++++++++++++++++++++++++++++------------
  kernel/sched/sched.h  |  1 -
  3 files changed, 52 insertions(+), 21 deletions(-)
include/linux/sched.h +1 -0
index c46f3a63b758..60ae022fa842 100644
@@ -815,6 +815,7 @@ struct task_struct {
 
  	unsigned int			policy;
  	int				nr_cpus_allowed;
+	int				cpus_affinity_set;
  	const cpumask_t			*cpus_ptr;
  	cpumask_t			*user_cpus_ptr;
  	cpumask_t			cpus_mask;
kernel/sched/core.c +0 -0
index da0bf6fe9ecd..7757828c7422 100644
@@ -2607,6 +2607,7 @@ int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src,
  		return -ENOMEM;
 
Quoted 6 lines

kernel/sched/core.c int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src, · line 2610

Quoted 2 lines
kernel/sched/core.c +1 -0
  	cpumask_copy(dst->user_cpus_ptr, src->user_cpus_ptr);
+	dst->cpus_affinity_set = src->cpus_affinity_set;

I haven't been through this thorougly, but it looks a bit suspicious to me to inherit this field directly across fork(). If a 64-bit task with this flag set forks and then exec's a 32-bit program, arm64 will be in trouble if we're not able to override the affinity forcefully.

I believe you can still override the affinity. What is in user_cpus_ptr is not the actual affinity which is in cpus_mask. It is just what the users desire. Its value has be masked off by the current cpuset as well as what is allowed in task_cpu_possible_mask().

Cheers,
Longman

maintainer
#5 lore raw

Re: [PATCH v2 2/2] cgroup/cpuset: Keep user set cpus affinity

In reply to Waiman Long

(cc'ing Linus)

Hello,

Quoted 11 lines · Waiman Long

It was found that any change to the current cpuset hierarchy may reset the cpumask of the tasks in the affected cpusets to the default cpuset value even if those tasks have cpus affinity explicitly set by the users before. That is especially easy to trigger under a cgroup v2 environment where writing "+cpuset" to the root cgroup's cgroup.subtree_control file will reset the cpus affinity of all the processes in the system.

That is problematic in a nohz_full environment where the tasks running in the nohz_full CPUs usually have their cpus affinity explicitly set and will behave incorrectly if cpus affinity changes.

Quoted 8 lines

Fix this problem by looking at user_cpus_ptr which will be set if cpus affinity have been explicitly set before and use it to restrcit the given cpumask unless there is no overlap. In that case, it will fallback to the given one.

With that change in place, it was verified that tasks that have its cpus affinity explicitly set will not be affected by changes made to the v2 cgroup.subtree_control files.

The fact that the kernel clobbers user-specified cpus_allowed as cpu availability changes always bothered me and it has been causing this sort of problems w/ cpu hotplug and cpuset. We've been patching this up partially here and there but I think it would be better if we just make the rules really simple - ie. allow users to configure whatever cpus_allowed as long as that's within cpu_possible_mask and override only the effective cpus_allowed if the mask leaves no runnable CPUs, so that we can restore the original configured behavior if and when some of the cpus become available again.

One obvious problem with changing the behavior is that it may affect / confuse users expecting the current behavior however inconsistent it may be, but given that we have partially changed how cpus_allowed interacts with hotplug in the past and the current behavior can be inconsistent and surprising, I don't think this is a bridge we can't cross. What do others think?

Thanks.

Signature · 1 line
tejun
author maintainer
#6 lore raw

Re: [PATCH v2 2/2] cgroup/cpuset: Keep user set cpus affinity

Quoted 34 lines · Tejun Heo

(cc'ing Linus)

Hello,

Quoted 11 lines · Waiman Long

It was found that any change to the current cpuset hierarchy may reset the cpumask of the tasks in the affected cpusets to the default cpuset value even if those tasks have cpus affinity explicitly set by the users before. That is especially easy to trigger under a cgroup v2 environment where writing "+cpuset" to the root cgroup's cgroup.subtree_control file will reset the cpus affinity of all the processes in the system.

That is problematic in a nohz_full environment where the tasks running in the nohz_full CPUs usually have their cpus affinity explicitly set and will behave incorrectly if cpus affinity changes.

Quoted 8 lines

Fix this problem by looking at user_cpus_ptr which will be set if cpus affinity have been explicitly set before and use it to restrcit the given cpumask unless there is no overlap. In that case, it will fallback to the given one.

With that change in place, it was verified that tasks that have its cpus affinity explicitly set will not be affected by changes made to the v2 cgroup.subtree_control files.

The fact that the kernel clobbers user-specified cpus_allowed as cpu availability changes always bothered me and it has been causing this sort of problems w/ cpu hotplug and cpuset. We've been patching this up partially here and there but I think it would be better if we just make the rules really simple - ie. allow users to configure whatever cpus_allowed as long as that's within cpu_possible_mask and override only the effective cpus_allowed if the mask leaves no runnable CPUs, so that we can restore the original configured behavior if and when some of the cpus become available again.

Quoted 6 lines

One obvious problem with changing the behavior is that it may affect / confuse users expecting the current behavior however inconsistent it may be, but given that we have partially changed how cpus_allowed interacts with hotplug in the past and the current behavior can be inconsistent and surprising, I don't think this is a bridge we can't cross. What do others think?

My patch will still subject the cpus_allowed list to the constraint imposed by the current cpuset. It will keep as much of what the user specified though. If we are worrying about backward compatibility, maybe we can restrict that change in behavior to cgroup v2 only or we can add a sysctl parameter to restore old behavior if the user choose to.

Users are now gradually migrating over to cgroup v2 and they do understand that there are some changes in behavior when using cgroup v2.

Cheers,
Longman