Home | History | Annotate | Line # | Download | only in kern
kern_kthread.c revision 1.45
      1 /*	$NetBSD: kern_kthread.c,v 1.45 2020/01/08 17:38:42 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999, 2007, 2009, 2019 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center, and by Andrew Doran.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: kern_kthread.c,v 1.45 2020/01/08 17:38:42 ad Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/kthread.h>
     40 #include <sys/mutex.h>
     41 #include <sys/sched.h>
     42 #include <sys/kmem.h>
     43 
     44 #include <uvm/uvm_extern.h>
     45 
     46 static lwp_t *		kthread_jtarget;
     47 static kmutex_t		kthread_lock;
     48 static kcondvar_t	kthread_cv;
     49 
     50 void
     51 kthread_sysinit(void)
     52 {
     53 
     54 	mutex_init(&kthread_lock, MUTEX_DEFAULT, IPL_NONE);
     55 	cv_init(&kthread_cv, "kthrwait");
     56 	kthread_jtarget = NULL;
     57 }
     58 
     59 /*
     60  * kthread_create: create a kernel thread, that is, system-only LWP.
     61  */
     62 int
     63 kthread_create(pri_t pri, int flag, struct cpu_info *ci,
     64     void (*func)(void *), void *arg, lwp_t **lp, const char *fmt, ...)
     65 {
     66 	lwp_t *l;
     67 	vaddr_t uaddr;
     68 	int error, lc;
     69 	va_list ap;
     70 
     71 	KASSERT((flag & KTHREAD_INTR) == 0 || (flag & KTHREAD_MPSAFE) != 0);
     72 
     73 	uaddr = uvm_uarea_system_alloc(
     74 	   (flag & (KTHREAD_INTR|KTHREAD_IDLE)) == KTHREAD_IDLE ? ci : NULL);
     75 	if (uaddr == 0) {
     76 		return ENOMEM;
     77 	}
     78 	if ((flag & KTHREAD_TS) != 0) {
     79 		lc = SCHED_OTHER;
     80 	} else {
     81 		lc = SCHED_RR;
     82 	}
     83 
     84 	error = lwp_create(&lwp0, &proc0, uaddr, LWP_DETACHED, NULL,
     85 	    0, func, arg, &l, lc, &lwp0.l_sigmask, &lwp0.l_sigstk);
     86 	if (error) {
     87 		uvm_uarea_system_free(uaddr);
     88 		return error;
     89 	}
     90 	if (fmt != NULL) {
     91 		l->l_name = kmem_alloc(MAXCOMLEN, KM_SLEEP);
     92 		va_start(ap, fmt);
     93 		vsnprintf(l->l_name, MAXCOMLEN, fmt, ap);
     94 		va_end(ap);
     95 	}
     96 
     97 	/*
     98 	 * Set parameters.
     99 	 */
    100 	if (pri == PRI_NONE) {
    101 		if ((flag & KTHREAD_TS) != 0) {
    102 			/* Maximum user priority level. */
    103 			pri = MAXPRI_USER;
    104 		} else {
    105 			/* Minimum kernel priority level. */
    106 			pri = PRI_KTHREAD;
    107 		}
    108 	}
    109 	mutex_enter(proc0.p_lock);
    110 	lwp_lock(l);
    111 	lwp_changepri(l, pri);
    112 	if (ci != NULL) {
    113 		if (ci != l->l_cpu) {
    114 			lwp_unlock_to(l, ci->ci_schedstate.spc_lwplock);
    115 			lwp_lock(l);
    116 		}
    117 		l->l_pflag |= LP_BOUND;
    118 		l->l_cpu = ci;
    119 	}
    120 
    121 	if ((flag & KTHREAD_MUSTJOIN) != 0) {
    122 		KASSERT(lp != NULL);
    123 		l->l_pflag |= LP_MUSTJOIN;
    124 	}
    125 	if ((flag & KTHREAD_INTR) != 0) {
    126 		l->l_pflag |= LP_INTR;
    127 	}
    128 	if ((flag & KTHREAD_MPSAFE) == 0) {
    129 		l->l_pflag &= ~LP_MPSAFE;
    130 	}
    131 
    132 	/*
    133 	 * Set the new LWP running, unless the caller has requested
    134 	 * otherwise.
    135 	 */
    136 	KASSERT(l->l_stat == LSIDL);
    137 	if ((flag & KTHREAD_IDLE) == 0) {
    138 		setrunnable(l);
    139 		/* LWP now unlocked */
    140 	} else {
    141 		lwp_unlock(l);
    142 	}
    143 	mutex_exit(proc0.p_lock);
    144 
    145 	/* All done! */
    146 	if (lp != NULL) {
    147 		*lp = l;
    148 	}
    149 	return 0;
    150 }
    151 
    152 /*
    153  * Cause a kernel thread to exit.  Assumes the exiting thread is the
    154  * current context.
    155  */
    156 void
    157 kthread_exit(int ecode)
    158 {
    159 	const char *name;
    160 	lwp_t *l = curlwp;
    161 
    162 	/* We can't do much with the exit code, so just report it. */
    163 	if (ecode != 0) {
    164 		if ((name = l->l_name) == NULL)
    165 			name = "unnamed";
    166 		printf("WARNING: kthread `%s' (%d) exits with status %d\n",
    167 		    name, l->l_lid, ecode);
    168 	}
    169 
    170 	/* Barrier for joining. */
    171 	if (l->l_pflag & LP_MUSTJOIN) {
    172 		mutex_enter(&kthread_lock);
    173 		while (kthread_jtarget != l) {
    174 			cv_wait(&kthread_cv, &kthread_lock);
    175 		}
    176 		kthread_jtarget = NULL;
    177 		cv_broadcast(&kthread_cv);
    178 		mutex_exit(&kthread_lock);
    179 	}
    180 
    181 	/* If the kernel lock is held, we need to drop it now. */
    182 	if ((l->l_pflag & LP_MPSAFE) == 0) {
    183 		KERNEL_UNLOCK_LAST(l);
    184 	}
    185 
    186 	/* And exit.. */
    187 	lwp_exit(l);
    188 	panic("kthread_exit");
    189 }
    190 
    191 /*
    192  * Wait for a kthread to exit, as pthread_join().
    193  */
    194 int
    195 kthread_join(lwp_t *l)
    196 {
    197 
    198 	KASSERT((l->l_flag & LW_SYSTEM) != 0);
    199 	KASSERT((l->l_pflag & LP_MUSTJOIN) != 0);
    200 
    201 	/*
    202 	 * - Wait if some other thread has occupied the target.
    203 	 * - Specify our kthread as a target and notify it.
    204 	 * - Wait for the target kthread to notify us.
    205 	 */
    206 	mutex_enter(&kthread_lock);
    207 	while (kthread_jtarget) {
    208 		cv_wait(&kthread_cv, &kthread_lock);
    209 	}
    210 	kthread_jtarget = l;
    211 	cv_broadcast(&kthread_cv);
    212 	while (kthread_jtarget == l) {
    213 		cv_wait(&kthread_cv, &kthread_lock);
    214 	}
    215 	mutex_exit(&kthread_lock);
    216 
    217 	return 0;
    218 }
    219