2 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
37 * (c) UNIX System Laboratories, Inc.
38 * All or some portions of this file are derived from material licensed
39 * to the University of California by American Telephone and Telegraph
40 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
41 * the permission of UNIX System Laboratories, Inc.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94
68 * $FreeBSD: src/sys/kern/kern_resource.c,v 1.55.2.5 2001/11/03 01:41:08 ps Exp $
69 * $DragonFly: src/sys/kern/kern_plimit.c,v 1.2 2006/09/05 02:55:45 dillon Exp $
72 #include <sys/resource.h>
73 #include <sys/spinlock.h>
76 #include <sys/lockf.h>
77 #include <sys/kern_syscall.h>
79 #include <vm/vm_param.h>
81 #include <vm/vm_map.h>
83 #include <machine/pmap.h>
85 #include <sys/spinlock2.h>
87 static struct plimit
*plimit_copy(struct plimit
*olimit
);
88 static void plimit_exclusive(struct plimit
**limitp
);
91 * Initialize proc0's plimit structure. All later plimit structures
92 * are inherited through fork.
95 plimit_init0(struct plimit
*limit
)
100 for (i
= 0; i
< RLIM_NLIMITS
; ++i
) {
101 limit
->pl_rlimit
[i
].rlim_cur
= RLIM_INFINITY
;
102 limit
->pl_rlimit
[i
].rlim_max
= RLIM_INFINITY
;
104 limit
->pl_rlimit
[RLIMIT_NOFILE
].rlim_cur
= maxfiles
;
105 limit
->pl_rlimit
[RLIMIT_NOFILE
].rlim_max
= maxfiles
;
106 limit
->pl_rlimit
[RLIMIT_NPROC
].rlim_cur
= maxproc
;
107 limit
->pl_rlimit
[RLIMIT_NPROC
].rlim_max
= maxproc
;
108 lim
= ptoa((rlim_t
)vmstats
.v_free_count
);
109 limit
->pl_rlimit
[RLIMIT_RSS
].rlim_max
= lim
;
110 limit
->pl_rlimit
[RLIMIT_MEMLOCK
].rlim_max
= lim
;
111 limit
->pl_rlimit
[RLIMIT_MEMLOCK
].rlim_cur
= lim
/ 3;
112 limit
->p_cpulimit
= RLIM_INFINITY
;
114 spin_init(&limit
->p_spin
);
118 * Return a plimit for use by a new forked process given the one
119 * contained in the parent process. p_exclusive will be set if
120 * the parent process has more then one thread, requiring a copy.
121 * Otherwise we can just inherit a reference.
126 plimit_fork(struct plimit
*limit
)
128 if (limit
->p_exclusive
) {
129 limit
= plimit_copy(limit
);
131 spin_lock_wr(&limit
->p_spin
);
133 spin_unlock_wr(&limit
->p_spin
);
139 * This routine is called when the second LWP is created for a process.
140 * The process's limit structure must be made exclusive and a copy is
143 * If the refcnt is 1 only the LWPs associated with the caller's process
144 * have access to the structure, and since all we do is set the exclusive
145 * but we don't need a spinlock.
151 plimit_exclusive(struct plimit
**limitp
)
153 struct plimit
*olimit
= *limitp
;
154 struct plimit
*nlimit
;
156 if (olimit
->p_refcnt
== 1) {
157 olimit
->p_exclusive
= 1;
159 nlimit
= plimit_copy(olimit
);
160 nlimit
->p_exclusive
= 1;
162 spin_lock_wr(&olimit
->p_spin
);
163 if (--olimit
->p_refcnt
== 0) {
164 spin_unlock_wr(&olimit
->p_spin
);
165 kfree(olimit
, M_SUBPROC
);
167 spin_unlock_wr(&olimit
->p_spin
);
173 * Return a copy of the passed plimit. The returned copy will have a refcnt
174 * of 1 and p_exclusive will be cleared.
180 plimit_copy(struct plimit
*olimit
)
182 struct plimit
*nlimit
;
184 nlimit
= kmalloc(sizeof(struct plimit
), M_SUBPROC
, M_WAITOK
);
186 spin_lock_rd(&olimit
->p_spin
);
188 spin_unlock_rd(&olimit
->p_spin
);
190 spin_init(&nlimit
->p_spin
);
191 nlimit
->p_refcnt
= 1;
192 nlimit
->p_exclusive
= 0;
197 * This routine is called to fixup a proces's p_limit structure prior
198 * to it being modified. If index >= 0 the specified modified is also
201 * A limit structure is potentially shared only if the process has exactly
202 * one LWP, otherwise it is guarenteed to be exclusive and no copy needs
203 * to be made. This means that we can safely replace *limitp in the copy
206 * We call plimit_exclusive() to do all the hard work, but the result does
207 * not actually have to be exclusive since the original was not, so just
208 * clear p_exclusive afterwords.
213 plimit_modify(struct plimit
**limitp
, int index
, struct rlimit
*rlim
)
215 struct plimit
*limit
= *limitp
;
217 if (limit
->p_exclusive
== 0 && limit
->p_refcnt
> 1) {
218 plimit_exclusive(limitp
);
220 limit
->p_exclusive
= 0;
223 spin_lock_wr(&limit
->p_spin
);
224 limit
->pl_rlimit
[index
] = *rlim
;
225 spin_unlock_wr(&limit
->p_spin
);
230 * Destroy a process's plimit structure.
235 plimit_free(struct plimit
**limitp
)
237 struct plimit
*limit
;
239 if ((limit
= *limitp
) != NULL
) {
242 spin_lock_wr(&limit
->p_spin
);
243 if (--limit
->p_refcnt
== 0) {
244 spin_unlock_wr(&limit
->p_spin
);
245 kfree(limit
, M_SUBPROC
);
247 spin_unlock_wr(&limit
->p_spin
);
253 * Modify a resource limit (from system call)
258 kern_setrlimit(u_int which
, struct rlimit
*limp
)
260 struct proc
*p
= curproc
;
261 struct plimit
*limit
;
262 struct rlimit
*alimp
;
265 if (which
>= RLIM_NLIMITS
)
269 * We will be modifying a resource, make a copy if necessary.
271 plimit_modify(&p
->p_limit
, -1, NULL
);
273 alimp
= &limit
->pl_rlimit
[which
];
276 * Preserve historical bugs by treating negative limits as unsigned.
278 if (limp
->rlim_cur
< 0)
279 limp
->rlim_cur
= RLIM_INFINITY
;
280 if (limp
->rlim_max
< 0)
281 limp
->rlim_max
= RLIM_INFINITY
;
283 spin_lock_rd(&limit
->p_spin
);
284 if (limp
->rlim_cur
> alimp
->rlim_max
||
285 limp
->rlim_max
> alimp
->rlim_max
) {
286 spin_unlock_rd(&limit
->p_spin
);
287 if ((error
= suser_cred(p
->p_ucred
, PRISON_ROOT
)))
290 spin_unlock_rd(&limit
->p_spin
);
292 if (limp
->rlim_cur
> limp
->rlim_max
)
293 limp
->rlim_cur
= limp
->rlim_max
;
297 spin_lock_wr(&limit
->p_spin
);
298 if (limp
->rlim_cur
> RLIM_INFINITY
/ (rlim_t
)1000000)
299 limit
->p_cpulimit
= RLIM_INFINITY
;
301 limit
->p_cpulimit
= (rlim_t
)1000000 * limp
->rlim_cur
;
302 spin_unlock_wr(&limit
->p_spin
);
305 if (limp
->rlim_cur
> maxdsiz
)
306 limp
->rlim_cur
= maxdsiz
;
307 if (limp
->rlim_max
> maxdsiz
)
308 limp
->rlim_max
= maxdsiz
;
312 if (limp
->rlim_cur
> maxssiz
)
313 limp
->rlim_cur
= maxssiz
;
314 if (limp
->rlim_max
> maxssiz
)
315 limp
->rlim_max
= maxssiz
;
317 * Stack is allocated to the max at exec time with only
318 * "rlim_cur" bytes accessible. If stack limit is going
319 * up make more accessible, if going down make inaccessible.
321 spin_lock_rd(&limit
->p_spin
);
322 if (limp
->rlim_cur
!= alimp
->rlim_cur
) {
327 if (limp
->rlim_cur
> alimp
->rlim_cur
) {
329 size
= limp
->rlim_cur
- alimp
->rlim_cur
;
330 addr
= USRSTACK
- limp
->rlim_cur
;
333 size
= alimp
->rlim_cur
- limp
->rlim_cur
;
334 addr
= USRSTACK
- alimp
->rlim_cur
;
336 spin_unlock_rd(&limit
->p_spin
);
337 addr
= trunc_page(addr
);
338 size
= round_page(size
);
339 (void) vm_map_protect(&p
->p_vmspace
->vm_map
,
340 addr
, addr
+size
, prot
, FALSE
);
342 spin_unlock_rd(&limit
->p_spin
);
347 if (limp
->rlim_cur
> maxfilesperproc
)
348 limp
->rlim_cur
= maxfilesperproc
;
349 if (limp
->rlim_max
> maxfilesperproc
)
350 limp
->rlim_max
= maxfilesperproc
;
354 if (limp
->rlim_cur
> maxprocperuid
)
355 limp
->rlim_cur
= maxprocperuid
;
356 if (limp
->rlim_max
> maxprocperuid
)
357 limp
->rlim_max
= maxprocperuid
;
358 if (limp
->rlim_cur
< 1)
360 if (limp
->rlim_max
< 1)
363 case RLIMIT_POSIXLOCKS
:
364 if (limp
->rlim_cur
> maxposixlocksperuid
)
365 limp
->rlim_cur
= maxposixlocksperuid
;
366 if (limp
->rlim_max
> maxposixlocksperuid
)
367 limp
->rlim_max
= maxposixlocksperuid
;
370 spin_lock_wr(&limit
->p_spin
);
372 spin_unlock_wr(&limit
->p_spin
);
377 * The rlimit indexed by which is returned in the second argument.
382 kern_getrlimit(u_int which
, struct rlimit
*limp
)
384 struct proc
*p
= curproc
;
385 struct plimit
*limit
;
387 if (which
>= RLIM_NLIMITS
)
391 spin_lock_rd(&limit
->p_spin
);
392 *limp
= p
->p_rlimit
[which
];
393 spin_unlock_rd(&limit
->p_spin
);
398 * Determine if the cpu limit has been reached and return an operations
399 * code for the caller to perform.
404 plimit_testcpulimit(struct plimit
*limit
, u_int64_t ttime
)
409 mode
= PLIMIT_TESTCPU_OK
;
410 if (limit
->p_cpulimit
!= RLIM_INFINITY
) {
411 spin_lock_rd(&limit
->p_spin
);
412 if (ttime
> limit
->p_cpulimit
) {
413 rlim
= &limit
->pl_rlimit
[RLIMIT_CPU
];
414 if (ttime
/ (rlim_t
)1000000 >= rlim
->rlim_max
+ 5) {
415 mode
= PLIMIT_TESTCPU_KILL
;
417 mode
= PLIMIT_TESTCPU_XCPU
;
420 spin_unlock_rd(&limit
->p_spin
);