xref: /onnv-gate/usr/src/uts/common/os/task.c (revision 5788:9b0eb06889bb)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53247Sgjelinek  * Common Development and Distribution License (the "License").
63247Sgjelinek  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*5788Smv143129  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/atomic.h>
290Sstevel@tonic-gate #include <sys/cmn_err.h>
300Sstevel@tonic-gate #include <sys/exacct.h>
310Sstevel@tonic-gate #include <sys/id_space.h>
320Sstevel@tonic-gate #include <sys/kmem.h>
330Sstevel@tonic-gate #include <sys/modhash.h>
340Sstevel@tonic-gate #include <sys/mutex.h>
350Sstevel@tonic-gate #include <sys/proc.h>
360Sstevel@tonic-gate #include <sys/project.h>
370Sstevel@tonic-gate #include <sys/rctl.h>
380Sstevel@tonic-gate #include <sys/systm.h>
390Sstevel@tonic-gate #include <sys/task.h>
400Sstevel@tonic-gate #include <sys/time.h>
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <sys/zone.h>
430Sstevel@tonic-gate #include <sys/cpuvar.h>
440Sstevel@tonic-gate #include <sys/fss.h>
450Sstevel@tonic-gate #include <sys/class.h>
460Sstevel@tonic-gate #include <sys/project.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * Tasks
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  *   A task is a collection of processes, associated with a common project ID
520Sstevel@tonic-gate  *   and related by a common initial parent.  The task primarily represents a
530Sstevel@tonic-gate  *   natural process sequence with known resource usage, although it can also be
540Sstevel@tonic-gate  *   viewed as a convenient grouping of processes for signal delivery, processor
550Sstevel@tonic-gate  *   binding, and administrative operations.
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  * Membership and observership
580Sstevel@tonic-gate  *   We can conceive of situations where processes outside of the task may wish
590Sstevel@tonic-gate  *   to examine the resource usage of the task.  Similarly, a number of the
600Sstevel@tonic-gate  *   administrative operations on a task can be performed by processes who are
610Sstevel@tonic-gate  *   not members of the task.  Accordingly, we must design a locking strategy
620Sstevel@tonic-gate  *   where observers of the task, who wish to examine or operate on the task,
630Sstevel@tonic-gate  *   and members of task, who can perform the mentioned operations, as well as
640Sstevel@tonic-gate  *   leave the task, see a consistent and correct representation of the task at
650Sstevel@tonic-gate  *   all times.
660Sstevel@tonic-gate  *
670Sstevel@tonic-gate  * Locking
680Sstevel@tonic-gate  *   Because the task membership is a new relation between processes, its
690Sstevel@tonic-gate  *   locking becomes an additional responsibility of the pidlock/p_lock locking
700Sstevel@tonic-gate  *   sequence; however, tasks closely resemble sessions and the session locking
710Sstevel@tonic-gate  *   model is mostly appropriate for the interaction of tasks, processes, and
720Sstevel@tonic-gate  *   procfs.
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  *   kmutex_t task_hash_lock
750Sstevel@tonic-gate  *     task_hash_lock is a global lock protecting the contents of the task
760Sstevel@tonic-gate  *     ID-to-task pointer hash.  Holders of task_hash_lock must not attempt to
770Sstevel@tonic-gate  *     acquire pidlock or p_lock.
780Sstevel@tonic-gate  *   uint_t tk_hold_count
790Sstevel@tonic-gate  *     tk_hold_count, the number of members and observers of the current task,
800Sstevel@tonic-gate  *     must be manipulated atomically.
810Sstevel@tonic-gate  *   proc_t *tk_memb_list
820Sstevel@tonic-gate  *   proc_t *p_tasknext
830Sstevel@tonic-gate  *   proc_t *p_taskprev
840Sstevel@tonic-gate  *     The task's membership list is protected by pidlock, and is therefore
850Sstevel@tonic-gate  *     always acquired before any of its members' p_lock mutexes.  The p_task
860Sstevel@tonic-gate  *     member of the proc structure is protected by pidlock or p_lock for
870Sstevel@tonic-gate  *     reading, and by both pidlock and p_lock for modification, as is done for
880Sstevel@tonic-gate  *     p_sessp.  The key point is that only the process can modify its p_task,
890Sstevel@tonic-gate  *     and not any entity on the system.  (/proc will use prlock() to prevent
900Sstevel@tonic-gate  *     the process from leaving, as opposed to pidlock.)
910Sstevel@tonic-gate  *   kmutex_t tk_usage_lock
920Sstevel@tonic-gate  *     tk_usage_lock is a per-task lock protecting the contents of the task
930Sstevel@tonic-gate  *     usage structure and tk_nlwps counter for the task.max-lwps resource
940Sstevel@tonic-gate  *     control.
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate int task_hash_size = 256;
980Sstevel@tonic-gate static kmutex_t task_hash_lock;
990Sstevel@tonic-gate static mod_hash_t *task_hash;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static id_space_t *taskid_space;	/* global taskid space */
1020Sstevel@tonic-gate static kmem_cache_t *task_cache;	/* kmem cache for task structures */
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate rctl_hndl_t rc_task_lwps;
1050Sstevel@tonic-gate rctl_hndl_t rc_task_cpu_time;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate  * static rctl_qty_t task_usage_lwps(void *taskp)
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  * Overview
1110Sstevel@tonic-gate  *   task_usage_lwps() is the usage operation for the resource control
1120Sstevel@tonic-gate  *   associated with the number of LWPs in a task.
1130Sstevel@tonic-gate  *
1140Sstevel@tonic-gate  * Return values
1150Sstevel@tonic-gate  *   The number of LWPs in the given task is returned.
1160Sstevel@tonic-gate  *
1170Sstevel@tonic-gate  * Caller's context
1180Sstevel@tonic-gate  *   The p->p_lock must be held across the call.
1190Sstevel@tonic-gate  */
1200Sstevel@tonic-gate /*ARGSUSED*/
1210Sstevel@tonic-gate static rctl_qty_t
1220Sstevel@tonic-gate task_lwps_usage(rctl_t *r, proc_t *p)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	task_t *t;
1250Sstevel@tonic-gate 	rctl_qty_t nlwps;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	t = p->p_task;
1300Sstevel@tonic-gate 	mutex_enter(&p->p_zone->zone_nlwps_lock);
1310Sstevel@tonic-gate 	nlwps = t->tk_nlwps;
1320Sstevel@tonic-gate 	mutex_exit(&p->p_zone->zone_nlwps_lock);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	return (nlwps);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate  * static int task_test_lwps(void *taskp, rctl_val_t *, int64_t incr,
1390Sstevel@tonic-gate  *   int flags)
1400Sstevel@tonic-gate  *
1410Sstevel@tonic-gate  * Overview
1420Sstevel@tonic-gate  *   task_test_lwps() is the test-if-valid-increment for the resource control
1430Sstevel@tonic-gate  *   for the number of processes in a task.
1440Sstevel@tonic-gate  *
1450Sstevel@tonic-gate  * Return values
1460Sstevel@tonic-gate  *   0 if the threshold limit was not passed, 1 if the limit was passed.
1470Sstevel@tonic-gate  *
1480Sstevel@tonic-gate  * Caller's context
1490Sstevel@tonic-gate  *   p->p_lock must be held across the call.
1500Sstevel@tonic-gate  */
1510Sstevel@tonic-gate /*ARGSUSED*/
1520Sstevel@tonic-gate static int
1530Sstevel@tonic-gate task_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
1540Sstevel@tonic-gate     rctl_qty_t incr,
1550Sstevel@tonic-gate     uint_t flags)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	rctl_qty_t nlwps;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
1600Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_TASK);
1610Sstevel@tonic-gate 	if (e->rcep_p.task == NULL)
1620Sstevel@tonic-gate 		return (0);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(e->rcep_p.task->tk_zone->zone_nlwps_lock)));
1650Sstevel@tonic-gate 	nlwps = e->rcep_p.task->tk_nlwps;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if (nlwps + incr > rcntl->rcv_value)
1680Sstevel@tonic-gate 		return (1);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	return (0);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate /*ARGSUSED*/
1730Sstevel@tonic-gate static int
1740Sstevel@tonic-gate task_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv) {
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
1770Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_TASK);
1780Sstevel@tonic-gate 	if (e->rcep_p.task == NULL)
1790Sstevel@tonic-gate 		return (0);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	e->rcep_p.task->tk_nlwps_ctl = nv;
1820Sstevel@tonic-gate 	return (0);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate  * static rctl_qty_t task_usage_cpu_secs(void *taskp)
1870Sstevel@tonic-gate  *
1880Sstevel@tonic-gate  * Overview
1890Sstevel@tonic-gate  *   task_usage_cpu_secs() is the usage operation for the resource control
1900Sstevel@tonic-gate  *   associated with the total accrued CPU seconds for a task.
1910Sstevel@tonic-gate  *
1920Sstevel@tonic-gate  * Return values
1930Sstevel@tonic-gate  *   The number of CPU seconds consumed by the task is returned.
1940Sstevel@tonic-gate  *
1950Sstevel@tonic-gate  * Caller's context
1960Sstevel@tonic-gate  *   The given task must be held across the call.
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate /*ARGSUSED*/
1990Sstevel@tonic-gate static rctl_qty_t
2000Sstevel@tonic-gate task_cpu_time_usage(rctl_t *r, proc_t *p)
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate 	task_t *t = p->p_task;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
205*5788Smv143129 	return (t->tk_cpu_time);
206*5788Smv143129 }
207*5788Smv143129 
208*5788Smv143129 /*
209*5788Smv143129  * int task_cpu_time_incr(task_t *t, rctl_qty_t incr)
210*5788Smv143129  *
211*5788Smv143129  * Overview
212*5788Smv143129  *   task_cpu_time_incr() increments the amount of CPU time used
213*5788Smv143129  *   by this task.
214*5788Smv143129  *
215*5788Smv143129  * Return values
216*5788Smv143129  *   1   if a second or more time is accumulated
217*5788Smv143129  *   0   otherwise
218*5788Smv143129  *
219*5788Smv143129  * Caller's context
220*5788Smv143129  *   This is called by the clock tick accounting function to charge
221*5788Smv143129  *   CPU time to a task.
222*5788Smv143129  */
223*5788Smv143129 rctl_qty_t
224*5788Smv143129 task_cpu_time_incr(task_t *t, rctl_qty_t incr)
225*5788Smv143129 {
226*5788Smv143129 	rctl_qty_t ret = 0;
227*5788Smv143129 
228*5788Smv143129 	mutex_enter(&t->tk_cpu_time_lock);
229*5788Smv143129 	t->tk_cpu_ticks += incr;
230*5788Smv143129 	if (t->tk_cpu_ticks >= hz) {
231*5788Smv143129 		t->tk_cpu_time += t->tk_cpu_ticks / hz;
232*5788Smv143129 		t->tk_cpu_ticks = t->tk_cpu_ticks % hz;
233*5788Smv143129 		ret = t->tk_cpu_time;
234*5788Smv143129 	}
235*5788Smv143129 	mutex_exit(&t->tk_cpu_time_lock);
236*5788Smv143129 
237*5788Smv143129 	return (ret);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate /*
2410Sstevel@tonic-gate  * static int task_test_cpu_secs(void *taskp, rctl_val_t *, int64_t incr,
2420Sstevel@tonic-gate  *   int flags)
2430Sstevel@tonic-gate  *
2440Sstevel@tonic-gate  * Overview
2450Sstevel@tonic-gate  *   task_test_cpu_secs() is the test-if-valid-increment for the resource
2460Sstevel@tonic-gate  *   control for the total accrued CPU seconds for a task.
2470Sstevel@tonic-gate  *
2480Sstevel@tonic-gate  * Return values
2490Sstevel@tonic-gate  *   0 if the threshold limit was not passed, 1 if the limit was passed.
2500Sstevel@tonic-gate  *
2510Sstevel@tonic-gate  * Caller's context
2520Sstevel@tonic-gate  *   The given task must be held across the call.
2530Sstevel@tonic-gate  */
2540Sstevel@tonic-gate /*ARGSUSED*/
2550Sstevel@tonic-gate static int
2560Sstevel@tonic-gate task_cpu_time_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
2570Sstevel@tonic-gate     struct rctl_val *rcntl, rctl_qty_t incr, uint_t flags)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
2600Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_TASK);
2610Sstevel@tonic-gate 	if (e->rcep_p.task == NULL)
2620Sstevel@tonic-gate 		return (0);
2630Sstevel@tonic-gate 
264*5788Smv143129 	if (incr >= rcntl->rcv_value)
2650Sstevel@tonic-gate 		return (1);
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	return (0);
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate static task_t *
2710Sstevel@tonic-gate task_find(taskid_t id, zoneid_t zoneid)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	task_t *tk;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&task_hash_lock));
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	if (mod_hash_find(task_hash, (mod_hash_key_t)(uintptr_t)id,
2780Sstevel@tonic-gate 	    (mod_hash_val_t *)&tk) == MH_ERR_NOTFOUND ||
2790Sstevel@tonic-gate 	    (zoneid != ALL_ZONES && zoneid != tk->tk_zone->zone_id))
2800Sstevel@tonic-gate 		return (NULL);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	return (tk);
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate /*
2860Sstevel@tonic-gate  * task_hold_by_id(), task_hold_by_id_zone()
2870Sstevel@tonic-gate  *
2880Sstevel@tonic-gate  * Overview
2890Sstevel@tonic-gate  *   task_hold_by_id() is used to take a reference on a task by its task id,
2900Sstevel@tonic-gate  *   supporting the various system call interfaces for obtaining resource data,
2910Sstevel@tonic-gate  *   delivering signals, and so forth.
2920Sstevel@tonic-gate  *
2930Sstevel@tonic-gate  * Return values
2940Sstevel@tonic-gate  *   Returns a pointer to the task_t with taskid_t id.  The task is returned
2950Sstevel@tonic-gate  *   with its hold count incremented by one.  Returns NULL if there
2960Sstevel@tonic-gate  *   is no task with the requested id.
2970Sstevel@tonic-gate  *
2980Sstevel@tonic-gate  * Caller's context
2990Sstevel@tonic-gate  *   Caller must not be holding task_hash_lock.  No restrictions on context.
3000Sstevel@tonic-gate  */
3010Sstevel@tonic-gate task_t *
3020Sstevel@tonic-gate task_hold_by_id_zone(taskid_t id, zoneid_t zoneid)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	task_t *tk;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	mutex_enter(&task_hash_lock);
3070Sstevel@tonic-gate 	if ((tk = task_find(id, zoneid)) != NULL)
3080Sstevel@tonic-gate 		atomic_add_32(&tk->tk_hold_count, 1);
3090Sstevel@tonic-gate 	mutex_exit(&task_hash_lock);
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	return (tk);
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate task_t *
3150Sstevel@tonic-gate task_hold_by_id(taskid_t id)
3160Sstevel@tonic-gate {
3170Sstevel@tonic-gate 	zoneid_t zoneid;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	if (INGLOBALZONE(curproc))
3200Sstevel@tonic-gate 		zoneid = ALL_ZONES;
3210Sstevel@tonic-gate 	else
3220Sstevel@tonic-gate 		zoneid = getzoneid();
3230Sstevel@tonic-gate 	return (task_hold_by_id_zone(id, zoneid));
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate  * void task_hold(task_t *)
3280Sstevel@tonic-gate  *
3290Sstevel@tonic-gate  * Overview
3300Sstevel@tonic-gate  *   task_hold() is used to take an additional reference to the given task.
3310Sstevel@tonic-gate  *
3320Sstevel@tonic-gate  * Return values
3330Sstevel@tonic-gate  *   None.
3340Sstevel@tonic-gate  *
3350Sstevel@tonic-gate  * Caller's context
3360Sstevel@tonic-gate  *   No restriction on context.
3370Sstevel@tonic-gate  */
3380Sstevel@tonic-gate void
3390Sstevel@tonic-gate task_hold(task_t *tk)
3400Sstevel@tonic-gate {
3410Sstevel@tonic-gate 	atomic_add_32(&tk->tk_hold_count, 1);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate /*
3450Sstevel@tonic-gate  * void task_rele(task_t *)
3460Sstevel@tonic-gate  *
3470Sstevel@tonic-gate  * Overview
3480Sstevel@tonic-gate  *   task_rele() relinquishes a reference on the given task, which was acquired
3490Sstevel@tonic-gate  *   via task_hold() or task_hold_by_id().  If this is the last member or
3500Sstevel@tonic-gate  *   observer of the task, dispatch it for commitment via the accounting
3510Sstevel@tonic-gate  *   subsystem.
3520Sstevel@tonic-gate  *
3530Sstevel@tonic-gate  * Return values
3540Sstevel@tonic-gate  *   None.
3550Sstevel@tonic-gate  *
3560Sstevel@tonic-gate  * Caller's context
3570Sstevel@tonic-gate  *   Caller must not be holding the task_hash_lock.
3580Sstevel@tonic-gate  *   Caller's context must be acceptable for KM_SLEEP allocations.
3590Sstevel@tonic-gate  */
3600Sstevel@tonic-gate void
3610Sstevel@tonic-gate task_rele(task_t *tk)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	mutex_enter(&task_hash_lock);
3640Sstevel@tonic-gate 	if (atomic_add_32_nv(&tk->tk_hold_count, -1) > 0) {
3650Sstevel@tonic-gate 		mutex_exit(&task_hash_lock);
3660Sstevel@tonic-gate 		return;
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	mutex_enter(&tk->tk_zone->zone_nlwps_lock);
3700Sstevel@tonic-gate 	tk->tk_proj->kpj_ntasks--;
3710Sstevel@tonic-gate 	mutex_exit(&tk->tk_zone->zone_nlwps_lock);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	if (mod_hash_destroy(task_hash,
3740Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)tk->tk_tkid) != 0)
3750Sstevel@tonic-gate 		panic("unable to delete task %d", tk->tk_tkid);
3760Sstevel@tonic-gate 	mutex_exit(&task_hash_lock);
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	/*
3790Sstevel@tonic-gate 	 * At this point, there are no members or observers of the task, so we
3800Sstevel@tonic-gate 	 * can safely send it on for commitment to the accounting subsystem.
3810Sstevel@tonic-gate 	 * The task will be destroyed in task_end() subsequent to commitment.
3820Sstevel@tonic-gate 	 */
3830Sstevel@tonic-gate 	(void) taskq_dispatch(exacct_queue, exacct_commit_task, tk, KM_SLEEP);
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate /*
3870Sstevel@tonic-gate  * task_t *task_create(projid_t, zone *)
3880Sstevel@tonic-gate  *
3890Sstevel@tonic-gate  * Overview
3900Sstevel@tonic-gate  *   A process constructing a new task calls task_create() to construct and
3910Sstevel@tonic-gate  *   preinitialize the task for the appropriate destination project.  Only one
3920Sstevel@tonic-gate  *   task, the primordial task0, is not created with task_create().
3930Sstevel@tonic-gate  *
3940Sstevel@tonic-gate  * Return values
3950Sstevel@tonic-gate  *   None.
3960Sstevel@tonic-gate  *
3970Sstevel@tonic-gate  * Caller's context
3980Sstevel@tonic-gate  *   Caller's context should be safe for KM_SLEEP allocations.
3990Sstevel@tonic-gate  *   The caller should appropriately bump the kpj_ntasks counter on the
4000Sstevel@tonic-gate  *   project that contains this task.
4010Sstevel@tonic-gate  */
4020Sstevel@tonic-gate task_t *
4030Sstevel@tonic-gate task_create(projid_t projid, zone_t *zone)
4040Sstevel@tonic-gate {
4050Sstevel@tonic-gate 	task_t *tk = kmem_cache_alloc(task_cache, KM_SLEEP);
4060Sstevel@tonic-gate 	task_t *ancestor_tk;
4070Sstevel@tonic-gate 	taskid_t tkid;
4080Sstevel@tonic-gate 	task_usage_t *tu = kmem_zalloc(sizeof (task_usage_t), KM_SLEEP);
4090Sstevel@tonic-gate 	mod_hash_hndl_t hndl;
4100Sstevel@tonic-gate 	rctl_set_t *set = rctl_set_create();
4110Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
4120Sstevel@tonic-gate 	rctl_entity_p_t e;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	bzero(tk, sizeof (task_t));
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	tk->tk_tkid = tkid = id_alloc(taskid_space);
4170Sstevel@tonic-gate 	tk->tk_nlwps = 0;
4180Sstevel@tonic-gate 	tk->tk_nlwps_ctl = INT_MAX;
4190Sstevel@tonic-gate 	tk->tk_usage = tu;
4204584Srh87107 	tk->tk_inherited = kmem_zalloc(sizeof (task_usage_t), KM_SLEEP);
4213792Sakolb 	tk->tk_proj = project_hold_by_id(projid, zone, PROJECT_HOLD_INSERT);
4220Sstevel@tonic-gate 	tk->tk_flags = TASK_NORMAL;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	/*
4250Sstevel@tonic-gate 	 * Copy ancestor task's resource controls.
4260Sstevel@tonic-gate 	 */
4270Sstevel@tonic-gate 	zone_task_hold(zone);
4280Sstevel@tonic-gate 	mutex_enter(&curproc->p_lock);
4290Sstevel@tonic-gate 	ancestor_tk = curproc->p_task;
4300Sstevel@tonic-gate 	task_hold(ancestor_tk);
4310Sstevel@tonic-gate 	tk->tk_zone = zone;
4320Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	for (;;) {
4350Sstevel@tonic-gate 		gp = rctl_set_dup_prealloc(ancestor_tk->tk_rctls);
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 		mutex_enter(&ancestor_tk->tk_rctls->rcs_lock);
4380Sstevel@tonic-gate 		if (rctl_set_dup_ready(ancestor_tk->tk_rctls, gp))
4390Sstevel@tonic-gate 			break;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 		mutex_exit(&ancestor_tk->tk_rctls->rcs_lock);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 		rctl_prealloc_destroy(gp);
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	/*
4470Sstevel@tonic-gate 	 * At this point, curproc does not have the appropriate linkage
4480Sstevel@tonic-gate 	 * through the task to the project. So, rctl_set_dup should only
4490Sstevel@tonic-gate 	 * copy the rctls, and leave the callbacks for later.
4500Sstevel@tonic-gate 	 */
4510Sstevel@tonic-gate 	e.rcep_p.task = tk;
4520Sstevel@tonic-gate 	e.rcep_t = RCENTITY_TASK;
4530Sstevel@tonic-gate 	tk->tk_rctls = rctl_set_dup(ancestor_tk->tk_rctls, curproc, curproc, &e,
4540Sstevel@tonic-gate 	    set, gp, RCD_DUP);
4550Sstevel@tonic-gate 	mutex_exit(&ancestor_tk->tk_rctls->rcs_lock);
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	/*
4600Sstevel@tonic-gate 	 * Record the ancestor task's ID for use by extended accounting.
4610Sstevel@tonic-gate 	 */
4620Sstevel@tonic-gate 	tu->tu_anctaskid = ancestor_tk->tk_tkid;
4630Sstevel@tonic-gate 	task_rele(ancestor_tk);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	/*
4660Sstevel@tonic-gate 	 * Put new task structure in the hash table.
4670Sstevel@tonic-gate 	 */
4680Sstevel@tonic-gate 	(void) mod_hash_reserve(task_hash, &hndl);
4690Sstevel@tonic-gate 	mutex_enter(&task_hash_lock);
4700Sstevel@tonic-gate 	ASSERT(task_find(tkid, getzoneid()) == NULL);
4710Sstevel@tonic-gate 	if (mod_hash_insert_reserve(task_hash, (mod_hash_key_t)(uintptr_t)tkid,
4720Sstevel@tonic-gate 	    (mod_hash_val_t *)tk, hndl) != 0) {
4730Sstevel@tonic-gate 		mod_hash_cancel(task_hash, &hndl);
4740Sstevel@tonic-gate 		panic("unable to insert task %d(%p)", tkid, (void *)tk);
4750Sstevel@tonic-gate 	}
4760Sstevel@tonic-gate 	mutex_exit(&task_hash_lock);
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	return (tk);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate /*
4820Sstevel@tonic-gate  * void task_attach(task_t *, proc_t *)
4830Sstevel@tonic-gate  *
4840Sstevel@tonic-gate  * Overview
4850Sstevel@tonic-gate  *   task_attach() is used to attach a process to a task; this operation is only
4860Sstevel@tonic-gate  *   performed as a result of a fork() or settaskid() system call.  The proc_t's
4870Sstevel@tonic-gate  *   p_tasknext and p_taskprev fields will be set such that the proc_t is a
4880Sstevel@tonic-gate  *   member of the doubly-linked list of proc_t's that make up the task.
4890Sstevel@tonic-gate  *
4900Sstevel@tonic-gate  * Return values
4910Sstevel@tonic-gate  *   None.
4920Sstevel@tonic-gate  *
4930Sstevel@tonic-gate  * Caller's context
4940Sstevel@tonic-gate  *   pidlock and p->p_lock must be held on entry.
4950Sstevel@tonic-gate  */
4960Sstevel@tonic-gate void
4970Sstevel@tonic-gate task_attach(task_t *tk, proc_t *p)
4980Sstevel@tonic-gate {
4990Sstevel@tonic-gate 	proc_t *first, *prev;
5000Sstevel@tonic-gate 	rctl_entity_p_t e;
5010Sstevel@tonic-gate 	ASSERT(tk != NULL);
5020Sstevel@tonic-gate 	ASSERT(p != NULL);
5030Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
5040Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	if (tk->tk_memb_list == NULL) {
5070Sstevel@tonic-gate 		p->p_tasknext = p;
5080Sstevel@tonic-gate 		p->p_taskprev = p;
5090Sstevel@tonic-gate 	} else {
5100Sstevel@tonic-gate 		first = tk->tk_memb_list;
5110Sstevel@tonic-gate 		prev = first->p_taskprev;
5120Sstevel@tonic-gate 		first->p_taskprev = p;
5130Sstevel@tonic-gate 		p->p_tasknext = first;
5140Sstevel@tonic-gate 		p->p_taskprev = prev;
5150Sstevel@tonic-gate 		prev->p_tasknext = p;
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate 	tk->tk_memb_list = p;
5180Sstevel@tonic-gate 	task_hold(tk);
5190Sstevel@tonic-gate 	p->p_task = tk;
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	/*
5220Sstevel@tonic-gate 	 * Now that the linkage from process to task and project is
5230Sstevel@tonic-gate 	 * complete, do the required callbacks for the task and project
5240Sstevel@tonic-gate 	 * rctl sets.
5250Sstevel@tonic-gate 	 */
5260Sstevel@tonic-gate 	e.rcep_p.proj = tk->tk_proj;
5270Sstevel@tonic-gate 	e.rcep_t = RCENTITY_PROJECT;
5280Sstevel@tonic-gate 	(void) rctl_set_dup(NULL, NULL, p, &e, tk->tk_proj->kpj_rctls, NULL,
5290Sstevel@tonic-gate 	    RCD_CALLBACK);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	e.rcep_p.task = tk;
5320Sstevel@tonic-gate 	e.rcep_t = RCENTITY_TASK;
5330Sstevel@tonic-gate 	(void) rctl_set_dup(NULL, NULL, p, &e, tk->tk_rctls, NULL,
5340Sstevel@tonic-gate 	    RCD_CALLBACK);
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate /*
5390Sstevel@tonic-gate  * task_begin()
5400Sstevel@tonic-gate  *
5410Sstevel@tonic-gate  * Overview
5420Sstevel@tonic-gate  *   A process constructing a new task calls task_begin() to initialize the
5430Sstevel@tonic-gate  *   task, by attaching itself as a member.
5440Sstevel@tonic-gate  *
5450Sstevel@tonic-gate  * Return values
5460Sstevel@tonic-gate  *   None.
5470Sstevel@tonic-gate  *
5480Sstevel@tonic-gate  * Caller's context
5490Sstevel@tonic-gate  *   pidlock and p_lock must be held across the call to task_begin().
5500Sstevel@tonic-gate  */
5510Sstevel@tonic-gate void
5520Sstevel@tonic-gate task_begin(task_t *tk, proc_t *p)
5530Sstevel@tonic-gate {
5540Sstevel@tonic-gate 	timestruc_t ts;
5550Sstevel@tonic-gate 	task_usage_t *tu;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
5580Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	mutex_enter(&tk->tk_usage_lock);
5610Sstevel@tonic-gate 	tu = tk->tk_usage;
5620Sstevel@tonic-gate 	gethrestime(&ts);
5630Sstevel@tonic-gate 	tu->tu_startsec = (uint64_t)ts.tv_sec;
5640Sstevel@tonic-gate 	tu->tu_startnsec = (uint64_t)ts.tv_nsec;
5650Sstevel@tonic-gate 	mutex_exit(&tk->tk_usage_lock);
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	/*
5680Sstevel@tonic-gate 	 * Join process to the task as a member.
5690Sstevel@tonic-gate 	 */
5700Sstevel@tonic-gate 	task_attach(tk, p);
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate /*
5740Sstevel@tonic-gate  * void task_detach(proc_t *)
5750Sstevel@tonic-gate  *
5760Sstevel@tonic-gate  * Overview
5770Sstevel@tonic-gate  *   task_detach() removes the specified process from its task.  task_detach
5780Sstevel@tonic-gate  *   sets the process's task membership to NULL, in anticipation of a final exit
5790Sstevel@tonic-gate  *   or of joining a new task.  Because task_rele() requires a context safe for
5800Sstevel@tonic-gate  *   KM_SLEEP allocations, a task_detach() is followed by a subsequent
5810Sstevel@tonic-gate  *   task_rele() once appropriate context is available.
5820Sstevel@tonic-gate  *
5830Sstevel@tonic-gate  *   Because task_detach() involves relinquishing the process's membership in
5840Sstevel@tonic-gate  *   the project, any observational rctls the process may have had on the task
5850Sstevel@tonic-gate  *   or project are destroyed.
5860Sstevel@tonic-gate  *
5870Sstevel@tonic-gate  * Return values
5880Sstevel@tonic-gate  *   None.
5890Sstevel@tonic-gate  *
5900Sstevel@tonic-gate  * Caller's context
5910Sstevel@tonic-gate  *   pidlock and p_lock held across task_detach().
5920Sstevel@tonic-gate  */
5930Sstevel@tonic-gate void
5940Sstevel@tonic-gate task_detach(proc_t *p)
5950Sstevel@tonic-gate {
5960Sstevel@tonic-gate 	task_t *tk = p->p_task;
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
5990Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
6000Sstevel@tonic-gate 	ASSERT(p->p_task != NULL);
6010Sstevel@tonic-gate 	ASSERT(tk->tk_memb_list != NULL);
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	if (tk->tk_memb_list == p)
6040Sstevel@tonic-gate 		tk->tk_memb_list = p->p_tasknext;
6050Sstevel@tonic-gate 	if (tk->tk_memb_list == p)
6060Sstevel@tonic-gate 		tk->tk_memb_list = NULL;
6070Sstevel@tonic-gate 	p->p_taskprev->p_tasknext = p->p_tasknext;
6080Sstevel@tonic-gate 	p->p_tasknext->p_taskprev = p->p_taskprev;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	rctl_set_tearoff(p->p_task->tk_rctls, p);
6110Sstevel@tonic-gate 	rctl_set_tearoff(p->p_task->tk_proj->kpj_rctls, p);
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	p->p_task = NULL;
6140Sstevel@tonic-gate 	p->p_tasknext = p->p_taskprev = NULL;
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate /*
6180Sstevel@tonic-gate  * task_change(task_t *, proc_t *)
6190Sstevel@tonic-gate  *
6200Sstevel@tonic-gate  * Overview
6210Sstevel@tonic-gate  *   task_change() removes the specified process from its current task.  The
6220Sstevel@tonic-gate  *   process is then attached to the specified task.  This routine is called
6230Sstevel@tonic-gate  *   from settaskid() when process is being moved to a new task.
6240Sstevel@tonic-gate  *
6250Sstevel@tonic-gate  * Return values
6260Sstevel@tonic-gate  *   None.
6270Sstevel@tonic-gate  *
6280Sstevel@tonic-gate  * Caller's context
6290Sstevel@tonic-gate  *   pidlock and p_lock held across task_change()
6300Sstevel@tonic-gate  */
6310Sstevel@tonic-gate void
6320Sstevel@tonic-gate task_change(task_t *newtk, proc_t *p)
6330Sstevel@tonic-gate {
6340Sstevel@tonic-gate 	task_t *oldtk = p->p_task;
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
6370Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
6380Sstevel@tonic-gate 	ASSERT(oldtk != NULL);
6390Sstevel@tonic-gate 	ASSERT(oldtk->tk_memb_list != NULL);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	mutex_enter(&p->p_zone->zone_nlwps_lock);
6420Sstevel@tonic-gate 	oldtk->tk_nlwps -= p->p_lwpcnt;
6430Sstevel@tonic-gate 	mutex_exit(&p->p_zone->zone_nlwps_lock);
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	mutex_enter(&newtk->tk_zone->zone_nlwps_lock);
6460Sstevel@tonic-gate 	newtk->tk_nlwps += p->p_lwpcnt;
6470Sstevel@tonic-gate 	mutex_exit(&newtk->tk_zone->zone_nlwps_lock);
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	task_detach(p);
6500Sstevel@tonic-gate 	task_begin(newtk, p);
6514584Srh87107 	exacct_move_mstate(p, oldtk, newtk);
6520Sstevel@tonic-gate }
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate /*
6550Sstevel@tonic-gate  * task_end()
6560Sstevel@tonic-gate  *
6570Sstevel@tonic-gate  * Overview
6580Sstevel@tonic-gate  *   task_end() contains the actions executed once the final member of
6590Sstevel@tonic-gate  *   a task has released the task, and all actions connected with the task, such
6600Sstevel@tonic-gate  *   as committing an accounting record to a file, are completed.  It is called
6610Sstevel@tonic-gate  *   by the known last consumer of the task information.  Additionally,
6620Sstevel@tonic-gate  *   task_end() must never refer to any process in the system.
6630Sstevel@tonic-gate  *
6640Sstevel@tonic-gate  * Return values
6650Sstevel@tonic-gate  *   None.
6660Sstevel@tonic-gate  *
6670Sstevel@tonic-gate  * Caller's context
6680Sstevel@tonic-gate  *   No restrictions on context, beyond that given above.
6690Sstevel@tonic-gate  */
6700Sstevel@tonic-gate void
6710Sstevel@tonic-gate task_end(task_t *tk)
6720Sstevel@tonic-gate {
6730Sstevel@tonic-gate 	ASSERT(tk->tk_hold_count == 0);
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	project_rele(tk->tk_proj);
6760Sstevel@tonic-gate 	kmem_free(tk->tk_usage, sizeof (task_usage_t));
6774584Srh87107 	kmem_free(tk->tk_inherited, sizeof (task_usage_t));
6780Sstevel@tonic-gate 	if (tk->tk_prevusage != NULL)
6790Sstevel@tonic-gate 		kmem_free(tk->tk_prevusage, sizeof (task_usage_t));
6800Sstevel@tonic-gate 	if (tk->tk_zoneusage != NULL)
6810Sstevel@tonic-gate 		kmem_free(tk->tk_zoneusage, sizeof (task_usage_t));
6820Sstevel@tonic-gate 	rctl_set_free(tk->tk_rctls);
6830Sstevel@tonic-gate 	id_free(taskid_space, tk->tk_tkid);
6840Sstevel@tonic-gate 	zone_task_rele(tk->tk_zone);
6850Sstevel@tonic-gate 	kmem_cache_free(task_cache, tk);
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate static void
6890Sstevel@tonic-gate changeproj(proc_t *p, kproject_t *kpj, zone_t *zone, void *projbuf,
6900Sstevel@tonic-gate     void *zonebuf)
6910Sstevel@tonic-gate {
6920Sstevel@tonic-gate 	kproject_t *oldkpj;
6930Sstevel@tonic-gate 	kthread_t *t;
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
6960Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	if ((t = p->p_tlist) != NULL) {
6990Sstevel@tonic-gate 		do {
7000Sstevel@tonic-gate 			(void) project_hold(kpj);
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 			thread_lock(t);
7030Sstevel@tonic-gate 			oldkpj = ttoproj(t);
7043792Sakolb 
7053792Sakolb 			/*
7063792Sakolb 			 * Kick this thread so that he doesn't sit
7073792Sakolb 			 * on a wrong wait queue.
7083792Sakolb 			 */
7093792Sakolb 			if (ISWAITING(t))
7103792Sakolb 				setrun_locked(t);
7113792Sakolb 
7123792Sakolb 			/*
7133792Sakolb 			 * The thread wants to go on the project wait queue, but
7143792Sakolb 			 * the waitq is changing.
7153792Sakolb 			 */
7163792Sakolb 			if (t->t_schedflag & TS_PROJWAITQ)
7173792Sakolb 				t->t_schedflag &= ~ TS_PROJWAITQ;
7183792Sakolb 
7190Sstevel@tonic-gate 			t->t_proj = kpj;
7200Sstevel@tonic-gate 			t->t_pre_sys = 1;		/* For cred update */
7210Sstevel@tonic-gate 			thread_unlock(t);
7220Sstevel@tonic-gate 			fss_changeproj(t, kpj, zone, projbuf, zonebuf);
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 			project_rele(oldkpj);
7250Sstevel@tonic-gate 		} while ((t = t->t_forw) != p->p_tlist);
7260Sstevel@tonic-gate 	}
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate /*
7300Sstevel@tonic-gate  * task_join()
7310Sstevel@tonic-gate  *
7320Sstevel@tonic-gate  * Overview
7330Sstevel@tonic-gate  *   task_join() contains the actions that must be executed when the first
7340Sstevel@tonic-gate  *   member (curproc) of a newly created task joins it.  It may never fail.
7350Sstevel@tonic-gate  *
7360Sstevel@tonic-gate  *   The caller must make sure holdlwps() is called so that all other lwps are
7370Sstevel@tonic-gate  *   stopped prior to calling this function.
7380Sstevel@tonic-gate  *
7390Sstevel@tonic-gate  *   NB: It returns with curproc->p_lock held.
7400Sstevel@tonic-gate  *
7410Sstevel@tonic-gate  * Return values
7420Sstevel@tonic-gate  *   Pointer to the old task.
7430Sstevel@tonic-gate  *
7440Sstevel@tonic-gate  * Caller's context
7450Sstevel@tonic-gate  *   cpu_lock must be held entering the function.  It will acquire pidlock,
7460Sstevel@tonic-gate  *   p_crlock and p_lock during execution.
7470Sstevel@tonic-gate  */
7480Sstevel@tonic-gate task_t *
7490Sstevel@tonic-gate task_join(task_t *tk, uint_t flags)
7500Sstevel@tonic-gate {
7510Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
7520Sstevel@tonic-gate 	task_t *prev_tk;
7530Sstevel@tonic-gate 	void *projbuf, *zonebuf;
7540Sstevel@tonic-gate 	zone_t *zone = tk->tk_zone;
7550Sstevel@tonic-gate 	projid_t projid = tk->tk_proj->kpj_id;
7560Sstevel@tonic-gate 	cred_t *oldcr;
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	/*
7590Sstevel@tonic-gate 	 * We can't know for sure if holdlwps() was called, but we can check to
7600Sstevel@tonic-gate 	 * ensure we're single-threaded.
7610Sstevel@tonic-gate 	 */
7620Sstevel@tonic-gate 	ASSERT(curthread == p->p_agenttp || p->p_lwprcnt == 1);
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	/*
7650Sstevel@tonic-gate 	 * Changing the credential is always hard because we cannot
7660Sstevel@tonic-gate 	 * allocate memory when holding locks but we don't know whether
7670Sstevel@tonic-gate 	 * we need to change it.  We first get a reference to the current
7680Sstevel@tonic-gate 	 * cred if we need to change it.  Then we create a credential
7690Sstevel@tonic-gate 	 * with an updated project id.  Finally we install it, first
7700Sstevel@tonic-gate 	 * releasing the reference we had on the p_cred at the time we
7710Sstevel@tonic-gate 	 * acquired the lock the first time and later we release the
7720Sstevel@tonic-gate 	 * reference to p_cred at the time we acquired the lock the
7730Sstevel@tonic-gate 	 * second time.
7740Sstevel@tonic-gate 	 */
7750Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
7760Sstevel@tonic-gate 	if (crgetprojid(p->p_cred) == projid)
7770Sstevel@tonic-gate 		oldcr = NULL;
7780Sstevel@tonic-gate 	else
7790Sstevel@tonic-gate 		crhold(oldcr = p->p_cred);
7800Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	if (oldcr != NULL) {
7830Sstevel@tonic-gate 		cred_t *newcr = crdup(oldcr);
7840Sstevel@tonic-gate 		crsetprojid(newcr, projid);
7850Sstevel@tonic-gate 		crfree(oldcr);
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 		mutex_enter(&p->p_crlock);
7880Sstevel@tonic-gate 		oldcr = p->p_cred;
7890Sstevel@tonic-gate 		p->p_cred = newcr;
7900Sstevel@tonic-gate 		mutex_exit(&p->p_crlock);
7910Sstevel@tonic-gate 		crfree(oldcr);
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	/*
7950Sstevel@tonic-gate 	 * Make sure that the number of processor sets is constant
7960Sstevel@tonic-gate 	 * across this operation.
7970Sstevel@tonic-gate 	 */
7980Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	projbuf = fss_allocbuf(FSS_NPSET_BUF, FSS_ALLOC_PROJ);
8010Sstevel@tonic-gate 	zonebuf = fss_allocbuf(FSS_NPSET_BUF, FSS_ALLOC_ZONE);
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	mutex_enter(&pidlock);
8040Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	prev_tk = p->p_task;
8070Sstevel@tonic-gate 	task_change(tk, p);
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	/*
8100Sstevel@tonic-gate 	 * Now move threads one by one to their new project.
8110Sstevel@tonic-gate 	 */
8120Sstevel@tonic-gate 	changeproj(p, tk->tk_proj, zone, projbuf, zonebuf);
8130Sstevel@tonic-gate 	if (flags & TASK_FINAL)
8140Sstevel@tonic-gate 		p->p_task->tk_flags |= TASK_FINAL;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	mutex_exit(&pidlock);
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	fss_freebuf(zonebuf, FSS_ALLOC_ZONE);
8190Sstevel@tonic-gate 	fss_freebuf(projbuf, FSS_ALLOC_PROJ);
8200Sstevel@tonic-gate 	return (prev_tk);
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate /*
8240Sstevel@tonic-gate  * rctl ops vectors
8250Sstevel@tonic-gate  */
8260Sstevel@tonic-gate static rctl_ops_t task_lwps_ops = {
8270Sstevel@tonic-gate 	rcop_no_action,
8280Sstevel@tonic-gate 	task_lwps_usage,
8290Sstevel@tonic-gate 	task_lwps_set,
8300Sstevel@tonic-gate 	task_lwps_test
8310Sstevel@tonic-gate };
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate static rctl_ops_t task_cpu_time_ops = {
8340Sstevel@tonic-gate 	rcop_no_action,
8350Sstevel@tonic-gate 	task_cpu_time_usage,
8360Sstevel@tonic-gate 	rcop_no_set,
8370Sstevel@tonic-gate 	task_cpu_time_test
8380Sstevel@tonic-gate };
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate /*ARGSUSED*/
8410Sstevel@tonic-gate /*
8420Sstevel@tonic-gate  * void task_init(void)
8430Sstevel@tonic-gate  *
8440Sstevel@tonic-gate  * Overview
8450Sstevel@tonic-gate  *   task_init() initializes task-related hashes, caches, and the task id
8460Sstevel@tonic-gate  *   space.  Additionally, task_init() establishes p0 as a member of task0.
8470Sstevel@tonic-gate  *   Called by main().
8480Sstevel@tonic-gate  *
8490Sstevel@tonic-gate  * Return values
8500Sstevel@tonic-gate  *   None.
8510Sstevel@tonic-gate  *
8520Sstevel@tonic-gate  * Caller's context
8530Sstevel@tonic-gate  *   task_init() must be called prior to MP startup.
8540Sstevel@tonic-gate  */
8550Sstevel@tonic-gate void
8560Sstevel@tonic-gate task_init(void)
8570Sstevel@tonic-gate {
8580Sstevel@tonic-gate 	proc_t *p = &p0;
8590Sstevel@tonic-gate 	mod_hash_hndl_t hndl;
8600Sstevel@tonic-gate 	rctl_set_t *set;
8610Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
8620Sstevel@tonic-gate 	rctl_entity_p_t e;
8630Sstevel@tonic-gate 	/*
8640Sstevel@tonic-gate 	 * Initialize task_cache and taskid_space.
8650Sstevel@tonic-gate 	 */
8660Sstevel@tonic-gate 	task_cache = kmem_cache_create("task_cache", sizeof (task_t),
8670Sstevel@tonic-gate 	    0, NULL, NULL, NULL, NULL, NULL, 0);
8680Sstevel@tonic-gate 	taskid_space = id_space_create("taskid_space", 0, MAX_TASKID);
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	/*
8710Sstevel@tonic-gate 	 * Initialize task hash table.
8720Sstevel@tonic-gate 	 */
8730Sstevel@tonic-gate 	task_hash = mod_hash_create_idhash("task_hash", task_hash_size,
8740Sstevel@tonic-gate 	    mod_hash_null_valdtor);
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	/*
8770Sstevel@tonic-gate 	 * Initialize task-based rctls.
8780Sstevel@tonic-gate 	 */
8790Sstevel@tonic-gate 	rc_task_lwps = rctl_register("task.max-lwps", RCENTITY_TASK,
8800Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_COUNT, INT_MAX, INT_MAX,
8810Sstevel@tonic-gate 	    &task_lwps_ops);
8820Sstevel@tonic-gate 	rc_task_cpu_time = rctl_register("task.max-cpu-time", RCENTITY_TASK,
8830Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_DENY_NEVER |
8840Sstevel@tonic-gate 	    RCTL_GLOBAL_CPU_TIME | RCTL_GLOBAL_INFINITE |
8850Sstevel@tonic-gate 	    RCTL_GLOBAL_UNOBSERVABLE | RCTL_GLOBAL_SECONDS, UINT64_MAX,
8860Sstevel@tonic-gate 	    UINT64_MAX, &task_cpu_time_ops);
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	/*
8890Sstevel@tonic-gate 	 * Create task0 and place p0 in it as a member.
8900Sstevel@tonic-gate 	 */
8910Sstevel@tonic-gate 	task0p = kmem_cache_alloc(task_cache, KM_SLEEP);
8920Sstevel@tonic-gate 	bzero(task0p, sizeof (task_t));
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	task0p->tk_tkid = id_alloc(taskid_space);
8950Sstevel@tonic-gate 	task0p->tk_usage = kmem_zalloc(sizeof (task_usage_t), KM_SLEEP);
8964584Srh87107 	task0p->tk_inherited = kmem_zalloc(sizeof (task_usage_t), KM_SLEEP);
8973247Sgjelinek 	task0p->tk_proj = project_hold_by_id(0, &zone0,
8980Sstevel@tonic-gate 	    PROJECT_HOLD_INSERT);
8990Sstevel@tonic-gate 	task0p->tk_flags = TASK_NORMAL;
9000Sstevel@tonic-gate 	task0p->tk_nlwps = p->p_lwpcnt;
9010Sstevel@tonic-gate 	task0p->tk_zone = global_zone;
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	set = rctl_set_create();
9040Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_TASK);
9050Sstevel@tonic-gate 	mutex_enter(&curproc->p_lock);
9060Sstevel@tonic-gate 	e.rcep_p.task = task0p;
9070Sstevel@tonic-gate 	e.rcep_t = RCENTITY_TASK;
9080Sstevel@tonic-gate 	task0p->tk_rctls = rctl_set_init(RCENTITY_TASK, curproc, &e, set, gp);
9090Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
9100Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	(void) mod_hash_reserve(task_hash, &hndl);
9130Sstevel@tonic-gate 	mutex_enter(&task_hash_lock);
9140Sstevel@tonic-gate 	ASSERT(task_find(task0p->tk_tkid, GLOBAL_ZONEID) == NULL);
9150Sstevel@tonic-gate 	if (mod_hash_insert_reserve(task_hash,
9160Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)task0p->tk_tkid,
9170Sstevel@tonic-gate 	    (mod_hash_val_t *)task0p, hndl) != 0) {
9180Sstevel@tonic-gate 		mod_hash_cancel(task_hash, &hndl);
9190Sstevel@tonic-gate 		panic("unable to insert task %d(%p)", task0p->tk_tkid,
9200Sstevel@tonic-gate 		    (void *)task0p);
9210Sstevel@tonic-gate 	}
9220Sstevel@tonic-gate 	mutex_exit(&task_hash_lock);
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	task0p->tk_memb_list = p;
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	/*
9270Sstevel@tonic-gate 	 * Initialize task pointers for p0, including doubly linked list of task
9280Sstevel@tonic-gate 	 * members.
9290Sstevel@tonic-gate 	 */
9300Sstevel@tonic-gate 	p->p_task = task0p;
9310Sstevel@tonic-gate 	p->p_taskprev = p->p_tasknext = p;
9320Sstevel@tonic-gate 	task_hold(task0p);
9330Sstevel@tonic-gate }
934