2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * The Mach Operating System project at Carnegie-Mellon University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * from: @(#)vm_glue.c 8.6 (Berkeley) 1/5/94
39 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40 * All rights reserved.
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.
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.
52 * Carnegie Mellon requests users of this software to return to
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
67 #include <sys/param.h>
68 #include <sys/systm.h>
70 #include <sys/mutex.h>
72 #include <sys/resourcevar.h>
74 #include <sys/vmmeter.h>
76 #include <sys/sysctl.h>
78 #include <sys/kernel.h>
80 #include <sys/unistd.h>
82 #include <machine/limits.h>
85 #include <vm/vm_param.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>
98 * System initialization
100 * Note: proc0 from proc.h
102 static void vm_init_limits(void *);
103 SYSINIT(vm_limits, SI_SUB_VM_CONF, SI_ORDER_FIRST, vm_init_limits, &proc0)
106 * THIS MUST BE THE LAST INITIALIZATION ITEM!!!
108 * Note: run scheduling should be divorced from the vm system.
110 static void scheduler(void *);
111 SYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_FIRST, scheduler, NULL)
114 static void swapout(struct proc *);
121 kernacc(addr, len, rw)
126 vm_offset_t saddr, eaddr;
129 KASSERT((rw & ~VM_PROT_ALL) == 0,
130 ("illegal ``rw'' argument to kernacc (%x)\n", 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);
142 useracc(addr, len, rw)
149 KASSERT((rw & ~VM_PROT_ALL) == 0,
150 ("illegal ``rw'' argument to useracc (%x)\n", rw));
153 * XXX - check separately to disallow access to user area and user
154 * page tables - they are in the map.
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.
161 if ((vm_offset_t) addr + len > /* XXX */ VM_MAXUSER_ADDRESS
162 || (vm_offset_t) addr + len < (vm_offset_t) addr) {
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),
180 vm_map_wire(&curproc->p_vmspace->vm_map, trunc_page((vm_offset_t)addr),
181 round_page((vm_offset_t)addr + len), FALSE);
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);
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.
207 vm_forkproc(td, p2, td2, flags)
213 struct proc *p1 = td->td_proc;
218 if ((flags & RFPROC) == 0) {
220 * Divorce the memory, if it is shared, essentially
221 * this changes shared memory amongst threads, into
224 if ((flags & RFMEM) == 0) {
225 if (p1->p_vmspace->vm_refcnt > 1) {
229 cpu_fork(td, p2, td2, flags);
234 p2->p_vmspace = p1->p_vmspace;
235 p1->p_vmspace->vm_refcnt++;
238 while (vm_page_count_severe()) {
242 if ((flags & RFMEM) == 0) {
243 p2->p_vmspace = vmspace_fork(p1->p_vmspace);
245 pmap_pinit2(vmspace_pmap(p2->p_vmspace));
247 if (p1->p_vmspace->vm_shm)
252 pmap_new_thread(td2); /* Initial thread */
254 /* XXXKSE this is unsatisfactory but should be adequate */
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).
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.
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;
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));
282 * cpu_fork will copy and update the pcb, set up the kernel stack,
283 * and make the child ready to run.
285 cpu_fork(td, p2, td2, flags);
289 * Called after process has been wait(2)'ed apon and is being reaped.
290 * The idea is to reclaim resources that we could not reclaim while
291 * the process was still executing.
301 pmap_dispose_proc(p); /* drop per-process resources */
302 /* XXXKSE by here there should not be any threads left! */
303 FOREACH_THREAD_IN_PROC(p, td) {
304 panic("vm_waitproc: Survivor thread!");
305 pmap_dispose_thread(td);
307 vmspace_exitfree(p); /* and clean-out the vmspace */
311 * Set default limits for VM system.
312 * Called for proc 0, and then inherited by all others.
314 * XXX should probably act directly on proc0.
317 vm_init_limits(udata)
320 struct proc *p = udata;
324 * Set up the initial limits on process VM. Set the maximum resident
325 * set size to be half of (reasonably) available memory. Since this
326 * is a soft limit, it comes into effect only when the system is out
327 * of memory - half of main memory helps to favor smaller processes,
328 * and reduces thrashing of the object cache.
330 p->p_rlimit[RLIMIT_STACK].rlim_cur = dflssiz;
331 p->p_rlimit[RLIMIT_STACK].rlim_max = maxssiz;
332 p->p_rlimit[RLIMIT_DATA].rlim_cur = dfldsiz;
333 p->p_rlimit[RLIMIT_DATA].rlim_max = maxdsiz;
334 /* limit the limit to no less than 2MB */
335 rss_limit = max(cnt.v_free_count, 512);
336 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit);
337 p->p_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY;
347 PROC_LOCK_ASSERT(p, MA_OWNED);
348 mtx_lock_spin(&sched_lock);
349 if ((p->p_sflag & PS_INMEM) == 0) {
351 mtx_unlock_spin(&sched_lock);
355 FOREACH_THREAD_IN_PROC (p, td)
356 pmap_swapin_thread(td);
359 mtx_lock_spin(&sched_lock);
360 FOREACH_THREAD_IN_PROC (p, td)
361 if (td->td_state == TDS_RUNQ) /* XXXKSE */
364 p->p_sflag |= PS_INMEM;
366 /* undo the effect of setting SLOCK above */
369 mtx_unlock_spin(&sched_lock);
373 * This swapin algorithm attempts to swap-in processes only if there
374 * is enough space for them. Of course, if a process waits for a long
375 * time, it will be swapped in anyway.
377 * XXXKSE - process with the thread with highest priority counts..
379 * Giant is still held at this point, to be released in tsleep.
392 mtx_assert(&Giant, MA_OWNED | MA_NOTRECURSED);
396 if (vm_page_count_min()) {
403 sx_slock(&allproc_lock);
404 FOREACH_PROC_IN_SYSTEM(p) {
406 if (p->p_sflag & (PS_INMEM | PS_SWAPPING)) {
409 mtx_lock_spin(&sched_lock);
410 FOREACH_THREAD_IN_PROC(p, td) {
411 /* Only consider runnable threads */
412 if (td->td_state == TDS_RUNQ) {
414 pri = p->p_swtime + kg->kg_slptime;
415 if ((p->p_sflag & PS_SWAPINREQ) == 0) {
416 pri -= kg->kg_nice * 8;
420 * if this ksegrp is higher priority
421 * and there is enough space, then select
422 * this process instead of the previous
431 mtx_unlock_spin(&sched_lock);
433 sx_sunlock(&allproc_lock);
436 * Nothing to do, back to sleep.
438 if ((p = pp) == NULL) {
439 tsleep(&proc0, PVM, "sched", maxslp * hz / 2);
442 mtx_lock_spin(&sched_lock);
443 p->p_sflag &= ~PS_SWAPINREQ;
444 mtx_unlock_spin(&sched_lock);
447 * We would like to bring someone in. (only if there is space).
448 * [What checks the space? ]
453 mtx_lock_spin(&sched_lock);
455 mtx_unlock_spin(&sched_lock);
462 * Swap_idle_threshold1 is the guaranteed swapped in time for a process
464 static int swap_idle_threshold1 = 2;
465 SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold1,
466 CTLFLAG_RW, &swap_idle_threshold1, 0, "");
469 * Swap_idle_threshold2 is the time that a process can be idle before
470 * it will be swapped out, if idle swapping is enabled.
472 static int swap_idle_threshold2 = 10;
473 SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold2,
474 CTLFLAG_RW, &swap_idle_threshold2, 0, "");
477 * Swapout is driven by the pageout daemon. Very simple, we find eligible
478 * procs and unwire their u-areas. We try to always "swap" at least one
479 * process in case we need the room for a swapin.
480 * If any procs have been sleeping/stopped for at least maxslp seconds,
481 * they are swapped. Else, we swap the longest-sleeping or stopped process,
482 * if any, otherwise the longest-resident process.
485 swapout_procs(action)
491 struct proc *outp, *outp2;
498 outpri = outpri2 = INT_MIN;
500 sx_slock(&allproc_lock);
501 FOREACH_PROC_IN_SYSTEM(p) {
503 int minslptime = 100000;
506 if (p->p_lock != 0 ||
507 (p->p_flag & (P_STOPPED_SNGL|P_TRACED|P_SYSTEM|P_WEXIT)) != 0) {
512 * only aiod changes vmspace, however it will be
513 * skipped because of the if statement above checking
517 mtx_lock_spin(&sched_lock);
518 if ((p->p_sflag & (PS_INMEM|PS_SWAPPING)) != PS_INMEM) {
519 mtx_unlock_spin(&sched_lock);
524 switch (p->p_state) {
526 /* Don't swap out processes in any sort
527 * of 'special' state. */
528 mtx_unlock_spin(&sched_lock);
534 * do not swapout a realtime process
535 * Check all the thread groups..
537 FOREACH_KSEGRP_IN_PROC(p, kg) {
538 if (PRI_IS_REALTIME(kg->kg_pri_class)) {
539 mtx_unlock_spin(&sched_lock);
545 * Do not swapout a process waiting
546 * on a critical event of some kind.
547 * Also guarantee swap_idle_threshold1
550 if (kg->kg_slptime < swap_idle_threshold1) {
551 mtx_unlock_spin(&sched_lock);
555 FOREACH_THREAD_IN_PROC(p, td) {
556 if ((td->td_priority) < PSOCK) {
557 mtx_unlock_spin(&sched_lock);
563 * If the system is under memory stress,
564 * or if we are swapping
565 * idle processes >= swap_idle_threshold2,
566 * then swap the process out.
568 if (((action & VM_SWAP_NORMAL) == 0) &&
569 (((action & VM_SWAP_IDLE) == 0) ||
570 (kg->kg_slptime < swap_idle_threshold2))) {
571 mtx_unlock_spin(&sched_lock);
575 if (minslptime > kg->kg_slptime)
576 minslptime = kg->kg_slptime;
579 mtx_unlock_spin(&sched_lock);
582 * do not swapout a process that
584 * data structures there is a
587 if (!vm_map_trylock(&vm->vm_map)) {
592 vm_map_unlock(&vm->vm_map);
594 * If the process has been asleep for awhile and had
595 * most of its pages taken away already, swap it out.
597 if ((action & VM_SWAP_NORMAL) ||
598 ((action & VM_SWAP_IDLE) &&
599 (minslptime > swap_idle_threshold2))) {
600 sx_sunlock(&allproc_lock);
612 sx_sunlock(&allproc_lock);
614 * If we swapped something out, and another process needed memory,
615 * then wakeup the sched process.
627 PROC_LOCK_ASSERT(p, MA_OWNED);
628 #if defined(SWAP_DEBUG)
629 printf("swapping out %d\n", p->p_pid);
631 ++p->p_stats->p_ru.ru_nswap;
633 * remember the process resident count
635 p->p_vmspace->vm_swrss = vmspace_resident_count(p->p_vmspace);
637 mtx_lock_spin(&sched_lock);
638 p->p_sflag &= ~PS_INMEM;
639 p->p_sflag |= PS_SWAPPING;
641 FOREACH_THREAD_IN_PROC (p, td)
642 if (td->td_state == TDS_RUNQ) /* XXXKSE */
643 remrunqueue(td); /* XXXKSE */
644 mtx_unlock_spin(&sched_lock);
646 pmap_swapout_proc(p);
647 FOREACH_THREAD_IN_PROC(p, td)
648 pmap_swapout_thread(td);
649 mtx_lock_spin(&sched_lock);
650 p->p_sflag &= ~PS_SWAPPING;
652 mtx_unlock_spin(&sched_lock);
654 #endif /* !NO_SWAPPING */