1 /*- 2 * Copyright (c) 1982, 1986, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include "opt_compat.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/sysproto.h> 45 #include <sys/file.h> 46 #include <sys/kernel.h> 47 #include <sys/lock.h> 48 #include <sys/malloc.h> 49 #include <sys/mutex.h> 50 #include <sys/proc.h> 51 #include <sys/refcount.h> 52 #include <sys/resourcevar.h> 53 #include <sys/sched.h> 54 #include <sys/sx.h> 55 #include <sys/syscallsubr.h> 56 #include <sys/sysent.h> 57 #include <sys/time.h> 58 59 #include <vm/vm.h> 60 #include <vm/vm_param.h> 61 #include <vm/pmap.h> 62 #include <vm/vm_map.h> 63 64 65 static MALLOC_DEFINE(M_PLIMIT, "plimit", "plimit structures"); 66 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures"); 67 #define UIHASH(uid) (&uihashtbl[(uid) & uihash]) 68 static struct mtx uihashtbl_mtx; 69 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl; 70 static u_long uihash; /* size of hash table - 1 */ 71 72 static void calcru1(struct proc *p, struct rusage_ext *ruxp, 73 struct timeval *up, struct timeval *sp); 74 static int donice(struct thread *td, struct proc *chgp, int n); 75 static struct uidinfo *uilookup(uid_t uid); 76 77 /* 78 * Resource controls and accounting. 79 */ 80 81 #ifndef _SYS_SYSPROTO_H_ 82 struct getpriority_args { 83 int which; 84 int who; 85 }; 86 #endif 87 /* 88 * MPSAFE 89 */ 90 int 91 getpriority(td, uap) 92 struct thread *td; 93 register struct getpriority_args *uap; 94 { 95 struct proc *p; 96 struct pgrp *pg; 97 int error, low; 98 99 error = 0; 100 low = PRIO_MAX + 1; 101 switch (uap->which) { 102 103 case PRIO_PROCESS: 104 if (uap->who == 0) 105 low = td->td_proc->p_nice; 106 else { 107 p = pfind(uap->who); 108 if (p == NULL) 109 break; 110 if (p_cansee(td, p) == 0) 111 low = p->p_nice; 112 PROC_UNLOCK(p); 113 } 114 break; 115 116 case PRIO_PGRP: 117 sx_slock(&proctree_lock); 118 if (uap->who == 0) { 119 pg = td->td_proc->p_pgrp; 120 PGRP_LOCK(pg); 121 } else { 122 pg = pgfind(uap->who); 123 if (pg == NULL) { 124 sx_sunlock(&proctree_lock); 125 break; 126 } 127 } 128 sx_sunlock(&proctree_lock); 129 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 130 PROC_LOCK(p); 131 if (!p_cansee(td, p)) { 132 if (p->p_nice < low) 133 low = p->p_nice; 134 } 135 PROC_UNLOCK(p); 136 } 137 PGRP_UNLOCK(pg); 138 break; 139 140 case PRIO_USER: 141 if (uap->who == 0) 142 uap->who = td->td_ucred->cr_uid; 143 sx_slock(&allproc_lock); 144 LIST_FOREACH(p, &allproc, p_list) { 145 PROC_LOCK(p); 146 if (!p_cansee(td, p) && 147 p->p_ucred->cr_uid == uap->who) { 148 if (p->p_nice < low) 149 low = p->p_nice; 150 } 151 PROC_UNLOCK(p); 152 } 153 sx_sunlock(&allproc_lock); 154 break; 155 156 default: 157 error = EINVAL; 158 break; 159 } 160 if (low == PRIO_MAX + 1 && error == 0) 161 error = ESRCH; 162 td->td_retval[0] = low; 163 return (error); 164 } 165 166 #ifndef _SYS_SYSPROTO_H_ 167 struct setpriority_args { 168 int which; 169 int who; 170 int prio; 171 }; 172 #endif 173 /* 174 * MPSAFE 175 */ 176 int 177 setpriority(td, uap) 178 struct thread *td; 179 struct setpriority_args *uap; 180 { 181 struct proc *curp, *p; 182 struct pgrp *pg; 183 int found = 0, error = 0; 184 185 curp = td->td_proc; 186 switch (uap->which) { 187 case PRIO_PROCESS: 188 if (uap->who == 0) { 189 PROC_LOCK(curp); 190 error = donice(td, curp, uap->prio); 191 PROC_UNLOCK(curp); 192 } else { 193 p = pfind(uap->who); 194 if (p == 0) 195 break; 196 if (p_cansee(td, p) == 0) 197 error = donice(td, p, uap->prio); 198 PROC_UNLOCK(p); 199 } 200 found++; 201 break; 202 203 case PRIO_PGRP: 204 sx_slock(&proctree_lock); 205 if (uap->who == 0) { 206 pg = curp->p_pgrp; 207 PGRP_LOCK(pg); 208 } else { 209 pg = pgfind(uap->who); 210 if (pg == NULL) { 211 sx_sunlock(&proctree_lock); 212 break; 213 } 214 } 215 sx_sunlock(&proctree_lock); 216 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 217 PROC_LOCK(p); 218 if (!p_cansee(td, p)) { 219 error = donice(td, p, uap->prio); 220 found++; 221 } 222 PROC_UNLOCK(p); 223 } 224 PGRP_UNLOCK(pg); 225 break; 226 227 case PRIO_USER: 228 if (uap->who == 0) 229 uap->who = td->td_ucred->cr_uid; 230 sx_slock(&allproc_lock); 231 FOREACH_PROC_IN_SYSTEM(p) { 232 PROC_LOCK(p); 233 if (p->p_ucred->cr_uid == uap->who && 234 !p_cansee(td, p)) { 235 error = donice(td, p, uap->prio); 236 found++; 237 } 238 PROC_UNLOCK(p); 239 } 240 sx_sunlock(&allproc_lock); 241 break; 242 243 default: 244 error = EINVAL; 245 break; 246 } 247 if (found == 0 && error == 0) 248 error = ESRCH; 249 return (error); 250 } 251 252 /* 253 * Set "nice" for a (whole) process. 254 */ 255 static int 256 donice(struct thread *td, struct proc *p, int n) 257 { 258 int error; 259 260 PROC_LOCK_ASSERT(p, MA_OWNED); 261 if ((error = p_cansched(td, p))) 262 return (error); 263 if (n > PRIO_MAX) 264 n = PRIO_MAX; 265 if (n < PRIO_MIN) 266 n = PRIO_MIN; 267 if (n < p->p_nice && suser(td) != 0) 268 return (EACCES); 269 mtx_lock_spin(&sched_lock); 270 sched_nice(p, n); 271 mtx_unlock_spin(&sched_lock); 272 return (0); 273 } 274 275 /* 276 * Set realtime priority. 277 * 278 * MPSAFE 279 */ 280 #ifndef _SYS_SYSPROTO_H_ 281 struct rtprio_args { 282 int function; 283 pid_t pid; 284 struct rtprio *rtp; 285 }; 286 #endif 287 288 int 289 rtprio(td, uap) 290 struct thread *td; /* curthread */ 291 register struct rtprio_args *uap; 292 { 293 struct proc *curp; 294 struct proc *p; 295 struct ksegrp *kg; 296 struct rtprio rtp; 297 int cierror, error; 298 299 /* Perform copyin before acquiring locks if needed. */ 300 if (uap->function == RTP_SET) 301 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio)); 302 else 303 cierror = 0; 304 305 curp = td->td_proc; 306 if (uap->pid == 0) { 307 p = curp; 308 PROC_LOCK(p); 309 } else { 310 p = pfind(uap->pid); 311 if (p == NULL) 312 return (ESRCH); 313 } 314 315 switch (uap->function) { 316 case RTP_LOOKUP: 317 if ((error = p_cansee(td, p))) 318 break; 319 mtx_lock_spin(&sched_lock); 320 /* 321 * Return OUR priority if no pid specified, 322 * or if one is, report the highest priority 323 * in the process. There isn't much more you can do as 324 * there is only room to return a single priority. 325 * XXXKSE: maybe need a new interface to report 326 * priorities of multiple system scope threads. 327 * Note: specifying our own pid is not the same 328 * as leaving it zero. 329 */ 330 if (uap->pid == 0) { 331 pri_to_rtp(td->td_ksegrp, &rtp); 332 } else { 333 struct rtprio rtp2; 334 335 rtp.type = RTP_PRIO_IDLE; 336 rtp.prio = RTP_PRIO_MAX; 337 FOREACH_KSEGRP_IN_PROC(p, kg) { 338 pri_to_rtp(kg, &rtp2); 339 if (rtp2.type < rtp.type || 340 (rtp2.type == rtp.type && 341 rtp2.prio < rtp.prio)) { 342 rtp.type = rtp2.type; 343 rtp.prio = rtp2.prio; 344 } 345 } 346 } 347 mtx_unlock_spin(&sched_lock); 348 PROC_UNLOCK(p); 349 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio))); 350 case RTP_SET: 351 if ((error = p_cansched(td, p)) || (error = cierror)) 352 break; 353 354 /* Disallow setting rtprio in most cases if not superuser. */ 355 if (suser(td) != 0) { 356 /* can't set someone else's */ 357 if (uap->pid) { 358 error = EPERM; 359 break; 360 } 361 /* can't set realtime priority */ 362 /* 363 * Realtime priority has to be restricted for reasons which should be 364 * obvious. However, for idle priority, there is a potential for 365 * system deadlock if an idleprio process gains a lock on a resource 366 * that other processes need (and the idleprio process can't run 367 * due to a CPU-bound normal process). Fix me! XXX 368 */ 369 #if 0 370 if (RTP_PRIO_IS_REALTIME(rtp.type)) { 371 #else 372 if (rtp.type != RTP_PRIO_NORMAL) { 373 #endif 374 error = EPERM; 375 break; 376 } 377 } 378 379 /* 380 * If we are setting our own priority, set just our 381 * KSEGRP but if we are doing another process, 382 * do all the groups on that process. If we 383 * specify our own pid we do the latter. 384 */ 385 mtx_lock_spin(&sched_lock); 386 if (uap->pid == 0) { 387 error = rtp_to_pri(&rtp, td->td_ksegrp); 388 } else { 389 FOREACH_KSEGRP_IN_PROC(p, kg) { 390 if ((error = rtp_to_pri(&rtp, kg)) != 0) { 391 break; 392 } 393 } 394 } 395 mtx_unlock_spin(&sched_lock); 396 break; 397 default: 398 error = EINVAL; 399 break; 400 } 401 PROC_UNLOCK(p); 402 return (error); 403 } 404 405 int 406 rtp_to_pri(struct rtprio *rtp, struct ksegrp *kg) 407 { 408 409 mtx_assert(&sched_lock, MA_OWNED); 410 if (rtp->prio > RTP_PRIO_MAX) 411 return (EINVAL); 412 switch (RTP_PRIO_BASE(rtp->type)) { 413 case RTP_PRIO_REALTIME: 414 kg->kg_user_pri = PRI_MIN_REALTIME + rtp->prio; 415 break; 416 case RTP_PRIO_NORMAL: 417 kg->kg_user_pri = PRI_MIN_TIMESHARE + rtp->prio; 418 break; 419 case RTP_PRIO_IDLE: 420 kg->kg_user_pri = PRI_MIN_IDLE + rtp->prio; 421 break; 422 default: 423 return (EINVAL); 424 } 425 sched_class(kg, rtp->type); 426 if (curthread->td_ksegrp == kg) { 427 sched_prio(curthread, kg->kg_user_pri); /* XXX dubious */ 428 } 429 return (0); 430 } 431 432 void 433 pri_to_rtp(struct ksegrp *kg, struct rtprio *rtp) 434 { 435 436 mtx_assert(&sched_lock, MA_OWNED); 437 switch (PRI_BASE(kg->kg_pri_class)) { 438 case PRI_REALTIME: 439 rtp->prio = kg->kg_user_pri - PRI_MIN_REALTIME; 440 break; 441 case PRI_TIMESHARE: 442 rtp->prio = kg->kg_user_pri - PRI_MIN_TIMESHARE; 443 break; 444 case PRI_IDLE: 445 rtp->prio = kg->kg_user_pri - PRI_MIN_IDLE; 446 break; 447 default: 448 break; 449 } 450 rtp->type = kg->kg_pri_class; 451 } 452 453 #if defined(COMPAT_43) 454 #ifndef _SYS_SYSPROTO_H_ 455 struct osetrlimit_args { 456 u_int which; 457 struct orlimit *rlp; 458 }; 459 #endif 460 /* 461 * MPSAFE 462 */ 463 int 464 osetrlimit(td, uap) 465 struct thread *td; 466 register struct osetrlimit_args *uap; 467 { 468 struct orlimit olim; 469 struct rlimit lim; 470 int error; 471 472 if ((error = copyin(uap->rlp, &olim, sizeof(struct orlimit)))) 473 return (error); 474 lim.rlim_cur = olim.rlim_cur; 475 lim.rlim_max = olim.rlim_max; 476 error = kern_setrlimit(td, uap->which, &lim); 477 return (error); 478 } 479 480 #ifndef _SYS_SYSPROTO_H_ 481 struct ogetrlimit_args { 482 u_int which; 483 struct orlimit *rlp; 484 }; 485 #endif 486 /* 487 * MPSAFE 488 */ 489 int 490 ogetrlimit(td, uap) 491 struct thread *td; 492 register struct ogetrlimit_args *uap; 493 { 494 struct orlimit olim; 495 struct rlimit rl; 496 struct proc *p; 497 int error; 498 499 if (uap->which >= RLIM_NLIMITS) 500 return (EINVAL); 501 p = td->td_proc; 502 PROC_LOCK(p); 503 lim_rlimit(p, uap->which, &rl); 504 PROC_UNLOCK(p); 505 506 /* 507 * XXX would be more correct to convert only RLIM_INFINITY to the 508 * old RLIM_INFINITY and fail with EOVERFLOW for other larger 509 * values. Most 64->32 and 32->16 conversions, including not 510 * unimportant ones of uids are even more broken than what we 511 * do here (they blindly truncate). We don't do this correctly 512 * here since we have little experience with EOVERFLOW yet. 513 * Elsewhere, getuid() can't fail... 514 */ 515 olim.rlim_cur = rl.rlim_cur > 0x7fffffff ? 0x7fffffff : rl.rlim_cur; 516 olim.rlim_max = rl.rlim_max > 0x7fffffff ? 0x7fffffff : rl.rlim_max; 517 error = copyout(&olim, uap->rlp, sizeof(olim)); 518 return (error); 519 } 520 #endif /* COMPAT_43 */ 521 522 #ifndef _SYS_SYSPROTO_H_ 523 struct __setrlimit_args { 524 u_int which; 525 struct rlimit *rlp; 526 }; 527 #endif 528 /* 529 * MPSAFE 530 */ 531 int 532 setrlimit(td, uap) 533 struct thread *td; 534 register struct __setrlimit_args *uap; 535 { 536 struct rlimit alim; 537 int error; 538 539 if ((error = copyin(uap->rlp, &alim, sizeof(struct rlimit)))) 540 return (error); 541 error = kern_setrlimit(td, uap->which, &alim); 542 return (error); 543 } 544 545 int 546 kern_setrlimit(td, which, limp) 547 struct thread *td; 548 u_int which; 549 struct rlimit *limp; 550 { 551 struct plimit *newlim, *oldlim; 552 struct proc *p; 553 register struct rlimit *alimp; 554 rlim_t oldssiz; 555 int error; 556 557 if (which >= RLIM_NLIMITS) 558 return (EINVAL); 559 560 /* 561 * Preserve historical bugs by treating negative limits as unsigned. 562 */ 563 if (limp->rlim_cur < 0) 564 limp->rlim_cur = RLIM_INFINITY; 565 if (limp->rlim_max < 0) 566 limp->rlim_max = RLIM_INFINITY; 567 568 oldssiz = 0; 569 p = td->td_proc; 570 newlim = lim_alloc(); 571 PROC_LOCK(p); 572 oldlim = p->p_limit; 573 alimp = &oldlim->pl_rlimit[which]; 574 if (limp->rlim_cur > alimp->rlim_max || 575 limp->rlim_max > alimp->rlim_max) 576 if ((error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL))) { 577 PROC_UNLOCK(p); 578 lim_free(newlim); 579 return (error); 580 } 581 if (limp->rlim_cur > limp->rlim_max) 582 limp->rlim_cur = limp->rlim_max; 583 lim_copy(newlim, oldlim); 584 alimp = &newlim->pl_rlimit[which]; 585 586 switch (which) { 587 588 case RLIMIT_CPU: 589 mtx_lock_spin(&sched_lock); 590 p->p_cpulimit = limp->rlim_cur; 591 mtx_unlock_spin(&sched_lock); 592 break; 593 case RLIMIT_DATA: 594 if (limp->rlim_cur > maxdsiz) 595 limp->rlim_cur = maxdsiz; 596 if (limp->rlim_max > maxdsiz) 597 limp->rlim_max = maxdsiz; 598 break; 599 600 case RLIMIT_STACK: 601 if (limp->rlim_cur > maxssiz) 602 limp->rlim_cur = maxssiz; 603 if (limp->rlim_max > maxssiz) 604 limp->rlim_max = maxssiz; 605 oldssiz = alimp->rlim_cur; 606 break; 607 608 case RLIMIT_NOFILE: 609 if (limp->rlim_cur > maxfilesperproc) 610 limp->rlim_cur = maxfilesperproc; 611 if (limp->rlim_max > maxfilesperproc) 612 limp->rlim_max = maxfilesperproc; 613 break; 614 615 case RLIMIT_NPROC: 616 if (limp->rlim_cur > maxprocperuid) 617 limp->rlim_cur = maxprocperuid; 618 if (limp->rlim_max > maxprocperuid) 619 limp->rlim_max = maxprocperuid; 620 if (limp->rlim_cur < 1) 621 limp->rlim_cur = 1; 622 if (limp->rlim_max < 1) 623 limp->rlim_max = 1; 624 break; 625 } 626 *alimp = *limp; 627 p->p_limit = newlim; 628 PROC_UNLOCK(p); 629 lim_free(oldlim); 630 631 if (which == RLIMIT_STACK) { 632 /* 633 * Stack is allocated to the max at exec time with only 634 * "rlim_cur" bytes accessible. If stack limit is going 635 * up make more accessible, if going down make inaccessible. 636 */ 637 if (limp->rlim_cur != oldssiz) { 638 vm_offset_t addr; 639 vm_size_t size; 640 vm_prot_t prot; 641 642 if (limp->rlim_cur > oldssiz) { 643 prot = p->p_sysent->sv_stackprot; 644 size = limp->rlim_cur - oldssiz; 645 addr = p->p_sysent->sv_usrstack - 646 limp->rlim_cur; 647 } else { 648 prot = VM_PROT_NONE; 649 size = oldssiz - limp->rlim_cur; 650 addr = p->p_sysent->sv_usrstack - oldssiz; 651 } 652 addr = trunc_page(addr); 653 size = round_page(size); 654 (void)vm_map_protect(&p->p_vmspace->vm_map, 655 addr, addr + size, prot, FALSE); 656 } 657 } 658 659 /* 660 * The data size limit may need to be changed to a value 661 * that makes sense for the 32 bit binary. 662 */ 663 if (p->p_sysent->sv_fixlimits != NULL) 664 p->p_sysent->sv_fixlimits(p); 665 return (0); 666 } 667 668 #ifndef _SYS_SYSPROTO_H_ 669 struct __getrlimit_args { 670 u_int which; 671 struct rlimit *rlp; 672 }; 673 #endif 674 /* 675 * MPSAFE 676 */ 677 /* ARGSUSED */ 678 int 679 getrlimit(td, uap) 680 struct thread *td; 681 register struct __getrlimit_args *uap; 682 { 683 struct rlimit rlim; 684 struct proc *p; 685 int error; 686 687 if (uap->which >= RLIM_NLIMITS) 688 return (EINVAL); 689 p = td->td_proc; 690 PROC_LOCK(p); 691 lim_rlimit(p, uap->which, &rlim); 692 PROC_UNLOCK(p); 693 error = copyout(&rlim, uap->rlp, sizeof(struct rlimit)); 694 return (error); 695 } 696 697 /* 698 * Transform the running time and tick information in proc p into user, 699 * system, and interrupt time usage. 700 */ 701 void 702 calcru(p, up, sp) 703 struct proc *p; 704 struct timeval *up; 705 struct timeval *sp; 706 { 707 uint64_t bt; 708 struct rusage_ext rux; 709 struct thread *td; 710 int bt_valid; 711 712 PROC_LOCK_ASSERT(p, MA_OWNED); 713 mtx_assert(&sched_lock, MA_NOTOWNED); 714 bt_valid = 0; 715 bt = 0; 716 mtx_lock_spin(&sched_lock); 717 rux = p->p_rux; 718 FOREACH_THREAD_IN_PROC(p, td) { 719 if (TD_IS_RUNNING(td)) { 720 /* 721 * Adjust for the current time slice. This is 722 * actually fairly important since the error here is 723 * on the order of a time quantum which is much 724 * greater than the precision of binuptime(). 725 */ 726 KASSERT(td->td_oncpu != NOCPU, 727 ("%s: running thread has no CPU", __func__)); 728 if (!bt_valid) { 729 bt = cpu_ticks(); 730 bt_valid = 1; 731 } 732 /* 733 * XXX: Doesn't this mean that this quantum will 734 * XXX: get counted twice if calcru() is called 735 * XXX: from SIGINFO ? 736 */ 737 rux.rux_runtime += 738 (bt - pcpu_find(td->td_oncpu)->pc_switchtime); 739 } 740 } 741 mtx_unlock_spin(&sched_lock); 742 calcru1(p, &rux, up, sp); 743 p->p_rux.rux_uu = rux.rux_uu; 744 p->p_rux.rux_su = rux.rux_su; 745 p->p_rux.rux_iu = rux.rux_iu; 746 } 747 748 void 749 calccru(p, up, sp) 750 struct proc *p; 751 struct timeval *up; 752 struct timeval *sp; 753 { 754 755 PROC_LOCK_ASSERT(p, MA_OWNED); 756 calcru1(p, &p->p_crux, up, sp); 757 } 758 759 static void 760 calcru1(p, ruxp, up, sp) 761 struct proc *p; 762 struct rusage_ext *ruxp; 763 struct timeval *up; 764 struct timeval *sp; 765 { 766 /* {user, system, interrupt, total} {ticks, usec}; previous tu: */ 767 u_int64_t ut, uu, st, su, it, iu, tt, tu, ptu; 768 769 ut = ruxp->rux_uticks; 770 st = ruxp->rux_sticks; 771 it = ruxp->rux_iticks; 772 tt = ut + st + it; 773 if (tt == 0) { 774 st = 1; 775 tt = 1; 776 } 777 tu = (ruxp->rux_runtime * 1000000LL) / cpu_tickrate(); 778 ptu = ruxp->rux_uu + ruxp->rux_su + ruxp->rux_iu; 779 if (tu < ptu) { 780 printf( 781 "calcru: runtime went backwards from %ju usec to %ju usec for pid %d (%s)\n", 782 (uintmax_t)ptu, (uintmax_t)tu, p->p_pid, p->p_comm); 783 tu = ptu; 784 } 785 if ((int64_t)tu < 0) { 786 printf("calcru: negative runtime of %jd usec for pid %d (%s)\n", 787 (intmax_t)tu, p->p_pid, p->p_comm); 788 tu = ptu; 789 } 790 791 /* Subdivide tu. */ 792 uu = (tu * ut) / tt; 793 su = (tu * st) / tt; 794 iu = tu - uu - su; 795 796 /* Enforce monotonicity. */ 797 if (uu < ruxp->rux_uu || su < ruxp->rux_su || iu < ruxp->rux_iu) { 798 if (uu < ruxp->rux_uu) 799 uu = ruxp->rux_uu; 800 else if (uu + ruxp->rux_su + ruxp->rux_iu > tu) 801 uu = tu - ruxp->rux_su - ruxp->rux_iu; 802 if (st == 0) 803 su = ruxp->rux_su; 804 else { 805 su = ((tu - uu) * st) / (st + it); 806 if (su < ruxp->rux_su) 807 su = ruxp->rux_su; 808 else if (uu + su + ruxp->rux_iu > tu) 809 su = tu - uu - ruxp->rux_iu; 810 } 811 KASSERT(uu + su + ruxp->rux_iu <= tu, 812 ("calcru: monotonisation botch 1")); 813 iu = tu - uu - su; 814 KASSERT(iu >= ruxp->rux_iu, 815 ("calcru: monotonisation botch 2")); 816 } 817 ruxp->rux_uu = uu; 818 ruxp->rux_su = su; 819 ruxp->rux_iu = iu; 820 821 up->tv_sec = uu / 1000000; 822 up->tv_usec = uu % 1000000; 823 sp->tv_sec = su / 1000000; 824 sp->tv_usec = su % 1000000; 825 } 826 827 #ifndef _SYS_SYSPROTO_H_ 828 struct getrusage_args { 829 int who; 830 struct rusage *rusage; 831 }; 832 #endif 833 /* 834 * MPSAFE 835 */ 836 int 837 getrusage(td, uap) 838 register struct thread *td; 839 register struct getrusage_args *uap; 840 { 841 struct rusage ru; 842 int error; 843 844 error = kern_getrusage(td, uap->who, &ru); 845 if (error == 0) 846 error = copyout(&ru, uap->rusage, sizeof(struct rusage)); 847 return (error); 848 } 849 850 int 851 kern_getrusage(td, who, rup) 852 struct thread *td; 853 int who; 854 struct rusage *rup; 855 { 856 struct proc *p; 857 858 p = td->td_proc; 859 PROC_LOCK(p); 860 switch (who) { 861 862 case RUSAGE_SELF: 863 *rup = p->p_stats->p_ru; 864 calcru(p, &rup->ru_utime, &rup->ru_stime); 865 break; 866 867 case RUSAGE_CHILDREN: 868 *rup = p->p_stats->p_cru; 869 calccru(p, &rup->ru_utime, &rup->ru_stime); 870 break; 871 872 default: 873 PROC_UNLOCK(p); 874 return (EINVAL); 875 } 876 PROC_UNLOCK(p); 877 return (0); 878 } 879 880 void 881 ruadd(ru, rux, ru2, rux2) 882 struct rusage *ru; 883 struct rusage_ext *rux; 884 struct rusage *ru2; 885 struct rusage_ext *rux2; 886 { 887 register long *ip, *ip2; 888 register int i; 889 890 rux->rux_runtime += rux2->rux_runtime; 891 rux->rux_uticks += rux2->rux_uticks; 892 rux->rux_sticks += rux2->rux_sticks; 893 rux->rux_iticks += rux2->rux_iticks; 894 rux->rux_uu += rux2->rux_uu; 895 rux->rux_su += rux2->rux_su; 896 rux->rux_iu += rux2->rux_iu; 897 if (ru->ru_maxrss < ru2->ru_maxrss) 898 ru->ru_maxrss = ru2->ru_maxrss; 899 ip = &ru->ru_first; 900 ip2 = &ru2->ru_first; 901 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--) 902 *ip++ += *ip2++; 903 } 904 905 /* 906 * Allocate a new resource limits structure and initialize its 907 * reference count and mutex pointer. 908 */ 909 struct plimit * 910 lim_alloc() 911 { 912 struct plimit *limp; 913 914 limp = malloc(sizeof(struct plimit), M_PLIMIT, M_WAITOK); 915 refcount_init(&limp->pl_refcnt, 1); 916 return (limp); 917 } 918 919 struct plimit * 920 lim_hold(limp) 921 struct plimit *limp; 922 { 923 924 refcount_acquire(&limp->pl_refcnt); 925 return (limp); 926 } 927 928 void 929 lim_free(limp) 930 struct plimit *limp; 931 { 932 933 KASSERT(limp->pl_refcnt > 0, ("plimit refcnt underflow")); 934 if (refcount_release(&limp->pl_refcnt)) 935 free((void *)limp, M_PLIMIT); 936 } 937 938 /* 939 * Make a copy of the plimit structure. 940 * We share these structures copy-on-write after fork. 941 */ 942 void 943 lim_copy(dst, src) 944 struct plimit *dst, *src; 945 { 946 947 KASSERT(dst->pl_refcnt == 1, ("lim_copy to shared limit")); 948 bcopy(src->pl_rlimit, dst->pl_rlimit, sizeof(src->pl_rlimit)); 949 } 950 951 /* 952 * Return the hard limit for a particular system resource. The 953 * which parameter specifies the index into the rlimit array. 954 */ 955 rlim_t 956 lim_max(struct proc *p, int which) 957 { 958 struct rlimit rl; 959 960 lim_rlimit(p, which, &rl); 961 return (rl.rlim_max); 962 } 963 964 /* 965 * Return the current (soft) limit for a particular system resource. 966 * The which parameter which specifies the index into the rlimit array 967 */ 968 rlim_t 969 lim_cur(struct proc *p, int which) 970 { 971 struct rlimit rl; 972 973 lim_rlimit(p, which, &rl); 974 return (rl.rlim_cur); 975 } 976 977 /* 978 * Return a copy of the entire rlimit structure for the system limit 979 * specified by 'which' in the rlimit structure pointed to by 'rlp'. 980 */ 981 void 982 lim_rlimit(struct proc *p, int which, struct rlimit *rlp) 983 { 984 985 PROC_LOCK_ASSERT(p, MA_OWNED); 986 KASSERT(which >= 0 && which < RLIM_NLIMITS, 987 ("request for invalid resource limit")); 988 *rlp = p->p_limit->pl_rlimit[which]; 989 } 990 991 /* 992 * Find the uidinfo structure for a uid. This structure is used to 993 * track the total resource consumption (process count, socket buffer 994 * size, etc.) for the uid and impose limits. 995 */ 996 void 997 uihashinit() 998 { 999 1000 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash); 1001 mtx_init(&uihashtbl_mtx, "uidinfo hash", NULL, MTX_DEF); 1002 } 1003 1004 /* 1005 * Look up a uidinfo struct for the parameter uid. 1006 * uihashtbl_mtx must be locked. 1007 */ 1008 static struct uidinfo * 1009 uilookup(uid) 1010 uid_t uid; 1011 { 1012 struct uihashhead *uipp; 1013 struct uidinfo *uip; 1014 1015 mtx_assert(&uihashtbl_mtx, MA_OWNED); 1016 uipp = UIHASH(uid); 1017 LIST_FOREACH(uip, uipp, ui_hash) 1018 if (uip->ui_uid == uid) 1019 break; 1020 1021 return (uip); 1022 } 1023 1024 /* 1025 * Find or allocate a struct uidinfo for a particular uid. 1026 * Increase refcount on uidinfo struct returned. 1027 * uifree() should be called on a struct uidinfo when released. 1028 */ 1029 struct uidinfo * 1030 uifind(uid) 1031 uid_t uid; 1032 { 1033 struct uidinfo *old_uip, *uip; 1034 1035 mtx_lock(&uihashtbl_mtx); 1036 uip = uilookup(uid); 1037 if (uip == NULL) { 1038 mtx_unlock(&uihashtbl_mtx); 1039 uip = malloc(sizeof(*uip), M_UIDINFO, M_WAITOK | M_ZERO); 1040 mtx_lock(&uihashtbl_mtx); 1041 /* 1042 * There's a chance someone created our uidinfo while we 1043 * were in malloc and not holding the lock, so we have to 1044 * make sure we don't insert a duplicate uidinfo. 1045 */ 1046 if ((old_uip = uilookup(uid)) != NULL) { 1047 /* Someone else beat us to it. */ 1048 free(uip, M_UIDINFO); 1049 uip = old_uip; 1050 } else { 1051 uip->ui_mtxp = mtx_pool_alloc(mtxpool_sleep); 1052 uip->ui_uid = uid; 1053 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash); 1054 } 1055 } 1056 uihold(uip); 1057 mtx_unlock(&uihashtbl_mtx); 1058 return (uip); 1059 } 1060 1061 /* 1062 * Place another refcount on a uidinfo struct. 1063 */ 1064 void 1065 uihold(uip) 1066 struct uidinfo *uip; 1067 { 1068 1069 UIDINFO_LOCK(uip); 1070 uip->ui_ref++; 1071 UIDINFO_UNLOCK(uip); 1072 } 1073 1074 /*- 1075 * Since uidinfo structs have a long lifetime, we use an 1076 * opportunistic refcounting scheme to avoid locking the lookup hash 1077 * for each release. 1078 * 1079 * If the refcount hits 0, we need to free the structure, 1080 * which means we need to lock the hash. 1081 * Optimal case: 1082 * After locking the struct and lowering the refcount, if we find 1083 * that we don't need to free, simply unlock and return. 1084 * Suboptimal case: 1085 * If refcount lowering results in need to free, bump the count 1086 * back up, loose the lock and aquire the locks in the proper 1087 * order to try again. 1088 */ 1089 void 1090 uifree(uip) 1091 struct uidinfo *uip; 1092 { 1093 1094 /* Prepare for optimal case. */ 1095 UIDINFO_LOCK(uip); 1096 1097 if (--uip->ui_ref != 0) { 1098 UIDINFO_UNLOCK(uip); 1099 return; 1100 } 1101 1102 /* Prepare for suboptimal case. */ 1103 uip->ui_ref++; 1104 UIDINFO_UNLOCK(uip); 1105 mtx_lock(&uihashtbl_mtx); 1106 UIDINFO_LOCK(uip); 1107 1108 /* 1109 * We must subtract one from the count again because we backed out 1110 * our initial subtraction before dropping the lock. 1111 * Since another thread may have added a reference after we dropped the 1112 * initial lock we have to test for zero again. 1113 */ 1114 if (--uip->ui_ref == 0) { 1115 LIST_REMOVE(uip, ui_hash); 1116 mtx_unlock(&uihashtbl_mtx); 1117 if (uip->ui_sbsize != 0) 1118 printf("freeing uidinfo: uid = %d, sbsize = %jd\n", 1119 uip->ui_uid, (intmax_t)uip->ui_sbsize); 1120 if (uip->ui_proccnt != 0) 1121 printf("freeing uidinfo: uid = %d, proccnt = %ld\n", 1122 uip->ui_uid, uip->ui_proccnt); 1123 UIDINFO_UNLOCK(uip); 1124 FREE(uip, M_UIDINFO); 1125 return; 1126 } 1127 1128 mtx_unlock(&uihashtbl_mtx); 1129 UIDINFO_UNLOCK(uip); 1130 } 1131 1132 /* 1133 * Change the count associated with number of processes 1134 * a given user is using. When 'max' is 0, don't enforce a limit 1135 */ 1136 int 1137 chgproccnt(uip, diff, max) 1138 struct uidinfo *uip; 1139 int diff; 1140 int max; 1141 { 1142 1143 UIDINFO_LOCK(uip); 1144 /* Don't allow them to exceed max, but allow subtraction. */ 1145 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0) { 1146 UIDINFO_UNLOCK(uip); 1147 return (0); 1148 } 1149 uip->ui_proccnt += diff; 1150 if (uip->ui_proccnt < 0) 1151 printf("negative proccnt for uid = %d\n", uip->ui_uid); 1152 UIDINFO_UNLOCK(uip); 1153 return (1); 1154 } 1155 1156 /* 1157 * Change the total socket buffer size a user has used. 1158 */ 1159 int 1160 chgsbsize(uip, hiwat, to, max) 1161 struct uidinfo *uip; 1162 u_int *hiwat; 1163 u_int to; 1164 rlim_t max; 1165 { 1166 rlim_t new; 1167 1168 UIDINFO_LOCK(uip); 1169 new = uip->ui_sbsize + to - *hiwat; 1170 /* Don't allow them to exceed max, but allow subtraction. */ 1171 if (to > *hiwat && new > max) { 1172 UIDINFO_UNLOCK(uip); 1173 return (0); 1174 } 1175 uip->ui_sbsize = new; 1176 UIDINFO_UNLOCK(uip); 1177 *hiwat = to; 1178 if (new < 0) 1179 printf("negative sbsize for uid = %d\n", uip->ui_uid); 1180 return (1); 1181 } 1182