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