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