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