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