FreeBSD kernel kern code
kern_resource.c File Reference
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysproto.h>
#include <sys/file.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/priv.h>
#include <sys/proc.h>
#include <sys/refcount.h>
#include <sys/racct.h>
#include <sys/resourcevar.h>
#include <sys/rwlock.h>
#include <sys/sched.h>
#include <sys/sx.h>
#include <sys/syscallsubr.h>
#include <sys/sysctl.h>
#include <sys/sysent.h>
#include <sys/time.h>
#include <sys/umtxvar.h>
#include <vm/vm.h>
#include <vm/vm_param.h>
#include <vm/pmap.h>
#include <vm/vm_map.h>
Include dependency graph for kern_resource.c:

Go to the source code of this file.

Data Structures

struct  setpriority_args
 
struct  rtprio_thread_args
 
struct  rtprio_args
 
struct  setrlimit_args
 
struct  getrlimit_args
 
struct  getrusage_args
 

Macros

#define UIHASH(uid)   (&uihashtbl[(uid) & uihash])
 
#define flsll(x)   (fls((x) >> 32) != 0 ? fls((x) >> 32) + 32 : fls(x))
 

Functions

 __FBSDID ("$FreeBSD$")
 
static MALLOC_DEFINE (M_PLIMIT, "plimit", "plimit structures")
 
static MALLOC_DEFINE (M_UIDINFO, "uidinfo", "uidinfo structures")
 
static LIST_HEAD (uihashhead, uidinfo)
 
int sys_getpriority (struct thread *td, struct getpriority_args *uap)
 
int kern_getpriority (struct thread *td, int which, int who)
 
int sys_setpriority (struct thread *td, struct setpriority_args *uap)
 
int kern_setpriority (struct thread *td, int which, int who, int prio)
 
static int donice (struct thread *td, struct proc *p, int n)
 
 SYSCTL_INT (_security_bsd, OID_AUTO, unprivileged_idprio, CTLFLAG_RW, &unprivileged_idprio, 0, "Allow non-root users to set an idle priority (deprecated)")
 
int sys_rtprio_thread (struct thread *td, struct rtprio_thread_args *uap)
 
int sys_rtprio (struct thread *td, struct rtprio_args *uap)
 
int rtp_to_pri (struct rtprio *rtp, struct thread *td)
 
void pri_to_rtp (struct thread *td, struct rtprio *rtp)
 
int sys_setrlimit (struct thread *td, struct setrlimit_args *uap)
 
static void lim_cb (void *arg)
 
int kern_setrlimit (struct thread *td, u_int which, struct rlimit *limp)
 
int kern_proc_setrlimit (struct thread *td, struct proc *p, u_int which, struct rlimit *limp)
 
int sys_getrlimit (struct thread *td, struct getrlimit_args *uap)
 
void calccru (struct proc *p, struct timeval *up, struct timeval *sp)
 
void calcru (struct proc *p, struct timeval *up, struct timeval *sp)
 
void rufetchtd (struct thread *td, struct rusage *ru)
 
static uint64_t mul64_by_fraction (uint64_t a, uint64_t b, uint64_t c)
 
static void calcru1 (struct proc *p, struct rusage_ext *ruxp, struct timeval *up, struct timeval *sp)
 
int sys_getrusage (struct thread *td, struct getrusage_args *uap)
 
int kern_getrusage (struct thread *td, int who, struct rusage *rup)
 
void rucollect (struct rusage *ru, struct rusage *ru2)
 
void ruadd (struct rusage *ru, struct rusage_ext *rux, struct rusage *ru2, struct rusage_ext *rux2)
 
static void ruxagg_ext_locked (struct rusage_ext *rux, struct thread *td)
 
void ruxagg_locked (struct proc *p, struct thread *td)
 
void ruxagg (struct proc *p, struct thread *td)
 
void rufetch (struct proc *p, struct rusage *ru)
 
void rufetchcalc (struct proc *p, struct rusage *ru, struct timeval *up, struct timeval *sp)
 
struct plimit * lim_alloc ()
 
struct plimit * lim_hold (struct plimit *limp)
 
struct plimit * lim_cowsync (void)
 
void lim_fork (struct proc *p1, struct proc *p2)
 
void lim_free (struct plimit *limp)
 
void lim_freen (struct plimit *limp, int n)
 
void lim_copy (struct plimit *dst, struct plimit *src)
 
