kern_synch.c revision 1.114
1/*	$OpenBSD: kern_synch.c,v 1.114 2014/01/23 01:48:44 guenther Exp $	*/
2/*	$NetBSD: kern_synch.c,v 1.37 1996/04/22 01:38:37 christos Exp $	*/
3
4/*
5 * Copyright (c) 1982, 1986, 1990, 1991, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 * (c) UNIX System Laboratories, Inc.
8 * All or some portions of this file are derived from material licensed
9 * to the University of California by American Telephone and Telegraph
10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 * the permission of UNIX System Laboratories, Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 *    may be used to endorse or promote products derived from this software
23 *    without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 *	@(#)kern_synch.c	8.6 (Berkeley) 1/21/94
38 */
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/proc.h>
43#include <sys/kernel.h>
44#include <sys/buf.h>
45#include <sys/signalvar.h>
46#include <sys/resourcevar.h>
47#include <uvm/uvm_extern.h>
48#include <sys/sched.h>
49#include <sys/timeout.h>
50#include <sys/mount.h>
51#include <sys/syscallargs.h>
52#include <sys/pool.h>
53
54#include <machine/spinlock.h>
55
56#ifdef KTRACE
57#include <sys/ktrace.h>
58#endif
59
60int	thrsleep(struct proc *, struct sys___thrsleep_args *);
61
62
63/*
64 * We're only looking at 7 bits of the address; everything is
65 * aligned to 4, lots of things are aligned to greater powers
66 * of 2.  Shift right by 8, i.e. drop the bottom 256 worth.
67 */
68#define TABLESIZE	128
69#define LOOKUP(x)	(((long)(x) >> 8) & (TABLESIZE - 1))
70TAILQ_HEAD(slpque,proc) slpque[TABLESIZE];
71
72void
73sleep_queue_init(void)
74{
75	int i;
76
77	for (i = 0; i < TABLESIZE; i++)
78		TAILQ_INIT(&slpque[i]);
79}
80
81
82/*
83 * During autoconfiguration or after a panic, a sleep will simply
84 * lower the priority briefly to allow interrupts, then return.
85 * The priority to be used (safepri) is machine-dependent, thus this
86 * value is initialized and maintained in the machine-dependent layers.
87 * This priority will typically be 0, or the lowest priority
88 * that is safe for use on the interrupt stack; it can be made
89 * higher to block network software interrupts after panics.
90 */
91extern int safepri;
92
93/*
94 * General sleep call.  Suspends the current process until a wakeup is
95 * performed on the specified identifier.  The process will then be made
96 * runnable with the specified priority.  Sleeps at most timo/hz seconds
97 * (0 means no timeout).  If pri includes PCATCH flag, signals are checked
98 * before and after sleeping, else signals are not checked.  Returns 0 if
99 * awakened, EWOULDBLOCK if the timeout expires.  If PCATCH is set and a
100 * signal needs to be delivered, ERESTART is returned if the current system
101 * call should be restarted if possible, and EINTR is returned if the system
102 * call should be interrupted by the signal (return EINTR).
103 */
104int
105tsleep(const volatile void *ident, int priority, const char *wmesg, int timo)
106{
107	struct sleep_state sls;
108	int error, error1;
109
110	KASSERT((priority & ~(PRIMASK | PCATCH)) == 0);
111
112#ifdef MULTIPROCESSOR
113	KASSERT(timo || __mp_lock_held(&kernel_lock));
114#endif
115
116	if (cold || panicstr) {
117		int s;
118		/*
119		 * After a panic, or during autoconfiguration,
120		 * just give interrupts a chance, then just return;
121		 * don't run any other procs or panic below,
122		 * in case this is the idle process and already asleep.
123		 */
124		s = splhigh();
125		splx(safepri);
126		splx(s);
127		return (0);
128	}
129
130	sleep_setup(&sls, ident, priority, wmesg);
131	sleep_setup_timeout(&sls, timo);
132	sleep_setup_signal(&sls, priority);
133
134	sleep_finish(&sls, 1);
135	error1 = sleep_finish_timeout(&sls);
136	error = sleep_finish_signal(&sls);
137
138	/* Signal errors are higher priority than timeouts. */
139	if (error == 0 && error1 != 0)
140		error = error1;
141
142	return (error);
143}
144
145/*
146 * Same as tsleep, but if we have a mutex provided, then once we've
147 * entered the sleep queue we drop the mutex. After sleeping we re-lock.
148 */
149int
150msleep(const volatile void *ident, struct mutex *mtx, int priority,
151    const char *wmesg, int timo)
152{
153	struct sleep_state sls;
154	int error, error1, spl;
155
156	KASSERT((priority & ~(PRIMASK | PCATCH | PNORELOCK)) == 0);
157	KASSERT(mtx != NULL);
158
159	sleep_setup(&sls, ident, priority, wmesg);
160	sleep_setup_timeout(&sls, timo);
161	sleep_setup_signal(&sls, priority);
162
163	/* XXX - We need to make sure that the mutex doesn't
164	 * unblock splsched. This can be made a bit more
165	 * correct when the sched_lock is a mutex.
166	 */
167	spl = MUTEX_OLDIPL(mtx);
168	MUTEX_OLDIPL(mtx) = splsched();
169	mtx_leave(mtx);
170
171	sleep_finish(&sls, 1);
172	error1 = sleep_finish_timeout(&sls);
173	error = sleep_finish_signal(&sls);
174
175	if ((priority & PNORELOCK) == 0) {
176		mtx_enter(mtx);
177		MUTEX_OLDIPL(mtx) = spl; /* put the ipl back */
178	} else
179		splx(spl);
180
181	/* Signal errors are higher priority than timeouts. */
182	if (error == 0 && error1 != 0)
183		error = error1;
184
185	return (error);
186}
187
188void
189sleep_setup(struct sleep_state *sls, const volatile void *ident, int prio,
190    const char *wmesg)
191{
192	struct proc *p = curproc;
193
194#ifdef DIAGNOSTIC
195	if (ident == NULL)
196		panic("tsleep: no ident");
197	if (p->p_stat != SONPROC)
198		panic("tsleep: not SONPROC");
199#endif
200
201#ifdef KTRACE
202	if (KTRPOINT(p, KTR_CSW))
203		ktrcsw(p, 1, 0);
204#endif
205
206	sls->sls_catch = 0;
207	sls->sls_do_sleep = 1;
208	sls->sls_sig = 1;
209
210	SCHED_LOCK(sls->sls_s);
211
212	p->p_wchan = ident;
213	p->p_wmesg = wmesg;
214	p->p_slptime = 0;
215	p->p_priority = prio & PRIMASK;
216	TAILQ_INSERT_TAIL(&slpque[LOOKUP(ident)], p, p_runq);
217}
218
219void
220sleep_finish(struct sleep_state *sls, int do_sleep)
221{
222	struct proc *p = curproc;
223
224	if (sls->sls_do_sleep && do_sleep) {
225		p->p_stat = SSLEEP;
226		p->p_ru.ru_nvcsw++;
227		SCHED_ASSERT_LOCKED();
228		mi_switch();
229	} else if (!do_sleep) {
230		unsleep(p);
231	}
232
233#ifdef DIAGNOSTIC
234	if (p->p_stat != SONPROC)
235		panic("sleep_finish !SONPROC");
236#endif
237
238	p->p_cpu->ci_schedstate.spc_curpriority = p->p_usrpri;
239	SCHED_UNLOCK(sls->sls_s);
240
241	/*
242	 * Even though this belongs to the signal handling part of sleep,
243	 * we need to clear it before the ktrace.
244	 */
245	atomic_clearbits_int(&p->p_flag, P_SINTR);
246
247#ifdef KTRACE
248	if (KTRPOINT(p, KTR_CSW))
249		ktrcsw(p, 0, 0);
250#endif
251}
252
253void
254sleep_setup_timeout(struct sleep_state *sls, int timo)
255{
256	if (timo)
257		timeout_add(&curproc->p_sleep_to, timo);
258}
259
260int
261sleep_finish_timeout(struct sleep_state *sls)
262{
263	struct proc *p = curproc;
264
265	if (p->p_flag & P_TIMEOUT) {
266		atomic_clearbits_int(&p->p_flag, P_TIMEOUT);
267		return (EWOULDBLOCK);
268	} else
269		timeout_del(&p->p_sleep_to);
270
271	return (0);
272}
273
274void
275sleep_setup_signal(struct sleep_state *sls, int prio)
276{
277	struct proc *p = curproc;
278
279	if ((sls->sls_catch = (prio & PCATCH)) == 0)
280		return;
281
282	/*
283	 * We put ourselves on the sleep queue and start our timeout
284	 * before calling CURSIG, as we could stop there, and a wakeup
285	 * or a SIGCONT (or both) could occur while we were stopped.
286	 * A SIGCONT would cause us to be marked as SSLEEP
287	 * without resuming us, thus we must be ready for sleep
288	 * when CURSIG is called.  If the wakeup happens while we're
289	 * stopped, p->p_wchan will be 0 upon return from CURSIG.
290	 */
291	atomic_setbits_int(&p->p_flag, P_SINTR);
292	if (p->p_p->ps_single != NULL || (sls->sls_sig = CURSIG(p)) != 0) {
293		if (p->p_wchan)
294			unsleep(p);
295		p->p_stat = SONPROC;
296		sls->sls_do_sleep = 0;
297	} else if (p->p_wchan == 0) {
298		sls->sls_catch = 0;
299		sls->sls_do_sleep = 0;
300	}
301}
302
303int
304sleep_finish_signal(struct sleep_state *sls)
305{
306	struct proc *p = curproc;
307	int error;
308
309	if (sls->sls_catch != 0) {
310		if ((error = single_thread_check(p, 1)))
311			return (error);
312		if (sls->sls_sig != 0 || (sls->sls_sig = CURSIG(p)) != 0) {
313			if (p->p_sigacts->ps_sigintr & sigmask(sls->sls_sig))
314				return (EINTR);
315			return (ERESTART);
316		}
317	}
318
319	return (0);
320}
321
322/*
323 * Implement timeout for tsleep.
324 * If process hasn't been awakened (wchan non-zero),
325 * set timeout flag and undo the sleep.  If proc
326 * is stopped, just unsleep so it will remain stopped.
327 */
328void
329endtsleep(void *arg)
330{
331	struct proc *p = arg;
332	int s;
333
334	SCHED_LOCK(s);
335	if (p->p_wchan) {
336		if (p->p_stat == SSLEEP)
337			setrunnable(p);
338		else
339			unsleep(p);
340		atomic_setbits_int(&p->p_flag, P_TIMEOUT);
341	}
342	SCHED_UNLOCK(s);
343}
344
345/*
346 * Remove a process from its wait queue
347 */
348void
349unsleep(struct proc *p)
350{
351	if (p->p_wchan) {
352		TAILQ_REMOVE(&slpque[LOOKUP(p->p_wchan)], p, p_runq);
353		p->p_wchan = NULL;
354	}
355}
356
357/*
358 * Make a number of processes sleeping on the specified identifier runnable.
359 */
360void
361wakeup_n(const volatile void *ident, int n)
362{
363	struct slpque *qp;
364	struct proc *p;
365	struct proc *pnext;
366	int s;
367
368	SCHED_LOCK(s);
369	qp = &slpque[LOOKUP(ident)];
370	for (p = TAILQ_FIRST(qp); p != NULL && n != 0; p = pnext) {
371		pnext = TAILQ_NEXT(p, p_runq);
372#ifdef DIAGNOSTIC
373		if (p->p_stat != SSLEEP && p->p_stat != SSTOP)
374			panic("wakeup: p_stat is %d", (int)p->p_stat);
375#endif
376		if (p->p_wchan == ident) {
377			--n;
378			p->p_wchan = 0;
379			TAILQ_REMOVE(qp, p, p_runq);
380			if (p->p_stat == SSLEEP)
381				setrunnable(p);
382		}
383	}
384	SCHED_UNLOCK(s);
385}
386
387/*
388 * Make all processes sleeping on the specified identifier runnable.
389 */
390void
391wakeup(const volatile void *chan)
392{
393	wakeup_n(chan, -1);
394}
395
396int
397sys_sched_yield(struct proc *p, void *v, register_t *retval)
398{
399	yield();
400	return (0);
401}
402
403int thrsleep_unlock(void *, int);
404int
405thrsleep_unlock(void *lock, int lockflags)
406{
407	static _atomic_lock_t unlocked = _ATOMIC_LOCK_UNLOCKED;
408	_atomic_lock_t *atomiclock = lock;
409	uint32_t *ticket = lock;
410	uint32_t ticketvalue;
411	int error;
412
413	if (!lock)
414		return (0);
415
416	if (lockflags) {
417		if ((error = copyin(ticket, &ticketvalue, sizeof(ticketvalue))))
418			return (error);
419		ticketvalue++;
420		error = copyout(&ticketvalue, ticket, sizeof(ticketvalue));
421	} else {
422		error = copyout(&unlocked, atomiclock, sizeof(unlocked));
423	}
424	return (error);
425}
426
427static int globalsleepaddr;
428
429int
430thrsleep(struct proc *p, struct sys___thrsleep_args *v)
431{
432	struct sys___thrsleep_args /* {
433		syscallarg(const volatile void *) ident;
434		syscallarg(clockid_t) clock_id;
435		syscallarg(const struct timespec *) tp;
436		syscallarg(void *) lock;
437		syscallarg(const int *) abort;
438	} */ *uap = v;
439	long ident = (long)SCARG(uap, ident);
440	struct timespec *tsp = (struct timespec *)SCARG(uap, tp);
441	void *lock = SCARG(uap, lock);
442	long long to_ticks = 0;
443	int abort, error;
444	clockid_t clock_id = SCARG(uap, clock_id) & 0x7;
445	int lockflags = SCARG(uap, clock_id) & 0x8;
446
447	if (ident == 0)
448		return (EINVAL);
449	if (tsp != NULL) {
450		struct timespec now;
451
452		if ((error = clock_gettime(p, clock_id, &now)))
453			return (error);
454#ifdef KTRACE
455		if (KTRPOINT(p, KTR_STRUCT))
456			ktrabstimespec(p, tsp);
457#endif
458
459		if (timespeccmp(tsp, &now, <)) {
460			/* already passed: still do the unlock */
461			if ((error = thrsleep_unlock(lock, lockflags)))
462				return (error);
463			return (EWOULDBLOCK);
464		}
465
466		timespecsub(tsp, &now, tsp);
467		to_ticks = (long long)hz * tsp->tv_sec +
468		    (tsp->tv_nsec + tick * 1000 - 1) / (tick * 1000) + 1;
469		if (to_ticks > INT_MAX)
470			to_ticks = INT_MAX;
471	}
472
473	p->p_thrslpid = ident;
474
475	if ((error = thrsleep_unlock(lock, lockflags))) {
476		goto out;
477	}
478
479	if (SCARG(uap, abort) != NULL) {
480		if ((error = copyin(SCARG(uap, abort), &abort,
481		    sizeof(abort))) != 0)
482			goto out;
483		if (abort) {
484			error = EINTR;
485			goto out;
486		}
487	}
488
489	if (p->p_thrslpid == 0)
490		error = 0;
491	else {
492		void *sleepaddr = &p->p_thrslpid;
493		if (ident == -1)
494			sleepaddr = &globalsleepaddr;
495		error = tsleep(sleepaddr, PUSER | PCATCH, "thrsleep",
496		    (int)to_ticks);
497	}
498
499out:
500	p->p_thrslpid = 0;
501
502	if (error == ERESTART)
503		error = EINTR;
504
505	return (error);
506
507}
508
509int
510sys___thrsleep(struct proc *p, void *v, register_t *retval)
511{
512	struct sys___thrsleep_args /* {
513		syscallarg(const volatile void *) ident;
514		syscallarg(clockid_t) clock_id;
515		syscallarg(struct timespec *) tp;
516		syscallarg(void *) lock;
517		syscallarg(const int *) abort;
518	} */ *uap = v;
519	struct timespec ts;
520	int error;
521
522	if (SCARG(uap, tp) != NULL) {
523		if ((error = copyin(SCARG(uap, tp), &ts, sizeof(ts)))) {
524			*retval = error;
525			return (0);
526		}
527		SCARG(uap, tp) = &ts;
528	}
529
530	*retval = thrsleep(p, uap);
531	return (0);
532}
533
534int
535sys___thrwakeup(struct proc *p, void *v, register_t *retval)
536{
537	struct sys___thrwakeup_args /* {
538		syscallarg(const volatile void *) ident;
539		syscallarg(int) n;
540	} */ *uap = v;
541	long ident = (long)SCARG(uap, ident);
542	int n = SCARG(uap, n);
543	struct proc *q;
544	int found = 0;
545
546	if (ident == 0)
547		*retval = EINVAL;
548	else if (ident == -1)
549		wakeup(&globalsleepaddr);
550	else {
551		TAILQ_FOREACH(q, &p->p_p->ps_threads, p_thr_link) {
552			if (q->p_thrslpid == ident) {
553				wakeup_one(&q->p_thrslpid);
554				q->p_thrslpid = 0;
555				if (++found == n)
556					break;
557			}
558		}
559		*retval = found ? 0 : ESRCH;
560	}
561
562	return (0);
563}
564