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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * from: @(#)vm_glue.c 8.6 (Berkeley) 1/5/94
35 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36 * All rights reserved.
38 * Permission to use, copy, modify and distribute this software and
39 * its documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
46 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie the
56 * rights to redistribute these changes.
58 * $FreeBSD: src/sys/vm/vm_glue.c,v 1.94.2.4 2003/01/13 22:51:17 dillon Exp $
63 #include <sys/param.h>
64 #include <sys/systm.h>
66 #include <sys/resourcevar.h>
69 #include <sys/vmmeter.h>
70 #include <sys/sysctl.h>
72 #include <sys/kernel.h>
73 #include <sys/unistd.h>
75 #include <machine/limits.h>
76 #include <machine/vmm.h>
79 #include <vm/vm_param.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_page.h>
84 #include <vm/vm_pageout.h>
85 #include <vm/vm_kern.h>
86 #include <vm/vm_extern.h>
89 #include <vm/vm_page2.h>
90 #include <sys/thread2.h>
91 #include <sys/sysref2.h>
94 * THIS MUST BE THE LAST INITIALIZATION ITEM!!!
96 * Note: run scheduling should be divorced from the vm system.
98 static void scheduler (void *);
99 SYSINIT(scheduler
, SI_SUB_RUN_SCHEDULER
, SI_ORDER_FIRST
, scheduler
, NULL
);
103 static int swap_debug
= 0;
104 SYSCTL_INT(_vm
, OID_AUTO
, swap_debug
,
105 CTLFLAG_RW
, &swap_debug
, 0, "");
109 static int scheduler_notify
;
111 static void swapout (struct proc
*);
117 kernacc(c_caddr_t addr
, int len
, int rw
)
120 vm_offset_t saddr
, eaddr
;
123 KASSERT((rw
& (~VM_PROT_ALL
)) == 0,
124 ("illegal ``rw'' argument to kernacc (%x)", rw
));
127 * The globaldata space is not part of the kernel_map proper,
128 * check access separately.
130 if (is_globaldata_space((vm_offset_t
)addr
, (vm_offset_t
)(addr
+ len
)))
134 * Nominal kernel memory access - check access via kernel_map.
136 if ((vm_offset_t
)addr
+ len
> kernel_map
.max_offset
||
137 (vm_offset_t
)addr
+ len
< (vm_offset_t
)addr
) {
141 saddr
= trunc_page((vm_offset_t
)addr
);
142 eaddr
= round_page((vm_offset_t
)addr
+ len
);
143 rv
= vm_map_check_protection(&kernel_map
, saddr
, eaddr
, prot
, FALSE
);
152 useracc(c_caddr_t addr
, int len
, int rw
)
160 KASSERT((rw
& (~VM_PROT_ALL
)) == 0,
161 ("illegal ``rw'' argument to useracc (%x)", rw
));
164 if (curthread
->td_vmm
) {
165 if (vmm_vm_get_gpa(curproc
, (register_t
*)&gpa
, (register_t
) addr
))
166 panic("%s: could not get GPA\n", __func__
);
167 addr
= (c_caddr_t
) gpa
;
171 * XXX - check separately to disallow access to user area and user
172 * page tables - they are in the map.
174 wrap
= (vm_offset_t
)addr
+ len
;
175 if (wrap
> VM_MAX_USER_ADDRESS
|| wrap
< (vm_offset_t
)addr
) {
178 map
= &curproc
->p_vmspace
->vm_map
;
179 vm_map_lock_read(map
);
181 rv
= vm_map_check_protection(map
, trunc_page((vm_offset_t
)addr
),
182 round_page(wrap
), prot
, TRUE
);
183 vm_map_unlock_read(map
);
192 vslock(caddr_t addr
, u_int len
)
195 vm_map_wire(&curproc
->p_vmspace
->vm_map
,
196 trunc_page((vm_offset_t
)addr
),
197 round_page((vm_offset_t
)addr
+ len
), 0);
205 vsunlock(caddr_t addr
, u_int len
)
208 vm_map_wire(&curproc
->p_vmspace
->vm_map
,
209 trunc_page((vm_offset_t
)addr
),
210 round_page((vm_offset_t
)addr
+ len
),
216 * Implement fork's actions on an address space.
217 * Here we arrange for the address space to be copied or referenced,
218 * allocate a user struct (pcb and kernel stack), then call the
219 * machine-dependent layer to fill those in and make the new process
220 * ready to run. The new process is set up so that it returns directly
221 * to user mode to avoid stack copying and relocation problems.
226 vm_fork(struct proc
*p1
, struct proc
*p2
, int flags
)
228 if ((flags
& RFPROC
) == 0) {
230 * Divorce the memory, if it is shared, essentially
231 * this changes shared memory amongst threads, into
234 if ((flags
& RFMEM
) == 0) {
235 if (vmspace_getrefs(p1
->p_vmspace
) > 1) {
239 cpu_fork(ONLY_LWP_IN_PROC(p1
), NULL
, flags
);
244 vmspace_ref(p1
->p_vmspace
);
245 p2
->p_vmspace
= p1
->p_vmspace
;
248 while (vm_page_count_severe()) {
252 if ((flags
& RFMEM
) == 0) {
253 p2
->p_vmspace
= vmspace_fork(p1
->p_vmspace
);
255 pmap_pinit2(vmspace_pmap(p2
->p_vmspace
));
257 if (p1
->p_vmspace
->vm_shm
)
265 * Set default limits for VM system. Call during proc0's initialization.
267 * Called from the low level boot code only.
270 vm_init_limits(struct proc
*p
)
275 * Set up the initial limits on process VM. Set the maximum resident
276 * set size to be half of (reasonably) available memory. Since this
277 * is a soft limit, it comes into effect only when the system is out
278 * of memory - half of main memory helps to favor smaller processes,
279 * and reduces thrashing of the object cache.
281 p
->p_rlimit
[RLIMIT_STACK
].rlim_cur
= dflssiz
;
282 p
->p_rlimit
[RLIMIT_STACK
].rlim_max
= maxssiz
;
283 p
->p_rlimit
[RLIMIT_DATA
].rlim_cur
= dfldsiz
;
284 p
->p_rlimit
[RLIMIT_DATA
].rlim_max
= maxdsiz
;
285 /* limit the limit to no less than 2MB */
286 rss_limit
= max(vmstats
.v_free_count
, 512);
287 p
->p_rlimit
[RLIMIT_RSS
].rlim_cur
= ptoa(rss_limit
);
288 p
->p_rlimit
[RLIMIT_RSS
].rlim_max
= RLIM_INFINITY
;
292 * Faultin the specified process. Note that the process can be in any
293 * state. Just clear P_SWAPPEDOUT and call wakeup in case the process is
299 faultin(struct proc
*p
)
301 if (p
->p_flags
& P_SWAPPEDOUT
) {
303 * The process is waiting in the kernel to return to user
304 * mode but cannot until P_SWAPPEDOUT gets cleared.
306 lwkt_gettoken(&p
->p_token
);
307 p
->p_flags
&= ~(P_SWAPPEDOUT
| P_SWAPWAIT
);
310 kprintf("swapping in %d (%s)\n", p
->p_pid
, p
->p_comm
);
313 lwkt_reltoken(&p
->p_token
);
318 * Kernel initialization eventually falls through to this function,
319 * which is process 0.
321 * This swapin algorithm attempts to swap-in processes only if there
322 * is enough space for them. Of course, if a process waits for a long
323 * time, it will be swapped in anyway.
325 struct scheduler_info
{
330 static int scheduler_callback(struct proc
*p
, void *data
);
333 scheduler(void *dummy
)
335 struct scheduler_info info
;
338 KKASSERT(!IN_CRITICAL_SECT(curthread
));
340 scheduler_notify
= 0;
342 * Don't try to swap anything in if we are low on memory.
344 if (vm_page_count_severe()) {
350 * Look for a good candidate to wake up
352 * XXX we should make the schedule thread pcpu and then use a
353 * segmented allproc scan.
357 allproc_scan(scheduler_callback
, &info
, 0);
360 * Nothing to do, back to sleep for at least 1/10 of a second. If
361 * we are woken up, immediately process the next request. If
362 * multiple requests have built up the first is processed
363 * immediately and the rest are staggered.
365 if ((p
= info
.pp
) == NULL
) {
366 tsleep(&proc0
, 0, "nowork", hz
/ 10);
367 if (scheduler_notify
== 0)
368 tsleep(&scheduler_notify
, 0, "nowork", 0);
373 * Fault the selected process in, then wait for a short period of
376 * XXX we need a heuristic to get a measure of system stress and
377 * then adjust our stagger wakeup delay accordingly.
379 lwkt_gettoken(&p
->p_token
);
382 lwkt_reltoken(&p
->p_token
);
384 tsleep(&proc0
, 0, "swapin", hz
/ 10);
389 * Process only has its hold count bumped, we need the token
390 * to safely scan the LWPs
393 scheduler_callback(struct proc
*p
, void *data
)
395 struct scheduler_info
*info
= data
;
402 * We only care about processes in swap-wait. Interlock test with
403 * token if the flag is found set.
405 if ((p
->p_flags
& P_SWAPWAIT
) == 0)
407 lwkt_gettoken_shared(&p
->p_token
);
408 if ((p
->p_flags
& P_SWAPWAIT
) == 0) {
409 lwkt_reltoken(&p
->p_token
);
414 * Calculate priority for swap-in
417 FOREACH_LWP_IN_PROC(lp
, p
) {
418 /* XXX lwp might need a different metric */
419 pri
+= lp
->lwp_slptime
;
421 pri
+= p
->p_swtime
- p
->p_nice
* 8;
424 * The more pages paged out while we were swapped,
425 * the more work we have to do to get up and running
426 * again and the lower our wakeup priority.
428 * Each second of sleep time is worth ~1MB
430 if ((vm
= p
->p_vmspace
) != NULL
) {
432 pgs
= vmspace_resident_count(vm
);
433 if (pgs
< vm
->vm_swrss
) {
434 pri
-= (vm
->vm_swrss
- pgs
) /
435 (1024 * 1024 / PAGE_SIZE
);
439 lwkt_reltoken(&p
->p_token
);
442 * If this process is higher priority and there is
443 * enough space, then select this process instead of
444 * the previous selection.
446 if (pri
> info
->ppri
) {
463 if (scheduler_notify
== 0) {
464 scheduler_notify
= 1;
465 wakeup(&scheduler_notify
);
471 #define swappable(p) \
472 (((p)->p_lock == 0) && \
473 ((p)->p_flags & (P_TRACED|P_SYSTEM|P_SWAPPEDOUT|P_WEXIT)) == 0)
477 * Swap_idle_threshold1 is the guaranteed swapped in time for a process
479 static int swap_idle_threshold1
= 15;
480 SYSCTL_INT(_vm
, OID_AUTO
, swap_idle_threshold1
,
481 CTLFLAG_RW
, &swap_idle_threshold1
, 0, "Guaranteed process resident time (sec)");
484 * Swap_idle_threshold2 is the time that a process can be idle before
485 * it will be swapped out, if idle swapping is enabled. Default is
488 static int swap_idle_threshold2
= 60;
489 SYSCTL_INT(_vm
, OID_AUTO
, swap_idle_threshold2
,
490 CTLFLAG_RW
, &swap_idle_threshold2
, 0, "Time (sec) a process can idle before being swapped");
493 * Swapout is driven by the pageout daemon. Very simple, we find eligible
494 * procs and mark them as being swapped out. This will cause the kernel
495 * to prefer to pageout those proc's pages first and the procs in question
496 * will not return to user mode until the swapper tells them they can.
498 * If any procs have been sleeping/stopped for at least maxslp seconds,
499 * they are swapped. Else, we swap the longest-sleeping or stopped process,
500 * if any, otherwise the longest-resident process.
503 static int swapout_procs_callback(struct proc
*p
, void *data
);
509 swapout_procs(int action
)
511 allproc_scan(swapout_procs_callback
, &action
, 0);
515 swapout_procs_callback(struct proc
*p
, void *data
)
518 int action
= *(int *)data
;
524 lwkt_gettoken(&p
->p_token
);
527 * We only consider active processes.
529 if (p
->p_stat
!= SACTIVE
&& p
->p_stat
!= SSTOP
) {
530 lwkt_reltoken(&p
->p_token
);
534 FOREACH_LWP_IN_PROC(lp
, p
) {
536 * do not swap out a realtime process
538 if (RTP_PRIO_IS_REALTIME(lp
->lwp_rtprio
.type
)) {
539 lwkt_reltoken(&p
->p_token
);
544 * Guarentee swap_idle_threshold time in memory
546 if (lp
->lwp_slptime
< swap_idle_threshold1
) {
547 lwkt_reltoken(&p
->p_token
);
552 * If the system is under memory stress, or if we
553 * are swapping idle processes >= swap_idle_threshold2,
554 * then swap the process out.
556 if (((action
& VM_SWAP_NORMAL
) == 0) &&
557 (((action
& VM_SWAP_IDLE
) == 0) ||
558 (lp
->lwp_slptime
< swap_idle_threshold2
))) {
559 lwkt_reltoken(&p
->p_token
);
563 if (minslp
== -1 || lp
->lwp_slptime
< minslp
)
564 minslp
= lp
->lwp_slptime
;
568 * If the process has been asleep for awhile, swap
571 if ((action
& VM_SWAP_NORMAL
) ||
572 ((action
& VM_SWAP_IDLE
) &&
573 (minslp
> swap_idle_threshold2
))) {
578 * cleanup our reference
580 lwkt_reltoken(&p
->p_token
);
586 * The caller must hold p->p_token
589 swapout(struct proc
*p
)
593 kprintf("swapping out %d (%s)\n", p
->p_pid
, p
->p_comm
);
598 * remember the process resident count
600 p
->p_vmspace
->vm_swrss
= vmspace_resident_count(p
->p_vmspace
);
601 p
->p_flags
|= P_SWAPPEDOUT
;
605 #endif /* !NO_SWAPPING */