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