rlim_t lim_max (struct thread *td, int which)
 
rlim_t lim_max_proc (struct proc *p, int which)
 
rlim_t() lim_cur (struct thread *td, int which)
 
rlim_t lim_cur_proc (struct proc *p, int which)
 
void lim_rlimit (struct thread *td, int which, struct rlimit *rlp)
 
void lim_rlimit_proc (struct proc *p, int which, struct rlimit *rlp)
 
void uihashinit ()
 
static struct uidinfo * uilookup (uid_t uid)
 
struct uidinfo * uifind (uid_t uid)
 
void uihold (struct uidinfo *uip)
 
void uifree (struct uidinfo *uip)
 
static int chglimit (struct uidinfo *uip, long *limit, int diff, rlim_t max, const char *name)
 
int chgproccnt (struct uidinfo *uip, int diff, rlim_t max)
 
int chgsbsize (struct uidinfo *uip, u_int *hiwat, u_int to, rlim_t max)
 
int chgptscnt (struct uidinfo *uip, int diff, rlim_t max)
 
int chgkqcnt (struct uidinfo *uip, int diff, rlim_t max)
 
int chgumtxcnt (struct uidinfo *uip, int diff, rlim_t max)
 

Variables

static struct rwlock uihashtbl_lock
 
static int unprivileged_idprio
 

Macro Definition Documentation

◆ flsll

#define flsll (   x)    (fls((x) >> 32) != 0 ? fls((x) >> 32) + 32 : fls(x))

Definition at line 890 of file kern_resource.c.

◆ UIHASH

#define UIHASH (   uid)    (&uihashtbl[(uid) & uihash])

Definition at line 71 of file kern_resource.c.

Function Documentation

◆ __FBSDID()

__FBSDID ( "$FreeBSD$"  )

◆ calccru()

void calccru ( struct proc *  p,
struct timeval *  up,
struct timeval *  sp 
)

Definition at line 817 of file kern_resource.c.

References calcru1().

Referenced by fill_kinfo_proc_only(), kern_getrusage(), and proc_to_reap().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ calcru()

void calcru ( struct proc *  p,
struct timeval *  up,
struct timeval *  sp 
)

Definition at line 830 of file kern_resource.c.

References calcru1(), cpu_ticks, and ruxagg().

Referenced by fill_kinfo_proc_only(), kern_clock_gettime(), proc_to_reap(), and rufetchcalc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ calcru1()

static void calcru1 ( struct proc *  p,
struct rusage_ext *  ruxp,
struct timeval *  up,
struct timeval *  sp 
)
static

Definition at line 973 of file kern_resource.c.

References cputick2usec(), mul64_by_fraction(), printf(), st, and vm_guest.

Referenced by calccru(), calcru(), and rufetchtd().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ chgkqcnt()

int chgkqcnt ( struct uidinfo *  uip,
int  diff,
rlim_t  max 
)

Definition at line 1581 of file kern_resource.c.

References chglimit().

Referenced by kern_kqueue(), and kqueue_close().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ chglimit()

static int chglimit ( struct uidinfo *  uip,
long *  limit,
int  diff,
rlim_t  max,
const char *  name 
)
inlinestatic

Definition at line 1523 of file kern_resource.c.

References name, and printf().

Referenced by chgkqcnt(), chgproccnt(), chgptscnt(), chgsbsize(), and chgumtxcnt().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ chgproccnt()

int chgproccnt ( struct uidinfo *  uip,
int  diff,
rlim_t  max 
)

Definition at line 1544 of file kern_resource.c.

References chglimit().

Referenced by change_ruid(), fork1(), proc0_init(), and proc_reap().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ chgptscnt()

int chgptscnt ( struct uidinfo *  uip,
int  diff,
rlim_t  max 
)

Definition at line 1574 of file kern_resource.c.

References chglimit().

Referenced by pts_alloc(), pts_alloc_external(), and ptsdrv_free().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ chgsbsize()

int chgsbsize ( struct uidinfo *  uip,
u_int *  hiwat,
u_int  to,
rlim_t  max 
)

Definition at line 1554 of file kern_resource.c.

References chglimit().

Referenced by sbrelease_internal(), sbreserve_locked(), and sodealloc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ chgumtxcnt()

int chgumtxcnt ( struct uidinfo *  uip,
int  diff,
rlim_t  max 
)

Definition at line 1588 of file kern_resource.c.

References chglimit().

