xref: /freebsd/sys/kern/kern_thr.c (revision cec50dea12481dc578c0805c887ab2097e1c06c5)
1 /*
2  * Copyright (c) 2003, Jeffrey Roberson <jeff@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 conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions 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/kernel.h>
32 #include <sys/lock.h>
33 #include <sys/mutex.h>
34 #include <sys/proc.h>
35 #include <sys/resourcevar.h>
36 #include <sys/sched.h>
37 #include <sys/sysctl.h>
38 #include <sys/smp.h>
39 #include <sys/sysent.h>
40 #include <sys/systm.h>
41 #include <sys/sysproto.h>
42 #include <sys/signalvar.h>
43 #include <sys/ucontext.h>
44 #include <sys/thr.h>
45 
46 #include <machine/frame.h>
47 
48 extern int max_threads_per_proc;
49 extern int max_groups_per_proc;
50 
51 SYSCTL_DECL(_kern_threads);
52 static int thr_scope_sys = 0;
53 SYSCTL_INT(_kern_threads, OID_AUTO, thr_scope_sys, CTLFLAG_RW,
54 	&thr_scope_sys, 0, "sys or proc scope scheduling");
55 
56 static int thr_concurrency = 0;
57 SYSCTL_INT(_kern_threads, OID_AUTO, thr_concurrency, CTLFLAG_RW,
58 	&thr_concurrency, 0, "a concurrency value if not default");
59 
60 /*
61  * Back end support functions.
62  */
63 
64 #define	RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start))
65 
66 /*
67  * System call interface.
68  */
69 int
70 thr_create(struct thread *td, struct thr_create_args *uap)
71     /* ucontext_t *ctx, long *id, int flags */
72 {
73 	struct thread *newtd;
74 	ucontext_t ctx;
75 	long id;
76 	int error;
77 	struct ksegrp *kg, *newkg;
78 	struct proc *p;
79 
80 	p = td->td_proc;
81 	kg = td->td_ksegrp;
82 	if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
83 		return (error);
84 
85 	/* Have race condition but it is cheap */
86 	if ((p->p_numksegrps >= max_groups_per_proc) ||
87 	    (p->p_numthreads >= max_threads_per_proc)) {
88 		return (EPROCLIM);
89 	}
90 	/* Initialize our td and new ksegrp.. */
91 	newtd = thread_alloc();
92 	if (thr_scope_sys)
93 		newkg = ksegrp_alloc();
94 	else
95 		newkg = kg;
96 	/*
97 	 * Try the copyout as soon as we allocate the td so we don't have to
98 	 * tear things down in a failure case below.
99 	 */
100 	id = newtd->td_tid;
101 	if ((error = copyout(&id, uap->id, sizeof(long)))) {
102 		if (thr_scope_sys)
103 			ksegrp_free(newkg);
104 		thread_free(newtd);
105 		return (error);
106 	}
107 
108 	bzero(&newtd->td_startzero,
109 	    (unsigned) RANGEOF(struct thread, td_startzero, td_endzero));
110 	bcopy(&td->td_startcopy, &newtd->td_startcopy,
111 	    (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy));
112 
113 	if (thr_scope_sys) {
114 		bzero(&newkg->kg_startzero,
115 		    (unsigned)RANGEOF(struct ksegrp, kg_startzero, kg_endzero));
116 		bcopy(&kg->kg_startcopy, &newkg->kg_startcopy,
117 		    (unsigned)RANGEOF(struct ksegrp, kg_startcopy, kg_endcopy));
118 	}
119 
120 	newtd->td_proc = td->td_proc;
121 	newtd->td_ucred = crhold(td->td_ucred);
122 
123 	/* Set up our machine context. */
124 	cpu_set_upcall(newtd, td);
125 	error = set_mcontext(newtd, &ctx.uc_mcontext);
126 	if (error != 0) {
127 		if (thr_scope_sys)
128 			ksegrp_free(newkg);
129 		thread_free(newtd);
130 		crfree(td->td_ucred);
131 		goto out;
132 	}
133 
134 	/* Link the thread and kse into the ksegrp and make it runnable. */
135 	PROC_LOCK(td->td_proc);
136 	if (thr_scope_sys) {
137 			sched_init_concurrency(newkg);
138 	} else {
139 		if ((td->td_proc->p_flag & P_HADTHREADS) == 0) {
140 			sched_set_concurrency(kg,
141 			    thr_concurrency ? thr_concurrency : (2*mp_ncpus));
142 		}
143 	}
144 
145 	td->td_proc->p_flag |= P_HADTHREADS;
146 	newtd->td_sigmask = td->td_sigmask;
147 	mtx_lock_spin(&sched_lock);
148 	if (thr_scope_sys)
149 		ksegrp_link(newkg, p);
150 	thread_link(newtd, newkg);
151 	mtx_unlock_spin(&sched_lock);
152 	PROC_UNLOCK(p);
153 
154 	/* let the scheduler know about these things. */
155 	mtx_lock_spin(&sched_lock);
156 	if (thr_scope_sys)
157 		sched_fork_ksegrp(td, newkg);
158 	sched_fork_thread(td, newtd);
159 
160 	TD_SET_CAN_RUN(newtd);
161 	if ((uap->flags & THR_SUSPENDED) == 0)
162 		setrunqueue(newtd, SRQ_BORING);
163 
164 	mtx_unlock_spin(&sched_lock);
165 
166 out:
167 	return (error);
168 }
169 
170 int
171 thr_self(struct thread *td, struct thr_self_args *uap)
172     /* long *id */
173 {
174 	long id;
175 	int error;
176 
177 	id = td->td_tid;
178 	if ((error = copyout(&id, uap->id, sizeof(long))))
179 		return (error);
180 
181 	return (0);
182 }
183 
184 int
185 thr_exit(struct thread *td, struct thr_exit_args *uap)
186     /* NULL */
187 {
188 	struct proc *p;
189 
190 	p = td->td_proc;
191 
192 	PROC_LOCK(p);
193 	mtx_lock_spin(&sched_lock);
194 
195 	/*
196 	 * Shutting down last thread in the proc.  This will actually
197 	 * call exit() in the trampoline when it returns.
198 	 */
199 	if (p->p_numthreads != 1) {
200 		thread_exit();
201 		/* NOTREACHED */
202 	}
203 	mtx_unlock_spin(&sched_lock);
204 	PROC_UNLOCK(p);
205 	return (0);
206 }
207 
208 int
209 thr_kill(struct thread *td, struct thr_kill_args *uap)
210     /* long id, int sig */
211 {
212 	struct thread *ttd;
213 	struct proc *p;
214 	int error;
215 
216 	p = td->td_proc;
217 	error = 0;
218 	PROC_LOCK(p);
219 	FOREACH_THREAD_IN_PROC(p, ttd) {
220 		if (ttd->td_tid == uap->id)
221 			break;
222 	}
223 	if (ttd == NULL) {
224 		error = ESRCH;
225 		goto out;
226 	}
227 	if (uap->sig == 0)
228 		goto out;
229 	if (!_SIG_VALID(uap->sig)) {
230 		error = EINVAL;
231 		goto out;
232 	}
233 	tdsignal(ttd, uap->sig, SIGTARGET_TD);
234 out:
235 	PROC_UNLOCK(p);
236 	return (error);
237 }
238 
239 int
240 thr_suspend(struct thread *td, struct thr_suspend_args *uap)
241 	/* const struct timespec *timeout */
242 {
243 	struct timespec ts;
244 	struct timeval	tv;
245 	int error;
246 	int hz;
247 
248 	hz = 0;
249 	error = 0;
250 	if (uap->timeout != NULL) {
251 		error = copyin((const void *)uap->timeout, (void *)&ts,
252 		    sizeof(struct timespec));
253 		if (error != 0)
254 			return (error);
255 		if (ts.tv_nsec < 0 || ts.tv_nsec > 1000000000)
256 			return (EINVAL);
257 		if (ts.tv_sec == 0 && ts.tv_nsec == 0)
258 			return (ETIMEDOUT);
259 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
260 		hz = tvtohz(&tv);
261 	}
262 	PROC_LOCK(td->td_proc);
263 	if ((td->td_flags & TDF_THRWAKEUP) == 0)
264 		error = msleep((void *)td, &td->td_proc->p_mtx,
265 		    td->td_priority | PCATCH, "lthr", hz);
266 	mtx_lock_spin(&sched_lock);
267 	td->td_flags &= ~TDF_THRWAKEUP;
268 	mtx_unlock_spin(&sched_lock);
269 	PROC_UNLOCK(td->td_proc);
270 	return (error == EWOULDBLOCK ? ETIMEDOUT : error);
271 }
272 
273 int
274 thr_wake(struct thread *td, struct thr_wake_args *uap)
275 	/* long id */
276 {
277 	struct thread *ttd;
278 
279 	PROC_LOCK(td->td_proc);
280 	FOREACH_THREAD_IN_PROC(td->td_proc, ttd) {
281 		if (ttd->td_tid == uap->id)
282 			break;
283 	}
284 	if (ttd == NULL) {
285 		PROC_UNLOCK(td->td_proc);
286 		return (ESRCH);
287 	}
288 	mtx_lock_spin(&sched_lock);
289 	ttd->td_flags |= TDF_THRWAKEUP;
290 	mtx_unlock_spin(&sched_lock);
291 	wakeup_one((void *)ttd);
292 	PROC_UNLOCK(td->td_proc);
293 	return (0);
294 }
295