]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_kthread.c
- Move softdep from using a global worklist to per-mount worklists. This
[FreeBSD/FreeBSD.git] / sys / kern / kern_kthread.c
1 /*-
2  * Copyright (c) 1999 Peter Wemm <peter@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kthread.h>
33 #include <sys/lock.h>
34 #include <sys/mutex.h>
35 #include <sys/proc.h>
36 #include <sys/resourcevar.h>
37 #include <sys/signalvar.h>
38 #include <sys/sx.h>
39 #include <sys/unistd.h>
40 #include <sys/wait.h>
41
42 #include <machine/stdarg.h>
43
44 /*
45  * Start a kernel process.  This is called after a fork() call in
46  * mi_startup() in the file kern/init_main.c.
47  *
48  * This function is used to start "internal" daemons and intended
49  * to be called from SYSINIT().
50  */
51 void
52 kproc_start(udata)
53         const void *udata;
54 {
55         const struct kproc_desc *kp = udata;
56         int error;
57
58         error = kthread_create((void (*)(void *))kp->func, NULL,
59                     kp->global_procpp, 0, 0, "%s", kp->arg0);
60         if (error)
61                 panic("kproc_start: %s: error %d", kp->arg0, error);
62 }
63
64 /*
65  * Create a kernel process/thread/whatever.  It shares its address space
66  * with proc0 - ie: kernel only.
67  *
68  * func is the function to start.
69  * arg is the parameter to pass to function on first startup.
70  * newpp is the return value pointing to the thread's struct proc.
71  * flags are flags to fork1 (in unistd.h)
72  * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.).
73  */
74 int
75 kthread_create(void (*func)(void *), void *arg,
76     struct proc **newpp, int flags, int pages, const char *fmt, ...)
77 {
78         int error;
79         va_list ap;
80         struct thread *td;
81         struct proc *p2;
82
83         if (!proc0.p_stats)
84                 panic("kthread_create called too soon");
85
86         error = fork1(&thread0, RFMEM | RFFDG | RFPROC | RFSTOPPED | flags,
87             pages, &p2);
88         if (error)
89                 return error;
90
91         /* save a global descriptor, if desired */
92         if (newpp != NULL)
93                 *newpp = p2;
94
95         /* this is a non-swapped system process */
96         PROC_LOCK(p2);
97         p2->p_flag |= P_SYSTEM | P_KTHREAD;
98         mtx_lock(&p2->p_sigacts->ps_mtx);
99         p2->p_sigacts->ps_flag |= PS_NOCLDWAIT;
100         mtx_unlock(&p2->p_sigacts->ps_mtx);
101         PROC_UNLOCK(p2);
102
103         /* set up arg0 for 'ps', et al */
104         va_start(ap, fmt);
105         vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
106         va_end(ap);
107
108         /* call the processes' main()... */
109         td = FIRST_THREAD_IN_PROC(p2);
110         cpu_set_fork_handler(td, func, arg);
111         TD_SET_CAN_RUN(td);
112
113         /* Delay putting it on the run queue until now. */
114         if (!(flags & RFSTOPPED)) {
115                 mtx_lock_spin(&sched_lock);
116                 setrunqueue(td, SRQ_BORING); 
117                 mtx_unlock_spin(&sched_lock);
118         }
119
120         return 0;
121 }
122
123 void
124 kthread_exit(int ecode)
125 {
126         struct thread *td;
127         struct proc *p;
128
129         td = curthread;
130         p = td->td_proc;
131
132         /*
133          * Reparent curthread from proc0 to init so that the zombie
134          * is harvested.
135          */
136         sx_xlock(&proctree_lock);
137         PROC_LOCK(p);
138         proc_reparent(p, initproc);
139         PROC_UNLOCK(p);
140         sx_xunlock(&proctree_lock);
141
142         /*
143          * Wakeup anyone waiting for us to exit.
144          */
145         wakeup(p);
146
147         /* Buh-bye! */
148         exit1(td, W_EXITCODE(ecode, 0));
149 }
150
151 /*
152  * Advise a kernel process to suspend (or resume) in its main loop.
153  * Participation is voluntary.
154  */
155 int
156 kthread_suspend(struct proc *p, int timo)
157 {
158         /*
159          * Make sure this is indeed a system process and we can safely
160          * use the p_siglist field.
161          */
162         PROC_LOCK(p);
163         if ((p->p_flag & P_KTHREAD) == 0) {
164                 PROC_UNLOCK(p);
165                 return (EINVAL);
166         }
167         SIGADDSET(p->p_siglist, SIGSTOP);
168         wakeup(p);
169         return msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP, "suspkt", timo);
170 }
171
172 int
173 kthread_resume(struct proc *p)
174 {
175         /*
176          * Make sure this is indeed a system process and we can safely
177          * use the p_siglist field.
178          */
179         PROC_LOCK(p);
180         if ((p->p_flag & P_KTHREAD) == 0) {
181                 PROC_UNLOCK(p);
182                 return (EINVAL);
183         }
184         SIGDELSET(p->p_siglist, SIGSTOP);
185         PROC_UNLOCK(p);
186         wakeup(&p->p_siglist);
187         return (0);
188 }
189
190 void
191 kthread_suspend_check(struct proc *p)
192 {
193         PROC_LOCK(p);
194         while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
195                 wakeup(&p->p_siglist);
196                 msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "ktsusp", 0);
197         }
198         PROC_UNLOCK(p);
199 }