1*44506434Swiz.\" $NetBSD: kthread.9,v 1.30 2020/08/01 09:50:42 wiz Exp $ 28752c231Sgmcgarry.\" 3a9743c24Sad.\" Copyright (c) 2000, 2007, 2008 The NetBSD Foundation, Inc. 48752c231Sgmcgarry.\" All rights reserved. 58752c231Sgmcgarry.\" 68752c231Sgmcgarry.\" This code is derived from software contributed to The NetBSD Foundation 758af9c20Sad.\" by Gregory McGarry, and by Andrew Doran. 88752c231Sgmcgarry.\" 98752c231Sgmcgarry.\" Redistribution and use in source and binary forms, with or without 108752c231Sgmcgarry.\" modification, are permitted provided that the following conditions 118752c231Sgmcgarry.\" are met: 128752c231Sgmcgarry.\" 1. Redistributions of source code must retain the above copyright 138752c231Sgmcgarry.\" notice, this list of conditions and the following disclaimer. 148752c231Sgmcgarry.\" 2. Redistributions in binary form must reproduce the above copyright 158752c231Sgmcgarry.\" notice, this list of conditions and the following disclaimer in the 168752c231Sgmcgarry.\" documentation and/or other materials provided with the distribution. 178752c231Sgmcgarry.\" 188752c231Sgmcgarry.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 198752c231Sgmcgarry.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 208752c231Sgmcgarry.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 218752c231Sgmcgarry.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 228752c231Sgmcgarry.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 238752c231Sgmcgarry.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 248752c231Sgmcgarry.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 258752c231Sgmcgarry.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 268752c231Sgmcgarry.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 278752c231Sgmcgarry.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 288752c231Sgmcgarry.\" POSSIBILITY OF SUCH DAMAGE. 298752c231Sgmcgarry.\" 30022fe450Spooka.Dd April 21, 2015 318752c231Sgmcgarry.Dt KTHREAD 9 328752c231Sgmcgarry.Os 338752c231Sgmcgarry.Sh NAME 3458af9c20Sad.Nm kthread_create , 35cf025cacSwiz.Nm kthread_exit , 367c54d842Sriastradh.Nm kthread_join , 377c54d842Sriastradh.Nm kthread_fpu_enter , 387c54d842Sriastradh.Nm kthread_fpu_exit 398752c231Sgmcgarry.Nd kernel threads 408752c231Sgmcgarry.Sh SYNOPSIS 41472351e1Swiz.In sys/kthread.h 4258af9c20Sad.Ft int 4358af9c20Sad.Fn kthread_create "pri_t pri" "int flags" "struct cpu_info *ci" \ 4458af9c20Sad"void (*func)(void *)" "void *arg" "lwp_t **newlp" "const char *fmt" "..." 458752c231Sgmcgarry.Ft void 468752c231Sgmcgarry.Fn kthread_exit "int ecode" 47a5d24f20Sjruoho.Ft int 48a5d24f20Sjruoho.Fn kthread_join "lwp_t *l" 497c54d842Sriastradh.Ft int 507c54d842Sriastradh.Fn kthread_fpu_enter 517c54d842Sriastradh.Ft void 527c54d842Sriastradh.Fn kthread_fpu_exit "int s" 538752c231Sgmcgarry.Sh DESCRIPTION 548752c231SgmcgarryKernel threads are light-weight processes which execute entirely 55770eef21Swizwithin the kernel. 5658af9c20Sad.Pp 57770eef21SwizAny process can request the creation of a new kernel thread. 58770eef21SwizKernel threads are not swapped out during memory congestion. 59770eef21SwizThe VM space and limits are shared with proc0 (usually swapper). 607c54d842Sriastradh.Pp 617c54d842SriastradhIf the machine has any per-CPU floating-point units or SIMD vector 627c54d842Sriastradhunits that are normally available to user threads, they can be used by 637c54d842Sriastradhkthreads between 647c54d842Sriastradh.Fn kthread_fpu_enter 657c54d842Sriastradhand 66*44506434Swiz.Fn kthread_fpu_exit . 678752c231Sgmcgarry.Sh FUNCTIONS 688752c231Sgmcgarry.Bl -tag -width compact 6958af9c20Sad.It Fn kthread_create "pri" "flags" "ci" "func" "arg" "newlp" "fmt" "..." 7058af9c20SadCreate a kernel thread. 7158af9c20SadThe arguments are as follows. 7258af9c20Sad.Bl -tag -width compact 7358af9c20Sad.It Fa pri 7458af9c20SadPriority level for the thread. 7558af9c20SadIf no priority level is desired specify 7658af9c20Sad.Dv PRI_NONE , 7758af9c20Sadcausing 7858af9c20Sad.Fn kthread_create 7958af9c20Sadto select the default priority level. 8058af9c20Sad.It Fa flags 8158af9c20SadFlags that can be logically ORed together to alter the thread's behaviour. 82a5d24f20Sjruoho.It Fa ci 83a5d24f20SjruohoIf 84a5d24f20Sjruoho.No non- Ns Dv NULL , 85a5d24f20Sjruohothe thread will be created bound to the CPU specified by 86a5d24f20Sjruoho.Fa ci , 87a5d24f20Sjruohomeaning that it will only ever execute on that CPU. 88a5d24f20SjruohoBy default, the threads are free to execute on any CPU in the system. 89a5d24f20Sjruoho.It Fa func 90a5d24f20SjruohoA function to be called when the thread begins executing. 91a5d24f20SjruohoThis function must not return. 92a5d24f20SjruohoIf the thread runs to completion, it must call 93a5d24f20Sjruoho.Fn kthread_exit 94a5d24f20Sjruohoto properly terminate itself. 95a5d24f20Sjruoho.It Fa arg 96a5d24f20SjruohoAn argument to be passed to 97a5d24f20Sjruoho.Fn func . 98a5d24f20SjruohoMay be 99a5d24f20Sjruoho.Dv NULL 100a5d24f20Sjruohoif not required. 101a5d24f20Sjruoho.It Fa newlp 10295ea6d26SrmindA pointer to receive the new LWP structure for the kernel thread. 10395ea6d26SrmindMay be 10495ea6d26Srmind.Dv NULL , 10595ea6d26Srmindunless 106a0ffc02aSrmind.Dv KTHREAD_MUSTJOIN 107a5d24f20Sjruohois specified in 10895ea6d26Srmind.Fa flags . 109a5d24f20Sjruoho.It Fa fmt 110a5d24f20SjruohoA string containing format information used to display the kernel 111a5d24f20Sjruohothread name. 112a5d24f20SjruohoMust not be 113a5d24f20Sjruoho.Dv NULL . 114a5d24f20Sjruoho.El 115a5d24f20Sjruoho.Pp 116a5d24f20SjruohoThe following 117a5d24f20Sjruoho.Va flags 118a5d24f20Sjruohoare defined. 119a0ffc02aSrmind.Bl -tag -width KTHREAD_MUSTJOIN 120cf025cacSwiz.It Dv KTHREAD_IDLE 121cf025cacSwizCauses the thread to be created in the 12258af9c20Sad.Dv LSIDL 12358af9c20Sad(idle) state. 12458af9c20SadBy default, the threads are created in the 12558af9c20Sad.Dv LSRUN 12658af9c20Sad(runnable) state, meaning they will begin execution shortly after creation. 127cf025cacSwiz.It Dv KTHREAD_MPSAFE 12858af9c20SadSpecifies that the thread does its own locking and so is multiprocessor safe. 12958af9c20SadIf not specified, the global kernel lock will be held whenever the thread is 13058af9c20Sadrunning (unless explicitly dropped by the thread). 131cf025cacSwiz.It Dv KTHREAD_INTR 13258af9c20SadSpecifies that the thread services device interrupts. 13358af9c20SadThis flag is intended for kernel internal use and should not normally be 13458af9c20Sadspecified. 135cf025cacSwiz.It Dv KTHREAD_TS 136ee30bd33SwizCauses the kthread to be created in the 137ee30bd33Swiz.Dv SCHED_OTHER 138ee30bd33Swizclass (timeshared). 139ee4a63b0SwizThe thread's priority will be dynamically adjusted by the scheduler. 140a9743c24SadIncreased activity by the kthread will cause its priority to fall; 141a9743c24Saddecreased activity will cause its priority to rise. 142ee30bd33SwizBy default, kthreads are created in the 143ee30bd33Swiz.Dv SCHED_RR 144ee30bd33Swizclass, with a fixed 145a9743c24Sadpriority specified by 146a9743c24Sad.Ar pri . 147ee30bd33SwizThreads in the 148ee30bd33Swiz.Dv SCHED_RR 149ee30bd33Swizclass do not have their priority dynamically 150a9743c24Sadadjusted by the scheduler. 151a0ffc02aSrmind.It Dv KTHREAD_MUSTJOIN 152a0ffc02aSrmindIndicates that created kthread must be joined. 15395ea6d26SrmindIn such case 15495ea6d26Srmind.Fn kthread_exit 15595ea6d26Srmindwill wait until 156a5d24f20Sjruoho.Fn kthread_join 15795ea6d26Srmindwill be called. 15858af9c20Sad.El 15958af9c20Sad.It Fn kthread_exit "ecode" 1608752c231SgmcgarryExit from a kernel thread. 16158af9c20SadMust only be called by a kernel thread. 162f4b66b25Shaad.It Fn kthread_join "l" 16395ea6d26SrmindSuspend execution of calling thread until the target kthread terminates. 164a5d24f20SjruohoConceptually the function can be compared to the user space 16595ea6d26Srmind.Xr pthread_join 3 , 166ee4a63b0Swizhowever it must be called only once for kernel thread which was 167ee4a63b0Swizcreated using the 168a0ffc02aSrmind.Dv KTHREAD_MUSTJOIN 16995ea6d26Srmindflag and would wait on 17095ea6d26Srmind.Fa kthread_exit . 1717c54d842Sriastradh.It Fn kthread_fpu_enter 1727c54d842SriastradhAllow the current kthread to use any machine-dependent per-CPU 1737c54d842Sriastradhfloating-point units or SIMD vector units normally available to user 1747c54d842Sriastradhthreads. 1757c54d842SriastradhReturns a cookie that must be passed to 1767c54d842Sriastradh.Fn kthread_fpu_exit 1777c54d842Sriastradhwhen done. 1787c54d842Sriastradh.Pp 1797c54d842SriastradhMatching pairs of 1807c54d842Sriastradh.Fn kthread_fpu_enter 1817c54d842Sriastradhand 1827c54d842Sriastradh.Fn kthread_fpu_exit 1837c54d842Sriastradhmay be nested. 1847c54d842Sriastradh.It Fn kthread_fpu_exit "s" 1857c54d842SriastradhRestore the current kthread's access to machine-dependent per-CPU 1867c54d842Sriastradhfloating-point units or SIMD vector units to what it was before the 1877c54d842Sriastradhcall to 1887c54d842Sriastradh.Fn kthread_fpu_enter 1897c54d842Sriastradhthat returned 1907c54d842Sriastradh.Fa s . 1917c54d842Sriastradh.Pp 1927c54d842SriastradhOn the last 1937c54d842Sriastradh.Fn kthread_fpu_exit , 1947c54d842Sriastradhzero all the units' registers to avoid leaking secrets \(em such units 1957c54d842Sriastradhare often used for cryptography. 1968752c231Sgmcgarry.El 1978752c231Sgmcgarry.Sh RETURN VALUES 1988752c231SgmcgarryUpon successful completion, 199efb07627Sxtraeme.Fn kthread_create 200770eef21Swizreturns 0. 201770eef21SwizOtherwise, the following error values are returned: 2028752c231Sgmcgarry.Bl -tag -width [EAGAIN] 2038752c231Sgmcgarry.It Bq Er EAGAIN 204*44506434SwizThe limit on the total number of system processes would be exceeded; 205*44506434Swizor the limit 2068752c231Sgmcgarry.Dv RLIMIT_NPROC 2078752c231Sgmcgarryon the total number of processes under execution by this 2088752c231Sgmcgarryuser id would be exceeded. 2098752c231Sgmcgarry.El 2108752c231Sgmcgarry.Sh CODE REFERENCES 2118752c231SgmcgarryThe kthread framework itself is implemented within the file 2128752c231Sgmcgarry.Pa sys/kern/kern_kthread.c . 2138752c231SgmcgarryData structures and function prototypes for the framework are located in 2148752c231Sgmcgarry.Pa sys/sys/kthread.h . 2158752c231Sgmcgarry.Sh SEE ALSO 2163def089fSrmind.Xr condvar 9 , 2173def089fSrmind.Xr driver 9 , 2183def089fSrmind.Xr softint 9 , 2193def089fSrmind.Xr workqueue 9 2208752c231Sgmcgarry.Sh HISTORY 2218752c231SgmcgarryThe kthread framework appeared in 2228752c231Sgmcgarry.Nx 1.4 . 223