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