Referenced by umtx_shm_create_reg(), and umtx_shm_free_reg().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ donice()

static int donice ( struct thread *  td,
struct proc *  p,
int  n 
)
static

Definition at line 268 of file kern_resource.c.

References p_cansched(), priv_check(), and sched_nice().

Referenced by kern_setpriority().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ kern_getpriority()

int kern_getpriority ( struct thread *  td,
int  which,
int  who 
)

Definition at line 99 of file kern_resource.c.

References allproc_lock, p_cansee(), pfind(), pgfind(), and proctree_lock.

Referenced by sys_getpriority().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ kern_getrusage()

int kern_getrusage ( struct thread *  td,
int  who,
struct rusage *  rup 
)

Definition at line 1072 of file kern_resource.c.

References calccru(), rufetchcalc(), and rufetchtd().

Referenced by dotrace(), and sys_getrusage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ kern_proc_setrlimit()

int kern_proc_setrlimit ( struct thread *  td,
struct proc *  p,
u_int  which,
struct rlimit *  limp 
)

Definition at line 656 of file kern_resource.c.

References addr, lim_alloc(), lim_cb(), lim_copy(), lim_cowsync(), lim_free(), lim_freen(), maxdsiz, maxfilesperproc, maxprocperuid, maxssiz, priv_check(), and thread_cow_synced().

Referenced by kern_setrlimit(), and sysctl_kern_proc_rlimit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ kern_setpriority()

int kern_setpriority ( struct thread *  td,
int  which,
int  who,
int  prio 
)

Definition at line 188 of file kern_resource.c.

References allproc_lock, donice(), p_cansee(), pfind(), pgfind(), and proctree_lock.

Referenced by sys_setpriority().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ kern_setrlimit()

int kern_setrlimit ( struct thread *  td,
u_int  which,
struct rlimit *  limp 
)

Definition at line 649 of file kern_resource.c.

References kern_proc_setrlimit().

Referenced by exec_map_stack(), and sys_setrlimit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lim_alloc()

struct plimit * lim_alloc ( )

Definition at line 1212 of file kern_resource.c.

References malloc().

Referenced by kern_proc_setrlimit(), and proc0_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lim_cb()

static void lim_cb ( void *  arg)
static

Definition at line 614 of file kern_resource.c.

References cpu_tickrate(), kern_psignal(), killproc(), lim_cb(), lim_rlimit_proc(), and ruxagg().

Referenced by kern_proc_setrlimit(), lim_cb(), and lim_fork().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lim_copy()

void lim_copy ( struct plimit *  dst,
struct plimit *  src 
)

Definition at line 1284 of file kern_resource.c.

References src.

Referenced by kern_proc_setrlimit().

Here is the caller graph for this function:

◆ lim_cowsync()

struct plimit * lim_cowsync ( void  )

Definition at line 1230 of file kern_resource.c.

References lim_hold().

Referenced by kern_proc_setrlimit(), and thread_cow_update().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lim_cur()

rlim_t() lim_cur ( struct thread *  td,
int  which 
)

Definition at line 1317 of file kern_resource.c.

References lim_rlimit().

Referenced by coredump(), fork1(), getmaxfd(), kern_kqueue(), pts_alloc(), pts_alloc_external(), sbreserve_locked(), umtx_shm_create_reg(), and vn_rlimit_fsize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lim_cur_proc()

rlim_t lim_cur_proc ( struct proc *  p,
int  which 
)

Definition at line 1327 of file kern_resource.c.

References lim_rlimit_proc().

Referenced by enforce_limits(), and exec_aout_imgact().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lim_fork()

void lim_fork ( struct proc *  p1,
struct proc *  p2 
)

Definition at line 1250 of file kern_resource.c.

References lim_cb(), and lim_hold().

Referenced by do_fork().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lim_free()

void lim_free ( struct plimit *  limp)

Definition at line 1264 of file kern_resource.c.

References free().

Referenced by exit1(), kern_proc_setrlimit(), thread_cow_free(), and thread_cow_update().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lim_freen()

void lim_freen ( struct plimit *  limp,
int  n 
)

Definition at line 1272 of file kern_resource.c.

References free().

Referenced by kern_proc_setrlimit(), and thread_reap_domain().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lim_hold()

struct plimit * lim_hold ( struct plimit *  limp)

Definition at line 1222 of file kern_resource.c.

