cgroup/cpuset: Support multiple source/destination cpusets for cpuset_*attach()
Subsystems (2)
Waiman Long
maintainercover cgroup/cpuset: Support multiple source/destination cpusets for cpuset_*attach()
Sashiko AI review of another cpuset patch had found that cpuset_attach() and cpuset_can_attach() can be passed a cgroup_taskset with tasks migrating from one source cpuset to multiple destination cpusets and vice versa. Further testing of the cpuset code indicates that this is indeed the case when the v2 cpuset controller is enabled or disabled.
Unfortunately, cpuset_attach() and cpuset_can_attach() still assume that there will be one source and one destinaton cpuset which may result in inocrrect behavior. This patch series is created to fix this issue. The first 3 patches are just preparatory patches to make it easier to review the last patch which fixes this problem.
Waiman Long (4):
cgroup/cpuset: Add an alloc_dl_bw() helper cgroup/cpuset: Expand the scope of cpuset_can_attach_check() cgroup/cpuset: Optimize cpuset_attach_task() cgroup/cpuset: Support multiple source/destination cpusets for
cpuset_*attach()
kernel/cgroup/cpuset-internal.h | 6 + kernel/cgroup/cpuset.c | 315 +++++++++++++++++++++++--------- 2 files changed, 230 insertions(+), 91 deletions(-)
Signature · 1 line
2.54.0
Waiman Long
maintainerPATCH 1/4 cgroup/cpuset: Add an alloc_dl_bw() helper
Extract the DL bandwidth allocation code in cpuset_attach() to a new alloc_dl_bw() helper to simplify code.
No functional change is expected.
--- kernel/cgroup/cpuset.c | 50 +++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 22 deletions(-)
kernel/cgroup/cpuset.c +28 -22
@@ -2980,6 +2980,25 @@ static int cpuset_can_attach_check(struct cpuset *cs)return 0 ; } +static int alloc_dl_bw (struct cpuset *cs) +{ +int cpu, ret; + +if (!cs->sum_migrate_dl_bw) +return 0 ; + + cpu =cpumask_any_and (cpu_active_mask, cs->effective_cpus); +if (unlikely (cpu >= nr_cpu_ids)) +return -EINVAL ; + + ret =dl_bw_alloc (cpu, cs->sum_migrate_dl_bw); +if (ret) +return ret; + + cs->dl_bw_cpu = cpu; +return 0 ; +} +static void reset_migrate_dl_data (struct cpuset *cs) { cs->nr_migrate_dl_tasks =0 ;
@@ -2994,7 +3013,7 @@ static int cpuset_can_attach(struct cgroup_taskset *tset)struct cpuset *cs, *oldcs;struct task_struct *task;bool setsched_check; -int cpu, ret; +int ret;/* used later by cpuset_attach() */ cpuset_attach_old_cs =task_cs (cgroup_taskset_first (tset, &css));
@@ -3050,31 +3069,18 @@ static int cpuset_can_attach(struct cgroup_taskset *tset) } } -if (!cs->sum_migrate_dl_bw) - goto out_success; - - cpu =cpumask_any_and (cpu_active_mask, cs->effective_cpus); -if (unlikely (cpu >= nr_cpu_ids)) { - ret = -EINVAL ; - goto out_unlock; - } - - ret =dl_bw_alloc (cpu, cs->sum_migrate_dl_bw); -if (ret) - goto out_unlock; - - cs->dl_bw_cpu = cpu; - -out_success: -/* -* Mark attach is in progress. This makes validate_change() fail -* changes which zero cpus/mems_allowed. -*/ - cs->attach_in_progress++; + ret =alloc_dl_bw (cs); out_unlock:if (ret)reset_migrate_dl_data (cs); +else +/* +* Mark attach is in progress. This makes validate_change() fail +* changes which zero cpus/mems_allowed. +*/ + cs->attach_in_progress++; +mutex_unlock (&cpuset_mutex);return ret; }
Signature · 1 line
2.54.0
Waiman Long
maintainerPATCH 2/4 cgroup/cpuset: Expand the scope of cpuset_can_attach_check()
In reply to Waiman LongExpand the scope of cpuset_can_attach_check() by including the setting of setsched flag inside cpuset_can_attach_check() with the new @oldcs and @psetsched argument. As cpuset_can_attach_check() is also called from cpuset_can_fork(), set the new arguments to NULL from that caller.
While at it, expose the source and destination cpuset cpu/memory check results in the new attach_cpus_updated and attach_mems_updated static flags so that these flags can be used directly from cpuset_attach() without the need to do the same computations again.
No functional change is expected.
--- kernel/cgroup/cpuset.c | 69 +++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 28 deletions(-)
kernel/cgroup/cpuset.c +41 -28
@@ -2964,19 +2964,55 @@ static int update_prstate(struct cpuset *cs, int new_prs)return 0 ; } +/* +* cpuset_can_attach() and cpuset_attach() specific internal data +*/ static struct cpuset *cpuset_attach_old_cs; +static bool attach_cpus_updated; +static bool attach_mems_updated;/* * Check to see if a cpuset can accept a new task * For v1, cpus_allowed and mems_allowed can't be empty. * For v2, effective_cpus can't be empty. * Note that in v1, effective_cpus = cpus_allowed. +* +* Also set the boolean flag passed in by @psetsched depending on if +* security_task_setscheduler() call is needed and @oldcs is not NULL. */ -static int cpuset_can_attach_check (struct cpuset *cs) +static int cpuset_can_attach_check (struct cpuset *cs,struct cpuset *oldcs, +bool *psetsched) {if (cpumask_empty (cs->effective_cpus) || (!is_in_v2_mode () &&nodes_empty (cs->mems_allowed)))return -ENOSPC ; + +if (!oldcs) +return 0 ; + +/* +* Update attach specific data +*/ + attach_cpus_updated = !cpumask_equal (cs->effective_cpus, oldcs->effective_cpus); + attach_mems_updated = !nodes_equal (cs->effective_mems, oldcs->effective_mems); + +/* +* Skip rights over task setsched check in v2 when nothing changes, +* migration permission derives from hierarchy ownership in +* cgroup_procs_write_permission()). +*/ + *psetsched = !cpuset_v2 () || attach_cpus_updated || attach_mems_updated; + +/* +* A v1 cpuset with tasks will have no CPU left only when CPU hotplug +* brings the last online CPU offline as users are not allowed to empty +* cpuset.cpus when there are active tasks inside. When that happens, +* we should allow tasks to migrate out without security check to make +* sure they will be able to run after migration. +*/ +if (!is_in_v2_mode () &&cpumask_empty (oldcs->effective_cpus)) + *psetsched = false; +return 0 ; }
@@ -3023,29 +3059,10 @@ static int cpuset_can_attach(struct cgroup_taskset *tset)mutex_lock (&cpuset_mutex);/* Check to see if task is allowed in the cpuset */ - ret =cpuset_can_attach_check (cs); + ret =cpuset_can_attach_check (cs, oldcs, &setsched_check);if (ret) goto out_unlock; -/* -* Skip rights over task setsched check in v2 when nothing changes, -* migration permission derives from hierarchy ownership in -* cgroup_procs_write_permission()). -*/ - setsched_check = !cpuset_v2 () || - !cpumask_equal (cs->effective_cpus, oldcs->effective_cpus) || - !nodes_equal (cs->effective_mems, oldcs->effective_mems); - -/* -* A v1 cpuset with tasks will have no CPU left only when CPU hotplug -* brings the last online CPU offline as users are not allowed to empty -* cpuset.cpus when there are active tasks inside. When that happens, -* we should allow tasks to migrate out without security check to make -* sure they will be able to run after migration. -*/ -if (!is_in_v2_mode () &&cpumask_empty (oldcs->effective_cpus)) - setsched_check = false; -cgroup_taskset_for_each (task, css, tset) { ret =task_can_attach (task);if (ret)
@@ -3139,7 +3156,6 @@ static void cpuset_attach(struct cgroup_taskset *tset)struct cgroup_subsys_state *css;struct cpuset *cs;struct cpuset *oldcs = cpuset_attach_old_cs; -bool cpus_updated, mems_updated;bool queue_task_work = false;cgroup_taskset_first (tset, &css);
@@ -3147,9 +3163,6 @@ static void cpuset_attach(struct cgroup_taskset *tset)lockdep_assert_cpus_held ();/* see cgroup_attach_lock() */ mutex_lock (&cpuset_mutex); - cpus_updated = !cpumask_equal (cs->effective_cpus, - oldcs->effective_cpus); - mems_updated = !nodes_equal (cs->effective_mems, oldcs->effective_mems);/* * In the default hierarchy, enabling cpuset in the child cgroups
@@ -3157,7 +3170,7 @@ static void cpuset_attach(struct cgroup_taskset *tset)* in effective cpus and mems. In that case, we can optimize out * by skipping the task iteration and update. */ -if (cpuset_v2 () && !cpus_updated && !mems_updated) { +if (cpuset_v2 () && !attach_cpus_updated && !attach_mems_updated) { cpuset_attach_nodemask_to = cs->effective_mems; goto out; }
@@ -3174,7 +3187,7 @@ static void cpuset_attach(struct cgroup_taskset *tset)* not set. */ cpuset_attach_nodemask_to = cs->effective_mems; -if (!is_memory_migrate (cs) && !mems_updated) +if (!is_memory_migrate (cs) && !attach_mems_updated) goto out;cgroup_taskset_for_each_leader (leader, css, tset) {
@@ -3589,7 +3602,7 @@ static int cpuset_can_fork(struct task_struct *task, struct css_set *cset)mutex_lock (&cpuset_mutex);/* Check to see if task is allowed in the cpuset */ - ret =cpuset_can_attach_check (cs); + ret =cpuset_can_attach_check (cs,NULL ,NULL );if (ret) goto out_unlock;
Signature · 1 line
2.54.0
Waiman Long
maintainerPATCH 3/4 cgroup/cpuset: Optimize cpuset_attach_task()
In reply to Waiman LongWithin cpuset_attach(), cpuset_attach_task() is called only if either the CPU and/or the memory setting are updated. If only one of the settings is updated, cpuset_attach_task() still updates both CPU and memory node setting of each task. Further optimize it by checking attach_cpus_updated and attach_mems_updated for v2 to skip the unnecessary update.
While at it, also move the mpol_rebind_mm() call for mm group leader to cpuset_attach_task(). This change shouldn't affect the cpuset_fork() caller as the newly cloned task isn't the group leader. For that caller, it is assumed that both CPU and memory nodes are updated to keep the existing behavior.
--- kernel/cgroup/cpuset.c | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-)
kernel/cgroup/cpuset.c +26 -10
@@ -3132,8 +3132,13 @@ static nodemask_t cpuset_attach_nodemask_to;static void cpuset_attach_task (struct cpuset *cs,struct task_struct *task) { +struct mm_struct *mm; +lockdep_assert_cpuset_lock_held (); +if (cpuset_v2 () && !attach_cpus_updated) + goto update_mem; +if (cs != &top_cpuset)guarantee_active_cpus (task, cpus_attach);else
@@ -3145,8 +3150,21 @@ static void cpuset_attach_task(struct cpuset *cs, struct task_struct *task)*/ WARN_ON_ONCE (set_cpus_allowed_ptr (task, cpus_attach)); +update_mem: +if (cpuset_v2 () && !attach_mems_updated) +return ; +cpuset_change_task_nodemask (task, &cpuset_attach_nodemask_to);cpuset1_update_task_spread_flags (cs, task); + +if (task != task->group_leader) +return ; + + mm =get_task_mm (task); +if (mm) { +mpol_rebind_mm (mm, &cs->effective_mems); +mmput (mm); + } }static void cpuset_attach (struct cgroup_taskset *tset)
@@ -3187,15 +3205,13 @@ static void cpuset_attach(struct cgroup_taskset *tset)* not set. */ cpuset_attach_nodemask_to = cs->effective_mems; -if (!is_memory_migrate (cs) && !attach_mems_updated) +if (!is_memory_migrate (cs)) goto out;cgroup_taskset_for_each_leader (leader, css, tset) {struct mm_struct *mm =get_task_mm (leader);if (mm) { -mpol_rebind_mm (mm, &cpuset_attach_nodemask_to); -/* * old_mems_allowed is the same with mems_allowed * here, except if this task is being moved
@@ -3204,18 +3220,15 @@ static void cpuset_attach(struct cgroup_taskset *tset)* @old_mems_allowed is the right nodesets that we * migrate mm from. */ -if (is_memory_migrate (cs)) { -cpuset_migrate_mm (mm, &oldcs->old_mems_allowed, - &cpuset_attach_nodemask_to); - queue_task_work = true; - }else -mmput (mm); +cpuset_migrate_mm (mm, &oldcs->old_mems_allowed, + &cpuset_attach_nodemask_to); + queue_task_work = true; } } -out:if (queue_task_work)schedule_flush_migrate_mm (); +out: cs->old_mems_allowed = cpuset_attach_nodemask_to;if (cs->nr_migrate_dl_tasks) {
@@ -3666,7 +3679,10 @@ static void cpuset_fork(struct task_struct *task)/* CLONE_INTO_CGROUP */ mutex_lock (&cpuset_mutex);guarantee_online_mems (cs, &cpuset_attach_nodemask_to); +/* Assume CPUs and memory nodes are updated */ + attach_cpus_updated = attach_mems_updated = true;cpuset_attach_task (cs, task); + attach_cpus_updated = attach_mems_updated = false;dec_attach_in_progress_locked (cs);mutex_unlock (&cpuset_mutex);
Signature · 1 line
2.54.0
Waiman Long
maintainerPATCH 4/4 cgroup/cpuset: Support multiple source/destination cpusets for cpuset_*attach()
In reply to Waiman LongWith cgroup v2, the cgroup_taskset structure passed into the cgroup can_attach() and attach() methods can contain task migration data with multiple destination or source cpusets when the cpuset controller is enabled or disabled respectively.
Since cpuset is threaded, another possible way to cause many-to-one migration is to move the whole process with multiple threads in different cpuset enabled threaded cgroups into another cpuset enabled cgroup. Alternatively, multiple processs from different cpusets can be written into cgroup.proc as a single operation.
The current cpuset_can_attach() and cpuset_attach() functions still expect task migration is from one source cpuset to one destination cpuset. This has been the case since cpuset was enabled for cgroup v2 in commit 4ec22e9c5a90 ("cpuset: Enable cpuset controller in default hierarchy").
This problem is less an issue when enabling the cpuset controller as all the newly created child cpusets will have exactly the same set of CPUs and memory nodes except when deadline tasks are involved in migration as the deadline task accounting data can be off.
It can be more problematic when the cpuset controller is disabled as their set of CPUs and memory nodes may differ from their parent or with the moving of multi-threaded process from different threaded cgroups.
Fix that by tracking the set of source (old) and destination cpusets in singly linked lists and iterating them all to properly update the internal data. Also keep the current cs and oldcs variables up-to-date with the css and task iterators. cpuset_attach_old_cs is now dropped as the old cpusets are now being tracked.
To ensure proper DL tasks accounting, the nr_migrate_dl_tasks in both the source and destination cpusets are decremented/incremented with their values added to nr_deadline_tasks when the migration is successful.
--- kernel/cgroup/cpuset-internal.h | 6 + kernel/cgroup/cpuset.c | 206 +++++++++++++++++++++++--------- 2 files changed, 158 insertions(+), 54 deletions(-)
kernel/cgroup/cpuset-internal.h +6 -0
@@ -161,6 +161,12 @@ struct cpuset {*/ bool remote_partition; +/* +* cpuset_can_attach() and cpuset_attach() specific data +*/ +bool attach_node_in_llist; +struct llist_node attach_node; +/* * number of SCHED_DEADLINE tasks attached to this cpuset, so that we * know when to rebuild associated root domain bandwidth information.
kernel/cgroup/cpuset.c +152 -54
318 lines not on this page · raw message
Signature · 1 line
2.54.0
Tejun Heo
maintainerHello,
Quick AI-assisted review pass; passing the points along for human eyes.
Patch 4:
- The leader loop comment says "Only v1 supports memory_migrate", but CS_MEMORY_MIGRATE is set unconditionally on v2 cpusets in cpuset_css_alloc(). With v2 controller-disable folding children with differing effective_mems into the parent, picking a single llist_entry(src_cs_head.first, ...) as oldcs passes the wrong source nodemask to cpuset_migrate_mm() for every leader whose actual source differs. Looks like the source needs to be looked up per leader.
- cs->old_mems_allowed updates are inconsistent across destinations: the mid-loop transition assigns cs->effective_mems (raw) while the tail assignment uses cpuset_attach_nodemask_to (after guarantee_online_mems). The v2 fast-path also updates only the first-task cs, leaving other destinations on dst_cs_head stale.
Patch 3:
- Changelog says "the newly cloned task isn't the group leader", but for CLONE_INTO_CGROUP without CLONE_THREAD the new task is its own group_leader, so the new mpol_rebind_mm() block in cpuset_attach_task() does run from cpuset_fork(). Either acknowledge as an incidental improvement or guard the new path.
Patch 1:
- alloc_dl_bw() reads confusingly next to the scheduler's dl_bw_alloc() while doing more (pick cpu, call dl_bw_alloc, record cs->dl_bw_cpu). Something like cpuset_reserve_dl_bw() would be clearer.
- The relocated "Mark attach is in progress" comment sits inside a braceless else; either move it above the if (ret) or brace both arms.
Patch 2 looked clean.
Thanks.
--
tejun
Waiman Long
maintainerQuoted 3 lines · Tejun Heo
Hello,
Quick AI-assisted review pass; passing the points along for human eyes.
Thank for passing the AI review info.
Quoted 9 lines
Patch 4:
- The leader loop comment says "Only v1 supports
memory_migrate", butCS_MEMORY_MIGRATEis set unconditionally on v2 cpusets incpuset_css_alloc(). With v2 controller-disable folding children with differingeffective_memsinto the parent, picking a singlellist_entry(src_cs_head.first, ...)as oldcs passes the wrong source nodemask tocpuset_migrate_mm()for every leader whose actual source differs. Looks like the source needs to be looked up per leader.
I falsely assumes that CS_MEMORY_MIGRATE isn't set for v2. I am going to move the cpuset_migrate_mm() call into cpuset_attach_task() as well. To enable access to right oldcs information, I have to reluctantly add the oldcs pointer to task_struct as that is the simplest way moving forward.
Quoted 6 lines
-
cs->old_mems_allowedupdates are inconsistent across destinations: the mid-loop transition assignscs->effective_mems(raw) while the tail assignment usescpuset_attach_nodemask_to(afterguarantee_online_mems). The v2 fast-path also updates only the first-task cs, leaving other destinations ondst_cs_headstale.
I will clarify the confusion and fix the v2 fast-path in v2.
Quoted 7 lines
Patch 3:
- Changelog says "the newly cloned task isn't the group leader", but for
CLONE_INTO_CGROUPwithoutCLONE_THREADthe new task is its owngroup_leader, so the newmpol_rebind_mm()block incpuset_attach_task()does run fromcpuset_fork(). Either acknowledge as an incidental improvement or guard the new path.
Will update the patch description.
Quoted 6 lines
Patch 1:
-
alloc_dl_bw()reads confusingly next to the scheduler'sdl_bw_alloc()while doing more (pick cpu, calldl_bw_alloc, recordcs->dl_bw_cpu). Something likecpuset_reserve_dl_bw()would be clearer.
Sure, cpuset_reserve_dl_bw() sounds like a good name.
Quoted 2 lines
- The relocated "Mark attach is in progress" comment sits inside a braceless else; either move it above the if (ret) or brace both arms.
Will add the additional braces though that will be removed in a later patch.
Cheers,
Longman
Quoted 8 lines
Patch 2 looked clean.
Thanks.
--
tejun