1*014f6938Sriastradh /* $NetBSD: linux_kthread.c,v 1.9 2021/12/19 12:43:05 riastradh Exp $ */
2f576a286Sriastradh
3f576a286Sriastradh /*-
4f576a286Sriastradh * Copyright (c) 2021 The NetBSD Foundation, Inc.
5f576a286Sriastradh * All rights reserved.
6f576a286Sriastradh *
7f576a286Sriastradh * This code is derived from software contributed to The NetBSD Foundation
8f576a286Sriastradh * by Taylor R. Campbell.
9f576a286Sriastradh *
10f576a286Sriastradh * Redistribution and use in source and binary forms, with or without
11f576a286Sriastradh * modification, are permitted provided that the following conditions
12f576a286Sriastradh * are met:
13f576a286Sriastradh * 1. Redistributions of source code must retain the above copyright
14f576a286Sriastradh * notice, this list of conditions and the following disclaimer.
15f576a286Sriastradh * 2. Redistributions in binary form must reproduce the above copyright
16f576a286Sriastradh * notice, this list of conditions and the following disclaimer in the
17f576a286Sriastradh * documentation and/or other materials provided with the distribution.
18f576a286Sriastradh *
19f576a286Sriastradh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20f576a286Sriastradh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21f576a286Sriastradh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22f576a286Sriastradh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23f576a286Sriastradh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24f576a286Sriastradh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25f576a286Sriastradh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26f576a286Sriastradh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27f576a286Sriastradh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28f576a286Sriastradh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29f576a286Sriastradh * POSSIBILITY OF SUCH DAMAGE.
30f576a286Sriastradh */
31f576a286Sriastradh
32f576a286Sriastradh #include <sys/cdefs.h>
33*014f6938Sriastradh __KERNEL_RCSID(0, "$NetBSD: linux_kthread.c,v 1.9 2021/12/19 12:43:05 riastradh Exp $");
34f576a286Sriastradh
35f576a286Sriastradh #include <sys/types.h>
36f576a286Sriastradh
37f576a286Sriastradh #include <sys/condvar.h>
38f576a286Sriastradh #include <sys/kmem.h>
39f576a286Sriastradh #include <sys/kthread.h>
40f576a286Sriastradh #include <sys/lwp.h>
41f576a286Sriastradh #include <sys/mutex.h>
42f576a286Sriastradh #include <sys/specificdata.h>
43f576a286Sriastradh
4469221f93Sriastradh #include <linux/err.h>
45f576a286Sriastradh #include <linux/kthread.h>
461f420662Sriastradh #include <linux/spinlock.h>
471f420662Sriastradh
481f420662Sriastradh #include <drm/drm_wait_netbsd.h>
49f576a286Sriastradh
50f576a286Sriastradh struct task_struct {
51f576a286Sriastradh kmutex_t kt_lock;
52f576a286Sriastradh kcondvar_t kt_cv;
53f576a286Sriastradh bool kt_shouldstop:1;
54f576a286Sriastradh bool kt_shouldpark:1;
55f576a286Sriastradh bool kt_parked:1;
56*014f6938Sriastradh bool kt_exited:1;
57*014f6938Sriastradh int kt_ret;
58f576a286Sriastradh
59f576a286Sriastradh int (*kt_func)(void *);
60f576a286Sriastradh void *kt_cookie;
611f420662Sriastradh spinlock_t *kt_interlock;
621f420662Sriastradh drm_waitqueue_t *kt_wq;
63f576a286Sriastradh struct lwp *kt_lwp;
64f576a286Sriastradh };
65f576a286Sriastradh
66f576a286Sriastradh static specificdata_key_t linux_kthread_key __read_mostly = -1;
67f576a286Sriastradh
68f576a286Sriastradh int
linux_kthread_init(void)69f576a286Sriastradh linux_kthread_init(void)
70f576a286Sriastradh {
71f576a286Sriastradh int error;
72f576a286Sriastradh
73f576a286Sriastradh error = lwp_specific_key_create(&linux_kthread_key, NULL);
74f576a286Sriastradh if (error)
75f576a286Sriastradh goto out;
76f576a286Sriastradh
77f576a286Sriastradh /* Success! */
78f576a286Sriastradh error = 0;
79f576a286Sriastradh
80f576a286Sriastradh out: if (error)
81f576a286Sriastradh linux_kthread_fini();
82f576a286Sriastradh return error;
83f576a286Sriastradh }
84f576a286Sriastradh
85f576a286Sriastradh void
linux_kthread_fini(void)86f576a286Sriastradh linux_kthread_fini(void)
87f576a286Sriastradh {
88f576a286Sriastradh
89f576a286Sriastradh if (linux_kthread_key != -1) {
90f576a286Sriastradh lwp_specific_key_delete(linux_kthread_key);
91f576a286Sriastradh linux_kthread_key = -1;
92f576a286Sriastradh }
93f576a286Sriastradh }
94f576a286Sriastradh
95f576a286Sriastradh #define linux_kthread() _linux_kthread(__func__)
96f576a286Sriastradh static struct task_struct *
_linux_kthread(const char * caller)97f576a286Sriastradh _linux_kthread(const char *caller)
98f576a286Sriastradh {
99f576a286Sriastradh struct task_struct *T;
100f576a286Sriastradh
101f576a286Sriastradh T = lwp_getspecific(linux_kthread_key);
102f576a286Sriastradh KASSERTMSG(T != NULL, "%s must be called from Linux kthread", caller);
103f576a286Sriastradh
104f576a286Sriastradh return T;
105f576a286Sriastradh }
106f576a286Sriastradh
107f576a286Sriastradh static void
linux_kthread_start(void * cookie)108f576a286Sriastradh linux_kthread_start(void *cookie)
109f576a286Sriastradh {
110f576a286Sriastradh struct task_struct *T = cookie;
111f576a286Sriastradh int ret;
112f576a286Sriastradh
113f576a286Sriastradh lwp_setspecific(linux_kthread_key, T);
114f576a286Sriastradh
115f576a286Sriastradh ret = (*T->kt_func)(T->kt_cookie);
116*014f6938Sriastradh
117*014f6938Sriastradh /*
118*014f6938Sriastradh * Mark the thread exited, set the return value, and wake any
119*014f6938Sriastradh * waiting kthread_stop.
120*014f6938Sriastradh */
121*014f6938Sriastradh mutex_enter(&T->kt_lock);
122*014f6938Sriastradh T->kt_exited = true;
123*014f6938Sriastradh T->kt_ret = ret;
124*014f6938Sriastradh cv_broadcast(&T->kt_cv);
125*014f6938Sriastradh mutex_exit(&T->kt_lock);
126*014f6938Sriastradh
127*014f6938Sriastradh /* Exit the (NetBSD) kthread. */
128*014f6938Sriastradh kthread_exit(0);
129f576a286Sriastradh }
130f576a286Sriastradh
131f576a286Sriastradh static struct task_struct *
kthread_alloc(int (* func)(void *),void * cookie,spinlock_t * interlock,drm_waitqueue_t * wq)1321f420662Sriastradh kthread_alloc(int (*func)(void *), void *cookie, spinlock_t *interlock,
1331f420662Sriastradh drm_waitqueue_t *wq)
134f576a286Sriastradh {
135f576a286Sriastradh struct task_struct *T;
136f576a286Sriastradh
137f576a286Sriastradh T = kmem_zalloc(sizeof(*T), KM_SLEEP);
138f576a286Sriastradh
139d59ebc27Sriastradh mutex_init(&T->kt_lock, MUTEX_DEFAULT, IPL_VM);
140f576a286Sriastradh cv_init(&T->kt_cv, "lnxkthrd");
141f576a286Sriastradh
142*014f6938Sriastradh T->kt_shouldstop = false;
143*014f6938Sriastradh T->kt_shouldpark = false;
144*014f6938Sriastradh T->kt_parked = false;
145*014f6938Sriastradh T->kt_exited = false;
146*014f6938Sriastradh T->kt_ret = 0;
147*014f6938Sriastradh
148f576a286Sriastradh T->kt_func = func;
149f576a286Sriastradh T->kt_cookie = cookie;
1501f420662Sriastradh T->kt_interlock = interlock;
1511f420662Sriastradh T->kt_wq = wq;
152f576a286Sriastradh
153f576a286Sriastradh return T;
154f576a286Sriastradh }
155f576a286Sriastradh
156f576a286Sriastradh static void
kthread_free(struct task_struct * T)157f576a286Sriastradh kthread_free(struct task_struct *T)
158f576a286Sriastradh {
159f576a286Sriastradh
160*014f6938Sriastradh KASSERT(T->kt_exited);
161*014f6938Sriastradh
162f576a286Sriastradh cv_destroy(&T->kt_cv);
163f576a286Sriastradh mutex_destroy(&T->kt_lock);
164f576a286Sriastradh kmem_free(T, sizeof(*T));
165f576a286Sriastradh }
166f576a286Sriastradh
167f576a286Sriastradh struct task_struct *
kthread_run(int (* func)(void *),void * cookie,const char * name,spinlock_t * interlock,drm_waitqueue_t * wq)1681f420662Sriastradh kthread_run(int (*func)(void *), void *cookie, const char *name,
1691f420662Sriastradh spinlock_t *interlock, drm_waitqueue_t *wq)
170f576a286Sriastradh {
171f576a286Sriastradh struct task_struct *T;
172f576a286Sriastradh int error;
173f576a286Sriastradh
1741f420662Sriastradh T = kthread_alloc(func, cookie, interlock, wq);
175*014f6938Sriastradh error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
176f576a286Sriastradh linux_kthread_start, T, &T->kt_lwp, "%s", name);
177f576a286Sriastradh if (error) {
178f576a286Sriastradh kthread_free(T);
17969221f93Sriastradh return ERR_PTR(-error); /* XXX errno NetBSD->Linux */
180f576a286Sriastradh }
181f576a286Sriastradh
182f576a286Sriastradh return T;
183f576a286Sriastradh }
184f576a286Sriastradh
185f576a286Sriastradh int
kthread_stop(struct task_struct * T)186f576a286Sriastradh kthread_stop(struct task_struct *T)
187f576a286Sriastradh {
188f576a286Sriastradh int ret;
189f576a286Sriastradh
1901f420662Sriastradh /* Lock order: interlock, then kthread lock. */
1911f420662Sriastradh spin_lock(T->kt_interlock);
1921f420662Sriastradh mutex_enter(&T->kt_lock);
1931f420662Sriastradh
19452541e76Sriastradh /*
19552541e76Sriastradh * Notify the thread that it's stopping, and wake it if it's
1961f420662Sriastradh * parked or sleeping on its own waitqueue.
19752541e76Sriastradh */
198f576a286Sriastradh T->kt_shouldpark = false;
199f576a286Sriastradh T->kt_shouldstop = true;
200f576a286Sriastradh cv_broadcast(&T->kt_cv);
2011f420662Sriastradh DRM_SPIN_WAKEUP_ALL(T->kt_wq, T->kt_interlock);
2021f420662Sriastradh
203*014f6938Sriastradh /* Release the interlock while we wait for thread to finish. */
2041f420662Sriastradh spin_unlock(T->kt_interlock);
205f576a286Sriastradh
206*014f6938Sriastradh /* Wait for the thread to finish. */
207*014f6938Sriastradh while (!T->kt_exited)
208*014f6938Sriastradh cv_wait(&T->kt_cv, &T->kt_lock);
209*014f6938Sriastradh
210*014f6938Sriastradh /* Grab the return code and release the lock -- we're done. */
211*014f6938Sriastradh ret = T->kt_ret;
212*014f6938Sriastradh mutex_exit(&T->kt_lock);
213f576a286Sriastradh
2141f420662Sriastradh /* Free the (Linux) kthread. */
215f576a286Sriastradh kthread_free(T);
216f576a286Sriastradh
2171f420662Sriastradh /* Return what the thread returned. */
218f576a286Sriastradh return ret;
219f576a286Sriastradh }
220f576a286Sriastradh
221f576a286Sriastradh int
kthread_should_stop(void)222f576a286Sriastradh kthread_should_stop(void)
223f576a286Sriastradh {
224f1b3bf61Sriastradh struct task_struct *T = linux_kthread();
225f1b3bf61Sriastradh bool shouldstop;
226f576a286Sriastradh
227f1b3bf61Sriastradh mutex_enter(&T->kt_lock);
228f1b3bf61Sriastradh shouldstop = T->kt_shouldstop;
229f1b3bf61Sriastradh mutex_exit(&T->kt_lock);
230f1b3bf61Sriastradh
231f1b3bf61Sriastradh return shouldstop;
232f576a286Sriastradh }
233f576a286Sriastradh
234f576a286Sriastradh void
kthread_park(struct task_struct * T)235f576a286Sriastradh kthread_park(struct task_struct *T)
236f576a286Sriastradh {
237f576a286Sriastradh
2381f420662Sriastradh /* Lock order: interlock, then kthread lock. */
2391f420662Sriastradh spin_lock(T->kt_interlock);
240f576a286Sriastradh mutex_enter(&T->kt_lock);
2419dfeae65Sriastradh
2429dfeae65Sriastradh /* Caller must not ask to park if they've already asked to stop. */
243f576a286Sriastradh KASSERT(!T->kt_shouldstop);
2449dfeae65Sriastradh
2459dfeae65Sriastradh /* Ask the thread to park. */
246f576a286Sriastradh T->kt_shouldpark = true;
2479dfeae65Sriastradh
2481f420662Sriastradh /*
2491f420662Sriastradh * Ensure the thread is not sleeping on its condvar. After
2501f420662Sriastradh * this point, we are done with the interlock, which we must
2511f420662Sriastradh * not hold while we wait on the kthread condvar.
2521f420662Sriastradh */
2531f420662Sriastradh DRM_SPIN_WAKEUP_ALL(T->kt_wq, T->kt_interlock);
2541f420662Sriastradh spin_unlock(T->kt_interlock);
2559dfeae65Sriastradh
2569dfeae65Sriastradh /*
2571f420662Sriastradh * Wait until the thread has issued kthread_parkme, unless we
2581f420662Sriastradh * are already the thread, which Linux allows and interprets to
2591f420662Sriastradh * mean don't wait.
2609dfeae65Sriastradh */
2611f420662Sriastradh if (T->kt_lwp != curlwp) {
262f576a286Sriastradh while (!T->kt_parked)
263f576a286Sriastradh cv_wait(&T->kt_cv, &T->kt_lock);
2641f420662Sriastradh }
2659dfeae65Sriastradh
2661f420662Sriastradh /* Release the kthread lock too. */
2671f420662Sriastradh mutex_exit(&T->kt_lock);
268f576a286Sriastradh }
269f576a286Sriastradh
270f576a286Sriastradh void
kthread_unpark(struct task_struct * T)271f576a286Sriastradh kthread_unpark(struct task_struct *T)
272f576a286Sriastradh {
273f576a286Sriastradh
274f576a286Sriastradh mutex_enter(&T->kt_lock);
275f576a286Sriastradh T->kt_shouldpark = false;
276f576a286Sriastradh cv_broadcast(&T->kt_cv);
277f576a286Sriastradh mutex_exit(&T->kt_lock);
278f576a286Sriastradh }
279f576a286Sriastradh
280f576a286Sriastradh int
__kthread_should_park(struct task_struct * T)281f576a286Sriastradh __kthread_should_park(struct task_struct *T)
282f576a286Sriastradh {
283f576a286Sriastradh bool shouldpark;
284f576a286Sriastradh
285f576a286Sriastradh mutex_enter(&T->kt_lock);
286f576a286Sriastradh shouldpark = T->kt_shouldpark;
287f576a286Sriastradh mutex_exit(&T->kt_lock);
288f576a286Sriastradh
289f576a286Sriastradh return shouldpark;
290f576a286Sriastradh }
291f576a286Sriastradh
292f576a286Sriastradh int
kthread_should_park(void)293f576a286Sriastradh kthread_should_park(void)
294f576a286Sriastradh {
295f576a286Sriastradh struct task_struct *T = linux_kthread();
296f576a286Sriastradh
297f576a286Sriastradh return __kthread_should_park(T);
298f576a286Sriastradh }
299f576a286Sriastradh
300f576a286Sriastradh void
kthread_parkme(void)301f576a286Sriastradh kthread_parkme(void)
302f576a286Sriastradh {
303f576a286Sriastradh struct task_struct *T = linux_kthread();
304f576a286Sriastradh
30530d3e3c2Sriastradh assert_spin_locked(T->kt_interlock);
30630d3e3c2Sriastradh
30730d3e3c2Sriastradh spin_unlock(T->kt_interlock);
308f576a286Sriastradh mutex_enter(&T->kt_lock);
309f576a286Sriastradh while (T->kt_shouldpark) {
310f576a286Sriastradh T->kt_parked = true;
311f576a286Sriastradh cv_broadcast(&T->kt_cv);
312f576a286Sriastradh cv_wait(&T->kt_cv, &T->kt_lock);
313f576a286Sriastradh T->kt_parked = false;
314f576a286Sriastradh }
315f576a286Sriastradh mutex_exit(&T->kt_lock);
31630d3e3c2Sriastradh spin_lock(T->kt_interlock);
317f576a286Sriastradh }
318