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 /* 223792Sakolb * Copyright 2007 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)); 2050Sstevel@tonic-gate return (t->tk_cpu_time / hz); 2060Sstevel@tonic-gate } 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate /* 2090Sstevel@tonic-gate * static int task_test_cpu_secs(void *taskp, rctl_val_t *, int64_t incr, 2100Sstevel@tonic-gate * int flags) 2110Sstevel@tonic-gate * 2120Sstevel@tonic-gate * Overview 2130Sstevel@tonic-gate * task_test_cpu_secs() is the test-if-valid-increment for the resource 2140Sstevel@tonic-gate * control for the total accrued CPU seconds for a task. 2150Sstevel@tonic-gate * 2160Sstevel@tonic-gate * Return values 2170Sstevel@tonic-gate * 0 if the threshold limit was not passed, 1 if the limit was passed. 2180Sstevel@tonic-gate * 2190Sstevel@tonic-gate * Caller's context 2200Sstevel@tonic-gate * The given task must be held across the call. 2210Sstevel@tonic-gate */ 2220Sstevel@tonic-gate /*ARGSUSED*/ 2230Sstevel@tonic-gate static int 2240Sstevel@tonic-gate task_cpu_time_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, 2250Sstevel@tonic-gate struct rctl_val *rcntl, rctl_qty_t incr, uint_t flags) 2260Sstevel@tonic-gate { 2270Sstevel@tonic-gate task_t *t; 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 2300Sstevel@tonic-gate ASSERT(e->rcep_t == RCENTITY_TASK); 2310Sstevel@tonic-gate if (e->rcep_p.task == NULL) 2320Sstevel@tonic-gate return (0); 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate t = e->rcep_p.task; 2350Sstevel@tonic-gate if ((t->tk_cpu_time + incr) / hz >= rcntl->rcv_value) 2360Sstevel@tonic-gate return (1); 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate return (0); 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate static task_t * 2420Sstevel@tonic-gate task_find(taskid_t id, zoneid_t zoneid) 2430Sstevel@tonic-gate { 2440Sstevel@tonic-gate task_t *tk; 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate ASSERT(MUTEX_HELD(&task_hash_lock)); 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate if (mod_hash_find(task_hash, (mod_hash_key_t)(uintptr_t)id, 2490Sstevel@tonic-gate (mod_hash_val_t *)&tk) == MH_ERR_NOTFOUND || 2500Sstevel@tonic-gate (zoneid != ALL_ZONES && zoneid != tk->tk_zone->zone_id)) 2510Sstevel@tonic-gate return (NULL); 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate return (tk); 2540Sstevel@tonic-gate } 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate /* 2570Sstevel@tonic-gate * task_hold_by_id(), task_hold_by_id_zone() 2580Sstevel@tonic-gate * 2590Sstevel@tonic-gate * Overview 2600Sstevel@tonic-gate * task_hold_by_id() is used to take a reference on a task by its task id, 2610Sstevel@tonic-gate * supporting the various system call interfaces for obtaining resource data, 2620Sstevel@tonic-gate * delivering signals, and so forth. 2630Sstevel@tonic-gate * 2640Sstevel@tonic-gate * Return values 2650Sstevel@tonic-gate * Returns a pointer to the task_t with taskid_t id. The task is returned 2660Sstevel@tonic-gate * with its hold count incremented by one. Returns NULL if there 2670Sstevel@tonic-gate * is no task with the requested id. 2680Sstevel@tonic-gate * 2690Sstevel@tonic-gate * Caller's context 2700Sstevel@tonic-gate * Caller must not be holding task_hash_lock. No restrictions on context. 2710Sstevel@tonic-gate */ 2720Sstevel@tonic-gate task_t * 2730Sstevel@tonic-gate task_hold_by_id_zone(taskid_t id, zoneid_t zoneid) 2740Sstevel@tonic-gate { 2750Sstevel@tonic-gate task_t *tk; 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate mutex_enter(&task_hash_lock); 2780Sstevel@tonic-gate if ((tk = task_find(id, zoneid)) != NULL) 2790Sstevel@tonic-gate atomic_add_32(&tk->tk_hold_count, 1); 2800Sstevel@tonic-gate mutex_exit(&task_hash_lock); 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate return (tk); 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate task_t * 2860Sstevel@tonic-gate task_hold_by_id(taskid_t id) 2870Sstevel@tonic-gate { 2880Sstevel@tonic-gate zoneid_t zoneid; 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate if (INGLOBALZONE(curproc)) 2910Sstevel@tonic-gate zoneid = ALL_ZONES; 2920Sstevel@tonic-gate else 2930Sstevel@tonic-gate zoneid = getzoneid(); 2940Sstevel@tonic-gate return (task_hold_by_id_zone(id, zoneid)); 2950Sstevel@tonic-gate } 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate /* 2980Sstevel@tonic-gate * void task_hold(task_t *) 2990Sstevel@tonic-gate * 3000Sstevel@tonic-gate * Overview 3010Sstevel@tonic-gate * task_hold() is used to take an additional reference to the given task. 3020Sstevel@tonic-gate * 3030Sstevel@tonic-gate * Return values 3040Sstevel@tonic-gate * None. 3050Sstevel@tonic-gate * 3060Sstevel@tonic-gate * Caller's context 3070Sstevel@tonic-gate * No restriction on context. 3080Sstevel@tonic-gate */ 3090Sstevel@tonic-gate void 3100Sstevel@tonic-gate task_hold(task_t *tk) 3110Sstevel@tonic-gate { 3120Sstevel@tonic-gate atomic_add_32(&tk->tk_hold_count, 1); 3130Sstevel@tonic-gate } 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate /* 3160Sstevel@tonic-gate * void task_rele(task_t *) 3170Sstevel@tonic-gate * 3180Sstevel@tonic-gate * Overview 3190Sstevel@tonic-gate * task_rele() relinquishes a reference on the given task, which was acquired 3200Sstevel@tonic-gate * via task_hold() or task_hold_by_id(). If this is the last member or 3210Sstevel@tonic-gate * observer of the task, dispatch it for commitment via the accounting 3220Sstevel@tonic-gate * subsystem. 3230Sstevel@tonic-gate * 3240Sstevel@tonic-gate * Return values 3250Sstevel@tonic-gate * None. 3260Sstevel@tonic-gate * 3270Sstevel@tonic-gate * Caller's context 3280Sstevel@tonic-gate * Caller must not be holding the task_hash_lock. 3290Sstevel@tonic-gate * Caller's context must be acceptable for KM_SLEEP allocations. 3300Sstevel@tonic-gate */ 3310Sstevel@tonic-gate void 3320Sstevel@tonic-gate task_rele(task_t *tk) 3330Sstevel@tonic-gate { 3340Sstevel@tonic-gate mutex_enter(&task_hash_lock); 3350Sstevel@tonic-gate if (atomic_add_32_nv(&tk->tk_hold_count, -1) > 0) { 3360Sstevel@tonic-gate mutex_exit(&task_hash_lock); 3370Sstevel@tonic-gate return; 3380Sstevel@tonic-gate } 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate mutex_enter(&tk->tk_zone->zone_nlwps_lock); 3410Sstevel@tonic-gate tk->tk_proj->kpj_ntasks--; 3420Sstevel@tonic-gate mutex_exit(&tk->tk_zone->zone_nlwps_lock); 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate if (mod_hash_destroy(task_hash, 3450Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)tk->tk_tkid) != 0) 3460Sstevel@tonic-gate panic("unable to delete task %d", tk->tk_tkid); 3470Sstevel@tonic-gate mutex_exit(&task_hash_lock); 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate /* 3500Sstevel@tonic-gate * At this point, there are no members or observers of the task, so we 3510Sstevel@tonic-gate * can safely send it on for commitment to the accounting subsystem. 3520Sstevel@tonic-gate * The task will be destroyed in task_end() subsequent to commitment. 3530Sstevel@tonic-gate */ 3540Sstevel@tonic-gate (void) taskq_dispatch(exacct_queue, exacct_commit_task, tk, KM_SLEEP); 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate /* 3580Sstevel@tonic-gate * task_t *task_create(projid_t, zone *) 3590Sstevel@tonic-gate * 3600Sstevel@tonic-gate * Overview 3610Sstevel@tonic-gate * A process constructing a new task calls task_create() to construct and 3620Sstevel@tonic-gate * preinitialize the task for the appropriate destination project. Only one 3630Sstevel@tonic-gate * task, the primordial task0, is not created with task_create(). 3640Sstevel@tonic-gate * 3650Sstevel@tonic-gate * Return values 3660Sstevel@tonic-gate * None. 3670Sstevel@tonic-gate * 3680Sstevel@tonic-gate * Caller's context 3690Sstevel@tonic-gate * Caller's context should be safe for KM_SLEEP allocations. 3700Sstevel@tonic-gate * The caller should appropriately bump the kpj_ntasks counter on the 3710Sstevel@tonic-gate * project that contains this task. 3720Sstevel@tonic-gate */ 3730Sstevel@tonic-gate task_t * 3740Sstevel@tonic-gate task_create(projid_t projid, zone_t *zone) 3750Sstevel@tonic-gate { 3760Sstevel@tonic-gate task_t *tk = kmem_cache_alloc(task_cache, KM_SLEEP); 3770Sstevel@tonic-gate task_t *ancestor_tk; 3780Sstevel@tonic-gate taskid_t tkid; 3790Sstevel@tonic-gate task_usage_t *tu = kmem_zalloc(sizeof (task_usage_t), KM_SLEEP); 3800Sstevel@tonic-gate mod_hash_hndl_t hndl; 3810Sstevel@tonic-gate rctl_set_t *set = rctl_set_create(); 3820Sstevel@tonic-gate rctl_alloc_gp_t *gp; 3830Sstevel@tonic-gate rctl_entity_p_t e; 3840Sstevel@tonic-gate 3850Sstevel@tonic-gate bzero(tk, sizeof (task_t)); 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate tk->tk_tkid = tkid = id_alloc(taskid_space); 3880Sstevel@tonic-gate tk->tk_nlwps = 0; 3890Sstevel@tonic-gate tk->tk_nlwps_ctl = INT_MAX; 3900Sstevel@tonic-gate tk->tk_usage = tu; 391*4584Srh87107 tk->tk_inherited = kmem_zalloc(sizeof (task_usage_t), KM_SLEEP); 3923792Sakolb tk->tk_proj = project_hold_by_id(projid, zone, PROJECT_HOLD_INSERT); 3930Sstevel@tonic-gate tk->tk_flags = TASK_NORMAL; 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate /* 3960Sstevel@tonic-gate * Copy ancestor task's resource controls. 3970Sstevel@tonic-gate */ 3980Sstevel@tonic-gate zone_task_hold(zone); 3990Sstevel@tonic-gate mutex_enter(&curproc->p_lock); 4000Sstevel@tonic-gate ancestor_tk = curproc->p_task; 4010Sstevel@tonic-gate task_hold(ancestor_tk); 4020Sstevel@tonic-gate tk->tk_zone = zone; 4030Sstevel@tonic-gate mutex_exit(&curproc->p_lock); 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate for (;;) { 4060Sstevel@tonic-gate gp = rctl_set_dup_prealloc(ancestor_tk->tk_rctls); 4070Sstevel@tonic-gate 4080Sstevel@tonic-gate mutex_enter(&ancestor_tk->tk_rctls->rcs_lock); 4090Sstevel@tonic-gate if (rctl_set_dup_ready(ancestor_tk->tk_rctls, gp)) 4100Sstevel@tonic-gate break; 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate mutex_exit(&ancestor_tk->tk_rctls->rcs_lock); 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate rctl_prealloc_destroy(gp); 4150Sstevel@tonic-gate } 4160Sstevel@tonic-gate 4170Sstevel@tonic-gate /* 4180Sstevel@tonic-gate * At this point, curproc does not have the appropriate linkage 4190Sstevel@tonic-gate * through the task to the project. So, rctl_set_dup should only 4200Sstevel@tonic-gate * copy the rctls, and leave the callbacks for later. 4210Sstevel@tonic-gate */ 4220Sstevel@tonic-gate e.rcep_p.task = tk; 4230Sstevel@tonic-gate e.rcep_t = RCENTITY_TASK; 4240Sstevel@tonic-gate tk->tk_rctls = rctl_set_dup(ancestor_tk->tk_rctls, curproc, curproc, &e, 4250Sstevel@tonic-gate set, gp, RCD_DUP); 4260Sstevel@tonic-gate mutex_exit(&ancestor_tk->tk_rctls->rcs_lock); 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate rctl_prealloc_destroy(gp); 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate /* 4310Sstevel@tonic-gate * Record the ancestor task's ID for use by extended accounting. 4320Sstevel@tonic-gate */ 4330Sstevel@tonic-gate tu->tu_anctaskid = ancestor_tk->tk_tkid; 4340Sstevel@tonic-gate task_rele(ancestor_tk); 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate /* 4370Sstevel@tonic-gate * Put new task structure in the hash table. 4380Sstevel@tonic-gate */ 4390Sstevel@tonic-gate (void) mod_hash_reserve(task_hash, &hndl); 4400Sstevel@tonic-gate mutex_enter(&task_hash_lock); 4410Sstevel@tonic-gate ASSERT(task_find(tkid, getzoneid()) == NULL); 4420Sstevel@tonic-gate if (mod_hash_insert_reserve(task_hash, (mod_hash_key_t)(uintptr_t)tkid, 4430Sstevel@tonic-gate (mod_hash_val_t *)tk, hndl) != 0) { 4440Sstevel@tonic-gate mod_hash_cancel(task_hash, &hndl); 4450Sstevel@tonic-gate panic("unable to insert task %d(%p)", tkid, (void *)tk); 4460Sstevel@tonic-gate } 4470Sstevel@tonic-gate mutex_exit(&task_hash_lock); 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate return (tk); 4500Sstevel@tonic-gate } 4510Sstevel@tonic-gate 4520Sstevel@tonic-gate /* 4530Sstevel@tonic-gate * void task_attach(task_t *, proc_t *) 4540Sstevel@tonic-gate * 4550Sstevel@tonic-gate * Overview 4560Sstevel@tonic-gate * task_attach() is used to attach a process to a task; this operation is only 4570Sstevel@tonic-gate * performed as a result of a fork() or settaskid() system call. The proc_t's 4580Sstevel@tonic-gate * p_tasknext and p_taskprev fields will be set such that the proc_t is a 4590Sstevel@tonic-gate * member of the doubly-linked list of proc_t's that make up the task. 4600Sstevel@tonic-gate * 4610Sstevel@tonic-gate * Return values 4620Sstevel@tonic-gate * None. 4630Sstevel@tonic-gate * 4640Sstevel@tonic-gate * Caller's context 4650Sstevel@tonic-gate * pidlock and p->p_lock must be held on entry. 4660Sstevel@tonic-gate */ 4670Sstevel@tonic-gate void 4680Sstevel@tonic-gate task_attach(task_t *tk, proc_t *p) 4690Sstevel@tonic-gate { 4700Sstevel@tonic-gate proc_t *first, *prev; 4710Sstevel@tonic-gate rctl_entity_p_t e; 4720Sstevel@tonic-gate ASSERT(tk != NULL); 4730Sstevel@tonic-gate ASSERT(p != NULL); 4740Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock)); 4750Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 4760Sstevel@tonic-gate 4770Sstevel@tonic-gate if (tk->tk_memb_list == NULL) { 4780Sstevel@tonic-gate p->p_tasknext = p; 4790Sstevel@tonic-gate p->p_taskprev = p; 4800Sstevel@tonic-gate } else { 4810Sstevel@tonic-gate first = tk->tk_memb_list; 4820Sstevel@tonic-gate prev = first->p_taskprev; 4830Sstevel@tonic-gate first->p_taskprev = p; 4840Sstevel@tonic-gate p->p_tasknext = first; 4850Sstevel@tonic-gate p->p_taskprev = prev; 4860Sstevel@tonic-gate prev->p_tasknext = p; 4870Sstevel@tonic-gate } 4880Sstevel@tonic-gate tk->tk_memb_list = p; 4890Sstevel@tonic-gate task_hold(tk); 4900Sstevel@tonic-gate p->p_task = tk; 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate /* 4930Sstevel@tonic-gate * Now that the linkage from process to task and project is 4940Sstevel@tonic-gate * complete, do the required callbacks for the task and project 4950Sstevel@tonic-gate * rctl sets. 4960Sstevel@tonic-gate */ 4970Sstevel@tonic-gate e.rcep_p.proj = tk->tk_proj; 4980Sstevel@tonic-gate e.rcep_t = RCENTITY_PROJECT; 4990Sstevel@tonic-gate (void) rctl_set_dup(NULL, NULL, p, &e, tk->tk_proj->kpj_rctls, NULL, 5000Sstevel@tonic-gate RCD_CALLBACK); 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate e.rcep_p.task = tk; 5030Sstevel@tonic-gate e.rcep_t = RCENTITY_TASK; 5040Sstevel@tonic-gate (void) rctl_set_dup(NULL, NULL, p, &e, tk->tk_rctls, NULL, 5050Sstevel@tonic-gate RCD_CALLBACK); 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate } 5080Sstevel@tonic-gate 5090Sstevel@tonic-gate /* 5100Sstevel@tonic-gate * task_begin() 5110Sstevel@tonic-gate * 5120Sstevel@tonic-gate * Overview 5130Sstevel@tonic-gate * A process constructing a new task calls task_begin() to initialize the 5140Sstevel@tonic-gate * task, by attaching itself as a member. 5150Sstevel@tonic-gate * 5160Sstevel@tonic-gate * Return values 5170Sstevel@tonic-gate * None. 5180Sstevel@tonic-gate * 5190Sstevel@tonic-gate * Caller's context 5200Sstevel@tonic-gate * pidlock and p_lock must be held across the call to task_begin(). 5210Sstevel@tonic-gate */ 5220Sstevel@tonic-gate void 5230Sstevel@tonic-gate task_begin(task_t *tk, proc_t *p) 5240Sstevel@tonic-gate { 5250Sstevel@tonic-gate timestruc_t ts; 5260Sstevel@tonic-gate task_usage_t *tu; 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock)); 5290Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate mutex_enter(&tk->tk_usage_lock); 5320Sstevel@tonic-gate tu = tk->tk_usage; 5330Sstevel@tonic-gate gethrestime(&ts); 5340Sstevel@tonic-gate tu->tu_startsec = (uint64_t)ts.tv_sec; 5350Sstevel@tonic-gate tu->tu_startnsec = (uint64_t)ts.tv_nsec; 5360Sstevel@tonic-gate mutex_exit(&tk->tk_usage_lock); 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate /* 5390Sstevel@tonic-gate * Join process to the task as a member. 5400Sstevel@tonic-gate */ 5410Sstevel@tonic-gate task_attach(tk, p); 5420Sstevel@tonic-gate } 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate /* 5450Sstevel@tonic-gate * void task_detach(proc_t *) 5460Sstevel@tonic-gate * 5470Sstevel@tonic-gate * Overview 5480Sstevel@tonic-gate * task_detach() removes the specified process from its task. task_detach 5490Sstevel@tonic-gate * sets the process's task membership to NULL, in anticipation of a final exit 5500Sstevel@tonic-gate * or of joining a new task. Because task_rele() requires a context safe for 5510Sstevel@tonic-gate * KM_SLEEP allocations, a task_detach() is followed by a subsequent 5520Sstevel@tonic-gate * task_rele() once appropriate context is available. 5530Sstevel@tonic-gate * 5540Sstevel@tonic-gate * Because task_detach() involves relinquishing the process's membership in 5550Sstevel@tonic-gate * the project, any observational rctls the process may have had on the task 5560Sstevel@tonic-gate * or project are destroyed. 5570Sstevel@tonic-gate * 5580Sstevel@tonic-gate * Return values 5590Sstevel@tonic-gate * None. 5600Sstevel@tonic-gate * 5610Sstevel@tonic-gate * Caller's context 5620Sstevel@tonic-gate * pidlock and p_lock held across task_detach(). 5630Sstevel@tonic-gate */ 5640Sstevel@tonic-gate void 5650Sstevel@tonic-gate task_detach(proc_t *p) 5660Sstevel@tonic-gate { 5670Sstevel@tonic-gate task_t *tk = p->p_task; 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock)); 5700Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 5710Sstevel@tonic-gate ASSERT(p->p_task != NULL); 5720Sstevel@tonic-gate ASSERT(tk->tk_memb_list != NULL); 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate if (tk->tk_memb_list == p) 5750Sstevel@tonic-gate tk->tk_memb_list = p->p_tasknext; 5760Sstevel@tonic-gate if (tk->tk_memb_list == p) 5770Sstevel@tonic-gate tk->tk_memb_list = NULL; 5780Sstevel@tonic-gate p->p_taskprev->p_tasknext = p->p_tasknext; 5790Sstevel@tonic-gate p->p_tasknext->p_taskprev = p->p_taskprev; 5800Sstevel@tonic-gate 5810Sstevel@tonic-gate rctl_set_tearoff(p->p_task->tk_rctls, p); 5820Sstevel@tonic-gate rctl_set_tearoff(p->p_task->tk_proj->kpj_rctls, p); 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate p->p_task = NULL; 5850Sstevel@tonic-gate p->p_tasknext = p->p_taskprev = NULL; 5860Sstevel@tonic-gate } 5870Sstevel@tonic-gate 5880Sstevel@tonic-gate /* 5890Sstevel@tonic-gate * task_change(task_t *, proc_t *) 5900Sstevel@tonic-gate * 5910Sstevel@tonic-gate * Overview 5920Sstevel@tonic-gate * task_change() removes the specified process from its current task. The 5930Sstevel@tonic-gate * process is then attached to the specified task. This routine is called 5940Sstevel@tonic-gate * from settaskid() when process is being moved to a new task. 5950Sstevel@tonic-gate * 5960Sstevel@tonic-gate * Return values 5970Sstevel@tonic-gate * None. 5980Sstevel@tonic-gate * 5990Sstevel@tonic-gate * Caller's context 6000Sstevel@tonic-gate * pidlock and p_lock held across task_change() 6010Sstevel@tonic-gate */ 6020Sstevel@tonic-gate void 6030Sstevel@tonic-gate task_change(task_t *newtk, proc_t *p) 6040Sstevel@tonic-gate { 6050Sstevel@tonic-gate task_t *oldtk = p->p_task; 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock)); 6080Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 6090Sstevel@tonic-gate ASSERT(oldtk != NULL); 6100Sstevel@tonic-gate ASSERT(oldtk->tk_memb_list != NULL); 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate mutex_enter(&p->p_zone->zone_nlwps_lock); 6130Sstevel@tonic-gate oldtk->tk_nlwps -= p->p_lwpcnt; 6140Sstevel@tonic-gate mutex_exit(&p->p_zone->zone_nlwps_lock); 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate mutex_enter(&newtk->tk_zone->zone_nlwps_lock); 6170Sstevel@tonic-gate newtk->tk_nlwps += p->p_lwpcnt; 6180Sstevel@tonic-gate mutex_exit(&newtk->tk_zone->zone_nlwps_lock); 6190Sstevel@tonic-gate 6200Sstevel@tonic-gate task_detach(p); 6210Sstevel@tonic-gate task_begin(newtk, p); 622*4584Srh87107 exacct_move_mstate(p, oldtk, newtk); 6230Sstevel@tonic-gate } 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate /* 6260Sstevel@tonic-gate * task_end() 6270Sstevel@tonic-gate * 6280Sstevel@tonic-gate * Overview 6290Sstevel@tonic-gate * task_end() contains the actions executed once the final member of 6300Sstevel@tonic-gate * a task has released the task, and all actions connected with the task, such 6310Sstevel@tonic-gate * as committing an accounting record to a file, are completed. It is called 6320Sstevel@tonic-gate * by the known last consumer of the task information. Additionally, 6330Sstevel@tonic-gate * task_end() must never refer to any process in the system. 6340Sstevel@tonic-gate * 6350Sstevel@tonic-gate * Return values 6360Sstevel@tonic-gate * None. 6370Sstevel@tonic-gate * 6380Sstevel@tonic-gate * Caller's context 6390Sstevel@tonic-gate * No restrictions on context, beyond that given above. 6400Sstevel@tonic-gate */ 6410Sstevel@tonic-gate void 6420Sstevel@tonic-gate task_end(task_t *tk) 6430Sstevel@tonic-gate { 6440Sstevel@tonic-gate ASSERT(tk->tk_hold_count == 0); 6450Sstevel@tonic-gate 6460Sstevel@tonic-gate project_rele(tk->tk_proj); 6470Sstevel@tonic-gate kmem_free(tk->tk_usage, sizeof (task_usage_t)); 648*4584Srh87107 kmem_free(tk->tk_inherited, sizeof (task_usage_t)); 6490Sstevel@tonic-gate if (tk->tk_prevusage != NULL) 6500Sstevel@tonic-gate kmem_free(tk->tk_prevusage, sizeof (task_usage_t)); 6510Sstevel@tonic-gate if (tk->tk_zoneusage != NULL) 6520Sstevel@tonic-gate kmem_free(tk->tk_zoneusage, sizeof (task_usage_t)); 6530Sstevel@tonic-gate rctl_set_free(tk->tk_rctls); 6540Sstevel@tonic-gate id_free(taskid_space, tk->tk_tkid); 6550Sstevel@tonic-gate zone_task_rele(tk->tk_zone); 6560Sstevel@tonic-gate kmem_cache_free(task_cache, tk); 6570Sstevel@tonic-gate } 6580Sstevel@tonic-gate 6590Sstevel@tonic-gate static void 6600Sstevel@tonic-gate changeproj(proc_t *p, kproject_t *kpj, zone_t *zone, void *projbuf, 6610Sstevel@tonic-gate void *zonebuf) 6620Sstevel@tonic-gate { 6630Sstevel@tonic-gate kproject_t *oldkpj; 6640Sstevel@tonic-gate kthread_t *t; 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock)); 6670Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 6680Sstevel@tonic-gate 6690Sstevel@tonic-gate if ((t = p->p_tlist) != NULL) { 6700Sstevel@tonic-gate do { 6710Sstevel@tonic-gate (void) project_hold(kpj); 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate thread_lock(t); 6740Sstevel@tonic-gate oldkpj = ttoproj(t); 6753792Sakolb 6763792Sakolb /* 6773792Sakolb * Kick this thread so that he doesn't sit 6783792Sakolb * on a wrong wait queue. 6793792Sakolb */ 6803792Sakolb if (ISWAITING(t)) 6813792Sakolb setrun_locked(t); 6823792Sakolb 6833792Sakolb /* 6843792Sakolb * The thread wants to go on the project wait queue, but 6853792Sakolb * the waitq is changing. 6863792Sakolb */ 6873792Sakolb if (t->t_schedflag & TS_PROJWAITQ) 6883792Sakolb t->t_schedflag &= ~ TS_PROJWAITQ; 6893792Sakolb 6900Sstevel@tonic-gate t->t_proj = kpj; 6910Sstevel@tonic-gate t->t_pre_sys = 1; /* For cred update */ 6920Sstevel@tonic-gate thread_unlock(t); 6930Sstevel@tonic-gate fss_changeproj(t, kpj, zone, projbuf, zonebuf); 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate project_rele(oldkpj); 6960Sstevel@tonic-gate } while ((t = t->t_forw) != p->p_tlist); 6970Sstevel@tonic-gate } 6980Sstevel@tonic-gate } 6990Sstevel@tonic-gate 7000Sstevel@tonic-gate /* 7010Sstevel@tonic-gate * task_join() 7020Sstevel@tonic-gate * 7030Sstevel@tonic-gate * Overview 7040Sstevel@tonic-gate * task_join() contains the actions that must be executed when the first 7050Sstevel@tonic-gate * member (curproc) of a newly created task joins it. It may never fail. 7060Sstevel@tonic-gate * 7070Sstevel@tonic-gate * The caller must make sure holdlwps() is called so that all other lwps are 7080Sstevel@tonic-gate * stopped prior to calling this function. 7090Sstevel@tonic-gate * 7100Sstevel@tonic-gate * NB: It returns with curproc->p_lock held. 7110Sstevel@tonic-gate * 7120Sstevel@tonic-gate * Return values 7130Sstevel@tonic-gate * Pointer to the old task. 7140Sstevel@tonic-gate * 7150Sstevel@tonic-gate * Caller's context 7160Sstevel@tonic-gate * cpu_lock must be held entering the function. It will acquire pidlock, 7170Sstevel@tonic-gate * p_crlock and p_lock during execution. 7180Sstevel@tonic-gate */ 7190Sstevel@tonic-gate task_t * 7200Sstevel@tonic-gate task_join(task_t *tk, uint_t flags) 7210Sstevel@tonic-gate { 7220Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 7230Sstevel@tonic-gate task_t *prev_tk; 7240Sstevel@tonic-gate void *projbuf, *zonebuf; 7250Sstevel@tonic-gate zone_t *zone = tk->tk_zone; 7260Sstevel@tonic-gate projid_t projid = tk->tk_proj->kpj_id; 7270Sstevel@tonic-gate cred_t *oldcr; 7280Sstevel@tonic-gate 7290Sstevel@tonic-gate /* 7300Sstevel@tonic-gate * We can't know for sure if holdlwps() was called, but we can check to 7310Sstevel@tonic-gate * ensure we're single-threaded. 7320Sstevel@tonic-gate */ 7330Sstevel@tonic-gate ASSERT(curthread == p->p_agenttp || p->p_lwprcnt == 1); 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate /* 7360Sstevel@tonic-gate * Changing the credential is always hard because we cannot 7370Sstevel@tonic-gate * allocate memory when holding locks but we don't know whether 7380Sstevel@tonic-gate * we need to change it. We first get a reference to the current 7390Sstevel@tonic-gate * cred if we need to change it. Then we create a credential 7400Sstevel@tonic-gate * with an updated project id. Finally we install it, first 7410Sstevel@tonic-gate * releasing the reference we had on the p_cred at the time we 7420Sstevel@tonic-gate * acquired the lock the first time and later we release the 7430Sstevel@tonic-gate * reference to p_cred at the time we acquired the lock the 7440Sstevel@tonic-gate * second time. 7450Sstevel@tonic-gate */ 7460Sstevel@tonic-gate mutex_enter(&p->p_crlock); 7470Sstevel@tonic-gate if (crgetprojid(p->p_cred) == projid) 7480Sstevel@tonic-gate oldcr = NULL; 7490Sstevel@tonic-gate else 7500Sstevel@tonic-gate crhold(oldcr = p->p_cred); 7510Sstevel@tonic-gate mutex_exit(&p->p_crlock); 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate if (oldcr != NULL) { 7540Sstevel@tonic-gate cred_t *newcr = crdup(oldcr); 7550Sstevel@tonic-gate crsetprojid(newcr, projid); 7560Sstevel@tonic-gate crfree(oldcr); 7570Sstevel@tonic-gate 7580Sstevel@tonic-gate mutex_enter(&p->p_crlock); 7590Sstevel@tonic-gate oldcr = p->p_cred; 7600Sstevel@tonic-gate p->p_cred = newcr; 7610Sstevel@tonic-gate mutex_exit(&p->p_crlock); 7620Sstevel@tonic-gate crfree(oldcr); 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate /* 7660Sstevel@tonic-gate * Make sure that the number of processor sets is constant 7670Sstevel@tonic-gate * across this operation. 7680Sstevel@tonic-gate */ 7690Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate projbuf = fss_allocbuf(FSS_NPSET_BUF, FSS_ALLOC_PROJ); 7720Sstevel@tonic-gate zonebuf = fss_allocbuf(FSS_NPSET_BUF, FSS_ALLOC_ZONE); 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate mutex_enter(&pidlock); 7750Sstevel@tonic-gate mutex_enter(&p->p_lock); 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate prev_tk = p->p_task; 7780Sstevel@tonic-gate task_change(tk, p); 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate /* 7810Sstevel@tonic-gate * Now move threads one by one to their new project. 7820Sstevel@tonic-gate */ 7830Sstevel@tonic-gate changeproj(p, tk->tk_proj, zone, projbuf, zonebuf); 7840Sstevel@tonic-gate if (flags & TASK_FINAL) 7850Sstevel@tonic-gate p->p_task->tk_flags |= TASK_FINAL; 7860Sstevel@tonic-gate 7870Sstevel@tonic-gate mutex_exit(&pidlock); 7880Sstevel@tonic-gate 7890Sstevel@tonic-gate fss_freebuf(zonebuf, FSS_ALLOC_ZONE); 7900Sstevel@tonic-gate fss_freebuf(projbuf, FSS_ALLOC_PROJ); 7910Sstevel@tonic-gate return (prev_tk); 7920Sstevel@tonic-gate } 7930Sstevel@tonic-gate 7940Sstevel@tonic-gate /* 7950Sstevel@tonic-gate * rctl ops vectors 7960Sstevel@tonic-gate */ 7970Sstevel@tonic-gate static rctl_ops_t task_lwps_ops = { 7980Sstevel@tonic-gate rcop_no_action, 7990Sstevel@tonic-gate task_lwps_usage, 8000Sstevel@tonic-gate task_lwps_set, 8010Sstevel@tonic-gate task_lwps_test 8020Sstevel@tonic-gate }; 8030Sstevel@tonic-gate 8040Sstevel@tonic-gate static rctl_ops_t task_cpu_time_ops = { 8050Sstevel@tonic-gate rcop_no_action, 8060Sstevel@tonic-gate task_cpu_time_usage, 8070Sstevel@tonic-gate rcop_no_set, 8080Sstevel@tonic-gate task_cpu_time_test 8090Sstevel@tonic-gate }; 8100Sstevel@tonic-gate 8110Sstevel@tonic-gate /*ARGSUSED*/ 8120Sstevel@tonic-gate /* 8130Sstevel@tonic-gate * void task_init(void) 8140Sstevel@tonic-gate * 8150Sstevel@tonic-gate * Overview 8160Sstevel@tonic-gate * task_init() initializes task-related hashes, caches, and the task id 8170Sstevel@tonic-gate * space. Additionally, task_init() establishes p0 as a member of task0. 8180Sstevel@tonic-gate * Called by main(). 8190Sstevel@tonic-gate * 8200Sstevel@tonic-gate * Return values 8210Sstevel@tonic-gate * None. 8220Sstevel@tonic-gate * 8230Sstevel@tonic-gate * Caller's context 8240Sstevel@tonic-gate * task_init() must be called prior to MP startup. 8250Sstevel@tonic-gate */ 8260Sstevel@tonic-gate void 8270Sstevel@tonic-gate task_init(void) 8280Sstevel@tonic-gate { 8290Sstevel@tonic-gate proc_t *p = &p0; 8300Sstevel@tonic-gate mod_hash_hndl_t hndl; 8310Sstevel@tonic-gate rctl_set_t *set; 8320Sstevel@tonic-gate rctl_alloc_gp_t *gp; 8330Sstevel@tonic-gate rctl_entity_p_t e; 8340Sstevel@tonic-gate /* 8350Sstevel@tonic-gate * Initialize task_cache and taskid_space. 8360Sstevel@tonic-gate */ 8370Sstevel@tonic-gate task_cache = kmem_cache_create("task_cache", sizeof (task_t), 8380Sstevel@tonic-gate 0, NULL, NULL, NULL, NULL, NULL, 0); 8390Sstevel@tonic-gate taskid_space = id_space_create("taskid_space", 0, MAX_TASKID); 8400Sstevel@tonic-gate 8410Sstevel@tonic-gate /* 8420Sstevel@tonic-gate * Initialize task hash table. 8430Sstevel@tonic-gate */ 8440Sstevel@tonic-gate task_hash = mod_hash_create_idhash("task_hash", task_hash_size, 8450Sstevel@tonic-gate mod_hash_null_valdtor); 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate /* 8480Sstevel@tonic-gate * Initialize task-based rctls. 8490Sstevel@tonic-gate */ 8500Sstevel@tonic-gate rc_task_lwps = rctl_register("task.max-lwps", RCENTITY_TASK, 8510Sstevel@tonic-gate RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_COUNT, INT_MAX, INT_MAX, 8520Sstevel@tonic-gate &task_lwps_ops); 8530Sstevel@tonic-gate rc_task_cpu_time = rctl_register("task.max-cpu-time", RCENTITY_TASK, 8540Sstevel@tonic-gate RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_DENY_NEVER | 8550Sstevel@tonic-gate RCTL_GLOBAL_CPU_TIME | RCTL_GLOBAL_INFINITE | 8560Sstevel@tonic-gate RCTL_GLOBAL_UNOBSERVABLE | RCTL_GLOBAL_SECONDS, UINT64_MAX, 8570Sstevel@tonic-gate UINT64_MAX, &task_cpu_time_ops); 8580Sstevel@tonic-gate 8590Sstevel@tonic-gate /* 8600Sstevel@tonic-gate * Create task0 and place p0 in it as a member. 8610Sstevel@tonic-gate */ 8620Sstevel@tonic-gate task0p = kmem_cache_alloc(task_cache, KM_SLEEP); 8630Sstevel@tonic-gate bzero(task0p, sizeof (task_t)); 8640Sstevel@tonic-gate 8650Sstevel@tonic-gate task0p->tk_tkid = id_alloc(taskid_space); 8660Sstevel@tonic-gate task0p->tk_usage = kmem_zalloc(sizeof (task_usage_t), KM_SLEEP); 867*4584Srh87107 task0p->tk_inherited = kmem_zalloc(sizeof (task_usage_t), KM_SLEEP); 8683247Sgjelinek task0p->tk_proj = project_hold_by_id(0, &zone0, 8690Sstevel@tonic-gate PROJECT_HOLD_INSERT); 8700Sstevel@tonic-gate task0p->tk_flags = TASK_NORMAL; 8710Sstevel@tonic-gate task0p->tk_nlwps = p->p_lwpcnt; 8720Sstevel@tonic-gate task0p->tk_zone = global_zone; 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate set = rctl_set_create(); 8750Sstevel@tonic-gate gp = rctl_set_init_prealloc(RCENTITY_TASK); 8760Sstevel@tonic-gate mutex_enter(&curproc->p_lock); 8770Sstevel@tonic-gate e.rcep_p.task = task0p; 8780Sstevel@tonic-gate e.rcep_t = RCENTITY_TASK; 8790Sstevel@tonic-gate task0p->tk_rctls = rctl_set_init(RCENTITY_TASK, curproc, &e, set, gp); 8800Sstevel@tonic-gate mutex_exit(&curproc->p_lock); 8810Sstevel@tonic-gate rctl_prealloc_destroy(gp); 8820Sstevel@tonic-gate 8830Sstevel@tonic-gate (void) mod_hash_reserve(task_hash, &hndl); 8840Sstevel@tonic-gate mutex_enter(&task_hash_lock); 8850Sstevel@tonic-gate ASSERT(task_find(task0p->tk_tkid, GLOBAL_ZONEID) == NULL); 8860Sstevel@tonic-gate if (mod_hash_insert_reserve(task_hash, 8870Sstevel@tonic-gate (mod_hash_key_t)(uintptr_t)task0p->tk_tkid, 8880Sstevel@tonic-gate (mod_hash_val_t *)task0p, hndl) != 0) { 8890Sstevel@tonic-gate mod_hash_cancel(task_hash, &hndl); 8900Sstevel@tonic-gate panic("unable to insert task %d(%p)", task0p->tk_tkid, 8910Sstevel@tonic-gate (void *)task0p); 8920Sstevel@tonic-gate } 8930Sstevel@tonic-gate mutex_exit(&task_hash_lock); 8940Sstevel@tonic-gate 8950Sstevel@tonic-gate task0p->tk_memb_list = p; 8960Sstevel@tonic-gate 8970Sstevel@tonic-gate /* 8980Sstevel@tonic-gate * Initialize task pointers for p0, including doubly linked list of task 8990Sstevel@tonic-gate * members. 9000Sstevel@tonic-gate */ 9010Sstevel@tonic-gate p->p_task = task0p; 9020Sstevel@tonic-gate p->p_taskprev = p->p_tasknext = p; 9030Sstevel@tonic-gate task_hold(task0p); 9040Sstevel@tonic-gate } 905