Merge commit 'b1e7e97d3b60469b243b3b2e22c7d8cbd11c7c90'
[unleashed.git] / kernel / os / task.c
blob7afe998556ed9328b97cd47e5f3469085ba7d3dd
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2016 by Delphix. All rights reserved.
26 #include <sys/atomic.h>
27 #include <sys/callb.h>
28 #include <sys/cmn_err.h>
29 #include <sys/exacct.h>
30 #include <sys/id_space.h>
31 #include <sys/kmem.h>
32 #include <sys/kstat.h>
33 #include <sys/modhash.h>
34 #include <sys/mutex.h>
35 #include <sys/proc.h>
36 #include <sys/project.h>
37 #include <sys/rctl.h>
38 #include <sys/systm.h>
39 #include <sys/task.h>
40 #include <sys/time.h>
41 #include <sys/types.h>
42 #include <sys/zone.h>
43 #include <sys/cpuvar.h>
44 #include <sys/fss.h>
45 #include <sys/class.h>
46 #include <sys/project.h>
49 * Tasks
51 * A task is a collection of processes, associated with a common project ID
52 * and related by a common initial parent. The task primarily represents a
53 * natural process sequence with known resource usage, although it can also be
54 * viewed as a convenient grouping of processes for signal delivery, processor
55 * binding, and administrative operations.
57 * Membership and observership
58 * We can conceive of situations where processes outside of the task may wish
59 * to examine the resource usage of the task. Similarly, a number of the
60 * administrative operations on a task can be performed by processes who are
61 * not members of the task. Accordingly, we must design a locking strategy
62 * where observers of the task, who wish to examine or operate on the task,
63 * and members of task, who can perform the mentioned operations, as well as
64 * leave the task, see a consistent and correct representation of the task at
65 * all times.
67 * Locking
68 * Because the task membership is a new relation between processes, its
69 * locking becomes an additional responsibility of the pidlock/p_lock locking
70 * sequence; however, tasks closely resemble sessions and the session locking
71 * model is mostly appropriate for the interaction of tasks, processes, and
72 * procfs.
74 * kmutex_t task_hash_lock
75 * task_hash_lock is a global lock protecting the contents of the task
76 * ID-to-task pointer hash. Holders of task_hash_lock must not attempt to
77 * acquire pidlock or p_lock.
78 * uint_t tk_hold_count
79 * tk_hold_count, the number of members and observers of the current task,
80 * must be manipulated atomically.
81 * proc_t *tk_memb_list
82 * proc_t *p_tasknext
83 * proc_t *p_taskprev
84 * The task's membership list is protected by pidlock, and is therefore
85 * always acquired before any of its members' p_lock mutexes. The p_task
86 * member of the proc structure is protected by pidlock or p_lock for
87 * reading, and by both pidlock and p_lock for modification, as is done for
88 * p_sessp. The key point is that only the process can modify its p_task,
89 * and not any entity on the system. (/proc will use prlock() to prevent
90 * the process from leaving, as opposed to pidlock.)
91 * kmutex_t tk_usage_lock
92 * tk_usage_lock is a per-task lock protecting the contents of the task
93 * usage structure and tk_nlwps counter for the task.max-lwps resource
94 * control.
97 int task_hash_size = 256;
98 static kmutex_t task_hash_lock;
99 static mod_hash_t *task_hash;
101 static id_space_t *taskid_space; /* global taskid space */
102 static kmem_cache_t *task_cache; /* kmem cache for task structures */
104 rctl_hndl_t rc_task_lwps;
105 rctl_hndl_t rc_task_nprocs;
106 rctl_hndl_t rc_task_cpu_time;
109 * Resource usage is committed using task queues; if taskq_dispatch() fails
110 * due to resource constraints, the task is placed on a list for background
111 * processing by the task_commit_thread() backup thread.
113 static kmutex_t task_commit_lock; /* protects list pointers and cv */
114 static kcondvar_t task_commit_cv; /* wakeup task_commit_thread */
115 static task_t *task_commit_head = NULL;
116 static task_t *task_commit_tail = NULL;
117 kthread_t *task_commit_thread;
119 static void task_commit();
120 static kstat_t *task_kstat_create(task_t *, zone_t *);
121 static void task_kstat_delete(task_t *);
124 * static rctl_qty_t task_usage_lwps(void *taskp)
126 * Overview
127 * task_usage_lwps() is the usage operation for the resource control
128 * associated with the number of LWPs in a task.
130 * Return values
131 * The number of LWPs in the given task is returned.
133 * Caller's context
134 * The p->p_lock must be held across the call.
136 /*ARGSUSED*/
137 static rctl_qty_t
138 task_lwps_usage(rctl_t *r, proc_t *p)
140 task_t *t;
141 rctl_qty_t nlwps;
143 ASSERT(MUTEX_HELD(&p->p_lock));
145 t = p->p_task;
146 mutex_enter(&p->p_zone->zone_nlwps_lock);
147 nlwps = t->tk_nlwps;
148 mutex_exit(&p->p_zone->zone_nlwps_lock);
150 return (nlwps);
154 * static int task_test_lwps(void *taskp, rctl_val_t *, int64_t incr,
155 * int flags)
157 * Overview
158 * task_test_lwps() is the test-if-valid-increment for the resource control
159 * for the number of processes in a task.
161 * Return values
162 * 0 if the threshold limit was not passed, 1 if the limit was passed.
164 * Caller's context
165 * p->p_lock must be held across the call.
167 /*ARGSUSED*/
168 static int
169 task_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
170 rctl_qty_t incr,
171 uint_t flags)
173 rctl_qty_t nlwps;
175 ASSERT(MUTEX_HELD(&p->p_lock));
176 ASSERT(e->rcep_t == RCENTITY_TASK);
177 if (e->rcep_p.task == NULL)
178 return (0);
180 ASSERT(MUTEX_HELD(&(e->rcep_p.task->tk_zone->zone_nlwps_lock)));
181 nlwps = e->rcep_p.task->tk_nlwps;
183 if (nlwps + incr > rcntl->rcv_value)
184 return (1);
186 return (0);
189 /*ARGSUSED*/
190 static int
191 task_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv)
194 ASSERT(MUTEX_HELD(&p->p_lock));
195 ASSERT(e->rcep_t == RCENTITY_TASK);
196 if (e->rcep_p.task == NULL)
197 return (0);
199 e->rcep_p.task->tk_nlwps_ctl = nv;
200 return (0);
203 /*ARGSUSED*/
204 static rctl_qty_t
205 task_nprocs_usage(rctl_t *r, proc_t *p)
207 task_t *t;
208 rctl_qty_t nprocs;
210 ASSERT(MUTEX_HELD(&p->p_lock));
212 t = p->p_task;
213 mutex_enter(&p->p_zone->zone_nlwps_lock);
214 nprocs = t->tk_nprocs;
215 mutex_exit(&p->p_zone->zone_nlwps_lock);
217 return (nprocs);
220 /*ARGSUSED*/
221 static int
222 task_nprocs_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
223 rctl_qty_t incr, uint_t flags)
225 rctl_qty_t nprocs;
227 ASSERT(MUTEX_HELD(&p->p_lock));
228 ASSERT(e->rcep_t == RCENTITY_TASK);
229 if (e->rcep_p.task == NULL)
230 return (0);
232 ASSERT(MUTEX_HELD(&(e->rcep_p.task->tk_zone->zone_nlwps_lock)));
233 nprocs = e->rcep_p.task->tk_nprocs;
235 if (nprocs + incr > rcntl->rcv_value)
236 return (1);
238 return (0);
241 /*ARGSUSED*/
242 static int
243 task_nprocs_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
244 rctl_qty_t nv)
247 ASSERT(MUTEX_HELD(&p->p_lock));
248 ASSERT(e->rcep_t == RCENTITY_TASK);
249 if (e->rcep_p.task == NULL)
250 return (0);
252 e->rcep_p.task->tk_nprocs_ctl = nv;
253 return (0);
257 * static rctl_qty_t task_usage_cpu_secs(void *taskp)
259 * Overview
260 * task_usage_cpu_secs() is the usage operation for the resource control
261 * associated with the total accrued CPU seconds for a task.
263 * Return values
264 * The number of CPU seconds consumed by the task is returned.
266 * Caller's context
267 * The given task must be held across the call.
269 /*ARGSUSED*/
270 static rctl_qty_t
271 task_cpu_time_usage(rctl_t *r, proc_t *p)
273 task_t *t = p->p_task;
275 ASSERT(MUTEX_HELD(&p->p_lock));
276 return (t->tk_cpu_time);
280 * int task_cpu_time_incr(task_t *t, rctl_qty_t incr)
282 * Overview
283 * task_cpu_time_incr() increments the amount of CPU time used
284 * by this task.
286 * Return values
287 * 1 if a second or more time is accumulated
288 * 0 otherwise
290 * Caller's context
291 * This is called by the clock tick accounting function to charge
292 * CPU time to a task.
294 rctl_qty_t
295 task_cpu_time_incr(task_t *t, rctl_qty_t incr)
297 rctl_qty_t ret = 0;
299 mutex_enter(&t->tk_cpu_time_lock);
300 t->tk_cpu_ticks += incr;
301 if (t->tk_cpu_ticks >= hz) {
302 t->tk_cpu_time += t->tk_cpu_ticks / hz;
303 t->tk_cpu_ticks = t->tk_cpu_ticks % hz;
304 ret = t->tk_cpu_time;
306 mutex_exit(&t->tk_cpu_time_lock);
308 return (ret);
312 * static int task_test_cpu_secs(void *taskp, rctl_val_t *, int64_t incr,
313 * int flags)
315 * Overview
316 * task_test_cpu_secs() is the test-if-valid-increment for the resource
317 * control for the total accrued CPU seconds for a task.
319 * Return values
320 * 0 if the threshold limit was not passed, 1 if the limit was passed.
322 * Caller's context
323 * The given task must be held across the call.
325 /*ARGSUSED*/
326 static int
327 task_cpu_time_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
328 struct rctl_val *rcntl, rctl_qty_t incr, uint_t flags)
330 ASSERT(MUTEX_HELD(&p->p_lock));
331 ASSERT(e->rcep_t == RCENTITY_TASK);
332 if (e->rcep_p.task == NULL)
333 return (0);
335 if (incr >= rcntl->rcv_value)
336 return (1);
338 return (0);
341 static task_t *
342 task_find(taskid_t id, zoneid_t zoneid)
344 task_t *tk;
346 ASSERT(MUTEX_HELD(&task_hash_lock));
348 if (mod_hash_find(task_hash, (mod_hash_key_t)(uintptr_t)id,
349 (mod_hash_val_t *)&tk) == MH_ERR_NOTFOUND ||
350 (zoneid != ALL_ZONES && zoneid != tk->tk_zone->zone_id))
351 return (NULL);
353 return (tk);
357 * task_hold_by_id(), task_hold_by_id_zone()
359 * Overview
360 * task_hold_by_id() is used to take a reference on a task by its task id,
361 * supporting the various system call interfaces for obtaining resource data,
362 * delivering signals, and so forth.
364 * Return values
365 * Returns a pointer to the task_t with taskid_t id. The task is returned
366 * with its hold count incremented by one. Returns NULL if there
367 * is no task with the requested id.
369 * Caller's context
370 * Caller must not be holding task_hash_lock. No restrictions on context.
372 task_t *
373 task_hold_by_id_zone(taskid_t id, zoneid_t zoneid)
375 task_t *tk;
377 mutex_enter(&task_hash_lock);
378 if ((tk = task_find(id, zoneid)) != NULL)
379 atomic_inc_32(&tk->tk_hold_count);
380 mutex_exit(&task_hash_lock);
382 return (tk);
385 task_t *
386 task_hold_by_id(taskid_t id)
388 zoneid_t zoneid;
390 if (INGLOBALZONE(curproc))
391 zoneid = ALL_ZONES;
392 else
393 zoneid = getzoneid();
394 return (task_hold_by_id_zone(id, zoneid));
398 * void task_hold(task_t *)
400 * Overview
401 * task_hold() is used to take an additional reference to the given task.
403 * Return values
404 * None.
406 * Caller's context
407 * No restriction on context.
409 void
410 task_hold(task_t *tk)
412 atomic_inc_32(&tk->tk_hold_count);
416 * void task_rele(task_t *)
418 * Overview
419 * task_rele() relinquishes a reference on the given task, which was acquired
420 * via task_hold() or task_hold_by_id(). If this is the last member or
421 * observer of the task, dispatch it for commitment via the accounting
422 * subsystem.
424 * Return values
425 * None.
427 * Caller's context
428 * Caller must not be holding the task_hash_lock.
430 void
431 task_rele(task_t *tk)
433 mutex_enter(&task_hash_lock);
434 if (atomic_add_32_nv(&tk->tk_hold_count, -1) > 0) {
435 mutex_exit(&task_hash_lock);
436 return;
439 ASSERT(tk->tk_nprocs == 0);
441 mutex_enter(&tk->tk_zone->zone_nlwps_lock);
442 tk->tk_proj->kpj_ntasks--;
443 mutex_exit(&tk->tk_zone->zone_nlwps_lock);
445 task_kstat_delete(tk);
447 if (mod_hash_destroy(task_hash,
448 (mod_hash_key_t)(uintptr_t)tk->tk_tkid) != 0)
449 panic("unable to delete task %d", tk->tk_tkid);
450 mutex_exit(&task_hash_lock);
453 * At this point, there are no members or observers of the task, so we
454 * can safely send it on for commitment to the accounting subsystem.
455 * The task will be destroyed in task_end() subsequent to commitment.
456 * Since we may be called with pidlock held, taskq_dispatch() cannot
457 * sleep. Commitment is handled by a backup thread in case dispatching
458 * the task fails.
460 if (taskq_dispatch(exacct_queue, exacct_commit_task, tk,
461 TQ_NOSLEEP | TQ_NOQUEUE) == (uintptr_t)NULL) {
462 mutex_enter(&task_commit_lock);
463 if (task_commit_head == NULL) {
464 task_commit_head = task_commit_tail = tk;
465 } else {
466 task_commit_tail->tk_commit_next = tk;
467 task_commit_tail = tk;
469 cv_signal(&task_commit_cv);
470 mutex_exit(&task_commit_lock);
475 * task_t *task_create(projid_t, zone *)
477 * Overview
478 * A process constructing a new task calls task_create() to construct and
479 * preinitialize the task for the appropriate destination project. Only one
480 * task, the primordial task0, is not created with task_create().
482 * Return values
483 * None.
485 * Caller's context
486 * Caller's context should be safe for KM_SLEEP allocations.
487 * The caller should appropriately bump the kpj_ntasks counter on the
488 * project that contains this task.
490 task_t *
491 task_create(projid_t projid, zone_t *zone)
493 task_t *tk = kmem_cache_alloc(task_cache, KM_SLEEP);
494 task_t *ancestor_tk;
495 taskid_t tkid;
496 task_usage_t *tu = kmem_zalloc(sizeof (task_usage_t), KM_SLEEP);
497 mod_hash_hndl_t hndl;
498 rctl_set_t *set = rctl_set_create();
499 rctl_alloc_gp_t *gp;
500 rctl_entity_p_t e;
502 bzero(tk, sizeof (task_t));
504 tk->tk_tkid = tkid = id_alloc(taskid_space);
505 tk->tk_nlwps = 0;
506 tk->tk_nlwps_ctl = INT_MAX;
507 tk->tk_nprocs = 0;
508 tk->tk_nprocs_ctl = INT_MAX;
509 tk->tk_usage = tu;
510 tk->tk_inherited = kmem_zalloc(sizeof (task_usage_t), KM_SLEEP);
511 tk->tk_proj = project_hold_by_id(projid, zone, PROJECT_HOLD_INSERT);
512 tk->tk_flags = TASK_NORMAL;
513 tk->tk_commit_next = NULL;
516 * Copy ancestor task's resource controls.
518 zone_task_hold(zone);
519 mutex_enter(&curproc->p_lock);
520 ancestor_tk = curproc->p_task;
521 task_hold(ancestor_tk);
522 tk->tk_zone = zone;
523 mutex_exit(&curproc->p_lock);
525 for (;;) {
526 gp = rctl_set_dup_prealloc(ancestor_tk->tk_rctls);
528 mutex_enter(&ancestor_tk->tk_rctls->rcs_lock);
529 if (rctl_set_dup_ready(ancestor_tk->tk_rctls, gp))
530 break;
532 mutex_exit(&ancestor_tk->tk_rctls->rcs_lock);
534 rctl_prealloc_destroy(gp);
538 * At this point, curproc does not have the appropriate linkage
539 * through the task to the project. So, rctl_set_dup should only
540 * copy the rctls, and leave the callbacks for later.
542 e.rcep_p.task = tk;
543 e.rcep_t = RCENTITY_TASK;
544 tk->tk_rctls = rctl_set_dup(ancestor_tk->tk_rctls, curproc, curproc, &e,
545 set, gp, RCD_DUP);
546 mutex_exit(&ancestor_tk->tk_rctls->rcs_lock);
548 rctl_prealloc_destroy(gp);
551 * Record the ancestor task's ID for use by extended accounting.
553 tu->tu_anctaskid = ancestor_tk->tk_tkid;
554 task_rele(ancestor_tk);
557 * Put new task structure in the hash table.
559 (void) mod_hash_reserve(task_hash, &hndl);
560 mutex_enter(&task_hash_lock);
561 ASSERT(task_find(tkid, zone->zone_id) == NULL);
562 if (mod_hash_insert_reserve(task_hash, (mod_hash_key_t)(uintptr_t)tkid,
563 (mod_hash_val_t *)tk, hndl) != 0) {
564 mod_hash_cancel(task_hash, &hndl);
565 panic("unable to insert task %d(%p)", tkid, (void *)tk);
567 mutex_exit(&task_hash_lock);
569 tk->tk_nprocs_kstat = task_kstat_create(tk, zone);
570 return (tk);
574 * void task_attach(task_t *, proc_t *)
576 * Overview
577 * task_attach() is used to attach a process to a task; this operation is only
578 * performed as a result of a fork() or settaskid() system call. The proc_t's
579 * p_tasknext and p_taskprev fields will be set such that the proc_t is a
580 * member of the doubly-linked list of proc_t's that make up the task.
582 * Return values
583 * None.
585 * Caller's context
586 * pidlock and p->p_lock must be held on entry.
588 void
589 task_attach(task_t *tk, proc_t *p)
591 proc_t *first, *prev;
592 ASSERT(tk != NULL);
593 ASSERT(p != NULL);
594 ASSERT(MUTEX_HELD(&pidlock));
595 ASSERT(MUTEX_HELD(&p->p_lock));
597 if (tk->tk_memb_list == NULL) {
598 p->p_tasknext = p;
599 p->p_taskprev = p;
600 } else {
601 first = tk->tk_memb_list;
602 prev = first->p_taskprev;
603 first->p_taskprev = p;
604 p->p_tasknext = first;
605 p->p_taskprev = prev;
606 prev->p_tasknext = p;
608 tk->tk_memb_list = p;
609 task_hold(tk);
610 p->p_task = tk;
614 * task_begin()
616 * Overview
617 * A process constructing a new task calls task_begin() to initialize the
618 * task, by attaching itself as a member.
620 * Return values
621 * None.
623 * Caller's context
624 * pidlock and p_lock must be held across the call to task_begin().
626 void
627 task_begin(task_t *tk, proc_t *p)
629 timestruc_t ts;
630 task_usage_t *tu;
631 rctl_entity_p_t e;
633 ASSERT(MUTEX_HELD(&pidlock));
634 ASSERT(MUTEX_HELD(&p->p_lock));
636 mutex_enter(&tk->tk_usage_lock);
637 tu = tk->tk_usage;
638 gethrestime(&ts);
639 tu->tu_startsec = (uint64_t)ts.tv_sec;
640 tu->tu_startnsec = (uint64_t)ts.tv_nsec;
641 mutex_exit(&tk->tk_usage_lock);
644 * Join process to the task as a member.
646 task_attach(tk, p);
649 * Now that the linkage from process to task is complete, do the
650 * required callback for the task rctl set.
652 e.rcep_p.task = tk;
653 e.rcep_t = RCENTITY_TASK;
654 (void) rctl_set_dup(NULL, NULL, p, &e, tk->tk_rctls, NULL,
655 RCD_CALLBACK);
659 * void task_detach(proc_t *)
661 * Overview
662 * task_detach() removes the specified process from its task. task_detach
663 * sets the process's task membership to NULL, in anticipation of a final exit
664 * or of joining a new task. Because task_rele() requires a context safe for
665 * KM_SLEEP allocations, a task_detach() is followed by a subsequent
666 * task_rele() once appropriate context is available.
668 * Because task_detach() involves relinquishing the process's membership in
669 * the project, any observational rctls the process may have had on the task
670 * or project are destroyed.
672 * Return values
673 * None.
675 * Caller's context
676 * pidlock and p_lock held across task_detach().
678 void
679 task_detach(proc_t *p)
681 task_t *tk = p->p_task;
683 ASSERT(MUTEX_HELD(&pidlock));
684 ASSERT(MUTEX_HELD(&p->p_lock));
685 ASSERT(p->p_task != NULL);
686 ASSERT(tk->tk_memb_list != NULL);
688 if (tk->tk_memb_list == p)
689 tk->tk_memb_list = p->p_tasknext;
690 if (tk->tk_memb_list == p)
691 tk->tk_memb_list = NULL;
692 p->p_taskprev->p_tasknext = p->p_tasknext;
693 p->p_tasknext->p_taskprev = p->p_taskprev;
695 rctl_set_tearoff(p->p_task->tk_rctls, p);
696 rctl_set_tearoff(p->p_task->tk_proj->kpj_rctls, p);
698 p->p_task = NULL;
699 p->p_tasknext = p->p_taskprev = NULL;
703 * task_change(task_t *, proc_t *)
705 * Overview
706 * task_change() removes the specified process from its current task. The
707 * process is then attached to the specified task. This routine is called
708 * from settaskid() when process is being moved to a new task.
710 * Return values
711 * None.
713 * Caller's context
714 * pidlock and p_lock held across task_change()
716 void
717 task_change(task_t *newtk, proc_t *p)
719 task_t *oldtk = p->p_task;
721 ASSERT(MUTEX_HELD(&pidlock));
722 ASSERT(MUTEX_HELD(&p->p_lock));
723 ASSERT(oldtk != NULL);
724 ASSERT(oldtk->tk_memb_list != NULL);
726 mutex_enter(&oldtk->tk_zone->zone_nlwps_lock);
727 oldtk->tk_nlwps -= p->p_lwpcnt;
728 oldtk->tk_nprocs--;
729 mutex_exit(&oldtk->tk_zone->zone_nlwps_lock);
731 mutex_enter(&newtk->tk_zone->zone_nlwps_lock);
732 newtk->tk_nlwps += p->p_lwpcnt;
733 newtk->tk_nprocs++;
734 mutex_exit(&newtk->tk_zone->zone_nlwps_lock);
736 task_detach(p);
737 task_begin(newtk, p);
738 exacct_move_mstate(p, oldtk, newtk);
742 * task_end()
744 * Overview
745 * task_end() contains the actions executed once the final member of
746 * a task has released the task, and all actions connected with the task, such
747 * as committing an accounting record to a file, are completed. It is called
748 * by the known last consumer of the task information. Additionally,
749 * task_end() must never refer to any process in the system.
751 * Return values
752 * None.
754 * Caller's context
755 * No restrictions on context, beyond that given above.
757 void
758 task_end(task_t *tk)
760 ASSERT(tk->tk_hold_count == 0);
762 project_rele(tk->tk_proj);
763 kmem_free(tk->tk_usage, sizeof (task_usage_t));
764 kmem_free(tk->tk_inherited, sizeof (task_usage_t));
765 if (tk->tk_prevusage != NULL)
766 kmem_free(tk->tk_prevusage, sizeof (task_usage_t));
767 if (tk->tk_zoneusage != NULL)
768 kmem_free(tk->tk_zoneusage, sizeof (task_usage_t));
769 rctl_set_free(tk->tk_rctls);
770 id_free(taskid_space, tk->tk_tkid);
771 zone_task_rele(tk->tk_zone);
772 kmem_cache_free(task_cache, tk);
775 static void
776 changeproj(proc_t *p, kproject_t *kpj, zone_t *zone, void *projbuf,
777 void *zonebuf)
779 kproject_t *oldkpj;
780 kthread_t *t;
782 ASSERT(MUTEX_HELD(&pidlock));
783 ASSERT(MUTEX_HELD(&p->p_lock));
785 if ((t = p->p_tlist) != NULL) {
786 do {
787 (void) project_hold(kpj);
789 thread_lock(t);
790 oldkpj = ttoproj(t);
793 * Kick this thread so that it doesn't sit
794 * on a wrong wait queue.
796 if (ISWAITING(t))
797 setrun_locked(t);
800 * The thread wants to go on the project wait queue, but
801 * the waitq is changing.
803 if (t->t_schedflag & TS_PROJWAITQ)
804 t->t_schedflag &= ~ TS_PROJWAITQ;
806 t->t_proj = kpj;
807 t->t_pre_sys = 1; /* For cred update */
808 thread_unlock(t);
809 fss_changeproj(t, kpj, zone, projbuf, zonebuf);
811 project_rele(oldkpj);
812 } while ((t = t->t_forw) != p->p_tlist);
817 * task_join()
819 * Overview
820 * task_join() contains the actions that must be executed when the first
821 * member (curproc) of a newly created task joins it. It may never fail.
823 * The caller must make sure holdlwps() is called so that all other lwps are
824 * stopped prior to calling this function.
826 * NB: It returns with curproc->p_lock held.
828 * Return values
829 * Pointer to the old task.
831 * Caller's context
832 * cpu_lock must be held entering the function. It will acquire pidlock,
833 * p_crlock and p_lock during execution.
835 task_t *
836 task_join(task_t *tk, uint_t flags)
838 proc_t *p = ttoproc(curthread);
839 task_t *prev_tk;
840 void *projbuf, *zonebuf;
841 zone_t *zone = tk->tk_zone;
842 projid_t projid = tk->tk_proj->kpj_id;
843 cred_t *oldcr;
846 * We can't know for sure if holdlwps() was called, but we can check to
847 * ensure we're single-threaded.
849 ASSERT(curthread == p->p_agenttp || p->p_lwprcnt == 1);
852 * Changing the credential is always hard because we cannot
853 * allocate memory when holding locks but we don't know whether
854 * we need to change it. We first get a reference to the current
855 * cred if we need to change it. Then we create a credential
856 * with an updated project id. Finally we install it, first
857 * releasing the reference we had on the p_cred at the time we
858 * acquired the lock the first time and later we release the
859 * reference to p_cred at the time we acquired the lock the
860 * second time.
862 mutex_enter(&p->p_crlock);
863 if (crgetprojid(p->p_cred) == projid)
864 oldcr = NULL;
865 else
866 crhold(oldcr = p->p_cred);
867 mutex_exit(&p->p_crlock);
869 if (oldcr != NULL) {
870 cred_t *newcr = crdup(oldcr);
871 crsetprojid(newcr, projid);
872 crfree(oldcr);
874 mutex_enter(&p->p_crlock);
875 oldcr = p->p_cred;
876 p->p_cred = newcr;
877 mutex_exit(&p->p_crlock);
878 crfree(oldcr);
882 * Make sure that the number of processor sets is constant
883 * across this operation.
885 ASSERT(MUTEX_HELD(&cpu_lock));
887 projbuf = fss_allocbuf(FSS_NPSET_BUF, FSS_ALLOC_PROJ);
888 zonebuf = fss_allocbuf(FSS_NPSET_BUF, FSS_ALLOC_ZONE);
890 mutex_enter(&pidlock);
891 mutex_enter(&p->p_lock);
893 prev_tk = p->p_task;
894 task_change(tk, p);
897 * Now move threads one by one to their new project.
899 changeproj(p, tk->tk_proj, zone, projbuf, zonebuf);
900 if (flags & TASK_FINAL)
901 p->p_task->tk_flags |= TASK_FINAL;
903 mutex_exit(&pidlock);
905 fss_freebuf(zonebuf, FSS_ALLOC_ZONE);
906 fss_freebuf(projbuf, FSS_ALLOC_PROJ);
907 return (prev_tk);
911 * rctl ops vectors
913 static rctl_ops_t task_lwps_ops = {
914 rcop_no_action,
915 task_lwps_usage,
916 task_lwps_set,
917 task_lwps_test
920 static rctl_ops_t task_procs_ops = {
921 rcop_no_action,
922 task_nprocs_usage,
923 task_nprocs_set,
924 task_nprocs_test
927 static rctl_ops_t task_cpu_time_ops = {
928 rcop_no_action,
929 task_cpu_time_usage,
930 rcop_no_set,
931 task_cpu_time_test
934 /*ARGSUSED*/
936 * void task_init(void)
938 * Overview
939 * task_init() initializes task-related hashes, caches, and the task id
940 * space. Additionally, task_init() establishes p0 as a member of task0.
941 * Called by main().
943 * Return values
944 * None.
946 * Caller's context
947 * task_init() must be called prior to MP startup.
949 void
950 task_init(void)
952 proc_t *p = &p0;
953 mod_hash_hndl_t hndl;
954 rctl_set_t *set;
955 rctl_alloc_gp_t *gp;
956 rctl_entity_p_t e;
959 * Initialize task_cache and taskid_space.
961 task_cache = kmem_cache_create("task_cache", sizeof (task_t),
962 0, NULL, NULL, NULL, NULL, NULL, 0);
963 taskid_space = id_space_create("taskid_space", 0, MAX_TASKID);
966 * Initialize task hash table.
968 task_hash = mod_hash_create_idhash("task_hash", task_hash_size,
969 mod_hash_null_valdtor);
972 * Initialize task-based rctls.
974 rc_task_lwps = rctl_register("task.max-lwps", RCENTITY_TASK,
975 RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_COUNT, INT_MAX, INT_MAX,
976 &task_lwps_ops);
977 rc_task_nprocs = rctl_register("task.max-processes", RCENTITY_TASK,
978 RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_COUNT, INT_MAX, INT_MAX,
979 &task_procs_ops);
980 rc_task_cpu_time = rctl_register("task.max-cpu-time", RCENTITY_TASK,
981 RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_DENY_NEVER |
982 RCTL_GLOBAL_CPU_TIME | RCTL_GLOBAL_INFINITE |
983 RCTL_GLOBAL_UNOBSERVABLE | RCTL_GLOBAL_SECONDS, UINT64_MAX,
984 UINT64_MAX, &task_cpu_time_ops);
987 * Create task0 and place p0 in it as a member.
989 task0p = kmem_cache_alloc(task_cache, KM_SLEEP);
990 bzero(task0p, sizeof (task_t));
992 task0p->tk_tkid = id_alloc(taskid_space);
993 task0p->tk_usage = kmem_zalloc(sizeof (task_usage_t), KM_SLEEP);
994 task0p->tk_inherited = kmem_zalloc(sizeof (task_usage_t), KM_SLEEP);
995 task0p->tk_proj = project_hold_by_id(0, &zone0,
996 PROJECT_HOLD_INSERT);
997 task0p->tk_flags = TASK_NORMAL;
998 task0p->tk_nlwps = p->p_lwpcnt;
999 task0p->tk_nprocs = 1;
1000 task0p->tk_zone = global_zone;
1001 task0p->tk_commit_next = NULL;
1003 set = rctl_set_create();
1004 gp = rctl_set_init_prealloc(RCENTITY_TASK);
1005 mutex_enter(&curproc->p_lock);
1006 e.rcep_p.task = task0p;
1007 e.rcep_t = RCENTITY_TASK;
1008 task0p->tk_rctls = rctl_set_init(RCENTITY_TASK, curproc, &e, set, gp);
1009 mutex_exit(&curproc->p_lock);
1010 rctl_prealloc_destroy(gp);
1012 (void) mod_hash_reserve(task_hash, &hndl);
1013 mutex_enter(&task_hash_lock);
1014 ASSERT(task_find(task0p->tk_tkid, GLOBAL_ZONEID) == NULL);
1015 if (mod_hash_insert_reserve(task_hash,
1016 (mod_hash_key_t)(uintptr_t)task0p->tk_tkid,
1017 (mod_hash_val_t *)task0p, hndl) != 0) {
1018 mod_hash_cancel(task_hash, &hndl);
1019 panic("unable to insert task %d(%p)", task0p->tk_tkid,
1020 (void *)task0p);
1022 mutex_exit(&task_hash_lock);
1024 task0p->tk_memb_list = p;
1026 task0p->tk_nprocs_kstat = task_kstat_create(task0p, task0p->tk_zone);
1029 * Initialize task pointers for p0, including doubly linked list of task
1030 * members.
1032 p->p_task = task0p;
1033 p->p_taskprev = p->p_tasknext = p;
1034 task_hold(task0p);
1037 static int
1038 task_nprocs_kstat_update(kstat_t *ksp, int rw)
1040 task_t *tk = ksp->ks_private;
1041 task_kstat_t *ktk = ksp->ks_data;
1043 if (rw == KSTAT_WRITE)
1044 return (EACCES);
1046 ktk->ktk_usage.value.ui64 = tk->tk_nprocs;
1047 ktk->ktk_value.value.ui64 = tk->tk_nprocs_ctl;
1048 return (0);
1051 static kstat_t *
1052 task_kstat_create(task_t *tk, zone_t *zone)
1054 kstat_t *ksp;
1055 task_kstat_t *ktk;
1056 char *zonename = zone->zone_name;
1058 ksp = rctl_kstat_create_task(tk, "nprocs", KSTAT_TYPE_NAMED,
1059 sizeof (task_kstat_t) / sizeof (kstat_named_t),
1060 KSTAT_FLAG_VIRTUAL);
1062 if (ksp == NULL)
1063 return (NULL);
1065 ktk = ksp->ks_data = kmem_alloc(sizeof (task_kstat_t), KM_SLEEP);
1066 ksp->ks_data_size += strlen(zonename) + 1;
1067 kstat_named_init(&ktk->ktk_zonename, "zonename", KSTAT_DATA_STRING);
1068 kstat_named_setstr(&ktk->ktk_zonename, zonename);
1069 kstat_named_init(&ktk->ktk_usage, "usage", KSTAT_DATA_UINT64);
1070 kstat_named_init(&ktk->ktk_value, "value", KSTAT_DATA_UINT64);
1071 ksp->ks_update = task_nprocs_kstat_update;
1072 ksp->ks_private = tk;
1073 kstat_install(ksp);
1075 return (ksp);
1078 static void
1079 task_kstat_delete(task_t *tk)
1081 void *data;
1083 if (tk->tk_nprocs_kstat != NULL) {
1084 data = tk->tk_nprocs_kstat->ks_data;
1085 kstat_delete(tk->tk_nprocs_kstat);
1086 kmem_free(data, sizeof (task_kstat_t));
1087 tk->tk_nprocs_kstat = NULL;
1091 void
1092 task_commit_thread_init()
1094 mutex_init(&task_commit_lock, NULL, MUTEX_DEFAULT, NULL);
1095 cv_init(&task_commit_cv, NULL, CV_DEFAULT, NULL);
1096 task_commit_thread = thread_create(NULL, 0, task_commit, NULL, 0,
1097 &p0, TS_RUN, minclsyspri);
1101 * Backup thread to commit task resource usage when taskq_dispatch() fails.
1103 static void
1104 task_commit()
1106 callb_cpr_t cprinfo;
1108 CALLB_CPR_INIT(&cprinfo, &task_commit_lock, callb_generic_cpr,
1109 "task_commit_thread");
1111 mutex_enter(&task_commit_lock);
1113 for (;;) {
1114 while (task_commit_head == NULL) {
1115 CALLB_CPR_SAFE_BEGIN(&cprinfo);
1116 cv_wait(&task_commit_cv, &task_commit_lock);
1117 CALLB_CPR_SAFE_END(&cprinfo, &task_commit_lock);
1119 while (task_commit_head != NULL) {
1120 task_t *tk;
1122 tk = task_commit_head;
1123 task_commit_head = task_commit_head->tk_commit_next;
1124 if (task_commit_head == NULL)
1125 task_commit_tail = NULL;
1126 mutex_exit(&task_commit_lock);
1127 exacct_commit_task(tk);
1128 mutex_enter(&task_commit_lock);