kern_thread.c revision 113641
1/* 2 * Copyright (C) 2001 Julian Elischer <julian@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(s), this list of conditions and the following disclaimer as 10 * the first lines of this file unmodified other than the possible 11 * addition of one or more copyright notices. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice(s), this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 26 * DAMAGE. 27 * 28 * $FreeBSD: head/sys/kern/kern_thread.c 113641 2003-04-18 00:16:13Z julian $ 29 */ 30 31#include <sys/param.h> 32#include <sys/systm.h> 33#include <sys/kernel.h> 34#include <sys/lock.h> 35#include <sys/malloc.h> 36#include <sys/mutex.h> 37#include <sys/proc.h> 38#include <sys/smp.h> 39#include <sys/sysctl.h> 40#include <sys/sysproto.h> 41#include <sys/filedesc.h> 42#include <sys/sched.h> 43#include <sys/signalvar.h> 44#include <sys/sx.h> 45#include <sys/tty.h> 46#include <sys/user.h> 47#include <sys/jail.h> 48#include <sys/kse.h> 49#include <sys/ktr.h> 50#include <sys/ucontext.h> 51 52#include <vm/vm.h> 53#include <vm/vm_object.h> 54#include <vm/pmap.h> 55#include <vm/uma.h> 56#include <vm/vm_map.h> 57 58#include <machine/frame.h> 59 60/* 61 * KSEGRP related storage. 62 */ 63static uma_zone_t ksegrp_zone; 64static uma_zone_t kse_zone; 65static uma_zone_t thread_zone; 66static uma_zone_t upcall_zone; 67 68/* DEBUG ONLY */ 69SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation"); 70static int thread_debug = 0; 71SYSCTL_INT(_kern_threads, OID_AUTO, debug, CTLFLAG_RW, 72 &thread_debug, 0, "thread debug"); 73 74static int max_threads_per_proc = 30; 75SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW, 76 &max_threads_per_proc, 0, "Limit on threads per proc"); 77 78static int max_groups_per_proc = 5; 79SYSCTL_INT(_kern_threads, OID_AUTO, max_groups_per_proc, CTLFLAG_RW, 80 &max_groups_per_proc, 0, "Limit on thread groups per proc"); 81 82static int max_threads_hits; 83SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD, 84 &max_threads_hits, 0, ""); 85 86static int virtual_cpu; 87 88#define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start)) 89 90TAILQ_HEAD(, thread) zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads); 91TAILQ_HEAD(, kse) zombie_kses = TAILQ_HEAD_INITIALIZER(zombie_kses); 92TAILQ_HEAD(, ksegrp) zombie_ksegrps = TAILQ_HEAD_INITIALIZER(zombie_ksegrps); 93TAILQ_HEAD(, kse_upcall) zombie_upcalls = 94 TAILQ_HEAD_INITIALIZER(zombie_upcalls); 95struct mtx kse_zombie_lock; 96MTX_SYSINIT(kse_zombie_lock, &kse_zombie_lock, "kse zombie lock", MTX_SPIN); 97 98static void kse_purge(struct proc *p, struct thread *td); 99static void kse_purge_group(struct thread *td); 100static int thread_update_usr_ticks(struct thread *td, int user); 101static void thread_alloc_spare(struct thread *td, struct thread *spare); 102 103static int 104sysctl_kse_virtual_cpu(SYSCTL_HANDLER_ARGS) 105{ 106 int error, new_val; 107 int def_val; 108 109#ifdef SMP 110 def_val = mp_ncpus; 111#else 112 def_val = 1; 113#endif 114 if (virtual_cpu == 0) 115 new_val = def_val; 116 else 117 new_val = virtual_cpu; 118 error = sysctl_handle_int(oidp, &new_val, 0, req); 119 if (error != 0 || req->newptr == NULL) 120 return (error); 121 if (new_val < 0) 122 return (EINVAL); 123 virtual_cpu = new_val; 124 return (0); 125} 126 127/* DEBUG ONLY */ 128SYSCTL_PROC(_kern_threads, OID_AUTO, virtual_cpu, CTLTYPE_INT|CTLFLAG_RW, 129 0, sizeof(virtual_cpu), sysctl_kse_virtual_cpu, "I", 130 "debug virtual cpus"); 131 132/* 133 * Prepare a thread for use. 134 */ 135static void 136thread_ctor(void *mem, int size, void *arg) 137{ 138 struct thread *td; 139 140 td = (struct thread *)mem; 141 td->td_state = TDS_INACTIVE; 142 td->td_oncpu = NOCPU; 143} 144 145/* 146 * Reclaim a thread after use. 147 */ 148static void 149thread_dtor(void *mem, int size, void *arg) 150{ 151 struct thread *td; 152 153 td = (struct thread *)mem; 154 155#ifdef INVARIANTS 156 /* Verify that this thread is in a safe state to free. */ 157 switch (td->td_state) { 158 case TDS_INHIBITED: 159 case TDS_RUNNING: 160 case TDS_CAN_RUN: 161 case TDS_RUNQ: 162 /* 163 * We must never unlink a thread that is in one of 164 * these states, because it is currently active. 165 */ 166 panic("bad state for thread unlinking"); 167 /* NOTREACHED */ 168 case TDS_INACTIVE: 169 break; 170 default: 171 panic("bad thread state"); 172 /* NOTREACHED */ 173 } 174#endif 175} 176 177/* 178 * Initialize type-stable parts of a thread (when newly created). 179 */ 180static void 181thread_init(void *mem, int size) 182{ 183 struct thread *td; 184 185 td = (struct thread *)mem; 186 mtx_lock(&Giant); 187 pmap_new_thread(td, 0); 188 mtx_unlock(&Giant); 189 cpu_thread_setup(td); 190 td->td_sched = (struct td_sched *)&td[1]; 191} 192 193/* 194 * Tear down type-stable parts of a thread (just before being discarded). 195 */ 196static void 197thread_fini(void *mem, int size) 198{ 199 struct thread *td; 200 201 td = (struct thread *)mem; 202 pmap_dispose_thread(td); 203} 204 205/* 206 * Initialize type-stable parts of a kse (when newly created). 207 */ 208static void 209kse_init(void *mem, int size) 210{ 211 struct kse *ke; 212 213 ke = (struct kse *)mem; 214 ke->ke_sched = (struct ke_sched *)&ke[1]; 215} 216 217/* 218 * Initialize type-stable parts of a ksegrp (when newly created). 219 */ 220static void 221ksegrp_init(void *mem, int size) 222{ 223 struct ksegrp *kg; 224 225 kg = (struct ksegrp *)mem; 226 kg->kg_sched = (struct kg_sched *)&kg[1]; 227} 228 229/* 230 * KSE is linked into kse group. 231 */ 232void 233kse_link(struct kse *ke, struct ksegrp *kg) 234{ 235 struct proc *p = kg->kg_proc; 236 237 TAILQ_INSERT_HEAD(&kg->kg_kseq, ke, ke_kglist); 238 kg->kg_kses++; 239 ke->ke_state = KES_UNQUEUED; 240 ke->ke_proc = p; 241 ke->ke_ksegrp = kg; 242 ke->ke_thread = NULL; 243 ke->ke_oncpu = NOCPU; 244 ke->ke_flags = 0; 245} 246 247void 248kse_unlink(struct kse *ke) 249{ 250 struct ksegrp *kg; 251 252 mtx_assert(&sched_lock, MA_OWNED); 253 kg = ke->ke_ksegrp; 254 TAILQ_REMOVE(&kg->kg_kseq, ke, ke_kglist); 255 if (ke->ke_state == KES_IDLE) { 256 TAILQ_REMOVE(&kg->kg_iq, ke, ke_kgrlist); 257 kg->kg_idle_kses--; 258 } 259 if (--kg->kg_kses == 0) 260 ksegrp_unlink(kg); 261 /* 262 * Aggregate stats from the KSE 263 */ 264 kse_stash(ke); 265} 266 267void 268ksegrp_link(struct ksegrp *kg, struct proc *p) 269{ 270 271 TAILQ_INIT(&kg->kg_threads); 272 TAILQ_INIT(&kg->kg_runq); /* links with td_runq */ 273 TAILQ_INIT(&kg->kg_slpq); /* links with td_runq */ 274 TAILQ_INIT(&kg->kg_kseq); /* all kses in ksegrp */ 275 TAILQ_INIT(&kg->kg_iq); /* all idle kses in ksegrp */ 276 TAILQ_INIT(&kg->kg_upcalls); /* all upcall structure in ksegrp */ 277 kg->kg_proc = p; 278 /* 279 * the following counters are in the -zero- section 280 * and may not need clearing 281 */ 282 kg->kg_numthreads = 0; 283 kg->kg_runnable = 0; 284 kg->kg_kses = 0; 285 kg->kg_runq_kses = 0; /* XXXKSE change name */ 286 kg->kg_idle_kses = 0; 287 kg->kg_numupcalls = 0; 288 /* link it in now that it's consistent */ 289 p->p_numksegrps++; 290 TAILQ_INSERT_HEAD(&p->p_ksegrps, kg, kg_ksegrp); 291} 292 293void 294ksegrp_unlink(struct ksegrp *kg) 295{ 296 struct proc *p; 297 298 mtx_assert(&sched_lock, MA_OWNED); 299 KASSERT((kg->kg_numthreads == 0), ("ksegrp_unlink: residual threads")); 300 KASSERT((kg->kg_kses == 0), ("ksegrp_unlink: residual kses")); 301 KASSERT((kg->kg_numupcalls == 0), ("ksegrp_unlink: residual upcalls")); 302 303 p = kg->kg_proc; 304 TAILQ_REMOVE(&p->p_ksegrps, kg, kg_ksegrp); 305 p->p_numksegrps--; 306 /* 307 * Aggregate stats from the KSE 308 */ 309 ksegrp_stash(kg); 310} 311 312struct kse_upcall * 313upcall_alloc(void) 314{ 315 struct kse_upcall *ku; 316 317 ku = uma_zalloc(upcall_zone, M_WAITOK); 318 bzero(ku, sizeof(*ku)); 319 return (ku); 320} 321 322void 323upcall_free(struct kse_upcall *ku) 324{ 325 326 uma_zfree(upcall_zone, ku); 327} 328 329void 330upcall_link(struct kse_upcall *ku, struct ksegrp *kg) 331{ 332 333 mtx_assert(&sched_lock, MA_OWNED); 334 TAILQ_INSERT_TAIL(&kg->kg_upcalls, ku, ku_link); 335 ku->ku_ksegrp = kg; 336 kg->kg_numupcalls++; 337} 338 339void 340upcall_unlink(struct kse_upcall *ku) 341{ 342 struct ksegrp *kg = ku->ku_ksegrp; 343 344 mtx_assert(&sched_lock, MA_OWNED); 345 KASSERT(ku->ku_owner == NULL, ("%s: have owner", __func__)); 346 TAILQ_REMOVE(&kg->kg_upcalls, ku, ku_link); 347 kg->kg_numupcalls--; 348 upcall_stash(ku); 349} 350 351void 352upcall_remove(struct thread *td) 353{ 354 355 if (td->td_upcall) { 356 td->td_upcall->ku_owner = NULL; 357 upcall_unlink(td->td_upcall); 358 td->td_upcall = 0; 359 } 360} 361 362/* 363 * For a newly created process, 364 * link up all the structures and its initial threads etc. 365 */ 366void 367proc_linkup(struct proc *p, struct ksegrp *kg, 368 struct kse *ke, struct thread *td) 369{ 370 371 TAILQ_INIT(&p->p_ksegrps); /* all ksegrps in proc */ 372 TAILQ_INIT(&p->p_threads); /* all threads in proc */ 373 TAILQ_INIT(&p->p_suspended); /* Threads suspended */ 374 p->p_numksegrps = 0; 375 p->p_numthreads = 0; 376 377 ksegrp_link(kg, p); 378 kse_link(ke, kg); 379 thread_link(td, kg); 380} 381 382/* 383struct kse_thr_interrupt_args { 384 struct kse_thr_mailbox * tmbx; 385}; 386*/ 387int 388kse_thr_interrupt(struct thread *td, struct kse_thr_interrupt_args *uap) 389{ 390 struct proc *p; 391 struct thread *td2; 392 393 p = td->td_proc; 394 if (!(p->p_flag & P_THREADED) || (uap->tmbx == NULL)) 395 return (EINVAL); 396 mtx_lock_spin(&sched_lock); 397 FOREACH_THREAD_IN_PROC(p, td2) { 398 if (td2->td_mailbox == uap->tmbx) { 399 td2->td_flags |= TDF_INTERRUPT; 400 if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR)) { 401 if (td2->td_flags & TDF_CVWAITQ) 402 cv_abort(td2); 403 else 404 abortsleep(td2); 405 } 406 mtx_unlock_spin(&sched_lock); 407 return (0); 408 } 409 } 410 mtx_unlock_spin(&sched_lock); 411 return (ESRCH); 412} 413 414/* 415struct kse_exit_args { 416 register_t dummy; 417}; 418*/ 419int 420kse_exit(struct thread *td, struct kse_exit_args *uap) 421{ 422 struct proc *p; 423 struct ksegrp *kg; 424 struct kse *ke; 425 426 p = td->td_proc; 427 /* 428 * Only UTS can call the syscall and current group 429 * should be a threaded group. 430 */ 431 if ((td->td_mailbox != NULL) || (td->td_ksegrp->kg_numupcalls == 0)) 432 return (EINVAL); 433 KASSERT((td->td_upcall != NULL), ("%s: not own an upcall", __func__)); 434 435 kg = td->td_ksegrp; 436 /* Serialize removing upcall */ 437 PROC_LOCK(p); 438 mtx_lock_spin(&sched_lock); 439 if ((kg->kg_numupcalls == 1) && (kg->kg_numthreads > 1)) { 440 mtx_unlock_spin(&sched_lock); 441 PROC_UNLOCK(p); 442 return (EDEADLK); 443 } 444 ke = td->td_kse; 445 upcall_remove(td); 446 if (p->p_numthreads == 1) { 447 kse_purge(p, td); 448 p->p_flag &= ~P_THREADED; 449 mtx_unlock_spin(&sched_lock); 450 PROC_UNLOCK(p); 451 } else { 452 if (kg->kg_numthreads == 1) { /* Shutdown a group */ 453 kse_purge_group(td); 454 ke->ke_flags |= KEF_EXIT; 455 } 456 thread_stopped(p); 457 thread_exit(); 458 /* NOTREACHED */ 459 } 460 return (0); 461} 462 463/* 464 * Either becomes an upcall or waits for an awakening event and 465 * then becomes an upcall. Only error cases return. 466 */ 467/* 468struct kse_release_args { 469 struct timespec *timeout; 470}; 471*/ 472int 473kse_release(struct thread *td, struct kse_release_args *uap) 474{ 475 struct proc *p; 476 struct ksegrp *kg; 477 struct timespec ts, ts2, ts3, timeout; 478 struct timeval tv; 479 int error; 480 481 p = td->td_proc; 482 kg = td->td_ksegrp; 483 /* 484 * Only UTS can call the syscall and current group 485 * should be a threaded group. 486 */ 487 if ((td->td_mailbox != NULL) || (td->td_ksegrp->kg_numupcalls == 0)) 488 return (EINVAL); 489 KASSERT((td->td_upcall != NULL), ("%s: not own an upcall", __func__)); 490 if (uap->timeout != NULL) { 491 if ((error = copyin(uap->timeout, &timeout, sizeof(timeout)))) 492 return (error); 493 getnanouptime(&ts); 494 timespecadd(&ts, &timeout); 495 TIMESPEC_TO_TIMEVAL(&tv, &timeout); 496 } 497 mtx_lock_spin(&sched_lock); 498 /* Change OURSELF to become an upcall. */ 499 td->td_flags = TDF_UPCALLING; 500#if 0 /* XXX This shouldn't be necessary */ 501 if (p->p_sflag & PS_NEEDSIGCHK) 502 td->td_flags |= TDF_ASTPENDING; 503#endif 504 mtx_unlock_spin(&sched_lock); 505 PROC_LOCK(p); 506 while ((td->td_upcall->ku_flags & KUF_DOUPCALL) == 0 && 507 (kg->kg_completed == NULL)) { 508 kg->kg_upsleeps++; 509 error = msleep(&kg->kg_completed, &p->p_mtx, PPAUSE|PCATCH, 510 "kse_rel", (uap->timeout ? tvtohz(&tv) : 0)); 511 kg->kg_upsleeps--; 512 PROC_UNLOCK(p); 513 if (uap->timeout == NULL || error != EWOULDBLOCK) 514 return (0); 515 getnanouptime(&ts2); 516 if (timespeccmp(&ts2, &ts, >=)) 517 return (0); 518 ts3 = ts; 519 timespecsub(&ts3, &ts2); 520 TIMESPEC_TO_TIMEVAL(&tv, &ts3); 521 PROC_LOCK(p); 522 } 523 PROC_UNLOCK(p); 524 return (0); 525} 526 527/* struct kse_wakeup_args { 528 struct kse_mailbox *mbx; 529}; */ 530int 531kse_wakeup(struct thread *td, struct kse_wakeup_args *uap) 532{ 533 struct proc *p; 534 struct ksegrp *kg; 535 struct kse_upcall *ku; 536 struct thread *td2; 537 538 p = td->td_proc; 539 td2 = NULL; 540 ku = NULL; 541 /* KSE-enabled processes only, please. */ 542 if (!(p->p_flag & P_THREADED)) 543 return (EINVAL); 544 PROC_LOCK(p); 545 mtx_lock_spin(&sched_lock); 546 if (uap->mbx) { 547 FOREACH_KSEGRP_IN_PROC(p, kg) { 548 FOREACH_UPCALL_IN_GROUP(kg, ku) { 549 if (ku->ku_mailbox == uap->mbx) 550 break; 551 } 552 if (ku) 553 break; 554 } 555 } else { 556 kg = td->td_ksegrp; 557 if (kg->kg_upsleeps) { 558 wakeup_one(&kg->kg_completed); 559 mtx_unlock_spin(&sched_lock); 560 PROC_UNLOCK(p); 561 return (0); 562 } 563 ku = TAILQ_FIRST(&kg->kg_upcalls); 564 } 565 if (ku) { 566 if ((td2 = ku->ku_owner) == NULL) { 567 panic("%s: no owner", __func__); 568 } else if (TD_ON_SLEEPQ(td2) && 569 (td2->td_wchan == &kg->kg_completed)) { 570 abortsleep(td2); 571 } else { 572 ku->ku_flags |= KUF_DOUPCALL; 573 } 574 mtx_unlock_spin(&sched_lock); 575 PROC_UNLOCK(p); 576 return (0); 577 } 578 mtx_unlock_spin(&sched_lock); 579 PROC_UNLOCK(p); 580 return (ESRCH); 581} 582 583/* 584 * No new KSEG: first call: use current KSE, don't schedule an upcall 585 * All other situations, do allocate max new KSEs and schedule an upcall. 586 */ 587/* struct kse_create_args { 588 struct kse_mailbox *mbx; 589 int newgroup; 590}; */ 591int 592kse_create(struct thread *td, struct kse_create_args *uap) 593{ 594 struct kse *newke; 595 struct ksegrp *newkg; 596 struct ksegrp *kg; 597 struct proc *p; 598 struct kse_mailbox mbx; 599 struct kse_upcall *newku; 600 int err, ncpus; 601 602 p = td->td_proc; 603 if ((err = copyin(uap->mbx, &mbx, sizeof(mbx)))) 604 return (err); 605 606 /* Too bad, why hasn't kernel always a cpu counter !? */ 607#ifdef SMP 608 ncpus = mp_ncpus; 609#else 610 ncpus = 1; 611#endif 612 if (thread_debug && virtual_cpu != 0) 613 ncpus = virtual_cpu; 614 615 /* Easier to just set it than to test and set */ 616 PROC_LOCK(p); 617 p->p_flag |= P_THREADED; 618 PROC_UNLOCK(p); 619 kg = td->td_ksegrp; 620 if (uap->newgroup) { 621 /* Have race condition but it is cheap */ 622 if (p->p_numksegrps >= max_groups_per_proc) 623 return (EPROCLIM); 624 /* 625 * If we want a new KSEGRP it doesn't matter whether 626 * we have already fired up KSE mode before or not. 627 * We put the process in KSE mode and create a new KSEGRP. 628 */ 629 newkg = ksegrp_alloc(); 630 bzero(&newkg->kg_startzero, RANGEOF(struct ksegrp, 631 kg_startzero, kg_endzero)); 632 bcopy(&kg->kg_startcopy, &newkg->kg_startcopy, 633 RANGEOF(struct ksegrp, kg_startcopy, kg_endcopy)); 634 mtx_lock_spin(&sched_lock); 635 if (p->p_numksegrps >= max_groups_per_proc) { 636 mtx_unlock_spin(&sched_lock); 637 ksegrp_free(newkg); 638 return (EPROCLIM); 639 } 640 ksegrp_link(newkg, p); 641 mtx_unlock_spin(&sched_lock); 642 } else { 643 newkg = kg; 644 } 645 646 /* 647 * Creating upcalls more than number of physical cpu does 648 * not help performance. 649 */ 650 if (newkg->kg_numupcalls >= ncpus) 651 return (EPROCLIM); 652 653 if (newkg->kg_numupcalls == 0) { 654 /* 655 * Initialize KSE group, optimized for MP. 656 * Create KSEs as many as physical cpus, this increases 657 * concurrent even if userland is not MP safe and can only run 658 * on single CPU (for early version of libpthread, it is true). 659 * In ideal world, every physical cpu should execute a thread. 660 * If there is enough KSEs, threads in kernel can be 661 * executed parallel on different cpus with full speed, 662 * Concurrent in kernel shouldn't be restricted by number of 663 * upcalls userland provides. 664 * Adding more upcall structures only increases concurrent 665 * in userland. 666 * Highest performance configuration is: 667 * N kses = N upcalls = N phyiscal cpus 668 */ 669 while (newkg->kg_kses < ncpus) { 670 newke = kse_alloc(); 671 bzero(&newke->ke_startzero, RANGEOF(struct kse, 672 ke_startzero, ke_endzero)); 673#if 0 674 mtx_lock_spin(&sched_lock); 675 bcopy(&ke->ke_startcopy, &newke->ke_startcopy, 676 RANGEOF(struct kse, ke_startcopy, ke_endcopy)); 677 mtx_unlock_spin(&sched_lock); 678#endif 679 mtx_lock_spin(&sched_lock); 680 kse_link(newke, newkg); 681 /* Add engine */ 682 kse_reassign(newke); 683 mtx_unlock_spin(&sched_lock); 684 } 685 } 686 newku = upcall_alloc(); 687 newku->ku_mailbox = uap->mbx; 688 newku->ku_func = mbx.km_func; 689 bcopy(&mbx.km_stack, &newku->ku_stack, sizeof(stack_t)); 690 691 /* For the first call this may not have been set */ 692 if (td->td_standin == NULL) 693 thread_alloc_spare(td, NULL); 694 695 mtx_lock_spin(&sched_lock); 696 if (newkg->kg_numupcalls >= ncpus) { 697 mtx_unlock_spin(&sched_lock); 698 upcall_free(newku); 699 return (EPROCLIM); 700 } 701 upcall_link(newku, newkg); 702 if (mbx.km_quantum) 703 newkg->kg_upquantum = max(1, mbx.km_quantum/tick); 704 705 /* 706 * Each upcall structure has an owner thread, find which 707 * one owns it. 708 */ 709 if (uap->newgroup) { 710 /* 711 * Because new ksegrp hasn't thread, 712 * create an initial upcall thread to own it. 713 */ 714 thread_schedule_upcall(td, newku); 715 } else { 716 /* 717 * If current thread hasn't an upcall structure, 718 * just assign the upcall to it. 719 */ 720 if (td->td_upcall == NULL) { 721 newku->ku_owner = td; 722 td->td_upcall = newku; 723 } else { 724 /* 725 * Create a new upcall thread to own it. 726 */ 727 thread_schedule_upcall(td, newku); 728 } 729 } 730 mtx_unlock_spin(&sched_lock); 731 return (0); 732} 733 734/* 735 * Fill a ucontext_t with a thread's context information. 736 * 737 * This is an analogue to getcontext(3). 738 */ 739void 740thread_getcontext(struct thread *td, ucontext_t *uc) 741{ 742 743/* 744 * XXX this is declared in a MD include file, i386/include/ucontext.h but 745 * is used in MI code. 746 */ 747#ifdef __i386__ 748 get_mcontext(td, &uc->uc_mcontext); 749#endif 750 PROC_LOCK(td->td_proc); 751 uc->uc_sigmask = td->td_sigmask; 752 PROC_UNLOCK(td->td_proc); 753} 754 755/* 756 * Set a thread's context from a ucontext_t. 757 * 758 * This is an analogue to setcontext(3). 759 */ 760int 761thread_setcontext(struct thread *td, ucontext_t *uc) 762{ 763 int ret; 764 765/* 766 * XXX this is declared in a MD include file, i386/include/ucontext.h but 767 * is used in MI code. 768 */ 769#ifdef __i386__ 770 ret = set_mcontext(td, &uc->uc_mcontext); 771#else 772 ret = ENOSYS; 773#endif 774 if (ret == 0) { 775 SIG_CANTMASK(uc->uc_sigmask); 776 PROC_LOCK(td->td_proc); 777 td->td_sigmask = uc->uc_sigmask; 778 PROC_UNLOCK(td->td_proc); 779 } 780 return (ret); 781} 782 783/* 784 * Initialize global thread allocation resources. 785 */ 786void 787threadinit(void) 788{ 789 790#ifndef __ia64__ 791 thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(), 792 thread_ctor, thread_dtor, thread_init, thread_fini, 793 UMA_ALIGN_CACHE, 0); 794#else 795 /* 796 * XXX the ia64 kstack allocator is really lame and is at the mercy 797 * of contigmallloc(). This hackery is to pre-construct a whole 798 * pile of thread structures with associated kernel stacks early 799 * in the system startup while contigmalloc() still works. Once we 800 * have them, keep them. Sigh. 801 */ 802 thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(), 803 thread_ctor, thread_dtor, thread_init, thread_fini, 804 UMA_ALIGN_CACHE, UMA_ZONE_NOFREE); 805 uma_prealloc(thread_zone, 512); /* XXX arbitary */ 806#endif 807 ksegrp_zone = uma_zcreate("KSEGRP", sched_sizeof_ksegrp(), 808 NULL, NULL, ksegrp_init, NULL, 809 UMA_ALIGN_CACHE, 0); 810 kse_zone = uma_zcreate("KSE", sched_sizeof_kse(), 811 NULL, NULL, kse_init, NULL, 812 UMA_ALIGN_CACHE, 0); 813 upcall_zone = uma_zcreate("UPCALL", sizeof(struct kse_upcall), 814 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0); 815} 816 817/* 818 * Stash an embarasingly extra thread into the zombie thread queue. 819 */ 820void 821thread_stash(struct thread *td) 822{ 823 mtx_lock_spin(&kse_zombie_lock); 824 TAILQ_INSERT_HEAD(&zombie_threads, td, td_runq); 825 mtx_unlock_spin(&kse_zombie_lock); 826} 827 828/* 829 * Stash an embarasingly extra kse into the zombie kse queue. 830 */ 831void 832kse_stash(struct kse *ke) 833{ 834 mtx_lock_spin(&kse_zombie_lock); 835 TAILQ_INSERT_HEAD(&zombie_kses, ke, ke_procq); 836 mtx_unlock_spin(&kse_zombie_lock); 837} 838 839/* 840 * Stash an embarasingly extra upcall into the zombie upcall queue. 841 */ 842 843void 844upcall_stash(struct kse_upcall *ku) 845{ 846 mtx_lock_spin(&kse_zombie_lock); 847 TAILQ_INSERT_HEAD(&zombie_upcalls, ku, ku_link); 848 mtx_unlock_spin(&kse_zombie_lock); 849} 850 851/* 852 * Stash an embarasingly extra ksegrp into the zombie ksegrp queue. 853 */ 854void 855ksegrp_stash(struct ksegrp *kg) 856{ 857 mtx_lock_spin(&kse_zombie_lock); 858 TAILQ_INSERT_HEAD(&zombie_ksegrps, kg, kg_ksegrp); 859 mtx_unlock_spin(&kse_zombie_lock); 860} 861 862/* 863 * Reap zombie kse resource. 864 */ 865void 866thread_reap(void) 867{ 868 struct thread *td_first, *td_next; 869 struct kse *ke_first, *ke_next; 870 struct ksegrp *kg_first, * kg_next; 871 struct kse_upcall *ku_first, *ku_next; 872 873 /* 874 * Don't even bother to lock if none at this instant, 875 * we really don't care about the next instant.. 876 */ 877 if ((!TAILQ_EMPTY(&zombie_threads)) 878 || (!TAILQ_EMPTY(&zombie_kses)) 879 || (!TAILQ_EMPTY(&zombie_ksegrps)) 880 || (!TAILQ_EMPTY(&zombie_upcalls))) { 881 mtx_lock_spin(&kse_zombie_lock); 882 td_first = TAILQ_FIRST(&zombie_threads); 883 ke_first = TAILQ_FIRST(&zombie_kses); 884 kg_first = TAILQ_FIRST(&zombie_ksegrps); 885 ku_first = TAILQ_FIRST(&zombie_upcalls); 886 if (td_first) 887 TAILQ_INIT(&zombie_threads); 888 if (ke_first) 889 TAILQ_INIT(&zombie_kses); 890 if (kg_first) 891 TAILQ_INIT(&zombie_ksegrps); 892 if (ku_first) 893 TAILQ_INIT(&zombie_upcalls); 894 mtx_unlock_spin(&kse_zombie_lock); 895 while (td_first) { 896 td_next = TAILQ_NEXT(td_first, td_runq); 897 if (td_first->td_ucred) 898 crfree(td_first->td_ucred); 899 thread_free(td_first); 900 td_first = td_next; 901 } 902 while (ke_first) { 903 ke_next = TAILQ_NEXT(ke_first, ke_procq); 904 kse_free(ke_first); 905 ke_first = ke_next; 906 } 907 while (kg_first) { 908 kg_next = TAILQ_NEXT(kg_first, kg_ksegrp); 909 ksegrp_free(kg_first); 910 kg_first = kg_next; 911 } 912 while (ku_first) { 913 ku_next = TAILQ_NEXT(ku_first, ku_link); 914 upcall_free(ku_first); 915 ku_first = ku_next; 916 } 917 } 918} 919 920/* 921 * Allocate a ksegrp. 922 */ 923struct ksegrp * 924ksegrp_alloc(void) 925{ 926 return (uma_zalloc(ksegrp_zone, M_WAITOK)); 927} 928 929/* 930 * Allocate a kse. 931 */ 932struct kse * 933kse_alloc(void) 934{ 935 return (uma_zalloc(kse_zone, M_WAITOK)); 936} 937 938/* 939 * Allocate a thread. 940 */ 941struct thread * 942thread_alloc(void) 943{ 944 thread_reap(); /* check if any zombies to get */ 945 return (uma_zalloc(thread_zone, M_WAITOK)); 946} 947 948/* 949 * Deallocate a ksegrp. 950 */ 951void 952ksegrp_free(struct ksegrp *td) 953{ 954 uma_zfree(ksegrp_zone, td); 955} 956 957/* 958 * Deallocate a kse. 959 */ 960void 961kse_free(struct kse *td) 962{ 963 uma_zfree(kse_zone, td); 964} 965 966/* 967 * Deallocate a thread. 968 */ 969void 970thread_free(struct thread *td) 971{ 972 973 cpu_thread_clean(td); 974 uma_zfree(thread_zone, td); 975} 976 977/* 978 * Store the thread context in the UTS's mailbox. 979 * then add the mailbox at the head of a list we are building in user space. 980 * The list is anchored in the ksegrp structure. 981 */ 982int 983thread_export_context(struct thread *td) 984{ 985 struct proc *p; 986 struct ksegrp *kg; 987 uintptr_t mbx; 988 void *addr; 989 int error,temp; 990 ucontext_t uc; 991 992 p = td->td_proc; 993 kg = td->td_ksegrp; 994 995 /* Export the user/machine context. */ 996 addr = (void *)(&td->td_mailbox->tm_context); 997 error = copyin(addr, &uc, sizeof(ucontext_t)); 998 if (error) 999 goto bad; 1000 1001 thread_getcontext(td, &uc); 1002 error = copyout(&uc, addr, sizeof(ucontext_t)); 1003 if (error) 1004 goto bad; 1005 1006 /* Exports clock ticks in kernel mode */ 1007 addr = (caddr_t)(&td->td_mailbox->tm_sticks); 1008 temp = fuword(addr) + td->td_usticks; 1009 if (suword(addr, temp)) 1010 goto bad; 1011 1012 /* Get address in latest mbox of list pointer */ 1013 addr = (void *)(&td->td_mailbox->tm_next); 1014 /* 1015 * Put the saved address of the previous first 1016 * entry into this one 1017 */ 1018 for (;;) { 1019 mbx = (uintptr_t)kg->kg_completed; 1020 if (suword(addr, mbx)) { 1021 error = EFAULT; 1022 goto bad; 1023 } 1024 PROC_LOCK(p); 1025 if (mbx == (uintptr_t)kg->kg_completed) { 1026 kg->kg_completed = td->td_mailbox; 1027 /* 1028 * The thread context may be taken away by 1029 * other upcall threads when we unlock 1030 * process lock. it's no longer valid to 1031 * use it again in any other places. 1032 */ 1033 td->td_mailbox = NULL; 1034 PROC_UNLOCK(p); 1035 break; 1036 } 1037 PROC_UNLOCK(p); 1038 } 1039 td->td_usticks = 0; 1040 return (0); 1041 1042bad: 1043 PROC_LOCK(p); 1044 psignal(p, SIGSEGV); 1045 PROC_UNLOCK(p); 1046 /* The mailbox is bad, don't use it */ 1047 td->td_mailbox = NULL; 1048 td->td_usticks = 0; 1049 return (error); 1050} 1051 1052/* 1053 * Take the list of completed mailboxes for this KSEGRP and put them on this 1054 * upcall's mailbox as it's the next one going up. 1055 */ 1056static int 1057thread_link_mboxes(struct ksegrp *kg, struct kse_upcall *ku) 1058{ 1059 struct proc *p = kg->kg_proc; 1060 void *addr; 1061 uintptr_t mbx; 1062 1063 addr = (void *)(&ku->ku_mailbox->km_completed); 1064 for (;;) { 1065 mbx = (uintptr_t)kg->kg_completed; 1066 if (suword(addr, mbx)) { 1067 PROC_LOCK(p); 1068 psignal(p, SIGSEGV); 1069 PROC_UNLOCK(p); 1070 return (EFAULT); 1071 } 1072 PROC_LOCK(p); 1073 if (mbx == (uintptr_t)kg->kg_completed) { 1074 kg->kg_completed = NULL; 1075 PROC_UNLOCK(p); 1076 break; 1077 } 1078 PROC_UNLOCK(p); 1079 } 1080 return (0); 1081} 1082 1083/* 1084 * This function should be called at statclock interrupt time 1085 */ 1086int 1087thread_statclock(int user) 1088{ 1089 struct thread *td = curthread; 1090 1091 if (td->td_ksegrp->kg_numupcalls == 0) 1092 return (-1); 1093 if (user) { 1094 /* Current always do via ast() */ 1095 mtx_lock_spin(&sched_lock); 1096 td->td_flags |= (TDF_USTATCLOCK|TDF_ASTPENDING); 1097 mtx_unlock_spin(&sched_lock); 1098 td->td_uuticks++; 1099 } else { 1100 if (td->td_mailbox != NULL) 1101 td->td_usticks++; 1102 else { 1103 /* XXXKSE 1104 * We will call thread_user_enter() for every 1105 * kernel entry in future, so if the thread mailbox 1106 * is NULL, it must be a UTS kernel, don't account 1107 * clock ticks for it. 1108 */ 1109 } 1110 } 1111 return (0); 1112} 1113 1114/* 1115 * Export state clock ticks for userland 1116 */ 1117static int 1118thread_update_usr_ticks(struct thread *td, int user) 1119{ 1120 struct proc *p = td->td_proc; 1121 struct kse_thr_mailbox *tmbx; 1122 struct kse_upcall *ku; 1123 struct ksegrp *kg; 1124 caddr_t addr; 1125 uint uticks; 1126 1127 if ((ku = td->td_upcall) == NULL) 1128 return (-1); 1129 1130 tmbx = (void *)fuword((void *)&ku->ku_mailbox->km_curthread); 1131 if ((tmbx == NULL) || (tmbx == (void *)-1)) 1132 return (-1); 1133 if (user) { 1134 uticks = td->td_uuticks; 1135 td->td_uuticks = 0; 1136 addr = (caddr_t)&tmbx->tm_uticks; 1137 } else { 1138 uticks = td->td_usticks; 1139 td->td_usticks = 0; 1140 addr = (caddr_t)&tmbx->tm_sticks; 1141 } 1142 if (uticks) { 1143 if (suword(addr, uticks+fuword(addr))) { 1144 PROC_LOCK(p); 1145 psignal(p, SIGSEGV); 1146 PROC_UNLOCK(p); 1147 return (-2); 1148 } 1149 } 1150 kg = td->td_ksegrp; 1151 if (kg->kg_upquantum && ticks >= kg->kg_nextupcall) { 1152 mtx_lock_spin(&sched_lock); 1153 td->td_upcall->ku_flags |= KUF_DOUPCALL; 1154 mtx_unlock_spin(&sched_lock); 1155 } 1156 return (0); 1157} 1158 1159/* 1160 * Discard the current thread and exit from its context. 1161 * 1162 * Because we can't free a thread while we're operating under its context, 1163 * push the current thread into our CPU's deadthread holder. This means 1164 * we needn't worry about someone else grabbing our context before we 1165 * do a cpu_throw(). 1166 */ 1167void 1168thread_exit(void) 1169{ 1170 struct thread *td; 1171 struct kse *ke; 1172 struct proc *p; 1173 struct ksegrp *kg; 1174 1175 td = curthread; 1176 kg = td->td_ksegrp; 1177 p = td->td_proc; 1178 ke = td->td_kse; 1179 1180 mtx_assert(&sched_lock, MA_OWNED); 1181 KASSERT(p != NULL, ("thread exiting without a process")); 1182 KASSERT(ke != NULL, ("thread exiting without a kse")); 1183 KASSERT(kg != NULL, ("thread exiting without a kse group")); 1184 PROC_LOCK_ASSERT(p, MA_OWNED); 1185 CTR1(KTR_PROC, "thread_exit: thread %p", td); 1186 KASSERT(!mtx_owned(&Giant), ("dying thread owns giant")); 1187 1188 if (td->td_standin != NULL) { 1189 thread_stash(td->td_standin); 1190 td->td_standin = NULL; 1191 } 1192 1193 cpu_thread_exit(td); /* XXXSMP */ 1194 1195 /* 1196 * The last thread is left attached to the process 1197 * So that the whole bundle gets recycled. Skip 1198 * all this stuff. 1199 */ 1200 if (p->p_numthreads > 1) { 1201 thread_unlink(td); 1202 if (p->p_maxthrwaits) 1203 wakeup(&p->p_numthreads); 1204 /* 1205 * The test below is NOT true if we are the 1206 * sole exiting thread. P_STOPPED_SNGL is unset 1207 * in exit1() after it is the only survivor. 1208 */ 1209 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 1210 if (p->p_numthreads == p->p_suspcount) { 1211 thread_unsuspend_one(p->p_singlethread); 1212 } 1213 } 1214 1215 /* 1216 * Because each upcall structure has an owner thread, 1217 * owner thread exits only when process is in exiting 1218 * state, so upcall to userland is no longer needed, 1219 * deleting upcall structure is safe here. 1220 * So when all threads in a group is exited, all upcalls 1221 * in the group should be automatically freed. 1222 */ 1223 if (td->td_upcall) 1224 upcall_remove(td); 1225 1226 ke->ke_state = KES_UNQUEUED; 1227 ke->ke_thread = NULL; 1228 /* 1229 * Decide what to do with the KSE attached to this thread. 1230 */ 1231 if (ke->ke_flags & KEF_EXIT) 1232 kse_unlink(ke); 1233 else 1234 kse_reassign(ke); 1235 PROC_UNLOCK(p); 1236 td->td_kse = NULL; 1237 td->td_state = TDS_INACTIVE; 1238#if 0 1239 td->td_proc = NULL; 1240#endif 1241 td->td_ksegrp = NULL; 1242 td->td_last_kse = NULL; 1243 PCPU_SET(deadthread, td); 1244 } else { 1245 PROC_UNLOCK(p); 1246 } 1247 /* XXX Shouldn't cpu_throw() here. */ 1248 mtx_assert(&sched_lock, MA_OWNED); 1249#if defined(__i386__) || defined(__sparc64__) 1250 cpu_throw(td, choosethread()); 1251#else 1252 cpu_throw(); 1253#endif 1254 panic("I'm a teapot!"); 1255 /* NOTREACHED */ 1256} 1257 1258/* 1259 * Do any thread specific cleanups that may be needed in wait() 1260 * called with Giant held, proc and schedlock not held. 1261 */ 1262void 1263thread_wait(struct proc *p) 1264{ 1265 struct thread *td; 1266 1267 KASSERT((p->p_numthreads == 1), ("Muliple threads in wait1()")); 1268 KASSERT((p->p_numksegrps == 1), ("Muliple ksegrps in wait1()")); 1269 FOREACH_THREAD_IN_PROC(p, td) { 1270 if (td->td_standin != NULL) { 1271 thread_free(td->td_standin); 1272 td->td_standin = NULL; 1273 } 1274 cpu_thread_clean(td); 1275 } 1276 thread_reap(); /* check for zombie threads etc. */ 1277} 1278 1279/* 1280 * Link a thread to a process. 1281 * set up anything that needs to be initialized for it to 1282 * be used by the process. 1283 * 1284 * Note that we do not link to the proc's ucred here. 1285 * The thread is linked as if running but no KSE assigned. 1286 */ 1287void 1288thread_link(struct thread *td, struct ksegrp *kg) 1289{ 1290 struct proc *p; 1291 1292 p = kg->kg_proc; 1293 td->td_state = TDS_INACTIVE; 1294 td->td_proc = p; 1295 td->td_ksegrp = kg; 1296 td->td_last_kse = NULL; 1297 td->td_flags = 0; 1298 td->td_kse = NULL; 1299 1300 LIST_INIT(&td->td_contested); 1301 callout_init(&td->td_slpcallout, 1); 1302 TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist); 1303 TAILQ_INSERT_HEAD(&kg->kg_threads, td, td_kglist); 1304 p->p_numthreads++; 1305 kg->kg_numthreads++; 1306} 1307 1308void 1309thread_unlink(struct thread *td) 1310{ 1311 struct proc *p = td->td_proc; 1312 struct ksegrp *kg = td->td_ksegrp; 1313 1314 TAILQ_REMOVE(&p->p_threads, td, td_plist); 1315 p->p_numthreads--; 1316 TAILQ_REMOVE(&kg->kg_threads, td, td_kglist); 1317 kg->kg_numthreads--; 1318 /* could clear a few other things here */ 1319} 1320 1321/* 1322 * Purge a ksegrp resource. When a ksegrp is preparing to 1323 * exit, it calls this function. 1324 */ 1325void 1326kse_purge_group(struct thread *td) 1327{ 1328 struct ksegrp *kg; 1329 struct kse *ke; 1330 1331 kg = td->td_ksegrp; 1332 KASSERT(kg->kg_numthreads == 1, ("%s: bad thread number", __func__)); 1333 while ((ke = TAILQ_FIRST(&kg->kg_iq)) != NULL) { 1334 KASSERT(ke->ke_state == KES_IDLE, 1335 ("%s: wrong idle KSE state", __func__)); 1336 kse_unlink(ke); 1337 } 1338 KASSERT((kg->kg_kses == 1), 1339 ("%s: ksegrp still has %d KSEs", __func__, kg->kg_kses)); 1340 KASSERT((kg->kg_numupcalls == 0), 1341 ("%s: ksegrp still has %d upcall datas", 1342 __func__, kg->kg_numupcalls)); 1343} 1344 1345/* 1346 * Purge a process's KSE resource. When a process is preparing to 1347 * exit, it calls kse_purge to release any extra KSE resources in 1348 * the process. 1349 */ 1350void 1351kse_purge(struct proc *p, struct thread *td) 1352{ 1353 struct ksegrp *kg; 1354 struct kse *ke; 1355 1356 KASSERT(p->p_numthreads == 1, ("bad thread number")); 1357 mtx_lock_spin(&sched_lock); 1358 while ((kg = TAILQ_FIRST(&p->p_ksegrps)) != NULL) { 1359 TAILQ_REMOVE(&p->p_ksegrps, kg, kg_ksegrp); 1360 p->p_numksegrps--; 1361 /* 1362 * There is no ownership for KSE, after all threads 1363 * in the group exited, it is possible that some KSEs 1364 * were left in idle queue, gc them now. 1365 */ 1366 while ((ke = TAILQ_FIRST(&kg->kg_iq)) != NULL) { 1367 KASSERT(ke->ke_state == KES_IDLE, 1368 ("%s: wrong idle KSE state", __func__)); 1369 TAILQ_REMOVE(&kg->kg_iq, ke, ke_kgrlist); 1370 kg->kg_idle_kses--; 1371 TAILQ_REMOVE(&kg->kg_kseq, ke, ke_kglist); 1372 kg->kg_kses--; 1373 kse_stash(ke); 1374 } 1375 KASSERT(((kg->kg_kses == 0) && (kg != td->td_ksegrp)) || 1376 ((kg->kg_kses == 1) && (kg == td->td_ksegrp)), 1377 ("ksegrp has wrong kg_kses: %d", kg->kg_kses)); 1378 KASSERT((kg->kg_numupcalls == 0), 1379 ("%s: ksegrp still has %d upcall datas", 1380 __func__, kg->kg_numupcalls)); 1381 1382 if (kg != td->td_ksegrp) 1383 ksegrp_stash(kg); 1384 } 1385 TAILQ_INSERT_HEAD(&p->p_ksegrps, td->td_ksegrp, kg_ksegrp); 1386 p->p_numksegrps++; 1387 mtx_unlock_spin(&sched_lock); 1388} 1389 1390/* 1391 * This function is intended to be used to initialize a spare thread 1392 * for upcall. Initialize thread's large data area outside sched_lock 1393 * for thread_schedule_upcall(). 1394 */ 1395void 1396thread_alloc_spare(struct thread *td, struct thread *spare) 1397{ 1398 if (td->td_standin) 1399 return; 1400 if (spare == NULL) 1401 spare = thread_alloc(); 1402 td->td_standin = spare; 1403 bzero(&spare->td_startzero, 1404 (unsigned)RANGEOF(struct thread, td_startzero, td_endzero)); 1405 spare->td_proc = td->td_proc; 1406 spare->td_ucred = crhold(td->td_ucred); 1407} 1408 1409/* 1410 * Create a thread and schedule it for upcall on the KSE given. 1411 * Use our thread's standin so that we don't have to allocate one. 1412 */ 1413struct thread * 1414thread_schedule_upcall(struct thread *td, struct kse_upcall *ku) 1415{ 1416 struct thread *td2; 1417 1418 mtx_assert(&sched_lock, MA_OWNED); 1419 1420 /* 1421 * Schedule an upcall thread on specified kse_upcall, 1422 * the kse_upcall must be free. 1423 * td must have a spare thread. 1424 */ 1425 KASSERT(ku->ku_owner == NULL, ("%s: upcall has owner", __func__)); 1426 if ((td2 = td->td_standin) != NULL) { 1427 td->td_standin = NULL; 1428 } else { 1429 panic("no reserve thread when scheduling an upcall"); 1430 return (NULL); 1431 } 1432 CTR3(KTR_PROC, "thread_schedule_upcall: thread %p (pid %d, %s)", 1433 td2, td->td_proc->p_pid, td->td_proc->p_comm); 1434 bcopy(&td->td_startcopy, &td2->td_startcopy, 1435 (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy)); 1436 thread_link(td2, ku->ku_ksegrp); 1437 /* inherit blocked thread's context */ 1438 bcopy(td->td_frame, td2->td_frame, sizeof(struct trapframe)); 1439 cpu_set_upcall(td2, td->td_pcb); 1440 /* Let the new thread become owner of the upcall */ 1441 ku->ku_owner = td2; 1442 td2->td_upcall = ku; 1443 td2->td_flags = TDF_UPCALLING; 1444#if 0 /* XXX This shouldn't be necessary */ 1445 if (td->td_proc->p_sflag & PS_NEEDSIGCHK) 1446 td2->td_flags |= TDF_ASTPENDING; 1447#endif 1448 td2->td_kse = NULL; 1449 td2->td_state = TDS_CAN_RUN; 1450 td2->td_inhibitors = 0; 1451 setrunqueue(td2); 1452 return (td2); /* bogus.. should be a void function */ 1453} 1454 1455void 1456thread_signal_add(struct thread *td, int sig) 1457{ 1458 struct kse_upcall *ku; 1459 struct proc *p; 1460 sigset_t ss; 1461 int error; 1462 1463 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); 1464 td = curthread; 1465 ku = td->td_upcall; 1466 p = td->td_proc; 1467 1468 PROC_UNLOCK(p); 1469 error = copyin(&ku->ku_mailbox->km_sigscaught, &ss, sizeof(sigset_t)); 1470 if (error) 1471 goto error; 1472 1473 SIGADDSET(ss, sig); 1474 1475 error = copyout(&ss, &ku->ku_mailbox->km_sigscaught, sizeof(sigset_t)); 1476 if (error) 1477 goto error; 1478 1479 PROC_LOCK(p); 1480 return; 1481error: 1482 PROC_LOCK(p); 1483 sigexit(td, SIGILL); 1484} 1485 1486 1487/* 1488 * Schedule an upcall to notify a KSE process recieved signals. 1489 * 1490 */ 1491void 1492thread_signal_upcall(struct thread *td) 1493{ 1494 mtx_lock_spin(&sched_lock); 1495 td->td_flags |= TDF_UPCALLING; 1496 mtx_unlock_spin(&sched_lock); 1497 1498 return; 1499} 1500 1501void 1502thread_switchout(struct thread *td) 1503{ 1504 struct kse_upcall *ku; 1505 1506 mtx_assert(&sched_lock, MA_OWNED); 1507 1508 /* 1509 * If the outgoing thread is in threaded group and has never 1510 * scheduled an upcall, decide whether this is a short 1511 * or long term event and thus whether or not to schedule 1512 * an upcall. 1513 * If it is a short term event, just suspend it in 1514 * a way that takes its KSE with it. 1515 * Select the events for which we want to schedule upcalls. 1516 * For now it's just sleep. 1517 * XXXKSE eventually almost any inhibition could do. 1518 */ 1519 if (TD_CAN_UNBIND(td) && (td->td_standin) && TD_ON_SLEEPQ(td)) { 1520 /* 1521 * Release ownership of upcall, and schedule an upcall 1522 * thread, this new upcall thread becomes the owner of 1523 * the upcall structure. 1524 */ 1525 ku = td->td_upcall; 1526 ku->ku_owner = NULL; 1527 td->td_upcall = NULL; 1528 td->td_flags &= ~TDF_CAN_UNBIND; 1529 thread_schedule_upcall(td, ku); 1530 } 1531} 1532 1533/* 1534 * Setup done on the thread when it enters the kernel. 1535 * XXXKSE Presently only for syscalls but eventually all kernel entries. 1536 */ 1537void 1538thread_user_enter(struct proc *p, struct thread *td) 1539{ 1540 struct ksegrp *kg; 1541 struct kse_upcall *ku; 1542 1543 kg = td->td_ksegrp; 1544 /* 1545 * First check that we shouldn't just abort. 1546 * But check if we are the single thread first! 1547 * XXX p_singlethread not locked, but should be safe. 1548 */ 1549 if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) { 1550 PROC_LOCK(p); 1551 mtx_lock_spin(&sched_lock); 1552 thread_stopped(p); 1553 thread_exit(); 1554 /* NOTREACHED */ 1555 } 1556 1557 /* 1558 * If we are doing a syscall in a KSE environment, 1559 * note where our mailbox is. There is always the 1560 * possibility that we could do this lazily (in kse_reassign()), 1561 * but for now do it every time. 1562 */ 1563 kg = td->td_ksegrp; 1564 if (kg->kg_numupcalls) { 1565 ku = td->td_upcall; 1566 KASSERT(ku, ("%s: no upcall owned", __func__)); 1567 KASSERT((ku->ku_owner == td), ("%s: wrong owner", __func__)); 1568 td->td_mailbox = 1569 (void *)fuword((void *)&ku->ku_mailbox->km_curthread); 1570 if ((td->td_mailbox == NULL) || 1571 (td->td_mailbox == (void *)-1)) { 1572 /* Don't schedule upcall when blocked */ 1573 td->td_mailbox = NULL; 1574 mtx_lock_spin(&sched_lock); 1575 td->td_flags &= ~TDF_CAN_UNBIND; 1576 mtx_unlock_spin(&sched_lock); 1577 } else { 1578 if (td->td_standin == NULL) 1579 thread_alloc_spare(td, NULL); 1580 mtx_lock_spin(&sched_lock); 1581 td->td_flags |= TDF_CAN_UNBIND; 1582 mtx_unlock_spin(&sched_lock); 1583 } 1584 } 1585} 1586 1587/* 1588 * The extra work we go through if we are a threaded process when we 1589 * return to userland. 1590 * 1591 * If we are a KSE process and returning to user mode, check for 1592 * extra work to do before we return (e.g. for more syscalls 1593 * to complete first). If we were in a critical section, we should 1594 * just return to let it finish. Same if we were in the UTS (in 1595 * which case the mailbox's context's busy indicator will be set). 1596 * The only traps we suport will have set the mailbox. 1597 * We will clear it here. 1598 */ 1599int 1600thread_userret(struct thread *td, struct trapframe *frame) 1601{ 1602 int error = 0, upcalls; 1603 struct kse_upcall *ku; 1604 struct ksegrp *kg, *kg2; 1605 struct proc *p; 1606 struct timespec ts; 1607 1608 p = td->td_proc; 1609 kg = td->td_ksegrp; 1610 1611 1612 /* Nothing to do with non-threaded group/process */ 1613 if (td->td_ksegrp->kg_numupcalls == 0) 1614 return (0); 1615 1616 /* 1617 * Stat clock interrupt hit in userland, it 1618 * is returning from interrupt, charge thread's 1619 * userland time for UTS. 1620 */ 1621 if (td->td_flags & TDF_USTATCLOCK) { 1622 thread_update_usr_ticks(td, 1); 1623 mtx_lock_spin(&sched_lock); 1624 td->td_flags &= ~TDF_USTATCLOCK; 1625 mtx_unlock_spin(&sched_lock); 1626 if (kg->kg_completed || 1627 (td->td_upcall->ku_flags & KUF_DOUPCALL)) 1628 thread_user_enter(p, td); 1629 } 1630 1631 /* 1632 * Optimisation: 1633 * This thread has not started any upcall. 1634 * If there is no work to report other than ourself, 1635 * then it can return direct to userland. 1636 */ 1637 if (TD_CAN_UNBIND(td)) { 1638 mtx_lock_spin(&sched_lock); 1639 td->td_flags &= ~TDF_CAN_UNBIND; 1640 ku = td->td_upcall; 1641 if ((td->td_flags & TDF_NEEDSIGCHK) == 0 && 1642 (kg->kg_completed == NULL) && 1643 (ku->ku_flags & KUF_DOUPCALL) == 0 && 1644 (kg->kg_upquantum && ticks >= kg->kg_nextupcall)) { 1645 mtx_unlock_spin(&sched_lock); 1646 thread_update_usr_ticks(td, 0); 1647 nanotime(&ts); 1648 error = copyout(&ts, 1649 (caddr_t)&ku->ku_mailbox->km_timeofday, 1650 sizeof(ts)); 1651 td->td_mailbox = 0; 1652 if (error) 1653 goto out; 1654 return (0); 1655 } 1656 mtx_unlock_spin(&sched_lock); 1657 error = thread_export_context(td); 1658 if (error) { 1659 /* 1660 * Failing to do the KSE operation just defaults 1661 * back to synchonous operation, so just return from 1662 * the syscall. 1663 */ 1664 return (0); 1665 } 1666 /* 1667 * There is something to report, and we own an upcall 1668 * strucuture, we can go to userland. 1669 * Turn ourself into an upcall thread. 1670 */ 1671 mtx_lock_spin(&sched_lock); 1672 td->td_flags |= TDF_UPCALLING; 1673 mtx_unlock_spin(&sched_lock); 1674 } else if (td->td_mailbox) { 1675 error = thread_export_context(td); 1676 /* possibly upcall with error? */ 1677 PROC_LOCK(p); 1678 /* 1679 * There are upcall threads waiting for 1680 * work to do, wake one of them up. 1681 * XXXKSE Maybe wake all of them up. 1682 */ 1683 if (!error && kg->kg_upsleeps) 1684 wakeup_one(&kg->kg_completed); 1685 mtx_lock_spin(&sched_lock); 1686 thread_stopped(p); 1687 thread_exit(); 1688 /* NOTREACHED */ 1689 } 1690 1691 KASSERT(TD_CAN_UNBIND(td) == 0, ("can unbind")); 1692 1693 if (p->p_numthreads > max_threads_per_proc) { 1694 max_threads_hits++; 1695 PROC_LOCK(p); 1696 while (p->p_numthreads > max_threads_per_proc) { 1697 if (P_SHOULDSTOP(p)) 1698 break; 1699 upcalls = 0; 1700 mtx_lock_spin(&sched_lock); 1701 FOREACH_KSEGRP_IN_PROC(p, kg2) { 1702 if (kg2->kg_numupcalls == 0) 1703 upcalls++; 1704 else 1705 upcalls += kg2->kg_numupcalls; 1706 } 1707 mtx_unlock_spin(&sched_lock); 1708 if (upcalls >= max_threads_per_proc) 1709 break; 1710 p->p_maxthrwaits++; 1711 msleep(&p->p_numthreads, &p->p_mtx, PPAUSE|PCATCH, 1712 "maxthreads", NULL); 1713 p->p_maxthrwaits--; 1714 } 1715 PROC_UNLOCK(p); 1716 } 1717 1718 if (td->td_flags & TDF_UPCALLING) { 1719 kg->kg_nextupcall = ticks+kg->kg_upquantum; 1720 ku = td->td_upcall; 1721 /* 1722 * There is no more work to do and we are going to ride 1723 * this thread up to userland as an upcall. 1724 * Do the last parts of the setup needed for the upcall. 1725 */ 1726 CTR3(KTR_PROC, "userret: upcall thread %p (pid %d, %s)", 1727 td, td->td_proc->p_pid, td->td_proc->p_comm); 1728 1729 /* 1730 * Set user context to the UTS. 1731 * Will use Giant in cpu_thread_clean() because it uses 1732 * kmem_free(kernel_map, ...) 1733 */ 1734 cpu_set_upcall_kse(td, ku); 1735 mtx_lock_spin(&sched_lock); 1736 td->td_flags &= ~TDF_UPCALLING; 1737 if (ku->ku_flags & KUF_DOUPCALL) 1738 ku->ku_flags &= ~KUF_DOUPCALL; 1739 mtx_unlock_spin(&sched_lock); 1740 1741 /* 1742 * Unhook the list of completed threads. 1743 * anything that completes after this gets to 1744 * come in next time. 1745 * Put the list of completed thread mailboxes on 1746 * this KSE's mailbox. 1747 */ 1748 error = thread_link_mboxes(kg, ku); 1749 if (error) 1750 goto out; 1751 1752 /* 1753 * Set state and clear the thread mailbox pointer. 1754 * From now on we are just a bound outgoing process. 1755 * **Problem** userret is often called several times. 1756 * it would be nice if this all happenned only on the first 1757 * time through. (the scan for extra work etc.) 1758 */ 1759 error = suword((caddr_t)&ku->ku_mailbox->km_curthread, 0); 1760 if (error) 1761 goto out; 1762 1763 /* Export current system time */ 1764 nanotime(&ts); 1765 error = copyout(&ts, (caddr_t)&ku->ku_mailbox->km_timeofday, 1766 sizeof(ts)); 1767 } 1768 1769out: 1770 if (error) { 1771 /* 1772 * Things are going to be so screwed we should just kill 1773 * the process. 1774 * how do we do that? 1775 */ 1776 PROC_LOCK(td->td_proc); 1777 psignal(td->td_proc, SIGSEGV); 1778 PROC_UNLOCK(td->td_proc); 1779 } else { 1780 /* 1781 * Optimisation: 1782 * Ensure that we have a spare thread available, 1783 * for when we re-enter the kernel. 1784 */ 1785 if (td->td_standin == NULL) 1786 thread_alloc_spare(td, NULL); 1787 } 1788 1789 /* 1790 * Clear thread mailbox first, then clear system tick count. 1791 * The order is important because thread_statclock() use 1792 * mailbox pointer to see if it is an userland thread or 1793 * an UTS kernel thread. 1794 */ 1795 td->td_mailbox = NULL; 1796 td->td_usticks = 0; 1797 return (error); /* go sync */ 1798} 1799 1800/* 1801 * Enforce single-threading. 1802 * 1803 * Returns 1 if the caller must abort (another thread is waiting to 1804 * exit the process or similar). Process is locked! 1805 * Returns 0 when you are successfully the only thread running. 1806 * A process has successfully single threaded in the suspend mode when 1807 * There are no threads in user mode. Threads in the kernel must be 1808 * allowed to continue until they get to the user boundary. They may even 1809 * copy out their return values and data before suspending. They may however be 1810 * accellerated in reaching the user boundary as we will wake up 1811 * any sleeping threads that are interruptable. (PCATCH). 1812 */ 1813int 1814thread_single(int force_exit) 1815{ 1816 struct thread *td; 1817 struct thread *td2; 1818 struct proc *p; 1819 1820 td = curthread; 1821 p = td->td_proc; 1822 mtx_assert(&Giant, MA_OWNED); 1823 PROC_LOCK_ASSERT(p, MA_OWNED); 1824 KASSERT((td != NULL), ("curthread is NULL")); 1825 1826 if ((p->p_flag & P_THREADED) == 0 && p->p_numthreads == 1) 1827 return (0); 1828 1829 /* Is someone already single threading? */ 1830 if (p->p_singlethread) 1831 return (1); 1832 1833 if (force_exit == SINGLE_EXIT) { 1834 p->p_flag |= P_SINGLE_EXIT; 1835 } else 1836 p->p_flag &= ~P_SINGLE_EXIT; 1837 p->p_flag |= P_STOPPED_SINGLE; 1838 p->p_singlethread = td; 1839 /* XXXKSE Which lock protects the below values? */ 1840 while ((p->p_numthreads - p->p_suspcount) != 1) { 1841 mtx_lock_spin(&sched_lock); 1842 FOREACH_THREAD_IN_PROC(p, td2) { 1843 if (td2 == td) 1844 continue; 1845 td->td_flags |= TDF_ASTPENDING; 1846 if (TD_IS_INHIBITED(td2)) { 1847 if (force_exit == SINGLE_EXIT) { 1848 if (TD_IS_SUSPENDED(td2)) { 1849 thread_unsuspend_one(td2); 1850 } 1851 if (TD_ON_SLEEPQ(td2) && 1852 (td2->td_flags & TDF_SINTR)) { 1853 if (td2->td_flags & TDF_CVWAITQ) 1854 cv_abort(td2); 1855 else 1856 abortsleep(td2); 1857 } 1858 } else { 1859 if (TD_IS_SUSPENDED(td2)) 1860 continue; 1861 /* 1862 * maybe other inhibitted states too? 1863 * XXXKSE Is it totally safe to 1864 * suspend a non-interruptable thread? 1865 */ 1866 if (td2->td_inhibitors & 1867 (TDI_SLEEPING | TDI_SWAPPED)) 1868 thread_suspend_one(td2); 1869 } 1870 } 1871 } 1872 /* 1873 * Maybe we suspended some threads.. was it enough? 1874 */ 1875 if ((p->p_numthreads - p->p_suspcount) == 1) { 1876 mtx_unlock_spin(&sched_lock); 1877 break; 1878 } 1879 1880 /* 1881 * Wake us up when everyone else has suspended. 1882 * In the mean time we suspend as well. 1883 */ 1884 thread_suspend_one(td); 1885 /* XXX If you recursed this is broken. */ 1886 mtx_unlock(&Giant); 1887 PROC_UNLOCK(p); 1888 p->p_stats->p_ru.ru_nvcsw++; 1889 mi_switch(); 1890 mtx_unlock_spin(&sched_lock); 1891 mtx_lock(&Giant); 1892 PROC_LOCK(p); 1893 } 1894 if (force_exit == SINGLE_EXIT) { 1895 if (td->td_upcall) { 1896 mtx_lock_spin(&sched_lock); 1897 upcall_remove(td); 1898 mtx_unlock_spin(&sched_lock); 1899 } 1900 kse_purge(p, td); 1901 } 1902 return (0); 1903} 1904 1905/* 1906 * Called in from locations that can safely check to see 1907 * whether we have to suspend or at least throttle for a 1908 * single-thread event (e.g. fork). 1909 * 1910 * Such locations include userret(). 1911 * If the "return_instead" argument is non zero, the thread must be able to 1912 * accept 0 (caller may continue), or 1 (caller must abort) as a result. 1913 * 1914 * The 'return_instead' argument tells the function if it may do a 1915 * thread_exit() or suspend, or whether the caller must abort and back 1916 * out instead. 1917 * 1918 * If the thread that set the single_threading request has set the 1919 * P_SINGLE_EXIT bit in the process flags then this call will never return 1920 * if 'return_instead' is false, but will exit. 1921 * 1922 * P_SINGLE_EXIT | return_instead == 0| return_instead != 0 1923 *---------------+--------------------+--------------------- 1924 * 0 | returns 0 | returns 0 or 1 1925 * | when ST ends | immediatly 1926 *---------------+--------------------+--------------------- 1927 * 1 | thread exits | returns 1 1928 * | | immediatly 1929 * 0 = thread_exit() or suspension ok, 1930 * other = return error instead of stopping the thread. 1931 * 1932 * While a full suspension is under effect, even a single threading 1933 * thread would be suspended if it made this call (but it shouldn't). 1934 * This call should only be made from places where 1935 * thread_exit() would be safe as that may be the outcome unless 1936 * return_instead is set. 1937 */ 1938int 1939thread_suspend_check(int return_instead) 1940{ 1941 struct thread *td; 1942 struct proc *p; 1943 struct ksegrp *kg; 1944 1945 td = curthread; 1946 p = td->td_proc; 1947 kg = td->td_ksegrp; 1948 PROC_LOCK_ASSERT(p, MA_OWNED); 1949 while (P_SHOULDSTOP(p)) { 1950 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 1951 KASSERT(p->p_singlethread != NULL, 1952 ("singlethread not set")); 1953 /* 1954 * The only suspension in action is a 1955 * single-threading. Single threader need not stop. 1956 * XXX Should be safe to access unlocked 1957 * as it can only be set to be true by us. 1958 */ 1959 if (p->p_singlethread == td) 1960 return (0); /* Exempt from stopping. */ 1961 } 1962 if (return_instead) 1963 return (1); 1964 1965 mtx_lock_spin(&sched_lock); 1966 thread_stopped(p); 1967 /* 1968 * If the process is waiting for us to exit, 1969 * this thread should just suicide. 1970 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SINGLE. 1971 */ 1972 if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) { 1973 while (mtx_owned(&Giant)) 1974 mtx_unlock(&Giant); 1975 if (p->p_flag & P_THREADED) 1976 thread_exit(); 1977 else 1978 thr_exit1(); 1979 } 1980 1981 mtx_assert(&Giant, MA_NOTOWNED); 1982 /* 1983 * When a thread suspends, it just 1984 * moves to the processes's suspend queue 1985 * and stays there. 1986 */ 1987 thread_suspend_one(td); 1988 PROC_UNLOCK(p); 1989 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) { 1990 if (p->p_numthreads == p->p_suspcount) { 1991 thread_unsuspend_one(p->p_singlethread); 1992 } 1993 } 1994 p->p_stats->p_ru.ru_nivcsw++; 1995 mi_switch(); 1996 mtx_unlock_spin(&sched_lock); 1997 PROC_LOCK(p); 1998 } 1999 return (0); 2000} 2001 2002void 2003thread_suspend_one(struct thread *td) 2004{ 2005 struct proc *p = td->td_proc; 2006 2007 mtx_assert(&sched_lock, MA_OWNED); 2008 KASSERT(!TD_IS_SUSPENDED(td), ("already suspended")); 2009 p->p_suspcount++; 2010 TD_SET_SUSPENDED(td); 2011 TAILQ_INSERT_TAIL(&p->p_suspended, td, td_runq); 2012 /* 2013 * Hack: If we are suspending but are on the sleep queue 2014 * then we are in msleep or the cv equivalent. We 2015 * want to look like we have two Inhibitors. 2016 * May already be set.. doesn't matter. 2017 */ 2018 if (TD_ON_SLEEPQ(td)) 2019 TD_SET_SLEEPING(td); 2020} 2021 2022void 2023thread_unsuspend_one(struct thread *td) 2024{ 2025 struct proc *p = td->td_proc; 2026 2027 mtx_assert(&sched_lock, MA_OWNED); 2028 TAILQ_REMOVE(&p->p_suspended, td, td_runq); 2029 TD_CLR_SUSPENDED(td); 2030 p->p_suspcount--; 2031 setrunnable(td); 2032} 2033 2034/* 2035 * Allow all threads blocked by single threading to continue running. 2036 */ 2037void 2038thread_unsuspend(struct proc *p) 2039{ 2040 struct thread *td; 2041 2042 mtx_assert(&sched_lock, MA_OWNED); 2043 PROC_LOCK_ASSERT(p, MA_OWNED); 2044 if (!P_SHOULDSTOP(p)) { 2045 while (( td = TAILQ_FIRST(&p->p_suspended))) { 2046 thread_unsuspend_one(td); 2047 } 2048 } else if ((P_SHOULDSTOP(p) == P_STOPPED_SINGLE) && 2049 (p->p_numthreads == p->p_suspcount)) { 2050 /* 2051 * Stopping everything also did the job for the single 2052 * threading request. Now we've downgraded to single-threaded, 2053 * let it continue. 2054 */ 2055 thread_unsuspend_one(p->p_singlethread); 2056 } 2057} 2058 2059void 2060thread_single_end(void) 2061{ 2062 struct thread *td; 2063 struct proc *p; 2064 2065 td = curthread; 2066 p = td->td_proc; 2067 PROC_LOCK_ASSERT(p, MA_OWNED); 2068 p->p_flag &= ~P_STOPPED_SINGLE; 2069 p->p_singlethread = NULL; 2070 /* 2071 * If there are other threads they mey now run, 2072 * unless of course there is a blanket 'stop order' 2073 * on the process. The single threader must be allowed 2074 * to continue however as this is a bad place to stop. 2075 */ 2076 if ((p->p_numthreads != 1) && (!P_SHOULDSTOP(p))) { 2077 mtx_lock_spin(&sched_lock); 2078 while (( td = TAILQ_FIRST(&p->p_suspended))) { 2079 thread_unsuspend_one(td); 2080 } 2081 mtx_unlock_spin(&sched_lock); 2082 } 2083} 2084 2085 2086