Referenced by lim_cowsync(), lim_fork(), thread_cow_get(), and thread_cow_get_proc().

Here is the caller graph for this function:

◆ lim_max()

rlim_t lim_max ( struct thread *  td,
int  which 
)

Definition at line 1296 of file kern_resource.c.

References lim_rlimit().

Referenced by copyout_map(), exec_map_stack(), kern_shmat_locked(), and shm_mmap_large().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lim_max_proc()

rlim_t lim_max_proc ( struct proc *  p,
int  which 
)

Definition at line 1305 of file kern_resource.c.

References lim_rlimit_proc().

Here is the call graph for this function:

◆ lim_rlimit()

void lim_rlimit ( struct thread *  td,
int  which,
struct rlimit *  rlp 
)

Definition at line 1340 of file kern_resource.c.

Referenced by lim_cur(), lim_max(), and sys_getrlimit().

Here is the caller graph for this function:

◆ lim_rlimit_proc()

void lim_rlimit_proc ( struct proc *  p,
int  which,
struct rlimit *  rlp 
)

Definition at line 1353 of file kern_resource.c.

Referenced by exec_map_stack(), lim_cb(), lim_cur_proc(), lim_max_proc(), note_procstat_rlimit(), and sysctl_kern_proc_rlimit().

Here is the caller graph for this function:

◆ LIST_HEAD()

static LIST_HEAD ( uihashhead  ,
uidinfo   
)
static

Definition at line 73 of file kern_resource.c.

◆ MALLOC_DEFINE() [1/2]

static MALLOC_DEFINE ( M_PLIMIT  ,
"plimit"  ,
"plimit structures"   
)
static

◆ MALLOC_DEFINE() [2/2]

static MALLOC_DEFINE ( M_UIDINFO  ,
"uidinfo"  ,
"uidinfo structures"   
)
static

◆ mul64_by_fraction()

static uint64_t mul64_by_fraction ( uint64_t  a,
uint64_t  b,
uint64_t  c 
)
static

Definition at line 894 of file kern_resource.c.

References flsll.

Referenced by calcru1().

Here is the caller graph for this function:

◆ pri_to_rtp()

void pri_to_rtp ( struct thread *  td,
struct rtprio *  rtp 
)

Definition at line 519 of file kern_resource.c.

Referenced by getscheduler(), ksched_getparam(), sys_rtprio(), and sys_rtprio_thread().

Here is the caller graph for this function:

◆ rtp_to_pri()

int rtp_to_pri ( struct rtprio *  rtp,
struct thread *  td 
)

Definition at line 476 of file kern_resource.c.

References sched_class(), sched_prio(), sched_user_prio(), and umtx_pi_adjust().

Referenced by ksched_setscheduler(), poll_idle(), sys_rtprio(), sys_rtprio_thread(), and thread_create().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ruadd()

void ruadd ( struct rusage *  ru,
struct rusage_ext *  rux,
struct rusage *  ru2,
struct rusage_ext *  rux2 
)

Definition at line 1121 of file kern_resource.c.

References rucollect().

Referenced by exit1(), and proc_reap().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rucollect()

void rucollect ( struct rusage *  ru,
struct rusage *  ru2 
)

Definition at line 1107 of file kern_resource.c.

Referenced by ruadd(), rufetch(), and thread_exit().

Here is the caller graph for this function:

◆ rufetch()

void rufetch ( struct proc *  p,
struct rusage *  ru 
)

Definition at line 1176 of file kern_resource.c.

References rucollect(), and ruxagg().

Referenced by fill_kinfo_proc_only(), kern_process_cputime(), proc0_post(), and rufetchcalc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rufetchcalc()

void rufetchcalc ( struct proc *  p,
struct rusage *  ru,
struct timeval *  up,
struct timeval *  sp 
)

Definition at line 1197 of file kern_resource.c.

References calcru(), and rufetch().

Referenced by acct_process(), kern_getrusage(), and tty_info().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rufetchtd()

void rufetchtd ( struct thread *  td,
struct rusage *  ru 
)

Definition at line 862 of file kern_resource.c.

References calcru1(), cpu_ticks, and ruxagg_locked().

Referenced by fill_kinfo_thread(), and kern_getrusage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ruxagg()

void ruxagg ( struct proc *  p,
struct thread *  td 
)

Definition at line 1163 of file kern_resource.c.

References ruxagg_locked().

Referenced by calcru(), lim_cb(), and rufetch().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ruxagg_ext_locked()

