xref: /freebsd/sys/compat/linuxkpi/common/src/linux_schedule.c (revision 4f52dfbb8d6c4d446500c5b097e3806ec219fbd4)
1 /*-
2  * Copyright (c) 2017 Mark Johnston <markj@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conds
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conds, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conds and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/proc.h>
33 #include <sys/signalvar.h>
34 #include <sys/sleepqueue.h>
35 
36 #include <linux/delay.h>
37 #include <linux/errno.h>
38 #include <linux/kernel.h>
39 #include <linux/list.h>
40 #include <linux/sched.h>
41 #include <linux/spinlock.h>
42 #include <linux/wait.h>
43 
44 static int
45 linux_add_to_sleepqueue(void *wchan, struct task_struct *task,
46     const char *wmesg, int timeout, int state)
47 {
48 	int flags, ret;
49 
50 	MPASS((state & ~TASK_NORMAL) == 0);
51 
52 	flags = SLEEPQ_SLEEP | ((state & TASK_INTERRUPTIBLE) != 0 ?
53 	    SLEEPQ_INTERRUPTIBLE : 0);
54 
55 	sleepq_add(wchan, NULL, wmesg, flags, 0);
56 	if (timeout != 0)
57 		sleepq_set_timeout(wchan, timeout);
58 
59 	DROP_GIANT();
60 	if ((state & TASK_INTERRUPTIBLE) != 0) {
61 		if (timeout == 0)
62 			ret = -sleepq_wait_sig(wchan, 0);
63 		else
64 			ret = -sleepq_timedwait_sig(wchan, 0);
65 	} else {
66 		if (timeout == 0) {
67 			sleepq_wait(wchan, 0);
68 			ret = 0;
69 		} else
70 			ret = -sleepq_timedwait(wchan, 0);
71 	}
72 	PICKUP_GIANT();
73 
74 	/* filter return value */
75 	if (ret != 0 && ret != -EWOULDBLOCK) {
76 		linux_schedule_save_interrupt_value(task, ret);
77 		ret = -ERESTARTSYS;
78 	}
79 	return (ret);
80 }
81 
82 unsigned int
83 linux_msleep_interruptible(unsigned int ms)
84 {
85 	int ret;
86 
87 	/* guard against invalid values */
88 	if (ms == 0)
89 		ms = 1;
90 	ret = -pause_sbt("lnxsleep", mstosbt(ms), 0, C_HARDCLOCK | C_CATCH);
91 
92 	switch (ret) {
93 	case -EWOULDBLOCK:
94 		return (0);
95 	default:
96 		linux_schedule_save_interrupt_value(current, ret);
97 		return (ms);
98 	}
99 }
100 
101 static int
102 wake_up_task(struct task_struct *task, unsigned int state)
103 {
104 	int ret, wakeup_swapper;
105 
106 	ret = wakeup_swapper = 0;
107 	sleepq_lock(task);
108 	if ((atomic_read(&task->state) & state) != 0) {
109 		set_task_state(task, TASK_WAKING);
110 		wakeup_swapper = sleepq_signal(task, SLEEPQ_SLEEP, 0, 0);
111 		ret = 1;
112 	}
113 	sleepq_release(task);
114 	if (wakeup_swapper)
115 		kick_proc0();
116 	return (ret);
117 }
118 
119 bool
120 linux_signal_pending(struct task_struct *task)
121 {
122 	struct thread *td;
123 	sigset_t pending;
124 
125 	td = task->task_thread;
126 	PROC_LOCK(td->td_proc);
127 	pending = td->td_siglist;
128 	SIGSETOR(pending, td->td_proc->p_siglist);
129 	SIGSETNAND(pending, td->td_sigmask);
130 	PROC_UNLOCK(td->td_proc);
131 	return (!SIGISEMPTY(pending));
132 }
133 
134 bool
135 linux_fatal_signal_pending(struct task_struct *task)
136 {
137 	struct thread *td;
138 	bool ret;
139 
140 	td = task->task_thread;
141 	PROC_LOCK(td->td_proc);
142 	ret = SIGISMEMBER(td->td_siglist, SIGKILL) ||
143 	    SIGISMEMBER(td->td_proc->p_siglist, SIGKILL);
144 	PROC_UNLOCK(td->td_proc);
145 	return (ret);
146 }
147 
148 bool
149 linux_signal_pending_state(long state, struct task_struct *task)
150 {
151 
152 	MPASS((state & ~TASK_NORMAL) == 0);
153 
154 	if ((state & TASK_INTERRUPTIBLE) == 0)
155 		return (false);
156 	return (linux_signal_pending(task));
157 }
158 
159 void
160 linux_send_sig(int signo, struct task_struct *task)
161 {
162 	struct thread *td;
163 
164 	td = task->task_thread;
165 	PROC_LOCK(td->td_proc);
166 	tdsignal(td, signo);
167 	PROC_UNLOCK(td->td_proc);
168 }
169 
170 int
171 autoremove_wake_function(wait_queue_t *wq, unsigned int state, int flags,
172     void *key __unused)
173 {
174 	struct task_struct *task;
175 	int ret;
176 
177 	task = wq->private;
178 	if ((ret = wake_up_task(task, state)) != 0)
179 		list_del_init(&wq->task_list);
180 	return (ret);
181 }
182 
183 int
184 default_wake_function(wait_queue_t *wq, unsigned int state, int flags,
185     void *key __unused)
186 {
187 	return (wake_up_task(wq->private, state));
188 }
189 
190 void
191 linux_wake_up(wait_queue_head_t *wqh, unsigned int state, int nr, bool locked)
192 {
193 	wait_queue_t *pos, *next;
194 
195 	if (!locked)
196 		spin_lock(&wqh->lock);
197 	list_for_each_entry_safe(pos, next, &wqh->task_list, task_list) {
198 		if (pos->func == NULL) {
199 			if (wake_up_task(pos->private, state) != 0 && --nr == 0)
200 				break;
201 		} else {
202 			if (pos->func(pos, state, 0, NULL) != 0 && --nr == 0)
203 				break;
204 		}
205 	}
206 	if (!locked)
207 		spin_unlock(&wqh->lock);
208 }
209 
210 void
211 linux_prepare_to_wait(wait_queue_head_t *wqh, wait_queue_t *wq, int state)
212 {
213 
214 	spin_lock(&wqh->lock);
215 	if (list_empty(&wq->task_list))
216 		__add_wait_queue(wqh, wq);
217 	set_task_state(current, state);
218 	spin_unlock(&wqh->lock);
219 }
220 
221 void
222 linux_finish_wait(wait_queue_head_t *wqh, wait_queue_t *wq)
223 {
224 
225 	spin_lock(&wqh->lock);
226 	set_task_state(current, TASK_RUNNING);
227 	if (!list_empty(&wq->task_list)) {
228 		__remove_wait_queue(wqh, wq);
229 		INIT_LIST_HEAD(&wq->task_list);
230 	}
231 	spin_unlock(&wqh->lock);
232 }
233 
234 bool
235 linux_waitqueue_active(wait_queue_head_t *wqh)
236 {
237 	bool ret;
238 
239 	spin_lock(&wqh->lock);
240 	ret = !list_empty(&wqh->task_list);
241 	spin_unlock(&wqh->lock);
242 	return (ret);
243 }
244 
245 int
246 linux_wait_event_common(wait_queue_head_t *wqh, wait_queue_t *wq, int timeout,
247     unsigned int state, spinlock_t *lock)
248 {
249 	struct task_struct *task;
250 	int ret;
251 
252 	if (lock != NULL)
253 		spin_unlock_irq(lock);
254 
255 	/* range check timeout */
256 	if (timeout < 1)
257 		timeout = 1;
258 	else if (timeout == MAX_SCHEDULE_TIMEOUT)
259 		timeout = 0;
260 
261 	task = current;
262 
263 	/*
264 	 * Our wait queue entry is on the stack - make sure it doesn't
265 	 * get swapped out while we sleep.
266 	 */
267 	PHOLD(task->task_thread->td_proc);
268 	sleepq_lock(task);
269 	if (atomic_read(&task->state) != TASK_WAKING) {
270 		ret = linux_add_to_sleepqueue(task, task, "wevent", timeout,
271 		    state);
272 	} else {
273 		sleepq_release(task);
274 		ret = 0;
275 	}
276 	PRELE(task->task_thread->td_proc);
277 
278 	if (lock != NULL)
279 		spin_lock_irq(lock);
280 	return (ret);
281 }
282 
283 int
284 linux_schedule_timeout(int timeout)
285 {
286 	struct task_struct *task;
287 	int ret;
288 	int state;
289 	int remainder;
290 
291 	task = current;
292 
293 	/* range check timeout */
294 	if (timeout < 1)
295 		timeout = 1;
296 	else if (timeout == MAX_SCHEDULE_TIMEOUT)
297 		timeout = 0;
298 
299 	remainder = ticks + timeout;
300 
301 	sleepq_lock(task);
302 	state = atomic_read(&task->state);
303 	if (state != TASK_WAKING) {
304 		ret = linux_add_to_sleepqueue(task, task, "sched", timeout,
305 		    state);
306 	} else {
307 		sleepq_release(task);
308 		ret = 0;
309 	}
310 	set_task_state(task, TASK_RUNNING);
311 
312 	if (timeout == 0)
313 		return (MAX_SCHEDULE_TIMEOUT);
314 
315 	/* range check return value */
316 	remainder -= ticks;
317 
318 	/* range check return value */
319 	if (ret == -ERESTARTSYS && remainder < 1)
320 		remainder = 1;
321 	else if (remainder < 0)
322 		remainder = 0;
323 	else if (remainder > timeout)
324 		remainder = timeout;
325 	return (remainder);
326 }
327 
328 static void
329 wake_up_sleepers(void *wchan)
330 {
331 	int wakeup_swapper;
332 
333 	sleepq_lock(wchan);
334 	wakeup_swapper = sleepq_signal(wchan, SLEEPQ_SLEEP, 0, 0);
335 	sleepq_release(wchan);
336 	if (wakeup_swapper)
337 		kick_proc0();
338 }
339 
340 #define	bit_to_wchan(word, bit)	((void *)(((uintptr_t)(word) << 6) | (bit)))
341 
342 void
343 linux_wake_up_bit(void *word, int bit)
344 {
345 
346 	wake_up_sleepers(bit_to_wchan(word, bit));
347 }
348 
349 int
350 linux_wait_on_bit_timeout(unsigned long *word, int bit, unsigned int state,
351     int timeout)
352 {
353 	struct task_struct *task;
354 	void *wchan;
355 	int ret;
356 
357 	/* range check timeout */
358 	if (timeout < 1)
359 		timeout = 1;
360 	else if (timeout == MAX_SCHEDULE_TIMEOUT)
361 		timeout = 0;
362 
363 	task = current;
364 	wchan = bit_to_wchan(word, bit);
365 	for (;;) {
366 		sleepq_lock(wchan);
367 		if ((*word & (1 << bit)) == 0) {
368 			sleepq_release(wchan);
369 			ret = 0;
370 			break;
371 		}
372 		set_task_state(task, state);
373 		ret = linux_add_to_sleepqueue(wchan, task, "wbit", timeout,
374 		    state);
375 		if (ret != 0)
376 			break;
377 	}
378 	set_task_state(task, TASK_RUNNING);
379 
380 	return (ret);
381 }
382 
383 void
384 linux_wake_up_atomic_t(atomic_t *a)
385 {
386 
387 	wake_up_sleepers(a);
388 }
389 
390 int
391 linux_wait_on_atomic_t(atomic_t *a, unsigned int state)
392 {
393 	struct task_struct *task;
394 	void *wchan;
395 	int ret;
396 
397 	task = current;
398 	wchan = a;
399 	for (;;) {
400 		sleepq_lock(wchan);
401 		if (atomic_read(a) == 0) {
402 			sleepq_release(wchan);
403 			ret = 0;
404 			break;
405 		}
406 		set_task_state(task, state);
407 		ret = linux_add_to_sleepqueue(wchan, task, "watomic", 0, state);
408 		if (ret != 0)
409 			break;
410 	}
411 	set_task_state(task, TASK_RUNNING);
412 
413 	return (ret);
414 }
415 
416 bool
417 linux_wake_up_state(struct task_struct *task, unsigned int state)
418 {
419 
420 	return (wake_up_task(task, state) != 0);
421 }
422