kern_kthread.c revision 230984
1139804Simp/*-
248391Speter * Copyright (c) 1999 Peter Wemm <peter@FreeBSD.org>
348391Speter * All rights reserved.
448391Speter *
548391Speter * Redistribution and use in source and binary forms, with or without
648391Speter * modification, are permitted provided that the following conditions
748391Speter * are met:
848391Speter * 1. Redistributions of source code must retain the above copyright
948391Speter *    notice, this list of conditions and the following disclaimer.
1048391Speter * 2. Redistributions in binary form must reproduce the above copyright
1148391Speter *    notice, this list of conditions and the following disclaimer in the
1248391Speter *    documentation and/or other materials provided with the distribution.
1348391Speter *
1448391Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1548391Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1648391Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1748391Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1848391Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1948391Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2048391Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2148391Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2248391Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2348391Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2448391Speter * SUCH DAMAGE.
2548391Speter */
2648391Speter
27116182Sobrien#include <sys/cdefs.h>
28116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_kthread.c 230984 2012-02-04 16:49:29Z rstone $");
29116182Sobrien
3048391Speter#include <sys/param.h>
3148391Speter#include <sys/systm.h>
32230984Srstone#include <sys/cpuset.h>
3348391Speter#include <sys/kthread.h>
3470317Sjake#include <sys/lock.h>
3574927Sjhb#include <sys/mutex.h>
3674927Sjhb#include <sys/proc.h>
3755722Simp#include <sys/resourcevar.h>
38214238Sdavidxu#include <sys/rwlock.h>
3955539Sluoqi#include <sys/signalvar.h>
4074927Sjhb#include <sys/sx.h>
4148391Speter#include <sys/unistd.h>
4248391Speter#include <sys/wait.h>
43166188Sjeff#include <sys/sched.h>
44173004Sjulian#include <vm/vm.h>
45173004Sjulian#include <vm/vm_extern.h>
4648391Speter
4748391Speter#include <machine/stdarg.h>
4848391Speter
4948391Speter/*
5048391Speter * Start a kernel process.  This is called after a fork() call in
5148391Speter * mi_startup() in the file kern/init_main.c.
5248391Speter *
5348391Speter * This function is used to start "internal" daemons and intended
5448391Speter * to be called from SYSINIT().
5548391Speter */
5648391Spetervoid
5748391Speterkproc_start(udata)
5848391Speter	const void *udata;
5948391Speter{
6048391Speter	const struct kproc_desc	*kp = udata;
6148391Speter	int error;
6248391Speter
63172836Sjulian	error = kproc_create((void (*)(void *))kp->func, NULL,
64104354Sscottl		    kp->global_procpp, 0, 0, "%s", kp->arg0);
6548391Speter	if (error)
6648391Speter		panic("kproc_start: %s: error %d", kp->arg0, error);
6748391Speter}
6848391Speter
6948391Speter/*
7065557Sjasone * Create a kernel process/thread/whatever.  It shares its address space
7148391Speter * with proc0 - ie: kernel only.
7265557Sjasone *
7365557Sjasone * func is the function to start.
7465557Sjasone * arg is the parameter to pass to function on first startup.
7565557Sjasone * newpp is the return value pointing to the thread's struct proc.
7665557Sjasone * flags are flags to fork1 (in unistd.h)
7765557Sjasone * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.).
7848391Speter */
7948391Speterint
80172836Sjuliankproc_create(void (*func)(void *), void *arg,
81104354Sscottl    struct proc **newpp, int flags, int pages, const char *fmt, ...)
8248391Speter{
8348391Speter	int error;
8448391Speter	va_list ap;
85103216Sjulian	struct thread *td;
8648391Speter	struct proc *p2;
8748391Speter
88114434Sdes	if (!proc0.p_stats)
89172836Sjulian		panic("kproc_create called too soon");
9065557Sjasone
9190375Speter	error = fork1(&thread0, RFMEM | RFFDG | RFPROC | RFSTOPPED | flags,
92224987Sjonathan	    pages, &p2, NULL, 0);
9348391Speter	if (error)
9448391Speter		return error;
9548391Speter
9648391Speter	/* save a global descriptor, if desired */
9748391Speter	if (newpp != NULL)
9848391Speter		*newpp = p2;
9948391Speter
10048391Speter	/* this is a non-swapped system process */
10171559Sjhb	PROC_LOCK(p2);
102173004Sjulian	td = FIRST_THREAD_IN_PROC(p2);
10371559Sjhb	p2->p_flag |= P_SYSTEM | P_KTHREAD;
104173004Sjulian	td->td_pflags |= TDP_KTHREAD;
105114983Sjhb	mtx_lock(&p2->p_sigacts->ps_mtx);
106114983Sjhb	p2->p_sigacts->ps_flag |= PS_NOCLDWAIT;
107114983Sjhb	mtx_unlock(&p2->p_sigacts->ps_mtx);
10871559Sjhb	PROC_UNLOCK(p2);
10948391Speter
11048391Speter	/* set up arg0 for 'ps', et al */
11148391Speter	va_start(ap, fmt);
11248391Speter	vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
11348391Speter	va_end(ap);
114173004Sjulian	/* set up arg0 for 'ps', et al */
115173004Sjulian	va_start(ap, fmt);
116173004Sjulian	vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
117173004Sjulian	va_end(ap);
11848391Speter
11948391Speter	/* call the processes' main()... */
120103216Sjulian	cpu_set_fork_handler(td, func, arg);
121230984Srstone
122230984Srstone	/* Avoid inheriting affinity from a random parent. */
123230984Srstone	cpuset_setthread(td->td_tid, cpuset_root);
124217079Sjhb	thread_lock(td);
125103216Sjulian	TD_SET_CAN_RUN(td);
126217079Sjhb	sched_prio(td, PVM);
127217079Sjhb	sched_user_prio(td, PUSER);
12848391Speter
12969657Sjhb	/* Delay putting it on the run queue until now. */
130217079Sjhb	if (!(flags & RFSTOPPED))
131166188Sjeff		sched_add(td, SRQ_BORING);
132217079Sjhb	thread_unlock(td);
13369657Sjhb
13448391Speter	return 0;
13548391Speter}
13648391Speter
13748391Spetervoid
138172836Sjuliankproc_exit(int ecode)
13948391Speter{
14086293Speter	struct thread *td;
14186293Speter	struct proc *p;
14270317Sjake
14386293Speter	td = curthread;
14486293Speter	p = td->td_proc;
145155400Sjhb
146155400Sjhb	/*
147155400Sjhb	 * Reparent curthread from proc0 to init so that the zombie
148155400Sjhb	 * is harvested.
149155400Sjhb	 */
15074927Sjhb	sx_xlock(&proctree_lock);
15186292Sdillon	PROC_LOCK(p);
15286292Sdillon	proc_reparent(p, initproc);
15386292Sdillon	PROC_UNLOCK(p);
15474927Sjhb	sx_xunlock(&proctree_lock);
155155400Sjhb
156155400Sjhb	/*
157155400Sjhb	 * Wakeup anyone waiting for us to exit.
158155400Sjhb	 */
159155400Sjhb	wakeup(p);
160155400Sjhb
161155400Sjhb	/* Buh-bye! */
16286293Speter	exit1(td, W_EXITCODE(ecode, 0));
16348391Speter}
16448391Speter
16555539Sluoqi/*
16655539Sluoqi * Advise a kernel process to suspend (or resume) in its main loop.
16755539Sluoqi * Participation is voluntary.
16855539Sluoqi */
16955539Sluoqiint
170172836Sjuliankproc_suspend(struct proc *p, int timo)
17155539Sluoqi{
17255539Sluoqi	/*
17355539Sluoqi	 * Make sure this is indeed a system process and we can safely
174104306Sjmallett	 * use the p_siglist field.
17555539Sluoqi	 */
17673911Sjhb	PROC_LOCK(p);
17773911Sjhb	if ((p->p_flag & P_KTHREAD) == 0) {
17873911Sjhb		PROC_UNLOCK(p);
17955539Sluoqi		return (EINVAL);
18073911Sjhb	}
181104306Sjmallett	SIGADDSET(p->p_siglist, SIGSTOP);
18288160Speter	wakeup(p);
183173004Sjulian	return msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP, "suspkp", timo);
18455539Sluoqi}
18555539Sluoqi
18655539Sluoqiint
187172836Sjuliankproc_resume(struct proc *p)
18855539Sluoqi{
18955539Sluoqi	/*
19055539Sluoqi	 * Make sure this is indeed a system process and we can safely
19155539Sluoqi	 * use the p_siglist field.
19255539Sluoqi	 */
19373911Sjhb	PROC_LOCK(p);
19473911Sjhb	if ((p->p_flag & P_KTHREAD) == 0) {
19573911Sjhb		PROC_UNLOCK(p);
19655539Sluoqi		return (EINVAL);
19773911Sjhb	}
198104306Sjmallett	SIGDELSET(p->p_siglist, SIGSTOP);
19973911Sjhb	PROC_UNLOCK(p);
200104306Sjmallett	wakeup(&p->p_siglist);
20155539Sluoqi	return (0);
20255539Sluoqi}
20355539Sluoqi
20455539Sluoqivoid
205172836Sjuliankproc_suspend_check(struct proc *p)
20655539Sluoqi{
20773911Sjhb	PROC_LOCK(p);
208104306Sjmallett	while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
209104306Sjmallett		wakeup(&p->p_siglist);
210173004Sjulian		msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "kpsusp", 0);
21155539Sluoqi	}
21273911Sjhb	PROC_UNLOCK(p);
21355539Sluoqi}
214173004Sjulian
215173004Sjulian
216173004Sjulian/*
217173004Sjulian * Start a kernel thread.
218173004Sjulian *
219173004Sjulian * This function is used to start "internal" daemons and intended
220173004Sjulian * to be called from SYSINIT().
221173004Sjulian */
222173004Sjulian
223173004Sjulianvoid
224173004Sjuliankthread_start(udata)
225173004Sjulian	const void *udata;
226173004Sjulian{
227173004Sjulian	const struct kthread_desc	*kp = udata;
228173004Sjulian	int error;
229173004Sjulian
230173004Sjulian	error = kthread_add((void (*)(void *))kp->func, NULL,
231173004Sjulian		    NULL, kp->global_threadpp, 0, 0, "%s", kp->arg0);
232173004Sjulian	if (error)
233173004Sjulian		panic("kthread_start: %s: error %d", kp->arg0, error);
234173004Sjulian}
235173004Sjulian
236173004Sjulian/*
237173004Sjulian * Create a kernel thread.  It shares its address space
238173004Sjulian * with proc0 - ie: kernel only.
239173004Sjulian *
240173004Sjulian * func is the function to start.
241173004Sjulian * arg is the parameter to pass to function on first startup.
242173004Sjulian * newtdp is the return value pointing to the thread's struct thread.
243173004Sjulian *  ** XXX fix this --> flags are flags to fork1 (in unistd.h)
244173004Sjulian * fmt and following will be *printf'd into (*newtd)->td_name (for ps, etc.).
245173004Sjulian */
246173004Sjulianint
247173004Sjuliankthread_add(void (*func)(void *), void *arg, struct proc *p,
248173004Sjulian    struct thread **newtdp, int flags, int pages, const char *fmt, ...)
249173004Sjulian{
250173004Sjulian	va_list ap;
251173004Sjulian	struct thread *newtd, *oldtd;
252173004Sjulian
253173004Sjulian	if (!proc0.p_stats)
254173004Sjulian		panic("kthread_add called too soon");
255173004Sjulian
256173052Sjulian	/* If no process supplied, put it on proc0 */
257173004Sjulian	if (p == NULL) {
258173004Sjulian		p = &proc0;
259173004Sjulian		oldtd = &thread0;
260173004Sjulian	} else {
261173052Sjulian		oldtd = FIRST_THREAD_IN_PROC(p);
262173004Sjulian	}
263173004Sjulian
264173052Sjulian	/* Initialize our new td  */
265196730Skib	newtd = thread_alloc(pages);
266173004Sjulian	if (newtd == NULL)
267173004Sjulian		return (ENOMEM);
268173004Sjulian
269173004Sjulian	bzero(&newtd->td_startzero,
270173004Sjulian	    __rangeof(struct thread, td_startzero, td_endzero));
271173004Sjulian/* XXX check if we should zero. */
272173004Sjulian	bcopy(&oldtd->td_startcopy, &newtd->td_startcopy,
273173004Sjulian	    __rangeof(struct thread, td_startcopy, td_endcopy));
274173004Sjulian
275173004Sjulian	/* set up arg0 for 'ps', et al */
276173004Sjulian	va_start(ap, fmt);
277173004Sjulian	vsnprintf(newtd->td_name, sizeof(newtd->td_name), fmt, ap);
278173004Sjulian	va_end(ap);
279173004Sjulian
280173004Sjulian	newtd->td_proc = p;  /* needed for cpu_set_upcall */
281173004Sjulian
282173004Sjulian	/* XXX optimise this probably? */
283173004Sjulian	/* On x86 (and probably the others too) it is way too full of junk */
284173004Sjulian	/* Needs a better name */
285173004Sjulian	cpu_set_upcall(newtd, oldtd);
286173004Sjulian	/* put the designated function(arg) as the resume context */
287173004Sjulian	cpu_set_fork_handler(newtd, func, arg);
288173004Sjulian
289173004Sjulian	newtd->td_pflags |= TDP_KTHREAD;
290173004Sjulian	newtd->td_ucred = crhold(p->p_ucred);
291173004Sjulian
292173004Sjulian	/* this code almost the same as create_thread() in kern_thr.c */
293173004Sjulian	PROC_LOCK(p);
294173004Sjulian	p->p_flag |= P_HADTHREADS;
295173004Sjulian	newtd->td_sigmask = oldtd->td_sigmask; /* XXX dubious */
296173004Sjulian	thread_link(newtd, p);
297173004Sjulian	thread_lock(oldtd);
298173004Sjulian	/* let the scheduler know about these things. */
299173004Sjulian	sched_fork_thread(oldtd, newtd);
300173004Sjulian	TD_SET_CAN_RUN(newtd);
301173004Sjulian	thread_unlock(oldtd);
302173004Sjulian	PROC_UNLOCK(p);
303173004Sjulian
304213642Sdavidxu	tidhash_add(newtd);
305173004Sjulian
306230984Srstone	/* Avoid inheriting affinity from a random parent. */
307230984Srstone	cpuset_setthread(newtd->td_tid, cpuset_root);
308230984Srstone
309173004Sjulian	/* Delay putting it on the run queue until now. */
310173004Sjulian	if (!(flags & RFSTOPPED)) {
311173004Sjulian		thread_lock(newtd);
312173004Sjulian		sched_add(newtd, SRQ_BORING);
313173004Sjulian		thread_unlock(newtd);
314173004Sjulian	}
315173004Sjulian	if (newtdp)
316173004Sjulian		*newtdp = newtd;
317173004Sjulian	return 0;
318173004Sjulian}
319173004Sjulian
320173004Sjulianvoid
321173031Sjuliankthread_exit(void)
322173004Sjulian{
323173658Sjhb	struct proc *p;
324173658Sjhb
325204087Sattilio	p = curthread->td_proc;
326204087Sattilio
327173658Sjhb	/* A module may be waiting for us to exit. */
328173052Sjulian	wakeup(curthread);
329216921Sjhb
330216921Sjhb	/*
331216921Sjhb	 * The last exiting thread in a kernel process must tear down
332216921Sjhb	 * the whole process.
333216921Sjhb	 */
334214238Sdavidxu	rw_wlock(&tidhash_lock);
335204087Sattilio	PROC_LOCK(p);
336204089Sattilio	if (p->p_numthreads == 1) {
337204087Sattilio		PROC_UNLOCK(p);
338214238Sdavidxu		rw_wunlock(&tidhash_lock);
339204087Sattilio		kproc_exit(0);
340204087Sattilio	}
341214238Sdavidxu	LIST_REMOVE(curthread, td_hash);
342214238Sdavidxu	rw_wunlock(&tidhash_lock);
343173658Sjhb	PROC_SLOCK(p);
344173004Sjulian	thread_exit();
345173004Sjulian}
346173004Sjulian
347173004Sjulian/*
348173004Sjulian * Advise a kernel process to suspend (or resume) in its main loop.
349173004Sjulian * Participation is voluntary.
350173004Sjulian */
351173004Sjulianint
352173004Sjuliankthread_suspend(struct thread *td, int timo)
353173004Sjulian{
354202933Sattilio	struct proc *p;
355202933Sattilio
356202933Sattilio	p = td->td_proc;
357202933Sattilio
358202933Sattilio	/*
359204088Sattilio	 * td_pflags should not be read by any thread other than
360202933Sattilio	 * curthread, but as long as this flag is invariant during the
361204088Sattilio	 * thread's lifetime, it is OK to check its state.
362202933Sattilio	 */
363202933Sattilio	if ((td->td_pflags & TDP_KTHREAD) == 0)
364173004Sjulian		return (EINVAL);
365202933Sattilio
366202933Sattilio	/*
367202933Sattilio	 * The caller of the primitive should have already checked that the
368202933Sattilio	 * thread is up and running, thus not being blocked by other
369202933Sattilio	 * conditions.
370202933Sattilio	 */
371202933Sattilio	PROC_LOCK(p);
372173004Sjulian	thread_lock(td);
373173004Sjulian	td->td_flags |= TDF_KTH_SUSP;
374173004Sjulian	thread_unlock(td);
375202933Sattilio	return (msleep(&td->td_flags, &p->p_mtx, PPAUSE | PDROP, "suspkt",
376202933Sattilio	    timo));
377173004Sjulian}
378173004Sjulian
379173004Sjulian/*
380202933Sattilio * Resume a thread previously put asleep with kthread_suspend().
381173004Sjulian */
382173004Sjulianint
383173004Sjuliankthread_resume(struct thread *td)
384173004Sjulian{
385202933Sattilio	struct proc *p;
386202933Sattilio
387202933Sattilio	p = td->td_proc;
388202933Sattilio
389202933Sattilio	/*
390204088Sattilio	 * td_pflags should not be read by any thread other than
391202933Sattilio	 * curthread, but as long as this flag is invariant during the
392204088Sattilio	 * thread's lifetime, it is OK to check its state.
393202933Sattilio	 */
394202933Sattilio	if ((td->td_pflags & TDP_KTHREAD) == 0)
395173004Sjulian		return (EINVAL);
396202933Sattilio
397202933Sattilio	PROC_LOCK(p);
398173004Sjulian	thread_lock(td);
399173004Sjulian	td->td_flags &= ~TDF_KTH_SUSP;
400173004Sjulian	thread_unlock(td);
401202933Sattilio	wakeup(&td->td_flags);
402202933Sattilio	PROC_UNLOCK(p);
403173004Sjulian	return (0);
404173004Sjulian}
405173004Sjulian
406173004Sjulian/*
407173004Sjulian * Used by the thread to poll as to whether it should yield/sleep
408173004Sjulian * and notify the caller that is has happened.
409173004Sjulian */
410173004Sjulianvoid
411202933Sattiliokthread_suspend_check()
412173004Sjulian{
413202933Sattilio	struct proc *p;
414202933Sattilio	struct thread *td;
415202933Sattilio
416202933Sattilio	td = curthread;
417202933Sattilio	p = td->td_proc;
418202933Sattilio
419202933Sattilio	if ((td->td_pflags & TDP_KTHREAD) == 0)
420202933Sattilio		panic("%s: curthread is not a valid kthread", __func__);
421202933Sattilio
422202933Sattilio	/*
423202933Sattilio	 * As long as the double-lock protection is used when accessing the
424202933Sattilio	 * TDF_KTH_SUSP flag, synchronizing the read operation via proc mutex
425202933Sattilio	 * is fine.
426202933Sattilio	 */
427202933Sattilio	PROC_LOCK(p);
428173004Sjulian	while (td->td_flags & TDF_KTH_SUSP) {
429173004Sjulian		wakeup(&td->td_flags);
430202933Sattilio		msleep(&td->td_flags, &p->p_mtx, PPAUSE, "ktsusp", 0);
431173004Sjulian	}
432202933Sattilio	PROC_UNLOCK(p);
433173004Sjulian}
434173004Sjulian
435173004Sjulianint
436173004Sjuliankproc_kthread_add(void (*func)(void *), void *arg,
437173004Sjulian            struct proc **procptr, struct thread **tdptr,
438208390Sjhb            int flags, int pages, const char *procname, const char *fmt, ...)
439173004Sjulian{
440173004Sjulian	int error;
441173004Sjulian	va_list ap;
442173004Sjulian	char buf[100];
443173004Sjulian	struct thread *td;
444173004Sjulian
445173004Sjulian	if (*procptr == 0) {
446173004Sjulian		error = kproc_create(func, arg,
447173004Sjulian		    	procptr, flags, pages, "%s", procname);
448173004Sjulian		if (error)
449173004Sjulian			return (error);
450173004Sjulian		td = FIRST_THREAD_IN_PROC(*procptr);
451178682Sjulian		if (tdptr)
452178682Sjulian			*tdptr = td;
453173004Sjulian		va_start(ap, fmt);
454173004Sjulian		vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
455173004Sjulian		va_end(ap);
456173004Sjulian		return (0);
457173004Sjulian	}
458173004Sjulian	va_start(ap, fmt);
459173004Sjulian	vsnprintf(buf, sizeof(buf), fmt, ap);
460173004Sjulian	va_end(ap);
461173004Sjulian	error = kthread_add(func, arg, *procptr,
462173004Sjulian		    tdptr, flags, pages, "%s", buf);
463173004Sjulian	return (error);
464173004Sjulian}
465