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