Fix various names of defined values.
[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 $
4530a3aa 69 * $DragonFly: src/sys/vm/vm_pageout.c,v 1.33 2008/03/20 06:02:50 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 *
4530a3aa
MD
398 * We must make the pages read-only. This will also force the
399 * modified bit in the related pmaps to be cleared. The pager
400 * cannot clear the bit for us since the I/O completion code
401 * typically runs from an interrupt. The act of making the page
402 * read-only handles the case for us.
984263bc
MD
403 */
404
405 for (i = 0; i < count; i++) {
406 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, ("vm_pageout_flush page %p index %d/%d: partially invalid page", mc[i], i, count));
407 vm_page_io_start(mc[i]);
408 vm_page_protect(mc[i], VM_PROT_READ);
409 }
410
411 object = mc[0]->object;
412 vm_object_pip_add(object, count);
413
414 vm_pager_put_pages(object, mc, count,
c439ad8f 415 (flags | ((object == &kernel_object) ? VM_PAGER_PUT_SYNC : 0)),
984263bc
MD
416 pageout_status);
417
418 for (i = 0; i < count; i++) {
419 vm_page_t mt = mc[i];
420
421 switch (pageout_status[i]) {
422 case VM_PAGER_OK:
423 numpagedout++;
424 break;
425 case VM_PAGER_PEND:
426 numpagedout++;
427 break;
428 case VM_PAGER_BAD:
429 /*
430 * Page outside of range of object. Right now we
431 * essentially lose the changes by pretending it
432 * worked.
433 */
434 pmap_clear_modify(mt);
435 vm_page_undirty(mt);
436 break;
437 case VM_PAGER_ERROR:
438 case VM_PAGER_FAIL:
439 /*
440 * If page couldn't be paged out, then reactivate the
441 * page so it doesn't clog the inactive list. (We
442 * will try paging out it again later).
443 */
444 vm_page_activate(mt);
445 break;
446 case VM_PAGER_AGAIN:
447 break;
448 }
449
450 /*
451 * If the operation is still going, leave the page busy to
452 * block all other accesses. Also, leave the paging in
453 * progress indicator set so that we don't attempt an object
454 * collapse.
455 */
456 if (pageout_status[i] != VM_PAGER_PEND) {
457 vm_object_pip_wakeup(object);
458 vm_page_io_finish(mt);
459 if (!vm_page_count_severe() || !vm_page_try_to_cache(mt))
460 vm_page_protect(mt, VM_PROT_READ);
461 }
462 }
463 return numpagedout;
464}
465
466#if !defined(NO_SWAPPING)
467/*
468 * vm_pageout_object_deactivate_pages
469 *
470 * deactivate enough pages to satisfy the inactive target
471 * requirements or if vm_page_proc_limit is set, then
472 * deactivate all of the pages in the object and its
473 * backing_objects.
474 *
475 * The object and map must be locked.
476 */
1f804340
MD
477static int vm_pageout_object_deactivate_pages_callback(vm_page_t, void *);
478
984263bc 479static void
57e43348 480vm_pageout_object_deactivate_pages(vm_map_t map, vm_object_t object,
06ecca5a 481 vm_pindex_t desired, int map_remove_only)
984263bc 482{
1f804340 483 struct rb_vm_page_scan_info info;
984263bc 484 int remove_mode;
984263bc
MD
485
486 if (object->type == OBJT_DEVICE || object->type == OBJT_PHYS)
487 return;
488
489 while (object) {
490 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
491 return;
492 if (object->paging_in_progress)
493 return;
494
495 remove_mode = map_remove_only;
496 if (object->shadow_count > 1)
497 remove_mode = 1;
06ecca5a
MD
498
499 /*
500 * scan the objects entire memory queue. spl protection is
501 * required to avoid an interrupt unbusy/free race against
502 * our busy check.
503 */
5fd012e0 504 crit_enter();
1f804340
MD
505 info.limit = remove_mode;
506 info.map = map;
507 info.desired = desired;
508 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
509 vm_pageout_object_deactivate_pages_callback,
510 &info
511 );
512 crit_exit();
513 object = object->backing_object;
514 }
515}
516
517static int
518vm_pageout_object_deactivate_pages_callback(vm_page_t p, void *data)
519{
520 struct rb_vm_page_scan_info *info = data;
521 int actcount;
984263bc 522
1f804340
MD
523 if (pmap_resident_count(vm_map_pmap(info->map)) <= info->desired) {
524 return(-1);
525 }
526 mycpu->gd_cnt.v_pdpages++;
527 if (p->wire_count != 0 || p->hold_count != 0 || p->busy != 0 ||
528 (p->flags & (PG_BUSY|PG_UNMANAGED)) ||
529 !pmap_page_exists_quick(vm_map_pmap(info->map), p)) {
530 return(0);
531 }
984263bc 532
1f804340
MD
533 actcount = pmap_ts_referenced(p);
534 if (actcount) {
535 vm_page_flag_set(p, PG_REFERENCED);
536 } else if (p->flags & PG_REFERENCED) {
537 actcount = 1;
538 }
539
540 if ((p->queue != PQ_ACTIVE) &&
541 (p->flags & PG_REFERENCED)) {
542 vm_page_activate(p);
543 p->act_count += actcount;
544 vm_page_flag_clear(p, PG_REFERENCED);
545 } else if (p->queue == PQ_ACTIVE) {
546 if ((p->flags & PG_REFERENCED) == 0) {
547 p->act_count -= min(p->act_count, ACT_DECLINE);
548 if (!info->limit && (vm_pageout_algorithm || (p->act_count == 0))) {
984263bc 549 vm_page_protect(p, VM_PROT_NONE);
1f804340
MD
550 vm_page_deactivate(p);
551 } else {
552 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
553 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
984263bc 554 }
1f804340
MD
555 } else {
556 vm_page_activate(p);
557 vm_page_flag_clear(p, PG_REFERENCED);
558 if (p->act_count < (ACT_MAX - ACT_ADVANCE))
559 p->act_count += ACT_ADVANCE;
560 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
561 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
984263bc 562 }
1f804340
MD
563 } else if (p->queue == PQ_INACTIVE) {
564 vm_page_protect(p, VM_PROT_NONE);
984263bc 565 }
1f804340 566 return(0);
984263bc
MD
567}
568
569/*
570 * deactivate some number of pages in a map, try to do it fairly, but
571 * that is really hard to do.
572 */
573static void
57e43348 574vm_pageout_map_deactivate_pages(vm_map_t map, vm_pindex_t desired)
984263bc
MD
575{
576 vm_map_entry_t tmpe;
577 vm_object_t obj, bigobj;
578 int nothingwired;
579
df4f70a6 580 if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT)) {
984263bc
MD
581 return;
582 }
583
584 bigobj = NULL;
585 nothingwired = TRUE;
586
587 /*
588 * first, search out the biggest object, and try to free pages from
589 * that.
590 */
591 tmpe = map->header.next;
592 while (tmpe != &map->header) {
1b874851
MD
593 switch(tmpe->maptype) {
594 case VM_MAPTYPE_NORMAL:
595 case VM_MAPTYPE_VPAGETABLE:
984263bc
MD
596 obj = tmpe->object.vm_object;
597 if ((obj != NULL) && (obj->shadow_count <= 1) &&
598 ((bigobj == NULL) ||
599 (bigobj->resident_page_count < obj->resident_page_count))) {
600 bigobj = obj;
601 }
1b874851
MD
602 break;
603 default:
604 break;
984263bc
MD
605 }
606 if (tmpe->wired_count > 0)
607 nothingwired = FALSE;
608 tmpe = tmpe->next;
609 }
610
611 if (bigobj)
612 vm_pageout_object_deactivate_pages(map, bigobj, desired, 0);
613
614 /*
615 * Next, hunt around for other pages to deactivate. We actually
616 * do this search sort of wrong -- .text first is not the best idea.
617 */
618 tmpe = map->header.next;
619 while (tmpe != &map->header) {
620 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
621 break;
1b874851
MD
622 switch(tmpe->maptype) {
623 case VM_MAPTYPE_NORMAL:
624 case VM_MAPTYPE_VPAGETABLE:
984263bc
MD
625 obj = tmpe->object.vm_object;
626 if (obj)
627 vm_pageout_object_deactivate_pages(map, obj, desired, 0);
1b874851
MD
628 break;
629 default:
630 break;
984263bc
MD
631 }
632 tmpe = tmpe->next;
633 };
634
635 /*
636 * Remove all mappings if a process is swapped out, this will free page
637 * table pages.
638 */
639 if (desired == 0 && nothingwired)
640 pmap_remove(vm_map_pmap(map),
88181b08 641 VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
984263bc 642 vm_map_unlock(map);
984263bc
MD
643}
644#endif
645
646/*
a11aaa81
MD
647 * Don't try to be fancy - being fancy can lead to vnode deadlocks. We
648 * only do it for OBJT_DEFAULT and OBJT_SWAP objects which we know can
649 * be trivially freed.
984263bc 650 */
984263bc 651void
95813af0
MD
652vm_pageout_page_free(vm_page_t m)
653{
984263bc
MD
654 vm_object_t object = m->object;
655 int type = object->type;
656
657 if (type == OBJT_SWAP || type == OBJT_DEFAULT)
658 vm_object_reference(object);
659 vm_page_busy(m);
660 vm_page_protect(m, VM_PROT_NONE);
661 vm_page_free(m);
662 if (type == OBJT_SWAP || type == OBJT_DEFAULT)
663 vm_object_deallocate(object);
664}
665
666/*
667 * vm_pageout_scan does the dirty work for the pageout daemon.
668 */
8fa76237
MD
669
670struct vm_pageout_scan_info {
671 struct proc *bigproc;
672 vm_offset_t bigsize;
673};
674
675static int vm_pageout_scan_callback(struct proc *p, void *data);
676
984263bc
MD
677static void
678vm_pageout_scan(int pass)
679{
8fa76237 680 struct vm_pageout_scan_info info;
984263bc
MD
681 vm_page_t m, next;
682 struct vm_page marker;
683 int page_shortage, maxscan, pcount;
684 int addl_page_shortage, addl_page_shortage_init;
984263bc
MD
685 vm_object_t object;
686 int actcount;
687 int vnodes_skipped = 0;
688 int maxlaunder;
984263bc
MD
689
690 /*
691 * Do whatever cleanup that the pmap code can.
692 */
693 pmap_collect();
694
695 addl_page_shortage_init = vm_pageout_deficit;
696 vm_pageout_deficit = 0;
697
698 /*
699 * Calculate the number of pages we want to either free or move
700 * to the cache.
701 */
702 page_shortage = vm_paging_target() + addl_page_shortage_init;
703
704 /*
705 * Initialize our marker
706 */
707 bzero(&marker, sizeof(marker));
708 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
709 marker.queue = PQ_INACTIVE;
710 marker.wire_count = 1;
711
712 /*
713 * Start scanning the inactive queue for pages we can move to the
714 * cache or free. The scan will stop when the target is reached or
715 * we have scanned the entire inactive queue. Note that m->act_count
716 * is not used to form decisions for the inactive queue, only for the
717 * active queue.
718 *
719 * maxlaunder limits the number of dirty pages we flush per scan.
720 * For most systems a smaller value (16 or 32) is more robust under
721 * extreme memory and disk pressure because any unnecessary writes
722 * to disk can result in extreme performance degredation. However,
723 * systems with excessive dirty pages (especially when MAP_NOSYNC is
724 * used) will die horribly with limited laundering. If the pageout
725 * daemon cannot clean enough pages in the first pass, we let it go
726 * all out in succeeding passes.
727 */
728 if ((maxlaunder = vm_max_launder) <= 1)
729 maxlaunder = 1;
730 if (pass)
731 maxlaunder = 10000;
732
06ecca5a 733 /*
5fd012e0
MD
734 * We will generally be in a critical section throughout the
735 * scan, but we can release it temporarily when we are sitting on a
736 * non-busy page without fear. this is required to prevent an
737 * interrupt from unbusying or freeing a page prior to our busy
738 * check, leaving us on the wrong queue or checking the wrong
739 * page.
06ecca5a 740 */
5fd012e0 741 crit_enter();
984263bc
MD
742rescan0:
743 addl_page_shortage = addl_page_shortage_init;
12e4aaff 744 maxscan = vmstats.v_inactive_count;
984263bc
MD
745 for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
746 m != NULL && maxscan-- > 0 && page_shortage > 0;
06ecca5a
MD
747 m = next
748 ) {
12e4aaff 749 mycpu->gd_cnt.v_pdpages++;
984263bc 750
06ecca5a
MD
751 /*
752 * Give interrupts a chance
753 */
5fd012e0
MD
754 crit_exit();
755 crit_enter();
984263bc 756
06ecca5a
MD
757 /*
758 * It's easier for some of the conditions below to just loop
759 * and catch queue changes here rather then check everywhere
760 * else.
761 */
762 if (m->queue != PQ_INACTIVE)
763 goto rescan0;
984263bc
MD
764 next = TAILQ_NEXT(m, pageq);
765
766 /*
767 * skip marker pages
768 */
769 if (m->flags & PG_MARKER)
770 continue;
771
772 /*
773 * A held page may be undergoing I/O, so skip it.
774 */
775 if (m->hold_count) {
984263bc
MD
776 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
777 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
984263bc
MD
778 addl_page_shortage++;
779 continue;
780 }
06ecca5a 781
984263bc
MD
782 /*
783 * Dont mess with busy pages, keep in the front of the
784 * queue, most likely are being paged out.
785 */
786 if (m->busy || (m->flags & PG_BUSY)) {
787 addl_page_shortage++;
788 continue;
789 }
790
984263bc 791 if (m->object->ref_count == 0) {
06ecca5a
MD
792 /*
793 * If the object is not being used, we ignore previous
794 * references.
795 */
984263bc
MD
796 vm_page_flag_clear(m, PG_REFERENCED);
797 pmap_clear_reference(m);
798
984263bc 799 } else if (((m->flags & PG_REFERENCED) == 0) &&
06ecca5a
MD
800 (actcount = pmap_ts_referenced(m))) {
801 /*
802 * Otherwise, if the page has been referenced while
803 * in the inactive queue, we bump the "activation
804 * count" upwards, making it less likely that the
805 * page will be added back to the inactive queue
806 * prematurely again. Here we check the page tables
807 * (or emulated bits, if any), given the upper level
808 * VM system not knowing anything about existing
809 * references.
810 */
984263bc
MD
811 vm_page_activate(m);
812 m->act_count += (actcount + ACT_ADVANCE);
813 continue;
814 }
815
816 /*
817 * If the upper level VM system knows about any page
818 * references, we activate the page. We also set the
819 * "activation count" higher than normal so that we will less
820 * likely place pages back onto the inactive queue again.
821 */
822 if ((m->flags & PG_REFERENCED) != 0) {
823 vm_page_flag_clear(m, PG_REFERENCED);
824 actcount = pmap_ts_referenced(m);
825 vm_page_activate(m);
826 m->act_count += (actcount + ACT_ADVANCE + 1);
827 continue;
828 }
829
830 /*
831 * If the upper level VM system doesn't know anything about
832 * the page being dirty, we have to check for it again. As
833 * far as the VM code knows, any partially dirty pages are
834 * fully dirty.
41a01a4d
MD
835 *
836 * Pages marked PG_WRITEABLE may be mapped into the user
837 * address space of a process running on another cpu. A
838 * user process (without holding the MP lock) running on
839 * another cpu may be able to touch the page while we are
840 * trying to remove it. To prevent this from occuring we
841 * must call pmap_remove_all() or otherwise make the page
842 * read-only. If the race occured pmap_remove_all() is
843 * responsible for setting m->dirty.
984263bc
MD
844 */
845 if (m->dirty == 0) {
846 vm_page_test_dirty(m);
41a01a4d
MD
847#if 0
848 if (m->dirty == 0 && (m->flags & PG_WRITEABLE) != 0)
849 pmap_remove_all(m);
850#endif
984263bc
MD
851 } else {
852 vm_page_dirty(m);
853 }
854
984263bc 855 if (m->valid == 0) {
41a01a4d
MD
856 /*
857 * Invalid pages can be easily freed
858 */
984263bc 859 vm_pageout_page_free(m);
12e4aaff 860 mycpu->gd_cnt.v_dfree++;
984263bc 861 --page_shortage;
984263bc
MD
862 } else if (m->dirty == 0) {
863 /*
41a01a4d
MD
864 * Clean pages can be placed onto the cache queue.
865 * This effectively frees them.
984263bc
MD
866 */
867 vm_page_cache(m);
868 --page_shortage;
869 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
870 /*
871 * Dirty pages need to be paged out, but flushing
872 * a page is extremely expensive verses freeing
873 * a clean page. Rather then artificially limiting
874 * the number of pages we can flush, we instead give
875 * dirty pages extra priority on the inactive queue
876 * by forcing them to be cycled through the queue
877 * twice before being flushed, after which the
878 * (now clean) page will cycle through once more
879 * before being freed. This significantly extends
880 * the thrash point for a heavily loaded machine.
881 */
984263bc
MD
882 vm_page_flag_set(m, PG_WINATCFLS);
883 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
884 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
984263bc
MD
885 } else if (maxlaunder > 0) {
886 /*
887 * We always want to try to flush some dirty pages if
888 * we encounter them, to keep the system stable.
889 * Normally this number is small, but under extreme
890 * pressure where there are insufficient clean pages
891 * on the inactive queue, we may have to go all out.
892 */
893 int swap_pageouts_ok;
894 struct vnode *vp = NULL;
895
896 object = m->object;
897
898 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
899 swap_pageouts_ok = 1;
900 } else {
901 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
902 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
903 vm_page_count_min());
904
905 }
906
907 /*
908 * We don't bother paging objects that are "dead".
909 * Those objects are in a "rundown" state.
910 */
911 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
984263bc
MD
912 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
913 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
984263bc
MD
914 continue;
915 }
916
917 /*
918 * The object is already known NOT to be dead. It
919 * is possible for the vget() to block the whole
920 * pageout daemon, but the new low-memory handling
921 * code should prevent it.
922 *
923 * The previous code skipped locked vnodes and, worse,
924 * reordered pages in the queue. This results in
925 * completely non-deterministic operation because,
926 * quite often, a vm_fault has initiated an I/O and
927 * is holding a locked vnode at just the point where
928 * the pageout daemon is woken up.
929 *
930 * We can't wait forever for the vnode lock, we might
931 * deadlock due to a vn_read() getting stuck in
932 * vm_wait while holding this vnode. We skip the
933 * vnode if we can't get it in a reasonable amount
934 * of time.
935 */
936
937 if (object->type == OBJT_VNODE) {
938 vp = object->handle;
939
87de5057 940 if (vget(vp, LK_EXCLUSIVE|LK_NOOBJ|LK_TIMELOCK)) {
984263bc
MD
941 ++pageout_lock_miss;
942 if (object->flags & OBJ_MIGHTBEDIRTY)
943 vnodes_skipped++;
944 continue;
945 }
946
947 /*
948 * The page might have been moved to another
949 * queue during potential blocking in vget()
950 * above. The page might have been freed and
951 * reused for another vnode. The object might
952 * have been reused for another vnode.
953 */
954 if (m->queue != PQ_INACTIVE ||
955 m->object != object ||
956 object->handle != vp) {
957 if (object->flags & OBJ_MIGHTBEDIRTY)
958 vnodes_skipped++;
959 vput(vp);
960 continue;
961 }
962
963 /*
964 * The page may have been busied during the
965 * blocking in vput(); We don't move the
966 * page back onto the end of the queue so that
967 * statistics are more correct if we don't.
968 */
969 if (m->busy || (m->flags & PG_BUSY)) {
970 vput(vp);
971 continue;
972 }
973
974 /*
975 * If the page has become held it might
976 * be undergoing I/O, so skip it
977 */
978 if (m->hold_count) {
984263bc
MD
979 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
980 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
984263bc
MD
981 if (object->flags & OBJ_MIGHTBEDIRTY)
982 vnodes_skipped++;
983 vput(vp);
984 continue;
985 }
986 }
987
988 /*
989 * If a page is dirty, then it is either being washed
990 * (but not yet cleaned) or it is still in the
991 * laundry. If it is still in the laundry, then we
992 * start the cleaning operation.
993 *
994 * This operation may cluster, invalidating the 'next'
995 * pointer. To prevent an inordinate number of
996 * restarts we use our marker to remember our place.
997 *
998 * decrement page_shortage on success to account for
999 * the (future) cleaned page. Otherwise we could wind
1000 * up laundering or cleaning too many pages.
1001 */
984263bc 1002 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq);
984263bc
MD
1003 if (vm_pageout_clean(m) != 0) {
1004 --page_shortage;
1005 --maxlaunder;
1006 }
984263bc
MD
1007 next = TAILQ_NEXT(&marker, pageq);
1008 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq);
984263bc
MD
1009 if (vp != NULL)
1010 vput(vp);
1011 }
1012 }
1013
1014 /*
1015 * Compute the number of pages we want to try to move from the
1016 * active queue to the inactive queue.
1017 */
1018 page_shortage = vm_paging_target() +
12e4aaff 1019 vmstats.v_inactive_target - vmstats.v_inactive_count;
984263bc
MD
1020 page_shortage += addl_page_shortage;
1021
1022 /*
1023 * Scan the active queue for things we can deactivate. We nominally
1024 * track the per-page activity counter and use it to locate
1025 * deactivation candidates.
06ecca5a 1026 *
5fd012e0 1027 * NOTE: we are still in a critical section.
984263bc 1028 */
12e4aaff 1029 pcount = vmstats.v_active_count;
984263bc
MD
1030 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1031
1032 while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
06ecca5a
MD
1033 /*
1034 * Give interrupts a chance.
1035 */
5fd012e0
MD
1036 crit_exit();
1037 crit_enter();
984263bc
MD
1038
1039 /*
06ecca5a 1040 * If the page was ripped out from under us, just stop.
984263bc 1041 */
06ecca5a 1042 if (m->queue != PQ_ACTIVE)
984263bc 1043 break;
984263bc 1044 next = TAILQ_NEXT(m, pageq);
06ecca5a 1045
984263bc
MD
1046 /*
1047 * Don't deactivate pages that are busy.
1048 */
1049 if ((m->busy != 0) ||
1050 (m->flags & PG_BUSY) ||
1051 (m->hold_count != 0)) {
984263bc
MD
1052 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1053 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
984263bc
MD
1054 m = next;
1055 continue;
1056 }
1057
1058 /*
1059 * The count for pagedaemon pages is done after checking the
1060 * page for eligibility...
1061 */
12e4aaff 1062 mycpu->gd_cnt.v_pdpages++;
984263bc
MD
1063
1064 /*
1065 * Check to see "how much" the page has been used.
1066 */
1067 actcount = 0;
1068 if (m->object->ref_count != 0) {
1069 if (m->flags & PG_REFERENCED) {
1070 actcount += 1;
1071 }
1072 actcount += pmap_ts_referenced(m);
1073 if (actcount) {
1074 m->act_count += ACT_ADVANCE + actcount;
1075 if (m->act_count > ACT_MAX)
1076 m->act_count = ACT_MAX;
1077 }
1078 }
1079
1080 /*
1081 * Since we have "tested" this bit, we need to clear it now.
1082 */
1083 vm_page_flag_clear(m, PG_REFERENCED);
1084
1085 /*
1086 * Only if an object is currently being used, do we use the
1087 * page activation count stats.
1088 */
1089 if (actcount && (m->object->ref_count != 0)) {
984263bc
MD
1090 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1091 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
984263bc
MD
1092 } else {
1093 m->act_count -= min(m->act_count, ACT_DECLINE);
1094 if (vm_pageout_algorithm ||
1095 m->object->ref_count == 0 ||
b5ccee96 1096 m->act_count < pass) {
984263bc
MD
1097 page_shortage--;
1098 if (m->object->ref_count == 0) {
1099 vm_page_protect(m, VM_PROT_NONE);
1100 if (m->dirty == 0)
1101 vm_page_cache(m);
1102 else
1103 vm_page_deactivate(m);
1104 } else {
1105 vm_page_deactivate(m);
1106 }
1107 } else {
984263bc
MD
1108 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1109 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
984263bc
MD
1110 }
1111 }
1112 m = next;
1113 }
1114
984263bc
MD
1115 /*
1116 * We try to maintain some *really* free pages, this allows interrupt
1117 * code to be guaranteed space. Since both cache and free queues
1118 * are considered basically 'free', moving pages from cache to free
1119 * does not effect other calculations.
06ecca5a 1120 *
5fd012e0 1121 * NOTE: we are still in a critical section.
984263bc
MD
1122 */
1123
12e4aaff 1124 while (vmstats.v_free_count < vmstats.v_free_reserved) {
984263bc
MD
1125 static int cache_rover = 0;
1126 m = vm_page_list_find(PQ_CACHE, cache_rover, FALSE);
1127 if (!m)
1128 break;
1129 if ((m->flags & (PG_BUSY|PG_UNMANAGED)) ||
1130 m->busy ||
1131 m->hold_count ||
1132 m->wire_count) {
1133#ifdef INVARIANTS
086c1d7e 1134 kprintf("Warning: busy page %p found in cache\n", m);
984263bc
MD
1135#endif
1136 vm_page_deactivate(m);
1137 continue;
1138 }
1139 cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK;
1140 vm_pageout_page_free(m);
12e4aaff 1141 mycpu->gd_cnt.v_dfree++;
984263bc 1142 }
06ecca5a 1143
5fd012e0 1144 crit_exit();
984263bc
MD
1145
1146#if !defined(NO_SWAPPING)
1147 /*
1148 * Idle process swapout -- run once per second.
1149 */
1150 if (vm_swap_idle_enabled) {
1151 static long lsec;
1152 if (time_second != lsec) {
1153 vm_pageout_req_swapout |= VM_SWAP_IDLE;
1154 vm_req_vmdaemon();
1155 lsec = time_second;
1156 }
1157 }
1158#endif
1159
1160 /*
1161 * If we didn't get enough free pages, and we have skipped a vnode
1162 * in a writeable object, wakeup the sync daemon. And kick swapout
1163 * if we did not get enough free pages.
1164 */
1165 if (vm_paging_target() > 0) {
1166 if (vnodes_skipped && vm_page_count_min())
418ff780 1167 speedup_syncer();
984263bc
MD
1168#if !defined(NO_SWAPPING)
1169 if (vm_swap_enabled && vm_page_count_target()) {
1170 vm_req_vmdaemon();
1171 vm_pageout_req_swapout |= VM_SWAP_NORMAL;
1172 }
1173#endif
1174 }
1175
1176 /*
1177 * If we are out of swap and were not able to reach our paging
1178 * target, kill the largest process.
1179 */
1180 if ((vm_swap_size < 64 && vm_page_count_min()) ||
1181 (swap_pager_full && vm_paging_target() > 0)) {
1182#if 0
1183 if ((vm_swap_size < 64 || swap_pager_full) && vm_page_count_min()) {
1184#endif
8fa76237
MD
1185 info.bigproc = NULL;
1186 info.bigsize = 0;
1187 allproc_scan(vm_pageout_scan_callback, &info);
1188 if (info.bigproc != NULL) {
1189 killproc(info.bigproc, "out of swap space");
1190 info.bigproc->p_nice = PRIO_MIN;
08f2f1bb
SS
1191 info.bigproc->p_usched->resetpriority(
1192 FIRST_LWP_IN_PROC(info.bigproc));
12e4aaff 1193 wakeup(&vmstats.v_free_count);
8fa76237 1194 PRELE(info.bigproc);
984263bc
MD
1195 }
1196 }
1197}
1198
8fa76237
MD
1199static int
1200vm_pageout_scan_callback(struct proc *p, void *data)
1201{
1202 struct vm_pageout_scan_info *info = data;
1203 vm_offset_t size;
1204
1205 /*
1206 * if this is a system process, skip it
1207 */
1208 if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
1209 ((p->p_pid < 48) && (vm_swap_size != 0))) {
1210 return (0);
1211 }
1212
1213 /*
1214 * if the process is in a non-running type state,
1215 * don't touch it.
1216 */
164b8401 1217 if (p->p_stat != SACTIVE && p->p_stat != SSTOP) {
8fa76237
MD
1218 return (0);
1219 }
1220
1221 /*
1222 * get the process size
1223 */
1224 size = vmspace_resident_count(p->p_vmspace) +
1225 vmspace_swap_count(p->p_vmspace);
1226
1227 /*
1228 * If the this process is bigger than the biggest one
1229 * remember it.
1230 */
1231 if (size > info->bigsize) {
1232 if (info->bigproc)
1233 PRELE(info->bigproc);
1234 PHOLD(p);
1235 info->bigproc = p;
1236 info->bigsize = size;
1237 }
1238 return(0);
1239}
1240
984263bc
MD
1241/*
1242 * This routine tries to maintain the pseudo LRU active queue,
1243 * so that during long periods of time where there is no paging,
1244 * that some statistic accumulation still occurs. This code
1245 * helps the situation where paging just starts to occur.
1246 */
1247static void
57e43348 1248vm_pageout_page_stats(void)
984263bc 1249{
984263bc
MD
1250 vm_page_t m,next;
1251 int pcount,tpcount; /* Number of pages to check */
1252 static int fullintervalcount = 0;
1253 int page_shortage;
984263bc
MD
1254
1255 page_shortage =
12e4aaff
MD
1256 (vmstats.v_inactive_target + vmstats.v_cache_max + vmstats.v_free_min) -
1257 (vmstats.v_free_count + vmstats.v_inactive_count + vmstats.v_cache_count);
984263bc
MD
1258
1259 if (page_shortage <= 0)
1260 return;
1261
5fd012e0 1262 crit_enter();
984263bc 1263
12e4aaff 1264 pcount = vmstats.v_active_count;
984263bc
MD
1265 fullintervalcount += vm_pageout_stats_interval;
1266 if (fullintervalcount < vm_pageout_full_stats_interval) {
12e4aaff 1267 tpcount = (vm_pageout_stats_max * vmstats.v_active_count) / vmstats.v_page_count;
984263bc
MD
1268 if (pcount > tpcount)
1269 pcount = tpcount;
1270 } else {
1271 fullintervalcount = 0;
1272 }
1273
1274 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1275 while ((m != NULL) && (pcount-- > 0)) {
1276 int actcount;
1277
1278 if (m->queue != PQ_ACTIVE) {
1279 break;
1280 }
1281
1282 next = TAILQ_NEXT(m, pageq);
1283 /*
1284 * Don't deactivate pages that are busy.
1285 */
1286 if ((m->busy != 0) ||
1287 (m->flags & PG_BUSY) ||
1288 (m->hold_count != 0)) {
984263bc
MD
1289 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1290 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
984263bc
MD
1291 m = next;
1292 continue;
1293 }
1294
1295 actcount = 0;
1296 if (m->flags & PG_REFERENCED) {
1297 vm_page_flag_clear(m, PG_REFERENCED);
1298 actcount += 1;
1299 }
1300
1301 actcount += pmap_ts_referenced(m);
1302 if (actcount) {
1303 m->act_count += ACT_ADVANCE + actcount;
1304 if (m->act_count > ACT_MAX)
1305 m->act_count = ACT_MAX;
984263bc
MD
1306 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1307 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
984263bc
MD
1308 } else {
1309 if (m->act_count == 0) {
1310 /*
1311 * We turn off page access, so that we have
1312 * more accurate RSS stats. We don't do this
1313 * in the normal page deactivation when the
1314 * system is loaded VM wise, because the
1315 * cost of the large number of page protect
1316 * operations would be higher than the value
1317 * of doing the operation.
1318 */
1319 vm_page_protect(m, VM_PROT_NONE);
1320 vm_page_deactivate(m);
1321 } else {
1322 m->act_count -= min(m->act_count, ACT_DECLINE);
984263bc
MD
1323 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1324 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
984263bc
MD
1325 }
1326 }
1327
1328 m = next;
1329 }
5fd012e0 1330 crit_exit();
984263bc
MD
1331}
1332
1333static int
57e43348 1334vm_pageout_free_page_calc(vm_size_t count)
984263bc 1335{
12e4aaff 1336 if (count < vmstats.v_page_count)
984263bc
MD
1337 return 0;
1338 /*
1339 * free_reserved needs to include enough for the largest swap pager
1340 * structures plus enough for any pv_entry structs when paging.
1341 */
12e4aaff
MD
1342 if (vmstats.v_page_count > 1024)
1343 vmstats.v_free_min = 4 + (vmstats.v_page_count - 1024) / 200;
984263bc 1344 else
12e4aaff
MD
1345 vmstats.v_free_min = 4;
1346 vmstats.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
1347 vmstats.v_interrupt_free_min;
1348 vmstats.v_free_reserved = vm_pageout_page_count +
1349 vmstats.v_pageout_free_min + (count / 768) + PQ_L2_SIZE;
1350 vmstats.v_free_severe = vmstats.v_free_min / 2;
1351 vmstats.v_free_min += vmstats.v_free_reserved;
1352 vmstats.v_free_severe += vmstats.v_free_reserved;
984263bc
MD
1353 return 1;
1354}
1355
1356
1357/*
1358 * vm_pageout is the high level pageout daemon.
1359 */
1360static void
57e43348 1361vm_pageout(void)
984263bc
MD
1362{
1363 int pass;
1364
1365 /*
1366 * Initialize some paging parameters.
1367 */
1368
12e4aaff
MD
1369 vmstats.v_interrupt_free_min = 2;
1370 if (vmstats.v_page_count < 2000)
984263bc
MD
1371 vm_pageout_page_count = 8;
1372
12e4aaff 1373 vm_pageout_free_page_calc(vmstats.v_page_count);
984263bc
MD
1374 /*
1375 * v_free_target and v_cache_min control pageout hysteresis. Note
1376 * that these are more a measure of the VM cache queue hysteresis
1377 * then the VM free queue. Specifically, v_free_target is the
1378 * high water mark (free+cache pages).
1379 *
1380 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1381 * low water mark, while v_free_min is the stop. v_cache_min must
1382 * be big enough to handle memory needs while the pageout daemon
1383 * is signalled and run to free more pages.
1384 */
12e4aaff
MD
1385 if (vmstats.v_free_count > 6144)
1386 vmstats.v_free_target = 4 * vmstats.v_free_min + vmstats.v_free_reserved;
984263bc 1387 else
12e4aaff 1388 vmstats.v_free_target = 2 * vmstats.v_free_min + vmstats.v_free_reserved;
984263bc 1389
12e4aaff
MD
1390 if (vmstats.v_free_count > 2048) {
1391 vmstats.v_cache_min = vmstats.v_free_target;
1392 vmstats.v_cache_max = 2 * vmstats.v_cache_min;
1393 vmstats.v_inactive_target = (3 * vmstats.v_free_target) / 2;
984263bc 1394 } else {
12e4aaff
MD
1395 vmstats.v_cache_min = 0;
1396 vmstats.v_cache_max = 0;
1397 vmstats.v_inactive_target = vmstats.v_free_count / 4;
984263bc 1398 }
12e4aaff
MD
1399 if (vmstats.v_inactive_target > vmstats.v_free_count / 3)
1400 vmstats.v_inactive_target = vmstats.v_free_count / 3;
984263bc
MD
1401
1402 /* XXX does not really belong here */
1403 if (vm_page_max_wired == 0)
12e4aaff 1404 vm_page_max_wired = vmstats.v_free_count / 3;
984263bc
MD
1405
1406 if (vm_pageout_stats_max == 0)
12e4aaff 1407 vm_pageout_stats_max = vmstats.v_free_target;
984263bc
MD
1408
1409 /*
1410 * Set interval in seconds for stats scan.
1411 */
1412 if (vm_pageout_stats_interval == 0)
1413 vm_pageout_stats_interval = 5;
1414 if (vm_pageout_full_stats_interval == 0)
1415 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1416
1417
1418 /*
1419 * Set maximum free per pass
1420 */
1421 if (vm_pageout_stats_free_max == 0)
1422 vm_pageout_stats_free_max = 5;
1423
1424 swap_pager_swap_init();
1425 pass = 0;
1426 /*
1427 * The pageout daemon is never done, so loop forever.
1428 */
1429 while (TRUE) {
1430 int error;
984263bc
MD
1431
1432 /*
1433 * If we have enough free memory, wakeup waiters. Do
1434 * not clear vm_pages_needed until we reach our target,
1435 * otherwise we may be woken up over and over again and
1436 * waste a lot of cpu.
1437 */
5fd012e0 1438 crit_enter();
984263bc
MD
1439 if (vm_pages_needed && !vm_page_count_min()) {
1440 if (vm_paging_needed() <= 0)
1441 vm_pages_needed = 0;
12e4aaff 1442 wakeup(&vmstats.v_free_count);
984263bc
MD
1443 }
1444 if (vm_pages_needed) {
1445 /*
1446 * Still not done, take a second pass without waiting
1447 * (unlimited dirty cleaning), otherwise sleep a bit
1448 * and try again.
1449 */
1450 ++pass;
1451 if (pass > 1)
377d4740 1452 tsleep(&vm_pages_needed, 0, "psleep", hz/2);
984263bc
MD
1453 } else {
1454 /*
1455 * Good enough, sleep & handle stats. Prime the pass
1456 * for the next run.
1457 */
1458 if (pass > 1)
1459 pass = 1;
1460 else
1461 pass = 0;
1462 error = tsleep(&vm_pages_needed,
377d4740 1463 0, "psleep", vm_pageout_stats_interval * hz);
984263bc 1464 if (error && !vm_pages_needed) {
5fd012e0 1465 crit_exit();
984263bc
MD
1466 pass = 0;
1467 vm_pageout_page_stats();
1468 continue;
1469 }
1470 }
1471
1472 if (vm_pages_needed)
12e4aaff 1473 mycpu->gd_cnt.v_pdwakeups++;
5fd012e0 1474 crit_exit();
984263bc
MD
1475 vm_pageout_scan(pass);
1476 vm_pageout_deficit = 0;
1477 }
1478}
1479
1480void
57e43348 1481pagedaemon_wakeup(void)
984263bc 1482{
bc6dffab 1483 if (!vm_pages_needed && curthread != pagethread) {
984263bc
MD
1484 vm_pages_needed++;
1485 wakeup(&vm_pages_needed);
1486 }
1487}
1488
1489#if !defined(NO_SWAPPING)
1490static void
57e43348 1491vm_req_vmdaemon(void)
984263bc
MD
1492{
1493 static int lastrun = 0;
1494
1495 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
1496 wakeup(&vm_daemon_needed);
1497 lastrun = ticks;
1498 }
1499}
1500
8fa76237
MD
1501static int vm_daemon_callback(struct proc *p, void *data __unused);
1502
984263bc 1503static void
57e43348 1504vm_daemon(void)
984263bc 1505{
984263bc 1506 while (TRUE) {
377d4740 1507 tsleep(&vm_daemon_needed, 0, "psleep", 0);
984263bc
MD
1508 if (vm_pageout_req_swapout) {
1509 swapout_procs(vm_pageout_req_swapout);
1510 vm_pageout_req_swapout = 0;
1511 }
1512 /*
1513 * scan the processes for exceeding their rlimits or if
1514 * process is swapped out -- deactivate pages
1515 */
8fa76237
MD
1516 allproc_scan(vm_daemon_callback, NULL);
1517 }
1518}
984263bc 1519
8fa76237
MD
1520static int
1521vm_daemon_callback(struct proc *p, void *data __unused)
1522{
1523 vm_pindex_t limit, size;
984263bc 1524
8fa76237
MD
1525 /*
1526 * if this is a system process or if we have already
1527 * looked at this process, skip it.
1528 */
1529 if (p->p_flag & (P_SYSTEM | P_WEXIT))
1530 return (0);
984263bc 1531
8fa76237
MD
1532 /*
1533 * if the process is in a non-running type state,
1534 * don't touch it.
1535 */
164b8401 1536 if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
8fa76237 1537 return (0);
984263bc 1538
8fa76237
MD
1539 /*
1540 * get a limit
1541 */
1542 limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
1543 p->p_rlimit[RLIMIT_RSS].rlim_max));
1544
1545 /*
1546 * let processes that are swapped out really be
1547 * swapped out. Set the limit to nothing to get as
1548 * many pages out to swap as possible.
1549 */
1550 if (p->p_flag & P_SWAPPEDOUT)
1551 limit = 0;
1552
1553 size = vmspace_resident_count(p->p_vmspace);
1554 if (limit >= 0 && size >= limit) {
1555 vm_pageout_map_deactivate_pages(
1556 &p->p_vmspace->vm_map, limit);
984263bc 1557 }
8fa76237 1558 return (0);
984263bc 1559}
8fa76237 1560
984263bc 1561#endif