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_page(vm_page_t m, int *max_launderp,
104 int *vnodes_skippedp, struct vnode **vpfailedp,
105 int pass, int vmflush_flags);
106 static int vm_pageout_clean_helper (vm_page_t, int);
107 static int vm_pageout_free_page_calc (vm_size_t count);
108 static void vm_pageout_page_free(vm_page_t m) ;
109 struct thread *pagethread;
111 #if !defined(NO_SWAPPING)
112 /* the kernel process "vm_daemon"*/
113 static void vm_daemon (void);
114 static struct thread *vmthread;
116 static struct kproc_desc vm_kp = {
121 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp);
124 int vm_pages_needed = 0; /* Event on which pageout daemon sleeps */
125 int vm_pageout_deficit = 0; /* Estimated number of pages deficit */
126 int vm_pageout_pages_needed = 0;/* pageout daemon needs pages */
127 int vm_page_free_hysteresis = 16;
129 #if !defined(NO_SWAPPING)
130 static int vm_pageout_req_swapout;
131 static int vm_daemon_needed;
133 static int vm_max_launder = 4096;
134 static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
135 static int vm_pageout_full_stats_interval = 0;
136 static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0;
137 static int defer_swap_pageouts=0;
138 static int disable_swap_pageouts=0;
139 static u_int vm_anonmem_decline = ACT_DECLINE;
140 static u_int vm_filemem_decline = ACT_DECLINE * 2;
142 #if defined(NO_SWAPPING)
143 static int vm_swap_enabled=0;
144 static int vm_swap_idle_enabled=0;
146 static int vm_swap_enabled=1;
147 static int vm_swap_idle_enabled=0;
150 SYSCTL_UINT(_vm, VM_PAGEOUT_ALGORITHM, anonmem_decline,
151 CTLFLAG_RW, &vm_anonmem_decline, 0, "active->inactive anon memory");
153 SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, filemem_decline,
154 CTLFLAG_RW, &vm_filemem_decline, 0, "active->inactive file cache");
156 SYSCTL_INT(_vm, OID_AUTO, page_free_hysteresis,
157 CTLFLAG_RW, &vm_page_free_hysteresis, 0,
158 "Free more pages than the minimum required");
160 SYSCTL_INT(_vm, OID_AUTO, max_launder,
161 CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
163 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
164 CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
166 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
167 CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
169 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
170 CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
172 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max,
173 CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented");
175 #if defined(NO_SWAPPING)
176 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
177 CTLFLAG_RD, &vm_swap_enabled, 0, "");
178 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
179 CTLFLAG_RD, &vm_swap_idle_enabled, 0, "");
181 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
182 CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
183 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
184 CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
187 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
188 CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
190 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
191 CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
193 static int pageout_lock_miss;
194 SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
195 CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
197 int vm_page_max_wired; /* XXX max # of wired pages system-wide */
199 #if !defined(NO_SWAPPING)
200 static vm_pindex_t vm_pageout_object_deactivate_pages(vm_map_t map,
201 vm_object_t object, vm_pindex_t limit,
202 vm_pindex_t obj_beg, vm_pindex_t obj_end);
203 static void vm_req_vmdaemon (void);
205 static void vm_pageout_page_stats(int q);
208 * Calculate approximately how many pages on each queue to try to
209 * clean. An exact calculation creates an edge condition when the
210 * queues are unbalanced so add significant slop. The queue scans
211 * will stop early when targets are reached and will start where they
212 * left off on the next pass.
214 * We need to be generous here because there are all sorts of loading
215 * conditions that can cause edge cases if try to average over all queues.
216 * In particular, storage subsystems have become so fast that paging
217 * activity can become quite frantic. Eventually we will probably need
218 * two paging threads, one for dirty pages and one for clean, to deal
219 * with the bandwidth requirements.
221 * So what we do is calculate a value that can be satisfied nominally by
222 * only having to scan half the queues.
230 avg = ((n + (PQ_L2_SIZE - 1)) / (PQ_L2_SIZE / 2) + 1);
232 avg = ((n - (PQ_L2_SIZE - 1)) / (PQ_L2_SIZE / 2) - 1);
238 * vm_pageout_clean_helper:
240 * Clean the page and remove it from the laundry. The page must not be
243 * We set the busy bit to cause potential page faults on this page to
244 * block. Note the careful timing, however, the busy bit isn't set till
245 * late and we cannot do anything that will mess with the page.
248 vm_pageout_clean_helper(vm_page_t m, int vmflush_flags)
251 vm_page_t mc[BLIST_MAX_ALLOC];
253 int ib, is, page_base;
254 vm_pindex_t pindex = m->pindex;
259 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
260 * with the new swapper, but we could have serious problems paging
261 * out other object types if there is insufficient memory.
263 * Unfortunately, checking free memory here is far too late, so the
264 * check has been moved up a procedural level.
268 * Don't mess with the page if it's busy, held, or special
270 * XXX do we really need to check hold_count here? hold_count
271 * isn't supposed to mess with vm_page ops except prevent the
272 * page from being reused.
274 if (m->hold_count != 0 || (m->flags & PG_UNMANAGED)) {
280 * Place page in cluster. Align cluster for optimal swap space
281 * allocation (whether it is swap or not). This is typically ~16-32
282 * pages, which also tends to align the cluster to multiples of the
283 * filesystem block size if backed by a filesystem.
285 page_base = pindex % BLIST_MAX_ALLOC;
291 * Scan object for clusterable pages.
293 * We can cluster ONLY if: ->> the page is NOT
294 * clean, wired, busy, held, or mapped into a
295 * buffer, and one of the following:
296 * 1) The page is inactive, or a seldom used
299 * 2) we force the issue.
301 * During heavy mmap/modification loads the pageout
302 * daemon can really fragment the underlying file
303 * due to flushing pages out of order and not trying
304 * align the clusters (which leave sporatic out-of-order
305 * holes). To solve this problem we do the reverse scan
306 * first and attempt to align our cluster, then do a
307 * forward scan if room remains.
309 vm_object_hold(object);
314 p = vm_page_lookup_busy_try(object, pindex - page_base + ib,
316 if (error || p == NULL)
318 if ((p->queue - p->pc) == PQ_CACHE ||
319 (p->flags & PG_UNMANAGED)) {
323 vm_page_test_dirty(p);
324 if (((p->dirty & p->valid) == 0 &&
325 (p->flags & PG_NEED_COMMIT) == 0) ||
326 p->wire_count != 0 || /* may be held by buf cache */
327 p->hold_count != 0) { /* may be undergoing I/O */
331 if (p->queue - p->pc != PQ_INACTIVE) {
332 if (p->queue - p->pc != PQ_ACTIVE ||
333 (vmflush_flags & VM_PAGER_ALLOW_ACTIVE) == 0) {
340 * Try to maintain page groupings in the cluster.
342 if (m->flags & PG_WINATCFLS)
343 vm_page_flag_set(p, PG_WINATCFLS);
345 vm_page_flag_clear(p, PG_WINATCFLS);
346 p->act_count = m->act_count;
353 while (is < BLIST_MAX_ALLOC &&
354 pindex - page_base + is < object->size) {
357 p = vm_page_lookup_busy_try(object, pindex - page_base + is,
359 if (error || p == NULL)
361 if (((p->queue - p->pc) == PQ_CACHE) ||
362 (p->flags & PG_UNMANAGED)) {
366 vm_page_test_dirty(p);
367 if (((p->dirty & p->valid) == 0 &&
368 (p->flags & PG_NEED_COMMIT) == 0) ||
369 p->wire_count != 0 || /* may be held by buf cache */
370 p->hold_count != 0) { /* may be undergoing I/O */
374 if (p->queue - p->pc != PQ_INACTIVE) {
375 if (p->queue - p->pc != PQ_ACTIVE ||
376 (vmflush_flags & VM_PAGER_ALLOW_ACTIVE) == 0) {
383 * Try to maintain page groupings in the cluster.
385 if (m->flags & PG_WINATCFLS)
386 vm_page_flag_set(p, PG_WINATCFLS);
388 vm_page_flag_clear(p, PG_WINATCFLS);
389 p->act_count = m->act_count;
395 vm_object_drop(object);
398 * we allow reads during pageouts...
400 return vm_pageout_flush(&mc[ib], is - ib, vmflush_flags);
404 * vm_pageout_flush() - launder the given pages
406 * The given pages are laundered. Note that we setup for the start of
407 * I/O ( i.e. busy the page ), mark it read-only, and bump the object
408 * reference count all in here rather then in the parent. If we want
409 * the parent to do more sophisticated things we may have to change
412 * The pages in the array must be busied by the caller and will be
413 * unbusied by this function.
416 vm_pageout_flush(vm_page_t *mc, int count, int vmflush_flags)
419 int pageout_status[count];
424 * Initiate I/O. Bump the vm_page_t->busy counter.
426 for (i = 0; i < count; i++) {
427 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
428 ("vm_pageout_flush page %p index %d/%d: partially "
429 "invalid page", mc[i], i, count));
430 vm_page_io_start(mc[i]);
434 * We must make the pages read-only. This will also force the
435 * modified bit in the related pmaps to be cleared. The pager
436 * cannot clear the bit for us since the I/O completion code
437 * typically runs from an interrupt. The act of making the page
438 * read-only handles the case for us.
440 * Then we can unbusy the pages, we still hold a reference by virtue
443 for (i = 0; i < count; i++) {
444 if (vmflush_flags & VM_PAGER_TRY_TO_CACHE)
445 vm_page_protect(mc[i], VM_PROT_NONE);
447 vm_page_protect(mc[i], VM_PROT_READ);
448 vm_page_wakeup(mc[i]);
451 object = mc[0]->object;
452 vm_object_pip_add(object, count);
454 vm_pager_put_pages(object, mc, count,
456 ((object == &kernel_object) ? VM_PAGER_PUT_SYNC : 0)),
459 for (i = 0; i < count; i++) {
460 vm_page_t mt = mc[i];
462 switch (pageout_status[i]) {
471 * Page outside of range of object. Right now we
472 * essentially lose the changes by pretending it
475 vm_page_busy_wait(mt, FALSE, "pgbad");
476 pmap_clear_modify(mt);
483 * A page typically cannot be paged out when we
484 * have run out of swap. We leave the page
485 * marked inactive and will try to page it out
488 * Starvation of the active page list is used to
489 * determine when the system is massively memory
498 * If not PENDing this was a synchronous operation and we
499 * clean up after the I/O. If it is PENDing the mess is
500 * cleaned up asynchronously.
502 * Also nominally act on the caller's wishes if the caller
503 * wants to try to really clean (cache or free) the page.
505 * Also nominally deactivate the page if the system is
508 if (pageout_status[i] != VM_PAGER_PEND) {
509 vm_page_busy_wait(mt, FALSE, "pgouw");
510 vm_page_io_finish(mt);
511 if (vmflush_flags & VM_PAGER_TRY_TO_CACHE) {
512 vm_page_try_to_cache(mt);
513 } else if (vm_page_count_severe()) {
514 vm_page_deactivate(mt);
519 vm_object_pip_wakeup(object);
525 #if !defined(NO_SWAPPING)
528 * Deactivate pages until the map RSS falls below the specified limit.
530 * This code is part of the process rlimit and vm_daemon handler and not
531 * part of the normal demand-paging code. We only check the top-level
534 * The map must be locked.
535 * The caller must hold the vm_object.
537 static int vm_pageout_object_deactivate_pages_callback(vm_page_t, void *);
538 static int vm_pageout_object_deactivate_pages_cmp(vm_page_t, void *);
541 vm_pageout_object_deactivate_pages(vm_map_t map, vm_object_t object,
546 struct rb_vm_page_scan_info info;
549 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
552 info.backing_offset_index = obj_beg;
553 info.backing_object = object;
558 if (pmap_resident_tlnw_count(vm_map_pmap(map)) <= limit)
560 if (object->type == OBJT_DEVICE ||
561 object->type == OBJT_MGTDEVICE ||
562 object->type == OBJT_PHYS) {
566 if (object->paging_in_progress)
571 if (object->shadow_count > 1)
575 * scan the objects entire memory queue. We hold the
576 * object's token so the scan should not race anything.
578 * The callback will adjust backing_offset_index past the
579 * last index scanned. This value only matters if we
582 info.limit = remove_mode;
584 info.desired = limit;
585 info.start_pindex = obj_beg;
586 info.end_pindex = obj_end;
587 info.object = object;
589 vm_page_rb_tree_RB_SCAN(&object->rb_memq,
590 vm_pageout_object_deactivate_pages_cmp,
591 vm_pageout_object_deactivate_pages_callback,
595 * Backing object recursion (we will loop up).
597 while ((object = info.object->backing_object) != NULL) {
598 vm_object_hold(object);
599 if (object != info.object->backing_object) {
600 vm_object_drop(object);
605 if (object == NULL) {
606 if (info.object != info.backing_object)
607 vm_object_drop(info.object);
610 advance = OFF_TO_IDX(info.object->backing_object_offset);
611 info.start_pindex += advance;
612 info.end_pindex += advance;
613 info.backing_offset_index += advance;
614 if (info.object != info.backing_object) {
615 vm_object_lock_swap();
616 vm_object_drop(info.object);
618 info.object = object;
623 * Return how far we want the caller to advance. The caller will
624 * ignore this value and use obj_end if the RSS limit is still not
627 return (info.backing_offset_index - info.start_pindex);
631 * Only page indices above start_pindex
635 vm_pageout_object_deactivate_pages_cmp(vm_page_t p, void *data)
637 struct rb_vm_page_scan_info *info = data;
639 if (p->pindex < info->start_pindex)
641 if (p->pindex >= info->end_pindex)
647 * The caller must hold the vm_object.
649 * info->count is bumped for every page removed from the process pmap.
651 * info->backing_offset_index is updated past the last scanned page.
652 * This value will be ignored and the scan forced to the mapent boundary
653 * by the caller if the resident count remains too high.
656 vm_pageout_object_deactivate_pages_callback(vm_page_t p, void *data)
658 struct rb_vm_page_scan_info *info = data;
663 * Basic tests - There should never be a marker, and we can stop
664 * once the RSS is below the required level.
666 KKASSERT((p->flags & PG_MARKER) == 0);
667 if (pmap_resident_tlnw_count(vm_map_pmap(info->map)) <=
672 mycpu->gd_cnt.v_pdpages++;
673 info->backing_offset_index = p->pindex + 1;
675 if (vm_page_busy_try(p, TRUE))
678 if (p->object != info->object) {
682 if (p->wire_count || p->hold_count || (p->flags & PG_UNMANAGED)) {
686 if (!pmap_page_exists_quick(vm_map_pmap(info->map), p)) {
691 actcount = pmap_ts_referenced(p);
693 vm_page_flag_set(p, PG_REFERENCED);
694 } else if (p->flags & PG_REFERENCED) {
698 vm_page_and_queue_spin_lock(p);
699 if (p->queue - p->pc != PQ_ACTIVE && (p->flags & PG_REFERENCED)) {
700 vm_page_and_queue_spin_unlock(p);
702 p->act_count += actcount;
703 vm_page_flag_clear(p, PG_REFERENCED);
704 } else if (p->queue - p->pc == PQ_ACTIVE) {
705 if ((p->flags & PG_REFERENCED) == 0) {
706 /* use ACT_ADVANCE for a faster decline */
707 p->act_count -= min(p->act_count, ACT_ADVANCE);
709 (vm_pageout_algorithm || (p->act_count == 0))) {
710 vm_page_and_queue_spin_unlock(p);
711 vm_page_deactivate(p);
714 TAILQ_REMOVE(&vm_page_queues[p->queue].pl,
716 TAILQ_INSERT_TAIL(&vm_page_queues[p->queue].pl,
718 vm_page_and_queue_spin_unlock(p);
721 vm_page_and_queue_spin_unlock(p);
723 vm_page_flag_clear(p, PG_REFERENCED);
725 vm_page_and_queue_spin_lock(p);
726 if (p->queue - p->pc == PQ_ACTIVE) {
727 if (p->act_count < (ACT_MAX - ACT_ADVANCE))
728 p->act_count += ACT_ADVANCE;
729 TAILQ_REMOVE(&vm_page_queues[p->queue].pl,
731 TAILQ_INSERT_TAIL(&vm_page_queues[p->queue].pl,
734 vm_page_and_queue_spin_unlock(p);
736 } else if (p->queue - p->pc == PQ_INACTIVE) {
738 TAILQ_REMOVE(&vm_page_queues[p->queue].pl,
740 TAILQ_INSERT_HEAD(&vm_page_queues[p->queue].pl,
743 /* use ACT_ADVANCE for a faster decline */
744 p->act_count -= min(p->act_count, ACT_ADVANCE);
745 vm_page_and_queue_spin_unlock(p);
746 if (p->act_count == 0) {
750 vm_page_and_queue_spin_unlock(p);
754 * Ok, try to fully clean the page and any nearby pages such that at
755 * least the requested page is freed or moved to the cache queue.
757 * We usually do this synchronously to allow us to get the page into
758 * the CACHE queue quickly, which will prevent memory exhaustion if
759 * a process with a memoryuse limit is running away. However, the
760 * sysadmin may desire to set vm.swap_user_async which relaxes this
761 * and improves write performance.
764 int max_launder = 0x7FFF;
765 int vnodes_skipped = 0;
767 struct vnode *vpfailed = NULL;
769 vmflush_flags = VM_PAGER_TRY_TO_CACHE | VM_PAGER_ALLOW_ACTIVE;
770 if (swap_user_async == 0)
771 vmflush_flags |= VM_PAGER_PUT_SYNC;
773 vm_page_protect(p, VM_PROT_NONE);
774 vm_page_flag_set(p, PG_WINATCFLS);
775 info->count += vm_pageout_page(p, &max_launder, &vnodes_skipped,
776 &vpfailed, 1, vmflush_flags);
787 * Deactivate some number of pages in a map due to set RLIMIT_RSS limits.
788 * that is relatively difficult to do.
791 vm_pageout_map_deactivate_pages(vm_map_t map, vm_pindex_t limit)
795 vm_ooffset_t pgout_offset;
796 vm_ooffset_t tmpe_end;
802 lockmgr(&map->lock, LK_EXCLUSIVE);
805 * Scan the map incrementally.
807 pgout_offset = map->pgout_offset;
809 tmpe = map->header.next;
815 while (tmpe != &map->header) {
816 if (tmpe->end <= pgout_offset) {
820 if (tmpe->maptype == VM_MAPTYPE_NORMAL ||
821 tmpe->maptype == VM_MAPTYPE_VPAGETABLE) {
822 obj = tmpe->object.vm_object;
823 if (obj && obj->shadow_count <= 1) {
824 if (pgout_offset < tmpe->start) {
825 obj_beg = tmpe->offset >> PAGE_SHIFT;
826 obj_end = ((tmpe->end - tmpe->start) +
827 tmpe->offset) >> PAGE_SHIFT;
829 obj_beg = (pgout_offset - tmpe->start +
830 tmpe->offset) >> PAGE_SHIFT;
831 obj_end = (tmpe->end - tmpe->start +
832 tmpe->offset) >> PAGE_SHIFT;
834 tmpe_end = tmpe->end;
843 * Attempt to continue where we left off until the RLIMIT is
844 * satisfied or we run out of retries. Note that the map remains
845 * locked, so the program is not going to be taking any faults
846 * while we are doing this.
850 count = vm_pageout_object_deactivate_pages(map, obj, limit,
853 if (pmap_resident_tlnw_count(vm_map_pmap(map)) > limit) {
854 pgout_offset = tmpe_end;
861 pgout_offset += count << PAGE_SHIFT;
864 if (pmap_resident_tlnw_count(vm_map_pmap(map)) > limit) {
872 map->pgout_offset = pgout_offset;
879 * Called when the pageout scan wants to free a page. We no longer
880 * try to cycle the vm_object here with a reference & dealloc, which can
881 * cause a non-trivial object collapse in a critical path.
883 * It is unclear why we cycled the ref_count in the past, perhaps to try
884 * to optimize shadow chain collapses but I don't quite see why it would
885 * be necessary. An OBJ_DEAD object should terminate any and all vm_pages
886 * synchronously and not have to be kicked-start.
889 vm_pageout_page_free(vm_page_t m)
891 vm_page_protect(m, VM_PROT_NONE);
896 * vm_pageout_scan does the dirty work for the pageout daemon.
898 struct vm_pageout_scan_info {
899 struct proc *bigproc;
903 static int vm_pageout_scan_callback(struct proc *p, void *data);
906 vm_pageout_scan_inactive(int pass, int q, int avail_shortage,
910 struct vm_page marker;
911 struct vnode *vpfailed; /* warning, allowed to be stale */
917 * Start scanning the inactive queue for pages we can move to the
918 * cache or free. The scan will stop when the target is reached or
919 * we have scanned the entire inactive queue. Note that m->act_count
920 * is not used to form decisions for the inactive queue, only for the
923 * max_launder limits the number of dirty pages we flush per scan.
924 * For most systems a smaller value (16 or 32) is more robust under
925 * extreme memory and disk pressure because any unnecessary writes
926 * to disk can result in extreme performance degredation. However,
927 * systems with excessive dirty pages (especially when MAP_NOSYNC is
928 * used) will die horribly with limited laundering. If the pageout
929 * daemon cannot clean enough pages in the first pass, we let it go
930 * all out in succeeding passes.
932 if ((max_launder = vm_max_launder) <= 1)
938 * Initialize our marker
940 bzero(&marker, sizeof(marker));
941 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
942 marker.queue = PQ_INACTIVE + q;
944 marker.wire_count = 1;
947 * Inactive queue scan.
949 * NOTE: The vm_page must be spinlocked before the queue to avoid
950 * deadlocks, so it is easiest to simply iterate the loop
951 * with the queue unlocked at the top.
955 vm_page_queues_spin_lock(PQ_INACTIVE + q);
956 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE + q].pl, &marker, pageq);
957 maxscan = vm_page_queues[PQ_INACTIVE + q].lcnt;
960 * Queue locked at top of loop to avoid stack marker issues.
962 while ((m = TAILQ_NEXT(&marker, pageq)) != NULL &&
963 maxscan-- > 0 && avail_shortage - delta > 0)
967 KKASSERT(m->queue == PQ_INACTIVE + q);
968 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE + q].pl,
970 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE + q].pl, m,
972 mycpu->gd_cnt.v_pdpages++;
975 * Skip marker pages (atomic against other markers to avoid
976 * infinite hop-over scans).
978 if (m->flags & PG_MARKER)
982 * Try to busy the page. Don't mess with pages which are
983 * already busy or reorder them in the queue.
985 if (vm_page_busy_try(m, TRUE))
989 * Remaining operations run with the page busy and neither
990 * the page or the queue will be spin-locked.
992 vm_page_queues_spin_unlock(PQ_INACTIVE + q);
993 KKASSERT(m->queue == PQ_INACTIVE + q);
995 count = vm_pageout_page(m, &max_launder, vnodes_skipped,
1000 * Systems with a ton of memory can wind up with huge
1001 * deactivation counts. Because the inactive scan is
1002 * doing a lot of flushing, the combination can result
1003 * in excessive paging even in situations where other
1004 * unrelated threads free up sufficient VM.
1006 * To deal with this we abort the nominal active->inactive
1007 * scan before we hit the inactive target when free+cache
1008 * levels have reached a reasonable target.
1010 * When deciding to stop early we need to add some slop to
1011 * the test and we need to return full completion to the caller
1012 * to prevent the caller from thinking there is something
1013 * wrong and issuing a low-memory+swap warning or pkill.
1015 * A deficit forces paging regardless of the state of the
1016 * VM page queues (used for RSS enforcement).
1019 vm_page_queues_spin_lock(PQ_INACTIVE + q);
1020 if (vm_paging_target() < -vm_max_launder) {
1022 * Stopping early, return full completion to caller.
1024 if (delta < avail_shortage)
1025 delta = avail_shortage;
1030 /* page queue still spin-locked */
1031 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE + q].pl, &marker, pageq);
1032 vm_page_queues_spin_unlock(PQ_INACTIVE + q);
1038 * Pageout the specified page, return the total number of pages paged out
1039 * (this routine may cluster).
1041 * The page must be busied and soft-busied by the caller and will be disposed
1042 * of by this function.
1045 vm_pageout_page(vm_page_t m, int *max_launderp, int *vnodes_skippedp,
1046 struct vnode **vpfailedp, int pass, int vmflush_flags)
1053 * It is possible for a page to be busied ad-hoc (e.g. the
1054 * pmap_collect() code) and wired and race against the
1055 * allocation of a new page. vm_page_alloc() may be forced
1056 * to deactivate the wired page in which case it winds up
1057 * on the inactive queue and must be handled here. We
1058 * correct the problem simply by unqueuing the page.
1060 if (m->wire_count) {
1061 vm_page_unqueue_nowakeup(m);
1063 kprintf("WARNING: pagedaemon: wired page on "
1064 "inactive queue %p\n", m);
1069 * A held page may be undergoing I/O, so skip it.
1071 if (m->hold_count) {
1072 vm_page_and_queue_spin_lock(m);
1073 if (m->queue - m->pc == PQ_INACTIVE) {
1075 &vm_page_queues[m->queue].pl, m, pageq);
1077 &vm_page_queues[m->queue].pl, m, pageq);
1078 ++vm_swapcache_inactive_heuristic;
1080 vm_page_and_queue_spin_unlock(m);
1085 if (m->object == NULL || m->object->ref_count == 0) {
1087 * If the object is not being used, we ignore previous
1090 vm_page_flag_clear(m, PG_REFERENCED);
1091 pmap_clear_reference(m);
1092 /* fall through to end */
1093 } else if (((m->flags & PG_REFERENCED) == 0) &&
1094 (actcount = pmap_ts_referenced(m))) {
1096 * Otherwise, if the page has been referenced while
1097 * in the inactive queue, we bump the "activation
1098 * count" upwards, making it less likely that the
1099 * page will be added back to the inactive queue
1100 * prematurely again. Here we check the page tables
1101 * (or emulated bits, if any), given the upper level
1102 * VM system not knowing anything about existing
1105 vm_page_activate(m);
1106 m->act_count += (actcount + ACT_ADVANCE);
1112 * (m) is still busied.
1114 * If the upper level VM system knows about any page
1115 * references, we activate the page. We also set the
1116 * "activation count" higher than normal so that we will less
1117 * likely place pages back onto the inactive queue again.
1119 if ((m->flags & PG_REFERENCED) != 0) {
1120 vm_page_flag_clear(m, PG_REFERENCED);
1121 actcount = pmap_ts_referenced(m);
1122 vm_page_activate(m);
1123 m->act_count += (actcount + ACT_ADVANCE + 1);
1129 * If the upper level VM system doesn't know anything about
1130 * the page being dirty, we have to check for it again. As
1131 * far as the VM code knows, any partially dirty pages are
1134 * Pages marked PG_WRITEABLE may be mapped into the user
1135 * address space of a process running on another cpu. A
1136 * user process (without holding the MP lock) running on
1137 * another cpu may be able to touch the page while we are
1138 * trying to remove it. vm_page_cache() will handle this
1141 if (m->dirty == 0) {
1142 vm_page_test_dirty(m);
1147 if (m->valid == 0 && (m->flags & PG_NEED_COMMIT) == 0) {
1149 * Invalid pages can be easily freed
1151 vm_pageout_page_free(m);
1152 mycpu->gd_cnt.v_dfree++;
1154 } else if (m->dirty == 0 && (m->flags & PG_NEED_COMMIT) == 0) {
1156 * Clean pages can be placed onto the cache queue.
1157 * This effectively frees them.
1161 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
1163 * Dirty pages need to be paged out, but flushing
1164 * a page is extremely expensive verses freeing
1165 * a clean page. Rather then artificially limiting
1166 * the number of pages we can flush, we instead give
1167 * dirty pages extra priority on the inactive queue
1168 * by forcing them to be cycled through the queue
1169 * twice before being flushed, after which the
1170 * (now clean) page will cycle through once more
1171 * before being freed. This significantly extends
1172 * the thrash point for a heavily loaded machine.
1174 vm_page_flag_set(m, PG_WINATCFLS);
1175 vm_page_and_queue_spin_lock(m);
1176 if (m->queue - m->pc == PQ_INACTIVE) {
1178 &vm_page_queues[m->queue].pl, m, pageq);
1180 &vm_page_queues[m->queue].pl, m, pageq);
1181 ++vm_swapcache_inactive_heuristic;
1183 vm_page_and_queue_spin_unlock(m);
1185 } else if (*max_launderp > 0) {
1187 * We always want to try to flush some dirty pages if
1188 * we encounter them, to keep the system stable.
1189 * Normally this number is small, but under extreme
1190 * pressure where there are insufficient clean pages
1191 * on the inactive queue, we may have to go all out.
1193 int swap_pageouts_ok;
1194 struct vnode *vp = NULL;
1196 swap_pageouts_ok = 0;
1199 (object->type != OBJT_SWAP) &&
1200 (object->type != OBJT_DEFAULT)) {
1201 swap_pageouts_ok = 1;
1203 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
1204 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
1205 vm_page_count_min(0));
1209 * We don't bother paging objects that are "dead".
1210 * Those objects are in a "rundown" state.
1212 if (!swap_pageouts_ok ||
1214 (object->flags & OBJ_DEAD)) {
1215 vm_page_and_queue_spin_lock(m);
1216 if (m->queue - m->pc == PQ_INACTIVE) {
1218 &vm_page_queues[m->queue].pl,
1221 &vm_page_queues[m->queue].pl,
1223 ++vm_swapcache_inactive_heuristic;
1225 vm_page_and_queue_spin_unlock(m);
1231 * (m) is still busied.
1233 * The object is already known NOT to be dead. It
1234 * is possible for the vget() to block the whole
1235 * pageout daemon, but the new low-memory handling
1236 * code should prevent it.
1238 * The previous code skipped locked vnodes and, worse,
1239 * reordered pages in the queue. This results in
1240 * completely non-deterministic operation because,
1241 * quite often, a vm_fault has initiated an I/O and
1242 * is holding a locked vnode at just the point where
1243 * the pageout daemon is woken up.
1245 * We can't wait forever for the vnode lock, we might
1246 * deadlock due to a vn_read() getting stuck in
1247 * vm_wait while holding this vnode. We skip the
1248 * vnode if we can't get it in a reasonable amount
1251 * vpfailed is used to (try to) avoid the case where
1252 * a large number of pages are associated with a
1253 * locked vnode, which could cause the pageout daemon
1254 * to stall for an excessive amount of time.
1256 if (object->type == OBJT_VNODE) {
1259 vp = object->handle;
1260 flags = LK_EXCLUSIVE;
1261 if (vp == *vpfailedp)
1264 flags |= LK_TIMELOCK;
1269 * We have unbusied (m) temporarily so we can
1270 * acquire the vp lock without deadlocking.
1271 * (m) is held to prevent destruction.
1273 if (vget(vp, flags) != 0) {
1275 ++pageout_lock_miss;
1276 if (object->flags & OBJ_MIGHTBEDIRTY)
1283 * The page might have been moved to another
1284 * queue during potential blocking in vget()
1285 * above. The page might have been freed and
1286 * reused for another vnode. The object might
1287 * have been reused for another vnode.
1289 if (m->queue - m->pc != PQ_INACTIVE ||
1290 m->object != object ||
1291 object->handle != vp) {
1292 if (object->flags & OBJ_MIGHTBEDIRTY)
1300 * The page may have been busied during the
1301 * blocking in vput(); We don't move the
1302 * page back onto the end of the queue so that
1303 * statistics are more correct if we don't.
1305 if (vm_page_busy_try(m, TRUE)) {
1313 * (m) is busied again
1315 * We own the busy bit and remove our hold
1316 * bit. If the page is still held it
1317 * might be undergoing I/O, so skip it.
1319 if (m->hold_count) {
1320 vm_page_and_queue_spin_lock(m);
1321 if (m->queue - m->pc == PQ_INACTIVE) {
1322 TAILQ_REMOVE(&vm_page_queues[m->queue].pl, m, pageq);
1323 TAILQ_INSERT_TAIL(&vm_page_queues[m->queue].pl, m, pageq);
1324 ++vm_swapcache_inactive_heuristic;
1326 vm_page_and_queue_spin_unlock(m);
1327 if (object->flags & OBJ_MIGHTBEDIRTY)
1333 /* (m) is left busied as we fall through */
1337 * page is busy and not held here.
1339 * If a page is dirty, then it is either being washed
1340 * (but not yet cleaned) or it is still in the
1341 * laundry. If it is still in the laundry, then we
1342 * start the cleaning operation.
1344 * decrement inactive_shortage on success to account
1345 * for the (future) cleaned page. Otherwise we
1346 * could wind up laundering or cleaning too many
1349 * NOTE: Cleaning the page here does not cause
1350 * force_deficit to be adjusted, because the
1351 * page is not being freed or moved to the
1354 count = vm_pageout_clean_helper(m, vmflush_flags);
1355 *max_launderp -= count;
1358 * Clean ate busy, page no longer accessible
1369 vm_pageout_scan_active(int pass, int q,
1370 int avail_shortage, int inactive_shortage,
1371 int *recycle_countp)
1373 struct vm_page marker;
1380 * We want to move pages from the active queue to the inactive
1381 * queue to get the inactive queue to the inactive target. If
1382 * we still have a page shortage from above we try to directly free
1383 * clean pages instead of moving them.
1385 * If we do still have a shortage we keep track of the number of
1386 * pages we free or cache (recycle_count) as a measure of thrashing
1387 * between the active and inactive queues.
1389 * If we were able to completely satisfy the free+cache targets
1390 * from the inactive pool we limit the number of pages we move
1391 * from the active pool to the inactive pool to 2x the pages we
1392 * had removed from the inactive pool (with a minimum of 1/5 the
1393 * inactive target). If we were not able to completely satisfy
1394 * the free+cache targets we go for the whole target aggressively.
1396 * NOTE: Both variables can end up negative.
1397 * NOTE: We are still in a critical section.
1400 bzero(&marker, sizeof(marker));
1401 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
1402 marker.queue = PQ_ACTIVE + q;
1404 marker.wire_count = 1;
1406 vm_page_queues_spin_lock(PQ_ACTIVE + q);
1407 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_ACTIVE + q].pl, &marker, pageq);
1408 maxscan = vm_page_queues[PQ_ACTIVE + q].lcnt;
1411 * Queue locked at top of loop to avoid stack marker issues.
1413 while ((m = TAILQ_NEXT(&marker, pageq)) != NULL &&
1414 maxscan-- > 0 && (avail_shortage - delta > 0 ||
1415 inactive_shortage > 0))
1417 KKASSERT(m->queue == PQ_ACTIVE + q);
1418 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE + q].pl,
1420 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_ACTIVE + q].pl, m,
1424 * Skip marker pages (atomic against other markers to avoid
1425 * infinite hop-over scans).
1427 if (m->flags & PG_MARKER)
1431 * Try to busy the page. Don't mess with pages which are
1432 * already busy or reorder them in the queue.
1434 if (vm_page_busy_try(m, TRUE))
1438 * Remaining operations run with the page busy and neither
1439 * the page or the queue will be spin-locked.
1441 vm_page_queues_spin_unlock(PQ_ACTIVE + q);
1442 KKASSERT(m->queue == PQ_ACTIVE + q);
1445 * Don't deactivate pages that are held, even if we can
1446 * busy them. (XXX why not?)
1448 if (m->hold_count != 0) {
1449 vm_page_and_queue_spin_lock(m);
1450 if (m->queue - m->pc == PQ_ACTIVE) {
1452 &vm_page_queues[PQ_ACTIVE + q].pl,
1455 &vm_page_queues[PQ_ACTIVE + q].pl,
1458 vm_page_and_queue_spin_unlock(m);
1464 * The count for pagedaemon pages is done after checking the
1465 * page for eligibility...
1467 mycpu->gd_cnt.v_pdpages++;
1470 * Check to see "how much" the page has been used and clear
1471 * the tracking access bits. If the object has no references
1472 * don't bother paying the expense.
1475 if (m->object && m->object->ref_count != 0) {
1476 if (m->flags & PG_REFERENCED)
1478 actcount += pmap_ts_referenced(m);
1480 m->act_count += ACT_ADVANCE + actcount;
1481 if (m->act_count > ACT_MAX)
1482 m->act_count = ACT_MAX;
1485 vm_page_flag_clear(m, PG_REFERENCED);
1488 * actcount is only valid if the object ref_count is non-zero.
1489 * If the page does not have an object, actcount will be zero.
1491 if (actcount && m->object->ref_count != 0) {
1492 vm_page_and_queue_spin_lock(m);
1493 if (m->queue - m->pc == PQ_ACTIVE) {
1495 &vm_page_queues[PQ_ACTIVE + q].pl,
1498 &vm_page_queues[PQ_ACTIVE + q].pl,
1501 vm_page_and_queue_spin_unlock(m);
1504 switch(m->object->type) {
1507 m->act_count -= min(m->act_count,
1508 vm_anonmem_decline);
1511 m->act_count -= min(m->act_count,
1512 vm_filemem_decline);
1515 if (vm_pageout_algorithm ||
1516 (m->object == NULL) ||
1517 (m->object && (m->object->ref_count == 0)) ||
1518 m->act_count < pass + 1
1521 * Deactivate the page. If we had a
1522 * shortage from our inactive scan try to
1523 * free (cache) the page instead.
1525 * Don't just blindly cache the page if
1526 * we do not have a shortage from the
1527 * inactive scan, that could lead to
1528 * gigabytes being moved.
1530 --inactive_shortage;
1531 if (avail_shortage - delta > 0 ||
1532 (m->object && (m->object->ref_count == 0)))
1534 if (avail_shortage - delta > 0)
1536 vm_page_protect(m, VM_PROT_NONE);
1537 if (m->dirty == 0 &&
1538 (m->flags & PG_NEED_COMMIT) == 0 &&
1539 avail_shortage - delta > 0) {
1542 vm_page_deactivate(m);
1546 vm_page_deactivate(m);
1551 vm_page_and_queue_spin_lock(m);
1552 if (m->queue - m->pc == PQ_ACTIVE) {
1554 &vm_page_queues[PQ_ACTIVE + q].pl,
1557 &vm_page_queues[PQ_ACTIVE + q].pl,
1560 vm_page_and_queue_spin_unlock(m);
1566 vm_page_queues_spin_lock(PQ_ACTIVE + q);
1570 * Clean out our local marker.
1572 * Page queue still spin-locked.
1574 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE + q].pl, &marker, pageq);
1575 vm_page_queues_spin_unlock(PQ_ACTIVE + q);
1581 * The number of actually free pages can drop down to v_free_reserved,
1582 * we try to build the free count back above v_free_min. Note that
1583 * vm_paging_needed() also returns TRUE if v_free_count is not at
1584 * least v_free_min so that is the minimum we must build the free
1587 * We use a slightly higher target to improve hysteresis,
1588 * ((v_free_target + v_free_min) / 2). Since v_free_target
1589 * is usually the same as v_cache_min this maintains about
1590 * half the pages in the free queue as are in the cache queue,
1591 * providing pretty good pipelining for pageout operation.
1593 * The system operator can manipulate vm.v_cache_min and
1594 * vm.v_free_target to tune the pageout demon. Be sure
1595 * to keep vm.v_free_min < vm.v_free_target.
1597 * Note that the original paging target is to get at least
1598 * (free_min + cache_min) into (free + cache). The slightly
1599 * higher target will shift additional pages from cache to free
1600 * without effecting the original paging target in order to
1601 * maintain better hysteresis and not have the free count always
1602 * be dead-on v_free_min.
1604 * NOTE: we are still in a critical section.
1606 * Pages moved from PQ_CACHE to totally free are not counted in the
1607 * pages_freed counter.
1610 vm_pageout_scan_cache(int avail_shortage, int pass,
1611 int vnodes_skipped, int recycle_count)
1613 static int lastkillticks;
1614 struct vm_pageout_scan_info info;
1617 while (vmstats.v_free_count <
1618 (vmstats.v_free_min + vmstats.v_free_target) / 2) {
1620 * This steals some code from vm/vm_page.c
1622 static int cache_rover = 0;
1624 m = vm_page_list_find(PQ_CACHE,
1625 cache_rover & PQ_L2_MASK, FALSE);
1628 /* page is returned removed from its queue and spinlocked */
1629 if (vm_page_busy_try(m, TRUE)) {
1630 vm_page_deactivate_locked(m);
1631 vm_page_spin_unlock(m);
1634 vm_page_spin_unlock(m);
1635 pagedaemon_wakeup();
1639 * Remaining operations run with the page busy and neither
1640 * the page or the queue will be spin-locked.
1642 if ((m->flags & (PG_UNMANAGED | PG_NEED_COMMIT)) ||
1645 vm_page_deactivate(m);
1649 KKASSERT((m->flags & PG_MAPPED) == 0);
1650 KKASSERT(m->dirty == 0);
1651 cache_rover += PQ_PRIME2;
1652 vm_pageout_page_free(m);
1653 mycpu->gd_cnt.v_dfree++;
1656 #if !defined(NO_SWAPPING)
1658 * Idle process swapout -- run once per second.
1660 if (vm_swap_idle_enabled) {
1662 if (time_uptime != lsec) {
1663 atomic_set_int(&vm_pageout_req_swapout, VM_SWAP_IDLE);
1671 * If we didn't get enough free pages, and we have skipped a vnode
1672 * in a writeable object, wakeup the sync daemon. And kick swapout
1673 * if we did not get enough free pages.
1675 if (vm_paging_target() > 0) {
1676 if (vnodes_skipped && vm_page_count_min(0))
1677 speedup_syncer(NULL);
1678 #if !defined(NO_SWAPPING)
1679 if (vm_swap_enabled && vm_page_count_target()) {
1680 atomic_set_int(&vm_pageout_req_swapout, VM_SWAP_NORMAL);
1687 * Handle catastrophic conditions. Under good conditions we should
1688 * be at the target, well beyond our minimum. If we could not even
1689 * reach our minimum the system is under heavy stress. But just being
1690 * under heavy stress does not trigger process killing.
1692 * We consider ourselves to have run out of memory if the swap pager
1693 * is full and avail_shortage is still positive. The secondary check
1694 * ensures that we do not kill processes if the instantanious
1695 * availability is good, even if the pageout demon pass says it
1696 * couldn't get to the target.
1698 if (swap_pager_almost_full &&
1700 (vm_page_count_min(recycle_count) || avail_shortage > 0)) {
1701 kprintf("Warning: system low on memory+swap "
1702 "shortage %d for %d ticks!\n",
1703 avail_shortage, ticks - swap_fail_ticks);
1705 if (swap_pager_full &&
1707 avail_shortage > 0 &&
1708 vm_paging_target() > 0 &&
1709 (unsigned int)(ticks - lastkillticks) >= hz) {
1711 * Kill something, maximum rate once per second to give
1712 * the process time to free up sufficient memory.
1714 lastkillticks = ticks;
1715 info.bigproc = NULL;
1717 allproc_scan(vm_pageout_scan_callback, &info);
1718 if (info.bigproc != NULL) {
1719 info.bigproc->p_nice = PRIO_MIN;
1720 info.bigproc->p_usched->resetpriority(
1721 FIRST_LWP_IN_PROC(info.bigproc));
1722 atomic_set_int(&info.bigproc->p_flags, P_LOWMEMKILL);
1723 killproc(info.bigproc, "out of swap space");
1724 wakeup(&vmstats.v_free_count);
1725 PRELE(info.bigproc);
1731 vm_pageout_scan_callback(struct proc *p, void *data)
1733 struct vm_pageout_scan_info *info = data;
1737 * Never kill system processes or init. If we have configured swap
1738 * then try to avoid killing low-numbered pids.
1740 if ((p->p_flags & P_SYSTEM) || (p->p_pid == 1) ||
1741 ((p->p_pid < 48) && (vm_swap_size != 0))) {
1745 lwkt_gettoken(&p->p_token);
1748 * if the process is in a non-running type state,
1751 if (p->p_stat != SACTIVE && p->p_stat != SSTOP && p->p_stat != SCORE) {
1752 lwkt_reltoken(&p->p_token);
1757 * Get the approximate process size. Note that anonymous pages
1758 * with backing swap will be counted twice, but there should not
1759 * be too many such pages due to the stress the VM system is
1760 * under at this point.
1762 size = vmspace_anonymous_count(p->p_vmspace) +
1763 vmspace_swap_count(p->p_vmspace);
1766 * If the this process is bigger than the biggest one
1769 if (info->bigsize < size) {
1771 PRELE(info->bigproc);
1774 info->bigsize = size;
1776 lwkt_reltoken(&p->p_token);
1783 * This routine tries to maintain the pseudo LRU active queue,
1784 * so that during long periods of time where there is no paging,
1785 * that some statistic accumulation still occurs. This code
1786 * helps the situation where paging just starts to occur.
1789 vm_pageout_page_stats(int q)
1791 static int fullintervalcount = 0;
1792 struct vm_page marker;
1794 int pcount, tpcount; /* Number of pages to check */
1797 page_shortage = (vmstats.v_inactive_target + vmstats.v_cache_max +
1798 vmstats.v_free_min) -
1799 (vmstats.v_free_count + vmstats.v_inactive_count +
1800 vmstats.v_cache_count);
1802 if (page_shortage <= 0)
1805 pcount = vm_page_queues[PQ_ACTIVE + q].lcnt;
1806 fullintervalcount += vm_pageout_stats_interval;
1807 if (fullintervalcount < vm_pageout_full_stats_interval) {
1808 tpcount = (vm_pageout_stats_max * pcount) /
1809 vmstats.v_page_count + 1;
1810 if (pcount > tpcount)
1813 fullintervalcount = 0;
1816 bzero(&marker, sizeof(marker));
1817 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
1818 marker.queue = PQ_ACTIVE + q;
1820 marker.wire_count = 1;
1822 vm_page_queues_spin_lock(PQ_ACTIVE + q);
1823 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_ACTIVE + q].pl, &marker, pageq);
1826 * Queue locked at top of loop to avoid stack marker issues.
1828 while ((m = TAILQ_NEXT(&marker, pageq)) != NULL &&
1833 KKASSERT(m->queue == PQ_ACTIVE + q);
1834 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE + q].pl, &marker, pageq);
1835 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_ACTIVE + q].pl, m,
1839 * Skip marker pages (atomic against other markers to avoid
1840 * infinite hop-over scans).
1842 if (m->flags & PG_MARKER)
1846 * Ignore pages we can't busy
1848 if (vm_page_busy_try(m, TRUE))
1852 * Remaining operations run with the page busy and neither
1853 * the page or the queue will be spin-locked.
1855 vm_page_queues_spin_unlock(PQ_ACTIVE + q);
1856 KKASSERT(m->queue == PQ_ACTIVE + q);
1859 * We now have a safely busied page, the page and queue
1860 * spinlocks have been released.
1864 if (m->hold_count) {
1870 * Calculate activity
1873 if (m->flags & PG_REFERENCED) {
1874 vm_page_flag_clear(m, PG_REFERENCED);
1877 actcount += pmap_ts_referenced(m);
1880 * Update act_count and move page to end of queue.
1883 m->act_count += ACT_ADVANCE + actcount;
1884 if (m->act_count > ACT_MAX)
1885 m->act_count = ACT_MAX;
1886 vm_page_and_queue_spin_lock(m);
1887 if (m->queue - m->pc == PQ_ACTIVE) {
1889 &vm_page_queues[PQ_ACTIVE + q].pl,
1892 &vm_page_queues[PQ_ACTIVE + q].pl,
1895 vm_page_and_queue_spin_unlock(m);
1900 if (m->act_count == 0) {
1902 * We turn off page access, so that we have
1903 * more accurate RSS stats. We don't do this
1904 * in the normal page deactivation when the
1905 * system is loaded VM wise, because the
1906 * cost of the large number of page protect
1907 * operations would be higher than the value
1908 * of doing the operation.
1910 * We use the marker to save our place so
1911 * we can release the spin lock. both (m)
1912 * and (next) will be invalid.
1914 vm_page_protect(m, VM_PROT_NONE);
1915 vm_page_deactivate(m);
1917 m->act_count -= min(m->act_count, ACT_DECLINE);
1918 vm_page_and_queue_spin_lock(m);
1919 if (m->queue - m->pc == PQ_ACTIVE) {
1921 &vm_page_queues[PQ_ACTIVE + q].pl,
1924 &vm_page_queues[PQ_ACTIVE + q].pl,
1927 vm_page_and_queue_spin_unlock(m);
1931 vm_page_queues_spin_lock(PQ_ACTIVE + q);
1935 * Remove our local marker
1937 * Page queue still spin-locked.
1939 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE + q].pl, &marker, pageq);
1940 vm_page_queues_spin_unlock(PQ_ACTIVE + q);
1944 vm_pageout_free_page_calc(vm_size_t count)
1946 if (count < vmstats.v_page_count)
1949 * free_reserved needs to include enough for the largest swap pager
1950 * structures plus enough for any pv_entry structs when paging.
1952 * v_free_min normal allocations
1953 * v_free_reserved system allocations
1954 * v_pageout_free_min allocations by pageout daemon
1955 * v_interrupt_free_min low level allocations (e.g swap structures)
1957 if (vmstats.v_page_count > 1024)
1958 vmstats.v_free_min = 64 + (vmstats.v_page_count - 1024) / 200;
1960 vmstats.v_free_min = 64;
1961 vmstats.v_free_reserved = vmstats.v_free_min * 4 / 8 + 7;
1962 vmstats.v_free_severe = vmstats.v_free_min * 4 / 8 + 0;
1963 vmstats.v_pageout_free_min = vmstats.v_free_min * 2 / 8 + 7;
1964 vmstats.v_interrupt_free_min = vmstats.v_free_min * 1 / 8 + 7;
1971 * vm_pageout is the high level pageout daemon.
1976 vm_pageout_thread(void)
1984 * Initialize some paging parameters.
1986 curthread->td_flags |= TDF_SYSTHREAD;
1988 vm_pageout_free_page_calc(vmstats.v_page_count);
1991 * v_free_target and v_cache_min control pageout hysteresis. Note
1992 * that these are more a measure of the VM cache queue hysteresis
1993 * then the VM free queue. Specifically, v_free_target is the
1994 * high water mark (free+cache pages).
1996 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1997 * low water mark, while v_free_min is the stop. v_cache_min must
1998 * be big enough to handle memory needs while the pageout daemon
1999 * is signalled and run to free more pages.
2001 if (vmstats.v_free_count > 6144)
2002 vmstats.v_free_target = 4 * vmstats.v_free_min + vmstats.v_free_reserved;
2004 vmstats.v_free_target = 2 * vmstats.v_free_min + vmstats.v_free_reserved;
2007 * NOTE: With the new buffer cache b_act_count we want the default
2008 * inactive target to be a percentage of available memory.
2010 * The inactive target essentially determines the minimum
2011 * number of 'temporary' pages capable of caching one-time-use
2012 * files when the VM system is otherwise full of pages
2013 * belonging to multi-time-use files or active program data.
2015 * NOTE: The inactive target is aggressively persued only if the
2016 * inactive queue becomes too small. If the inactive queue
2017 * is large enough to satisfy page movement to free+cache
2018 * then it is repopulated more slowly from the active queue.
2019 * This allows a general inactive_target default to be set.
2021 * There is an issue here for processes which sit mostly idle
2022 * 'overnight', such as sshd, tcsh, and X. Any movement from
2023 * the active queue will eventually cause such pages to
2024 * recycle eventually causing a lot of paging in the morning.
2025 * To reduce the incidence of this pages cycled out of the
2026 * buffer cache are moved directly to the inactive queue if
2027 * they were only used once or twice.
2029 * The vfs.vm_cycle_point sysctl can be used to adjust this.
2030 * Increasing the value (up to 64) increases the number of
2031 * buffer recyclements which go directly to the inactive queue.
2033 if (vmstats.v_free_count > 2048) {
2034 vmstats.v_cache_min = vmstats.v_free_target;
2035 vmstats.v_cache_max = 2 * vmstats.v_cache_min;
2037 vmstats.v_cache_min = 0;
2038 vmstats.v_cache_max = 0;
2040 vmstats.v_inactive_target = vmstats.v_free_count / 4;
2042 /* XXX does not really belong here */
2043 if (vm_page_max_wired == 0)
2044 vm_page_max_wired = vmstats.v_free_count / 3;
2046 if (vm_pageout_stats_max == 0)
2047 vm_pageout_stats_max = vmstats.v_free_target;
2050 * Set interval in seconds for stats scan.
2052 if (vm_pageout_stats_interval == 0)
2053 vm_pageout_stats_interval = 5;
2054 if (vm_pageout_full_stats_interval == 0)
2055 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
2059 * Set maximum free per pass
2061 if (vm_pageout_stats_free_max == 0)
2062 vm_pageout_stats_free_max = 5;
2064 swap_pager_swap_init();
2068 * The pageout daemon is never done, so loop forever.
2073 int inactive_shortage;
2074 int vnodes_skipped = 0;
2075 int recycle_count = 0;
2079 * Wait for an action request. If we timeout check to
2080 * see if paging is needed (in case the normal wakeup
2083 if (vm_pages_needed == 0) {
2084 error = tsleep(&vm_pages_needed,
2086 vm_pageout_stats_interval * hz);
2088 vm_paging_needed() == 0 &&
2089 vm_pages_needed == 0) {
2090 for (q = 0; q < PQ_L2_SIZE; ++q)
2091 vm_pageout_page_stats(q);
2094 vm_pages_needed = 1;
2097 mycpu->gd_cnt.v_pdwakeups++;
2100 * Scan for INACTIVE->CLEAN/PAGEOUT
2102 * This routine tries to avoid thrashing the system with
2103 * unnecessary activity.
2105 * Calculate our target for the number of free+cache pages we
2106 * want to get to. This is higher then the number that causes
2107 * allocations to stall (severe) in order to provide hysteresis,
2108 * and if we don't make it all the way but get to the minimum
2109 * we're happy. Goose it a bit if there are multiple requests
2112 * Don't reduce avail_shortage inside the loop or the
2113 * PQAVERAGE() calculation will break.
2115 * NOTE! deficit is differentiated from avail_shortage as
2116 * REQUIRING at least (deficit) pages to be cleaned,
2117 * even if the page queues are in good shape. This
2118 * is used primarily for handling per-process
2119 * RLIMIT_RSS and may also see small values when
2120 * processes block due to low memory.
2122 avail_shortage = vm_paging_target() + vm_pageout_deficit;
2123 vm_pageout_deficit = 0;
2125 if (avail_shortage > 0) {
2128 for (q = 0; q < PQ_L2_SIZE; ++q) {
2129 delta += vm_pageout_scan_inactive(
2131 (q + q1iterator) & PQ_L2_MASK,
2132 PQAVERAGE(avail_shortage),
2134 if (avail_shortage - delta <= 0)
2137 avail_shortage -= delta;
2142 * Figure out how many active pages we must deactivate. If
2143 * we were able to reach our target with just the inactive
2144 * scan above we limit the number of active pages we
2145 * deactivate to reduce unnecessary work.
2147 inactive_shortage = vmstats.v_inactive_target -
2148 vmstats.v_inactive_count;
2151 * If we were unable to free sufficient inactive pages to
2152 * satisfy the free/cache queue requirements then simply
2153 * reaching the inactive target may not be good enough.
2154 * Try to deactivate pages in excess of the target based
2157 * However to prevent thrashing the VM system do not
2158 * deactivate more than an additional 1/10 the inactive
2159 * target's worth of active pages.
2161 if (avail_shortage > 0) {
2162 tmp = avail_shortage * 2;
2163 if (tmp > vmstats.v_inactive_target / 10)
2164 tmp = vmstats.v_inactive_target / 10;
2165 inactive_shortage += tmp;
2169 * Only trigger a pmap cleanup on inactive shortage.
2171 if (inactive_shortage > 0) {
2176 * Scan for ACTIVE->INACTIVE
2178 * Only trigger on inactive shortage. Triggering on
2179 * avail_shortage can starve the active queue with
2180 * unnecessary active->inactive transitions and destroy
2183 if (/*avail_shortage > 0 ||*/ inactive_shortage > 0) {
2186 for (q = 0; q < PQ_L2_SIZE; ++q) {
2187 delta += vm_pageout_scan_active(
2189 (q + q2iterator) & PQ_L2_MASK,
2190 PQAVERAGE(avail_shortage),
2191 PQAVERAGE(inactive_shortage),
2193 if (inactive_shortage - delta <= 0 &&
2194 avail_shortage - delta <= 0) {
2198 inactive_shortage -= delta;
2199 avail_shortage -= delta;
2204 * Scan for CACHE->FREE
2206 * Finally free enough cache pages to meet our free page
2207 * requirement and take more drastic measures if we are
2210 vm_pageout_scan_cache(avail_shortage, pass,
2211 vnodes_skipped, recycle_count);
2214 * Wait for more work.
2216 if (avail_shortage > 0) {
2218 if (pass < 10 && vm_pages_needed > 1) {
2220 * Normal operation, additional processes
2221 * have already kicked us. Retry immediately
2222 * unless swap space is completely full in
2223 * which case delay a bit.
2225 if (swap_pager_full) {
2226 tsleep(&vm_pages_needed, 0, "pdelay",
2228 } /* else immediate retry */
2229 } else if (pass < 10) {
2231 * Normal operation, fewer processes. Delay
2232 * a bit but allow wakeups.
2234 vm_pages_needed = 0;
2235 tsleep(&vm_pages_needed, 0, "pdelay", hz / 10);
2236 vm_pages_needed = 1;
2237 } else if (swap_pager_full == 0) {
2239 * We've taken too many passes, forced delay.
2241 tsleep(&vm_pages_needed, 0, "pdelay", hz / 10);
2244 * Running out of memory, catastrophic
2245 * back-off to one-second intervals.
2247 tsleep(&vm_pages_needed, 0, "pdelay", hz);
2249 } else if (vm_pages_needed) {
2251 * Interlocked wakeup of waiters (non-optional).
2253 * Similar to vm_page_free_wakeup() in vm_page.c,
2257 if (!vm_page_count_min(vm_page_free_hysteresis) ||
2258 !vm_page_count_target()) {
2259 vm_pages_needed = 0;
2260 wakeup(&vmstats.v_free_count);
2268 static struct kproc_desc page_kp = {
2273 SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp);
2277 * Called after allocating a page out of the cache or free queue
2278 * to possibly wake the pagedaemon up to replentish our supply.
2280 * We try to generate some hysteresis by waking the pagedaemon up
2281 * when our free+cache pages go below the free_min+cache_min level.
2282 * The pagedaemon tries to get the count back up to at least the
2283 * minimum, and through to the target level if possible.
2285 * If the pagedaemon is already active bump vm_pages_needed as a hint
2286 * that there are even more requests pending.
2292 pagedaemon_wakeup(void)
2294 if (vm_paging_needed() && curthread != pagethread) {
2295 if (vm_pages_needed == 0) {
2296 vm_pages_needed = 1; /* SMP race ok */
2297 wakeup(&vm_pages_needed);
2298 } else if (vm_page_count_min(0)) {
2299 ++vm_pages_needed; /* SMP race ok */
2304 #if !defined(NO_SWAPPING)
2311 vm_req_vmdaemon(void)
2313 static int lastrun = 0;
2315 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
2316 wakeup(&vm_daemon_needed);
2321 static int vm_daemon_callback(struct proc *p, void *data __unused);
2332 tsleep(&vm_daemon_needed, 0, "psleep", 0);
2333 req_swapout = atomic_swap_int(&vm_pageout_req_swapout, 0);
2339 swapout_procs(vm_pageout_req_swapout);
2342 * scan the processes for exceeding their rlimits or if
2343 * process is swapped out -- deactivate pages
2345 allproc_scan(vm_daemon_callback, NULL);
2350 vm_daemon_callback(struct proc *p, void *data __unused)
2353 vm_pindex_t limit, size;
2356 * if this is a system process or if we have already
2357 * looked at this process, skip it.
2359 lwkt_gettoken(&p->p_token);
2361 if (p->p_flags & (P_SYSTEM | P_WEXIT)) {
2362 lwkt_reltoken(&p->p_token);
2367 * if the process is in a non-running type state,
2370 if (p->p_stat != SACTIVE && p->p_stat != SSTOP && p->p_stat != SCORE) {
2371 lwkt_reltoken(&p->p_token);
2378 limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
2379 p->p_rlimit[RLIMIT_RSS].rlim_max));
2382 * let processes that are swapped out really be
2383 * swapped out. Set the limit to nothing to get as
2384 * many pages out to swap as possible.
2386 if (p->p_flags & P_SWAPPEDOUT)
2391 size = pmap_resident_tlnw_count(&vm->vm_pmap);
2392 if (limit >= 0 && size >= limit) {
2393 vm_pageout_map_deactivate_pages(&vm->vm_map, limit);
2397 lwkt_reltoken(&p->p_token);