xref: /dragonfly/sys/kern/subr_param.c (revision 8a0bcd56)
1 /*
2  * Copyright (c) 1980, 1986, 1989, 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  *	@(#)param.c	8.3 (Berkeley) 8/20/94
39  * $FreeBSD: src/sys/kern/subr_param.c,v 1.42.2.10 2002/03/09 21:05:47 silby Exp $
40  * $DragonFly: src/sys/kern/subr_param.c,v 1.7 2005/06/26 22:03:22 dillon Exp $
41  */
42 
43 #include "opt_param.h"
44 #include "opt_maxusers.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <vm/pmap.h>
50 #include <machine/vmparam.h>
51 
52 /*
53  * System parameter formulae.
54  */
55 
56 #ifndef HZ
57 #define	HZ 100
58 #endif
59 #define	NPROC (20 + 16 * maxusers)
60 #ifndef NBUF
61 #define NBUF 0
62 #endif
63 #ifndef MAXFILES
64 #define	MAXFILES (maxproc * 16)
65 #endif
66 #ifndef MAXPOSIXLOCKSPERUID
67 #define MAXPOSIXLOCKSPERUID (maxusers * 64) /* Should be a safe value */
68 #endif
69 
70 int	hz;
71 int	stathz;
72 int	profhz;
73 int	ustick;				/* tick interval in microseconds */
74 int	nstick;				/* tick interval in nanoseconds */
75 int	maxusers;			/* base tunable */
76 int	maxproc;			/* maximum # of processes */
77 int	maxprocperuid;			/* max # of procs per user */
78 int	maxfiles;			/* system wide open files limit */
79 int	maxfilesrootres;		/* descriptors reserved for root use */
80 int	minfilesperproc;		/* per-proc min open files (safety) */
81 int	maxfilesperproc;		/* per-proc open files limit */
82 int	maxfilesperuser;		/* per-user open files limit */
83 int	maxposixlocksperuid;		/* max # POSIX locks per uid */
84 int	ncallout;			/* maximum # of timer events */
85 int	mbuf_wait = 32;			/* mbuf sleep time in ticks */
86 int	nbuf;
87 int	nswbuf;
88 long	maxswzone;			/* max swmeta KVA storage */
89 long	maxbcache;			/* max buffer cache KVA storage */
90 u_quad_t	maxtsiz;			/* max text size */
91 u_quad_t	dfldsiz;			/* initial data size limit */
92 u_quad_t	maxdsiz;			/* max data size */
93 u_quad_t	dflssiz;			/* initial stack size limit */
94 u_quad_t	maxssiz;			/* max stack size */
95 u_quad_t	sgrowsiz;			/* amount to grow stack */
96 
97 /*
98  * These have to be allocated somewhere; allocating
99  * them here forces loader errors if this file is omitted
100  * (if they've been externed everywhere else; hah!).
101  */
102 struct	buf *swbuf;
103 
104 /*
105  * Boot time overrides that are not scaled against main memory
106  */
107 void
108 init_param1(void)
109 {
110 	hz = HZ;
111 	TUNABLE_INT_FETCH("kern.hz", &hz);
112 	stathz = hz * 128 / 100;
113 	profhz = stathz;
114 	ustick = 1000000 / hz;
115 	nstick = 1000000000 / hz;
116 	/* can adjust 30ms in 60s */
117 	ntp_default_tick_delta = howmany(30000000, 60 * hz);
118 
119 #ifdef VM_SWZONE_SIZE_MAX
120 	maxswzone = VM_SWZONE_SIZE_MAX;
121 #endif
122 	TUNABLE_LONG_FETCH("kern.maxswzone", &maxswzone);
123 #ifdef VM_BCACHE_SIZE_MAX
124 	maxbcache = VM_BCACHE_SIZE_MAX;
125 #endif
126 	TUNABLE_LONG_FETCH("kern.maxbcache", &maxbcache);
127 	maxtsiz = MAXTSIZ;
128 	TUNABLE_QUAD_FETCH("kern.maxtsiz", &maxtsiz);
129 	dfldsiz = DFLDSIZ;
130 	TUNABLE_QUAD_FETCH("kern.dfldsiz", &dfldsiz);
131 	maxdsiz = MAXDSIZ;
132 	TUNABLE_QUAD_FETCH("kern.maxdsiz", &maxdsiz);
133 	dflssiz = DFLSSIZ;
134 	TUNABLE_QUAD_FETCH("kern.dflssiz", &dflssiz);
135 	maxssiz = MAXSSIZ;
136 	TUNABLE_QUAD_FETCH("kern.maxssiz", &maxssiz);
137 	sgrowsiz = SGROWSIZ;
138 	TUNABLE_QUAD_FETCH("kern.sgrowsiz", &sgrowsiz);
139 }
140 
141 /*
142  * Boot time overrides that are scaled against main memory
143  */
144 void
145 init_param2(int physpages)
146 {
147 
148 	/* Base parameters */
149 	maxusers = MAXUSERS;
150 	TUNABLE_INT_FETCH("kern.maxusers", &maxusers);
151 	if (maxusers == 0) {
152 		maxusers = physpages / (2 * 1024 * 1024 / PAGE_SIZE);
153 		if (maxusers < 32)
154 		    maxusers = 32;
155 		if (maxusers > 384)
156 		    maxusers = 384;
157 	}
158 
159 	/*
160 	 * The following can be overridden after boot via sysctl.  Note:
161 	 * unless overriden, these macros are ultimately based on maxusers.
162 	 */
163 	maxproc = NPROC;
164 	TUNABLE_INT_FETCH("kern.maxproc", &maxproc);
165 
166 	/*
167 	 * Limit maxproc so that kmap entries cannot be exhausted by
168 	 * processes.
169 	 */
170 	if (maxproc > (physpages / 12))
171 		maxproc = physpages / 12;
172 	maxfiles = MAXFILES;
173 	TUNABLE_INT_FETCH("kern.maxfiles", &maxfiles);
174 	if (maxfiles < 128)
175 		maxfiles = 128;
176 
177 	/*
178 	 * Limit file descriptors so no single user can exhaust the
179 	 * system.
180 	 *
181 	 * WARNING: Do not set minfilesperproc too high or the user
182 	 *	    can exhaust the system with a combination of fork()
183 	 *	    and open().  Actual worst case is:
184 	 *
185 	 *	    (minfilesperproc * maxprocperuid) + maxfilesperuser
186 	 */
187 	maxprocperuid = maxproc / 4;
188 	if (maxprocperuid < 128)
189 		maxprocperuid = maxproc / 2;
190 	minfilesperproc = 8;
191 	maxfilesperproc = maxfiles / 4;
192 	maxfilesperuser = maxfilesperproc * 2;
193 	maxfilesrootres = maxfiles / 20;
194 
195 	/*
196 	 * Severe hack to try to prevent pipe() descriptors from
197 	 * blowing away kernel memory.
198 	 */
199 	if (KvaSize <= (vm_offset_t)(1536LL * 1024 * 1024) &&
200 	    maxfilesperuser > 20000) {
201 		maxfilesperuser = 20000;
202 	}
203 
204 	maxposixlocksperuid = MAXPOSIXLOCKSPERUID;
205 	TUNABLE_INT_FETCH("kern.maxposixlocksperuid", &maxposixlocksperuid);
206 
207 	/*
208 	 * Unless overriden, NBUF is typically 0 (auto-sized later).
209 	 */
210 	nbuf = NBUF;
211 	TUNABLE_INT_FETCH("kern.nbuf", &nbuf);
212 
213 	ncallout = 16 + maxproc + maxfiles;
214 	TUNABLE_INT_FETCH("kern.ncallout", &ncallout);
215 }
216 
217