The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_resource.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    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: releng/6.3/sys/kern/kern_resource.c 173886 2007-11-24 19:45:58Z cvs2svn $");
   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/imgact.h>
   47 #include <sys/kernel.h>
   48 #include <sys/lock.h>
   49 #include <sys/malloc.h>
   50 #include <sys/mutex.h>
   51 #include <sys/proc.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                         /* Do not bother to check PRS_NEW processes */
  233                         if (p->p_state == PRS_NEW)
  234                                 continue;
  235                         PROC_LOCK(p);
  236                         if (p->p_ucred->cr_uid == uap->who &&
  237                             !p_cansee(td, p)) {
  238                                 error = donice(td, p, uap->prio);
  239                                 found++;
  240                         }
  241                         PROC_UNLOCK(p);
  242                 }
  243                 sx_sunlock(&allproc_lock);
  244                 break;
  245 
  246         default:
  247                 error = EINVAL;
  248                 break;
  249         }
  250         if (found == 0 && error == 0)
  251                 error = ESRCH;
  252         return (error);
  253 }
  254 
  255 /*
  256  * Set "nice" for a (whole) process.
  257  */
  258 static int
  259 donice(struct thread *td, struct proc *p, int n)
  260 {
  261         int error;
  262 
  263         PROC_LOCK_ASSERT(p, MA_OWNED);
  264         if ((error = p_cansched(td, p)))
  265                 return (error);
  266         if (n > PRIO_MAX)
  267                 n = PRIO_MAX;
  268         if (n < PRIO_MIN)
  269                 n = PRIO_MIN;
  270         if (n < p->p_nice && suser(td) != 0)
  271                 return (EACCES);
  272         mtx_lock_spin(&sched_lock);
  273         sched_nice(p, n);
  274         mtx_unlock_spin(&sched_lock);
  275         return (0);
  276 }
  277 
  278 /*
  279  * Set realtime priority.
  280  *
  281  * MPSAFE
  282  */
  283 #ifndef _SYS_SYSPROTO_H_
  284 struct rtprio_args {
  285         int             function;
  286         pid_t           pid;
  287         struct rtprio   *rtp;
  288 };
  289 #endif
  290 
  291 int
  292 rtprio(td, uap)
  293         struct thread *td;              /* curthread */
  294         register struct rtprio_args *uap;
  295 {
  296         struct proc *curp;
  297         struct proc *p;
  298         struct ksegrp *kg;
  299         struct rtprio rtp;
  300         int cierror, error;
  301 
  302         /* Perform copyin before acquiring locks if needed. */
  303         if (uap->function == RTP_SET)
  304                 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
  305         else
  306                 cierror = 0;
  307 
  308         curp = td->td_proc;
  309         if (uap->pid == 0) {
  310                 p = curp;
  311                 PROC_LOCK(p);
  312         } else {
  313                 p = pfind(uap->pid);
  314                 if (p == NULL)
  315                         return (ESRCH);
  316         }
  317 
  318         switch (uap->function) {
  319         case RTP_LOOKUP:
  320                 if ((error = p_cansee(td, p)))
  321                         break;
  322                 mtx_lock_spin(&sched_lock);
  323                 /*
  324                  * Return OUR priority if no pid specified,
  325                  * or if one is, report the highest priority
  326                  * in the process.  There isn't much more you can do as 
  327                  * there is only room to return a single priority.
  328                  * XXXKSE: maybe need a new interface to report 
  329                  * priorities of multiple system scope threads.
  330                  * Note: specifying our own pid is not the same
  331                  * as leaving it zero.
  332                  */
  333                 if (uap->pid == 0) {
  334                         pri_to_rtp(td->td_ksegrp, &rtp);
  335                 } else {
  336                         struct rtprio rtp2;
  337 
  338                         rtp.type = RTP_PRIO_IDLE;
  339                         rtp.prio = RTP_PRIO_MAX;
  340                         FOREACH_KSEGRP_IN_PROC(p, kg) {
  341                                 pri_to_rtp(kg, &rtp2);
  342                                 if (rtp2.type <  rtp.type ||
  343                                     (rtp2.type == rtp.type &&
  344                                     rtp2.prio < rtp.prio)) {
  345                                         rtp.type = rtp2.type;
  346                                         rtp.prio = rtp2.prio;
  347                                 }
  348                         }
  349                 }
  350                 mtx_unlock_spin(&sched_lock);
  351                 PROC_UNLOCK(p);
  352                 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
  353         case RTP_SET:
  354                 if ((error = p_cansched(td, p)) || (error = cierror))
  355                         break;
  356 
  357                 /* Disallow setting rtprio in most cases if not superuser. */
  358                 if (suser(td) != 0) {
  359                         /* can't set someone else's */
  360                         if (uap->pid) {
  361                                 error = EPERM;
  362                                 break;
  363                         }
  364                         /* can't set realtime priority */
  365 /*
  366  * Realtime priority has to be restricted for reasons which should be
  367  * obvious.  However, for idle priority, there is a potential for
  368  * system deadlock if an idleprio process gains a lock on a resource
  369  * that other processes need (and the idleprio process can't run
  370  * due to a CPU-bound normal process).  Fix me!  XXX
  371  */
  372 #if 0
  373                         if (RTP_PRIO_IS_REALTIME(rtp.type)) {
  374 #else
  375                         if (rtp.type != RTP_PRIO_NORMAL) {
  376 #endif
  377                                 error = EPERM;
  378                                 break;
  379                         }
  380                 }
  381 
  382                 /*
  383                  * If we are setting our own priority, set just our
  384                  * KSEGRP but if we are doing another process,
  385                  * do all the groups on that process. If we
  386                  * specify our own pid we do the latter.
  387                  */
  388                 mtx_lock_spin(&sched_lock);
  389                 if (uap->pid == 0) {
  390                         error = rtp_to_pri(&rtp, td->td_ksegrp);
  391                 } else {
  392                         FOREACH_KSEGRP_IN_PROC(p, kg) {
  393                                 if ((error = rtp_to_pri(&rtp, kg)) != 0) {
  394                                         break;
  395                                 }
  396                         }
  397                 }
  398                 mtx_unlock_spin(&sched_lock);
  399                 break;
  400         default:
  401                 error = EINVAL;
  402                 break;
  403         }
  404         PROC_UNLOCK(p);
  405         return (error);
  406 }
  407 
  408 int
  409 rtp_to_pri(struct rtprio *rtp, struct ksegrp *kg)
  410 {
  411 
  412         mtx_assert(&sched_lock, MA_OWNED);
  413         if (rtp->prio > RTP_PRIO_MAX)
  414                 return (EINVAL);
  415         switch (RTP_PRIO_BASE(rtp->type)) {
  416         case RTP_PRIO_REALTIME:
  417                 kg->kg_user_pri = PRI_MIN_REALTIME + rtp->prio;
  418                 break;
  419         case RTP_PRIO_NORMAL:
  420                 kg->kg_user_pri = PRI_MIN_TIMESHARE + rtp->prio;
  421                 break;
  422         case RTP_PRIO_IDLE:
  423                 kg->kg_user_pri = PRI_MIN_IDLE + rtp->prio;
  424                 break;
  425         default:
  426                 return (EINVAL);
  427         }
  428         sched_class(kg, rtp->type);
  429         if (curthread->td_ksegrp == kg) {
  430                 sched_prio(curthread, kg->kg_user_pri); /* XXX dubious */
  431         }
  432         return (0);
  433 }
  434 
  435 void
  436 pri_to_rtp(struct ksegrp *kg, struct rtprio *rtp)
  437 {
  438 
  439         mtx_assert(&sched_lock, MA_OWNED);
  440         switch (PRI_BASE(kg->kg_pri_class)) {
  441         case PRI_REALTIME:
  442                 rtp->prio = kg->kg_user_pri - PRI_MIN_REALTIME;
  443                 break;
  444         case PRI_TIMESHARE:
  445                 rtp->prio = kg->kg_user_pri - PRI_MIN_TIMESHARE;
  446                 break;
  447         case PRI_IDLE:
  448                 rtp->prio = kg->kg_user_pri - PRI_MIN_IDLE;
  449                 break;
  450         default:
  451                 break;
  452         }
  453         rtp->type = kg->kg_pri_class;
  454 }
  455 
  456 #if defined(COMPAT_43)
  457 #ifndef _SYS_SYSPROTO_H_
  458 struct osetrlimit_args {
  459         u_int   which;
  460         struct  orlimit *rlp;
  461 };
  462 #endif
  463 /*
  464  * MPSAFE
  465  */
  466 int
  467 osetrlimit(td, uap)
  468         struct thread *td;
  469         register struct osetrlimit_args *uap;
  470 {
  471         struct orlimit olim;
  472         struct rlimit lim;
  473         int error;
  474 
  475         if ((error = copyin(uap->rlp, &olim, sizeof(struct orlimit))))
  476                 return (error);
  477         lim.rlim_cur = olim.rlim_cur;
  478         lim.rlim_max = olim.rlim_max;
  479         error = kern_setrlimit(td, uap->which, &lim);
  480         return (error);
  481 }
  482 
  483 #ifndef _SYS_SYSPROTO_H_
  484 struct ogetrlimit_args {
  485         u_int   which;
  486         struct  orlimit *rlp;
  487 };
  488 #endif
  489 /*
  490  * MPSAFE
  491  */
  492 int
  493 ogetrlimit(td, uap)
  494         struct thread *td;
  495         register struct ogetrlimit_args *uap;
  496 {
  497         struct orlimit olim;
  498         struct rlimit rl;
  499         struct proc *p;
  500         int error;
  501 
  502         if (uap->which >= RLIM_NLIMITS)
  503                 return (EINVAL);
  504         p = td->td_proc;
  505         PROC_LOCK(p);
  506         lim_rlimit(p, uap->which, &rl);
  507         PROC_UNLOCK(p);
  508 
  509         /*
  510          * XXX would be more correct to convert only RLIM_INFINITY to the
  511          * old RLIM_INFINITY and fail with EOVERFLOW for other larger
  512          * values.  Most 64->32 and 32->16 conversions, including not
  513          * unimportant ones of uids are even more broken than what we
  514          * do here (they blindly truncate).  We don't do this correctly
  515          * here since we have little experience with EOVERFLOW yet.
  516          * Elsewhere, getuid() can't fail...
  517          */
  518         olim.rlim_cur = rl.rlim_cur > 0x7fffffff ? 0x7fffffff : rl.rlim_cur;
  519         olim.rlim_max = rl.rlim_max > 0x7fffffff ? 0x7fffffff : rl.rlim_max;
  520         error = copyout(&olim, uap->rlp, sizeof(olim));
  521         return (error);
  522 }
  523 #endif /* COMPAT_43 */
  524 
  525 #ifndef _SYS_SYSPROTO_H_
  526 struct __setrlimit_args {
  527         u_int   which;
  528         struct  rlimit *rlp;
  529 };
  530 #endif
  531 /*
  532  * MPSAFE
  533  */
  534 int
  535 setrlimit(td, uap)
  536         struct thread *td;
  537         register struct __setrlimit_args *uap;
  538 {
  539         struct rlimit alim;
  540         int error;
  541 
  542         if ((error = copyin(uap->rlp, &alim, sizeof(struct rlimit))))
  543                 return (error);
  544         error = kern_setrlimit(td, uap->which, &alim);
  545         return (error);
  546 }
  547 
  548 int
  549 kern_setrlimit(td, which, limp)
  550         struct thread *td;
  551         u_int which;
  552         struct rlimit *limp;
  553 {
  554         struct plimit *newlim, *oldlim;
  555         struct proc *p;
  556         register struct rlimit *alimp;
  557         rlim_t oldssiz;
  558         int error;
  559 
  560         if (which >= RLIM_NLIMITS)
  561                 return (EINVAL);
  562 
  563         /*
  564          * Preserve historical bugs by treating negative limits as unsigned.
  565          */
  566         if (limp->rlim_cur < 0)
  567                 limp->rlim_cur = RLIM_INFINITY;
  568         if (limp->rlim_max < 0)
  569                 limp->rlim_max = RLIM_INFINITY;
  570 
  571         oldssiz = 0;
  572         p = td->td_proc;
  573         newlim = lim_alloc();
  574         PROC_LOCK(p);
  575         oldlim = p->p_limit;
  576         alimp = &oldlim->pl_rlimit[which];
  577         if (limp->rlim_cur > alimp->rlim_max ||
  578             limp->rlim_max > alimp->rlim_max)
  579                 if ((error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL))) {
  580                         PROC_UNLOCK(p);
  581                         lim_free(newlim);
  582                         return (error);
  583                 }
  584         if (limp->rlim_cur > limp->rlim_max)
  585                 limp->rlim_cur = limp->rlim_max;
  586         lim_copy(newlim, oldlim);
  587         alimp = &newlim->pl_rlimit[which];
  588 
  589         switch (which) {
  590 
  591         case RLIMIT_CPU:
  592                 mtx_lock_spin(&sched_lock);
  593                 p->p_cpulimit = limp->rlim_cur;
  594                 mtx_unlock_spin(&sched_lock);
  595                 break;
  596         case RLIMIT_DATA:
  597                 if (limp->rlim_cur > maxdsiz)
  598                         limp->rlim_cur = maxdsiz;
  599                 if (limp->rlim_max > maxdsiz)
  600                         limp->rlim_max = maxdsiz;
  601                 break;
  602 
  603         case RLIMIT_STACK:
  604                 if (limp->rlim_cur > maxssiz)
  605                         limp->rlim_cur = maxssiz;
  606                 if (limp->rlim_max > maxssiz)
  607                         limp->rlim_max = maxssiz;
  608                 oldssiz = alimp->rlim_cur;
  609                 break;
  610 
  611         case RLIMIT_NOFILE:
  612                 if (limp->rlim_cur > maxfilesperproc)
  613                         limp->rlim_cur = maxfilesperproc;
  614                 if (limp->rlim_max > maxfilesperproc)
  615                         limp->rlim_max = maxfilesperproc;
  616                 break;
  617 
  618         case RLIMIT_NPROC:
  619                 if (limp->rlim_cur > maxprocperuid)
  620                         limp->rlim_cur = maxprocperuid;
  621                 if (limp->rlim_max > maxprocperuid)
  622                         limp->rlim_max = maxprocperuid;
  623                 if (limp->rlim_cur < 1)
  624                         limp->rlim_cur = 1;
  625                 if (limp->rlim_max < 1)
  626                         limp->rlim_max = 1;
  627                 break;
  628         }
  629         if (td->td_proc->p_sysent->sv_fixlimit != NULL)
  630                 td->td_proc->p_sysent->sv_fixlimit(limp, which);
  631         *alimp = *limp;
  632         p->p_limit = newlim;
  633         PROC_UNLOCK(p);
  634         lim_free(oldlim);
  635 
  636         if (which == RLIMIT_STACK) {
  637                 /*
  638                  * Stack is allocated to the max at exec time with only
  639                  * "rlim_cur" bytes accessible.  If stack limit is going
  640                  * up make more accessible, if going down make inaccessible.
  641                  */
  642                 if (limp->rlim_cur != oldssiz) {
  643                         vm_offset_t addr;
  644                         vm_size_t size;
  645                         vm_prot_t prot;
  646 
  647                         if (limp->rlim_cur > oldssiz) {
  648                                 prot = p->p_sysent->sv_stackprot;
  649                                 size = limp->rlim_cur - oldssiz;
  650                                 addr = p->p_sysent->sv_usrstack -
  651                                     limp->rlim_cur;
  652                         } else {
  653                                 prot = VM_PROT_NONE;
  654                                 size = oldssiz - limp->rlim_cur;
  655                                 addr = p->p_sysent->sv_usrstack - oldssiz;
  656                         }
  657                         addr = trunc_page(addr);
  658                         size = round_page(size);
  659                         (void)vm_map_protect(&p->p_vmspace->vm_map,
  660                             addr, addr + size, prot, FALSE);
  661                 }
  662         }
  663 
  664         return (0);
  665 }
  666 
  667 #ifndef _SYS_SYSPROTO_H_
  668 struct __getrlimit_args {
  669         u_int   which;
  670         struct  rlimit *rlp;
  671 };
  672 #endif
  673 /*
  674  * MPSAFE
  675  */
  676 /* ARGSUSED */
  677 int
  678 getrlimit(td, uap)
  679         struct thread *td;
  680         register struct __getrlimit_args *uap;
  681 {
  682         struct rlimit rlim;
  683         struct proc *p;
  684         int error;
  685 
  686         if (uap->which >= RLIM_NLIMITS)
  687                 return (EINVAL);
  688         p = td->td_proc;
  689         PROC_LOCK(p);
  690         lim_rlimit(p, uap->which, &rlim);
  691         PROC_UNLOCK(p);
  692         error = copyout(&rlim, uap->rlp, sizeof(struct rlimit));
  693         return (error);
  694 }
  695 
  696 /*
  697  * Transform the running time and tick information in proc p into user,
  698  * system, and interrupt time usage.
  699  */
  700 void
  701 calcru(p, up, sp)
  702         struct proc *p;
  703         struct timeval *up;
  704         struct timeval *sp;
  705 {
  706         struct bintime bt;
  707         struct rusage_ext rux;
  708         struct thread *td;
  709         int bt_valid;
  710 
  711         PROC_LOCK_ASSERT(p, MA_OWNED);
  712         mtx_assert(&sched_lock, MA_NOTOWNED);
  713         bt_valid = 0;
  714         mtx_lock_spin(&sched_lock);
  715         rux = p->p_rux;
  716         FOREACH_THREAD_IN_PROC(p, td) {
  717                 if (TD_IS_RUNNING(td)) {
  718                         /*
  719                          * Adjust for the current time slice.  This is
  720                          * actually fairly important since the error here is
  721                          * on the order of a time quantum which is much
  722                          * greater than the precision of binuptime().
  723                          */
  724                         KASSERT(td->td_oncpu != NOCPU,
  725                             ("%s: running thread has no CPU", __func__));
  726                         if (!bt_valid) {
  727                                 binuptime(&bt);
  728                                 bt_valid = 1;
  729                         }
  730                         bintime_add(&rux.rux_runtime, &bt);
  731                         bintime_sub(&rux.rux_runtime,
  732                             &pcpu_find(td->td_oncpu)->pc_switchtime);
  733                 }
  734         }
  735         mtx_unlock_spin(&sched_lock);
  736         calcru1(p, &rux, up, sp);
  737         p->p_rux.rux_uu = rux.rux_uu;
  738         p->p_rux.rux_su = rux.rux_su;
  739         p->p_rux.rux_iu = rux.rux_iu;
  740 }
  741 
  742 void
  743 calccru(p, up, sp)
  744         struct proc *p;
  745         struct timeval *up;
  746         struct timeval *sp;
  747 {
  748 
  749         PROC_LOCK_ASSERT(p, MA_OWNED);
  750         calcru1(p, &p->p_crux, up, sp);
  751 }
  752 
  753 static void
  754 calcru1(p, ruxp, up, sp)
  755         struct proc *p;
  756         struct rusage_ext *ruxp;
  757         struct timeval *up;
  758         struct timeval *sp;
  759 {
  760         struct timeval tv;
  761         /* {user, system, interrupt, total} {ticks, usec}; previous tu: */
  762         u_int64_t ut, uu, st, su, it, iu, tt, tu, ptu;
  763 
  764         ut = ruxp->rux_uticks;
  765         st = ruxp->rux_sticks;
  766         it = ruxp->rux_iticks;
  767         tt = ut + st + it;
  768         if (tt == 0) {
  769                 st = 1;
  770                 tt = 1;
  771         }
  772         bintime2timeval(&ruxp->rux_runtime, &tv);
  773         tu = (u_int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
  774         ptu = ruxp->rux_uu + ruxp->rux_su + ruxp->rux_iu;
  775         if (tu < ptu) {
  776                 printf(
  777 "calcru: runtime went backwards from %ju usec to %ju usec for pid %d (%s)\n",
  778                     (uintmax_t)ptu, (uintmax_t)tu, p->p_pid, p->p_comm);
  779                 tu = ptu;
  780         }
  781         if ((int64_t)tu < 0) {
  782                 printf("calcru: negative runtime of %jd usec for pid %d (%s)\n",
  783                     (intmax_t)tu, p->p_pid, p->p_comm);
  784                 tu = ptu;
  785         }
  786 
  787         /* Subdivide tu. */
  788         uu = (tu * ut) / tt;
  789         su = (tu * st) / tt;
  790         iu = tu - uu - su;
  791 
  792         /* Enforce monotonicity. */
  793         if (uu < ruxp->rux_uu || su < ruxp->rux_su || iu < ruxp->rux_iu) {
  794                 if (uu < ruxp->rux_uu)
  795                         uu = ruxp->rux_uu;
  796                 else if (uu + ruxp->rux_su + ruxp->rux_iu > tu)
  797                         uu = tu - ruxp->rux_su - ruxp->rux_iu;
  798                 if (st == 0)
  799                         su = ruxp->rux_su;
  800                 else {
  801                         su = ((tu - uu) * st) / (st + it);
  802                         if (su < ruxp->rux_su)
  803                                 su = ruxp->rux_su;
  804                         else if (uu + su + ruxp->rux_iu > tu)
  805                                 su = tu - uu - ruxp->rux_iu;
  806                 }
  807                 KASSERT(uu + su + ruxp->rux_iu <= tu,
  808                     ("calcru: monotonisation botch 1"));
  809                 iu = tu - uu - su;
  810                 KASSERT(iu >= ruxp->rux_iu,
  811                     ("calcru: monotonisation botch 2"));
  812         }
  813         ruxp->rux_uu = uu;
  814         ruxp->rux_su = su;
  815         ruxp->rux_iu = iu;
  816 
  817         up->tv_sec = uu / 1000000;
  818         up->tv_usec = uu % 1000000;
  819         sp->tv_sec = su / 1000000;
  820         sp->tv_usec = su % 1000000;
  821 }
  822 
  823 #ifndef _SYS_SYSPROTO_H_
  824 struct getrusage_args {
  825         int     who;
  826         struct  rusage *rusage;
  827 };
  828 #endif
  829 /*
  830  * MPSAFE
  831  */
  832 int
  833 getrusage(td, uap)
  834         register struct thread *td;
  835         register struct getrusage_args *uap;
  836 {
  837         struct rusage ru;
  838         int error;
  839 
  840         error = kern_getrusage(td, uap->who, &ru);
  841         if (error == 0)
  842                 error = copyout(&ru, uap->rusage, sizeof(struct rusage));
  843         return (error);
  844 }
  845 
  846 int
  847 kern_getrusage(td, who, rup)
  848         struct thread *td;
  849         int who;
  850         struct rusage *rup;
  851 {
  852         struct proc *p;
  853 
  854         p = td->td_proc;
  855         PROC_LOCK(p);
  856         switch (who) {
  857 
  858         case RUSAGE_SELF:
  859                 *rup = p->p_stats->p_ru;
  860                 calcru(p, &rup->ru_utime, &rup->ru_stime);
  861                 break;
  862 
  863         case RUSAGE_CHILDREN:
  864                 *rup = p->p_stats->p_cru;
  865                 calccru(p, &rup->ru_utime, &rup->ru_stime);
  866                 break;
  867 
  868         default:
  869                 PROC_UNLOCK(p);
  870                 return (EINVAL);
  871         }
  872         PROC_UNLOCK(p);
  873         return (0);
  874 }
  875 
  876 void
  877 ruadd(ru, rux, ru2, rux2)
  878         struct rusage *ru;
  879         struct rusage_ext *rux;
  880         struct rusage *ru2;
  881         struct rusage_ext *rux2;
  882 {
  883         register long *ip, *ip2;
  884         register int i;
  885 
  886         bintime_add(&rux->rux_runtime, &rux2->rux_runtime);
  887         rux->rux_uticks += rux2->rux_uticks;
  888         rux->rux_sticks += rux2->rux_sticks;
  889         rux->rux_iticks += rux2->rux_iticks;
  890         rux->rux_uu += rux2->rux_uu;
  891         rux->rux_su += rux2->rux_su;
  892         rux->rux_iu += rux2->rux_iu;
  893         if (ru->ru_maxrss < ru2->ru_maxrss)
  894                 ru->ru_maxrss = ru2->ru_maxrss;
  895         ip = &ru->ru_first;
  896         ip2 = &ru2->ru_first;
  897         for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
  898                 *ip++ += *ip2++;
  899 }
  900 
  901 /*
  902  * Allocate a new resource limits structure and initialize its
  903  * reference count and mutex pointer.
  904  */
  905 struct plimit *
  906 lim_alloc()
  907 {
  908         struct plimit *limp;
  909 
  910         limp = malloc(sizeof(struct plimit), M_PLIMIT, M_WAITOK);
  911         limp->pl_refcnt = 1;
  912         limp->pl_mtx = mtx_pool_alloc(mtxpool_sleep);
  913         return (limp);
  914 }
  915 
  916 struct plimit *
  917 lim_hold(limp)
  918         struct plimit *limp;
  919 {
  920 
  921         LIM_LOCK(limp);
  922         limp->pl_refcnt++;
  923         LIM_UNLOCK(limp);
  924         return (limp);
  925 }
  926 
  927 void
  928 lim_free(limp)
  929         struct plimit *limp;
  930 {
  931 
  932         LIM_LOCK(limp);
  933         KASSERT(limp->pl_refcnt > 0, ("plimit refcnt underflow"));
  934         if (--limp->pl_refcnt == 0) {
  935                 LIM_UNLOCK(limp);
  936                 free((void *)limp, M_PLIMIT);
  937                 return;
  938         }
  939         LIM_UNLOCK(limp);
  940 }
  941 
  942 /*
  943  * Make a copy of the plimit structure.
  944  * We share these structures copy-on-write after fork.
  945  */
  946 void
  947 lim_copy(dst, src)
  948         struct plimit *dst, *src;
  949 {
  950 
  951         KASSERT(dst->pl_refcnt == 1, ("lim_copy to shared limit"));
  952         bcopy(src->pl_rlimit, dst->pl_rlimit, sizeof(src->pl_rlimit));
  953 }
  954 
  955 /*
  956  * Return the hard limit for a particular system resource.  The
  957  * which parameter specifies the index into the rlimit array.
  958  */
  959 rlim_t
  960 lim_max(struct proc *p, int which)
  961 {
  962         struct rlimit rl;
  963 
  964         lim_rlimit(p, which, &rl);
  965         return (rl.rlim_max);
  966 }
  967 
  968 /*
  969  * Return the current (soft) limit for a particular system resource.
  970  * The which parameter which specifies the index into the rlimit array
  971  */
  972 rlim_t
  973 lim_cur(struct proc *p, int which)
  974 {
  975         struct rlimit rl;
  976 
  977         lim_rlimit(p, which, &rl);
  978         return (rl.rlim_cur);
  979 }
  980 
  981 /*
  982  * Return a copy of the entire rlimit structure for the system limit
  983  * specified by 'which' in the rlimit structure pointed to by 'rlp'.
  984  */
  985 void
  986 lim_rlimit(struct proc *p, int which, struct rlimit *rlp)
  987 {
  988 
  989         PROC_LOCK_ASSERT(p, MA_OWNED);
  990         KASSERT(which >= 0 && which < RLIM_NLIMITS,
  991             ("request for invalid resource limit"));
  992         *rlp = p->p_limit->pl_rlimit[which];
  993         if (p->p_sysent->sv_fixlimit != NULL)
  994                 p->p_sysent->sv_fixlimit(rlp, which);
  995 }
  996 
  997 /*
  998  * Find the uidinfo structure for a uid.  This structure is used to
  999  * track the total resource consumption (process count, socket buffer
 1000  * size, etc.) for the uid and impose limits.
 1001  */
 1002 void
 1003 uihashinit()
 1004 {
 1005 
 1006         uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
 1007         mtx_init(&uihashtbl_mtx, "uidinfo hash", NULL, MTX_DEF);
 1008 }
 1009 
 1010 /*
 1011  * Look up a uidinfo struct for the parameter uid.
 1012  * uihashtbl_mtx must be locked.
 1013  */
 1014 static struct uidinfo *
 1015 uilookup(uid)
 1016         uid_t uid;
 1017 {
 1018         struct uihashhead *uipp;
 1019         struct uidinfo *uip;
 1020 
 1021         mtx_assert(&uihashtbl_mtx, MA_OWNED);
 1022         uipp = UIHASH(uid);
 1023         LIST_FOREACH(uip, uipp, ui_hash)
 1024                 if (uip->ui_uid == uid)
 1025                         break;
 1026 
 1027         return (uip);
 1028 }
 1029 
 1030 /*
 1031  * Find or allocate a struct uidinfo for a particular uid.
 1032  * Increase refcount on uidinfo struct returned.
 1033  * uifree() should be called on a struct uidinfo when released.
 1034  */
 1035 struct uidinfo *
 1036 uifind(uid)
 1037         uid_t uid;
 1038 {
 1039         struct uidinfo *old_uip, *uip;
 1040 
 1041         mtx_lock(&uihashtbl_mtx);
 1042         uip = uilookup(uid);
 1043         if (uip == NULL) {
 1044                 mtx_unlock(&uihashtbl_mtx);
 1045                 uip = malloc(sizeof(*uip), M_UIDINFO, M_WAITOK | M_ZERO);
 1046                 mtx_lock(&uihashtbl_mtx);
 1047                 /*
 1048                  * There's a chance someone created our uidinfo while we
 1049                  * were in malloc and not holding the lock, so we have to
 1050                  * make sure we don't insert a duplicate uidinfo.
 1051                  */
 1052                 if ((old_uip = uilookup(uid)) != NULL) {
 1053                         /* Someone else beat us to it. */
 1054                         free(uip, M_UIDINFO);
 1055                         uip = old_uip;
 1056                 } else {
 1057                         uip->ui_mtxp = mtx_pool_alloc(mtxpool_sleep);
 1058                         uip->ui_uid = uid;
 1059                         LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
 1060                 }
 1061         }
 1062         uihold(uip);
 1063         mtx_unlock(&uihashtbl_mtx);
 1064         return (uip);
 1065 }
 1066 
 1067 /*
 1068  * Place another refcount on a uidinfo struct.
 1069  */
 1070 void
 1071 uihold(uip)
 1072         struct uidinfo *uip;
 1073 {
 1074 
 1075         UIDINFO_LOCK(uip);
 1076         uip->ui_ref++;
 1077         UIDINFO_UNLOCK(uip);
 1078 }
 1079 
 1080 /*-
 1081  * Since uidinfo structs have a long lifetime, we use an
 1082  * opportunistic refcounting scheme to avoid locking the lookup hash
 1083  * for each release.
 1084  *
 1085  * If the refcount hits 0, we need to free the structure,
 1086  * which means we need to lock the hash.
 1087  * Optimal case:
 1088  *   After locking the struct and lowering the refcount, if we find
 1089  *   that we don't need to free, simply unlock and return.
 1090  * Suboptimal case:
 1091  *   If refcount lowering results in need to free, bump the count
 1092  *   back up, loose the lock and aquire the locks in the proper
 1093  *   order to try again.
 1094  */
 1095 void
 1096 uifree(uip)
 1097         struct uidinfo *uip;
 1098 {
 1099 
 1100         /* Prepare for optimal case. */
 1101         UIDINFO_LOCK(uip);
 1102 
 1103         if (--uip->ui_ref != 0) {
 1104                 UIDINFO_UNLOCK(uip);
 1105                 return;
 1106         }
 1107 
 1108         /* Prepare for suboptimal case. */
 1109         uip->ui_ref++;
 1110         UIDINFO_UNLOCK(uip);
 1111         mtx_lock(&uihashtbl_mtx);
 1112         UIDINFO_LOCK(uip);
 1113 
 1114         /*
 1115          * We must subtract one from the count again because we backed out
 1116          * our initial subtraction before dropping the lock.
 1117          * Since another thread may have added a reference after we dropped the
 1118          * initial lock we have to test for zero again.
 1119          */
 1120         if (--uip->ui_ref == 0) {
 1121                 LIST_REMOVE(uip, ui_hash);
 1122                 mtx_unlock(&uihashtbl_mtx);
 1123                 if (uip->ui_sbsize != 0)
 1124                         printf("freeing uidinfo: uid = %d, sbsize = %jd\n",
 1125                             uip->ui_uid, (intmax_t)uip->ui_sbsize);
 1126                 if (uip->ui_proccnt != 0)
 1127                         printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
 1128                             uip->ui_uid, uip->ui_proccnt);
 1129                 UIDINFO_UNLOCK(uip);
 1130                 FREE(uip, M_UIDINFO);
 1131                 return;
 1132         }
 1133 
 1134         mtx_unlock(&uihashtbl_mtx);
 1135         UIDINFO_UNLOCK(uip);
 1136 }
 1137 
 1138 /*
 1139  * Change the count associated with number of processes
 1140  * a given user is using.  When 'max' is 0, don't enforce a limit
 1141  */
 1142 int
 1143 chgproccnt(uip, diff, max)
 1144         struct  uidinfo *uip;
 1145         int     diff;
 1146         int     max;
 1147 {
 1148 
 1149         UIDINFO_LOCK(uip);
 1150         /* Don't allow them to exceed max, but allow subtraction. */
 1151         if (diff > 0 && uip->ui_proccnt + diff > max && max != 0) {
 1152                 UIDINFO_UNLOCK(uip);
 1153                 return (0);
 1154         }
 1155         uip->ui_proccnt += diff;
 1156         if (uip->ui_proccnt < 0)
 1157                 printf("negative proccnt for uid = %d\n", uip->ui_uid);
 1158         UIDINFO_UNLOCK(uip);
 1159         return (1);
 1160 }
 1161 
 1162 /*
 1163  * Change the total socket buffer size a user has used.
 1164  */
 1165 int
 1166 chgsbsize(uip, hiwat, to, max)
 1167         struct  uidinfo *uip;
 1168         u_int  *hiwat;
 1169         u_int   to;
 1170         rlim_t  max;
 1171 {
 1172         rlim_t new;
 1173 
 1174         UIDINFO_LOCK(uip);
 1175         new = uip->ui_sbsize + to - *hiwat;
 1176         /* Don't allow them to exceed max, but allow subtraction. */
 1177         if (to > *hiwat && new > max) {
 1178                 UIDINFO_UNLOCK(uip);
 1179                 return (0);
 1180         }
 1181         uip->ui_sbsize = new;
 1182         UIDINFO_UNLOCK(uip);
 1183         *hiwat = to;
 1184         if (new < 0)
 1185                 printf("negative sbsize for uid = %d\n", uip->ui_uid);
 1186         return (1);
 1187 }

Cache object: 6d11977e50ebc09d2c54e8655471a65f


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.