task.c revision ff19e029e81c950f4e0f40f1f1ee1f7d8f8d8041
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
#include <sys/id_space.h>
/*
* Tasks
*
* A task is a collection of processes, associated with a common project ID
* and related by a common initial parent. The task primarily represents a
* natural process sequence with known resource usage, although it can also be
* viewed as a convenient grouping of processes for signal delivery, processor
* binding, and administrative operations.
*
* Membership and observership
* We can conceive of situations where processes outside of the task may wish
* to examine the resource usage of the task. Similarly, a number of the
* administrative operations on a task can be performed by processes who are
* not members of the task. Accordingly, we must design a locking strategy
* where observers of the task, who wish to examine or operate on the task,
* and members of task, who can perform the mentioned operations, as well as
* leave the task, see a consistent and correct representation of the task at
* all times.
*
* Locking
* Because the task membership is a new relation between processes, its
* sequence; however, tasks closely resemble sessions and the session locking
* model is mostly appropriate for the interaction of tasks, processes, and
* procfs.
*
* kmutex_t task_hash_lock
* task_hash_lock is a global lock protecting the contents of the task
* ID-to-task pointer hash. Holders of task_hash_lock must not attempt to
* acquire pidlock or p_lock.
* uint_t tk_hold_count
* tk_hold_count, the number of members and observers of the current task,
* must be manipulated atomically.
* proc_t *tk_memb_list
* proc_t *p_tasknext
* proc_t *p_taskprev
* The task's membership list is protected by pidlock, and is therefore
* always acquired before any of its members' p_lock mutexes. The p_task
* member of the proc structure is protected by pidlock or p_lock for
* reading, and by both pidlock and p_lock for modification, as is done for
* p_sessp. The key point is that only the process can modify its p_task,
* and not any entity on the system. (/proc will use prlock() to prevent
* the process from leaving, as opposed to pidlock.)
* kmutex_t tk_usage_lock
* tk_usage_lock is a per-task lock protecting the contents of the task
* usage structure and tk_nlwps counter for the task.max-lwps resource
* control.
*/
int task_hash_size = 256;
static kmutex_t task_hash_lock;
static mod_hash_t *task_hash;
/*
* Resource usage is committed using task queues; if taskq_dispatch() fails
* due to resource constraints, the task is placed on a list for background
* processing by the task_commit_thread() backup thread.
*/
static void task_commit();
static void task_kstat_delete(task_t *);
/*
* static rctl_qty_t task_usage_lwps(void *taskp)
*
* Overview
* task_usage_lwps() is the usage operation for the resource control
* associated with the number of LWPs in a task.
*
* Return values
* The number of LWPs in the given task is returned.
*
* Caller's context
* The p->p_lock must be held across the call.
*/
/*ARGSUSED*/
static rctl_qty_t
{
task_t *t;
t = p->p_task;
return (nlwps);
}
/*
* static int task_test_lwps(void *taskp, rctl_val_t *, int64_t incr,
* int flags)
*
* Overview
* task_test_lwps() is the test-if-valid-increment for the resource control
* for the number of processes in a task.
*
* Return values
* 0 if the threshold limit was not passed, 1 if the limit was passed.
*
* Caller's context
* p->p_lock must be held across the call.
*/
/*ARGSUSED*/
static int
{
return (0);
return (1);
return (0);
}
/*ARGSUSED*/
static int
return (0);
return (0);
}
/*ARGSUSED*/
static rctl_qty_t
{
task_t *t;
t = p->p_task;
return (nprocs);
}
/*ARGSUSED*/
static int
{
return (0);
return (1);
return (0);
}
/*ARGSUSED*/
static int
rctl_qty_t nv) {
return (0);
return (0);
}
/*
* static rctl_qty_t task_usage_cpu_secs(void *taskp)
*
* Overview
* task_usage_cpu_secs() is the usage operation for the resource control
* associated with the total accrued CPU seconds for a task.
*
* Return values
* The number of CPU seconds consumed by the task is returned.
*
* Caller's context
* The given task must be held across the call.
*/
/*ARGSUSED*/
static rctl_qty_t
{
return (t->tk_cpu_time);
}
/*
* int task_cpu_time_incr(task_t *t, rctl_qty_t incr)
*
* Overview
* task_cpu_time_incr() increments the amount of CPU time used
* by this task.
*
* Return values
* 1 if a second or more time is accumulated
* 0 otherwise
*
* Caller's context
* This is called by the clock tick accounting function to charge
* CPU time to a task.
*/
{
rctl_qty_t ret = 0;
mutex_enter(&t->tk_cpu_time_lock);
t->tk_cpu_ticks += incr;
if (t->tk_cpu_ticks >= hz) {
ret = t->tk_cpu_time;
}
mutex_exit(&t->tk_cpu_time_lock);
return (ret);
}
/*
* static int task_test_cpu_secs(void *taskp, rctl_val_t *, int64_t incr,
* int flags)
*
* Overview
* task_test_cpu_secs() is the test-if-valid-increment for the resource
* control for the total accrued CPU seconds for a task.
*
* Return values
* 0 if the threshold limit was not passed, 1 if the limit was passed.
*
* Caller's context
* The given task must be held across the call.
*/
/*ARGSUSED*/
static int
{
return (0);
return (1);
return (0);
}
static task_t *
{
return (NULL);
return (tk);
}
/*
* task_hold_by_id(), task_hold_by_id_zone()
*
* Overview
* task_hold_by_id() is used to take a reference on a task by its task id,
* supporting the various system call interfaces for obtaining resource data,
* delivering signals, and so forth.
*
* Return values
* Returns a pointer to the task_t with taskid_t id. The task is returned
* with its hold count incremented by one. Returns NULL if there
* is no task with the requested id.
*
* Caller's context
* Caller must not be holding task_hash_lock. No restrictions on context.
*/
task_t *
{
return (tk);
}
task_t *
{
if (INGLOBALZONE(curproc))
else
}
/*
* void task_hold(task_t *)
*
* Overview
* task_hold() is used to take an additional reference to the given task.
*
* Return values
* None.
*
* Caller's context
* No restriction on context.
*/
void
{
}
/*
* void task_rele(task_t *)
*
* Overview
* task_rele() relinquishes a reference on the given task, which was acquired
* via task_hold() or task_hold_by_id(). If this is the last member or
* observer of the task, dispatch it for commitment via the accounting
* subsystem.
*
* Return values
* None.
*
* Caller's context
* Caller must not be holding the task_hash_lock.
*/
void
{
return;
}
/*
* At this point, there are no members or observers of the task, so we
* can safely send it on for commitment to the accounting subsystem.
* The task will be destroyed in task_end() subsequent to commitment.
* Since we may be called with pidlock held, taskq_dispatch() cannot
* sleep. Commitment is handled by a backup thread in case dispatching
* the task fails.
*/
if (task_commit_head == NULL) {
} else {
}
}
}
/*
* task_t *task_create(projid_t, zone *)
*
* Overview
* A process constructing a new task calls task_create() to construct and
* preinitialize the task for the appropriate destination project. Only one
* task, the primordial task0, is not created with task_create().
*
* Return values
* None.
*
* Caller's context
* Caller's context should be safe for KM_SLEEP allocations.
* The caller should appropriately bump the kpj_ntasks counter on the
* project that contains this task.
*/
task_t *
{
/*
* Copy ancestor task's resource controls.
*/
for (;;) {
break;
}
/*
* At this point, curproc does not have the appropriate linkage
* through the task to the project. So, rctl_set_dup should only
* copy the rctls, and leave the callbacks for later.
*/
e.rcep_t = RCENTITY_TASK;
/*
* Record the ancestor task's ID for use by extended accounting.
*/
/*
* Put new task structure in the hash table.
*/
}
return (tk);
}
/*
* void task_attach(task_t *, proc_t *)
*
* Overview
* task_attach() is used to attach a process to a task; this operation is only
* performed as a result of a fork() or settaskid() system call. The proc_t's
* p_tasknext and p_taskprev fields will be set such that the proc_t is a
* member of the doubly-linked list of proc_t's that make up the task.
*
* Return values
* None.
*
* Caller's context
* pidlock and p->p_lock must be held on entry.
*/
void
{
p->p_tasknext = p;
p->p_taskprev = p;
} else {
first->p_taskprev = p;
p->p_tasknext = first;
p->p_taskprev = prev;
prev->p_tasknext = p;
}
tk->tk_memb_list = p;
}
/*
* task_begin()
*
* Overview
* A process constructing a new task calls task_begin() to initialize the
* task, by attaching itself as a member.
*
* Return values
* None.
*
* Caller's context
* pidlock and p_lock must be held across the call to task_begin().
*/
void
{
gethrestime(&ts);
/*
* Join process to the task as a member.
*/
task_attach(tk, p);
/*
* Now that the linkage from process to task is complete, do the
* required callback for the task rctl set.
*/
e.rcep_t = RCENTITY_TASK;
}
/*
* void task_detach(proc_t *)
*
* Overview
* task_detach() removes the specified process from its task. task_detach
* sets the process's task membership to NULL, in anticipation of a final exit
* or of joining a new task. Because task_rele() requires a context safe for
* KM_SLEEP allocations, a task_detach() is followed by a subsequent
* task_rele() once appropriate context is available.
*
* Because task_detach() involves relinquishing the process's membership in
* the project, any observational rctls the process may have had on the task
* or project are destroyed.
*
* Return values
* None.
*
* Caller's context
* pidlock and p_lock held across task_detach().
*/
void
task_detach(proc_t *p)
{
if (tk->tk_memb_list == p)
if (tk->tk_memb_list == p)
}
/*
* task_change(task_t *, proc_t *)
*
* Overview
* task_change() removes the specified process from its current task. The
* process is then attached to the specified task. This routine is called
* from settaskid() when process is being moved to a new task.
*
* Return values
* None.
*
* Caller's context
* pidlock and p_lock held across task_change()
*/
void
{
task_detach(p);
task_begin(newtk, p);
}
/*
* task_end()
*
* Overview
* task_end() contains the actions executed once the final member of
* a task has released the task, and all actions connected with the task, such
* as committing an accounting record to a file, are completed. It is called
* by the known last consumer of the task information. Additionally,
* task_end() must never refer to any process in the system.
*
* Return values
* None.
*
* Caller's context
* No restrictions on context, beyond that given above.
*/
void
{
}
static void
void *zonebuf)
{
kthread_t *t;
do {
(void) project_hold(kpj);
thread_lock(t);
/*
* Kick this thread so that he doesn't sit
* on a wrong wait queue.
*/
if (ISWAITING(t))
setrun_locked(t);
/*
* The thread wants to go on the project wait queue, but
* the waitq is changing.
*/
if (t->t_schedflag & TS_PROJWAITQ)
t->t_schedflag &= ~ TS_PROJWAITQ;
thread_unlock(t);
}
}
/*
* task_join()
*
* Overview
* task_join() contains the actions that must be executed when the first
* member (curproc) of a newly created task joins it. It may never fail.
*
* The caller must make sure holdlwps() is called so that all other lwps are
* stopped prior to calling this function.
*
* NB: It returns with curproc->p_lock held.
*
* Return values
* Pointer to the old task.
*
* Caller's context
* cpu_lock must be held entering the function. It will acquire pidlock,
* p_crlock and p_lock during execution.
*/
task_t *
{
/*
* We can't know for sure if holdlwps() was called, but we can check to
* ensure we're single-threaded.
*/
/*
* Changing the credential is always hard because we cannot
* allocate memory when holding locks but we don't know whether
* we need to change it. We first get a reference to the current
* cred if we need to change it. Then we create a credential
* with an updated project id. Finally we install it, first
* releasing the reference we had on the p_cred at the time we
* acquired the lock the first time and later we release the
* reference to p_cred at the time we acquired the lock the
* second time.
*/
mutex_enter(&p->p_crlock);
else
mutex_exit(&p->p_crlock);
mutex_enter(&p->p_crlock);
mutex_exit(&p->p_crlock);
}
/*
* Make sure that the number of processor sets is constant
* across this operation.
*/
mutex_enter(&p->p_lock);
task_change(tk, p);
/*
* Now move threads one by one to their new project.
*/
if (flags & TASK_FINAL)
return (prev_tk);
}
/*
* rctl ops vectors
*/
static rctl_ops_t task_lwps_ops = {
};
static rctl_ops_t task_procs_ops = {
};
static rctl_ops_t task_cpu_time_ops = {
};
/*ARGSUSED*/
/*
* void task_init(void)
*
* Overview
* task_init() initializes task-related hashes, caches, and the task id
* space. Additionally, task_init() establishes p0 as a member of task0.
* Called by main().
*
* Return values
* None.
*
* Caller's context
* task_init() must be called prior to MP startup.
*/
void
task_init(void)
{
/*
* Initialize task_cache and taskid_space.
*/
/*
* Initialize task hash table.
*/
/*
* Initialize task-based rctls.
*/
/*
* Create task0 and place p0 in it as a member.
*/
set = rctl_set_create();
e.rcep_t = RCENTITY_TASK;
(void *)task0p);
}
task0p->tk_memb_list = p;
/*
* Initialize task pointers for p0, including doubly linked list of task
* members.
*/
p->p_taskprev = p->p_tasknext = p;
}
static int
{
if (rw == KSTAT_WRITE)
return (EACCES);
return (0);
}
static kstat_t *
{
sizeof (task_kstat_t) / sizeof (kstat_named_t),
return (NULL);
return (ksp);
}
static void
{
void *data;
}
}
void
{
}
/*
* Backup thread to commit task resource usage when taskq_dispatch() fails.
*/
static void
{
"task_commit_thread");
for (;;) {
while (task_commit_head == NULL) {
}
while (task_commit_head != NULL) {
if (task_commit_head == NULL)
}
}
}