kthread_create_on_node(9)
π kthread_create_on_node(9) π
okay so this is a page from the Kernel API Manual, and it's literally about spawning kernel threads. bestie, this is the lowkey main character of thread creation β let's get into it π§
β¨ NAME β¨
kthread_create_on_node β create a kthread. that's it. that's the whole vibe π«Ά
πΈ SYNOPSIS πΈ
struct task_struct * kthread_create_on_node (int (*threadfn)(void *data)
, void *data , int node , const char namefmt[] , ... );
π ARGUMENTS π
threadfnβ the function your thread is gonna run, and it keeps running untilsignal_pending(current)says stop. that's the assignment, no cap πdataβ a data pointer that gets handed straight tothreadfn. it's the little gift bag your function gets at the door πnodeβ the task and thread structures for this thread get allocated on this node. NUMA girlies stay winning β¨namefmtβ aprintf-style name for the thread. give it a cute name, it deserves one π...β variable arguments. the little...doing its thing, iykyk
π¦ DESCRIPTION π¦
so this helper function creates and names a kernel thread. iconic behavior honestly π
heads up though: the thread will be stopped when it's created. like, it does not go anywhere until you hit it with wake_up_process to actually start it. don't be out here wondering why nothing's happening, bestie π
see also kthread_run, which is the other one you'll probably meet.
the new thread comes out the box with SCHED_NORMAL policy and is affine to all CPUs. all of them. it's very social π
if the thread is gonna be bound to a particular cpu, pass its node in node so you get NUMA affinity for the kthread stack β or else just give NUMA_NO_NODE. pick one, don't be weird about it π
when it gets woken, the thread runs threadfn with data as its argument. and here's the part everyone asks about: threadfn can either
- return directly, if it's a standalone thread and nobody's ever gonna call
kthread_stopon it, or - return when
'kthread_should_stop'is true β which meanskthread_stophas been called π«Ά
the return value should be zero or a negative error number, and it will be passed to kthread_stop. so like, keep it clean β¨
returns a task_struct or ERR_PTR(-ENOMEM) or ERR_PTR(-EINTR). those last two are the "it didn't work out" ones, no drama π
π₯ SEE ALSO π₯
Kernel file ./kernel/kthread.c β and honestly the whole kthread friend group, they're all here:
kthread_should_stop(9)kthread_should_park(9)kthread_freezable_should_stop(9)kthread_func(9)kthread_data(9)kthread_probe_data(9)kthread_complete_and_exit(9)kthread_bind(9)kthread_create_on_cpu(9)kthread_unpark(9)kthread_park(9)kthread_stop(9)kthread_stop_put(9)kthread_affine_preferred(9)kthreads_update_housekeeping(9)kthread_worker_fn(9)kthread_create_worker_on_node(9)kthread_create_worker_on_cpu(9)kthread_queue_work(9)kthread_delayed_work_timer_fn(9)kthread_queue_delayed_work(9)kthread_flush_work(9)kthread_mod_delayed_work(9)kthread_cancel_work_sync(9)kthread_cancel_delayed_work_sync(9)kthread_flush_worker(9)kthread_destroy_worker(9)kthread_use_mm(9)kthread_unuse_mm(9)kthread_associate_blkcg(9)kthread_blkcg(9)
and that's the page, bestie!! this one lives in the source tree at ./kernel and the version/date stamp on it says October 2026 β so yeah, it's giving very "from the future" energy πβ¨ go forth and create threads responsibly π§π
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms