vm_glue.c revision 78481
1/* 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * The Mach Operating System project at Carnegie-Mellon University. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: @(#)vm_glue.c 8.6 (Berkeley) 1/5/94 37 * 38 * 39 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 40 * All rights reserved. 41 * 42 * Permission to use, copy, modify and distribute this software and 43 * its documentation is hereby granted, provided that both the copyright 44 * notice and this permission notice appear in all copies of the 45 * software, derivative works or modified versions, and any portions 46 * thereof, and that both notices appear in supporting documentation. 47 * 48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51 * 52 * Carnegie Mellon requests users of this software to return to 53 * 54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 55 * School of Computer Science 56 * Carnegie Mellon University 57 * Pittsburgh PA 15213-3890 58 * 59 * any improvements or extensions that they make and grant Carnegie the 60 * rights to redistribute these changes. 61 * 62 * $FreeBSD: head/sys/vm/vm_glue.c 78481 2001-06-20 00:48:20Z jhb $ 63 */ 64 65#include "opt_rlimit.h" 66#include "opt_vm.h" 67 68#include <sys/param.h> 69#include <sys/systm.h> 70#include <sys/lock.h> 71#include <sys/mutex.h> 72#include <sys/proc.h> 73#include <sys/resourcevar.h> 74#include <sys/shm.h> 75#include <sys/vmmeter.h> 76#include <sys/sx.h> 77#include <sys/sysctl.h> 78 79#include <sys/kernel.h> 80#include <sys/ktr.h> 81#include <sys/unistd.h> 82 83#include <machine/limits.h> 84 85#include <vm/vm.h> 86#include <vm/vm_param.h> 87#include <vm/pmap.h> 88#include <vm/vm_map.h> 89#include <vm/vm_page.h> 90#include <vm/vm_pageout.h> 91#include <vm/vm_kern.h> 92#include <vm/vm_extern.h> 93 94#include <sys/user.h> 95 96extern int maxslp; 97 98/* 99 * System initialization 100 * 101 * Note: proc0 from proc.h 102 */ 103 104static void vm_init_limits __P((void *)); 105SYSINIT(vm_limits, SI_SUB_VM_CONF, SI_ORDER_FIRST, vm_init_limits, &proc0) 106 107/* 108 * THIS MUST BE THE LAST INITIALIZATION ITEM!!! 109 * 110 * Note: run scheduling should be divorced from the vm system. 111 */ 112static void scheduler __P((void *)); 113SYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_FIRST, scheduler, NULL) 114 115 116static void swapout __P((struct proc *)); 117 118int 119kernacc(addr, len, rw) 120 caddr_t addr; 121 int len, rw; 122{ 123 boolean_t rv; 124 vm_offset_t saddr, eaddr; 125 vm_prot_t prot; 126 127 KASSERT((rw & (~VM_PROT_ALL)) == 0, 128 ("illegal ``rw'' argument to kernacc (%x)\n", rw)); 129 prot = rw; 130 saddr = trunc_page((vm_offset_t)addr); 131 eaddr = round_page((vm_offset_t)addr + len); 132 vm_map_lock_read(kernel_map); 133 rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot); 134 vm_map_unlock_read(kernel_map); 135 return (rv == TRUE); 136} 137 138int 139useracc(addr, len, rw) 140 caddr_t addr; 141 int len, rw; 142{ 143 boolean_t rv; 144 vm_prot_t prot; 145 vm_map_t map; 146 vm_map_entry_t save_hint; 147 148 KASSERT((rw & (~VM_PROT_ALL)) == 0, 149 ("illegal ``rw'' argument to useracc (%x)\n", rw)); 150 prot = rw; 151 /* 152 * XXX - check separately to disallow access to user area and user 153 * page tables - they are in the map. 154 * 155 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max. It was once 156 * only used (as an end address) in trap.c. Use it as an end address 157 * here too. This bogusness has spread. I just fixed where it was 158 * used as a max in vm_mmap.c. 159 */ 160 if ((vm_offset_t) addr + len > /* XXX */ VM_MAXUSER_ADDRESS 161 || (vm_offset_t) addr + len < (vm_offset_t) addr) { 162 return (FALSE); 163 } 164 mtx_lock(&vm_mtx); 165 map = &curproc->p_vmspace->vm_map; 166 vm_map_lock_read(map); 167 /* 168 * We save the map hint, and restore it. Useracc appears to distort 169 * the map hint unnecessarily. 170 */ 171 save_hint = map->hint; 172 rv = vm_map_check_protection(map, 173 trunc_page((vm_offset_t)addr), round_page((vm_offset_t)addr + len), prot); 174 map->hint = save_hint; 175 vm_map_unlock_read(map); 176 mtx_unlock(&vm_mtx); 177 178 return (rv == TRUE); 179} 180 181void 182vslock(addr, len) 183 caddr_t addr; 184 u_int len; 185{ 186 187 mtx_lock(&vm_mtx); 188 vm_map_pageable(&curproc->p_vmspace->vm_map, 189 trunc_page((vm_offset_t)addr), 190 round_page((vm_offset_t)addr + len), FALSE); 191 mtx_unlock(&vm_mtx); 192} 193 194void 195vsunlock(addr, len) 196 caddr_t addr; 197 u_int len; 198{ 199 200 mtx_lock(&vm_mtx); 201 vm_map_pageable(&curproc->p_vmspace->vm_map, 202 trunc_page((vm_offset_t)addr), 203 round_page((vm_offset_t)addr + len), TRUE); 204 mtx_unlock(&vm_mtx); 205} 206 207/* 208 * Implement fork's actions on an address space. 209 * Here we arrange for the address space to be copied or referenced, 210 * allocate a user struct (pcb and kernel stack), then call the 211 * machine-dependent layer to fill those in and make the new process 212 * ready to run. The new process is set up so that it returns directly 213 * to user mode to avoid stack copying and relocation problems. 214 * 215 * Called without vm_mtx. 216 */ 217void 218vm_fork(p1, p2, flags) 219 register struct proc *p1, *p2; 220 int flags; 221{ 222 register struct user *up; 223 224 mtx_lock(&vm_mtx); 225 if ((flags & RFPROC) == 0) { 226 /* 227 * Divorce the memory, if it is shared, essentially 228 * this changes shared memory amongst threads, into 229 * COW locally. 230 */ 231 if ((flags & RFMEM) == 0) { 232 if (p1->p_vmspace->vm_refcnt > 1) { 233 vmspace_unshare(p1); 234 } 235 } 236 cpu_fork(p1, p2, flags); 237 mtx_unlock(&vm_mtx); 238 return; 239 } 240 241 if (flags & RFMEM) { 242 p2->p_vmspace = p1->p_vmspace; 243 p1->p_vmspace->vm_refcnt++; 244 } 245 246 while (vm_page_count_severe()) { 247 VM_WAIT; 248 } 249 250 if ((flags & RFMEM) == 0) { 251 p2->p_vmspace = vmspace_fork(p1->p_vmspace); 252 253 pmap_pinit2(vmspace_pmap(p2->p_vmspace)); 254 255 if (p1->p_vmspace->vm_shm) 256 shmfork(p1, p2); 257 } 258 259 pmap_new_proc(p2); 260 261 up = p2->p_addr; 262 263 /* 264 * p_stats currently points at fields in the user struct 265 * but not at &u, instead at p_addr. Copy parts of 266 * p_stats; zero the rest of p_stats (statistics). 267 * 268 * If procsig->ps_refcnt is 1 and p2->p_sigacts is NULL we dont' need 269 * to share sigacts, so we use the up->u_sigacts. 270 */ 271 p2->p_stats = &up->u_stats; 272 if (p2->p_sigacts == NULL) { 273 if (p2->p_procsig->ps_refcnt != 1) 274 printf ("PID:%d NULL sigacts with refcnt not 1!\n",p2->p_pid); 275 p2->p_sigacts = &up->u_sigacts; 276 up->u_sigacts = *p1->p_sigacts; 277 } 278 279 bzero(&up->u_stats.pstat_startzero, 280 (unsigned) ((caddr_t) &up->u_stats.pstat_endzero - 281 (caddr_t) &up->u_stats.pstat_startzero)); 282 bcopy(&p1->p_stats->pstat_startcopy, &up->u_stats.pstat_startcopy, 283 ((caddr_t) &up->u_stats.pstat_endcopy - 284 (caddr_t) &up->u_stats.pstat_startcopy)); 285 286 287 /* 288 * cpu_fork will copy and update the pcb, set up the kernel stack, 289 * and make the child ready to run. 290 */ 291 cpu_fork(p1, p2, flags); 292 mtx_unlock(&vm_mtx); 293} 294 295/* 296 * Set default limits for VM system. 297 * Called for proc 0, and then inherited by all others. 298 * 299 * XXX should probably act directly on proc0. 300 */ 301static void 302vm_init_limits(udata) 303 void *udata; 304{ 305 register struct proc *p = udata; 306 int rss_limit; 307 308 /* 309 * Set up the initial limits on process VM. Set the maximum resident 310 * set size to be half of (reasonably) available memory. Since this 311 * is a soft limit, it comes into effect only when the system is out 312 * of memory - half of main memory helps to favor smaller processes, 313 * and reduces thrashing of the object cache. 314 */ 315 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ; 316 p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ; 317 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ; 318 p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ; 319 /* limit the limit to no less than 2MB */ 320 rss_limit = max(cnt.v_free_count, 512); 321 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit); 322 p->p_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY; 323} 324 325/* 326 * Must be called with the proc struc mutex held. 327 */ 328void 329faultin(p) 330 struct proc *p; 331{ 332 333 PROC_LOCK_ASSERT(p, MA_OWNED); 334 mtx_lock_spin(&sched_lock); 335 if ((p->p_sflag & PS_INMEM) == 0) { 336 337 ++p->p_lock; 338 mtx_unlock_spin(&sched_lock); 339 PROC_UNLOCK(p); 340 341 mtx_lock(&vm_mtx); 342 pmap_swapin_proc(p); 343 mtx_unlock(&vm_mtx); 344 345 PROC_LOCK(p); 346 mtx_lock_spin(&sched_lock); 347 if (p->p_stat == SRUN) { 348 setrunqueue(p); 349 } 350 351 p->p_sflag |= PS_INMEM; 352 353 /* undo the effect of setting SLOCK above */ 354 --p->p_lock; 355 } 356 mtx_unlock_spin(&sched_lock); 357} 358 359/* 360 * This swapin algorithm attempts to swap-in processes only if there 361 * is enough space for them. Of course, if a process waits for a long 362 * time, it will be swapped in anyway. 363 * 364 * Giant is still held at this point, to be released in tsleep. 365 */ 366/* ARGSUSED*/ 367static void 368scheduler(dummy) 369 void *dummy; 370{ 371 register struct proc *p; 372 register int pri; 373 struct proc *pp; 374 int ppri; 375 376 mtx_assert(&Giant, MA_OWNED | MA_NOTRECURSED); 377 378loop: 379 mtx_lock(&vm_mtx); 380 if (vm_page_count_min()) { 381 VM_WAIT; 382 mtx_unlock(&vm_mtx); 383 goto loop; 384 } 385 mtx_unlock(&vm_mtx); 386 387 pp = NULL; 388 ppri = INT_MIN; 389 sx_slock(&allproc_lock); 390 LIST_FOREACH(p, &allproc, p_list) { 391 mtx_lock_spin(&sched_lock); 392 if (p->p_stat == SRUN && 393 (p->p_sflag & (PS_INMEM | PS_SWAPPING)) == 0) { 394 395 pri = p->p_swtime + p->p_slptime; 396 if ((p->p_sflag & PS_SWAPINREQ) == 0) { 397 pri -= p->p_nice * 8; 398 } 399 400 /* 401 * if this process is higher priority and there is 402 * enough space, then select this process instead of 403 * the previous selection. 404 */ 405 if (pri > ppri) { 406 pp = p; 407 ppri = pri; 408 } 409 } 410 mtx_unlock_spin(&sched_lock); 411 } 412 sx_sunlock(&allproc_lock); 413 414 /* 415 * Nothing to do, back to sleep. 416 */ 417 if ((p = pp) == NULL) { 418 tsleep(&proc0, PVM, "sched", maxslp * hz / 2); 419 goto loop; 420 } 421 mtx_lock_spin(&sched_lock); 422 p->p_sflag &= ~PS_SWAPINREQ; 423 mtx_unlock_spin(&sched_lock); 424 425 /* 426 * We would like to bring someone in. (only if there is space). 427 */ 428 PROC_LOCK(p); 429 faultin(p); 430 PROC_UNLOCK(p); 431 mtx_lock_spin(&sched_lock); 432 p->p_swtime = 0; 433 mtx_unlock_spin(&sched_lock); 434 goto loop; 435} 436 437#ifndef NO_SWAPPING 438 439/* 440 * Swap_idle_threshold1 is the guaranteed swapped in time for a process 441 */ 442static int swap_idle_threshold1 = 2; 443SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold1, 444 CTLFLAG_RW, &swap_idle_threshold1, 0, ""); 445 446/* 447 * Swap_idle_threshold2 is the time that a process can be idle before 448 * it will be swapped out, if idle swapping is enabled. 449 */ 450static int swap_idle_threshold2 = 10; 451SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold2, 452 CTLFLAG_RW, &swap_idle_threshold2, 0, ""); 453 454/* 455 * Swapout is driven by the pageout daemon. Very simple, we find eligible 456 * procs and unwire their u-areas. We try to always "swap" at least one 457 * process in case we need the room for a swapin. 458 * If any procs have been sleeping/stopped for at least maxslp seconds, 459 * they are swapped. Else, we swap the longest-sleeping or stopped process, 460 * if any, otherwise the longest-resident process. 461 * 462 * Can block 463 * must be called with vm_mtx 464 */ 465void 466swapout_procs(action) 467int action; 468{ 469 register struct proc *p; 470 struct proc *outp, *outp2; 471 int outpri, outpri2; 472 int didswap = 0; 473 474 mtx_assert(&vm_mtx, MA_OWNED); 475 mtx_unlock(&vm_mtx); 476 outp = outp2 = NULL; 477 outpri = outpri2 = INT_MIN; 478retry: 479 sx_slock(&allproc_lock); 480 LIST_FOREACH(p, &allproc, p_list) { 481 struct vmspace *vm; 482 483 mtx_lock(&vm_mtx); 484 PROC_LOCK(p); 485 if (p->p_lock != 0 || 486 (p->p_flag & (P_TRACED|P_SYSTEM|P_WEXIT)) != 0) { 487 PROC_UNLOCK(p); 488 mtx_unlock(&vm_mtx); 489 continue; 490 } 491 /* 492 * only aiod changes vmspace, however it will be 493 * skipped because of the if statement above checking 494 * for P_SYSTEM 495 */ 496 vm = p->p_vmspace; 497 mtx_lock_spin(&sched_lock); 498 if ((p->p_sflag & (PS_INMEM|PS_SWAPPING)) != PS_INMEM) { 499 mtx_unlock_spin(&sched_lock); 500 PROC_UNLOCK(p); 501 mtx_unlock(&vm_mtx); 502 continue; 503 } 504 505 switch (p->p_stat) { 506 default: 507 mtx_unlock_spin(&sched_lock); 508 PROC_UNLOCK(p); 509 mtx_unlock(&vm_mtx); 510 continue; 511 512 case SSLEEP: 513 case SSTOP: 514 /* 515 * do not swapout a realtime process 516 */ 517 if (PRI_IS_REALTIME(p->p_pri.pri_class)) { 518 mtx_unlock_spin(&sched_lock); 519 PROC_UNLOCK(p); 520 mtx_unlock(&vm_mtx); 521 continue; 522 } 523 524 /* 525 * Do not swapout a process waiting on a critical 526 * event of some kind. Also guarantee swap_idle_threshold1 527 * time in memory. 528 */ 529 if (((p->p_pri.pri_level) < PSOCK) || 530 (p->p_slptime < swap_idle_threshold1)) { 531 mtx_unlock_spin(&sched_lock); 532 PROC_UNLOCK(p); 533 mtx_unlock(&vm_mtx); 534 continue; 535 } 536 537 /* 538 * If the system is under memory stress, or if we are swapping 539 * idle processes >= swap_idle_threshold2, then swap the process 540 * out. 541 */ 542 if (((action & VM_SWAP_NORMAL) == 0) && 543 (((action & VM_SWAP_IDLE) == 0) || 544 (p->p_slptime < swap_idle_threshold2))) { 545 mtx_unlock_spin(&sched_lock); 546 PROC_UNLOCK(p); 547 mtx_unlock(&vm_mtx); 548 continue; 549 } 550 mtx_unlock_spin(&sched_lock); 551 552 ++vm->vm_refcnt; 553 /* 554 * do not swapout a process that is waiting for VM 555 * data structures there is a possible deadlock. 556 */ 557 if (lockmgr(&vm->vm_map.lock, 558 LK_EXCLUSIVE | LK_NOWAIT, 559 NULL, curproc)) { 560 vmspace_free(vm); 561 PROC_UNLOCK(p); 562 mtx_unlock(&vm_mtx); 563 continue; 564 } 565 vm_map_unlock(&vm->vm_map); 566 /* 567 * If the process has been asleep for awhile and had 568 * most of its pages taken away already, swap it out. 569 */ 570 if ((action & VM_SWAP_NORMAL) || 571 ((action & VM_SWAP_IDLE) && 572 (p->p_slptime > swap_idle_threshold2))) { 573 sx_sunlock(&allproc_lock); 574 swapout(p); 575 vmspace_free(vm); 576 didswap++; 577 mtx_unlock(&vm_mtx); 578 goto retry; 579 } 580 PROC_UNLOCK(p); 581 vmspace_free(vm); 582 mtx_unlock(&vm_mtx); 583 } 584 } 585 sx_sunlock(&allproc_lock); 586 /* 587 * If we swapped something out, and another process needed memory, 588 * then wakeup the sched process. 589 */ 590 mtx_lock(&vm_mtx); 591 if (didswap) 592 wakeup(&proc0); 593} 594 595static void 596swapout(p) 597 register struct proc *p; 598{ 599 600 PROC_LOCK_ASSERT(p, MA_OWNED); 601#if defined(SWAP_DEBUG) 602 printf("swapping out %d\n", p->p_pid); 603#endif 604 ++p->p_stats->p_ru.ru_nswap; 605 /* 606 * remember the process resident count 607 */ 608 p->p_vmspace->vm_swrss = vmspace_resident_count(p->p_vmspace); 609 610 mtx_lock_spin(&sched_lock); 611 p->p_sflag &= ~PS_INMEM; 612 p->p_sflag |= PS_SWAPPING; 613 PROC_UNLOCK_NOSWITCH(p); 614 if (p->p_stat == SRUN) 615 remrunqueue(p); 616 mtx_unlock_spin(&sched_lock); 617 618 pmap_swapout_proc(p); 619 620 mtx_lock_spin(&sched_lock); 621 p->p_sflag &= ~PS_SWAPPING; 622 p->p_swtime = 0; 623 mtx_unlock_spin(&sched_lock); 624} 625#endif /* !NO_SWAPPING */ 626