Skip to content

Commit 77f8879

Browse files
committed
cgroup, kthread: close race window where new kthreads can be migrated to non-root cgroups
Creation of a kthread goes through a couple interlocked stages between the kthread itself and its creator. Once the new kthread starts running, it initializes itself and wakes up the creator. The creator then can further configure the kthread and then let it start doing its job by waking it up. In this configuration-by-creator stage, the creator is the only one that can wake it up but the kthread is visible to userland. When altering the kthread's attributes from userland is allowed, this is fine; however, for cases where CPU affinity is critical, kthread_bind() is used to first disable affinity changes from userland and then set the affinity. This also prevents the kthread from being migrated into non-root cgroups as that can affect the CPU affinity and many other things. Unfortunately, the cgroup side of protection is racy. While the PF_NO_SETAFFINITY flag prevents further migrations, userland can win the race before the creator sets the flag with kthread_bind() and put the kthread in a non-root cgroup, which can lead to all sorts of problems including incorrect CPU affinity and starvation. This bug got triggered by userland which periodically tries to migrate all processes in the root cpuset cgroup to a non-root one. Per-cpu workqueue workers got caught while being created and ended up with incorrected CPU affinity breaking concurrency management and sometimes stalling workqueue execution. This patch adds task->no_cgroup_migration which disallows the task to be migrated by userland. kthreadd starts with the flag set making every child kthread start in the root cgroup with migration disallowed. The flag is cleared after the kthread finishes initialization by which time PF_NO_SETAFFINITY is set if the kthread should stay in the root cgroup. It'd be better to wait for the initialization instead of failing but I couldn't think of a way of implementing that without adding either a new PF flag, or sleeping and retrying from waiting side. Even if userland depends on changing cgroup membership of a kthread, it either has to be synchronized with kthread_create() or periodically repeat, so it's unlikely that this would break anything. v2: Switch to a simpler implementation using a new task_struct bit field suggested by Oleg. Signed-off-by: Tejun Heo <[email protected]> Suggested-by: Oleg Nesterov <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Peter Zijlstra (Intel) <[email protected]> Cc: Thomas Gleixner <[email protected]> Reported-and-debugged-by: Chris Mason <[email protected]> Cc: [email protected] # v4.3+ (we can't close the race on < v4.3) Signed-off-by: Tejun Heo <[email protected]>
1 parent b6a6759 commit 77f8879

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

include/linux/cgroup.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,25 @@ static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
570570
pr_cont_kernfs_path(cgrp->kn);
571571
}
572572

573+
static inline void cgroup_init_kthreadd(void)
574+
{
575+
/*
576+
* kthreadd is inherited by all kthreads, keep it in the root so
577+
* that the new kthreads are guaranteed to stay in the root until
578+
* initialization is finished.
579+
*/
580+
current->no_cgroup_migration = 1;
581+
}
582+
583+
static inline void cgroup_kthread_ready(void)
584+
{
585+
/*
586+
* This kthread finished initialization. The creator should have
587+
* set PF_NO_SETAFFINITY if this kthread should stay in the root.
588+
*/
589+
current->no_cgroup_migration = 0;
590+
}
591+
573592
#else /* !CONFIG_CGROUPS */
574593

575594
struct cgroup_subsys_state;
@@ -590,6 +609,8 @@ static inline void cgroup_free(struct task_struct *p) {}
590609

591610
static inline int cgroup_init_early(void) { return 0; }
592611
static inline int cgroup_init(void) { return 0; }
612+
static inline void cgroup_init_kthreadd(void) {}
613+
static inline void cgroup_kthread_ready(void) {}
593614

594615
static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
595616
struct cgroup *ancestor)

include/linux/sched.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,10 @@ struct task_struct {
604604
#ifdef CONFIG_COMPAT_BRK
605605
unsigned brk_randomized:1;
606606
#endif
607+
#ifdef CONFIG_CGROUPS
608+
/* disallow userland-initiated cgroup migration */
609+
unsigned no_cgroup_migration:1;
610+
#endif
607611

608612
unsigned long atomic_flags; /* Flags requiring atomic access. */
609613

kernel/cgroup/cgroup.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,11 +2425,12 @@ ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
24252425
tsk = tsk->group_leader;
24262426

24272427
/*
2428-
* Workqueue threads may acquire PF_NO_SETAFFINITY and become
2429-
* trapped in a cpuset, or RT worker may be born in a cgroup
2430-
* with no rt_runtime allocated. Just say no.
2428+
* kthreads may acquire PF_NO_SETAFFINITY during initialization.
2429+
* If userland migrates such a kthread to a non-root cgroup, it can
2430+
* become trapped in a cpuset, or RT kthread may be born in a
2431+
* cgroup with no rt_runtime allocated. Just say no.
24312432
*/
2432-
if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
2433+
if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) {
24332434
ret = -EINVAL;
24342435
goto out_unlock_rcu;
24352436
}

kernel/kthread.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/freezer.h>
2121
#include <linux/ptrace.h>
2222
#include <linux/uaccess.h>
23+
#include <linux/cgroup.h>
2324
#include <trace/events/sched.h>
2425

2526
static DEFINE_SPINLOCK(kthread_create_lock);
@@ -225,6 +226,7 @@ static int kthread(void *_create)
225226

226227
ret = -EINTR;
227228
if (!test_bit(KTHREAD_SHOULD_STOP, &self->flags)) {
229+
cgroup_kthread_ready();
228230
__kthread_parkme(self);
229231
ret = threadfn(data);
230232
}
@@ -538,6 +540,7 @@ int kthreadd(void *unused)
538540
set_mems_allowed(node_states[N_MEMORY]);
539541

540542
current->flags |= PF_NOFREEZE;
543+
cgroup_init_kthreadd();
541544

542545
for (;;) {
543546
set_current_state(TASK_INTERRUPTIBLE);

0 commit comments

Comments
 (0)