xref: /titanic_51/usr/src/uts/common/os/task.c (revision bb5ca623d9cfae0efb343eed949af6d1a9163207)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50209230bSgjelinek  * Common Development and Distribution License (the "License").
60209230bSgjelinek  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*bb5ca623SVamsi Nagineni  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
277c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
287c478bd9Sstevel@tonic-gate #include <sys/exacct.h>
297c478bd9Sstevel@tonic-gate #include <sys/id_space.h>
307c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
317c478bd9Sstevel@tonic-gate #include <sys/modhash.h>
327c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
337c478bd9Sstevel@tonic-gate #include <sys/proc.h>
347c478bd9Sstevel@tonic-gate #include <sys/project.h>
357c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <sys/task.h>
387c478bd9Sstevel@tonic-gate #include <sys/time.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/zone.h>
417c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
427c478bd9Sstevel@tonic-gate #include <sys/fss.h>
437c478bd9Sstevel@tonic-gate #include <sys/class.h>
447c478bd9Sstevel@tonic-gate #include <sys/project.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * Tasks
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  *   A task is a collection of processes, associated with a common project ID
507c478bd9Sstevel@tonic-gate  *   and related by a common initial parent.  The task primarily represents a
517c478bd9Sstevel@tonic-gate  *   natural process sequence with known resource usage, although it can also be
527c478bd9Sstevel@tonic-gate  *   viewed as a convenient grouping of processes for signal delivery, processor
537c478bd9Sstevel@tonic-gate  *   binding, and administrative operations.
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  * Membership and observership
567c478bd9Sstevel@tonic-gate  *   We can conceive of situations where processes outside of the task may wish
577c478bd9Sstevel@tonic-gate  *   to examine the resource usage of the task.  Similarly, a number of the
587c478bd9Sstevel@tonic-gate  *   administrative operations on a task can be performed by processes who are
597c478bd9Sstevel@tonic-gate  *   not members of the task.  Accordingly, we must design a locking strategy
607c478bd9Sstevel@tonic-gate  *   where observers of the task, who wish to examine or operate on the task,
617c478bd9Sstevel@tonic-gate  *   and members of task, who can perform the mentioned operations, as well as
627c478bd9Sstevel@tonic-gate  *   leave the task, see a consistent and correct representation of the task at
637c478bd9Sstevel@tonic-gate  *   all times.
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * Locking
667c478bd9Sstevel@tonic-gate  *   Because the task membership is a new relation between processes, its
677c478bd9Sstevel@tonic-gate  *   locking becomes an additional responsibility of the pidlock/p_lock locking
687c478bd9Sstevel@tonic-gate  *   sequence; however, tasks closely resemble sessions and the session locking
697c478bd9Sstevel@tonic-gate  *   model is mostly appropriate for the interaction of tasks, processes, and
707c478bd9Sstevel@tonic-gate  *   procfs.
717c478bd9Sstevel@tonic-gate  *
727c478bd9Sstevel@tonic-gate  *   kmutex_t task_hash_lock
737c478bd9Sstevel@tonic-gate  *     task_hash_lock is a global lock protecting the contents of the task
747c478bd9Sstevel@tonic-gate  *     ID-to-task pointer hash.  Holders of task_hash_lock must not attempt to
757c478bd9Sstevel@tonic-gate  *     acquire pidlock or p_lock.
767c478bd9Sstevel@tonic-gate  *   uint_t tk_hold_count
777c478bd9Sstevel@tonic-gate  *     tk_hold_count, the number of members and observers of the current task,
787c478bd9Sstevel@tonic-gate  *     must be manipulated atomically.
797c478bd9Sstevel@tonic-gate  *   proc_t *tk_memb_list
807c478bd9Sstevel@tonic-gate  *   proc_t *p_tasknext
817c478bd9Sstevel@tonic-gate  *   proc_t *p_taskprev
827c478bd9Sstevel@tonic-gate  *     The task's membership list is protected by pidlock, and is therefore
837c478bd9Sstevel@tonic-gate  *     always acquired before any of its members' p_lock mutexes.  The p_task
847c478bd9Sstevel@tonic-gate  *     member of the proc structure is protected by pidlock or p_lock for
857c478bd9Sstevel@tonic-gate  *     reading, and by both pidlock and p_lock for modification, as is done for
867c478bd9Sstevel@tonic-gate  *     p_sessp.  The key point is that only the process can modify its p_task,
877c478bd9Sstevel@tonic-gate  *     and not any entity on the system.  (/proc will use prlock() to prevent
887c478bd9Sstevel@tonic-gate  *     the process from leaving, as opposed to pidlock.)
897c478bd9Sstevel@tonic-gate  *   kmutex_t tk_usage_lock
907c478bd9Sstevel@tonic-gate  *     tk_usage_lock is a per-task lock protecting the contents of the task
917c478bd9Sstevel@tonic-gate  *     usage structure and tk_nlwps counter for the task.max-lwps resource
927c478bd9Sstevel@tonic-gate  *     control.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate int task_hash_size = 256;
967c478bd9Sstevel@tonic-gate static kmutex_t task_hash_lock;
977c478bd9Sstevel@tonic-gate static mod_hash_t *task_hash;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static id_space_t *taskid_space;	/* global taskid space */
1007c478bd9Sstevel@tonic-gate static kmem_cache_t *task_cache;	/* kmem cache for task structures */
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate rctl_hndl_t rc_task_lwps;
1037c478bd9Sstevel@tonic-gate rctl_hndl_t rc_task_cpu_time;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * static rctl_qty_t task_usage_lwps(void *taskp)
1077c478bd9Sstevel@tonic-gate  *
1087c478bd9Sstevel@tonic-gate  * Overview
1097c478bd9Sstevel@tonic-gate  *   task_usage_lwps() is the usage operation for the resource control
1107c478bd9Sstevel@tonic-gate  *   associated with the number of LWPs in a task.
1117c478bd9Sstevel@tonic-gate  *
1127c478bd9Sstevel@tonic-gate  * Return values
1137c478bd9Sstevel@tonic-gate  *   The number of LWPs in the given task is returned.
1147c478bd9Sstevel@tonic-gate  *
1157c478bd9Sstevel@tonic-gate  * Caller's context
1167c478bd9Sstevel@tonic-gate  *   The p->p_lock must be held across the call.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1197c478bd9Sstevel@tonic-gate static rctl_qty_t
1207c478bd9Sstevel@tonic-gate task_lwps_usage(rctl_t *r, proc_t *p)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	task_t *t;
1237c478bd9Sstevel@tonic-gate 	rctl_qty_t nlwps;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	t = p->p_task;
1287c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_zone->zone_nlwps_lock);
1297c478bd9Sstevel@tonic-gate 	nlwps = t->tk_nlwps;
1307c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_zone->zone_nlwps_lock);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	return (nlwps);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate  * static int task_test_lwps(void *taskp, rctl_val_t *, int64_t incr,
1377c478bd9Sstevel@tonic-gate  *   int flags)
1387c478bd9Sstevel@tonic-gate  *
1397c478bd9Sstevel@tonic-gate  * Overview
1407c478bd9Sstevel@tonic-gate  *   task_test_lwps() is the test-if-valid-increment for the resource control
1417c478bd9Sstevel@tonic-gate  *   for the number of processes in a task.
1427c478bd9Sstevel@tonic-gate  *
1437c478bd9Sstevel@tonic-gate  * Return values
1447c478bd9Sstevel@tonic-gate  *   0 if the threshold limit was not passed, 1 if the limit was passed.
1457c478bd9Sstevel@tonic-gate  *
1467c478bd9Sstevel@tonic-gate  * Caller's context
1477c478bd9Sstevel@tonic-gate  *   p->p_lock must be held across the call.
1487c478bd9Sstevel@tonic-gate  */
1497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1507c478bd9Sstevel@tonic-gate static int
1517c478bd9Sstevel@tonic-gate task_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
1527c478bd9Sstevel@tonic-gate     rctl_qty_t incr,
1537c478bd9Sstevel@tonic-gate     uint_t flags)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	rctl_qty_t nlwps;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
1587c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_TASK);
1597c478bd9Sstevel@tonic-gate 	if (e->rcep_p.task == NULL)
1607c478bd9Sstevel@tonic-gate 		return (0);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(e->rcep_p.task->tk_zone->zone_nlwps_lock)));
1637c478bd9Sstevel@tonic-gate 	nlwps = e->rcep_p.task->tk_nlwps;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if (nlwps + incr > rcntl->rcv_value)
1667c478bd9Sstevel@tonic-gate 		return (1);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	return (0);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1717c478bd9Sstevel@tonic-gate static int
1727c478bd9Sstevel@tonic-gate task_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv) {
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
1757c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_TASK);
1767c478bd9Sstevel@tonic-gate 	if (e->rcep_p.task == NULL)
1777c478bd9Sstevel@tonic-gate 		return (0);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	e->rcep_p.task->tk_nlwps_ctl = nv;
1807c478bd9Sstevel@tonic-gate 	return (0);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate  * static rctl_qty_t task_usage_cpu_secs(void *taskp)
1857c478bd9Sstevel@tonic-gate  *
1867c478bd9Sstevel@tonic-gate  * Overview
1877c478bd9Sstevel@tonic-gate  *   task_usage_cpu_secs() is the usage operation for the resource control
1887c478bd9Sstevel@tonic-gate  *   associated with the total accrued CPU seconds for a task.
1897c478bd9Sstevel@tonic-gate  *
1907c478bd9Sstevel@tonic-gate  * Return values
1917c478bd9Sstevel@tonic-gate  *   The number of CPU seconds consumed by the task is returned.
1927c478bd9Sstevel@tonic-gate  *
1937c478bd9Sstevel@tonic-gate  * Caller's context
1947c478bd9Sstevel@tonic-gate  *   The given task must be held across the call.
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1977c478bd9Sstevel@tonic-gate static rctl_qty_t
1987c478bd9Sstevel@tonic-gate task_cpu_time_usage(rctl_t *r, proc_t *p)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	task_t *t = p->p_task;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
2032850d85bSmv143129 	return (t->tk_cpu_time);
2042850d85bSmv143129 }
2052850d85bSmv143129 
2062850d85bSmv143129 /*
2072850d85bSmv143129  * int task_cpu_time_incr(task_t *t, rctl_qty_t incr)
2082850d85bSmv143129  *
2092850d85bSmv143129  * Overview
2102850d85bSmv143129  *   task_cpu_time_incr() increments the amount of CPU time used
2112850d85bSmv143129  *   by this task.
2122850d85bSmv143129  *
2132850d85bSmv143129  * Return values
2142850d85bSmv143129  *   1   if a second or more time is accumulated
2152850d85bSmv143129  *   0   otherwise
2162850d85bSmv143129  *
2172850d85bSmv143129  * Caller's context
2182850d85bSmv143129  *   This is called by the clock tick accounting function to charge
2192850d85bSmv143129  *   CPU time to a task.
2202850d85bSmv143129  */
2212850d85bSmv143129 rctl_qty_t
2222850d85bSmv143129 task_cpu_time_incr(task_t *t, rctl_qty_t incr)
2232850d85bSmv143129 {
2242850d85bSmv143129 	rctl_qty_t ret = 0;
2252850d85bSmv143129 
2262850d85bSmv143129 	mutex_enter(&t->tk_cpu_time_lock);
2272850d85bSmv143129 	t->tk_cpu_ticks += incr;
2282850d85bSmv143129 	if (t->tk_cpu_ticks >= hz) {
2292850d85bSmv143129 		t->tk_cpu_time += t->tk_cpu_ticks / hz;
2302850d85bSmv143129 		t->tk_cpu_ticks = t->tk_cpu_ticks % hz;
2312850d85bSmv143129 		ret = t->tk_cpu_time;
2322850d85bSmv143129 	}
2332850d85bSmv143129 	mutex_exit(&t->tk_cpu_time_lock);
2342850d85bSmv143129 
2352850d85bSmv143129 	return (ret);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate  * static int task_test_cpu_secs(void *taskp, rctl_val_t *, int64_t incr,
2407c478bd9Sstevel@tonic-gate  *   int flags)
2417c478bd9Sstevel@tonic-gate  *
2427c478bd9Sstevel@tonic-gate  * Overview
2437c478bd9Sstevel@tonic-gate  *   task_test_cpu_secs() is the test-if-valid-increment for the resource
2447c478bd9Sstevel@tonic-gate  *   control for the total accrued CPU seconds for a task.
2457c478bd9Sstevel@tonic-gate  *
2467c478bd9Sstevel@tonic-gate  * Return values
2477c478bd9Sstevel@tonic-gate  *   0 if the threshold limit was not passed, 1 if the limit was passed.
2487c478bd9Sstevel@tonic-gate  *
2497c478bd9Sstevel@tonic-gate  * Caller's context
2507c478bd9Sstevel@tonic-gate  *   The given task must be held across the call.
2517c478bd9Sstevel@tonic-gate  */
2527c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2537c478bd9Sstevel@tonic-gate static int
2547c478bd9Sstevel@tonic-gate task_cpu_time_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
2557c478bd9Sstevel@tonic-gate     struct rctl_val *rcntl, rctl_qty_t incr, uint_t flags)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
2587c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_TASK);
2597c478bd9Sstevel@tonic-gate 	if (e->rcep_p.task == NULL)
2607c478bd9Sstevel@tonic-gate 		return (0);
2617c478bd9Sstevel@tonic-gate 
2622850d85bSmv143129 	if (incr >= rcntl->rcv_value)
2637c478bd9Sstevel@tonic-gate 		return (1);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	return (0);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate static task_t *
2697c478bd9Sstevel@tonic-gate task_find(taskid_t id, zoneid_t zoneid)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	task_t *tk;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&task_hash_lock));
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	if (mod_hash_find(task_hash, (mod_hash_key_t)(uintptr_t)id,
2767c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t *)&tk) == MH_ERR_NOTFOUND ||
2777c478bd9Sstevel@tonic-gate 	    (zoneid != ALL_ZONES && zoneid != tk->tk_zone->zone_id))
2787c478bd9Sstevel@tonic-gate 		return (NULL);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	return (tk);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate  * task_hold_by_id(), task_hold_by_id_zone()
2857c478bd9Sstevel@tonic-gate  *
2867c478bd9Sstevel@tonic-gate  * Overview
2877c478bd9Sstevel@tonic-gate  *   task_hold_by_id() is used to take a reference on a task by its task id,
2887c478bd9Sstevel@tonic-gate  *   supporting the various system call interfaces for obtaining resource data,
2897c478bd9Sstevel@tonic-gate  *   delivering signals, and so forth.
2907c478bd9Sstevel@tonic-gate  *
2917c478bd9Sstevel@tonic-gate  * Return values
2927c478bd9Sstevel@tonic-gate  *   Returns a pointer to the task_t with taskid_t id.  The task is returned
2937c478bd9Sstevel@tonic-gate  *   with its hold count incremented by one.  Returns NULL if there
2947c478bd9Sstevel@tonic-gate  *   is no task with the requested id.
2957c478bd9Sstevel@tonic-gate  *
2967c478bd9Sstevel@tonic-gate  * Caller's context
2977c478bd9Sstevel@tonic-gate  *   Caller must not be holding task_hash_lock.  No restrictions on context.
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate task_t *
3007c478bd9Sstevel@tonic-gate task_hold_by_id_zone(taskid_t id, zoneid_t zoneid)
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate 	task_t *tk;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	mutex_enter(&task_hash_lock);
3057c478bd9Sstevel@tonic-gate 	if ((tk = task_find(id, zoneid)) != NULL)
3067c478bd9Sstevel@tonic-gate 		atomic_add_32(&tk->tk_hold_count, 1);
3077c478bd9Sstevel@tonic-gate 	mutex_exit(&task_hash_lock);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	return (tk);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate task_t *
3137c478bd9Sstevel@tonic-gate task_hold_by_id(taskid_t id)
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if (INGLOBALZONE(curproc))
3187c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
3197c478bd9Sstevel@tonic-gate 	else
3207c478bd9Sstevel@tonic-gate 		zoneid = getzoneid();
3217c478bd9Sstevel@tonic-gate 	return (task_hold_by_id_zone(id, zoneid));
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate /*
3257c478bd9Sstevel@tonic-gate  * void task_hold(task_t *)
3267c478bd9Sstevel@tonic-gate  *
3277c478bd9Sstevel@tonic-gate  * Overview
3287c478bd9Sstevel@tonic-gate  *   task_hold() is used to take an additional reference to the given task.
3297c478bd9Sstevel@tonic-gate  *
3307c478bd9Sstevel@tonic-gate  * Return values
3317c478bd9Sstevel@tonic-gate  *   None.
3327c478bd9Sstevel@tonic-gate  *
3337c478bd9Sstevel@tonic-gate  * Caller's context
3347c478bd9Sstevel@tonic-gate  *   No restriction on context.
3357c478bd9Sstevel@tonic-gate  */
3367c478bd9Sstevel@tonic-gate void
3377c478bd9Sstevel@tonic-gate task_hold(task_t *tk)
3387c478bd9Sstevel@tonic-gate {
3397c478bd9Sstevel@tonic-gate 	atomic_add_32(&tk->tk_hold_count, 1);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate /*
3437c478bd9Sstevel@tonic-gate  * void task_rele(task_t *)
3447c478bd9Sstevel@tonic-gate  *
3457c478bd9Sstevel@tonic-gate  * Overview
3467c478bd9Sstevel@tonic-gate  *   task_rele() relinquishes a reference on the given task, which was acquired
3477c478bd9Sstevel@tonic-gate  *   via task_hold() or task_hold_by_id().  If this is the last member or
3487c478bd9Sstevel@tonic-gate  *   observer of the task, dispatch it for commitment via the accounting
3497c478bd9Sstevel@tonic-gate  *   subsystem.
3507c478bd9Sstevel@tonic-gate  *
3517c478bd9Sstevel@tonic-gate  * Return values
3527c478bd9Sstevel@tonic-gate  *   None.
3537c478bd9Sstevel@tonic-gate  *
3547c478bd9Sstevel@tonic-gate  * Caller's context
3557c478bd9Sstevel@tonic-gate  *   Caller must not be holding the task_hash_lock.
3567c478bd9Sstevel@tonic-gate  *   Caller's context must be acceptable for KM_SLEEP allocations.
3577c478bd9Sstevel@tonic-gate  */
3587c478bd9Sstevel@tonic-gate void
3597c478bd9Sstevel@tonic-gate task_rele(task_t *tk)
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate 	mutex_enter(&task_hash_lock);
3627c478bd9Sstevel@tonic-gate 	if (atomic_add_32_nv(&tk->tk_hold_count, -1) > 0) {
3637c478bd9Sstevel@tonic-gate 		mutex_exit(&task_hash_lock);
3647c478bd9Sstevel@tonic-gate 		return;
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	mutex_enter(&tk->tk_zone->zone_nlwps_lock);
3687c478bd9Sstevel@tonic-gate 	tk->tk_proj->kpj_ntasks--;
3697c478bd9Sstevel@tonic-gate 	mutex_exit(&tk->tk_zone->zone_nlwps_lock);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	if (mod_hash_destroy(task_hash,
3727c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)tk->tk_tkid) != 0)
3737c478bd9Sstevel@tonic-gate 		panic("unable to delete task %d", tk->tk_tkid);
3747c478bd9Sstevel@tonic-gate 	mutex_exit(&task_hash_lock);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/*
3777c478bd9Sstevel@tonic-gate 	 * At this point, there are no members or observers of the task, so we
3787c478bd9Sstevel@tonic-gate 	 * can safely send it on for commitment to the accounting subsystem.
3797c478bd9Sstevel@tonic-gate 	 * The task will be destroyed in task_end() subsequent to commitment.
3807c478bd9Sstevel@tonic-gate 	 */
3817c478bd9Sstevel@tonic-gate 	(void) taskq_dispatch(exacct_queue, exacct_commit_task, tk, KM_SLEEP);
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate /*
3857c478bd9Sstevel@tonic-gate  * task_t *task_create(projid_t, zone *)
3867c478bd9Sstevel@tonic-gate  *
3877c478bd9Sstevel@tonic-gate  * Overview
3887c478bd9Sstevel@tonic-gate  *   A process constructing a new task calls task_create() to construct and
3897c478bd9Sstevel@tonic-gate  *   preinitialize the task for the appropriate destination project.  Only one
3907c478bd9Sstevel@tonic-gate  *   task, the primordial task0, is not created with task_create().
3917c478bd9Sstevel@tonic-gate  *
3927c478bd9Sstevel@tonic-gate  * Return values
3937c478bd9Sstevel@tonic-gate  *   None.
3947c478bd9Sstevel@tonic-gate  *
3957c478bd9Sstevel@tonic-gate  * Caller's context
3967c478bd9Sstevel@tonic-gate  *   Caller's context should be safe for KM_SLEEP allocations.
3977c478bd9Sstevel@tonic-gate  *   The caller should appropriately bump the kpj_ntasks counter on the
3987c478bd9Sstevel@tonic-gate  *   project that contains this task.
3997c478bd9Sstevel@tonic-gate  */
4007c478bd9Sstevel@tonic-gate task_t *
4017c478bd9Sstevel@tonic-gate task_create(projid_t projid, zone_t *zone)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	task_t *tk = kmem_cache_alloc(task_cache, KM_SLEEP);
4047c478bd9Sstevel@tonic-gate 	task_t *ancestor_tk;
4057c478bd9Sstevel@tonic-gate 	taskid_t tkid;
4067c478bd9Sstevel@tonic-gate 	task_usage_t *tu = kmem_zalloc(sizeof (task_usage_t), KM_SLEEP);
4077c478bd9Sstevel@tonic-gate 	mod_hash_hndl_t hndl;
4087c478bd9Sstevel@tonic-gate 	rctl_set_t *set = rctl_set_create();
4097c478bd9Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
4107c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	bzero(tk, sizeof (task_t));
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	tk->tk_tkid = tkid = id_alloc(taskid_space);
4157c478bd9Sstevel@tonic-gate 	tk->tk_nlwps = 0;
4167c478bd9Sstevel@tonic-gate 	tk->tk_nlwps_ctl = INT_MAX;
4177c478bd9Sstevel@tonic-gate 	tk->tk_usage = tu;
4187eceb558Srh87107 	tk->tk_inherited = kmem_zalloc(sizeof (task_usage_t), KM_SLEEP);
419c97ad5cdSakolb 	tk->tk_proj = project_hold_by_id(projid, zone, PROJECT_HOLD_INSERT);
4207c478bd9Sstevel@tonic-gate 	tk->tk_flags = TASK_NORMAL;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	/*
4237c478bd9Sstevel@tonic-gate 	 * Copy ancestor task's resource controls.
4247c478bd9Sstevel@tonic-gate 	 */
4257c478bd9Sstevel@tonic-gate 	zone_task_hold(zone);
4267c478bd9Sstevel@tonic-gate 	mutex_enter(&curproc->p_lock);
4277c478bd9Sstevel@tonic-gate 	ancestor_tk = curproc->p_task;
4287c478bd9Sstevel@tonic-gate 	task_hold(ancestor_tk);
4297c478bd9Sstevel@tonic-gate 	tk->tk_zone = zone;
4307c478bd9Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	for (;;) {
4337c478bd9Sstevel@tonic-gate 		gp = rctl_set_dup_prealloc(ancestor_tk->tk_rctls);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 		mutex_enter(&ancestor_tk->tk_rctls->rcs_lock);
4367c478bd9Sstevel@tonic-gate 		if (rctl_set_dup_ready(ancestor_tk->tk_rctls, gp))
4377c478bd9Sstevel@tonic-gate 			break;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 		mutex_exit(&ancestor_tk->tk_rctls->rcs_lock);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 		rctl_prealloc_destroy(gp);
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	/*
4457c478bd9Sstevel@tonic-gate 	 * At this point, curproc does not have the appropriate linkage
4467c478bd9Sstevel@tonic-gate 	 * through the task to the project. So, rctl_set_dup should only
4477c478bd9Sstevel@tonic-gate 	 * copy the rctls, and leave the callbacks for later.
4487c478bd9Sstevel@tonic-gate 	 */
4497c478bd9Sstevel@tonic-gate 	e.rcep_p.task = tk;
4507c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_TASK;
4517c478bd9Sstevel@tonic-gate 	tk->tk_rctls = rctl_set_dup(ancestor_tk->tk_rctls, curproc, curproc, &e,
4527c478bd9Sstevel@tonic-gate 	    set, gp, RCD_DUP);
4537c478bd9Sstevel@tonic-gate 	mutex_exit(&ancestor_tk->tk_rctls->rcs_lock);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	/*
4587c478bd9Sstevel@tonic-gate 	 * Record the ancestor task's ID for use by extended accounting.
4597c478bd9Sstevel@tonic-gate 	 */
4607c478bd9Sstevel@tonic-gate 	tu->tu_anctaskid = ancestor_tk->tk_tkid;
4617c478bd9Sstevel@tonic-gate 	task_rele(ancestor_tk);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	/*
4647c478bd9Sstevel@tonic-gate 	 * Put new task structure in the hash table.
4657c478bd9Sstevel@tonic-gate 	 */
4667c478bd9Sstevel@tonic-gate 	(void) mod_hash_reserve(task_hash, &hndl);
4677c478bd9Sstevel@tonic-gate 	mutex_enter(&task_hash_lock);
468*bb5ca623SVamsi Nagineni 	ASSERT(task_find(tkid, zone->zone_id) == NULL);
4697c478bd9Sstevel@tonic-gate 	if (mod_hash_insert_reserve(task_hash, (mod_hash_key_t)(uintptr_t)tkid,
4707c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t *)tk, hndl) != 0) {
4717c478bd9Sstevel@tonic-gate 		mod_hash_cancel(task_hash, &hndl);
4727c478bd9Sstevel@tonic-gate 		panic("unable to insert task %d(%p)", tkid, (void *)tk);
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 	mutex_exit(&task_hash_lock);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	return (tk);
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate /*
4807c478bd9Sstevel@tonic-gate  * void task_attach(task_t *, proc_t *)
4817c478bd9Sstevel@tonic-gate  *
4827c478bd9Sstevel@tonic-gate  * Overview
4837c478bd9Sstevel@tonic-gate  *   task_attach() is used to attach a process to a task; this operation is only
4847c478bd9Sstevel@tonic-gate  *   performed as a result of a fork() or settaskid() system call.  The proc_t's
4857c478bd9Sstevel@tonic-gate  *   p_tasknext and p_taskprev fields will be set such that the proc_t is a
4867c478bd9Sstevel@tonic-gate  *   member of the doubly-linked list of proc_t's that make up the task.
4877c478bd9Sstevel@tonic-gate  *
4887c478bd9Sstevel@tonic-gate  * Return values
4897c478bd9Sstevel@tonic-gate  *   None.
4907c478bd9Sstevel@tonic-gate  *
4917c478bd9Sstevel@tonic-gate  * Caller's context
4927c478bd9Sstevel@tonic-gate  *   pidlock and p->p_lock must be held on entry.
4937c478bd9Sstevel@tonic-gate  */
4947c478bd9Sstevel@tonic-gate void
4957c478bd9Sstevel@tonic-gate task_attach(task_t *tk, proc_t *p)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	proc_t *first, *prev;
4987c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
4997c478bd9Sstevel@tonic-gate 	ASSERT(tk != NULL);
5007c478bd9Sstevel@tonic-gate 	ASSERT(p != NULL);
5017c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
5027c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	if (tk->tk_memb_list == NULL) {
5057c478bd9Sstevel@tonic-gate 		p->p_tasknext = p;
5067c478bd9Sstevel@tonic-gate 		p->p_taskprev = p;
5077c478bd9Sstevel@tonic-gate 	} else {
5087c478bd9Sstevel@tonic-gate 		first = tk->tk_memb_list;
5097c478bd9Sstevel@tonic-gate 		prev = first->p_taskprev;
5107c478bd9Sstevel@tonic-gate 		first->p_taskprev = p;
5117c478bd9Sstevel@tonic-gate 		p->p_tasknext = first;
5127c478bd9Sstevel@tonic-gate 		p->p_taskprev = prev;
5137c478bd9Sstevel@tonic-gate 		prev->p_tasknext = p;
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 	tk->tk_memb_list = p;
5167c478bd9Sstevel@tonic-gate 	task_hold(tk);
5177c478bd9Sstevel@tonic-gate 	p->p_task = tk;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	/*
5207c478bd9Sstevel@tonic-gate 	 * Now that the linkage from process to task and project is
5217c478bd9Sstevel@tonic-gate 	 * complete, do the required callbacks for the task and project
5227c478bd9Sstevel@tonic-gate 	 * rctl sets.
5237c478bd9Sstevel@tonic-gate 	 */
5247c478bd9Sstevel@tonic-gate 	e.rcep_p.proj = tk->tk_proj;
5257c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_PROJECT;
5267c478bd9Sstevel@tonic-gate 	(void) rctl_set_dup(NULL, NULL, p, &e, tk->tk_proj->kpj_rctls, NULL,
5277c478bd9Sstevel@tonic-gate 	    RCD_CALLBACK);
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	e.rcep_p.task = tk;
5307c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_TASK;
5317c478bd9Sstevel@tonic-gate 	(void) rctl_set_dup(NULL, NULL, p, &e, tk->tk_rctls, NULL,
5327c478bd9Sstevel@tonic-gate 	    RCD_CALLBACK);
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate /*
5377c478bd9Sstevel@tonic-gate  * task_begin()
5387c478bd9Sstevel@tonic-gate  *
5397c478bd9Sstevel@tonic-gate  * Overview
5407c478bd9Sstevel@tonic-gate  *   A process constructing a new task calls task_begin() to initialize the
5417c478bd9Sstevel@tonic-gate  *   task, by attaching itself as a member.
5427c478bd9Sstevel@tonic-gate  *
5437c478bd9Sstevel@tonic-gate  * Return values
5447c478bd9Sstevel@tonic-gate  *   None.
5457c478bd9Sstevel@tonic-gate  *
5467c478bd9Sstevel@tonic-gate  * Caller's context
5477c478bd9Sstevel@tonic-gate  *   pidlock and p_lock must be held across the call to task_begin().
5487c478bd9Sstevel@tonic-gate  */
5497c478bd9Sstevel@tonic-gate void
5507c478bd9Sstevel@tonic-gate task_begin(task_t *tk, proc_t *p)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate 	timestruc_t ts;
5537c478bd9Sstevel@tonic-gate 	task_usage_t *tu;
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
5567c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	mutex_enter(&tk->tk_usage_lock);
5597c478bd9Sstevel@tonic-gate 	tu = tk->tk_usage;
5607c478bd9Sstevel@tonic-gate 	gethrestime(&ts);
5617c478bd9Sstevel@tonic-gate 	tu->tu_startsec = (uint64_t)ts.tv_sec;
5627c478bd9Sstevel@tonic-gate 	tu->tu_startnsec = (uint64_t)ts.tv_nsec;
5637c478bd9Sstevel@tonic-gate 	mutex_exit(&tk->tk_usage_lock);
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	/*
5667c478bd9Sstevel@tonic-gate 	 * Join process to the task as a member.
5677c478bd9Sstevel@tonic-gate 	 */
5687c478bd9Sstevel@tonic-gate 	task_attach(tk, p);
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate /*
5727c478bd9Sstevel@tonic-gate  * void task_detach(proc_t *)
5737c478bd9Sstevel@tonic-gate  *
5747c478bd9Sstevel@tonic-gate  * Overview
5757c478bd9Sstevel@tonic-gate  *   task_detach() removes the specified process from its task.  task_detach
5767c478bd9Sstevel@tonic-gate  *   sets the process's task membership to NULL, in anticipation of a final exit
5777c478bd9Sstevel@tonic-gate  *   or of joining a new task.  Because task_rele() requires a context safe for
5787c478bd9Sstevel@tonic-gate  *   KM_SLEEP allocations, a task_detach() is followed by a subsequent
5797c478bd9Sstevel@tonic-gate  *   task_rele() once appropriate context is available.
5807c478bd9Sstevel@tonic-gate  *
5817c478bd9Sstevel@tonic-gate  *   Because task_detach() involves relinquishing the process's membership in
5827c478bd9Sstevel@tonic-gate  *   the project, any observational rctls the process may have had on the task
5837c478bd9Sstevel@tonic-gate  *   or project are destroyed.
5847c478bd9Sstevel@tonic-gate  *
5857c478bd9Sstevel@tonic-gate  * Return values
5867c478bd9Sstevel@tonic-gate  *   None.
5877c478bd9Sstevel@tonic-gate  *
5887c478bd9Sstevel@tonic-gate  * Caller's context
5897c478bd9Sstevel@tonic-gate  *   pidlock and p_lock held across task_detach().
5907c478bd9Sstevel@tonic-gate  */
5917c478bd9Sstevel@tonic-gate void
5927c478bd9Sstevel@tonic-gate task_detach(proc_t *p)
5937c478bd9Sstevel@tonic-gate {
5947c478bd9Sstevel@tonic-gate 	task_t *tk = p->p_task;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
5977c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
5987c478bd9Sstevel@tonic-gate 	ASSERT(p->p_task != NULL);
5997c478bd9Sstevel@tonic-gate 	ASSERT(tk->tk_memb_list != NULL);
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	if (tk->tk_memb_list == p)
6027c478bd9Sstevel@tonic-gate 		tk->tk_memb_list = p->p_tasknext;
6037c478bd9Sstevel@tonic-gate 	if (tk->tk_memb_list == p)
6047c478bd9Sstevel@tonic-gate 		tk->tk_memb_list = NULL;
6057c478bd9Sstevel@tonic-gate 	p->p_taskprev->p_tasknext = p->p_tasknext;
6067c478bd9Sstevel@tonic-gate 	p->p_tasknext->p_taskprev = p->p_taskprev;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	rctl_set_tearoff(p->p_task->tk_rctls, p);
6097c478bd9Sstevel@tonic-gate 	rctl_set_tearoff(p->p_task->tk_proj->kpj_rctls, p);
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	p->p_task = NULL;
6127c478bd9Sstevel@tonic-gate 	p->p_tasknext = p->p_taskprev = NULL;
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate /*
6167c478bd9Sstevel@tonic-gate  * task_change(task_t *, proc_t *)
6177c478bd9Sstevel@tonic-gate  *
6187c478bd9Sstevel@tonic-gate  * Overview
6197c478bd9Sstevel@tonic-gate  *   task_change() removes the specified process from its current task.  The
6207c478bd9Sstevel@tonic-gate  *   process is then attached to the specified task.  This routine is called
6217c478bd9Sstevel@tonic-gate  *   from settaskid() when process is being moved to a new task.
6227c478bd9Sstevel@tonic-gate  *
6237c478bd9Sstevel@tonic-gate  * Return values
6247c478bd9Sstevel@tonic-gate  *   None.
6257c478bd9Sstevel@tonic-gate  *
6267c478bd9Sstevel@tonic-gate  * Caller's context
6277c478bd9Sstevel@tonic-gate  *   pidlock and p_lock held across task_change()
6287c478bd9Sstevel@tonic-gate  */
6297c478bd9Sstevel@tonic-gate void
6307c478bd9Sstevel@tonic-gate task_change(task_t *newtk, proc_t *p)
6317c478bd9Sstevel@tonic-gate {
6327c478bd9Sstevel@tonic-gate 	task_t *oldtk = p->p_task;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
6357c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
6367c478bd9Sstevel@tonic-gate 	ASSERT(oldtk != NULL);
6377c478bd9Sstevel@tonic-gate 	ASSERT(oldtk->tk_memb_list != NULL);
6387c478bd9Sstevel@tonic-gate 
639*bb5ca623SVamsi Nagineni 	mutex_enter(&oldtk->tk_zone->zone_nlwps_lock);
6407c478bd9Sstevel@tonic-gate 	oldtk->tk_nlwps -= p->p_lwpcnt;
641*bb5ca623SVamsi Nagineni 	mutex_exit(&oldtk->tk_zone->zone_nlwps_lock);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	mutex_enter(&newtk->tk_zone->zone_nlwps_lock);
6447c478bd9Sstevel@tonic-gate 	newtk->tk_nlwps += p->p_lwpcnt;
6457c478bd9Sstevel@tonic-gate 	mutex_exit(&newtk->tk_zone->zone_nlwps_lock);
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	task_detach(p);
6487c478bd9Sstevel@tonic-gate 	task_begin(newtk, p);
6497eceb558Srh87107 	exacct_move_mstate(p, oldtk, newtk);
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate /*
6537c478bd9Sstevel@tonic-gate  * task_end()
6547c478bd9Sstevel@tonic-gate  *
6557c478bd9Sstevel@tonic-gate  * Overview
6567c478bd9Sstevel@tonic-gate  *   task_end() contains the actions executed once the final member of
6577c478bd9Sstevel@tonic-gate  *   a task has released the task, and all actions connected with the task, such
6587c478bd9Sstevel@tonic-gate  *   as committing an accounting record to a file, are completed.  It is called
6597c478bd9Sstevel@tonic-gate  *   by the known last consumer of the task information.  Additionally,
6607c478bd9Sstevel@tonic-gate  *   task_end() must never refer to any process in the system.
6617c478bd9Sstevel@tonic-gate  *
6627c478bd9Sstevel@tonic-gate  * Return values
6637c478bd9Sstevel@tonic-gate  *   None.
6647c478bd9Sstevel@tonic-gate  *
6657c478bd9Sstevel@tonic-gate  * Caller's context
6667c478bd9Sstevel@tonic-gate  *   No restrictions on context, beyond that given above.
6677c478bd9Sstevel@tonic-gate  */
6687c478bd9Sstevel@tonic-gate void
6697c478bd9Sstevel@tonic-gate task_end(task_t *tk)
6707c478bd9Sstevel@tonic-gate {
6717c478bd9Sstevel@tonic-gate 	ASSERT(tk->tk_hold_count == 0);
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	project_rele(tk->tk_proj);
6747c478bd9Sstevel@tonic-gate 	kmem_free(tk->tk_usage, sizeof (task_usage_t));
6757eceb558Srh87107 	kmem_free(tk->tk_inherited, sizeof (task_usage_t));
6767c478bd9Sstevel@tonic-gate 	if (tk->tk_prevusage != NULL)
6777c478bd9Sstevel@tonic-gate 		kmem_free(tk->tk_prevusage, sizeof (task_usage_t));
6787c478bd9Sstevel@tonic-gate 	if (tk->tk_zoneusage != NULL)
6797c478bd9Sstevel@tonic-gate 		kmem_free(tk->tk_zoneusage, sizeof (task_usage_t));
6807c478bd9Sstevel@tonic-gate 	rctl_set_free(tk->tk_rctls);
6817c478bd9Sstevel@tonic-gate 	id_free(taskid_space, tk->tk_tkid);
6827c478bd9Sstevel@tonic-gate 	zone_task_rele(tk->tk_zone);
6837c478bd9Sstevel@tonic-gate 	kmem_cache_free(task_cache, tk);
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate static void
6877c478bd9Sstevel@tonic-gate changeproj(proc_t *p, kproject_t *kpj, zone_t *zone, void *projbuf,
6887c478bd9Sstevel@tonic-gate     void *zonebuf)
6897c478bd9Sstevel@tonic-gate {
6907c478bd9Sstevel@tonic-gate 	kproject_t *oldkpj;
6917c478bd9Sstevel@tonic-gate 	kthread_t *t;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
6947c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	if ((t = p->p_tlist) != NULL) {
6977c478bd9Sstevel@tonic-gate 		do {
6987c478bd9Sstevel@tonic-gate 			(void) project_hold(kpj);
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 			thread_lock(t);
7017c478bd9Sstevel@tonic-gate 			oldkpj = ttoproj(t);
702c97ad5cdSakolb 
703c97ad5cdSakolb 			/*
704c97ad5cdSakolb 			 * Kick this thread so that he doesn't sit
705c97ad5cdSakolb 			 * on a wrong wait queue.
706c97ad5cdSakolb 			 */
707c97ad5cdSakolb 			if (ISWAITING(t))
708c97ad5cdSakolb 				setrun_locked(t);
709c97ad5cdSakolb 
710c97ad5cdSakolb 			/*
711c97ad5cdSakolb 			 * The thread wants to go on the project wait queue, but
712c97ad5cdSakolb 			 * the waitq is changing.
713c97ad5cdSakolb 			 */
714c97ad5cdSakolb 			if (t->t_schedflag & TS_PROJWAITQ)
715c97ad5cdSakolb 				t->t_schedflag &= ~ TS_PROJWAITQ;
716c97ad5cdSakolb 
7177c478bd9Sstevel@tonic-gate 			t->t_proj = kpj;
7187c478bd9Sstevel@tonic-gate 			t->t_pre_sys = 1;		/* For cred update */
7197c478bd9Sstevel@tonic-gate 			thread_unlock(t);
7207c478bd9Sstevel@tonic-gate 			fss_changeproj(t, kpj, zone, projbuf, zonebuf);
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 			project_rele(oldkpj);
7237c478bd9Sstevel@tonic-gate 		} while ((t = t->t_forw) != p->p_tlist);
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate }
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate /*
7287c478bd9Sstevel@tonic-gate  * task_join()
7297c478bd9Sstevel@tonic-gate  *
7307c478bd9Sstevel@tonic-gate  * Overview
7317c478bd9Sstevel@tonic-gate  *   task_join() contains the actions that must be executed when the first
7327c478bd9Sstevel@tonic-gate  *   member (curproc) of a newly created task joins it.  It may never fail.
7337c478bd9Sstevel@tonic-gate  *
7347c478bd9Sstevel@tonic-gate  *   The caller must make sure holdlwps() is called so that all other lwps are
7357c478bd9Sstevel@tonic-gate  *   stopped prior to calling this function.
7367c478bd9Sstevel@tonic-gate  *
7377c478bd9Sstevel@tonic-gate  *   NB: It returns with curproc->p_lock held.
7387c478bd9Sstevel@tonic-gate  *
7397c478bd9Sstevel@tonic-gate  * Return values
7407c478bd9Sstevel@tonic-gate  *   Pointer to the old task.
7417c478bd9Sstevel@tonic-gate  *
7427c478bd9Sstevel@tonic-gate  * Caller's context
7437c478bd9Sstevel@tonic-gate  *   cpu_lock must be held entering the function.  It will acquire pidlock,
7447c478bd9Sstevel@tonic-gate  *   p_crlock and p_lock during execution.
7457c478bd9Sstevel@tonic-gate  */
7467c478bd9Sstevel@tonic-gate task_t *
7477c478bd9Sstevel@tonic-gate task_join(task_t *tk, uint_t flags)
7487c478bd9Sstevel@tonic-gate {
7497c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
7507c478bd9Sstevel@tonic-gate 	task_t *prev_tk;
7517c478bd9Sstevel@tonic-gate 	void *projbuf, *zonebuf;
7527c478bd9Sstevel@tonic-gate 	zone_t *zone = tk->tk_zone;
7537c478bd9Sstevel@tonic-gate 	projid_t projid = tk->tk_proj->kpj_id;
7547c478bd9Sstevel@tonic-gate 	cred_t *oldcr;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	/*
7577c478bd9Sstevel@tonic-gate 	 * We can't know for sure if holdlwps() was called, but we can check to
7587c478bd9Sstevel@tonic-gate 	 * ensure we're single-threaded.
7597c478bd9Sstevel@tonic-gate 	 */
7607c478bd9Sstevel@tonic-gate 	ASSERT(curthread == p->p_agenttp || p->p_lwprcnt == 1);
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	/*
7637c478bd9Sstevel@tonic-gate 	 * Changing the credential is always hard because we cannot
7647c478bd9Sstevel@tonic-gate 	 * allocate memory when holding locks but we don't know whether
7657c478bd9Sstevel@tonic-gate 	 * we need to change it.  We first get a reference to the current
7667c478bd9Sstevel@tonic-gate 	 * cred if we need to change it.  Then we create a credential
7677c478bd9Sstevel@tonic-gate 	 * with an updated project id.  Finally we install it, first
7687c478bd9Sstevel@tonic-gate 	 * releasing the reference we had on the p_cred at the time we
7697c478bd9Sstevel@tonic-gate 	 * acquired the lock the first time and later we release the
7707c478bd9Sstevel@tonic-gate 	 * reference to p_cred at the time we acquired the lock the
7717c478bd9Sstevel@tonic-gate 	 * second time.
7727c478bd9Sstevel@tonic-gate 	 */
7737c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
7747c478bd9Sstevel@tonic-gate 	if (crgetprojid(p->p_cred) == projid)
7757c478bd9Sstevel@tonic-gate 		oldcr = NULL;
7767c478bd9Sstevel@tonic-gate 	else
7777c478bd9Sstevel@tonic-gate 		crhold(oldcr = p->p_cred);
7787c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	if (oldcr != NULL) {
7817c478bd9Sstevel@tonic-gate 		cred_t *newcr = crdup(oldcr);
7827c478bd9Sstevel@tonic-gate 		crsetprojid(newcr, projid);
7837c478bd9Sstevel@tonic-gate 		crfree(oldcr);
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_crlock);
7867c478bd9Sstevel@tonic-gate 		oldcr = p->p_cred;
7877c478bd9Sstevel@tonic-gate 		p->p_cred = newcr;
7887c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_crlock);
7897c478bd9Sstevel@tonic-gate 		crfree(oldcr);
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	/*
7937c478bd9Sstevel@tonic-gate 	 * Make sure that the number of processor sets is constant
7947c478bd9Sstevel@tonic-gate 	 * across this operation.
7957c478bd9Sstevel@tonic-gate 	 */
7967c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	projbuf = fss_allocbuf(FSS_NPSET_BUF, FSS_ALLOC_PROJ);
7997c478bd9Sstevel@tonic-gate 	zonebuf = fss_allocbuf(FSS_NPSET_BUF, FSS_ALLOC_ZONE);
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
8027c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	prev_tk = p->p_task;
8057c478bd9Sstevel@tonic-gate 	task_change(tk, p);
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	/*
8087c478bd9Sstevel@tonic-gate 	 * Now move threads one by one to their new project.
8097c478bd9Sstevel@tonic-gate 	 */
8107c478bd9Sstevel@tonic-gate 	changeproj(p, tk->tk_proj, zone, projbuf, zonebuf);
8117c478bd9Sstevel@tonic-gate 	if (flags & TASK_FINAL)
8127c478bd9Sstevel@tonic-gate 		p->p_task->tk_flags |= TASK_FINAL;
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	fss_freebuf(zonebuf, FSS_ALLOC_ZONE);
8177c478bd9Sstevel@tonic-gate 	fss_freebuf(projbuf, FSS_ALLOC_PROJ);
8187c478bd9Sstevel@tonic-gate 	return (prev_tk);
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate /*
8227c478bd9Sstevel@tonic-gate  * rctl ops vectors
8237c478bd9Sstevel@tonic-gate  */
8247c478bd9Sstevel@tonic-gate static rctl_ops_t task_lwps_ops = {
8257c478bd9Sstevel@tonic-gate 	rcop_no_action,
8267c478bd9Sstevel@tonic-gate 	task_lwps_usage,
8277c478bd9Sstevel@tonic-gate 	task_lwps_set,
8287c478bd9Sstevel@tonic-gate 	task_lwps_test
8297c478bd9Sstevel@tonic-gate };
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate static rctl_ops_t task_cpu_time_ops = {
8327c478bd9Sstevel@tonic-gate 	rcop_no_action,
8337c478bd9Sstevel@tonic-gate 	task_cpu_time_usage,
8347c478bd9Sstevel@tonic-gate 	rcop_no_set,
8357c478bd9Sstevel@tonic-gate 	task_cpu_time_test
8367c478bd9Sstevel@tonic-gate };
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8397c478bd9Sstevel@tonic-gate /*
8407c478bd9Sstevel@tonic-gate  * void task_init(void)
8417c478bd9Sstevel@tonic-gate  *
8427c478bd9Sstevel@tonic-gate  * Overview
8437c478bd9Sstevel@tonic-gate  *   task_init() initializes task-related hashes, caches, and the task id
8447c478bd9Sstevel@tonic-gate  *   space.  Additionally, task_init() establishes p0 as a member of task0.
8457c478bd9Sstevel@tonic-gate  *   Called by main().
8467c478bd9Sstevel@tonic-gate  *
8477c478bd9Sstevel@tonic-gate  * Return values
8487c478bd9Sstevel@tonic-gate  *   None.
8497c478bd9Sstevel@tonic-gate  *
8507c478bd9Sstevel@tonic-gate  * Caller's context
8517c478bd9Sstevel@tonic-gate  *   task_init() must be called prior to MP startup.
8527c478bd9Sstevel@tonic-gate  */
8537c478bd9Sstevel@tonic-gate void
8547c478bd9Sstevel@tonic-gate task_init(void)
8557c478bd9Sstevel@tonic-gate {
8567c478bd9Sstevel@tonic-gate 	proc_t *p = &p0;
8577c478bd9Sstevel@tonic-gate 	mod_hash_hndl_t hndl;
8587c478bd9Sstevel@tonic-gate 	rctl_set_t *set;
8597c478bd9Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
8607c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
8617c478bd9Sstevel@tonic-gate 	/*
8627c478bd9Sstevel@tonic-gate 	 * Initialize task_cache and taskid_space.
8637c478bd9Sstevel@tonic-gate 	 */
8647c478bd9Sstevel@tonic-gate 	task_cache = kmem_cache_create("task_cache", sizeof (task_t),
8657c478bd9Sstevel@tonic-gate 	    0, NULL, NULL, NULL, NULL, NULL, 0);
8667c478bd9Sstevel@tonic-gate 	taskid_space = id_space_create("taskid_space", 0, MAX_TASKID);
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	/*
8697c478bd9Sstevel@tonic-gate 	 * Initialize task hash table.
8707c478bd9Sstevel@tonic-gate 	 */
8717c478bd9Sstevel@tonic-gate 	task_hash = mod_hash_create_idhash("task_hash", task_hash_size,
8727c478bd9Sstevel@tonic-gate 	    mod_hash_null_valdtor);
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	/*
8757c478bd9Sstevel@tonic-gate 	 * Initialize task-based rctls.
8767c478bd9Sstevel@tonic-gate 	 */
8777c478bd9Sstevel@tonic-gate 	rc_task_lwps = rctl_register("task.max-lwps", RCENTITY_TASK,
8787c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_COUNT, INT_MAX, INT_MAX,
8797c478bd9Sstevel@tonic-gate 	    &task_lwps_ops);
8807c478bd9Sstevel@tonic-gate 	rc_task_cpu_time = rctl_register("task.max-cpu-time", RCENTITY_TASK,
8817c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_DENY_NEVER |
8827c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_CPU_TIME | RCTL_GLOBAL_INFINITE |
8837c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_UNOBSERVABLE | RCTL_GLOBAL_SECONDS, UINT64_MAX,
8847c478bd9Sstevel@tonic-gate 	    UINT64_MAX, &task_cpu_time_ops);
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	/*
8877c478bd9Sstevel@tonic-gate 	 * Create task0 and place p0 in it as a member.
8887c478bd9Sstevel@tonic-gate 	 */
8897c478bd9Sstevel@tonic-gate 	task0p = kmem_cache_alloc(task_cache, KM_SLEEP);
8907c478bd9Sstevel@tonic-gate 	bzero(task0p, sizeof (task_t));
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	task0p->tk_tkid = id_alloc(taskid_space);
8937c478bd9Sstevel@tonic-gate 	task0p->tk_usage = kmem_zalloc(sizeof (task_usage_t), KM_SLEEP);
8947eceb558Srh87107 	task0p->tk_inherited = kmem_zalloc(sizeof (task_usage_t), KM_SLEEP);
8950209230bSgjelinek 	task0p->tk_proj = project_hold_by_id(0, &zone0,
8967c478bd9Sstevel@tonic-gate 	    PROJECT_HOLD_INSERT);
8977c478bd9Sstevel@tonic-gate 	task0p->tk_flags = TASK_NORMAL;
8987c478bd9Sstevel@tonic-gate 	task0p->tk_nlwps = p->p_lwpcnt;
8997c478bd9Sstevel@tonic-gate 	task0p->tk_zone = global_zone;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	set = rctl_set_create();
9027c478bd9Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_TASK);
9037c478bd9Sstevel@tonic-gate 	mutex_enter(&curproc->p_lock);
9047c478bd9Sstevel@tonic-gate 	e.rcep_p.task = task0p;
9057c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_TASK;
9067c478bd9Sstevel@tonic-gate 	task0p->tk_rctls = rctl_set_init(RCENTITY_TASK, curproc, &e, set, gp);
9077c478bd9Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
9087c478bd9Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	(void) mod_hash_reserve(task_hash, &hndl);
9117c478bd9Sstevel@tonic-gate 	mutex_enter(&task_hash_lock);
9127c478bd9Sstevel@tonic-gate 	ASSERT(task_find(task0p->tk_tkid, GLOBAL_ZONEID) == NULL);
9137c478bd9Sstevel@tonic-gate 	if (mod_hash_insert_reserve(task_hash,
9147c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)task0p->tk_tkid,
9157c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t *)task0p, hndl) != 0) {
9167c478bd9Sstevel@tonic-gate 		mod_hash_cancel(task_hash, &hndl);
9177c478bd9Sstevel@tonic-gate 		panic("unable to insert task %d(%p)", task0p->tk_tkid,
9187c478bd9Sstevel@tonic-gate 		    (void *)task0p);
9197c478bd9Sstevel@tonic-gate 	}
9207c478bd9Sstevel@tonic-gate 	mutex_exit(&task_hash_lock);
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	task0p->tk_memb_list = p;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	/*
9257c478bd9Sstevel@tonic-gate 	 * Initialize task pointers for p0, including doubly linked list of task
9267c478bd9Sstevel@tonic-gate 	 * members.
9277c478bd9Sstevel@tonic-gate 	 */
9287c478bd9Sstevel@tonic-gate 	p->p_task = task0p;
9297c478bd9Sstevel@tonic-gate 	p->p_taskprev = p->p_tasknext = p;
9307c478bd9Sstevel@tonic-gate 	task_hold(task0p);
9317c478bd9Sstevel@tonic-gate }
932