A BPF kthread runs in the root cgroup, as every kernel thread does, so whatever it does is accounted to nobody. A pool of reclaim workers that serves one memory cgroup each, or a thread that does a container's work on its behalf, should have its CPU time and the pressure it causes charged to the cgroup it works for, and should run within that cgroup's cpuset. The program can read the thread's PID and leave the placement to user space, but the thread then runs unplaced until a round trip through user space completes, and a program that creates threads as its load demands has no user space thread waiting for each one. Add bpf_kthread_attach_cgroup(), which moves the thread into a cgroup the program names, usually one it got from bpf_cgroup_from_id(). The cgroup core gains cgroup_attach_kthread() for it, which is what writing the thread's PID to cgroup.procs does once the permission check, which the program's privilege covers, is left out: it takes cgroup_mutex, cpus_read_lock() and the threadgroup rwsem, the moved process's own under favordynmods as cgroup.procs does, refuses a dying cgroup and one that cannot host processes on the default hierarchy, and migrates the thread as a thread group of one. The thread then counts as a member of the cgroup, which cannot be removed until the thread leaves or exits; a program that places threads in cgroups it does not own has to stop them when the cgroup's owner wants it gone, which is why the move is a separate call and not a parameter of the creation. A cpuset applies its CPUs when the thread joins, so a bind has to follow the move. The move does not make the thread a user task. Kernel memory it allocates is still charged to no memory cgroup, and it is not frozen with the cgroup, which then never reports frozen, as with any kernel thread in a cgroup. A running thread joins a cpuset at its next park, and the move waits for that under cgroup_mutex, cpus_read_lock() and the threadgroup rwsem, so it runs with migration enabled, as the stop and the bind do. Pinned, it could wait for a thread that is itself waiting for the caller to enable migration, through a cpuset writer or through a stop, and a thread on a CPU going offline could wait for cpus_read_lock() while the offline waits for the thread to enable migration; migratable, the caller satisfies whatever waits for it and the move completes. The core helper refuses what cgroup_procs_write_start() refuses, a task that may not migrate between cgroups or that has PF_NO_SETAFFINITY, and leaves a thread whose callback has returned where it is, as cgroup.procs leaves an exiting task. Signed-off-by: Kumar Kartikeya Dwivedi --- include/linux/cgroup.h | 3 +++ kernel/bpf/kthread.c | 56 ++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 9 ++++++- kernel/cgroup/cgroup.c | 45 +++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 1 deletion(-) diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index 2afb4cb2bb4f..f649973801f4 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -158,6 +158,7 @@ struct cgroup *cgroup_v1v2_get_from_fd(int fd); bool cgroup1_ssid_disabled(int ssid); int cgroup_attach_task_all(struct task_struct *from, struct task_struct *); +int cgroup_attach_kthread(struct cgroup *dst_cgrp, struct task_struct *task); int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from); int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts); @@ -804,6 +805,8 @@ static inline void cgroup_lock(void) {} static inline void cgroup_unlock(void) {} static inline int cgroup_attach_task_all(struct task_struct *from, struct task_struct *t) { return 0; } +static inline int cgroup_attach_kthread(struct cgroup *dst_cgrp, + struct task_struct *task) { return -ENODEV; } static inline int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) { return -EINVAL; } diff --git a/kernel/bpf/kthread.c b/kernel/bpf/kthread.c index 444fff4bd102..d4b8110545f0 100644 --- a/kernel/bpf/kthread.c +++ b/kernel/bpf/kthread.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -397,6 +398,58 @@ __bpf_kfunc int bpf_kthread_bind(struct task_struct *task, u32 cpu, u64 flags) return ret; } +#ifdef CONFIG_CGROUPS +/** + * bpf_kthread_attach_cgroup - Move a BPF kthread into a cgroup + * @task: a thread created by bpf_kthread_create() + * @cgrp: the cgroup to move it into + * @flags: must be 0 + * + * Moves the thread into @cgrp, started or not, so that its CPU time and the + * pressure it causes are accounted to @cgrp and its affinity follows @cgrp's + * cpuset. Until then it runs in the root cgroup, as a kernel thread does. + * Kernel memory the thread allocates is still not charged to @cgrp, since + * the allocations of a kernel thread are charged to no memory cgroup. The + * thread is a member of @cgrp like any process: @cgrp cannot be removed until + * the thread leaves or exits, and @cgrp has to be able to host processes, so + * on cgroup v2 it may not have controllers enabled in its children. Like any + * kernel thread it is not frozen with @cgrp, which then never reports frozen. + * A cpuset applies its CPUs to the thread as it joins, so bind the + * thread with bpf_kthread_bind() afterwards, not before. A running thread + * joins a cpuset at its next park, and the move waits for that with + * migration enabled, holding cgroup_mutex meanwhile, so the caller may return + * on another CPU and a thread may move itself. A thread whose callback has + * returned but that has not been stopped is left where it is and the call + * reports success, as cgroup.procs does for an exiting task. + * + * Return: 0 on success, -EINVAL if @flags is not 0, -ESRCH if @task is not a + * BPF kthread or has been stopped, -ENODEV if @cgrp is being removed, -EBUSY + * or -EOPNOTSUPP if @cgrp cannot host processes, or an error of the move. + */ +__bpf_kfunc int bpf_kthread_attach_cgroup(struct task_struct *task, struct cgroup *cgrp, + u64 flags) +{ + unsigned long irqflags; + bool found; + int ret; + + if (flags) + return -EINVAL; + + raw_spin_lock_irqsave(&bpf_kthreads_lock, irqflags); + found = !!bpf_kthread_find(task); + raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags); + if (!found) + return -ESRCH; + + /* A running thread joins at its next park: see the comment at the top. */ + migrate_enable(); + ret = cgroup_attach_kthread(cgrp, task); + migrate_disable(); + return ret; +} +#endif + /** * bpf_kthread_stop - Stop a BPF kthread and wait for it to exit * @task: a thread created by bpf_kthread_create() @@ -500,6 +553,9 @@ BTF_KFUNCS_START(bpf_kthread_kfunc_ids) BTF_ID_FLAGS(func, bpf_kthread_create, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE | KF_IMPLICIT_ARGS) BTF_ID_FLAGS(func, bpf_kthread_start, KF_RCU) BTF_ID_FLAGS(func, bpf_kthread_bind, KF_SLEEPABLE) +#ifdef CONFIG_CGROUPS +BTF_ID_FLAGS(func, bpf_kthread_attach_cgroup, KF_SLEEPABLE) +#endif BTF_ID_FLAGS(func, bpf_kthread_stop, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_kthread_should_stop) BTF_KFUNCS_END(bpf_kthread_kfunc_ids) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 54fc44dd8fcc..e7769999e8c3 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -14367,6 +14367,7 @@ enum special_kfunc_type { KF_bpf_kthread_create, KF_bpf_kthread_stop, KF_bpf_kthread_bind, + KF_bpf_kthread_attach_cgroup, KF_bpf_arena_alloc_pages, KF_bpf_arena_free_pages, KF_bpf_session_is_return, @@ -14463,6 +14464,11 @@ BTF_ID(func, bpf_call_rcu_tasks_trace) BTF_ID(func, bpf_kthread_create) BTF_ID(func, bpf_kthread_stop) BTF_ID(func, bpf_kthread_bind) +#ifdef CONFIG_CGROUPS +BTF_ID(func, bpf_kthread_attach_cgroup) +#else +BTF_ID_UNUSED +#endif #if defined(CONFIG_MMU) && defined(CONFIG_64BIT) BTF_ID(func, bpf_arena_alloc_pages) BTF_ID(func, bpf_arena_free_pages) @@ -14510,7 +14516,8 @@ static bool is_migrating_kfunc(u32 btf_id) return btf_id == special_kfunc_list[KF_bpf_task_park] || btf_id == special_kfunc_list[KF_bpf_kthread_create] || btf_id == special_kfunc_list[KF_bpf_kthread_stop] || - btf_id == special_kfunc_list[KF_bpf_kthread_bind]; + btf_id == special_kfunc_list[KF_bpf_kthread_bind] || + btf_id == special_kfunc_list[KF_bpf_kthread_attach_cgroup]; } static bool is_bpf_obj_new_kfunc(u32 func_id) diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index a284225f8dc9..41adae216ada 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -3124,6 +3124,51 @@ int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader, return ret; } +/** + * cgroup_attach_kthread - move a kernel thread into a cgroup + * @dst_cgrp: the cgroup to move it into + * @task: the kernel thread + * + * For kernel threads the kernel creates on behalf of a user and accounts to a + * cgroup of the user's choosing rather than, as cgroup_attach_task_all() does, + * to the creator's own. @dst_cgrp is checked as a destination of cgroup.procs + * is, except for the permission check, which the caller owes: it must not be + * dying and, on the default hierarchy, must be able to host processes, and + * @task must be one the cgroup core may migrate. + * + * Return: 0 on success, -ENODEV if @dst_cgrp is being removed, -EBUSY or + * -EOPNOTSUPP if it cannot host processes, -EINVAL if @task may not be + * migrated, or the error of the migration. + */ +int cgroup_attach_kthread(struct cgroup *dst_cgrp, struct task_struct *task) +{ + enum cgroup_attach_lock_mode lock_mode; + int ret; + + cgroup_lock(); + if (cgroup_is_dead(dst_cgrp)) { + ret = -ENODEV; + goto out_unlock; + } + /* As cgroup_procs_write_start() refuses them: cpusets cannot move these. */ + if (task->no_cgroup_migration || (task->flags & PF_NO_SETAFFINITY)) { + ret = -EINVAL; + goto out_unlock; + } + ret = cgroup_migrate_vet_dst(dst_cgrp); + if (ret) + goto out_unlock; + /* The moved process's own rwsem under favordynmods, as cgroup.procs takes. */ + lock_mode = cgroup_enable_per_threadgroup_rwsem ? CGRP_ATTACH_LOCK_PER_THREADGROUP : + CGRP_ATTACH_LOCK_GLOBAL; + cgroup_attach_lock(lock_mode, task); + ret = cgroup_attach_task(dst_cgrp, task, true); + cgroup_attach_unlock(lock_mode, task); +out_unlock: + cgroup_unlock(); + return ret; +} + struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup, enum cgroup_attach_lock_mode *lock_mode) { -- 2.53.0