-
Notifications
You must be signed in to change notification settings - Fork 764
/
thread.c
2368 lines (2089 loc) · 57.6 KB
/
thread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2021 Joyent, Inc.
* Copyright 2021 Oxide Computer Company
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysmacros.h>
#include <sys/signal.h>
#include <sys/stack.h>
#include <sys/pcb.h>
#include <sys/user.h>
#include <sys/systm.h>
#include <sys/sysinfo.h>
#include <sys/errno.h>
#include <sys/cmn_err.h>
#include <sys/cred.h>
#include <sys/resource.h>
#include <sys/task.h>
#include <sys/project.h>
#include <sys/proc.h>
#include <sys/debug.h>
#include <sys/disp.h>
#include <sys/class.h>
#include <vm/seg_kmem.h>
#include <vm/seg_kp.h>
#include <sys/machlock.h>
#include <sys/kmem.h>
#include <sys/varargs.h>
#include <sys/turnstile.h>
#include <sys/poll.h>
#include <sys/vtrace.h>
#include <sys/callb.h>
#include <c2/audit.h>
#include <sys/sobject.h>
#include <sys/cpupart.h>
#include <sys/pset.h>
#include <sys/door.h>
#include <sys/spl.h>
#include <sys/copyops.h>
#include <sys/rctl.h>
#include <sys/brand.h>
#include <sys/pool.h>
#include <sys/zone.h>
#include <sys/tsol/label.h>
#include <sys/tsol/tndb.h>
#include <sys/cpc_impl.h>
#include <sys/sdt.h>
#include <sys/reboot.h>
#include <sys/kdi.h>
#include <sys/schedctl.h>
#include <sys/waitq.h>
#include <sys/cpucaps.h>
#include <sys/kiconv.h>
#include <sys/ctype.h>
#include <sys/smt.h>
struct kmem_cache *thread_cache; /* cache of free threads */
struct kmem_cache *lwp_cache; /* cache of free lwps */
struct kmem_cache *turnstile_cache; /* cache of free turnstiles */
/*
* allthreads is only for use by kmem_readers. All kernel loops can use
* the current thread as a start/end point.
*/
kthread_t *allthreads = &t0; /* circular list of all threads */
static kcondvar_t reaper_cv; /* synchronization var */
kthread_t *thread_deathrow; /* circular list of reapable threads */
kthread_t *lwp_deathrow; /* circular list of reapable threads */
kmutex_t reaplock; /* protects lwp and thread deathrows */
int thread_reapcnt = 0; /* number of threads on deathrow */
int lwp_reapcnt = 0; /* number of lwps on deathrow */
int reaplimit = 16; /* delay reaping until reaplimit */
thread_free_lock_t *thread_free_lock;
/* protects tick thread from reaper */
extern int nthread;
/* System Scheduling classes. */
id_t syscid; /* system scheduling class ID */
id_t sysdccid = CLASS_UNUSED; /* reset when SDC loads */
void *segkp_thread; /* cookie for segkp pool */
int lwp_cache_sz = 32;
int t_cache_sz = 8;
static kt_did_t next_t_id = 1;
/* Default mode for thread binding to CPUs and processor sets */
int default_binding_mode = TB_ALLHARD;
/*
* Min/Max stack sizes for stack size parameters
*/
#define MAX_STKSIZE (32 * DEFAULTSTKSZ)
#define MIN_STKSIZE DEFAULTSTKSZ
/*
* default_stksize overrides lwp_default_stksize if it is set.
*/
int default_stksize;
int lwp_default_stksize;
static zone_key_t zone_thread_key;
unsigned int kmem_stackinfo; /* stackinfo feature on-off */
kmem_stkinfo_t *kmem_stkinfo_log; /* stackinfo circular log */
static kmutex_t kmem_stkinfo_lock; /* protects kmem_stkinfo_log */
/*
* forward declarations for internal thread specific data (tsd)
*/
static void *tsd_realloc(void *, size_t, size_t);
void thread_reaper(void);
/* forward declarations for stackinfo feature */
static void stkinfo_begin(kthread_t *);
static void stkinfo_end(kthread_t *);
static size_t stkinfo_percent(caddr_t, caddr_t, caddr_t);
/*ARGSUSED*/
static int
turnstile_constructor(void *buf, void *cdrarg, int kmflags)
{
bzero(buf, sizeof (turnstile_t));
return (0);
}
/*ARGSUSED*/
static void
turnstile_destructor(void *buf, void *cdrarg)
{
turnstile_t *ts = buf;
ASSERT(ts->ts_free == NULL);
ASSERT(ts->ts_waiters == 0);
ASSERT(ts->ts_inheritor == NULL);
ASSERT(ts->ts_sleepq[0].sq_first == NULL);
ASSERT(ts->ts_sleepq[1].sq_first == NULL);
}
void
thread_init(void)
{
kthread_t *tp;
extern char sys_name[];
extern void idle();
struct cpu *cpu = CPU;
int i;
kmutex_t *lp;
mutex_init(&reaplock, NULL, MUTEX_SPIN, (void *)ipltospl(DISP_LEVEL));
thread_free_lock =
kmem_alloc(sizeof (thread_free_lock_t) * THREAD_FREE_NUM, KM_SLEEP);
for (i = 0; i < THREAD_FREE_NUM; i++) {
lp = &thread_free_lock[i].tf_lock;
mutex_init(lp, NULL, MUTEX_DEFAULT, NULL);
}
#if defined(__x86)
thread_cache = kmem_cache_create("thread_cache", sizeof (kthread_t),
PTR24_ALIGN, NULL, NULL, NULL, NULL, NULL, 0);
/*
* "struct _klwp" includes a "struct pcb", which includes a
* "struct fpu", which needs to be 64-byte aligned on amd64
* (and even on i386) for xsave/xrstor.
*/
lwp_cache = kmem_cache_create("lwp_cache", sizeof (klwp_t),
64, NULL, NULL, NULL, NULL, NULL, 0);
#else
/*
* Allocate thread structures from static_arena. This prevents
* issues where a thread tries to relocate its own thread
* structure and touches it after the mapping has been suspended.
*/
thread_cache = kmem_cache_create("thread_cache", sizeof (kthread_t),
PTR24_ALIGN, NULL, NULL, NULL, NULL, static_arena, 0);
lwp_stk_cache_init();
lwp_cache = kmem_cache_create("lwp_cache", sizeof (klwp_t),
0, NULL, NULL, NULL, NULL, NULL, 0);
#endif
turnstile_cache = kmem_cache_create("turnstile_cache",
sizeof (turnstile_t), 0,
turnstile_constructor, turnstile_destructor, NULL, NULL, NULL, 0);
label_init();
cred_init();
/*
* Initialize various resource management facilities.
*/
rctl_init();
cpucaps_init();
/*
* Zone_init() should be called before project_init() so that project ID
* for the first project is initialized correctly.
*/
zone_init();
project_init();
brand_init();
kiconv_init();
task_init();
tcache_init();
pool_init();
curthread->t_ts = kmem_cache_alloc(turnstile_cache, KM_SLEEP);
/*
* Originally, we had two parameters to set default stack
* size: one for lwp's (lwp_default_stksize), and one for
* kernel-only threads (DEFAULTSTKSZ, a.k.a. _defaultstksz).
* Now we have a third parameter that overrides both if it is
* set to a legal stack size, called default_stksize.
*/
if (default_stksize == 0) {
default_stksize = DEFAULTSTKSZ;
} else if (default_stksize % PAGESIZE != 0 ||
default_stksize > MAX_STKSIZE ||
default_stksize < MIN_STKSIZE) {
cmn_err(CE_WARN, "Illegal stack size. Using %d",
(int)DEFAULTSTKSZ);
default_stksize = DEFAULTSTKSZ;
} else {
lwp_default_stksize = default_stksize;
}
if (lwp_default_stksize == 0) {
lwp_default_stksize = default_stksize;
} else if (lwp_default_stksize % PAGESIZE != 0 ||
lwp_default_stksize > MAX_STKSIZE ||
lwp_default_stksize < MIN_STKSIZE) {
cmn_err(CE_WARN, "Illegal stack size. Using %d",
default_stksize);
lwp_default_stksize = default_stksize;
}
segkp_lwp = segkp_cache_init(segkp, lwp_cache_sz,
lwp_default_stksize,
(KPD_NOWAIT | KPD_HASREDZONE | KPD_LOCKED));
segkp_thread = segkp_cache_init(segkp, t_cache_sz,
default_stksize, KPD_HASREDZONE | KPD_LOCKED | KPD_NO_ANON);
(void) getcid(sys_name, &syscid);
curthread->t_cid = syscid; /* current thread is t0 */
/*
* Set up the first CPU's idle thread.
* It runs whenever the CPU has nothing worthwhile to do.
*/
tp = thread_create(NULL, 0, idle, NULL, 0, &p0, TS_STOPPED, -1);
cpu->cpu_idle_thread = tp;
tp->t_preempt = 1;
tp->t_disp_queue = cpu->cpu_disp;
ASSERT(tp->t_disp_queue != NULL);
tp->t_bound_cpu = cpu;
tp->t_affinitycnt = 1;
/*
* Registering a thread in the callback table is usually
* done in the initialization code of the thread. In this
* case, we do it right after thread creation to avoid
* blocking idle thread while registering itself. It also
* avoids the possibility of reregistration in case a CPU
* restarts its idle thread.
*/
CALLB_CPR_INIT_SAFE(tp, "idle");
/*
* Create the thread_reaper daemon. From this point on, exited
* threads will get reaped.
*/
(void) thread_create(NULL, 0, (void (*)())thread_reaper,
NULL, 0, &p0, TS_RUN, minclsyspri);
/*
* Finish initializing the kernel memory allocator now that
* thread_create() is available.
*/
kmem_thread_init();
if (boothowto & RB_DEBUG)
kdi_dvec_thravail();
}
/*
* Create a thread.
*
* thread_create() blocks for memory if necessary. It never fails.
*
* If stk is NULL, the thread is created at the base of the stack
* and cannot be swapped.
*/
kthread_t *
thread_create(
caddr_t stk,
size_t stksize,
void (*proc)(),
void *arg,
size_t len,
proc_t *pp,
int state,
pri_t pri)
{
kthread_t *t;
extern struct classfuncs sys_classfuncs;
turnstile_t *ts;
/*
* Every thread keeps a turnstile around in case it needs to block.
* The only reason the turnstile is not simply part of the thread
* structure is that we may have to break the association whenever
* more than one thread blocks on a given synchronization object.
* From a memory-management standpoint, turnstiles are like the
* "attached mblks" that hang off dblks in the streams allocator.
*/
ts = kmem_cache_alloc(turnstile_cache, KM_SLEEP);
if (stk == NULL) {
/*
* alloc both thread and stack in segkp chunk
*/
if (stksize < default_stksize)
stksize = default_stksize;
if (stksize == default_stksize) {
stk = (caddr_t)segkp_cache_get(segkp_thread);
} else {
stksize = roundup(stksize, PAGESIZE);
stk = (caddr_t)segkp_get(segkp, stksize,
(KPD_HASREDZONE | KPD_NO_ANON | KPD_LOCKED));
}
ASSERT(stk != NULL);
/*
* The machine-dependent mutex code may require that
* thread pointers (since they may be used for mutex owner
* fields) have certain alignment requirements.
* PTR24_ALIGN is the size of the alignment quanta.
* XXX - assumes stack grows toward low addresses.
*/
if (stksize <= sizeof (kthread_t) + PTR24_ALIGN)
cmn_err(CE_PANIC, "thread_create: proposed stack size"
" too small to hold thread.");
#ifdef STACK_GROWTH_DOWN
stksize -= SA(sizeof (kthread_t) + PTR24_ALIGN - 1);
stksize &= -PTR24_ALIGN; /* make thread aligned */
t = (kthread_t *)(stk + stksize);
bzero(t, sizeof (kthread_t));
if (audit_active)
audit_thread_create(t);
t->t_stk = stk + stksize;
t->t_stkbase = stk;
#else /* stack grows to larger addresses */
stksize -= SA(sizeof (kthread_t));
t = (kthread_t *)(stk);
bzero(t, sizeof (kthread_t));
t->t_stk = stk + sizeof (kthread_t);
t->t_stkbase = stk + stksize + sizeof (kthread_t);
#endif /* STACK_GROWTH_DOWN */
t->t_flag |= T_TALLOCSTK;
t->t_swap = stk;
} else {
t = kmem_cache_alloc(thread_cache, KM_SLEEP);
bzero(t, sizeof (kthread_t));
ASSERT(((uintptr_t)t & (PTR24_ALIGN - 1)) == 0);
if (audit_active)
audit_thread_create(t);
/*
* Initialize t_stk to the kernel stack pointer to use
* upon entry to the kernel
*/
#ifdef STACK_GROWTH_DOWN
t->t_stk = stk + stksize;
t->t_stkbase = stk;
#else
t->t_stk = stk; /* 3b2-like */
t->t_stkbase = stk + stksize;
#endif /* STACK_GROWTH_DOWN */
}
if (kmem_stackinfo != 0) {
stkinfo_begin(t);
}
t->t_ts = ts;
/*
* p_cred could be NULL if it thread_create is called before cred_init
* is called in main.
*/
mutex_enter(&pp->p_crlock);
if (pp->p_cred)
crhold(t->t_cred = pp->p_cred);
mutex_exit(&pp->p_crlock);
t->t_start = gethrestime_sec();
t->t_startpc = proc;
t->t_procp = pp;
t->t_clfuncs = &sys_classfuncs.thread;
t->t_cid = syscid;
t->t_pri = pri;
t->t_stime = ddi_get_lbolt();
t->t_schedflag = TS_LOAD | TS_DONT_SWAP;
t->t_bind_cpu = PBIND_NONE;
t->t_bindflag = (uchar_t)default_binding_mode;
t->t_bind_pset = PS_NONE;
t->t_plockp = &pp->p_lock;
t->t_copyops = NULL;
t->t_taskq = NULL;
t->t_anttime = 0;
t->t_hatdepth = 0;
t->t_dtrace_vtime = 1; /* assure vtimestamp is always non-zero */
CPU_STATS_ADDQ(CPU, sys, nthreads, 1);
LOCK_INIT_CLEAR(&t->t_lock);
/*
* Callers who give us a NULL proc must do their own
* stack initialization. e.g. lwp_create()
*/
if (proc != NULL) {
t->t_stk = thread_stk_init(t->t_stk);
thread_load(t, proc, arg, len);
}
/*
* Put a hold on project0. If this thread is actually in a
* different project, then t_proj will be changed later in
* lwp_create(). All kernel-only threads must be in project 0.
*/
t->t_proj = project_hold(proj0p);
lgrp_affinity_init(&t->t_lgrp_affinity);
mutex_enter(&pidlock);
nthread++;
t->t_did = next_t_id++;
t->t_prev = curthread->t_prev;
t->t_next = curthread;
/*
* Add the thread to the list of all threads, and initialize
* its t_cpu pointer. We need to block preemption since
* cpu_offline walks the thread list looking for threads
* with t_cpu pointing to the CPU being offlined. We want
* to make sure that the list is consistent and that if t_cpu
* is set, the thread is on the list.
*/
kpreempt_disable();
curthread->t_prev->t_next = t;
curthread->t_prev = t;
/*
* We'll always create in the default partition since that's where
* kernel threads go (we'll change this later if needed, in
* lwp_create()).
*/
t->t_cpupart = &cp_default;
/*
* For now, affiliate this thread with the root lgroup.
* Since the kernel does not (presently) allocate its memory
* in a locality aware fashion, the root is an appropriate home.
* If this thread is later associated with an lwp, it will have
* its lgroup re-assigned at that time.
*/
lgrp_move_thread(t, &cp_default.cp_lgrploads[LGRP_ROOTID], 1);
/*
* If the current CPU is in the default cpupart, use it. Otherwise,
* pick one that is; before entering the dispatcher code, we'll
* make sure to keep the invariant that ->t_cpu is set. (In fact, we
* rely on this, in ht_should_run(), in the call tree of
* disp_lowpri_cpu().)
*/
if (CPU->cpu_part == &cp_default) {
t->t_cpu = CPU;
} else {
t->t_cpu = cp_default.cp_cpulist;
t->t_cpu = disp_lowpri_cpu(t->t_cpu, t, t->t_pri);
}
t->t_disp_queue = t->t_cpu->cpu_disp;
kpreempt_enable();
/*
* Initialize thread state and the dispatcher lock pointer.
* Need to hold onto pidlock to block allthreads walkers until
* the state is set.
*/
switch (state) {
case TS_RUN:
curthread->t_oldspl = splhigh(); /* get dispatcher spl */
THREAD_SET_STATE(t, TS_STOPPED, &transition_lock);
CL_SETRUN(t);
thread_unlock(t);
break;
case TS_ONPROC:
THREAD_ONPROC(t, t->t_cpu);
break;
case TS_FREE:
/*
* Free state will be used for intr threads.
* The interrupt routine must set the thread dispatcher
* lock pointer (t_lockp) if starting on a CPU
* other than the current one.
*/
THREAD_FREEINTR(t, CPU);
break;
case TS_STOPPED:
THREAD_SET_STATE(t, TS_STOPPED, &stop_lock);
break;
default: /* TS_SLEEP, TS_ZOMB or TS_TRANS */
cmn_err(CE_PANIC, "thread_create: invalid state %d", state);
}
mutex_exit(&pidlock);
return (t);
}
/*
* Move thread to project0 and take care of project reference counters.
*/
void
thread_rele(kthread_t *t)
{
kproject_t *kpj;
thread_lock(t);
ASSERT(t == curthread || t->t_state == TS_FREE || t->t_procp == &p0);
kpj = ttoproj(t);
t->t_proj = proj0p;
thread_unlock(t);
if (kpj != proj0p) {
project_rele(kpj);
(void) project_hold(proj0p);
}
}
void
thread_exit(void)
{
kthread_t *t = curthread;
if ((t->t_proc_flag & TP_ZTHREAD) != 0)
cmn_err(CE_PANIC, "thread_exit: zthread_exit() not called");
tsd_exit(); /* Clean up this thread's TSD */
kcpc_passivate(); /* clean up performance counter state */
/*
* No kernel thread should have called poll() without arranging
* calling pollcleanup() here.
*/
ASSERT(t->t_pollstate == NULL);
ASSERT(t->t_schedctl == NULL);
if (t->t_door)
door_slam(); /* in case thread did an upcall */
thread_rele(t);
t->t_preempt++;
/*
* remove thread from the all threads list so that
* death-row can use the same pointers.
*/
mutex_enter(&pidlock);
t->t_next->t_prev = t->t_prev;
t->t_prev->t_next = t->t_next;
ASSERT(allthreads != t); /* t0 never exits */
cv_broadcast(&t->t_joincv); /* wake up anyone in thread_join */
mutex_exit(&pidlock);
if (t->t_ctx != NULL)
exitctx(t);
if (t->t_procp->p_pctx != NULL)
exitpctx(t->t_procp);
if (kmem_stackinfo != 0) {
stkinfo_end(t);
}
t->t_state = TS_ZOMB; /* set zombie thread */
swtch_from_zombie(); /* give up the CPU */
/* NOTREACHED */
}
/*
* Check to see if the specified thread is active (defined as being on
* the thread list). This is certainly a slow way to do this; if there's
* ever a reason to speed it up, we could maintain a hash table of active
* threads indexed by their t_did.
*/
static kthread_t *
did_to_thread(kt_did_t tid)
{
kthread_t *t;
ASSERT(MUTEX_HELD(&pidlock));
for (t = curthread->t_next; t != curthread; t = t->t_next) {
if (t->t_did == tid)
break;
}
if (t->t_did == tid)
return (t);
else
return (NULL);
}
/*
* Wait for specified thread to exit. Returns immediately if the thread
* could not be found, meaning that it has either already exited or never
* existed.
*/
void
thread_join(kt_did_t tid)
{
kthread_t *t;
ASSERT(tid != curthread->t_did);
ASSERT(tid != t0.t_did);
mutex_enter(&pidlock);
/*
* Make sure we check that the thread is on the thread list
* before blocking on it; otherwise we could end up blocking on
* a cv that's already been freed. In other words, don't cache
* the thread pointer across calls to cv_wait.
*
* The choice of loop invariant means that whenever a thread
* is taken off the allthreads list, a cv_broadcast must be
* performed on that thread's t_joincv to wake up any waiters.
* The broadcast doesn't have to happen right away, but it
* shouldn't be postponed indefinitely (e.g., by doing it in
* thread_free which may only be executed when the deathrow
* queue is processed.
*/
while (t = did_to_thread(tid))
cv_wait(&t->t_joincv, &pidlock);
mutex_exit(&pidlock);
}
void
thread_free_prevent(kthread_t *t)
{
kmutex_t *lp;
lp = &thread_free_lock[THREAD_FREE_HASH(t)].tf_lock;
mutex_enter(lp);
}
void
thread_free_allow(kthread_t *t)
{
kmutex_t *lp;
lp = &thread_free_lock[THREAD_FREE_HASH(t)].tf_lock;
mutex_exit(lp);
}
static void
thread_free_barrier(kthread_t *t)
{
kmutex_t *lp;
lp = &thread_free_lock[THREAD_FREE_HASH(t)].tf_lock;
mutex_enter(lp);
mutex_exit(lp);
}
void
thread_free(kthread_t *t)
{
boolean_t allocstk = (t->t_flag & T_TALLOCSTK);
klwp_t *lwp = t->t_lwp;
caddr_t swap = t->t_swap;
ASSERT(t != &t0 && t->t_state == TS_FREE);
ASSERT(t->t_door == NULL);
ASSERT(t->t_schedctl == NULL);
ASSERT(t->t_pollstate == NULL);
t->t_pri = 0;
t->t_pc = 0;
t->t_sp = 0;
t->t_wchan0 = NULL;
t->t_wchan = NULL;
if (t->t_cred != NULL) {
crfree(t->t_cred);
t->t_cred = 0;
}
if (t->t_pdmsg) {
kmem_free(t->t_pdmsg, strlen(t->t_pdmsg) + 1);
t->t_pdmsg = NULL;
}
if (audit_active)
audit_thread_free(t);
if (t->t_cldata) {
CL_EXITCLASS(t->t_cid, (caddr_t *)t->t_cldata);
}
if (t->t_rprof != NULL) {
kmem_free(t->t_rprof, sizeof (*t->t_rprof));
t->t_rprof = NULL;
}
t->t_lockp = NULL; /* nothing should try to lock this thread now */
if (lwp)
lwp_freeregs(lwp, 0);
if (t->t_ctx)
freectx(t, 0);
t->t_stk = NULL;
if (lwp)
lwp_stk_fini(lwp);
lock_clear(&t->t_lock);
if (t->t_ts->ts_waiters > 0)
panic("thread_free: turnstile still active");
kmem_cache_free(turnstile_cache, t->t_ts);
free_afd(&t->t_activefd);
/*
* Barrier for the tick accounting code. The tick accounting code
* holds this lock to keep the thread from going away while it's
* looking at it.
*/
thread_free_barrier(t);
ASSERT(ttoproj(t) == proj0p);
project_rele(ttoproj(t));
lgrp_affinity_free(&t->t_lgrp_affinity);
mutex_enter(&pidlock);
nthread--;
mutex_exit(&pidlock);
if (t->t_name != NULL) {
kmem_free(t->t_name, THREAD_NAME_MAX);
t->t_name = NULL;
}
/*
* Free thread, lwp and stack. This needs to be done carefully, since
* if T_TALLOCSTK is set, the thread is part of the stack.
*/
t->t_lwp = NULL;
t->t_swap = NULL;
if (swap) {
segkp_release(segkp, swap);
}
if (lwp) {
kmem_cache_free(lwp_cache, lwp);
}
if (!allocstk) {
kmem_cache_free(thread_cache, t);
}
}
/*
* Removes threads associated with the given zone from a deathrow queue.
* tp is a pointer to the head of the deathrow queue, and countp is a
* pointer to the current deathrow count. Returns a linked list of
* threads removed from the list.
*/
static kthread_t *
thread_zone_cleanup(kthread_t **tp, int *countp, zoneid_t zoneid)
{
kthread_t *tmp, *list = NULL;
cred_t *cr;
ASSERT(MUTEX_HELD(&reaplock));
while (*tp != NULL) {
if ((cr = (*tp)->t_cred) != NULL && crgetzoneid(cr) == zoneid) {
tmp = *tp;
*tp = tmp->t_forw;
tmp->t_forw = list;
list = tmp;
(*countp)--;
} else {
tp = &(*tp)->t_forw;
}
}
return (list);
}
static void
thread_reap_list(kthread_t *t)
{
kthread_t *next;
while (t != NULL) {
next = t->t_forw;
thread_free(t);
t = next;
}
}
/* ARGSUSED */
static void
thread_zone_destroy(zoneid_t zoneid, void *unused)
{
kthread_t *t, *l;
mutex_enter(&reaplock);
/*
* Pull threads and lwps associated with zone off deathrow lists.
*/
t = thread_zone_cleanup(&thread_deathrow, &thread_reapcnt, zoneid);
l = thread_zone_cleanup(&lwp_deathrow, &lwp_reapcnt, zoneid);
mutex_exit(&reaplock);
/*
* Guard against race condition in mutex_owner_running:
* thread=owner(mutex)
* <interrupt>
* thread exits mutex
* thread exits
* thread reaped
* thread struct freed
* cpu = thread->t_cpu <- BAD POINTER DEREFERENCE.
* A cross call to all cpus will cause the interrupt handler
* to reset the PC if it is in mutex_owner_running, refreshing
* stale thread pointers.
*/
mutex_sync(); /* sync with mutex code */
/*
* Reap threads
*/
thread_reap_list(t);
/*
* Reap lwps
*/
thread_reap_list(l);
}
/*
* cleanup zombie threads that are on deathrow.
*/
void
thread_reaper()
{
kthread_t *t, *l;
callb_cpr_t cprinfo;
/*
* Register callback to clean up threads when zone is destroyed.
*/
zone_key_create(&zone_thread_key, NULL, NULL, thread_zone_destroy);
CALLB_CPR_INIT(&cprinfo, &reaplock, callb_generic_cpr, "t_reaper");
for (;;) {
mutex_enter(&reaplock);
while (thread_deathrow == NULL && lwp_deathrow == NULL) {
CALLB_CPR_SAFE_BEGIN(&cprinfo);
cv_wait(&reaper_cv, &reaplock);
CALLB_CPR_SAFE_END(&cprinfo, &reaplock);
}
/*
* mutex_sync() needs to be called when reaping, but
* not too often. We limit reaping rate to once
* per second. Reaplimit is max rate at which threads can
* be freed. Does not impact thread destruction/creation.
*/
t = thread_deathrow;
l = lwp_deathrow;
thread_deathrow = NULL;
lwp_deathrow = NULL;
thread_reapcnt = 0;
lwp_reapcnt = 0;
mutex_exit(&reaplock);
/*
* Guard against race condition in mutex_owner_running:
* thread=owner(mutex)
* <interrupt>
* thread exits mutex
* thread exits
* thread reaped
* thread struct freed
* cpu = thread->t_cpu <- BAD POINTER DEREFERENCE.
* A cross call to all cpus will cause the interrupt handler
* to reset the PC if it is in mutex_owner_running, refreshing
* stale thread pointers.
*/
mutex_sync(); /* sync with mutex code */
/*
* Reap threads
*/
thread_reap_list(t);
/*
* Reap lwps
*/
thread_reap_list(l);
delay(hz);
}
}
/*
* This is called by lwpcreate, etc.() to put a lwp_deathrow thread onto
* thread_deathrow. The thread's state is changed already TS_FREE to indicate
* that is reapable. The thread already holds the reaplock, and was already
* freed.
*/
void
reapq_move_lq_to_tq(kthread_t *t)
{
ASSERT(t->t_state == TS_FREE);
ASSERT(MUTEX_HELD(&reaplock));
t->t_forw = thread_deathrow;
thread_deathrow = t;
thread_reapcnt++;
if (lwp_reapcnt + thread_reapcnt > reaplimit)
cv_signal(&reaper_cv); /* wake the reaper */
}
/*
* This is called by resume() to put a zombie thread onto deathrow.
* The thread's state is changed to TS_FREE to indicate that is reapable.
* This is called from the idle thread so it must not block - just spin.
*/
void
reapq_add(kthread_t *t)
{
mutex_enter(&reaplock);
/*
* lwp_deathrow contains threads with lwp linkage and
* swappable thread stacks which have the default stacksize.
* These threads' lwps and stacks may be reused by lwp_create().
*
* Anything else goes on thread_deathrow(), where it will eventually
* be thread_free()d.
*/
if (t->t_flag & T_LWPREUSE) {
ASSERT(ttolwp(t) != NULL);
t->t_forw = lwp_deathrow;
lwp_deathrow = t;
lwp_reapcnt++;
} else {
t->t_forw = thread_deathrow;
thread_deathrow = t;
thread_reapcnt++;
}
if (lwp_reapcnt + thread_reapcnt > reaplimit)
cv_signal(&reaper_cv); /* wake the reaper */
t->t_state = TS_FREE;
lock_clear(&t->t_lock);
/*
* Before we return, we need to grab and drop the thread lock for
* the dead thread. At this point, the current thread is the idle
* thread, and the dead thread's CPU lock points to the current
* CPU -- and we must grab and drop the lock to synchronize with
* a racing thread walking a blocking chain that the zombie thread