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> 47239301Skib#include <sys/limits.h> 48239301Skib#include <sys/msgbuf.h> 49172696Salfred#include <sys/sysctl.h> 50239301Skib#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 79288690Skib#define MAXFILES (40 + 32 * maxusers) 8045515Sdes#endif 818747Sdg 82186286Sivorasstatic int sysctl_kern_vm_guest(SYSCTL_HANDLER_ARGS); 83186286Sivoras 84247777Sdavideint hz; /* system clock's frequency */ 85247777Sdavideint tick; /* usec per tick (1000000 / hz) */ 86247777Sdavidestruct bintime tick_bt; /* bintime per tick (1s / hz) */ 87247777Sdavidesbintime_t tick_sbt; 8880418Speterint maxusers; /* base tunable */ 8980418Speterint maxproc; /* maximum # of processes */ 9080418Speterint maxprocperuid; /* max # of procs per user */ 9180418Speterint maxfiles; /* sys. wide open files limit */ 9280418Speterint maxfilesperproc; /* per-proc open files limit */ 93217688Spluknetint msgbufsize; /* size of kernel message buffer */ 9480418Speterint nbuf; 95248508Skibint bio_transient_maxcnt; 96202143Sbrooksint ngroups_max; /* max # groups per process */ 9780418Speterint nswbuf; 98239301Skibpid_t pid_max = PID_MAX; 99189595Sjhblong maxswzone; /* max swmeta KVA storage */ 100189595Sjhblong maxbcache; /* max buffer cache KVA storage */ 101189649Sjhblong maxpipekva; /* Limit on pipe KVA */ 102278522Sjhb#ifdef XEN 103278522Sjhbint vm_guest = VM_GUEST_XEN; 104278522Sjhb#else 105278522Sjhbint vm_guest = VM_GUEST_NO; /* Running as virtual machine guest? */ 106278522Sjhb#endif 107137393Sdesu_long maxtsiz; /* max text size */ 108137393Sdesu_long dfldsiz; /* initial data size limit */ 109137393Sdesu_long maxdsiz; /* max data size */ 110137393Sdesu_long dflssiz; /* initial stack size limit */ 111137393Sdesu_long maxssiz; /* max stack size */ 112137393Sdesu_long sgrowsiz; /* amount to grow stack */ 1131541Srgrimes 114190331SjhbSYSCTL_INT(_kern, OID_AUTO, hz, CTLFLAG_RDTUN, &hz, 0, 115190331Sjhb "Number of clock ticks per second"); 116189744SjhbSYSCTL_INT(_kern, OID_AUTO, nbuf, CTLFLAG_RDTUN, &nbuf, 0, 117190331Sjhb "Number of buffers in the buffer cache"); 118189744SjhbSYSCTL_INT(_kern, OID_AUTO, nswbuf, CTLFLAG_RDTUN, &nswbuf, 0, 119189744Sjhb "Number of swap buffers"); 120217688SpluknetSYSCTL_INT(_kern, OID_AUTO, msgbufsize, CTLFLAG_RDTUN, &msgbufsize, 0, 121217688Spluknet "Size of the kernel message buffer"); 122189745SjhbSYSCTL_LONG(_kern, OID_AUTO, maxswzone, CTLFLAG_RDTUN, &maxswzone, 0, 123190331Sjhb "Maximum memory for swap metadata"); 124189745SjhbSYSCTL_LONG(_kern, OID_AUTO, maxbcache, CTLFLAG_RDTUN, &maxbcache, 0, 125190331Sjhb "Maximum value of vfs.maxbufspace"); 126248508SkibSYSCTL_INT(_kern, OID_AUTO, bio_transient_maxcnt, CTLFLAG_RDTUN, 127248508Skib &bio_transient_maxcnt, 0, 128248508Skib "Maximum number of transient BIOs mappings"); 129240068SzontSYSCTL_ULONG(_kern, OID_AUTO, maxtsiz, CTLFLAG_RW | CTLFLAG_TUN, &maxtsiz, 0, 130190331Sjhb "Maximum text size"); 131240068SzontSYSCTL_ULONG(_kern, OID_AUTO, dfldsiz, CTLFLAG_RW | CTLFLAG_TUN, &dfldsiz, 0, 132190331Sjhb "Initial data size limit"); 133240068SzontSYSCTL_ULONG(_kern, OID_AUTO, maxdsiz, CTLFLAG_RW | CTLFLAG_TUN, &maxdsiz, 0, 134190331Sjhb "Maximum data size"); 135240068SzontSYSCTL_ULONG(_kern, OID_AUTO, dflssiz, CTLFLAG_RW | CTLFLAG_TUN, &dflssiz, 0, 136190331Sjhb "Initial stack size limit"); 137240068SzontSYSCTL_ULONG(_kern, OID_AUTO, maxssiz, CTLFLAG_RW | CTLFLAG_TUN, &maxssiz, 0, 138190331Sjhb "Maximum stack size"); 139240068SzontSYSCTL_ULONG(_kern, OID_AUTO, sgrowsiz, CTLFLAG_RW | CTLFLAG_TUN, &sgrowsiz, 0, 140240068Szont "Amount to grow stack on a stack fault"); 141186286SivorasSYSCTL_PROC(_kern, OID_AUTO, vm_guest, CTLFLAG_RD | CTLTYPE_STRING, 142186286Sivoras NULL, 0, sysctl_kern_vm_guest, "A", 143278522Sjhb "Virtual machine guest detected?"); 144172696Salfred 1451541Srgrimes/* 1461541Srgrimes * These have to be allocated somewhere; allocating 1471541Srgrimes * them here forces loader errors if this file is omitted 1481541Srgrimes * (if they've been externed everywhere else; hah!). 1491541Srgrimes */ 1509759Sbdestruct buf *swbuf; 15167046Sjasone 152204420Salc/* 153204420Salc * The elements of this array are ordered based upon the values of the 154204420Salc * corresponding enum VM_GUEST members. 155204420Salc */ 156186522Sbzstatic const char *const vm_guest_sysctl_names[] = { 157186522Sbz "none", 158186522Sbz "generic", 159186522Sbz "xen", 160258127Spluknet "hv", 161278522Sjhb "vmware", 162186522Sbz NULL 163186522Sbz}; 164258127SpluknetCTASSERT(nitems(vm_guest_sysctl_names) - 1 == VM_LAST); 165186522Sbz 166186286Sivoras/* 16787546Sdillon * Boot time overrides that are not scaled against main memory 16880418Speter */ 16980418Spetervoid 17087546Sdilloninit_param1(void) 17180418Speter{ 172278522Sjhb 173184323Ssobomax hz = -1; 17480418Speter TUNABLE_INT_FETCH("kern.hz", &hz); 175185772Sjkim if (hz == -1) 176186252Sivoras hz = vm_guest > VM_GUEST_NO ? HZ_VM : HZ; 17780418Speter tick = 1000000 / hz; 178247777Sdavide tick_sbt = SBT_1S / hz; 179247777Sdavide tick_bt = sbttobt(tick_sbt); 18080418Speter 18181986Sdillon#ifdef VM_SWZONE_SIZE_MAX 18281933Sdillon maxswzone = VM_SWZONE_SIZE_MAX; 18381986Sdillon#endif 184189595Sjhb TUNABLE_LONG_FETCH("kern.maxswzone", &maxswzone); 18581986Sdillon#ifdef VM_BCACHE_SIZE_MAX 18681933Sdillon maxbcache = VM_BCACHE_SIZE_MAX; 18781986Sdillon#endif 188189595Sjhb TUNABLE_LONG_FETCH("kern.maxbcache", &maxbcache); 189217688Spluknet msgbufsize = MSGBUF_SIZE; 190217688Spluknet TUNABLE_INT_FETCH("kern.msgbufsize", &msgbufsize); 19184783Sps 19284783Sps maxtsiz = MAXTSIZ; 193137393Sdes TUNABLE_ULONG_FETCH("kern.maxtsiz", &maxtsiz); 19484783Sps dfldsiz = DFLDSIZ; 195137393Sdes TUNABLE_ULONG_FETCH("kern.dfldsiz", &dfldsiz); 19684783Sps maxdsiz = MAXDSIZ; 197137393Sdes TUNABLE_ULONG_FETCH("kern.maxdsiz", &maxdsiz); 19884783Sps dflssiz = DFLSSIZ; 199137393Sdes TUNABLE_ULONG_FETCH("kern.dflssiz", &dflssiz); 20084783Sps maxssiz = MAXSSIZ; 201137393Sdes TUNABLE_ULONG_FETCH("kern.maxssiz", &maxssiz); 20284783Sps sgrowsiz = SGROWSIZ; 203137393Sdes TUNABLE_ULONG_FETCH("kern.sgrowsiz", &sgrowsiz); 204202143Sbrooks 205202143Sbrooks /* 206202143Sbrooks * Let the administrator set {NGROUPS_MAX}, but disallow values 207202143Sbrooks * less than NGROUPS_MAX which would violate POSIX.1-2008 or 208202143Sbrooks * greater than INT_MAX-1 which would result in overflow. 209202143Sbrooks */ 210202143Sbrooks ngroups_max = NGROUPS_MAX; 211202143Sbrooks TUNABLE_INT_FETCH("kern.ngroups", &ngroups_max); 212202143Sbrooks if (ngroups_max < NGROUPS_MAX) 213202143Sbrooks ngroups_max = NGROUPS_MAX; 214239301Skib 215239301Skib /* 216239301Skib * Only allow to lower the maximal pid. 217239329Skib * Prevent setting up a non-bootable system if pid_max is too low. 218239301Skib */ 219239301Skib TUNABLE_INT_FETCH("kern.pid_max", &pid_max); 220239301Skib if (pid_max > PID_MAX) 221239301Skib pid_max = PID_MAX; 222239329Skib else if (pid_max < 300) 223239329Skib pid_max = 300; 224248508Skib 225248508Skib TUNABLE_INT_FETCH("vfs.unmapped_buf_allowed", &unmapped_buf_allowed); 22680418Speter} 22787546Sdillon 22887546Sdillon/* 22987546Sdillon * Boot time overrides that are scaled against main memory 23087546Sdillon */ 23187546Sdillonvoid 232102600Speterinit_param2(long physpages) 23387546Sdillon{ 23487546Sdillon 23587546Sdillon /* Base parameters */ 23690274Sdillon maxusers = MAXUSERS; 23790274Sdillon TUNABLE_INT_FETCH("kern.maxusers", &maxusers); 23890274Sdillon if (maxusers == 0) { 23989769Sdillon maxusers = physpages / (2 * 1024 * 1024 / PAGE_SIZE); 24087546Sdillon if (maxusers < 32) 24187546Sdillon maxusers = 32; 242242847Salfred#ifdef VM_MAX_AUTOTUNE_MAXUSERS 243242847Salfred if (maxusers > VM_MAX_AUTOTUNE_MAXUSERS) 244242847Salfred maxusers = VM_MAX_AUTOTUNE_MAXUSERS; 245242847Salfred#endif 246242847Salfred /* 247242847Salfred * Scales down the function in which maxusers grows once 248242847Salfred * we hit 384. 249242847Salfred */ 250242847Salfred if (maxusers > 384) 251242847Salfred maxusers = 384 + ((maxusers - 384) / 8); 252242847Salfred } 25387546Sdillon 25487546Sdillon /* 25587546Sdillon * The following can be overridden after boot via sysctl. Note: 25687546Sdillon * unless overriden, these macros are ultimately based on maxusers. 257243631Sandre * Limit maxproc so that kmap entries cannot be exhausted by 258243631Sandre * processes. 25987546Sdillon */ 26087546Sdillon maxproc = NPROC; 26187546Sdillon TUNABLE_INT_FETCH("kern.maxproc", &maxproc); 262243631Sandre if (maxproc > (physpages / 12)) 263243631Sandre maxproc = physpages / 12; 264288690Skib if (maxproc > pid_max) 265288690Skib maxproc = pid_max; 266243631Sandre maxprocperuid = (maxproc * 9) / 10; 267243631Sandre 26891780Ssilby /* 269243631Sandre * The default limit for maxfiles is 1/12 of the number of 270243631Sandre * physical page but not less than 16 times maxusers. 271243631Sandre * At most it can be 1/6 the number of physical pages. 27291780Ssilby */ 273243631Sandre maxfiles = imax(MAXFILES, physpages / 8); 27487546Sdillon TUNABLE_INT_FETCH("kern.maxfiles", &maxfiles); 275243631Sandre if (maxfiles > (physpages / 4)) 276243631Sandre maxfiles = physpages / 4; 277243631Sandre maxfilesperproc = (maxfiles / 10) * 9; 278117391Ssilby 279117391Ssilby /* 280117391Ssilby * Cannot be changed after boot. 281117391Ssilby */ 282117391Ssilby nbuf = NBUF; 283117391Ssilby TUNABLE_INT_FETCH("kern.nbuf", &nbuf); 284248508Skib TUNABLE_INT_FETCH("kern.bio_transient_maxcnt", &bio_transient_maxcnt); 28587546Sdillon 286243631Sandre /* 287219920Salc * The default for maxpipekva is min(1/64 of the kernel address space, 288219920Salc * max(1/64 of main memory, 512KB)). See sys_pipe.c for more details. 289117325Ssilby */ 290219920Salc maxpipekva = (physpages / 64) * PAGE_SIZE; 291243631Sandre TUNABLE_LONG_FETCH("kern.ipc.maxpipekva", &maxpipekva); 292117325Ssilby if (maxpipekva < 512 * 1024) 293117325Ssilby maxpipekva = 512 * 1024; 294219920Salc if (maxpipekva > (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / 64) 295219920Salc maxpipekva = (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / 296219920Salc 64; 29787546Sdillon} 298186286Sivoras 299186286Sivoras/* 300257432Skib * Sysctl stringifying handler for kern.vm_guest. 301186286Sivoras */ 302186286Sivorasstatic int 303186286Sivorassysctl_kern_vm_guest(SYSCTL_HANDLER_ARGS) 304186286Sivoras{ 305186286Sivoras return (SYSCTL_OUT(req, vm_guest_sysctl_names[vm_guest], 306186286Sivoras strlen(vm_guest_sysctl_names[vm_guest]))); 307186286Sivoras} 308