1139804Simp/*- 21541Srgrimes * Copyright (c) 1980, 1986, 1989, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * (c) UNIX System Laboratories, Inc. 51541Srgrimes * All or some portions of this file are derived from material licensed 61541Srgrimes * to the University of California by American Telephone and Telegraph 71541Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 81541Srgrimes * the permission of UNIX System Laboratories, Inc. 91541Srgrimes * 101541Srgrimes * Redistribution and use in source and binary forms, with or without 111541Srgrimes * modification, are permitted provided that the following conditions 121541Srgrimes * are met: 131541Srgrimes * 1. Redistributions of source code must retain the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer. 151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 161541Srgrimes * notice, this list of conditions and the following disclaimer in the 171541Srgrimes * documentation and/or other materials provided with the distribution. 181541Srgrimes * 4. Neither the name of the University nor the names of its contributors 191541Srgrimes * may be used to endorse or promote products derived from this software 201541Srgrimes * without specific prior written permission. 211541Srgrimes * 221541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 231541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 241541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 251541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 261541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 271541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 281541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 291541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 301541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 311541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 321541Srgrimes * SUCH DAMAGE. 331541Srgrimes * 3414526Shsu * @(#)param.c 8.3 (Berkeley) 8/20/94 351541Srgrimes */ 361541Srgrimes 37116182Sobrien#include <sys/cdefs.h> 38116182Sobrien__FBSDID("$FreeBSD$"); 39116182Sobrien 4014328Speter#include "opt_param.h" 41217688Spluknet#include "opt_msgbuf.h" 4280418Speter#include "opt_maxusers.h" 4313226Swollman 441541Srgrimes#include <sys/param.h> 4580418Speter#include <sys/systm.h> 4680418Speter#include <sys/kernel.h> 47239582Skib#include <sys/limits.h> 48239582Skib#include <sys/msgbuf.h> 49172696Salfred#include <sys/sysctl.h> 50239582Skib#include <sys/proc.h> 511541Srgrimes 52219920Salc#include <vm/vm.h> 53137393Sdes#include <vm/vm_param.h> 54219920Salc#include <vm/pmap.h> 5584783Sps 561541Srgrimes/* 571541Srgrimes * System parameter formulae. 581541Srgrimes */ 591541Srgrimes 601541Srgrimes#ifndef HZ 61209492Snwhitehorn# if defined(__mips__) || defined(__arm__) 62209492Snwhitehorn# define HZ 100 63209492Snwhitehorn# else 64137307Sphk# define HZ 1000 65138214Sbms# endif 66184323Ssobomax# ifndef HZ_VM 67195430Ssilby# define HZ_VM 100 68184323Ssobomax# endif 69184323Ssobomax#else 70184323Ssobomax# ifndef HZ_VM 71184323Ssobomax# define HZ_VM HZ 72184323Ssobomax# endif 731541Srgrimes#endif 7480418Speter#define NPROC (20 + 16 * maxusers) 7580418Speter#ifndef NBUF 7680418Speter#define NBUF 0 7780418Speter#endif 7845515Sdes#ifndef MAXFILES 7980418Speter#define MAXFILES (maxproc * 2) 8045515Sdes#endif 818747Sdg 82186286Sivorasstatic int sysctl_kern_vm_guest(SYSCTL_HANDLER_ARGS); 83186286Sivoras 8480418Speterint hz; 8580418Speterint tick; 8680418Speterint maxusers; /* base tunable */ 8780418Speterint maxproc; /* maximum # of processes */ 8880418Speterint maxprocperuid; /* max # of procs per user */ 8980418Speterint maxfiles; /* sys. wide open files limit */ 9080418Speterint maxfilesperproc; /* per-proc open files limit */ 91217688Spluknetint msgbufsize; /* size of kernel message buffer */ 9280418Speterint ncallout; /* maximum # of timer events */ 9380418Speterint nbuf; 94251897Sscottlint bio_transient_maxcnt; 95202143Sbrooksint ngroups_max; /* max # groups per process */ 9680418Speterint nswbuf; 97239582Skibpid_t pid_max = PID_MAX; 98189595Sjhblong maxswzone; /* max swmeta KVA storage */ 99189595Sjhblong maxbcache; /* max buffer cache KVA storage */ 100189649Sjhblong maxpipekva; /* Limit on pipe KVA */ 101186286Sivorasint vm_guest; /* Running as virtual machine guest? */ 102137393Sdesu_long maxtsiz; /* max text size */ 103137393Sdesu_long dfldsiz; /* initial data size limit */ 104137393Sdesu_long maxdsiz; /* max data size */ 105137393Sdesu_long dflssiz; /* initial stack size limit */ 106137393Sdesu_long maxssiz; /* max stack size */ 107137393Sdesu_long sgrowsiz; /* amount to grow stack */ 1081541Srgrimes 109190331SjhbSYSCTL_INT(_kern, OID_AUTO, hz, CTLFLAG_RDTUN, &hz, 0, 110190331Sjhb "Number of clock ticks per second"); 111189744SjhbSYSCTL_INT(_kern, OID_AUTO, ncallout, CTLFLAG_RDTUN, &ncallout, 0, 112189744Sjhb "Number of pre-allocated timer events"); 113189744SjhbSYSCTL_INT(_kern, OID_AUTO, nbuf, CTLFLAG_RDTUN, &nbuf, 0, 114190331Sjhb "Number of buffers in the buffer cache"); 115189744SjhbSYSCTL_INT(_kern, OID_AUTO, nswbuf, CTLFLAG_RDTUN, &nswbuf, 0, 116189744Sjhb "Number of swap buffers"); 117217688SpluknetSYSCTL_INT(_kern, OID_AUTO, msgbufsize, CTLFLAG_RDTUN, &msgbufsize, 0, 118217688Spluknet "Size of the kernel message buffer"); 119189745SjhbSYSCTL_LONG(_kern, OID_AUTO, maxswzone, CTLFLAG_RDTUN, &maxswzone, 0, 120190331Sjhb "Maximum memory for swap metadata"); 121189745SjhbSYSCTL_LONG(_kern, OID_AUTO, maxbcache, CTLFLAG_RDTUN, &maxbcache, 0, 122190331Sjhb "Maximum value of vfs.maxbufspace"); 123251897SscottlSYSCTL_INT(_kern, OID_AUTO, bio_transient_maxcnt, CTLFLAG_RDTUN, 124251897Sscottl &bio_transient_maxcnt, 0, 125251897Sscottl "Maximum number of transient BIOs mappings"); 126240501SzontSYSCTL_ULONG(_kern, OID_AUTO, maxtsiz, CTLFLAG_RW | CTLFLAG_TUN, &maxtsiz, 0, 127190331Sjhb "Maximum text size"); 128240501SzontSYSCTL_ULONG(_kern, OID_AUTO, dfldsiz, CTLFLAG_RW | CTLFLAG_TUN, &dfldsiz, 0, 129190331Sjhb "Initial data size limit"); 130240501SzontSYSCTL_ULONG(_kern, OID_AUTO, maxdsiz, CTLFLAG_RW | CTLFLAG_TUN, &maxdsiz, 0, 131190331Sjhb "Maximum data size"); 132240501SzontSYSCTL_ULONG(_kern, OID_AUTO, dflssiz, CTLFLAG_RW | CTLFLAG_TUN, &dflssiz, 0, 133190331Sjhb "Initial stack size limit"); 134240501SzontSYSCTL_ULONG(_kern, OID_AUTO, maxssiz, CTLFLAG_RW | CTLFLAG_TUN, &maxssiz, 0, 135190331Sjhb "Maximum stack size"); 136240501SzontSYSCTL_ULONG(_kern, OID_AUTO, sgrowsiz, CTLFLAG_RW | CTLFLAG_TUN, &sgrowsiz, 0, 137190331Sjhb "Amount to grow stack on a stack fault"); 138186286SivorasSYSCTL_PROC(_kern, OID_AUTO, vm_guest, CTLFLAG_RD | CTLTYPE_STRING, 139186286Sivoras NULL, 0, sysctl_kern_vm_guest, "A", 140204611Sivoras "Virtual machine guest detected? (none|generic|xen)"); 141172696Salfred 1421541Srgrimes/* 1431541Srgrimes * These have to be allocated somewhere; allocating 1441541Srgrimes * them here forces loader errors if this file is omitted 1451541Srgrimes * (if they've been externed everywhere else; hah!). 1461541Srgrimes */ 1479759Sbdestruct buf *swbuf; 14867046Sjasone 149204420Salc/* 150204420Salc * The elements of this array are ordered based upon the values of the 151204420Salc * corresponding enum VM_GUEST members. 152204420Salc */ 153186522Sbzstatic const char *const vm_guest_sysctl_names[] = { 154186522Sbz "none", 155186522Sbz "generic", 156186522Sbz "xen", 157186522Sbz NULL 158186522Sbz}; 159186522Sbz 160186522Sbz#ifndef XEN 161185772Sjkimstatic const char *const vm_bnames[] = { 162185772Sjkim "QEMU", /* QEMU */ 163185772Sjkim "Plex86", /* Plex86 */ 164185772Sjkim "Bochs", /* Bochs */ 165210935Scsjp "Xen", /* Xen */ 166253250Sgrehan "BHYVE", /* bhyve */ 167246400Szont "Seabios", /* KVM */ 168185772Sjkim NULL 169185772Sjkim}; 170185772Sjkim 171184326Ssobomaxstatic const char *const vm_pnames[] = { 172184323Ssobomax "VMware Virtual Platform", /* VMWare VM */ 173184323Ssobomax "Virtual Machine", /* Microsoft VirtualPC */ 174184323Ssobomax "VirtualBox", /* Sun xVM VirtualBox */ 175184323Ssobomax "Parallels Virtual Platform", /* Parallels VM */ 176246400Szont "KVM", /* KVM */ 177184323Ssobomax NULL 178184323Ssobomax}; 179184323Ssobomax 180186286Sivoras 181186286Sivoras/* 182186286Sivoras * Detect known Virtual Machine hosts by inspecting the emulated BIOS. 183186286Sivoras */ 184186252Sivorasstatic enum VM_GUEST 185184323Ssobomaxdetect_virtual(void) 186184323Ssobomax{ 187184323Ssobomax char *sysenv; 188184323Ssobomax int i; 189184323Ssobomax 190185772Sjkim sysenv = getenv("smbios.bios.vendor"); 191185772Sjkim if (sysenv != NULL) { 192185772Sjkim for (i = 0; vm_bnames[i] != NULL; i++) 193185772Sjkim if (strcmp(sysenv, vm_bnames[i]) == 0) { 194185772Sjkim freeenv(sysenv); 195186252Sivoras return (VM_GUEST_VM); 196185772Sjkim } 197185772Sjkim freeenv(sysenv); 198185772Sjkim } 199184323Ssobomax sysenv = getenv("smbios.system.product"); 200184323Ssobomax if (sysenv != NULL) { 201185772Sjkim for (i = 0; vm_pnames[i] != NULL; i++) 202185772Sjkim if (strcmp(sysenv, vm_pnames[i]) == 0) { 203185772Sjkim freeenv(sysenv); 204186252Sivoras return (VM_GUEST_VM); 205185772Sjkim } 206185772Sjkim freeenv(sysenv); 207184323Ssobomax } 208186252Sivoras return (VM_GUEST_NO); 209184323Ssobomax} 210186522Sbz#endif 211184323Ssobomax 21267046Sjasone/* 21387546Sdillon * Boot time overrides that are not scaled against main memory 21480418Speter */ 21580418Spetervoid 21687546Sdilloninit_param1(void) 21780418Speter{ 218186252Sivoras#ifndef XEN 219186252Sivoras vm_guest = detect_virtual(); 220186252Sivoras#else 221186252Sivoras vm_guest = VM_GUEST_XEN; 222186252Sivoras#endif 223184323Ssobomax hz = -1; 22480418Speter TUNABLE_INT_FETCH("kern.hz", &hz); 225185772Sjkim if (hz == -1) 226186252Sivoras hz = vm_guest > VM_GUEST_NO ? HZ_VM : HZ; 22780418Speter tick = 1000000 / hz; 22880418Speter 22981986Sdillon#ifdef VM_SWZONE_SIZE_MAX 23081933Sdillon maxswzone = VM_SWZONE_SIZE_MAX; 23181986Sdillon#endif 232189595Sjhb TUNABLE_LONG_FETCH("kern.maxswzone", &maxswzone); 23381986Sdillon#ifdef VM_BCACHE_SIZE_MAX 23481933Sdillon maxbcache = VM_BCACHE_SIZE_MAX; 23581986Sdillon#endif 236189595Sjhb TUNABLE_LONG_FETCH("kern.maxbcache", &maxbcache); 237217688Spluknet msgbufsize = MSGBUF_SIZE; 238217688Spluknet TUNABLE_INT_FETCH("kern.msgbufsize", &msgbufsize); 23984783Sps 24084783Sps maxtsiz = MAXTSIZ; 241137393Sdes TUNABLE_ULONG_FETCH("kern.maxtsiz", &maxtsiz); 24284783Sps dfldsiz = DFLDSIZ; 243137393Sdes TUNABLE_ULONG_FETCH("kern.dfldsiz", &dfldsiz); 24484783Sps maxdsiz = MAXDSIZ; 245137393Sdes TUNABLE_ULONG_FETCH("kern.maxdsiz", &maxdsiz); 24684783Sps dflssiz = DFLSSIZ; 247137393Sdes TUNABLE_ULONG_FETCH("kern.dflssiz", &dflssiz); 24884783Sps maxssiz = MAXSSIZ; 249137393Sdes TUNABLE_ULONG_FETCH("kern.maxssiz", &maxssiz); 25084783Sps sgrowsiz = SGROWSIZ; 251137393Sdes TUNABLE_ULONG_FETCH("kern.sgrowsiz", &sgrowsiz); 252202143Sbrooks 253202143Sbrooks /* 254202143Sbrooks * Let the administrator set {NGROUPS_MAX}, but disallow values 255202143Sbrooks * less than NGROUPS_MAX which would violate POSIX.1-2008 or 256202143Sbrooks * greater than INT_MAX-1 which would result in overflow. 257202143Sbrooks */ 258202143Sbrooks ngroups_max = NGROUPS_MAX; 259202143Sbrooks TUNABLE_INT_FETCH("kern.ngroups", &ngroups_max); 260202143Sbrooks if (ngroups_max < NGROUPS_MAX) 261202143Sbrooks ngroups_max = NGROUPS_MAX; 262239582Skib 263239582Skib /* 264239582Skib * Only allow to lower the maximal pid. 265239582Skib * Prevent setting up a non-bootable system if pid_max is too low. 266239582Skib */ 267239582Skib TUNABLE_INT_FETCH("kern.pid_max", &pid_max); 268239582Skib if (pid_max > PID_MAX) 269239582Skib pid_max = PID_MAX; 270239582Skib else if (pid_max < 300) 271239582Skib pid_max = 300; 272251897Sscottl 273251897Sscottl TUNABLE_INT_FETCH("vfs.unmapped_buf_allowed", &unmapped_buf_allowed); 27480418Speter} 27587546Sdillon 27687546Sdillon/* 27787546Sdillon * Boot time overrides that are scaled against main memory 27887546Sdillon */ 27987546Sdillonvoid 280102600Speterinit_param2(long physpages) 28187546Sdillon{ 28287546Sdillon 28387546Sdillon /* Base parameters */ 28490274Sdillon maxusers = MAXUSERS; 28590274Sdillon TUNABLE_INT_FETCH("kern.maxusers", &maxusers); 28690274Sdillon if (maxusers == 0) { 28789769Sdillon maxusers = physpages / (2 * 1024 * 1024 / PAGE_SIZE); 28887546Sdillon if (maxusers < 32) 28987546Sdillon maxusers = 32; 290254515Sandre#ifdef VM_MAX_AUTOTUNE_MAXUSERS 291254515Sandre if (maxusers > VM_MAX_AUTOTUNE_MAXUSERS) 292254515Sandre maxusers = VM_MAX_AUTOTUNE_MAXUSERS; 293254515Sandre#endif 294254515Sandre /* 295254515Sandre * Scales down the function in which maxusers grows once 296254515Sandre * we hit 384. 297254515Sandre */ 298254515Sandre if (maxusers > 384) 299254515Sandre maxusers = 384 + ((maxusers - 384) / 8); 300254515Sandre } 30187546Sdillon 30287546Sdillon /* 30387546Sdillon * The following can be overridden after boot via sysctl. Note: 30487546Sdillon * unless overriden, these macros are ultimately based on maxusers. 305254515Sandre * Limit maxproc so that kmap entries cannot be exhausted by 306254515Sandre * processes. 30787546Sdillon */ 30887546Sdillon maxproc = NPROC; 30987546Sdillon TUNABLE_INT_FETCH("kern.maxproc", &maxproc); 310254515Sandre if (maxproc > (physpages / 12)) 311254515Sandre maxproc = physpages / 12; 312254515Sandre maxprocperuid = (maxproc * 9) / 10; 313254515Sandre 31491780Ssilby /* 315254515Sandre * The default limit for maxfiles is 1/12 of the number of 316254515Sandre * physical page but not less than 16 times maxusers. 317254515Sandre * At most it can be 1/6 the number of physical pages. 31891780Ssilby */ 319254515Sandre maxfiles = imax(MAXFILES, physpages / 8); 32087546Sdillon TUNABLE_INT_FETCH("kern.maxfiles", &maxfiles); 321254515Sandre if (maxfiles > (physpages / 4)) 322254515Sandre maxfiles = physpages / 4; 323254515Sandre maxfilesperproc = (maxfiles / 10) * 9; 324117391Ssilby 325117391Ssilby /* 326117391Ssilby * Cannot be changed after boot. 327117391Ssilby */ 328117391Ssilby nbuf = NBUF; 329117391Ssilby TUNABLE_INT_FETCH("kern.nbuf", &nbuf); 330251897Sscottl TUNABLE_INT_FETCH("kern.bio_transient_maxcnt", &bio_transient_maxcnt); 33187546Sdillon 332254515Sandre /* 333254515Sandre * XXX: Does the callout wheel have to be so big? 334254515Sandre * 335254515Sandre * Clip callout to result of previous function of maxusers maximum 336254515Sandre * 384. This is still huge, but acceptable. 337254515Sandre */ 338254515Sandre ncallout = imin(16 + maxproc + maxfiles, 18508); 339117391Ssilby TUNABLE_INT_FETCH("kern.ncallout", &ncallout); 340117391Ssilby 34187546Sdillon /* 342219920Salc * The default for maxpipekva is min(1/64 of the kernel address space, 343219920Salc * max(1/64 of main memory, 512KB)). See sys_pipe.c for more details. 344117325Ssilby */ 345219920Salc maxpipekva = (physpages / 64) * PAGE_SIZE; 346254515Sandre TUNABLE_LONG_FETCH("kern.ipc.maxpipekva", &maxpipekva); 347117325Ssilby if (maxpipekva < 512 * 1024) 348117325Ssilby maxpipekva = 512 * 1024; 349219920Salc if (maxpipekva > (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / 64) 350219920Salc maxpipekva = (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / 351219920Salc 64; 35287546Sdillon} 353186286Sivoras 354186286Sivoras/* 355257433Skib * Sysctl stringifying handler for kern.vm_guest. 356186286Sivoras */ 357186286Sivorasstatic int 358186286Sivorassysctl_kern_vm_guest(SYSCTL_HANDLER_ARGS) 359186286Sivoras{ 360186286Sivoras return (SYSCTL_OUT(req, vm_guest_sysctl_names[vm_guest], 361186286Sivoras strlen(vm_guest_sysctl_names[vm_guest]))); 362186286Sivoras} 363