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