static void ruxagg_ext_locked ( struct rusage_ext *  rux,
struct thread *  td 
)
static

Definition at line 1139 of file kern_resource.c.

Referenced by ruxagg_locked().

Here is the caller graph for this function:

◆ ruxagg_locked()

void ruxagg_locked ( struct proc *  p,
struct thread *  td 
)

Definition at line 1149 of file kern_resource.c.

References ruxagg_ext_locked().

Referenced by rufetchtd(), ruxagg(), and thread_exit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sys_getpriority()

int sys_getpriority ( struct thread *  td,
struct getpriority_args *  uap 
)

Definition at line 92 of file kern_resource.c.

References kern_getpriority().

Here is the call graph for this function:

◆ sys_getrlimit()

int sys_getrlimit ( struct thread *  td,
struct getrlimit_args uap 
)

Definition at line 800 of file kern_resource.c.

References lim_rlimit(), getrlimit_args::rlp, and getrlimit_args::which.

Here is the call graph for this function:

◆ sys_getrusage()

int sys_getrusage ( struct thread *  td,
struct getrusage_args uap 
)

Definition at line 1060 of file kern_resource.c.

References kern_getrusage(), getrusage_args::rusage, and getrusage_args::who.

Here is the call graph for this function:

◆ sys_rtprio()

int sys_rtprio ( struct thread *  td,
struct rtprio_args uap 
)

Definition at line 382 of file kern_resource.c.

References rtprio_args::function, p_cansched(), p_cansee(), pfind(), rtprio_args::pid, pri_to_rtp(), priv_check(), rtprio_args::rtp, rtp_to_pri(), and unprivileged_idprio.

Here is the call graph for this function:

◆ sys_rtprio_thread()

int sys_rtprio_thread ( struct thread *  td,
struct rtprio_thread_args uap 
)

◆ sys_setpriority()

int sys_setpriority ( struct thread *  td,
struct setpriority_args uap 
)

Definition at line 181 of file kern_resource.c.

References kern_setpriority(), setpriority_args::prio, setpriority_args::which, and setpriority_args::who.

Here is the call graph for this function:

◆ sys_setrlimit()

int sys_setrlimit ( struct thread *  td,
struct setrlimit_args uap 
)

Definition at line 602 of file kern_resource.c.

References kern_setrlimit(), setrlimit_args::rlp, and setrlimit_args::which.

Here is the call graph for this function:

◆ SYSCTL_INT()

SYSCTL_INT ( _security_bsd  ,
OID_AUTO  ,
unprivileged_idprio  ,
CTLFLAG_RW  ,
unprivileged_idprio,
,
"Allow non-root users to set an idle priority (deprecated)"   
)

◆ uifind()

struct uidinfo * uifind ( uid_t  uid)

Definition at line 1400 of file kern_resource.c.

References free(), malloc(), UIHASH, uihashtbl_lock, uihold(), and uilookup().

Referenced by do_execve(), proc0_init(), sys_seteuid(), sys_setresuid(), sys_setreuid(), and sys_setuid().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uifree()

void uifree ( struct uidinfo *  uip)

Definition at line 1471 of file kern_resource.c.

References free(), printf(), and uihashtbl_lock.

Referenced by change_euid(), change_ruid(), crfree_final(), do_execve(), sys_seteuid(), sys_setresuid(), sys_setreuid(), and sys_setuid().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uihashinit()

void uihashinit ( )

Definition at line 1365 of file kern_resource.c.

References hashinit(), maxproc, and uihashtbl_lock.

Referenced by procinit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uihold()

void uihold ( struct uidinfo *  uip)

Definition at line 1449 of file kern_resource.c.

Referenced by change_euid(), change_ruid(), crcopy(), uifind(), and uilookup().

Here is the caller graph for this function:

◆ uilookup()

static struct uidinfo * uilookup ( uid_t  uid)
static

Definition at line 1378 of file kern_resource.c.

References UIHASH, uihashtbl_lock, and uihold().

Referenced by uifind().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ uihashtbl_lock

struct rwlock uihashtbl_lock
static

Definition at line 72 of file kern_resource.c.

Referenced by uifind(), uifree(), uihashinit(), and uilookup().

◆ unprivileged_idprio

int unprivileged_idprio
static

Definition at line 285 of file kern_resource.c.

Referenced by sys_rtprio(), and sys_rtprio_thread().