1 1.9 riastrad /* $NetBSD: linux_kthread.c,v 1.9 2021/12/19 12:43:05 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.9 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_kthread.c,v 1.9 2021/12/19 12:43:05 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.8 riastrad #include <linux/err.h> 45 1.1 riastrad #include <linux/kthread.h> 46 1.6 riastrad #include <linux/spinlock.h> 47 1.6 riastrad 48 1.6 riastrad #include <drm/drm_wait_netbsd.h> 49 1.1 riastrad 50 1.1 riastrad struct task_struct { 51 1.1 riastrad kmutex_t kt_lock; 52 1.1 riastrad kcondvar_t kt_cv; 53 1.1 riastrad bool kt_shouldstop:1; 54 1.1 riastrad bool kt_shouldpark:1; 55 1.1 riastrad bool kt_parked:1; 56 1.9 riastrad bool kt_exited:1; 57 1.9 riastrad int kt_ret; 58 1.1 riastrad 59 1.1 riastrad int (*kt_func)(void *); 60 1.1 riastrad void *kt_cookie; 61 1.6 riastrad spinlock_t *kt_interlock; 62 1.6 riastrad drm_waitqueue_t *kt_wq; 63 1.1 riastrad struct lwp *kt_lwp; 64 1.1 riastrad }; 65 1.1 riastrad 66 1.1 riastrad static specificdata_key_t linux_kthread_key __read_mostly = -1; 67 1.1 riastrad 68 1.1 riastrad int 69 1.1 riastrad linux_kthread_init(void) 70 1.1 riastrad { 71 1.1 riastrad int error; 72 1.1 riastrad 73 1.1 riastrad error = lwp_specific_key_create(&linux_kthread_key, NULL); 74 1.1 riastrad if (error) 75 1.1 riastrad goto out; 76 1.1 riastrad 77 1.1 riastrad /* Success! */ 78 1.1 riastrad error = 0; 79 1.1 riastrad 80 1.1 riastrad out: if (error) 81 1.1 riastrad linux_kthread_fini(); 82 1.1 riastrad return error; 83 1.1 riastrad } 84 1.1 riastrad 85 1.1 riastrad void 86 1.1 riastrad linux_kthread_fini(void) 87 1.1 riastrad { 88 1.1 riastrad 89 1.1 riastrad if (linux_kthread_key != -1) { 90 1.1 riastrad lwp_specific_key_delete(linux_kthread_key); 91 1.1 riastrad linux_kthread_key = -1; 92 1.1 riastrad } 93 1.1 riastrad } 94 1.1 riastrad 95 1.1 riastrad #define linux_kthread() _linux_kthread(__func__) 96 1.1 riastrad static struct task_struct * 97 1.1 riastrad _linux_kthread(const char *caller) 98 1.1 riastrad { 99 1.1 riastrad struct task_struct *T; 100 1.1 riastrad 101 1.1 riastrad T = lwp_getspecific(linux_kthread_key); 102 1.1 riastrad KASSERTMSG(T != NULL, "%s must be called from Linux kthread", caller); 103 1.1 riastrad 104 1.1 riastrad return T; 105 1.1 riastrad } 106 1.1 riastrad 107 1.1 riastrad static void 108 1.1 riastrad linux_kthread_start(void *cookie) 109 1.1 riastrad { 110 1.1 riastrad struct task_struct *T = cookie; 111 1.1 riastrad int ret; 112 1.1 riastrad 113 1.1 riastrad lwp_setspecific(linux_kthread_key, T); 114 1.1 riastrad 115 1.1 riastrad ret = (*T->kt_func)(T->kt_cookie); 116 1.9 riastrad 117 1.9 riastrad /* 118 1.9 riastrad * Mark the thread exited, set the return value, and wake any 119 1.9 riastrad * waiting kthread_stop. 120 1.9 riastrad */ 121 1.9 riastrad mutex_enter(&T->kt_lock); 122 1.9 riastrad T->kt_exited = true; 123 1.9 riastrad T->kt_ret = ret; 124 1.9 riastrad cv_broadcast(&T->kt_cv); 125 1.9 riastrad mutex_exit(&T->kt_lock); 126 1.9 riastrad 127 1.9 riastrad /* Exit the (NetBSD) kthread. */ 128 1.9 riastrad kthread_exit(0); 129 1.1 riastrad } 130 1.1 riastrad 131 1.1 riastrad static struct task_struct * 132 1.6 riastrad kthread_alloc(int (*func)(void *), void *cookie, spinlock_t *interlock, 133 1.6 riastrad drm_waitqueue_t *wq) 134 1.1 riastrad { 135 1.1 riastrad struct task_struct *T; 136 1.1 riastrad 137 1.1 riastrad T = kmem_zalloc(sizeof(*T), KM_SLEEP); 138 1.1 riastrad 139 1.3 riastrad mutex_init(&T->kt_lock, MUTEX_DEFAULT, IPL_VM); 140 1.1 riastrad cv_init(&T->kt_cv, "lnxkthrd"); 141 1.1 riastrad 142 1.9 riastrad T->kt_shouldstop = false; 143 1.9 riastrad T->kt_shouldpark = false; 144 1.9 riastrad T->kt_parked = false; 145 1.9 riastrad T->kt_exited = false; 146 1.9 riastrad T->kt_ret = 0; 147 1.9 riastrad 148 1.1 riastrad T->kt_func = func; 149 1.1 riastrad T->kt_cookie = cookie; 150 1.6 riastrad T->kt_interlock = interlock; 151 1.6 riastrad T->kt_wq = wq; 152 1.1 riastrad 153 1.1 riastrad return T; 154 1.1 riastrad } 155 1.1 riastrad 156 1.1 riastrad static void 157 1.1 riastrad kthread_free(struct task_struct *T) 158 1.1 riastrad { 159 1.1 riastrad 160 1.9 riastrad KASSERT(T->kt_exited); 161 1.9 riastrad 162 1.1 riastrad cv_destroy(&T->kt_cv); 163 1.1 riastrad mutex_destroy(&T->kt_lock); 164 1.1 riastrad kmem_free(T, sizeof(*T)); 165 1.1 riastrad } 166 1.1 riastrad 167 1.1 riastrad struct task_struct * 168 1.6 riastrad kthread_run(int (*func)(void *), void *cookie, const char *name, 169 1.6 riastrad spinlock_t *interlock, drm_waitqueue_t *wq) 170 1.1 riastrad { 171 1.1 riastrad struct task_struct *T; 172 1.1 riastrad int error; 173 1.1 riastrad 174 1.6 riastrad T = kthread_alloc(func, cookie, interlock, wq); 175 1.9 riastrad error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, 176 1.1 riastrad linux_kthread_start, T, &T->kt_lwp, "%s", name); 177 1.1 riastrad if (error) { 178 1.1 riastrad kthread_free(T); 179 1.8 riastrad return ERR_PTR(-error); /* XXX errno NetBSD->Linux */ 180 1.1 riastrad } 181 1.1 riastrad 182 1.1 riastrad return T; 183 1.1 riastrad } 184 1.1 riastrad 185 1.1 riastrad int 186 1.1 riastrad kthread_stop(struct task_struct *T) 187 1.1 riastrad { 188 1.1 riastrad int ret; 189 1.1 riastrad 190 1.6 riastrad /* Lock order: interlock, then kthread lock. */ 191 1.6 riastrad spin_lock(T->kt_interlock); 192 1.6 riastrad mutex_enter(&T->kt_lock); 193 1.6 riastrad 194 1.5 riastrad /* 195 1.5 riastrad * Notify the thread that it's stopping, and wake it if it's 196 1.6 riastrad * parked or sleeping on its own waitqueue. 197 1.5 riastrad */ 198 1.1 riastrad T->kt_shouldpark = false; 199 1.1 riastrad T->kt_shouldstop = true; 200 1.1 riastrad cv_broadcast(&T->kt_cv); 201 1.6 riastrad DRM_SPIN_WAKEUP_ALL(T->kt_wq, T->kt_interlock); 202 1.6 riastrad 203 1.9 riastrad /* Release the interlock while we wait for thread to finish. */ 204 1.6 riastrad spin_unlock(T->kt_interlock); 205 1.1 riastrad 206 1.9 riastrad /* Wait for the thread to finish. */ 207 1.9 riastrad while (!T->kt_exited) 208 1.9 riastrad cv_wait(&T->kt_cv, &T->kt_lock); 209 1.9 riastrad 210 1.9 riastrad /* Grab the return code and release the lock -- we're done. */ 211 1.9 riastrad ret = T->kt_ret; 212 1.9 riastrad mutex_exit(&T->kt_lock); 213 1.1 riastrad 214 1.6 riastrad /* Free the (Linux) kthread. */ 215 1.1 riastrad kthread_free(T); 216 1.1 riastrad 217 1.6 riastrad /* Return what the thread returned. */ 218 1.1 riastrad return ret; 219 1.1 riastrad } 220 1.1 riastrad 221 1.1 riastrad int 222 1.1 riastrad kthread_should_stop(void) 223 1.1 riastrad { 224 1.2 riastrad struct task_struct *T = linux_kthread(); 225 1.2 riastrad bool shouldstop; 226 1.2 riastrad 227 1.2 riastrad mutex_enter(&T->kt_lock); 228 1.2 riastrad shouldstop = T->kt_shouldstop; 229 1.2 riastrad mutex_exit(&T->kt_lock); 230 1.1 riastrad 231 1.2 riastrad return shouldstop; 232 1.1 riastrad } 233 1.1 riastrad 234 1.1 riastrad void 235 1.1 riastrad kthread_park(struct task_struct *T) 236 1.1 riastrad { 237 1.1 riastrad 238 1.6 riastrad /* Lock order: interlock, then kthread lock. */ 239 1.6 riastrad spin_lock(T->kt_interlock); 240 1.1 riastrad mutex_enter(&T->kt_lock); 241 1.4 riastrad 242 1.4 riastrad /* Caller must not ask to park if they've already asked to stop. */ 243 1.1 riastrad KASSERT(!T->kt_shouldstop); 244 1.4 riastrad 245 1.4 riastrad /* Ask the thread to park. */ 246 1.1 riastrad T->kt_shouldpark = true; 247 1.4 riastrad 248 1.6 riastrad /* 249 1.6 riastrad * Ensure the thread is not sleeping on its condvar. After 250 1.6 riastrad * this point, we are done with the interlock, which we must 251 1.6 riastrad * not hold while we wait on the kthread condvar. 252 1.6 riastrad */ 253 1.6 riastrad DRM_SPIN_WAKEUP_ALL(T->kt_wq, T->kt_interlock); 254 1.6 riastrad spin_unlock(T->kt_interlock); 255 1.4 riastrad 256 1.4 riastrad /* 257 1.6 riastrad * Wait until the thread has issued kthread_parkme, unless we 258 1.6 riastrad * are already the thread, which Linux allows and interprets to 259 1.6 riastrad * mean don't wait. 260 1.4 riastrad */ 261 1.6 riastrad if (T->kt_lwp != curlwp) { 262 1.6 riastrad while (!T->kt_parked) 263 1.6 riastrad cv_wait(&T->kt_cv, &T->kt_lock); 264 1.6 riastrad } 265 1.4 riastrad 266 1.6 riastrad /* Release the kthread lock too. */ 267 1.6 riastrad mutex_exit(&T->kt_lock); 268 1.1 riastrad } 269 1.1 riastrad 270 1.1 riastrad void 271 1.1 riastrad kthread_unpark(struct task_struct *T) 272 1.1 riastrad { 273 1.1 riastrad 274 1.1 riastrad mutex_enter(&T->kt_lock); 275 1.1 riastrad T->kt_shouldpark = false; 276 1.1 riastrad cv_broadcast(&T->kt_cv); 277 1.1 riastrad mutex_exit(&T->kt_lock); 278 1.1 riastrad } 279 1.1 riastrad 280 1.1 riastrad int 281 1.1 riastrad __kthread_should_park(struct task_struct *T) 282 1.1 riastrad { 283 1.1 riastrad bool shouldpark; 284 1.1 riastrad 285 1.1 riastrad mutex_enter(&T->kt_lock); 286 1.1 riastrad shouldpark = T->kt_shouldpark; 287 1.1 riastrad mutex_exit(&T->kt_lock); 288 1.1 riastrad 289 1.1 riastrad return shouldpark; 290 1.1 riastrad } 291 1.1 riastrad 292 1.1 riastrad int 293 1.1 riastrad kthread_should_park(void) 294 1.1 riastrad { 295 1.1 riastrad struct task_struct *T = linux_kthread(); 296 1.1 riastrad 297 1.1 riastrad return __kthread_should_park(T); 298 1.1 riastrad } 299 1.1 riastrad 300 1.1 riastrad void 301 1.1 riastrad kthread_parkme(void) 302 1.1 riastrad { 303 1.1 riastrad struct task_struct *T = linux_kthread(); 304 1.1 riastrad 305 1.7 riastrad assert_spin_locked(T->kt_interlock); 306 1.7 riastrad 307 1.7 riastrad spin_unlock(T->kt_interlock); 308 1.1 riastrad mutex_enter(&T->kt_lock); 309 1.1 riastrad while (T->kt_shouldpark) { 310 1.1 riastrad T->kt_parked = true; 311 1.1 riastrad cv_broadcast(&T->kt_cv); 312 1.1 riastrad cv_wait(&T->kt_cv, &T->kt_lock); 313 1.1 riastrad T->kt_parked = false; 314 1.1 riastrad } 315 1.1 riastrad mutex_exit(&T->kt_lock); 316 1.7 riastrad spin_lock(T->kt_interlock); 317 1.1 riastrad } 318