KPROC(9)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 14.5, freebsd 15.1

πŸ’… KPROC(9) β€” kernel processes, bestie edition 🧚

okay so this is from the Kernel Developer's Manual and it's literally the whole kproc_* family: kproc_start, kproc_shutdown, kproc_create, kproc_exit, kproc_resume, kproc_suspend, kproc_suspend_check. executive dysfunction? no. executive function? yes. let's go πŸŽ€


✨ SYNOPSIS ✨

#include <sys/kthread.h>

void
kproc_start(const void *udata);

void
kproc_shutdown(void *arg, int howto);

int
kproc_create(void (*func)(void *), void *arg, struct proc **newpp,
    int flags, int pages, const char *fmt, ...);

void
kproc_exit(int ecode);

int
kproc_resume(struct proc *p);

int
kproc_suspend(struct proc *p, int timo);

void
kproc_suspend_check(struct proc *p);

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 🌸

history time, and it's giving glow-up era πŸ’–: back in FreeBSD 8.0 the kthread*(9) family of functions got renamed to the kproc*(9) family, because they were misnamed and were, in fact, producing kernel processes the whole time. embarrassing but we move. so a whole new family of different kthread_*(9) functions was added to produce actual real kernel threads β€” peep the kthread(9) man page for the tea on those calls. also note πŸ“Œ that kproc_kthread_add(9) shows up on both pages, because its functionality is split between them. messy? a little. iconic? absolutely.


🫢 kproc_start() β€” the "internal daemons" girly

The function kproc_start() is used to start "internal" daemons such as 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 kproc_desc which describes the kernel process that should be created:

struct kproc_desc {
        char            *arg0;
        void            (*func)(void);
        struct proc     **global_procpp;
};

The structure members are used by kproc_start() as follows:

  • arg0 β€” string to be used for the name of the process. This string will be copied into the p_comm member of the new process' struct proc.
  • func β€” the main function for this kernel process to run.
  • global_procpp β€” a pointer to a struct proc pointer that should be updated to point to the newly created process' process structure. If this variable is NULL, then it is ignored. πŸ«₯

πŸ’œ kproc_create() β€” birth a whole process

