/* $NetBSD: sys_pset.c,v 1.24 2020/05/23 23:42:43 ad Exp $ */
/*
* Copyright (c) 2008, Mindaugas Rasiukevicius <rmind at NetBSD org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Implementation of the Processor Sets.
*
* Locking
* The array of the processor-set structures and its members are protected
* by the global cpu_lock. Note that in scheduler, the very l_psid value
* might be used without lock held.
*/
static int psets_realloc(int);
static int psid_validate(psetid_t, bool);
static int kern_pset_create(psetid_t *);
static int kern_pset_destroy(psetid_t);
/* Check if we can lower the size of the array */
if (new_psets_max < psets_max) {
for (i = new_psets_max; i < psets_max; i++) {
if (psets[i] == NULL)
continue;
mutex_exit(&cpu_lock);
kmem_free(new_psets, newsize);
return EBUSY;
}
}
/* Copy all pointers to the new array */
memcpy(new_psets, psets, newsize);
psets_max = new_psets_max;
psets = new_psets;
mutex_exit(&cpu_lock);
/* Unmark the processor-set ID from each thread */
mutex_enter(&proc_lock);
LIST_FOREACH(l, &alllwp, l_list) {
/* Safe to check and set without lock held */
if (l->l_psid != psid)
continue;
l->l_psid = PS_NONE;
}
mutex_exit(&proc_lock);
/* Available only for super-user */
if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_PSET,
KAUTH_REQ_SYSTEM_PSET_CREATE, NULL, NULL, NULL))
return EPERM;
error = kern_pset_create(&psid);
if (error)
return error;
error = copyout(&psid, SCARG(uap, psid), sizeof(psetid_t));
if (error)
(void)kern_pset_destroy(psid);
/* Available only for super-user */
if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_PSET,
KAUTH_REQ_SYSTEM_PSET_DESTROY,
KAUTH_ARG(SCARG(uap, psid)), NULL, NULL))
return EPERM;
/* Available only for super-user, except the case of PS_QUERY */
if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_PSET,
KAUTH_REQ_SYSTEM_PSET_ASSIGN, KAUTH_ARG(SCARG(uap, psid)), NULL,
NULL))
return EPERM;
/* Find the target CPU */
mutex_enter(&cpu_lock);
for (CPU_INFO_FOREACH(cii, ici)) {
struct schedstate_percpu *ispc;
ispc = &ici->ci_schedstate;
if (cpu_index(ici) == SCARG(uap, cpuid)) {
ci = ici;
spc = ispc;
}
nnone += (ispc->spc_psid == PS_NONE);
}
if (ci == NULL) {
mutex_exit(&cpu_lock);
return EINVAL;
}
error = psid_validate(psid, true);
if (error) {
mutex_exit(&cpu_lock);
return error;
}
opsid = spc->spc_psid;
switch (psid) {
case PS_QUERY:
break;
case PS_MYID:
psid = curlwp->l_psid;
/* FALLTHROUGH */
default:
/*
* Just finish if old and new processor-sets are
* the same.
*/
if (spc->spc_psid == psid)
break;
/*
* Ensure at least one CPU stays in the default set,
* and that specified CPU is not offline.
*/
if (psid != PS_NONE && ((spc->spc_flags & SPCF_OFFLINE) ||
(nnone == 1 && spc->spc_psid == PS_NONE))) {
mutex_exit(&cpu_lock);
return EBUSY;
}
mutex_enter(&proc_lock);
/*
* Ensure that none of the threads are using affinity mask
* with this target CPU in it.
*/
LIST_FOREACH(t, &alllwp, l_list) {
if (t->l_affinity == NULL) {
continue;
}
lwp_lock(t);
if (t->l_affinity == NULL) {
lwp_unlock(t);
continue;
}
if (kcpuset_isset(t->l_affinity, cpu_index(ci))) {
lwp_unlock(t);
mutex_exit(&proc_lock);
mutex_exit(&cpu_lock);
return EPERM;
}
lwp_unlock(t);
}
/*
* Set the processor-set ID.
* Migrate out any threads running on this CPU.
*/
spc->spc_psid = psid;
/* Available only for super-user, except the case of PS_QUERY */
if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_PSET,
KAUTH_REQ_SYSTEM_PSET_BIND, KAUTH_ARG(SCARG(uap, psid)), NULL,
NULL))
return EPERM;
mutex_enter(&cpu_lock);
error = psid_validate(psid, true);
if (error) {
mutex_exit(&cpu_lock);
return error;
}
if (psid == PS_MYID)
psid = curlwp->l_psid;
/*
* Get PID and LID from the ID.
*/
p = l->l_proc;
id1 = SCARG(uap, first_id);
id2 = SCARG(uap, second_id);
mutex_enter(&proc_lock);
switch (SCARG(uap, idtype)) {
case P_PID:
/*
* Process:
* First ID - PID;
* Second ID - ignored;
*/
pid = (id1 == P_MYID) ? p->p_pid : id1;
lid = 0;
break;
case P_LWPID:
/*
* Thread (LWP):
* First ID - LID;
* Second ID - PID;
*/
if (id1 == P_MYID) {
pid = p->p_pid;
lid = l->l_lid;
break;
}
lid = id1;
pid = (id2 == P_MYID) ? p->p_pid : id2;
break;
default:
error = EINVAL;
goto error;
}
/* Find the process */
p = proc_find(pid);
if (p == NULL) {
error = ESRCH;
goto error;
}
/* Disallow modification of the system processes */
if (p->p_flag & PK_SYSTEM) {
error = EPERM;
goto error;
}
/* Find the LWP(s) */
lcnt = 0;
ci = NULL;
mutex_enter(p->p_lock);
LIST_FOREACH(t, &p->p_lwps, l_sibling) {
if (lid && lid != t->l_lid)
continue;
/*
* Bind the thread to the processor-set,
* take some CPU and migrate.
*/
lwp_lock(t);
opsid = t->l_psid;
t->l_psid = psid;
ci = sched_takecpu(t);
/* Unlocks LWP */
lwp_migrate(t, ci);
lcnt++;
}
mutex_exit(p->p_lock);
if (lcnt == 0) {
error = ESRCH;
}
error:
mutex_exit(&proc_lock);
mutex_exit(&cpu_lock);
if (error == 0 && SCARG(uap, opsid))
error = copyout(&opsid, SCARG(uap, opsid), sizeof(psetid_t));
return error;
}
/*
* Sysctl nodes and initialization.
*/
static int
sysctl_psets_max(SYSCTLFN_ARGS)
{
struct sysctlnode node;
int error, newsize;