kernel/cgroup/cgroup.c
Source file repositories/reference/linux-study-clean/kernel/cgroup/cgroup.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/cgroup/cgroup.c- Extension
.c- Size
- 202336 bytes
- Lines
- 7524
- Domain
- Core OS
- Bucket
- Scheduler, Processes, Timers, Sync, And Syscalls
- Inferred role
- Core OS: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
cgroup-internal.hlinux/bpf-cgroup.hlinux/cred.hlinux/errno.hlinux/init_task.hlinux/kernel.hlinux/magic.hlinux/mutex.hlinux/mount.hlinux/pagemap.hlinux/proc_fs.hlinux/rcupdate.hlinux/sched.hlinux/sched/task.hlinux/slab.hlinux/spinlock.hlinux/percpu-rwsem.hlinux/string.hlinux/hashtable.hlinux/idr.hlinux/kthread.hlinux/atomic.hlinux/cpuset.hlinux/proc_ns.hlinux/nsproxy.hlinux/file.hlinux/fs_parser.hlinux/sched/cputime.hlinux/sched/deadline.hlinux/psi.hlinux/nstree.hlinux/irq_work.h
Detected Declarations
enum cgroup_opt_featuresenum cgroup2_paramenum cpuset_paramfunction cgroup_subsys_enabledfunction cgroup_on_dflfunction cgroup_idr_allocfunction cgroup_idr_removefunction cgroup_is_threadedfunction cgroup_is_mixablefunction cgroup_can_be_thread_rootfunction cgroup_is_thread_rootfunction cgroup_is_valid_domainfunction cgroup_controlfunction cgroup_ss_maskfunction cgroup_cssfunction css_putfunction cgroup_get_livefunction __cgroup_task_countfunction cgroup_task_countfunction css_set_threadedfunction css_set_populatedfunction css_update_populatedfunction css_set_update_populatedfunction for_each_subsysfunction css_set_skip_task_itersfunction css_set_move_taskfunction css_set_hashfunction put_css_set_lockedfunction list_for_each_entry_safefunction compare_css_setsfunction free_cgrp_cset_linksfunction list_for_each_entry_safefunction allocate_cgrp_cset_linksfunction link_css_setfunction for_each_subsysfunction cgroup_favor_dynmodsfunction cgroup_init_root_idfunction cgroup_exit_root_idfunction cgroup_free_rootfunction cgroup_destroy_rootfunction list_for_each_entry_safefunction list_for_each_entryfunction current_cgns_cgroup_from_rootfunction current_cgns_cgroup_from_rootfunction cgroup_file_modefunction cgroup_calc_subtree_ss_maskfunction do_each_subsys_maskfunction cgroup_kn_lock_live
Annotated Snippet
* cgroup_fork - initialize cgroup related fields during copy_process()
* @child: pointer to task_struct of forking parent process.
*
* A task is associated with the init_css_set until cgroup_post_fork()
* attaches it to the target css_set.
*/
void cgroup_fork(struct task_struct *child)
{
RCU_INIT_POINTER(child->cgroups, &init_css_set);
INIT_LIST_HEAD(&child->cg_list);
}
/**
* cgroup_v1v2_get_from_file - get a cgroup pointer from a file pointer
* @f: file corresponding to cgroup_dir
*
* Find the cgroup from a file pointer associated with a cgroup directory.
* Returns a pointer to the cgroup on success. ERR_PTR is returned if the
* cgroup cannot be found.
*/
static struct cgroup *cgroup_v1v2_get_from_file(struct file *f)
{
struct cgroup_subsys_state *css;
css = css_tryget_online_from_dir(f->f_path.dentry, NULL);
if (IS_ERR(css))
return ERR_CAST(css);
return css->cgroup;
}
/**
* cgroup_get_from_file - same as cgroup_v1v2_get_from_file, but only supports
* cgroup2.
* @f: file corresponding to cgroup2_dir
*/
static struct cgroup *cgroup_get_from_file(struct file *f)
{
struct cgroup *cgrp = cgroup_v1v2_get_from_file(f);
if (IS_ERR(cgrp))
return ERR_CAST(cgrp);
if (!cgroup_on_dfl(cgrp)) {
cgroup_put(cgrp);
return ERR_PTR(-EBADF);
}
return cgrp;
}
/**
* cgroup_css_set_fork - find or create a css_set for a child process
* @kargs: the arguments passed to create the child process
*
* This functions finds or creates a new css_set which the child
* process will be attached to in cgroup_post_fork(). By default,
* the child process will be given the same css_set as its parent.
*
* If CLONE_INTO_CGROUP is specified this function will try to find an
* existing css_set which includes the requested cgroup and if not create
* a new css_set that the child will be attached to later. If this function
* succeeds it will hold cgroup_threadgroup_rwsem on return. If
* CLONE_INTO_CGROUP is requested this function will grab cgroup mutex
* before grabbing cgroup_threadgroup_rwsem and will hold a reference
* to the target cgroup.
*/
static int cgroup_css_set_fork(struct kernel_clone_args *kargs)
__acquires(&cgroup_mutex) __acquires(&cgroup_threadgroup_rwsem)
{
int ret;
struct cgroup *dst_cgrp = NULL;
struct css_set *cset;
struct super_block *sb;
if (kargs->flags & CLONE_INTO_CGROUP)
cgroup_lock();
cgroup_threadgroup_change_begin(current);
spin_lock_irq(&css_set_lock);
cset = task_css_set(current);
get_css_set(cset);
if (kargs->cgrp)
kargs->kill_seq = kargs->cgrp->kill_seq;
else
kargs->kill_seq = cset->dfl_cgrp->kill_seq;
spin_unlock_irq(&css_set_lock);
if (!(kargs->flags & CLONE_INTO_CGROUP)) {
Annotation
- Immediate include surface: `cgroup-internal.h`, `linux/bpf-cgroup.h`, `linux/cred.h`, `linux/errno.h`, `linux/init_task.h`, `linux/kernel.h`, `linux/magic.h`, `linux/mutex.h`.
- Detected declarations: `enum cgroup_opt_features`, `enum cgroup2_param`, `enum cpuset_param`, `function cgroup_subsys_enabled`, `function cgroup_on_dfl`, `function cgroup_idr_alloc`, `function cgroup_idr_remove`, `function cgroup_is_threaded`, `function cgroup_is_mixable`, `function cgroup_can_be_thread_root`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- Implementation status: integration implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.