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