Print this page
OS-4818 contract template disappears on exec
OS-5311 docker init children not always in the correct contract
Reviewed by: Patrick Mooney <patrick.mooney@joyent.com>
OS-4937 lxbrand ptracer count updates can race
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Reviewed by: Joshua M. Clulow <jmc@joyent.com>
OS-4534 lwp_exit P_PR_LOCK assertion failure
OS-4188 NULL dereference in lwp_hash_in
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Reviewed by: Joshua M. Clulow <jmc@joyent.com>
OS-4151 setbrand hooks should be sane during fork
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Reviewed by: Joshua M. Clulow <jmc@joyent.com>
OS-4129 lxbrand should not abuse p_brand_data for storing exit signal
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Reviewed by: Joshua M. Clulow <jmc@joyent.com>
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/os/lwp.c
+++ new/usr/src/uts/common/os/lwp.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]
|
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 27 /*
28 - * Copyright (c) 2013, Joyent, Inc. All rights reserved.
28 + * Copyright 2016, Joyent, Inc.
29 29 */
30 30
31 31 #include <sys/param.h>
32 32 #include <sys/types.h>
33 33 #include <sys/sysmacros.h>
34 34 #include <sys/systm.h>
35 35 #include <sys/thread.h>
36 36 #include <sys/proc.h>
37 37 #include <sys/task.h>
38 38 #include <sys/project.h>
39 39 #include <sys/signal.h>
40 40 #include <sys/errno.h>
41 41 #include <sys/vmparam.h>
42 42 #include <sys/stack.h>
43 43 #include <sys/procfs.h>
44 44 #include <sys/prsystm.h>
45 45 #include <sys/cpuvar.h>
46 46 #include <sys/kmem.h>
47 47 #include <sys/vtrace.h>
48 48 #include <sys/door.h>
49 49 #include <vm/seg_kp.h>
|
↓ open down ↓ |
11 lines elided |
↑ open up ↑ |
50 50 #include <sys/debug.h>
51 51 #include <sys/tnf.h>
52 52 #include <sys/schedctl.h>
53 53 #include <sys/poll.h>
54 54 #include <sys/copyops.h>
55 55 #include <sys/lwp_upimutex_impl.h>
56 56 #include <sys/cpupart.h>
57 57 #include <sys/lgrp.h>
58 58 #include <sys/rctl.h>
59 59 #include <sys/contract_impl.h>
60 +#include <sys/contract/process.h>
61 +#include <sys/contract/process_impl.h>
60 62 #include <sys/cpc_impl.h>
61 63 #include <sys/sdt.h>
62 64 #include <sys/cmn_err.h>
63 65 #include <sys/brand.h>
64 66 #include <sys/cyclic.h>
65 67 #include <sys/pool.h>
66 68
67 69 /* hash function for the lwpid hash table, p->p_tidhash[] */
68 70 #define TIDHASH(tid, hash_sz) ((tid) & ((hash_sz) - 1))
69 71
70 72 void *segkp_lwp; /* cookie for pool of segkp resources */
71 73 extern void reapq_move_lq_to_tq(kthread_t *);
72 74 extern void freectx_ctx(struct ctxop *);
73 75
74 76 /*
75 77 * Create a kernel thread associated with a particular system process. Give
76 78 * it an LWP so that microstate accounting will be available for it.
77 79 */
78 80 kthread_t *
79 81 lwp_kernel_create(proc_t *p, void (*proc)(), void *arg, int state, pri_t pri)
80 82 {
81 83 klwp_t *lwp;
82 84
83 85 VERIFY((p->p_flag & SSYS) != 0);
84 86
85 87 lwp = lwp_create(proc, arg, 0, p, state, pri, &t0.t_hold, syscid, 0);
86 88
87 89 VERIFY(lwp != NULL);
88 90
89 91 return (lwptot(lwp));
90 92 }
91 93
92 94 /*
93 95 * Create a thread that appears to be stopped at sys_rtt.
94 96 */
95 97 klwp_t *
96 98 lwp_create(void (*proc)(), caddr_t arg, size_t len, proc_t *p,
97 99 int state, int pri, const k_sigset_t *smask, int cid, id_t lwpid)
98 100 {
99 101 klwp_t *lwp = NULL;
100 102 kthread_t *t;
101 103 kthread_t *tx;
102 104 cpupart_t *oldpart = NULL;
103 105 size_t stksize;
104 106 caddr_t lwpdata = NULL;
105 107 processorid_t binding;
106 108 int err = 0;
107 109 kproject_t *oldkpj, *newkpj;
|
↓ open down ↓ |
38 lines elided |
↑ open up ↑ |
108 110 void *bufp = NULL;
109 111 klwp_t *curlwp;
110 112 lwpent_t *lep;
111 113 lwpdir_t *old_dir = NULL;
112 114 uint_t old_dirsz = 0;
113 115 tidhash_t *old_hash = NULL;
114 116 uint_t old_hashsz = 0;
115 117 ret_tidhash_t *ret_tidhash = NULL;
116 118 int i;
117 119 int rctlfail = 0;
118 - boolean_t branded = 0;
120 + void *brand_data = NULL;
119 121 struct ctxop *ctx = NULL;
120 122
121 123 ASSERT(cid != sysdccid); /* system threads must start in SYS */
122 124
123 125 ASSERT(p != &p0); /* No new LWPs in p0. */
124 126
125 127 mutex_enter(&p->p_lock);
126 128 mutex_enter(&p->p_zone->zone_nlwps_lock);
127 129 /*
128 130 * don't enforce rctl limits on system processes
129 131 */
130 132 if (!CLASS_KERNEL(cid)) {
131 133 if (p->p_task->tk_nlwps >= p->p_task->tk_nlwps_ctl)
132 134 if (rctl_test(rc_task_lwps, p->p_task->tk_rctls, p,
133 135 1, 0) & RCT_DENY)
134 136 rctlfail = 1;
135 137 if (p->p_task->tk_proj->kpj_nlwps >=
136 138 p->p_task->tk_proj->kpj_nlwps_ctl)
137 139 if (rctl_test(rc_project_nlwps,
138 140 p->p_task->tk_proj->kpj_rctls, p, 1, 0)
139 141 & RCT_DENY)
140 142 rctlfail = 1;
141 143 if (p->p_zone->zone_nlwps >= p->p_zone->zone_nlwps_ctl)
142 144 if (rctl_test(rc_zone_nlwps, p->p_zone->zone_rctls, p,
143 145 1, 0) & RCT_DENY)
144 146 rctlfail = 1;
145 147 }
146 148 if (rctlfail) {
147 149 mutex_exit(&p->p_zone->zone_nlwps_lock);
148 150 mutex_exit(&p->p_lock);
149 151 atomic_inc_32(&p->p_zone->zone_ffcap);
150 152 return (NULL);
151 153 }
152 154 p->p_task->tk_nlwps++;
153 155 p->p_task->tk_proj->kpj_nlwps++;
154 156 p->p_zone->zone_nlwps++;
155 157 mutex_exit(&p->p_zone->zone_nlwps_lock);
156 158 mutex_exit(&p->p_lock);
157 159
158 160 curlwp = ttolwp(curthread);
159 161 if (curlwp == NULL || (stksize = curlwp->lwp_childstksz) == 0)
160 162 stksize = lwp_default_stksize;
161 163
162 164 if (CLASS_KERNEL(cid)) {
163 165 /*
164 166 * Since we are creating an LWP in an SSYS process, we do not
165 167 * inherit anything from the current thread's LWP. We set
166 168 * stksize and lwpdata to 0 in order to let thread_create()
167 169 * allocate a regular kernel thread stack for this thread.
168 170 */
169 171 curlwp = NULL;
170 172 stksize = 0;
171 173 lwpdata = NULL;
172 174
173 175 } else if (stksize == lwp_default_stksize) {
174 176 /*
175 177 * Try to reuse an <lwp,stack> from the LWP deathrow.
176 178 */
177 179 if (lwp_reapcnt > 0) {
178 180 mutex_enter(&reaplock);
179 181 if ((t = lwp_deathrow) != NULL) {
180 182 ASSERT(t->t_swap);
181 183 lwp_deathrow = t->t_forw;
182 184 lwp_reapcnt--;
183 185 lwpdata = t->t_swap;
184 186 lwp = t->t_lwp;
185 187 ctx = t->t_ctx;
186 188 t->t_swap = NULL;
187 189 t->t_lwp = NULL;
188 190 t->t_ctx = NULL;
189 191 reapq_move_lq_to_tq(t);
190 192 }
191 193 mutex_exit(&reaplock);
192 194 if (lwp != NULL) {
193 195 lwp_stk_fini(lwp);
194 196 }
195 197 if (ctx != NULL) {
196 198 freectx_ctx(ctx);
197 199 }
198 200 }
199 201 if (lwpdata == NULL &&
200 202 (lwpdata = (caddr_t)segkp_cache_get(segkp_lwp)) == NULL) {
201 203 mutex_enter(&p->p_lock);
202 204 mutex_enter(&p->p_zone->zone_nlwps_lock);
203 205 p->p_task->tk_nlwps--;
204 206 p->p_task->tk_proj->kpj_nlwps--;
205 207 p->p_zone->zone_nlwps--;
206 208 mutex_exit(&p->p_zone->zone_nlwps_lock);
207 209 mutex_exit(&p->p_lock);
208 210 atomic_inc_32(&p->p_zone->zone_ffnomem);
209 211 return (NULL);
210 212 }
211 213 } else {
212 214 stksize = roundup(stksize, PAGESIZE);
213 215 if ((lwpdata = (caddr_t)segkp_get(segkp, stksize,
214 216 (KPD_NOWAIT | KPD_HASREDZONE | KPD_LOCKED))) == NULL) {
215 217 mutex_enter(&p->p_lock);
216 218 mutex_enter(&p->p_zone->zone_nlwps_lock);
217 219 p->p_task->tk_nlwps--;
218 220 p->p_task->tk_proj->kpj_nlwps--;
219 221 p->p_zone->zone_nlwps--;
220 222 mutex_exit(&p->p_zone->zone_nlwps_lock);
221 223 mutex_exit(&p->p_lock);
222 224 atomic_inc_32(&p->p_zone->zone_ffnomem);
223 225 return (NULL);
224 226 }
225 227 }
226 228
227 229 /*
228 230 * Create a thread, initializing the stack pointer
229 231 */
230 232 t = thread_create(lwpdata, stksize, NULL, NULL, 0, p, TS_STOPPED, pri);
231 233
232 234 /*
233 235 * If a non-NULL stack base is passed in, thread_create() assumes
234 236 * that the stack might be statically allocated (as opposed to being
235 237 * allocated from segkp), and so it does not set t_swap. Since
236 238 * the lwpdata was allocated from segkp, we must set t_swap to point
237 239 * to it ourselves.
238 240 *
239 241 * This would be less confusing if t_swap had a better name; it really
240 242 * indicates that the stack is allocated from segkp, regardless of
241 243 * whether or not it is swappable.
242 244 */
243 245 if (lwpdata != NULL) {
244 246 ASSERT(!CLASS_KERNEL(cid));
245 247 ASSERT(t->t_swap == NULL);
246 248 t->t_swap = lwpdata; /* Start of page-able data */
247 249 }
248 250
249 251 /*
250 252 * If the stack and lwp can be reused, mark the thread as such.
251 253 * When we get to reapq_add() from resume_from_zombie(), these
252 254 * threads will go onto lwp_deathrow instead of thread_deathrow.
253 255 */
254 256 if (!CLASS_KERNEL(cid) && stksize == lwp_default_stksize)
255 257 t->t_flag |= T_LWPREUSE;
256 258
257 259 if (lwp == NULL)
258 260 lwp = kmem_cache_alloc(lwp_cache, KM_SLEEP);
259 261 bzero(lwp, sizeof (*lwp));
260 262 t->t_lwp = lwp;
261 263
262 264 t->t_hold = *smask;
263 265 lwp->lwp_thread = t;
264 266 lwp->lwp_procp = p;
265 267 lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
266 268 if (curlwp != NULL && curlwp->lwp_childstksz != 0)
267 269 lwp->lwp_childstksz = curlwp->lwp_childstksz;
268 270
269 271 t->t_stk = lwp_stk_init(lwp, t->t_stk);
270 272 thread_load(t, proc, arg, len);
271 273
272 274 /*
273 275 * Allocate the SIGPROF buffer if ITIMER_REALPROF is in effect.
274 276 */
275 277 if (p->p_rprof_cyclic != CYCLIC_NONE)
|
↓ open down ↓ |
147 lines elided |
↑ open up ↑ |
276 278 t->t_rprof = kmem_zalloc(sizeof (struct rprof), KM_SLEEP);
277 279
278 280 if (cid != NOCLASS)
279 281 (void) CL_ALLOC(&bufp, cid, KM_SLEEP);
280 282
281 283 /*
282 284 * Allocate an lwp directory entry for the new lwp.
283 285 */
284 286 lep = kmem_zalloc(sizeof (*lep), KM_SLEEP);
285 287
288 + /*
289 + * If necessary, speculatively allocate lwp brand data. This is done
290 + * ahead of time so p_lock need not be dropped during lwp branding.
291 + */
292 + if (PROC_IS_BRANDED(p) && BROP(p)->b_lwpdata_alloc != NULL) {
293 + if ((brand_data = BROP(p)->b_lwpdata_alloc(p)) == NULL) {
294 + mutex_enter(&p->p_lock);
295 + err = 1;
296 + atomic_inc_32(&p->p_zone->zone_ffmisc);
297 + goto error;
298 + }
299 + }
300 +
286 301 mutex_enter(&p->p_lock);
287 302 grow:
288 303 /*
289 304 * Grow the lwp (thread) directory and lwpid hash table if necessary.
290 305 * A note on the growth algorithm:
291 306 * The new lwp directory size is computed as:
292 307 * new = 2 * old + 2
293 308 * Starting with an initial size of 2 (see exec_common()),
294 309 * this yields numbers that are a power of two minus 2:
295 310 * 2, 6, 14, 30, 62, 126, 254, 510, 1022, ...
296 311 * The size of the lwpid hash table must be a power of two
297 312 * and must be commensurate in size with the lwp directory
298 313 * so that hash bucket chains remain short. Therefore,
299 314 * the lwpid hash table size is computed as:
300 315 * hashsz = (dirsz + 2) / 2
301 316 * which leads to these hash table sizes corresponding to
302 317 * the above directory sizes:
303 318 * 2, 4, 8, 16, 32, 64, 128, 256, 512, ...
304 319 * A note on growing the hash table:
305 320 * For performance reasons, code in lwp_unpark() does not
306 321 * acquire curproc->p_lock when searching the hash table.
307 322 * Rather, it calls lwp_hash_lookup_and_lock() which
308 323 * acquires only the individual hash bucket lock, taking
309 324 * care to deal with reallocation of the hash table
310 325 * during the time it takes to acquire the lock.
311 326 *
312 327 * This is sufficient to protect the integrity of the
313 328 * hash table, but it requires us to acquire all of the
314 329 * old hash bucket locks before growing the hash table
315 330 * and to release them afterwards. It also requires us
316 331 * not to free the old hash table because some thread
317 332 * in lwp_hash_lookup_and_lock() might still be trying
318 333 * to acquire the old bucket lock.
319 334 *
320 335 * So we adopt the tactic of keeping all of the retired
321 336 * hash tables on a linked list, so they can be safely
322 337 * freed when the process exits or execs.
323 338 *
324 339 * Because the hash table grows in powers of two, the
325 340 * total size of all of the hash tables will be slightly
326 341 * less than twice the size of the largest hash table.
327 342 */
328 343 while (p->p_lwpfree == NULL) {
329 344 uint_t dirsz = p->p_lwpdir_sz;
330 345 lwpdir_t *new_dir;
331 346 uint_t new_dirsz;
332 347 lwpdir_t *ldp;
333 348 tidhash_t *new_hash;
334 349 uint_t new_hashsz;
335 350
336 351 mutex_exit(&p->p_lock);
337 352
338 353 /*
339 354 * Prepare to remember the old p_tidhash for later
340 355 * kmem_free()ing when the process exits or execs.
341 356 */
342 357 if (ret_tidhash == NULL)
343 358 ret_tidhash = kmem_zalloc(sizeof (ret_tidhash_t),
344 359 KM_SLEEP);
345 360 if (old_dir != NULL)
346 361 kmem_free(old_dir, old_dirsz * sizeof (*old_dir));
347 362 if (old_hash != NULL)
348 363 kmem_free(old_hash, old_hashsz * sizeof (*old_hash));
349 364
350 365 new_dirsz = 2 * dirsz + 2;
351 366 new_dir = kmem_zalloc(new_dirsz * sizeof (lwpdir_t), KM_SLEEP);
352 367 for (ldp = new_dir, i = 1; i < new_dirsz; i++, ldp++)
353 368 ldp->ld_next = ldp + 1;
354 369 new_hashsz = (new_dirsz + 2) / 2;
355 370 new_hash = kmem_zalloc(new_hashsz * sizeof (tidhash_t),
356 371 KM_SLEEP);
357 372
358 373 mutex_enter(&p->p_lock);
359 374 if (p == curproc)
360 375 prbarrier(p);
361 376
362 377 if (dirsz != p->p_lwpdir_sz || p->p_lwpfree != NULL) {
363 378 /*
364 379 * Someone else beat us to it or some lwp exited.
365 380 * Set up to free our memory and take a lap.
366 381 */
367 382 old_dir = new_dir;
368 383 old_dirsz = new_dirsz;
369 384 old_hash = new_hash;
370 385 old_hashsz = new_hashsz;
371 386 } else {
372 387 /*
373 388 * For the benefit of lwp_hash_lookup_and_lock(),
374 389 * called from lwp_unpark(), which searches the
375 390 * tid hash table without acquiring p->p_lock,
376 391 * we must acquire all of the tid hash table
377 392 * locks before replacing p->p_tidhash.
378 393 */
379 394 old_hash = p->p_tidhash;
380 395 old_hashsz = p->p_tidhash_sz;
381 396 for (i = 0; i < old_hashsz; i++) {
382 397 mutex_enter(&old_hash[i].th_lock);
383 398 mutex_enter(&new_hash[i].th_lock);
384 399 }
385 400
386 401 /*
387 402 * We simply hash in all of the old directory entries.
388 403 * This works because the old directory has no empty
389 404 * slots and the new hash table starts out empty.
390 405 * This reproduces the original directory ordering
391 406 * (required for /proc directory semantics).
392 407 */
393 408 old_dir = p->p_lwpdir;
394 409 old_dirsz = p->p_lwpdir_sz;
395 410 p->p_lwpdir = new_dir;
396 411 p->p_lwpfree = new_dir;
397 412 p->p_lwpdir_sz = new_dirsz;
398 413 for (ldp = old_dir, i = 0; i < old_dirsz; i++, ldp++)
399 414 lwp_hash_in(p, ldp->ld_entry,
400 415 new_hash, new_hashsz, 0);
401 416
402 417 /*
403 418 * Remember the old hash table along with all
404 419 * of the previously-remembered hash tables.
405 420 * We will free them at process exit or exec.
406 421 */
407 422 ret_tidhash->rth_tidhash = old_hash;
408 423 ret_tidhash->rth_tidhash_sz = old_hashsz;
409 424 ret_tidhash->rth_next = p->p_ret_tidhash;
410 425 p->p_ret_tidhash = ret_tidhash;
411 426
412 427 /*
413 428 * Now establish the new tid hash table.
414 429 * As soon as we assign p->p_tidhash,
415 430 * code in lwp_unpark() can start using it.
416 431 */
417 432 membar_producer();
418 433 p->p_tidhash = new_hash;
419 434
420 435 /*
421 436 * It is necessary that p_tidhash reach global
422 437 * visibility before p_tidhash_sz. Otherwise,
423 438 * code in lwp_hash_lookup_and_lock() could
424 439 * index into the old p_tidhash using the new
425 440 * p_tidhash_sz and thereby access invalid data.
426 441 */
427 442 membar_producer();
428 443 p->p_tidhash_sz = new_hashsz;
429 444
430 445 /*
431 446 * Release the locks; allow lwp_unpark() to carry on.
432 447 */
433 448 for (i = 0; i < old_hashsz; i++) {
434 449 mutex_exit(&old_hash[i].th_lock);
435 450 mutex_exit(&new_hash[i].th_lock);
436 451 }
437 452
438 453 /*
439 454 * Avoid freeing these objects below.
440 455 */
441 456 ret_tidhash = NULL;
442 457 old_hash = NULL;
443 458 old_hashsz = 0;
444 459 }
445 460 }
446 461
447 462 /*
448 463 * Block the process against /proc while we manipulate p->p_tlist,
449 464 * unless lwp_create() was called by /proc for the PCAGENT operation.
450 465 * We want to do this early enough so that we don't drop p->p_lock
451 466 * until the thread is put on the p->p_tlist.
452 467 */
453 468 if (p == curproc) {
454 469 prbarrier(p);
455 470 /*
456 471 * If the current lwp has been requested to stop, do so now.
457 472 * Otherwise we have a race condition between /proc attempting
458 473 * to stop the process and this thread creating a new lwp
459 474 * that was not seen when the /proc PCSTOP request was issued.
460 475 * We rely on stop() to call prbarrier(p) before returning.
461 476 */
462 477 while ((curthread->t_proc_flag & TP_PRSTOP) &&
463 478 !ttolwp(curthread)->lwp_nostop) {
464 479 /*
465 480 * We called pool_barrier_enter() before calling
466 481 * here to lwp_create(). We have to call
467 482 * pool_barrier_exit() before stopping.
468 483 */
469 484 pool_barrier_exit();
470 485 prbarrier(p);
471 486 stop(PR_REQUESTED, 0);
472 487 /*
473 488 * And we have to repeat the call to
474 489 * pool_barrier_enter after stopping.
475 490 */
476 491 pool_barrier_enter();
477 492 prbarrier(p);
478 493 }
479 494
480 495 /*
481 496 * If process is exiting, there could be a race between
482 497 * the agent lwp creation and the new lwp currently being
483 498 * created. So to prevent this race lwp creation is failed
484 499 * if the process is exiting.
485 500 */
486 501 if (p->p_flag & (SEXITLWPS|SKILLED)) {
487 502 err = 1;
488 503 goto error;
489 504 }
490 505
491 506 /*
492 507 * Since we might have dropped p->p_lock, the
493 508 * lwp directory free list might have changed.
494 509 */
495 510 if (p->p_lwpfree == NULL)
496 511 goto grow;
497 512 }
498 513
499 514 kpreempt_disable(); /* can't grab cpu_lock here */
500 515
501 516 /*
502 517 * Inherit processor and processor set bindings from curthread.
503 518 *
504 519 * For kernel LWPs, we do not inherit processor set bindings at
505 520 * process creation time (i.e. when p != curproc). After the
506 521 * kernel process is created, any subsequent LWPs must be created
507 522 * by threads in the kernel process, at which point we *will*
508 523 * inherit processor set bindings.
509 524 */
510 525 if (CLASS_KERNEL(cid) && p != curproc) {
511 526 t->t_bind_cpu = binding = PBIND_NONE;
512 527 t->t_cpupart = oldpart = &cp_default;
513 528 t->t_bind_pset = PS_NONE;
514 529 t->t_bindflag = (uchar_t)default_binding_mode;
515 530 } else {
516 531 binding = curthread->t_bind_cpu;
517 532 t->t_bind_cpu = binding;
518 533 oldpart = t->t_cpupart;
519 534 t->t_cpupart = curthread->t_cpupart;
520 535 t->t_bind_pset = curthread->t_bind_pset;
521 536 t->t_bindflag = curthread->t_bindflag |
522 537 (uchar_t)default_binding_mode;
523 538 }
524 539
525 540 /*
526 541 * thread_create() initializes this thread's home lgroup to the root.
527 542 * Choose a more suitable lgroup, since this thread is associated
528 543 * with an lwp.
529 544 */
530 545 ASSERT(oldpart != NULL);
531 546 if (binding != PBIND_NONE && t->t_affinitycnt == 0) {
532 547 t->t_bound_cpu = cpu[binding];
533 548 if (t->t_lpl != t->t_bound_cpu->cpu_lpl)
534 549 lgrp_move_thread(t, t->t_bound_cpu->cpu_lpl, 1);
535 550 } else if (CLASS_KERNEL(cid)) {
536 551 /*
537 552 * Kernel threads are always in the root lgrp.
538 553 */
539 554 lgrp_move_thread(t,
540 555 &t->t_cpupart->cp_lgrploads[LGRP_ROOTID], 1);
541 556 } else {
542 557 lgrp_move_thread(t, lgrp_choose(t, t->t_cpupart), 1);
543 558 }
544 559
545 560 kpreempt_enable();
546 561
547 562 /*
548 563 * make sure lpl points to our own partition
549 564 */
550 565 ASSERT(t->t_lpl >= t->t_cpupart->cp_lgrploads);
551 566 ASSERT(t->t_lpl < t->t_cpupart->cp_lgrploads +
552 567 t->t_cpupart->cp_nlgrploads);
553 568
554 569 /*
555 570 * It is safe to point the thread to the new project without holding it
556 571 * since we're holding the target process' p_lock here and therefore
557 572 * we're guaranteed that it will not move to another project.
558 573 */
559 574 newkpj = p->p_task->tk_proj;
560 575 oldkpj = ttoproj(t);
561 576 if (newkpj != oldkpj) {
562 577 t->t_proj = newkpj;
563 578 (void) project_hold(newkpj);
564 579 project_rele(oldkpj);
565 580 }
566 581
567 582 if (cid != NOCLASS) {
568 583 /*
569 584 * If the lwp is being created in the current process
570 585 * and matches the current thread's scheduling class,
571 586 * we should propagate the current thread's scheduling
572 587 * parameters by calling CL_FORK. Otherwise just use
573 588 * the defaults by calling CL_ENTERCLASS.
574 589 */
575 590 if (p != curproc || curthread->t_cid != cid) {
576 591 err = CL_ENTERCLASS(t, cid, NULL, NULL, bufp);
577 592 t->t_pri = pri; /* CL_ENTERCLASS may have changed it */
578 593 /*
579 594 * We don't call schedctl_set_cidpri(t) here
580 595 * because the schedctl data is not yet set
581 596 * up for the newly-created lwp.
582 597 */
583 598 } else {
584 599 t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
585 600 err = CL_FORK(curthread, t, bufp);
586 601 t->t_cid = cid;
587 602 }
588 603 if (err) {
589 604 atomic_inc_32(&p->p_zone->zone_ffmisc);
590 605 goto error;
591 606 } else {
592 607 bufp = NULL;
593 608 }
594 609 }
595 610
596 611 /*
597 612 * If we were given an lwpid then use it, else allocate one.
598 613 */
599 614 if (lwpid != 0)
600 615 t->t_tid = lwpid;
601 616 else {
602 617 /*
603 618 * lwp/thread id 0 is never valid; reserved for special checks.
604 619 * lwp/thread id 1 is reserved for the main thread.
605 620 * Start again at 2 when INT_MAX has been reached
606 621 * (id_t is a signed 32-bit integer).
607 622 */
608 623 id_t prev_id = p->p_lwpid; /* last allocated tid */
609 624
610 625 do { /* avoid lwpid duplication */
611 626 if (p->p_lwpid == INT_MAX) {
612 627 p->p_flag |= SLWPWRAP;
613 628 p->p_lwpid = 1;
614 629 }
615 630 if ((t->t_tid = ++p->p_lwpid) == prev_id) {
616 631 /*
617 632 * All lwpids are allocated; fail the request.
618 633 */
619 634 err = 1;
620 635 atomic_inc_32(&p->p_zone->zone_ffnoproc);
621 636 goto error;
622 637 }
|
↓ open down ↓ |
327 lines elided |
↑ open up ↑ |
623 638 /*
624 639 * We only need to worry about colliding with an id
625 640 * that's already in use if this process has
626 641 * cycled through all available lwp ids.
627 642 */
628 643 if ((p->p_flag & SLWPWRAP) == 0)
629 644 break;
630 645 } while (lwp_hash_lookup(p, t->t_tid) != NULL);
631 646 }
632 647
633 - /*
634 - * If this is a branded process, let the brand do any necessary lwp
635 - * initialization.
636 - */
637 - if (PROC_IS_BRANDED(p)) {
638 - if (BROP(p)->b_initlwp(lwp)) {
639 - err = 1;
640 - atomic_inc_32(&p->p_zone->zone_ffmisc);
641 - goto error;
642 - }
643 - branded = 1;
644 - }
645 648
646 649 if (t->t_tid == 1) {
647 650 kpreempt_disable();
648 651 ASSERT(t->t_lpl != NULL);
649 652 p->p_t1_lgrpid = t->t_lpl->lpl_lgrpid;
650 653 kpreempt_enable();
651 654 if (p->p_tr_lgrpid != LGRP_NONE &&
652 655 p->p_tr_lgrpid != p->p_t1_lgrpid) {
653 656 lgrp_update_trthr_migrations(1);
654 657 }
655 658 }
656 659
657 - p->p_lwpcnt++;
658 660 t->t_waitfor = -1;
659 661
660 662 /*
661 663 * Turn microstate accounting on for thread if on for process.
662 664 */
663 665 if (p->p_flag & SMSACCT)
664 666 t->t_proc_flag |= TP_MSACCT;
665 667
666 668 /*
667 669 * If the process has watchpoints, mark the new thread as such.
668 670 */
669 671 if (pr_watch_active(p))
670 672 watch_enable(t);
671 673
672 674 /*
673 675 * The lwp is being created in the stopped state.
674 676 * We set all the necessary flags to indicate that fact here.
675 677 * We omit the TS_CREATE flag from t_schedflag so that the lwp
676 678 * cannot be set running until the caller is finished with it,
677 679 * even if lwp_continue() is called on it after we drop p->p_lock.
678 680 * When the caller is finished with the newly-created lwp,
679 681 * the caller must call lwp_create_done() to allow the lwp
680 682 * to be set running. If the TP_HOLDLWP is left set, the
681 683 * lwp will suspend itself after reaching system call exit.
682 684 */
683 685 init_mstate(t, LMS_STOPPED);
684 686 t->t_proc_flag |= TP_HOLDLWP;
685 687 t->t_schedflag |= (TS_ALLSTART & ~(TS_CSTART | TS_CREATE));
686 688 t->t_whystop = PR_SUSPENDED;
687 689 t->t_whatstop = SUSPEND_NORMAL;
688 690 t->t_sig_check = 1; /* ensure that TP_HOLDLWP is honored */
|
↓ open down ↓ |
21 lines elided |
↑ open up ↑ |
689 691
690 692 /*
691 693 * Set system call processing flags in case tracing or profiling
692 694 * is set. The first system call will evaluate these and turn
693 695 * them off if they aren't needed.
694 696 */
695 697 t->t_pre_sys = 1;
696 698 t->t_post_sys = 1;
697 699
698 700 /*
701 + * Perform lwp branding
702 + *
703 + * The b_initlwp hook is _not_ allowed to drop p->p_lock as it must be
704 + * continuously held between when the tidhash is sized and when the lwp
705 + * is inserted into it. Operations requiring p->p_lock to be
706 + * temporarily dropped can be performed in b_initlwp_post.
707 + */
708 + if (PROC_IS_BRANDED(p)) {
709 + BROP(p)->b_initlwp(lwp, brand_data);
710 + /*
711 + * The b_initlwp hook is expected to consume any preallocated
712 + * brand_data in a way that prepares it for deallocation by the
713 + * b_freelwp hook.
714 + */
715 + brand_data = NULL;
716 + }
717 +
718 + /*
699 719 * Insert the new thread into the list of all threads.
700 720 */
721 + p->p_lwpcnt++;
701 722 if ((tx = p->p_tlist) == NULL) {
702 723 t->t_back = t;
703 724 t->t_forw = t;
704 725 p->p_tlist = t;
705 726 } else {
706 727 t->t_forw = tx;
707 728 t->t_back = tx->t_back;
708 729 tx->t_back->t_forw = t;
709 730 tx->t_back = t;
710 731 }
711 732
712 733 /*
713 734 * Insert the new lwp into an lwp directory slot position
714 735 * and into the lwpid hash table.
715 736 */
716 737 lep->le_thread = t;
717 738 lep->le_lwpid = t->t_tid;
718 739 lep->le_start = t->t_start;
719 740 lwp_hash_in(p, lep, p->p_tidhash, p->p_tidhash_sz, 1);
720 741
742 + /*
743 + * Complete lwp branding
744 + */
745 + if (PROC_IS_BRANDED(p) && BROP(p)->b_initlwp_post != NULL) {
746 + BROP(p)->b_initlwp_post(lwp);
747 + }
748 +
721 749 if (state == TS_RUN) {
722 750 /*
723 751 * We set the new lwp running immediately.
724 752 */
725 753 t->t_proc_flag &= ~TP_HOLDLWP;
726 754 lwp_create_done(t);
727 755 }
728 756
729 757 error:
730 758 if (err) {
731 759 if (CLASS_KERNEL(cid)) {
732 760 /*
733 761 * This should only happen if a system process runs
734 762 * out of lwpids, which shouldn't occur.
735 763 */
736 764 panic("Failed to create a system LWP");
737 765 }
738 766 /*
739 767 * We have failed to create an lwp, so decrement the number
740 768 * of lwps in the task and let the lgroup load averages know
741 769 * that this thread isn't going to show up.
742 770 */
743 771 kpreempt_disable();
744 772 lgrp_move_thread(t, NULL, 1);
745 773 kpreempt_enable();
|
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
746 774
747 775 ASSERT(MUTEX_HELD(&p->p_lock));
748 776 mutex_enter(&p->p_zone->zone_nlwps_lock);
749 777 p->p_task->tk_nlwps--;
750 778 p->p_task->tk_proj->kpj_nlwps--;
751 779 p->p_zone->zone_nlwps--;
752 780 mutex_exit(&p->p_zone->zone_nlwps_lock);
753 781 if (cid != NOCLASS && bufp != NULL)
754 782 CL_FREE(cid, bufp);
755 783
756 - if (branded)
757 - BROP(p)->b_freelwp(lwp);
784 + if (brand_data != NULL) {
785 + BROP(p)->b_lwpdata_free(brand_data);
786 + }
758 787
759 788 mutex_exit(&p->p_lock);
760 789 t->t_state = TS_FREE;
761 790 thread_rele(t);
762 791
763 792 /*
764 793 * We need to remove t from the list of all threads
765 794 * because thread_exit()/lwp_exit() isn't called on t.
766 795 */
767 796 mutex_enter(&pidlock);
768 797 ASSERT(t != t->t_next); /* t0 never exits */
769 798 t->t_next->t_prev = t->t_prev;
770 799 t->t_prev->t_next = t->t_next;
771 800 mutex_exit(&pidlock);
772 801
773 802 thread_free(t);
774 803 kmem_free(lep, sizeof (*lep));
775 804 lwp = NULL;
776 805 } else {
777 806 mutex_exit(&p->p_lock);
778 807 }
779 808
780 809 if (old_dir != NULL)
781 810 kmem_free(old_dir, old_dirsz * sizeof (*old_dir));
782 811 if (old_hash != NULL)
783 812 kmem_free(old_hash, old_hashsz * sizeof (*old_hash));
784 813 if (ret_tidhash != NULL)
785 814 kmem_free(ret_tidhash, sizeof (ret_tidhash_t));
786 815
787 816 DTRACE_PROC1(lwp__create, kthread_t *, t);
788 817 return (lwp);
789 818 }
790 819
791 820 /*
792 821 * lwp_create_done() is called by the caller of lwp_create() to set the
793 822 * newly-created lwp running after the caller has finished manipulating it.
794 823 */
795 824 void
796 825 lwp_create_done(kthread_t *t)
797 826 {
798 827 proc_t *p = ttoproc(t);
799 828
800 829 ASSERT(MUTEX_HELD(&p->p_lock));
801 830
802 831 /*
803 832 * We set the TS_CREATE and TS_CSTART flags and call setrun_locked().
804 833 * (The absence of the TS_CREATE flag prevents the lwp from running
805 834 * until we are finished with it, even if lwp_continue() is called on
806 835 * it by some other lwp in the process or elsewhere in the kernel.)
807 836 */
808 837 thread_lock(t);
809 838 ASSERT(t->t_state == TS_STOPPED && !(t->t_schedflag & TS_CREATE));
810 839 /*
811 840 * If TS_CSTART is set, lwp_continue(t) has been called and
812 841 * has already incremented p_lwprcnt; avoid doing this twice.
813 842 */
814 843 if (!(t->t_schedflag & TS_CSTART))
815 844 p->p_lwprcnt++;
816 845 t->t_schedflag |= (TS_CSTART | TS_CREATE);
817 846 setrun_locked(t);
818 847 thread_unlock(t);
819 848 }
|
↓ open down ↓ |
52 lines elided |
↑ open up ↑ |
820 849
821 850 /*
822 851 * Copy an LWP's active templates, and clear the latest contracts.
823 852 */
824 853 void
825 854 lwp_ctmpl_copy(klwp_t *dst, klwp_t *src)
826 855 {
827 856 int i;
828 857
829 858 for (i = 0; i < ct_ntypes; i++) {
830 - dst->lwp_ct_active[i] = ctmpl_dup(src->lwp_ct_active[i]);
859 + ct_template_t *tmpl = src->lwp_ct_active[i];
860 +
861 + /*
862 + * If the process contract template is setup to be preserved
863 + * across exec, then if we're forking, perform an implicit
864 + * template_clear now. This ensures that future children of
865 + * this child will remain in the same contract unless they're
866 + * explicitly setup differently. We know we're forking if the
867 + * two LWPs belong to different processes.
868 + */
869 + if (i == CTT_PROCESS && tmpl != NULL) {
870 + ctmpl_process_t *ctp = tmpl->ctmpl_data;
871 +
872 + if (dst->lwp_procp != src->lwp_procp &&
873 + (ctp->ctp_params & CT_PR_KEEP_EXEC) != 0)
874 + tmpl = NULL;
875 + }
876 +
877 + dst->lwp_ct_active[i] = ctmpl_dup(tmpl);
831 878 dst->lwp_ct_latest[i] = NULL;
879 +
832 880 }
833 881 }
834 882
835 883 /*
836 884 * Clear an LWP's contract template state.
837 885 */
838 886 void
839 -lwp_ctmpl_clear(klwp_t *lwp)
887 +lwp_ctmpl_clear(klwp_t *lwp, boolean_t is_exec)
840 888 {
841 889 ct_template_t *tmpl;
842 890 int i;
843 891
844 892 for (i = 0; i < ct_ntypes; i++) {
845 - if ((tmpl = lwp->lwp_ct_active[i]) != NULL) {
846 - ctmpl_free(tmpl);
847 - lwp->lwp_ct_active[i] = NULL;
848 - }
849 -
850 893 if (lwp->lwp_ct_latest[i] != NULL) {
851 894 contract_rele(lwp->lwp_ct_latest[i]);
852 895 lwp->lwp_ct_latest[i] = NULL;
853 896 }
897 +
898 + if ((tmpl = lwp->lwp_ct_active[i]) != NULL) {
899 + /*
900 + * If we're exec-ing a new program and the process
901 + * contract template is setup to be preserved across
902 + * exec, then don't clear it.
903 + */
904 + if (is_exec && i == CTT_PROCESS) {
905 + ctmpl_process_t *ctp = tmpl->ctmpl_data;
906 +
907 + if ((ctp->ctp_params & CT_PR_KEEP_EXEC) != 0)
908 + continue;
909 + }
910 +
911 + ctmpl_free(tmpl);
912 + lwp->lwp_ct_active[i] = NULL;
913 + }
854 914 }
855 915 }
856 916
857 917 /*
858 918 * Individual lwp exit.
859 919 * If this is the last lwp, exit the whole process.
860 920 */
861 921 void
862 922 lwp_exit(void)
863 923 {
864 924 kthread_t *t = curthread;
865 925 klwp_t *lwp = ttolwp(t);
866 926 proc_t *p = ttoproc(t);
867 927
868 928 ASSERT(MUTEX_HELD(&p->p_lock));
869 929
870 930 mutex_exit(&p->p_lock);
871 931
872 932 #if defined(__sparc)
873 933 /*
874 934 * Ensure that the user stack is fully abandoned..
875 935 */
876 936 trash_user_windows();
877 937 #endif
878 938
879 939 tsd_exit(); /* free thread specific data */
880 940
881 941 kcpc_passivate(); /* Clean up performance counter state */
882 942
883 943 pollcleanup();
|
↓ open down ↓ |
20 lines elided |
↑ open up ↑ |
884 944
885 945 if (t->t_door)
886 946 door_slam();
887 947
888 948 if (t->t_schedctl != NULL)
889 949 schedctl_lwp_cleanup(t);
890 950
891 951 if (t->t_upimutex != NULL)
892 952 upimutex_cleanup();
893 953
894 - /*
895 - * Perform any brand specific exit processing, then release any
896 - * brand data associated with the lwp
897 - */
898 - if (PROC_IS_BRANDED(p))
899 - BROP(p)->b_lwpexit(lwp);
900 -
901 954 lwp_pcb_exit();
902 955
903 956 mutex_enter(&p->p_lock);
904 957 lwp_cleanup();
905 958
906 959 /*
907 960 * When this process is dumping core, its lwps are held here
908 961 * until the core dump is finished. Then exitlwps() is called
909 962 * again to release these lwps so that they can finish exiting.
910 963 */
911 964 if (p->p_flag & SCOREDUMP)
912 965 stop(PR_SUSPENDED, SUSPEND_NORMAL);
913 966
914 967 /*
915 968 * Block the process against /proc now that we have really acquired
916 969 * p->p_lock (to decrement p_lwpcnt and manipulate p_tlist at least).
917 970 */
918 971 prbarrier(p);
919 972
920 973 /*
921 974 * Call proc_exit() if this is the last non-daemon lwp in the process.
922 975 */
923 976 if (!(t->t_proc_flag & TP_DAEMON) &&
924 977 p->p_lwpcnt == p->p_lwpdaemon + 1) {
925 978 mutex_exit(&p->p_lock);
926 979 if (proc_exit(CLD_EXITED, 0) == 0) {
927 980 /* Restarting init. */
928 981 return;
929 982 }
930 983
931 984 /*
932 985 * proc_exit() returns a non-zero value when some other
933 986 * lwp got there first. We just have to continue in
|
↓ open down ↓ |
23 lines elided |
↑ open up ↑ |
934 987 * lwp_exit().
935 988 */
936 989 mutex_enter(&p->p_lock);
937 990 ASSERT(curproc->p_flag & SEXITLWPS);
938 991 prbarrier(p);
939 992 }
940 993
941 994 DTRACE_PROC(lwp__exit);
942 995
943 996 /*
997 + * Perform any brand specific exit processing, then release any
998 + * brand data associated with the lwp
999 + */
1000 + if (PROC_IS_BRANDED(p)) {
1001 + mutex_exit(&p->p_lock);
1002 + BROP(p)->b_lwpexit(lwp);
1003 + BROP(p)->b_freelwp(lwp);
1004 + mutex_enter(&p->p_lock);
1005 + prbarrier(p);
1006 + }
1007 +
1008 + /*
944 1009 * If the lwp is a detached lwp or if the process is exiting,
945 1010 * remove (lwp_hash_out()) the lwp from the lwp directory.
946 1011 * Otherwise null out the lwp's le_thread pointer in the lwp
947 1012 * directory so that other threads will see it as a zombie lwp.
948 1013 */
949 1014 prlwpexit(t); /* notify /proc */
950 1015 if (!(t->t_proc_flag & TP_TWAIT) || (p->p_flag & SEXITLWPS))
951 1016 lwp_hash_out(p, t->t_tid);
952 1017 else {
953 1018 ASSERT(!(t->t_proc_flag & TP_DAEMON));
954 1019 p->p_lwpdir[t->t_dslot].ld_entry->le_thread = NULL;
955 1020 p->p_zombcnt++;
956 1021 cv_broadcast(&p->p_lwpexit);
957 1022 }
958 1023 if (t->t_proc_flag & TP_DAEMON) {
959 1024 p->p_lwpdaemon--;
960 1025 t->t_proc_flag &= ~TP_DAEMON;
961 1026 }
962 1027 t->t_proc_flag &= ~TP_TWAIT;
963 1028
964 1029 /*
965 1030 * Maintain accurate lwp count for task.max-lwps resource control.
966 1031 */
967 1032 mutex_enter(&p->p_zone->zone_nlwps_lock);
968 1033 p->p_task->tk_nlwps--;
969 1034 p->p_task->tk_proj->kpj_nlwps--;
970 1035 p->p_zone->zone_nlwps--;
971 1036 mutex_exit(&p->p_zone->zone_nlwps_lock);
972 1037
973 1038 CL_EXIT(t); /* tell the scheduler that t is exiting */
974 1039 ASSERT(p->p_lwpcnt != 0);
975 1040 p->p_lwpcnt--;
976 1041
977 1042 /*
978 1043 * If all remaining non-daemon lwps are waiting in lwp_wait(),
979 1044 * wake them up so someone can return EDEADLK.
980 1045 * (See the block comment preceeding lwp_wait().)
981 1046 */
982 1047 if (p->p_lwpcnt == p->p_lwpdaemon + (p->p_lwpwait - p->p_lwpdwait))
983 1048 cv_broadcast(&p->p_lwpexit);
984 1049
985 1050 t->t_proc_flag |= TP_LWPEXIT;
986 1051 term_mstate(t);
987 1052
988 1053 #ifndef NPROBE
989 1054 /* Kernel probe */
990 1055 if (t->t_tnf_tpdp)
991 1056 tnf_thread_exit();
992 1057 #endif /* NPROBE */
993 1058
994 1059 t->t_forw->t_back = t->t_back;
995 1060 t->t_back->t_forw = t->t_forw;
996 1061 if (t == p->p_tlist)
997 1062 p->p_tlist = t->t_forw;
998 1063
999 1064 /*
1000 1065 * Clean up the signal state.
1001 1066 */
1002 1067 if (t->t_sigqueue != NULL)
1003 1068 sigdelq(p, t, 0);
1004 1069 if (lwp->lwp_curinfo != NULL) {
1005 1070 siginfofree(lwp->lwp_curinfo);
1006 1071 lwp->lwp_curinfo = NULL;
1007 1072 }
1008 1073
1009 1074 /*
1010 1075 * If we have spymaster information (that is, if we're an agent LWP),
1011 1076 * free that now.
1012 1077 */
1013 1078 if (lwp->lwp_spymaster != NULL) {
1014 1079 kmem_free(lwp->lwp_spymaster, sizeof (psinfo_t));
1015 1080 lwp->lwp_spymaster = NULL;
1016 1081 }
1017 1082
1018 1083 thread_rele(t);
1019 1084
1020 1085 /*
1021 1086 * Terminated lwps are associated with process zero and are put onto
1022 1087 * death-row by resume(). Avoid preemption after resetting t->t_procp.
1023 1088 */
1024 1089 t->t_preempt++;
1025 1090
1026 1091 if (t->t_ctx != NULL)
1027 1092 exitctx(t);
1028 1093 if (p->p_pctx != NULL)
1029 1094 exitpctx(p);
1030 1095
1031 1096 t->t_procp = &p0;
1032 1097
1033 1098 /*
1034 1099 * Notify the HAT about the change of address space
1035 1100 */
1036 1101 hat_thread_exit(t);
1037 1102 /*
1038 1103 * When this is the last running lwp in this process and some lwp is
1039 1104 * waiting for this condition to become true, or this thread was being
1040 1105 * suspended, then the waiting lwp is awakened.
1041 1106 *
1042 1107 * Also, if the process is exiting, we may have a thread waiting in
1043 1108 * exitlwps() that needs to be notified.
1044 1109 */
1045 1110 if (--p->p_lwprcnt == 0 || (t->t_proc_flag & TP_HOLDLWP) ||
1046 1111 (p->p_flag & SEXITLWPS))
1047 1112 cv_broadcast(&p->p_holdlwps);
1048 1113
1049 1114 /*
1050 1115 * Need to drop p_lock so we can reacquire pidlock.
1051 1116 */
1052 1117 mutex_exit(&p->p_lock);
1053 1118 mutex_enter(&pidlock);
1054 1119
1055 1120 ASSERT(t != t->t_next); /* t0 never exits */
1056 1121 t->t_next->t_prev = t->t_prev;
1057 1122 t->t_prev->t_next = t->t_next;
1058 1123 cv_broadcast(&t->t_joincv); /* wake up anyone in thread_join */
1059 1124 mutex_exit(&pidlock);
1060 1125
1061 1126 t->t_state = TS_ZOMB;
1062 1127 swtch_from_zombie();
1063 1128 /* never returns */
1064 1129 }
1065 1130
1066 1131
1067 1132 /*
1068 1133 * Cleanup function for an exiting lwp.
1069 1134 * Called both from lwp_exit() and from proc_exit().
1070 1135 * p->p_lock is repeatedly released and grabbed in this function.
1071 1136 */
1072 1137 void
1073 1138 lwp_cleanup(void)
1074 1139 {
1075 1140 kthread_t *t = curthread;
1076 1141 proc_t *p = ttoproc(t);
1077 1142
1078 1143 ASSERT(MUTEX_HELD(&p->p_lock));
1079 1144
1080 1145 /* untimeout any lwp-bound realtime timers */
1081 1146 if (p->p_itimer != NULL)
1082 1147 timer_lwpexit();
1083 1148
1084 1149 /*
1085 1150 * If this is the /proc agent lwp that is exiting, readjust p_lwpid
1086 1151 * so it appears that the agent never existed, and clear p_agenttp.
1087 1152 */
1088 1153 if (t == p->p_agenttp) {
1089 1154 ASSERT(t->t_tid == p->p_lwpid);
1090 1155 p->p_lwpid--;
1091 1156 p->p_agenttp = NULL;
1092 1157 }
1093 1158
|
↓ open down ↓ |
140 lines elided |
↑ open up ↑ |
1094 1159 /*
1095 1160 * Do lgroup bookkeeping to account for thread exiting.
1096 1161 */
1097 1162 kpreempt_disable();
1098 1163 lgrp_move_thread(t, NULL, 1);
1099 1164 if (t->t_tid == 1) {
1100 1165 p->p_t1_lgrpid = LGRP_NONE;
1101 1166 }
1102 1167 kpreempt_enable();
1103 1168
1104 - lwp_ctmpl_clear(ttolwp(t));
1169 + lwp_ctmpl_clear(ttolwp(t), B_FALSE);
1105 1170 }
1106 1171
1107 1172 int
1108 1173 lwp_suspend(kthread_t *t)
1109 1174 {
1110 1175 int tid;
1111 1176 proc_t *p = ttoproc(t);
1112 1177
1113 1178 ASSERT(MUTEX_HELD(&p->p_lock));
1114 1179
1115 1180 /*
1116 1181 * Set the thread's TP_HOLDLWP flag so it will stop in holdlwp().
1117 1182 * If an lwp is stopping itself, there is no need to wait.
1118 1183 */
1119 1184 top:
1120 1185 t->t_proc_flag |= TP_HOLDLWP;
1121 1186 if (t == curthread) {
1122 1187 t->t_sig_check = 1;
1123 1188 } else {
1124 1189 /*
1125 1190 * Make sure the lwp stops promptly.
1126 1191 */
1127 1192 thread_lock(t);
1128 1193 t->t_sig_check = 1;
1129 1194 /*
1130 1195 * XXX Should use virtual stop like /proc does instead of
1131 1196 * XXX waking the thread to get it to stop.
1132 1197 */
1133 1198 if (ISWAKEABLE(t) || ISWAITING(t)) {
1134 1199 setrun_locked(t);
1135 1200 } else if (t->t_state == TS_ONPROC && t->t_cpu != CPU) {
1136 1201 poke_cpu(t->t_cpu->cpu_id);
1137 1202 }
1138 1203
1139 1204 tid = t->t_tid; /* remember thread ID */
1140 1205 /*
1141 1206 * Wait for lwp to stop
1142 1207 */
1143 1208 while (!SUSPENDED(t)) {
1144 1209 /*
1145 1210 * Drop the thread lock before waiting and reacquire it
1146 1211 * afterwards, so the thread can change its t_state
1147 1212 * field.
1148 1213 */
1149 1214 thread_unlock(t);
1150 1215
1151 1216 /*
1152 1217 * Check if aborted by exitlwps().
1153 1218 */
1154 1219 if (p->p_flag & SEXITLWPS)
1155 1220 lwp_exit();
1156 1221
1157 1222 /*
1158 1223 * Cooperate with jobcontrol signals and /proc stopping
1159 1224 * by calling cv_wait_sig() to wait for the target
1160 1225 * lwp to stop. Just using cv_wait() can lead to
1161 1226 * deadlock because, if some other lwp has stopped
1162 1227 * by either of these mechanisms, then p_lwprcnt will
1163 1228 * never become zero if we do a cv_wait().
1164 1229 */
1165 1230 if (!cv_wait_sig(&p->p_holdlwps, &p->p_lock))
1166 1231 return (EINTR);
1167 1232
1168 1233 /*
1169 1234 * Check to see if thread died while we were
1170 1235 * waiting for it to suspend.
1171 1236 */
1172 1237 if (idtot(p, tid) == NULL)
1173 1238 return (ESRCH);
1174 1239
1175 1240 thread_lock(t);
1176 1241 /*
1177 1242 * If the TP_HOLDLWP flag went away, lwp_continue()
1178 1243 * or vfork() must have been called while we were
1179 1244 * waiting, so start over again.
1180 1245 */
1181 1246 if ((t->t_proc_flag & TP_HOLDLWP) == 0) {
1182 1247 thread_unlock(t);
1183 1248 goto top;
1184 1249 }
1185 1250 }
1186 1251 thread_unlock(t);
1187 1252 }
1188 1253 return (0);
1189 1254 }
1190 1255
1191 1256 /*
1192 1257 * continue a lwp that's been stopped by lwp_suspend().
1193 1258 */
1194 1259 void
1195 1260 lwp_continue(kthread_t *t)
1196 1261 {
1197 1262 proc_t *p = ttoproc(t);
1198 1263 int was_suspended = t->t_proc_flag & TP_HOLDLWP;
1199 1264
1200 1265 ASSERT(MUTEX_HELD(&p->p_lock));
1201 1266
1202 1267 t->t_proc_flag &= ~TP_HOLDLWP;
1203 1268 thread_lock(t);
1204 1269 if (SUSPENDED(t) &&
1205 1270 !(p->p_flag & (SHOLDFORK | SHOLDFORK1 | SHOLDWATCH))) {
1206 1271 p->p_lwprcnt++;
1207 1272 t->t_schedflag |= TS_CSTART;
1208 1273 setrun_locked(t);
1209 1274 }
1210 1275 thread_unlock(t);
1211 1276 /*
1212 1277 * Wakeup anyone waiting for this thread to be suspended
1213 1278 */
1214 1279 if (was_suspended)
1215 1280 cv_broadcast(&p->p_holdlwps);
1216 1281 }
1217 1282
1218 1283 /*
1219 1284 * ********************************
1220 1285 * Miscellaneous lwp routines *
1221 1286 * ********************************
1222 1287 */
1223 1288 /*
1224 1289 * When a process is undergoing a forkall(), its p_flag is set to SHOLDFORK.
1225 1290 * This will cause the process's lwps to stop at a hold point. A hold
1226 1291 * point is where a kernel thread has a flat stack. This is at the
1227 1292 * return from a system call and at the return from a user level trap.
1228 1293 *
1229 1294 * When a process is undergoing a fork1() or vfork(), its p_flag is set to
1230 1295 * SHOLDFORK1. This will cause the process's lwps to stop at a modified
1231 1296 * hold point. The lwps in the process are not being cloned, so they
1232 1297 * are held at the usual hold points and also within issig_forreal().
1233 1298 * This has the side-effect that their system calls do not return
1234 1299 * showing EINTR.
1235 1300 *
1236 1301 * An lwp can also be held. This is identified by the TP_HOLDLWP flag on
1237 1302 * the thread. The TP_HOLDLWP flag is set in lwp_suspend(), where the active
1238 1303 * lwp is waiting for the target lwp to be stopped.
1239 1304 */
1240 1305 void
1241 1306 holdlwp(void)
1242 1307 {
1243 1308 proc_t *p = curproc;
1244 1309 kthread_t *t = curthread;
1245 1310
1246 1311 mutex_enter(&p->p_lock);
1247 1312 /*
1248 1313 * Don't terminate immediately if the process is dumping core.
1249 1314 * Once the process has dumped core, all lwps are terminated.
1250 1315 */
1251 1316 if (!(p->p_flag & SCOREDUMP)) {
1252 1317 if ((p->p_flag & SEXITLWPS) || (t->t_proc_flag & TP_EXITLWP))
1253 1318 lwp_exit();
1254 1319 }
1255 1320 if (!(ISHOLD(p)) && !(p->p_flag & (SHOLDFORK1 | SHOLDWATCH))) {
1256 1321 mutex_exit(&p->p_lock);
1257 1322 return;
1258 1323 }
1259 1324 /*
1260 1325 * stop() decrements p->p_lwprcnt and cv_signal()s &p->p_holdlwps
1261 1326 * when p->p_lwprcnt becomes zero.
1262 1327 */
1263 1328 stop(PR_SUSPENDED, SUSPEND_NORMAL);
1264 1329 if (p->p_flag & SEXITLWPS)
1265 1330 lwp_exit();
1266 1331 mutex_exit(&p->p_lock);
1267 1332 }
1268 1333
1269 1334 /*
1270 1335 * Have all lwps within the process hold at a point where they are
1271 1336 * cloneable (SHOLDFORK) or just safe w.r.t. fork1 (SHOLDFORK1).
1272 1337 */
1273 1338 int
1274 1339 holdlwps(int holdflag)
1275 1340 {
1276 1341 proc_t *p = curproc;
1277 1342
1278 1343 ASSERT(holdflag == SHOLDFORK || holdflag == SHOLDFORK1);
1279 1344 mutex_enter(&p->p_lock);
1280 1345 schedctl_finish_sigblock(curthread);
1281 1346 again:
1282 1347 while (p->p_flag & (SEXITLWPS | SHOLDFORK | SHOLDFORK1 | SHOLDWATCH)) {
1283 1348 /*
1284 1349 * If another lwp is doing a forkall() or proc_exit(), bail out.
1285 1350 */
1286 1351 if (p->p_flag & (SEXITLWPS | SHOLDFORK)) {
1287 1352 mutex_exit(&p->p_lock);
1288 1353 return (0);
1289 1354 }
1290 1355 /*
1291 1356 * Another lwp is doing a fork1() or is undergoing
1292 1357 * watchpoint activity. We hold here for it to complete.
1293 1358 */
1294 1359 stop(PR_SUSPENDED, SUSPEND_NORMAL);
1295 1360 }
1296 1361 p->p_flag |= holdflag;
1297 1362 pokelwps(p);
1298 1363 --p->p_lwprcnt;
1299 1364 /*
1300 1365 * Wait for the process to become quiescent (p->p_lwprcnt == 0).
1301 1366 */
1302 1367 while (p->p_lwprcnt > 0) {
1303 1368 /*
1304 1369 * Check if aborted by exitlwps().
1305 1370 * Also check if SHOLDWATCH is set; it takes precedence.
1306 1371 */
1307 1372 if (p->p_flag & (SEXITLWPS | SHOLDWATCH)) {
1308 1373 p->p_lwprcnt++;
1309 1374 p->p_flag &= ~holdflag;
1310 1375 cv_broadcast(&p->p_holdlwps);
1311 1376 goto again;
1312 1377 }
1313 1378 /*
1314 1379 * Cooperate with jobcontrol signals and /proc stopping.
1315 1380 * If some other lwp has stopped by either of these
1316 1381 * mechanisms, then p_lwprcnt will never become zero
1317 1382 * and the process will appear deadlocked unless we
1318 1383 * stop here in sympathy with the other lwp before
1319 1384 * doing the cv_wait() below.
1320 1385 *
1321 1386 * If the other lwp stops after we do the cv_wait(), it
1322 1387 * will wake us up to loop around and do the sympathy stop.
1323 1388 *
1324 1389 * Since stop() drops p->p_lock, we must start from
1325 1390 * the top again on returning from stop().
1326 1391 */
1327 1392 if (p->p_stopsig | (curthread->t_proc_flag & TP_PRSTOP)) {
1328 1393 int whystop = p->p_stopsig? PR_JOBCONTROL :
1329 1394 PR_REQUESTED;
1330 1395 p->p_lwprcnt++;
1331 1396 p->p_flag &= ~holdflag;
1332 1397 stop(whystop, p->p_stopsig);
1333 1398 goto again;
1334 1399 }
1335 1400 cv_wait(&p->p_holdlwps, &p->p_lock);
1336 1401 }
1337 1402 p->p_lwprcnt++;
1338 1403 p->p_flag &= ~holdflag;
1339 1404 mutex_exit(&p->p_lock);
1340 1405 return (1);
1341 1406 }
1342 1407
1343 1408 /*
1344 1409 * See comments for holdwatch(), below.
1345 1410 */
1346 1411 static int
1347 1412 holdcheck(int clearflags)
1348 1413 {
1349 1414 proc_t *p = curproc;
1350 1415
1351 1416 /*
1352 1417 * If we are trying to exit, that takes precedence over anything else.
1353 1418 */
1354 1419 if (p->p_flag & SEXITLWPS) {
1355 1420 p->p_lwprcnt++;
1356 1421 p->p_flag &= ~clearflags;
1357 1422 lwp_exit();
1358 1423 }
1359 1424
1360 1425 /*
1361 1426 * If another thread is calling fork1(), stop the current thread so the
1362 1427 * other can complete.
1363 1428 */
1364 1429 if (p->p_flag & SHOLDFORK1) {
1365 1430 p->p_lwprcnt++;
1366 1431 stop(PR_SUSPENDED, SUSPEND_NORMAL);
1367 1432 if (p->p_flag & SEXITLWPS) {
1368 1433 p->p_flag &= ~clearflags;
1369 1434 lwp_exit();
1370 1435 }
1371 1436 return (-1);
1372 1437 }
1373 1438
1374 1439 /*
1375 1440 * If another thread is calling fork(), then indicate we are doing
1376 1441 * watchpoint activity. This will cause holdlwps() above to stop the
1377 1442 * forking thread, at which point we can continue with watchpoint
1378 1443 * activity.
1379 1444 */
1380 1445 if (p->p_flag & SHOLDFORK) {
1381 1446 p->p_lwprcnt++;
1382 1447 while (p->p_flag & SHOLDFORK) {
1383 1448 p->p_flag |= SHOLDWATCH;
1384 1449 cv_broadcast(&p->p_holdlwps);
1385 1450 cv_wait(&p->p_holdlwps, &p->p_lock);
1386 1451 p->p_flag &= ~SHOLDWATCH;
1387 1452 }
1388 1453 return (-1);
1389 1454 }
1390 1455
1391 1456 return (0);
1392 1457 }
1393 1458
1394 1459 /*
1395 1460 * Stop all lwps within the process, holding themselves in the kernel while the
1396 1461 * active lwp undergoes watchpoint activity. This is more complicated than
1397 1462 * expected because stop() relies on calling holdwatch() in order to copyin data
1398 1463 * from the user's address space. A double barrier is used to prevent an
1399 1464 * infinite loop.
1400 1465 *
1401 1466 * o The first thread into holdwatch() is the 'master' thread and does
1402 1467 * the following:
1403 1468 *
1404 1469 * - Sets SHOLDWATCH on the current process
1405 1470 * - Sets TP_WATCHSTOP on the current thread
1406 1471 * - Waits for all threads to be either stopped or have
1407 1472 * TP_WATCHSTOP set.
1408 1473 * - Sets the SWATCHOK flag on the process
1409 1474 * - Unsets TP_WATCHSTOP
1410 1475 * - Waits for the other threads to completely stop
1411 1476 * - Unsets SWATCHOK
1412 1477 *
1413 1478 * o If SHOLDWATCH is already set when we enter this function, then another
1414 1479 * thread is already trying to stop this thread. This 'slave' thread
1415 1480 * does the following:
1416 1481 *
1417 1482 * - Sets TP_WATCHSTOP on the current thread
1418 1483 * - Waits for SWATCHOK flag to be set
1419 1484 * - Calls stop()
1420 1485 *
1421 1486 * o If SWATCHOK is set on the process, then this function immediately
1422 1487 * returns, as we must have been called via stop().
1423 1488 *
1424 1489 * In addition, there are other flags that take precedence over SHOLDWATCH:
1425 1490 *
1426 1491 * o If SEXITLWPS is set, exit immediately.
1427 1492 *
1428 1493 * o If SHOLDFORK1 is set, wait for fork1() to complete.
1429 1494 *
1430 1495 * o If SHOLDFORK is set, then watchpoint activity takes precedence In this
1431 1496 * case, set SHOLDWATCH, signalling the forking thread to stop first.
1432 1497 *
1433 1498 * o If the process is being stopped via /proc (TP_PRSTOP is set), then we
1434 1499 * stop the current thread.
1435 1500 *
1436 1501 * Returns 0 if all threads have been quiesced. Returns non-zero if not all
1437 1502 * threads were stopped, or the list of watched pages has changed.
1438 1503 */
1439 1504 int
1440 1505 holdwatch(void)
1441 1506 {
1442 1507 proc_t *p = curproc;
1443 1508 kthread_t *t = curthread;
1444 1509 int ret = 0;
1445 1510
1446 1511 mutex_enter(&p->p_lock);
1447 1512
1448 1513 p->p_lwprcnt--;
1449 1514
1450 1515 /*
1451 1516 * Check for bail-out conditions as outlined above.
1452 1517 */
1453 1518 if (holdcheck(0) != 0) {
1454 1519 mutex_exit(&p->p_lock);
1455 1520 return (-1);
1456 1521 }
1457 1522
1458 1523 if (!(p->p_flag & SHOLDWATCH)) {
1459 1524 /*
1460 1525 * We are the master watchpoint thread. Set SHOLDWATCH and poke
1461 1526 * the other threads.
1462 1527 */
1463 1528 p->p_flag |= SHOLDWATCH;
1464 1529 pokelwps(p);
1465 1530
1466 1531 /*
1467 1532 * Wait for all threads to be stopped or have TP_WATCHSTOP set.
1468 1533 */
1469 1534 while (pr_allstopped(p, 1) > 0) {
1470 1535 if (holdcheck(SHOLDWATCH) != 0) {
1471 1536 p->p_flag &= ~SHOLDWATCH;
1472 1537 mutex_exit(&p->p_lock);
1473 1538 return (-1);
1474 1539 }
1475 1540
1476 1541 cv_wait(&p->p_holdlwps, &p->p_lock);
1477 1542 }
1478 1543
1479 1544 /*
1480 1545 * All threads are now stopped or in the process of stopping.
1481 1546 * Set SWATCHOK and let them stop completely.
1482 1547 */
1483 1548 p->p_flag |= SWATCHOK;
1484 1549 t->t_proc_flag &= ~TP_WATCHSTOP;
1485 1550 cv_broadcast(&p->p_holdlwps);
1486 1551
1487 1552 while (pr_allstopped(p, 0) > 0) {
1488 1553 /*
1489 1554 * At first glance, it may appear that we don't need a
1490 1555 * call to holdcheck() here. But if the process gets a
1491 1556 * SIGKILL signal, one of our stopped threads may have
1492 1557 * been awakened and is waiting in exitlwps(), which
1493 1558 * takes precedence over watchpoints.
1494 1559 */
1495 1560 if (holdcheck(SHOLDWATCH | SWATCHOK) != 0) {
1496 1561 p->p_flag &= ~(SHOLDWATCH | SWATCHOK);
1497 1562 mutex_exit(&p->p_lock);
1498 1563 return (-1);
1499 1564 }
1500 1565
1501 1566 cv_wait(&p->p_holdlwps, &p->p_lock);
1502 1567 }
1503 1568
1504 1569 /*
1505 1570 * All threads are now completely stopped.
1506 1571 */
1507 1572 p->p_flag &= ~SWATCHOK;
1508 1573 p->p_flag &= ~SHOLDWATCH;
1509 1574 p->p_lwprcnt++;
1510 1575
1511 1576 } else if (!(p->p_flag & SWATCHOK)) {
1512 1577
1513 1578 /*
1514 1579 * SHOLDWATCH is set, so another thread is trying to do
1515 1580 * watchpoint activity. Indicate this thread is stopping, and
1516 1581 * wait for the OK from the master thread.
1517 1582 */
1518 1583 t->t_proc_flag |= TP_WATCHSTOP;
1519 1584 cv_broadcast(&p->p_holdlwps);
1520 1585
1521 1586 while (!(p->p_flag & SWATCHOK)) {
1522 1587 if (holdcheck(0) != 0) {
1523 1588 t->t_proc_flag &= ~TP_WATCHSTOP;
1524 1589 mutex_exit(&p->p_lock);
1525 1590 return (-1);
1526 1591 }
1527 1592
1528 1593 cv_wait(&p->p_holdlwps, &p->p_lock);
1529 1594 }
1530 1595
1531 1596 /*
1532 1597 * Once the master thread has given the OK, this thread can
1533 1598 * actually call stop().
1534 1599 */
1535 1600 t->t_proc_flag &= ~TP_WATCHSTOP;
1536 1601 p->p_lwprcnt++;
1537 1602
1538 1603 stop(PR_SUSPENDED, SUSPEND_NORMAL);
1539 1604
1540 1605 /*
1541 1606 * It's not OK to do watchpoint activity, notify caller to
1542 1607 * retry.
1543 1608 */
1544 1609 ret = -1;
1545 1610
1546 1611 } else {
1547 1612
1548 1613 /*
1549 1614 * The only way we can hit the case where SHOLDWATCH is set and
1550 1615 * SWATCHOK is set is if we are triggering this from within a
1551 1616 * stop() call. Assert that this is the case.
1552 1617 */
1553 1618
1554 1619 ASSERT(t->t_proc_flag & TP_STOPPING);
1555 1620 p->p_lwprcnt++;
1556 1621 }
1557 1622
1558 1623 mutex_exit(&p->p_lock);
1559 1624
1560 1625 return (ret);
1561 1626 }
1562 1627
1563 1628 /*
1564 1629 * force all interruptible lwps to trap into the kernel.
1565 1630 */
1566 1631 void
1567 1632 pokelwps(proc_t *p)
1568 1633 {
1569 1634 kthread_t *t;
1570 1635
1571 1636 ASSERT(MUTEX_HELD(&p->p_lock));
1572 1637
1573 1638 t = p->p_tlist;
1574 1639 do {
1575 1640 if (t == curthread)
1576 1641 continue;
1577 1642 thread_lock(t);
1578 1643 aston(t); /* make thread trap or do post_syscall */
1579 1644 if (ISWAKEABLE(t) || ISWAITING(t)) {
1580 1645 setrun_locked(t);
1581 1646 } else if (t->t_state == TS_STOPPED) {
1582 1647 /*
1583 1648 * Ensure that proc_exit() is not blocked by lwps
1584 1649 * that were stopped via jobcontrol or /proc.
1585 1650 */
1586 1651 if (p->p_flag & SEXITLWPS) {
1587 1652 p->p_stopsig = 0;
1588 1653 t->t_schedflag |= (TS_XSTART | TS_PSTART);
1589 1654 setrun_locked(t);
1590 1655 }
1591 1656 /*
1592 1657 * If we are holding lwps for a forkall(),
1593 1658 * force lwps that have been suspended via
1594 1659 * lwp_suspend() and are suspended inside
1595 1660 * of a system call to proceed to their
1596 1661 * holdlwp() points where they are clonable.
1597 1662 */
1598 1663 if ((p->p_flag & SHOLDFORK) && SUSPENDED(t)) {
1599 1664 if ((t->t_schedflag & TS_CSTART) == 0) {
1600 1665 p->p_lwprcnt++;
1601 1666 t->t_schedflag |= TS_CSTART;
1602 1667 setrun_locked(t);
1603 1668 }
1604 1669 }
1605 1670 } else if (t->t_state == TS_ONPROC) {
1606 1671 if (t->t_cpu != CPU)
1607 1672 poke_cpu(t->t_cpu->cpu_id);
1608 1673 }
1609 1674 thread_unlock(t);
1610 1675 } while ((t = t->t_forw) != p->p_tlist);
1611 1676 }
1612 1677
1613 1678 /*
1614 1679 * undo the effects of holdlwps() or holdwatch().
1615 1680 */
1616 1681 void
1617 1682 continuelwps(proc_t *p)
1618 1683 {
1619 1684 kthread_t *t;
1620 1685
1621 1686 /*
1622 1687 * If this flag is set, then the original holdwatch() didn't actually
1623 1688 * stop the process. See comments for holdwatch().
1624 1689 */
1625 1690 if (p->p_flag & SWATCHOK) {
1626 1691 ASSERT(curthread->t_proc_flag & TP_STOPPING);
1627 1692 return;
1628 1693 }
1629 1694
1630 1695 ASSERT(MUTEX_HELD(&p->p_lock));
1631 1696 ASSERT((p->p_flag & (SHOLDFORK | SHOLDFORK1 | SHOLDWATCH)) == 0);
1632 1697
1633 1698 t = p->p_tlist;
1634 1699 do {
1635 1700 thread_lock(t); /* SUSPENDED looks at t_schedflag */
1636 1701 if (SUSPENDED(t) && !(t->t_proc_flag & TP_HOLDLWP)) {
1637 1702 p->p_lwprcnt++;
1638 1703 t->t_schedflag |= TS_CSTART;
1639 1704 setrun_locked(t);
1640 1705 }
1641 1706 thread_unlock(t);
1642 1707 } while ((t = t->t_forw) != p->p_tlist);
1643 1708 }
1644 1709
1645 1710 /*
1646 1711 * Force all other LWPs in the current process other than the caller to exit,
1647 1712 * and then cv_wait() on p_holdlwps for them to exit. The exitlwps() function
1648 1713 * is typically used in these situations:
1649 1714 *
1650 1715 * (a) prior to an exec() system call
1651 1716 * (b) prior to dumping a core file
1652 1717 * (c) prior to a uadmin() shutdown
1653 1718 *
1654 1719 * If the 'coredump' flag is set, other LWPs are quiesced but not destroyed.
1655 1720 * Multiple threads in the process can call this function at one time by
1656 1721 * triggering execs or core dumps simultaneously, so the SEXITLWPS bit is used
1657 1722 * to declare one particular thread the winner who gets to kill the others.
1658 1723 * If a thread wins the exitlwps() dance, zero is returned; otherwise an
1659 1724 * appropriate errno value is returned to caller for its system call to return.
1660 1725 */
1661 1726 int
1662 1727 exitlwps(int coredump)
1663 1728 {
1664 1729 proc_t *p = curproc;
1665 1730 int heldcnt;
1666 1731
1667 1732 if (curthread->t_door)
1668 1733 door_slam();
1669 1734 if (p->p_door_list)
1670 1735 door_revoke_all();
1671 1736 if (curthread->t_schedctl != NULL)
1672 1737 schedctl_lwp_cleanup(curthread);
1673 1738
1674 1739 /*
1675 1740 * Ensure that before starting to wait for other lwps to exit,
1676 1741 * cleanup all upimutexes held by curthread. Otherwise, some other
1677 1742 * lwp could be waiting (uninterruptibly) for a upimutex held by
1678 1743 * curthread, and the call to pokelwps() below would deadlock.
1679 1744 * Even if a blocked upimutex_lock is made interruptible,
1680 1745 * curthread's upimutexes need to be unlocked: do it here.
1681 1746 */
1682 1747 if (curthread->t_upimutex != NULL)
1683 1748 upimutex_cleanup();
1684 1749
1685 1750 /*
1686 1751 * Grab p_lock in order to check and set SEXITLWPS to declare a winner.
1687 1752 * We must also block any further /proc access from this point forward.
1688 1753 */
1689 1754 mutex_enter(&p->p_lock);
1690 1755 prbarrier(p);
1691 1756
1692 1757 if (p->p_flag & SEXITLWPS) {
1693 1758 mutex_exit(&p->p_lock);
1694 1759 aston(curthread); /* force a trip through post_syscall */
1695 1760 return (set_errno(EINTR));
1696 1761 }
1697 1762
1698 1763 p->p_flag |= SEXITLWPS;
1699 1764 if (coredump) /* tell other lwps to stop, not exit */
1700 1765 p->p_flag |= SCOREDUMP;
1701 1766
1702 1767 /*
1703 1768 * Give precedence to exitlwps() if a holdlwps() is
1704 1769 * in progress. The lwp doing the holdlwps() operation
1705 1770 * is aborted when it is awakened.
1706 1771 */
1707 1772 while (p->p_flag & (SHOLDFORK | SHOLDFORK1 | SHOLDWATCH)) {
1708 1773 cv_broadcast(&p->p_holdlwps);
1709 1774 cv_wait(&p->p_holdlwps, &p->p_lock);
1710 1775 prbarrier(p);
1711 1776 }
1712 1777 p->p_flag |= SHOLDFORK;
1713 1778 pokelwps(p);
1714 1779
1715 1780 /*
1716 1781 * Wait for process to become quiescent.
1717 1782 */
1718 1783 --p->p_lwprcnt;
1719 1784 while (p->p_lwprcnt > 0) {
1720 1785 cv_wait(&p->p_holdlwps, &p->p_lock);
1721 1786 prbarrier(p);
1722 1787 }
1723 1788 p->p_lwprcnt++;
1724 1789 ASSERT(p->p_lwprcnt == 1);
1725 1790
1726 1791 /*
1727 1792 * The SCOREDUMP flag puts the process into a quiescent
1728 1793 * state. The process's lwps remain attached to this
1729 1794 * process until exitlwps() is called again without the
1730 1795 * 'coredump' flag set, then the lwps are terminated
1731 1796 * and the process can exit.
1732 1797 */
1733 1798 if (coredump) {
1734 1799 p->p_flag &= ~(SCOREDUMP | SHOLDFORK | SEXITLWPS);
1735 1800 goto out;
1736 1801 }
1737 1802
1738 1803 /*
1739 1804 * Determine if there are any lwps left dangling in
1740 1805 * the stopped state. This happens when exitlwps()
1741 1806 * aborts a holdlwps() operation.
1742 1807 */
1743 1808 p->p_flag &= ~SHOLDFORK;
1744 1809 if ((heldcnt = p->p_lwpcnt) > 1) {
1745 1810 kthread_t *t;
1746 1811 for (t = curthread->t_forw; --heldcnt > 0; t = t->t_forw) {
1747 1812 t->t_proc_flag &= ~TP_TWAIT;
1748 1813 lwp_continue(t);
1749 1814 }
1750 1815 }
1751 1816
1752 1817 /*
1753 1818 * Wait for all other lwps to exit.
1754 1819 */
1755 1820 --p->p_lwprcnt;
1756 1821 while (p->p_lwpcnt > 1) {
1757 1822 cv_wait(&p->p_holdlwps, &p->p_lock);
1758 1823 prbarrier(p);
1759 1824 }
1760 1825 ++p->p_lwprcnt;
1761 1826 ASSERT(p->p_lwpcnt == 1 && p->p_lwprcnt == 1);
1762 1827
1763 1828 p->p_flag &= ~SEXITLWPS;
1764 1829 curthread->t_proc_flag &= ~TP_TWAIT;
1765 1830
1766 1831 out:
1767 1832 if (!coredump && p->p_zombcnt) { /* cleanup the zombie lwps */
1768 1833 lwpdir_t *ldp;
1769 1834 lwpent_t *lep;
1770 1835 int i;
1771 1836
1772 1837 for (ldp = p->p_lwpdir, i = 0; i < p->p_lwpdir_sz; i++, ldp++) {
1773 1838 lep = ldp->ld_entry;
1774 1839 if (lep != NULL && lep->le_thread != curthread) {
1775 1840 ASSERT(lep->le_thread == NULL);
1776 1841 p->p_zombcnt--;
1777 1842 lwp_hash_out(p, lep->le_lwpid);
1778 1843 }
1779 1844 }
1780 1845 ASSERT(p->p_zombcnt == 0);
1781 1846 }
1782 1847
1783 1848 /*
1784 1849 * If some other LWP in the process wanted us to suspend ourself,
1785 1850 * then we will not do it. The other LWP is now terminated and
1786 1851 * no one will ever continue us again if we suspend ourself.
1787 1852 */
1788 1853 curthread->t_proc_flag &= ~TP_HOLDLWP;
1789 1854 p->p_flag &= ~(SHOLDFORK | SHOLDFORK1 | SHOLDWATCH | SLWPWRAP);
1790 1855 mutex_exit(&p->p_lock);
1791 1856 return (0);
1792 1857 }
1793 1858
1794 1859 /*
1795 1860 * duplicate a lwp.
1796 1861 */
1797 1862 klwp_t *
1798 1863 forklwp(klwp_t *lwp, proc_t *cp, id_t lwpid)
1799 1864 {
1800 1865 klwp_t *clwp;
1801 1866 void *tregs, *tfpu;
1802 1867 kthread_t *t = lwptot(lwp);
1803 1868 kthread_t *ct;
1804 1869 proc_t *p = lwptoproc(lwp);
1805 1870 int cid;
1806 1871 void *bufp;
1807 1872 void *brand_data;
1808 1873 int val;
1809 1874
1810 1875 ASSERT(p == curproc);
1811 1876 ASSERT(t == curthread || (SUSPENDED(t) && lwp->lwp_asleep == 0));
1812 1877
1813 1878 #if defined(__sparc)
1814 1879 if (t == curthread)
1815 1880 (void) flush_user_windows_to_stack(NULL);
1816 1881 #endif
1817 1882
1818 1883 if (t == curthread)
1819 1884 /* copy args out of registers first */
1820 1885 (void) save_syscall_args();
1821 1886
1822 1887 clwp = lwp_create(cp->p_lwpcnt == 0 ? lwp_rtt_initial : lwp_rtt,
1823 1888 NULL, 0, cp, TS_STOPPED, t->t_pri, &t->t_hold, NOCLASS, lwpid);
1824 1889 if (clwp == NULL)
1825 1890 return (NULL);
1826 1891
1827 1892 /*
1828 1893 * most of the parent's lwp can be copied to its duplicate,
1829 1894 * except for the fields that are unique to each lwp, like
1830 1895 * lwp_thread, lwp_procp, lwp_regs, and lwp_ap.
1831 1896 */
1832 1897 ct = clwp->lwp_thread;
1833 1898 tregs = clwp->lwp_regs;
1834 1899 tfpu = clwp->lwp_fpu;
1835 1900 brand_data = clwp->lwp_brand;
1836 1901
1837 1902 /*
1838 1903 * Copy parent lwp to child lwp. Hold child's p_lock to prevent
1839 1904 * mstate_aggr_state() from reading stale mstate entries copied
1840 1905 * from lwp to clwp.
1841 1906 */
1842 1907 mutex_enter(&cp->p_lock);
1843 1908 *clwp = *lwp;
1844 1909
1845 1910 /* clear microstate and resource usage data in new lwp */
1846 1911 init_mstate(ct, LMS_STOPPED);
1847 1912 bzero(&clwp->lwp_ru, sizeof (clwp->lwp_ru));
1848 1913 mutex_exit(&cp->p_lock);
1849 1914
1850 1915 /* fix up child's lwp */
1851 1916
1852 1917 clwp->lwp_pcb.pcb_flags = 0;
1853 1918 #if defined(__sparc)
1854 1919 clwp->lwp_pcb.pcb_step = STEP_NONE;
1855 1920 #endif
1856 1921 clwp->lwp_cursig = 0;
1857 1922 clwp->lwp_extsig = 0;
1858 1923 clwp->lwp_curinfo = (struct sigqueue *)0;
1859 1924 clwp->lwp_thread = ct;
1860 1925 ct->t_sysnum = t->t_sysnum;
1861 1926 clwp->lwp_regs = tregs;
1862 1927 clwp->lwp_fpu = tfpu;
1863 1928 clwp->lwp_brand = brand_data;
1864 1929 clwp->lwp_ap = clwp->lwp_arg;
1865 1930 clwp->lwp_procp = cp;
1866 1931 bzero(clwp->lwp_timer, sizeof (clwp->lwp_timer));
1867 1932 clwp->lwp_lastfault = 0;
1868 1933 clwp->lwp_lastfaddr = 0;
1869 1934
1870 1935 /* copy parent's struct regs to child. */
1871 1936 lwp_forkregs(lwp, clwp);
1872 1937
1873 1938 /*
1874 1939 * Fork thread context ops, if any.
1875 1940 */
1876 1941 if (t->t_ctx)
1877 1942 forkctx(t, ct);
1878 1943
1879 1944 /* fix door state in the child */
1880 1945 if (t->t_door)
1881 1946 door_fork(t, ct);
1882 1947
1883 1948 /* copy current contract templates, clear latest contracts */
1884 1949 lwp_ctmpl_copy(clwp, lwp);
1885 1950
1886 1951 mutex_enter(&cp->p_lock);
1887 1952 /* lwp_create() set the TP_HOLDLWP flag */
1888 1953 if (!(t->t_proc_flag & TP_HOLDLWP))
1889 1954 ct->t_proc_flag &= ~TP_HOLDLWP;
1890 1955 if (cp->p_flag & SMSACCT)
1891 1956 ct->t_proc_flag |= TP_MSACCT;
1892 1957 mutex_exit(&cp->p_lock);
1893 1958
1894 1959 /* Allow brand to propagate brand-specific state */
1895 1960 if (PROC_IS_BRANDED(p))
1896 1961 BROP(p)->b_forklwp(lwp, clwp);
1897 1962
1898 1963 retry:
1899 1964 cid = t->t_cid;
1900 1965
1901 1966 val = CL_ALLOC(&bufp, cid, KM_SLEEP);
1902 1967 ASSERT(val == 0);
1903 1968
1904 1969 mutex_enter(&p->p_lock);
1905 1970 if (cid != t->t_cid) {
1906 1971 /*
1907 1972 * Someone just changed this thread's scheduling class,
1908 1973 * so try pre-allocating the buffer again. Hopefully we
1909 1974 * don't hit this often.
1910 1975 */
1911 1976 mutex_exit(&p->p_lock);
1912 1977 CL_FREE(cid, bufp);
1913 1978 goto retry;
1914 1979 }
1915 1980
1916 1981 ct->t_unpark = t->t_unpark;
1917 1982 ct->t_clfuncs = t->t_clfuncs;
1918 1983 CL_FORK(t, ct, bufp);
1919 1984 ct->t_cid = t->t_cid; /* after data allocated so prgetpsinfo works */
1920 1985 mutex_exit(&p->p_lock);
1921 1986
1922 1987 return (clwp);
1923 1988 }
1924 1989
1925 1990 /*
1926 1991 * Add a new lwp entry to the lwp directory and to the lwpid hash table.
1927 1992 */
1928 1993 void
1929 1994 lwp_hash_in(proc_t *p, lwpent_t *lep, tidhash_t *tidhash, uint_t tidhash_sz,
1930 1995 int do_lock)
1931 1996 {
1932 1997 tidhash_t *thp = &tidhash[TIDHASH(lep->le_lwpid, tidhash_sz)];
1933 1998 lwpdir_t **ldpp;
1934 1999 lwpdir_t *ldp;
1935 2000 kthread_t *t;
1936 2001
1937 2002 /*
1938 2003 * Allocate a directory element from the free list.
1939 2004 * Code elsewhere guarantees a free slot.
1940 2005 */
1941 2006 ldp = p->p_lwpfree;
1942 2007 p->p_lwpfree = ldp->ld_next;
1943 2008 ASSERT(ldp->ld_entry == NULL);
1944 2009 ldp->ld_entry = lep;
1945 2010
1946 2011 if (do_lock)
1947 2012 mutex_enter(&thp->th_lock);
1948 2013
1949 2014 /*
1950 2015 * Insert it into the lwpid hash table.
1951 2016 */
1952 2017 ldpp = &thp->th_list;
1953 2018 ldp->ld_next = *ldpp;
1954 2019 *ldpp = ldp;
1955 2020
1956 2021 /*
1957 2022 * Set the active thread's directory slot entry.
1958 2023 */
1959 2024 if ((t = lep->le_thread) != NULL) {
1960 2025 ASSERT(lep->le_lwpid == t->t_tid);
1961 2026 t->t_dslot = (int)(ldp - p->p_lwpdir);
1962 2027 }
1963 2028
1964 2029 if (do_lock)
1965 2030 mutex_exit(&thp->th_lock);
1966 2031 }
1967 2032
1968 2033 /*
1969 2034 * Remove an lwp from the lwpid hash table and free its directory entry.
1970 2035 * This is done when a detached lwp exits in lwp_exit() or
1971 2036 * when a non-detached lwp is waited for in lwp_wait() or
1972 2037 * when a zombie lwp is detached in lwp_detach().
1973 2038 */
1974 2039 void
1975 2040 lwp_hash_out(proc_t *p, id_t lwpid)
1976 2041 {
1977 2042 tidhash_t *thp = &p->p_tidhash[TIDHASH(lwpid, p->p_tidhash_sz)];
1978 2043 lwpdir_t **ldpp;
1979 2044 lwpdir_t *ldp;
1980 2045 lwpent_t *lep;
1981 2046
1982 2047 mutex_enter(&thp->th_lock);
1983 2048 for (ldpp = &thp->th_list;
1984 2049 (ldp = *ldpp) != NULL; ldpp = &ldp->ld_next) {
1985 2050 lep = ldp->ld_entry;
1986 2051 if (lep->le_lwpid == lwpid) {
1987 2052 prlwpfree(p, lep); /* /proc deals with le_trace */
1988 2053 *ldpp = ldp->ld_next;
1989 2054 ldp->ld_entry = NULL;
1990 2055 ldp->ld_next = p->p_lwpfree;
1991 2056 p->p_lwpfree = ldp;
1992 2057 kmem_free(lep, sizeof (*lep));
1993 2058 break;
1994 2059 }
1995 2060 }
1996 2061 mutex_exit(&thp->th_lock);
1997 2062 }
1998 2063
1999 2064 /*
2000 2065 * Lookup an lwp in the lwpid hash table by lwpid.
2001 2066 */
2002 2067 lwpdir_t *
2003 2068 lwp_hash_lookup(proc_t *p, id_t lwpid)
2004 2069 {
2005 2070 tidhash_t *thp;
2006 2071 lwpdir_t *ldp;
2007 2072
2008 2073 /*
2009 2074 * The process may be exiting, after p_tidhash has been set to NULL in
2010 2075 * proc_exit() but before prfee() has been called. Return failure in
2011 2076 * this case.
2012 2077 */
2013 2078 if (p->p_tidhash == NULL)
2014 2079 return (NULL);
2015 2080
2016 2081 thp = &p->p_tidhash[TIDHASH(lwpid, p->p_tidhash_sz)];
2017 2082 for (ldp = thp->th_list; ldp != NULL; ldp = ldp->ld_next) {
2018 2083 if (ldp->ld_entry->le_lwpid == lwpid)
2019 2084 return (ldp);
2020 2085 }
2021 2086
2022 2087 return (NULL);
2023 2088 }
2024 2089
2025 2090 /*
2026 2091 * Same as lwp_hash_lookup(), but acquire and return
2027 2092 * the tid hash table entry lock on success.
2028 2093 */
2029 2094 lwpdir_t *
2030 2095 lwp_hash_lookup_and_lock(proc_t *p, id_t lwpid, kmutex_t **mpp)
2031 2096 {
2032 2097 tidhash_t *tidhash;
2033 2098 uint_t tidhash_sz;
2034 2099 tidhash_t *thp;
2035 2100 lwpdir_t *ldp;
2036 2101
2037 2102 top:
2038 2103 tidhash_sz = p->p_tidhash_sz;
2039 2104 membar_consumer();
2040 2105 if ((tidhash = p->p_tidhash) == NULL)
2041 2106 return (NULL);
2042 2107
2043 2108 thp = &tidhash[TIDHASH(lwpid, tidhash_sz)];
2044 2109 mutex_enter(&thp->th_lock);
2045 2110
2046 2111 /*
2047 2112 * Since we are not holding p->p_lock, the tid hash table
2048 2113 * may have changed. If so, start over. If not, then
2049 2114 * it cannot change until after we drop &thp->th_lock;
2050 2115 */
2051 2116 if (tidhash != p->p_tidhash || tidhash_sz != p->p_tidhash_sz) {
2052 2117 mutex_exit(&thp->th_lock);
2053 2118 goto top;
2054 2119 }
2055 2120
2056 2121 for (ldp = thp->th_list; ldp != NULL; ldp = ldp->ld_next) {
2057 2122 if (ldp->ld_entry->le_lwpid == lwpid) {
2058 2123 *mpp = &thp->th_lock;
2059 2124 return (ldp);
2060 2125 }
2061 2126 }
2062 2127
2063 2128 mutex_exit(&thp->th_lock);
2064 2129 return (NULL);
2065 2130 }
2066 2131
2067 2132 /*
2068 2133 * Update the indicated LWP usage statistic for the current LWP.
2069 2134 */
2070 2135 void
2071 2136 lwp_stat_update(lwp_stat_id_t lwp_stat_id, long inc)
2072 2137 {
2073 2138 klwp_t *lwp = ttolwp(curthread);
2074 2139
2075 2140 if (lwp == NULL)
2076 2141 return;
2077 2142
2078 2143 switch (lwp_stat_id) {
2079 2144 case LWP_STAT_INBLK:
2080 2145 lwp->lwp_ru.inblock += inc;
2081 2146 break;
2082 2147 case LWP_STAT_OUBLK:
2083 2148 lwp->lwp_ru.oublock += inc;
2084 2149 break;
2085 2150 case LWP_STAT_MSGRCV:
2086 2151 lwp->lwp_ru.msgrcv += inc;
2087 2152 break;
2088 2153 case LWP_STAT_MSGSND:
2089 2154 lwp->lwp_ru.msgsnd += inc;
2090 2155 break;
2091 2156 default:
2092 2157 panic("lwp_stat_update: invalid lwp_stat_id 0x%x", lwp_stat_id);
2093 2158 }
2094 2159 }
|
↓ open down ↓ |
980 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX