kthread_create_on_node(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’œ 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 until signal_pending(current) says stop. that's the assignment, no cap πŸ’…
  • data β€” a data pointer that gets handed straight to threadfn. 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 β€” a printf-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_stop on it, or
  • return when 'kthread_should_stop' is true β€” which means kthread_stop has 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:


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