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
103 static void vm_init_limits __P((void *));
104 SYSINIT(vm_limits, SI_SUB_VM_CONF, SI_ORDER_FIRST, vm_init_limits, &proc0)
107 * THIS MUST BE THE LAST INITIALIZATION ITEM!!!
109 * Note: run scheduling should be divorced from the vm system.
111 static void scheduler __P((void *));
112 SYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_FIRST, scheduler, NULL)
115 static void swapout __P((struct proc *));
119 kernacc(addr, len, rw)
124 vm_offset_t saddr, eaddr;
127 KASSERT((rw & ~VM_PROT_ALL) == 0,
128 ("illegal ``rw'' argument to kernacc (%x)\n", 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);
139 useracc(addr, len, rw)
146 vm_map_entry_t save_hint;
150 KASSERT((rw & ~VM_PROT_ALL) == 0,
151 ("illegal ``rw'' argument to useracc (%x)\n", rw));
154 * XXX - check separately to disallow access to user area and user
155 * page tables - they are in the map.
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.
162 if ((vm_offset_t) addr + len > /* XXX */ VM_MAXUSER_ADDRESS
163 || (vm_offset_t) addr + len < (vm_offset_t) addr) {
166 map = &curproc->p_vmspace->vm_map;
167 vm_map_lock_read(map);
169 * We save the map hint, and restore it. Useracc appears to distort
170 * the map hint unnecessarily.
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);
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);
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);
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.
212 vm_forkproc(td, p2, flags)
217 struct proc *p1 = td->td_proc;
222 if ((flags & RFPROC) == 0) {
224 * Divorce the memory, if it is shared, essentially
225 * this changes shared memory amongst threads, into
228 if ((flags & RFMEM) == 0) {
229 if (p1->p_vmspace->vm_refcnt > 1) {
233 cpu_fork(td, p2, flags);
238 p2->p_vmspace = p1->p_vmspace;
239 p1->p_vmspace->vm_refcnt++;
242 while (vm_page_count_severe()) {
246 if ((flags & RFMEM) == 0) {
247 p2->p_vmspace = vmspace_fork(p1->p_vmspace);
249 pmap_pinit2(vmspace_pmap(p2->p_vmspace));
251 if (p1->p_vmspace->vm_shm)
256 pmap_new_thread(&p2->p_thread); /* Initial thread */
258 /* XXXKSE this is unsatisfactory but should be adequate */
262 * p_stats currently points at fields in the user struct
263 * but not at &u, instead at p_addr. Copy parts of
264 * p_stats; zero the rest of p_stats (statistics).
266 * If procsig->ps_refcnt is 1 and p2->p_sigacts is NULL we dont' need
267 * to share sigacts, so we use the up->u_sigacts.
269 p2->p_stats = &up->u_stats;
270 if (p2->p_sigacts == NULL) {
271 if (p2->p_procsig->ps_refcnt != 1)
272 printf ("PID:%d NULL sigacts with refcnt not 1!\n",p2->p_pid);
273 p2->p_sigacts = &up->u_sigacts;
274 up->u_sigacts = *p1->p_sigacts;
277 bzero(&up->u_stats.pstat_startzero,
278 (unsigned) ((caddr_t) &up->u_stats.pstat_endzero -
279 (caddr_t) &up->u_stats.pstat_startzero));
280 bcopy(&p1->p_stats->pstat_startcopy, &up->u_stats.pstat_startcopy,
281 ((caddr_t) &up->u_stats.pstat_endcopy -
282 (caddr_t) &up->u_stats.pstat_startcopy));
286 * cpu_fork will copy and update the pcb, set up the kernel stack,
287 * and make the child ready to run.
289 cpu_fork(td, p2, flags);
293 * Called after process has been wait(2)'ed apon and is being reaped.
294 * The idea is to reclaim resources that we could not reclaim while
295 * the process was still executing.
305 pmap_dispose_proc(p); /* drop per-process resources */
306 FOREACH_THREAD_IN_PROC(p, td)
307 pmap_dispose_thread(td);
308 vmspace_free(p->p_vmspace); /* and clean-out the vmspace */
312 * Set default limits for VM system.
313 * Called for proc 0, and then inherited by all others.
315 * XXX should probably act directly on proc0.
318 vm_init_limits(udata)
321 struct proc *p = udata;
325 * Set up the initial limits on process VM. Set the maximum resident
326 * set size to be half of (reasonably) available memory. Since this
327 * is a soft limit, it comes into effect only when the system is out
328 * of memory - half of main memory helps to favor smaller processes,
329 * and reduces thrashing of the object cache.
331 p->p_rlimit[RLIMIT_STACK].rlim_cur = dflssiz;
332 p->p_rlimit[RLIMIT_STACK].rlim_max = maxssiz;
333 p->p_rlimit[RLIMIT_DATA].rlim_cur = dfldsiz;
334 p->p_rlimit[RLIMIT_DATA].rlim_max = maxdsiz;
335 /* limit the limit to no less than 2MB */
336 rss_limit = max(cnt.v_free_count, 512);
337 p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit);
338 p->p_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY;
348 PROC_LOCK_ASSERT(p, MA_OWNED);
349 mtx_lock_spin(&sched_lock);
350 if ((p->p_sflag & PS_INMEM) == 0) {
352 mtx_unlock_spin(&sched_lock);
356 FOREACH_THREAD_IN_PROC (p, td)
357 pmap_swapin_thread(td);
360 mtx_lock_spin(&sched_lock);
361 FOREACH_THREAD_IN_PROC (p, td)
362 if (td->td_proc->p_stat == SRUN) /* XXXKSE */
365 p->p_sflag |= PS_INMEM;
367 /* undo the effect of setting SLOCK above */
370 mtx_unlock_spin(&sched_lock);
374 * This swapin algorithm attempts to swap-in processes only if there
375 * is enough space for them. Of course, if a process waits for a long
376 * time, it will be swapped in anyway.
378 * XXXKSE - KSEGRP with highest priority counts..
380 * 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 mtx_lock_spin(&sched_lock);
407 if (p->p_stat == SRUN
408 && (p->p_sflag & (PS_INMEM | PS_SWAPPING)) == 0) {
409 /* Find the minimum sleeptime for the process */
410 FOREACH_KSEGRP_IN_PROC(p, kg) {
411 pri = p->p_swtime + kg->kg_slptime;
412 if ((p->p_sflag & PS_SWAPINREQ) == 0) {
413 pri -= kg->kg_nice * 8;
418 * if this ksegrp is higher priority
419 * and there is enough space, then select
420 * this process instead of the previous
429 mtx_unlock_spin(&sched_lock);
431 sx_sunlock(&allproc_lock);
434 * Nothing to do, back to sleep.
436 if ((p = pp) == NULL) {
437 tsleep(&proc0, PVM, "sched", maxslp * hz / 2);
440 mtx_lock_spin(&sched_lock);
441 p->p_sflag &= ~PS_SWAPINREQ;
442 mtx_unlock_spin(&sched_lock);
445 * We would like to bring someone in. (only if there is space).
450 mtx_lock_spin(&sched_lock);
452 mtx_unlock_spin(&sched_lock);
459 * Swap_idle_threshold1 is the guaranteed swapped in time for a process
461 static int swap_idle_threshold1 = 2;
462 SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold1,
463 CTLFLAG_RW, &swap_idle_threshold1, 0, "");
466 * Swap_idle_threshold2 is the time that a process can be idle before
467 * it will be swapped out, if idle swapping is enabled.
469 static int swap_idle_threshold2 = 10;
470 SYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold2,
471 CTLFLAG_RW, &swap_idle_threshold2, 0, "");
474 * Swapout is driven by the pageout daemon. Very simple, we find eligible
475 * procs and unwire their u-areas. We try to always "swap" at least one
476 * process in case we need the room for a swapin.
477 * If any procs have been sleeping/stopped for at least maxslp seconds,
478 * they are swapped. Else, we swap the longest-sleeping or stopped process,
479 * if any, otherwise the longest-resident process.
482 swapout_procs(action)
487 struct proc *outp, *outp2;
494 outpri = outpri2 = INT_MIN;
496 sx_slock(&allproc_lock);
497 LIST_FOREACH(p, &allproc, p_list) {
499 int minslptime = 100000;
502 if (p->p_lock != 0 ||
503 (p->p_flag & (P_TRACED|P_SYSTEM|P_WEXIT)) != 0) {
508 * only aiod changes vmspace, however it will be
509 * skipped because of the if statement above checking
513 mtx_lock_spin(&sched_lock);
514 if ((p->p_sflag & (PS_INMEM|PS_SWAPPING)) != PS_INMEM) {
515 mtx_unlock_spin(&sched_lock);
522 mtx_unlock_spin(&sched_lock);
529 * do not swapout a realtime process
530 * Check all the thread groups..
532 FOREACH_KSEGRP_IN_PROC(p, kg) {
533 if (PRI_IS_REALTIME(kg->kg_pri.pri_class)) {
534 mtx_unlock_spin(&sched_lock);
540 * Do not swapout a process waiting
541 * on a critical event of some kind.
542 * Also guarantee swap_idle_threshold1
545 if (((kg->kg_pri.pri_level) < PSOCK) ||
546 (kg->kg_slptime < swap_idle_threshold1)) {
547 mtx_unlock_spin(&sched_lock);
553 * If the system is under memory stress,
554 * or if we are swapping
555 * idle processes >= swap_idle_threshold2,
556 * then swap the process out.
558 if (((action & VM_SWAP_NORMAL) == 0) &&
559 (((action & VM_SWAP_IDLE) == 0) ||
560 (kg->kg_slptime < swap_idle_threshold2))) {
561 mtx_unlock_spin(&sched_lock);
565 if (minslptime > kg->kg_slptime)
566 minslptime = kg->kg_slptime;
569 mtx_unlock_spin(&sched_lock);
572 * do not swapout a process that
574 * data structures there is a
577 if (lockmgr(&vm->vm_map.lock,
578 LK_EXCLUSIVE | LK_NOWAIT,
584 vm_map_unlock(&vm->vm_map);
586 * If the process has been asleep for awhile and had
587 * most of its pages taken away already, swap it out.
589 if ((action & VM_SWAP_NORMAL) ||
590 ((action & VM_SWAP_IDLE) &&
591 (minslptime > swap_idle_threshold2))) {
592 sx_sunlock(&allproc_lock);
603 sx_sunlock(&allproc_lock);
605 * If we swapped something out, and another process needed memory,
606 * then wakeup the sched process.
618 PROC_LOCK_ASSERT(p, MA_OWNED);
619 #if defined(SWAP_DEBUG)
620 printf("swapping out %d\n", p->p_pid);
622 ++p->p_stats->p_ru.ru_nswap;
624 * remember the process resident count
626 p->p_vmspace->vm_swrss = vmspace_resident_count(p->p_vmspace);
628 mtx_lock_spin(&sched_lock);
629 p->p_sflag &= ~PS_INMEM;
630 p->p_sflag |= PS_SWAPPING;
632 FOREACH_THREAD_IN_PROC (p, td)
633 if (td->td_proc->p_stat == SRUN) /* XXXKSE */
634 remrunqueue(td); /* XXXKSE */
635 mtx_unlock_spin(&sched_lock);
637 pmap_swapout_proc(p);
638 FOREACH_THREAD_IN_PROC(p, td)
639 pmap_swapout_thread(td);
641 mtx_lock_spin(&sched_lock);
642 p->p_sflag &= ~PS_SWAPPING;
644 mtx_unlock_spin(&sched_lock);
646 #endif /* !NO_SWAPPING */