vm_glue.c revision 41936
1129198Scognet/* 2129198Scognet * Copyright (c) 1991, 1993 3129198Scognet * The Regents of the University of California. All rights reserved. 4129198Scognet * 5129198Scognet * This code is derived from software contributed to Berkeley by 6129198Scognet * The Mach Operating System project at Carnegie-Mellon University. 7129198Scognet * 8129198Scognet * Redistribution and use in source and binary forms, with or without 9129198Scognet * modification, are permitted provided that the following conditions 10129198Scognet * are met: 11129198Scognet * 1. Redistributions of source code must retain the above copyright 12129198Scognet * notice, this list of conditions and the following disclaimer. 13129198Scognet * 2. Redistributions in binary form must reproduce the above copyright 14129198Scognet * notice, this list of conditions and the following disclaimer in the 15129198Scognet * documentation and/or other materials provided with the distribution. 16129198Scognet * 3. All advertising materials mentioning features or use of this software 17129198Scognet * must display the following acknowledgement: 18129198Scognet * This product includes software developed by the University of 19129198Scognet * California, Berkeley and its contributors. 20129198Scognet * 4. Neither the name of the University nor the names of its contributors 21129198Scognet * may be used to endorse or promote products derived from this software 22129198Scognet * without specific prior written permission. 23129198Scognet * 24129198Scognet * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25129198Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26129198Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27129198Scognet * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28129198Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29129198Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30129198Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31129198Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32129198Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33129198Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34129198Scognet * SUCH DAMAGE. 35129198Scognet * 36129198Scognet * from: @(#)vm_glue.c 8.6 (Berkeley) 1/5/94 37129198Scognet * 38129198Scognet * 39129198Scognet * Copyright (c) 1987, 1990 Carnegie-Mellon University. 40129198Scognet * All rights reserved. 41129198Scognet * 42129198Scognet * Permission to use, copy, modify and distribute this software and 43129198Scognet * its documentation is hereby granted, provided that both the copyright 44129198Scognet * notice and this permission notice appear in all copies of the 45129198Scognet * software, derivative works or modified versions, and any portions 46129198Scognet * thereof, and that both notices appear in supporting documentation. 47129198Scognet * 48129198Scognet * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 49129198Scognet * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 50129198Scognet * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51129198Scognet * 52129198Scognet * Carnegie Mellon requests users of this software to return to 53129198Scognet * 54129198Scognet * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 55129198Scognet * School of Computer Science 56129198Scognet * Carnegie Mellon University 57129198Scognet * Pittsburgh PA 15213-3890 58129198Scognet * 59129198Scognet * any improvements or extensions that they make and grant Carnegie the 60129198Scognet * rights to redistribute these changes. 61129198Scognet * 62129198Scognet * $Id: vm_glue.c,v 1.78 1998/12/19 02:55:34 julian Exp $ 63129198Scognet */ 64129198Scognet 65129198Scognet#include "opt_rlimit.h" 66129198Scognet#include "opt_vm.h" 67129198Scognet 68129198Scognet#include <sys/param.h> 69129198Scognet#include <sys/systm.h> 70129198Scognet#include <sys/proc.h> 71129198Scognet#include <sys/resourcevar.h> 72129198Scognet#include <sys/buf.h> 73129198Scognet#include <sys/shm.h> 74129198Scognet#include <sys/vmmeter.h> 75129198Scognet#include <sys/sysctl.h> 76129198Scognet 77129198Scognet#include <sys/kernel.h> 78130023Stjr#include <sys/unistd.h> 79130023Stjr 80129198Scognet#include <machine/limits.h> 81129198Scognet 82129198Scognet#include <vm/vm.h> 83129198Scognet#include <vm/vm_param.h> 84129198Scognet#include <vm/vm_prot.h> 85129198Scognet#include <sys/lock.h> 86129198Scognet#include <vm/pmap.h> 87129198Scognet#include <vm/vm_map.h> 88129198Scognet#include <vm/vm_page.h> 89129198Scognet#include <vm/vm_pageout.h> 90129198Scognet#include <vm/vm_kern.h> 91129198Scognet#include <vm/vm_extern.h> 92129198Scognet 93129198Scognet#include <sys/user.h> 94129198Scognet 95129198Scognet/* 96129198Scognet * System initialization 97129198Scognet * 98129198Scognet * Note: proc0 from proc.h 99129198Scognet */ 100129198Scognet 101129198Scognetstatic void vm_init_limits __P((void *)); 102129198ScognetSYSINIT(vm_limits, SI_SUB_VM_CONF, SI_ORDER_FIRST, vm_init_limits, &proc0) 103129198Scognet 104129198Scognet/* 105129198Scognet * THIS MUST BE THE LAST INITIALIZATION ITEM!!! 106129198Scognet * 107129198Scognet * Note: run scheduling should be divorced from the vm system. 108129198Scognet */ 109129198Scognetstatic void scheduler __P((void *)); 110129198ScognetSYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_FIRST, scheduler, NULL) 111129198Scognet 112129198Scognet 113129198Scognetstatic void swapout __P((struct proc *)); 114129198Scognet 115129198Scognetextern char kstack[]; 116129198Scognet 117129198Scognet/* vm_map_t upages_map; */ 118129198Scognet 119129198Scognetint 120129198Scognetkernacc(addr, len, rw) 121129198Scognet caddr_t addr; 122129198Scognet int len, rw; 123130023Stjr{ 124130023Stjr boolean_t rv; 125129198Scognet vm_offset_t saddr, eaddr; 126129198Scognet vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 127 128 saddr = trunc_page((vm_offset_t)addr); 129 eaddr = round_page((vm_offset_t)addr + len); 130 vm_map_lock_read(kernel_map); 131 rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot); 132 vm_map_unlock_read(kernel_map); 133 return (rv == TRUE); 134} 135 136int 137useracc(addr, len, rw) 138 caddr_t addr; 139 int len, rw; 140{ 141 boolean_t rv; 142 vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE; 143 vm_map_t map; 144 vm_map_entry_t save_hint; 145 146 /* 147 * XXX - check separately to disallow access to user area and user 148 * page tables - they are in the map. 149 * 150 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max. It was once 151 * only used (as an end address) in trap.c. Use it as an end address 152 * here too. This bogusness has spread. I just fixed where it was 153 * used as a max in vm_mmap.c. 154 */ 155 if ((vm_offset_t) addr + len > /* XXX */ VM_MAXUSER_ADDRESS 156 || (vm_offset_t) addr + len < (vm_offset_t) addr) { 157 return (FALSE); 158 } 159 map = &curproc->p_vmspace->vm_map; 160 vm_map_lock_read(map); 161 /* 162 * We save the map hint, and restore it. Useracc appears to distort 163 * the map hint unnecessarily. 164 */ 165 save_hint = map->hint; 166 rv = vm_map_check_protection(map, 167 trunc_page((vm_offset_t)addr), round_page((vm_offset_t)addr + len), prot); 168 map->hint = save_hint; 169 vm_map_unlock_read(map); 170 171 return (rv == TRUE); 172} 173 174void 175vslock(addr, len) 176 caddr_t addr; 177 u_int len; 178{ 179 vm_map_pageable(&curproc->p_vmspace->vm_map, trunc_page((vm_offset_t)addr), 180 round_page((vm_offset_t)addr + len), FALSE); 181} 182 183void 184vsunlock(addr, len, dirtied) 185 caddr_t addr; 186 u_int len; 187 int dirtied; 188{ 189#ifdef lint 190 dirtied++; 191#endif /* lint */ 192 vm_map_pageable(&curproc->p_vmspace->vm_map, trunc_page((vm_offset_t)addr), 193 round_page((vm_offset_t)addr + len), TRUE); 194} 195 196/* 197 * Implement fork's actions on an address space. 198 * Here we arrange for the address space to be copied or referenced, 199 * allocate a user struct (pcb and kernel stack), then call the 200 * machine-dependent layer to fill those in and make the new process 201 * ready to run. The new process is set up so that it returns directly 202 * to user mode to avoid stack copying and relocation problems. 203 */ 204void 205vm_fork(p1, p2, flags) 206 register struct proc *p1, *p2; 207 int flags; 208{ 209 register struct user *up; 210 211 if (flags & RFMEM) { 212 p2->p_vmspace = p1->p_vmspace; 213 p1->p_vmspace->vm_refcnt++; 214 } 215 216 while ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min) { 217 vm_pageout_deficit += (UPAGES + VM_INITIAL_PAGEIN); 218 VM_WAIT; 219 } 220 221 if ((flags & RFMEM) == 0) { 222 p2->p_vmspace = vmspace_fork(p1->p_vmspace); 223 224 if (p1->p_vmspace->vm_shm) 225 shmfork(p1, p2); 226 } 227 228 pmap_new_proc(p2); 229 230 up = p2->p_addr; 231 232#ifndef COMPAT_LINUX_THREADS 233 /* 234 * p_stats and p_sigacts currently point at fields in the user struct 235 * but not at &u, instead at p_addr. Copy p_sigacts and parts of 236 * p_stats; zero the rest of p_stats (statistics). 237 */ 238 p2->p_stats = &up->u_stats; 239 p2->p_sigacts = &up->u_sigacts; 240 up->u_sigacts = *p1->p_sigacts; 241#else 242 /* 243 * p_stats currently points at fields in the user struct 244 * but not at &u, instead at p_addr. Copy parts of 245 * p_stats; zero the rest of p_stats (statistics). 246 */ 247 p2->p_stats = &up->u_stats; 248#endif /* COMPAT_LINUX_THREADS */ 249 bzero(&up->u_stats.pstat_startzero, 250 (unsigned) ((caddr_t) &up->u_stats.pstat_endzero - 251 (caddr_t) &up->u_stats.pstat_startzero)); 252 bcopy(&p1->p_stats->pstat_startcopy, &up->u_stats.pstat_startcopy, 253 ((caddr_t) &up->u_stats.pstat_endcopy - 254 (caddr_t) &up->u_stats.pstat_startcopy)); 255 256 257 /* 258 * cpu_fork will copy and update the pcb, set up the kernel stack, 259 * and make the child ready to run. 260 */ 261 cpu_fork(p1, p2); 262} 263 264/* 265 * Set default limits for VM system. 266 * Called for proc 0, and then inherited by all others. 267 * 268 * XXX should probably act directly on proc0. 269 */ 270static void 271vm_init_limits(udata) 272 void *udata; 273{ 274 register struct proc *p = udata; 275 int rss_limit; 276 277 /* 278 * Set up the initial limits on process VM. Set the maximum resident 279 * set size to be half of (reasonably) available memory. Since this 280 * is a soft limit, it comes into effect only when the system is out 281 * of memory - half of main memory helps to favor smaller processes, 282 * and reduces thrashing of the object cache. 283 */ 284 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ; 285 p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ; 286 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ; 287 p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ; 288 /* limit the limit to no less than 2MB */ 289 rss_limit = max(cnt.v_free_count, 512); 290 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit); 291 p->p_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY; 292} 293 294void 295faultin(p) 296 struct proc *p; 297{ 298 int s; 299 300 if ((p->p_flag & P_INMEM) == 0) { 301 302 ++p->p_lock; 303 304 pmap_swapin_proc(p); 305 306 s = splhigh(); 307 308 if (p->p_stat == SRUN) 309 setrunqueue(p); 310 311 p->p_flag |= P_INMEM; 312 313 /* undo the effect of setting SLOCK above */ 314 --p->p_lock; 315 splx(s); 316 317 } 318} 319 320/* 321 * This swapin algorithm attempts to swap-in processes only if there 322 * is enough space for them. Of course, if a process waits for a long 323 * time, it will be swapped in anyway. 324 */ 325/* ARGSUSED*/ 326static void 327scheduler(dummy) 328 void *dummy; 329{ 330 register struct proc *p; 331 register int pri; 332 struct proc *pp; 333 int ppri; 334 335loop: 336 while ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min) { 337 VM_WAIT; 338 } 339 340 pp = NULL; 341 ppri = INT_MIN; 342 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 343 if (p->p_stat == SRUN && 344 (p->p_flag & (P_INMEM | P_SWAPPING)) == 0) { 345 346 pri = p->p_swtime + p->p_slptime; 347 if ((p->p_flag & P_SWAPINREQ) == 0) { 348 pri -= p->p_nice * 8; 349 } 350 351 /* 352 * if this process is higher priority and there is 353 * enough space, then select this process instead of 354 * the previous selection. 355 */ 356 if (pri > ppri) { 357 pp = p; 358 ppri = pri; 359 } 360 } 361 } 362 363 /* 364 * Nothing to do, back to sleep. 365 */ 366 if ((p = pp) == NULL) { 367 tsleep(&proc0, PVM, "sched", 0); 368 goto loop; 369 } 370 p->p_flag &= ~P_SWAPINREQ; 371 372 /* 373 * We would like to bring someone in. (only if there is space). 374 */ 375 faultin(p); 376 p->p_swtime = 0; 377 goto loop; 378} 379 380#ifndef NO_SWAPPING 381 382#define swappable(p) \ 383 (((p)->p_lock == 0) && \ 384 ((p)->p_flag & (P_TRACED|P_NOSWAP|P_SYSTEM|P_INMEM|P_WEXIT|P_PHYSIO|P_SWAPPING)) == P_INMEM) 385 386 387/* 388 * Swap_idle_threshold1 is the guaranteed swapped in time for a process 389 */ 390static int swap_idle_threshold1 = 2; 391SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold1, 392 CTLFLAG_RW, &swap_idle_threshold1, 0, ""); 393 394/* 395 * Swap_idle_threshold2 is the time that a process can be idle before 396 * it will be swapped out, if idle swapping is enabled. 397 */ 398static int swap_idle_threshold2 = 10; 399SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold2, 400 CTLFLAG_RW, &swap_idle_threshold2, 0, ""); 401 402/* 403 * Swapout is driven by the pageout daemon. Very simple, we find eligible 404 * procs and unwire their u-areas. We try to always "swap" at least one 405 * process in case we need the room for a swapin. 406 * If any procs have been sleeping/stopped for at least maxslp seconds, 407 * they are swapped. Else, we swap the longest-sleeping or stopped process, 408 * if any, otherwise the longest-resident process. 409 */ 410void 411swapout_procs(action) 412int action; 413{ 414 register struct proc *p; 415 struct proc *outp, *outp2; 416 int outpri, outpri2; 417 int didswap = 0; 418 419 outp = outp2 = NULL; 420 outpri = outpri2 = INT_MIN; 421retry: 422 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 423 struct vmspace *vm; 424 if (!swappable(p)) 425 continue; 426 427 vm = p->p_vmspace; 428 429 switch (p->p_stat) { 430 default: 431 continue; 432 433 case SSLEEP: 434 case SSTOP: 435 /* 436 * do not swapout a realtime process 437 */ 438 if (RTP_PRIO_IS_REALTIME(p->p_rtprio.type)) 439 continue; 440 441 /* 442 * Do not swapout a process waiting on a critical 443 * event of some kind. Also guarantee swap_idle_threshold1 444 * time in memory. 445 */ 446 if (((p->p_priority & 0x7f) < PSOCK) || 447 (p->p_slptime < swap_idle_threshold1)) 448 continue; 449 450 /* 451 * If the system is under memory stress, or if we are swapping 452 * idle processes >= swap_idle_threshold2, then swap the process 453 * out. 454 */ 455 if (((action & VM_SWAP_NORMAL) == 0) && 456 (((action & VM_SWAP_IDLE) == 0) || 457 (p->p_slptime < swap_idle_threshold2))) 458 continue; 459 460 ++vm->vm_refcnt; 461 /* 462 * do not swapout a process that is waiting for VM 463 * data structures there is a possible deadlock. 464 */ 465 if (lockmgr(&vm->vm_map.lock, 466 LK_EXCLUSIVE | LK_NOWAIT, 467 (void *)0, curproc)) { 468 vmspace_free(vm); 469 continue; 470 } 471 vm_map_unlock(&vm->vm_map); 472 /* 473 * If the process has been asleep for awhile and had 474 * most of its pages taken away already, swap it out. 475 */ 476 if ((action & VM_SWAP_NORMAL) || 477 ((action & VM_SWAP_IDLE) && 478 (p->p_slptime > swap_idle_threshold2))) { 479 swapout(p); 480 vmspace_free(vm); 481 didswap++; 482 goto retry; 483 } 484 } 485 } 486 /* 487 * If we swapped something out, and another process needed memory, 488 * then wakeup the sched process. 489 */ 490 if (didswap) 491 wakeup(&proc0); 492} 493 494static void 495swapout(p) 496 register struct proc *p; 497{ 498 499#if defined(SWAP_DEBUG) 500 printf("swapping out %d\n", p->p_pid); 501#endif 502 ++p->p_stats->p_ru.ru_nswap; 503 /* 504 * remember the process resident count 505 */ 506 p->p_vmspace->vm_swrss = 507 p->p_vmspace->vm_pmap.pm_stats.resident_count; 508 509 (void) splhigh(); 510 p->p_flag &= ~P_INMEM; 511 p->p_flag |= P_SWAPPING; 512 if (p->p_stat == SRUN) 513 remrq(p); 514 (void) spl0(); 515 516 pmap_swapout_proc(p); 517 518 p->p_flag &= ~P_SWAPPING; 519 p->p_swtime = 0; 520} 521#endif /* !NO_SWAPPING */ 522