Home | History | Annotate | Line # | Download | only in kern
sys_pset.c revision 1.9.2.2
      1  1.9.2.2  skrll /*	$NetBSD: sys_pset.c,v 1.9.2.2 2009/04/28 07:37:00 skrll Exp $	*/
      2      1.1  rmind 
      3      1.1  rmind /*
      4      1.1  rmind  * Copyright (c) 2008, Mindaugas Rasiukevicius <rmind at NetBSD org>
      5      1.1  rmind  * All rights reserved.
      6      1.1  rmind  *
      7      1.1  rmind  * Redistribution and use in source and binary forms, with or without
      8      1.1  rmind  * modification, are permitted provided that the following conditions
      9      1.1  rmind  * are met:
     10      1.1  rmind  * 1. Redistributions of source code must retain the above copyright
     11      1.1  rmind  *    notice, this list of conditions and the following disclaimer.
     12      1.1  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  rmind  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  rmind  *    documentation and/or other materials provided with the distribution.
     15      1.1  rmind  *
     16      1.7  rmind  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17      1.7  rmind  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18      1.7  rmind  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19      1.7  rmind  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20      1.7  rmind  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21      1.7  rmind  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22      1.7  rmind  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23      1.7  rmind  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24      1.7  rmind  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25      1.7  rmind  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26      1.7  rmind  * SUCH DAMAGE.
     27      1.1  rmind  */
     28      1.1  rmind 
     29      1.1  rmind /*
     30      1.1  rmind  * Implementation of the Processor Sets.
     31      1.1  rmind  *
     32      1.1  rmind  * Locking
     33      1.1  rmind  *  The array of the processor-set structures and its members are protected
     34      1.8     ad  *  by the global cpu_lock.  Note that in scheduler, the very l_psid value
     35      1.1  rmind  *  might be used without lock held.
     36      1.1  rmind  */
     37      1.1  rmind 
     38      1.1  rmind #include <sys/cdefs.h>
     39  1.9.2.2  skrll __KERNEL_RCSID(0, "$NetBSD: sys_pset.c,v 1.9.2.2 2009/04/28 07:37:00 skrll Exp $");
     40      1.1  rmind 
     41      1.1  rmind #include <sys/param.h>
     42      1.1  rmind 
     43      1.1  rmind #include <sys/cpu.h>
     44      1.1  rmind #include <sys/kauth.h>
     45      1.1  rmind #include <sys/kmem.h>
     46      1.1  rmind #include <sys/lwp.h>
     47      1.1  rmind #include <sys/mutex.h>
     48      1.1  rmind #include <sys/proc.h>
     49      1.1  rmind #include <sys/pset.h>
     50      1.1  rmind #include <sys/sched.h>
     51      1.1  rmind #include <sys/syscallargs.h>
     52      1.1  rmind #include <sys/sysctl.h>
     53      1.1  rmind #include <sys/systm.h>
     54      1.1  rmind #include <sys/types.h>
     55      1.1  rmind 
     56      1.1  rmind static pset_info_t **	psets;
     57      1.1  rmind static u_int		psets_max;
     58      1.1  rmind static u_int		psets_count;
     59      1.1  rmind 
     60      1.1  rmind static int	psets_realloc(int);
     61      1.1  rmind static int	psid_validate(psetid_t, bool);
     62      1.1  rmind static int	kern_pset_create(psetid_t *);
     63      1.1  rmind static int	kern_pset_destroy(psetid_t);
     64      1.1  rmind 
     65      1.1  rmind /*
     66      1.1  rmind  * Initialization of the processor-sets.
     67      1.1  rmind  */
     68      1.1  rmind void
     69      1.1  rmind psets_init(void)
     70      1.1  rmind {
     71      1.1  rmind 
     72      1.1  rmind 	psets_max = max(MAXCPUS, 32);
     73      1.1  rmind 	psets = kmem_zalloc(psets_max * sizeof(void *), KM_SLEEP);
     74      1.1  rmind 	psets_count = 0;
     75      1.1  rmind }
     76      1.1  rmind 
     77      1.1  rmind /*
     78      1.1  rmind  * Reallocate the array of the processor-set structures.
     79      1.1  rmind  */
     80      1.1  rmind static int
     81      1.1  rmind psets_realloc(int new_psets_max)
     82      1.1  rmind {
     83      1.1  rmind 	pset_info_t **new_psets, **old_psets;
     84      1.1  rmind 	const u_int newsize = new_psets_max * sizeof(void *);
     85      1.1  rmind 	u_int i, oldsize;
     86      1.1  rmind 
     87      1.1  rmind 	if (new_psets_max < 1)
     88      1.1  rmind 		return EINVAL;
     89      1.1  rmind 
     90      1.1  rmind 	new_psets = kmem_zalloc(newsize, KM_SLEEP);
     91      1.8     ad 	mutex_enter(&cpu_lock);
     92      1.1  rmind 	old_psets = psets;
     93      1.1  rmind 	oldsize = psets_max * sizeof(void *);
     94      1.1  rmind 
     95      1.1  rmind 	/* Check if we can lower the size of the array */
     96      1.1  rmind 	if (new_psets_max < psets_max) {
     97      1.1  rmind 		for (i = new_psets_max; i < psets_max; i++) {
     98      1.1  rmind 			if (psets[i] == NULL)
     99      1.1  rmind 				continue;
    100      1.8     ad 			mutex_exit(&cpu_lock);
    101      1.1  rmind 			kmem_free(new_psets, newsize);
    102      1.1  rmind 			return EBUSY;
    103      1.1  rmind 		}
    104      1.1  rmind 	}
    105      1.1  rmind 
    106      1.1  rmind 	/* Copy all pointers to the new array */
    107      1.1  rmind 	memcpy(new_psets, psets, newsize);
    108      1.1  rmind 	psets_max = new_psets_max;
    109      1.1  rmind 	psets = new_psets;
    110      1.8     ad 	mutex_exit(&cpu_lock);
    111      1.1  rmind 
    112      1.1  rmind 	kmem_free(old_psets, oldsize);
    113      1.1  rmind 	return 0;
    114      1.1  rmind }
    115      1.1  rmind 
    116      1.1  rmind /*
    117      1.1  rmind  * Validate processor-set ID.
    118      1.1  rmind  */
    119      1.1  rmind static int
    120      1.1  rmind psid_validate(psetid_t psid, bool chkps)
    121      1.1  rmind {
    122      1.1  rmind 
    123      1.8     ad 	KASSERT(mutex_owned(&cpu_lock));
    124      1.1  rmind 
    125      1.1  rmind 	if (chkps && (psid == PS_NONE || psid == PS_QUERY || psid == PS_MYID))
    126      1.1  rmind 		return 0;
    127      1.1  rmind 	if (psid <= 0 || psid > psets_max)
    128      1.1  rmind 		return EINVAL;
    129      1.1  rmind 	if (psets[psid - 1] == NULL)
    130      1.1  rmind 		return EINVAL;
    131      1.1  rmind 	if (psets[psid - 1]->ps_flags & PSET_BUSY)
    132      1.1  rmind 		return EBUSY;
    133      1.1  rmind 
    134      1.1  rmind 	return 0;
    135      1.1  rmind }
    136      1.1  rmind 
    137      1.1  rmind /*
    138      1.1  rmind  * Create a processor-set.
    139      1.1  rmind  */
    140      1.1  rmind static int
    141      1.1  rmind kern_pset_create(psetid_t *psid)
    142      1.1  rmind {
    143      1.1  rmind 	pset_info_t *pi;
    144      1.1  rmind 	u_int i;
    145      1.1  rmind 
    146      1.1  rmind 	if (psets_count == psets_max)
    147      1.1  rmind 		return ENOMEM;
    148      1.1  rmind 
    149      1.1  rmind 	pi = kmem_zalloc(sizeof(pset_info_t), KM_SLEEP);
    150      1.1  rmind 
    151      1.8     ad 	mutex_enter(&cpu_lock);
    152      1.1  rmind 	if (psets_count == psets_max) {
    153      1.8     ad 		mutex_exit(&cpu_lock);
    154      1.1  rmind 		kmem_free(pi, sizeof(pset_info_t));
    155      1.1  rmind 		return ENOMEM;
    156      1.1  rmind 	}
    157      1.1  rmind 
    158      1.1  rmind 	/* Find a free entry in the array */
    159      1.1  rmind 	for (i = 0; i < psets_max; i++)
    160      1.1  rmind 		if (psets[i] == NULL)
    161      1.1  rmind 			break;
    162      1.1  rmind 	KASSERT(i != psets_max);
    163      1.1  rmind 
    164      1.1  rmind 	psets[i] = pi;
    165      1.1  rmind 	psets_count++;
    166      1.8     ad 	mutex_exit(&cpu_lock);
    167      1.1  rmind 
    168      1.1  rmind 	*psid = i + 1;
    169      1.1  rmind 	return 0;
    170      1.1  rmind }
    171      1.1  rmind 
    172      1.1  rmind /*
    173      1.1  rmind  * Destroy a processor-set.
    174      1.1  rmind  */
    175      1.1  rmind static int
    176      1.1  rmind kern_pset_destroy(psetid_t psid)
    177      1.1  rmind {
    178      1.1  rmind 	struct cpu_info *ci;
    179      1.1  rmind 	pset_info_t *pi;
    180      1.1  rmind 	struct lwp *l;
    181      1.1  rmind 	CPU_INFO_ITERATOR cii;
    182      1.1  rmind 	int error;
    183      1.1  rmind 
    184      1.8     ad 	mutex_enter(&cpu_lock);
    185      1.1  rmind 	if (psid == PS_MYID) {
    186      1.1  rmind 		/* Use caller's processor-set ID */
    187      1.1  rmind 		psid = curlwp->l_psid;
    188      1.1  rmind 	}
    189      1.1  rmind 	error = psid_validate(psid, false);
    190      1.1  rmind 	if (error) {
    191      1.8     ad 		mutex_exit(&cpu_lock);
    192      1.1  rmind 		return error;
    193      1.1  rmind 	}
    194      1.1  rmind 
    195      1.1  rmind 	/* Release the processor-set from all CPUs */
    196      1.1  rmind 	for (CPU_INFO_FOREACH(cii, ci)) {
    197      1.1  rmind 		struct schedstate_percpu *spc;
    198      1.1  rmind 
    199      1.1  rmind 		spc = &ci->ci_schedstate;
    200      1.1  rmind 		if (spc->spc_psid != psid)
    201      1.1  rmind 			continue;
    202      1.1  rmind 		spc->spc_psid = PS_NONE;
    203      1.1  rmind 	}
    204      1.1  rmind 	/* Mark that processor-set is going to be destroyed */
    205      1.1  rmind 	pi = psets[psid - 1];
    206      1.1  rmind 	pi->ps_flags |= PSET_BUSY;
    207      1.8     ad 	mutex_exit(&cpu_lock);
    208      1.1  rmind 
    209      1.1  rmind 	/* Unmark the processor-set ID from each thread */
    210      1.5     ad 	mutex_enter(proc_lock);
    211      1.1  rmind 	LIST_FOREACH(l, &alllwp, l_list) {
    212      1.1  rmind 		/* Safe to check and set without lock held */
    213      1.1  rmind 		if (l->l_psid != psid)
    214      1.1  rmind 			continue;
    215      1.1  rmind 		l->l_psid = PS_NONE;
    216      1.1  rmind 	}
    217      1.5     ad 	mutex_exit(proc_lock);
    218      1.1  rmind 
    219      1.1  rmind 	/* Destroy the processor-set */
    220      1.8     ad 	mutex_enter(&cpu_lock);
    221      1.1  rmind 	psets[psid - 1] = NULL;
    222      1.1  rmind 	psets_count--;
    223      1.8     ad 	mutex_exit(&cpu_lock);
    224      1.1  rmind 
    225      1.1  rmind 	kmem_free(pi, sizeof(pset_info_t));
    226      1.1  rmind 	return 0;
    227      1.1  rmind }
    228      1.1  rmind 
    229      1.1  rmind /*
    230      1.1  rmind  * General system calls for the processor-sets.
    231      1.1  rmind  */
    232      1.1  rmind 
    233      1.1  rmind int
    234      1.1  rmind sys_pset_create(struct lwp *l, const struct sys_pset_create_args *uap,
    235      1.1  rmind     register_t *retval)
    236      1.1  rmind {
    237      1.1  rmind 	/* {
    238      1.1  rmind 		syscallarg(psetid_t) *psid;
    239      1.1  rmind 	} */
    240      1.1  rmind 	psetid_t psid;
    241      1.1  rmind 	int error;
    242      1.1  rmind 
    243      1.1  rmind 	/* Available only for super-user */
    244      1.4   elad 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_PSET,
    245      1.4   elad 	    KAUTH_REQ_SYSTEM_PSET_CREATE, NULL, NULL, NULL))
    246      1.1  rmind 		return EPERM;
    247      1.1  rmind 
    248      1.1  rmind 	error = kern_pset_create(&psid);
    249      1.1  rmind 	if (error)
    250      1.1  rmind 		return error;
    251      1.1  rmind 
    252      1.1  rmind 	error = copyout(&psid, SCARG(uap, psid), sizeof(psetid_t));
    253      1.1  rmind 	if (error)
    254      1.1  rmind 		(void)kern_pset_destroy(psid);
    255      1.1  rmind 
    256      1.1  rmind 	return error;
    257      1.1  rmind }
    258      1.1  rmind 
    259      1.1  rmind int
    260      1.1  rmind sys_pset_destroy(struct lwp *l, const struct sys_pset_destroy_args *uap,
    261      1.1  rmind     register_t *retval)
    262      1.1  rmind {
    263      1.1  rmind 	/* {
    264      1.1  rmind 		syscallarg(psetid_t) psid;
    265      1.1  rmind 	} */
    266      1.1  rmind 
    267      1.1  rmind 	/* Available only for super-user */
    268      1.4   elad 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_PSET,
    269      1.4   elad 	    KAUTH_REQ_SYSTEM_PSET_DESTROY,
    270      1.4   elad 	    KAUTH_ARG(SCARG(uap, psid)), NULL, NULL))
    271      1.1  rmind 		return EPERM;
    272      1.1  rmind 
    273      1.1  rmind 	return kern_pset_destroy(SCARG(uap, psid));
    274      1.1  rmind }
    275      1.1  rmind 
    276      1.1  rmind int
    277      1.1  rmind sys_pset_assign(struct lwp *l, const struct sys_pset_assign_args *uap,
    278      1.1  rmind     register_t *retval)
    279      1.1  rmind {
    280      1.1  rmind 	/* {
    281      1.1  rmind 		syscallarg(psetid_t) psid;
    282      1.1  rmind 		syscallarg(cpuid_t) cpuid;
    283      1.1  rmind 		syscallarg(psetid_t) *opsid;
    284      1.1  rmind 	} */
    285  1.9.2.1  skrll 	struct cpu_info *ici, *ci = NULL;
    286      1.9  rmind 	struct schedstate_percpu *spc = NULL;
    287  1.9.2.1  skrll 	struct lwp *t;
    288      1.1  rmind 	psetid_t psid = SCARG(uap, psid), opsid = 0;
    289      1.1  rmind 	CPU_INFO_ITERATOR cii;
    290      1.9  rmind 	int error = 0, nnone = 0;
    291      1.1  rmind 
    292      1.1  rmind 	/* Available only for super-user, except the case of PS_QUERY */
    293      1.4   elad 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_PSET,
    294      1.4   elad 	    KAUTH_REQ_SYSTEM_PSET_ASSIGN, KAUTH_ARG(SCARG(uap, psid)), NULL,
    295      1.4   elad 	    NULL))
    296      1.1  rmind 		return EPERM;
    297      1.1  rmind 
    298      1.1  rmind 	/* Find the target CPU */
    299      1.8     ad 	mutex_enter(&cpu_lock);
    300  1.9.2.1  skrll 	for (CPU_INFO_FOREACH(cii, ici)) {
    301  1.9.2.1  skrll 		struct schedstate_percpu *ispc;
    302  1.9.2.1  skrll 		ispc = &ici->ci_schedstate;
    303  1.9.2.1  skrll 		if (cpu_index(ici) == SCARG(uap, cpuid)) {
    304  1.9.2.1  skrll 			ci = ici;
    305  1.9.2.1  skrll 			spc = ispc;
    306  1.9.2.1  skrll 		}
    307  1.9.2.1  skrll 		nnone += (ispc->spc_psid == PS_NONE);
    308      1.8     ad 	}
    309  1.9.2.1  skrll 	if (ci == NULL) {
    310      1.8     ad 		mutex_exit(&cpu_lock);
    311      1.1  rmind 		return EINVAL;
    312      1.8     ad 	}
    313      1.1  rmind 	error = psid_validate(psid, true);
    314      1.1  rmind 	if (error) {
    315      1.8     ad 		mutex_exit(&cpu_lock);
    316      1.1  rmind 		return error;
    317      1.1  rmind 	}
    318      1.1  rmind 	opsid = spc->spc_psid;
    319      1.1  rmind 	switch (psid) {
    320      1.1  rmind 	case PS_QUERY:
    321      1.1  rmind 		break;
    322      1.1  rmind 	case PS_MYID:
    323      1.1  rmind 		psid = curlwp->l_psid;
    324      1.8     ad 		/* FALLTHROUGH */
    325      1.1  rmind 	default:
    326      1.9  rmind 		/*
    327      1.9  rmind 		 * Ensure at least one CPU stays in the default set,
    328      1.9  rmind 		 * and that specified CPU is not offline.
    329      1.9  rmind 		 */
    330      1.9  rmind 		if (psid != PS_NONE && ((spc->spc_flags & SPCF_OFFLINE) ||
    331      1.9  rmind 		    (nnone == 1 && spc->spc_psid == PS_NONE))) {
    332      1.8     ad 			mutex_exit(&cpu_lock);
    333      1.8     ad 			return EBUSY;
    334      1.8     ad 		}
    335  1.9.2.1  skrll 		mutex_enter(proc_lock);
    336  1.9.2.1  skrll 		/*
    337  1.9.2.1  skrll 		 * Ensure that none of the threads are using affinity mask
    338  1.9.2.1  skrll 		 * with this target CPU in it.
    339  1.9.2.1  skrll 		 */
    340  1.9.2.1  skrll 		LIST_FOREACH(t, &alllwp, l_list) {
    341  1.9.2.1  skrll 			if ((t->l_flag & LW_AFFINITY) == 0)
    342  1.9.2.1  skrll 				continue;
    343  1.9.2.2  skrll 			lwp_lock(t);
    344  1.9.2.2  skrll 			if ((t->l_flag & LW_AFFINITY) == 0) {
    345  1.9.2.2  skrll 				lwp_unlock(t);
    346  1.9.2.2  skrll 				continue;
    347  1.9.2.2  skrll 			}
    348  1.9.2.1  skrll 			if (kcpuset_isset(cpu_index(ci), t->l_affinity)) {
    349  1.9.2.2  skrll 				lwp_unlock(t);
    350  1.9.2.1  skrll 				mutex_exit(proc_lock);
    351  1.9.2.1  skrll 				mutex_exit(&cpu_lock);
    352  1.9.2.1  skrll 				return EPERM;
    353  1.9.2.1  skrll 			}
    354  1.9.2.1  skrll 		}
    355  1.9.2.1  skrll 		/*
    356  1.9.2.1  skrll 		 * Set the processor-set ID.
    357  1.9.2.1  skrll 		 * Migrate out any threads running on this CPU.
    358  1.9.2.1  skrll 		 */
    359      1.1  rmind 		spc->spc_psid = psid;
    360  1.9.2.1  skrll 
    361  1.9.2.1  skrll 		LIST_FOREACH(t, &alllwp, l_list) {
    362  1.9.2.1  skrll 			struct cpu_info *tci;
    363  1.9.2.1  skrll 			if (t->l_cpu != ci)
    364  1.9.2.1  skrll 				continue;
    365  1.9.2.1  skrll 			if (t->l_pflag & (LP_BOUND | LP_INTR))
    366  1.9.2.1  skrll 				continue;
    367  1.9.2.1  skrll 			lwp_lock(t);
    368  1.9.2.1  skrll 			tci = sched_takecpu(t);
    369  1.9.2.1  skrll 			KASSERT(tci != ci);
    370  1.9.2.1  skrll 			lwp_migrate(t, tci);
    371  1.9.2.1  skrll 		}
    372  1.9.2.1  skrll 		mutex_exit(proc_lock);
    373      1.8     ad 		break;
    374      1.1  rmind 	}
    375      1.8     ad 	mutex_exit(&cpu_lock);
    376      1.1  rmind 
    377      1.1  rmind 	if (SCARG(uap, opsid) != NULL)
    378      1.1  rmind 		error = copyout(&opsid, SCARG(uap, opsid), sizeof(psetid_t));
    379      1.1  rmind 
    380      1.1  rmind 	return error;
    381      1.1  rmind }
    382      1.1  rmind 
    383      1.1  rmind int
    384      1.1  rmind sys__pset_bind(struct lwp *l, const struct sys__pset_bind_args *uap,
    385      1.1  rmind     register_t *retval)
    386      1.1  rmind {
    387      1.1  rmind 	/* {
    388      1.1  rmind 		syscallarg(idtype_t) idtype;
    389      1.1  rmind 		syscallarg(id_t) first_id;
    390      1.1  rmind 		syscallarg(id_t) second_id;
    391      1.1  rmind 		syscallarg(psetid_t) psid;
    392      1.1  rmind 		syscallarg(psetid_t) *opsid;
    393      1.1  rmind 	} */
    394      1.1  rmind 	struct cpu_info *ci;
    395      1.1  rmind 	struct proc *p;
    396      1.1  rmind 	struct lwp *t;
    397      1.1  rmind 	id_t id1, id2;
    398      1.1  rmind 	pid_t pid = 0;
    399      1.1  rmind 	lwpid_t lid = 0;
    400      1.1  rmind 	psetid_t psid, opsid;
    401      1.1  rmind 	int error = 0, lcnt;
    402      1.1  rmind 
    403      1.1  rmind 	psid = SCARG(uap, psid);
    404      1.1  rmind 
    405      1.1  rmind 	/* Available only for super-user, except the case of PS_QUERY */
    406      1.4   elad 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_PSET,
    407      1.4   elad 	    KAUTH_REQ_SYSTEM_PSET_BIND, KAUTH_ARG(SCARG(uap, psid)), NULL,
    408      1.4   elad 	    NULL))
    409      1.1  rmind 		return EPERM;
    410      1.1  rmind 
    411      1.8     ad 	mutex_enter(&cpu_lock);
    412      1.1  rmind 	error = psid_validate(psid, true);
    413      1.1  rmind 	if (error) {
    414      1.8     ad 		mutex_exit(&cpu_lock);
    415      1.1  rmind 		return error;
    416      1.1  rmind 	}
    417      1.1  rmind 	if (psid == PS_MYID)
    418      1.1  rmind 		psid = curlwp->l_psid;
    419      1.1  rmind 	if (psid != PS_QUERY && psid != PS_NONE)
    420      1.1  rmind 		psets[psid - 1]->ps_flags |= PSET_BUSY;
    421      1.8     ad 	mutex_exit(&cpu_lock);
    422      1.1  rmind 
    423      1.1  rmind 	/*
    424      1.1  rmind 	 * Get PID and LID from the ID.
    425      1.1  rmind 	 */
    426      1.1  rmind 	p = l->l_proc;
    427      1.1  rmind 	id1 = SCARG(uap, first_id);
    428      1.1  rmind 	id2 = SCARG(uap, second_id);
    429      1.1  rmind 
    430      1.1  rmind 	switch (SCARG(uap, idtype)) {
    431      1.1  rmind 	case P_PID:
    432      1.1  rmind 		/*
    433      1.1  rmind 		 * Process:
    434      1.1  rmind 		 *  First ID	- PID;
    435      1.1  rmind 		 *  Second ID	- ignored;
    436      1.1  rmind 		 */
    437      1.1  rmind 		pid = (id1 == P_MYID) ? p->p_pid : id1;
    438      1.1  rmind 		lid = 0;
    439      1.1  rmind 		break;
    440      1.1  rmind 	case P_LWPID:
    441      1.1  rmind 		/*
    442      1.1  rmind 		 * Thread (LWP):
    443      1.1  rmind 		 *  First ID	- LID;
    444      1.1  rmind 		 *  Second ID	- PID;
    445      1.1  rmind 		 */
    446      1.1  rmind 		if (id1 == P_MYID) {
    447      1.1  rmind 			pid = p->p_pid;
    448      1.1  rmind 			lid = l->l_lid;
    449      1.1  rmind 			break;
    450      1.1  rmind 		}
    451      1.1  rmind 		lid = id1;
    452      1.1  rmind 		pid = (id2 == P_MYID) ? p->p_pid : id2;
    453      1.1  rmind 		break;
    454      1.1  rmind 	default:
    455      1.2   yamt 		error = EINVAL;
    456      1.2   yamt 		goto error;
    457      1.1  rmind 	}
    458      1.1  rmind 
    459      1.1  rmind 	/* Find the process */
    460      1.5     ad 	mutex_enter(proc_lock);
    461      1.5     ad 	p = p_find(pid, PFIND_LOCKED);
    462      1.1  rmind 	if (p == NULL) {
    463      1.5     ad 		mutex_exit(proc_lock);
    464      1.1  rmind 		error = ESRCH;
    465      1.1  rmind 		goto error;
    466      1.1  rmind 	}
    467      1.6     ad 	mutex_enter(p->p_lock);
    468      1.5     ad 	mutex_exit(proc_lock);
    469      1.1  rmind 
    470      1.1  rmind 	/* Disallow modification of the system processes */
    471      1.1  rmind 	if (p->p_flag & PK_SYSTEM) {
    472      1.6     ad 		mutex_exit(p->p_lock);
    473      1.1  rmind 		error = EPERM;
    474      1.1  rmind 		goto error;
    475      1.1  rmind 	}
    476      1.1  rmind 
    477      1.1  rmind 	/* Find the LWP(s) */
    478      1.1  rmind 	lcnt = 0;
    479      1.1  rmind 	ci = NULL;
    480      1.1  rmind 	LIST_FOREACH(t, &p->p_lwps, l_sibling) {
    481      1.1  rmind 		if (lid && lid != t->l_lid)
    482      1.1  rmind 			continue;
    483      1.1  rmind 		/*
    484      1.1  rmind 		 * Bind the thread to the processor-set,
    485      1.1  rmind 		 * take some CPU and migrate.
    486      1.1  rmind 		 */
    487      1.1  rmind 		lwp_lock(t);
    488      1.1  rmind 		opsid = t->l_psid;
    489      1.1  rmind 		t->l_psid = psid;
    490  1.9.2.1  skrll 		ci = sched_takecpu(t);
    491      1.1  rmind 		/* Unlocks LWP */
    492      1.1  rmind 		lwp_migrate(t, ci);
    493      1.1  rmind 		lcnt++;
    494      1.1  rmind 	}
    495      1.6     ad 	mutex_exit(p->p_lock);
    496      1.1  rmind 	if (lcnt == 0) {
    497      1.1  rmind 		error = ESRCH;
    498      1.1  rmind 		goto error;
    499      1.1  rmind 	}
    500      1.1  rmind 	if (SCARG(uap, opsid))
    501      1.1  rmind 		error = copyout(&opsid, SCARG(uap, opsid), sizeof(psetid_t));
    502      1.1  rmind error:
    503      1.1  rmind 	if (psid != PS_QUERY && psid != PS_NONE) {
    504      1.8     ad 		mutex_enter(&cpu_lock);
    505      1.1  rmind 		psets[psid - 1]->ps_flags &= ~PSET_BUSY;
    506      1.8     ad 		mutex_exit(&cpu_lock);
    507      1.1  rmind 	}
    508      1.1  rmind 	return error;
    509      1.1  rmind }
    510      1.1  rmind 
    511      1.1  rmind /*
    512      1.1  rmind  * Sysctl nodes and initialization.
    513      1.1  rmind  */
    514      1.1  rmind 
    515      1.1  rmind static int
    516      1.1  rmind sysctl_psets_max(SYSCTLFN_ARGS)
    517      1.1  rmind {
    518      1.1  rmind 	struct sysctlnode node;
    519      1.1  rmind 	int error, newsize;
    520      1.1  rmind 
    521      1.1  rmind 	node = *rnode;
    522      1.1  rmind 	node.sysctl_data = &newsize;
    523      1.1  rmind 
    524      1.1  rmind 	newsize = psets_max;
    525      1.1  rmind 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    526      1.1  rmind 	if (error || newp == NULL)
    527      1.1  rmind 		return error;
    528      1.1  rmind 
    529      1.1  rmind 	if (newsize <= 0)
    530      1.1  rmind 		return EINVAL;
    531      1.1  rmind 
    532      1.1  rmind 	sysctl_unlock();
    533      1.1  rmind 	error = psets_realloc(newsize);
    534      1.1  rmind 	sysctl_relock();
    535      1.1  rmind 	return error;
    536      1.1  rmind }
    537      1.1  rmind 
    538      1.8     ad static int
    539      1.8     ad sysctl_psets_list(SYSCTLFN_ARGS)
    540      1.8     ad {
    541      1.8     ad 	const size_t bufsz = 1024;
    542      1.8     ad 	char *buf, tbuf[16];
    543      1.8     ad 	int i, error;
    544      1.8     ad 	size_t len;
    545      1.8     ad 
    546      1.8     ad 	sysctl_unlock();
    547      1.8     ad 	buf = kmem_alloc(bufsz, KM_SLEEP);
    548      1.8     ad 	snprintf(buf, bufsz, "%d:1", PS_NONE);	/* XXX */
    549      1.8     ad 
    550      1.8     ad 	mutex_enter(&cpu_lock);
    551      1.8     ad 	for (i = 0; i < psets_max; i++) {
    552      1.8     ad 		if (psets[i] == NULL)
    553      1.8     ad 			continue;
    554      1.8     ad 		snprintf(tbuf, sizeof(tbuf), ",%d:2", i + 1);	/* XXX */
    555      1.8     ad 		strlcat(buf, tbuf, bufsz);
    556      1.8     ad 	}
    557      1.8     ad 	mutex_exit(&cpu_lock);
    558      1.8     ad 	len = strlen(buf) + 1;
    559      1.8     ad 	error = 0;
    560      1.8     ad 	if (oldp != NULL)
    561      1.8     ad 		error = copyout(buf, oldp, min(len, *oldlenp));
    562      1.8     ad 	*oldlenp = len;
    563      1.8     ad 	kmem_free(buf, bufsz);
    564      1.8     ad 	sysctl_relock();
    565      1.8     ad 	return error;
    566      1.8     ad }
    567      1.8     ad 
    568      1.1  rmind SYSCTL_SETUP(sysctl_pset_setup, "sysctl kern.pset subtree setup")
    569      1.1  rmind {
    570      1.1  rmind 	const struct sysctlnode *node = NULL;
    571      1.1  rmind 
    572      1.1  rmind 	sysctl_createv(clog, 0, NULL, NULL,
    573      1.1  rmind 		CTLFLAG_PERMANENT,
    574      1.1  rmind 		CTLTYPE_NODE, "kern", NULL,
    575      1.1  rmind 		NULL, 0, NULL, 0,
    576      1.1  rmind 		CTL_KERN, CTL_EOL);
    577      1.1  rmind 	sysctl_createv(clog, 0, NULL, &node,
    578      1.1  rmind 		CTLFLAG_PERMANENT,
    579      1.1  rmind 		CTLTYPE_NODE, "pset",
    580      1.1  rmind 		SYSCTL_DESCR("Processor-set options"),
    581      1.1  rmind 		NULL, 0, NULL, 0,
    582      1.1  rmind 		CTL_KERN, CTL_CREATE, CTL_EOL);
    583      1.1  rmind 
    584      1.1  rmind 	if (node == NULL)
    585      1.1  rmind 		return;
    586      1.1  rmind 
    587      1.1  rmind 	sysctl_createv(clog, 0, &node, NULL,
    588      1.1  rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    589      1.1  rmind 		CTLTYPE_INT, "psets_max",
    590      1.1  rmind 		SYSCTL_DESCR("Maximal count of the processor-sets"),
    591      1.1  rmind 		sysctl_psets_max, 0, &psets_max, 0,
    592      1.1  rmind 		CTL_CREATE, CTL_EOL);
    593      1.8     ad 	sysctl_createv(clog, 0, &node, NULL,
    594      1.8     ad 		CTLFLAG_PERMANENT,
    595      1.8     ad 		CTLTYPE_STRING, "list",
    596      1.8     ad 		SYSCTL_DESCR("List of active sets"),
    597      1.8     ad 		sysctl_psets_list, 0, NULL, 0,
    598      1.8     ad 		CTL_CREATE, CTL_EOL);
    599      1.1  rmind }
    600