1*1eaf0ac3Slogwang /*- 2*1eaf0ac3Slogwang * Copyright (c) 1991, 1993 3*1eaf0ac3Slogwang * The Regents of the University of California. All rights reserved. 4*1eaf0ac3Slogwang * 5*1eaf0ac3Slogwang * Redistribution and use in source and binary forms, with or without 6*1eaf0ac3Slogwang * modification, are permitted provided that the following conditions 7*1eaf0ac3Slogwang * are met: 8*1eaf0ac3Slogwang * 1. Redistributions of source code must retain the above copyright 9*1eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer. 10*1eaf0ac3Slogwang * 2. Redistributions in binary form must reproduce the above copyright 11*1eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer in the 12*1eaf0ac3Slogwang * documentation and/or other materials provided with the distribution. 13*1eaf0ac3Slogwang * 4. Neither the name of the University nor the names of its contributors 14*1eaf0ac3Slogwang * may be used to endorse or promote products derived from this software 15*1eaf0ac3Slogwang * without specific prior written permission. 16*1eaf0ac3Slogwang * 17*1eaf0ac3Slogwang * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18*1eaf0ac3Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19*1eaf0ac3Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20*1eaf0ac3Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21*1eaf0ac3Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22*1eaf0ac3Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23*1eaf0ac3Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24*1eaf0ac3Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25*1eaf0ac3Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26*1eaf0ac3Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27*1eaf0ac3Slogwang * SUCH DAMAGE. 28*1eaf0ac3Slogwang * 29*1eaf0ac3Slogwang * @(#)resourcevar.h 8.4 (Berkeley) 1/9/95 30*1eaf0ac3Slogwang * $FreeBSD$ 31*1eaf0ac3Slogwang */ 32*1eaf0ac3Slogwang 33*1eaf0ac3Slogwang #ifndef _SYS_RESOURCEVAR_H_ 34*1eaf0ac3Slogwang #define _SYS_RESOURCEVAR_H_ 35*1eaf0ac3Slogwang 36*1eaf0ac3Slogwang #include <sys/resource.h> 37*1eaf0ac3Slogwang #include <sys/queue.h> 38*1eaf0ac3Slogwang #ifdef _KERNEL 39*1eaf0ac3Slogwang #include <sys/_lock.h> 40*1eaf0ac3Slogwang #include <sys/_mutex.h> 41*1eaf0ac3Slogwang #endif 42*1eaf0ac3Slogwang 43*1eaf0ac3Slogwang /* 44*1eaf0ac3Slogwang * Kernel per-process accounting / statistics 45*1eaf0ac3Slogwang * (not necessarily resident except when running). 46*1eaf0ac3Slogwang * 47*1eaf0ac3Slogwang * Locking key: 48*1eaf0ac3Slogwang * b - created at fork, never changes 49*1eaf0ac3Slogwang * c - locked by proc mtx 50*1eaf0ac3Slogwang * k - only accessed by curthread 51*1eaf0ac3Slogwang * w - locked by proc itim lock 52*1eaf0ac3Slogwang * w2 - locked by proc prof lock 53*1eaf0ac3Slogwang */ 54*1eaf0ac3Slogwang struct pstats { 55*1eaf0ac3Slogwang #define pstat_startzero p_cru 56*1eaf0ac3Slogwang struct rusage p_cru; /* Stats for reaped children. */ 57*1eaf0ac3Slogwang struct itimerval p_timer[3]; /* (w) Virtual-time timers. */ 58*1eaf0ac3Slogwang #define pstat_endzero pstat_startcopy 59*1eaf0ac3Slogwang 60*1eaf0ac3Slogwang #define pstat_startcopy p_prof 61*1eaf0ac3Slogwang struct uprof { /* Profile arguments. */ 62*1eaf0ac3Slogwang caddr_t pr_base; /* (c + w2) Buffer base. */ 63*1eaf0ac3Slogwang u_long pr_size; /* (c + w2) Buffer size. */ 64*1eaf0ac3Slogwang u_long pr_off; /* (c + w2) PC offset. */ 65*1eaf0ac3Slogwang u_long pr_scale; /* (c + w2) PC scaling. */ 66*1eaf0ac3Slogwang } p_prof; 67*1eaf0ac3Slogwang #define pstat_endcopy p_start 68*1eaf0ac3Slogwang struct timeval p_start; /* (b) Starting time. */ 69*1eaf0ac3Slogwang }; 70*1eaf0ac3Slogwang 71*1eaf0ac3Slogwang #ifdef _KERNEL 72*1eaf0ac3Slogwang 73*1eaf0ac3Slogwang /* 74*1eaf0ac3Slogwang * Kernel shareable process resource limits. Because this structure 75*1eaf0ac3Slogwang * is moderately large but changes infrequently, it is normally 76*1eaf0ac3Slogwang * shared copy-on-write after forks. 77*1eaf0ac3Slogwang */ 78*1eaf0ac3Slogwang struct plimit { 79*1eaf0ac3Slogwang struct rlimit pl_rlimit[RLIM_NLIMITS]; 80*1eaf0ac3Slogwang int pl_refcnt; /* number of references */ 81*1eaf0ac3Slogwang }; 82*1eaf0ac3Slogwang 83*1eaf0ac3Slogwang struct racct; 84*1eaf0ac3Slogwang 85*1eaf0ac3Slogwang /*- 86*1eaf0ac3Slogwang * Per uid resource consumption. This structure is used to track 87*1eaf0ac3Slogwang * the total resource consumption (process count, socket buffer size, 88*1eaf0ac3Slogwang * etc) for the uid and impose limits. 89*1eaf0ac3Slogwang * 90*1eaf0ac3Slogwang * Locking guide: 91*1eaf0ac3Slogwang * (a) Constant from inception 92*1eaf0ac3Slogwang * (b) Lockless, updated using atomics 93*1eaf0ac3Slogwang * (c) Locked by global uihashtbl_lock 94*1eaf0ac3Slogwang * (d) Locked by the ui_vmsize_mtx 95*1eaf0ac3Slogwang */ 96*1eaf0ac3Slogwang struct uidinfo { 97*1eaf0ac3Slogwang LIST_ENTRY(uidinfo) ui_hash; /* (c) hash chain of uidinfos */ 98*1eaf0ac3Slogwang struct mtx ui_vmsize_mtx; 99*1eaf0ac3Slogwang vm_ooffset_t ui_vmsize; /* (d) swap reservation by uid */ 100*1eaf0ac3Slogwang long ui_sbsize; /* (b) socket buffer space consumed */ 101*1eaf0ac3Slogwang long ui_proccnt; /* (b) number of processes */ 102*1eaf0ac3Slogwang long ui_ptscnt; /* (b) number of pseudo-terminals */ 103*1eaf0ac3Slogwang long ui_kqcnt; /* (b) number of kqueues */ 104*1eaf0ac3Slogwang long ui_umtxcnt; /* (b) number of shared umtxs */ 105*1eaf0ac3Slogwang uid_t ui_uid; /* (a) uid */ 106*1eaf0ac3Slogwang u_int ui_ref; /* (b) reference count */ 107*1eaf0ac3Slogwang #ifdef RACCT 108*1eaf0ac3Slogwang struct racct *ui_racct; /* (a) resource accounting */ 109*1eaf0ac3Slogwang #endif 110*1eaf0ac3Slogwang }; 111*1eaf0ac3Slogwang 112*1eaf0ac3Slogwang #define UIDINFO_VMSIZE_LOCK(ui) mtx_lock(&((ui)->ui_vmsize_mtx)) 113*1eaf0ac3Slogwang #define UIDINFO_VMSIZE_UNLOCK(ui) mtx_unlock(&((ui)->ui_vmsize_mtx)) 114*1eaf0ac3Slogwang 115*1eaf0ac3Slogwang struct proc; 116*1eaf0ac3Slogwang struct rusage_ext; 117*1eaf0ac3Slogwang struct thread; 118*1eaf0ac3Slogwang 119*1eaf0ac3Slogwang void addupc_intr(struct thread *td, uintfptr_t pc, u_int ticks); 120*1eaf0ac3Slogwang void addupc_task(struct thread *td, uintfptr_t pc, u_int ticks); 121*1eaf0ac3Slogwang void calccru(struct proc *p, struct timeval *up, struct timeval *sp); 122*1eaf0ac3Slogwang void calcru(struct proc *p, struct timeval *up, struct timeval *sp); 123*1eaf0ac3Slogwang int chgkqcnt(struct uidinfo *uip, int diff, rlim_t max); 124*1eaf0ac3Slogwang int chgproccnt(struct uidinfo *uip, int diff, rlim_t maxval); 125*1eaf0ac3Slogwang int chgsbsize(struct uidinfo *uip, u_int *hiwat, u_int to, 126*1eaf0ac3Slogwang rlim_t maxval); 127*1eaf0ac3Slogwang int chgptscnt(struct uidinfo *uip, int diff, rlim_t maxval); 128*1eaf0ac3Slogwang int chgumtxcnt(struct uidinfo *uip, int diff, rlim_t maxval); 129*1eaf0ac3Slogwang int fuswintr(void *base); 130*1eaf0ac3Slogwang int kern_proc_setrlimit(struct thread *td, struct proc *p, u_int which, 131*1eaf0ac3Slogwang struct rlimit *limp); 132*1eaf0ac3Slogwang struct plimit 133*1eaf0ac3Slogwang *lim_alloc(void); 134*1eaf0ac3Slogwang void lim_copy(struct plimit *dst, struct plimit *src); 135*1eaf0ac3Slogwang rlim_t lim_cur(struct thread *td, int which); 136*1eaf0ac3Slogwang rlim_t lim_cur_proc(struct proc *p, int which); 137*1eaf0ac3Slogwang void lim_fork(struct proc *p1, struct proc *p2); 138*1eaf0ac3Slogwang void lim_free(struct plimit *limp); 139*1eaf0ac3Slogwang struct plimit 140*1eaf0ac3Slogwang *lim_hold(struct plimit *limp); 141*1eaf0ac3Slogwang rlim_t lim_max(struct thread *td, int which); 142*1eaf0ac3Slogwang rlim_t lim_max_proc(struct proc *p, int which); 143*1eaf0ac3Slogwang void lim_rlimit(struct thread *td, int which, struct rlimit *rlp); 144*1eaf0ac3Slogwang void lim_rlimit_proc(struct proc *p, int which, struct rlimit *rlp); 145*1eaf0ac3Slogwang void ruadd(struct rusage *ru, struct rusage_ext *rux, struct rusage *ru2, 146*1eaf0ac3Slogwang struct rusage_ext *rux2); 147*1eaf0ac3Slogwang void rucollect(struct rusage *ru, struct rusage *ru2); 148*1eaf0ac3Slogwang void rufetch(struct proc *p, struct rusage *ru); 149*1eaf0ac3Slogwang void rufetchcalc(struct proc *p, struct rusage *ru, struct timeval *up, 150*1eaf0ac3Slogwang struct timeval *sp); 151*1eaf0ac3Slogwang void rufetchtd(struct thread *td, struct rusage *ru); 152*1eaf0ac3Slogwang void ruxagg(struct proc *p, struct thread *td); 153*1eaf0ac3Slogwang int suswintr(void *base, int word); 154*1eaf0ac3Slogwang struct uidinfo 155*1eaf0ac3Slogwang *uifind(uid_t uid); 156*1eaf0ac3Slogwang void uifree(struct uidinfo *uip); 157*1eaf0ac3Slogwang void uihashinit(void); 158*1eaf0ac3Slogwang void uihold(struct uidinfo *uip); 159*1eaf0ac3Slogwang #ifdef RACCT 160*1eaf0ac3Slogwang void ui_racct_foreach(void (*callback)(struct racct *racct, 161*1eaf0ac3Slogwang void *arg2, void *arg3), void (*pre)(void), void (*post)(void), 162*1eaf0ac3Slogwang void *arg2, void *arg3); 163*1eaf0ac3Slogwang #endif 164*1eaf0ac3Slogwang 165*1eaf0ac3Slogwang #endif /* _KERNEL */ 166*1eaf0ac3Slogwang #endif /* !_SYS_RESOURCEVAR_H_ */ 167