kernel - misc MPSAFe work
[dragonfly.git] / sys / kern / kern_resource.c
CommitLineData
984263bc
MD
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/kern_resource.c,v 1.55.2.5 2001/11/03 01:41:08 ps Exp $
3a6117bb 40 * $DragonFly: src/sys/kern/kern_resource.c,v 1.35 2008/05/27 05:25:34 dillon Exp $
984263bc
MD
41 */
42
43#include "opt_compat.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/sysproto.h>
48#include <sys/file.h>
9697c509 49#include <sys/kern_syscall.h>
984263bc
MD
50#include <sys/kernel.h>
51#include <sys/resourcevar.h>
52#include <sys/malloc.h>
53#include <sys/proc.h>
895c1f85 54#include <sys/priv.h>
984263bc 55#include <sys/time.h>
508ceb09 56#include <sys/lockf.h>
984263bc
MD
57
58#include <vm/vm.h>
59#include <vm/vm_param.h>
60#include <sys/lock.h>
61#include <vm/pmap.h>
62#include <vm/vm_map.h>
63
37af14fe 64#include <sys/thread2.h>
9d7a637e 65#include <sys/spinlock2.h>
684a93c4 66#include <sys/mplock2.h>
37af14fe 67
402ed7e1 68static int donice (struct proc *chgp, int n);
984263bc
MD
69
70static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures");
71#define UIHASH(uid) (&uihashtbl[(uid) & uihash])
9d7a637e 72static struct spinlock uihash_lock;
984263bc
MD
73static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
74static u_long uihash; /* size of hash table - 1 */
75
402ed7e1
RG
76static struct uidinfo *uicreate (uid_t uid);
77static struct uidinfo *uilookup (uid_t uid);
984263bc
MD
78
79/*
80 * Resource controls and accounting.
81 */
82
8fa76237
MD
83struct getpriority_info {
84 int low;
85 int who;
86};
87
88static int getpriority_callback(struct proc *p, void *data);
89
3919ced0
MD
90/*
91 * MPALMOSTSAFE
92 */
984263bc 93int
753fd850 94sys_getpriority(struct getpriority_args *uap)
984263bc 95{
8fa76237 96 struct getpriority_info info;
41c20dac
MD
97 struct proc *curp = curproc;
98 struct proc *p;
99 int low = PRIO_MAX + 1;
3919ced0
MD
100 int error;
101
102 get_mplock();
984263bc
MD
103
104 switch (uap->which) {
984263bc
MD
105 case PRIO_PROCESS:
106 if (uap->who == 0)
107 p = curp;
108 else
109 p = pfind(uap->who);
110 if (p == 0)
111 break;
41c20dac 112 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
984263bc
MD
113 break;
114 low = p->p_nice;
115 break;
116
41c20dac
MD
117 case PRIO_PGRP:
118 {
1fd87d54 119 struct pgrp *pg;
984263bc
MD
120
121 if (uap->who == 0)
122 pg = curp->p_pgrp;
123 else if ((pg = pgfind(uap->who)) == NULL)
124 break;
125 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
41c20dac 126 if ((PRISON_CHECK(curp->p_ucred, p->p_ucred) && p->p_nice < low))
984263bc
MD
127 low = p->p_nice;
128 }
129 break;
130 }
984263bc
MD
131 case PRIO_USER:
132 if (uap->who == 0)
133 uap->who = curp->p_ucred->cr_uid;
8fa76237
MD
134 info.low = low;
135 info.who = uap->who;
136 allproc_scan(getpriority_callback, &info);
137 low = info.low;
984263bc
MD
138 break;
139
140 default:
3919ced0
MD
141 error = EINVAL;
142 goto done;
143 }
144 if (low == PRIO_MAX + 1) {
145 error = ESRCH;
146 goto done;
984263bc 147 }
c7114eea 148 uap->sysmsg_result = low;
3919ced0
MD
149 error = 0;
150done:
151 rel_mplock();
152 return (error);
984263bc
MD
153}
154
8fa76237
MD
155/*
156 * Figure out the current lowest nice priority for processes owned
157 * by the specified user.
158 */
159static
160int
161getpriority_callback(struct proc *p, void *data)
162{
163 struct getpriority_info *info = data;
164
165 if (PRISON_CHECK(curproc->p_ucred, p->p_ucred) &&
166 p->p_ucred->cr_uid == info->who &&
167 p->p_nice < info->low) {
168 info->low = p->p_nice;
169 }
170 return(0);
171}
172
173struct setpriority_info {
174 int prio;
175 int who;
176 int error;
177 int found;
178};
179
180static int setpriority_callback(struct proc *p, void *data);
181
3919ced0
MD
182/*
183 * MPALMOSTSAFE
184 */
984263bc 185int
753fd850 186sys_setpriority(struct setpriority_args *uap)
984263bc 187{
8fa76237 188 struct setpriority_info info;
41c20dac
MD
189 struct proc *curp = curproc;
190 struct proc *p;
984263bc
MD
191 int found = 0, error = 0;
192
3919ced0
MD
193 get_mplock();
194
984263bc 195 switch (uap->which) {
984263bc
MD
196 case PRIO_PROCESS:
197 if (uap->who == 0)
198 p = curp;
199 else
200 p = pfind(uap->who);
201 if (p == 0)
202 break;
41c20dac 203 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
984263bc 204 break;
41c20dac 205 error = donice(p, uap->prio);
984263bc
MD
206 found++;
207 break;
208
41c20dac
MD
209 case PRIO_PGRP:
210 {
1fd87d54 211 struct pgrp *pg;
984263bc
MD
212
213 if (uap->who == 0)
214 pg = curp->p_pgrp;
215 else if ((pg = pgfind(uap->who)) == NULL)
216 break;
217 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
41c20dac
MD
218 if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
219 error = donice(p, uap->prio);
984263bc
MD
220 found++;
221 }
222 }
223 break;
224 }
984263bc
MD
225 case PRIO_USER:
226 if (uap->who == 0)
227 uap->who = curp->p_ucred->cr_uid;
8fa76237
MD
228 info.prio = uap->prio;
229 info.who = uap->who;
230 info.error = 0;
231 info.found = 0;
232 allproc_scan(setpriority_callback, &info);
233 error = info.error;
234 found = info.found;
984263bc
MD
235 break;
236
237 default:
3919ced0
MD
238 error = EINVAL;
239 found = 1;
240 break;
984263bc 241 }
3919ced0
MD
242
243 rel_mplock();
984263bc 244 if (found == 0)
3919ced0 245 error = ESRCH;
984263bc
MD
246 return (error);
247}
248
8fa76237
MD
249static
250int
251setpriority_callback(struct proc *p, void *data)
252{
253 struct setpriority_info *info = data;
254 int error;
255
256 if (p->p_ucred->cr_uid == info->who &&
257 PRISON_CHECK(curproc->p_ucred, p->p_ucred)) {
258 error = donice(p, info->prio);
259 if (error)
260 info->error = error;
261 ++info->found;
262 }
263 return(0);
264}
265
984263bc 266static int
41c20dac 267donice(struct proc *chgp, int n)
984263bc 268{
41c20dac
MD
269 struct proc *curp = curproc;
270 struct ucred *cr = curp->p_ucred;
08f2f1bb 271 struct lwp *lp;
984263bc 272
41c20dac
MD
273 if (cr->cr_uid && cr->cr_ruid &&
274 cr->cr_uid != chgp->p_ucred->cr_uid &&
275 cr->cr_ruid != chgp->p_ucred->cr_uid)
984263bc
MD
276 return (EPERM);
277 if (n > PRIO_MAX)
278 n = PRIO_MAX;
279 if (n < PRIO_MIN)
280 n = PRIO_MIN;
3b1d99e9 281 if (n < chgp->p_nice && priv_check_cred(cr, PRIV_SCHED_SETPRIORITY, 0))
984263bc
MD
282 return (EACCES);
283 chgp->p_nice = n;
08f2f1bb
SS
284 FOREACH_LWP_IN_PROC(lp, chgp)
285 chgp->p_usched->resetpriority(lp);
984263bc
MD
286 return (0);
287}
288
3919ced0
MD
289/*
290 * MPALMOSTSAFE
291 */
649d3bd2
MD
292int
293sys_lwp_rtprio(struct lwp_rtprio_args *uap)
294{
295 struct proc *p = curproc;
296 struct lwp *lp;
297 struct rtprio rtp;
9910d07b 298 struct ucred *cr = curthread->td_ucred;
649d3bd2
MD
299 int error;
300
301 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
302 if (error)
303 return error;
3919ced0 304 if (uap->pid < 0)
649d3bd2 305 return EINVAL;
3919ced0
MD
306
307 get_mplock();
308 if (uap->pid == 0) {
649d3bd2
MD
309 /* curproc already loaded on p */
310 } else {
311 p = pfind(uap->pid);
312 }
313
3919ced0
MD
314 if (p == NULL) {
315 error = ESRCH;
316 goto done;
649d3bd2
MD
317 }
318
319 if (uap->tid < -1) {
3919ced0
MD
320 error = EINVAL;
321 goto done;
322 }
323 if (uap->tid == -1) {
649d3bd2
MD
324 /*
325 * sadly, tid can be 0 so we can't use 0 here
326 * like sys_rtprio()
327 */
328 lp = curthread->td_lwp;
329 } else {
3e291793 330 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, uap->tid);
3919ced0
MD
331 if (lp == NULL) {
332 error = ESRCH;
333 goto done;
334 }
649d3bd2
MD
335 }
336
337 switch (uap->function) {
338 case RTP_LOOKUP:
3919ced0
MD
339 error = copyout(&lp->lwp_rtprio, uap->rtp,
340 sizeof(struct rtprio));
341 break;
649d3bd2
MD
342 case RTP_SET:
343 if (cr->cr_uid && cr->cr_ruid &&
344 cr->cr_uid != p->p_ucred->cr_uid &&
345 cr->cr_ruid != p->p_ucred->cr_uid) {
3919ced0
MD
346 error = EPERM;
347 break;
649d3bd2
MD
348 }
349 /* disallow setting rtprio in most cases if not superuser */
3b1d99e9 350 if (priv_check_cred(cr, PRIV_SCHED_RTPRIO, 0)) {
649d3bd2
MD
351 /* can't set someone else's */
352 if (uap->pid) { /* XXX */
3919ced0
MD
353 error = EPERM;
354 break;
649d3bd2
MD
355 }
356 /* can't set realtime priority */
357/*
358 * Realtime priority has to be restricted for reasons which should be
359 * obvious. However, for idle priority, there is a potential for
360 * system deadlock if an idleprio process gains a lock on a resource
361 * that other processes need (and the idleprio process can't run
362 * due to a CPU-bound normal process). Fix me! XXX
363 */
364 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
3919ced0
MD
365 error = EPERM;
366 break;
649d3bd2
MD
367 }
368 }
369 switch (rtp.type) {
370#ifdef RTP_PRIO_FIFO
371 case RTP_PRIO_FIFO:
372#endif
373 case RTP_PRIO_REALTIME:
374 case RTP_PRIO_NORMAL:
375 case RTP_PRIO_IDLE:
376 if (rtp.prio > RTP_PRIO_MAX)
377 return EINVAL;
378 lp->lwp_rtprio = rtp;
3919ced0
MD
379 error = 0;
380 break;
649d3bd2 381 default:
3919ced0
MD
382 error = EINVAL;
383 break;
649d3bd2 384 }
3919ced0 385 break;
649d3bd2 386 default:
3919ced0
MD
387 error = EINVAL;
388 break;
649d3bd2 389 }
3919ced0
MD
390
391done:
392 rel_mplock();
393 return (error);
649d3bd2
MD
394}
395
984263bc
MD
396/*
397 * Set realtime priority
3919ced0
MD
398 *
399 * MPALMOSTSAFE
984263bc 400 */
984263bc 401int
753fd850 402sys_rtprio(struct rtprio_args *uap)
984263bc 403{
41c20dac
MD
404 struct proc *curp = curproc;
405 struct proc *p;
08f2f1bb 406 struct lwp *lp;
9910d07b 407 struct ucred *cr = curthread->td_ucred;
984263bc
MD
408 struct rtprio rtp;
409 int error;
410
411 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
412 if (error)
413 return (error);
414
3919ced0 415 get_mplock();
984263bc
MD
416 if (uap->pid == 0)
417 p = curp;
418 else
419 p = pfind(uap->pid);
420
3919ced0
MD
421 if (p == NULL) {
422 error = ESRCH;
423 goto done;
424 }
984263bc 425
08f2f1bb
SS
426 /* XXX lwp */
427 lp = FIRST_LWP_IN_PROC(p);
984263bc
MD
428 switch (uap->function) {
429 case RTP_LOOKUP:
3919ced0
MD
430 error = copyout(&lp->lwp_rtprio, uap->rtp,
431 sizeof(struct rtprio));
432 break;
984263bc 433 case RTP_SET:
41c20dac
MD
434 if (cr->cr_uid && cr->cr_ruid &&
435 cr->cr_uid != p->p_ucred->cr_uid &&
3919ced0
MD
436 cr->cr_ruid != p->p_ucred->cr_uid) {
437 error = EPERM;
438 break;
439 }
984263bc 440 /* disallow setting rtprio in most cases if not superuser */
3b1d99e9 441 if (priv_check_cred(cr, PRIV_SCHED_RTPRIO, 0)) {
984263bc 442 /* can't set someone else's */
3919ced0
MD
443 if (uap->pid) {
444 error = EPERM;
445 break;
446 }
984263bc
MD
447 /* can't set realtime priority */
448/*
449 * Realtime priority has to be restricted for reasons which should be
450 * obvious. However, for idle priority, there is a potential for
451 * system deadlock if an idleprio process gains a lock on a resource
452 * that other processes need (and the idleprio process can't run
453 * due to a CPU-bound normal process). Fix me! XXX
454 */
3919ced0
MD
455 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
456 error = EPERM;
457 break;
458 }
984263bc
MD
459 }
460 switch (rtp.type) {
461#ifdef RTP_PRIO_FIFO
462 case RTP_PRIO_FIFO:
463#endif
464 case RTP_PRIO_REALTIME:
465 case RTP_PRIO_NORMAL:
466 case RTP_PRIO_IDLE:
3919ced0
MD
467 if (rtp.prio > RTP_PRIO_MAX) {
468 error = EINVAL;
469 break;
470 }
08f2f1bb 471 lp->lwp_rtprio = rtp;
3919ced0
MD
472 error = 0;
473 break;
984263bc 474 default:
3919ced0
MD
475 error = EINVAL;
476 break;
984263bc 477 }
3919ced0 478 break;
984263bc 479 default:
3919ced0
MD
480 error = EINVAL;
481 break;
984263bc 482 }
3919ced0
MD
483done:
484 rel_mplock();
485 return (error);
984263bc
MD
486}
487
3919ced0
MD
488/*
489 * MPSAFE
490 */
984263bc 491int
753fd850 492sys_setrlimit(struct __setrlimit_args *uap)
984263bc
MD
493{
494 struct rlimit alim;
495 int error;
496
9697c509
DRJ
497 error = copyin(uap->rlp, &alim, sizeof(alim));
498 if (error)
984263bc 499 return (error);
9697c509
DRJ
500
501 error = kern_setrlimit(uap->which, &alim);
502
503 return (error);
984263bc
MD
504}
505
3919ced0
MD
506/*
507 * MPSAFE
508 */
984263bc 509int
753fd850 510sys_getrlimit(struct __getrlimit_args *uap)
9697c509
DRJ
511{
512 struct rlimit lim;
513 int error;
514
515 error = kern_getrlimit(uap->which, &lim);
516
517 if (error == 0)
518 error = copyout(&lim, uap->rlp, sizeof(*uap->rlp));
519 return error;
984263bc
MD
520}
521
522/*
fde7ac71 523 * Transform the running time and tick information in lwp lp's thread into user,
984263bc 524 * system, and interrupt time usage.
d16a8831
MD
525 *
526 * Since we are limited to statclock tick granularity this is a statisical
527 * calculation which will be correct over the long haul, but should not be
528 * expected to measure fine grained deltas.
585aafb6
MD
529 *
530 * It is possible to catch a lwp in the midst of being created, so
531 * check whether lwp_thread is NULL or not.
984263bc
MD
532 */
533void
fde7ac71 534calcru(struct lwp *lp, struct timeval *up, struct timeval *sp)
984263bc 535{
585aafb6 536 struct thread *td;
984263bc 537
d16a8831
MD
538 /*
539 * Calculate at the statclock level. YYY if the thread is owned by
540 * another cpu we need to forward the request to the other cpu, or
585aafb6
MD
541 * have a token to interlock the information in order to avoid racing
542 * thread destruction.
d16a8831 543 */
585aafb6
MD
544 if ((td = lp->lwp_thread) != NULL) {
545 crit_enter();
546 up->tv_sec = td->td_uticks / 1000000;
547 up->tv_usec = td->td_uticks % 1000000;
548 sp->tv_sec = td->td_sticks / 1000000;
549 sp->tv_usec = td->td_sticks % 1000000;
550 crit_exit();
551 }
984263bc
MD
552}
553
fde7ac71
SS
554/*
555 * Aggregate resource statistics of all lwps of a process.
556 *
557 * proc.p_ru keeps track of all statistics directly related to a proc. This
558 * consists of RSS usage and nswap information and aggregate numbers for all
559 * former lwps of this proc.
560 *
561 * proc.p_cru is the sum of all stats of reaped children.
562 *
563 * lwp.lwp_ru contains the stats directly related to one specific lwp, meaning
564 * packet, scheduler switch or page fault counts, etc. This information gets
565 * added to lwp.lwp_proc.p_ru when the lwp exits.
566 */
567void
568calcru_proc(struct proc *p, struct rusage *ru)
569{
570 struct timeval upt, spt;
571 long *rip1, *rip2;
572 struct lwp *lp;
573
574 *ru = p->p_ru;
575
576 FOREACH_LWP_IN_PROC(lp, p) {
577 calcru(lp, &upt, &spt);
578 timevaladd(&ru->ru_utime, &upt);
579 timevaladd(&ru->ru_stime, &spt);
580 for (rip1 = &ru->ru_first, rip2 = &lp->lwp_ru.ru_first;
581 rip1 <= &ru->ru_last;
582 rip1++, rip2++)
583 *rip1 += *rip2;
584 }
585}
586
587
3919ced0
MD
588/*
589 * MPALMOSTSAFE
590 */
984263bc 591int
753fd850 592sys_getrusage(struct getrusage_args *uap)
984263bc 593{
fde7ac71 594 struct rusage ru;
41c20dac 595 struct rusage *rup;
3919ced0 596 int error;
984263bc 597
3919ced0 598 get_mplock();
984263bc 599
3919ced0 600 switch (uap->who) {
984263bc 601 case RUSAGE_SELF:
fde7ac71
SS
602 rup = &ru;
603 calcru_proc(curproc, rup);
3919ced0 604 error = 0;
984263bc 605 break;
984263bc 606 case RUSAGE_CHILDREN:
fde7ac71 607 rup = &curproc->p_cru;
3919ced0 608 error = 0;
984263bc 609 break;
984263bc 610 default:
3919ced0
MD
611 error = EINVAL;
612 break;
984263bc 613 }
3919ced0
MD
614 if (error == 0)
615 error = copyout(rup, uap->rusage, sizeof(struct rusage));
616 rel_mplock();
617 return (error);
984263bc
MD
618}
619
620void
792033e7 621ruadd(struct rusage *ru, struct rusage *ru2)
984263bc 622{
1fd87d54
RG
623 long *ip, *ip2;
624 int i;
984263bc
MD
625
626 timevaladd(&ru->ru_utime, &ru2->ru_utime);
627 timevaladd(&ru->ru_stime, &ru2->ru_stime);
628 if (ru->ru_maxrss < ru2->ru_maxrss)
629 ru->ru_maxrss = ru2->ru_maxrss;
630 ip = &ru->ru_first; ip2 = &ru2->ru_first;
631 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
632 *ip++ += *ip2++;
633}
634
635/*
984263bc
MD
636 * Find the uidinfo structure for a uid. This structure is used to
637 * track the total resource consumption (process count, socket buffer
638 * size, etc.) for the uid and impose limits.
639 */
640void
792033e7 641uihashinit(void)
984263bc 642{
9d7a637e 643 spin_init(&uihash_lock);
984263bc
MD
644 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
645}
646
0d355d3b
MD
647/*
648 * NOTE: Must be called with uihash_lock held
649 *
650 * MPSAFE
651 */
984263bc 652static struct uidinfo *
792033e7 653uilookup(uid_t uid)
984263bc
MD
654{
655 struct uihashhead *uipp;
656 struct uidinfo *uip;
657
658 uipp = UIHASH(uid);
792033e7 659 LIST_FOREACH(uip, uipp, ui_hash) {
984263bc
MD
660 if (uip->ui_uid == uid)
661 break;
792033e7 662 }
984263bc
MD
663 return (uip);
664}
665
0d355d3b
MD
666/*
667 * MPSAFE
668 */
984263bc 669static struct uidinfo *
792033e7 670uicreate(uid_t uid)
984263bc 671{
9d7a637e 672 struct uidinfo *uip, *tmp;
f61e468f
MD
673 /*
674 * Allocate space and check for a race
675 */
676 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_WAITOK);
f61e468f
MD
677 /*
678 * Initialize structure and enter it into the hash table
679 */
9d7a637e 680 spin_init(&uip->ui_lock);
984263bc
MD
681 uip->ui_uid = uid;
682 uip->ui_proccnt = 0;
683 uip->ui_sbsize = 0;
9d7a637e 684 uip->ui_ref = 1; /* we're returning a ref */
508ceb09 685 uip->ui_posixlocks = 0;
98a7f915 686 varsymset_init(&uip->ui_varsymset, NULL);
9d7a637e
AE
687
688 /*
689 * Somebody may have already created the uidinfo for this
690 * uid. If so, return that instead.
691 */
692 spin_lock_wr(&uihash_lock);
693 tmp = uilookup(uid);
694 if (tmp != NULL) {
695 varsymset_clean(&uip->ui_varsymset);
696 spin_uninit(&uip->ui_lock);
697 FREE(uip, M_UIDINFO);
698 uip = tmp;
699 } else {
700 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
701 }
702 spin_unlock_wr(&uihash_lock);
703
984263bc
MD
704 return (uip);
705}
706
0d355d3b
MD
707/*
708 * MPSAFE
709 */
984263bc 710struct uidinfo *
792033e7 711uifind(uid_t uid)
984263bc
MD
712{
713 struct uidinfo *uip;
714
9d7a637e 715 spin_lock_rd(&uihash_lock);
984263bc 716 uip = uilookup(uid);
9d7a637e
AE
717 if (uip == NULL) {
718 spin_unlock_rd(&uihash_lock);
984263bc 719 uip = uicreate(uid);
9d7a637e
AE
720 } else {
721 uihold(uip);
722 spin_unlock_rd(&uihash_lock);
723 }
984263bc
MD
724 return (uip);
725}
726
0d355d3b
MD
727/*
728 * MPSAFE
729 */
792033e7
MD
730static __inline void
731uifree(struct uidinfo *uip)
984263bc 732{
9d7a637e
AE
733 spin_lock_wr(&uihash_lock);
734
735 /*
736 * Note that we're taking a read lock even though we
737 * modify the structure because we know nobody can find
738 * it now that we've locked uihash_lock. If somebody
739 * can get to it through a stored pointer, the reference
740 * count will not be 0 and in that case we don't modify
741 * the struct.
742 */
743 spin_lock_rd(&uip->ui_lock);
744 if (uip->ui_ref != 0) {
745 /*
746 * Someone found the uid and got a ref when we
747 * unlocked. No need to free any more.
748 */
749 spin_unlock_rd(&uip->ui_lock);
750 return;
751 }
792033e7
MD
752 if (uip->ui_sbsize != 0)
753 /* XXX no %qd in kernel. Truncate. */
6ea70f76 754 kprintf("freeing uidinfo: uid = %d, sbsize = %ld\n",
792033e7
MD
755 uip->ui_uid, (long)uip->ui_sbsize);
756 if (uip->ui_proccnt != 0)
6ea70f76 757 kprintf("freeing uidinfo: uid = %d, proccnt = %ld\n",
792033e7 758 uip->ui_uid, uip->ui_proccnt);
9d7a637e 759
792033e7 760 LIST_REMOVE(uip, ui_hash);
9d7a637e 761 spin_unlock_wr(&uihash_lock);
98a7f915 762 varsymset_clean(&uip->ui_varsymset);
9d7a637e
AE
763 lockuninit(&uip->ui_varsymset.vx_lock);
764 spin_unlock_rd(&uip->ui_lock);
765 spin_uninit(&uip->ui_lock);
792033e7
MD
766 FREE(uip, M_UIDINFO);
767}
984263bc 768
0d355d3b
MD
769/*
770 * MPSAFE
771 */
792033e7
MD
772void
773uihold(struct uidinfo *uip)
774{
9d7a637e 775 atomic_add_int(&uip->ui_ref, 1);
61f96b6f 776 KKASSERT(uip->ui_ref >= 0);
792033e7
MD
777}
778
0d355d3b
MD
779/*
780 * MPSAFE
781 */
792033e7
MD
782void
783uidrop(struct uidinfo *uip)
784{
61f96b6f 785 KKASSERT(uip->ui_ref > 0);
9d7a637e 786 if (atomic_fetchadd_int(&uip->ui_ref, -1) == 1) {
792033e7 787 uifree(uip);
9d7a637e 788 }
792033e7
MD
789}
790
791void
792uireplace(struct uidinfo **puip, struct uidinfo *nuip)
793{
794 uidrop(*puip);
795 *puip = nuip;
984263bc
MD
796}
797
798/*
799 * Change the count associated with number of processes
800 * a given user is using. When 'max' is 0, don't enforce a limit
801 */
802int
792033e7 803chgproccnt(struct uidinfo *uip, int diff, int max)
984263bc 804{
9d7a637e
AE
805 int ret;
806 spin_lock_wr(&uip->ui_lock);
984263bc 807 /* don't allow them to exceed max, but allow subtraction */
9d7a637e
AE
808 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0) {
809 ret = 0;
810 } else {
811 uip->ui_proccnt += diff;
812 if (uip->ui_proccnt < 0)
813 kprintf("negative proccnt for uid = %d\n", uip->ui_uid);
814 ret = 1;
815 }
816 spin_unlock_wr(&uip->ui_lock);
817 return ret;
984263bc
MD
818}
819
820/*
821 * Change the total socket buffer size a user has used.
822 */
823int
792033e7 824chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t max)
984263bc
MD
825{
826 rlim_t new;
984263bc 827
9d7a637e 828 spin_lock_wr(&uip->ui_lock);
984263bc 829 new = uip->ui_sbsize + to - *hiwat;
9d7a637e 830 KKASSERT(new >= 0);
3a6117bb
MD
831
832 /*
833 * If we are trying to increase the socket buffer size
834 * Scale down the hi water mark when we exceed the user's
835 * allowed socket buffer space.
836 *
837 * We can't scale down too much or we will blow up atomic packet
838 * operations.
839 */
840 if (to > *hiwat && to > MCLBYTES && new > max) {
841 to = to * max / new;
842 if (to < MCLBYTES)
843 to = MCLBYTES;
984263bc
MD
844 }
845 uip->ui_sbsize = new;
846 *hiwat = to;
9d7a637e 847 spin_unlock_wr(&uip->ui_lock);
984263bc
MD
848 return (1);
849}
792033e7 850