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