Home | History | Annotate | Line # | Download | only in linux
linux_kthread.c revision 1.1
      1 /*	$NetBSD: linux_kthread.c,v 1.1 2021/12/19 12:23:07 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2021 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: linux_kthread.c,v 1.1 2021/12/19 12:23:07 riastradh Exp $");
     34 
     35 #include <sys/types.h>
     36 
     37 #include <sys/condvar.h>
     38 #include <sys/kmem.h>
     39 #include <sys/kthread.h>
     40 #include <sys/lwp.h>
     41 #include <sys/mutex.h>
     42 #include <sys/specificdata.h>
     43 
     44 #include <linux/kthread.h>
     45 
     46 struct task_struct {
     47 	kmutex_t	kt_lock;
     48 	kcondvar_t	kt_cv;
     49 	bool		kt_shouldstop:1;
     50 	bool		kt_shouldpark:1;
     51 	bool		kt_parked:1;
     52 
     53 	int		(*kt_func)(void *);
     54 	void		*kt_cookie;
     55 	struct lwp	*kt_lwp;
     56 };
     57 
     58 static specificdata_key_t linux_kthread_key __read_mostly = -1;
     59 
     60 int
     61 linux_kthread_init(void)
     62 {
     63 	int error;
     64 
     65 	error = lwp_specific_key_create(&linux_kthread_key, NULL);
     66 	if (error)
     67 		goto out;
     68 
     69 	/* Success!  */
     70 	error = 0;
     71 
     72 out:	if (error)
     73 		linux_kthread_fini();
     74 	return error;
     75 }
     76 
     77 void
     78 linux_kthread_fini(void)
     79 {
     80 
     81 	if (linux_kthread_key != -1) {
     82 		lwp_specific_key_delete(linux_kthread_key);
     83 		linux_kthread_key = -1;
     84 	}
     85 }
     86 
     87 #define	linux_kthread()	_linux_kthread(__func__)
     88 static struct task_struct *
     89 _linux_kthread(const char *caller)
     90 {
     91 	struct task_struct *T;
     92 
     93 	T = lwp_getspecific(linux_kthread_key);
     94 	KASSERTMSG(T != NULL, "%s must be called from Linux kthread", caller);
     95 
     96 	return T;
     97 }
     98 
     99 static void
    100 linux_kthread_start(void *cookie)
    101 {
    102 	struct task_struct *T = cookie;
    103 	int ret;
    104 
    105 	lwp_setspecific(linux_kthread_key, T);
    106 
    107 	ret = (*T->kt_func)(T->kt_cookie);
    108 	kthread_exit(ret);
    109 }
    110 
    111 static struct task_struct *
    112 kthread_alloc(int (*func)(void *), void *cookie)
    113 {
    114 	struct task_struct *T;
    115 
    116 	T = kmem_zalloc(sizeof(*T), KM_SLEEP);
    117 
    118 	mutex_init(&T->kt_lock, MUTEX_DEFAULT, IPL_NONE);
    119 	cv_init(&T->kt_cv, "lnxkthrd");
    120 
    121 	T->kt_func = func;
    122 	T->kt_cookie = cookie;
    123 
    124 	return T;
    125 }
    126 
    127 static void
    128 kthread_free(struct task_struct *T)
    129 {
    130 
    131 	cv_destroy(&T->kt_cv);
    132 	mutex_destroy(&T->kt_lock);
    133 	kmem_free(T, sizeof(*T));
    134 }
    135 
    136 struct task_struct *
    137 kthread_run(int (*func)(void *), void *cookie, const char *name)
    138 {
    139 	struct task_struct *T;
    140 	int error;
    141 
    142 	T = kthread_alloc(func, cookie);
    143 	error = kthread_create(PRI_NONE, KTHREAD_MPSAFE|KTHREAD_MUSTJOIN, NULL,
    144 	    linux_kthread_start, T, &T->kt_lwp, "%s", name);
    145 	if (error) {
    146 		kthread_free(T);
    147 		T = NULL;
    148 	}
    149 
    150 	return T;
    151 }
    152 
    153 int
    154 kthread_stop(struct task_struct *T)
    155 {
    156 	int ret;
    157 
    158 	mutex_enter(&T->kt_lock);
    159 	T->kt_shouldpark = false;
    160 	T->kt_shouldstop = true;
    161 	cv_broadcast(&T->kt_cv);
    162 	mutex_exit(&T->kt_lock);
    163 
    164 	ret = kthread_join(T->kt_lwp);
    165 
    166 	kthread_free(T);
    167 
    168 	return ret;
    169 }
    170 
    171 int
    172 kthread_should_stop(void)
    173 {
    174 
    175 	return linux_kthread()->kt_shouldstop;
    176 }
    177 
    178 void
    179 kthread_park(struct task_struct *T)
    180 {
    181 
    182 	mutex_enter(&T->kt_lock);
    183 	KASSERT(!T->kt_shouldstop);
    184 	T->kt_shouldpark = true;
    185 	while (!T->kt_parked)
    186 		cv_wait(&T->kt_cv, &T->kt_lock);
    187 	mutex_exit(&T->kt_lock);
    188 }
    189 
    190 void
    191 kthread_unpark(struct task_struct *T)
    192 {
    193 
    194 	mutex_enter(&T->kt_lock);
    195 	T->kt_shouldpark = false;
    196 	cv_broadcast(&T->kt_cv);
    197 	mutex_exit(&T->kt_lock);
    198 }
    199 
    200 int
    201 __kthread_should_park(struct task_struct *T)
    202 {
    203 	bool shouldpark;
    204 
    205 	mutex_enter(&T->kt_lock);
    206 	shouldpark = T->kt_shouldpark;
    207 	mutex_exit(&T->kt_lock);
    208 
    209 	return shouldpark;
    210 }
    211 
    212 int
    213 kthread_should_park(void)
    214 {
    215 	struct task_struct *T = linux_kthread();
    216 
    217 	return __kthread_should_park(T);
    218 }
    219 
    220 void
    221 kthread_parkme(void)
    222 {
    223 	struct task_struct *T = linux_kthread();
    224 
    225 	mutex_enter(&T->kt_lock);
    226 	while (T->kt_shouldpark) {
    227 		T->kt_parked = true;
    228 		cv_broadcast(&T->kt_cv);
    229 		cv_wait(&T->kt_cv, &T->kt_lock);
    230 		T->kt_parked = false;
    231 	}
    232 	mutex_exit(&T->kt_lock);
    233 }
    234