1 /*-
2 * Copyright (c) 2008, Jeffrey Roberson <jeff@freebsd.org>
3 * All rights reserved.
4 *
5 * Copyright (c) 2008 Nokia Corporation
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
13 * disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include "opt_ddb.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/sysproto.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/priv.h>
44 #include <sys/proc.h>
45 #include <sys/refcount.h>
46 #include <sys/sched.h>
47 #include <sys/smp.h>
48 #include <sys/syscallsubr.h>
49 #include <sys/cpuset.h>
50 #include <sys/sx.h>
51 #include <sys/refcount.h>
52 #include <sys/queue.h>
53 #include <sys/limits.h>
54 #include <sys/bus.h>
55 #include <sys/interrupt.h>
56
57 #include <vm/uma.h>
58
59 #ifdef DDB
60 #include <ddb/ddb.h>
61 #endif /* DDB */
62
63 /*
64 * cpusets provide a mechanism for creating and manipulating sets of
65 * processors for the purpose of constraining the scheduling of threads to
66 * specific processors.
67 *
68 * Each process belongs to an identified set, by default this is set 1. Each
69 * thread may further restrict the cpus it may run on to a subset of this
70 * named set. This creates an anonymous set which other threads and processes
71 * may not join by number.
72 *
73 * The named set is referred to herein as the 'base' set to avoid ambiguity.
74 * This set is usually a child of a 'root' set while the anonymous set may
75 * simply be referred to as a mask. In the syscall api these are referred to
76 * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here.
77 *
78 * Threads inherit their set from their creator whether it be anonymous or
79 * not. This means that anonymous sets are immutable because they may be
80 * shared. To modify an anonymous set a new set is created with the desired
81 * mask and the same parent as the existing anonymous set. This gives the
82 * illusion of each thread having a private mask.A
83 *
84 * Via the syscall apis a user may ask to retrieve or modify the root, base,
85 * or mask that is discovered via a pid, tid, or setid. Modifying a set
86 * modifies all numbered and anonymous child sets to comply with the new mask.
87 * Modifying a pid or tid's mask applies only to that tid but must still
88 * exist within the assigned parent set.
89 *
90 * A thread may not be assigned to a a group seperate from other threads in
91 * the process. This is to remove ambiguity when the setid is queried with
92 * a pid argument. There is no other technical limitation.
93 *
94 * This somewhat complex arrangement is intended to make it easy for
95 * applications to query available processors and bind their threads to
96 * specific processors while also allowing administrators to dynamically
97 * reprovision by changing sets which apply to groups of processes.
98 *
99 * A simple application should not concern itself with sets at all and
100 * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id
101 * meaning 'curthread'. It may query availble cpus for that tid with a
102 * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...).
103 */
104 static uma_zone_t cpuset_zone;
105 static struct mtx cpuset_lock;
106 static struct setlist cpuset_ids;
107 static struct unrhdr *cpuset_unr;
108 static struct cpuset *cpuset_zero;
109
110 cpuset_t *cpuset_root;
111
112 /*
113 * Acquire a reference to a cpuset, all pointers must be tracked with refs.
114 */
115 struct cpuset *
116 cpuset_ref(struct cpuset *set)
117 {
118
119 refcount_acquire(&set->cs_ref);
120 return (set);
121 }
122
123 /*
124 * Walks up the tree from 'set' to find the root. Returns the root
125 * referenced.
126 */
127 static struct cpuset *
128 cpuset_refroot(struct cpuset *set)
129 {
130
131 for (; set->cs_parent != NULL; set = set->cs_parent)
132 if (set->cs_flags & CPU_SET_ROOT)
133 break;
134 cpuset_ref(set);
135
136 return (set);
137 }
138
139 /*
140 * Find the first non-anonymous set starting from 'set'. Returns this set
141 * referenced. May return the passed in set with an extra ref if it is
142 * not anonymous.
143 */
144 static struct cpuset *
145 cpuset_refbase(struct cpuset *set)
146 {
147
148 if (set->cs_id == CPUSET_INVALID)
149 set = set->cs_parent;
150 cpuset_ref(set);
151
152 return (set);
153 }
154
155 /*
156 * Release a reference in a context where it is safe to allocte.
157 */
158 void
159 cpuset_rel(struct cpuset *set)
160 {
161 cpusetid_t id;
162
163 if (refcount_release(&set->cs_ref) == 0)
164 return;
165 mtx_lock_spin(&cpuset_lock);
166 LIST_REMOVE(set, cs_siblings);
167 id = set->cs_id;
168 if (id != CPUSET_INVALID)
169 LIST_REMOVE(set, cs_link);
170 mtx_unlock_spin(&cpuset_lock);
171 cpuset_rel(set->cs_parent);
172 uma_zfree(cpuset_zone, set);
173 if (id != CPUSET_INVALID)
174 free_unr(cpuset_unr, id);
175 }
176
177 /*
178 * Deferred release must be used when in a context that is not safe to
179 * allocate/free. This places any unreferenced sets on the list 'head'.
180 */
181 static void
182 cpuset_rel_defer(struct setlist *head, struct cpuset *set)
183 {
184
185 if (refcount_release(&set->cs_ref) == 0)
186 return;
187 mtx_lock_spin(&cpuset_lock);
188 LIST_REMOVE(set, cs_siblings);
189 if (set->cs_id != CPUSET_INVALID)
190 LIST_REMOVE(set, cs_link);
191 LIST_INSERT_HEAD(head, set, cs_link);
192 mtx_unlock_spin(&cpuset_lock);
193 }
194
195 /*
196 * Complete a deferred release. Removes the set from the list provided to
197 * cpuset_rel_defer.
198 */
199 static void
200 cpuset_rel_complete(struct cpuset *set)
201 {
202 LIST_REMOVE(set, cs_link);
203 cpuset_rel(set->cs_parent);
204 uma_zfree(cpuset_zone, set);
205 }
206
207 /*
208 * Find a set based on an id. Returns it with a ref.
209 */
210 static struct cpuset *
211 cpuset_lookup(cpusetid_t setid)
212 {
213 struct cpuset *set;
214
215 if (setid == CPUSET_INVALID)
216 return (NULL);
217 mtx_lock_spin(&cpuset_lock);
218 LIST_FOREACH(set, &cpuset_ids, cs_link)
219 if (set->cs_id == setid)
220 break;
221 if (set)
222 cpuset_ref(set);
223 mtx_unlock_spin(&cpuset_lock);
224 return (set);
225 }
226
227 /*
228 * Create a set in the space provided in 'set' with the provided parameters.
229 * The set is returned with a single ref. May return EDEADLK if the set
230 * will have no valid cpu based on restrictions from the parent.
231 */
232 static int
233 _cpuset_create(struct cpuset *set, struct cpuset *parent, cpuset_t *mask,
234 cpusetid_t id)
235 {
236
237 if (!CPU_OVERLAP(&parent->cs_mask, mask))
238 return (EDEADLK);
239 CPU_COPY(mask, &set->cs_mask);
240 LIST_INIT(&set->cs_children);
241 refcount_init(&set->cs_ref, 1);
242 set->cs_flags = 0;
243 mtx_lock_spin(&cpuset_lock);
244 CPU_AND(mask, &parent->cs_mask);
245 set->cs_id = id;
246 set->cs_parent = cpuset_ref(parent);
247 LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings);
248 if (set->cs_id != CPUSET_INVALID)
249 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
250 mtx_unlock_spin(&cpuset_lock);
251
252 return (0);
253 }
254
255 /*
256 * Create a new non-anonymous set with the requested parent and mask. May
257 * return failures if the mask is invalid or a new number can not be
258 * allocated.
259 */
260 static int
261 cpuset_create(struct cpuset **setp, struct cpuset *parent, cpuset_t *mask)
262 {
263 struct cpuset *set;
264 cpusetid_t id;
265 int error;
266
267 id = alloc_unr(cpuset_unr);
268 if (id == -1)
269 return (ENFILE);
270 *setp = set = uma_zalloc(cpuset_zone, M_WAITOK);
271 error = _cpuset_create(set, parent, mask, id);
272 if (error == 0)
273 return (0);
274 free_unr(cpuset_unr, id);
275 uma_zfree(cpuset_zone, set);
276
277 return (error);
278 }
279
280 /*
281 * Recursively check for errors that would occur from applying mask to
282 * the tree of sets starting at 'set'. Checks for sets that would become
283 * empty as well as RDONLY flags.
284 */
285 static int
286 cpuset_testupdate(struct cpuset *set, cpuset_t *mask)
287 {
288 struct cpuset *nset;
289 cpuset_t newmask;
290 int error;
291
292 mtx_assert(&cpuset_lock, MA_OWNED);
293 if (set->cs_flags & CPU_SET_RDONLY)
294 return (EPERM);
295 if (!CPU_OVERLAP(&set->cs_mask, mask))
296 return (EDEADLK);
297 CPU_COPY(&set->cs_mask, &newmask);
298 CPU_AND(&newmask, mask);
299 error = 0;
300 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
301 if ((error = cpuset_testupdate(nset, &newmask)) != 0)
302 break;
303 return (error);
304 }
305
306 /*
307 * Applies the mask 'mask' without checking for empty sets or permissions.
308 */
309 static void
310 cpuset_update(struct cpuset *set, cpuset_t *mask)
311 {
312 struct cpuset *nset;
313
314 mtx_assert(&cpuset_lock, MA_OWNED);
315 CPU_AND(&set->cs_mask, mask);
316 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
317 cpuset_update(nset, &set->cs_mask);
318
319 return;
320 }
321
322 /*
323 * Modify the set 'set' to use a copy of the mask provided. Apply this new
324 * mask to restrict all children in the tree. Checks for validity before
325 * applying the changes.
326 */
327 static int
328 cpuset_modify(struct cpuset *set, cpuset_t *mask)
329 {
330 struct cpuset *root;
331 int error;
332
333 error = priv_check(curthread, PRIV_SCHED_CPUSET);
334 if (error)
335 return (error);
336 /*
337 * Verify that we have access to this set of
338 * cpus.
339 */
340 root = set->cs_parent;
341 if (root && !CPU_SUBSET(&root->cs_mask, mask))
342 return (EINVAL);
343 mtx_lock_spin(&cpuset_lock);
344 error = cpuset_testupdate(set, mask);
345 if (error)
346 goto out;
347 cpuset_update(set, mask);
348 CPU_COPY(mask, &set->cs_mask);
349 out:
350 mtx_unlock_spin(&cpuset_lock);
351
352 return (error);
353 }
354
355 /*
356 * Resolve the 'which' parameter of several cpuset apis.
357 *
358 * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid. Also
359 * checks for permission via p_cansched().
360 *
361 * For WHICH_SET returns a valid set with a new reference.
362 *
363 * -1 may be supplied for any argument to mean the current proc/thread or
364 * the base set of the current thread. May fail with ESRCH/EPERM.
365 */
366 static int
367 cpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp,
368 struct cpuset **setp)
369 {
370 struct cpuset *set;
371 struct thread *td;
372 struct proc *p;
373 int error;
374
375 *pp = p = NULL;
376 *tdp = td = NULL;
377 *setp = set = NULL;
378 switch (which) {
379 case CPU_WHICH_PID:
380 if (id == -1) {
381 PROC_LOCK(curproc);
382 p = curproc;
383 break;
384 }
385 if ((p = pfind(id)) == NULL)
386 return (ESRCH);
387 break;
388 case CPU_WHICH_TID:
389 if (id == -1) {
390 PROC_LOCK(curproc);
391 p = curproc;
392 td = curthread;
393 break;
394 }
395 sx_slock(&allproc_lock);
396 FOREACH_PROC_IN_SYSTEM(p) {
397 PROC_LOCK(p);
398 PROC_SLOCK(p);
399 FOREACH_THREAD_IN_PROC(p, td)
400 if (td->td_tid == id)
401 break;
402 PROC_SUNLOCK(p);
403 if (td != NULL)
404 break;
405 PROC_UNLOCK(p);
406 }
407 sx_sunlock(&allproc_lock);
408 if (td == NULL)
409 return (ESRCH);
410 break;
411 case CPU_WHICH_CPUSET:
412 if (id == -1) {
413 thread_lock(curthread);
414 set = cpuset_refbase(curthread->td_cpuset);
415 thread_unlock(curthread);
416 } else
417 set = cpuset_lookup(id);
418 if (set) {
419 *setp = set;
420 return (0);
421 }
422 return (ESRCH);
423 default:
424 return (EINVAL);
425 }
426 error = p_cansched(curthread, p);
427 if (error) {
428 PROC_UNLOCK(p);
429 return (error);
430 }
431 if (td == NULL)
432 td = FIRST_THREAD_IN_PROC(p);
433 *pp = p;
434 *tdp = td;
435 return (0);
436 }
437
438 /*
439 * Create an anonymous set with the provided mask in the space provided by
440 * 'fset'. If the passed in set is anonymous we use its parent otherwise
441 * the new set is a child of 'set'.
442 */
443 static int
444 cpuset_shadow(struct cpuset *set, struct cpuset *fset, cpuset_t *mask)
445 {
446 struct cpuset *parent;
447
448 if (set->cs_id == CPUSET_INVALID)
449 parent = set->cs_parent;
450 else
451 parent = set;
452 if (!CPU_SUBSET(&parent->cs_mask, mask))
453 return (EDEADLK);
454 return (_cpuset_create(fset, parent, mask, CPUSET_INVALID));
455 }
456
457 /*
458 * Handle two cases for replacing the base set or mask of an entire process.
459 *
460 * 1) Set is non-null and mask is null. This reparents all anonymous sets
461 * to the provided set and replaces all non-anonymous td_cpusets with the
462 * provided set.
463 * 2) Mask is non-null and set is null. This replaces or creates anonymous
464 * sets for every thread with the existing base as a parent.
465 *
466 * This is overly complicated because we can't allocate while holding a
467 * spinlock and spinlocks must be held while changing and examining thread
468 * state.
469 */
470 static int
471 cpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask)
472 {
473 struct setlist freelist;
474 struct setlist droplist;
475 struct cpuset *tdset;
476 struct cpuset *nset;
477 struct thread *td;
478 struct proc *p;
479 int threads;
480 int nfree;
481 int error;
482 /*
483 * The algorithm requires two passes due to locking considerations.
484 *
485 * 1) Lookup the process and acquire the locks in the required order.
486 * 2) If enough cpusets have not been allocated release the locks and
487 * allocate them. Loop.
488 */
489 LIST_INIT(&freelist);
490 LIST_INIT(&droplist);
491 nfree = 0;
492 for (;;) {
493 error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset);
494 if (error)
495 goto out;
496 PROC_SLOCK(p);
497 if (nfree >= p->p_numthreads)
498 break;
499 threads = p->p_numthreads;
500 PROC_SUNLOCK(p);
501 PROC_UNLOCK(p);
502 for (; nfree < threads; nfree++) {
503 nset = uma_zalloc(cpuset_zone, M_WAITOK);
504 LIST_INSERT_HEAD(&freelist, nset, cs_link);
505 }
506 }
507 PROC_LOCK_ASSERT(p, MA_OWNED);
508 PROC_SLOCK_ASSERT(p, MA_OWNED);
509 /*
510 * Now that the appropriate locks are held and we have enough cpusets,
511 * make sure the operation will succeed before applying changes. The
512 * proc lock prevents td_cpuset from changing between calls.
513 */
514 error = 0;
515 FOREACH_THREAD_IN_PROC(p, td) {
516 thread_lock(td);
517 tdset = td->td_cpuset;
518 /*
519 * Verify that a new mask doesn't specify cpus outside of
520 * the set the thread is a member of.
521 */
522 if (mask) {
523 if (tdset->cs_id == CPUSET_INVALID)
524 tdset = tdset->cs_parent;
525 if (!CPU_SUBSET(&tdset->cs_mask, mask))
526 error = EDEADLK;
527 /*
528 * Verify that a new set won't leave an existing thread
529 * mask without a cpu to run on. It can, however, restrict
530 * the set.
531 */
532 } else if (tdset->cs_id == CPUSET_INVALID) {
533 if (!CPU_OVERLAP(&set->cs_mask, &tdset->cs_mask))
534 error = EDEADLK;
535 }
536 thread_unlock(td);
537 if (error)
538 goto unlock_out;
539 }
540 /*
541 * Replace each thread's cpuset while using deferred release. We
542 * must do this because the PROC_SLOCK has to be held while traversing
543 * the thread list and this limits the type of operations allowed.
544 */
545 FOREACH_THREAD_IN_PROC(p, td) {
546 thread_lock(td);
547 /*
548 * If we presently have an anonymous set or are applying a
549 * mask we must create an anonymous shadow set. That is
550 * either parented to our existing base or the supplied set.
551 *
552 * If we have a base set with no anonymous shadow we simply
553 * replace it outright.
554 */
555 tdset = td->td_cpuset;
556 if (tdset->cs_id == CPUSET_INVALID || mask) {
557 nset = LIST_FIRST(&freelist);
558 LIST_REMOVE(nset, cs_link);
559 if (mask)
560 error = cpuset_shadow(tdset, nset, mask);
561 else
562 error = _cpuset_create(nset, set,
563 &tdset->cs_mask, CPUSET_INVALID);
564 if (error) {
565 LIST_INSERT_HEAD(&freelist, nset, cs_link);
566 thread_unlock(td);
567 break;
568 }
569 } else
570 nset = cpuset_ref(set);
571 cpuset_rel_defer(&droplist, tdset);
572 td->td_cpuset = nset;
573 sched_affinity(td);
574 thread_unlock(td);
575 }
576 unlock_out:
577 PROC_SUNLOCK(p);
578 PROC_UNLOCK(p);
579 out:
580 while ((nset = LIST_FIRST(&droplist)) != NULL)
581 cpuset_rel_complete(nset);
582 while ((nset = LIST_FIRST(&freelist)) != NULL) {
583 LIST_REMOVE(nset, cs_link);
584 uma_zfree(cpuset_zone, nset);
585 }
586 return (error);
587 }
588
589 /*
590 * Apply an anonymous mask to a single thread.
591 */
592 int
593 cpuset_setthread(lwpid_t id, cpuset_t *mask)
594 {
595 struct cpuset *nset;
596 struct cpuset *set;
597 struct thread *td;
598 struct proc *p;
599 int error;
600
601 nset = uma_zalloc(cpuset_zone, M_WAITOK);
602 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set);
603 if (error)
604 goto out;
605 set = NULL;
606 thread_lock(td);
607 error = cpuset_shadow(td->td_cpuset, nset, mask);
608 if (error == 0) {
609 set = td->td_cpuset;
610 td->td_cpuset = nset;
611 sched_affinity(td);
612 nset = NULL;
613 }
614 thread_unlock(td);
615 PROC_UNLOCK(p);
616 if (set)
617 cpuset_rel(set);
618 out:
619 if (nset)
620 uma_zfree(cpuset_zone, nset);
621 return (error);
622 }
623
624 /*
625 * Creates the cpuset for thread0. We make two sets:
626 *
627 * 0 - The root set which should represent all valid processors in the
628 * system. It is initially created with a mask of all processors
629 * because we don't know what processors are valid until cpuset_init()
630 * runs. This set is immutable.
631 * 1 - The default set which all processes are a member of until changed.
632 * This allows an administrator to move all threads off of given cpus to
633 * dedicate them to high priority tasks or save power etc.
634 */
635 struct cpuset *
636 cpuset_thread0(void)
637 {
638 struct cpuset *set;
639 int error;
640
641 cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL,
642 NULL, NULL, UMA_ALIGN_PTR, 0);
643 mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE);
644 /*
645 * Create the root system set for the whole machine. Doesn't use
646 * cpuset_create() due to NULL parent.
647 */
648 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
649 set->cs_mask.__bits[0] = -1;
650 LIST_INIT(&set->cs_children);
651 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
652 set->cs_ref = 1;
653 set->cs_flags = CPU_SET_ROOT;
654 cpuset_zero = set;
655 cpuset_root = &set->cs_mask;
656 /*
657 * Now derive a default, modifiable set from that to give out.
658 */
659 set = uma_zalloc(cpuset_zone, M_WAITOK);
660 error = _cpuset_create(set, cpuset_zero, &cpuset_zero->cs_mask, 1);
661 KASSERT(error == 0, ("Error creating default set: %d\n", error));
662 /*
663 * Initialize the unit allocator. 0 and 1 are allocated above.
664 */
665 cpuset_unr = new_unrhdr(2, INT_MAX, NULL);
666
667 return (set);
668 }
669
670 /*
671 * This is called once the final set of system cpus is known. Modifies
672 * the root set and all children and mark the root readonly.
673 */
674 static void
675 cpuset_init(void *arg)
676 {
677 cpuset_t mask;
678
679 CPU_ZERO(&mask);
680 #ifdef SMP
681 mask.__bits[0] = all_cpus;
682 #else
683 mask.__bits[0] = 1;
684 #endif
685 if (cpuset_modify(cpuset_zero, &mask))
686 panic("Can't set initial cpuset mask.\n");
687 cpuset_zero->cs_flags |= CPU_SET_RDONLY;
688 }
689 SYSINIT(cpuset, SI_SUB_SMP, SI_ORDER_ANY, cpuset_init, NULL);
690
691 #ifndef _SYS_SYSPROTO_H_
692 struct cpuset_args {
693 cpusetid_t *setid;
694 };
695 #endif
696 int
697 cpuset(struct thread *td, struct cpuset_args *uap)
698 {
699 struct cpuset *root;
700 struct cpuset *set;
701 int error;
702
703 thread_lock(td);
704 root = cpuset_refroot(td->td_cpuset);
705 thread_unlock(td);
706 error = cpuset_create(&set, root, &root->cs_mask);
707 cpuset_rel(root);
708 if (error)
709 return (error);
710 error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id));
711 if (error == 0)
712 error = cpuset_setproc(-1, set, NULL);
713 cpuset_rel(set);
714 return (error);
715 }
716
717 #ifndef _SYS_SYSPROTO_H_
718 struct cpuset_setid_args {
719 cpuwhich_t which;
720 id_t id;
721 cpusetid_t setid;
722 };
723 #endif
724 int
725 cpuset_setid(struct thread *td, struct cpuset_setid_args *uap)
726 {
727 struct cpuset *set;
728 int error;
729
730 /*
731 * Presently we only support per-process sets.
732 */
733 if (uap->which != CPU_WHICH_PID)
734 return (EINVAL);
735 set = cpuset_lookup(uap->setid);
736 if (set == NULL)
737 return (ESRCH);
738 error = cpuset_setproc(uap->id, set, NULL);
739 cpuset_rel(set);
740 return (error);
741 }
742
743 #ifndef _SYS_SYSPROTO_H_
744 struct cpuset_getid_args {
745 cpulevel_t level;
746 cpuwhich_t which;
747 id_t id;
748 cpusetid_t *setid;
749 #endif
750 int
751 cpuset_getid(struct thread *td, struct cpuset_getid_args *uap)
752 {
753 struct cpuset *nset;
754 struct cpuset *set;
755 struct thread *ttd;
756 struct proc *p;
757 cpusetid_t id;
758 int error;
759
760 if (uap->level == CPU_LEVEL_WHICH && uap->which != CPU_WHICH_CPUSET)
761 return (EINVAL);
762 error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
763 if (error)
764 return (error);
765 switch (uap->which) {
766 case CPU_WHICH_TID:
767 case CPU_WHICH_PID:
768 thread_lock(ttd);
769 set = cpuset_refbase(ttd->td_cpuset);
770 thread_unlock(ttd);
771 PROC_UNLOCK(p);
772 break;
773 case CPU_WHICH_CPUSET:
774 break;
775 }
776 switch (uap->level) {
777 case CPU_LEVEL_ROOT:
778 nset = cpuset_refroot(set);
779 cpuset_rel(set);
780 set = nset;
781 break;
782 case CPU_LEVEL_CPUSET:
783 break;
784 case CPU_LEVEL_WHICH:
785 break;
786 }
787 id = set->cs_id;
788 cpuset_rel(set);
789 if (error == 0)
790 error = copyout(&id, uap->setid, sizeof(id));
791
792 return (error);
793 }
794
795 #ifndef _SYS_SYSPROTO_H_
796 struct cpuset_getaffinity_args {
797 cpulevel_t level;
798 cpuwhich_t which;
799 id_t id;
800 size_t cpusetsize;
801 cpuset_t *mask;
802 };
803 #endif
804 int
805 cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap)
806 {
807 struct thread *ttd;
808 struct cpuset *nset;
809 struct cpuset *set;
810 struct proc *p;
811 cpuset_t *mask;
812 int error;
813 size_t size;
814
815 if (uap->cpusetsize < sizeof(cpuset_t) ||
816 uap->cpusetsize > CPU_MAXSIZE / NBBY)
817 return (ERANGE);
818 size = uap->cpusetsize;
819 mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
820 error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
821 if (error)
822 goto out;
823 switch (uap->level) {
824 case CPU_LEVEL_ROOT:
825 case CPU_LEVEL_CPUSET:
826 switch (uap->which) {
827 case CPU_WHICH_TID:
828 case CPU_WHICH_PID:
829 thread_lock(ttd);
830 set = cpuset_ref(ttd->td_cpuset);
831 thread_unlock(ttd);
832 break;
833 case CPU_WHICH_CPUSET:
834 break;
835 }
836 if (uap->level == CPU_LEVEL_ROOT)
837 nset = cpuset_refroot(set);
838 else
839 nset = cpuset_refbase(set);
840 CPU_COPY(&nset->cs_mask, mask);
841 cpuset_rel(nset);
842 break;
843 case CPU_LEVEL_WHICH:
844 switch (uap->which) {
845 case CPU_WHICH_TID:
846 thread_lock(ttd);
847 CPU_COPY(&ttd->td_cpuset->cs_mask, mask);
848 thread_unlock(ttd);
849 break;
850 case CPU_WHICH_PID:
851 PROC_SLOCK(p);
852 FOREACH_THREAD_IN_PROC(p, ttd) {
853 thread_lock(ttd);
854 CPU_OR(mask, &ttd->td_cpuset->cs_mask);
855 thread_unlock(ttd);
856 }
857 PROC_SUNLOCK(p);
858 break;
859 case CPU_WHICH_CPUSET:
860 CPU_COPY(&set->cs_mask, mask);
861 break;
862 }
863 break;
864 default:
865 error = EINVAL;
866 break;
867 }
868 if (set)
869 cpuset_rel(set);
870 if (p)
871 PROC_UNLOCK(p);
872 if (error == 0)
873 error = copyout(mask, uap->mask, size);
874 out:
875 free(mask, M_TEMP);
876 return (error);
877 }
878
879 #ifndef _SYS_SYSPROTO_H_
880 struct cpuset_setaffinity_args {
881 cpulevel_t level;
882 cpuwhich_t which;
883 id_t id;
884 size_t cpusetsize;
885 const cpuset_t *mask;
886 };
887 #endif
888 int
889 cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap)
890 {
891 struct cpuset *nset;
892 struct cpuset *set;
893 struct thread *ttd;
894 struct proc *p;
895 cpuset_t *mask;
896 int error;
897
898 if (uap->cpusetsize < sizeof(cpuset_t) ||
899 uap->cpusetsize > CPU_MAXSIZE / NBBY)
900 return (ERANGE);
901 mask = malloc(uap->cpusetsize, M_TEMP, M_WAITOK | M_ZERO);
902 error = copyin(uap->mask, mask, uap->cpusetsize);
903 if (error)
904 goto out;
905 /*
906 * Verify that no high bits are set.
907 */
908 if (uap->cpusetsize > sizeof(cpuset_t)) {
909 char *end;
910 char *cp;
911
912 end = cp = (char *)&mask->__bits;
913 end += uap->cpusetsize;
914 cp += sizeof(cpuset_t);
915 while (cp != end)
916 if (*cp++ != 0) {
917 error = EINVAL;
918 goto out;
919 }
920
921 }
922 switch (uap->level) {
923 case CPU_LEVEL_ROOT:
924 case CPU_LEVEL_CPUSET:
925 error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
926 if (error)
927 break;
928 switch (uap->which) {
929 case CPU_WHICH_TID:
930 case CPU_WHICH_PID:
931 thread_lock(ttd);
932 set = cpuset_ref(ttd->td_cpuset);
933 thread_unlock(ttd);
934 PROC_UNLOCK(p);
935 break;
936 case CPU_WHICH_CPUSET:
937 break;
938 }
939 if (uap->level == CPU_LEVEL_ROOT)
940 nset = cpuset_refroot(set);
941 else
942 nset = cpuset_refbase(set);
943 error = cpuset_modify(nset, mask);
944 cpuset_rel(nset);
945 cpuset_rel(set);
946 break;
947 case CPU_LEVEL_WHICH:
948 switch (uap->which) {
949 case CPU_WHICH_TID:
950 error = cpuset_setthread(uap->id, mask);
951 break;
952 case CPU_WHICH_PID:
953 error = cpuset_setproc(uap->id, NULL, mask);
954 break;
955 case CPU_WHICH_CPUSET:
956 error = cpuset_which(CPU_WHICH_CPUSET, uap->id, &p,
957 &ttd, &set);
958 if (error == 0) {
959 error = cpuset_modify(set, mask);
960 cpuset_rel(set);
961 }
962 break;
963 default:
964 error = EINVAL;
965 break;
966 }
967 break;
968 default:
969 error = EINVAL;
970 break;
971 }
972 out:
973 free(mask, M_TEMP);
974 return (error);
975 }
976
977 #ifdef DDB
978 DB_SHOW_COMMAND(cpusets, db_show_cpusets)
979 {
980 struct cpuset *set;
981 int cpu, once;
982
983 LIST_FOREACH(set, &cpuset_ids, cs_link) {
984 db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n",
985 set, set->cs_id, set->cs_ref, set->cs_flags,
986 (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0);
987 db_printf(" mask=");
988 for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) {
989 if (CPU_ISSET(cpu, &set->cs_mask)) {
990 if (once == 0) {
991 db_printf("%d", cpu);
992 once = 1;
993 } else
994 db_printf(",%d", cpu);
995 }
996 }
997 db_printf("\n");
998 if (db_pager_quit)
999 break;
1000 }
1001 }
1002 #endif /* DDB */
Cache object: ae10d5112f7d8c8003f1807b4a91d594
|