KTHREAD(9)
β¨ KTHREAD(9) β¨ β kernel threads, bestie π§΅π
okay so this is the kernel developer's manual for the kthread_*(9) girlies. welcome to the group chat π
πΈ NAME πΈ
kthread_start, kthread_shutdown, kthread_add, kthread_exit, kthread_resume, kthread_suspend, kthread_suspend_check β kernel threads π§΅β¨
π SYNOPSIS π
#include <sys/kthread.h>
void
kthread_start(const void *udata);
void
kthread_shutdown(void *arg, int howto);
void
kthread_exit(void);
int
kthread_resume(struct thread *td);
int
kthread_suspend(struct thread *td, int timo);
void
kthread_suspend_check(void);
#include <sys/unistd.h>
int
kthread_add(void (*func)(void *), void *arg, struct proc *procp,
struct thread **newtdpp, int flags, int pages, const char *fmt,
...);
int
kproc_kthread_add(void (*func)(void *), void *arg,
struct proc **procptr, struct thread **tdptr, int flags, int pages,
char * procname, const char *fmt, ...);
π DESCRIPTION π
so back in FreeBSD 8.0, the older family of kthread_*(9) functions got a glow-up rebrand and became the kproc_*(9) family, because β and this is the tea β β they were previously misnamed and were actually out here producing kernel processes, not threads. messy of them fr fr π This new family of kthread_*(9) functions was added to produce real kernel threads. peep the kproc(9) man page for more info on the renamed calls. also ngl, the kproc_kthread_add(9) function shows up in both pages because its functionality is split between them π«Ά
The function kthread_start() is used to start "internal" daemons like bufdaemon, pagedaemon, vmdaemon, and the syncer, and it's intended to be called from SYSINIT(9). The udata argument is actually a pointer to a struct kthread_desc which describes the kernel thread that should be created π
struct kthread_desc {
char *arg0;
void (*func)(void);
struct thread **global_threadpp;
};
The structure members are used by kthread_start() as follows:
arg0β String to be used for the name of the thread. This string will be copied into thetd_namemember of the new threads'struct thread. β¨funcβ The main function for this kernel thread to run.global_threadppβ A pointer to astruct threadpointer that should be updated to point to the newly created thread's thread structure. If this variable isNULL, then it is ignored. The thread will be a subthread ofproc0(PID 0). π
The kthread_add() function is used to create a kernel thread. The new thread runs in kernel mode only. It is added to the process specified by the procp argument, or if that is NULL, to proc0. The func argument specifies the function that the thread should execute. The arg argument is an arbitrary pointer that is passed in as the only argument to func when it is called by the new thread. The newtdpp pointer points to a struct thread pointer that is to be updated to point to the newly created thread. If this argument is NULL, then it is ignored. The flags argument may be set to RFSTOPPED to leave the thread in a stopped state β and bestie, if you do that, the caller must call sched_add() to start the thread π«΅ no skipping this step. The pages argument specifies the size of the new kernel thread's stack in pages. If 0 is used, the default kernel stack size is allocated. The rest of the arguments form a printf(9) argument list that is used to build the name of the new thread, and it's stored in the td_name member of the new thread's struct thread. π
The kproc_kthread_add() function is much like the kthread_add() function above except that if the kproc does not already exist, it is created. This function is better documented in the kproc(9) manual page π
The kthread_exit() function is used to terminate kernel threads. It should be called by the main function of the kernel thread rather than letting the main function return to its caller. like, do not just walk away from your thread, that's rude π
The kthread_resume(), kthread_suspend(), and kthread_suspend_check() functions are used to suspend and resume a kernel thread. During the main loop of its execution, a kernel thread that wishes to allow itself to be suspended should call kthread_suspend_check() in order to check if the it has been asked to suspend. If it has, it will msleep(9) until it is told to resume. Once it has been told to resume it will return, allowing execution of the kernel thread to continue. The other two functions are used to notify a kernel thread of a suspend or resume request. The td argument points to the struct thread of the kernel thread to suspend or resume. For kthread_suspend(), the timo argument specifies a timeout to wait for the kernel thread to acknowledge the suspend request and suspend itself. β³
The kthread_shutdown() function is meant to be registered as a shutdown event for kernel threads that need to be suspended voluntarily during system shutdown so as not to interfere with system shutdown activities. The actual suspension of the kernel thread is done with kthread_suspend(). π
β RETURN VALUES β
The kthread_add(), kthread_resume(), and kthread_suspend() functions return zero on success and non-zero on failure. simple as that bestie β¨
π¦ EXAMPLES π¦
This example demonstrates the use of a struct kthread_desc and the functions kthread_start(), kthread_shutdown(), and kthread_suspend_check() to run the bufdaemon process. iconic behaviour honestly π
static struct thread *bufdaemonthread;
static struct kthread_desc buf_kp = {
"bufdaemon",
buf_daemon,
&bufdaemonthread
};
SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kthread_start,
&buf_kp)
static void
buf_daemon()
{
...
/*
* This process needs to be suspended prior to shutdown sync.
*/
EVENTHANDLER_REGISTER(shutdown_pre_sync, kthread_shutdown,
bufdaemonthread, SHUTDOWN_PRI_LAST);
...
for (;;) {
kthread_suspend_check();
...
}
}
π ERRORS π
The kthread_resume() and kthread_suspend() functions will fail if:
[EINVAL]β Thetdargument does not reference a kernel thread. not a thread bestie, try again π ββοΈ
The kthread_add() function will fail if:
[ENOMEM]β Memory for a thread's stack could not be allocated. the stack said no π
π SEE ALSO π
kproc(9), SYSINIT(9), wakeup(9)
π HISTORY π
The kthread_start() function first appeared in FreeBSD 2.2 where it created a whole process. It was converted to create threads in FreeBSD 8.0. The kthread_shutdown(), kthread_exit(), kthread_resume(), kthread_suspend(), and kthread_suspend_check() functions were introduced in FreeBSD 4.0 and were converted to threads in FreeBSD 8.0. The kthread_create() call was renamed to kthread_add() in FreeBSD 8.0. The old functionality of creating a kernel process was renamed to kproc_create(9). Prior to FreeBSD 5.0, the kthread_shutdown(), kthread_resume(), kthread_suspend(), and kthread_suspend_check() functions were named shutdown_kproc(), resume_kproc(), shutdown_kproc(), and kproc_suspend_loop(), respectively. the lore is real πβ¨
and that's the whole page bestie!! π this Debian build of the page is dated July 15, 2014, so go forth and spawn your kernel threads responsibly, mwah π«ΆππΈ
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2000-2001 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms