2000-10-24 03:33:24 +00:00
|
|
|
.\" Copyright (c) 2000
|
|
|
|
.\" The Regents of the University of California. All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
.\"
|
|
|
|
.\" $FreeBSD$
|
|
|
|
.\"
|
2000-11-13 20:34:20 +00:00
|
|
|
.Dd October 24, 2000
|
2000-10-24 03:33:24 +00:00
|
|
|
.Dt KTHREAD 9
|
2000-11-13 20:34:20 +00:00
|
|
|
.Os
|
2000-10-24 03:33:24 +00:00
|
|
|
.Sh NAME
|
2000-11-13 20:34:20 +00:00
|
|
|
.Nm kproc_start ,
|
2000-12-15 20:39:35 +00:00
|
|
|
.Nm kproc_shutdown ,
|
2000-10-24 03:33:24 +00:00
|
|
|
.Nm kthread_create ,
|
|
|
|
.Nm kthread_exit ,
|
2000-12-15 20:39:35 +00:00
|
|
|
.Nm kthread_resume ,
|
|
|
|
.Nm kthread_suspend ,
|
|
|
|
.Nm kthread_suspend_check
|
2000-10-24 03:33:24 +00:00
|
|
|
.Nd kernel threads
|
|
|
|
.Sh SYNOPSIS
|
|
|
|
.Fd #include <sys/kthread.h>
|
|
|
|
.Ft void
|
|
|
|
.Fn kproc_start "const void *udata"
|
2000-11-13 20:34:20 +00:00
|
|
|
.Ft void
|
2000-12-15 20:39:35 +00:00
|
|
|
.Fn kproc_shutdown "void *arg" "int howto"
|
2000-10-24 03:33:24 +00:00
|
|
|
.Ft int
|
2000-11-13 20:34:20 +00:00
|
|
|
.Fn kthread_create "void (*func)(void *)" "void *arg" "struct proc **newpp" "int flags" "const char *fmt" "..."
|
2000-10-24 03:33:24 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn kthread_exit "int ecode"
|
|
|
|
.Ft int
|
2000-12-15 20:39:35 +00:00
|
|
|
.Fn kthread_resume "struct proc *p"
|
2000-11-13 20:34:20 +00:00
|
|
|
.Ft int
|
2000-12-15 20:39:35 +00:00
|
|
|
.Fn kthread_suspend "struct proc *p" "int timo"
|
|
|
|
.Ft void
|
|
|
|
.Fn kthread_suspend_check "struct proc *p"
|
2000-10-24 03:33:24 +00:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
.Pp
|
|
|
|
The function
|
|
|
|
.Fn kproc_start
|
2000-11-13 20:34:20 +00:00
|
|
|
is used to start
|
|
|
|
.Dq internal
|
|
|
|
daemons such as bufdaemon, pagedaemon, vmdaemon, and the syncer and is intended
|
|
|
|
to be called from
|
|
|
|
.Xr SYSINIT 9 .
|
|
|
|
The
|
|
|
|
.Fa udata
|
|
|
|
argument is actually a pointer to a
|
|
|
|
.Li struct kproc_desc
|
|
|
|
which describes the kernel thread that should be created:
|
|
|
|
.Bd -literal -offset indent
|
|
|
|
struct kproc_desc {
|
|
|
|
char *arg0;
|
|
|
|
void (*func) __P((void));
|
|
|
|
struct proc **global_procpp;
|
|
|
|
};
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
The structure members are used by
|
|
|
|
.Fn kproc_start
|
|
|
|
as follows:
|
|
|
|
.Bl -tag -width "global_procpp" -offset indent
|
|
|
|
.It Va arg0
|
|
|
|
String to be used for the name of the process.
|
|
|
|
This string will be copied into the
|
|
|
|
.Va p_comm
|
|
|
|
member of the new process'
|
|
|
|
.Li struct proc .
|
|
|
|
.It Va func
|
|
|
|
The main function for this kernel process to run.
|
|
|
|
.It Va global_procpp
|
|
|
|
A pointer to a
|
|
|
|
.Li struct proc
|
|
|
|
pointer that should be updated to point to the newly created process' process
|
|
|
|
structure.
|
|
|
|
If this variable is
|
|
|
|
.Dv NULL ,
|
|
|
|
then it is ignored.
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
The
|
2000-10-24 03:33:24 +00:00
|
|
|
.Fn kthread_create
|
2000-11-13 20:34:20 +00:00
|
|
|
function is used to create a kernel thread.
|
|
|
|
The new thread shares its address space with process 0, the swapper process,
|
|
|
|
and runs in kernel mode only.
|
|
|
|
The
|
|
|
|
.Fa func
|
|
|
|
argument specifies the function that the thread should execute.
|
|
|
|
The
|
|
|
|
.Fa arg
|
|
|
|
argument is an arbitrary pointer that is passed in as the only argument to
|
|
|
|
.Fa func
|
|
|
|
when it is called by the new process.
|
|
|
|
The
|
|
|
|
.Fa newpp
|
|
|
|
pointer points to a
|
|
|
|
.Li struct proc
|
|
|
|
pointer that is to be updated to point to the newly created process.
|
|
|
|
If this argument is
|
|
|
|
.Dv NULL ,
|
|
|
|
then it is ignored.
|
|
|
|
The
|
|
|
|
.Fa flags
|
|
|
|
argument specifies a set of flags as described in
|
|
|
|
.Xr rfork 2 .
|
|
|
|
The rest of the arguments form a
|
|
|
|
.Xr printf 9
|
|
|
|
argument list that is used to build the name of the new thread and is stored
|
|
|
|
in the
|
|
|
|
.Va p_comm
|
|
|
|
member of the new thread's
|
|
|
|
.Li struct proc .
|
|
|
|
.Pp
|
|
|
|
The
|
2000-10-24 03:33:24 +00:00
|
|
|
.Fn kthread_exit
|
2000-11-13 20:34:20 +00:00
|
|
|
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.
|
|
|
|
The
|
|
|
|
.Fa ecode
|
|
|
|
argument specifies the exit status of the thread.
|
|
|
|
.Pp
|
|
|
|
The
|
2000-12-15 20:39:35 +00:00
|
|
|
.Fn kthread_resume ,
|
|
|
|
.Fn kthread_suspend ,
|
2000-11-13 20:34:20 +00:00
|
|
|
and
|
2000-12-15 20:39:35 +00:00
|
|
|
.Fn kthread_suspend_check
|
2000-11-13 20:34:20 +00:00
|
|
|
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
|
2000-12-15 20:39:35 +00:00
|
|
|
.Fn kthread_suspend_check
|
2000-11-13 20:34:20 +00:00
|
|
|
passing in
|
|
|
|
.Va curproc
|
|
|
|
as the only argument.
|
|
|
|
This function checks to see if the kernel thread has been asked to suspend.
|
|
|
|
If it has, it will
|
|
|
|
.Xr tsleep 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
|
|
|
|
.Fa p
|
|
|
|
argument points to the
|
|
|
|
.Li struct proc
|
|
|
|
of the kernel thread to suspend or resume.
|
|
|
|
For
|
2000-12-15 20:39:35 +00:00
|
|
|
.Fn kthread_suspend ,
|
2000-11-13 20:34:20 +00:00
|
|
|
the
|
|
|
|
.Fa timo
|
|
|
|
argument specifies a timeout to wait for the kernel thread to acknowledge the
|
|
|
|
suspend request and suspend itself.
|
|
|
|
.Pp
|
|
|
|
The
|
2000-12-15 20:39:35 +00:00
|
|
|
.Fn kproc_shutdown
|
2000-11-13 20:34:20 +00:00
|
|
|
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
|
2000-12-15 20:39:35 +00:00
|
|
|
.Fn kthread_suspend .
|
2000-11-13 20:34:20 +00:00
|
|
|
.Sh RETURN VALUES
|
|
|
|
The
|
|
|
|
.Fn kthread_create ,
|
2000-12-15 20:39:35 +00:00
|
|
|
.Fn kthread_resume ,
|
2000-11-13 20:34:20 +00:00
|
|
|
and
|
2000-12-15 20:39:35 +00:00
|
|
|
.Fn kthread_suspend
|
2000-11-13 20:34:20 +00:00
|
|
|
functions return zero on success and non-zero on failure.
|
|
|
|
Upon failure, the global variable
|
|
|
|
.Va errno
|
|
|
|
is set to indicate the error.
|
|
|
|
.Sh EXAMPLES
|
|
|
|
This example demonstrates the use of a
|
|
|
|
.Li struct kproc_desc
|
|
|
|
and the functions
|
|
|
|
.Fn kproc_start ,
|
2000-12-15 20:39:35 +00:00
|
|
|
.Fn kproc_shutdown ,
|
2000-10-24 03:33:24 +00:00
|
|
|
and
|
2000-12-15 20:39:35 +00:00
|
|
|
.Fn kthread_suspend_check
|
2000-11-13 20:34:20 +00:00
|
|
|
to run the
|
|
|
|
.Dq bufdaemon
|
|
|
|
process.
|
|
|
|
.Bd -literal -offset indent
|
|
|
|
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.
|
|
|
|
*/
|
2000-12-15 20:39:35 +00:00
|
|
|
EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown,
|
2000-11-13 20:34:20 +00:00
|
|
|
bufdaemonproc, SHUTDOWN_PRI_LAST);
|
|
|
|
...
|
|
|
|
for (;;) {
|
2000-12-15 20:39:35 +00:00
|
|
|
kthread_suspend_check(bufdaemonproc);
|
2000-11-13 20:34:20 +00:00
|
|
|
...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.Ed
|
|
|
|
.Sh ERRORS
|
|
|
|
The
|
2000-12-15 20:39:35 +00:00
|
|
|
.Fn kthread_resume
|
2000-11-13 20:34:20 +00:00
|
|
|
and
|
2000-12-15 20:39:35 +00:00
|
|
|
.Fn kthread_suspend
|
2000-11-13 20:34:20 +00:00
|
|
|
functions will fail if:
|
|
|
|
.Bl -tag -width Er
|
|
|
|
.It Bq Er EINVAL
|
|
|
|
The
|
|
|
|
.Fa p
|
|
|
|
argument does not reference a kernel thread.
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn kthread_create
|
|
|
|
function will fail if:
|
|
|
|
.Bl -tag -width Er
|
|
|
|
.It Bq Er EAGAIN
|
|
|
|
The system-imposed limit on the total
|
|
|
|
number of processes under execution would be exceeded.
|
|
|
|
The limit is given by the
|
|
|
|
.Xr sysctl 3
|
|
|
|
MIB variable
|
|
|
|
.Dv KERN_MAXPROC .
|
|
|
|
.It Bq Er EINVAL
|
|
|
|
The
|
|
|
|
.Dv RFCFDG
|
|
|
|
flag was specified in the
|
|
|
|
.Fa flags
|
|
|
|
parameter.
|
2000-12-29 09:18:45 +00:00
|
|
|
.El
|
2000-10-24 03:33:24 +00:00
|
|
|
.Sh SEE ALSO
|
2000-11-13 20:34:20 +00:00
|
|
|
.Xr SYSINIT 9 ,
|
|
|
|
.Xr rfork 2
|
|
|
|
.Sh HISTORY
|
|
|
|
The
|
|
|
|
.Fn kproc_start
|
|
|
|
function first appeared in
|
|
|
|
.Fx 2.2 .
|
|
|
|
The
|
2000-12-15 20:39:35 +00:00
|
|
|
.Fn kproc_shutdown ,
|
2000-11-13 20:34:20 +00:00
|
|
|
.Fn kthread_create ,
|
|
|
|
.Fn kthread_exit ,
|
2000-12-15 20:39:35 +00:00
|
|
|
.Fn kthread_resume ,
|
|
|
|
.Fn kthread_suspend ,
|
2000-11-13 20:34:20 +00:00
|
|
|
and
|
2000-12-15 20:39:35 +00:00
|
|
|
.Fn kthread_suspend_check
|
2000-11-13 20:34:20 +00:00
|
|
|
functions were introducted in
|
|
|
|
.Fx 4.0 .
|
2000-12-15 20:39:35 +00:00
|
|
|
Prior to
|
|
|
|
.Fx 5.0 ,
|
|
|
|
the
|
|
|
|
.Fn kproc_shutdown ,
|
|
|
|
.Fn kthread_resume ,
|
|
|
|
.Fn kthread_suspend ,
|
|
|
|
and
|
|
|
|
.Fn kthread_suspend_check
|
|
|
|
functions were named
|
|
|
|
.Fn shutdown_kproc ,
|
|
|
|
.Fn resume_kproc ,
|
|
|
|
.Fn shutdown_kproc ,
|
|
|
|
and
|
|
|
|
.Fn kproc_suspend_loop ,
|
|
|
|
respectively.
|
2000-11-13 20:34:20 +00:00
|
|
|
.Sh BUGS
|
|
|
|
If a kernel thread exits its main function (specified as the
|
|
|
|
.Fa func
|
|
|
|
argument to
|
|
|
|
.Fn kthread_create
|
|
|
|
or in the
|
|
|
|
.Li struct kproc_desc
|
|
|
|
passed to
|
|
|
|
.Fn kpoc_start )
|
|
|
|
then it will attempt to return backwards through the
|
|
|
|
.Fn fork1
|
|
|
|
trampoline code with unpredictable results.
|
|
|
|
The code should instead use a trampoline function to launch
|
|
|
|
.Fa func
|
|
|
|
and call
|
|
|
|
.Fn kthread_exit
|
|
|
|
if
|
|
|
|
.Fa func
|
|
|
|
returns.
|