The kproc_create() function is used to create a kernel process. The new process shares its address space with process 0, the swapper process, and runs in kernel mode only (no cap, it's built different). Let's break the args down, bestie:

  • func β€” specifies the function that the process should execute.
  • arg β€” an arbitrary pointer that is passed in as the only argument to func when it is called by the new process.
  • newpp β€” points to a struct proc pointer that is to be updated to point to the newly created process. If this argument is NULL, then it is ignored.
  • flags β€” specifies a set of flags as described in rfork(2).
  • pages β€” specifies the size of the new kernel process'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 process and is stored in the p_comm member of the new process's struct proc.

πŸ¦‹ kproc_exit() β€” say bye gracefully

The kproc_exit() function is used to terminate kernel processes. It should be called by the main function of the kernel process rather than letting the main function return to its caller (do not just dip, use the door πŸšͺ✨). The ecode argument specifies the exit status of the process. While exiting, the function exit1(9) will initiate a call to wakeup(9) on the process handle.


πŸ’… suspend / resume squad

The kproc_resume(), kproc_suspend(), and kproc_suspend_check() functions are used to suspend and resume a kernel process. here's the choreography:

During the main loop of its execution, a kernel process that wishes to allow itself to be suspended should call kproc_suspend_check() passing in curproc as the only argument. This function checks to see if the kernel process has been asked to suspend. If it has, it will tsleep(9) until it is told to resume. Once it has been told to resume it will return, allowing execution of the kernel process to continue. ✨

The other two functions are used to notify a kernel process of a suspend or resume request:

  • p β€” points to the struct proc of the kernel process to suspend or resume.
  • timo (for kproc_suspend()) β€” specifies a timeout to wait for the kernel process to acknowledge the suspend request and suspend itself.

πŸ“ kproc_shutdown() β€” the polite one

The kproc_shutdown() function is meant to be registered as a shutdown event for kernel processes that need to be suspended voluntarily during system shutdown so as not to interfere with system shutdown activities. The actual suspension of the kernel process is done with kproc_suspend(). teamwork makes the dream work fr fr 🀝


🌈 kproc_kthread_add() β€” the "already exists? cool" one

The kproc_kthread_add() function is much like the kproc_create() function above, except that if the kproc already exists, then only a new thread (see kthread(9)) is created on the existing process. okay, arg-by-arg:

  • func β€” specifies the function that the process should execute.
  • arg β€” an arbitrary pointer that is passed in as the only argument to func when it is called by the new process.
  • procptr β€” points to a struct proc pointer that is the location to be updated with the new proc pointer if a new process is created, or if not NULL, must contain the process pointer for the already existing process. If this argument points to NULL, then a new process is created and the field updated.
  • tdptr β€” if not NULL, points to a struct thread pointer that is the location to be updated with the new thread pointer.
  • flags β€” specifies a set of flags as described in rfork(2).
  • pages β€” specifies the size of the new kernel thread's stack in pages. If 0 is used, the default kernel stack size is allocated.
  • procname β€” the name the new process should be given if it needs to be created. It is NOT a printf style format specifier but a simple string. ⚠️ read that twice.
  • the rest of the arguments β€” form a printf(9) argument list that is used to build the name of the new thread and is stored in the td_name member of the new thread's struct thread.

πŸ‘‘ RETURN VALUES πŸ‘‘

The kproc_create(), kproc_resume(), and kproc_suspend() functions return zero on success and non-zero on failure. classic. 🎯


🧚 EXAMPLES 🧚

This example demonstrates the use of a struct kproc_desc and the functions kproc_start(), kproc_shutdown(), and kproc_suspend_check() to run the bufdaemon process. she's the blueprint, lowkey:

static struct proc *bufdaemonproc;

static struct kproc_desc buf_kp = {
        "bufdaemon",
        buf_daemon,
        &bufdaemonproc
};
SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kproc_start,
    &buf_kp)

static void
buf_daemon()
{
        ...
        /*
         * This process needs to be suspended prior to shutdown sync.
         */
        EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown,
            bufdaemonproc, SHUTDOWN_PRI_LAST);
        ...
        for (;;) {
                kproc_suspend_check(bufdaemonproc);
                ...
        }
}

πŸ’€ ERRORS πŸ’€

The kproc_resume() and kproc_suspend() functions will fail if:

  • [EINVAL] β€” the p argument does not reference a kernel process. 🚩 not a kernel process, not invited.

The kproc_create() function will fail if:

  • [EAGAIN] β€” the system-imposed limit on the total number of processes under execution would be exceeded. The limit is given by the sysctl(3) MIB variable KERN_MAXPROC.
  • [EINVAL] β€” the RFCFDG flag was specified in the flags parameter.
ngl, if you're hitting EAGAIN you're kinda doing too much. no shade. ok maybe a little shade. πŸ’…

πŸ”₯ SEE ALSO πŸ”₯

rfork(2), exit1(9), kthread(9), SYSINIT(9), wakeup(9)

go read them, iykyk πŸ“š


⭐ HISTORY ⭐

The kproc_start() function first appeared in FreeBSD 2.2. The kproc_shutdown(), kproc_create(), kproc_exit(), kproc_resume(), kproc_suspend(), and kproc_suspend_check() functions were introduced in FreeBSD 4.0.

Prior to FreeBSD 5.0, the kproc_shutdown(), kproc_resume(), kproc_suspend(), and kproc_suspend_check() functions were named shutdown_kproc(), resume_kproc(), shutdown_kproc(), and kproc_suspend_loop(), respectively (yes, shutdown_kproc() is listed twice in the original and we are NOT fixing it, that's canon 🫢). Originally they had the names kthread_*() but were changed to kproc_*() when real kthreads became available. rebrand era πŸ’«


and that's the whole page, bestie!! πŸŽ€ this one's from the Debian build, dated October 19, 2007 β€” practically vintage, an antique, a little historical artifact. go forth and spawn some kernel processes responsibly. mwah πŸ’–

πŸ“¦ where this page comes from