kern_thr.c revision 284665
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: stable/10/sys/kern/kern_thr.c 284665 2015-06-21 06:28:26Z trasz $");
29
30#include "opt_compat.h"
31#include "opt_posix.h"
32#include <sys/param.h>
33#include <sys/kernel.h>
34#include <sys/lock.h>
35#include <sys/mutex.h>
36#include <sys/priv.h>
37#include <sys/proc.h>
38#include <sys/posix4.h>
39#include <sys/racct.h>
40#include <sys/resourcevar.h>
41#include <sys/rwlock.h>
42#include <sys/sched.h>
43#include <sys/sysctl.h>
44#include <sys/smp.h>
45#include <sys/syscallsubr.h>
46#include <sys/sysent.h>
47#include <sys/systm.h>
48#include <sys/sysproto.h>
49#include <sys/signalvar.h>
50#include <sys/sysctl.h>
51#include <sys/ucontext.h>
52#include <sys/thr.h>
53#include <sys/rtprio.h>
54#include <sys/umtx.h>
55#include <sys/limits.h>
56
57#include <machine/frame.h>
58
59#include <security/audit/audit.h>
60
61static SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0,
62    "thread allocation");
63
64static int max_threads_per_proc = 1500;
65SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW,
66    &max_threads_per_proc, 0, "Limit on threads per proc");
67
68static int max_threads_hits;
69SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD,
70    &max_threads_hits, 0, "kern.threads.max_threads_per_proc hit count");
71
72#ifdef COMPAT_FREEBSD32
73
74static inline int
75suword_lwpid(void *addr, lwpid_t lwpid)
76{
77	int error;
78
79	if (SV_CURPROC_FLAG(SV_LP64))
80		error = suword(addr, lwpid);
81	else
82		error = suword32(addr, lwpid);
83	return (error);
84}
85
86#else
87#define suword_lwpid	suword
88#endif
89
90static int create_thread(struct thread *td, mcontext_t *ctx,
91			 void (*start_func)(void *), void *arg,
92			 char *stack_base, size_t stack_size,
93			 char *tls_base,
94			 long *child_tid, long *parent_tid,
95			 int flags, struct rtprio *rtp);
96
97/*
98 * System call interface.
99 */
100int
101sys_thr_create(struct thread *td, struct thr_create_args *uap)
102    /* ucontext_t *ctx, long *id, int flags */
103{
104	ucontext_t ctx;
105	int error;
106
107	if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
108		return (error);
109
110	error = create_thread(td, &ctx.uc_mcontext, NULL, NULL,
111		NULL, 0, NULL, uap->id, NULL, uap->flags, NULL);
112	return (error);
113}
114
115int
116sys_thr_new(struct thread *td, struct thr_new_args *uap)
117    /* struct thr_param * */
118{
119	struct thr_param param;
120	int error;
121
122	if (uap->param_size < 0 || uap->param_size > sizeof(param))
123		return (EINVAL);
124	bzero(&param, sizeof(param));
125	if ((error = copyin(uap->param, &param, uap->param_size)))
126		return (error);
127	return (kern_thr_new(td, &param));
128}
129
130int
131kern_thr_new(struct thread *td, struct thr_param *param)
132{
133	struct rtprio rtp, *rtpp;
134	int error;
135
136	rtpp = NULL;
137	if (param->rtp != 0) {
138		error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
139		if (error)
140			return (error);
141		rtpp = &rtp;
142	}
143	error = create_thread(td, NULL, param->start_func, param->arg,
144		param->stack_base, param->stack_size, param->tls_base,
145		param->child_tid, param->parent_tid, param->flags,
146		rtpp);
147	return (error);
148}
149
150static int
151create_thread(struct thread *td, mcontext_t *ctx,
152	    void (*start_func)(void *), void *arg,
153	    char *stack_base, size_t stack_size,
154	    char *tls_base,
155	    long *child_tid, long *parent_tid,
156	    int flags, struct rtprio *rtp)
157{
158	stack_t stack;
159	struct thread *newtd;
160	struct proc *p;
161	int error;
162
163	p = td->td_proc;
164
165	/* Have race condition but it is cheap. */
166	if (p->p_numthreads >= max_threads_per_proc) {
167		++max_threads_hits;
168		return (EPROCLIM);
169	}
170
171	if (rtp != NULL) {
172		switch(rtp->type) {
173		case RTP_PRIO_REALTIME:
174		case RTP_PRIO_FIFO:
175			/* Only root can set scheduler policy */
176			if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
177				return (EPERM);
178			if (rtp->prio > RTP_PRIO_MAX)
179				return (EINVAL);
180			break;
181		case RTP_PRIO_NORMAL:
182			rtp->prio = 0;
183			break;
184		default:
185			return (EINVAL);
186		}
187	}
188
189#ifdef RACCT
190	PROC_LOCK(td->td_proc);
191	error = racct_add(p, RACCT_NTHR, 1);
192	PROC_UNLOCK(td->td_proc);
193	if (error != 0)
194		return (EPROCLIM);
195#endif
196
197	/* Initialize our td */
198	newtd = thread_alloc(0);
199	if (newtd == NULL) {
200		error = ENOMEM;
201		goto fail;
202	}
203
204	cpu_set_upcall(newtd, td);
205
206	/*
207	 * Try the copyout as soon as we allocate the td so we don't
208	 * have to tear things down in a failure case below.
209	 * Here we copy out tid to two places, one for child and one
210	 * for parent, because pthread can create a detached thread,
211	 * if parent wants to safely access child tid, it has to provide
212	 * its storage, because child thread may exit quickly and
213	 * memory is freed before parent thread can access it.
214	 */
215	if ((child_tid != NULL &&
216	    suword_lwpid(child_tid, newtd->td_tid)) ||
217	    (parent_tid != NULL &&
218	    suword_lwpid(parent_tid, newtd->td_tid))) {
219		thread_free(newtd);
220		error = EFAULT;
221		goto fail;
222	}
223
224	bzero(&newtd->td_startzero,
225	    __rangeof(struct thread, td_startzero, td_endzero));
226	newtd->td_su = NULL;
227	bcopy(&td->td_startcopy, &newtd->td_startcopy,
228	    __rangeof(struct thread, td_startcopy, td_endcopy));
229	newtd->td_proc = td->td_proc;
230	newtd->td_ucred = crhold(td->td_ucred);
231
232	if (ctx != NULL) { /* old way to set user context */
233		error = set_mcontext(newtd, ctx);
234		if (error != 0) {
235			thread_free(newtd);
236			crfree(td->td_ucred);
237			goto fail;
238		}
239	} else {
240		/* Set up our machine context. */
241		stack.ss_sp = stack_base;
242		stack.ss_size = stack_size;
243		/* Set upcall address to user thread entry function. */
244		cpu_set_upcall_kse(newtd, start_func, arg, &stack);
245		/* Setup user TLS address and TLS pointer register. */
246		error = cpu_set_user_tls(newtd, tls_base);
247		if (error != 0) {
248			thread_free(newtd);
249			crfree(td->td_ucred);
250			goto fail;
251		}
252	}
253
254	PROC_LOCK(td->td_proc);
255	td->td_proc->p_flag |= P_HADTHREADS;
256	thread_link(newtd, p);
257	bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
258	thread_lock(td);
259	/* let the scheduler know about these things. */
260	sched_fork_thread(td, newtd);
261	thread_unlock(td);
262	if (P_SHOULDSTOP(p))
263		newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
264	PROC_UNLOCK(p);
265
266	tidhash_add(newtd);
267
268	thread_lock(newtd);
269	if (rtp != NULL) {
270		if (!(td->td_pri_class == PRI_TIMESHARE &&
271		      rtp->type == RTP_PRIO_NORMAL)) {
272			rtp_to_pri(rtp, newtd);
273			sched_prio(newtd, newtd->td_user_pri);
274		} /* ignore timesharing class */
275	}
276	TD_SET_CAN_RUN(newtd);
277	sched_add(newtd, SRQ_BORING);
278	thread_unlock(newtd);
279
280	return (0);
281
282fail:
283#ifdef RACCT
284	if (racct_enable) {
285		PROC_LOCK(p);
286		racct_sub(p, RACCT_NTHR, 1);
287		PROC_UNLOCK(p);
288	}
289#endif
290	return (error);
291}
292
293int
294sys_thr_self(struct thread *td, struct thr_self_args *uap)
295    /* long *id */
296{
297	int error;
298
299	error = suword_lwpid(uap->id, (unsigned)td->td_tid);
300	if (error == -1)
301		return (EFAULT);
302	return (0);
303}
304
305int
306sys_thr_exit(struct thread *td, struct thr_exit_args *uap)
307    /* long *state */
308{
309	struct proc *p;
310
311	p = td->td_proc;
312
313	/* Signal userland that it can free the stack. */
314	if ((void *)uap->state != NULL) {
315		suword_lwpid(uap->state, 1);
316		kern_umtx_wake(td, uap->state, INT_MAX, 0);
317	}
318
319	rw_wlock(&tidhash_lock);
320
321	PROC_LOCK(p);
322
323	if (p->p_numthreads != 1) {
324		racct_sub(p, RACCT_NTHR, 1);
325		LIST_REMOVE(td, td_hash);
326		rw_wunlock(&tidhash_lock);
327		tdsigcleanup(td);
328		umtx_thread_exit(td);
329		PROC_SLOCK(p);
330		thread_stopped(p);
331		thread_exit();
332		/* NOTREACHED */
333	}
334
335	/*
336	 * Ignore attempts to shut down last thread in the proc.  This
337	 * will actually call _exit(2) in the usermode trampoline when
338	 * it returns.
339	 */
340	PROC_UNLOCK(p);
341	rw_wunlock(&tidhash_lock);
342	return (0);
343}
344
345int
346sys_thr_kill(struct thread *td, struct thr_kill_args *uap)
347    /* long id, int sig */
348{
349	ksiginfo_t ksi;
350	struct thread *ttd;
351	struct proc *p;
352	int error;
353
354	p = td->td_proc;
355	ksiginfo_init(&ksi);
356	ksi.ksi_signo = uap->sig;
357	ksi.ksi_code = SI_LWP;
358	ksi.ksi_pid = p->p_pid;
359	ksi.ksi_uid = td->td_ucred->cr_ruid;
360	if (uap->id == -1) {
361		if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
362			error = EINVAL;
363		} else {
364			error = ESRCH;
365			PROC_LOCK(p);
366			FOREACH_THREAD_IN_PROC(p, ttd) {
367				if (ttd != td) {
368					error = 0;
369					if (uap->sig == 0)
370						break;
371					tdksignal(ttd, uap->sig, &ksi);
372				}
373			}
374			PROC_UNLOCK(p);
375		}
376	} else {
377		error = 0;
378		ttd = tdfind((lwpid_t)uap->id, p->p_pid);
379		if (ttd == NULL)
380			return (ESRCH);
381		if (uap->sig == 0)
382			;
383		else if (!_SIG_VALID(uap->sig))
384			error = EINVAL;
385		else
386			tdksignal(ttd, uap->sig, &ksi);
387		PROC_UNLOCK(ttd->td_proc);
388	}
389	return (error);
390}
391
392int
393sys_thr_kill2(struct thread *td, struct thr_kill2_args *uap)
394    /* pid_t pid, long id, int sig */
395{
396	ksiginfo_t ksi;
397	struct thread *ttd;
398	struct proc *p;
399	int error;
400
401	AUDIT_ARG_SIGNUM(uap->sig);
402
403	ksiginfo_init(&ksi);
404	ksi.ksi_signo = uap->sig;
405	ksi.ksi_code = SI_LWP;
406	ksi.ksi_pid = td->td_proc->p_pid;
407	ksi.ksi_uid = td->td_ucred->cr_ruid;
408	if (uap->id == -1) {
409		if ((p = pfind(uap->pid)) == NULL)
410			return (ESRCH);
411		AUDIT_ARG_PROCESS(p);
412		error = p_cansignal(td, p, uap->sig);
413		if (error) {
414			PROC_UNLOCK(p);
415			return (error);
416		}
417		if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
418			error = EINVAL;
419		} else {
420			error = ESRCH;
421			FOREACH_THREAD_IN_PROC(p, ttd) {
422				if (ttd != td) {
423					error = 0;
424					if (uap->sig == 0)
425						break;
426					tdksignal(ttd, uap->sig, &ksi);
427				}
428			}
429		}
430		PROC_UNLOCK(p);
431	} else {
432		ttd = tdfind((lwpid_t)uap->id, uap->pid);
433		if (ttd == NULL)
434			return (ESRCH);
435		p = ttd->td_proc;
436		AUDIT_ARG_PROCESS(p);
437		error = p_cansignal(td, p, uap->sig);
438		if (uap->sig == 0)
439			;
440		else if (!_SIG_VALID(uap->sig))
441			error = EINVAL;
442		else
443			tdksignal(ttd, uap->sig, &ksi);
444		PROC_UNLOCK(p);
445	}
446	return (error);
447}
448
449int
450sys_thr_suspend(struct thread *td, struct thr_suspend_args *uap)
451	/* const struct timespec *timeout */
452{
453	struct timespec ts, *tsp;
454	int error;
455
456	tsp = NULL;
457	if (uap->timeout != NULL) {
458		error = umtx_copyin_timeout(uap->timeout, &ts);
459		if (error != 0)
460			return (error);
461		tsp = &ts;
462	}
463
464	return (kern_thr_suspend(td, tsp));
465}
466
467int
468kern_thr_suspend(struct thread *td, struct timespec *tsp)
469{
470	struct proc *p = td->td_proc;
471	struct timeval tv;
472	int error = 0;
473	int timo = 0;
474
475	if (td->td_pflags & TDP_WAKEUP) {
476		td->td_pflags &= ~TDP_WAKEUP;
477		return (0);
478	}
479
480	if (tsp != NULL) {
481		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
482			error = EWOULDBLOCK;
483		else {
484			TIMESPEC_TO_TIMEVAL(&tv, tsp);
485			timo = tvtohz(&tv);
486		}
487	}
488
489	PROC_LOCK(p);
490	if (error == 0 && (td->td_flags & TDF_THRWAKEUP) == 0)
491		error = msleep((void *)td, &p->p_mtx,
492			 PCATCH, "lthr", timo);
493
494	if (td->td_flags & TDF_THRWAKEUP) {
495		thread_lock(td);
496		td->td_flags &= ~TDF_THRWAKEUP;
497		thread_unlock(td);
498		PROC_UNLOCK(p);
499		return (0);
500	}
501	PROC_UNLOCK(p);
502	if (error == EWOULDBLOCK)
503		error = ETIMEDOUT;
504	else if (error == ERESTART) {
505		if (timo != 0)
506			error = EINTR;
507	}
508	return (error);
509}
510
511int
512sys_thr_wake(struct thread *td, struct thr_wake_args *uap)
513	/* long id */
514{
515	struct proc *p;
516	struct thread *ttd;
517
518	if (uap->id == td->td_tid) {
519		td->td_pflags |= TDP_WAKEUP;
520		return (0);
521	}
522
523	p = td->td_proc;
524	ttd = tdfind((lwpid_t)uap->id, p->p_pid);
525	if (ttd == NULL)
526		return (ESRCH);
527	thread_lock(ttd);
528	ttd->td_flags |= TDF_THRWAKEUP;
529	thread_unlock(ttd);
530	wakeup((void *)ttd);
531	PROC_UNLOCK(p);
532	return (0);
533}
534
535int
536sys_thr_set_name(struct thread *td, struct thr_set_name_args *uap)
537{
538	struct proc *p;
539	char name[MAXCOMLEN + 1];
540	struct thread *ttd;
541	int error;
542
543	error = 0;
544	name[0] = '\0';
545	if (uap->name != NULL) {
546		error = copyinstr(uap->name, name, sizeof(name),
547			NULL);
548		if (error)
549			return (error);
550	}
551	p = td->td_proc;
552	ttd = tdfind((lwpid_t)uap->id, p->p_pid);
553	if (ttd == NULL)
554		return (ESRCH);
555	strcpy(ttd->td_name, name);
556#ifdef KTR
557	sched_clear_tdname(ttd);
558#endif
559	PROC_UNLOCK(p);
560	return (error);
561}
562