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