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$ 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 96 extern int maxslp; 97 98 /* 99 * System initialization 100 * 101 * Note: proc0 from proc.h 102 */ 103 104 static void vm_init_limits __P((void *)); 105 SYSINIT(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 */ 112 static void scheduler __P((void *)); 113 SYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_FIRST, scheduler, NULL) 114 115 116 static void swapout __P((struct proc *)); 117 118 int 119 kernacc(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 138 int 139 useracc(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 GIANT_REQUIRED; 149 150 KASSERT((rw & (~VM_PROT_ALL)) == 0, 151 ("illegal ``rw'' argument to useracc (%x)\n", rw)); 152 prot = rw; 153 /* 154 * XXX - check separately to disallow access to user area and user 155 * page tables - they are in the map. 156 * 157 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max. It was once 158 * only used (as an end address) in trap.c. Use it as an end address 159 * here too. This bogusness has spread. I just fixed where it was 160 * used as a max in vm_mmap.c. 161 */ 162 if ((vm_offset_t) addr + len > /* XXX */ VM_MAXUSER_ADDRESS 163 || (vm_offset_t) addr + len < (vm_offset_t) addr) { 164 return (FALSE); 165 } 166 map = &curproc->p_vmspace->vm_map; 167 vm_map_lock_read(map); 168 /* 169 * We save the map hint, and restore it. Useracc appears to distort 170 * the map hint unnecessarily. 171 */ 172 save_hint = map->hint; 173 rv = vm_map_check_protection(map, 174 trunc_page((vm_offset_t)addr), round_page((vm_offset_t)addr + len), prot); 175 map->hint = save_hint; 176 vm_map_unlock_read(map); 177 178 return (rv == TRUE); 179 } 180 181 void 182 vslock(addr, len) 183 caddr_t addr; 184 u_int len; 185 { 186 GIANT_REQUIRED; 187 vm_map_pageable(&curproc->p_vmspace->vm_map, 188 trunc_page((vm_offset_t)addr), 189 round_page((vm_offset_t)addr + len), FALSE); 190 } 191 192 void 193 vsunlock(addr, len) 194 caddr_t addr; 195 u_int len; 196 { 197 GIANT_REQUIRED; 198 vm_map_pageable(&curproc->p_vmspace->vm_map, 199 trunc_page((vm_offset_t)addr), 200 round_page((vm_offset_t)addr + len), TRUE); 201 } 202 203 /* 204 * Implement fork's actions on an address space. 205 * Here we arrange for the address space to be copied or referenced, 206 * allocate a user struct (pcb and kernel stack), then call the 207 * machine-dependent layer to fill those in and make the new process 208 * ready to run. The new process is set up so that it returns directly 209 * to user mode to avoid stack copying and relocation problems. 210 */ 211 void 212 vm_fork(p1, p2, flags) 213 struct proc *p1, *p2; 214 int flags; 215 { 216 struct user *up; 217 218 GIANT_REQUIRED; 219 220 if ((flags & RFPROC) == 0) { 221 /* 222 * Divorce the memory, if it is shared, essentially 223 * this changes shared memory amongst threads, into 224 * COW locally. 225 */ 226 if ((flags & RFMEM) == 0) { 227 if (p1->p_vmspace->vm_refcnt > 1) { 228 vmspace_unshare(p1); 229 } 230 } 231 cpu_fork(p1, p2, flags); 232 return; 233 } 234 235 if (flags & RFMEM) { 236 p2->p_vmspace = p1->p_vmspace; 237 p1->p_vmspace->vm_refcnt++; 238 } 239 240 while (vm_page_count_severe()) { 241 VM_WAIT; 242 } 243 244 if ((flags & RFMEM) == 0) { 245 p2->p_vmspace = vmspace_fork(p1->p_vmspace); 246 247 pmap_pinit2(vmspace_pmap(p2->p_vmspace)); 248 249 if (p1->p_vmspace->vm_shm) 250 shmfork(p1, p2); 251 } 252 253 pmap_new_proc(p2); 254 255 up = p2->p_addr; 256 257 /* 258 * p_stats currently points at fields in the user struct 259 * but not at &u, instead at p_addr. Copy parts of 260 * p_stats; zero the rest of p_stats (statistics). 261 * 262 * If procsig->ps_refcnt is 1 and p2->p_sigacts is NULL we dont' need 263 * to share sigacts, so we use the up->u_sigacts. 264 */ 265 p2->p_stats = &up->u_stats; 266 if (p2->p_sigacts == NULL) { 267 if (p2->p_procsig->ps_refcnt != 1) 268 printf ("PID:%d NULL sigacts with refcnt not 1!\n",p2->p_pid); 269 p2->p_sigacts = &up->u_sigacts; 270 up->u_sigacts = *p1->p_sigacts; 271 } 272 273 bzero(&up->u_stats.pstat_startzero, 274 (unsigned) ((caddr_t) &up->u_stats.pstat_endzero - 275 (caddr_t) &up->u_stats.pstat_startzero)); 276 bcopy(&p1->p_stats->pstat_startcopy, &up->u_stats.pstat_startcopy, 277 ((caddr_t) &up->u_stats.pstat_endcopy - 278 (caddr_t) &up->u_stats.pstat_startcopy)); 279 280 281 /* 282 * cpu_fork will copy and update the pcb, set up the kernel stack, 283 * and make the child ready to run. 284 */ 285 cpu_fork(p1, p2, flags); 286 } 287 288 /* 289 * Set default limits for VM system. 290 * Called for proc 0, and then inherited by all others. 291 * 292 * XXX should probably act directly on proc0. 293 */ 294 static void 295 vm_init_limits(udata) 296 void *udata; 297 { 298 struct proc *p = udata; 299 int rss_limit; 300 301 /* 302 * Set up the initial limits on process VM. Set the maximum resident 303 * set size to be half of (reasonably) available memory. Since this 304 * is a soft limit, it comes into effect only when the system is out 305 * of memory - half of main memory helps to favor smaller processes, 306 * and reduces thrashing of the object cache. 307 */ 308 p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ; 309 p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ; 310 p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ; 311 p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ; 312 /* limit the limit to no less than 2MB */ 313 rss_limit = max(cnt.v_free_count, 512); 314 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit); 315 p->p_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY; 316 } 317 318 /* 319 * Must be called with the proc struc mutex held. 320 */ 321 void 322 faultin(p) 323 struct proc *p; 324 { 325 GIANT_REQUIRED; 326 327 PROC_LOCK_ASSERT(p, MA_OWNED); 328 mtx_lock_spin(&sched_lock); 329 if ((p->p_sflag & PS_INMEM) == 0) { 330 ++p->p_lock; 331 mtx_unlock_spin(&sched_lock); 332 PROC_UNLOCK(p); 333 334 pmap_swapin_proc(p); 335 336 PROC_LOCK(p); 337 mtx_lock_spin(&sched_lock); 338 if (p->p_stat == SRUN) { 339 setrunqueue(p); 340 } 341 342 p->p_sflag |= PS_INMEM; 343 344 /* undo the effect of setting SLOCK above */ 345 --p->p_lock; 346 } 347 mtx_unlock_spin(&sched_lock); 348 } 349 350 /* 351 * This swapin algorithm attempts to swap-in processes only if there 352 * is enough space for them. Of course, if a process waits for a long 353 * time, it will be swapped in anyway. 354 * 355 * Giant is still held at this point, to be released in tsleep. 356 */ 357 /* ARGSUSED*/ 358 static void 359 scheduler(dummy) 360 void *dummy; 361 { 362 struct proc *p; 363 int pri; 364 struct proc *pp; 365 int ppri; 366 367 mtx_assert(&Giant, MA_OWNED | MA_NOTRECURSED); 368 /* GIANT_REQUIRED */ 369 370 loop: 371 if (vm_page_count_min()) { 372 VM_WAIT; 373 goto loop; 374 } 375 376 pp = NULL; 377 ppri = INT_MIN; 378 sx_slock(&allproc_lock); 379 LIST_FOREACH(p, &allproc, p_list) { 380 mtx_lock_spin(&sched_lock); 381 if (p->p_stat == SRUN && 382 (p->p_sflag & (PS_INMEM | PS_SWAPPING)) == 0) { 383 384 pri = p->p_swtime + p->p_slptime; 385 if ((p->p_sflag & PS_SWAPINREQ) == 0) { 386 pri -= p->p_nice * 8; 387 } 388 389 /* 390 * if this process is higher priority and there is 391 * enough space, then select this process instead of 392 * the previous selection. 393 */ 394 if (pri > ppri) { 395 pp = p; 396 ppri = pri; 397 } 398 } 399 mtx_unlock_spin(&sched_lock); 400 } 401 sx_sunlock(&allproc_lock); 402 403 /* 404 * Nothing to do, back to sleep. 405 */ 406 if ((p = pp) == NULL) { 407 tsleep(&proc0, PVM, "sched", maxslp * hz / 2); 408 goto loop; 409 } 410 mtx_lock_spin(&sched_lock); 411 p->p_sflag &= ~PS_SWAPINREQ; 412 mtx_unlock_spin(&sched_lock); 413 414 /* 415 * We would like to bring someone in. (only if there is space). 416 */ 417 PROC_LOCK(p); 418 faultin(p); 419 PROC_UNLOCK(p); 420 mtx_lock_spin(&sched_lock); 421 p->p_swtime = 0; 422 mtx_unlock_spin(&sched_lock); 423 goto loop; 424 } 425 426 #ifndef NO_SWAPPING 427 428 /* 429 * Swap_idle_threshold1 is the guaranteed swapped in time for a process 430 */ 431 static int swap_idle_threshold1 = 2; 432 SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold1, 433 CTLFLAG_RW, &swap_idle_threshold1, 0, ""); 434 435 /* 436 * Swap_idle_threshold2 is the time that a process can be idle before 437 * it will be swapped out, if idle swapping is enabled. 438 */ 439 static int swap_idle_threshold2 = 10; 440 SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold2, 441 CTLFLAG_RW, &swap_idle_threshold2, 0, ""); 442 443 /* 444 * Swapout is driven by the pageout daemon. Very simple, we find eligible 445 * procs and unwire their u-areas. We try to always "swap" at least one 446 * process in case we need the room for a swapin. 447 * If any procs have been sleeping/stopped for at least maxslp seconds, 448 * they are swapped. Else, we swap the longest-sleeping or stopped process, 449 * if any, otherwise the longest-resident process. 450 */ 451 void 452 swapout_procs(action) 453 int action; 454 { 455 struct proc *p; 456 struct proc *outp, *outp2; 457 int outpri, outpri2; 458 int didswap = 0; 459 460 GIANT_REQUIRED; 461 462 outp = outp2 = NULL; 463 outpri = outpri2 = INT_MIN; 464 retry: 465 sx_slock(&allproc_lock); 466 LIST_FOREACH(p, &allproc, p_list) { 467 struct vmspace *vm; 468 469 PROC_LOCK(p); 470 if (p->p_lock != 0 || 471 (p->p_flag & (P_TRACED|P_SYSTEM|P_WEXIT)) != 0) { 472 PROC_UNLOCK(p); 473 continue; 474 } 475 /* 476 * only aiod changes vmspace, however it will be 477 * skipped because of the if statement above checking 478 * for P_SYSTEM 479 */ 480 vm = p->p_vmspace; 481 mtx_lock_spin(&sched_lock); 482 if ((p->p_sflag & (PS_INMEM|PS_SWAPPING)) != PS_INMEM) { 483 mtx_unlock_spin(&sched_lock); 484 PROC_UNLOCK(p); 485 continue; 486 } 487 488 switch (p->p_stat) { 489 default: 490 mtx_unlock_spin(&sched_lock); 491 PROC_UNLOCK(p); 492 continue; 493 494 case SSLEEP: 495 case SSTOP: 496 /* 497 * do not swapout a realtime process 498 */ 499 if (PRI_IS_REALTIME(p->p_pri.pri_class)) { 500 mtx_unlock_spin(&sched_lock); 501 PROC_UNLOCK(p); 502 continue; 503 } 504 505 /* 506 * Do not swapout a process waiting on a critical 507 * event of some kind. Also guarantee swap_idle_threshold1 508 * time in memory. 509 */ 510 if (((p->p_pri.pri_level) < PSOCK) || 511 (p->p_slptime < swap_idle_threshold1)) { 512 mtx_unlock_spin(&sched_lock); 513 PROC_UNLOCK(p); 514 continue; 515 } 516 517 /* 518 * If the system is under memory stress, or if we are swapping 519 * idle processes >= swap_idle_threshold2, then swap the process 520 * out. 521 */ 522 if (((action & VM_SWAP_NORMAL) == 0) && 523 (((action & VM_SWAP_IDLE) == 0) || 524 (p->p_slptime < swap_idle_threshold2))) { 525 mtx_unlock_spin(&sched_lock); 526 PROC_UNLOCK(p); 527 continue; 528 } 529 mtx_unlock_spin(&sched_lock); 530 531 ++vm->vm_refcnt; 532 /* 533 * do not swapout a process that is waiting for VM 534 * data structures there is a possible deadlock. 535 */ 536 if (lockmgr(&vm->vm_map.lock, 537 LK_EXCLUSIVE | LK_NOWAIT, 538 NULL, curproc)) { 539 vmspace_free(vm); 540 PROC_UNLOCK(p); 541 continue; 542 } 543 vm_map_unlock(&vm->vm_map); 544 /* 545 * If the process has been asleep for awhile and had 546 * most of its pages taken away already, swap it out. 547 */ 548 if ((action & VM_SWAP_NORMAL) || 549 ((action & VM_SWAP_IDLE) && 550 (p->p_slptime > swap_idle_threshold2))) { 551 sx_sunlock(&allproc_lock); 552 swapout(p); 553 vmspace_free(vm); 554 didswap++; 555 goto retry; 556 } 557 PROC_UNLOCK(p); 558 vmspace_free(vm); 559 } 560 } 561 sx_sunlock(&allproc_lock); 562 /* 563 * If we swapped something out, and another process needed memory, 564 * then wakeup the sched process. 565 */ 566 if (didswap) 567 wakeup(&proc0); 568 } 569 570 static void 571 swapout(p) 572 struct proc *p; 573 { 574 575 PROC_LOCK_ASSERT(p, MA_OWNED); 576 #if defined(SWAP_DEBUG) 577 printf("swapping out %d\n", p->p_pid); 578 #endif 579 ++p->p_stats->p_ru.ru_nswap; 580 /* 581 * remember the process resident count 582 */ 583 p->p_vmspace->vm_swrss = vmspace_resident_count(p->p_vmspace); 584 585 mtx_lock_spin(&sched_lock); 586 p->p_sflag &= ~PS_INMEM; 587 p->p_sflag |= PS_SWAPPING; 588 PROC_UNLOCK_NOSWITCH(p); 589 if (p->p_stat == SRUN) 590 remrunqueue(p); 591 mtx_unlock_spin(&sched_lock); 592 593 pmap_swapout_proc(p); 594 595 mtx_lock_spin(&sched_lock); 596 p->p_sflag &= ~PS_SWAPPING; 597 p->p_swtime = 0; 598 mtx_unlock_spin(&sched_lock); 599 } 600 #endif /* !NO_SWAPPING */ 601