kernel - Clean up spinlock code, add more lwkt_yield()s
[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 847
d2d8515b
MD
848 lwkt_yield();
849
984263bc 850 /*
b12defdc
MD
851 * The page has been successfully busied and is now no
852 * longer spinlocked. The queue is no longer spinlocked
853 * either.
984263bc 854 */
06ecca5a 855
984263bc 856 /*
b12defdc 857 * A held page may be undergoing I/O, so skip it.
984263bc 858 */
b12defdc
MD
859 if (m->hold_count) {
860 vm_page_and_queue_spin_lock(m);
861 if (m->queue == PQ_INACTIVE) {
862 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl,
863 m, pageq);
864 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl,
865 m, pageq);
866 }
867 vm_page_and_queue_spin_unlock(m);
868 ++vm_swapcache_inactive_heuristic;
869 vm_page_wakeup(m);
984263bc
MD
870 continue;
871 }
872
984263bc 873 if (m->object->ref_count == 0) {
06ecca5a
MD
874 /*
875 * If the object is not being used, we ignore previous
876 * references.
877 */
984263bc
MD
878 vm_page_flag_clear(m, PG_REFERENCED);
879 pmap_clear_reference(m);
b12defdc 880 /* fall through to end */
984263bc 881 } else if (((m->flags & PG_REFERENCED) == 0) &&
06ecca5a
MD
882 (actcount = pmap_ts_referenced(m))) {
883 /*
884 * Otherwise, if the page has been referenced while
885 * in the inactive queue, we bump the "activation
886 * count" upwards, making it less likely that the
887 * page will be added back to the inactive queue
888 * prematurely again. Here we check the page tables
889 * (or emulated bits, if any), given the upper level
890 * VM system not knowing anything about existing
891 * references.
892 */
984263bc
MD
893 vm_page_activate(m);
894 m->act_count += (actcount + ACT_ADVANCE);
b12defdc 895 vm_page_wakeup(m);
984263bc
MD
896 continue;
897 }
898
899 /*
b12defdc
MD
900 * (m) is still busied.
901 *
984263bc
MD
902 * If the upper level VM system knows about any page
903 * references, we activate the page. We also set the
904 * "activation count" higher than normal so that we will less
905 * likely place pages back onto the inactive queue again.
906 */
907 if ((m->flags & PG_REFERENCED) != 0) {
908 vm_page_flag_clear(m, PG_REFERENCED);
909 actcount = pmap_ts_referenced(m);
910 vm_page_activate(m);
911 m->act_count += (actcount + ACT_ADVANCE + 1);
b12defdc 912 vm_page_wakeup(m);
984263bc
MD
913 continue;
914 }
915
916 /*
917 * If the upper level VM system doesn't know anything about
918 * the page being dirty, we have to check for it again. As
919 * far as the VM code knows, any partially dirty pages are
920 * fully dirty.
41a01a4d
MD
921 *
922 * Pages marked PG_WRITEABLE may be mapped into the user
923 * address space of a process running on another cpu. A
924 * user process (without holding the MP lock) running on
925 * another cpu may be able to touch the page while we are
17cde63e
MD
926 * trying to remove it. vm_page_cache() will handle this
927 * case for us.
984263bc
MD
928 */
929 if (m->dirty == 0) {
930 vm_page_test_dirty(m);
931 } else {
932 vm_page_dirty(m);
933 }
934
984263bc 935 if (m->valid == 0) {
41a01a4d
MD
936 /*
937 * Invalid pages can be easily freed
938 */
984263bc 939 vm_pageout_page_free(m);
12e4aaff 940 mycpu->gd_cnt.v_dfree++;
20479584 941 --inactive_shortage;
984263bc
MD
942 } else if (m->dirty == 0) {
943 /*
41a01a4d
MD
944 * Clean pages can be placed onto the cache queue.
945 * This effectively frees them.
984263bc
MD
946 */
947 vm_page_cache(m);
20479584 948 --inactive_shortage;
984263bc
MD
949 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
950 /*
951 * Dirty pages need to be paged out, but flushing
952 * a page is extremely expensive verses freeing
953 * a clean page. Rather then artificially limiting
954 * the number of pages we can flush, we instead give
955 * dirty pages extra priority on the inactive queue
956 * by forcing them to be cycled through the queue
957 * twice before being flushed, after which the
958 * (now clean) page will cycle through once more
959 * before being freed. This significantly extends
960 * the thrash point for a heavily loaded machine.
961 */
984263bc 962 vm_page_flag_set(m, PG_WINATCFLS);
b12defdc
MD
963 vm_page_and_queue_spin_lock(m);
964 if (m->queue == PQ_INACTIVE) {
965 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
966 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
967 }
968 vm_page_and_queue_spin_unlock(m);
e527fb6b 969 ++vm_swapcache_inactive_heuristic;
b12defdc 970 vm_page_wakeup(m);
984263bc
MD
971 } else if (maxlaunder > 0) {
972 /*
973 * We always want to try to flush some dirty pages if
974 * we encounter them, to keep the system stable.
975 * Normally this number is small, but under extreme
976 * pressure where there are insufficient clean pages
977 * on the inactive queue, we may have to go all out.
978 */
979 int swap_pageouts_ok;
980 struct vnode *vp = NULL;
981
982 object = m->object;
983
984 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
985 swap_pageouts_ok = 1;
986 } else {
987 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
988 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
20479584 989 vm_page_count_min(0));
984263bc
MD
990
991 }
992
993 /*
994 * We don't bother paging objects that are "dead".
995 * Those objects are in a "rundown" state.
996 */
997 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
b12defdc
MD
998 vm_page_and_queue_spin_lock(m);
999 if (m->queue == PQ_INACTIVE) {
1000 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1001 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1002 }
1003 vm_page_and_queue_spin_unlock(m);
e527fb6b 1004 ++vm_swapcache_inactive_heuristic;
b12defdc 1005 vm_page_wakeup(m);
984263bc
MD
1006 continue;
1007 }
1008
1009 /*
b12defdc
MD
1010 * (m) is still busied.
1011 *
984263bc
MD
1012 * The object is already known NOT to be dead. It
1013 * is possible for the vget() to block the whole
1014 * pageout daemon, but the new low-memory handling
1015 * code should prevent it.
1016 *
1017 * The previous code skipped locked vnodes and, worse,
1018 * reordered pages in the queue. This results in
1019 * completely non-deterministic operation because,
1020 * quite often, a vm_fault has initiated an I/O and
1021 * is holding a locked vnode at just the point where
1022 * the pageout daemon is woken up.
1023 *
1024 * We can't wait forever for the vnode lock, we might
1025 * deadlock due to a vn_read() getting stuck in
1026 * vm_wait while holding this vnode. We skip the
1027 * vnode if we can't get it in a reasonable amount
1028 * of time.
5d6a945b
MD
1029 *
1030 * vpfailed is used to (try to) avoid the case where
1031 * a large number of pages are associated with a
1032 * locked vnode, which could cause the pageout daemon
1033 * to stall for an excessive amount of time.
984263bc 1034 */
984263bc 1035 if (object->type == OBJT_VNODE) {
5d6a945b 1036 int flags;
984263bc 1037
5d6a945b
MD
1038 vp = object->handle;
1039 flags = LK_EXCLUSIVE | LK_NOOBJ;
1040 if (vp == vpfailed)
1041 flags |= LK_NOWAIT;
1042 else
1043 flags |= LK_TIMELOCK;
b12defdc
MD
1044 vm_page_hold(m);
1045 vm_page_wakeup(m);
1046
1047 /*
1048 * We have unbusied (m) temporarily so we can
1049 * acquire the vp lock without deadlocking.
1050 * (m) is held to prevent destruction.
1051 */
5d6a945b
MD
1052 if (vget(vp, flags) != 0) {
1053 vpfailed = vp;
984263bc
MD
1054 ++pageout_lock_miss;
1055 if (object->flags & OBJ_MIGHTBEDIRTY)
1056 vnodes_skipped++;
b12defdc 1057 vm_page_unhold(m);
984263bc
MD
1058 continue;
1059 }
1060
1061 /*
1062 * The page might have been moved to another
1063 * queue during potential blocking in vget()
1064 * above. The page might have been freed and
1065 * reused for another vnode. The object might
1066 * have been reused for another vnode.
1067 */
1068 if (m->queue != PQ_INACTIVE ||
1069 m->object != object ||
1070 object->handle != vp) {
1071 if (object->flags & OBJ_MIGHTBEDIRTY)
1072 vnodes_skipped++;
1073 vput(vp);
b12defdc 1074 vm_page_unhold(m);
984263bc
MD
1075 continue;
1076 }
1077
1078 /*
1079 * The page may have been busied during the
1080 * blocking in vput(); We don't move the
1081 * page back onto the end of the queue so that
1082 * statistics are more correct if we don't.
1083 */
b12defdc 1084 if (vm_page_busy_try(m, TRUE)) {
984263bc 1085 vput(vp);
b12defdc 1086 vm_page_unhold(m);
984263bc
MD
1087 continue;
1088 }
b12defdc 1089 vm_page_unhold(m);
984263bc
MD
1090
1091 /*
b12defdc
MD
1092 * (m) is busied again
1093 *
1094 * We own the busy bit and remove our hold
1095 * bit. If the page is still held it
1096 * might be undergoing I/O, so skip it.
984263bc
MD
1097 */
1098 if (m->hold_count) {
b12defdc
MD
1099 vm_page_and_queue_spin_lock(m);
1100 if (m->queue == PQ_INACTIVE) {
1101 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1102 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1103 }
1104 vm_page_and_queue_spin_unlock(m);
e527fb6b 1105 ++vm_swapcache_inactive_heuristic;
984263bc
MD
1106 if (object->flags & OBJ_MIGHTBEDIRTY)
1107 vnodes_skipped++;
b12defdc 1108 vm_page_wakeup(m);
984263bc
MD
1109 vput(vp);
1110 continue;
1111 }
b12defdc 1112 /* (m) is left busied as we fall through */
984263bc
MD
1113 }
1114
1115 /*
b12defdc
MD
1116 * page is busy and not held here.
1117 *
984263bc
MD
1118 * If a page is dirty, then it is either being washed
1119 * (but not yet cleaned) or it is still in the
1120 * laundry. If it is still in the laundry, then we
1121 * start the cleaning operation.
1122 *
20479584
MD
1123 * decrement inactive_shortage on success to account
1124 * for the (future) cleaned page. Otherwise we
1125 * could wind up laundering or cleaning too many
1126 * pages.
984263bc 1127 */
984263bc 1128 if (vm_pageout_clean(m) != 0) {
20479584 1129 --inactive_shortage;
984263bc 1130 --maxlaunder;
c84c24da 1131 }
b12defdc 1132 /* clean ate busy, page no longer accessible */
984263bc
MD
1133 if (vp != NULL)
1134 vput(vp);
b12defdc
MD
1135 } else {
1136 vm_page_wakeup(m);
984263bc
MD
1137 }
1138 }
b12defdc
MD
1139 vm_page_queues_spin_lock(PQ_INACTIVE);
1140 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq);
1141 vm_page_queues_spin_unlock(PQ_INACTIVE);
984263bc
MD
1142
1143 /*
20479584
MD
1144 * We want to move pages from the active queue to the inactive
1145 * queue to get the inactive queue to the inactive target. If
1146 * we still have a page shortage from above we try to directly free
1147 * clean pages instead of moving them.
06ecca5a 1148 *
20479584
MD
1149 * If we do still have a shortage we keep track of the number of
1150 * pages we free or cache (recycle_count) as a measure of thrashing
1151 * between the active and inactive queues.
1152 *
51db7ca2
MD
1153 * If we were able to completely satisfy the free+cache targets
1154 * from the inactive pool we limit the number of pages we move
1155 * from the active pool to the inactive pool to 2x the pages we
e6e9a0c3
MD
1156 * had removed from the inactive pool (with a minimum of 1/5 the
1157 * inactive target). If we were not able to completely satisfy
1158 * the free+cache targets we go for the whole target aggressively.
20479584
MD
1159 *
1160 * NOTE: Both variables can end up negative.
1161 * NOTE: We are still in a critical section.
984263bc 1162 */
20479584 1163 active_shortage = vmstats.v_inactive_target - vmstats.v_inactive_count;
e6e9a0c3
MD
1164 if (inactive_original_shortage < vmstats.v_inactive_target / 10)
1165 inactive_original_shortage = vmstats.v_inactive_target / 10;
51db7ca2
MD
1166 if (inactive_shortage <= 0 &&
1167 active_shortage > inactive_original_shortage * 2) {
1168 active_shortage = inactive_original_shortage * 2;
1169 }
20479584 1170
20479584 1171 recycle_count = 0;
b12defdc
MD
1172 marker.queue = PQ_ACTIVE;
1173
1174 vm_page_queues_spin_lock(PQ_ACTIVE);
1175 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_ACTIVE].pl, &marker, pageq);
1176 vm_page_queues_spin_unlock(PQ_ACTIVE);
1177 pcount = vmstats.v_active_count;
1178
1179 while ((m = TAILQ_NEXT(&marker, pageq)) != NULL &&
1180 pcount-- > 0 && (inactive_shortage > 0 || active_shortage > 0))
1181 {
1182 vm_page_and_queue_spin_lock(m);
1183 if (m != TAILQ_NEXT(&marker, pageq)) {
1184 vm_page_and_queue_spin_unlock(m);
1185 ++pcount;
1186 continue;
1187 }
1188 KKASSERT(m->queue == PQ_ACTIVE);
1189 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl,
1190 &marker, pageq);
1191 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_ACTIVE].pl, m,
1192 &marker, pageq);
984263bc 1193
984263bc 1194 /*
b12defdc 1195 * Skip marker pages
984263bc 1196 */
b12defdc
MD
1197 if (m->flags & PG_MARKER) {
1198 vm_page_and_queue_spin_unlock(m);
1199 continue;
1200 }
06ecca5a 1201
984263bc 1202 /*
b12defdc
MD
1203 * Try to busy the page. Don't mess with pages which are
1204 * already busy or reorder them in the queue.
984263bc 1205 */
b12defdc
MD
1206 if (vm_page_busy_try(m, TRUE)) {
1207 vm_page_and_queue_spin_unlock(m);
984263bc
MD
1208 continue;
1209 }
1210
b12defdc
MD
1211 /*
1212 * Don't deactivate pages that are held, even if we can
1213 * busy them. (XXX why not?)
1214 */
1215 if (m->hold_count != 0) {
1216 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl,
1217 m, pageq);
1218 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl,
1219 m, pageq);
1220 vm_page_and_queue_spin_unlock(m);
1221 vm_page_wakeup(m);
1222 continue;
1223 }
1224 vm_page_and_queue_spin_unlock(m);
d2d8515b 1225 lwkt_yield();
b12defdc
MD
1226
1227 /*
1228 * The page has been successfully busied and the page and
1229 * queue are no longer locked.
1230 */
1231
984263bc
MD
1232 /*
1233 * The count for pagedaemon pages is done after checking the
1234 * page for eligibility...
1235 */
12e4aaff 1236 mycpu->gd_cnt.v_pdpages++;
984263bc
MD
1237
1238 /*
20479584
MD
1239 * Check to see "how much" the page has been used and clear
1240 * the tracking access bits. If the object has no references
1241 * don't bother paying the expense.
984263bc
MD
1242 */
1243 actcount = 0;
1244 if (m->object->ref_count != 0) {
20479584
MD
1245 if (m->flags & PG_REFERENCED)
1246 ++actcount;
984263bc
MD
1247 actcount += pmap_ts_referenced(m);
1248 if (actcount) {
1249 m->act_count += ACT_ADVANCE + actcount;
1250 if (m->act_count > ACT_MAX)
1251 m->act_count = ACT_MAX;
1252 }
1253 }
984263bc
MD
1254 vm_page_flag_clear(m, PG_REFERENCED);
1255
1256 /*
20479584 1257 * actcount is only valid if the object ref_count is non-zero.
984263bc 1258 */
20479584 1259 if (actcount && m->object->ref_count != 0) {
b12defdc
MD
1260 vm_page_and_queue_spin_lock(m);
1261 if (m->queue == PQ_ACTIVE) {
1262 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl,
1263 m, pageq);
1264 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl,
1265 m, pageq);
1266 }
1267 vm_page_and_queue_spin_unlock(m);
1268 vm_page_wakeup(m);
984263bc
MD
1269 } else {
1270 m->act_count -= min(m->act_count, ACT_DECLINE);
1271 if (vm_pageout_algorithm ||
1272 m->object->ref_count == 0 ||
20479584
MD
1273 m->act_count < pass + 1
1274 ) {
1275 /*
1276 * Deactivate the page. If we had a
1277 * shortage from our inactive scan try to
1278 * free (cache) the page instead.
e6e9a0c3
MD
1279 *
1280 * Don't just blindly cache the page if
1281 * we do not have a shortage from the
1282 * inactive scan, that could lead to
1283 * gigabytes being moved.
20479584
MD
1284 */
1285 --active_shortage;
1286 if (inactive_shortage > 0 ||
1287 m->object->ref_count == 0) {
1288 if (inactive_shortage > 0)
1289 ++recycle_count;
984263bc 1290 vm_page_protect(m, VM_PROT_NONE);
e6e9a0c3
MD
1291 if (m->dirty == 0 &&
1292 inactive_shortage > 0) {
20479584 1293 --inactive_shortage;
984263bc 1294 vm_page_cache(m);
c84c24da 1295 } else {
984263bc 1296 vm_page_deactivate(m);
a491077e 1297 vm_page_wakeup(m);
c84c24da 1298 }
984263bc
MD
1299 } else {
1300 vm_page_deactivate(m);
b12defdc 1301 vm_page_wakeup(m);
984263bc
MD
1302 }
1303 } else {
b12defdc
MD
1304 vm_page_and_queue_spin_lock(m);
1305 if (m->queue == PQ_ACTIVE) {
1306 TAILQ_REMOVE(
1307 &vm_page_queues[PQ_ACTIVE].pl,
1308 m, pageq);
1309 TAILQ_INSERT_TAIL(
1310 &vm_page_queues[PQ_ACTIVE].pl,
1311 m, pageq);
1312 }
1313 vm_page_and_queue_spin_unlock(m);
1314 vm_page_wakeup(m);
984263bc
MD
1315 }
1316 }
984263bc
MD
1317 }
1318
b12defdc
MD
1319 /*
1320 * Clean out our local marker.
1321 */
1322 vm_page_queues_spin_lock(PQ_ACTIVE);
1323 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, &marker, pageq);
1324 vm_page_queues_spin_unlock(PQ_ACTIVE);
1325
984263bc 1326 /*
cd3c66bd
MD
1327 * The number of actually free pages can drop down to v_free_reserved,
1328 * we try to build the free count back above v_free_min. Note that
1329 * vm_paging_needed() also returns TRUE if v_free_count is not at
1330 * least v_free_min so that is the minimum we must build the free
1331 * count to.
1332 *
1333 * We use a slightly higher target to improve hysteresis,
1334 * ((v_free_target + v_free_min) / 2). Since v_free_target
1335 * is usually the same as v_cache_min this maintains about
1336 * half the pages in the free queue as are in the cache queue,
1337 * providing pretty good pipelining for pageout operation.
1338 *
1339 * The system operator can manipulate vm.v_cache_min and
1340 * vm.v_free_target to tune the pageout demon. Be sure
1341 * to keep vm.v_free_min < vm.v_free_target.
1342 *
1343 * Note that the original paging target is to get at least
1344 * (free_min + cache_min) into (free + cache). The slightly
1345 * higher target will shift additional pages from cache to free
1346 * without effecting the original paging target in order to
1347 * maintain better hysteresis and not have the free count always
1348 * be dead-on v_free_min.
06ecca5a 1349 *
5fd012e0 1350 * NOTE: we are still in a critical section.
c84c24da
MD
1351 *
1352 * Pages moved from PQ_CACHE to totally free are not counted in the
1353 * pages_freed counter.
984263bc 1354 */
cd3c66bd
MD
1355 while (vmstats.v_free_count <
1356 (vmstats.v_free_min + vmstats.v_free_target) / 2) {
1357 /*
b12defdc 1358 * This steals some code from vm/vm_page.c
cd3c66bd 1359 */
984263bc 1360 static int cache_rover = 0;
b12defdc
MD
1361
1362 m = vm_page_list_find(PQ_CACHE, cache_rover & PQ_L2_MASK, FALSE);
20479584 1363 if (m == NULL)
984263bc 1364 break;
b12defdc
MD
1365 /* page is returned removed from its queue and spinlocked */
1366 if (vm_page_busy_try(m, TRUE)) {
1367 vm_page_deactivate_locked(m);
1368 vm_page_spin_unlock(m);
984263bc 1369#ifdef INVARIANTS
086c1d7e 1370 kprintf("Warning: busy page %p found in cache\n", m);
984263bc 1371#endif
b12defdc
MD
1372 continue;
1373 }
1374 vm_page_spin_unlock(m);
1375 pagedaemon_wakeup();
d2d8515b 1376 lwkt_yield();
b12defdc
MD
1377
1378 /*
1379 * Page has been successfully busied and it and its queue
1380 * is no longer spinlocked.
1381 */
1382 if ((m->flags & PG_UNMANAGED) ||
1383 m->hold_count ||
1384 m->wire_count) {
984263bc 1385 vm_page_deactivate(m);
b12defdc 1386 vm_page_wakeup(m);
984263bc
MD
1387 continue;
1388 }
17cde63e
MD
1389 KKASSERT((m->flags & PG_MAPPED) == 0);
1390 KKASSERT(m->dirty == 0);
b12defdc 1391 cache_rover += PQ_PRIME2;
984263bc 1392 vm_pageout_page_free(m);
12e4aaff 1393 mycpu->gd_cnt.v_dfree++;
984263bc 1394 }
06ecca5a 1395
984263bc
MD
1396#if !defined(NO_SWAPPING)
1397 /*
1398 * Idle process swapout -- run once per second.
1399 */
1400 if (vm_swap_idle_enabled) {
1401 static long lsec;
1402 if (time_second != lsec) {
1403 vm_pageout_req_swapout |= VM_SWAP_IDLE;
1404 vm_req_vmdaemon();
1405 lsec = time_second;
1406 }
1407 }
1408#endif
1409
1410 /*
1411 * If we didn't get enough free pages, and we have skipped a vnode
1412 * in a writeable object, wakeup the sync daemon. And kick swapout
1413 * if we did not get enough free pages.
1414 */
1415 if (vm_paging_target() > 0) {
20479584 1416 if (vnodes_skipped && vm_page_count_min(0))
418ff780 1417 speedup_syncer();
984263bc
MD
1418#if !defined(NO_SWAPPING)
1419 if (vm_swap_enabled && vm_page_count_target()) {
1420 vm_req_vmdaemon();
1421 vm_pageout_req_swapout |= VM_SWAP_NORMAL;
1422 }
1423#endif
1424 }
1425
1426 /*
20479584
MD
1427 * Handle catastrophic conditions. Under good conditions we should
1428 * be at the target, well beyond our minimum. If we could not even
1429 * reach our minimum the system is under heavy stress.
1430 *
1431 * Determine whether we have run out of memory. This occurs when
1432 * swap_pager_full is TRUE and the only pages left in the page
1433 * queues are dirty. We will still likely have page shortages.
c84c24da
MD
1434 *
1435 * - swap_pager_full is set if insufficient swap was
1436 * available to satisfy a requested pageout.
1437 *
20479584
MD
1438 * - the inactive queue is bloated (4 x size of active queue),
1439 * meaning it is unable to get rid of dirty pages and.
c84c24da 1440 *
20479584
MD
1441 * - vm_page_count_min() without counting pages recycled from the
1442 * active queue (recycle_count) means we could not recover
1443 * enough pages to meet bare minimum needs. This test only
1444 * works if the inactive queue is bloated.
c84c24da 1445 *
20479584
MD
1446 * - due to a positive inactive_shortage we shifted the remaining
1447 * dirty pages from the active queue to the inactive queue
1448 * trying to find clean ones to free.
984263bc 1449 */
20479584 1450 if (swap_pager_full && vm_page_count_min(recycle_count))
c84c24da 1451 kprintf("Warning: system low on memory+swap!\n");
20479584
MD
1452 if (swap_pager_full && vm_page_count_min(recycle_count) &&
1453 vmstats.v_inactive_count > vmstats.v_active_count * 4 &&
1454 inactive_shortage > 0) {
1455 /*
1456 * Kill something.
1457 */
8fa76237
MD
1458 info.bigproc = NULL;
1459 info.bigsize = 0;
1460 allproc_scan(vm_pageout_scan_callback, &info);
1461 if (info.bigproc != NULL) {
1462 killproc(info.bigproc, "out of swap space");
1463 info.bigproc->p_nice = PRIO_MIN;
08f2f1bb
SS
1464 info.bigproc->p_usched->resetpriority(
1465 FIRST_LWP_IN_PROC(info.bigproc));
12e4aaff 1466 wakeup(&vmstats.v_free_count);
8fa76237 1467 PRELE(info.bigproc);
984263bc
MD
1468 }
1469 }
20479584 1470 return(inactive_shortage);
984263bc
MD
1471}
1472
99ad9bc4 1473/*
b12defdc 1474 * The caller must hold proc_token.
99ad9bc4 1475 */
8fa76237
MD
1476static int
1477vm_pageout_scan_callback(struct proc *p, void *data)
1478{
1479 struct vm_pageout_scan_info *info = data;
1480 vm_offset_t size;
1481
1482 /*
20479584
MD
1483 * Never kill system processes or init. If we have configured swap
1484 * then try to avoid killing low-numbered pids.
8fa76237
MD
1485 */
1486 if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
1487 ((p->p_pid < 48) && (vm_swap_size != 0))) {
1488 return (0);
1489 }
1490
1491 /*
1492 * if the process is in a non-running type state,
1493 * don't touch it.
1494 */
20479584 1495 if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
8fa76237 1496 return (0);
8fa76237
MD
1497
1498 /*
20479584
MD
1499 * Get the approximate process size. Note that anonymous pages
1500 * with backing swap will be counted twice, but there should not
1501 * be too many such pages due to the stress the VM system is
1502 * under at this point.
8fa76237 1503 */
20479584 1504 size = vmspace_anonymous_count(p->p_vmspace) +
8fa76237
MD
1505 vmspace_swap_count(p->p_vmspace);
1506
1507 /*
1508 * If the this process is bigger than the biggest one
1509 * remember it.
1510 */
20479584 1511 if (info->bigsize < size) {
8fa76237
MD
1512 if (info->bigproc)
1513 PRELE(info->bigproc);
1514 PHOLD(p);
1515 info->bigproc = p;
1516 info->bigsize = size;
1517 }
d2d8515b 1518 lwkt_yield();
8fa76237
MD
1519 return(0);
1520}
1521
984263bc
MD
1522/*
1523 * This routine tries to maintain the pseudo LRU active queue,
1524 * so that during long periods of time where there is no paging,
1525 * that some statistic accumulation still occurs. This code
1526 * helps the situation where paging just starts to occur.
1527 */
1528static void
57e43348 1529vm_pageout_page_stats(void)
984263bc 1530{
984263bc 1531 static int fullintervalcount = 0;
b12defdc
MD
1532 struct vm_page marker;
1533 vm_page_t m;
1534 int pcount, tpcount; /* Number of pages to check */
984263bc 1535 int page_shortage;
984263bc 1536
b12defdc
MD
1537 page_shortage = (vmstats.v_inactive_target + vmstats.v_cache_max +
1538 vmstats.v_free_min) -
1539 (vmstats.v_free_count + vmstats.v_inactive_count +
1540 vmstats.v_cache_count);
984263bc
MD
1541
1542 if (page_shortage <= 0)
1543 return;
1544
12e4aaff 1545 pcount = vmstats.v_active_count;
984263bc
MD
1546 fullintervalcount += vm_pageout_stats_interval;
1547 if (fullintervalcount < vm_pageout_full_stats_interval) {
b12defdc
MD
1548 tpcount = (vm_pageout_stats_max * vmstats.v_active_count) /
1549 vmstats.v_page_count;
984263bc
MD
1550 if (pcount > tpcount)
1551 pcount = tpcount;
1552 } else {
1553 fullintervalcount = 0;
1554 }
1555
b12defdc
MD
1556 bzero(&marker, sizeof(marker));
1557 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
1558 marker.queue = PQ_ACTIVE;
1559 marker.wire_count = 1;
1560
1561 vm_page_queues_spin_lock(PQ_ACTIVE);
1562 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_ACTIVE].pl, &marker, pageq);
1563 vm_page_queues_spin_unlock(PQ_ACTIVE);
1564
1565 while ((m = TAILQ_NEXT(&marker, pageq)) != NULL &&
1566 pcount-- > 0)
1567 {
984263bc
MD
1568 int actcount;
1569
b12defdc
MD
1570 vm_page_and_queue_spin_lock(m);
1571 if (m != TAILQ_NEXT(&marker, pageq)) {
1572 vm_page_and_queue_spin_unlock(m);
1573 ++pcount;
1574 continue;
984263bc 1575 }
b12defdc
MD
1576 KKASSERT(m->queue == PQ_ACTIVE);
1577 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, &marker, pageq);
1578 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_ACTIVE].pl, m,
1579 &marker, pageq);
984263bc 1580
984263bc 1581 /*
b12defdc 1582 * Ignore markers
984263bc 1583 */
b12defdc
MD
1584 if (m->flags & PG_MARKER) {
1585 vm_page_and_queue_spin_unlock(m);
984263bc
MD
1586 continue;
1587 }
1588
b12defdc
MD
1589 /*
1590 * Ignore pages we can't busy
1591 */
1592 if (vm_page_busy_try(m, TRUE)) {
1593 vm_page_and_queue_spin_unlock(m);
1594 continue;
1595 }
1596 vm_page_and_queue_spin_unlock(m);
1597 KKASSERT(m->queue == PQ_ACTIVE);
1598
1599 /*
1600 * We now have a safely busied page, the page and queue
1601 * spinlocks have been released.
1602 *
1603 * Ignore held pages
1604 */
1605 if (m->hold_count) {
1606 vm_page_wakeup(m);
1607 continue;
1608 }
1609
1610 /*
1611 * Calculate activity
1612 */
984263bc
MD
1613 actcount = 0;
1614 if (m->flags & PG_REFERENCED) {
1615 vm_page_flag_clear(m, PG_REFERENCED);
1616 actcount += 1;
1617 }
984263bc 1618 actcount += pmap_ts_referenced(m);
b12defdc
MD
1619
1620 /*
1621 * Update act_count and move page to end of queue.
1622 */
984263bc
MD
1623 if (actcount) {
1624 m->act_count += ACT_ADVANCE + actcount;
1625 if (m->act_count > ACT_MAX)
1626 m->act_count = ACT_MAX;
b12defdc
MD
1627 vm_page_and_queue_spin_lock(m);
1628 if (m->queue == PQ_ACTIVE) {
1629 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl,
1630 m, pageq);
1631 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl,
1632 m, pageq);
984263bc 1633 }
b12defdc
MD
1634 vm_page_and_queue_spin_unlock(m);
1635 vm_page_wakeup(m);
1636 continue;
984263bc
MD
1637 }
1638
b12defdc
MD
1639 if (m->act_count == 0) {
1640 /*
1641 * We turn off page access, so that we have
1642 * more accurate RSS stats. We don't do this
1643 * in the normal page deactivation when the
1644 * system is loaded VM wise, because the
1645 * cost of the large number of page protect
1646 * operations would be higher than the value
1647 * of doing the operation.
1648 *
1649 * We use the marker to save our place so
1650 * we can release the spin lock. both (m)
1651 * and (next) will be invalid.
1652 */
1653 vm_page_protect(m, VM_PROT_NONE);
1654 vm_page_deactivate(m);
1655 } else {
1656 m->act_count -= min(m->act_count, ACT_DECLINE);
1657 vm_page_and_queue_spin_lock(m);
1658 if (m->queue == PQ_ACTIVE) {
1659 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl,
1660 m, pageq);
1661 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl,
1662 m, pageq);
1663 }
1664 vm_page_and_queue_spin_unlock(m);
1665 }
1666 vm_page_wakeup(m);
984263bc 1667 }
b12defdc
MD
1668
1669 /*
1670 * Remove our local marker
1671 */
1672 vm_page_queues_spin_lock(PQ_ACTIVE);
1673 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, &marker, pageq);
1674 vm_page_queues_spin_unlock(PQ_ACTIVE);
1675
984263bc
MD
1676}
1677
1678static int
57e43348 1679vm_pageout_free_page_calc(vm_size_t count)
984263bc 1680{
12e4aaff 1681 if (count < vmstats.v_page_count)
984263bc
MD
1682 return 0;
1683 /*
1684 * free_reserved needs to include enough for the largest swap pager
1685 * structures plus enough for any pv_entry structs when paging.
0a4d4828
MD
1686 *
1687 * v_free_min normal allocations
1688 * v_free_reserved system allocations
1689 * v_pageout_free_min allocations by pageout daemon
1690 * v_interrupt_free_min low level allocations (e.g swap structures)
984263bc 1691 */
12e4aaff 1692 if (vmstats.v_page_count > 1024)
0a4d4828 1693 vmstats.v_free_min = 64 + (vmstats.v_page_count - 1024) / 200;
984263bc 1694 else
0a4d4828
MD
1695 vmstats.v_free_min = 64;
1696 vmstats.v_free_reserved = vmstats.v_free_min * 4 / 8 + 7;
1697 vmstats.v_free_severe = vmstats.v_free_min * 4 / 8 + 0;
1698 vmstats.v_pageout_free_min = vmstats.v_free_min * 2 / 8 + 7;
1699 vmstats.v_interrupt_free_min = vmstats.v_free_min * 1 / 8 + 7;
1700
984263bc
MD
1701 return 1;
1702}
1703
1704
1705/*
20479584 1706 * vm_pageout is the high level pageout daemon.
99ad9bc4
MD
1707 *
1708 * No requirements.
984263bc
MD
1709 */
1710static void
cd8ab232 1711vm_pageout_thread(void)
984263bc
MD
1712{
1713 int pass;
20479584 1714 int inactive_shortage;
984263bc
MD
1715
1716 /*
1717 * Initialize some paging parameters.
1718 */
4ecf7cc9 1719 curthread->td_flags |= TDF_SYSTHREAD;
984263bc 1720
12e4aaff 1721 if (vmstats.v_page_count < 2000)
984263bc
MD
1722 vm_pageout_page_count = 8;
1723
12e4aaff 1724 vm_pageout_free_page_calc(vmstats.v_page_count);
20479584 1725
984263bc
MD
1726 /*
1727 * v_free_target and v_cache_min control pageout hysteresis. Note
1728 * that these are more a measure of the VM cache queue hysteresis
1729 * then the VM free queue. Specifically, v_free_target is the
1730 * high water mark (free+cache pages).
1731 *
1732 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1733 * low water mark, while v_free_min is the stop. v_cache_min must
1734 * be big enough to handle memory needs while the pageout daemon
1735 * is signalled and run to free more pages.
1736 */
12e4aaff
MD
1737 if (vmstats.v_free_count > 6144)
1738 vmstats.v_free_target = 4 * vmstats.v_free_min + vmstats.v_free_reserved;
984263bc 1739 else
12e4aaff 1740 vmstats.v_free_target = 2 * vmstats.v_free_min + vmstats.v_free_reserved;
984263bc 1741
0e8bd897
MD
1742 /*
1743 * NOTE: With the new buffer cache b_act_count we want the default
1744 * inactive target to be a percentage of available memory.
1745 *
1746 * The inactive target essentially determines the minimum
1747 * number of 'temporary' pages capable of caching one-time-use
1748 * files when the VM system is otherwise full of pages
1749 * belonging to multi-time-use files or active program data.
51db7ca2
MD
1750 *
1751 * NOTE: The inactive target is aggressively persued only if the
1752 * inactive queue becomes too small. If the inactive queue
1753 * is large enough to satisfy page movement to free+cache
1754 * then it is repopulated more slowly from the active queue.
e15708fc 1755 * This allows a general inactive_target default to be set.
51db7ca2
MD
1756 *
1757 * There is an issue here for processes which sit mostly idle
1758 * 'overnight', such as sshd, tcsh, and X. Any movement from
1759 * the active queue will eventually cause such pages to
1760 * recycle eventually causing a lot of paging in the morning.
1761 * To reduce the incidence of this pages cycled out of the
1762 * buffer cache are moved directly to the inactive queue if
e15708fc
MD
1763 * they were only used once or twice.
1764 *
1765 * The vfs.vm_cycle_point sysctl can be used to adjust this.
1766 * Increasing the value (up to 64) increases the number of
1767 * buffer recyclements which go directly to the inactive queue.
0e8bd897 1768 */
12e4aaff
MD
1769 if (vmstats.v_free_count > 2048) {
1770 vmstats.v_cache_min = vmstats.v_free_target;
1771 vmstats.v_cache_max = 2 * vmstats.v_cache_min;
984263bc 1772 } else {
12e4aaff
MD
1773 vmstats.v_cache_min = 0;
1774 vmstats.v_cache_max = 0;
984263bc 1775 }
e15708fc 1776 vmstats.v_inactive_target = vmstats.v_free_count / 4;
984263bc
MD
1777
1778 /* XXX does not really belong here */
1779 if (vm_page_max_wired == 0)
12e4aaff 1780 vm_page_max_wired = vmstats.v_free_count / 3;
984263bc
MD
1781
1782 if (vm_pageout_stats_max == 0)
12e4aaff 1783 vm_pageout_stats_max = vmstats.v_free_target;
984263bc
MD
1784
1785 /*
1786 * Set interval in seconds for stats scan.
1787 */
1788 if (vm_pageout_stats_interval == 0)
1789 vm_pageout_stats_interval = 5;
1790 if (vm_pageout_full_stats_interval == 0)
1791 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1792
1793
1794 /*
1795 * Set maximum free per pass
1796 */
1797 if (vm_pageout_stats_free_max == 0)
1798 vm_pageout_stats_free_max = 5;
1799
1800 swap_pager_swap_init();
1801 pass = 0;
20479584 1802
984263bc
MD
1803 /*
1804 * The pageout daemon is never done, so loop forever.
1805 */
1806 while (TRUE) {
1807 int error;
984263bc 1808
12d8aca7 1809 /*
1bfac262
MD
1810 * Wait for an action request. If we timeout check to
1811 * see if paging is needed (in case the normal wakeup
1812 * code raced us).
12d8aca7 1813 */
20479584 1814 if (vm_pages_needed == 0) {
984263bc 1815 error = tsleep(&vm_pages_needed,
20479584
MD
1816 0, "psleep",
1817 vm_pageout_stats_interval * hz);
1bfac262
MD
1818 if (error &&
1819 vm_paging_needed() == 0 &&
1820 vm_pages_needed == 0) {
984263bc
MD
1821 vm_pageout_page_stats();
1822 continue;
1823 }
20479584 1824 vm_pages_needed = 1;
984263bc
MD
1825 }
1826
20479584 1827 mycpu->gd_cnt.v_pdwakeups++;
20479584
MD
1828
1829 /*
12d8aca7
MD
1830 * Scan for pageout. Try to avoid thrashing the system
1831 * with activity.
20479584 1832 */
12d8aca7 1833 inactive_shortage = vm_pageout_scan(pass);
20479584
MD
1834 if (inactive_shortage > 0) {
1835 ++pass;
1836 if (swap_pager_full) {
1837 /*
1838 * Running out of memory, catastrophic back-off
1839 * to one-second intervals.
1840 */
1841 tsleep(&vm_pages_needed, 0, "pdelay", hz);
1842 } else if (pass < 10 && vm_pages_needed > 1) {
1843 /*
1844 * Normal operation, additional processes
1845 * have already kicked us. Retry immediately.
1846 */
1847 } else if (pass < 10) {
1848 /*
1849 * Normal operation, fewer processes. Delay
1850 * a bit but allow wakeups.
1851 */
1852 vm_pages_needed = 0;
1853 tsleep(&vm_pages_needed, 0, "pdelay", hz / 10);
1854 vm_pages_needed = 1;
1855 } else {
1856 /*
1857 * We've taken too many passes, forced delay.
1858 */
1859 tsleep(&vm_pages_needed, 0, "pdelay", hz / 10);
1860 }
1861 } else {
12d8aca7
MD
1862 /*
1863 * Interlocked wakeup of waiters (non-optional)
1864 */
20479584 1865 pass = 0;
12d8aca7
MD
1866 if (vm_pages_needed && !vm_page_count_min(0)) {
1867 wakeup(&vmstats.v_free_count);
1868 vm_pages_needed = 0;
1869 }
20479584 1870 }
984263bc
MD
1871 }
1872}
1873
cd8ab232
MD
1874static struct kproc_desc page_kp = {
1875 "pagedaemon",
1876 vm_pageout_thread,
1877 &pagethread
1878};
1879SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
1880
1881
20479584
MD
1882/*
1883 * Called after allocating a page out of the cache or free queue
1884 * to possibly wake the pagedaemon up to replentish our supply.
1885 *
1886 * We try to generate some hysteresis by waking the pagedaemon up
1bfac262
MD
1887 * when our free+cache pages go below the free_min+cache_min level.
1888 * The pagedaemon tries to get the count back up to at least the
1889 * minimum, and through to the target level if possible.
20479584
MD
1890 *
1891 * If the pagedaemon is already active bump vm_pages_needed as a hint
1892 * that there are even more requests pending.
99ad9bc4
MD
1893 *
1894 * SMP races ok?
1895 * No requirements.
20479584 1896 */
984263bc 1897void
57e43348 1898pagedaemon_wakeup(void)
984263bc 1899{
1bfac262 1900 if (vm_paging_needed() && curthread != pagethread) {
20479584 1901 if (vm_pages_needed == 0) {
1bfac262 1902 vm_pages_needed = 1; /* SMP race ok */
20479584
MD
1903 wakeup(&vm_pages_needed);
1904 } else if (vm_page_count_min(0)) {
1bfac262 1905 ++vm_pages_needed; /* SMP race ok */
20479584 1906 }
984263bc
MD
1907 }
1908}
1909
1910#if !defined(NO_SWAPPING)
99ad9bc4
MD
1911
1912/*
1913 * SMP races ok?
1914 * No requirements.
1915 */
984263bc 1916static void
57e43348 1917vm_req_vmdaemon(void)
984263bc
MD
1918{
1919 static int lastrun = 0;
1920
1921 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
1922 wakeup(&vm_daemon_needed);
1923 lastrun = ticks;
1924 }
1925}
1926
8fa76237
MD
1927static int vm_daemon_callback(struct proc *p, void *data __unused);
1928
99ad9bc4
MD
1929/*
1930 * No requirements.
1931 */
984263bc 1932static void
57e43348 1933vm_daemon(void)
984263bc 1934{
99ad9bc4 1935 /*
b12defdc 1936 * XXX vm_daemon_needed specific token?
99ad9bc4 1937 */
984263bc 1938 while (TRUE) {
377d4740 1939 tsleep(&vm_daemon_needed, 0, "psleep", 0);
984263bc
MD
1940 if (vm_pageout_req_swapout) {
1941 swapout_procs(vm_pageout_req_swapout);
1942 vm_pageout_req_swapout = 0;
1943 }
1944 /*
1945 * scan the processes for exceeding their rlimits or if
1946 * process is swapped out -- deactivate pages
1947 */
8fa76237
MD
1948 allproc_scan(vm_daemon_callback, NULL);
1949 }
1950}
984263bc 1951
99ad9bc4 1952/*
b12defdc 1953 * Caller must hold proc_token.
99ad9bc4 1954 */
8fa76237
MD
1955static int
1956vm_daemon_callback(struct proc *p, void *data __unused)
1957{
1958 vm_pindex_t limit, size;
984263bc 1959
8fa76237
MD
1960 /*
1961 * if this is a system process or if we have already
1962 * looked at this process, skip it.
1963 */
1964 if (p->p_flag & (P_SYSTEM | P_WEXIT))
1965 return (0);
984263bc 1966
8fa76237
MD
1967 /*
1968 * if the process is in a non-running type state,
1969 * don't touch it.
1970 */
164b8401 1971 if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
8fa76237 1972 return (0);
984263bc 1973
8fa76237
MD
1974 /*
1975 * get a limit
1976 */
1977 limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
1978 p->p_rlimit[RLIMIT_RSS].rlim_max));
1979
1980 /*
1981 * let processes that are swapped out really be
1982 * swapped out. Set the limit to nothing to get as
1983 * many pages out to swap as possible.
1984 */
1985 if (p->p_flag & P_SWAPPEDOUT)
1986 limit = 0;
1987
b12defdc 1988 lwkt_gettoken(&p->p_vmspace->vm_map.token);
8fa76237
MD
1989 size = vmspace_resident_count(p->p_vmspace);
1990 if (limit >= 0 && size >= limit) {
b12defdc 1991 vm_pageout_map_deactivate_pages(&p->p_vmspace->vm_map, limit);
984263bc 1992 }
b12defdc 1993 lwkt_reltoken(&p->p_vmspace->vm_map.token);
8fa76237 1994 return (0);
984263bc 1995}
8fa76237 1996
984263bc 1997#endif