FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_thr.c
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: releng/11.2/sys/kern/kern_thr.c 331842 2018-03-31 13:19:27Z kib $");
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/ptrace.h>
40 #include <sys/racct.h>
41 #include <sys/resourcevar.h>
42 #include <sys/rwlock.h>
43 #include <sys/sched.h>
44 #include <sys/sysctl.h>
45 #include <sys/smp.h>
46 #include <sys/syscallsubr.h>
47 #include <sys/sysent.h>
48 #include <sys/systm.h>
49 #include <sys/sysproto.h>
50 #include <sys/signalvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/ucontext.h>
53 #include <sys/thr.h>
54 #include <sys/rtprio.h>
55 #include <sys/umtx.h>
56 #include <sys/limits.h>
57
58 #include <vm/vm_domain.h>
59
60 #include <machine/frame.h>
61
62 #include <security/audit/audit.h>
63
64 static SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0,
65 "thread allocation");
66
67 static int max_threads_per_proc = 1500;
68 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW,
69 &max_threads_per_proc, 0, "Limit on threads per proc");
70
71 static int max_threads_hits;
72 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD,
73 &max_threads_hits, 0, "kern.threads.max_threads_per_proc hit count");
74
75 #ifdef COMPAT_FREEBSD32
76
77 static inline int
78 suword_lwpid(void *addr, lwpid_t lwpid)
79 {
80 int error;
81
82 if (SV_CURPROC_FLAG(SV_LP64))
83 error = suword(addr, lwpid);
84 else
85 error = suword32(addr, lwpid);
86 return (error);
87 }
88
89 #else
90 #define suword_lwpid suword
91 #endif
92
93 /*
94 * System call interface.
95 */
96
97 struct thr_create_initthr_args {
98 ucontext_t ctx;
99 long *tid;
100 };
101
102 static int
103 thr_create_initthr(struct thread *td, void *thunk)
104 {
105 struct thr_create_initthr_args *args;
106
107 /* Copy out the child tid. */
108 args = thunk;
109 if (args->tid != NULL && suword_lwpid(args->tid, td->td_tid))
110 return (EFAULT);
111
112 return (set_mcontext(td, &args->ctx.uc_mcontext));
113 }
114
115 int
116 sys_thr_create(struct thread *td, struct thr_create_args *uap)
117 /* ucontext_t *ctx, long *id, int flags */
118 {
119 struct thr_create_initthr_args args;
120 int error;
121
122 if ((error = copyin(uap->ctx, &args.ctx, sizeof(args.ctx))))
123 return (error);
124 args.tid = uap->id;
125 return (thread_create(td, NULL, thr_create_initthr, &args));
126 }
127
128 int
129 sys_thr_new(struct thread *td, struct thr_new_args *uap)
130 /* struct thr_param * */
131 {
132 struct thr_param param;
133 int error;
134
135 if (uap->param_size < 0 || uap->param_size > sizeof(param))
136 return (EINVAL);
137 bzero(¶m, sizeof(param));
138 if ((error = copyin(uap->param, ¶m, uap->param_size)))
139 return (error);
140 return (kern_thr_new(td, ¶m));
141 }
142
143 static int
144 thr_new_initthr(struct thread *td, void *thunk)
145 {
146 stack_t stack;
147 struct thr_param *param;
148
149 /*
150 * Here we copy out tid to two places, one for child and one
151 * for parent, because pthread can create a detached thread,
152 * if parent wants to safely access child tid, it has to provide
153 * its storage, because child thread may exit quickly and
154 * memory is freed before parent thread can access it.
155 */
156 param = thunk;
157 if ((param->child_tid != NULL &&
158 suword_lwpid(param->child_tid, td->td_tid)) ||
159 (param->parent_tid != NULL &&
160 suword_lwpid(param->parent_tid, td->td_tid)))
161 return (EFAULT);
162
163 /* Set up our machine context. */
164 stack.ss_sp = param->stack_base;
165 stack.ss_size = param->stack_size;
166 /* Set upcall address to user thread entry function. */
167 cpu_set_upcall(td, param->start_func, param->arg, &stack);
168 /* Setup user TLS address and TLS pointer register. */
169 return (cpu_set_user_tls(td, param->tls_base));
170 }
171
172 int
173 kern_thr_new(struct thread *td, struct thr_param *param)
174 {
175 struct rtprio rtp, *rtpp;
176 int error;
177
178 rtpp = NULL;
179 if (param->rtp != 0) {
180 error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
181 if (error)
182 return (error);
183 rtpp = &rtp;
184 }
185 return (thread_create(td, rtpp, thr_new_initthr, param));
186 }
187
188 int
189 thread_create(struct thread *td, struct rtprio *rtp,
190 int (*initialize_thread)(struct thread *, void *), void *thunk)
191 {
192 struct thread *newtd;
193 struct proc *p;
194 int error;
195
196 p = td->td_proc;
197
198 if (rtp != NULL) {
199 switch(rtp->type) {
200 case RTP_PRIO_REALTIME:
201 case RTP_PRIO_FIFO:
202 /* Only root can set scheduler policy */
203 if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
204 return (EPERM);
205 if (rtp->prio > RTP_PRIO_MAX)
206 return (EINVAL);
207 break;
208 case RTP_PRIO_NORMAL:
209 rtp->prio = 0;
210 break;
211 default:
212 return (EINVAL);
213 }
214 }
215
216 #ifdef RACCT
217 if (racct_enable) {
218 PROC_LOCK(p);
219 error = racct_add(p, RACCT_NTHR, 1);
220 PROC_UNLOCK(p);
221 if (error != 0)
222 return (EPROCLIM);
223 }
224 #endif
225
226 /* Initialize our td */
227 error = kern_thr_alloc(p, 0, &newtd);
228 if (error)
229 goto fail;
230
231 cpu_copy_thread(newtd, td);
232
233 bzero(&newtd->td_startzero,
234 __rangeof(struct thread, td_startzero, td_endzero));
235 newtd->td_sleeptimo = 0;
236 newtd->td_vslock_sz = 0;
237 bzero(&newtd->td_si, sizeof(newtd->td_si));
238 bcopy(&td->td_startcopy, &newtd->td_startcopy,
239 __rangeof(struct thread, td_startcopy, td_endcopy));
240 newtd->td_sa = td->td_sa;
241 newtd->td_proc = td->td_proc;
242 newtd->td_rb_list = newtd->td_rbp_list = newtd->td_rb_inact = 0;
243 thread_cow_get(newtd, td);
244
245 error = initialize_thread(newtd, thunk);
246 if (error != 0) {
247 thread_cow_free(newtd);
248 thread_free(newtd);
249 goto fail;
250 }
251
252 PROC_LOCK(p);
253 p->p_flag |= P_HADTHREADS;
254 thread_link(newtd, p);
255 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
256 thread_lock(td);
257 /* let the scheduler know about these things. */
258 sched_fork_thread(td, newtd);
259 thread_unlock(td);
260 if (P_SHOULDSTOP(p))
261 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
262 if (p->p_ptevents & PTRACE_LWP)
263 newtd->td_dbgflags |= TDB_BORN;
264
265 /*
266 * Copy the existing thread VM policy into the new thread.
267 */
268 vm_domain_policy_localcopy(&newtd->td_vm_dom_policy,
269 &td->td_vm_dom_policy);
270
271 PROC_UNLOCK(p);
272
273 tidhash_add(newtd);
274
275 thread_lock(newtd);
276 if (rtp != NULL) {
277 if (!(td->td_pri_class == PRI_TIMESHARE &&
278 rtp->type == RTP_PRIO_NORMAL)) {
279 rtp_to_pri(rtp, newtd);
280 sched_prio(newtd, newtd->td_user_pri);
281 } /* ignore timesharing class */
282 }
283 TD_SET_CAN_RUN(newtd);
284 sched_add(newtd, SRQ_BORING);
285 thread_unlock(newtd);
286
287 return (0);
288
289 fail:
290 #ifdef RACCT
291 if (racct_enable) {
292 PROC_LOCK(p);
293 racct_sub(p, RACCT_NTHR, 1);
294 PROC_UNLOCK(p);
295 }
296 #endif
297 return (error);
298 }
299
300 int
301 sys_thr_self(struct thread *td, struct thr_self_args *uap)
302 /* long *id */
303 {
304 int error;
305
306 error = suword_lwpid(uap->id, (unsigned)td->td_tid);
307 if (error == -1)
308 return (EFAULT);
309 return (0);
310 }
311
312 int
313 sys_thr_exit(struct thread *td, struct thr_exit_args *uap)
314 /* long *state */
315 {
316
317 umtx_thread_exit(td);
318
319 /* Signal userland that it can free the stack. */
320 if ((void *)uap->state != NULL) {
321 suword_lwpid(uap->state, 1);
322 kern_umtx_wake(td, uap->state, INT_MAX, 0);
323 }
324
325 return (kern_thr_exit(td));
326 }
327
328 int
329 kern_thr_exit(struct thread *td)
330 {
331 struct proc *p;
332
333 p = td->td_proc;
334
335 /*
336 * If all of the threads in a process call this routine to
337 * exit (e.g. all threads call pthread_exit()), exactly one
338 * thread should return to the caller to terminate the process
339 * instead of the thread.
340 *
341 * Checking p_numthreads alone is not sufficient since threads
342 * might be committed to terminating while the PROC_LOCK is
343 * dropped in either ptracestop() or while removing this thread
344 * from the tidhash. Instead, the p_pendingexits field holds
345 * the count of threads in either of those states and a thread
346 * is considered the "last" thread if all of the other threads
347 * in a process are already terminating.
348 */
349 PROC_LOCK(p);
350 if (p->p_numthreads == p->p_pendingexits + 1) {
351 /*
352 * Ignore attempts to shut down last thread in the
353 * proc. This will actually call _exit(2) in the
354 * usermode trampoline when it returns.
355 */
356 PROC_UNLOCK(p);
357 return (0);
358 }
359
360 p->p_pendingexits++;
361 td->td_dbgflags |= TDB_EXIT;
362 if (p->p_ptevents & PTRACE_LWP)
363 ptracestop(td, SIGTRAP, NULL);
364 PROC_UNLOCK(p);
365 tidhash_remove(td);
366 PROC_LOCK(p);
367 p->p_pendingexits--;
368
369 /*
370 * The check above should prevent all other threads from this
371 * process from exiting while the PROC_LOCK is dropped, so
372 * there must be at least one other thread other than the
373 * current thread.
374 */
375 KASSERT(p->p_numthreads > 1, ("too few threads"));
376 racct_sub(p, RACCT_NTHR, 1);
377 tdsigcleanup(td);
378 PROC_SLOCK(p);
379 thread_stopped(p);
380 thread_exit();
381 /* NOTREACHED */
382 }
383
384 int
385 sys_thr_kill(struct thread *td, struct thr_kill_args *uap)
386 /* long id, int sig */
387 {
388 ksiginfo_t ksi;
389 struct thread *ttd;
390 struct proc *p;
391 int error;
392
393 p = td->td_proc;
394 ksiginfo_init(&ksi);
395 ksi.ksi_signo = uap->sig;
396 ksi.ksi_code = SI_LWP;
397 ksi.ksi_pid = p->p_pid;
398 ksi.ksi_uid = td->td_ucred->cr_ruid;
399 if (uap->id == -1) {
400 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
401 error = EINVAL;
402 } else {
403 error = ESRCH;
404 PROC_LOCK(p);
405 FOREACH_THREAD_IN_PROC(p, ttd) {
406 if (ttd != td) {
407 error = 0;
408 if (uap->sig == 0)
409 break;
410 tdksignal(ttd, uap->sig, &ksi);
411 }
412 }
413 PROC_UNLOCK(p);
414 }
415 } else {
416 error = 0;
417 ttd = tdfind((lwpid_t)uap->id, p->p_pid);
418 if (ttd == NULL)
419 return (ESRCH);
420 if (uap->sig == 0)
421 ;
422 else if (!_SIG_VALID(uap->sig))
423 error = EINVAL;
424 else
425 tdksignal(ttd, uap->sig, &ksi);
426 PROC_UNLOCK(ttd->td_proc);
427 }
428 return (error);
429 }
430
431 int
432 sys_thr_kill2(struct thread *td, struct thr_kill2_args *uap)
433 /* pid_t pid, long id, int sig */
434 {
435 ksiginfo_t ksi;
436 struct thread *ttd;
437 struct proc *p;
438 int error;
439
440 AUDIT_ARG_SIGNUM(uap->sig);
441
442 ksiginfo_init(&ksi);
443 ksi.ksi_signo = uap->sig;
444 ksi.ksi_code = SI_LWP;
445 ksi.ksi_pid = td->td_proc->p_pid;
446 ksi.ksi_uid = td->td_ucred->cr_ruid;
447 if (uap->id == -1) {
448 if ((p = pfind(uap->pid)) == NULL)
449 return (ESRCH);
450 AUDIT_ARG_PROCESS(p);
451 error = p_cansignal(td, p, uap->sig);
452 if (error) {
453 PROC_UNLOCK(p);
454 return (error);
455 }
456 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
457 error = EINVAL;
458 } else {
459 error = ESRCH;
460 FOREACH_THREAD_IN_PROC(p, ttd) {
461 if (ttd != td) {
462 error = 0;
463 if (uap->sig == 0)
464 break;
465 tdksignal(ttd, uap->sig, &ksi);
466 }
467 }
468 }
469 PROC_UNLOCK(p);
470 } else {
471 ttd = tdfind((lwpid_t)uap->id, uap->pid);
472 if (ttd == NULL)
473 return (ESRCH);
474 p = ttd->td_proc;
475 AUDIT_ARG_PROCESS(p);
476 error = p_cansignal(td, p, uap->sig);
477 if (uap->sig == 0)
478 ;
479 else if (!_SIG_VALID(uap->sig))
480 error = EINVAL;
481 else
482 tdksignal(ttd, uap->sig, &ksi);
483 PROC_UNLOCK(p);
484 }
485 return (error);
486 }
487
488 int
489 sys_thr_suspend(struct thread *td, struct thr_suspend_args *uap)
490 /* const struct timespec *timeout */
491 {
492 struct timespec ts, *tsp;
493 int error;
494
495 tsp = NULL;
496 if (uap->timeout != NULL) {
497 error = umtx_copyin_timeout(uap->timeout, &ts);
498 if (error != 0)
499 return (error);
500 tsp = &ts;
501 }
502
503 return (kern_thr_suspend(td, tsp));
504 }
505
506 int
507 kern_thr_suspend(struct thread *td, struct timespec *tsp)
508 {
509 struct proc *p = td->td_proc;
510 struct timeval tv;
511 int error = 0;
512 int timo = 0;
513
514 if (td->td_pflags & TDP_WAKEUP) {
515 td->td_pflags &= ~TDP_WAKEUP;
516 return (0);
517 }
518
519 if (tsp != NULL) {
520 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
521 error = EWOULDBLOCK;
522 else {
523 TIMESPEC_TO_TIMEVAL(&tv, tsp);
524 timo = tvtohz(&tv);
525 }
526 }
527
528 PROC_LOCK(p);
529 if (error == 0 && (td->td_flags & TDF_THRWAKEUP) == 0)
530 error = msleep((void *)td, &p->p_mtx,
531 PCATCH, "lthr", timo);
532
533 if (td->td_flags & TDF_THRWAKEUP) {
534 thread_lock(td);
535 td->td_flags &= ~TDF_THRWAKEUP;
536 thread_unlock(td);
537 PROC_UNLOCK(p);
538 return (0);
539 }
540 PROC_UNLOCK(p);
541 if (error == EWOULDBLOCK)
542 error = ETIMEDOUT;
543 else if (error == ERESTART) {
544 if (timo != 0)
545 error = EINTR;
546 }
547 return (error);
548 }
549
550 int
551 sys_thr_wake(struct thread *td, struct thr_wake_args *uap)
552 /* long id */
553 {
554 struct proc *p;
555 struct thread *ttd;
556
557 if (uap->id == td->td_tid) {
558 td->td_pflags |= TDP_WAKEUP;
559 return (0);
560 }
561
562 p = td->td_proc;
563 ttd = tdfind((lwpid_t)uap->id, p->p_pid);
564 if (ttd == NULL)
565 return (ESRCH);
566 thread_lock(ttd);
567 ttd->td_flags |= TDF_THRWAKEUP;
568 thread_unlock(ttd);
569 wakeup((void *)ttd);
570 PROC_UNLOCK(p);
571 return (0);
572 }
573
574 int
575 sys_thr_set_name(struct thread *td, struct thr_set_name_args *uap)
576 {
577 struct proc *p;
578 char name[MAXCOMLEN + 1];
579 struct thread *ttd;
580 int error;
581
582 error = 0;
583 name[0] = '\0';
584 if (uap->name != NULL) {
585 error = copyinstr(uap->name, name, sizeof(name), NULL);
586 if (error == ENAMETOOLONG) {
587 error = copyin(uap->name, name, sizeof(name) - 1);
588 name[sizeof(name) - 1] = '\0';
589 }
590 if (error)
591 return (error);
592 }
593 p = td->td_proc;
594 ttd = tdfind((lwpid_t)uap->id, p->p_pid);
595 if (ttd == NULL)
596 return (ESRCH);
597 strcpy(ttd->td_name, name);
598 #ifdef KTR
599 sched_clear_tdname(ttd);
600 #endif
601 PROC_UNLOCK(p);
602 return (error);
603 }
604
605 int
606 kern_thr_alloc(struct proc *p, int pages, struct thread **ntd)
607 {
608
609 /* Have race condition but it is cheap. */
610 if (p->p_numthreads >= max_threads_per_proc) {
611 ++max_threads_hits;
612 return (EPROCLIM);
613 }
614
615 *ntd = thread_alloc(pages);
616 if (*ntd == NULL)
617 return (ENOMEM);
618
619 return (0);
620 }
Cache object: da90865be8645bfe35524c40c281a5f3
|