1 /* $NetBSD: uvm_pdaemon.c,v 1.99 2009/08/18 02:43:49 yamt Exp $ */
4 * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 * Copyright (c) 1991, 1993, The Regents of the University of California.
9 * This code is derived from software contributed to Berkeley by
10 * The Mach Operating System project at Carnegie-Mellon University.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Charles D. Cranor,
23 * Washington University, the University of California, Berkeley and
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * @(#)vm_pageout.c 8.5 (Berkeley) 2/14/94
42 * from: Id: uvm_pdaemon.c,v 1.1.2.32 1998/02/06 05:26:30 chs Exp
45 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46 * All rights reserved.
48 * Permission to use, copy, modify and distribute this software and
49 * its documentation is hereby granted, provided that both the copyright
50 * notice and this permission notice appear in all copies of the
51 * software, derivative works or modified versions, and any portions
52 * thereof, and that both notices appear in supporting documentation.
54 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
55 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
56 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58 * Carnegie Mellon requests users of this software to return to
60 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
61 * School of Computer Science
62 * Carnegie Mellon University
63 * Pittsburgh PA 15213-3890
65 * any improvements or extensions that they make and grant Carnegie the
66 * rights to redistribute these changes.
70 * uvm_pdaemon.c: the page daemon
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: uvm_pdaemon.c,v 1.99 2009/08/18 02:43:49 yamt Exp $");
76 #include "opt_uvmhist.h"
77 #include "opt_readahead.h"
79 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
85 #include <sys/module.h>
86 #include <sys/atomic.h>
89 #include <uvm/uvm_pdpolicy.h>
92 * UVMPD_NUMDIRTYREACTS is how many dirty pages the pagedaemon will reactivate
93 * in a pass thru the inactive list when swap is full. the value should be
94 * "small"... if it's too large we'll cycle the active pages thru the inactive
95 * queue too quickly to for them to be referenced and avoid being freed.
98 #define UVMPD_NUMDIRTYREACTS 16
100 #define UVMPD_NUMTRYLOCKOWNER 16
106 static void uvmpd_scan(void);
107 static void uvmpd_scan_queue(void);
108 static void uvmpd_tune(void);
110 unsigned int uvm_pagedaemon_waiters
;
113 * XXX hack to avoid hangs when large processes fork.
115 u_int uvm_extrapages
;
117 static kmutex_t uvm_reclaim_lock
;
119 SLIST_HEAD(uvm_reclaim_hooks
, uvm_reclaim_hook
) uvm_reclaim_list
;
122 * uvm_wait: wait (sleep) for the page daemon to free some pages
124 * => should be called with all locks released
125 * => should _not_ be called by the page daemon (to avoid deadlock)
129 uvm_wait(const char *wmsg
)
133 mutex_spin_enter(&uvm_fpageqlock
);
136 * check for page daemon going to sleep (waiting for itself)
139 if (curlwp
== uvm
.pagedaemon_lwp
&& uvmexp
.paging
== 0) {
141 * now we have a problem: the pagedaemon wants to go to
142 * sleep until it frees more memory. but how can it
143 * free more memory if it is asleep? that is a deadlock.
144 * we have two options:
146 * [2] put a timeout on the sleep, thus causing the
147 * pagedaemon to only pause (rather than sleep forever)
149 * note that option [2] will only help us if we get lucky
150 * and some other process on the system breaks the deadlock
151 * by exiting or freeing memory (thus allowing the pagedaemon
152 * to continue). for now we panic if DEBUG is defined,
153 * otherwise we hope for the best with option [2] (better
154 * yet, this should never happen in the first place!).
157 printf("pagedaemon: deadlock detected!\n");
158 timo
= hz
>> 3; /* set timeout */
160 /* DEBUG: panic so we can debug it */
161 panic("pagedaemon deadlock");
165 uvm_pagedaemon_waiters
++;
166 wakeup(&uvm
.pagedaemon
); /* wake the daemon! */
167 UVM_UNLOCK_AND_WAIT(&uvmexp
.free
, &uvm_fpageqlock
, false, wmsg
, timo
);
171 * uvm_kick_pdaemon: perform checks to determine if we need to
172 * give the pagedaemon a nudge, and do so if necessary.
174 * => called with uvm_fpageqlock held.
178 uvm_kick_pdaemon(void)
181 KASSERT(mutex_owned(&uvm_fpageqlock
));
183 if (uvmexp
.free
+ uvmexp
.paging
< uvmexp
.freemin
||
184 (uvmexp
.free
+ uvmexp
.paging
< uvmexp
.freetarg
&&
185 uvmpdpol_needsscan_p())) {
186 wakeup(&uvm
.pagedaemon
);
191 * uvmpd_tune: tune paging parameters
193 * => called when ever memory is added (or removed?) to the system
194 * => caller must call with page queues locked
202 UVMHIST_FUNC("uvmpd_tune"); UVMHIST_CALLED(pdhist
);
205 * try to keep 0.5% of available RAM free, but limit to between
206 * 128k and 1024k per-CPU. XXX: what are these values good for?
208 val
= uvmexp
.npages
/ 200;
209 val
= MAX(val
, (128*1024) >> PAGE_SHIFT
);
210 val
= MIN(val
, (1024*1024) >> PAGE_SHIFT
);
213 /* Make sure there's always a user page free. */
214 if (val
< uvmexp
.reserve_kernel
+ 1)
215 val
= uvmexp
.reserve_kernel
+ 1;
216 uvmexp
.freemin
= val
;
218 /* Calculate free target. */
219 val
= (uvmexp
.freemin
* 4) / 3;
220 if (val
<= uvmexp
.freemin
)
221 val
= uvmexp
.freemin
+ 1;
222 uvmexp
.freetarg
= val
+ atomic_swap_uint(&uvm_extrapages
, 0);
224 uvmexp
.wiredmax
= uvmexp
.npages
/ 3;
225 UVMHIST_LOG(pdhist
, "<- done, freemin=%d, freetarg=%d, wiredmax=%d",
226 uvmexp
.freemin
, uvmexp
.freetarg
, uvmexp
.wiredmax
, 0);
230 * uvm_pageout: the main loop for the pagedaemon
234 uvm_pageout(void *arg
)
236 int bufcnt
, npages
= 0;
240 struct uvm_reclaim_hook
*hook
;
242 UVMHIST_FUNC("uvm_pageout"); UVMHIST_CALLED(pdhist
);
244 UVMHIST_LOG(pdhist
,"<starting uvm pagedaemon>", 0, 0, 0, 0);
247 * ensure correct priority and set paging parameters...
250 uvm
.pagedaemon_lwp
= curlwp
;
251 mutex_enter(&uvm_pageqlock
);
252 npages
= uvmexp
.npages
;
254 mutex_exit(&uvm_pageqlock
);
261 bool needsscan
, needsfree
;
263 mutex_spin_enter(&uvm_fpageqlock
);
264 if (uvm_pagedaemon_waiters
== 0 || uvmexp
.paging
> 0) {
265 UVMHIST_LOG(pdhist
," <<SLEEPING>>",0,0,0,0);
266 UVM_UNLOCK_AND_WAIT(&uvm
.pagedaemon
,
267 &uvm_fpageqlock
, false, "pgdaemon", 0);
269 UVMHIST_LOG(pdhist
," <<WOKE UP>>",0,0,0,0);
271 mutex_spin_exit(&uvm_fpageqlock
);
275 * now lock page queues and recompute inactive count
278 mutex_enter(&uvm_pageqlock
);
279 if (npages
!= uvmexp
.npages
|| extrapages
!= uvm_extrapages
) {
280 npages
= uvmexp
.npages
;
281 extrapages
= uvm_extrapages
;
282 mutex_spin_enter(&uvm_fpageqlock
);
284 mutex_spin_exit(&uvm_fpageqlock
);
290 * Estimate a hint. Note that bufmem are returned to
291 * system only when entire pool page is empty.
293 mutex_spin_enter(&uvm_fpageqlock
);
294 bufcnt
= uvmexp
.freetarg
- uvmexp
.free
;
298 UVMHIST_LOG(pdhist
," free/ftarg=%d/%d",
299 uvmexp
.free
, uvmexp
.freetarg
, 0,0);
301 needsfree
= uvmexp
.free
+ uvmexp
.paging
< uvmexp
.freetarg
;
302 needsscan
= needsfree
|| uvmpdpol_needsscan_p();
308 mutex_spin_exit(&uvm_fpageqlock
);
310 mutex_spin_enter(&uvm_fpageqlock
);
314 * if there's any free memory to be had,
315 * wake up any waiters.
317 if (uvmexp
.free
> uvmexp
.reserve_kernel
||
318 uvmexp
.paging
== 0) {
319 wakeup(&uvmexp
.free
);
320 uvm_pagedaemon_waiters
= 0;
322 mutex_spin_exit(&uvm_fpageqlock
);
325 * scan done. unlock page queues (the only lock we are holding)
327 mutex_exit(&uvm_pageqlock
);
330 * if we don't need free memory, we're done.
337 * start draining pool resources now that we're not
340 pool_drain_start(&pp
, &where
);
343 * kill unused metadata buffers.
345 mutex_enter(&bufcache_lock
);
346 buf_drain(bufcnt
<< PAGE_SHIFT
);
347 mutex_exit(&bufcache_lock
);
349 mutex_enter(&uvm_reclaim_lock
);
350 SLIST_FOREACH(hook
, &uvm_reclaim_list
, uvm_reclaim_next
) {
351 (*hook
->uvm_reclaim_hook
)();
353 mutex_exit(&uvm_reclaim_lock
);
356 * complete draining the pools.
358 pool_drain_end(pp
, where
);
365 * uvm_aiodone_worker: a workqueue callback for the aiodone daemon.
369 uvm_aiodone_worker(struct work
*wk
, void *dummy
)
371 struct buf
*bp
= (void *)wk
;
373 KASSERT(&bp
->b_work
== wk
);
376 * process an i/o that's done.
383 uvm_pageout_start(int npages
)
386 mutex_spin_enter(&uvm_fpageqlock
);
387 uvmexp
.paging
+= npages
;
388 mutex_spin_exit(&uvm_fpageqlock
);
392 uvm_pageout_done(int npages
)
395 mutex_spin_enter(&uvm_fpageqlock
);
396 KASSERT(uvmexp
.paging
>= npages
);
397 uvmexp
.paging
-= npages
;
400 * wake up either of pagedaemon or LWPs waiting for it.
403 if (uvmexp
.free
<= uvmexp
.reserve_kernel
) {
404 wakeup(&uvm
.pagedaemon
);
406 wakeup(&uvmexp
.free
);
407 uvm_pagedaemon_waiters
= 0;
409 mutex_spin_exit(&uvm_fpageqlock
);
413 * uvmpd_trylockowner: trylock the page's owner.
415 * => called with pageq locked.
416 * => resolve orphaned O->A loaned page.
417 * => return the locked mutex on success. otherwise, return NULL.
421 uvmpd_trylockowner(struct vm_page
*pg
)
423 struct uvm_object
*uobj
= pg
->uobject
;
426 KASSERT(mutex_owned(&uvm_pageqlock
));
429 slock
= &uobj
->vmobjlock
;
431 struct vm_anon
*anon
= pg
->uanon
;
433 KASSERT(anon
!= NULL
);
434 slock
= &anon
->an_lock
;
437 if (!mutex_tryenter(slock
)) {
444 * set PQ_ANON if it isn't set already.
447 if ((pg
->pqflags
& PQ_ANON
) == 0) {
448 KASSERT(pg
->loan_count
> 0);
450 pg
->pqflags
|= PQ_ANON
;
451 /* anon now owns it */
463 struct vm_page
*swc_pages
[howmany(MAXPHYS
, MIN_PAGE_SIZE
)];
467 swapcluster_init(struct swapcluster
*swc
)
475 swapcluster_allocslots(struct swapcluster
*swc
)
480 if (swc
->swc_slot
!= 0) {
484 /* Even with strange MAXPHYS, the shift
485 implicitly rounds down to a page. */
486 npages
= MAXPHYS
>> PAGE_SHIFT
;
487 slot
= uvm_swap_alloc(&npages
, true);
491 swc
->swc_slot
= slot
;
492 swc
->swc_nallocated
= npages
;
499 swapcluster_add(struct swapcluster
*swc
, struct vm_page
*pg
)
502 struct uvm_object
*uobj
;
504 KASSERT(swc
->swc_slot
!= 0);
505 KASSERT(swc
->swc_nused
< swc
->swc_nallocated
);
506 KASSERT((pg
->pqflags
& PQ_SWAPBACKED
) != 0);
508 slot
= swc
->swc_slot
+ swc
->swc_nused
;
511 KASSERT(mutex_owned(&pg
->uanon
->an_lock
));
512 pg
->uanon
->an_swslot
= slot
;
516 KASSERT(mutex_owned(&uobj
->vmobjlock
));
517 result
= uao_set_swslot(uobj
, pg
->offset
>> PAGE_SHIFT
, slot
);
522 swc
->swc_pages
[swc
->swc_nused
] = pg
;
529 swapcluster_flush(struct swapcluster
*swc
, bool now
)
536 if (swc
->swc_slot
== 0) {
539 KASSERT(swc
->swc_nused
<= swc
->swc_nallocated
);
541 slot
= swc
->swc_slot
;
542 nused
= swc
->swc_nused
;
543 nallocated
= swc
->swc_nallocated
;
546 * if this is the final pageout we could have a few
547 * unused swap blocks. if so, free them now.
550 if (nused
< nallocated
) {
554 uvm_swap_free(slot
+ nused
, nallocated
- nused
);
558 * now start the pageout.
563 uvm_pageout_start(nused
);
564 error
= uvm_swap_put(slot
, swc
->swc_pages
, nused
, 0);
565 KASSERT(error
== 0 || error
== ENOMEM
);
569 * zero swslot to indicate that we are
570 * no longer building a swap-backed cluster.
578 swapcluster_nused(struct swapcluster
*swc
)
581 return swc
->swc_nused
;
585 * uvmpd_dropswap: free any swap allocated to this page.
587 * => called with owner locked.
588 * => return true if a page had an associated slot.
592 uvmpd_dropswap(struct vm_page
*pg
)
595 struct vm_anon
*anon
= pg
->uanon
;
597 if ((pg
->pqflags
& PQ_ANON
) && anon
->an_swslot
) {
598 uvm_swap_free(anon
->an_swslot
, 1);
600 pg
->flags
&= ~PG_CLEAN
;
602 } else if (pg
->pqflags
& PQ_AOBJ
) {
603 int slot
= uao_set_swslot(pg
->uobject
,
604 pg
->offset
>> PAGE_SHIFT
, 0);
606 uvm_swap_free(slot
, 1);
607 pg
->flags
&= ~PG_CLEAN
;
616 * uvmpd_trydropswap: try to free any swap allocated to this page.
618 * => return true if a slot is successfully freed.
622 uvmpd_trydropswap(struct vm_page
*pg
)
627 if ((pg
->flags
& PG_BUSY
) != 0) {
632 * lock the page's owner.
635 slock
= uvmpd_trylockowner(pg
);
641 * skip this page if it's busy.
644 if ((pg
->flags
& PG_BUSY
) != 0) {
649 result
= uvmpd_dropswap(pg
);
656 #endif /* defined(VMSWAP) */
659 * uvmpd_scan_queue: scan an replace candidate list for pages
662 * => called with page queues locked
663 * => we work on meeting our free target by converting inactive pages
665 * => we handle the building of swap-backed clusters
669 uvmpd_scan_queue(void)
672 struct uvm_object
*uobj
;
673 struct vm_anon
*anon
;
675 struct swapcluster swc
;
676 #endif /* defined(VMSWAP) */
680 UVMHIST_FUNC("uvmpd_scan_queue"); UVMHIST_CALLED(pdhist
);
683 * swslot is non-zero if we are building a swap cluster. we want
684 * to stay in the loop while we have a page to scan or we have
685 * a swap-cluster to build.
689 swapcluster_init(&swc
);
690 #endif /* defined(VMSWAP) */
696 while (/* CONSTCOND */ 1) {
699 * see if we've met the free target.
702 if (uvmexp
.free
+ uvmexp
.paging
704 + swapcluster_nused(&swc
)
705 #endif /* defined(VMSWAP) */
706 >= uvmexp
.freetarg
<< 2 ||
707 dirtyreacts
== UVMPD_NUMDIRTYREACTS
) {
708 UVMHIST_LOG(pdhist
," met free target: "
709 "exit loop", 0, 0, 0, 0);
713 p
= uvmpdpol_selectvictim();
717 KASSERT(uvmpdpol_pageisqueued_p(p
));
718 KASSERT(p
->wire_count
== 0);
721 * we are below target and have a new page to consider.
728 * first we attempt to lock the object that this page
729 * belongs to. if our attempt fails we skip on to
730 * the next page (no harm done). it is important to
731 * "try" locking the object as we are locking in the
732 * wrong order (pageq -> object) and we don't want to
735 * the only time we expect to see an ownerless page
736 * (i.e. a page with no uobject and !PQ_ANON) is if an
737 * anon has loaned a page from a uvm_object and the
738 * uvm_object has dropped the ownership. in that
739 * case, the anon can "take over" the loaned page
740 * and make it its own.
743 slock
= uvmpd_trylockowner(p
);
746 * yield cpu to make a chance for an LWP holding
747 * the lock run. otherwise we can busy-loop too long
748 * if the page queue is filled with a lot of pages
752 if (lockownerfail
> UVMPD_NUMTRYLOCKOWNER
) {
753 mutex_exit(&uvm_pageqlock
);
754 /* XXX Better than yielding but inadequate. */
755 kpause("livelock", false, 1, NULL
);
756 mutex_enter(&uvm_pageqlock
);
761 if (p
->flags
& PG_BUSY
) {
767 /* does the page belong to an object? */
772 KASSERT(anon
!= NULL
);
774 #else /* defined(VMSWAP) */
775 panic("%s: anon", __func__
);
776 #endif /* defined(VMSWAP) */
781 * we now have the object and the page queues locked.
782 * if the page is not swap-backed, call the object's
783 * pager to flush and free the page.
786 #if defined(READAHEAD_STATS)
787 if ((p
->pqflags
& PQ_READAHEAD
) != 0) {
788 p
->pqflags
&= ~PQ_READAHEAD
;
789 uvm_ra_miss
.ev_count
++;
791 #endif /* defined(READAHEAD_STATS) */
793 if ((p
->pqflags
& PQ_SWAPBACKED
) == 0) {
794 KASSERT(uobj
!= NULL
);
795 mutex_exit(&uvm_pageqlock
);
796 (void) (uobj
->pgops
->pgo_put
)(uobj
, p
->offset
,
797 p
->offset
+ PAGE_SIZE
, PGO_CLEANIT
|PGO_FREE
);
798 mutex_enter(&uvm_pageqlock
);
803 * the page is swap-backed. remove all the permissions
804 * from the page so we can sync the modified info
805 * without any race conditions. if the page is clean
806 * we can free it now and continue.
809 pmap_page_protect(p
, VM_PROT_NONE
);
810 if ((p
->flags
& PG_CLEAN
) && pmap_clear_modify(p
)) {
811 p
->flags
&= ~(PG_CLEAN
);
813 if (p
->flags
& PG_CLEAN
) {
817 pageidx
= p
->offset
>> PAGE_SHIFT
;
822 * for anons, we need to remove the page
823 * from the anon ourselves. for aobjs,
824 * pagefree did that for us.
828 KASSERT(anon
->an_swslot
!= 0);
829 anon
->an_page
= NULL
;
830 slot
= anon
->an_swslot
;
832 slot
= uao_find_swslot(uobj
, pageidx
);
837 /* this page is now only in swap. */
838 mutex_enter(&uvm_swap_data_lock
);
839 KASSERT(uvmexp
.swpgonly
< uvmexp
.swpginuse
);
841 mutex_exit(&uvm_swap_data_lock
);
848 * this page is dirty, skip it if we'll have met our
849 * free target when all the current pageouts complete.
852 if (uvmexp
.free
+ uvmexp
.paging
> uvmexp
.freetarg
<< 2) {
858 * free any swap space allocated to the page since
859 * we'll have to write it again with its new data.
865 * start new swap pageout cluster (if necessary).
867 * if swap is full reactivate this page so that
868 * we eventually cycle all pages through the
872 if (swapcluster_allocslots(&swc
)) {
880 * at this point, we're definitely going reuse this
881 * page. mark the page busy and delayed-free.
882 * we should remove the page from the page queues
883 * so we don't ever look at it again.
884 * adjust counters and such.
888 UVM_PAGE_OWN(p
, "scan_queue");
890 p
->flags
|= PG_PAGEOUT
;
894 mutex_exit(&uvm_pageqlock
);
897 * add the new page to the cluster.
900 if (swapcluster_add(&swc
, p
)) {
901 p
->flags
&= ~(PG_BUSY
|PG_PAGEOUT
);
902 UVM_PAGE_OWN(p
, NULL
);
903 mutex_enter(&uvm_pageqlock
);
911 swapcluster_flush(&swc
, false);
912 mutex_enter(&uvm_pageqlock
);
915 * the pageout is in progress. bump counters and set up
921 #else /* defined(VMSWAP) */
924 #endif /* defined(VMSWAP) */
928 mutex_exit(&uvm_pageqlock
);
929 swapcluster_flush(&swc
, true);
930 mutex_enter(&uvm_pageqlock
);
931 #endif /* defined(VMSWAP) */
935 * uvmpd_scan: scan the page queues and attempt to meet our targets.
937 * => called with pageq's locked
943 int swap_shortage
, pages_freed
;
944 UVMHIST_FUNC("uvmpd_scan"); UVMHIST_CALLED(pdhist
);
949 * work on meeting our targets. first we work on our free target
950 * by converting inactive pages into free pages. then we work on
951 * meeting our inactive target by converting active pages to
955 UVMHIST_LOG(pdhist
, " starting 'free' loop",0,0,0,0);
957 pages_freed
= uvmexp
.pdfreed
;
959 pages_freed
= uvmexp
.pdfreed
- pages_freed
;
962 * detect if we're not going to be able to page anything out
963 * until we free some swap resources from active pages.
967 if (uvmexp
.free
< uvmexp
.freetarg
&&
968 uvmexp
.swpginuse
>= uvmexp
.swpgavail
&&
971 swap_shortage
= uvmexp
.freetarg
- uvmexp
.free
;
974 uvmpdpol_balancequeue(swap_shortage
);
977 * if still below the minimum target, try unloading kernel
981 if (uvmexp
.free
< uvmexp
.freemin
) {
982 module_thread_kick();
987 * uvm_reclaimable: decide whether to wait for pagedaemon.
989 * => return true if it seems to be worth to do uvm_wait.
991 * XXX should be tunable.
992 * XXX should consider pools, etc?
996 uvm_reclaimable(void)
999 int active
, inactive
;
1002 * if swap is not full, no problem.
1005 if (!uvm_swapisfull()) {
1010 * file-backed pages can be reclaimed even when swap is full.
1011 * if we have more than 1/16 of pageable memory or 5MB, try to reclaim.
1013 * XXX assume the worst case, ie. all wired pages are file-backed.
1015 * XXX should consider about other reclaimable memory.
1016 * XXX ie. pools, traditional buffer cache.
1019 filepages
= uvmexp
.filepages
+ uvmexp
.execpages
- uvmexp
.wired
;
1020 uvm_estimatepageable(&active
, &inactive
);
1021 if (filepages
>= MIN((active
+ inactive
) >> 4,
1022 5 * 1024 * 1024 >> PAGE_SHIFT
)) {
1027 * kill the process, fail allocation, etc..
1034 uvm_estimatepageable(int *active
, int *inactive
)
1037 uvmpdpol_estimatepageable(active
, inactive
);
1041 uvm_reclaim_init(void)
1044 /* Initialize UVM reclaim hooks. */
1045 mutex_init(&uvm_reclaim_lock
, MUTEX_DEFAULT
, IPL_NONE
);
1046 SLIST_INIT(&uvm_reclaim_list
);
1050 uvm_reclaim_hook_add(struct uvm_reclaim_hook
*hook
)
1053 KASSERT(hook
!= NULL
);
1055 mutex_enter(&uvm_reclaim_lock
);
1056 SLIST_INSERT_HEAD(&uvm_reclaim_list
, hook
, uvm_reclaim_next
);
1057 mutex_exit(&uvm_reclaim_lock
);
1061 uvm_reclaim_hook_del(struct uvm_reclaim_hook
*hook_entry
)
1063 struct uvm_reclaim_hook
*hook
;
1065 KASSERT(hook_entry
!= NULL
);
1067 mutex_enter(&uvm_reclaim_lock
);
1068 SLIST_FOREACH(hook
, &uvm_reclaim_list
, uvm_reclaim_next
) {
1069 if (hook
!= hook_entry
) {
1073 SLIST_REMOVE(&uvm_reclaim_list
, hook
, uvm_reclaim_hook
,
1078 mutex_exit(&uvm_reclaim_lock
);