UPGRADING: Fix typo.
[dragonfly.git] / sys / vm / vm_pageout.c
CommitLineData
984263bc 1/*
99ad9bc4
MD
2 * (MPSAFE)
3 *
984263bc
MD
4 * Copyright (c) 1991 Regents of the University of California.
5 * All rights reserved.
6 * Copyright (c) 1994 John S. Dyson
7 * All rights reserved.
8 * Copyright (c) 1994 David Greenman
9 * All rights reserved.
10 *
11 * This code is derived from software contributed to Berkeley by
12 * The Mach Operating System project at Carnegie-Mellon University.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91
43 *
44 *
45 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46 * All rights reserved.
47 *
48 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
49 *
50 * Permission to use, copy, modify and distribute this software and
51 * its documentation is hereby granted, provided that both the copyright
52 * notice and this permission notice appear in all copies of the
53 * software, derivative works or modified versions, and any portions
54 * thereof, and that both notices appear in supporting documentation.
55 *
56 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
57 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
58 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
59 *
60 * Carnegie Mellon requests users of this software to return to
61 *
62 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
63 * School of Computer Science
64 * Carnegie Mellon University
65 * Pittsburgh PA 15213-3890
66 *
67 * any improvements or extensions that they make and grant Carnegie the
68 * rights to redistribute these changes.
69 *
70 * $FreeBSD: src/sys/vm/vm_pageout.c,v 1.151.2.15 2002/12/29 18:21:04 dillon Exp $
4ecf7cc9 71 * $DragonFly: src/sys/vm/vm_pageout.c,v 1.36 2008/07/01 02:02:56 dillon Exp $
984263bc
MD
72 */
73
74/*
75 * The proverbial page-out daemon.
76 */
77
78#include "opt_vm.h"
79#include <sys/param.h>
80#include <sys/systm.h>
81#include <sys/kernel.h>
82#include <sys/proc.h>
83#include <sys/kthread.h>
84#include <sys/resourcevar.h>
85#include <sys/signalvar.h>
86#include <sys/vnode.h>
87#include <sys/vmmeter.h>
88#include <sys/sysctl.h>
89
90#include <vm/vm.h>
91#include <vm/vm_param.h>
92#include <sys/lock.h>
93#include <vm/vm_object.h>
94#include <vm/vm_page.h>
95#include <vm/vm_map.h>
96#include <vm/vm_pageout.h>
97#include <vm/vm_pager.h>
98#include <vm/swap_pager.h>
99#include <vm/vm_extern.h>
5fd012e0
MD
100
101#include <sys/thread2.h>
12e4aaff 102#include <vm/vm_page2.h>
984263bc
MD
103
104/*
105 * System initialization
106 */
107
108/* the kernel process "vm_pageout"*/
1388df65 109static int vm_pageout_clean (vm_page_t);
20479584 110static int vm_pageout_scan (int pass);
1388df65 111static int vm_pageout_free_page_calc (vm_size_t count);
bc6dffab 112struct thread *pagethread;
984263bc 113
984263bc
MD
114#if !defined(NO_SWAPPING)
115/* the kernel process "vm_daemon"*/
1388df65 116static void vm_daemon (void);
bc6dffab 117static struct thread *vmthread;
984263bc
MD
118
119static struct kproc_desc vm_kp = {
120 "vmdaemon",
121 vm_daemon,
bc6dffab 122 &vmthread
984263bc
MD
123};
124SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp)
125#endif
126
127
128int vm_pages_needed=0; /* Event on which pageout daemon sleeps */
129int vm_pageout_deficit=0; /* Estimated number of pages deficit */
130int vm_pageout_pages_needed=0; /* flag saying that the pageout daemon needs pages */
131
132#if !defined(NO_SWAPPING)
133static int vm_pageout_req_swapout; /* XXX */
134static int vm_daemon_needed;
135#endif
984263bc
MD
136static int vm_max_launder = 32;
137static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
138static int vm_pageout_full_stats_interval = 0;
139static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0;
140static int defer_swap_pageouts=0;
141static int disable_swap_pageouts=0;
142
143#if defined(NO_SWAPPING)
144static int vm_swap_enabled=0;
145static int vm_swap_idle_enabled=0;
146#else
147static int vm_swap_enabled=1;
148static int vm_swap_idle_enabled=0;
149#endif
150
151SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
152 CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
153
154SYSCTL_INT(_vm, OID_AUTO, max_launder,
155 CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
156
157SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
158 CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
159
160SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
161 CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
162
163SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
164 CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
165
166SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max,
167 CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented");
168
169#if defined(NO_SWAPPING)
170SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
171 CTLFLAG_RD, &vm_swap_enabled, 0, "");
172SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
173 CTLFLAG_RD, &vm_swap_idle_enabled, 0, "");
174#else
175SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
176 CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
177SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
178 CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
179#endif
180
181SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
182 CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
183
184SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
185 CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
186
187static int pageout_lock_miss;
188SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
189 CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
190
46311ac2
MD
191int vm_load;
192SYSCTL_INT(_vm, OID_AUTO, vm_load,
193 CTLFLAG_RD, &vm_load, 0, "load on the VM system");
194int vm_load_enable = 1;
195SYSCTL_INT(_vm, OID_AUTO, vm_load_enable,
196 CTLFLAG_RW, &vm_load_enable, 0, "enable vm_load rate limiting");
197#ifdef INVARIANTS
198int vm_load_debug;
199SYSCTL_INT(_vm, OID_AUTO, vm_load_debug,
200 CTLFLAG_RW, &vm_load_debug, 0, "debug vm_load");
201#endif
202
984263bc
MD
203#define VM_PAGEOUT_PAGE_COUNT 16
204int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
205
206int vm_page_max_wired; /* XXX max # of wired pages system-wide */
207
208#if !defined(NO_SWAPPING)
1388df65
RG
209typedef void freeer_fcn_t (vm_map_t, vm_object_t, vm_pindex_t, int);
210static void vm_pageout_map_deactivate_pages (vm_map_t, vm_pindex_t);
984263bc 211static freeer_fcn_t vm_pageout_object_deactivate_pages;
1388df65 212static void vm_req_vmdaemon (void);
984263bc
MD
213#endif
214static void vm_pageout_page_stats(void);
215
46311ac2 216/*
20479584 217 * Update vm_load to slow down faulting processes.
99ad9bc4
MD
218 *
219 * SMP races ok.
220 * No requirements.
46311ac2
MD
221 */
222void
223vm_fault_ratecheck(void)
224{
225 if (vm_pages_needed) {
226 if (vm_load < 1000)
227 ++vm_load;
228 } else {
229 if (vm_load > 0)
230 --vm_load;
231 }
232}
233
984263bc
MD
234/*
235 * vm_pageout_clean:
236 *
06ecca5a
MD
237 * Clean the page and remove it from the laundry. The page must not be
238 * busy on-call.
984263bc
MD
239 *
240 * We set the busy bit to cause potential page faults on this page to
241 * block. Note the careful timing, however, the busy bit isn't set till
242 * late and we cannot do anything that will mess with the page.
99ad9bc4
MD
243 *
244 * The caller must hold vm_token.
984263bc 245 */
984263bc 246static int
57e43348 247vm_pageout_clean(vm_page_t m)
984263bc 248{
5f910b2f 249 vm_object_t object;
984263bc
MD
250 vm_page_t mc[2*vm_pageout_page_count];
251 int pageout_count;
252 int ib, is, page_base;
253 vm_pindex_t pindex = m->pindex;
254
255 object = m->object;
256
257 /*
258 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
259 * with the new swapper, but we could have serious problems paging
260 * out other object types if there is insufficient memory.
261 *
262 * Unfortunately, checking free memory here is far too late, so the
263 * check has been moved up a procedural level.
264 */
265
266 /*
267 * Don't mess with the page if it's busy, held, or special
268 */
269 if ((m->hold_count != 0) ||
270 ((m->busy != 0) || (m->flags & (PG_BUSY|PG_UNMANAGED)))) {
271 return 0;
272 }
273
274 mc[vm_pageout_page_count] = m;
275 pageout_count = 1;
276 page_base = vm_pageout_page_count;
277 ib = 1;
278 is = 1;
279
280 /*
281 * Scan object for clusterable pages.
282 *
283 * We can cluster ONLY if: ->> the page is NOT
284 * clean, wired, busy, held, or mapped into a
285 * buffer, and one of the following:
286 * 1) The page is inactive, or a seldom used
287 * active page.
288 * -or-
289 * 2) we force the issue.
290 *
291 * During heavy mmap/modification loads the pageout
292 * daemon can really fragment the underlying file
293 * due to flushing pages out of order and not trying
294 * align the clusters (which leave sporatic out-of-order
295 * holes). To solve this problem we do the reverse scan
296 * first and attempt to align our cluster, then do a
297 * forward scan if room remains.
298 */
299
300more:
301 while (ib && pageout_count < vm_pageout_page_count) {
302 vm_page_t p;
303
304 if (ib > pindex) {
305 ib = 0;
306 break;
307 }
308
309 if ((p = vm_page_lookup(object, pindex - ib)) == NULL) {
310 ib = 0;
311 break;
312 }
313 if (((p->queue - p->pc) == PQ_CACHE) ||
314 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
315 ib = 0;
316 break;
317 }
318 vm_page_test_dirty(p);
319 if ((p->dirty & p->valid) == 0 ||
320 p->queue != PQ_INACTIVE ||
321 p->wire_count != 0 || /* may be held by buf cache */
322 p->hold_count != 0) { /* may be undergoing I/O */
323 ib = 0;
324 break;
325 }
326 mc[--page_base] = p;
327 ++pageout_count;
328 ++ib;
329 /*
330 * alignment boundry, stop here and switch directions. Do
331 * not clear ib.
332 */
333 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
334 break;
335 }
336
337 while (pageout_count < vm_pageout_page_count &&
338 pindex + is < object->size) {
339 vm_page_t p;
340
341 if ((p = vm_page_lookup(object, pindex + is)) == NULL)
342 break;
343 if (((p->queue - p->pc) == PQ_CACHE) ||
344 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
345 break;
346 }
347 vm_page_test_dirty(p);
348 if ((p->dirty & p->valid) == 0 ||
349 p->queue != PQ_INACTIVE ||
350 p->wire_count != 0 || /* may be held by buf cache */
351 p->hold_count != 0) { /* may be undergoing I/O */
352 break;
353 }
354 mc[page_base + pageout_count] = p;
355 ++pageout_count;
356 ++is;
357 }
358
359 /*
360 * If we exhausted our forward scan, continue with the reverse scan
361 * when possible, even past a page boundry. This catches boundry
362 * conditions.
363 */
364 if (ib && pageout_count < vm_pageout_page_count)
365 goto more;
366
367 /*
368 * we allow reads during pageouts...
369 */
370 return vm_pageout_flush(&mc[page_base], pageout_count, 0);
371}
372
373/*
374 * vm_pageout_flush() - launder the given pages
375 *
376 * The given pages are laundered. Note that we setup for the start of
377 * I/O ( i.e. busy the page ), mark it read-only, and bump the object
378 * reference count all in here rather then in the parent. If we want
379 * the parent to do more sophisticated things we may have to change
380 * the ordering.
99ad9bc4
MD
381 *
382 * The caller must hold vm_token.
984263bc 383 */
984263bc 384int
57e43348 385vm_pageout_flush(vm_page_t *mc, int count, int flags)
984263bc 386{
5f910b2f 387 vm_object_t object;
984263bc
MD
388 int pageout_status[count];
389 int numpagedout = 0;
390 int i;
391
99ad9bc4
MD
392 ASSERT_LWKT_TOKEN_HELD(&vm_token);
393
984263bc 394 /*
17cde63e
MD
395 * Initiate I/O. Bump the vm_page_t->busy counter.
396 */
397 for (i = 0; i < count; i++) {
398 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, ("vm_pageout_flush page %p index %d/%d: partially invalid page", mc[i], i, count));
399 vm_page_io_start(mc[i]);
400 }
401
402 /*
4530a3aa
MD
403 * We must make the pages read-only. This will also force the
404 * modified bit in the related pmaps to be cleared. The pager
405 * cannot clear the bit for us since the I/O completion code
406 * typically runs from an interrupt. The act of making the page
407 * read-only handles the case for us.
984263bc 408 */
984263bc 409 for (i = 0; i < count; i++) {
984263bc
MD
410 vm_page_protect(mc[i], VM_PROT_READ);
411 }
412
413 object = mc[0]->object;
414 vm_object_pip_add(object, count);
415
416 vm_pager_put_pages(object, mc, count,
c439ad8f 417 (flags | ((object == &kernel_object) ? VM_PAGER_PUT_SYNC : 0)),
984263bc
MD
418 pageout_status);
419
420 for (i = 0; i < count; i++) {
421 vm_page_t mt = mc[i];
422
423 switch (pageout_status[i]) {
424 case VM_PAGER_OK:
425 numpagedout++;
426 break;
427 case VM_PAGER_PEND:
428 numpagedout++;
429 break;
430 case VM_PAGER_BAD:
431 /*
432 * Page outside of range of object. Right now we
433 * essentially lose the changes by pretending it
434 * worked.
435 */
436 pmap_clear_modify(mt);
437 vm_page_undirty(mt);
438 break;
439 case VM_PAGER_ERROR:
440 case VM_PAGER_FAIL:
441 /*
c84c24da
MD
442 * A page typically cannot be paged out when we
443 * have run out of swap. We leave the page
444 * marked inactive and will try to page it out
445 * again later.
446 *
447 * Starvation of the active page list is used to
448 * determine when the system is massively memory
449 * starved.
984263bc 450 */
984263bc
MD
451 break;
452 case VM_PAGER_AGAIN:
453 break;
454 }
455
456 /*
457 * If the operation is still going, leave the page busy to
458 * block all other accesses. Also, leave the paging in
459 * progress indicator set so that we don't attempt an object
460 * collapse.
93afe6be
MD
461 *
462 * For any pages which have completed synchronously,
463 * deactivate the page if we are under a severe deficit.
464 * Do not try to enter them into the cache, though, they
465 * might still be read-heavy.
984263bc
MD
466 */
467 if (pageout_status[i] != VM_PAGER_PEND) {
93afe6be
MD
468 if (vm_page_count_severe())
469 vm_page_deactivate(mt);
470#if 0
984263bc
MD
471 if (!vm_page_count_severe() || !vm_page_try_to_cache(mt))
472 vm_page_protect(mt, VM_PROT_READ);
93afe6be 473#endif
a491077e
MD
474 vm_page_io_finish(mt);
475 vm_object_pip_wakeup(object);
984263bc
MD
476 }
477 }
478 return numpagedout;
479}
480
481#if !defined(NO_SWAPPING)
482/*
483 * vm_pageout_object_deactivate_pages
484 *
485 * deactivate enough pages to satisfy the inactive target
486 * requirements or if vm_page_proc_limit is set, then
487 * deactivate all of the pages in the object and its
488 * backing_objects.
489 *
99ad9bc4
MD
490 * The map must be locked.
491 * The caller must hold vm_token.
984263bc 492 */
1f804340
MD
493static int vm_pageout_object_deactivate_pages_callback(vm_page_t, void *);
494
984263bc 495static void
57e43348 496vm_pageout_object_deactivate_pages(vm_map_t map, vm_object_t object,
99ad9bc4 497 vm_pindex_t desired, int map_remove_only)
984263bc 498{
1f804340 499 struct rb_vm_page_scan_info info;
984263bc 500 int remove_mode;
984263bc
MD
501
502 if (object->type == OBJT_DEVICE || object->type == OBJT_PHYS)
503 return;
504
505 while (object) {
506 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
507 return;
508 if (object->paging_in_progress)
509 return;
510
511 remove_mode = map_remove_only;
512 if (object->shadow_count > 1)
513 remove_mode = 1;
06ecca5a
MD
514
515 /*
516 * scan the objects entire memory queue. spl protection is
517 * required to avoid an interrupt unbusy/free race against
518 * our busy check.
519 */
5fd012e0 520 crit_enter();
1f804340
MD
521 info.limit = remove_mode;
522 info.map = map;
523 info.desired = desired;
524 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
525 vm_pageout_object_deactivate_pages_callback,
526 &info
527 );
528 crit_exit();
529 object = object->backing_object;
530 }
531}
99ad9bc4
MD
532
533/*
534 * The caller must hold vm_token.
535 */
1f804340
MD
536static int
537vm_pageout_object_deactivate_pages_callback(vm_page_t p, void *data)
538{
539 struct rb_vm_page_scan_info *info = data;
540 int actcount;
984263bc 541
1f804340
MD
542 if (pmap_resident_count(vm_map_pmap(info->map)) <= info->desired) {
543 return(-1);
544 }
545 mycpu->gd_cnt.v_pdpages++;
546 if (p->wire_count != 0 || p->hold_count != 0 || p->busy != 0 ||
547 (p->flags & (PG_BUSY|PG_UNMANAGED)) ||
548 !pmap_page_exists_quick(vm_map_pmap(info->map), p)) {
549 return(0);
550 }
984263bc 551
1f804340
MD
552 actcount = pmap_ts_referenced(p);
553 if (actcount) {
554 vm_page_flag_set(p, PG_REFERENCED);
555 } else if (p->flags & PG_REFERENCED) {
556 actcount = 1;
557 }
558
559 if ((p->queue != PQ_ACTIVE) &&
560 (p->flags & PG_REFERENCED)) {
561 vm_page_activate(p);
562 p->act_count += actcount;
563 vm_page_flag_clear(p, PG_REFERENCED);
564 } else if (p->queue == PQ_ACTIVE) {
565 if ((p->flags & PG_REFERENCED) == 0) {
566 p->act_count -= min(p->act_count, ACT_DECLINE);
567 if (!info->limit && (vm_pageout_algorithm || (p->act_count == 0))) {
17cde63e 568 vm_page_busy(p);
984263bc 569 vm_page_protect(p, VM_PROT_NONE);
1f804340 570 vm_page_deactivate(p);
a491077e 571 vm_page_wakeup(p);
1f804340
MD
572 } else {
573 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
574 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
984263bc 575 }
1f804340
MD
576 } else {
577 vm_page_activate(p);
578 vm_page_flag_clear(p, PG_REFERENCED);
579 if (p->act_count < (ACT_MAX - ACT_ADVANCE))
580 p->act_count += ACT_ADVANCE;
581 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
582 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
984263bc 583 }
1f804340 584 } else if (p->queue == PQ_INACTIVE) {
17cde63e 585 vm_page_busy(p);
1f804340 586 vm_page_protect(p, VM_PROT_NONE);
17cde63e 587 vm_page_wakeup(p);
984263bc 588 }
1f804340 589 return(0);
984263bc
MD
590}
591
592/*
99ad9bc4 593 * Deactivate some number of pages in a map, try to do it fairly, but
984263bc 594 * that is really hard to do.
99ad9bc4
MD
595 *
596 * The caller must hold vm_token.
984263bc
MD
597 */
598static void
57e43348 599vm_pageout_map_deactivate_pages(vm_map_t map, vm_pindex_t desired)
984263bc
MD
600{
601 vm_map_entry_t tmpe;
602 vm_object_t obj, bigobj;
603 int nothingwired;
604
df4f70a6 605 if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT)) {
984263bc
MD
606 return;
607 }
608
609 bigobj = NULL;
610 nothingwired = TRUE;
611
612 /*
613 * first, search out the biggest object, and try to free pages from
614 * that.
615 */
616 tmpe = map->header.next;
617 while (tmpe != &map->header) {
1b874851
MD
618 switch(tmpe->maptype) {
619 case VM_MAPTYPE_NORMAL:
620 case VM_MAPTYPE_VPAGETABLE:
984263bc
MD
621 obj = tmpe->object.vm_object;
622 if ((obj != NULL) && (obj->shadow_count <= 1) &&
623 ((bigobj == NULL) ||
624 (bigobj->resident_page_count < obj->resident_page_count))) {
625 bigobj = obj;
626 }
1b874851
MD
627 break;
628 default:
629 break;
984263bc
MD
630 }
631 if (tmpe->wired_count > 0)
632 nothingwired = FALSE;
633 tmpe = tmpe->next;
634 }
635
636 if (bigobj)
637 vm_pageout_object_deactivate_pages(map, bigobj, desired, 0);
638
639 /*
640 * Next, hunt around for other pages to deactivate. We actually
641 * do this search sort of wrong -- .text first is not the best idea.
642 */
643 tmpe = map->header.next;
644 while (tmpe != &map->header) {
645 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
646 break;
1b874851
MD
647 switch(tmpe->maptype) {
648 case VM_MAPTYPE_NORMAL:
649 case VM_MAPTYPE_VPAGETABLE:
984263bc
MD
650 obj = tmpe->object.vm_object;
651 if (obj)
652 vm_pageout_object_deactivate_pages(map, obj, desired, 0);
1b874851
MD
653 break;
654 default:
655 break;
984263bc
MD
656 }
657 tmpe = tmpe->next;
658 };
659
660 /*
661 * Remove all mappings if a process is swapped out, this will free page
662 * table pages.
663 */
664 if (desired == 0 && nothingwired)
665 pmap_remove(vm_map_pmap(map),
88181b08 666 VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
984263bc 667 vm_map_unlock(map);
984263bc
MD
668}
669#endif
670
671/*
a11aaa81
MD
672 * Don't try to be fancy - being fancy can lead to vnode deadlocks. We
673 * only do it for OBJT_DEFAULT and OBJT_SWAP objects which we know can
674 * be trivially freed.
99ad9bc4
MD
675 *
676 * The caller must hold vm_token.
a491077e
MD
677 *
678 * WARNING: vm_object_reference() can block.
984263bc 679 */
99ad9bc4 680static void
95813af0
MD
681vm_pageout_page_free(vm_page_t m)
682{
984263bc
MD
683 vm_object_t object = m->object;
684 int type = object->type;
685
a491077e 686 vm_page_busy(m);
984263bc
MD
687 if (type == OBJT_SWAP || type == OBJT_DEFAULT)
688 vm_object_reference(object);
984263bc
MD
689 vm_page_protect(m, VM_PROT_NONE);
690 vm_page_free(m);
691 if (type == OBJT_SWAP || type == OBJT_DEFAULT)
692 vm_object_deallocate(object);
693}
694
695/*
20479584 696 * vm_pageout_scan does the dirty work for the pageout daemon.
984263bc 697 */
8fa76237
MD
698struct vm_pageout_scan_info {
699 struct proc *bigproc;
700 vm_offset_t bigsize;
701};
702
703static int vm_pageout_scan_callback(struct proc *p, void *data);
704
99ad9bc4
MD
705/*
706 * The caller must hold vm_token.
707 */
20479584 708static int
984263bc
MD
709vm_pageout_scan(int pass)
710{
8fa76237 711 struct vm_pageout_scan_info info;
984263bc
MD
712 vm_page_t m, next;
713 struct vm_page marker;
5d6a945b 714 struct vnode *vpfailed; /* warning, allowed to be stale */
20479584
MD
715 int maxscan, pcount;
716 int recycle_count;
717 int inactive_shortage, active_shortage;
51db7ca2 718 int inactive_original_shortage;
984263bc
MD
719 vm_object_t object;
720 int actcount;
721 int vnodes_skipped = 0;
722 int maxlaunder;
984263bc
MD
723
724 /*
725 * Do whatever cleanup that the pmap code can.
726 */
727 pmap_collect();
728
984263bc 729 /*
20479584
MD
730 * Calculate our target for the number of free+cache pages we
731 * want to get to. This is higher then the number that causes
732 * allocations to stall (severe) in order to provide hysteresis,
733 * and if we don't make it all the way but get to the minimum
734 * we're happy.
984263bc 735 */
20479584 736 inactive_shortage = vm_paging_target() + vm_pageout_deficit;
51db7ca2 737 inactive_original_shortage = inactive_shortage;
20479584 738 vm_pageout_deficit = 0;
984263bc
MD
739
740 /*
741 * Initialize our marker
742 */
743 bzero(&marker, sizeof(marker));
744 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
745 marker.queue = PQ_INACTIVE;
746 marker.wire_count = 1;
747
748 /*
749 * Start scanning the inactive queue for pages we can move to the
750 * cache or free. The scan will stop when the target is reached or
751 * we have scanned the entire inactive queue. Note that m->act_count
752 * is not used to form decisions for the inactive queue, only for the
753 * active queue.
754 *
755 * maxlaunder limits the number of dirty pages we flush per scan.
756 * For most systems a smaller value (16 or 32) is more robust under
757 * extreme memory and disk pressure because any unnecessary writes
758 * to disk can result in extreme performance degredation. However,
759 * systems with excessive dirty pages (especially when MAP_NOSYNC is
760 * used) will die horribly with limited laundering. If the pageout
761 * daemon cannot clean enough pages in the first pass, we let it go
762 * all out in succeeding passes.
763 */
764 if ((maxlaunder = vm_max_launder) <= 1)
765 maxlaunder = 1;
766 if (pass)
767 maxlaunder = 10000;
768
06ecca5a 769 /*
5fd012e0
MD
770 * We will generally be in a critical section throughout the
771 * scan, but we can release it temporarily when we are sitting on a
772 * non-busy page without fear. this is required to prevent an
773 * interrupt from unbusying or freeing a page prior to our busy
774 * check, leaving us on the wrong queue or checking the wrong
775 * page.
06ecca5a 776 */
5fd012e0 777 crit_enter();
984263bc 778rescan0:
5d6a945b 779 vpfailed = NULL;
12e4aaff 780 maxscan = vmstats.v_inactive_count;
984263bc 781 for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
20479584 782 m != NULL && maxscan-- > 0 && inactive_shortage > 0;
06ecca5a
MD
783 m = next
784 ) {
12e4aaff 785 mycpu->gd_cnt.v_pdpages++;
984263bc 786
06ecca5a
MD
787 /*
788 * Give interrupts a chance
789 */
5fd012e0
MD
790 crit_exit();
791 crit_enter();
984263bc 792
06ecca5a
MD
793 /*
794 * It's easier for some of the conditions below to just loop
795 * and catch queue changes here rather then check everywhere
796 * else.
797 */
798 if (m->queue != PQ_INACTIVE)
799 goto rescan0;
984263bc
MD
800 next = TAILQ_NEXT(m, pageq);
801
802 /*
803 * skip marker pages
804 */
805 if (m->flags & PG_MARKER)
806 continue;
807
808 /*
809 * A held page may be undergoing I/O, so skip it.
810 */
811 if (m->hold_count) {
984263bc
MD
812 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
813 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
e527fb6b 814 ++vm_swapcache_inactive_heuristic;
984263bc
MD
815 continue;
816 }
06ecca5a 817
984263bc
MD
818 /*
819 * Dont mess with busy pages, keep in the front of the
820 * queue, most likely are being paged out.
821 */
822 if (m->busy || (m->flags & PG_BUSY)) {
984263bc
MD
823 continue;
824 }
825
984263bc 826 if (m->object->ref_count == 0) {
06ecca5a
MD
827 /*
828 * If the object is not being used, we ignore previous
829 * references.
830 */
984263bc
MD
831 vm_page_flag_clear(m, PG_REFERENCED);
832 pmap_clear_reference(m);
833
984263bc 834 } else if (((m->flags & PG_REFERENCED) == 0) &&
06ecca5a
MD
835 (actcount = pmap_ts_referenced(m))) {
836 /*
837 * Otherwise, if the page has been referenced while
838 * in the inactive queue, we bump the "activation
839 * count" upwards, making it less likely that the
840 * page will be added back to the inactive queue
841 * prematurely again. Here we check the page tables
842 * (or emulated bits, if any), given the upper level
843 * VM system not knowing anything about existing
844 * references.
845 */
984263bc
MD
846 vm_page_activate(m);
847 m->act_count += (actcount + ACT_ADVANCE);
848 continue;
849 }
850
851 /*
852 * If the upper level VM system knows about any page
853 * references, we activate the page. We also set the
854 * "activation count" higher than normal so that we will less
855 * likely place pages back onto the inactive queue again.
856 */
857 if ((m->flags & PG_REFERENCED) != 0) {
858 vm_page_flag_clear(m, PG_REFERENCED);
859 actcount = pmap_ts_referenced(m);
860 vm_page_activate(m);
861 m->act_count += (actcount + ACT_ADVANCE + 1);
862 continue;
863 }
864
865 /*
866 * If the upper level VM system doesn't know anything about
867 * the page being dirty, we have to check for it again. As
868 * far as the VM code knows, any partially dirty pages are
869 * fully dirty.
41a01a4d
MD
870 *
871 * Pages marked PG_WRITEABLE may be mapped into the user
872 * address space of a process running on another cpu. A
873 * user process (without holding the MP lock) running on
874 * another cpu may be able to touch the page while we are
17cde63e
MD
875 * trying to remove it. vm_page_cache() will handle this
876 * case for us.
984263bc
MD
877 */
878 if (m->dirty == 0) {
879 vm_page_test_dirty(m);
880 } else {
881 vm_page_dirty(m);
882 }
883
984263bc 884 if (m->valid == 0) {
41a01a4d
MD
885 /*
886 * Invalid pages can be easily freed
887 */
984263bc 888 vm_pageout_page_free(m);
12e4aaff 889 mycpu->gd_cnt.v_dfree++;
20479584 890 --inactive_shortage;
984263bc
MD
891 } else if (m->dirty == 0) {
892 /*
41a01a4d
MD
893 * Clean pages can be placed onto the cache queue.
894 * This effectively frees them.
984263bc 895 */
a491077e 896 vm_page_busy(m);
984263bc 897 vm_page_cache(m);
20479584 898 --inactive_shortage;
984263bc
MD
899 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
900 /*
901 * Dirty pages need to be paged out, but flushing
902 * a page is extremely expensive verses freeing
903 * a clean page. Rather then artificially limiting
904 * the number of pages we can flush, we instead give
905 * dirty pages extra priority on the inactive queue
906 * by forcing them to be cycled through the queue
907 * twice before being flushed, after which the
908 * (now clean) page will cycle through once more
909 * before being freed. This significantly extends
910 * the thrash point for a heavily loaded machine.
911 */
984263bc
MD
912 vm_page_flag_set(m, PG_WINATCFLS);
913 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
914 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
e527fb6b 915 ++vm_swapcache_inactive_heuristic;
984263bc
MD
916 } else if (maxlaunder > 0) {
917 /*
918 * We always want to try to flush some dirty pages if
919 * we encounter them, to keep the system stable.
920 * Normally this number is small, but under extreme
921 * pressure where there are insufficient clean pages
922 * on the inactive queue, we may have to go all out.
923 */
924 int swap_pageouts_ok;
925 struct vnode *vp = NULL;
926
927 object = m->object;
928
929 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
930 swap_pageouts_ok = 1;
931 } else {
932 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
933 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
20479584 934 vm_page_count_min(0));
984263bc
MD
935
936 }
937
938 /*
939 * We don't bother paging objects that are "dead".
940 * Those objects are in a "rundown" state.
941 */
942 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
984263bc
MD
943 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
944 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
e527fb6b 945 ++vm_swapcache_inactive_heuristic;
984263bc
MD
946 continue;
947 }
948
949 /*
950 * The object is already known NOT to be dead. It
951 * is possible for the vget() to block the whole
952 * pageout daemon, but the new low-memory handling
953 * code should prevent it.
954 *
955 * The previous code skipped locked vnodes and, worse,
956 * reordered pages in the queue. This results in
957 * completely non-deterministic operation because,
958 * quite often, a vm_fault has initiated an I/O and
959 * is holding a locked vnode at just the point where
960 * the pageout daemon is woken up.
961 *
962 * We can't wait forever for the vnode lock, we might
963 * deadlock due to a vn_read() getting stuck in
964 * vm_wait while holding this vnode. We skip the
965 * vnode if we can't get it in a reasonable amount
966 * of time.
5d6a945b
MD
967 *
968 * vpfailed is used to (try to) avoid the case where
969 * a large number of pages are associated with a
970 * locked vnode, which could cause the pageout daemon
971 * to stall for an excessive amount of time.
984263bc 972 */
984263bc 973 if (object->type == OBJT_VNODE) {
5d6a945b 974 int flags;
984263bc 975
5d6a945b
MD
976 vp = object->handle;
977 flags = LK_EXCLUSIVE | LK_NOOBJ;
978 if (vp == vpfailed)
979 flags |= LK_NOWAIT;
980 else
981 flags |= LK_TIMELOCK;
982 if (vget(vp, flags) != 0) {
983 vpfailed = vp;
984263bc
MD
984 ++pageout_lock_miss;
985 if (object->flags & OBJ_MIGHTBEDIRTY)
986 vnodes_skipped++;
987 continue;
988 }
989
990 /*
991 * The page might have been moved to another
992 * queue during potential blocking in vget()
993 * above. The page might have been freed and
994 * reused for another vnode. The object might
995 * have been reused for another vnode.
996 */
997 if (m->queue != PQ_INACTIVE ||
998 m->object != object ||
999 object->handle != vp) {
1000 if (object->flags & OBJ_MIGHTBEDIRTY)
1001 vnodes_skipped++;
1002 vput(vp);
1003 continue;
1004 }
1005
1006 /*
1007 * The page may have been busied during the
1008 * blocking in vput(); We don't move the
1009 * page back onto the end of the queue so that
1010 * statistics are more correct if we don't.
1011 */
1012 if (m->busy || (m->flags & PG_BUSY)) {
1013 vput(vp);
1014 continue;
1015 }
1016
1017 /*
1018 * If the page has become held it might
1019 * be undergoing I/O, so skip it
1020 */
1021 if (m->hold_count) {
984263bc
MD
1022 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1023 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
e527fb6b 1024 ++vm_swapcache_inactive_heuristic;
984263bc
MD
1025 if (object->flags & OBJ_MIGHTBEDIRTY)
1026 vnodes_skipped++;
1027 vput(vp);
1028 continue;
1029 }
1030 }
1031
1032 /*
1033 * If a page is dirty, then it is either being washed
1034 * (but not yet cleaned) or it is still in the
1035 * laundry. If it is still in the laundry, then we
1036 * start the cleaning operation.
1037 *
1038 * This operation may cluster, invalidating the 'next'
1039 * pointer. To prevent an inordinate number of
1040 * restarts we use our marker to remember our place.
1041 *
20479584
MD
1042 * decrement inactive_shortage on success to account
1043 * for the (future) cleaned page. Otherwise we
1044 * could wind up laundering or cleaning too many
1045 * pages.
984263bc 1046 */
984263bc 1047 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq);
984263bc 1048 if (vm_pageout_clean(m) != 0) {
20479584 1049 --inactive_shortage;
984263bc 1050 --maxlaunder;
c84c24da 1051 }
984263bc
MD
1052 next = TAILQ_NEXT(&marker, pageq);
1053 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq);
984263bc
MD
1054 if (vp != NULL)
1055 vput(vp);
1056 }
1057 }
1058
1059 /*
20479584
MD
1060 * We want to move pages from the active queue to the inactive
1061 * queue to get the inactive queue to the inactive target. If
1062 * we still have a page shortage from above we try to directly free
1063 * clean pages instead of moving them.
06ecca5a 1064 *
20479584
MD
1065 * If we do still have a shortage we keep track of the number of
1066 * pages we free or cache (recycle_count) as a measure of thrashing
1067 * between the active and inactive queues.
1068 *
51db7ca2
MD
1069 * If we were able to completely satisfy the free+cache targets
1070 * from the inactive pool we limit the number of pages we move
1071 * from the active pool to the inactive pool to 2x the pages we
e6e9a0c3
MD
1072 * had removed from the inactive pool (with a minimum of 1/5 the
1073 * inactive target). If we were not able to completely satisfy
1074 * the free+cache targets we go for the whole target aggressively.
20479584
MD
1075 *
1076 * NOTE: Both variables can end up negative.
1077 * NOTE: We are still in a critical section.
984263bc 1078 */
20479584 1079 active_shortage = vmstats.v_inactive_target - vmstats.v_inactive_count;
e6e9a0c3
MD
1080 if (inactive_original_shortage < vmstats.v_inactive_target / 10)
1081 inactive_original_shortage = vmstats.v_inactive_target / 10;
51db7ca2
MD
1082 if (inactive_shortage <= 0 &&
1083 active_shortage > inactive_original_shortage * 2) {
1084 active_shortage = inactive_original_shortage * 2;
1085 }
20479584 1086
12e4aaff 1087 pcount = vmstats.v_active_count;
20479584 1088 recycle_count = 0;
984263bc
MD
1089 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1090
20479584
MD
1091 while ((m != NULL) && (pcount-- > 0) &&
1092 (inactive_shortage > 0 || active_shortage > 0)
1093 ) {
06ecca5a
MD
1094 /*
1095 * Give interrupts a chance.
1096 */
5fd012e0
MD
1097 crit_exit();
1098 crit_enter();
984263bc
MD
1099
1100 /*
06ecca5a 1101 * If the page was ripped out from under us, just stop.
984263bc 1102 */
06ecca5a 1103 if (m->queue != PQ_ACTIVE)
984263bc 1104 break;
984263bc 1105 next = TAILQ_NEXT(m, pageq);
06ecca5a 1106
984263bc
MD
1107 /*
1108 * Don't deactivate pages that are busy.
1109 */
1110 if ((m->busy != 0) ||
1111 (m->flags & PG_BUSY) ||
1112 (m->hold_count != 0)) {
984263bc
MD
1113 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1114 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
984263bc
MD
1115 m = next;
1116 continue;
1117 }
1118
1119 /*
1120 * The count for pagedaemon pages is done after checking the
1121 * page for eligibility...
1122 */
12e4aaff 1123 mycpu->gd_cnt.v_pdpages++;
984263bc
MD
1124
1125 /*
20479584
MD
1126 * Check to see "how much" the page has been used and clear
1127 * the tracking access bits. If the object has no references
1128 * don't bother paying the expense.
984263bc
MD
1129 */
1130 actcount = 0;
1131 if (m->object->ref_count != 0) {
20479584
MD
1132 if (m->flags & PG_REFERENCED)
1133 ++actcount;
984263bc
MD
1134 actcount += pmap_ts_referenced(m);
1135 if (actcount) {
1136 m->act_count += ACT_ADVANCE + actcount;
1137 if (m->act_count > ACT_MAX)
1138 m->act_count = ACT_MAX;
1139 }
1140 }
984263bc
MD
1141 vm_page_flag_clear(m, PG_REFERENCED);
1142
1143 /*
20479584 1144 * actcount is only valid if the object ref_count is non-zero.
984263bc 1145 */
20479584 1146 if (actcount && m->object->ref_count != 0) {
984263bc
MD
1147 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1148 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
984263bc
MD
1149 } else {
1150 m->act_count -= min(m->act_count, ACT_DECLINE);
1151 if (vm_pageout_algorithm ||
1152 m->object->ref_count == 0 ||
20479584
MD
1153 m->act_count < pass + 1
1154 ) {
1155 /*
1156 * Deactivate the page. If we had a
1157 * shortage from our inactive scan try to
1158 * free (cache) the page instead.
e6e9a0c3
MD
1159 *
1160 * Don't just blindly cache the page if
1161 * we do not have a shortage from the
1162 * inactive scan, that could lead to
1163 * gigabytes being moved.
20479584
MD
1164 */
1165 --active_shortage;
1166 if (inactive_shortage > 0 ||
1167 m->object->ref_count == 0) {
1168 if (inactive_shortage > 0)
1169 ++recycle_count;
17cde63e 1170 vm_page_busy(m);
984263bc 1171 vm_page_protect(m, VM_PROT_NONE);
e6e9a0c3
MD
1172 if (m->dirty == 0 &&
1173 inactive_shortage > 0) {
20479584 1174 --inactive_shortage;
984263bc 1175 vm_page_cache(m);
c84c24da 1176 } else {
984263bc 1177 vm_page_deactivate(m);
a491077e 1178 vm_page_wakeup(m);
c84c24da 1179 }
984263bc
MD
1180 } else {
1181 vm_page_deactivate(m);
1182 }
1183 } else {
984263bc
MD
1184 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1185 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
984263bc
MD
1186 }
1187 }
1188 m = next;
1189 }
1190
984263bc 1191 /*
cd3c66bd
MD
1192 * The number of actually free pages can drop down to v_free_reserved,
1193 * we try to build the free count back above v_free_min. Note that
1194 * vm_paging_needed() also returns TRUE if v_free_count is not at
1195 * least v_free_min so that is the minimum we must build the free
1196 * count to.
1197 *
1198 * We use a slightly higher target to improve hysteresis,
1199 * ((v_free_target + v_free_min) / 2). Since v_free_target
1200 * is usually the same as v_cache_min this maintains about
1201 * half the pages in the free queue as are in the cache queue,
1202 * providing pretty good pipelining for pageout operation.
1203 *
1204 * The system operator can manipulate vm.v_cache_min and
1205 * vm.v_free_target to tune the pageout demon. Be sure
1206 * to keep vm.v_free_min < vm.v_free_target.
1207 *
1208 * Note that the original paging target is to get at least
1209 * (free_min + cache_min) into (free + cache). The slightly
1210 * higher target will shift additional pages from cache to free
1211 * without effecting the original paging target in order to
1212 * maintain better hysteresis and not have the free count always
1213 * be dead-on v_free_min.
06ecca5a 1214 *
5fd012e0 1215 * NOTE: we are still in a critical section.
c84c24da
MD
1216 *
1217 * Pages moved from PQ_CACHE to totally free are not counted in the
1218 * pages_freed counter.
984263bc 1219 */
cd3c66bd
MD
1220 while (vmstats.v_free_count <
1221 (vmstats.v_free_min + vmstats.v_free_target) / 2) {
1222 /*
1223 *
1224 */
984263bc
MD
1225 static int cache_rover = 0;
1226 m = vm_page_list_find(PQ_CACHE, cache_rover, FALSE);
20479584 1227 if (m == NULL)
984263bc
MD
1228 break;
1229 if ((m->flags & (PG_BUSY|PG_UNMANAGED)) ||
1230 m->busy ||
1231 m->hold_count ||
1232 m->wire_count) {
1233#ifdef INVARIANTS
086c1d7e 1234 kprintf("Warning: busy page %p found in cache\n", m);
984263bc
MD
1235#endif
1236 vm_page_deactivate(m);
1237 continue;
1238 }
17cde63e
MD
1239 KKASSERT((m->flags & PG_MAPPED) == 0);
1240 KKASSERT(m->dirty == 0);
984263bc
MD
1241 cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK;
1242 vm_pageout_page_free(m);
12e4aaff 1243 mycpu->gd_cnt.v_dfree++;
984263bc 1244 }
06ecca5a 1245
5fd012e0 1246 crit_exit();
984263bc
MD
1247
1248#if !defined(NO_SWAPPING)
1249 /*
1250 * Idle process swapout -- run once per second.
1251 */
1252 if (vm_swap_idle_enabled) {
1253 static long lsec;
1254 if (time_second != lsec) {
1255 vm_pageout_req_swapout |= VM_SWAP_IDLE;
1256 vm_req_vmdaemon();
1257 lsec = time_second;
1258 }
1259 }
1260#endif
1261
1262 /*
1263 * If we didn't get enough free pages, and we have skipped a vnode
1264 * in a writeable object, wakeup the sync daemon. And kick swapout
1265 * if we did not get enough free pages.
1266 */
1267 if (vm_paging_target() > 0) {
20479584 1268 if (vnodes_skipped && vm_page_count_min(0))
418ff780 1269 speedup_syncer();
984263bc
MD
1270#if !defined(NO_SWAPPING)
1271 if (vm_swap_enabled && vm_page_count_target()) {
1272 vm_req_vmdaemon();
1273 vm_pageout_req_swapout |= VM_SWAP_NORMAL;
1274 }
1275#endif
1276 }
1277
1278 /*
20479584
MD
1279 * Handle catastrophic conditions. Under good conditions we should
1280 * be at the target, well beyond our minimum. If we could not even
1281 * reach our minimum the system is under heavy stress.
1282 *
1283 * Determine whether we have run out of memory. This occurs when
1284 * swap_pager_full is TRUE and the only pages left in the page
1285 * queues are dirty. We will still likely have page shortages.
c84c24da
MD
1286 *
1287 * - swap_pager_full is set if insufficient swap was
1288 * available to satisfy a requested pageout.
1289 *
20479584
MD
1290 * - the inactive queue is bloated (4 x size of active queue),
1291 * meaning it is unable to get rid of dirty pages and.
c84c24da 1292 *
20479584
MD
1293 * - vm_page_count_min() without counting pages recycled from the
1294 * active queue (recycle_count) means we could not recover
1295 * enough pages to meet bare minimum needs. This test only
1296 * works if the inactive queue is bloated.
c84c24da 1297 *
20479584
MD
1298 * - due to a positive inactive_shortage we shifted the remaining
1299 * dirty pages from the active queue to the inactive queue
1300 * trying to find clean ones to free.
984263bc 1301 */
20479584 1302 if (swap_pager_full && vm_page_count_min(recycle_count))
c84c24da 1303 kprintf("Warning: system low on memory+swap!\n");
20479584
MD
1304 if (swap_pager_full && vm_page_count_min(recycle_count) &&
1305 vmstats.v_inactive_count > vmstats.v_active_count * 4 &&
1306 inactive_shortage > 0) {
1307 /*
1308 * Kill something.
1309 */
8fa76237
MD
1310 info.bigproc = NULL;
1311 info.bigsize = 0;
1312 allproc_scan(vm_pageout_scan_callback, &info);
1313 if (info.bigproc != NULL) {
1314 killproc(info.bigproc, "out of swap space");
1315 info.bigproc->p_nice = PRIO_MIN;
08f2f1bb
SS
1316 info.bigproc->p_usched->resetpriority(
1317 FIRST_LWP_IN_PROC(info.bigproc));
12e4aaff 1318 wakeup(&vmstats.v_free_count);
8fa76237 1319 PRELE(info.bigproc);
984263bc
MD
1320 }
1321 }
20479584 1322 return(inactive_shortage);
984263bc
MD
1323}
1324
99ad9bc4
MD
1325/*
1326 * The caller must hold vm_token and proc_token.
1327 */
8fa76237
MD
1328static int
1329vm_pageout_scan_callback(struct proc *p, void *data)
1330{
1331 struct vm_pageout_scan_info *info = data;
1332 vm_offset_t size;
1333
1334 /*
20479584
MD
1335 * Never kill system processes or init. If we have configured swap
1336 * then try to avoid killing low-numbered pids.
8fa76237
MD
1337 */
1338 if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
1339 ((p->p_pid < 48) && (vm_swap_size != 0))) {
1340 return (0);
1341 }
1342
1343 /*
1344 * if the process is in a non-running type state,
1345 * don't touch it.
1346 */
20479584 1347 if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
8fa76237 1348 return (0);
8fa76237
MD
1349
1350 /*
20479584
MD
1351 * Get the approximate process size. Note that anonymous pages
1352 * with backing swap will be counted twice, but there should not
1353 * be too many such pages due to the stress the VM system is
1354 * under at this point.
8fa76237 1355 */
20479584 1356 size = vmspace_anonymous_count(p->p_vmspace) +
8fa76237
MD
1357 vmspace_swap_count(p->p_vmspace);
1358
1359 /*
1360 * If the this process is bigger than the biggest one
1361 * remember it.
1362 */
20479584 1363 if (info->bigsize < size) {
8fa76237
MD
1364 if (info->bigproc)
1365 PRELE(info->bigproc);
1366 PHOLD(p);
1367 info->bigproc = p;
1368 info->bigsize = size;
1369 }
1370 return(0);
1371}
1372
984263bc
MD
1373/*
1374 * This routine tries to maintain the pseudo LRU active queue,
1375 * so that during long periods of time where there is no paging,
1376 * that some statistic accumulation still occurs. This code
1377 * helps the situation where paging just starts to occur.
99ad9bc4
MD
1378 *
1379 * The caller must hold vm_token.
984263bc
MD
1380 */
1381static void
57e43348 1382vm_pageout_page_stats(void)
984263bc 1383{
984263bc
MD
1384 vm_page_t m,next;
1385 int pcount,tpcount; /* Number of pages to check */
1386 static int fullintervalcount = 0;
1387 int page_shortage;
984263bc
MD
1388
1389 page_shortage =
12e4aaff
MD
1390 (vmstats.v_inactive_target + vmstats.v_cache_max + vmstats.v_free_min) -
1391 (vmstats.v_free_count + vmstats.v_inactive_count + vmstats.v_cache_count);
984263bc
MD
1392
1393 if (page_shortage <= 0)
1394 return;
1395
5fd012e0 1396 crit_enter();
984263bc 1397
12e4aaff 1398 pcount = vmstats.v_active_count;
984263bc
MD
1399 fullintervalcount += vm_pageout_stats_interval;
1400 if (fullintervalcount < vm_pageout_full_stats_interval) {
12e4aaff 1401 tpcount = (vm_pageout_stats_max * vmstats.v_active_count) / vmstats.v_page_count;
984263bc
MD
1402 if (pcount > tpcount)
1403 pcount = tpcount;
1404 } else {
1405 fullintervalcount = 0;
1406 }
1407
1408 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1409 while ((m != NULL) && (pcount-- > 0)) {
1410 int actcount;
1411
1412 if (m->queue != PQ_ACTIVE) {
1413 break;
1414 }
1415
1416 next = TAILQ_NEXT(m, pageq);
1417 /*
1418 * Don't deactivate pages that are busy.
1419 */
1420 if ((m->busy != 0) ||
1421 (m->flags & PG_BUSY) ||
1422 (m->hold_count != 0)) {
984263bc
MD
1423 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1424 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
984263bc
MD
1425 m = next;
1426 continue;
1427 }
1428
1429 actcount = 0;
1430 if (m->flags & PG_REFERENCED) {
1431 vm_page_flag_clear(m, PG_REFERENCED);
1432 actcount += 1;
1433 }
1434
1435 actcount += pmap_ts_referenced(m);
1436 if (actcount) {
1437 m->act_count += ACT_ADVANCE + actcount;
1438 if (m->act_count > ACT_MAX)
1439 m->act_count = ACT_MAX;
984263bc
MD
1440 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1441 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
984263bc
MD
1442 } else {
1443 if (m->act_count == 0) {
1444 /*
1445 * We turn off page access, so that we have
1446 * more accurate RSS stats. We don't do this
1447 * in the normal page deactivation when the
1448 * system is loaded VM wise, because the
1449 * cost of the large number of page protect
1450 * operations would be higher than the value
1451 * of doing the operation.
1452 */
17cde63e 1453 vm_page_busy(m);
984263bc
MD
1454 vm_page_protect(m, VM_PROT_NONE);
1455 vm_page_deactivate(m);
a491077e 1456 vm_page_wakeup(m);
984263bc
MD
1457 } else {
1458 m->act_count -= min(m->act_count, ACT_DECLINE);
984263bc
MD
1459 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1460 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
984263bc
MD
1461 }
1462 }
1463
1464 m = next;
1465 }
5fd012e0 1466 crit_exit();
984263bc
MD
1467}
1468
99ad9bc4
MD
1469/*
1470 * The caller must hold vm_token.
1471 */
984263bc 1472static int
57e43348 1473vm_pageout_free_page_calc(vm_size_t count)
984263bc 1474{
12e4aaff 1475 if (count < vmstats.v_page_count)
984263bc
MD
1476 return 0;
1477 /*
1478 * free_reserved needs to include enough for the largest swap pager
1479 * structures plus enough for any pv_entry structs when paging.
1480 */
12e4aaff
MD
1481 if (vmstats.v_page_count > 1024)
1482 vmstats.v_free_min = 4 + (vmstats.v_page_count - 1024) / 200;
984263bc 1483 else
12e4aaff
MD
1484 vmstats.v_free_min = 4;
1485 vmstats.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
1486 vmstats.v_interrupt_free_min;
1487 vmstats.v_free_reserved = vm_pageout_page_count +
1488 vmstats.v_pageout_free_min + (count / 768) + PQ_L2_SIZE;
1489 vmstats.v_free_severe = vmstats.v_free_min / 2;
1490 vmstats.v_free_min += vmstats.v_free_reserved;
1491 vmstats.v_free_severe += vmstats.v_free_reserved;
984263bc
MD
1492 return 1;
1493}
1494
1495
1496/*
20479584 1497 * vm_pageout is the high level pageout daemon.
99ad9bc4
MD
1498 *
1499 * No requirements.
984263bc
MD
1500 */
1501static void
cd8ab232 1502vm_pageout_thread(void)
984263bc
MD
1503{
1504 int pass;
20479584 1505 int inactive_shortage;
984263bc 1506
99ad9bc4
MD
1507 /*
1508 * Permanently hold vm_token.
1509 */
1510 lwkt_gettoken(&vm_token);
1511
984263bc
MD
1512 /*
1513 * Initialize some paging parameters.
1514 */
4ecf7cc9 1515 curthread->td_flags |= TDF_SYSTHREAD;
984263bc 1516
12e4aaff
MD
1517 vmstats.v_interrupt_free_min = 2;
1518 if (vmstats.v_page_count < 2000)
984263bc
MD
1519 vm_pageout_page_count = 8;
1520
12e4aaff 1521 vm_pageout_free_page_calc(vmstats.v_page_count);
20479584 1522
984263bc
MD
1523 /*
1524 * v_free_target and v_cache_min control pageout hysteresis. Note
1525 * that these are more a measure of the VM cache queue hysteresis
1526 * then the VM free queue. Specifically, v_free_target is the
1527 * high water mark (free+cache pages).
1528 *
1529 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1530 * low water mark, while v_free_min is the stop. v_cache_min must
1531 * be big enough to handle memory needs while the pageout daemon
1532 * is signalled and run to free more pages.
1533 */
12e4aaff
MD
1534 if (vmstats.v_free_count > 6144)
1535 vmstats.v_free_target = 4 * vmstats.v_free_min + vmstats.v_free_reserved;
984263bc 1536 else
12e4aaff 1537 vmstats.v_free_target = 2 * vmstats.v_free_min + vmstats.v_free_reserved;
984263bc 1538
0e8bd897
MD
1539 /*
1540 * NOTE: With the new buffer cache b_act_count we want the default
1541 * inactive target to be a percentage of available memory.
1542 *
1543 * The inactive target essentially determines the minimum
1544 * number of 'temporary' pages capable of caching one-time-use
1545 * files when the VM system is otherwise full of pages
1546 * belonging to multi-time-use files or active program data.
51db7ca2
MD
1547 *
1548 * NOTE: The inactive target is aggressively persued only if the
1549 * inactive queue becomes too small. If the inactive queue
1550 * is large enough to satisfy page movement to free+cache
1551 * then it is repopulated more slowly from the active queue.
e15708fc 1552 * This allows a general inactive_target default to be set.
51db7ca2
MD
1553 *
1554 * There is an issue here for processes which sit mostly idle
1555 * 'overnight', such as sshd, tcsh, and X. Any movement from
1556 * the active queue will eventually cause such pages to
1557 * recycle eventually causing a lot of paging in the morning.
1558 * To reduce the incidence of this pages cycled out of the
1559 * buffer cache are moved directly to the inactive queue if
e15708fc
MD
1560 * they were only used once or twice.
1561 *
1562 * The vfs.vm_cycle_point sysctl can be used to adjust this.
1563 * Increasing the value (up to 64) increases the number of
1564 * buffer recyclements which go directly to the inactive queue.
0e8bd897 1565 */
12e4aaff
MD
1566 if (vmstats.v_free_count > 2048) {
1567 vmstats.v_cache_min = vmstats.v_free_target;
1568 vmstats.v_cache_max = 2 * vmstats.v_cache_min;
984263bc 1569 } else {
12e4aaff
MD
1570 vmstats.v_cache_min = 0;
1571 vmstats.v_cache_max = 0;
984263bc 1572 }
e15708fc 1573 vmstats.v_inactive_target = vmstats.v_free_count / 4;
984263bc
MD
1574
1575 /* XXX does not really belong here */
1576 if (vm_page_max_wired == 0)
12e4aaff 1577 vm_page_max_wired = vmstats.v_free_count / 3;
984263bc
MD
1578
1579 if (vm_pageout_stats_max == 0)
12e4aaff 1580 vm_pageout_stats_max = vmstats.v_free_target;
984263bc
MD
1581
1582 /*
1583 * Set interval in seconds for stats scan.
1584 */
1585 if (vm_pageout_stats_interval == 0)
1586 vm_pageout_stats_interval = 5;
1587 if (vm_pageout_full_stats_interval == 0)
1588 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1589
1590
1591 /*
1592 * Set maximum free per pass
1593 */
1594 if (vm_pageout_stats_free_max == 0)
1595 vm_pageout_stats_free_max = 5;
1596
1597 swap_pager_swap_init();
1598 pass = 0;
20479584 1599
984263bc
MD
1600 /*
1601 * The pageout daemon is never done, so loop forever.
1602 */
1603 while (TRUE) {
1604 int error;
984263bc 1605
12d8aca7 1606 /*
1bfac262
MD
1607 * Wait for an action request. If we timeout check to
1608 * see if paging is needed (in case the normal wakeup
1609 * code raced us).
12d8aca7 1610 */
20479584 1611 if (vm_pages_needed == 0) {
984263bc 1612 error = tsleep(&vm_pages_needed,
20479584
MD
1613 0, "psleep",
1614 vm_pageout_stats_interval * hz);
1bfac262
MD
1615 if (error &&
1616 vm_paging_needed() == 0 &&
1617 vm_pages_needed == 0) {
984263bc
MD
1618 vm_pageout_page_stats();
1619 continue;
1620 }
20479584 1621 vm_pages_needed = 1;
984263bc
MD
1622 }
1623
20479584 1624 mycpu->gd_cnt.v_pdwakeups++;
20479584
MD
1625
1626 /*
12d8aca7
MD
1627 * Scan for pageout. Try to avoid thrashing the system
1628 * with activity.
20479584 1629 */
12d8aca7 1630 inactive_shortage = vm_pageout_scan(pass);
20479584
MD
1631 if (inactive_shortage > 0) {
1632 ++pass;
1633 if (swap_pager_full) {
1634 /*
1635 * Running out of memory, catastrophic back-off
1636 * to one-second intervals.
1637 */
1638 tsleep(&vm_pages_needed, 0, "pdelay", hz);
1639 } else if (pass < 10 && vm_pages_needed > 1) {
1640 /*
1641 * Normal operation, additional processes
1642 * have already kicked us. Retry immediately.
1643 */
1644 } else if (pass < 10) {
1645 /*
1646 * Normal operation, fewer processes. Delay
1647 * a bit but allow wakeups.
1648 */
1649 vm_pages_needed = 0;
1650 tsleep(&vm_pages_needed, 0, "pdelay", hz / 10);
1651 vm_pages_needed = 1;
1652 } else {
1653 /*
1654 * We've taken too many passes, forced delay.
1655 */
1656 tsleep(&vm_pages_needed, 0, "pdelay", hz / 10);
1657 }
1658 } else {
12d8aca7
MD
1659 /*
1660 * Interlocked wakeup of waiters (non-optional)
1661 */
20479584 1662 pass = 0;
12d8aca7
MD
1663 if (vm_pages_needed && !vm_page_count_min(0)) {
1664 wakeup(&vmstats.v_free_count);
1665 vm_pages_needed = 0;
1666 }
20479584 1667 }
984263bc
MD
1668 }
1669}
1670
cd8ab232
MD
1671static struct kproc_desc page_kp = {
1672 "pagedaemon",
1673 vm_pageout_thread,
1674 &pagethread
1675};
1676SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
1677
1678
20479584
MD
1679/*
1680 * Called after allocating a page out of the cache or free queue
1681 * to possibly wake the pagedaemon up to replentish our supply.
1682 *
1683 * We try to generate some hysteresis by waking the pagedaemon up
1bfac262
MD
1684 * when our free+cache pages go below the free_min+cache_min level.
1685 * The pagedaemon tries to get the count back up to at least the
1686 * minimum, and through to the target level if possible.
20479584
MD
1687 *
1688 * If the pagedaemon is already active bump vm_pages_needed as a hint
1689 * that there are even more requests pending.
99ad9bc4
MD
1690 *
1691 * SMP races ok?
1692 * No requirements.
20479584 1693 */
984263bc 1694void
57e43348 1695pagedaemon_wakeup(void)
984263bc 1696{
1bfac262 1697 if (vm_paging_needed() && curthread != pagethread) {
20479584 1698 if (vm_pages_needed == 0) {
1bfac262 1699 vm_pages_needed = 1; /* SMP race ok */
20479584
MD
1700 wakeup(&vm_pages_needed);
1701 } else if (vm_page_count_min(0)) {
1bfac262 1702 ++vm_pages_needed; /* SMP race ok */
20479584 1703 }
984263bc
MD
1704 }
1705}
1706
1707#if !defined(NO_SWAPPING)
99ad9bc4
MD
1708
1709/*
1710 * SMP races ok?
1711 * No requirements.
1712 */
984263bc 1713static void
57e43348 1714vm_req_vmdaemon(void)
984263bc
MD
1715{
1716 static int lastrun = 0;
1717
1718 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
1719 wakeup(&vm_daemon_needed);
1720 lastrun = ticks;
1721 }
1722}
1723
8fa76237
MD
1724static int vm_daemon_callback(struct proc *p, void *data __unused);
1725
99ad9bc4
MD
1726/*
1727 * No requirements.
1728 */
984263bc 1729static void
57e43348 1730vm_daemon(void)
984263bc 1731{
99ad9bc4
MD
1732 /*
1733 * Permanently hold vm_token.
1734 */
1735 lwkt_gettoken(&vm_token);
1736
984263bc 1737 while (TRUE) {
377d4740 1738 tsleep(&vm_daemon_needed, 0, "psleep", 0);
984263bc
MD
1739 if (vm_pageout_req_swapout) {
1740 swapout_procs(vm_pageout_req_swapout);
1741 vm_pageout_req_swapout = 0;
1742 }
1743 /*
1744 * scan the processes for exceeding their rlimits or if
1745 * process is swapped out -- deactivate pages
1746 */
8fa76237
MD
1747 allproc_scan(vm_daemon_callback, NULL);
1748 }
1749}
984263bc 1750
99ad9bc4
MD
1751/*
1752 * Caller must hold vm_token and proc_token.
1753 */
8fa76237
MD
1754static int
1755vm_daemon_callback(struct proc *p, void *data __unused)
1756{
1757 vm_pindex_t limit, size;
984263bc 1758
8fa76237
MD
1759 /*
1760 * if this is a system process or if we have already
1761 * looked at this process, skip it.
1762 */
1763 if (p->p_flag & (P_SYSTEM | P_WEXIT))
1764 return (0);
984263bc 1765
8fa76237
MD
1766 /*
1767 * if the process is in a non-running type state,
1768 * don't touch it.
1769 */
164b8401 1770 if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
8fa76237 1771 return (0);
984263bc 1772
8fa76237
MD
1773 /*
1774 * get a limit
1775 */
1776 limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
1777 p->p_rlimit[RLIMIT_RSS].rlim_max));
1778
1779 /*
1780 * let processes that are swapped out really be
1781 * swapped out. Set the limit to nothing to get as
1782 * many pages out to swap as possible.
1783 */
1784 if (p->p_flag & P_SWAPPEDOUT)
1785 limit = 0;
1786
1787 size = vmspace_resident_count(p->p_vmspace);
1788 if (limit >= 0 && size >= limit) {
1789 vm_pageout_map_deactivate_pages(
1790 &p->p_vmspace->vm_map, limit);
984263bc 1791 }
8fa76237 1792 return (0);
984263bc 1793}
8fa76237 1794
984263bc 1795#endif