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