Print this page
10924 Need mitigation of L1TF (CVE-2018-3646)
Reviewed by: Robert Mustacchi <rm@joyent.com>
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Reviewed by: Peter Tribble <peter.tribble@gmail.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/disp/thread.c
+++ new/usr/src/uts/common/disp/thread.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright (c) 2018 Joyent, Inc.
25 25 */
26 26
27 27 #include <sys/types.h>
28 28 #include <sys/param.h>
29 29 #include <sys/sysmacros.h>
30 30 #include <sys/signal.h>
31 31 #include <sys/stack.h>
32 32 #include <sys/pcb.h>
33 33 #include <sys/user.h>
34 34 #include <sys/systm.h>
35 35 #include <sys/sysinfo.h>
36 36 #include <sys/errno.h>
37 37 #include <sys/cmn_err.h>
38 38 #include <sys/cred.h>
39 39 #include <sys/resource.h>
40 40 #include <sys/task.h>
41 41 #include <sys/project.h>
42 42 #include <sys/proc.h>
43 43 #include <sys/debug.h>
44 44 #include <sys/disp.h>
45 45 #include <sys/class.h>
46 46 #include <vm/seg_kmem.h>
47 47 #include <vm/seg_kp.h>
48 48 #include <sys/machlock.h>
49 49 #include <sys/kmem.h>
50 50 #include <sys/varargs.h>
51 51 #include <sys/turnstile.h>
52 52 #include <sys/poll.h>
53 53 #include <sys/vtrace.h>
54 54 #include <sys/callb.h>
55 55 #include <c2/audit.h>
56 56 #include <sys/tnf.h>
57 57 #include <sys/sobject.h>
58 58 #include <sys/cpupart.h>
59 59 #include <sys/pset.h>
60 60 #include <sys/door.h>
61 61 #include <sys/spl.h>
62 62 #include <sys/copyops.h>
63 63 #include <sys/rctl.h>
64 64 #include <sys/brand.h>
65 65 #include <sys/pool.h>
66 66 #include <sys/zone.h>
67 67 #include <sys/tsol/label.h>
↓ open down ↓ |
67 lines elided |
↑ open up ↑ |
68 68 #include <sys/tsol/tndb.h>
69 69 #include <sys/cpc_impl.h>
70 70 #include <sys/sdt.h>
71 71 #include <sys/reboot.h>
72 72 #include <sys/kdi.h>
73 73 #include <sys/schedctl.h>
74 74 #include <sys/waitq.h>
75 75 #include <sys/cpucaps.h>
76 76 #include <sys/kiconv.h>
77 77 #include <sys/ctype.h>
78 +#include <sys/ht.h>
78 79
79 80 struct kmem_cache *thread_cache; /* cache of free threads */
80 81 struct kmem_cache *lwp_cache; /* cache of free lwps */
81 82 struct kmem_cache *turnstile_cache; /* cache of free turnstiles */
82 83
83 84 /*
84 85 * allthreads is only for use by kmem_readers. All kernel loops can use
85 86 * the current thread as a start/end point.
86 87 */
87 88 kthread_t *allthreads = &t0; /* circular list of all threads */
88 89
89 90 static kcondvar_t reaper_cv; /* synchronization var */
90 91 kthread_t *thread_deathrow; /* circular list of reapable threads */
91 92 kthread_t *lwp_deathrow; /* circular list of reapable threads */
92 93 kmutex_t reaplock; /* protects lwp and thread deathrows */
93 94 int thread_reapcnt = 0; /* number of threads on deathrow */
94 95 int lwp_reapcnt = 0; /* number of lwps on deathrow */
95 96 int reaplimit = 16; /* delay reaping until reaplimit */
96 97
97 98 thread_free_lock_t *thread_free_lock;
98 99 /* protects tick thread from reaper */
99 100
100 101 extern int nthread;
101 102
102 103 /* System Scheduling classes. */
103 104 id_t syscid; /* system scheduling class ID */
104 105 id_t sysdccid = CLASS_UNUSED; /* reset when SDC loads */
105 106
106 107 void *segkp_thread; /* cookie for segkp pool */
107 108
108 109 int lwp_cache_sz = 32;
109 110 int t_cache_sz = 8;
110 111 static kt_did_t next_t_id = 1;
111 112
112 113 /* Default mode for thread binding to CPUs and processor sets */
113 114 int default_binding_mode = TB_ALLHARD;
114 115
115 116 /*
116 117 * Min/Max stack sizes for stack size parameters
117 118 */
118 119 #define MAX_STKSIZE (32 * DEFAULTSTKSZ)
119 120 #define MIN_STKSIZE DEFAULTSTKSZ
120 121
121 122 /*
122 123 * default_stksize overrides lwp_default_stksize if it is set.
123 124 */
124 125 int default_stksize;
125 126 int lwp_default_stksize;
126 127
127 128 static zone_key_t zone_thread_key;
128 129
129 130 unsigned int kmem_stackinfo; /* stackinfo feature on-off */
130 131 kmem_stkinfo_t *kmem_stkinfo_log; /* stackinfo circular log */
131 132 static kmutex_t kmem_stkinfo_lock; /* protects kmem_stkinfo_log */
132 133
133 134 /*
134 135 * forward declarations for internal thread specific data (tsd)
135 136 */
136 137 static void *tsd_realloc(void *, size_t, size_t);
137 138
138 139 void thread_reaper(void);
139 140
140 141 /* forward declarations for stackinfo feature */
141 142 static void stkinfo_begin(kthread_t *);
142 143 static void stkinfo_end(kthread_t *);
143 144 static size_t stkinfo_percent(caddr_t, caddr_t, caddr_t);
144 145
145 146 /*ARGSUSED*/
146 147 static int
147 148 turnstile_constructor(void *buf, void *cdrarg, int kmflags)
148 149 {
149 150 bzero(buf, sizeof (turnstile_t));
150 151 return (0);
151 152 }
152 153
153 154 /*ARGSUSED*/
154 155 static void
155 156 turnstile_destructor(void *buf, void *cdrarg)
156 157 {
157 158 turnstile_t *ts = buf;
158 159
159 160 ASSERT(ts->ts_free == NULL);
160 161 ASSERT(ts->ts_waiters == 0);
161 162 ASSERT(ts->ts_inheritor == NULL);
162 163 ASSERT(ts->ts_sleepq[0].sq_first == NULL);
163 164 ASSERT(ts->ts_sleepq[1].sq_first == NULL);
164 165 }
165 166
166 167 void
167 168 thread_init(void)
168 169 {
169 170 kthread_t *tp;
170 171 extern char sys_name[];
171 172 extern void idle();
172 173 struct cpu *cpu = CPU;
173 174 int i;
174 175 kmutex_t *lp;
175 176
176 177 mutex_init(&reaplock, NULL, MUTEX_SPIN, (void *)ipltospl(DISP_LEVEL));
177 178 thread_free_lock =
178 179 kmem_alloc(sizeof (thread_free_lock_t) * THREAD_FREE_NUM, KM_SLEEP);
179 180 for (i = 0; i < THREAD_FREE_NUM; i++) {
180 181 lp = &thread_free_lock[i].tf_lock;
181 182 mutex_init(lp, NULL, MUTEX_DEFAULT, NULL);
182 183 }
183 184
184 185 #if defined(__i386) || defined(__amd64)
185 186 thread_cache = kmem_cache_create("thread_cache", sizeof (kthread_t),
186 187 PTR24_ALIGN, NULL, NULL, NULL, NULL, NULL, 0);
187 188
188 189 /*
189 190 * "struct _klwp" includes a "struct pcb", which includes a
190 191 * "struct fpu", which needs to be 64-byte aligned on amd64
191 192 * (and even on i386) for xsave/xrstor.
192 193 */
193 194 lwp_cache = kmem_cache_create("lwp_cache", sizeof (klwp_t),
194 195 64, NULL, NULL, NULL, NULL, NULL, 0);
195 196 #else
196 197 /*
197 198 * Allocate thread structures from static_arena. This prevents
198 199 * issues where a thread tries to relocate its own thread
199 200 * structure and touches it after the mapping has been suspended.
200 201 */
201 202 thread_cache = kmem_cache_create("thread_cache", sizeof (kthread_t),
202 203 PTR24_ALIGN, NULL, NULL, NULL, NULL, static_arena, 0);
203 204
204 205 lwp_stk_cache_init();
205 206
206 207 lwp_cache = kmem_cache_create("lwp_cache", sizeof (klwp_t),
207 208 0, NULL, NULL, NULL, NULL, NULL, 0);
208 209 #endif
209 210
210 211 turnstile_cache = kmem_cache_create("turnstile_cache",
211 212 sizeof (turnstile_t), 0,
212 213 turnstile_constructor, turnstile_destructor, NULL, NULL, NULL, 0);
213 214
214 215 label_init();
215 216 cred_init();
216 217
217 218 /*
218 219 * Initialize various resource management facilities.
219 220 */
220 221 rctl_init();
221 222 cpucaps_init();
222 223 /*
223 224 * Zone_init() should be called before project_init() so that project ID
224 225 * for the first project is initialized correctly.
225 226 */
226 227 zone_init();
227 228 project_init();
228 229 brand_init();
229 230 kiconv_init();
230 231 task_init();
231 232 tcache_init();
232 233 pool_init();
233 234
234 235 curthread->t_ts = kmem_cache_alloc(turnstile_cache, KM_SLEEP);
235 236
236 237 /*
237 238 * Originally, we had two parameters to set default stack
238 239 * size: one for lwp's (lwp_default_stksize), and one for
239 240 * kernel-only threads (DEFAULTSTKSZ, a.k.a. _defaultstksz).
240 241 * Now we have a third parameter that overrides both if it is
241 242 * set to a legal stack size, called default_stksize.
242 243 */
243 244
244 245 if (default_stksize == 0) {
245 246 default_stksize = DEFAULTSTKSZ;
246 247 } else if (default_stksize % PAGESIZE != 0 ||
247 248 default_stksize > MAX_STKSIZE ||
248 249 default_stksize < MIN_STKSIZE) {
249 250 cmn_err(CE_WARN, "Illegal stack size. Using %d",
250 251 (int)DEFAULTSTKSZ);
251 252 default_stksize = DEFAULTSTKSZ;
252 253 } else {
253 254 lwp_default_stksize = default_stksize;
254 255 }
255 256
256 257 if (lwp_default_stksize == 0) {
257 258 lwp_default_stksize = default_stksize;
258 259 } else if (lwp_default_stksize % PAGESIZE != 0 ||
259 260 lwp_default_stksize > MAX_STKSIZE ||
260 261 lwp_default_stksize < MIN_STKSIZE) {
261 262 cmn_err(CE_WARN, "Illegal stack size. Using %d",
262 263 default_stksize);
263 264 lwp_default_stksize = default_stksize;
264 265 }
265 266
266 267 segkp_lwp = segkp_cache_init(segkp, lwp_cache_sz,
267 268 lwp_default_stksize,
268 269 (KPD_NOWAIT | KPD_HASREDZONE | KPD_LOCKED));
269 270
270 271 segkp_thread = segkp_cache_init(segkp, t_cache_sz,
271 272 default_stksize, KPD_HASREDZONE | KPD_LOCKED | KPD_NO_ANON);
272 273
273 274 (void) getcid(sys_name, &syscid);
274 275 curthread->t_cid = syscid; /* current thread is t0 */
275 276
276 277 /*
277 278 * Set up the first CPU's idle thread.
278 279 * It runs whenever the CPU has nothing worthwhile to do.
279 280 */
280 281 tp = thread_create(NULL, 0, idle, NULL, 0, &p0, TS_STOPPED, -1);
281 282 cpu->cpu_idle_thread = tp;
282 283 tp->t_preempt = 1;
283 284 tp->t_disp_queue = cpu->cpu_disp;
284 285 ASSERT(tp->t_disp_queue != NULL);
285 286 tp->t_bound_cpu = cpu;
286 287 tp->t_affinitycnt = 1;
287 288
288 289 /*
289 290 * Registering a thread in the callback table is usually
290 291 * done in the initialization code of the thread. In this
291 292 * case, we do it right after thread creation to avoid
292 293 * blocking idle thread while registering itself. It also
293 294 * avoids the possibility of reregistration in case a CPU
294 295 * restarts its idle thread.
295 296 */
296 297 CALLB_CPR_INIT_SAFE(tp, "idle");
297 298
298 299 /*
299 300 * Create the thread_reaper daemon. From this point on, exited
300 301 * threads will get reaped.
301 302 */
302 303 (void) thread_create(NULL, 0, (void (*)())thread_reaper,
303 304 NULL, 0, &p0, TS_RUN, minclsyspri);
304 305
305 306 /*
306 307 * Finish initializing the kernel memory allocator now that
307 308 * thread_create() is available.
308 309 */
309 310 kmem_thread_init();
310 311
311 312 if (boothowto & RB_DEBUG)
312 313 kdi_dvec_thravail();
313 314 }
314 315
315 316 /*
316 317 * Create a thread.
317 318 *
318 319 * thread_create() blocks for memory if necessary. It never fails.
319 320 *
320 321 * If stk is NULL, the thread is created at the base of the stack
321 322 * and cannot be swapped.
322 323 */
323 324 kthread_t *
324 325 thread_create(
325 326 caddr_t stk,
326 327 size_t stksize,
327 328 void (*proc)(),
328 329 void *arg,
329 330 size_t len,
330 331 proc_t *pp,
331 332 int state,
332 333 pri_t pri)
333 334 {
334 335 kthread_t *t;
335 336 extern struct classfuncs sys_classfuncs;
336 337 turnstile_t *ts;
337 338
338 339 /*
339 340 * Every thread keeps a turnstile around in case it needs to block.
340 341 * The only reason the turnstile is not simply part of the thread
341 342 * structure is that we may have to break the association whenever
342 343 * more than one thread blocks on a given synchronization object.
343 344 * From a memory-management standpoint, turnstiles are like the
344 345 * "attached mblks" that hang off dblks in the streams allocator.
345 346 */
346 347 ts = kmem_cache_alloc(turnstile_cache, KM_SLEEP);
347 348
348 349 if (stk == NULL) {
349 350 /*
350 351 * alloc both thread and stack in segkp chunk
351 352 */
352 353
353 354 if (stksize < default_stksize)
354 355 stksize = default_stksize;
355 356
356 357 if (stksize == default_stksize) {
357 358 stk = (caddr_t)segkp_cache_get(segkp_thread);
358 359 } else {
359 360 stksize = roundup(stksize, PAGESIZE);
360 361 stk = (caddr_t)segkp_get(segkp, stksize,
361 362 (KPD_HASREDZONE | KPD_NO_ANON | KPD_LOCKED));
362 363 }
363 364
364 365 ASSERT(stk != NULL);
365 366
366 367 /*
367 368 * The machine-dependent mutex code may require that
368 369 * thread pointers (since they may be used for mutex owner
369 370 * fields) have certain alignment requirements.
370 371 * PTR24_ALIGN is the size of the alignment quanta.
371 372 * XXX - assumes stack grows toward low addresses.
372 373 */
373 374 if (stksize <= sizeof (kthread_t) + PTR24_ALIGN)
374 375 cmn_err(CE_PANIC, "thread_create: proposed stack size"
375 376 " too small to hold thread.");
376 377 #ifdef STACK_GROWTH_DOWN
377 378 stksize -= SA(sizeof (kthread_t) + PTR24_ALIGN - 1);
378 379 stksize &= -PTR24_ALIGN; /* make thread aligned */
379 380 t = (kthread_t *)(stk + stksize);
380 381 bzero(t, sizeof (kthread_t));
381 382 if (audit_active)
382 383 audit_thread_create(t);
383 384 t->t_stk = stk + stksize;
384 385 t->t_stkbase = stk;
385 386 #else /* stack grows to larger addresses */
386 387 stksize -= SA(sizeof (kthread_t));
387 388 t = (kthread_t *)(stk);
388 389 bzero(t, sizeof (kthread_t));
389 390 t->t_stk = stk + sizeof (kthread_t);
390 391 t->t_stkbase = stk + stksize + sizeof (kthread_t);
391 392 #endif /* STACK_GROWTH_DOWN */
392 393 t->t_flag |= T_TALLOCSTK;
393 394 t->t_swap = stk;
394 395 } else {
395 396 t = kmem_cache_alloc(thread_cache, KM_SLEEP);
396 397 bzero(t, sizeof (kthread_t));
397 398 ASSERT(((uintptr_t)t & (PTR24_ALIGN - 1)) == 0);
398 399 if (audit_active)
399 400 audit_thread_create(t);
400 401 /*
401 402 * Initialize t_stk to the kernel stack pointer to use
402 403 * upon entry to the kernel
403 404 */
404 405 #ifdef STACK_GROWTH_DOWN
405 406 t->t_stk = stk + stksize;
406 407 t->t_stkbase = stk;
407 408 #else
408 409 t->t_stk = stk; /* 3b2-like */
409 410 t->t_stkbase = stk + stksize;
410 411 #endif /* STACK_GROWTH_DOWN */
411 412 }
412 413
413 414 if (kmem_stackinfo != 0) {
414 415 stkinfo_begin(t);
415 416 }
416 417
417 418 t->t_ts = ts;
418 419
419 420 /*
420 421 * p_cred could be NULL if it thread_create is called before cred_init
421 422 * is called in main.
422 423 */
423 424 mutex_enter(&pp->p_crlock);
424 425 if (pp->p_cred)
425 426 crhold(t->t_cred = pp->p_cred);
426 427 mutex_exit(&pp->p_crlock);
427 428 t->t_start = gethrestime_sec();
428 429 t->t_startpc = proc;
429 430 t->t_procp = pp;
430 431 t->t_clfuncs = &sys_classfuncs.thread;
431 432 t->t_cid = syscid;
432 433 t->t_pri = pri;
433 434 t->t_stime = ddi_get_lbolt();
434 435 t->t_schedflag = TS_LOAD | TS_DONT_SWAP;
435 436 t->t_bind_cpu = PBIND_NONE;
436 437 t->t_bindflag = (uchar_t)default_binding_mode;
437 438 t->t_bind_pset = PS_NONE;
438 439 t->t_plockp = &pp->p_lock;
439 440 t->t_copyops = NULL;
440 441 t->t_taskq = NULL;
441 442 t->t_anttime = 0;
442 443 t->t_hatdepth = 0;
443 444
444 445 t->t_dtrace_vtime = 1; /* assure vtimestamp is always non-zero */
445 446
446 447 CPU_STATS_ADDQ(CPU, sys, nthreads, 1);
447 448 #ifndef NPROBE
448 449 /* Kernel probe */
449 450 tnf_thread_create(t);
450 451 #endif /* NPROBE */
451 452 LOCK_INIT_CLEAR(&t->t_lock);
452 453
453 454 /*
454 455 * Callers who give us a NULL proc must do their own
455 456 * stack initialization. e.g. lwp_create()
456 457 */
457 458 if (proc != NULL) {
458 459 t->t_stk = thread_stk_init(t->t_stk);
459 460 thread_load(t, proc, arg, len);
460 461 }
461 462
462 463 /*
463 464 * Put a hold on project0. If this thread is actually in a
464 465 * different project, then t_proj will be changed later in
465 466 * lwp_create(). All kernel-only threads must be in project 0.
466 467 */
467 468 t->t_proj = project_hold(proj0p);
468 469
469 470 lgrp_affinity_init(&t->t_lgrp_affinity);
470 471
471 472 mutex_enter(&pidlock);
472 473 nthread++;
473 474 t->t_did = next_t_id++;
474 475 t->t_prev = curthread->t_prev;
475 476 t->t_next = curthread;
476 477
477 478 /*
478 479 * Add the thread to the list of all threads, and initialize
479 480 * its t_cpu pointer. We need to block preemption since
↓ open down ↓ |
392 lines elided |
↑ open up ↑ |
480 481 * cpu_offline walks the thread list looking for threads
481 482 * with t_cpu pointing to the CPU being offlined. We want
482 483 * to make sure that the list is consistent and that if t_cpu
483 484 * is set, the thread is on the list.
484 485 */
485 486 kpreempt_disable();
486 487 curthread->t_prev->t_next = t;
487 488 curthread->t_prev = t;
488 489
489 490 /*
490 - * Threads should never have a NULL t_cpu pointer so assign it
491 - * here. If the thread is being created with state TS_RUN a
492 - * better CPU may be chosen when it is placed on the run queue.
493 - *
494 - * We need to keep kernel preemption disabled when setting all
495 - * three fields to keep them in sync. Also, always create in
496 - * the default partition since that's where kernel threads go
497 - * (if this isn't a kernel thread, t_cpupart will be changed
498 - * in lwp_create before setting the thread runnable).
491 + * We'll always create in the default partition since that's where
492 + * kernel threads go (we'll change this later if needed, in
493 + * lwp_create()).
499 494 */
500 495 t->t_cpupart = &cp_default;
501 496
502 497 /*
503 498 * For now, affiliate this thread with the root lgroup.
504 499 * Since the kernel does not (presently) allocate its memory
505 500 * in a locality aware fashion, the root is an appropriate home.
506 501 * If this thread is later associated with an lwp, it will have
507 - * it's lgroup re-assigned at that time.
502 + * its lgroup re-assigned at that time.
508 503 */
509 504 lgrp_move_thread(t, &cp_default.cp_lgrploads[LGRP_ROOTID], 1);
510 505
511 506 /*
512 - * Inherit the current cpu. If this cpu isn't part of the chosen
513 - * lgroup, a new cpu will be chosen by cpu_choose when the thread
514 - * is ready to run.
507 + * If the current CPU is in the default cpupart, use it. Otherwise,
508 + * pick one that is; before entering the dispatcher code, we'll
509 + * make sure to keep the invariant that ->t_cpu is set. (In fact, we
510 + * rely on this, in ht_should_run(), in the call tree of
511 + * disp_lowpri_cpu().)
515 512 */
516 - if (CPU->cpu_part == &cp_default)
513 + if (CPU->cpu_part == &cp_default) {
517 514 t->t_cpu = CPU;
518 - else
519 - t->t_cpu = disp_lowpri_cpu(cp_default.cp_cpulist, t->t_lpl,
520 - t->t_pri, NULL);
515 + } else {
516 + t->t_cpu = cp_default.cp_cpulist;
517 + t->t_cpu = disp_lowpri_cpu(t->t_cpu, t, t->t_pri);
518 + }
521 519
522 520 t->t_disp_queue = t->t_cpu->cpu_disp;
523 521 kpreempt_enable();
524 522
525 523 /*
526 524 * Initialize thread state and the dispatcher lock pointer.
527 525 * Need to hold onto pidlock to block allthreads walkers until
528 526 * the state is set.
529 527 */
530 528 switch (state) {
531 529 case TS_RUN:
532 530 curthread->t_oldspl = splhigh(); /* get dispatcher spl */
533 531 THREAD_SET_STATE(t, TS_STOPPED, &transition_lock);
534 532 CL_SETRUN(t);
535 533 thread_unlock(t);
536 534 break;
537 535
538 536 case TS_ONPROC:
539 537 THREAD_ONPROC(t, t->t_cpu);
540 538 break;
541 539
542 540 case TS_FREE:
543 541 /*
544 542 * Free state will be used for intr threads.
545 543 * The interrupt routine must set the thread dispatcher
546 544 * lock pointer (t_lockp) if starting on a CPU
547 545 * other than the current one.
548 546 */
549 547 THREAD_FREEINTR(t, CPU);
550 548 break;
551 549
552 550 case TS_STOPPED:
553 551 THREAD_SET_STATE(t, TS_STOPPED, &stop_lock);
554 552 break;
555 553
556 554 default: /* TS_SLEEP, TS_ZOMB or TS_TRANS */
557 555 cmn_err(CE_PANIC, "thread_create: invalid state %d", state);
558 556 }
559 557 mutex_exit(&pidlock);
560 558 return (t);
561 559 }
562 560
563 561 /*
564 562 * Move thread to project0 and take care of project reference counters.
565 563 */
566 564 void
567 565 thread_rele(kthread_t *t)
568 566 {
569 567 kproject_t *kpj;
570 568
571 569 thread_lock(t);
572 570
573 571 ASSERT(t == curthread || t->t_state == TS_FREE || t->t_procp == &p0);
574 572 kpj = ttoproj(t);
575 573 t->t_proj = proj0p;
576 574
577 575 thread_unlock(t);
578 576
579 577 if (kpj != proj0p) {
580 578 project_rele(kpj);
581 579 (void) project_hold(proj0p);
582 580 }
583 581 }
584 582
585 583 void
586 584 thread_exit(void)
587 585 {
588 586 kthread_t *t = curthread;
589 587
590 588 if ((t->t_proc_flag & TP_ZTHREAD) != 0)
591 589 cmn_err(CE_PANIC, "thread_exit: zthread_exit() not called");
592 590
593 591 tsd_exit(); /* Clean up this thread's TSD */
594 592
595 593 kcpc_passivate(); /* clean up performance counter state */
596 594
597 595 /*
598 596 * No kernel thread should have called poll() without arranging
599 597 * calling pollcleanup() here.
600 598 */
601 599 ASSERT(t->t_pollstate == NULL);
602 600 ASSERT(t->t_schedctl == NULL);
603 601 if (t->t_door)
604 602 door_slam(); /* in case thread did an upcall */
605 603
606 604 #ifndef NPROBE
607 605 /* Kernel probe */
608 606 if (t->t_tnf_tpdp)
609 607 tnf_thread_exit();
610 608 #endif /* NPROBE */
611 609
612 610 thread_rele(t);
613 611 t->t_preempt++;
614 612
615 613 /*
616 614 * remove thread from the all threads list so that
617 615 * death-row can use the same pointers.
618 616 */
619 617 mutex_enter(&pidlock);
620 618 t->t_next->t_prev = t->t_prev;
621 619 t->t_prev->t_next = t->t_next;
622 620 ASSERT(allthreads != t); /* t0 never exits */
623 621 cv_broadcast(&t->t_joincv); /* wake up anyone in thread_join */
624 622 mutex_exit(&pidlock);
625 623
626 624 if (t->t_ctx != NULL)
627 625 exitctx(t);
628 626 if (t->t_procp->p_pctx != NULL)
629 627 exitpctx(t->t_procp);
630 628
631 629 if (kmem_stackinfo != 0) {
632 630 stkinfo_end(t);
633 631 }
634 632
635 633 t->t_state = TS_ZOMB; /* set zombie thread */
636 634
637 635 swtch_from_zombie(); /* give up the CPU */
638 636 /* NOTREACHED */
639 637 }
640 638
641 639 /*
642 640 * Check to see if the specified thread is active (defined as being on
643 641 * the thread list). This is certainly a slow way to do this; if there's
644 642 * ever a reason to speed it up, we could maintain a hash table of active
645 643 * threads indexed by their t_did.
646 644 */
647 645 static kthread_t *
648 646 did_to_thread(kt_did_t tid)
649 647 {
650 648 kthread_t *t;
651 649
652 650 ASSERT(MUTEX_HELD(&pidlock));
653 651 for (t = curthread->t_next; t != curthread; t = t->t_next) {
654 652 if (t->t_did == tid)
655 653 break;
656 654 }
657 655 if (t->t_did == tid)
658 656 return (t);
659 657 else
660 658 return (NULL);
661 659 }
662 660
663 661 /*
664 662 * Wait for specified thread to exit. Returns immediately if the thread
665 663 * could not be found, meaning that it has either already exited or never
666 664 * existed.
667 665 */
668 666 void
669 667 thread_join(kt_did_t tid)
670 668 {
671 669 kthread_t *t;
672 670
673 671 ASSERT(tid != curthread->t_did);
674 672 ASSERT(tid != t0.t_did);
675 673
676 674 mutex_enter(&pidlock);
677 675 /*
678 676 * Make sure we check that the thread is on the thread list
679 677 * before blocking on it; otherwise we could end up blocking on
680 678 * a cv that's already been freed. In other words, don't cache
681 679 * the thread pointer across calls to cv_wait.
682 680 *
683 681 * The choice of loop invariant means that whenever a thread
684 682 * is taken off the allthreads list, a cv_broadcast must be
685 683 * performed on that thread's t_joincv to wake up any waiters.
686 684 * The broadcast doesn't have to happen right away, but it
687 685 * shouldn't be postponed indefinitely (e.g., by doing it in
688 686 * thread_free which may only be executed when the deathrow
689 687 * queue is processed.
690 688 */
691 689 while (t = did_to_thread(tid))
692 690 cv_wait(&t->t_joincv, &pidlock);
693 691 mutex_exit(&pidlock);
694 692 }
695 693
696 694 void
697 695 thread_free_prevent(kthread_t *t)
698 696 {
699 697 kmutex_t *lp;
700 698
701 699 lp = &thread_free_lock[THREAD_FREE_HASH(t)].tf_lock;
702 700 mutex_enter(lp);
703 701 }
704 702
705 703 void
706 704 thread_free_allow(kthread_t *t)
707 705 {
708 706 kmutex_t *lp;
709 707
710 708 lp = &thread_free_lock[THREAD_FREE_HASH(t)].tf_lock;
711 709 mutex_exit(lp);
712 710 }
713 711
714 712 static void
715 713 thread_free_barrier(kthread_t *t)
716 714 {
717 715 kmutex_t *lp;
718 716
719 717 lp = &thread_free_lock[THREAD_FREE_HASH(t)].tf_lock;
720 718 mutex_enter(lp);
721 719 mutex_exit(lp);
722 720 }
723 721
724 722 void
725 723 thread_free(kthread_t *t)
726 724 {
727 725 boolean_t allocstk = (t->t_flag & T_TALLOCSTK);
728 726 klwp_t *lwp = t->t_lwp;
729 727 caddr_t swap = t->t_swap;
730 728
731 729 ASSERT(t != &t0 && t->t_state == TS_FREE);
732 730 ASSERT(t->t_door == NULL);
733 731 ASSERT(t->t_schedctl == NULL);
734 732 ASSERT(t->t_pollstate == NULL);
735 733
736 734 t->t_pri = 0;
737 735 t->t_pc = 0;
738 736 t->t_sp = 0;
739 737 t->t_wchan0 = NULL;
740 738 t->t_wchan = NULL;
741 739 if (t->t_cred != NULL) {
742 740 crfree(t->t_cred);
743 741 t->t_cred = 0;
744 742 }
745 743 if (t->t_pdmsg) {
746 744 kmem_free(t->t_pdmsg, strlen(t->t_pdmsg) + 1);
747 745 t->t_pdmsg = NULL;
748 746 }
749 747 if (audit_active)
750 748 audit_thread_free(t);
751 749 #ifndef NPROBE
752 750 if (t->t_tnf_tpdp)
753 751 tnf_thread_free(t);
754 752 #endif /* NPROBE */
755 753 if (t->t_cldata) {
756 754 CL_EXITCLASS(t->t_cid, (caddr_t *)t->t_cldata);
757 755 }
758 756 if (t->t_rprof != NULL) {
759 757 kmem_free(t->t_rprof, sizeof (*t->t_rprof));
760 758 t->t_rprof = NULL;
761 759 }
762 760 t->t_lockp = NULL; /* nothing should try to lock this thread now */
763 761 if (lwp)
764 762 lwp_freeregs(lwp, 0);
765 763 if (t->t_ctx)
766 764 freectx(t, 0);
767 765 t->t_stk = NULL;
768 766 if (lwp)
769 767 lwp_stk_fini(lwp);
770 768 lock_clear(&t->t_lock);
771 769
772 770 if (t->t_ts->ts_waiters > 0)
773 771 panic("thread_free: turnstile still active");
774 772
775 773 kmem_cache_free(turnstile_cache, t->t_ts);
776 774
777 775 free_afd(&t->t_activefd);
778 776
779 777 /*
780 778 * Barrier for the tick accounting code. The tick accounting code
781 779 * holds this lock to keep the thread from going away while it's
782 780 * looking at it.
783 781 */
784 782 thread_free_barrier(t);
785 783
786 784 ASSERT(ttoproj(t) == proj0p);
787 785 project_rele(ttoproj(t));
788 786
789 787 lgrp_affinity_free(&t->t_lgrp_affinity);
790 788
791 789 mutex_enter(&pidlock);
792 790 nthread--;
793 791 mutex_exit(&pidlock);
794 792
795 793 if (t->t_name != NULL) {
796 794 kmem_free(t->t_name, THREAD_NAME_MAX);
797 795 t->t_name = NULL;
798 796 }
799 797
800 798 /*
801 799 * Free thread, lwp and stack. This needs to be done carefully, since
802 800 * if T_TALLOCSTK is set, the thread is part of the stack.
803 801 */
804 802 t->t_lwp = NULL;
805 803 t->t_swap = NULL;
806 804
807 805 if (swap) {
808 806 segkp_release(segkp, swap);
809 807 }
810 808 if (lwp) {
811 809 kmem_cache_free(lwp_cache, lwp);
812 810 }
813 811 if (!allocstk) {
814 812 kmem_cache_free(thread_cache, t);
815 813 }
816 814 }
817 815
818 816 /*
819 817 * Removes threads associated with the given zone from a deathrow queue.
820 818 * tp is a pointer to the head of the deathrow queue, and countp is a
821 819 * pointer to the current deathrow count. Returns a linked list of
822 820 * threads removed from the list.
823 821 */
824 822 static kthread_t *
825 823 thread_zone_cleanup(kthread_t **tp, int *countp, zoneid_t zoneid)
826 824 {
827 825 kthread_t *tmp, *list = NULL;
828 826 cred_t *cr;
829 827
830 828 ASSERT(MUTEX_HELD(&reaplock));
831 829 while (*tp != NULL) {
832 830 if ((cr = (*tp)->t_cred) != NULL && crgetzoneid(cr) == zoneid) {
833 831 tmp = *tp;
834 832 *tp = tmp->t_forw;
835 833 tmp->t_forw = list;
836 834 list = tmp;
837 835 (*countp)--;
838 836 } else {
839 837 tp = &(*tp)->t_forw;
840 838 }
841 839 }
842 840 return (list);
843 841 }
844 842
845 843 static void
846 844 thread_reap_list(kthread_t *t)
847 845 {
848 846 kthread_t *next;
849 847
850 848 while (t != NULL) {
851 849 next = t->t_forw;
852 850 thread_free(t);
853 851 t = next;
854 852 }
855 853 }
856 854
857 855 /* ARGSUSED */
858 856 static void
859 857 thread_zone_destroy(zoneid_t zoneid, void *unused)
860 858 {
861 859 kthread_t *t, *l;
862 860
↓ open down ↓ |
332 lines elided |
↑ open up ↑ |
863 861 mutex_enter(&reaplock);
864 862 /*
865 863 * Pull threads and lwps associated with zone off deathrow lists.
866 864 */
867 865 t = thread_zone_cleanup(&thread_deathrow, &thread_reapcnt, zoneid);
868 866 l = thread_zone_cleanup(&lwp_deathrow, &lwp_reapcnt, zoneid);
869 867 mutex_exit(&reaplock);
870 868
871 869 /*
872 870 * Guard against race condition in mutex_owner_running:
873 - * thread=owner(mutex)
874 - * <interrupt>
875 - * thread exits mutex
876 - * thread exits
877 - * thread reaped
878 - * thread struct freed
871 + * thread=owner(mutex)
872 + * <interrupt>
873 + * thread exits mutex
874 + * thread exits
875 + * thread reaped
876 + * thread struct freed
879 877 * cpu = thread->t_cpu <- BAD POINTER DEREFERENCE.
880 878 * A cross call to all cpus will cause the interrupt handler
881 879 * to reset the PC if it is in mutex_owner_running, refreshing
882 880 * stale thread pointers.
883 881 */
884 882 mutex_sync(); /* sync with mutex code */
885 883
886 884 /*
887 885 * Reap threads
888 886 */
889 887 thread_reap_list(t);
890 888
891 889 /*
892 890 * Reap lwps
893 891 */
894 892 thread_reap_list(l);
895 893 }
896 894
897 895 /*
898 896 * cleanup zombie threads that are on deathrow.
899 897 */
900 898 void
901 899 thread_reaper()
902 900 {
903 901 kthread_t *t, *l;
904 902 callb_cpr_t cprinfo;
905 903
906 904 /*
907 905 * Register callback to clean up threads when zone is destroyed.
908 906 */
909 907 zone_key_create(&zone_thread_key, NULL, NULL, thread_zone_destroy);
910 908
911 909 CALLB_CPR_INIT(&cprinfo, &reaplock, callb_generic_cpr, "t_reaper");
912 910 for (;;) {
913 911 mutex_enter(&reaplock);
914 912 while (thread_deathrow == NULL && lwp_deathrow == NULL) {
915 913 CALLB_CPR_SAFE_BEGIN(&cprinfo);
916 914 cv_wait(&reaper_cv, &reaplock);
917 915 CALLB_CPR_SAFE_END(&cprinfo, &reaplock);
918 916 }
919 917 /*
920 918 * mutex_sync() needs to be called when reaping, but
921 919 * not too often. We limit reaping rate to once
922 920 * per second. Reaplimit is max rate at which threads can
923 921 * be freed. Does not impact thread destruction/creation.
924 922 */
↓ open down ↓ |
36 lines elided |
↑ open up ↑ |
925 923 t = thread_deathrow;
926 924 l = lwp_deathrow;
927 925 thread_deathrow = NULL;
928 926 lwp_deathrow = NULL;
929 927 thread_reapcnt = 0;
930 928 lwp_reapcnt = 0;
931 929 mutex_exit(&reaplock);
932 930
933 931 /*
934 932 * Guard against race condition in mutex_owner_running:
935 - * thread=owner(mutex)
936 - * <interrupt>
937 - * thread exits mutex
938 - * thread exits
939 - * thread reaped
940 - * thread struct freed
933 + * thread=owner(mutex)
934 + * <interrupt>
935 + * thread exits mutex
936 + * thread exits
937 + * thread reaped
938 + * thread struct freed
941 939 * cpu = thread->t_cpu <- BAD POINTER DEREFERENCE.
942 940 * A cross call to all cpus will cause the interrupt handler
943 941 * to reset the PC if it is in mutex_owner_running, refreshing
944 942 * stale thread pointers.
945 943 */
946 944 mutex_sync(); /* sync with mutex code */
947 945 /*
948 946 * Reap threads
949 947 */
950 948 thread_reap_list(t);
951 949
952 950 /*
953 951 * Reap lwps
954 952 */
955 953 thread_reap_list(l);
956 954 delay(hz);
957 955 }
958 956 }
959 957
960 958 /*
961 959 * This is called by lwpcreate, etc.() to put a lwp_deathrow thread onto
962 960 * thread_deathrow. The thread's state is changed already TS_FREE to indicate
963 961 * that is reapable. The thread already holds the reaplock, and was already
964 962 * freed.
965 963 */
966 964 void
967 965 reapq_move_lq_to_tq(kthread_t *t)
968 966 {
969 967 ASSERT(t->t_state == TS_FREE);
970 968 ASSERT(MUTEX_HELD(&reaplock));
971 969 t->t_forw = thread_deathrow;
972 970 thread_deathrow = t;
973 971 thread_reapcnt++;
974 972 if (lwp_reapcnt + thread_reapcnt > reaplimit)
975 973 cv_signal(&reaper_cv); /* wake the reaper */
976 974 }
977 975
978 976 /*
979 977 * This is called by resume() to put a zombie thread onto deathrow.
980 978 * The thread's state is changed to TS_FREE to indicate that is reapable.
981 979 * This is called from the idle thread so it must not block - just spin.
982 980 */
983 981 void
984 982 reapq_add(kthread_t *t)
985 983 {
986 984 mutex_enter(&reaplock);
987 985
988 986 /*
989 987 * lwp_deathrow contains threads with lwp linkage and
990 988 * swappable thread stacks which have the default stacksize.
991 989 * These threads' lwps and stacks may be reused by lwp_create().
992 990 *
993 991 * Anything else goes on thread_deathrow(), where it will eventually
994 992 * be thread_free()d.
995 993 */
996 994 if (t->t_flag & T_LWPREUSE) {
997 995 ASSERT(ttolwp(t) != NULL);
998 996 t->t_forw = lwp_deathrow;
999 997 lwp_deathrow = t;
1000 998 lwp_reapcnt++;
1001 999 } else {
1002 1000 t->t_forw = thread_deathrow;
1003 1001 thread_deathrow = t;
1004 1002 thread_reapcnt++;
1005 1003 }
1006 1004 if (lwp_reapcnt + thread_reapcnt > reaplimit)
1007 1005 cv_signal(&reaper_cv); /* wake the reaper */
1008 1006 t->t_state = TS_FREE;
1009 1007 lock_clear(&t->t_lock);
1010 1008
1011 1009 /*
1012 1010 * Before we return, we need to grab and drop the thread lock for
1013 1011 * the dead thread. At this point, the current thread is the idle
1014 1012 * thread, and the dead thread's CPU lock points to the current
1015 1013 * CPU -- and we must grab and drop the lock to synchronize with
1016 1014 * a racing thread walking a blocking chain that the zombie thread
1017 1015 * was recently in. By this point, that blocking chain is (by
1018 1016 * definition) stale: the dead thread is not holding any locks, and
1019 1017 * is therefore not in any blocking chains -- but if we do not regrab
1020 1018 * our lock before freeing the dead thread's data structures, the
1021 1019 * thread walking the (stale) blocking chain will die on memory
1022 1020 * corruption when it attempts to drop the dead thread's lock. We
1023 1021 * only need do this once because there is no way for the dead thread
1024 1022 * to ever again be on a blocking chain: once we have grabbed and
1025 1023 * dropped the thread lock, we are guaranteed that anyone that could
1026 1024 * have seen this thread in a blocking chain can no longer see it.
1027 1025 */
1028 1026 thread_lock(t);
1029 1027 thread_unlock(t);
1030 1028
1031 1029 mutex_exit(&reaplock);
1032 1030 }
1033 1031
1034 1032 /*
1035 1033 * Install thread context ops for the current thread.
1036 1034 */
1037 1035 void
1038 1036 installctx(
1039 1037 kthread_t *t,
1040 1038 void *arg,
1041 1039 void (*save)(void *),
1042 1040 void (*restore)(void *),
1043 1041 void (*fork)(void *, void *),
1044 1042 void (*lwp_create)(void *, void *),
1045 1043 void (*exit)(void *),
1046 1044 void (*free)(void *, int))
1047 1045 {
1048 1046 struct ctxop *ctx;
1049 1047
1050 1048 ctx = kmem_alloc(sizeof (struct ctxop), KM_SLEEP);
1051 1049 ctx->save_op = save;
1052 1050 ctx->restore_op = restore;
1053 1051 ctx->fork_op = fork;
1054 1052 ctx->lwp_create_op = lwp_create;
1055 1053 ctx->exit_op = exit;
1056 1054 ctx->free_op = free;
1057 1055 ctx->arg = arg;
1058 1056 ctx->next = t->t_ctx;
1059 1057 t->t_ctx = ctx;
1060 1058 }
1061 1059
1062 1060 /*
1063 1061 * Remove the thread context ops from a thread.
1064 1062 */
1065 1063 int
1066 1064 removectx(
1067 1065 kthread_t *t,
1068 1066 void *arg,
1069 1067 void (*save)(void *),
1070 1068 void (*restore)(void *),
1071 1069 void (*fork)(void *, void *),
1072 1070 void (*lwp_create)(void *, void *),
1073 1071 void (*exit)(void *),
1074 1072 void (*free)(void *, int))
1075 1073 {
1076 1074 struct ctxop *ctx, *prev_ctx;
1077 1075
1078 1076 /*
1079 1077 * The incoming kthread_t (which is the thread for which the
1080 1078 * context ops will be removed) should be one of the following:
1081 1079 *
1082 1080 * a) the current thread,
1083 1081 *
1084 1082 * b) a thread of a process that's being forked (SIDL),
1085 1083 *
1086 1084 * c) a thread that belongs to the same process as the current
1087 1085 * thread and for which the current thread is the agent thread,
1088 1086 *
1089 1087 * d) a thread that is TS_STOPPED which is indicative of it
1090 1088 * being (if curthread is not an agent) a thread being created
1091 1089 * as part of an lwp creation.
1092 1090 */
1093 1091 ASSERT(t == curthread || ttoproc(t)->p_stat == SIDL ||
1094 1092 ttoproc(t)->p_agenttp == curthread || t->t_state == TS_STOPPED);
1095 1093
1096 1094 /*
1097 1095 * Serialize modifications to t->t_ctx to prevent the agent thread
1098 1096 * and the target thread from racing with each other during lwp exit.
1099 1097 */
1100 1098 mutex_enter(&t->t_ctx_lock);
1101 1099 prev_ctx = NULL;
1102 1100 kpreempt_disable();
1103 1101 for (ctx = t->t_ctx; ctx != NULL; ctx = ctx->next) {
1104 1102 if (ctx->save_op == save && ctx->restore_op == restore &&
1105 1103 ctx->fork_op == fork && ctx->lwp_create_op == lwp_create &&
1106 1104 ctx->exit_op == exit && ctx->free_op == free &&
1107 1105 ctx->arg == arg) {
1108 1106 if (prev_ctx)
1109 1107 prev_ctx->next = ctx->next;
1110 1108 else
1111 1109 t->t_ctx = ctx->next;
1112 1110 mutex_exit(&t->t_ctx_lock);
1113 1111 if (ctx->free_op != NULL)
1114 1112 (ctx->free_op)(ctx->arg, 0);
1115 1113 kmem_free(ctx, sizeof (struct ctxop));
1116 1114 kpreempt_enable();
1117 1115 return (1);
1118 1116 }
1119 1117 prev_ctx = ctx;
1120 1118 }
1121 1119 mutex_exit(&t->t_ctx_lock);
1122 1120 kpreempt_enable();
1123 1121
1124 1122 return (0);
1125 1123 }
1126 1124
1127 1125 void
1128 1126 savectx(kthread_t *t)
1129 1127 {
1130 1128 struct ctxop *ctx;
1131 1129
1132 1130 ASSERT(t == curthread);
1133 1131 for (ctx = t->t_ctx; ctx != 0; ctx = ctx->next)
1134 1132 if (ctx->save_op != NULL)
1135 1133 (ctx->save_op)(ctx->arg);
1136 1134 }
1137 1135
1138 1136 void
1139 1137 restorectx(kthread_t *t)
1140 1138 {
1141 1139 struct ctxop *ctx;
1142 1140
1143 1141 ASSERT(t == curthread);
1144 1142 for (ctx = t->t_ctx; ctx != 0; ctx = ctx->next)
1145 1143 if (ctx->restore_op != NULL)
1146 1144 (ctx->restore_op)(ctx->arg);
1147 1145 }
1148 1146
1149 1147 void
1150 1148 forkctx(kthread_t *t, kthread_t *ct)
1151 1149 {
1152 1150 struct ctxop *ctx;
1153 1151
1154 1152 for (ctx = t->t_ctx; ctx != NULL; ctx = ctx->next)
1155 1153 if (ctx->fork_op != NULL)
1156 1154 (ctx->fork_op)(t, ct);
1157 1155 }
1158 1156
1159 1157 /*
1160 1158 * Note that this operator is only invoked via the _lwp_create
1161 1159 * system call. The system may have other reasons to create lwps
1162 1160 * e.g. the agent lwp or the doors unreferenced lwp.
1163 1161 */
1164 1162 void
1165 1163 lwp_createctx(kthread_t *t, kthread_t *ct)
1166 1164 {
1167 1165 struct ctxop *ctx;
1168 1166
1169 1167 for (ctx = t->t_ctx; ctx != NULL; ctx = ctx->next)
1170 1168 if (ctx->lwp_create_op != NULL)
1171 1169 (ctx->lwp_create_op)(t, ct);
1172 1170 }
1173 1171
1174 1172 /*
1175 1173 * exitctx is called from thread_exit() and lwp_exit() to perform any actions
1176 1174 * needed when the thread/LWP leaves the processor for the last time. This
1177 1175 * routine is not intended to deal with freeing memory; freectx() is used for
1178 1176 * that purpose during thread_free(). This routine is provided to allow for
1179 1177 * clean-up that can't wait until thread_free().
1180 1178 */
1181 1179 void
1182 1180 exitctx(kthread_t *t)
1183 1181 {
1184 1182 struct ctxop *ctx;
1185 1183
1186 1184 for (ctx = t->t_ctx; ctx != NULL; ctx = ctx->next)
1187 1185 if (ctx->exit_op != NULL)
1188 1186 (ctx->exit_op)(t);
1189 1187 }
1190 1188
1191 1189 /*
1192 1190 * freectx is called from thread_free() and exec() to get
1193 1191 * rid of old thread context ops.
1194 1192 */
1195 1193 void
1196 1194 freectx(kthread_t *t, int isexec)
1197 1195 {
1198 1196 struct ctxop *ctx;
1199 1197
1200 1198 kpreempt_disable();
1201 1199 while ((ctx = t->t_ctx) != NULL) {
1202 1200 t->t_ctx = ctx->next;
1203 1201 if (ctx->free_op != NULL)
1204 1202 (ctx->free_op)(ctx->arg, isexec);
1205 1203 kmem_free(ctx, sizeof (struct ctxop));
1206 1204 }
1207 1205 kpreempt_enable();
1208 1206 }
1209 1207
1210 1208 /*
1211 1209 * freectx_ctx is called from lwp_create() when lwp is reused from
1212 1210 * lwp_deathrow and its thread structure is added to thread_deathrow.
1213 1211 * The thread structure to which this ctx was attached may be already
1214 1212 * freed by the thread reaper so free_op implementations shouldn't rely
1215 1213 * on thread structure to which this ctx was attached still being around.
1216 1214 */
1217 1215 void
1218 1216 freectx_ctx(struct ctxop *ctx)
1219 1217 {
1220 1218 struct ctxop *nctx;
1221 1219
1222 1220 ASSERT(ctx != NULL);
1223 1221
1224 1222 kpreempt_disable();
1225 1223 do {
1226 1224 nctx = ctx->next;
1227 1225 if (ctx->free_op != NULL)
1228 1226 (ctx->free_op)(ctx->arg, 0);
1229 1227 kmem_free(ctx, sizeof (struct ctxop));
1230 1228 } while ((ctx = nctx) != NULL);
1231 1229 kpreempt_enable();
1232 1230 }
1233 1231
1234 1232 /*
1235 1233 * Set the thread running; arrange for it to be swapped in if necessary.
1236 1234 */
1237 1235 void
1238 1236 setrun_locked(kthread_t *t)
1239 1237 {
1240 1238 ASSERT(THREAD_LOCK_HELD(t));
1241 1239 if (t->t_state == TS_SLEEP) {
1242 1240 /*
1243 1241 * Take off sleep queue.
1244 1242 */
1245 1243 SOBJ_UNSLEEP(t->t_sobj_ops, t);
1246 1244 } else if (t->t_state & (TS_RUN | TS_ONPROC)) {
1247 1245 /*
1248 1246 * Already on dispatcher queue.
1249 1247 */
1250 1248 return;
1251 1249 } else if (t->t_state == TS_WAIT) {
1252 1250 waitq_setrun(t);
1253 1251 } else if (t->t_state == TS_STOPPED) {
1254 1252 /*
1255 1253 * All of the sending of SIGCONT (TC_XSTART) and /proc
1256 1254 * (TC_PSTART) and lwp_continue() (TC_CSTART) must have
1257 1255 * requested that the thread be run.
1258 1256 * Just calling setrun() is not sufficient to set a stopped
1259 1257 * thread running. TP_TXSTART is always set if the thread
1260 1258 * is not stopped by a jobcontrol stop signal.
1261 1259 * TP_TPSTART is always set if /proc is not controlling it.
1262 1260 * TP_TCSTART is always set if lwp_suspend() didn't stop it.
1263 1261 * The thread won't be stopped unless one of these
1264 1262 * three mechanisms did it.
1265 1263 *
1266 1264 * These flags must be set before calling setrun_locked(t).
1267 1265 * They can't be passed as arguments because the streams
1268 1266 * code calls setrun() indirectly and the mechanism for
1269 1267 * doing so admits only one argument. Note that the
1270 1268 * thread must be locked in order to change t_schedflags.
1271 1269 */
1272 1270 if ((t->t_schedflag & TS_ALLSTART) != TS_ALLSTART)
1273 1271 return;
1274 1272 /*
1275 1273 * Process is no longer stopped (a thread is running).
1276 1274 */
1277 1275 t->t_whystop = 0;
1278 1276 t->t_whatstop = 0;
1279 1277 /*
1280 1278 * Strictly speaking, we do not have to clear these
1281 1279 * flags here; they are cleared on entry to stop().
1282 1280 * However, they are confusing when doing kernel
1283 1281 * debugging or when they are revealed by ps(1).
1284 1282 */
1285 1283 t->t_schedflag &= ~TS_ALLSTART;
1286 1284 THREAD_TRANSITION(t); /* drop stopped-thread lock */
1287 1285 ASSERT(t->t_lockp == &transition_lock);
1288 1286 ASSERT(t->t_wchan0 == NULL && t->t_wchan == NULL);
1289 1287 /*
1290 1288 * Let the class put the process on the dispatcher queue.
1291 1289 */
1292 1290 CL_SETRUN(t);
1293 1291 }
1294 1292 }
1295 1293
1296 1294 void
1297 1295 setrun(kthread_t *t)
1298 1296 {
1299 1297 thread_lock(t);
1300 1298 setrun_locked(t);
1301 1299 thread_unlock(t);
1302 1300 }
1303 1301
1304 1302 /*
1305 1303 * Unpin an interrupted thread.
1306 1304 * When an interrupt occurs, the interrupt is handled on the stack
1307 1305 * of an interrupt thread, taken from a pool linked to the CPU structure.
1308 1306 *
1309 1307 * When swtch() is switching away from an interrupt thread because it
1310 1308 * blocked or was preempted, this routine is called to complete the
1311 1309 * saving of the interrupted thread state, and returns the interrupted
1312 1310 * thread pointer so it may be resumed.
1313 1311 *
1314 1312 * Called by swtch() only at high spl.
1315 1313 */
1316 1314 kthread_t *
1317 1315 thread_unpin()
1318 1316 {
↓ open down ↓ |
368 lines elided |
↑ open up ↑ |
1319 1317 kthread_t *t = curthread; /* current thread */
1320 1318 kthread_t *itp; /* interrupted thread */
1321 1319 int i; /* interrupt level */
1322 1320 extern int intr_passivate();
1323 1321
1324 1322 ASSERT(t->t_intr != NULL);
1325 1323
1326 1324 itp = t->t_intr; /* interrupted thread */
1327 1325 t->t_intr = NULL; /* clear interrupt ptr */
1328 1326
1327 + ht_end_intr();
1328 +
1329 1329 /*
1330 1330 * Get state from interrupt thread for the one
1331 1331 * it interrupted.
1332 1332 */
1333 1333
1334 1334 i = intr_passivate(t, itp);
1335 1335
1336 1336 TRACE_5(TR_FAC_INTR, TR_INTR_PASSIVATE,
1337 1337 "intr_passivate:level %d curthread %p (%T) ithread %p (%T)",
1338 1338 i, t, t, itp, itp);
1339 1339
1340 1340 /*
1341 1341 * Dissociate the current thread from the interrupted thread's LWP.
1342 1342 */
1343 1343 t->t_lwp = NULL;
1344 1344
1345 1345 /*
1346 1346 * Interrupt handlers above the level that spinlocks block must
1347 1347 * not block.
1348 1348 */
1349 1349 #if DEBUG
1350 1350 if (i < 0 || i > LOCK_LEVEL)
1351 1351 cmn_err(CE_PANIC, "thread_unpin: ipl out of range %x", i);
1352 1352 #endif
1353 1353
1354 1354 /*
1355 1355 * Compute the CPU's base interrupt level based on the active
1356 1356 * interrupts.
1357 1357 */
1358 1358 ASSERT(CPU->cpu_intr_actv & (1 << i));
1359 1359 set_base_spl();
1360 1360
1361 1361 return (itp);
1362 1362 }
1363 1363
1364 1364 /*
1365 1365 * Create and initialize an interrupt thread.
1366 1366 * Returns non-zero on error.
1367 1367 * Called at spl7() or better.
1368 1368 */
1369 1369 void
1370 1370 thread_create_intr(struct cpu *cp)
1371 1371 {
1372 1372 kthread_t *tp;
1373 1373
1374 1374 tp = thread_create(NULL, 0,
1375 1375 (void (*)())thread_create_intr, NULL, 0, &p0, TS_ONPROC, 0);
1376 1376
1377 1377 /*
1378 1378 * Set the thread in the TS_FREE state. The state will change
1379 1379 * to TS_ONPROC only while the interrupt is active. Think of these
1380 1380 * as being on a private free list for the CPU. Being TS_FREE keeps
1381 1381 * inactive interrupt threads out of debugger thread lists.
1382 1382 *
1383 1383 * We cannot call thread_create with TS_FREE because of the current
1384 1384 * checks there for ONPROC. Fix this when thread_create takes flags.
1385 1385 */
1386 1386 THREAD_FREEINTR(tp, cp);
1387 1387
1388 1388 /*
1389 1389 * Nobody should ever reference the credentials of an interrupt
1390 1390 * thread so make it NULL to catch any such references.
1391 1391 */
1392 1392 tp->t_cred = NULL;
1393 1393 tp->t_flag |= T_INTR_THREAD;
1394 1394 tp->t_cpu = cp;
1395 1395 tp->t_bound_cpu = cp;
1396 1396 tp->t_disp_queue = cp->cpu_disp;
1397 1397 tp->t_affinitycnt = 1;
1398 1398 tp->t_preempt = 1;
1399 1399
1400 1400 /*
1401 1401 * Don't make a user-requested binding on this thread so that
1402 1402 * the processor can be offlined.
1403 1403 */
1404 1404 tp->t_bind_cpu = PBIND_NONE; /* no USER-requested binding */
1405 1405 tp->t_bind_pset = PS_NONE;
1406 1406
1407 1407 #if defined(__i386) || defined(__amd64)
1408 1408 tp->t_stk -= STACK_ALIGN;
1409 1409 *(tp->t_stk) = 0; /* terminate intr thread stack */
1410 1410 #endif
1411 1411
1412 1412 /*
1413 1413 * Link onto CPU's interrupt pool.
1414 1414 */
↓ open down ↓ |
76 lines elided |
↑ open up ↑ |
1415 1415 tp->t_link = cp->cpu_intr_thread;
1416 1416 cp->cpu_intr_thread = tp;
1417 1417 }
1418 1418
1419 1419 /*
1420 1420 * TSD -- THREAD SPECIFIC DATA
1421 1421 */
1422 1422 static kmutex_t tsd_mutex; /* linked list spin lock */
1423 1423 static uint_t tsd_nkeys; /* size of destructor array */
1424 1424 /* per-key destructor funcs */
1425 -static void (**tsd_destructor)(void *);
1425 +static void (**tsd_destructor)(void *);
1426 1426 /* list of tsd_thread's */
1427 1427 static struct tsd_thread *tsd_list;
1428 1428
1429 1429 /*
1430 1430 * Default destructor
1431 1431 * Needed because NULL destructor means that the key is unused
1432 1432 */
1433 1433 /* ARGSUSED */
1434 1434 void
1435 1435 tsd_defaultdestructor(void *value)
1436 1436 {}
1437 1437
1438 1438 /*
1439 1439 * Create a key (index into per thread array)
1440 1440 * Locks out tsd_create, tsd_destroy, and tsd_exit
1441 1441 * May allocate memory with lock held
1442 1442 */
1443 1443 void
1444 1444 tsd_create(uint_t *keyp, void (*destructor)(void *))
1445 1445 {
1446 1446 int i;
1447 1447 uint_t nkeys;
1448 1448
1449 1449 /*
1450 1450 * if key is allocated, do nothing
1451 1451 */
1452 1452 mutex_enter(&tsd_mutex);
1453 1453 if (*keyp) {
1454 1454 mutex_exit(&tsd_mutex);
1455 1455 return;
1456 1456 }
1457 1457 /*
1458 1458 * find an unused key
1459 1459 */
1460 1460 if (destructor == NULL)
1461 1461 destructor = tsd_defaultdestructor;
1462 1462
1463 1463 for (i = 0; i < tsd_nkeys; ++i)
1464 1464 if (tsd_destructor[i] == NULL)
1465 1465 break;
1466 1466
1467 1467 /*
1468 1468 * if no unused keys, increase the size of the destructor array
1469 1469 */
1470 1470 if (i == tsd_nkeys) {
1471 1471 if ((nkeys = (tsd_nkeys << 1)) == 0)
1472 1472 nkeys = 1;
1473 1473 tsd_destructor =
1474 1474 (void (**)(void *))tsd_realloc((void *)tsd_destructor,
1475 1475 (size_t)(tsd_nkeys * sizeof (void (*)(void *))),
1476 1476 (size_t)(nkeys * sizeof (void (*)(void *))));
1477 1477 tsd_nkeys = nkeys;
1478 1478 }
1479 1479
1480 1480 /*
1481 1481 * allocate the next available unused key
1482 1482 */
1483 1483 tsd_destructor[i] = destructor;
1484 1484 *keyp = i + 1;
1485 1485 mutex_exit(&tsd_mutex);
1486 1486 }
1487 1487
1488 1488 /*
1489 1489 * Destroy a key -- this is for unloadable modules
1490 1490 *
1491 1491 * Assumes that the caller is preventing tsd_set and tsd_get
1492 1492 * Locks out tsd_create, tsd_destroy, and tsd_exit
1493 1493 * May free memory with lock held
1494 1494 */
1495 1495 void
1496 1496 tsd_destroy(uint_t *keyp)
1497 1497 {
1498 1498 uint_t key;
1499 1499 struct tsd_thread *tsd;
1500 1500
1501 1501 /*
1502 1502 * protect the key namespace and our destructor lists
1503 1503 */
1504 1504 mutex_enter(&tsd_mutex);
1505 1505 key = *keyp;
1506 1506 *keyp = 0;
1507 1507
1508 1508 ASSERT(key <= tsd_nkeys);
1509 1509
1510 1510 /*
1511 1511 * if the key is valid
1512 1512 */
1513 1513 if (key != 0) {
1514 1514 uint_t k = key - 1;
1515 1515 /*
1516 1516 * for every thread with TSD, call key's destructor
1517 1517 */
1518 1518 for (tsd = tsd_list; tsd; tsd = tsd->ts_next) {
1519 1519 /*
1520 1520 * no TSD for key in this thread
1521 1521 */
1522 1522 if (key > tsd->ts_nkeys)
1523 1523 continue;
1524 1524 /*
1525 1525 * call destructor for key
1526 1526 */
1527 1527 if (tsd->ts_value[k] && tsd_destructor[k])
1528 1528 (*tsd_destructor[k])(tsd->ts_value[k]);
1529 1529 /*
1530 1530 * reset value for key
1531 1531 */
1532 1532 tsd->ts_value[k] = NULL;
1533 1533 }
1534 1534 /*
1535 1535 * actually free the key (NULL destructor == unused)
1536 1536 */
1537 1537 tsd_destructor[k] = NULL;
1538 1538 }
1539 1539
1540 1540 mutex_exit(&tsd_mutex);
1541 1541 }
1542 1542
1543 1543 /*
1544 1544 * Quickly return the per thread value that was stored with the specified key
1545 1545 * Assumes the caller is protecting key from tsd_create and tsd_destroy
1546 1546 */
1547 1547 void *
1548 1548 tsd_get(uint_t key)
1549 1549 {
1550 1550 return (tsd_agent_get(curthread, key));
1551 1551 }
1552 1552
1553 1553 /*
1554 1554 * Set a per thread value indexed with the specified key
1555 1555 */
1556 1556 int
1557 1557 tsd_set(uint_t key, void *value)
1558 1558 {
1559 1559 return (tsd_agent_set(curthread, key, value));
1560 1560 }
1561 1561
1562 1562 /*
1563 1563 * Like tsd_get(), except that the agent lwp can get the tsd of
1564 1564 * another thread in the same process (the agent thread only runs when the
1565 1565 * process is completely stopped by /proc), or syslwp is creating a new lwp.
1566 1566 */
1567 1567 void *
1568 1568 tsd_agent_get(kthread_t *t, uint_t key)
1569 1569 {
1570 1570 struct tsd_thread *tsd = t->t_tsd;
1571 1571
1572 1572 ASSERT(t == curthread ||
1573 1573 ttoproc(t)->p_agenttp == curthread || t->t_state == TS_STOPPED);
1574 1574
1575 1575 if (key && tsd != NULL && key <= tsd->ts_nkeys)
1576 1576 return (tsd->ts_value[key - 1]);
1577 1577 return (NULL);
1578 1578 }
1579 1579
1580 1580 /*
1581 1581 * Like tsd_set(), except that the agent lwp can set the tsd of
1582 1582 * another thread in the same process, or syslwp can set the tsd
1583 1583 * of a thread it's in the middle of creating.
1584 1584 *
1585 1585 * Assumes the caller is protecting key from tsd_create and tsd_destroy
1586 1586 * May lock out tsd_destroy (and tsd_create), may allocate memory with
1587 1587 * lock held
1588 1588 */
1589 1589 int
1590 1590 tsd_agent_set(kthread_t *t, uint_t key, void *value)
1591 1591 {
1592 1592 struct tsd_thread *tsd = t->t_tsd;
1593 1593
1594 1594 ASSERT(t == curthread ||
1595 1595 ttoproc(t)->p_agenttp == curthread || t->t_state == TS_STOPPED);
1596 1596
1597 1597 if (key == 0)
1598 1598 return (EINVAL);
1599 1599 if (tsd == NULL)
1600 1600 tsd = t->t_tsd = kmem_zalloc(sizeof (*tsd), KM_SLEEP);
1601 1601 if (key <= tsd->ts_nkeys) {
1602 1602 tsd->ts_value[key - 1] = value;
1603 1603 return (0);
1604 1604 }
1605 1605
1606 1606 ASSERT(key <= tsd_nkeys);
1607 1607
1608 1608 /*
1609 1609 * lock out tsd_destroy()
1610 1610 */
1611 1611 mutex_enter(&tsd_mutex);
1612 1612 if (tsd->ts_nkeys == 0) {
1613 1613 /*
1614 1614 * Link onto list of threads with TSD
1615 1615 */
1616 1616 if ((tsd->ts_next = tsd_list) != NULL)
1617 1617 tsd_list->ts_prev = tsd;
1618 1618 tsd_list = tsd;
1619 1619 }
1620 1620
1621 1621 /*
1622 1622 * Allocate thread local storage and set the value for key
1623 1623 */
1624 1624 tsd->ts_value = tsd_realloc(tsd->ts_value,
1625 1625 tsd->ts_nkeys * sizeof (void *),
1626 1626 key * sizeof (void *));
1627 1627 tsd->ts_nkeys = key;
1628 1628 tsd->ts_value[key - 1] = value;
1629 1629 mutex_exit(&tsd_mutex);
1630 1630
1631 1631 return (0);
1632 1632 }
1633 1633
1634 1634
1635 1635 /*
1636 1636 * Return the per thread value that was stored with the specified key
1637 1637 * If necessary, create the key and the value
1638 1638 * Assumes the caller is protecting *keyp from tsd_destroy
1639 1639 */
1640 1640 void *
1641 1641 tsd_getcreate(uint_t *keyp, void (*destroy)(void *), void *(*allocate)(void))
1642 1642 {
1643 1643 void *value;
1644 1644 uint_t key = *keyp;
1645 1645 struct tsd_thread *tsd = curthread->t_tsd;
1646 1646
1647 1647 if (tsd == NULL)
1648 1648 tsd = curthread->t_tsd = kmem_zalloc(sizeof (*tsd), KM_SLEEP);
1649 1649 if (key && key <= tsd->ts_nkeys && (value = tsd->ts_value[key - 1]))
1650 1650 return (value);
1651 1651 if (key == 0)
1652 1652 tsd_create(keyp, destroy);
1653 1653 (void) tsd_set(*keyp, value = (*allocate)());
1654 1654
1655 1655 return (value);
1656 1656 }
1657 1657
1658 1658 /*
1659 1659 * Called from thread_exit() to run the destructor function for each tsd
1660 1660 * Locks out tsd_create and tsd_destroy
1661 1661 * Assumes that the destructor *DOES NOT* use tsd
1662 1662 */
1663 1663 void
1664 1664 tsd_exit(void)
1665 1665 {
1666 1666 int i;
1667 1667 struct tsd_thread *tsd = curthread->t_tsd;
1668 1668
1669 1669 if (tsd == NULL)
1670 1670 return;
1671 1671
1672 1672 if (tsd->ts_nkeys == 0) {
1673 1673 kmem_free(tsd, sizeof (*tsd));
1674 1674 curthread->t_tsd = NULL;
1675 1675 return;
1676 1676 }
1677 1677
1678 1678 /*
1679 1679 * lock out tsd_create and tsd_destroy, call
1680 1680 * the destructor, and mark the value as destroyed.
1681 1681 */
1682 1682 mutex_enter(&tsd_mutex);
1683 1683
1684 1684 for (i = 0; i < tsd->ts_nkeys; i++) {
1685 1685 if (tsd->ts_value[i] && tsd_destructor[i])
1686 1686 (*tsd_destructor[i])(tsd->ts_value[i]);
1687 1687 tsd->ts_value[i] = NULL;
1688 1688 }
1689 1689
1690 1690 /*
1691 1691 * remove from linked list of threads with TSD
1692 1692 */
1693 1693 if (tsd->ts_next)
1694 1694 tsd->ts_next->ts_prev = tsd->ts_prev;
1695 1695 if (tsd->ts_prev)
1696 1696 tsd->ts_prev->ts_next = tsd->ts_next;
1697 1697 if (tsd_list == tsd)
1698 1698 tsd_list = tsd->ts_next;
1699 1699
1700 1700 mutex_exit(&tsd_mutex);
1701 1701
1702 1702 /*
1703 1703 * free up the TSD
1704 1704 */
1705 1705 kmem_free(tsd->ts_value, tsd->ts_nkeys * sizeof (void *));
1706 1706 kmem_free(tsd, sizeof (struct tsd_thread));
1707 1707 curthread->t_tsd = NULL;
1708 1708 }
1709 1709
1710 1710 /*
1711 1711 * realloc
1712 1712 */
1713 1713 static void *
1714 1714 tsd_realloc(void *old, size_t osize, size_t nsize)
1715 1715 {
1716 1716 void *new;
1717 1717
1718 1718 new = kmem_zalloc(nsize, KM_SLEEP);
1719 1719 if (old) {
1720 1720 bcopy(old, new, osize);
1721 1721 kmem_free(old, osize);
1722 1722 }
1723 1723 return (new);
1724 1724 }
1725 1725
1726 1726 /*
1727 1727 * Return non-zero if an interrupt is being serviced.
1728 1728 */
1729 1729 int
1730 1730 servicing_interrupt()
1731 1731 {
1732 1732 int onintr = 0;
1733 1733
1734 1734 /* Are we an interrupt thread */
1735 1735 if (curthread->t_flag & T_INTR_THREAD)
1736 1736 return (1);
1737 1737 /* Are we servicing a high level interrupt? */
1738 1738 if (CPU_ON_INTR(CPU)) {
1739 1739 kpreempt_disable();
1740 1740 onintr = CPU_ON_INTR(CPU);
1741 1741 kpreempt_enable();
1742 1742 }
1743 1743 return (onintr);
1744 1744 }
1745 1745
1746 1746
1747 1747 /*
1748 1748 * Change the dispatch priority of a thread in the system.
1749 1749 * Used when raising or lowering a thread's priority.
1750 1750 * (E.g., priority inheritance)
1751 1751 *
1752 1752 * Since threads are queued according to their priority, we
1753 1753 * we must check the thread's state to determine whether it
1754 1754 * is on a queue somewhere. If it is, we've got to:
1755 1755 *
1756 1756 * o Dequeue the thread.
1757 1757 * o Change its effective priority.
1758 1758 * o Enqueue the thread.
1759 1759 *
1760 1760 * Assumptions: The thread whose priority we wish to change
1761 1761 * must be locked before we call thread_change_(e)pri().
1762 1762 * The thread_change(e)pri() function doesn't drop the thread
1763 1763 * lock--that must be done by its caller.
1764 1764 */
1765 1765 void
1766 1766 thread_change_epri(kthread_t *t, pri_t disp_pri)
1767 1767 {
1768 1768 uint_t state;
1769 1769
1770 1770 ASSERT(THREAD_LOCK_HELD(t));
1771 1771
1772 1772 /*
1773 1773 * If the inherited priority hasn't actually changed,
1774 1774 * just return.
1775 1775 */
1776 1776 if (t->t_epri == disp_pri)
1777 1777 return;
1778 1778
1779 1779 state = t->t_state;
1780 1780
1781 1781 /*
1782 1782 * If it's not on a queue, change the priority with impunity.
1783 1783 */
1784 1784 if ((state & (TS_SLEEP | TS_RUN | TS_WAIT)) == 0) {
1785 1785 t->t_epri = disp_pri;
1786 1786 if (state == TS_ONPROC) {
1787 1787 cpu_t *cp = t->t_disp_queue->disp_cpu;
1788 1788
1789 1789 if (t == cp->cpu_dispthread)
1790 1790 cp->cpu_dispatch_pri = DISP_PRIO(t);
1791 1791 }
1792 1792 } else if (state == TS_SLEEP) {
1793 1793 /*
1794 1794 * Take the thread out of its sleep queue.
1795 1795 * Change the inherited priority.
1796 1796 * Re-enqueue the thread.
1797 1797 * Each synchronization object exports a function
1798 1798 * to do this in an appropriate manner.
1799 1799 */
1800 1800 SOBJ_CHANGE_EPRI(t->t_sobj_ops, t, disp_pri);
1801 1801 } else if (state == TS_WAIT) {
1802 1802 /*
1803 1803 * Re-enqueue a thread on the wait queue if its
1804 1804 * effective priority needs to change.
1805 1805 */
1806 1806 if (disp_pri != t->t_epri)
1807 1807 waitq_change_pri(t, disp_pri);
1808 1808 } else {
1809 1809 /*
1810 1810 * The thread is on a run queue.
1811 1811 * Note: setbackdq() may not put the thread
1812 1812 * back on the same run queue where it originally
1813 1813 * resided.
1814 1814 */
1815 1815 (void) dispdeq(t);
1816 1816 t->t_epri = disp_pri;
1817 1817 setbackdq(t);
1818 1818 }
1819 1819 schedctl_set_cidpri(t);
1820 1820 }
1821 1821
1822 1822 /*
1823 1823 * Function: Change the t_pri field of a thread.
1824 1824 * Side Effects: Adjust the thread ordering on a run queue
1825 1825 * or sleep queue, if necessary.
1826 1826 * Returns: 1 if the thread was on a run queue, else 0.
1827 1827 */
1828 1828 int
1829 1829 thread_change_pri(kthread_t *t, pri_t disp_pri, int front)
1830 1830 {
1831 1831 uint_t state;
1832 1832 int on_rq = 0;
1833 1833
1834 1834 ASSERT(THREAD_LOCK_HELD(t));
1835 1835
1836 1836 state = t->t_state;
1837 1837 THREAD_WILLCHANGE_PRI(t, disp_pri);
1838 1838
1839 1839 /*
1840 1840 * If it's not on a queue, change the priority with impunity.
1841 1841 */
1842 1842 if ((state & (TS_SLEEP | TS_RUN | TS_WAIT)) == 0) {
1843 1843 t->t_pri = disp_pri;
1844 1844
1845 1845 if (state == TS_ONPROC) {
1846 1846 cpu_t *cp = t->t_disp_queue->disp_cpu;
1847 1847
1848 1848 if (t == cp->cpu_dispthread)
1849 1849 cp->cpu_dispatch_pri = DISP_PRIO(t);
1850 1850 }
1851 1851 } else if (state == TS_SLEEP) {
1852 1852 /*
1853 1853 * If the priority has changed, take the thread out of
1854 1854 * its sleep queue and change the priority.
1855 1855 * Re-enqueue the thread.
1856 1856 * Each synchronization object exports a function
1857 1857 * to do this in an appropriate manner.
1858 1858 */
1859 1859 if (disp_pri != t->t_pri)
1860 1860 SOBJ_CHANGE_PRI(t->t_sobj_ops, t, disp_pri);
1861 1861 } else if (state == TS_WAIT) {
1862 1862 /*
1863 1863 * Re-enqueue a thread on the wait queue if its
1864 1864 * priority needs to change.
1865 1865 */
1866 1866 if (disp_pri != t->t_pri)
1867 1867 waitq_change_pri(t, disp_pri);
1868 1868 } else {
1869 1869 /*
1870 1870 * The thread is on a run queue.
1871 1871 * Note: setbackdq() may not put the thread
1872 1872 * back on the same run queue where it originally
1873 1873 * resided.
1874 1874 *
1875 1875 * We still requeue the thread even if the priority
1876 1876 * is unchanged to preserve round-robin (and other)
1877 1877 * effects between threads of the same priority.
1878 1878 */
1879 1879 on_rq = dispdeq(t);
1880 1880 ASSERT(on_rq);
1881 1881 t->t_pri = disp_pri;
1882 1882 if (front) {
1883 1883 setfrontdq(t);
1884 1884 } else {
1885 1885 setbackdq(t);
1886 1886 }
1887 1887 }
1888 1888 schedctl_set_cidpri(t);
1889 1889 return (on_rq);
1890 1890 }
1891 1891
1892 1892 /*
1893 1893 * Tunable kmem_stackinfo is set, fill the kernel thread stack with a
1894 1894 * specific pattern.
1895 1895 */
1896 1896 static void
1897 1897 stkinfo_begin(kthread_t *t)
1898 1898 {
1899 1899 caddr_t start; /* stack start */
1900 1900 caddr_t end; /* stack end */
1901 1901 uint64_t *ptr; /* pattern pointer */
1902 1902
1903 1903 /*
1904 1904 * Stack grows up or down, see thread_create(),
1905 1905 * compute stack memory area start and end (start < end).
1906 1906 */
1907 1907 if (t->t_stk > t->t_stkbase) {
1908 1908 /* stack grows down */
1909 1909 start = t->t_stkbase;
1910 1910 end = t->t_stk;
1911 1911 } else {
1912 1912 /* stack grows up */
1913 1913 start = t->t_stk;
1914 1914 end = t->t_stkbase;
1915 1915 }
1916 1916
1917 1917 /*
1918 1918 * Stackinfo pattern size is 8 bytes. Ensure proper 8 bytes
1919 1919 * alignement for start and end in stack area boundaries
1920 1920 * (protection against corrupt t_stkbase/t_stk data).
1921 1921 */
1922 1922 if ((((uintptr_t)start) & 0x7) != 0) {
1923 1923 start = (caddr_t)((((uintptr_t)start) & (~0x7)) + 8);
1924 1924 }
1925 1925 end = (caddr_t)(((uintptr_t)end) & (~0x7));
1926 1926
1927 1927 if ((end <= start) || (end - start) > (1024 * 1024)) {
1928 1928 /* negative or stack size > 1 meg, assume bogus */
1929 1929 return;
1930 1930 }
1931 1931
1932 1932 /* fill stack area with a pattern (instead of zeros) */
1933 1933 ptr = (uint64_t *)((void *)start);
1934 1934 while (ptr < (uint64_t *)((void *)end)) {
1935 1935 *ptr++ = KMEM_STKINFO_PATTERN;
1936 1936 }
1937 1937 }
1938 1938
1939 1939
1940 1940 /*
1941 1941 * Tunable kmem_stackinfo is set, create stackinfo log if doesn't already exist,
1942 1942 * compute the percentage of kernel stack really used, and set in the log
1943 1943 * if it's the latest highest percentage.
1944 1944 */
1945 1945 static void
1946 1946 stkinfo_end(kthread_t *t)
1947 1947 {
1948 1948 caddr_t start; /* stack start */
1949 1949 caddr_t end; /* stack end */
1950 1950 uint64_t *ptr; /* pattern pointer */
1951 1951 size_t stksz; /* stack size */
1952 1952 size_t smallest = 0;
1953 1953 size_t percent = 0;
1954 1954 uint_t index = 0;
1955 1955 uint_t i;
1956 1956 static size_t smallest_percent = (size_t)-1;
1957 1957 static uint_t full = 0;
1958 1958
1959 1959 /* create the stackinfo log, if doesn't already exist */
1960 1960 mutex_enter(&kmem_stkinfo_lock);
1961 1961 if (kmem_stkinfo_log == NULL) {
1962 1962 kmem_stkinfo_log = (kmem_stkinfo_t *)
1963 1963 kmem_zalloc(KMEM_STKINFO_LOG_SIZE *
1964 1964 (sizeof (kmem_stkinfo_t)), KM_NOSLEEP);
1965 1965 if (kmem_stkinfo_log == NULL) {
1966 1966 mutex_exit(&kmem_stkinfo_lock);
1967 1967 return;
1968 1968 }
1969 1969 }
1970 1970 mutex_exit(&kmem_stkinfo_lock);
1971 1971
1972 1972 /*
1973 1973 * Stack grows up or down, see thread_create(),
1974 1974 * compute stack memory area start and end (start < end).
1975 1975 */
1976 1976 if (t->t_stk > t->t_stkbase) {
1977 1977 /* stack grows down */
1978 1978 start = t->t_stkbase;
1979 1979 end = t->t_stk;
1980 1980 } else {
1981 1981 /* stack grows up */
1982 1982 start = t->t_stk;
1983 1983 end = t->t_stkbase;
1984 1984 }
1985 1985
1986 1986 /* stack size as found in kthread_t */
1987 1987 stksz = end - start;
1988 1988
1989 1989 /*
1990 1990 * Stackinfo pattern size is 8 bytes. Ensure proper 8 bytes
1991 1991 * alignement for start and end in stack area boundaries
1992 1992 * (protection against corrupt t_stkbase/t_stk data).
1993 1993 */
1994 1994 if ((((uintptr_t)start) & 0x7) != 0) {
1995 1995 start = (caddr_t)((((uintptr_t)start) & (~0x7)) + 8);
1996 1996 }
1997 1997 end = (caddr_t)(((uintptr_t)end) & (~0x7));
1998 1998
1999 1999 if ((end <= start) || (end - start) > (1024 * 1024)) {
2000 2000 /* negative or stack size > 1 meg, assume bogus */
2001 2001 return;
2002 2002 }
2003 2003
2004 2004 /* search until no pattern in the stack */
2005 2005 if (t->t_stk > t->t_stkbase) {
2006 2006 /* stack grows down */
2007 2007 #if defined(__i386) || defined(__amd64)
2008 2008 /*
2009 2009 * 6 longs are pushed on stack, see thread_load(). Skip
2010 2010 * them, so if kthread has never run, percent is zero.
2011 2011 * 8 bytes alignement is preserved for a 32 bit kernel,
2012 2012 * 6 x 4 = 24, 24 is a multiple of 8.
2013 2013 *
2014 2014 */
2015 2015 end -= (6 * sizeof (long));
2016 2016 #endif
2017 2017 ptr = (uint64_t *)((void *)start);
2018 2018 while (ptr < (uint64_t *)((void *)end)) {
2019 2019 if (*ptr != KMEM_STKINFO_PATTERN) {
2020 2020 percent = stkinfo_percent(end,
2021 2021 start, (caddr_t)ptr);
2022 2022 break;
2023 2023 }
2024 2024 ptr++;
2025 2025 }
2026 2026 } else {
2027 2027 /* stack grows up */
2028 2028 ptr = (uint64_t *)((void *)end);
2029 2029 ptr--;
2030 2030 while (ptr >= (uint64_t *)((void *)start)) {
2031 2031 if (*ptr != KMEM_STKINFO_PATTERN) {
2032 2032 percent = stkinfo_percent(start,
2033 2033 end, (caddr_t)ptr);
2034 2034 break;
2035 2035 }
2036 2036 ptr--;
2037 2037 }
2038 2038 }
2039 2039
2040 2040 DTRACE_PROBE3(stack__usage, kthread_t *, t,
2041 2041 size_t, stksz, size_t, percent);
2042 2042
2043 2043 if (percent == 0) {
2044 2044 return;
2045 2045 }
2046 2046
2047 2047 mutex_enter(&kmem_stkinfo_lock);
2048 2048 if (full == KMEM_STKINFO_LOG_SIZE && percent < smallest_percent) {
2049 2049 /*
2050 2050 * The log is full and already contains the highest values
2051 2051 */
2052 2052 mutex_exit(&kmem_stkinfo_lock);
2053 2053 return;
2054 2054 }
2055 2055
2056 2056 /* keep a log of the highest used stack */
2057 2057 for (i = 0; i < KMEM_STKINFO_LOG_SIZE; i++) {
2058 2058 if (kmem_stkinfo_log[i].percent == 0) {
2059 2059 index = i;
2060 2060 full++;
2061 2061 break;
2062 2062 }
2063 2063 if (smallest == 0) {
2064 2064 smallest = kmem_stkinfo_log[i].percent;
2065 2065 index = i;
2066 2066 continue;
2067 2067 }
2068 2068 if (kmem_stkinfo_log[i].percent < smallest) {
2069 2069 smallest = kmem_stkinfo_log[i].percent;
2070 2070 index = i;
2071 2071 }
2072 2072 }
2073 2073
2074 2074 if (percent >= kmem_stkinfo_log[index].percent) {
2075 2075 kmem_stkinfo_log[index].kthread = (caddr_t)t;
2076 2076 kmem_stkinfo_log[index].t_startpc = (caddr_t)t->t_startpc;
2077 2077 kmem_stkinfo_log[index].start = start;
2078 2078 kmem_stkinfo_log[index].stksz = stksz;
2079 2079 kmem_stkinfo_log[index].percent = percent;
2080 2080 kmem_stkinfo_log[index].t_tid = t->t_tid;
2081 2081 kmem_stkinfo_log[index].cmd[0] = '\0';
2082 2082 if (t->t_tid != 0) {
2083 2083 stksz = strlen((t->t_procp)->p_user.u_comm);
2084 2084 if (stksz >= KMEM_STKINFO_STR_SIZE) {
2085 2085 stksz = KMEM_STKINFO_STR_SIZE - 1;
2086 2086 kmem_stkinfo_log[index].cmd[stksz] = '\0';
2087 2087 } else {
2088 2088 stksz += 1;
2089 2089 }
2090 2090 (void) memcpy(kmem_stkinfo_log[index].cmd,
2091 2091 (t->t_procp)->p_user.u_comm, stksz);
2092 2092 }
2093 2093 if (percent < smallest_percent) {
2094 2094 smallest_percent = percent;
2095 2095 }
2096 2096 }
2097 2097 mutex_exit(&kmem_stkinfo_lock);
2098 2098 }
2099 2099
2100 2100 /*
2101 2101 * Tunable kmem_stackinfo is set, compute stack utilization percentage.
2102 2102 */
2103 2103 static size_t
2104 2104 stkinfo_percent(caddr_t t_stk, caddr_t t_stkbase, caddr_t sp)
2105 2105 {
2106 2106 size_t percent;
2107 2107 size_t s;
2108 2108
2109 2109 if (t_stk > t_stkbase) {
2110 2110 /* stack grows down */
2111 2111 if (sp > t_stk) {
2112 2112 return (0);
2113 2113 }
2114 2114 if (sp < t_stkbase) {
2115 2115 return (100);
2116 2116 }
2117 2117 percent = t_stk - sp + 1;
2118 2118 s = t_stk - t_stkbase + 1;
2119 2119 } else {
2120 2120 /* stack grows up */
2121 2121 if (sp < t_stk) {
2122 2122 return (0);
2123 2123 }
2124 2124 if (sp > t_stkbase) {
2125 2125 return (100);
2126 2126 }
2127 2127 percent = sp - t_stk + 1;
2128 2128 s = t_stkbase - t_stk + 1;
2129 2129 }
2130 2130 percent = ((100 * percent) / s) + 1;
2131 2131 if (percent > 100) {
2132 2132 percent = 100;
2133 2133 }
2134 2134 return (percent);
2135 2135 }
2136 2136
2137 2137 /*
2138 2138 * NOTE: This will silently truncate a name > THREAD_NAME_MAX - 1 characters
2139 2139 * long. It is expected that callers (acting on behalf of userland clients)
2140 2140 * will perform any required checks to return the correct error semantics.
2141 2141 * It is also expected callers on behalf of userland clients have done
2142 2142 * any necessary permission checks.
2143 2143 */
2144 2144 int
2145 2145 thread_setname(kthread_t *t, const char *name)
2146 2146 {
2147 2147 char *buf = NULL;
2148 2148
2149 2149 /*
2150 2150 * We optimistically assume that a thread's name will only be set
2151 2151 * once and so allocate memory in preparation of setting t_name.
2152 2152 * If it turns out a name has already been set, we just discard (free)
2153 2153 * the buffer we just allocated and reuse the current buffer
2154 2154 * (as all should be THREAD_NAME_MAX large).
2155 2155 *
2156 2156 * Such an arrangement means over the lifetime of a kthread_t, t_name
2157 2157 * is either NULL or has one value (the address of the buffer holding
2158 2158 * the current thread name). The assumption is that most kthread_t
2159 2159 * instances will not have a name assigned, so dynamically allocating
2160 2160 * the memory should minimize the footprint of this feature, but by
2161 2161 * having the buffer persist for the life of the thread, it simplifies
2162 2162 * usage in highly constrained situations (e.g. dtrace).
2163 2163 */
2164 2164 if (name != NULL && name[0] != '\0') {
2165 2165 for (size_t i = 0; name[i] != '\0'; i++) {
2166 2166 if (!isprint(name[i]))
2167 2167 return (EINVAL);
2168 2168 }
2169 2169
2170 2170 buf = kmem_zalloc(THREAD_NAME_MAX, KM_SLEEP);
2171 2171 (void) strlcpy(buf, name, THREAD_NAME_MAX);
2172 2172 }
2173 2173
2174 2174 mutex_enter(&ttoproc(t)->p_lock);
2175 2175 if (t->t_name == NULL) {
2176 2176 t->t_name = buf;
2177 2177 } else {
2178 2178 if (buf != NULL) {
2179 2179 (void) strlcpy(t->t_name, name, THREAD_NAME_MAX);
2180 2180 kmem_free(buf, THREAD_NAME_MAX);
2181 2181 } else {
2182 2182 bzero(t->t_name, THREAD_NAME_MAX);
2183 2183 }
2184 2184 }
2185 2185 mutex_exit(&ttoproc(t)->p_lock);
2186 2186 return (0);
2187 2187 }
2188 2188
2189 2189 int
2190 2190 thread_vsetname(kthread_t *t, const char *fmt, ...)
2191 2191 {
2192 2192 char name[THREAD_NAME_MAX];
2193 2193 va_list va;
2194 2194 int rc;
2195 2195
2196 2196 va_start(va, fmt);
2197 2197 rc = vsnprintf(name, sizeof (name), fmt, va);
2198 2198 va_end(va);
2199 2199
2200 2200 if (rc < 0)
2201 2201 return (EINVAL);
2202 2202
2203 2203 if (rc >= sizeof (name))
2204 2204 return (ENAMETOOLONG);
2205 2205
2206 2206 return (thread_setname(t, name));
2207 2207 }
↓ open down ↓ |
772 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX