2 * Copyright (c) 1991 Regents of the University of California.
4 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
9 * This code is derived from software contributed to Berkeley by
10 * The Mach Operating System project at Carnegie-Mellon University.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. 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_pageout.c 7.4 (Berkeley) 5/7/91
39 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40 * All rights reserved.
42 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
44 * Permission to use, copy, modify and distribute this software and
45 * its documentation is hereby granted, provided that both the copyright
46 * notice and this permission notice appear in all copies of the
47 * software, derivative works or modified versions, and any portions
48 * thereof, and that both notices appear in supporting documentation.
50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
54 * Carnegie Mellon requests users of this software to return to
56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
57 * School of Computer Science
58 * Carnegie Mellon University
59 * Pittsburgh PA 15213-3890
61 * any improvements or extensions that they make and grant Carnegie the
62 * rights to redistribute these changes.
64 * $FreeBSD: src/sys/vm/vm_pageout.c,v 1.151.2.15 2002/12/29 18:21:04 dillon Exp $
68 * The proverbial page-out daemon.
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
76 #include <sys/kthread.h>
77 #include <sys/resourcevar.h>
78 #include <sys/signalvar.h>
79 #include <sys/vnode.h>
80 #include <sys/vmmeter.h>
81 #include <sys/sysctl.h>
84 #include <vm/vm_param.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_pageout.h>
90 #include <vm/vm_pager.h>
91 #include <vm/swap_pager.h>
92 #include <vm/vm_extern.h>
94 #include <sys/thread2.h>
95 #include <sys/spinlock2.h>
96 #include <vm/vm_page2.h>
99 * System initialization
102 /* the kernel process "vm_pageout"*/
103 static int vm_pageout_clean (vm_page_t
);
104 static int vm_pageout_free_page_calc (vm_size_t count
);
105 struct thread
*pagethread
;
107 #if !defined(NO_SWAPPING)
108 /* the kernel process "vm_daemon"*/
109 static void vm_daemon (void);
110 static struct thread
*vmthread
;
112 static struct kproc_desc vm_kp
= {
117 SYSINIT(vmdaemon
, SI_SUB_KTHREAD_VM
, SI_ORDER_FIRST
, kproc_start
, &vm_kp
);
120 int vm_pages_needed
=0; /* Event on which pageout daemon sleeps */
121 int vm_pageout_deficit
=0; /* Estimated number of pages deficit */
122 int vm_pageout_pages_needed
=0; /* pageout daemon needs pages */
123 int vm_page_free_hysteresis
= 16;
125 #if !defined(NO_SWAPPING)
126 static int vm_pageout_req_swapout
; /* XXX */
127 static int vm_daemon_needed
;
129 static int vm_max_launder
= 4096;
130 static int vm_pageout_stats_max
=0, vm_pageout_stats_interval
= 0;
131 static int vm_pageout_full_stats_interval
= 0;
132 static int vm_pageout_stats_free_max
=0, vm_pageout_algorithm
=0;
133 static int defer_swap_pageouts
=0;
134 static int disable_swap_pageouts
=0;
135 static u_int vm_anonmem_decline
= ACT_DECLINE
;
136 static u_int vm_filemem_decline
= ACT_DECLINE
* 2;
138 #if defined(NO_SWAPPING)
139 static int vm_swap_enabled
=0;
140 static int vm_swap_idle_enabled
=0;
142 static int vm_swap_enabled
=1;
143 static int vm_swap_idle_enabled
=0;
146 SYSCTL_UINT(_vm
, VM_PAGEOUT_ALGORITHM
, anonmem_decline
,
147 CTLFLAG_RW
, &vm_anonmem_decline
, 0, "active->inactive anon memory");
149 SYSCTL_INT(_vm
, VM_PAGEOUT_ALGORITHM
, filemem_decline
,
150 CTLFLAG_RW
, &vm_filemem_decline
, 0, "active->inactive file cache");
152 SYSCTL_INT(_vm
, OID_AUTO
, page_free_hysteresis
,
153 CTLFLAG_RW
, &vm_page_free_hysteresis
, 0,
154 "Free more pages than the minimum required");
156 SYSCTL_INT(_vm
, OID_AUTO
, max_launder
,
157 CTLFLAG_RW
, &vm_max_launder
, 0, "Limit dirty flushes in pageout");
159 SYSCTL_INT(_vm
, OID_AUTO
, pageout_stats_max
,
160 CTLFLAG_RW
, &vm_pageout_stats_max
, 0, "Max pageout stats scan length");
162 SYSCTL_INT(_vm
, OID_AUTO
, pageout_full_stats_interval
,
163 CTLFLAG_RW
, &vm_pageout_full_stats_interval
, 0, "Interval for full stats scan");
165 SYSCTL_INT(_vm
, OID_AUTO
, pageout_stats_interval
,
166 CTLFLAG_RW
, &vm_pageout_stats_interval
, 0, "Interval for partial stats scan");
168 SYSCTL_INT(_vm
, OID_AUTO
, pageout_stats_free_max
,
169 CTLFLAG_RW
, &vm_pageout_stats_free_max
, 0, "Not implemented");
171 #if defined(NO_SWAPPING)
172 SYSCTL_INT(_vm
, VM_SWAPPING_ENABLED
, swap_enabled
,
173 CTLFLAG_RD
, &vm_swap_enabled
, 0, "");
174 SYSCTL_INT(_vm
, OID_AUTO
, swap_idle_enabled
,
175 CTLFLAG_RD
, &vm_swap_idle_enabled
, 0, "");
177 SYSCTL_INT(_vm
, VM_SWAPPING_ENABLED
, swap_enabled
,
178 CTLFLAG_RW
, &vm_swap_enabled
, 0, "Enable entire process swapout");
179 SYSCTL_INT(_vm
, OID_AUTO
, swap_idle_enabled
,
180 CTLFLAG_RW
, &vm_swap_idle_enabled
, 0, "Allow swapout on idle criteria");
183 SYSCTL_INT(_vm
, OID_AUTO
, defer_swapspace_pageouts
,
184 CTLFLAG_RW
, &defer_swap_pageouts
, 0, "Give preference to dirty pages in mem");
186 SYSCTL_INT(_vm
, OID_AUTO
, disable_swapspace_pageouts
,
187 CTLFLAG_RW
, &disable_swap_pageouts
, 0, "Disallow swapout of dirty pages");
189 static int pageout_lock_miss
;
190 SYSCTL_INT(_vm
, OID_AUTO
, pageout_lock_miss
,
191 CTLFLAG_RD
, &pageout_lock_miss
, 0, "vget() lock misses during pageout");
193 int vm_page_max_wired
; /* XXX max # of wired pages system-wide */
195 #if !defined(NO_SWAPPING)
196 typedef void freeer_fcn_t (vm_map_t
, vm_object_t
, vm_pindex_t
, int);
197 static void vm_pageout_map_deactivate_pages (vm_map_t
, vm_pindex_t
);
198 static freeer_fcn_t vm_pageout_object_deactivate_pages
;
199 static void vm_req_vmdaemon (void);
201 static void vm_pageout_page_stats(int q
);
207 return((n
+ (PQ_L2_SIZE
- 1)) / PQ_L2_SIZE
+ 1);
209 return((n
- (PQ_L2_SIZE
- 1)) / PQ_L2_SIZE
- 1);
215 * Clean the page and remove it from the laundry. The page must not be
218 * We set the busy bit to cause potential page faults on this page to
219 * block. Note the careful timing, however, the busy bit isn't set till
220 * late and we cannot do anything that will mess with the page.
223 vm_pageout_clean(vm_page_t m
)
226 vm_page_t mc
[BLIST_MAX_ALLOC
];
228 int ib
, is
, page_base
;
229 vm_pindex_t pindex
= m
->pindex
;
234 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
235 * with the new swapper, but we could have serious problems paging
236 * out other object types if there is insufficient memory.
238 * Unfortunately, checking free memory here is far too late, so the
239 * check has been moved up a procedural level.
243 * Don't mess with the page if it's busy, held, or special
245 * XXX do we really need to check hold_count here? hold_count
246 * isn't supposed to mess with vm_page ops except prevent the
247 * page from being reused.
249 if (m
->hold_count
!= 0 || (m
->flags
& PG_UNMANAGED
)) {
255 * Place page in cluster. Align cluster for optimal swap space
256 * allocation (whether it is swap or not). This is typically ~16-32
257 * pages, which also tends to align the cluster to multiples of the
258 * filesystem block size if backed by a filesystem.
260 page_base
= pindex
% BLIST_MAX_ALLOC
;
266 * Scan object for clusterable pages.
268 * We can cluster ONLY if: ->> the page is NOT
269 * clean, wired, busy, held, or mapped into a
270 * buffer, and one of the following:
271 * 1) The page is inactive, or a seldom used
274 * 2) we force the issue.
276 * During heavy mmap/modification loads the pageout
277 * daemon can really fragment the underlying file
278 * due to flushing pages out of order and not trying
279 * align the clusters (which leave sporatic out-of-order
280 * holes). To solve this problem we do the reverse scan
281 * first and attempt to align our cluster, then do a
282 * forward scan if room remains.
285 vm_object_hold(object
);
289 p
= vm_page_lookup_busy_try(object
, pindex
- page_base
+ ib
,
291 if (error
|| p
== NULL
)
293 if ((p
->queue
- p
->pc
) == PQ_CACHE
||
294 (p
->flags
& PG_UNMANAGED
)) {
298 vm_page_test_dirty(p
);
299 if (((p
->dirty
& p
->valid
) == 0 &&
300 (p
->flags
& PG_NEED_COMMIT
) == 0) ||
301 p
->queue
- p
->pc
!= PQ_INACTIVE
||
302 p
->wire_count
!= 0 || /* may be held by buf cache */
303 p
->hold_count
!= 0) { /* may be undergoing I/O */
312 while (is
< BLIST_MAX_ALLOC
&&
313 pindex
- page_base
+ is
< object
->size
) {
316 p
= vm_page_lookup_busy_try(object
, pindex
- page_base
+ is
,
318 if (error
|| p
== NULL
)
320 if (((p
->queue
- p
->pc
) == PQ_CACHE
) ||
321 (p
->flags
& (PG_BUSY
|PG_UNMANAGED
)) || p
->busy
) {
325 vm_page_test_dirty(p
);
326 if (((p
->dirty
& p
->valid
) == 0 &&
327 (p
->flags
& PG_NEED_COMMIT
) == 0) ||
328 p
->queue
- p
->pc
!= PQ_INACTIVE
||
329 p
->wire_count
!= 0 || /* may be held by buf cache */
330 p
->hold_count
!= 0) { /* may be undergoing I/O */
338 vm_object_drop(object
);
341 * we allow reads during pageouts...
343 return vm_pageout_flush(&mc
[ib
], is
- ib
, 0);
347 * vm_pageout_flush() - launder the given pages
349 * The given pages are laundered. Note that we setup for the start of
350 * I/O ( i.e. busy the page ), mark it read-only, and bump the object
351 * reference count all in here rather then in the parent. If we want
352 * the parent to do more sophisticated things we may have to change
355 * The pages in the array must be busied by the caller and will be
356 * unbusied by this function.
359 vm_pageout_flush(vm_page_t
*mc
, int count
, int flags
)
362 int pageout_status
[count
];
367 * Initiate I/O. Bump the vm_page_t->busy counter.
369 for (i
= 0; i
< count
; i
++) {
370 KASSERT(mc
[i
]->valid
== VM_PAGE_BITS_ALL
,
371 ("vm_pageout_flush page %p index %d/%d: partially "
372 "invalid page", mc
[i
], i
, count
));
373 vm_page_io_start(mc
[i
]);
377 * We must make the pages read-only. This will also force the
378 * modified bit in the related pmaps to be cleared. The pager
379 * cannot clear the bit for us since the I/O completion code
380 * typically runs from an interrupt. The act of making the page
381 * read-only handles the case for us.
383 * Then we can unbusy the pages, we still hold a reference by virtue
386 for (i
= 0; i
< count
; i
++) {
387 vm_page_protect(mc
[i
], VM_PROT_READ
);
388 vm_page_wakeup(mc
[i
]);
391 object
= mc
[0]->object
;
392 vm_object_pip_add(object
, count
);
394 vm_pager_put_pages(object
, mc
, count
,
395 (flags
| ((object
== &kernel_object
) ? VM_PAGER_PUT_SYNC
: 0)),
398 for (i
= 0; i
< count
; i
++) {
399 vm_page_t mt
= mc
[i
];
401 switch (pageout_status
[i
]) {
410 * Page outside of range of object. Right now we
411 * essentially lose the changes by pretending it
414 vm_page_busy_wait(mt
, FALSE
, "pgbad");
415 pmap_clear_modify(mt
);
422 * A page typically cannot be paged out when we
423 * have run out of swap. We leave the page
424 * marked inactive and will try to page it out
427 * Starvation of the active page list is used to
428 * determine when the system is massively memory
437 * If the operation is still going, leave the page busy to
438 * block all other accesses. Also, leave the paging in
439 * progress indicator set so that we don't attempt an object
442 * For any pages which have completed synchronously,
443 * deactivate the page if we are under a severe deficit.
444 * Do not try to enter them into the cache, though, they
445 * might still be read-heavy.
447 if (pageout_status
[i
] != VM_PAGER_PEND
) {
448 vm_page_busy_wait(mt
, FALSE
, "pgouw");
449 if (vm_page_count_severe())
450 vm_page_deactivate(mt
);
452 if (!vm_page_count_severe() || !vm_page_try_to_cache(mt
))
453 vm_page_protect(mt
, VM_PROT_READ
);
455 vm_page_io_finish(mt
);
457 vm_object_pip_wakeup(object
);
463 #if !defined(NO_SWAPPING)
465 * deactivate enough pages to satisfy the inactive target
466 * requirements or if vm_page_proc_limit is set, then
467 * deactivate all of the pages in the object and its
470 * The map must be locked.
471 * The caller must hold the vm_object.
473 static int vm_pageout_object_deactivate_pages_callback(vm_page_t
, void *);
476 vm_pageout_object_deactivate_pages(vm_map_t map
, vm_object_t object
,
477 vm_pindex_t desired
, int map_remove_only
)
479 struct rb_vm_page_scan_info info
;
484 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object
));
488 if (pmap_resident_count(vm_map_pmap(map
)) <= desired
)
490 if (lobject
->type
== OBJT_DEVICE
||
491 lobject
->type
== OBJT_MGTDEVICE
||
492 lobject
->type
== OBJT_PHYS
)
494 if (lobject
->paging_in_progress
)
497 remove_mode
= map_remove_only
;
498 if (lobject
->shadow_count
> 1)
502 * scan the objects entire memory queue. We hold the
503 * object's token so the scan should not race anything.
505 info
.limit
= remove_mode
;
507 info
.desired
= desired
;
508 vm_page_rb_tree_RB_SCAN(&lobject
->rb_memq
, NULL
,
509 vm_pageout_object_deactivate_pages_callback
,
512 while ((tobject
= lobject
->backing_object
) != NULL
) {
513 KKASSERT(tobject
!= object
);
514 vm_object_hold(tobject
);
515 if (tobject
== lobject
->backing_object
)
517 vm_object_drop(tobject
);
519 if (lobject
!= object
) {
521 vm_object_lock_swap();
522 vm_object_drop(lobject
);
523 /* leaves tobject locked & at top */
527 if (lobject
!= object
)
528 vm_object_drop(lobject
); /* NULL ok */
532 * The caller must hold the vm_object.
535 vm_pageout_object_deactivate_pages_callback(vm_page_t p
, void *data
)
537 struct rb_vm_page_scan_info
*info
= data
;
540 if (pmap_resident_count(vm_map_pmap(info
->map
)) <= info
->desired
) {
543 mycpu
->gd_cnt
.v_pdpages
++;
545 if (vm_page_busy_try(p
, TRUE
))
547 if (p
->wire_count
|| p
->hold_count
|| (p
->flags
& PG_UNMANAGED
)) {
551 if (!pmap_page_exists_quick(vm_map_pmap(info
->map
), p
)) {
556 actcount
= pmap_ts_referenced(p
);
558 vm_page_flag_set(p
, PG_REFERENCED
);
559 } else if (p
->flags
& PG_REFERENCED
) {
563 vm_page_and_queue_spin_lock(p
);
564 if (p
->queue
- p
->pc
!= PQ_ACTIVE
&& (p
->flags
& PG_REFERENCED
)) {
565 vm_page_and_queue_spin_unlock(p
);
567 p
->act_count
+= actcount
;
568 vm_page_flag_clear(p
, PG_REFERENCED
);
569 } else if (p
->queue
- p
->pc
== PQ_ACTIVE
) {
570 if ((p
->flags
& PG_REFERENCED
) == 0) {
571 p
->act_count
-= min(p
->act_count
, ACT_DECLINE
);
573 (vm_pageout_algorithm
|| (p
->act_count
== 0))) {
574 vm_page_and_queue_spin_unlock(p
);
575 vm_page_protect(p
, VM_PROT_NONE
);
576 vm_page_deactivate(p
);
578 TAILQ_REMOVE(&vm_page_queues
[p
->queue
].pl
,
580 TAILQ_INSERT_TAIL(&vm_page_queues
[p
->queue
].pl
,
582 vm_page_and_queue_spin_unlock(p
);
585 vm_page_and_queue_spin_unlock(p
);
587 vm_page_flag_clear(p
, PG_REFERENCED
);
589 vm_page_and_queue_spin_lock(p
);
590 if (p
->queue
- p
->pc
== PQ_ACTIVE
) {
591 if (p
->act_count
< (ACT_MAX
- ACT_ADVANCE
))
592 p
->act_count
+= ACT_ADVANCE
;
593 TAILQ_REMOVE(&vm_page_queues
[p
->queue
].pl
,
595 TAILQ_INSERT_TAIL(&vm_page_queues
[p
->queue
].pl
,
598 vm_page_and_queue_spin_unlock(p
);
600 } else if (p
->queue
- p
->pc
== PQ_INACTIVE
) {
601 vm_page_and_queue_spin_unlock(p
);
602 vm_page_protect(p
, VM_PROT_NONE
);
604 vm_page_and_queue_spin_unlock(p
);
611 * Deactivate some number of pages in a map, try to do it fairly, but
612 * that is really hard to do.
615 vm_pageout_map_deactivate_pages(vm_map_t map
, vm_pindex_t desired
)
618 vm_object_t obj
, bigobj
;
621 if (lockmgr(&map
->lock
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
629 * first, search out the biggest object, and try to free pages from
632 tmpe
= map
->header
.next
;
633 while (tmpe
!= &map
->header
) {
634 switch(tmpe
->maptype
) {
635 case VM_MAPTYPE_NORMAL
:
636 case VM_MAPTYPE_VPAGETABLE
:
637 obj
= tmpe
->object
.vm_object
;
638 if ((obj
!= NULL
) && (obj
->shadow_count
<= 1) &&
640 (bigobj
->resident_page_count
< obj
->resident_page_count
))) {
647 if (tmpe
->wired_count
> 0)
648 nothingwired
= FALSE
;
653 vm_object_hold(bigobj
);
654 vm_pageout_object_deactivate_pages(map
, bigobj
, desired
, 0);
655 vm_object_drop(bigobj
);
659 * Next, hunt around for other pages to deactivate. We actually
660 * do this search sort of wrong -- .text first is not the best idea.
662 tmpe
= map
->header
.next
;
663 while (tmpe
!= &map
->header
) {
664 if (pmap_resident_count(vm_map_pmap(map
)) <= desired
)
666 switch(tmpe
->maptype
) {
667 case VM_MAPTYPE_NORMAL
:
668 case VM_MAPTYPE_VPAGETABLE
:
669 obj
= tmpe
->object
.vm_object
;
672 vm_pageout_object_deactivate_pages(map
, obj
, desired
, 0);
683 * Remove all mappings if a process is swapped out, this will free page
686 if (desired
== 0 && nothingwired
)
687 pmap_remove(vm_map_pmap(map
),
688 VM_MIN_USER_ADDRESS
, VM_MAX_USER_ADDRESS
);
694 * Called when the pageout scan wants to free a page. We no longer
695 * try to cycle the vm_object here with a reference & dealloc, which can
696 * cause a non-trivial object collapse in a critical path.
698 * It is unclear why we cycled the ref_count in the past, perhaps to try
699 * to optimize shadow chain collapses but I don't quite see why it would
700 * be necessary. An OBJ_DEAD object should terminate any and all vm_pages
701 * synchronously and not have to be kicked-start.
704 vm_pageout_page_free(vm_page_t m
)
706 vm_page_protect(m
, VM_PROT_NONE
);
711 * vm_pageout_scan does the dirty work for the pageout daemon.
713 struct vm_pageout_scan_info
{
714 struct proc
*bigproc
;
718 static int vm_pageout_scan_callback(struct proc
*p
, void *data
);
721 vm_pageout_scan_inactive(int pass
, int q
, int avail_shortage
,
722 int *vnodes_skippedp
)
725 struct vm_page marker
;
726 struct vnode
*vpfailed
; /* warning, allowed to be stale */
735 * Start scanning the inactive queue for pages we can move to the
736 * cache or free. The scan will stop when the target is reached or
737 * we have scanned the entire inactive queue. Note that m->act_count
738 * is not used to form decisions for the inactive queue, only for the
741 * maxlaunder limits the number of dirty pages we flush per scan.
742 * For most systems a smaller value (16 or 32) is more robust under
743 * extreme memory and disk pressure because any unnecessary writes
744 * to disk can result in extreme performance degredation. However,
745 * systems with excessive dirty pages (especially when MAP_NOSYNC is
746 * used) will die horribly with limited laundering. If the pageout
747 * daemon cannot clean enough pages in the first pass, we let it go
748 * all out in succeeding passes.
750 if ((maxlaunder
= vm_max_launder
) <= 1)
756 * Initialize our marker
758 bzero(&marker
, sizeof(marker
));
759 marker
.flags
= PG_BUSY
| PG_FICTITIOUS
| PG_MARKER
;
760 marker
.queue
= PQ_INACTIVE
+ q
;
762 marker
.wire_count
= 1;
765 * Inactive queue scan.
767 * NOTE: The vm_page must be spinlocked before the queue to avoid
768 * deadlocks, so it is easiest to simply iterate the loop
769 * with the queue unlocked at the top.
773 vm_page_queues_spin_lock(PQ_INACTIVE
+ q
);
774 TAILQ_INSERT_HEAD(&vm_page_queues
[PQ_INACTIVE
+ q
].pl
, &marker
, pageq
);
775 maxscan
= vm_page_queues
[PQ_INACTIVE
+ q
].lcnt
;
778 * Queue locked at top of loop to avoid stack marker issues.
780 while ((m
= TAILQ_NEXT(&marker
, pageq
)) != NULL
&&
781 maxscan
-- > 0 && avail_shortage
- delta
> 0)
783 KKASSERT(m
->queue
- m
->pc
== PQ_INACTIVE
);
784 TAILQ_REMOVE(&vm_page_queues
[PQ_INACTIVE
+ q
].pl
,
786 TAILQ_INSERT_AFTER(&vm_page_queues
[PQ_INACTIVE
+ q
].pl
, m
,
788 mycpu
->gd_cnt
.v_pdpages
++;
791 * Skip marker pages (atomic against other markers to avoid
792 * infinite hop-over scans).
794 if (m
->flags
& PG_MARKER
)
798 * Try to busy the page. Don't mess with pages which are
799 * already busy or reorder them in the queue.
801 if (vm_page_busy_try(m
, TRUE
))
805 * Remaining operations run with the page busy and neither
806 * the page or the queue will be spin-locked.
808 vm_page_queues_spin_unlock(PQ_INACTIVE
+ q
);
809 KKASSERT(m
->queue
- m
->pc
== PQ_INACTIVE
);
813 * It is possible for a page to be busied ad-hoc (e.g. the
814 * pmap_collect() code) and wired and race against the
815 * allocation of a new page. vm_page_alloc() may be forced
816 * to deactivate the wired page in which case it winds up
817 * on the inactive queue and must be handled here. We
818 * correct the problem simply by unqueuing the page.
821 vm_page_unqueue_nowakeup(m
);
823 kprintf("WARNING: pagedaemon: wired page on "
824 "inactive queue %p\n", m
);
829 * A held page may be undergoing I/O, so skip it.
832 vm_page_and_queue_spin_lock(m
);
833 if (m
->queue
- m
->pc
== PQ_INACTIVE
) {
835 &vm_page_queues
[PQ_INACTIVE
+ q
].pl
,
838 &vm_page_queues
[PQ_INACTIVE
+ q
].pl
,
840 ++vm_swapcache_inactive_heuristic
;
842 vm_page_and_queue_spin_unlock(m
);
847 if (m
->object
== NULL
|| m
->object
->ref_count
== 0) {
849 * If the object is not being used, we ignore previous
852 vm_page_flag_clear(m
, PG_REFERENCED
);
853 pmap_clear_reference(m
);
854 /* fall through to end */
855 } else if (((m
->flags
& PG_REFERENCED
) == 0) &&
856 (actcount
= pmap_ts_referenced(m
))) {
858 * Otherwise, if the page has been referenced while
859 * in the inactive queue, we bump the "activation
860 * count" upwards, making it less likely that the
861 * page will be added back to the inactive queue
862 * prematurely again. Here we check the page tables
863 * (or emulated bits, if any), given the upper level
864 * VM system not knowing anything about existing
868 m
->act_count
+= (actcount
+ ACT_ADVANCE
);
874 * (m) is still busied.
876 * If the upper level VM system knows about any page
877 * references, we activate the page. We also set the
878 * "activation count" higher than normal so that we will less
879 * likely place pages back onto the inactive queue again.
881 if ((m
->flags
& PG_REFERENCED
) != 0) {
882 vm_page_flag_clear(m
, PG_REFERENCED
);
883 actcount
= pmap_ts_referenced(m
);
885 m
->act_count
+= (actcount
+ ACT_ADVANCE
+ 1);
891 * If the upper level VM system doesn't know anything about
892 * the page being dirty, we have to check for it again. As
893 * far as the VM code knows, any partially dirty pages are
896 * Pages marked PG_WRITEABLE may be mapped into the user
897 * address space of a process running on another cpu. A
898 * user process (without holding the MP lock) running on
899 * another cpu may be able to touch the page while we are
900 * trying to remove it. vm_page_cache() will handle this
904 vm_page_test_dirty(m
);
909 if (m
->valid
== 0 && (m
->flags
& PG_NEED_COMMIT
) == 0) {
911 * Invalid pages can be easily freed
913 vm_pageout_page_free(m
);
914 mycpu
->gd_cnt
.v_dfree
++;
916 } else if (m
->dirty
== 0 && (m
->flags
& PG_NEED_COMMIT
) == 0) {
918 * Clean pages can be placed onto the cache queue.
919 * This effectively frees them.
923 } else if ((m
->flags
& PG_WINATCFLS
) == 0 && pass
== 0) {
925 * Dirty pages need to be paged out, but flushing
926 * a page is extremely expensive verses freeing
927 * a clean page. Rather then artificially limiting
928 * the number of pages we can flush, we instead give
929 * dirty pages extra priority on the inactive queue
930 * by forcing them to be cycled through the queue
931 * twice before being flushed, after which the
932 * (now clean) page will cycle through once more
933 * before being freed. This significantly extends
934 * the thrash point for a heavily loaded machine.
936 vm_page_flag_set(m
, PG_WINATCFLS
);
937 vm_page_and_queue_spin_lock(m
);
938 if (m
->queue
- m
->pc
== PQ_INACTIVE
) {
940 &vm_page_queues
[PQ_INACTIVE
+ q
].pl
,
943 &vm_page_queues
[PQ_INACTIVE
+ q
].pl
,
945 ++vm_swapcache_inactive_heuristic
;
947 vm_page_and_queue_spin_unlock(m
);
949 } else if (maxlaunder
> 0) {
951 * We always want to try to flush some dirty pages if
952 * we encounter them, to keep the system stable.
953 * Normally this number is small, but under extreme
954 * pressure where there are insufficient clean pages
955 * on the inactive queue, we may have to go all out.
957 int swap_pageouts_ok
;
958 struct vnode
*vp
= NULL
;
960 swap_pageouts_ok
= 0;
963 (object
->type
!= OBJT_SWAP
) &&
964 (object
->type
!= OBJT_DEFAULT
)) {
965 swap_pageouts_ok
= 1;
967 swap_pageouts_ok
= !(defer_swap_pageouts
|| disable_swap_pageouts
);
968 swap_pageouts_ok
|= (!disable_swap_pageouts
&& defer_swap_pageouts
&&
969 vm_page_count_min(0));
974 * We don't bother paging objects that are "dead".
975 * Those objects are in a "rundown" state.
977 if (!swap_pageouts_ok
||
979 (object
->flags
& OBJ_DEAD
)) {
980 vm_page_and_queue_spin_lock(m
);
981 if (m
->queue
- m
->pc
== PQ_INACTIVE
) {
983 &vm_page_queues
[PQ_INACTIVE
+ q
].pl
,
986 &vm_page_queues
[PQ_INACTIVE
+ q
].pl
,
988 ++vm_swapcache_inactive_heuristic
;
990 vm_page_and_queue_spin_unlock(m
);
996 * (m) is still busied.
998 * The object is already known NOT to be dead. It
999 * is possible for the vget() to block the whole
1000 * pageout daemon, but the new low-memory handling
1001 * code should prevent it.
1003 * The previous code skipped locked vnodes and, worse,
1004 * reordered pages in the queue. This results in
1005 * completely non-deterministic operation because,
1006 * quite often, a vm_fault has initiated an I/O and
1007 * is holding a locked vnode at just the point where
1008 * the pageout daemon is woken up.
1010 * We can't wait forever for the vnode lock, we might
1011 * deadlock due to a vn_read() getting stuck in
1012 * vm_wait while holding this vnode. We skip the
1013 * vnode if we can't get it in a reasonable amount
1016 * vpfailed is used to (try to) avoid the case where
1017 * a large number of pages are associated with a
1018 * locked vnode, which could cause the pageout daemon
1019 * to stall for an excessive amount of time.
1021 if (object
->type
== OBJT_VNODE
) {
1024 vp
= object
->handle
;
1025 flags
= LK_EXCLUSIVE
;
1029 flags
|= LK_TIMELOCK
;
1034 * We have unbusied (m) temporarily so we can
1035 * acquire the vp lock without deadlocking.
1036 * (m) is held to prevent destruction.
1038 if (vget(vp
, flags
) != 0) {
1040 ++pageout_lock_miss
;
1041 if (object
->flags
& OBJ_MIGHTBEDIRTY
)
1048 * The page might have been moved to another
1049 * queue during potential blocking in vget()
1050 * above. The page might have been freed and
1051 * reused for another vnode. The object might
1052 * have been reused for another vnode.
1054 if (m
->queue
- m
->pc
!= PQ_INACTIVE
||
1055 m
->object
!= object
||
1056 object
->handle
!= vp
) {
1057 if (object
->flags
& OBJ_MIGHTBEDIRTY
)
1065 * The page may have been busied during the
1066 * blocking in vput(); We don't move the
1067 * page back onto the end of the queue so that
1068 * statistics are more correct if we don't.
1070 if (vm_page_busy_try(m
, TRUE
)) {
1078 * (m) is busied again
1080 * We own the busy bit and remove our hold
1081 * bit. If the page is still held it
1082 * might be undergoing I/O, so skip it.
1084 if (m
->hold_count
) {
1085 vm_page_and_queue_spin_lock(m
);
1086 if (m
->queue
- m
->pc
== PQ_INACTIVE
) {
1087 TAILQ_REMOVE(&vm_page_queues
[PQ_INACTIVE
+ q
].pl
, m
, pageq
);
1088 TAILQ_INSERT_TAIL(&vm_page_queues
[PQ_INACTIVE
+ q
].pl
, m
, pageq
);
1089 ++vm_swapcache_inactive_heuristic
;
1091 vm_page_and_queue_spin_unlock(m
);
1092 if (object
->flags
& OBJ_MIGHTBEDIRTY
)
1098 /* (m) is left busied as we fall through */
1102 * page is busy and not held here.
1104 * If a page is dirty, then it is either being washed
1105 * (but not yet cleaned) or it is still in the
1106 * laundry. If it is still in the laundry, then we
1107 * start the cleaning operation.
1109 * decrement inactive_shortage on success to account
1110 * for the (future) cleaned page. Otherwise we
1111 * could wind up laundering or cleaning too many
1114 count
= vm_pageout_clean(m
);
1116 maxlaunder
-= count
;
1119 * Clean ate busy, page no longer accessible
1129 * Systems with a ton of memory can wind up with huge
1130 * deactivation counts. Because the inactive scan is
1131 * doing a lot of flushing, the combination can result
1132 * in excessive paging even in situations where other
1133 * unrelated threads free up sufficient VM.
1135 * To deal with this we abort the nominal active->inactive
1136 * scan before we hit the inactive target when free+cache
1137 * levels have already reached their target.
1139 * Note that nominally the inactive scan is not freeing or
1140 * caching pages, it is deactivating active pages, so it
1141 * will not by itself cause the abort condition.
1143 vm_page_queues_spin_lock(PQ_INACTIVE
+ q
);
1144 if (vm_paging_target() < 0)
1148 /* page queue still spin-locked */
1149 TAILQ_REMOVE(&vm_page_queues
[PQ_INACTIVE
+ q
].pl
, &marker
, pageq
);
1150 vm_page_queues_spin_unlock(PQ_INACTIVE
+ q
);
1156 vm_pageout_scan_active(int pass
, int q
,
1157 int avail_shortage
, int inactive_shortage
,
1158 int *recycle_countp
)
1160 struct vm_page marker
;
1167 * We want to move pages from the active queue to the inactive
1168 * queue to get the inactive queue to the inactive target. If
1169 * we still have a page shortage from above we try to directly free
1170 * clean pages instead of moving them.
1172 * If we do still have a shortage we keep track of the number of
1173 * pages we free or cache (recycle_count) as a measure of thrashing
1174 * between the active and inactive queues.
1176 * If we were able to completely satisfy the free+cache targets
1177 * from the inactive pool we limit the number of pages we move
1178 * from the active pool to the inactive pool to 2x the pages we
1179 * had removed from the inactive pool (with a minimum of 1/5 the
1180 * inactive target). If we were not able to completely satisfy
1181 * the free+cache targets we go for the whole target aggressively.
1183 * NOTE: Both variables can end up negative.
1184 * NOTE: We are still in a critical section.
1187 bzero(&marker
, sizeof(marker
));
1188 marker
.flags
= PG_BUSY
| PG_FICTITIOUS
| PG_MARKER
;
1189 marker
.queue
= PQ_ACTIVE
+ q
;
1191 marker
.wire_count
= 1;
1193 vm_page_queues_spin_lock(PQ_ACTIVE
+ q
);
1194 TAILQ_INSERT_HEAD(&vm_page_queues
[PQ_ACTIVE
+ q
].pl
, &marker
, pageq
);
1195 maxscan
= vm_page_queues
[PQ_ACTIVE
+ q
].lcnt
;
1198 * Queue locked at top of loop to avoid stack marker issues.
1200 while ((m
= TAILQ_NEXT(&marker
, pageq
)) != NULL
&&
1201 maxscan
-- > 0 && (avail_shortage
- delta
> 0 ||
1202 inactive_shortage
> 0))
1204 KKASSERT(m
->queue
- m
->pc
== PQ_ACTIVE
);
1205 TAILQ_REMOVE(&vm_page_queues
[PQ_ACTIVE
+ q
].pl
,
1207 TAILQ_INSERT_AFTER(&vm_page_queues
[PQ_ACTIVE
+ q
].pl
, m
,
1211 * Skip marker pages (atomic against other markers to avoid
1212 * infinite hop-over scans).
1214 if (m
->flags
& PG_MARKER
)
1218 * Try to busy the page. Don't mess with pages which are
1219 * already busy or reorder them in the queue.
1221 if (vm_page_busy_try(m
, TRUE
))
1225 * Remaining operations run with the page busy and neither
1226 * the page or the queue will be spin-locked.
1228 vm_page_queues_spin_unlock(PQ_ACTIVE
+ q
);
1229 KKASSERT(m
->queue
- m
->pc
== PQ_ACTIVE
);
1233 * Don't deactivate pages that are held, even if we can
1234 * busy them. (XXX why not?)
1236 if (m
->hold_count
!= 0) {
1237 vm_page_and_queue_spin_lock(m
);
1238 if (m
->queue
- m
->pc
== PQ_ACTIVE
) {
1240 &vm_page_queues
[PQ_ACTIVE
+ q
].pl
,
1243 &vm_page_queues
[PQ_ACTIVE
+ q
].pl
,
1246 vm_page_and_queue_spin_unlock(m
);
1252 * The count for pagedaemon pages is done after checking the
1253 * page for eligibility...
1255 mycpu
->gd_cnt
.v_pdpages
++;
1258 * Check to see "how much" the page has been used and clear
1259 * the tracking access bits. If the object has no references
1260 * don't bother paying the expense.
1263 if (m
->object
&& m
->object
->ref_count
!= 0) {
1264 if (m
->flags
& PG_REFERENCED
)
1266 actcount
+= pmap_ts_referenced(m
);
1268 m
->act_count
+= ACT_ADVANCE
+ actcount
;
1269 if (m
->act_count
> ACT_MAX
)
1270 m
->act_count
= ACT_MAX
;
1273 vm_page_flag_clear(m
, PG_REFERENCED
);
1276 * actcount is only valid if the object ref_count is non-zero.
1277 * If the page does not have an object, actcount will be zero.
1279 if (actcount
&& m
->object
->ref_count
!= 0) {
1280 vm_page_and_queue_spin_lock(m
);
1281 if (m
->queue
- m
->pc
== PQ_ACTIVE
) {
1283 &vm_page_queues
[PQ_ACTIVE
+ q
].pl
,
1286 &vm_page_queues
[PQ_ACTIVE
+ q
].pl
,
1289 vm_page_and_queue_spin_unlock(m
);
1292 switch(m
->object
->type
) {
1295 m
->act_count
-= min(m
->act_count
,
1296 vm_anonmem_decline
);
1299 m
->act_count
-= min(m
->act_count
,
1300 vm_filemem_decline
);
1303 if (vm_pageout_algorithm
||
1304 (m
->object
== NULL
) ||
1305 (m
->object
&& (m
->object
->ref_count
== 0)) ||
1306 m
->act_count
< pass
+ 1
1309 * Deactivate the page. If we had a
1310 * shortage from our inactive scan try to
1311 * free (cache) the page instead.
1313 * Don't just blindly cache the page if
1314 * we do not have a shortage from the
1315 * inactive scan, that could lead to
1316 * gigabytes being moved.
1318 --inactive_shortage
;
1319 if (avail_shortage
- delta
> 0 ||
1320 (m
->object
&& (m
->object
->ref_count
== 0)))
1322 if (avail_shortage
- delta
> 0)
1324 vm_page_protect(m
, VM_PROT_NONE
);
1325 if (m
->dirty
== 0 &&
1326 (m
->flags
& PG_NEED_COMMIT
) == 0 &&
1327 avail_shortage
- delta
> 0) {
1330 vm_page_deactivate(m
);
1334 vm_page_deactivate(m
);
1339 vm_page_and_queue_spin_lock(m
);
1340 if (m
->queue
- m
->pc
== PQ_ACTIVE
) {
1342 &vm_page_queues
[PQ_ACTIVE
+ q
].pl
,
1345 &vm_page_queues
[PQ_ACTIVE
+ q
].pl
,
1348 vm_page_and_queue_spin_unlock(m
);
1353 vm_page_queues_spin_lock(PQ_ACTIVE
+ q
);
1357 * Clean out our local marker.
1359 * Page queue still spin-locked.
1361 TAILQ_REMOVE(&vm_page_queues
[PQ_ACTIVE
+ q
].pl
, &marker
, pageq
);
1362 vm_page_queues_spin_unlock(PQ_ACTIVE
+ q
);
1368 * The number of actually free pages can drop down to v_free_reserved,
1369 * we try to build the free count back above v_free_min. Note that
1370 * vm_paging_needed() also returns TRUE if v_free_count is not at
1371 * least v_free_min so that is the minimum we must build the free
1374 * We use a slightly higher target to improve hysteresis,
1375 * ((v_free_target + v_free_min) / 2). Since v_free_target
1376 * is usually the same as v_cache_min this maintains about
1377 * half the pages in the free queue as are in the cache queue,
1378 * providing pretty good pipelining for pageout operation.
1380 * The system operator can manipulate vm.v_cache_min and
1381 * vm.v_free_target to tune the pageout demon. Be sure
1382 * to keep vm.v_free_min < vm.v_free_target.
1384 * Note that the original paging target is to get at least
1385 * (free_min + cache_min) into (free + cache). The slightly
1386 * higher target will shift additional pages from cache to free
1387 * without effecting the original paging target in order to
1388 * maintain better hysteresis and not have the free count always
1389 * be dead-on v_free_min.
1391 * NOTE: we are still in a critical section.
1393 * Pages moved from PQ_CACHE to totally free are not counted in the
1394 * pages_freed counter.
1397 vm_pageout_scan_cache(int avail_shortage
, int pass
,
1398 int vnodes_skipped
, int recycle_count
)
1400 struct vm_pageout_scan_info info
;
1403 while (vmstats
.v_free_count
<
1404 (vmstats
.v_free_min
+ vmstats
.v_free_target
) / 2) {
1406 * This steals some code from vm/vm_page.c
1408 static int cache_rover
= 0;
1410 m
= vm_page_list_find(PQ_CACHE
,
1411 cache_rover
& PQ_L2_MASK
, FALSE
);
1414 /* page is returned removed from its queue and spinlocked */
1415 if (vm_page_busy_try(m
, TRUE
)) {
1416 vm_page_deactivate_locked(m
);
1417 vm_page_spin_unlock(m
);
1420 vm_page_spin_unlock(m
);
1421 pagedaemon_wakeup();
1425 * Remaining operations run with the page busy and neither
1426 * the page or the queue will be spin-locked.
1428 if ((m
->flags
& (PG_UNMANAGED
| PG_NEED_COMMIT
)) ||
1431 vm_page_deactivate(m
);
1435 KKASSERT((m
->flags
& PG_MAPPED
) == 0);
1436 KKASSERT(m
->dirty
== 0);
1437 cache_rover
+= PQ_PRIME2
;
1438 vm_pageout_page_free(m
);
1439 mycpu
->gd_cnt
.v_dfree
++;
1442 #if !defined(NO_SWAPPING)
1444 * Idle process swapout -- run once per second.
1446 if (vm_swap_idle_enabled
) {
1448 if (time_uptime
!= lsec
) {
1449 vm_pageout_req_swapout
|= VM_SWAP_IDLE
;
1457 * If we didn't get enough free pages, and we have skipped a vnode
1458 * in a writeable object, wakeup the sync daemon. And kick swapout
1459 * if we did not get enough free pages.
1461 if (vm_paging_target() > 0) {
1462 if (vnodes_skipped
&& vm_page_count_min(0))
1463 speedup_syncer(NULL
);
1464 #if !defined(NO_SWAPPING)
1465 if (vm_swap_enabled
&& vm_page_count_target()) {
1467 vm_pageout_req_swapout
|= VM_SWAP_NORMAL
;
1473 * Handle catastrophic conditions. Under good conditions we should
1474 * be at the target, well beyond our minimum. If we could not even
1475 * reach our minimum the system is under heavy stress. But just being
1476 * under heavy stress does not trigger process killing.
1478 * We consider ourselves to have run out of memory if the swap pager
1479 * is full and avail_shortage is still positive. The secondary check
1480 * ensures that we do not kill processes if the instantanious
1481 * availability is good, even if the pageout demon pass says it
1482 * couldn't get to the target.
1484 if (swap_pager_almost_full
&&
1485 (vm_page_count_min(recycle_count
) || avail_shortage
> 0)) {
1486 kprintf("Warning: system low on memory+swap "
1487 "shortage %d for %d ticks!\n",
1488 avail_shortage
, ticks
- swap_fail_ticks
);
1490 if (swap_pager_full
&&
1491 avail_shortage
> 0 &&
1492 vm_paging_target() > 0) {
1496 info
.bigproc
= NULL
;
1498 allproc_scan(vm_pageout_scan_callback
, &info
);
1499 if (info
.bigproc
!= NULL
) {
1500 killproc(info
.bigproc
, "out of swap space");
1501 info
.bigproc
->p_nice
= PRIO_MIN
;
1502 info
.bigproc
->p_usched
->resetpriority(
1503 FIRST_LWP_IN_PROC(info
.bigproc
));
1504 wakeup(&vmstats
.v_free_count
);
1505 PRELE(info
.bigproc
);
1511 vm_pageout_scan_callback(struct proc
*p
, void *data
)
1513 struct vm_pageout_scan_info
*info
= data
;
1517 * Never kill system processes or init. If we have configured swap
1518 * then try to avoid killing low-numbered pids.
1520 if ((p
->p_flags
& P_SYSTEM
) || (p
->p_pid
== 1) ||
1521 ((p
->p_pid
< 48) && (vm_swap_size
!= 0))) {
1525 lwkt_gettoken(&p
->p_token
);
1528 * if the process is in a non-running type state,
1531 if (p
->p_stat
!= SACTIVE
&& p
->p_stat
!= SSTOP
) {
1532 lwkt_reltoken(&p
->p_token
);
1537 * Get the approximate process size. Note that anonymous pages
1538 * with backing swap will be counted twice, but there should not
1539 * be too many such pages due to the stress the VM system is
1540 * under at this point.
1542 size
= vmspace_anonymous_count(p
->p_vmspace
) +
1543 vmspace_swap_count(p
->p_vmspace
);
1546 * If the this process is bigger than the biggest one
1549 if (info
->bigsize
< size
) {
1551 PRELE(info
->bigproc
);
1554 info
->bigsize
= size
;
1556 lwkt_reltoken(&p
->p_token
);
1563 * This routine tries to maintain the pseudo LRU active queue,
1564 * so that during long periods of time where there is no paging,
1565 * that some statistic accumulation still occurs. This code
1566 * helps the situation where paging just starts to occur.
1569 vm_pageout_page_stats(int q
)
1571 static int fullintervalcount
= 0;
1572 struct vm_page marker
;
1574 int pcount
, tpcount
; /* Number of pages to check */
1577 page_shortage
= (vmstats
.v_inactive_target
+ vmstats
.v_cache_max
+
1578 vmstats
.v_free_min
) -
1579 (vmstats
.v_free_count
+ vmstats
.v_inactive_count
+
1580 vmstats
.v_cache_count
);
1582 if (page_shortage
<= 0)
1585 pcount
= vm_page_queues
[PQ_ACTIVE
+ q
].lcnt
;
1586 fullintervalcount
+= vm_pageout_stats_interval
;
1587 if (fullintervalcount
< vm_pageout_full_stats_interval
) {
1588 tpcount
= (vm_pageout_stats_max
* pcount
) /
1589 vmstats
.v_page_count
+ 1;
1590 if (pcount
> tpcount
)
1593 fullintervalcount
= 0;
1596 bzero(&marker
, sizeof(marker
));
1597 marker
.flags
= PG_BUSY
| PG_FICTITIOUS
| PG_MARKER
;
1598 marker
.queue
= PQ_ACTIVE
+ q
;
1600 marker
.wire_count
= 1;
1602 vm_page_queues_spin_lock(PQ_ACTIVE
+ q
);
1603 TAILQ_INSERT_HEAD(&vm_page_queues
[PQ_ACTIVE
+ q
].pl
, &marker
, pageq
);
1606 * Queue locked at top of loop to avoid stack marker issues.
1608 while ((m
= TAILQ_NEXT(&marker
, pageq
)) != NULL
&&
1613 KKASSERT(m
->queue
- m
->pc
== PQ_ACTIVE
);
1614 TAILQ_REMOVE(&vm_page_queues
[PQ_ACTIVE
+ q
].pl
, &marker
, pageq
);
1615 TAILQ_INSERT_AFTER(&vm_page_queues
[PQ_ACTIVE
+ q
].pl
, m
,
1619 * Skip marker pages (atomic against other markers to avoid
1620 * infinite hop-over scans).
1622 if (m
->flags
& PG_MARKER
)
1626 * Ignore pages we can't busy
1628 if (vm_page_busy_try(m
, TRUE
))
1632 * Remaining operations run with the page busy and neither
1633 * the page or the queue will be spin-locked.
1635 vm_page_queues_spin_unlock(PQ_ACTIVE
+ q
);
1636 KKASSERT(m
->queue
- m
->pc
== PQ_ACTIVE
);
1639 * We now have a safely busied page, the page and queue
1640 * spinlocks have been released.
1644 if (m
->hold_count
) {
1650 * Calculate activity
1653 if (m
->flags
& PG_REFERENCED
) {
1654 vm_page_flag_clear(m
, PG_REFERENCED
);
1657 actcount
+= pmap_ts_referenced(m
);
1660 * Update act_count and move page to end of queue.
1663 m
->act_count
+= ACT_ADVANCE
+ actcount
;
1664 if (m
->act_count
> ACT_MAX
)
1665 m
->act_count
= ACT_MAX
;
1666 vm_page_and_queue_spin_lock(m
);
1667 if (m
->queue
- m
->pc
== PQ_ACTIVE
) {
1669 &vm_page_queues
[PQ_ACTIVE
+ q
].pl
,
1672 &vm_page_queues
[PQ_ACTIVE
+ q
].pl
,
1675 vm_page_and_queue_spin_unlock(m
);
1680 if (m
->act_count
== 0) {
1682 * We turn off page access, so that we have
1683 * more accurate RSS stats. We don't do this
1684 * in the normal page deactivation when the
1685 * system is loaded VM wise, because the
1686 * cost of the large number of page protect
1687 * operations would be higher than the value
1688 * of doing the operation.
1690 * We use the marker to save our place so
1691 * we can release the spin lock. both (m)
1692 * and (next) will be invalid.
1694 vm_page_protect(m
, VM_PROT_NONE
);
1695 vm_page_deactivate(m
);
1697 m
->act_count
-= min(m
->act_count
, ACT_DECLINE
);
1698 vm_page_and_queue_spin_lock(m
);
1699 if (m
->queue
- m
->pc
== PQ_ACTIVE
) {
1701 &vm_page_queues
[PQ_ACTIVE
+ q
].pl
,
1704 &vm_page_queues
[PQ_ACTIVE
+ q
].pl
,
1707 vm_page_and_queue_spin_unlock(m
);
1711 vm_page_queues_spin_lock(PQ_ACTIVE
+ q
);
1715 * Remove our local marker
1717 * Page queue still spin-locked.
1719 TAILQ_REMOVE(&vm_page_queues
[PQ_ACTIVE
+ q
].pl
, &marker
, pageq
);
1720 vm_page_queues_spin_unlock(PQ_ACTIVE
+ q
);
1724 vm_pageout_free_page_calc(vm_size_t count
)
1726 if (count
< vmstats
.v_page_count
)
1729 * free_reserved needs to include enough for the largest swap pager
1730 * structures plus enough for any pv_entry structs when paging.
1732 * v_free_min normal allocations
1733 * v_free_reserved system allocations
1734 * v_pageout_free_min allocations by pageout daemon
1735 * v_interrupt_free_min low level allocations (e.g swap structures)
1737 if (vmstats
.v_page_count
> 1024)
1738 vmstats
.v_free_min
= 64 + (vmstats
.v_page_count
- 1024) / 200;
1740 vmstats
.v_free_min
= 64;
1741 vmstats
.v_free_reserved
= vmstats
.v_free_min
* 4 / 8 + 7;
1742 vmstats
.v_free_severe
= vmstats
.v_free_min
* 4 / 8 + 0;
1743 vmstats
.v_pageout_free_min
= vmstats
.v_free_min
* 2 / 8 + 7;
1744 vmstats
.v_interrupt_free_min
= vmstats
.v_free_min
* 1 / 8 + 7;
1751 * vm_pageout is the high level pageout daemon.
1756 vm_pageout_thread(void)
1764 * Initialize some paging parameters.
1766 curthread
->td_flags
|= TDF_SYSTHREAD
;
1768 vm_pageout_free_page_calc(vmstats
.v_page_count
);
1771 * v_free_target and v_cache_min control pageout hysteresis. Note
1772 * that these are more a measure of the VM cache queue hysteresis
1773 * then the VM free queue. Specifically, v_free_target is the
1774 * high water mark (free+cache pages).
1776 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1777 * low water mark, while v_free_min is the stop. v_cache_min must
1778 * be big enough to handle memory needs while the pageout daemon
1779 * is signalled and run to free more pages.
1781 if (vmstats
.v_free_count
> 6144)
1782 vmstats
.v_free_target
= 4 * vmstats
.v_free_min
+ vmstats
.v_free_reserved
;
1784 vmstats
.v_free_target
= 2 * vmstats
.v_free_min
+ vmstats
.v_free_reserved
;
1787 * NOTE: With the new buffer cache b_act_count we want the default
1788 * inactive target to be a percentage of available memory.
1790 * The inactive target essentially determines the minimum
1791 * number of 'temporary' pages capable of caching one-time-use
1792 * files when the VM system is otherwise full of pages
1793 * belonging to multi-time-use files or active program data.
1795 * NOTE: The inactive target is aggressively persued only if the
1796 * inactive queue becomes too small. If the inactive queue
1797 * is large enough to satisfy page movement to free+cache
1798 * then it is repopulated more slowly from the active queue.
1799 * This allows a general inactive_target default to be set.
1801 * There is an issue here for processes which sit mostly idle
1802 * 'overnight', such as sshd, tcsh, and X. Any movement from
1803 * the active queue will eventually cause such pages to
1804 * recycle eventually causing a lot of paging in the morning.
1805 * To reduce the incidence of this pages cycled out of the
1806 * buffer cache are moved directly to the inactive queue if
1807 * they were only used once or twice.
1809 * The vfs.vm_cycle_point sysctl can be used to adjust this.
1810 * Increasing the value (up to 64) increases the number of
1811 * buffer recyclements which go directly to the inactive queue.
1813 if (vmstats
.v_free_count
> 2048) {
1814 vmstats
.v_cache_min
= vmstats
.v_free_target
;
1815 vmstats
.v_cache_max
= 2 * vmstats
.v_cache_min
;
1817 vmstats
.v_cache_min
= 0;
1818 vmstats
.v_cache_max
= 0;
1820 vmstats
.v_inactive_target
= vmstats
.v_free_count
/ 4;
1822 /* XXX does not really belong here */
1823 if (vm_page_max_wired
== 0)
1824 vm_page_max_wired
= vmstats
.v_free_count
/ 3;
1826 if (vm_pageout_stats_max
== 0)
1827 vm_pageout_stats_max
= vmstats
.v_free_target
;
1830 * Set interval in seconds for stats scan.
1832 if (vm_pageout_stats_interval
== 0)
1833 vm_pageout_stats_interval
= 5;
1834 if (vm_pageout_full_stats_interval
== 0)
1835 vm_pageout_full_stats_interval
= vm_pageout_stats_interval
* 4;
1839 * Set maximum free per pass
1841 if (vm_pageout_stats_free_max
== 0)
1842 vm_pageout_stats_free_max
= 5;
1844 swap_pager_swap_init();
1848 * The pageout daemon is never done, so loop forever.
1853 int inactive_shortage
;
1854 int vnodes_skipped
= 0;
1855 int recycle_count
= 0;
1859 * Wait for an action request. If we timeout check to
1860 * see if paging is needed (in case the normal wakeup
1863 if (vm_pages_needed
== 0) {
1864 error
= tsleep(&vm_pages_needed
,
1866 vm_pageout_stats_interval
* hz
);
1868 vm_paging_needed() == 0 &&
1869 vm_pages_needed
== 0) {
1870 for (q
= 0; q
< PQ_L2_SIZE
; ++q
)
1871 vm_pageout_page_stats(q
);
1874 vm_pages_needed
= 1;
1877 mycpu
->gd_cnt
.v_pdwakeups
++;
1880 * Do whatever cleanup that the pmap code can.
1885 * Scan for pageout. Try to avoid thrashing the system
1888 * Calculate our target for the number of free+cache pages we
1889 * want to get to. This is higher then the number that causes
1890 * allocations to stall (severe) in order to provide hysteresis,
1891 * and if we don't make it all the way but get to the minimum
1892 * we're happy. Goose it a bit if there are multiple requests
1895 * Don't reduce avail_shortage inside the loop or the
1896 * PQAVERAGE() calculation will break.
1898 avail_shortage
= vm_paging_target() + vm_pageout_deficit
;
1899 vm_pageout_deficit
= 0;
1901 if (avail_shortage
> 0) {
1904 for (q
= 0; q
< PQ_L2_SIZE
; ++q
) {
1905 delta
+= vm_pageout_scan_inactive(
1907 (q
+ q1iterator
) & PQ_L2_MASK
,
1908 PQAVERAGE(avail_shortage
),
1910 if (avail_shortage
- delta
<= 0)
1913 avail_shortage
-= delta
;
1918 * Figure out how many active pages we must deactivate. If
1919 * we were able to reach our target with just the inactive
1920 * scan above we limit the number of active pages we
1921 * deactivate to reduce unnecessary work.
1923 inactive_shortage
= vmstats
.v_inactive_target
-
1924 vmstats
.v_inactive_count
;
1927 * If we were unable to free sufficient inactive pages to
1928 * satisfy the free/cache queue requirements then simply
1929 * reaching the inactive target may not be good enough.
1930 * Try to deactivate pages in excess of the target based
1933 * However to prevent thrashing the VM system do not
1934 * deactivate more than an additional 1/10 the inactive
1935 * target's worth of active pages.
1937 if (avail_shortage
> 0) {
1938 tmp
= avail_shortage
* 2;
1939 if (tmp
> vmstats
.v_inactive_target
/ 10)
1940 tmp
= vmstats
.v_inactive_target
/ 10;
1941 inactive_shortage
+= tmp
;
1945 * Only trigger on inactive shortage. Triggering on
1946 * avail_shortage can starve the active queue with
1947 * unnecessary active->inactive transitions and destroy
1950 if (/*avail_shortage > 0 ||*/ inactive_shortage
> 0) {
1953 for (q
= 0; q
< PQ_L2_SIZE
; ++q
) {
1954 delta
+= vm_pageout_scan_active(
1956 (q
+ q2iterator
) & PQ_L2_MASK
,
1957 PQAVERAGE(avail_shortage
),
1958 PQAVERAGE(inactive_shortage
),
1960 if (inactive_shortage
- delta
<= 0 &&
1961 avail_shortage
- delta
<= 0) {
1965 inactive_shortage
-= delta
;
1966 avail_shortage
-= delta
;
1971 * Finally free enough cache pages to meet our free page
1972 * requirement and take more drastic measures if we are
1975 vm_pageout_scan_cache(avail_shortage
, pass
,
1976 vnodes_skipped
, recycle_count
);
1979 * Wait for more work.
1981 if (avail_shortage
> 0) {
1983 if (swap_pager_full
) {
1985 * Running out of memory, catastrophic back-off
1986 * to one-second intervals.
1988 tsleep(&vm_pages_needed
, 0, "pdelay", hz
);
1989 } else if (pass
< 10 && vm_pages_needed
> 1) {
1991 * Normal operation, additional processes
1992 * have already kicked us. Retry immediately.
1994 } else if (pass
< 10) {
1996 * Normal operation, fewer processes. Delay
1997 * a bit but allow wakeups.
1999 vm_pages_needed
= 0;
2000 tsleep(&vm_pages_needed
, 0, "pdelay", hz
/ 10);
2001 vm_pages_needed
= 1;
2004 * We've taken too many passes, forced delay.
2006 tsleep(&vm_pages_needed
, 0, "pdelay", hz
/ 10);
2008 } else if (vm_pages_needed
) {
2010 * Interlocked wakeup of waiters (non-optional).
2012 * Similar to vm_page_free_wakeup() in vm_page.c,
2016 if (!vm_page_count_min(vm_page_free_hysteresis
) ||
2017 !vm_page_count_target()) {
2018 vm_pages_needed
= 0;
2019 wakeup(&vmstats
.v_free_count
);
2027 static struct kproc_desc page_kp
= {
2032 SYSINIT(pagedaemon
, SI_SUB_KTHREAD_PAGE
, SI_ORDER_FIRST
, kproc_start
, &page_kp
);
2036 * Called after allocating a page out of the cache or free queue
2037 * to possibly wake the pagedaemon up to replentish our supply.
2039 * We try to generate some hysteresis by waking the pagedaemon up
2040 * when our free+cache pages go below the free_min+cache_min level.
2041 * The pagedaemon tries to get the count back up to at least the
2042 * minimum, and through to the target level if possible.
2044 * If the pagedaemon is already active bump vm_pages_needed as a hint
2045 * that there are even more requests pending.
2051 pagedaemon_wakeup(void)
2053 if (vm_paging_needed() && curthread
!= pagethread
) {
2054 if (vm_pages_needed
== 0) {
2055 vm_pages_needed
= 1; /* SMP race ok */
2056 wakeup(&vm_pages_needed
);
2057 } else if (vm_page_count_min(0)) {
2058 ++vm_pages_needed
; /* SMP race ok */
2063 #if !defined(NO_SWAPPING)
2070 vm_req_vmdaemon(void)
2072 static int lastrun
= 0;
2074 if ((ticks
> (lastrun
+ hz
)) || (ticks
< lastrun
)) {
2075 wakeup(&vm_daemon_needed
);
2080 static int vm_daemon_callback(struct proc
*p
, void *data __unused
);
2089 * XXX vm_daemon_needed specific token?
2092 tsleep(&vm_daemon_needed
, 0, "psleep", 0);
2093 if (vm_pageout_req_swapout
) {
2094 swapout_procs(vm_pageout_req_swapout
);
2095 vm_pageout_req_swapout
= 0;
2098 * scan the processes for exceeding their rlimits or if
2099 * process is swapped out -- deactivate pages
2101 allproc_scan(vm_daemon_callback
, NULL
);
2106 vm_daemon_callback(struct proc
*p
, void *data __unused
)
2109 vm_pindex_t limit
, size
;
2112 * if this is a system process or if we have already
2113 * looked at this process, skip it.
2115 lwkt_gettoken(&p
->p_token
);
2117 if (p
->p_flags
& (P_SYSTEM
| P_WEXIT
)) {
2118 lwkt_reltoken(&p
->p_token
);
2123 * if the process is in a non-running type state,
2126 if (p
->p_stat
!= SACTIVE
&& p
->p_stat
!= SSTOP
) {
2127 lwkt_reltoken(&p
->p_token
);
2134 limit
= OFF_TO_IDX(qmin(p
->p_rlimit
[RLIMIT_RSS
].rlim_cur
,
2135 p
->p_rlimit
[RLIMIT_RSS
].rlim_max
));
2138 * let processes that are swapped out really be
2139 * swapped out. Set the limit to nothing to get as
2140 * many pages out to swap as possible.
2142 if (p
->p_flags
& P_SWAPPEDOUT
)
2147 size
= vmspace_resident_count(vm
);
2148 if (limit
>= 0 && size
>= limit
) {
2149 vm_pageout_map_deactivate_pages(&vm
->vm_map
, limit
);
2153 lwkt_reltoken(&p
->p_token
);