kernel/kthread.c
Source file repositories/reference/linux-study-clean/kernel/kthread.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/kthread.c- Extension
.c- Size
- 48706 bytes
- Lines
- 1739
- 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.
- 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
uapi/linux/sched/types.hlinux/mm.hlinux/mmu_context.hlinux/sched.hlinux/sched/mm.hlinux/sched/task.hlinux/kthread.hlinux/completion.hlinux/err.hlinux/cgroup.hlinux/cpuset.hlinux/unistd.hlinux/file.hlinux/export.hlinux/mutex.hlinux/slab.hlinux/freezer.hlinux/ptrace.hlinux/uaccess.hlinux/numa.hlinux/sched/isolation.htrace/events/sched.h
Detected Declarations
struct kthread_create_infostruct kthreadstruct kthread_flush_workenum KTHREAD_BITSfunction get_kthread_commfunction set_kthread_structfunction free_kthread_structfunction kthread_stopfunction __kthread_should_parkfunction kthread_parkfunction kthread_should_stop_or_parkfunction kthread_should_stopfunction __kthread_parkmefunction kthread_parkmefunction kthread_do_exitfunction kthread_complete_and_exitfunction kthread_fetch_affinityfunction kthread_affine_nodefunction kthreadfunction tsk_fork_get_nodefunction create_kthreadfunction __printffunction ERR_PTRfunction __kthread_bind_maskfunction __kthread_bindfunction kthread_bind_maskfunction stoppedfunction kthread_set_per_cpufunction kthread_is_per_cpufunction kthread_should_parkfunction kthread_should_parkfunction kthread_should_stopfunction kthread_createfunction kthreaddfunction kthread_bind_maskfunction kthreads_update_affinityfunction list_for_each_entryfunction kthreads_update_housekeepingfunction select_fallback_rqfunction kthreads_initfunction __kthread_init_workerfunction kthread_stopfunction __printffunction kthread_create_worker_on_nodefunction kthread_create_worker_on_cpufunction queuing_blockedfunction kthread_insert_work_sanity_checkfunction kthread_insert_work
Annotated Snippet
struct kthread {
unsigned long flags;
unsigned int cpu;
unsigned int node;
int started;
int result;
int (*threadfn)(void *);
void *data;
struct completion parked;
struct completion exited;
#ifdef CONFIG_BLK_CGROUP
struct cgroup_subsys_state *blkcg_css;
#endif
/* To store the full name if task comm is truncated. */
char *full_name;
struct task_struct *task;
struct list_head affinity_node;
struct cpumask *preferred_affinity;
};
enum KTHREAD_BITS {
KTHREAD_IS_PER_CPU = 0,
KTHREAD_SHOULD_STOP,
KTHREAD_SHOULD_PARK,
};
static inline struct kthread *to_kthread(struct task_struct *k)
{
WARN_ON(!(k->flags & PF_KTHREAD));
return k->worker_private;
}
void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk)
{
struct kthread *kthread = to_kthread(tsk);
if (!kthread || !kthread->full_name) {
strscpy(buf, tsk->comm, buf_size);
return;
}
strscpy_pad(buf, kthread->full_name, buf_size);
}
bool set_kthread_struct(struct task_struct *p)
{
struct kthread *kthread;
if (WARN_ON_ONCE(to_kthread(p)))
return false;
kthread = kzalloc_obj(*kthread);
if (!kthread)
return false;
init_completion(&kthread->exited);
init_completion(&kthread->parked);
INIT_LIST_HEAD(&kthread->affinity_node);
p->vfork_done = &kthread->exited;
kthread->task = p;
kthread->node = tsk_fork_get_node(current);
p->worker_private = kthread;
return true;
}
void free_kthread_struct(struct task_struct *k)
{
struct kthread *kthread;
/*
* Can be NULL if kmalloc() in set_kthread_struct() failed.
*/
kthread = to_kthread(k);
if (!kthread)
return;
#ifdef CONFIG_BLK_CGROUP
WARN_ON_ONCE(kthread->blkcg_css);
#endif
k->worker_private = NULL;
kfree(kthread->full_name);
kfree(kthread);
}
/**
* kthread_should_stop - should this kthread return now?
*
* When someone calls kthread_stop() on your kthread, it will be woken
* and this will return true. You should then return, and your return
Annotation
- Immediate include surface: `uapi/linux/sched/types.h`, `linux/mm.h`, `linux/mmu_context.h`, `linux/sched.h`, `linux/sched/mm.h`, `linux/sched/task.h`, `linux/kthread.h`, `linux/completion.h`.
- Detected declarations: `struct kthread_create_info`, `struct kthread`, `struct kthread_flush_work`, `enum KTHREAD_BITS`, `function get_kthread_comm`, `function set_kthread_struct`, `function free_kthread_struct`, `function kthread_stop`, `function __kthread_should_park`, `function kthread_park`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- Implementation status: integration implementation candidate.
- 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.