Print this page
6583 remove whole-process swapping
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/disp/rt.c
+++ new/usr/src/uts/common/disp/rt.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 * Copyright 2013 Joyent, Inc. All rights reserved.
26 26 */
27 27
28 28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 29 /* All Rights Reserved */
30 30
31 31 #include <sys/types.h>
32 32 #include <sys/param.h>
33 33 #include <sys/sysmacros.h>
34 34 #include <sys/cred.h>
35 35 #include <sys/proc.h>
36 36 #include <sys/pcb.h>
37 37 #include <sys/signal.h>
38 38 #include <sys/user.h>
39 39 #include <sys/priocntl.h>
40 40 #include <sys/class.h>
41 41 #include <sys/disp.h>
42 42 #include <sys/procset.h>
43 43 #include <sys/cmn_err.h>
44 44 #include <sys/debug.h>
45 45 #include <sys/rt.h>
46 46 #include <sys/rtpriocntl.h>
47 47 #include <sys/kmem.h>
48 48 #include <sys/systm.h>
49 49 #include <sys/schedctl.h>
50 50 #include <sys/errno.h>
51 51 #include <sys/cpuvar.h>
52 52 #include <sys/vmsystm.h>
53 53 #include <sys/time.h>
54 54 #include <sys/policy.h>
55 55 #include <sys/sdt.h>
56 56 #include <sys/cpupart.h>
57 57 #include <sys/modctl.h>
58 58
59 59 static pri_t rt_init(id_t, int, classfuncs_t **);
60 60
61 61 static struct sclass csw = {
62 62 "RT",
63 63 rt_init,
64 64 0
65 65 };
66 66
67 67 static struct modlsched modlsched = {
68 68 &mod_schedops, "realtime scheduling class", &csw
69 69 };
70 70
71 71 static struct modlinkage modlinkage = {
72 72 MODREV_1, (void *)&modlsched, NULL
73 73 };
74 74
75 75 int
76 76 _init()
77 77 {
78 78 return (mod_install(&modlinkage));
79 79 }
80 80
81 81 int
82 82 _fini()
83 83 {
84 84 return (EBUSY); /* don't remove RT for now */
85 85 }
86 86
87 87 int
88 88 _info(struct modinfo *modinfop)
89 89 {
90 90 return (mod_info(&modlinkage, modinfop));
91 91 }
92 92
93 93
94 94 /*
95 95 * Class specific code for the real-time class
96 96 */
97 97
98 98 /*
99 99 * Extern declarations for variables defined in the rt master file
100 100 */
101 101 #define RTMAXPRI 59
102 102
103 103 pri_t rt_maxpri = RTMAXPRI; /* maximum real-time priority */
104 104 rtdpent_t *rt_dptbl; /* real-time dispatcher parameter table */
105 105
106 106 /*
107 107 * control flags (kparms->rt_cflags).
108 108 */
109 109 #define RT_DOPRI 0x01 /* change priority */
110 110 #define RT_DOTQ 0x02 /* change RT time quantum */
111 111 #define RT_DOSIG 0x04 /* change RT time quantum signal */
112 112
113 113 static int rt_admin(caddr_t, cred_t *);
114 114 static int rt_enterclass(kthread_t *, id_t, void *, cred_t *, void *);
115 115 static int rt_fork(kthread_t *, kthread_t *, void *);
116 116 static int rt_getclinfo(void *);
117 117 static int rt_getclpri(pcpri_t *);
118 118 static int rt_parmsin(void *);
119 119 static int rt_parmsout(void *, pc_vaparms_t *);
120 120 static int rt_vaparmsin(void *, pc_vaparms_t *);
121 121 static int rt_vaparmsout(void *, pc_vaparms_t *);
122 122 static int rt_parmsset(kthread_t *, void *, id_t, cred_t *);
123 123 static int rt_donice(kthread_t *, cred_t *, int, int *);
↓ open down ↓ |
123 lines elided |
↑ open up ↑ |
124 124 static int rt_doprio(kthread_t *, cred_t *, int, int *);
125 125 static void rt_exitclass(void *);
126 126 static int rt_canexit(kthread_t *, cred_t *);
127 127 static void rt_forkret(kthread_t *, kthread_t *);
128 128 static void rt_nullsys();
129 129 static void rt_parmsget(kthread_t *, void *);
130 130 static void rt_preempt(kthread_t *);
131 131 static void rt_setrun(kthread_t *);
132 132 static void rt_tick(kthread_t *);
133 133 static void rt_wakeup(kthread_t *);
134 -static pri_t rt_swapin(kthread_t *, int);
135 -static pri_t rt_swapout(kthread_t *, int);
136 134 static pri_t rt_globpri(kthread_t *);
137 135 static void rt_yield(kthread_t *);
138 136 static int rt_alloc(void **, int);
139 137 static void rt_free(void *);
140 138
141 139 static void rt_change_priority(kthread_t *, rtproc_t *);
142 140
143 141 static id_t rt_cid; /* real-time class ID */
144 142 static rtproc_t rt_plisthead; /* dummy rtproc at head of rtproc list */
145 143 static kmutex_t rt_dptblock; /* protects realtime dispatch table */
146 144 static kmutex_t rt_list_lock; /* protects RT thread list */
147 145
148 146 extern rtdpent_t *rt_getdptbl(void);
149 147
150 148 static struct classfuncs rt_classfuncs = {
151 149 /* class ops */
152 150 rt_admin,
153 151 rt_getclinfo,
154 152 rt_parmsin,
155 153 rt_parmsout,
156 154 rt_vaparmsin,
157 155 rt_vaparmsout,
158 156 rt_getclpri,
159 157 rt_alloc,
160 158 rt_free,
161 159 /* thread ops */
162 160 rt_enterclass,
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
163 161 rt_exitclass,
164 162 rt_canexit,
165 163 rt_fork,
166 164 rt_forkret,
167 165 rt_parmsget,
168 166 rt_parmsset,
169 167 rt_nullsys, /* stop */
170 168 rt_nullsys, /* exit */
171 169 rt_nullsys, /* active */
172 170 rt_nullsys, /* inactive */
173 - rt_swapin,
174 - rt_swapout,
175 171 rt_nullsys, /* trapret */
176 172 rt_preempt,
177 173 rt_setrun,
178 174 rt_nullsys, /* sleep */
179 175 rt_tick,
180 176 rt_wakeup,
181 177 rt_donice,
182 178 rt_globpri,
183 179 rt_nullsys, /* set_process_group */
184 180 rt_yield,
185 181 rt_doprio,
186 182 };
187 183
188 184 /*
189 185 * Real-time class initialization. Called by dispinit() at boot time.
190 186 * We can ignore the clparmsz argument since we know that the smallest
191 187 * possible parameter buffer is big enough for us.
192 188 */
193 189 /* ARGSUSED */
194 190 pri_t
195 191 rt_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp)
196 192 {
197 193 rt_dptbl = rt_getdptbl();
198 194 rt_cid = cid; /* Record our class ID */
199 195
200 196 /*
201 197 * Initialize the rtproc list.
202 198 */
203 199 rt_plisthead.rt_next = rt_plisthead.rt_prev = &rt_plisthead;
204 200
205 201 /*
206 202 * We're required to return a pointer to our classfuncs
207 203 * structure and the highest global priority value we use.
208 204 */
209 205 *clfuncspp = &rt_classfuncs;
210 206 mutex_init(&rt_dptblock, NULL, MUTEX_DEFAULT, NULL);
211 207 mutex_init(&rt_list_lock, NULL, MUTEX_DEFAULT, NULL);
212 208 return (rt_dptbl[rt_maxpri].rt_globpri);
213 209 }
214 210
215 211 /*
216 212 * Get or reset the rt_dptbl values per the user's request.
217 213 */
218 214 /* ARGSUSED */
219 215 static int
220 216 rt_admin(caddr_t uaddr, cred_t *reqpcredp)
221 217 {
222 218 rtadmin_t rtadmin;
223 219 rtdpent_t *tmpdpp;
224 220 size_t userdpsz;
225 221 size_t rtdpsz;
226 222 int i;
227 223
228 224 if (get_udatamodel() == DATAMODEL_NATIVE) {
229 225 if (copyin(uaddr, &rtadmin, sizeof (rtadmin_t)))
230 226 return (EFAULT);
231 227 }
232 228 #ifdef _SYSCALL32_IMPL
233 229 else {
234 230 /* rtadmin struct from ILP32 callers */
235 231 rtadmin32_t rtadmin32;
236 232 if (copyin(uaddr, &rtadmin32, sizeof (rtadmin32_t)))
237 233 return (EFAULT);
238 234 rtadmin.rt_dpents =
239 235 (struct rtdpent *)(uintptr_t)rtadmin32.rt_dpents;
240 236 rtadmin.rt_ndpents = rtadmin32.rt_ndpents;
241 237 rtadmin.rt_cmd = rtadmin32.rt_cmd;
242 238 }
243 239 #endif /* _SYSCALL32_IMPL */
244 240
245 241 rtdpsz = (rt_maxpri + 1) * sizeof (rtdpent_t);
246 242
247 243 switch (rtadmin.rt_cmd) {
248 244
249 245 case RT_GETDPSIZE:
250 246 rtadmin.rt_ndpents = rt_maxpri + 1;
251 247
252 248 if (get_udatamodel() == DATAMODEL_NATIVE) {
253 249 if (copyout(&rtadmin, uaddr, sizeof (rtadmin_t)))
254 250 return (EFAULT);
255 251 }
256 252 #ifdef _SYSCALL32_IMPL
257 253 else {
258 254 /* return rtadmin struct to ILP32 callers */
259 255 rtadmin32_t rtadmin32;
260 256 rtadmin32.rt_dpents =
261 257 (caddr32_t)(uintptr_t)rtadmin.rt_dpents;
262 258 rtadmin32.rt_ndpents = rtadmin.rt_ndpents;
263 259 rtadmin32.rt_cmd = rtadmin.rt_cmd;
264 260 if (copyout(&rtadmin32, uaddr, sizeof (rtadmin32_t)))
265 261 return (EFAULT);
266 262 }
267 263 #endif /* _SYSCALL32_IMPL */
268 264
269 265 break;
270 266
271 267 case RT_GETDPTBL:
272 268 userdpsz = MIN(rtadmin.rt_ndpents * sizeof (rtdpent_t),
273 269 rtdpsz);
274 270 if (copyout(rt_dptbl, rtadmin.rt_dpents, userdpsz))
275 271 return (EFAULT);
276 272 rtadmin.rt_ndpents = userdpsz / sizeof (rtdpent_t);
277 273
278 274 if (get_udatamodel() == DATAMODEL_NATIVE) {
279 275 if (copyout(&rtadmin, uaddr, sizeof (rtadmin_t)))
280 276 return (EFAULT);
281 277 }
282 278 #ifdef _SYSCALL32_IMPL
283 279 else {
284 280 /* return rtadmin struct to ILP32 callers */
285 281 rtadmin32_t rtadmin32;
286 282 rtadmin32.rt_dpents =
287 283 (caddr32_t)(uintptr_t)rtadmin.rt_dpents;
288 284 rtadmin32.rt_ndpents = rtadmin.rt_ndpents;
289 285 rtadmin32.rt_cmd = rtadmin.rt_cmd;
290 286 if (copyout(&rtadmin32, uaddr, sizeof (rtadmin32_t)))
291 287 return (EFAULT);
292 288 }
293 289 #endif /* _SYSCALL32_IMPL */
294 290 break;
295 291
296 292 case RT_SETDPTBL:
297 293 /*
298 294 * We require that the requesting process has sufficient
299 295 * priveleges. We also require that the table supplied by
300 296 * the user exactly match the current rt_dptbl in size.
301 297 */
302 298 if (secpolicy_dispadm(reqpcredp) != 0)
303 299 return (EPERM);
304 300 if (rtadmin.rt_ndpents * sizeof (rtdpent_t) != rtdpsz)
305 301 return (EINVAL);
306 302
307 303 /*
308 304 * We read the user supplied table into a temporary buffer
309 305 * where the time quantum values are validated before
310 306 * being copied to the rt_dptbl.
311 307 */
312 308 tmpdpp = kmem_alloc(rtdpsz, KM_SLEEP);
313 309 if (copyin(rtadmin.rt_dpents, tmpdpp, rtdpsz)) {
314 310 kmem_free(tmpdpp, rtdpsz);
315 311 return (EFAULT);
316 312 }
317 313 for (i = 0; i < rtadmin.rt_ndpents; i++) {
318 314
319 315 /*
320 316 * Validate the user supplied time quantum values.
321 317 */
322 318 if (tmpdpp[i].rt_quantum <= 0 &&
323 319 tmpdpp[i].rt_quantum != RT_TQINF) {
324 320 kmem_free(tmpdpp, rtdpsz);
325 321 return (EINVAL);
326 322 }
327 323 }
328 324
329 325 /*
330 326 * Copy the user supplied values over the current rt_dptbl
331 327 * values. The rt_globpri member is read-only so we don't
332 328 * overwrite it.
333 329 */
334 330 mutex_enter(&rt_dptblock);
335 331 for (i = 0; i < rtadmin.rt_ndpents; i++)
336 332 rt_dptbl[i].rt_quantum = tmpdpp[i].rt_quantum;
337 333 mutex_exit(&rt_dptblock);
338 334 kmem_free(tmpdpp, rtdpsz);
339 335 break;
340 336
341 337 default:
342 338 return (EINVAL);
343 339 }
344 340 return (0);
345 341 }
346 342
347 343
348 344 /*
349 345 * Allocate a real-time class specific proc structure and
350 346 * initialize it with the parameters supplied. Also move thread
351 347 * to specified real-time priority.
352 348 */
353 349 /* ARGSUSED */
354 350 static int
355 351 rt_enterclass(kthread_t *t, id_t cid, void *parmsp, cred_t *reqpcredp,
356 352 void *bufp)
357 353 {
358 354 rtkparms_t *rtkparmsp = (rtkparms_t *)parmsp;
359 355 rtproc_t *rtpp;
360 356
361 357 /*
362 358 * For a thread to enter the real-time class the thread
363 359 * which initiates the request must be privileged.
364 360 * This may have been checked previously but if our
365 361 * caller passed us a credential structure we assume it
366 362 * hasn't and we check it here.
367 363 */
368 364 if (reqpcredp != NULL && secpolicy_setpriority(reqpcredp) != 0)
369 365 return (EPERM);
370 366
371 367 rtpp = (rtproc_t *)bufp;
372 368 ASSERT(rtpp != NULL);
373 369
374 370 /*
375 371 * If this thread's lwp is swapped out, it will be brought in
376 372 * when it is put onto the runqueue.
377 373 *
378 374 * Now, Initialize the rtproc structure.
379 375 */
380 376 if (rtkparmsp == NULL) {
381 377 /*
382 378 * Use default values
383 379 */
384 380 rtpp->rt_pri = 0;
385 381 rtpp->rt_pquantum = rt_dptbl[0].rt_quantum;
386 382 rtpp->rt_tqsignal = 0;
387 383 } else {
388 384 /*
389 385 * Use supplied values
390 386 */
391 387 if ((rtkparmsp->rt_cflags & RT_DOPRI) == 0)
392 388 rtpp->rt_pri = 0;
393 389 else
394 390 rtpp->rt_pri = rtkparmsp->rt_pri;
395 391
396 392 if (rtkparmsp->rt_tqntm == RT_TQINF)
397 393 rtpp->rt_pquantum = RT_TQINF;
398 394 else if (rtkparmsp->rt_tqntm == RT_TQDEF ||
399 395 (rtkparmsp->rt_cflags & RT_DOTQ) == 0)
400 396 rtpp->rt_pquantum = rt_dptbl[rtpp->rt_pri].rt_quantum;
401 397 else
402 398 rtpp->rt_pquantum = rtkparmsp->rt_tqntm;
403 399
404 400 if ((rtkparmsp->rt_cflags & RT_DOSIG) == 0)
405 401 rtpp->rt_tqsignal = 0;
406 402 else
407 403 rtpp->rt_tqsignal = rtkparmsp->rt_tqsig;
408 404 }
409 405 rtpp->rt_flags = 0;
410 406 rtpp->rt_tp = t;
411 407 /*
412 408 * Reset thread priority
413 409 */
414 410 thread_lock(t);
415 411 t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
416 412 t->t_cid = cid;
417 413 t->t_cldata = (void *)rtpp;
418 414 t->t_schedflag &= ~TS_RUNQMATCH;
419 415 rt_change_priority(t, rtpp);
420 416 thread_unlock(t);
421 417 /*
422 418 * Link new structure into rtproc list
423 419 */
424 420 mutex_enter(&rt_list_lock);
425 421 rtpp->rt_next = rt_plisthead.rt_next;
426 422 rtpp->rt_prev = &rt_plisthead;
427 423 rt_plisthead.rt_next->rt_prev = rtpp;
428 424 rt_plisthead.rt_next = rtpp;
429 425 mutex_exit(&rt_list_lock);
430 426 return (0);
431 427 }
432 428
433 429
434 430 /*
435 431 * Free rtproc structure of thread.
436 432 */
437 433 static void
438 434 rt_exitclass(void *procp)
439 435 {
440 436 rtproc_t *rtprocp = (rtproc_t *)procp;
441 437
442 438 mutex_enter(&rt_list_lock);
443 439 rtprocp->rt_prev->rt_next = rtprocp->rt_next;
444 440 rtprocp->rt_next->rt_prev = rtprocp->rt_prev;
445 441 mutex_exit(&rt_list_lock);
446 442 kmem_free(rtprocp, sizeof (rtproc_t));
447 443 }
448 444
449 445
450 446 /*
451 447 * Allocate and initialize real-time class specific
452 448 * proc structure for child.
453 449 */
454 450 /* ARGSUSED */
455 451 static int
456 452 rt_fork(kthread_t *t, kthread_t *ct, void *bufp)
457 453 {
458 454 rtproc_t *prtpp;
459 455 rtproc_t *crtpp;
460 456
461 457 ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
462 458
463 459 /*
464 460 * Initialize child's rtproc structure
465 461 */
466 462 crtpp = (rtproc_t *)bufp;
467 463 ASSERT(crtpp != NULL);
468 464 prtpp = (rtproc_t *)t->t_cldata;
469 465 thread_lock(t);
470 466 crtpp->rt_timeleft = crtpp->rt_pquantum = prtpp->rt_pquantum;
471 467 crtpp->rt_pri = prtpp->rt_pri;
472 468 crtpp->rt_flags = prtpp->rt_flags & ~RTBACKQ;
473 469 crtpp->rt_tqsignal = prtpp->rt_tqsignal;
474 470
475 471 crtpp->rt_tp = ct;
476 472 thread_unlock(t);
477 473
478 474 /*
479 475 * Link new structure into rtproc list
480 476 */
481 477 ct->t_cldata = (void *)crtpp;
482 478 mutex_enter(&rt_list_lock);
483 479 crtpp->rt_next = rt_plisthead.rt_next;
484 480 crtpp->rt_prev = &rt_plisthead;
485 481 rt_plisthead.rt_next->rt_prev = crtpp;
486 482 rt_plisthead.rt_next = crtpp;
487 483 mutex_exit(&rt_list_lock);
488 484 return (0);
489 485 }
490 486
491 487
492 488 /*
493 489 * The child goes to the back of its dispatcher queue while the
494 490 * parent continues to run after a real time thread forks.
495 491 */
496 492 /* ARGSUSED */
497 493 static void
498 494 rt_forkret(kthread_t *t, kthread_t *ct)
499 495 {
500 496 proc_t *pp = ttoproc(t);
501 497 proc_t *cp = ttoproc(ct);
502 498
503 499 ASSERT(t == curthread);
504 500 ASSERT(MUTEX_HELD(&pidlock));
505 501
506 502 /*
507 503 * Grab the child's p_lock before dropping pidlock to ensure
508 504 * the process does not disappear before we set it running.
509 505 */
510 506 mutex_enter(&cp->p_lock);
511 507 mutex_exit(&pidlock);
512 508 continuelwps(cp);
513 509 mutex_exit(&cp->p_lock);
514 510
515 511 mutex_enter(&pp->p_lock);
516 512 continuelwps(pp);
517 513 mutex_exit(&pp->p_lock);
518 514 }
519 515
520 516
521 517 /*
522 518 * Get information about the real-time class into the buffer
523 519 * pointed to by rtinfop. The maximum configured real-time
524 520 * priority is the only information we supply. We ignore the
525 521 * class and credential arguments because anyone can have this
526 522 * information.
527 523 */
528 524 /* ARGSUSED */
529 525 static int
530 526 rt_getclinfo(void *infop)
531 527 {
532 528 rtinfo_t *rtinfop = (rtinfo_t *)infop;
533 529 rtinfop->rt_maxpri = rt_maxpri;
534 530 return (0);
535 531 }
536 532
537 533 /*
538 534 * Return the user mode scheduling priority range.
539 535 */
540 536 static int
541 537 rt_getclpri(pcpri_t *pcprip)
542 538 {
543 539 pcprip->pc_clpmax = rt_maxpri;
544 540 pcprip->pc_clpmin = 0;
545 541 return (0);
546 542 }
547 543
548 544 static void
549 545 rt_nullsys()
550 546 {
551 547 }
552 548
553 549 /* ARGSUSED */
554 550 static int
555 551 rt_canexit(kthread_t *t, cred_t *cred)
556 552 {
557 553 /*
558 554 * Thread can always leave RT class
559 555 */
560 556 return (0);
561 557 }
562 558
563 559 /*
564 560 * Get the real-time scheduling parameters of the thread pointed to by
565 561 * rtprocp into the buffer pointed to by rtkparmsp.
566 562 */
567 563 static void
568 564 rt_parmsget(kthread_t *t, void *parmsp)
569 565 {
570 566 rtproc_t *rtprocp = (rtproc_t *)t->t_cldata;
571 567 rtkparms_t *rtkparmsp = (rtkparms_t *)parmsp;
572 568
573 569 rtkparmsp->rt_pri = rtprocp->rt_pri;
574 570 rtkparmsp->rt_tqntm = rtprocp->rt_pquantum;
575 571 rtkparmsp->rt_tqsig = rtprocp->rt_tqsignal;
576 572 }
577 573
578 574
579 575
580 576 /*
581 577 * Check the validity of the real-time parameters in the buffer
582 578 * pointed to by rtprmsp.
583 579 * We convert the rtparms buffer from the user supplied format to
584 580 * our internal format (i.e. time quantum expressed in ticks).
585 581 */
586 582 static int
587 583 rt_parmsin(void *prmsp)
588 584 {
589 585 rtparms_t *rtprmsp = (rtparms_t *)prmsp;
590 586 longlong_t ticks;
591 587 uint_t cflags;
592 588
593 589 /*
594 590 * First check the validity of parameters and convert
595 591 * the buffer to kernel format.
596 592 */
597 593 if ((rtprmsp->rt_pri < 0 || rtprmsp->rt_pri > rt_maxpri) &&
598 594 rtprmsp->rt_pri != RT_NOCHANGE)
599 595 return (EINVAL);
600 596
601 597 cflags = (rtprmsp->rt_pri != RT_NOCHANGE ? RT_DOPRI : 0);
602 598
603 599 if ((rtprmsp->rt_tqsecs == 0 && rtprmsp->rt_tqnsecs == 0) ||
604 600 rtprmsp->rt_tqnsecs >= NANOSEC)
605 601 return (EINVAL);
606 602
607 603 if (rtprmsp->rt_tqnsecs != RT_NOCHANGE)
608 604 cflags |= RT_DOTQ;
609 605
610 606 if (rtprmsp->rt_tqnsecs >= 0) {
611 607 if ((ticks = SEC_TO_TICK((longlong_t)rtprmsp->rt_tqsecs) +
612 608 NSEC_TO_TICK_ROUNDUP(rtprmsp->rt_tqnsecs)) > INT_MAX)
613 609 return (ERANGE);
614 610
615 611 ((rtkparms_t *)rtprmsp)->rt_tqntm = (int)ticks;
616 612 } else {
617 613 if (rtprmsp->rt_tqnsecs != RT_NOCHANGE &&
618 614 rtprmsp->rt_tqnsecs != RT_TQINF &&
619 615 rtprmsp->rt_tqnsecs != RT_TQDEF)
620 616 return (EINVAL);
621 617
622 618 ((rtkparms_t *)rtprmsp)->rt_tqntm = rtprmsp->rt_tqnsecs;
623 619 }
624 620 ((rtkparms_t *)rtprmsp)->rt_cflags = cflags;
625 621
626 622 return (0);
627 623 }
628 624
629 625
630 626 /*
631 627 * Check the validity of the real-time parameters in the pc_vaparms_t
632 628 * structure vaparmsp and put them in the buffer pointed to by rtprmsp.
633 629 * pc_vaparms_t contains (key, value) pairs of parameter.
634 630 * rt_vaparmsin() is the variable parameter version of rt_parmsin().
635 631 */
636 632 static int
637 633 rt_vaparmsin(void *prmsp, pc_vaparms_t *vaparmsp)
638 634 {
639 635 uint_t secs = 0;
640 636 uint_t cnt;
641 637 int nsecs = 0;
642 638 int priflag, secflag, nsecflag, sigflag;
643 639 longlong_t ticks;
644 640 rtkparms_t *rtprmsp = (rtkparms_t *)prmsp;
645 641 pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
646 642
647 643
648 644 /*
649 645 * First check the validity of parameters and convert them
650 646 * from the user supplied format to the internal format.
651 647 */
652 648 priflag = secflag = nsecflag = sigflag = 0;
653 649 rtprmsp->rt_cflags = 0;
654 650
655 651 if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
656 652 return (EINVAL);
657 653
658 654 for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
659 655
660 656 switch (vpp->pc_key) {
661 657 case RT_KY_PRI:
662 658 if (priflag++)
663 659 return (EINVAL);
664 660 rtprmsp->rt_cflags |= RT_DOPRI;
665 661 rtprmsp->rt_pri = (pri_t)vpp->pc_parm;
666 662 if (rtprmsp->rt_pri < 0 || rtprmsp->rt_pri > rt_maxpri)
667 663 return (EINVAL);
668 664 break;
669 665
670 666 case RT_KY_TQSECS:
671 667 if (secflag++)
672 668 return (EINVAL);
673 669 rtprmsp->rt_cflags |= RT_DOTQ;
674 670 secs = (uint_t)vpp->pc_parm;
675 671 break;
676 672
677 673 case RT_KY_TQNSECS:
678 674 if (nsecflag++)
679 675 return (EINVAL);
680 676 rtprmsp->rt_cflags |= RT_DOTQ;
681 677 nsecs = (int)vpp->pc_parm;
682 678 break;
683 679
684 680 case RT_KY_TQSIG:
685 681 if (sigflag++)
686 682 return (EINVAL);
687 683 rtprmsp->rt_cflags |= RT_DOSIG;
688 684 rtprmsp->rt_tqsig = (int)vpp->pc_parm;
689 685 if (rtprmsp->rt_tqsig < 0 || rtprmsp->rt_tqsig >= NSIG)
690 686 return (EINVAL);
691 687 break;
692 688
693 689 default:
694 690 return (EINVAL);
695 691 }
696 692 }
697 693
698 694 if (vaparmsp->pc_vaparmscnt == 0) {
699 695 /*
700 696 * Use default parameters.
701 697 */
702 698 rtprmsp->rt_pri = 0;
703 699 rtprmsp->rt_tqntm = RT_TQDEF;
704 700 rtprmsp->rt_tqsig = 0;
705 701 rtprmsp->rt_cflags = RT_DOPRI | RT_DOTQ | RT_DOSIG;
706 702 } else if ((rtprmsp->rt_cflags & RT_DOTQ) != 0) {
707 703 if ((secs == 0 && nsecs == 0) || nsecs >= NANOSEC)
708 704 return (EINVAL);
709 705
710 706 if (nsecs >= 0) {
711 707 if ((ticks = SEC_TO_TICK((longlong_t)secs) +
712 708 NSEC_TO_TICK_ROUNDUP(nsecs)) > INT_MAX)
713 709 return (ERANGE);
714 710
715 711 rtprmsp->rt_tqntm = (int)ticks;
716 712 } else {
717 713 if (nsecs != RT_TQINF && nsecs != RT_TQDEF)
718 714 return (EINVAL);
719 715 rtprmsp->rt_tqntm = nsecs;
720 716 }
721 717 }
722 718
723 719 return (0);
724 720 }
725 721
726 722 /*
727 723 * Do required processing on the real-time parameter buffer
728 724 * before it is copied out to the user.
729 725 * All we have to do is convert the buffer from kernel to user format
730 726 * (i.e. convert time quantum from ticks to seconds-nanoseconds).
731 727 */
732 728 /* ARGSUSED */
733 729 static int
734 730 rt_parmsout(void *prmsp, pc_vaparms_t *vaparmsp)
735 731 {
736 732 rtkparms_t *rtkprmsp = (rtkparms_t *)prmsp;
737 733
738 734 if (vaparmsp != NULL)
739 735 return (0);
740 736
741 737 if (rtkprmsp->rt_tqntm < 0) {
742 738 /*
743 739 * Quantum field set to special value (e.g. RT_TQINF)
744 740 */
745 741 ((rtparms_t *)rtkprmsp)->rt_tqnsecs = rtkprmsp->rt_tqntm;
746 742 ((rtparms_t *)rtkprmsp)->rt_tqsecs = 0;
747 743 } else {
748 744 /* Convert quantum from ticks to seconds-nanoseconds */
749 745
750 746 timestruc_t ts;
751 747 TICK_TO_TIMESTRUC(rtkprmsp->rt_tqntm, &ts);
752 748 ((rtparms_t *)rtkprmsp)->rt_tqsecs = ts.tv_sec;
753 749 ((rtparms_t *)rtkprmsp)->rt_tqnsecs = ts.tv_nsec;
754 750 }
755 751
756 752 return (0);
757 753 }
758 754
759 755
760 756 /*
761 757 * Copy all selected real-time class parameters to the user.
762 758 * The parameters are specified by a key.
763 759 */
764 760 static int
765 761 rt_vaparmsout(void *prmsp, pc_vaparms_t *vaparmsp)
766 762 {
767 763 rtkparms_t *rtkprmsp = (rtkparms_t *)prmsp;
768 764 timestruc_t ts;
769 765 uint_t cnt;
770 766 uint_t secs;
771 767 int nsecs;
772 768 int priflag, secflag, nsecflag, sigflag;
773 769 pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
774 770
775 771 ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
776 772
777 773 priflag = secflag = nsecflag = sigflag = 0;
778 774
779 775 if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
780 776 return (EINVAL);
781 777
782 778 if (rtkprmsp->rt_tqntm < 0) {
783 779 /*
784 780 * Quantum field set to special value (e.g. RT_TQINF).
785 781 */
786 782 secs = 0;
787 783 nsecs = rtkprmsp->rt_tqntm;
788 784 } else {
789 785 /*
790 786 * Convert quantum from ticks to seconds-nanoseconds.
791 787 */
792 788 TICK_TO_TIMESTRUC(rtkprmsp->rt_tqntm, &ts);
793 789 secs = ts.tv_sec;
794 790 nsecs = ts.tv_nsec;
795 791 }
796 792
797 793
798 794 for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
799 795
800 796 switch (vpp->pc_key) {
801 797 case RT_KY_PRI:
802 798 if (priflag++)
803 799 return (EINVAL);
804 800 if (copyout(&rtkprmsp->rt_pri,
805 801 (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
806 802 return (EFAULT);
807 803 break;
808 804
809 805 case RT_KY_TQSECS:
810 806 if (secflag++)
811 807 return (EINVAL);
812 808 if (copyout(&secs, (caddr_t)(uintptr_t)vpp->pc_parm,
813 809 sizeof (uint_t)))
814 810 return (EFAULT);
815 811 break;
816 812
817 813 case RT_KY_TQNSECS:
818 814 if (nsecflag++)
819 815 return (EINVAL);
820 816 if (copyout(&nsecs, (caddr_t)(uintptr_t)vpp->pc_parm,
821 817 sizeof (int)))
822 818 return (EFAULT);
823 819 break;
824 820
825 821 case RT_KY_TQSIG:
826 822 if (sigflag++)
827 823 return (EINVAL);
828 824 if (copyout(&rtkprmsp->rt_tqsig,
829 825 (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (int)))
830 826 return (EFAULT);
831 827 break;
832 828
833 829 default:
834 830 return (EINVAL);
835 831 }
836 832 }
837 833
838 834 return (0);
839 835 }
840 836
841 837
842 838 /*
843 839 * Set the scheduling parameters of the thread pointed to by rtprocp
844 840 * to those specified in the buffer pointed to by rtkprmsp.
845 841 * Note that the parameters are expected to be in kernel format
846 842 * (i.e. time quantm expressed in ticks). Real time parameters copied
847 843 * in from the user should be processed by rt_parmsin() before they are
848 844 * passed to this function.
849 845 */
850 846 static int
851 847 rt_parmsset(kthread_t *tx, void *prmsp, id_t reqpcid, cred_t *reqpcredp)
852 848 {
853 849 rtkparms_t *rtkprmsp = (rtkparms_t *)prmsp;
854 850 rtproc_t *rtpp = (rtproc_t *)tx->t_cldata;
855 851
856 852 ASSERT(MUTEX_HELD(&(ttoproc(tx))->p_lock));
857 853
858 854 /*
859 855 * Basic permissions enforced by generic kernel code
860 856 * for all classes require that a thread attempting
861 857 * to change the scheduling parameters of a target thread
862 858 * be privileged or have a real or effective UID
863 859 * matching that of the target thread. We are not
864 860 * called unless these basic permission checks have
865 861 * already passed. The real-time class requires in addition
866 862 * that the requesting thread be real-time unless it is privileged.
867 863 * This may also have been checked previously but if our caller
868 864 * passes us a credential structure we assume it hasn't and
869 865 * we check it here.
870 866 */
871 867 if (reqpcredp != NULL && reqpcid != rt_cid &&
872 868 secpolicy_raisepriority(reqpcredp) != 0)
873 869 return (EPERM);
874 870
875 871 thread_lock(tx);
876 872 if ((rtkprmsp->rt_cflags & RT_DOPRI) != 0) {
877 873 rtpp->rt_pri = rtkprmsp->rt_pri;
878 874 rt_change_priority(tx, rtpp);
879 875 }
880 876 if (rtkprmsp->rt_tqntm == RT_TQINF)
881 877 rtpp->rt_pquantum = RT_TQINF;
882 878 else if (rtkprmsp->rt_tqntm == RT_TQDEF)
883 879 rtpp->rt_timeleft = rtpp->rt_pquantum =
884 880 rt_dptbl[rtpp->rt_pri].rt_quantum;
885 881 else if ((rtkprmsp->rt_cflags & RT_DOTQ) != 0)
886 882 rtpp->rt_timeleft = rtpp->rt_pquantum = rtkprmsp->rt_tqntm;
887 883
888 884 if ((rtkprmsp->rt_cflags & RT_DOSIG) != 0)
889 885 rtpp->rt_tqsignal = rtkprmsp->rt_tqsig;
890 886
891 887 thread_unlock(tx);
892 888 return (0);
893 889 }
894 890
↓ open down ↓ |
710 lines elided |
↑ open up ↑ |
895 891
896 892 /*
897 893 * Arrange for thread to be placed in appropriate location
898 894 * on dispatcher queue. Runs at splhi() since the clock
899 895 * interrupt can cause RTBACKQ to be set.
900 896 */
901 897 static void
902 898 rt_preempt(kthread_t *t)
903 899 {
904 900 rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
905 - klwp_t *lwp;
906 901
907 902 ASSERT(THREAD_LOCK_HELD(t));
908 903
909 - /*
910 - * If the state is user I allow swapping because I know I won't
911 - * be holding any locks.
912 - */
913 - if ((lwp = curthread->t_lwp) != NULL && lwp->lwp_state == LWP_USER)
914 - t->t_schedflag &= ~TS_DONT_SWAP;
915 904 if ((rtpp->rt_flags & RTBACKQ) != 0) {
916 905 rtpp->rt_timeleft = rtpp->rt_pquantum;
917 906 rtpp->rt_flags &= ~RTBACKQ;
918 907 setbackdq(t);
919 908 } else
920 909 setfrontdq(t);
921 910
922 911 }
923 912
924 913 /*
925 914 * Return the global priority associated with this rt_pri.
926 915 */
927 916 static pri_t
928 917 rt_globpri(kthread_t *t)
929 918 {
930 919 rtproc_t *rtprocp = (rtproc_t *)t->t_cldata;
931 920 return (rt_dptbl[rtprocp->rt_pri].rt_globpri);
932 921 }
933 922
↓ open down ↓ |
9 lines elided |
↑ open up ↑ |
934 923 static void
935 924 rt_setrun(kthread_t *t)
936 925 {
937 926 rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
938 927
939 928 ASSERT(THREAD_LOCK_HELD(t));
940 929
941 930 rtpp->rt_timeleft = rtpp->rt_pquantum;
942 931 rtpp->rt_flags &= ~RTBACKQ;
943 932 setbackdq(t);
944 -}
945 -
946 -/*
947 - * Returns the priority of the thread, -1 if the thread is loaded or ineligible
948 - * for swapin.
949 - *
950 - * FX and RT threads are designed so that they don't swapout; however, it
951 - * is possible that while the thread is swapped out and in another class, it
952 - * can be changed to FX or RT. Since these threads should be swapped in as
953 - * soon as they're runnable, rt_swapin returns SHRT_MAX, and fx_swapin
954 - * returns SHRT_MAX - 1, so that it gives deference to any swapped out RT
955 - * threads.
956 - */
957 -/* ARGSUSED */
958 -static pri_t
959 -rt_swapin(kthread_t *t, int flags)
960 -{
961 - pri_t tpri = -1;
962 -
963 - ASSERT(THREAD_LOCK_HELD(t));
964 -
965 - if (t->t_state == TS_RUN && (t->t_schedflag & TS_LOAD) == 0) {
966 - tpri = (pri_t)SHRT_MAX;
967 - }
968 -
969 - return (tpri);
970 -}
971 -
972 -/*
973 - * Return an effective priority for swapout.
974 - */
975 -/* ARGSUSED */
976 -static pri_t
977 -rt_swapout(kthread_t *t, int flags)
978 -{
979 - ASSERT(THREAD_LOCK_HELD(t));
980 -
981 - return (-1);
982 933 }
983 934
984 935 /*
985 936 * Check for time slice expiration (unless thread has infinite time
986 937 * slice). If time slice has expired arrange for thread to be preempted
987 938 * and placed on back of queue.
988 939 */
989 940 static void
990 941 rt_tick(kthread_t *t)
991 942 {
992 943 rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
993 944
994 945 ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
995 946
996 947 thread_lock(t);
997 948 if ((rtpp->rt_pquantum != RT_TQINF && --rtpp->rt_timeleft == 0) ||
998 949 (t->t_state == TS_ONPROC && DISP_MUST_SURRENDER(t))) {
999 950 if (rtpp->rt_timeleft == 0 && rtpp->rt_tqsignal) {
1000 951 thread_unlock(t);
1001 952 sigtoproc(ttoproc(t), t, rtpp->rt_tqsignal);
1002 953 thread_lock(t);
1003 954 }
1004 955 rtpp->rt_flags |= RTBACKQ;
1005 956 cpu_surrender(t);
1006 957 }
1007 958 thread_unlock(t);
1008 959 }
1009 960
1010 961
1011 962 /*
1012 963 * Place the thread waking up on the dispatcher queue.
1013 964 */
1014 965 static void
1015 966 rt_wakeup(kthread_t *t)
1016 967 {
1017 968 rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
1018 969
1019 970 ASSERT(THREAD_LOCK_HELD(t));
1020 971
1021 972 rtpp->rt_timeleft = rtpp->rt_pquantum;
1022 973 rtpp->rt_flags &= ~RTBACKQ;
1023 974 setbackdq(t);
1024 975 }
1025 976
1026 977 static void
1027 978 rt_yield(kthread_t *t)
1028 979 {
1029 980 rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
1030 981
1031 982 ASSERT(t == curthread);
1032 983 ASSERT(THREAD_LOCK_HELD(t));
1033 984
1034 985 rtpp->rt_flags &= ~RTBACKQ;
1035 986 setbackdq(t);
1036 987 }
1037 988
1038 989 /* ARGSUSED */
1039 990 static int
1040 991 rt_donice(kthread_t *t, cred_t *cr, int incr, int *retvalp)
1041 992 {
1042 993 return (EINVAL);
1043 994 }
1044 995
1045 996 /*
1046 997 * Increment the priority of the specified thread by incr and
1047 998 * return the new value in *retvalp.
1048 999 */
1049 1000 static int
1050 1001 rt_doprio(kthread_t *t, cred_t *cr, int incr, int *retvalp)
1051 1002 {
1052 1003 int newpri;
1053 1004 rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
1054 1005 rtkparms_t rtkparms;
1055 1006
1056 1007 /* If there's no change to the priority, just return current setting */
1057 1008 if (incr == 0) {
1058 1009 *retvalp = rtpp->rt_pri;
1059 1010 return (0);
1060 1011 }
1061 1012
1062 1013 newpri = rtpp->rt_pri + incr;
1063 1014 if (newpri > rt_maxpri || newpri < 0)
1064 1015 return (EINVAL);
1065 1016
1066 1017 *retvalp = newpri;
1067 1018 rtkparms.rt_pri = newpri;
1068 1019 rtkparms.rt_tqntm = RT_NOCHANGE;
1069 1020 rtkparms.rt_tqsig = 0;
1070 1021 rtkparms.rt_cflags = RT_DOPRI;
1071 1022 return (rt_parmsset(t, &rtkparms, rt_cid, cr));
1072 1023 }
1073 1024
1074 1025 static int
1075 1026 rt_alloc(void **p, int flag)
1076 1027 {
1077 1028 void *bufp;
1078 1029 bufp = kmem_alloc(sizeof (rtproc_t), flag);
1079 1030 if (bufp == NULL) {
1080 1031 return (ENOMEM);
1081 1032 } else {
1082 1033 *p = bufp;
1083 1034 return (0);
1084 1035 }
1085 1036 }
1086 1037
1087 1038 static void
1088 1039 rt_free(void *bufp)
1089 1040 {
1090 1041 if (bufp)
1091 1042 kmem_free(bufp, sizeof (rtproc_t));
1092 1043 }
1093 1044
1094 1045 static void
1095 1046 rt_change_priority(kthread_t *t, rtproc_t *rtpp)
1096 1047 {
1097 1048 pri_t new_pri;
1098 1049
1099 1050 ASSERT(THREAD_LOCK_HELD(t));
1100 1051
1101 1052 new_pri = rt_dptbl[rtpp->rt_pri].rt_globpri;
1102 1053
1103 1054 t->t_cpri = rtpp->rt_pri;
1104 1055 if (t == curthread || t->t_state == TS_ONPROC) {
1105 1056 cpu_t *cp = t->t_disp_queue->disp_cpu;
1106 1057 THREAD_CHANGE_PRI(t, new_pri);
1107 1058 if (t == cp->cpu_dispthread)
1108 1059 cp->cpu_dispatch_pri = DISP_PRIO(t);
1109 1060 if (DISP_MUST_SURRENDER(t)) {
1110 1061 rtpp->rt_flags |= RTBACKQ;
1111 1062 cpu_surrender(t);
1112 1063 } else {
1113 1064 rtpp->rt_timeleft = rtpp->rt_pquantum;
1114 1065 }
1115 1066 } else {
1116 1067 /*
1117 1068 * When the priority of a thread is changed,
1118 1069 * it may be necessary to adjust its position
1119 1070 * on a sleep queue or dispatch queue. The
1120 1071 * function thread_change_pri() accomplishes this.
1121 1072 */
1122 1073 if (thread_change_pri(t, new_pri, 0)) {
1123 1074 /*
1124 1075 * The thread was on a run queue.
1125 1076 * Reset its CPU timeleft.
1126 1077 */
1127 1078 rtpp->rt_timeleft = rtpp->rt_pquantum;
1128 1079 } else {
1129 1080 rtpp->rt_flags |= RTBACKQ;
1130 1081 }
1131 1082 }
1132 1083 }
↓ open down ↓ |
141 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX