vm_glue.c revision 99408
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 99408 2002-07-04 12:37:13Z julian $ 63 */ 64 65#include "opt_vm.h" 66 67#include <sys/param.h> 68#include <sys/systm.h> 69#include <sys/lock.h> 70#include <sys/mutex.h> 71#include <sys/proc.h> 72#include <sys/resourcevar.h> 73#include <sys/shm.h> 74#include <sys/vmmeter.h> 75#include <sys/sx.h> 76#include <sys/sysctl.h> 77 78#include <sys/kernel.h> 79#include <sys/ktr.h> 80#include <sys/unistd.h> 81 82#include <machine/limits.h> 83 84#include <vm/vm.h> 85#include <vm/vm_param.h> 86#include <vm/pmap.h> 87#include <vm/vm_map.h> 88#include <vm/vm_page.h> 89#include <vm/vm_pageout.h> 90#include <vm/vm_kern.h> 91#include <vm/vm_extern.h> 92 93#include <sys/user.h> 94 95extern int maxslp; 96 97/* 98 * System initialization 99 * 100 * Note: proc0 from proc.h 101 */ 102static void vm_init_limits(void *); 103SYSINIT(vm_limits, SI_SUB_VM_CONF, SI_ORDER_FIRST, vm_init_limits, &proc0) 104 105/* 106 * THIS MUST BE THE LAST INITIALIZATION ITEM!!! 107 * 108 * Note: run scheduling should be divorced from the vm system. 109 */ 110static void scheduler(void *); 111SYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_FIRST, scheduler, NULL) 112 113#ifndef NO_SWAPPING 114static void swapout(struct proc *); 115#endif 116 117/* 118 * MPSAFE 119 */ 120int 121kernacc(addr, len, rw) 122 caddr_t addr; 123 int len, rw; 124{ 125 boolean_t rv; 126 vm_offset_t saddr, eaddr; 127 vm_prot_t prot; 128 129 KASSERT((rw & ~VM_PROT_ALL) == 0, 130 ("illegal ``rw'' argument to kernacc (%x)\n", rw)); 131 prot = rw; 132 saddr = trunc_page((vm_offset_t)addr); 133 eaddr = round_page((vm_offset_t)addr + len); 134 rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot); 135 return (rv == TRUE); 136} 137 138/* 139 * MPSAFE 140 */ 141int 142useracc(addr, len, rw) 143 caddr_t addr; 144 int len, rw; 145{ 146 boolean_t rv; 147 vm_prot_t prot; 148 149 KASSERT((rw & ~VM_PROT_ALL) == 0, 150 ("illegal ``rw'' argument to useracc (%x)\n", rw)); 151 prot = rw; 152 /* 153 * XXX - check separately to disallow access to user area and user 154 * page tables - they are in the map. 155 * 156 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max. It was once 157 * only used (as an end address) in trap.c. Use it as an end address 158 * here too. This bogusness has spread. I just fixed where it was 159 * used as a max in vm_mmap.c. 160 */ 161 if ((vm_offset_t) addr + len > /* XXX */ VM_MAXUSER_ADDRESS 162 || (vm_offset_t) addr + len < (vm_offset_t) addr) { 163 return (FALSE); 164 } 165 rv = vm_map_check_protection(&curproc->p_vmspace->vm_map, 166 trunc_page((vm_offset_t)addr), round_page((vm_offset_t)addr + len), 167 prot); 168 return (rv == TRUE); 169} 170 171/* 172 * MPSAFE 173 */ 174void 175vslock(addr, len) 176 caddr_t addr; 177 u_int len; 178{ 179 180 vm_map_wire(&curproc->p_vmspace->vm_map, trunc_page((vm_offset_t)addr), 181 round_page((vm_offset_t)addr + len), FALSE); 182} 183 184/* 185 * MPSAFE 186 */ 187void 188vsunlock(addr, len) 189 caddr_t addr; 190 u_int len; 191{ 192 193 vm_map_unwire(&curproc->p_vmspace->vm_map, 194 trunc_page((vm_offset_t)addr), 195 round_page((vm_offset_t)addr + len), FALSE); 196} 197 198/* 199 * Implement fork's actions on an address space. 200 * Here we arrange for the address space to be copied or referenced, 201 * allocate a user struct (pcb and kernel stack), then call the 202 * machine-dependent layer to fill those in and make the new process 203 * ready to run. The new process is set up so that it returns directly 204 * to user mode to avoid stack copying and relocation problems. 205 */ 206void 207vm_forkproc(td, p2, td2, flags) 208 struct thread *td; 209 struct proc *p2; 210 struct thread *td2; 211 int flags; 212{ 213 struct proc *p1 = td->td_proc; 214 struct user *up; 215 216 GIANT_REQUIRED; 217 218 if ((flags & RFPROC) == 0) { 219 /* 220 * Divorce the memory, if it is shared, essentially 221 * this changes shared memory amongst threads, into 222 * COW locally. 223 */ 224 if ((flags & RFMEM) == 0) { 225 if (p1->p_vmspace->vm_refcnt > 1) { 226 vmspace_unshare(p1); 227 } 228 } 229 cpu_fork(td, p2, td2, flags); 230 return; 231 } 232 233 if (flags & RFMEM) { 234 p2->p_vmspace = p1->p_vmspace; 235 p1->p_vmspace->vm_refcnt++; 236 } 237 238 while (vm_page_count_severe()) { 239 VM_WAIT; 240 } 241 242 if ((flags & RFMEM) == 0) { 243 p2->p_vmspace = vmspace_fork(p1->p_vmspace); 244 245 pmap_pinit2(vmspace_pmap(p2->p_vmspace)); 246 247 if (p1->p_vmspace->vm_shm) 248 shmfork(p1, p2); 249 } 250 251 pmap_new_proc(p2); 252 253 /* XXXKSE this is unsatisfactory but should be adequate */ 254 up = p2->p_uarea; 255 256 /* 257 * p_stats currently points at fields in the user struct 258 * but not at &u, instead at p_addr. Copy parts of 259 * p_stats; zero the rest of p_stats (statistics). 260 * 261 * If procsig->ps_refcnt is 1 and p2->p_sigacts is NULL we dont' need 262 * to share sigacts, so we use the up->u_sigacts. 263 */ 264 p2->p_stats = &up->u_stats; 265 if (p2->p_sigacts == NULL) { 266 if (p2->p_procsig->ps_refcnt != 1) 267 printf ("PID:%d NULL sigacts with refcnt not 1!\n",p2->p_pid); 268 p2->p_sigacts = &up->u_sigacts; 269 up->u_sigacts = *p1->p_sigacts; 270 } 271 272 bzero(&up->u_stats.pstat_startzero, 273 (unsigned) ((caddr_t) &up->u_stats.pstat_endzero - 274 (caddr_t) &up->u_stats.pstat_startzero)); 275 bcopy(&p1->p_stats->pstat_startcopy, &up->u_stats.pstat_startcopy, 276 ((caddr_t) &up->u_stats.pstat_endcopy - 277 (caddr_t) &up->u_stats.pstat_startcopy)); 278 279 280 /* 281 * cpu_fork will copy and update the pcb, set up the kernel stack, 282 * and make the child ready to run. 283 */ 284 cpu_fork(td, p2, td2, flags); 285} 286 287/* 288 * Called after process has been wait(2)'ed apon and is being reaped. 289 * The idea is to reclaim resources that we could not reclaim while 290 * the process was still executing. 291 */ 292void 293vm_waitproc(p) 294 struct proc *p; 295{ 296 struct thread *td; 297 298 GIANT_REQUIRED; 299 cpu_wait(p); 300 pmap_dispose_proc(p); /* drop per-process resources */ 301/* XXXKSE by here there should not be any threads left! */ 302 FOREACH_THREAD_IN_PROC(p, td) { 303 panic("vm_waitproc: Survivor thread!"); 304 } 305 vmspace_exitfree(p); /* and clean-out the vmspace */ 306} 307 308/* 309 * Set default limits for VM system. 310 * Called for proc 0, and then inherited by all others. 311 * 312 * XXX should probably act directly on proc0. 313 */ 314static void 315vm_init_limits(udata) 316 void *udata; 317{ 318 struct proc *p = udata; 319 int rss_limit; 320 321 /* 322 * Set up the initial limits on process VM. Set the maximum resident 323 * set size to be half of (reasonably) available memory. Since this 324 * is a soft limit, it comes into effect only when the system is out 325 * of memory - half of main memory helps to favor smaller processes, 326 * and reduces thrashing of the object cache. 327 */ 328 p->p_rlimit[RLIMIT_STACK].rlim_cur = dflssiz; 329 p->p_rlimit[RLIMIT_STACK].rlim_max = maxssiz; 330 p->p_rlimit[RLIMIT_DATA].rlim_cur = dfldsiz; 331 p->p_rlimit[RLIMIT_DATA].rlim_max = maxdsiz; 332 /* limit the limit to no less than 2MB */ 333 rss_limit = max(cnt.v_free_count, 512); 334 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit); 335 p->p_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY; 336} 337 338void 339faultin(p) 340 struct proc *p; 341{ 342 struct thread *td; 343 GIANT_REQUIRED; 344 345 PROC_LOCK_ASSERT(p, MA_OWNED); 346 mtx_lock_spin(&sched_lock); 347 if ((p->p_sflag & PS_INMEM) == 0) { 348 ++p->p_lock; 349 mtx_unlock_spin(&sched_lock); 350 PROC_UNLOCK(p); 351 352 pmap_swapin_proc(p); 353 FOREACH_THREAD_IN_PROC (p, td) 354 pmap_swapin_thread(td); 355 356 PROC_LOCK(p); 357 mtx_lock_spin(&sched_lock); 358 FOREACH_THREAD_IN_PROC (p, td) 359 if (td->td_state == TDS_RUNQ) /* XXXKSE */ 360 setrunqueue(td); 361 362 p->p_sflag |= PS_INMEM; 363 364 /* undo the effect of setting SLOCK above */ 365 --p->p_lock; 366 } 367 mtx_unlock_spin(&sched_lock); 368} 369 370/* 371 * This swapin algorithm attempts to swap-in processes only if there 372 * is enough space for them. Of course, if a process waits for a long 373 * time, it will be swapped in anyway. 374 * 375 * XXXKSE - process with the thread with highest priority counts.. 376 * 377 * Giant is still held at this point, to be released in tsleep. 378 */ 379/* ARGSUSED*/ 380static void 381scheduler(dummy) 382 void *dummy; 383{ 384 struct proc *p; 385 struct thread *td; 386 int pri; 387 struct proc *pp; 388 int ppri; 389 390 mtx_assert(&Giant, MA_OWNED | MA_NOTRECURSED); 391 /* GIANT_REQUIRED */ 392 393loop: 394 if (vm_page_count_min()) { 395 VM_WAIT; 396 goto loop; 397 } 398 399 pp = NULL; 400 ppri = INT_MIN; 401 sx_slock(&allproc_lock); 402 FOREACH_PROC_IN_SYSTEM(p) { 403 struct ksegrp *kg; 404 if (p->p_sflag & (PS_INMEM | PS_SWAPPING)) { 405 continue; 406 } 407 mtx_lock_spin(&sched_lock); 408 FOREACH_THREAD_IN_PROC(p, td) { 409 /* Only consider runnable threads */ 410 if (td->td_state == TDS_RUNQ) { 411 kg = td->td_ksegrp; 412 pri = p->p_swtime + kg->kg_slptime; 413 if ((p->p_sflag & PS_SWAPINREQ) == 0) { 414 pri -= kg->kg_nice * 8; 415 } 416 417 /* 418 * if this ksegrp is higher priority 419 * and there is enough space, then select 420 * this process instead of the previous 421 * selection. 422 */ 423 if (pri > ppri) { 424 pp = p; 425 ppri = pri; 426 } 427 } 428 } 429 mtx_unlock_spin(&sched_lock); 430 } 431 sx_sunlock(&allproc_lock); 432 433 /* 434 * Nothing to do, back to sleep. 435 */ 436 if ((p = pp) == NULL) { 437 tsleep(&proc0, PVM, "sched", maxslp * hz / 2); 438 goto loop; 439 } 440 mtx_lock_spin(&sched_lock); 441 p->p_sflag &= ~PS_SWAPINREQ; 442 mtx_unlock_spin(&sched_lock); 443 444 /* 445 * We would like to bring someone in. (only if there is space). 446 * [What checks the space? ] 447 */ 448 PROC_LOCK(p); 449 faultin(p); 450 PROC_UNLOCK(p); 451 mtx_lock_spin(&sched_lock); 452 p->p_swtime = 0; 453 mtx_unlock_spin(&sched_lock); 454 goto loop; 455} 456 457#ifndef NO_SWAPPING 458 459/* 460 * Swap_idle_threshold1 is the guaranteed swapped in time for a process 461 */ 462static int swap_idle_threshold1 = 2; 463SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold1, 464 CTLFLAG_RW, &swap_idle_threshold1, 0, ""); 465 466/* 467 * Swap_idle_threshold2 is the time that a process can be idle before 468 * it will be swapped out, if idle swapping is enabled. 469 */ 470static int swap_idle_threshold2 = 10; 471SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold2, 472 CTLFLAG_RW, &swap_idle_threshold2, 0, ""); 473 474/* 475 * Swapout is driven by the pageout daemon. Very simple, we find eligible 476 * procs and unwire their u-areas. We try to always "swap" at least one 477 * process in case we need the room for a swapin. 478 * If any procs have been sleeping/stopped for at least maxslp seconds, 479 * they are swapped. Else, we swap the longest-sleeping or stopped process, 480 * if any, otherwise the longest-resident process. 481 */ 482void 483swapout_procs(action) 484int action; 485{ 486 struct proc *p; 487 struct thread *td; 488 struct ksegrp *kg; 489 struct proc *outp, *outp2; 490 int outpri, outpri2; 491 int didswap = 0; 492 493 GIANT_REQUIRED; 494 495 outp = outp2 = NULL; 496 outpri = outpri2 = INT_MIN; 497retry: 498 sx_slock(&allproc_lock); 499 FOREACH_PROC_IN_SYSTEM(p) { 500 struct vmspace *vm; 501 int minslptime = 100000; 502 503 PROC_LOCK(p); 504 if (p->p_lock != 0 || 505 (p->p_flag & (P_STOPPED_SNGL|P_TRACED|P_SYSTEM|P_WEXIT)) != 0) { 506 PROC_UNLOCK(p); 507 continue; 508 } 509 /* 510 * only aiod changes vmspace, however it will be 511 * skipped because of the if statement above checking 512 * for P_SYSTEM 513 */ 514 vm = p->p_vmspace; 515 mtx_lock_spin(&sched_lock); 516 if ((p->p_sflag & (PS_INMEM|PS_SWAPPING)) != PS_INMEM) { 517 mtx_unlock_spin(&sched_lock); 518 PROC_UNLOCK(p); 519 continue; 520 } 521 522 switch (p->p_state) { 523 default: 524 /* Don't swap out processes in any sort 525 * of 'special' state. */ 526 mtx_unlock_spin(&sched_lock); 527 PROC_UNLOCK(p); 528 continue; 529 530 case PRS_NORMAL: 531 /* 532 * do not swapout a realtime process 533 * Check all the thread groups.. 534 */ 535 FOREACH_KSEGRP_IN_PROC(p, kg) { 536 if (PRI_IS_REALTIME(kg->kg_pri_class)) { 537 mtx_unlock_spin(&sched_lock); 538 PROC_UNLOCK(p); 539 goto nextproc; 540 } 541 542 /* 543 * Do not swapout a process waiting 544 * on a critical event of some kind. 545 * Also guarantee swap_idle_threshold1 546 * time in memory. 547 */ 548 if (kg->kg_slptime < swap_idle_threshold1) { 549 mtx_unlock_spin(&sched_lock); 550 PROC_UNLOCK(p); 551 goto nextproc; 552 } 553 FOREACH_THREAD_IN_PROC(p, td) { 554 if ((td->td_priority) < PSOCK) { 555 mtx_unlock_spin(&sched_lock); 556 PROC_UNLOCK(p); 557 goto nextproc; 558 } 559 } 560 /* 561 * If the system is under memory stress, 562 * or if we are swapping 563 * idle processes >= swap_idle_threshold2, 564 * then swap the process out. 565 */ 566 if (((action & VM_SWAP_NORMAL) == 0) && 567 (((action & VM_SWAP_IDLE) == 0) || 568 (kg->kg_slptime < swap_idle_threshold2))) { 569 mtx_unlock_spin(&sched_lock); 570 PROC_UNLOCK(p); 571 goto nextproc; 572 } 573 if (minslptime > kg->kg_slptime) 574 minslptime = kg->kg_slptime; 575 } 576 577 mtx_unlock_spin(&sched_lock); 578 ++vm->vm_refcnt; 579 /* 580 * do not swapout a process that 581 * is waiting for VM 582 * data structures there is a 583 * possible deadlock. 584 */ 585 if (!vm_map_trylock(&vm->vm_map)) { 586 vmspace_free(vm); 587 PROC_UNLOCK(p); 588 goto nextproc; 589 } 590 vm_map_unlock(&vm->vm_map); 591 /* 592 * If the process has been asleep for awhile and had 593 * most of its pages taken away already, swap it out. 594 */ 595 if ((action & VM_SWAP_NORMAL) || 596 ((action & VM_SWAP_IDLE) && 597 (minslptime > swap_idle_threshold2))) { 598 sx_sunlock(&allproc_lock); 599 swapout(p); 600 vmspace_free(vm); 601 didswap++; 602 goto retry; 603 } 604 PROC_UNLOCK(p); 605 vmspace_free(vm); 606 } 607nextproc: 608 continue; 609 } 610 sx_sunlock(&allproc_lock); 611 /* 612 * If we swapped something out, and another process needed memory, 613 * then wakeup the sched process. 614 */ 615 if (didswap) 616 wakeup(&proc0); 617} 618 619static void 620swapout(p) 621 struct proc *p; 622{ 623 struct thread *td; 624 625 PROC_LOCK_ASSERT(p, MA_OWNED); 626#if defined(SWAP_DEBUG) 627 printf("swapping out %d\n", p->p_pid); 628#endif 629 ++p->p_stats->p_ru.ru_nswap; 630 /* 631 * remember the process resident count 632 */ 633 p->p_vmspace->vm_swrss = vmspace_resident_count(p->p_vmspace); 634 635 mtx_lock_spin(&sched_lock); 636 p->p_sflag &= ~PS_INMEM; 637 p->p_sflag |= PS_SWAPPING; 638 PROC_UNLOCK(p); 639 FOREACH_THREAD_IN_PROC (p, td) 640 if (td->td_state == TDS_RUNQ) /* XXXKSE */ 641 remrunqueue(td); /* XXXKSE */ 642 mtx_unlock_spin(&sched_lock); 643 644 pmap_swapout_proc(p); 645 FOREACH_THREAD_IN_PROC(p, td) 646 pmap_swapout_thread(td); 647 mtx_lock_spin(&sched_lock); 648 p->p_sflag &= ~PS_SWAPPING; 649 p->p_swtime = 0; 650 mtx_unlock_spin(&sched_lock); 651} 652#endif /* !NO_SWAPPING */ 653