sys/kern/subr_param.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | /* * Copyright (c) 1980, 1986, 1989, 1993 * The Regents of the University of California. All rights reserved. * (c) UNIX System Laboratories, Inc. * All or some portions of this file are derived from material licensed * to the University of California by American Telephone and Telegraph * Co. or Unix System Laboratories, Inc. and are reproduced herein with * the permission of UNIX System Laboratories, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)param.c 8.3 (Berkeley) 8/20/94 * $FreeBSD: src/sys/kern/subr_param.c,v 1.42.2.10 2002/03/09 21:05:47 silby Exp $ */ #include "opt_param.h" #include "opt_maxusers.h" #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/malloc.h> #include <sys/sysctl.h> #include <vm/pmap.h> #include <machine/vmparam.h> /* * System parameter formulae. */ #ifndef HZ_DEFAULT #define HZ_DEFAULT 100 #endif #define NPROC (20 + 16 * maxusers) #ifndef NBUF #define NBUF 0 #endif #ifndef MAXFILES #define MAXFILES (maxproc * 16) #endif #ifndef MAXPOSIXLOCKSPERUID #define MAXPOSIXLOCKSPERUID (maxproc * 4) #endif static int sysctl_kern_vmm_guest(SYSCTL_HANDLER_ARGS); int hz; int stathz; int profhz; int ustick; /* tick interval in microseconds */ int nstick; /* tick interval in nanoseconds */ int maxusers; /* base tunable */ int maxproc; /* maximum # of processes */ int maxprocperuid; /* max # of procs per user */ int maxfiles; /* system wide open files limit */ int maxfilesrootres; /* descriptors reserved for root use */ int minfilesperproc; /* per-proc min open files (safety) */ int maxfilesperproc; /* per-proc open files limit */ int maxfilesperuser; /* per-user open files limit */ int maxposixlocksperuid; /* max # POSIX locks per uid */ int ncallout; /* maximum # of timer events */ long nbuf; long nswbuf_mem; long nswbuf_kva; long nswbuf_raw; long maxswzone; /* max swmeta KVA storage */ long maxbcache; /* max buffer cache KVA storage */ enum vmm_guest_type vmm_guest = VMM_GUEST_NONE; /* Running as VM guest? */ u_quad_t maxtsiz; /* max text size */ u_quad_t dfldsiz; /* initial data size limit */ u_quad_t maxdsiz; /* max data size */ u_quad_t dflssiz; /* initial stack size limit */ u_quad_t maxssiz; /* max stack size */ u_quad_t sgrowsiz; /* amount to grow stack */ u_quad_t maxthrssiz; /* thread stack area */ SYSCTL_PROC(_kern, OID_AUTO, vmm_guest, CTLFLAG_RD | CTLTYPE_STRING, NULL, 0, sysctl_kern_vmm_guest, "A", "Virtual machine guest type"); SYSCTL_QUAD(_kern, OID_AUTO, maxssiz, CTLFLAG_RD, &maxssiz, 0, "Maximum user stack size"); SYSCTL_QUAD(_kern, OID_AUTO, maxthrssiz, CTLFLAG_RD, &maxthrssiz, 0, "Nominal threading stack area"); /* * These have to be allocated somewhere; allocating * them here forces loader errors if this file is omitted * (if they've been externed everywhere else; hah!). */ struct buf *swbuf_mem; struct buf *swbuf_kva; struct buf *swbuf_raw; struct vmm_bname { const char *str; enum vmm_guest_type type; }; static struct vmm_bname vmm_bnames[] = { { "QEMU", VMM_GUEST_QEMU }, /* QEMU */ { "Plex86", VMM_GUEST_PLEX86 }, /* Plex86 */ { "Bochs", VMM_GUEST_BOCHS }, /* Bochs */ { "Xen", VMM_GUEST_XEN }, /* Xen */ { "BHYVE", VMM_GUEST_BHYVE }, /* bhyve */ { "Seabios", VMM_GUEST_KVM}, /* KVM */ { NULL, 0 } }; static struct vmm_bname vmm_pnames[] = { { "VMware Virtual Platform", VMM_GUEST_VMWARE }, /* VMWare VM */ { "Virtual Machine", VMM_GUEST_HYPERV }, /* MS Hyper-V */ { "VirtualBox", VMM_GUEST_VBOX }, /* Sun VirtualBox */ { "Parallels Virtual Platform", VMM_GUEST_PARALLELS }, /* Parallels VM */ { "KVM", VMM_GUEST_KVM }, /* KVM */ { NULL, 0 } }; static const char *const vmm_guest_sysctl_names[] = { "none", "qemu", "plex86", "bochs", "xen", "bhyve", "kvm", "vmware", "hyperv", "vbox", "parallels", "vkernel", "nvmm", "unknown", NULL }; CTASSERT(NELEM(vmm_guest_sysctl_names) - 1 == VMM_GUEST_LAST); char vmm_vendor[16]; SYSCTL_STRING(_kern, OID_AUTO, vmm_vendor, CTLFLAG_RD, vmm_vendor, 0, "Virtual machine vendor"); /* * Detect known Virtual Machine hosts by inspecting the emulated BIOS. */ enum vmm_guest_type detect_virtual(void) { char *sysenv; int i; sysenv = kgetenv("smbios.bios.vendor"); if (sysenv != NULL) { for (i = 0; vmm_bnames[i].str != NULL; i++) if (strcmp(sysenv, vmm_bnames[i].str) == 0) { kfreeenv(sysenv); return (vmm_bnames[i].type); } kfreeenv(sysenv); } sysenv = kgetenv("smbios.system.product"); if (sysenv != NULL) { for (i = 0; vmm_pnames[i].str != NULL; i++) if (strcmp(sysenv, vmm_pnames[i].str) == 0) { kfreeenv(sysenv); return (vmm_pnames[i].type); } kfreeenv(sysenv); } return (VMM_GUEST_NONE); } /* * Boot time overrides that are not scaled against main memory */ void init_param1(void) { hz = HZ_DEFAULT; TUNABLE_INT_FETCH("kern.hz", &hz); stathz = hz + 1; TUNABLE_INT_FETCH("kern.stathz", &stathz); profhz = stathz; ustick = 1000000 / hz; nstick = 1000000000 / hz; /* can adjust 30ms in 60s */ ntp_default_tick_delta = howmany(30000000, 60 * hz); #ifdef VM_SWZONE_SIZE_MAX maxswzone = VM_SWZONE_SIZE_MAX; #endif TUNABLE_LONG_FETCH("kern.maxswzone", &maxswzone); #ifdef VM_BCACHE_SIZE_MAX maxbcache = VM_BCACHE_SIZE_MAX; #endif TUNABLE_LONG_FETCH("kern.maxbcache", &maxbcache); maxtsiz = MAXTSIZ; TUNABLE_QUAD_FETCH("kern.maxtsiz", &maxtsiz); dfldsiz = DFLDSIZ; TUNABLE_QUAD_FETCH("kern.dfldsiz", &dfldsiz); maxdsiz = MAXDSIZ; TUNABLE_QUAD_FETCH("kern.maxdsiz", &maxdsiz); dflssiz = DFLSSIZ; TUNABLE_QUAD_FETCH("kern.dflssiz", &dflssiz); maxssiz = MAXSSIZ; TUNABLE_QUAD_FETCH("kern.maxssiz", &maxssiz); sgrowsiz = SGROWSIZ; TUNABLE_QUAD_FETCH("kern.sgrowsiz", &sgrowsiz); maxthrssiz = MAXTHRSSIZ; TUNABLE_QUAD_FETCH("kern.maxthrssiz", &maxthrssiz); } /* * Boot time overrides that are scaled against main memory */ void init_param2(int physpages) { size_t limsize; /* * Calculate manually becaus the VM page queues / system is not set * up yet. */ limsize = (size_t)physpages * PAGE_SIZE; if (limsize > KvaSize) limsize = KvaSize; if (maxswzone > limsize / 2) /* maxswzone size (1/2 of phys mem) */ maxswzone = limsize / 2; limsize /= 1024 * 1024; /* smaller of KVM or physmem in MB */ /* Base parameters */ maxusers = MAXUSERS; TUNABLE_INT_FETCH("kern.maxusers", &maxusers); if (maxusers == 0) { maxusers = limsize / 8; /* ~384 per 3G */ if (maxusers < 32) maxusers = 32; /* no upper limit */ } /* * The following can be overridden after boot via sysctl. Note: * unless overridden, these macros are ultimately based on maxusers. * * Limit maxproc so that kmap entries cannot be exhausted by * processes. This limitation can be a bit problematic because * processes can have a wide range of complexity. */ maxproc = NPROC; TUNABLE_INT_FETCH("kern.maxproc", &maxproc); if (maxproc < 32) maxproc = 32; if (maxproc > limsize * 40) maxproc = limsize * 40; /* * Maximum number of open files */ maxfiles = MAXFILES; TUNABLE_INT_FETCH("kern.maxfiles", &maxfiles); if (maxfiles < 128) maxfiles = 128; /* * Limit file descriptors so no single user can exhaust the * system. * * WARNING: Do not set minfilesperproc too high or the user * can exhaust the system with a combination of fork() * and open(). Actual worst case is: * * (minfilesperproc * maxprocperuid) + maxfilesperuser */ maxprocperuid = maxproc / 4; if (maxprocperuid < 128) maxprocperuid = maxproc / 2; minfilesperproc = 8; maxfilesperproc = maxfiles / 4; maxfilesperuser = maxfilesperproc * 2; maxfilesrootres = maxfiles / 20; /* * Severe hack to try to prevent pipe() descriptors from * blowing away kernel memory. */ if (KvaSize <= (vm_offset_t)(1536LL * 1024 * 1024) && maxfilesperuser > 20000) { maxfilesperuser = 20000; } maxposixlocksperuid = MAXPOSIXLOCKSPERUID; TUNABLE_INT_FETCH("kern.maxposixlocksperuid", &maxposixlocksperuid); /* * Unless overridden, NBUF is typically 0 (auto-sized later). */ nbuf = NBUF; TUNABLE_LONG_FETCH("kern.nbuf", &nbuf); /* * Calculate the size of the callout wheel. Limit to approximately * 5 minutes worth of table (maxproc would have to be pretty huge), * as more is not likely to gain us anything. */ ncallout = 16 + maxproc + maxfiles; if (ncallout > 5*60*hz) ncallout = 5*60*hz; TUNABLE_INT_FETCH("kern.ncallout", &ncallout); } /* * Sysctl stringifying handler for kern.vmm_guest. */ static int sysctl_kern_vmm_guest(SYSCTL_HANDLER_ARGS) { return (SYSCTL_OUT(req, vmm_guest_sysctl_names[vmm_guest], strlen(vmm_guest_sysctl_names[vmm_guest]))); } |