kernel - Increase VM page free minimums for allocations
[dragonfly.git] / sys / vm / vm_pageout.c
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (c) 1991 Regents of the University of California.
5  * All rights reserved.
6  * Copyright (c) 1994 John S. Dyson
7  * All rights reserved.
8  * Copyright (c) 1994 David Greenman
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  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      from: @(#)vm_pageout.c  7.4 (Berkeley) 5/7/91
39  *
40  *
41  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
42  * All rights reserved.
43  *
44  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
45  *
46  * Permission to use, copy, modify and distribute this software and
47  * its documentation is hereby granted, provided that both the copyright
48  * notice and this permission notice appear in all copies of the
49  * software, derivative works or modified versions, and any portions
50  * thereof, and that both notices appear in supporting documentation.
51  *
52  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
54  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
55  *
56  * Carnegie Mellon requests users of this software to return to
57  *
58  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
59  *  School of Computer Science
60  *  Carnegie Mellon University
61  *  Pittsburgh PA 15213-3890
62  *
63  * any improvements or extensions that they make and grant Carnegie the
64  * rights to redistribute these changes.
65  *
66  * $FreeBSD: src/sys/vm/vm_pageout.c,v 1.151.2.15 2002/12/29 18:21:04 dillon Exp $
67  */
68
69 /*
70  *      The proverbial page-out daemon.
71  */
72
73 #include "opt_vm.h"
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/proc.h>
78 #include <sys/kthread.h>
79 #include <sys/resourcevar.h>
80 #include <sys/signalvar.h>
81 #include <sys/vnode.h>
82 #include <sys/vmmeter.h>
83 #include <sys/sysctl.h>
84
85 #include <vm/vm.h>
86 #include <vm/vm_param.h>
87 #include <sys/lock.h>
88 #include <vm/vm_object.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_pageout.h>
92 #include <vm/vm_pager.h>
93 #include <vm/swap_pager.h>
94 #include <vm/vm_extern.h>
95
96 #include <sys/thread2.h>
97 #include <sys/spinlock2.h>
98 #include <vm/vm_page2.h>
99
100 /*
101  * System initialization
102  */
103
104 /* the kernel process "vm_pageout"*/
105 static int vm_pageout_clean (vm_page_t);
106 static int vm_pageout_scan (int pass);
107 static int vm_pageout_free_page_calc (vm_size_t count);
108 struct thread *pagethread;
109
110 #if !defined(NO_SWAPPING)
111 /* the kernel process "vm_daemon"*/
112 static void vm_daemon (void);
113 static struct   thread *vmthread;
114
115 static struct kproc_desc vm_kp = {
116         "vmdaemon",
117         vm_daemon,
118         &vmthread
119 };
120 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp)
121 #endif
122
123
124 int vm_pages_needed=0;          /* Event on which pageout daemon sleeps */
125 int vm_pageout_deficit=0;       /* Estimated number of pages deficit */
126 int vm_pageout_pages_needed=0;  /* flag saying that the pageout daemon needs pages */
127
128 #if !defined(NO_SWAPPING)
129 static int vm_pageout_req_swapout;      /* XXX */
130 static int vm_daemon_needed;
131 #endif
132 static int vm_max_launder = 32;
133 static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
134 static int vm_pageout_full_stats_interval = 0;
135 static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0;
136 static int defer_swap_pageouts=0;
137 static int disable_swap_pageouts=0;
138
139 #if defined(NO_SWAPPING)
140 static int vm_swap_enabled=0;
141 static int vm_swap_idle_enabled=0;
142 #else
143 static int vm_swap_enabled=1;
144 static int vm_swap_idle_enabled=0;
145 #endif
146
147 SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
148         CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
149
150 SYSCTL_INT(_vm, OID_AUTO, max_launder,
151         CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
152
153 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
154         CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
155
156 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
157         CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
158
159 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
160         CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
161
162 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max,
163         CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented");
164
165 #if defined(NO_SWAPPING)
166 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
167         CTLFLAG_RD, &vm_swap_enabled, 0, "");
168 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
169         CTLFLAG_RD, &vm_swap_idle_enabled, 0, "");
170 #else
171 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
172         CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
173 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
174         CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
175 #endif
176
177 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
178         CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
179
180 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
181         CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
182
183 static int pageout_lock_miss;
184 SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
185         CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
186
187 int vm_load;
188 SYSCTL_INT(_vm, OID_AUTO, vm_load,
189         CTLFLAG_RD, &vm_load, 0, "load on the VM system");
190 int vm_load_enable = 1;
191 SYSCTL_INT(_vm, OID_AUTO, vm_load_enable,
192         CTLFLAG_RW, &vm_load_enable, 0, "enable vm_load rate limiting");
193 #ifdef INVARIANTS
194 int vm_load_debug;
195 SYSCTL_INT(_vm, OID_AUTO, vm_load_debug,
196         CTLFLAG_RW, &vm_load_debug, 0, "debug vm_load");
197 #endif
198
199 #define VM_PAGEOUT_PAGE_COUNT 16
200 int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
201
202 int vm_page_max_wired;          /* XXX max # of wired pages system-wide */
203
204 #if !defined(NO_SWAPPING)
205 typedef void freeer_fcn_t (vm_map_t, vm_object_t, vm_pindex_t, int);
206 static void vm_pageout_map_deactivate_pages (vm_map_t, vm_pindex_t);
207 static freeer_fcn_t vm_pageout_object_deactivate_pages;
208 static void vm_req_vmdaemon (void);
209 #endif
210 static void vm_pageout_page_stats(void);
211
212 /*
213  * Update vm_load to slow down faulting processes.
214  *
215  * SMP races ok.
216  * No requirements.
217  */
218 void
219 vm_fault_ratecheck(void)
220 {
221         if (vm_pages_needed) {
222                 if (vm_load < 1000)
223                         ++vm_load;
224         } else {
225                 if (vm_load > 0)
226                         --vm_load;
227         }
228 }
229
230 /*
231  * vm_pageout_clean:
232  *
233  * Clean the page and remove it from the laundry.  The page must not be
234  * busy on-call.
235  * 
236  * We set the busy bit to cause potential page faults on this page to
237  * block.  Note the careful timing, however, the busy bit isn't set till
238  * late and we cannot do anything that will mess with the page.
239  */
240 static int
241 vm_pageout_clean(vm_page_t m)
242 {
243         vm_object_t object;
244         vm_page_t mc[2*vm_pageout_page_count];
245         int pageout_count;
246         int error;
247         int ib, is, page_base;
248         vm_pindex_t pindex = m->pindex;
249
250         object = m->object;
251
252         /*
253          * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
254          * with the new swapper, but we could have serious problems paging
255          * out other object types if there is insufficient memory.  
256          *
257          * Unfortunately, checking free memory here is far too late, so the
258          * check has been moved up a procedural level.
259          */
260
261         /*
262          * Don't mess with the page if it's busy, held, or special
263          *
264          * XXX do we really need to check hold_count here?  hold_count
265          * isn't supposed to mess with vm_page ops except prevent the
266          * page from being reused.
267          */
268         if (m->hold_count != 0 || (m->flags & PG_UNMANAGED)) {
269                 vm_page_wakeup(m);
270                 return 0;
271         }
272
273         mc[vm_pageout_page_count] = m;
274         pageout_count = 1;
275         page_base = vm_pageout_page_count;
276         ib = 1;
277         is = 1;
278
279         /*
280          * Scan object for clusterable pages.
281          *
282          * We can cluster ONLY if: ->> the page is NOT
283          * clean, wired, busy, held, or mapped into a
284          * buffer, and one of the following:
285          * 1) The page is inactive, or a seldom used
286          *    active page.
287          * -or-
288          * 2) we force the issue.
289          *
290          * During heavy mmap/modification loads the pageout
291          * daemon can really fragment the underlying file
292          * due to flushing pages out of order and not trying
293          * align the clusters (which leave sporatic out-of-order
294          * holes).  To solve this problem we do the reverse scan
295          * first and attempt to align our cluster, then do a 
296          * forward scan if room remains.
297          */
298
299         vm_object_hold(object);
300 more:
301         while (ib && pageout_count < vm_pageout_page_count) {
302                 vm_page_t p;
303
304                 if (ib > pindex) {
305                         ib = 0;
306                         break;
307                 }
308
309                 p = vm_page_lookup_busy_try(object, pindex - ib, TRUE, &error);
310                 if (error || p == NULL) {
311                         ib = 0;
312                         break;
313                 }
314                 if ((p->queue - p->pc) == PQ_CACHE ||
315                     (p->flags & PG_UNMANAGED)) {
316                         vm_page_wakeup(p);
317                         ib = 0;
318                         break;
319                 }
320                 vm_page_test_dirty(p);
321                 if ((p->dirty & p->valid) == 0 ||
322                     p->queue != PQ_INACTIVE ||
323                     p->wire_count != 0 ||       /* may be held by buf cache */
324                     p->hold_count != 0) {       /* may be undergoing I/O */
325                         vm_page_wakeup(p);
326                         ib = 0;
327                         break;
328                 }
329                 mc[--page_base] = p;
330                 ++pageout_count;
331                 ++ib;
332                 /*
333                  * alignment boundry, stop here and switch directions.  Do
334                  * not clear ib.
335                  */
336                 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
337                         break;
338         }
339
340         while (pageout_count < vm_pageout_page_count && 
341             pindex + is < object->size) {
342                 vm_page_t p;
343
344                 p = vm_page_lookup_busy_try(object, pindex + is, TRUE, &error);
345                 if (error || p == NULL)
346                         break;
347                 if (((p->queue - p->pc) == PQ_CACHE) ||
348                     (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
349                         vm_page_wakeup(p);
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                         vm_page_wakeup(p);
358                         break;
359                 }
360                 mc[page_base + pageout_count] = p;
361                 ++pageout_count;
362                 ++is;
363         }
364
365         /*
366          * If we exhausted our forward scan, continue with the reverse scan
367          * when possible, even past a page boundry.  This catches boundry
368          * conditions.
369          */
370         if (ib && pageout_count < vm_pageout_page_count)
371                 goto more;
372
373         vm_object_drop(object);
374
375         /*
376          * we allow reads during pageouts...
377          */
378         return vm_pageout_flush(&mc[page_base], pageout_count, 0);
379 }
380
381 /*
382  * vm_pageout_flush() - launder the given pages
383  *
384  *      The given pages are laundered.  Note that we setup for the start of
385  *      I/O ( i.e. busy the page ), mark it read-only, and bump the object
386  *      reference count all in here rather then in the parent.  If we want
387  *      the parent to do more sophisticated things we may have to change
388  *      the ordering.
389  *
390  *      The pages in the array must be busied by the caller and will be
391  *      unbusied by this function.
392  */
393 int
394 vm_pageout_flush(vm_page_t *mc, int count, int flags)
395 {
396         vm_object_t object;
397         int pageout_status[count];
398         int numpagedout = 0;
399         int i;
400
401         /*
402          * Initiate I/O.  Bump the vm_page_t->busy counter.
403          */
404         for (i = 0; i < count; i++) {
405                 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
406                         ("vm_pageout_flush page %p index %d/%d: partially "
407                          "invalid page", mc[i], i, count));
408                 vm_page_io_start(mc[i]);
409         }
410
411         /*
412          * We must make the pages read-only.  This will also force the
413          * modified bit in the related pmaps to be cleared.  The pager
414          * cannot clear the bit for us since the I/O completion code
415          * typically runs from an interrupt.  The act of making the page
416          * read-only handles the case for us.
417          *
418          * Then we can unbusy the pages, we still hold a reference by virtue
419          * of our soft-busy.
420          */
421         for (i = 0; i < count; i++) {
422                 vm_page_protect(mc[i], VM_PROT_READ);
423                 vm_page_wakeup(mc[i]);
424         }
425
426         object = mc[0]->object;
427         vm_object_pip_add(object, count);
428
429         vm_pager_put_pages(object, mc, count,
430             (flags | ((object == &kernel_object) ? VM_PAGER_PUT_SYNC : 0)),
431             pageout_status);
432
433         for (i = 0; i < count; i++) {
434                 vm_page_t mt = mc[i];
435
436                 switch (pageout_status[i]) {
437                 case VM_PAGER_OK:
438                         numpagedout++;
439                         break;
440                 case VM_PAGER_PEND:
441                         numpagedout++;
442                         break;
443                 case VM_PAGER_BAD:
444                         /*
445                          * Page outside of range of object. Right now we
446                          * essentially lose the changes by pretending it
447                          * worked.
448                          */
449                         vm_page_busy_wait(mt, FALSE, "pgbad");
450                         pmap_clear_modify(mt);
451                         vm_page_undirty(mt);
452                         vm_page_wakeup(mt);
453                         break;
454                 case VM_PAGER_ERROR:
455                 case VM_PAGER_FAIL:
456                         /*
457                          * A page typically cannot be paged out when we
458                          * have run out of swap.  We leave the page
459                          * marked inactive and will try to page it out
460                          * again later.
461                          *
462                          * Starvation of the active page list is used to
463                          * determine when the system is massively memory
464                          * starved.
465                          */
466                         break;
467                 case VM_PAGER_AGAIN:
468                         break;
469                 }
470
471                 /*
472                  * If the operation is still going, leave the page busy to
473                  * block all other accesses. Also, leave the paging in
474                  * progress indicator set so that we don't attempt an object
475                  * collapse.
476                  *
477                  * For any pages which have completed synchronously, 
478                  * deactivate the page if we are under a severe deficit.
479                  * Do not try to enter them into the cache, though, they
480                  * might still be read-heavy.
481                  */
482                 if (pageout_status[i] != VM_PAGER_PEND) {
483                         vm_page_busy_wait(mt, FALSE, "pgouw");
484                         if (vm_page_count_severe())
485                                 vm_page_deactivate(mt);
486 #if 0
487                         if (!vm_page_count_severe() || !vm_page_try_to_cache(mt))
488                                 vm_page_protect(mt, VM_PROT_READ);
489 #endif
490                         vm_page_io_finish(mt);
491                         vm_page_wakeup(mt);
492                         vm_object_pip_wakeup(object);
493                 }
494         }
495         return numpagedout;
496 }
497
498 #if !defined(NO_SWAPPING)
499 /*
500  * deactivate enough pages to satisfy the inactive target
501  * requirements or if vm_page_proc_limit is set, then
502  * deactivate all of the pages in the object and its
503  * backing_objects.
504  *
505  * The map must be locked.
506  * The caller must hold the vm_object.
507  */
508 static int vm_pageout_object_deactivate_pages_callback(vm_page_t, void *);
509
510 static void
511 vm_pageout_object_deactivate_pages(vm_map_t map, vm_object_t object,
512                                    vm_pindex_t desired, int map_remove_only)
513 {
514         struct rb_vm_page_scan_info info;
515         vm_object_t lobject;
516         vm_object_t tobject;
517         int remove_mode;
518
519         lobject = object;
520
521         while (lobject) {
522                 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
523                         break;
524                 if (lobject->type == OBJT_DEVICE || lobject->type == OBJT_PHYS)
525                         break;
526                 if (lobject->paging_in_progress)
527                         break;
528
529                 remove_mode = map_remove_only;
530                 if (lobject->shadow_count > 1)
531                         remove_mode = 1;
532
533                 /*
534                  * scan the objects entire memory queue.  We hold the
535                  * object's token so the scan should not race anything.
536                  */
537                 info.limit = remove_mode;
538                 info.map = map;
539                 info.desired = desired;
540                 vm_page_rb_tree_RB_SCAN(&lobject->rb_memq, NULL,
541                                 vm_pageout_object_deactivate_pages_callback,
542                                 &info
543                 );
544                 while ((tobject = lobject->backing_object) != NULL) {
545                         KKASSERT(tobject != object);
546                         vm_object_hold(tobject);
547                         if (tobject == lobject->backing_object)
548                                 break;
549                         vm_object_drop(tobject);
550                 }
551                 if (lobject != object)
552                         vm_object_drop(lobject);
553                 lobject = tobject;
554         }
555         if (lobject != object)
556                 vm_object_drop(lobject);
557 }
558
559 /*
560  * The caller must hold the vm_object.
561  */
562 static int
563 vm_pageout_object_deactivate_pages_callback(vm_page_t p, void *data)
564 {
565         struct rb_vm_page_scan_info *info = data;
566         int actcount;
567
568         if (pmap_resident_count(vm_map_pmap(info->map)) <= info->desired) {
569                 return(-1);
570         }
571         mycpu->gd_cnt.v_pdpages++;
572
573         if (vm_page_busy_try(p, TRUE))
574                 return(0);
575         if (p->wire_count || p->hold_count || (p->flags & PG_UNMANAGED)) {
576                 vm_page_wakeup(p);
577                 return(0);
578         }
579         if (!pmap_page_exists_quick(vm_map_pmap(info->map), p)) {
580                 vm_page_wakeup(p);
581                 return(0);
582         }
583
584         actcount = pmap_ts_referenced(p);
585         if (actcount) {
586                 vm_page_flag_set(p, PG_REFERENCED);
587         } else if (p->flags & PG_REFERENCED) {
588                 actcount = 1;
589         }
590
591         vm_page_and_queue_spin_lock(p);
592         if (p->queue != PQ_ACTIVE && (p->flags & PG_REFERENCED)) {
593                 vm_page_and_queue_spin_unlock(p);
594                 vm_page_activate(p);
595                 p->act_count += actcount;
596                 vm_page_flag_clear(p, PG_REFERENCED);
597         } else if (p->queue == PQ_ACTIVE) {
598                 if ((p->flags & PG_REFERENCED) == 0) {
599                         p->act_count -= min(p->act_count, ACT_DECLINE);
600                         if (!info->limit &&
601                             (vm_pageout_algorithm || (p->act_count == 0))) {
602                                 vm_page_and_queue_spin_unlock(p);
603                                 vm_page_protect(p, VM_PROT_NONE);
604                                 vm_page_deactivate(p);
605                         } else {
606                                 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
607                                 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
608                                 vm_page_and_queue_spin_unlock(p);
609                         }
610                 } else {
611                         vm_page_and_queue_spin_unlock(p);
612                         vm_page_activate(p);
613                         vm_page_flag_clear(p, PG_REFERENCED);
614
615                         vm_page_and_queue_spin_lock(p);
616                         if (p->queue == PQ_ACTIVE) {
617                                 if (p->act_count < (ACT_MAX - ACT_ADVANCE))
618                                         p->act_count += ACT_ADVANCE;
619                                 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
620                                 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
621                         }
622                         vm_page_and_queue_spin_unlock(p);
623                 }
624         } else if (p->queue == PQ_INACTIVE) {
625                 vm_page_and_queue_spin_unlock(p);
626                 vm_page_protect(p, VM_PROT_NONE);
627         } else {
628                 vm_page_and_queue_spin_unlock(p);
629         }
630         vm_page_wakeup(p);
631         return(0);
632 }
633
634 /*
635  * Deactivate some number of pages in a map, try to do it fairly, but
636  * that is really hard to do.
637  */
638 static void
639 vm_pageout_map_deactivate_pages(vm_map_t map, vm_pindex_t desired)
640 {
641         vm_map_entry_t tmpe;
642         vm_object_t obj, bigobj;
643         int nothingwired;
644
645         if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT)) {
646                 return;
647         }
648
649         bigobj = NULL;
650         nothingwired = TRUE;
651
652         /*
653          * first, search out the biggest object, and try to free pages from
654          * that.
655          */
656         tmpe = map->header.next;
657         while (tmpe != &map->header) {
658                 switch(tmpe->maptype) {
659                 case VM_MAPTYPE_NORMAL:
660                 case VM_MAPTYPE_VPAGETABLE:
661                         obj = tmpe->object.vm_object;
662                         if ((obj != NULL) && (obj->shadow_count <= 1) &&
663                                 ((bigobj == NULL) ||
664                                  (bigobj->resident_page_count < obj->resident_page_count))) {
665                                 bigobj = obj;
666                         }
667                         break;
668                 default:
669                         break;
670                 }
671                 if (tmpe->wired_count > 0)
672                         nothingwired = FALSE;
673                 tmpe = tmpe->next;
674         }
675
676         if (bigobj) 
677                 vm_pageout_object_deactivate_pages(map, bigobj, desired, 0);
678
679         /*
680          * Next, hunt around for other pages to deactivate.  We actually
681          * do this search sort of wrong -- .text first is not the best idea.
682          */
683         tmpe = map->header.next;
684         while (tmpe != &map->header) {
685                 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
686                         break;
687                 switch(tmpe->maptype) {
688                 case VM_MAPTYPE_NORMAL:
689                 case VM_MAPTYPE_VPAGETABLE:
690                         obj = tmpe->object.vm_object;
691                         if (obj) 
692                                 vm_pageout_object_deactivate_pages(map, obj, desired, 0);
693                         break;
694                 default:
695                         break;
696                 }
697                 tmpe = tmpe->next;
698         };
699
700         /*
701          * Remove all mappings if a process is swapped out, this will free page
702          * table pages.
703          */
704         if (desired == 0 && nothingwired)
705                 pmap_remove(vm_map_pmap(map),
706                             VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
707         vm_map_unlock(map);
708 }
709 #endif
710
711 /*
712  * Called when the pageout scan wants to free a page.  We no longer
713  * try to cycle the vm_object here with a reference & dealloc, which can
714  * cause a non-trivial object collapse in a critical path.
715  *
716  * It is unclear why we cycled the ref_count in the past, perhaps to try
717  * to optimize shadow chain collapses but I don't quite see why it would
718  * be necessary.  An OBJ_DEAD object should terminate any and all vm_pages
719  * synchronously and not have to be kicked-start.
720  */
721 static void
722 vm_pageout_page_free(vm_page_t m) 
723 {
724         vm_page_protect(m, VM_PROT_NONE);
725         vm_page_free(m);
726 }
727
728 /*
729  * vm_pageout_scan does the dirty work for the pageout daemon.
730  */
731 struct vm_pageout_scan_info {
732         struct proc *bigproc;
733         vm_offset_t bigsize;
734 };
735
736 static int vm_pageout_scan_callback(struct proc *p, void *data);
737
738 static int
739 vm_pageout_scan(int pass)
740 {
741         struct vm_pageout_scan_info info;
742         vm_page_t m;
743         struct vm_page marker;
744         struct vnode *vpfailed;         /* warning, allowed to be stale */
745         int maxscan, pcount;
746         int recycle_count;
747         int inactive_shortage, active_shortage;
748         int inactive_original_shortage;
749         vm_object_t object;
750         int actcount;
751         int vnodes_skipped = 0;
752         int maxlaunder;
753
754         /*
755          * Do whatever cleanup that the pmap code can.
756          */
757         pmap_collect();
758
759         /*
760          * Calculate our target for the number of free+cache pages we
761          * want to get to.  This is higher then the number that causes
762          * allocations to stall (severe) in order to provide hysteresis,
763          * and if we don't make it all the way but get to the minimum
764          * we're happy.
765          */
766         inactive_shortage = vm_paging_target() + vm_pageout_deficit;
767         inactive_original_shortage = inactive_shortage;
768         vm_pageout_deficit = 0;
769
770         /*
771          * Start scanning the inactive queue for pages we can move to the
772          * cache or free.  The scan will stop when the target is reached or
773          * we have scanned the entire inactive queue.  Note that m->act_count
774          * is not used to form decisions for the inactive queue, only for the
775          * active queue.
776          *
777          * maxlaunder limits the number of dirty pages we flush per scan.
778          * For most systems a smaller value (16 or 32) is more robust under
779          * extreme memory and disk pressure because any unnecessary writes
780          * to disk can result in extreme performance degredation.  However,
781          * systems with excessive dirty pages (especially when MAP_NOSYNC is
782          * used) will die horribly with limited laundering.  If the pageout
783          * daemon cannot clean enough pages in the first pass, we let it go
784          * all out in succeeding passes.
785          */
786         if ((maxlaunder = vm_max_launder) <= 1)
787                 maxlaunder = 1;
788         if (pass)
789                 maxlaunder = 10000;
790
791         /*
792          * Initialize our marker
793          */
794         bzero(&marker, sizeof(marker));
795         marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
796         marker.queue = PQ_INACTIVE;
797         marker.wire_count = 1;
798
799         /*
800          * Inactive queue scan.
801          *
802          * NOTE: The vm_page must be spinlocked before the queue to avoid
803          *       deadlocks, so it is easiest to simply iterate the loop
804          *       with the queue unlocked at the top.
805          */
806         vpfailed = NULL;
807
808         vm_page_queues_spin_lock(PQ_INACTIVE);
809         TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq);
810         maxscan = vmstats.v_inactive_count;
811         vm_page_queues_spin_unlock(PQ_INACTIVE);
812
813         while ((m = TAILQ_NEXT(&marker, pageq)) != NULL &&
814                maxscan-- > 0 && inactive_shortage > 0)
815         {
816                 vm_page_and_queue_spin_lock(m);
817                 if (m != TAILQ_NEXT(&marker, pageq)) {
818                         vm_page_and_queue_spin_unlock(m);
819                         ++maxscan;
820                         continue;
821                 }
822                 KKASSERT(m->queue == PQ_INACTIVE);
823                 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl,
824                              &marker, pageq);
825                 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m,
826                                    &marker, pageq);
827                 mycpu->gd_cnt.v_pdpages++;
828
829                 /*
830                  * Skip marker pages
831                  */
832                 if (m->flags & PG_MARKER) {
833                         vm_page_and_queue_spin_unlock(m);
834                         continue;
835                 }
836
837                 /*
838                  * Try to busy the page.  Don't mess with pages which are
839                  * already busy or reorder them in the queue.
840                  */
841                 if (vm_page_busy_try(m, TRUE)) {
842                         vm_page_and_queue_spin_unlock(m);
843                         continue;
844                 }
845                 vm_page_and_queue_spin_unlock(m);
846                 KKASSERT(m->queue == PQ_INACTIVE);
847
848                 /*
849                  * The page has been successfully busied and is now no
850                  * longer spinlocked.  The queue is no longer spinlocked
851                  * either.
852                  */
853
854                 /*
855                  * A held page may be undergoing I/O, so skip it.
856                  */
857                 if (m->hold_count) {
858                         vm_page_and_queue_spin_lock(m);
859                         if (m->queue == PQ_INACTIVE) {
860                                 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl,
861                                              m, pageq);
862                                 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl,
863                                                   m, pageq);
864                         }
865                         vm_page_and_queue_spin_unlock(m);
866                         ++vm_swapcache_inactive_heuristic;
867                         vm_page_wakeup(m);
868                         continue;
869                 }
870
871                 if (m->object->ref_count == 0) {
872                         /*
873                          * If the object is not being used, we ignore previous 
874                          * references.
875                          */
876                         vm_page_flag_clear(m, PG_REFERENCED);
877                         pmap_clear_reference(m);
878                         /* fall through to end */
879                 } else if (((m->flags & PG_REFERENCED) == 0) &&
880                             (actcount = pmap_ts_referenced(m))) {
881                         /*
882                          * Otherwise, if the page has been referenced while 
883                          * in the inactive queue, we bump the "activation
884                          * count" upwards, making it less likely that the
885                          * page will be added back to the inactive queue
886                          * prematurely again.  Here we check the page tables
887                          * (or emulated bits, if any), given the upper level
888                          * VM system not knowing anything about existing 
889                          * references.
890                          */
891                         vm_page_activate(m);
892                         m->act_count += (actcount + ACT_ADVANCE);
893                         vm_page_wakeup(m);
894                         continue;
895                 }
896
897                 /*
898                  * (m) is still busied.
899                  *
900                  * If the upper level VM system knows about any page 
901                  * references, we activate the page.  We also set the 
902                  * "activation count" higher than normal so that we will less 
903                  * likely place pages back onto the inactive queue again.
904                  */
905                 if ((m->flags & PG_REFERENCED) != 0) {
906                         vm_page_flag_clear(m, PG_REFERENCED);
907                         actcount = pmap_ts_referenced(m);
908                         vm_page_activate(m);
909                         m->act_count += (actcount + ACT_ADVANCE + 1);
910                         vm_page_wakeup(m);
911                         continue;
912                 }
913
914                 /*
915                  * If the upper level VM system doesn't know anything about 
916                  * the page being dirty, we have to check for it again.  As 
917                  * far as the VM code knows, any partially dirty pages are 
918                  * fully dirty.
919                  *
920                  * Pages marked PG_WRITEABLE may be mapped into the user
921                  * address space of a process running on another cpu.  A
922                  * user process (without holding the MP lock) running on
923                  * another cpu may be able to touch the page while we are
924                  * trying to remove it.  vm_page_cache() will handle this
925                  * case for us.
926                  */
927                 if (m->dirty == 0) {
928                         vm_page_test_dirty(m);
929                 } else {
930                         vm_page_dirty(m);
931                 }
932
933                 if (m->valid == 0) {
934                         /*
935                          * Invalid pages can be easily freed
936                          */
937                         vm_pageout_page_free(m);
938                         mycpu->gd_cnt.v_dfree++;
939                         --inactive_shortage;
940                 } else if (m->dirty == 0) {
941                         /*
942                          * Clean pages can be placed onto the cache queue.
943                          * This effectively frees them.
944                          */
945                         vm_page_cache(m);
946                         --inactive_shortage;
947                 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
948                         /*
949                          * Dirty pages need to be paged out, but flushing
950                          * a page is extremely expensive verses freeing
951                          * a clean page.  Rather then artificially limiting
952                          * the number of pages we can flush, we instead give
953                          * dirty pages extra priority on the inactive queue
954                          * by forcing them to be cycled through the queue
955                          * twice before being flushed, after which the 
956                          * (now clean) page will cycle through once more
957                          * before being freed.  This significantly extends
958                          * the thrash point for a heavily loaded machine.
959                          */
960                         vm_page_flag_set(m, PG_WINATCFLS);
961                         vm_page_and_queue_spin_lock(m);
962                         if (m->queue == PQ_INACTIVE) {
963                                 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
964                                 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
965                         }
966                         vm_page_and_queue_spin_unlock(m);
967                         ++vm_swapcache_inactive_heuristic;
968                         vm_page_wakeup(m);
969                 } else if (maxlaunder > 0) {
970                         /*
971                          * We always want to try to flush some dirty pages if
972                          * we encounter them, to keep the system stable.
973                          * Normally this number is small, but under extreme
974                          * pressure where there are insufficient clean pages
975                          * on the inactive queue, we may have to go all out.
976                          */
977                         int swap_pageouts_ok;
978                         struct vnode *vp = NULL;
979
980                         object = m->object;
981
982                         if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
983                                 swap_pageouts_ok = 1;
984                         } else {
985                                 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
986                                 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
987                                 vm_page_count_min(0));
988                                                                                 
989                         }
990
991                         /*
992                          * We don't bother paging objects that are "dead".  
993                          * Those objects are in a "rundown" state.
994                          */
995                         if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
996                                 vm_page_and_queue_spin_lock(m);
997                                 if (m->queue == PQ_INACTIVE) {
998                                         TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
999                                         TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1000                                 }
1001                                 vm_page_and_queue_spin_unlock(m);
1002                                 ++vm_swapcache_inactive_heuristic;
1003                                 vm_page_wakeup(m);
1004                                 continue;
1005                         }
1006
1007                         /*
1008                          * (m) is still busied.
1009                          *
1010                          * The object is already known NOT to be dead.   It
1011                          * is possible for the vget() to block the whole
1012                          * pageout daemon, but the new low-memory handling
1013                          * code should prevent it.
1014                          *
1015                          * The previous code skipped locked vnodes and, worse,
1016                          * reordered pages in the queue.  This results in
1017                          * completely non-deterministic operation because,
1018                          * quite often, a vm_fault has initiated an I/O and
1019                          * is holding a locked vnode at just the point where
1020                          * the pageout daemon is woken up.
1021                          *
1022                          * We can't wait forever for the vnode lock, we might
1023                          * deadlock due to a vn_read() getting stuck in
1024                          * vm_wait while holding this vnode.  We skip the 
1025                          * vnode if we can't get it in a reasonable amount
1026                          * of time.
1027                          *
1028                          * vpfailed is used to (try to) avoid the case where
1029                          * a large number of pages are associated with a
1030                          * locked vnode, which could cause the pageout daemon
1031                          * to stall for an excessive amount of time.
1032                          */
1033                         if (object->type == OBJT_VNODE) {
1034                                 int flags;
1035
1036                                 vp = object->handle;
1037                                 flags = LK_EXCLUSIVE | LK_NOOBJ;
1038                                 if (vp == vpfailed)
1039                                         flags |= LK_NOWAIT;
1040                                 else
1041                                         flags |= LK_TIMELOCK;
1042                                 vm_page_hold(m);
1043                                 vm_page_wakeup(m);
1044
1045                                 /*
1046                                  * We have unbusied (m) temporarily so we can
1047                                  * acquire the vp lock without deadlocking.
1048                                  * (m) is held to prevent destruction.
1049                                  */
1050                                 if (vget(vp, flags) != 0) {
1051                                         vpfailed = vp;
1052                                         ++pageout_lock_miss;
1053                                         if (object->flags & OBJ_MIGHTBEDIRTY)
1054                                                     vnodes_skipped++;
1055                                         vm_page_unhold(m);
1056                                         continue;
1057                                 }
1058
1059                                 /*
1060                                  * The page might have been moved to another
1061                                  * queue during potential blocking in vget()
1062                                  * above.  The page might have been freed and
1063                                  * reused for another vnode.  The object might
1064                                  * have been reused for another vnode.
1065                                  */
1066                                 if (m->queue != PQ_INACTIVE ||
1067                                     m->object != object ||
1068                                     object->handle != vp) {
1069                                         if (object->flags & OBJ_MIGHTBEDIRTY)
1070                                                 vnodes_skipped++;
1071                                         vput(vp);
1072                                         vm_page_unhold(m);
1073                                         continue;
1074                                 }
1075         
1076                                 /*
1077                                  * The page may have been busied during the
1078                                  * blocking in vput();  We don't move the
1079                                  * page back onto the end of the queue so that
1080                                  * statistics are more correct if we don't.
1081                                  */
1082                                 if (vm_page_busy_try(m, TRUE)) {
1083                                         vput(vp);
1084                                         vm_page_unhold(m);
1085                                         continue;
1086                                 }
1087                                 vm_page_unhold(m);
1088
1089                                 /*
1090                                  * (m) is busied again
1091                                  *
1092                                  * We own the busy bit and remove our hold
1093                                  * bit.  If the page is still held it
1094                                  * might be undergoing I/O, so skip it.
1095                                  */
1096                                 if (m->hold_count) {
1097                                         vm_page_and_queue_spin_lock(m);
1098                                         if (m->queue == PQ_INACTIVE) {
1099                                                 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1100                                                 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1101                                         }
1102                                         vm_page_and_queue_spin_unlock(m);
1103                                         ++vm_swapcache_inactive_heuristic;
1104                                         if (object->flags & OBJ_MIGHTBEDIRTY)
1105                                                 vnodes_skipped++;
1106                                         vm_page_wakeup(m);
1107                                         vput(vp);
1108                                         continue;
1109                                 }
1110                                 /* (m) is left busied as we fall through */
1111                         }
1112
1113                         /*
1114                          * page is busy and not held here.
1115                          *
1116                          * If a page is dirty, then it is either being washed
1117                          * (but not yet cleaned) or it is still in the
1118                          * laundry.  If it is still in the laundry, then we
1119                          * start the cleaning operation. 
1120                          *
1121                          * decrement inactive_shortage on success to account
1122                          * for the (future) cleaned page.  Otherwise we
1123                          * could wind up laundering or cleaning too many
1124                          * pages.
1125                          */
1126                         if (vm_pageout_clean(m) != 0) {
1127                                 --inactive_shortage;
1128                                 --maxlaunder;
1129                         }
1130                         /* clean ate busy, page no longer accessible */
1131                         if (vp != NULL)
1132                                 vput(vp);
1133                 } else {
1134                         vm_page_wakeup(m);
1135                 }
1136         }
1137         vm_page_queues_spin_lock(PQ_INACTIVE);
1138         TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq);
1139         vm_page_queues_spin_unlock(PQ_INACTIVE);
1140
1141         /*
1142          * We want to move pages from the active queue to the inactive
1143          * queue to get the inactive queue to the inactive target.  If
1144          * we still have a page shortage from above we try to directly free
1145          * clean pages instead of moving them.
1146          *
1147          * If we do still have a shortage we keep track of the number of
1148          * pages we free or cache (recycle_count) as a measure of thrashing
1149          * between the active and inactive queues.
1150          *
1151          * If we were able to completely satisfy the free+cache targets
1152          * from the inactive pool we limit the number of pages we move
1153          * from the active pool to the inactive pool to 2x the pages we
1154          * had removed from the inactive pool (with a minimum of 1/5 the
1155          * inactive target).  If we were not able to completely satisfy
1156          * the free+cache targets we go for the whole target aggressively.
1157          *
1158          * NOTE: Both variables can end up negative.
1159          * NOTE: We are still in a critical section.
1160          */
1161         active_shortage = vmstats.v_inactive_target - vmstats.v_inactive_count;
1162         if (inactive_original_shortage < vmstats.v_inactive_target / 10)
1163                 inactive_original_shortage = vmstats.v_inactive_target / 10;
1164         if (inactive_shortage <= 0 &&
1165             active_shortage > inactive_original_shortage * 2) {
1166                 active_shortage = inactive_original_shortage * 2;
1167         }
1168
1169         recycle_count = 0;
1170         marker.queue = PQ_ACTIVE;
1171
1172         vm_page_queues_spin_lock(PQ_ACTIVE);
1173         TAILQ_INSERT_HEAD(&vm_page_queues[PQ_ACTIVE].pl, &marker, pageq);
1174         vm_page_queues_spin_unlock(PQ_ACTIVE);
1175         pcount = vmstats.v_active_count;
1176
1177         while ((m = TAILQ_NEXT(&marker, pageq)) != NULL &&
1178                pcount-- > 0 && (inactive_shortage > 0 || active_shortage > 0))
1179         {
1180                 vm_page_and_queue_spin_lock(m);
1181                 if (m != TAILQ_NEXT(&marker, pageq)) {
1182                         vm_page_and_queue_spin_unlock(m);
1183                         ++pcount;
1184                         continue;
1185                 }
1186                 KKASSERT(m->queue == PQ_ACTIVE);
1187                 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl,
1188                              &marker, pageq);
1189                 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_ACTIVE].pl, m,
1190                                    &marker, pageq);
1191
1192                 /*
1193                  * Skip marker pages
1194                  */
1195                 if (m->flags & PG_MARKER) {
1196                         vm_page_and_queue_spin_unlock(m);
1197                         continue;
1198                 }
1199
1200                 /*
1201                  * Try to busy the page.  Don't mess with pages which are
1202                  * already busy or reorder them in the queue.
1203                  */
1204                 if (vm_page_busy_try(m, TRUE)) {
1205                         vm_page_and_queue_spin_unlock(m);
1206                         continue;
1207                 }
1208
1209                 /*
1210                  * Don't deactivate pages that are held, even if we can
1211                  * busy them.  (XXX why not?)
1212                  */
1213                 if (m->hold_count != 0) {
1214                         TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl,
1215                                      m, pageq);
1216                         TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl,
1217                                           m, pageq);
1218                         vm_page_and_queue_spin_unlock(m);
1219                         vm_page_wakeup(m);
1220                         continue;
1221                 }
1222                 vm_page_and_queue_spin_unlock(m);
1223
1224                 /*
1225                  * The page has been successfully busied and the page and
1226                  * queue are no longer locked.
1227                  */
1228
1229                 /*
1230                  * The count for pagedaemon pages is done after checking the
1231                  * page for eligibility...
1232                  */
1233                 mycpu->gd_cnt.v_pdpages++;
1234
1235                 /*
1236                  * Check to see "how much" the page has been used and clear
1237                  * the tracking access bits.  If the object has no references
1238                  * don't bother paying the expense.
1239                  */
1240                 actcount = 0;
1241                 if (m->object->ref_count != 0) {
1242                         if (m->flags & PG_REFERENCED)
1243                                 ++actcount;
1244                         actcount += pmap_ts_referenced(m);
1245                         if (actcount) {
1246                                 m->act_count += ACT_ADVANCE + actcount;
1247                                 if (m->act_count > ACT_MAX)
1248                                         m->act_count = ACT_MAX;
1249                         }
1250                 }
1251                 vm_page_flag_clear(m, PG_REFERENCED);
1252
1253                 /*
1254                  * actcount is only valid if the object ref_count is non-zero.
1255                  */
1256                 if (actcount && m->object->ref_count != 0) {
1257                         vm_page_and_queue_spin_lock(m);
1258                         if (m->queue == PQ_ACTIVE) {
1259                                 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl,
1260                                              m, pageq);
1261                                 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl,
1262                                                   m, pageq);
1263                         }
1264                         vm_page_and_queue_spin_unlock(m);
1265                         vm_page_wakeup(m);
1266                 } else {
1267                         m->act_count -= min(m->act_count, ACT_DECLINE);
1268                         if (vm_pageout_algorithm ||
1269                             m->object->ref_count == 0 ||
1270                             m->act_count < pass + 1
1271                         ) {
1272                                 /*
1273                                  * Deactivate the page.  If we had a
1274                                  * shortage from our inactive scan try to
1275                                  * free (cache) the page instead.
1276                                  *
1277                                  * Don't just blindly cache the page if
1278                                  * we do not have a shortage from the
1279                                  * inactive scan, that could lead to
1280                                  * gigabytes being moved.
1281                                  */
1282                                 --active_shortage;
1283                                 if (inactive_shortage > 0 ||
1284                                     m->object->ref_count == 0) {
1285                                         if (inactive_shortage > 0)
1286                                                 ++recycle_count;
1287                                         vm_page_protect(m, VM_PROT_NONE);
1288                                         if (m->dirty == 0 &&
1289                                             inactive_shortage > 0) {
1290                                                 --inactive_shortage;
1291                                                 vm_page_cache(m);
1292                                         } else {
1293                                                 vm_page_deactivate(m);
1294                                                 vm_page_wakeup(m);
1295                                         }
1296                                 } else {
1297                                         vm_page_deactivate(m);
1298                                         vm_page_wakeup(m);
1299                                 }
1300                         } else {
1301                                 vm_page_and_queue_spin_lock(m);
1302                                 if (m->queue == PQ_ACTIVE) {
1303                                         TAILQ_REMOVE(
1304                                                 &vm_page_queues[PQ_ACTIVE].pl,
1305                                                 m, pageq);
1306                                         TAILQ_INSERT_TAIL(
1307                                                 &vm_page_queues[PQ_ACTIVE].pl,
1308                                                 m, pageq);
1309                                 }
1310                                 vm_page_and_queue_spin_unlock(m);
1311                                 vm_page_wakeup(m);
1312                         }
1313                 }
1314         }
1315
1316         /*
1317          * Clean out our local marker.
1318          */
1319         vm_page_queues_spin_lock(PQ_ACTIVE);
1320         TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, &marker, pageq);
1321         vm_page_queues_spin_unlock(PQ_ACTIVE);
1322
1323         /*
1324          * The number of actually free pages can drop down to v_free_reserved,
1325          * we try to build the free count back above v_free_min.  Note that
1326          * vm_paging_needed() also returns TRUE if v_free_count is not at
1327          * least v_free_min so that is the minimum we must build the free
1328          * count to.
1329          *
1330          * We use a slightly higher target to improve hysteresis,
1331          * ((v_free_target + v_free_min) / 2).  Since v_free_target
1332          * is usually the same as v_cache_min this maintains about
1333          * half the pages in the free queue as are in the cache queue,
1334          * providing pretty good pipelining for pageout operation.
1335          *
1336          * The system operator can manipulate vm.v_cache_min and
1337          * vm.v_free_target to tune the pageout demon.  Be sure
1338          * to keep vm.v_free_min < vm.v_free_target.
1339          *
1340          * Note that the original paging target is to get at least
1341          * (free_min + cache_min) into (free + cache).  The slightly
1342          * higher target will shift additional pages from cache to free
1343          * without effecting the original paging target in order to
1344          * maintain better hysteresis and not have the free count always
1345          * be dead-on v_free_min.
1346          *
1347          * NOTE: we are still in a critical section.
1348          *
1349          * Pages moved from PQ_CACHE to totally free are not counted in the
1350          * pages_freed counter.
1351          */
1352         while (vmstats.v_free_count <
1353                (vmstats.v_free_min + vmstats.v_free_target) / 2) {
1354                 /*
1355                  * This steals some code from vm/vm_page.c
1356                  */
1357                 static int cache_rover = 0;
1358
1359                 m = vm_page_list_find(PQ_CACHE, cache_rover & PQ_L2_MASK, FALSE);
1360                 if (m == NULL)
1361                         break;
1362                 /* page is returned removed from its queue and spinlocked */
1363                 if (vm_page_busy_try(m, TRUE)) {
1364                         vm_page_deactivate_locked(m);
1365                         vm_page_spin_unlock(m);
1366 #ifdef INVARIANTS
1367                         kprintf("Warning: busy page %p found in cache\n", m);
1368 #endif
1369                         continue;
1370                 }
1371                 vm_page_spin_unlock(m);
1372                 pagedaemon_wakeup();
1373
1374                 /*
1375                  * Page has been successfully busied and it and its queue
1376                  * is no longer spinlocked.
1377                  */
1378                 if ((m->flags & PG_UNMANAGED) ||
1379                     m->hold_count ||
1380                     m->wire_count) {
1381                         vm_page_deactivate(m);
1382                         vm_page_wakeup(m);
1383                         continue;
1384                 }
1385                 KKASSERT((m->flags & PG_MAPPED) == 0);
1386                 KKASSERT(m->dirty == 0);
1387                 cache_rover += PQ_PRIME2;
1388                 vm_pageout_page_free(m);
1389                 mycpu->gd_cnt.v_dfree++;
1390         }
1391
1392 #if !defined(NO_SWAPPING)
1393         /*
1394          * Idle process swapout -- run once per second.
1395          */
1396         if (vm_swap_idle_enabled) {
1397                 static long lsec;
1398                 if (time_second != lsec) {
1399                         vm_pageout_req_swapout |= VM_SWAP_IDLE;
1400                         vm_req_vmdaemon();
1401                         lsec = time_second;
1402                 }
1403         }
1404 #endif
1405                 
1406         /*
1407          * If we didn't get enough free pages, and we have skipped a vnode
1408          * in a writeable object, wakeup the sync daemon.  And kick swapout
1409          * if we did not get enough free pages.
1410          */
1411         if (vm_paging_target() > 0) {
1412                 if (vnodes_skipped && vm_page_count_min(0))
1413                         speedup_syncer();
1414 #if !defined(NO_SWAPPING)
1415                 if (vm_swap_enabled && vm_page_count_target()) {
1416                         vm_req_vmdaemon();
1417                         vm_pageout_req_swapout |= VM_SWAP_NORMAL;
1418                 }
1419 #endif
1420         }
1421
1422         /*
1423          * Handle catastrophic conditions.  Under good conditions we should
1424          * be at the target, well beyond our minimum.  If we could not even
1425          * reach our minimum the system is under heavy stress.
1426          *
1427          * Determine whether we have run out of memory.  This occurs when
1428          * swap_pager_full is TRUE and the only pages left in the page
1429          * queues are dirty.  We will still likely have page shortages.
1430          *
1431          * - swap_pager_full is set if insufficient swap was
1432          *   available to satisfy a requested pageout.
1433          *
1434          * - the inactive queue is bloated (4 x size of active queue),
1435          *   meaning it is unable to get rid of dirty pages and.
1436          *
1437          * - vm_page_count_min() without counting pages recycled from the
1438          *   active queue (recycle_count) means we could not recover
1439          *   enough pages to meet bare minimum needs.  This test only
1440          *   works if the inactive queue is bloated.
1441          *
1442          * - due to a positive inactive_shortage we shifted the remaining
1443          *   dirty pages from the active queue to the inactive queue
1444          *   trying to find clean ones to free.
1445          */
1446         if (swap_pager_full && vm_page_count_min(recycle_count))
1447                 kprintf("Warning: system low on memory+swap!\n");
1448         if (swap_pager_full && vm_page_count_min(recycle_count) &&
1449             vmstats.v_inactive_count > vmstats.v_active_count * 4 &&
1450             inactive_shortage > 0) {
1451                 /*
1452                  * Kill something.
1453                  */
1454                 info.bigproc = NULL;
1455                 info.bigsize = 0;
1456                 allproc_scan(vm_pageout_scan_callback, &info);
1457                 if (info.bigproc != NULL) {
1458                         killproc(info.bigproc, "out of swap space");
1459                         info.bigproc->p_nice = PRIO_MIN;
1460                         info.bigproc->p_usched->resetpriority(
1461                                 FIRST_LWP_IN_PROC(info.bigproc));
1462                         wakeup(&vmstats.v_free_count);
1463                         PRELE(info.bigproc);
1464                 }
1465         }
1466         return(inactive_shortage);
1467 }
1468
1469 /*
1470  * The caller must hold proc_token.
1471  */
1472 static int
1473 vm_pageout_scan_callback(struct proc *p, void *data)
1474 {
1475         struct vm_pageout_scan_info *info = data;
1476         vm_offset_t size;
1477
1478         /*
1479          * Never kill system processes or init.  If we have configured swap
1480          * then try to avoid killing low-numbered pids.
1481          */
1482         if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
1483             ((p->p_pid < 48) && (vm_swap_size != 0))) {
1484                 return (0);
1485         }
1486
1487         /*
1488          * if the process is in a non-running type state,
1489          * don't touch it.
1490          */
1491         if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
1492                 return (0);
1493
1494         /*
1495          * Get the approximate process size.  Note that anonymous pages
1496          * with backing swap will be counted twice, but there should not
1497          * be too many such pages due to the stress the VM system is
1498          * under at this point.
1499          */
1500         size = vmspace_anonymous_count(p->p_vmspace) +
1501                 vmspace_swap_count(p->p_vmspace);
1502
1503         /*
1504          * If the this process is bigger than the biggest one
1505          * remember it.
1506          */
1507         if (info->bigsize < size) {
1508                 if (info->bigproc)
1509                         PRELE(info->bigproc);
1510                 PHOLD(p);
1511                 info->bigproc = p;
1512                 info->bigsize = size;
1513         }
1514         return(0);
1515 }
1516
1517 /*
1518  * This routine tries to maintain the pseudo LRU active queue,
1519  * so that during long periods of time where there is no paging,
1520  * that some statistic accumulation still occurs.  This code
1521  * helps the situation where paging just starts to occur.
1522  */
1523 static void
1524 vm_pageout_page_stats(void)
1525 {
1526         static int fullintervalcount = 0;
1527         struct vm_page marker;
1528         vm_page_t m;
1529         int pcount, tpcount;            /* Number of pages to check */
1530         int page_shortage;
1531
1532         page_shortage = (vmstats.v_inactive_target + vmstats.v_cache_max +
1533                          vmstats.v_free_min) -
1534                         (vmstats.v_free_count + vmstats.v_inactive_count +
1535                          vmstats.v_cache_count);
1536
1537         if (page_shortage <= 0)
1538                 return;
1539
1540         pcount = vmstats.v_active_count;
1541         fullintervalcount += vm_pageout_stats_interval;
1542         if (fullintervalcount < vm_pageout_full_stats_interval) {
1543                 tpcount = (vm_pageout_stats_max * vmstats.v_active_count) /
1544                           vmstats.v_page_count;
1545                 if (pcount > tpcount)
1546                         pcount = tpcount;
1547         } else {
1548                 fullintervalcount = 0;
1549         }
1550
1551         bzero(&marker, sizeof(marker));
1552         marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
1553         marker.queue = PQ_ACTIVE;
1554         marker.wire_count = 1;
1555
1556         vm_page_queues_spin_lock(PQ_ACTIVE);
1557         TAILQ_INSERT_HEAD(&vm_page_queues[PQ_ACTIVE].pl, &marker, pageq);
1558         vm_page_queues_spin_unlock(PQ_ACTIVE);
1559
1560         while ((m = TAILQ_NEXT(&marker, pageq)) != NULL &&
1561                pcount-- > 0)
1562         {
1563                 int actcount;
1564
1565                 vm_page_and_queue_spin_lock(m);
1566                 if (m != TAILQ_NEXT(&marker, pageq)) {
1567                         vm_page_and_queue_spin_unlock(m);
1568                         ++pcount;
1569                         continue;
1570                 }
1571                 KKASSERT(m->queue == PQ_ACTIVE);
1572                 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, &marker, pageq);
1573                 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_ACTIVE].pl, m,
1574                                    &marker, pageq);
1575
1576                 /*
1577                  * Ignore markers
1578                  */
1579                 if (m->flags & PG_MARKER) {
1580                         vm_page_and_queue_spin_unlock(m);
1581                         continue;
1582                 }
1583
1584                 /*
1585                  * Ignore pages we can't busy
1586                  */
1587                 if (vm_page_busy_try(m, TRUE)) {
1588                         vm_page_and_queue_spin_unlock(m);
1589                         continue;
1590                 }
1591                 vm_page_and_queue_spin_unlock(m);
1592                 KKASSERT(m->queue == PQ_ACTIVE);
1593
1594                 /*
1595                  * We now have a safely busied page, the page and queue
1596                  * spinlocks have been released.
1597                  *
1598                  * Ignore held pages
1599                  */
1600                 if (m->hold_count) {
1601                         vm_page_wakeup(m);
1602                         continue;
1603                 }
1604
1605                 /*
1606                  * Calculate activity
1607                  */
1608                 actcount = 0;
1609                 if (m->flags & PG_REFERENCED) {
1610                         vm_page_flag_clear(m, PG_REFERENCED);
1611                         actcount += 1;
1612                 }
1613                 actcount += pmap_ts_referenced(m);
1614
1615                 /*
1616                  * Update act_count and move page to end of queue.
1617                  */
1618                 if (actcount) {
1619                         m->act_count += ACT_ADVANCE + actcount;
1620                         if (m->act_count > ACT_MAX)
1621                                 m->act_count = ACT_MAX;
1622                         vm_page_and_queue_spin_lock(m);
1623                         if (m->queue == PQ_ACTIVE) {
1624                                 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl,
1625                                              m, pageq);
1626                                 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl,
1627                                                   m, pageq);
1628                         }
1629                         vm_page_and_queue_spin_unlock(m);
1630                         vm_page_wakeup(m);
1631                         continue;
1632                 }
1633
1634                 if (m->act_count == 0) {
1635                         /*
1636                          * We turn off page access, so that we have
1637                          * more accurate RSS stats.  We don't do this
1638                          * in the normal page deactivation when the
1639                          * system is loaded VM wise, because the
1640                          * cost of the large number of page protect
1641                          * operations would be higher than the value
1642                          * of doing the operation.
1643                          *
1644                          * We use the marker to save our place so
1645                          * we can release the spin lock.  both (m)
1646                          * and (next) will be invalid.
1647                          */
1648                         vm_page_protect(m, VM_PROT_NONE);
1649                         vm_page_deactivate(m);
1650                 } else {
1651                         m->act_count -= min(m->act_count, ACT_DECLINE);
1652                         vm_page_and_queue_spin_lock(m);
1653                         if (m->queue == PQ_ACTIVE) {
1654                                 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl,
1655                                              m, pageq);
1656                                 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl,
1657                                                   m, pageq);
1658                         }
1659                         vm_page_and_queue_spin_unlock(m);
1660                 }
1661                 vm_page_wakeup(m);
1662         }
1663
1664         /*
1665          * Remove our local marker
1666          */
1667         vm_page_queues_spin_lock(PQ_ACTIVE);
1668         TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, &marker, pageq);
1669         vm_page_queues_spin_unlock(PQ_ACTIVE);
1670
1671 }
1672
1673 static int
1674 vm_pageout_free_page_calc(vm_size_t count)
1675 {
1676         if (count < vmstats.v_page_count)
1677                  return 0;
1678         /*
1679          * free_reserved needs to include enough for the largest swap pager
1680          * structures plus enough for any pv_entry structs when paging.
1681          *
1682          * v_free_min           normal allocations
1683          * v_free_reserved      system allocations
1684          * v_pageout_free_min   allocations by pageout daemon
1685          * v_interrupt_free_min low level allocations (e.g swap structures)
1686          */
1687         if (vmstats.v_page_count > 1024)
1688                 vmstats.v_free_min = 64 + (vmstats.v_page_count - 1024) / 200;
1689         else
1690                 vmstats.v_free_min = 64;
1691         vmstats.v_free_reserved = vmstats.v_free_min * 4 / 8 + 7;
1692         vmstats.v_free_severe = vmstats.v_free_min * 4 / 8 + 0;
1693         vmstats.v_pageout_free_min = vmstats.v_free_min * 2 / 8 + 7;
1694         vmstats.v_interrupt_free_min = vmstats.v_free_min * 1 / 8 + 7;
1695
1696         return 1;
1697 }
1698
1699
1700 /*
1701  * vm_pageout is the high level pageout daemon.
1702  *
1703  * No requirements.
1704  */
1705 static void
1706 vm_pageout_thread(void)
1707 {
1708         int pass;
1709         int inactive_shortage;
1710
1711         /*
1712          * Initialize some paging parameters.
1713          */
1714         curthread->td_flags |= TDF_SYSTHREAD;
1715
1716         if (vmstats.v_page_count < 2000)
1717                 vm_pageout_page_count = 8;
1718
1719         vm_pageout_free_page_calc(vmstats.v_page_count);
1720
1721         /*
1722          * v_free_target and v_cache_min control pageout hysteresis.  Note
1723          * that these are more a measure of the VM cache queue hysteresis
1724          * then the VM free queue.  Specifically, v_free_target is the
1725          * high water mark (free+cache pages).
1726          *
1727          * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1728          * low water mark, while v_free_min is the stop.  v_cache_min must
1729          * be big enough to handle memory needs while the pageout daemon
1730          * is signalled and run to free more pages.
1731          */
1732         if (vmstats.v_free_count > 6144)
1733                 vmstats.v_free_target = 4 * vmstats.v_free_min + vmstats.v_free_reserved;
1734         else
1735                 vmstats.v_free_target = 2 * vmstats.v_free_min + vmstats.v_free_reserved;
1736
1737         /*
1738          * NOTE: With the new buffer cache b_act_count we want the default
1739          *       inactive target to be a percentage of available memory.
1740          *
1741          *       The inactive target essentially determines the minimum
1742          *       number of 'temporary' pages capable of caching one-time-use
1743          *       files when the VM system is otherwise full of pages
1744          *       belonging to multi-time-use files or active program data.
1745          *
1746          * NOTE: The inactive target is aggressively persued only if the
1747          *       inactive queue becomes too small.  If the inactive queue
1748          *       is large enough to satisfy page movement to free+cache
1749          *       then it is repopulated more slowly from the active queue.
1750          *       This allows a general inactive_target default to be set.
1751          *
1752          *       There is an issue here for processes which sit mostly idle
1753          *       'overnight', such as sshd, tcsh, and X.  Any movement from
1754          *       the active queue will eventually cause such pages to
1755          *       recycle eventually causing a lot of paging in the morning.
1756          *       To reduce the incidence of this pages cycled out of the
1757          *       buffer cache are moved directly to the inactive queue if
1758          *       they were only used once or twice.
1759          *
1760          *       The vfs.vm_cycle_point sysctl can be used to adjust this.
1761          *       Increasing the value (up to 64) increases the number of
1762          *       buffer recyclements which go directly to the inactive queue.
1763          */
1764         if (vmstats.v_free_count > 2048) {
1765                 vmstats.v_cache_min = vmstats.v_free_target;
1766                 vmstats.v_cache_max = 2 * vmstats.v_cache_min;
1767         } else {
1768                 vmstats.v_cache_min = 0;
1769                 vmstats.v_cache_max = 0;
1770         }
1771         vmstats.v_inactive_target = vmstats.v_free_count / 4;
1772
1773         /* XXX does not really belong here */
1774         if (vm_page_max_wired == 0)
1775                 vm_page_max_wired = vmstats.v_free_count / 3;
1776
1777         if (vm_pageout_stats_max == 0)
1778                 vm_pageout_stats_max = vmstats.v_free_target;
1779
1780         /*
1781          * Set interval in seconds for stats scan.
1782          */
1783         if (vm_pageout_stats_interval == 0)
1784                 vm_pageout_stats_interval = 5;
1785         if (vm_pageout_full_stats_interval == 0)
1786                 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1787         
1788
1789         /*
1790          * Set maximum free per pass
1791          */
1792         if (vm_pageout_stats_free_max == 0)
1793                 vm_pageout_stats_free_max = 5;
1794
1795         swap_pager_swap_init();
1796         pass = 0;
1797
1798         /*
1799          * The pageout daemon is never done, so loop forever.
1800          */
1801         while (TRUE) {
1802                 int error;
1803
1804                 /*
1805                  * Wait for an action request.  If we timeout check to
1806                  * see if paging is needed (in case the normal wakeup
1807                  * code raced us).
1808                  */
1809                 if (vm_pages_needed == 0) {
1810                         error = tsleep(&vm_pages_needed,
1811                                        0, "psleep",
1812                                        vm_pageout_stats_interval * hz);
1813                         if (error &&
1814                             vm_paging_needed() == 0 &&
1815                             vm_pages_needed == 0) {
1816                                 vm_pageout_page_stats();
1817                                 continue;
1818                         }
1819                         vm_pages_needed = 1;
1820                 }
1821
1822                 mycpu->gd_cnt.v_pdwakeups++;
1823
1824                 /*
1825                  * Scan for pageout.  Try to avoid thrashing the system
1826                  * with activity.
1827                  */
1828                 inactive_shortage = vm_pageout_scan(pass);
1829                 if (inactive_shortage > 0) {
1830                         ++pass;
1831                         if (swap_pager_full) {
1832                                 /*
1833                                  * Running out of memory, catastrophic back-off
1834                                  * to one-second intervals.
1835                                  */
1836                                 tsleep(&vm_pages_needed, 0, "pdelay", hz);
1837                         } else if (pass < 10 && vm_pages_needed > 1) {
1838                                 /*
1839                                  * Normal operation, additional processes
1840                                  * have already kicked us.  Retry immediately.
1841                                  */
1842                         } else if (pass < 10) {
1843                                 /*
1844                                  * Normal operation, fewer processes.  Delay
1845                                  * a bit but allow wakeups.
1846                                  */
1847                                 vm_pages_needed = 0;
1848                                 tsleep(&vm_pages_needed, 0, "pdelay", hz / 10);
1849                                 vm_pages_needed = 1;
1850                         } else {
1851                                 /*
1852                                  * We've taken too many passes, forced delay.
1853                                  */
1854                                 tsleep(&vm_pages_needed, 0, "pdelay", hz / 10);
1855                         }
1856                 } else {
1857                         /*
1858                          * Interlocked wakeup of waiters (non-optional)
1859                          */
1860                         pass = 0;
1861                         if (vm_pages_needed && !vm_page_count_min(0)) {
1862                                 wakeup(&vmstats.v_free_count);
1863                                 vm_pages_needed = 0;
1864                         }
1865                 }
1866         }
1867 }
1868
1869 static struct kproc_desc page_kp = {
1870         "pagedaemon",
1871         vm_pageout_thread,
1872         &pagethread
1873 };
1874 SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
1875
1876
1877 /*
1878  * Called after allocating a page out of the cache or free queue
1879  * to possibly wake the pagedaemon up to replentish our supply.
1880  *
1881  * We try to generate some hysteresis by waking the pagedaemon up
1882  * when our free+cache pages go below the free_min+cache_min level.
1883  * The pagedaemon tries to get the count back up to at least the
1884  * minimum, and through to the target level if possible.
1885  *
1886  * If the pagedaemon is already active bump vm_pages_needed as a hint
1887  * that there are even more requests pending.
1888  *
1889  * SMP races ok?
1890  * No requirements.
1891  */
1892 void
1893 pagedaemon_wakeup(void)
1894 {
1895         if (vm_paging_needed() && curthread != pagethread) {
1896                 if (vm_pages_needed == 0) {
1897                         vm_pages_needed = 1;    /* SMP race ok */
1898                         wakeup(&vm_pages_needed);
1899                 } else if (vm_page_count_min(0)) {
1900                         ++vm_pages_needed;      /* SMP race ok */
1901                 }
1902         }
1903 }
1904
1905 #if !defined(NO_SWAPPING)
1906
1907 /*
1908  * SMP races ok?
1909  * No requirements.
1910  */
1911 static void
1912 vm_req_vmdaemon(void)
1913 {
1914         static int lastrun = 0;
1915
1916         if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
1917                 wakeup(&vm_daemon_needed);
1918                 lastrun = ticks;
1919         }
1920 }
1921
1922 static int vm_daemon_callback(struct proc *p, void *data __unused);
1923
1924 /*
1925  * No requirements.
1926  */
1927 static void
1928 vm_daemon(void)
1929 {
1930         /*
1931          * XXX vm_daemon_needed specific token?
1932          */
1933         while (TRUE) {
1934                 tsleep(&vm_daemon_needed, 0, "psleep", 0);
1935                 if (vm_pageout_req_swapout) {
1936                         swapout_procs(vm_pageout_req_swapout);
1937                         vm_pageout_req_swapout = 0;
1938                 }
1939                 /*
1940                  * scan the processes for exceeding their rlimits or if
1941                  * process is swapped out -- deactivate pages
1942                  */
1943                 allproc_scan(vm_daemon_callback, NULL);
1944         }
1945 }
1946
1947 /*
1948  * Caller must hold proc_token.
1949  */
1950 static int
1951 vm_daemon_callback(struct proc *p, void *data __unused)
1952 {
1953         vm_pindex_t limit, size;
1954
1955         /*
1956          * if this is a system process or if we have already
1957          * looked at this process, skip it.
1958          */
1959         if (p->p_flag & (P_SYSTEM | P_WEXIT))
1960                 return (0);
1961
1962         /*
1963          * if the process is in a non-running type state,
1964          * don't touch it.
1965          */
1966         if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
1967                 return (0);
1968
1969         /*
1970          * get a limit
1971          */
1972         limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
1973                                 p->p_rlimit[RLIMIT_RSS].rlim_max));
1974
1975         /*
1976          * let processes that are swapped out really be
1977          * swapped out.  Set the limit to nothing to get as
1978          * many pages out to swap as possible.
1979          */
1980         if (p->p_flag & P_SWAPPEDOUT)
1981                 limit = 0;
1982
1983         lwkt_gettoken(&p->p_vmspace->vm_map.token);
1984         size = vmspace_resident_count(p->p_vmspace);
1985         if (limit >= 0 && size >= limit) {
1986                 vm_pageout_map_deactivate_pages(&p->p_vmspace->vm_map, limit);
1987         }
1988         lwkt_reltoken(&p->p_vmspace->vm_map.token);
1989         return (0);
1990 }
1991
1992 #endif