uvm_pdaemon.c revision 1.102 1 /* $NetBSD: uvm_pdaemon.c,v 1.102 2011/02/02 15:25:27 chuck Exp $ */
2
3 /*
4 * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 * Copyright (c) 1991, 1993, The Regents of the University of California.
6 *
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * The Mach Operating System project at Carnegie-Mellon University.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)vm_pageout.c 8.5 (Berkeley) 2/14/94
37 * from: Id: uvm_pdaemon.c,v 1.1.2.32 1998/02/06 05:26:30 chs Exp
38 *
39 *
40 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
41 * All rights reserved.
42 *
43 * Permission to use, copy, modify and distribute this software and
44 * its documentation is hereby granted, provided that both the copyright
45 * notice and this permission notice appear in all copies of the
46 * software, derivative works or modified versions, and any portions
47 * thereof, and that both notices appear in supporting documentation.
48 *
49 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
50 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
51 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52 *
53 * Carnegie Mellon requests users of this software to return to
54 *
55 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
56 * School of Computer Science
57 * Carnegie Mellon University
58 * Pittsburgh PA 15213-3890
59 *
60 * any improvements or extensions that they make and grant Carnegie the
61 * rights to redistribute these changes.
62 */
63
64 /*
65 * uvm_pdaemon.c: the page daemon
66 */
67
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: uvm_pdaemon.c,v 1.102 2011/02/02 15:25:27 chuck Exp $");
70
71 #include "opt_uvmhist.h"
72 #include "opt_readahead.h"
73
74 #include <sys/param.h>
75 #include <sys/proc.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/pool.h>
79 #include <sys/buf.h>
80 #include <sys/module.h>
81 #include <sys/atomic.h>
82
83 #include <uvm/uvm.h>
84 #include <uvm/uvm_pdpolicy.h>
85
86 /*
87 * UVMPD_NUMDIRTYREACTS is how many dirty pages the pagedaemon will reactivate
88 * in a pass thru the inactive list when swap is full. the value should be
89 * "small"... if it's too large we'll cycle the active pages thru the inactive
90 * queue too quickly to for them to be referenced and avoid being freed.
91 */
92
93 #define UVMPD_NUMDIRTYREACTS 16
94
95 #define UVMPD_NUMTRYLOCKOWNER 16
96
97 /*
98 * local prototypes
99 */
100
101 static void uvmpd_scan(void);
102 static void uvmpd_scan_queue(void);
103 static void uvmpd_tune(void);
104
105 static unsigned int uvm_pagedaemon_waiters;
106
107 /*
108 * XXX hack to avoid hangs when large processes fork.
109 */
110 u_int uvm_extrapages;
111
112 static kmutex_t uvm_reclaim_lock;
113
114 SLIST_HEAD(uvm_reclaim_hooks, uvm_reclaim_hook) uvm_reclaim_list;
115
116 /*
117 * uvm_wait: wait (sleep) for the page daemon to free some pages
118 *
119 * => should be called with all locks released
120 * => should _not_ be called by the page daemon (to avoid deadlock)
121 */
122
123 void
124 uvm_wait(const char *wmsg)
125 {
126 int timo = 0;
127
128 mutex_spin_enter(&uvm_fpageqlock);
129
130 /*
131 * check for page daemon going to sleep (waiting for itself)
132 */
133
134 if (curlwp == uvm.pagedaemon_lwp && uvmexp.paging == 0) {
135 /*
136 * now we have a problem: the pagedaemon wants to go to
137 * sleep until it frees more memory. but how can it
138 * free more memory if it is asleep? that is a deadlock.
139 * we have two options:
140 * [1] panic now
141 * [2] put a timeout on the sleep, thus causing the
142 * pagedaemon to only pause (rather than sleep forever)
143 *
144 * note that option [2] will only help us if we get lucky
145 * and some other process on the system breaks the deadlock
146 * by exiting or freeing memory (thus allowing the pagedaemon
147 * to continue). for now we panic if DEBUG is defined,
148 * otherwise we hope for the best with option [2] (better
149 * yet, this should never happen in the first place!).
150 */
151
152 printf("pagedaemon: deadlock detected!\n");
153 timo = hz >> 3; /* set timeout */
154 #if defined(DEBUG)
155 /* DEBUG: panic so we can debug it */
156 panic("pagedaemon deadlock");
157 #endif
158 }
159
160 uvm_pagedaemon_waiters++;
161 wakeup(&uvm.pagedaemon); /* wake the daemon! */
162 UVM_UNLOCK_AND_WAIT(&uvmexp.free, &uvm_fpageqlock, false, wmsg, timo);
163 }
164
165 /*
166 * uvm_kick_pdaemon: perform checks to determine if we need to
167 * give the pagedaemon a nudge, and do so if necessary.
168 *
169 * => called with uvm_fpageqlock held.
170 */
171
172 void
173 uvm_kick_pdaemon(void)
174 {
175
176 KASSERT(mutex_owned(&uvm_fpageqlock));
177
178 if (uvmexp.free + uvmexp.paging < uvmexp.freemin ||
179 (uvmexp.free + uvmexp.paging < uvmexp.freetarg &&
180 uvmpdpol_needsscan_p())) {
181 wakeup(&uvm.pagedaemon);
182 }
183 }
184
185 /*
186 * uvmpd_tune: tune paging parameters
187 *
188 * => called when ever memory is added (or removed?) to the system
189 * => caller must call with page queues locked
190 */
191
192 static void
193 uvmpd_tune(void)
194 {
195 int val;
196
197 UVMHIST_FUNC("uvmpd_tune"); UVMHIST_CALLED(pdhist);
198
199 /*
200 * try to keep 0.5% of available RAM free, but limit to between
201 * 128k and 1024k per-CPU. XXX: what are these values good for?
202 */
203 val = uvmexp.npages / 200;
204 val = MAX(val, (128*1024) >> PAGE_SHIFT);
205 val = MIN(val, (1024*1024) >> PAGE_SHIFT);
206 val *= ncpu;
207
208 /* Make sure there's always a user page free. */
209 if (val < uvmexp.reserve_kernel + 1)
210 val = uvmexp.reserve_kernel + 1;
211 uvmexp.freemin = val;
212
213 /* Calculate free target. */
214 val = (uvmexp.freemin * 4) / 3;
215 if (val <= uvmexp.freemin)
216 val = uvmexp.freemin + 1;
217 uvmexp.freetarg = val + atomic_swap_uint(&uvm_extrapages, 0);
218
219 uvmexp.wiredmax = uvmexp.npages / 3;
220 UVMHIST_LOG(pdhist, "<- done, freemin=%d, freetarg=%d, wiredmax=%d",
221 uvmexp.freemin, uvmexp.freetarg, uvmexp.wiredmax, 0);
222 }
223
224 /*
225 * uvm_pageout: the main loop for the pagedaemon
226 */
227
228 void
229 uvm_pageout(void *arg)
230 {
231 int bufcnt, npages = 0;
232 int extrapages = 0;
233 struct pool *pp;
234 uint64_t where;
235 struct uvm_reclaim_hook *hook;
236
237 UVMHIST_FUNC("uvm_pageout"); UVMHIST_CALLED(pdhist);
238
239 UVMHIST_LOG(pdhist,"<starting uvm pagedaemon>", 0, 0, 0, 0);
240
241 /*
242 * ensure correct priority and set paging parameters...
243 */
244
245 uvm.pagedaemon_lwp = curlwp;
246 mutex_enter(&uvm_pageqlock);
247 npages = uvmexp.npages;
248 uvmpd_tune();
249 mutex_exit(&uvm_pageqlock);
250
251 /*
252 * main loop
253 */
254
255 for (;;) {
256 bool needsscan, needsfree;
257
258 mutex_spin_enter(&uvm_fpageqlock);
259 if (uvm_pagedaemon_waiters == 0 || uvmexp.paging > 0) {
260 UVMHIST_LOG(pdhist," <<SLEEPING>>",0,0,0,0);
261 UVM_UNLOCK_AND_WAIT(&uvm.pagedaemon,
262 &uvm_fpageqlock, false, "pgdaemon", 0);
263 uvmexp.pdwoke++;
264 UVMHIST_LOG(pdhist," <<WOKE UP>>",0,0,0,0);
265 } else {
266 mutex_spin_exit(&uvm_fpageqlock);
267 }
268
269 /*
270 * now lock page queues and recompute inactive count
271 */
272
273 mutex_enter(&uvm_pageqlock);
274 if (npages != uvmexp.npages || extrapages != uvm_extrapages) {
275 npages = uvmexp.npages;
276 extrapages = uvm_extrapages;
277 mutex_spin_enter(&uvm_fpageqlock);
278 uvmpd_tune();
279 mutex_spin_exit(&uvm_fpageqlock);
280 }
281
282 uvmpdpol_tune();
283
284 /*
285 * Estimate a hint. Note that bufmem are returned to
286 * system only when entire pool page is empty.
287 */
288 mutex_spin_enter(&uvm_fpageqlock);
289 bufcnt = uvmexp.freetarg - uvmexp.free;
290 if (bufcnt < 0)
291 bufcnt = 0;
292
293 UVMHIST_LOG(pdhist," free/ftarg=%d/%d",
294 uvmexp.free, uvmexp.freetarg, 0,0);
295
296 needsfree = uvmexp.free + uvmexp.paging < uvmexp.freetarg;
297 needsscan = needsfree || uvmpdpol_needsscan_p();
298
299 /*
300 * scan if needed
301 */
302 if (needsscan) {
303 mutex_spin_exit(&uvm_fpageqlock);
304 uvmpd_scan();
305 mutex_spin_enter(&uvm_fpageqlock);
306 }
307
308 /*
309 * if there's any free memory to be had,
310 * wake up any waiters.
311 */
312 if (uvmexp.free > uvmexp.reserve_kernel ||
313 uvmexp.paging == 0) {
314 wakeup(&uvmexp.free);
315 uvm_pagedaemon_waiters = 0;
316 }
317 mutex_spin_exit(&uvm_fpageqlock);
318
319 /*
320 * scan done. unlock page queues (the only lock we are holding)
321 */
322 mutex_exit(&uvm_pageqlock);
323
324 /*
325 * if we don't need free memory, we're done.
326 */
327
328 if (!needsfree)
329 continue;
330
331 /*
332 * start draining pool resources now that we're not
333 * holding any locks.
334 */
335 pool_drain_start(&pp, &where);
336
337 /*
338 * kill unused metadata buffers.
339 */
340 mutex_enter(&bufcache_lock);
341 buf_drain(bufcnt << PAGE_SHIFT);
342 mutex_exit(&bufcache_lock);
343
344 mutex_enter(&uvm_reclaim_lock);
345 SLIST_FOREACH(hook, &uvm_reclaim_list, uvm_reclaim_next) {
346 (*hook->uvm_reclaim_hook)();
347 }
348 mutex_exit(&uvm_reclaim_lock);
349
350 /*
351 * complete draining the pools.
352 */
353 pool_drain_end(pp, where);
354 }
355 /*NOTREACHED*/
356 }
357
358
359 /*
360 * uvm_aiodone_worker: a workqueue callback for the aiodone daemon.
361 */
362
363 void
364 uvm_aiodone_worker(struct work *wk, void *dummy)
365 {
366 struct buf *bp = (void *)wk;
367
368 KASSERT(&bp->b_work == wk);
369
370 /*
371 * process an i/o that's done.
372 */
373
374 (*bp->b_iodone)(bp);
375 }
376
377 void
378 uvm_pageout_start(int npages)
379 {
380
381 mutex_spin_enter(&uvm_fpageqlock);
382 uvmexp.paging += npages;
383 mutex_spin_exit(&uvm_fpageqlock);
384 }
385
386 void
387 uvm_pageout_done(int npages)
388 {
389
390 mutex_spin_enter(&uvm_fpageqlock);
391 KASSERT(uvmexp.paging >= npages);
392 uvmexp.paging -= npages;
393
394 /*
395 * wake up either of pagedaemon or LWPs waiting for it.
396 */
397
398 if (uvmexp.free <= uvmexp.reserve_kernel) {
399 wakeup(&uvm.pagedaemon);
400 } else {
401 wakeup(&uvmexp.free);
402 uvm_pagedaemon_waiters = 0;
403 }
404 mutex_spin_exit(&uvm_fpageqlock);
405 }
406
407 /*
408 * uvmpd_trylockowner: trylock the page's owner.
409 *
410 * => called with pageq locked.
411 * => resolve orphaned O->A loaned page.
412 * => return the locked mutex on success. otherwise, return NULL.
413 */
414
415 kmutex_t *
416 uvmpd_trylockowner(struct vm_page *pg)
417 {
418 struct uvm_object *uobj = pg->uobject;
419 kmutex_t *slock;
420
421 KASSERT(mutex_owned(&uvm_pageqlock));
422
423 if (uobj != NULL) {
424 slock = &uobj->vmobjlock;
425 } else {
426 struct vm_anon *anon = pg->uanon;
427
428 KASSERT(anon != NULL);
429 slock = &anon->an_lock;
430 }
431
432 if (!mutex_tryenter(slock)) {
433 return NULL;
434 }
435
436 if (uobj == NULL) {
437
438 /*
439 * set PQ_ANON if it isn't set already.
440 */
441
442 if ((pg->pqflags & PQ_ANON) == 0) {
443 KASSERT(pg->loan_count > 0);
444 pg->loan_count--;
445 pg->pqflags |= PQ_ANON;
446 /* anon now owns it */
447 }
448 }
449
450 return slock;
451 }
452
453 #if defined(VMSWAP)
454 struct swapcluster {
455 int swc_slot;
456 int swc_nallocated;
457 int swc_nused;
458 struct vm_page *swc_pages[howmany(MAXPHYS, MIN_PAGE_SIZE)];
459 };
460
461 static void
462 swapcluster_init(struct swapcluster *swc)
463 {
464
465 swc->swc_slot = 0;
466 swc->swc_nused = 0;
467 }
468
469 static int
470 swapcluster_allocslots(struct swapcluster *swc)
471 {
472 int slot;
473 int npages;
474
475 if (swc->swc_slot != 0) {
476 return 0;
477 }
478
479 /* Even with strange MAXPHYS, the shift
480 implicitly rounds down to a page. */
481 npages = MAXPHYS >> PAGE_SHIFT;
482 slot = uvm_swap_alloc(&npages, true);
483 if (slot == 0) {
484 return ENOMEM;
485 }
486 swc->swc_slot = slot;
487 swc->swc_nallocated = npages;
488 swc->swc_nused = 0;
489
490 return 0;
491 }
492
493 static int
494 swapcluster_add(struct swapcluster *swc, struct vm_page *pg)
495 {
496 int slot;
497 struct uvm_object *uobj;
498
499 KASSERT(swc->swc_slot != 0);
500 KASSERT(swc->swc_nused < swc->swc_nallocated);
501 KASSERT((pg->pqflags & PQ_SWAPBACKED) != 0);
502
503 slot = swc->swc_slot + swc->swc_nused;
504 uobj = pg->uobject;
505 if (uobj == NULL) {
506 KASSERT(mutex_owned(&pg->uanon->an_lock));
507 pg->uanon->an_swslot = slot;
508 } else {
509 int result;
510
511 KASSERT(mutex_owned(&uobj->vmobjlock));
512 result = uao_set_swslot(uobj, pg->offset >> PAGE_SHIFT, slot);
513 if (result == -1) {
514 return ENOMEM;
515 }
516 }
517 swc->swc_pages[swc->swc_nused] = pg;
518 swc->swc_nused++;
519
520 return 0;
521 }
522
523 static void
524 swapcluster_flush(struct swapcluster *swc, bool now)
525 {
526 int slot;
527 int nused;
528 int nallocated;
529 int error;
530
531 if (swc->swc_slot == 0) {
532 return;
533 }
534 KASSERT(swc->swc_nused <= swc->swc_nallocated);
535
536 slot = swc->swc_slot;
537 nused = swc->swc_nused;
538 nallocated = swc->swc_nallocated;
539
540 /*
541 * if this is the final pageout we could have a few
542 * unused swap blocks. if so, free them now.
543 */
544
545 if (nused < nallocated) {
546 if (!now) {
547 return;
548 }
549 uvm_swap_free(slot + nused, nallocated - nused);
550 }
551
552 /*
553 * now start the pageout.
554 */
555
556 if (nused > 0) {
557 uvmexp.pdpageouts++;
558 uvm_pageout_start(nused);
559 error = uvm_swap_put(slot, swc->swc_pages, nused, 0);
560 KASSERT(error == 0 || error == ENOMEM);
561 }
562
563 /*
564 * zero swslot to indicate that we are
565 * no longer building a swap-backed cluster.
566 */
567
568 swc->swc_slot = 0;
569 swc->swc_nused = 0;
570 }
571
572 static int
573 swapcluster_nused(struct swapcluster *swc)
574 {
575
576 return swc->swc_nused;
577 }
578
579 /*
580 * uvmpd_dropswap: free any swap allocated to this page.
581 *
582 * => called with owner locked.
583 * => return true if a page had an associated slot.
584 */
585
586 static bool
587 uvmpd_dropswap(struct vm_page *pg)
588 {
589 bool result = false;
590 struct vm_anon *anon = pg->uanon;
591
592 if ((pg->pqflags & PQ_ANON) && anon->an_swslot) {
593 uvm_swap_free(anon->an_swslot, 1);
594 anon->an_swslot = 0;
595 pg->flags &= ~PG_CLEAN;
596 result = true;
597 } else if (pg->pqflags & PQ_AOBJ) {
598 int slot = uao_set_swslot(pg->uobject,
599 pg->offset >> PAGE_SHIFT, 0);
600 if (slot) {
601 uvm_swap_free(slot, 1);
602 pg->flags &= ~PG_CLEAN;
603 result = true;
604 }
605 }
606
607 return result;
608 }
609
610 /*
611 * uvmpd_trydropswap: try to free any swap allocated to this page.
612 *
613 * => return true if a slot is successfully freed.
614 */
615
616 bool
617 uvmpd_trydropswap(struct vm_page *pg)
618 {
619 kmutex_t *slock;
620 bool result;
621
622 if ((pg->flags & PG_BUSY) != 0) {
623 return false;
624 }
625
626 /*
627 * lock the page's owner.
628 */
629
630 slock = uvmpd_trylockowner(pg);
631 if (slock == NULL) {
632 return false;
633 }
634
635 /*
636 * skip this page if it's busy.
637 */
638
639 if ((pg->flags & PG_BUSY) != 0) {
640 mutex_exit(slock);
641 return false;
642 }
643
644 result = uvmpd_dropswap(pg);
645
646 mutex_exit(slock);
647
648 return result;
649 }
650
651 #endif /* defined(VMSWAP) */
652
653 /*
654 * uvmpd_scan_queue: scan an replace candidate list for pages
655 * to clean or free.
656 *
657 * => called with page queues locked
658 * => we work on meeting our free target by converting inactive pages
659 * into free pages.
660 * => we handle the building of swap-backed clusters
661 */
662
663 static void
664 uvmpd_scan_queue(void)
665 {
666 struct vm_page *p;
667 struct uvm_object *uobj;
668 struct vm_anon *anon;
669 #if defined(VMSWAP)
670 struct swapcluster swc;
671 #endif /* defined(VMSWAP) */
672 int dirtyreacts;
673 int lockownerfail;
674 kmutex_t *slock;
675 UVMHIST_FUNC("uvmpd_scan_queue"); UVMHIST_CALLED(pdhist);
676
677 /*
678 * swslot is non-zero if we are building a swap cluster. we want
679 * to stay in the loop while we have a page to scan or we have
680 * a swap-cluster to build.
681 */
682
683 #if defined(VMSWAP)
684 swapcluster_init(&swc);
685 #endif /* defined(VMSWAP) */
686
687 dirtyreacts = 0;
688 lockownerfail = 0;
689 uvmpdpol_scaninit();
690
691 while (/* CONSTCOND */ 1) {
692
693 /*
694 * see if we've met the free target.
695 */
696
697 if (uvmexp.free + uvmexp.paging
698 #if defined(VMSWAP)
699 + swapcluster_nused(&swc)
700 #endif /* defined(VMSWAP) */
701 >= uvmexp.freetarg << 2 ||
702 dirtyreacts == UVMPD_NUMDIRTYREACTS) {
703 UVMHIST_LOG(pdhist," met free target: "
704 "exit loop", 0, 0, 0, 0);
705 break;
706 }
707
708 p = uvmpdpol_selectvictim();
709 if (p == NULL) {
710 break;
711 }
712 KASSERT(uvmpdpol_pageisqueued_p(p));
713 KASSERT(p->wire_count == 0);
714
715 /*
716 * we are below target and have a new page to consider.
717 */
718
719 anon = p->uanon;
720 uobj = p->uobject;
721
722 /*
723 * first we attempt to lock the object that this page
724 * belongs to. if our attempt fails we skip on to
725 * the next page (no harm done). it is important to
726 * "try" locking the object as we are locking in the
727 * wrong order (pageq -> object) and we don't want to
728 * deadlock.
729 *
730 * the only time we expect to see an ownerless page
731 * (i.e. a page with no uobject and !PQ_ANON) is if an
732 * anon has loaned a page from a uvm_object and the
733 * uvm_object has dropped the ownership. in that
734 * case, the anon can "take over" the loaned page
735 * and make it its own.
736 */
737
738 slock = uvmpd_trylockowner(p);
739 if (slock == NULL) {
740 /*
741 * yield cpu to make a chance for an LWP holding
742 * the lock run. otherwise we can busy-loop too long
743 * if the page queue is filled with a lot of pages
744 * from few objects.
745 */
746 lockownerfail++;
747 if (lockownerfail > UVMPD_NUMTRYLOCKOWNER) {
748 mutex_exit(&uvm_pageqlock);
749 /* XXX Better than yielding but inadequate. */
750 kpause("livelock", false, 1, NULL);
751 mutex_enter(&uvm_pageqlock);
752 lockownerfail = 0;
753 }
754 continue;
755 }
756 if (p->flags & PG_BUSY) {
757 mutex_exit(slock);
758 uvmexp.pdbusy++;
759 continue;
760 }
761
762 /* does the page belong to an object? */
763 if (uobj != NULL) {
764 uvmexp.pdobscan++;
765 } else {
766 #if defined(VMSWAP)
767 KASSERT(anon != NULL);
768 uvmexp.pdanscan++;
769 #else /* defined(VMSWAP) */
770 panic("%s: anon", __func__);
771 #endif /* defined(VMSWAP) */
772 }
773
774
775 /*
776 * we now have the object and the page queues locked.
777 * if the page is not swap-backed, call the object's
778 * pager to flush and free the page.
779 */
780
781 #if defined(READAHEAD_STATS)
782 if ((p->pqflags & PQ_READAHEAD) != 0) {
783 p->pqflags &= ~PQ_READAHEAD;
784 uvm_ra_miss.ev_count++;
785 }
786 #endif /* defined(READAHEAD_STATS) */
787
788 if ((p->pqflags & PQ_SWAPBACKED) == 0) {
789 KASSERT(uobj != NULL);
790 mutex_exit(&uvm_pageqlock);
791 (void) (uobj->pgops->pgo_put)(uobj, p->offset,
792 p->offset + PAGE_SIZE, PGO_CLEANIT|PGO_FREE);
793 mutex_enter(&uvm_pageqlock);
794 continue;
795 }
796
797 /*
798 * the page is swap-backed. remove all the permissions
799 * from the page so we can sync the modified info
800 * without any race conditions. if the page is clean
801 * we can free it now and continue.
802 */
803
804 pmap_page_protect(p, VM_PROT_NONE);
805 if ((p->flags & PG_CLEAN) && pmap_clear_modify(p)) {
806 p->flags &= ~(PG_CLEAN);
807 }
808 if (p->flags & PG_CLEAN) {
809 int slot;
810 int pageidx;
811
812 pageidx = p->offset >> PAGE_SHIFT;
813 uvm_pagefree(p);
814 uvmexp.pdfreed++;
815
816 /*
817 * for anons, we need to remove the page
818 * from the anon ourselves. for aobjs,
819 * pagefree did that for us.
820 */
821
822 if (anon) {
823 KASSERT(anon->an_swslot != 0);
824 anon->an_page = NULL;
825 slot = anon->an_swslot;
826 } else {
827 slot = uao_find_swslot(uobj, pageidx);
828 }
829 mutex_exit(slock);
830
831 if (slot > 0) {
832 /* this page is now only in swap. */
833 mutex_enter(&uvm_swap_data_lock);
834 KASSERT(uvmexp.swpgonly < uvmexp.swpginuse);
835 uvmexp.swpgonly++;
836 mutex_exit(&uvm_swap_data_lock);
837 }
838 continue;
839 }
840
841 #if defined(VMSWAP)
842 /*
843 * this page is dirty, skip it if we'll have met our
844 * free target when all the current pageouts complete.
845 */
846
847 if (uvmexp.free + uvmexp.paging > uvmexp.freetarg << 2) {
848 mutex_exit(slock);
849 continue;
850 }
851
852 /*
853 * free any swap space allocated to the page since
854 * we'll have to write it again with its new data.
855 */
856
857 uvmpd_dropswap(p);
858
859 /*
860 * start new swap pageout cluster (if necessary).
861 *
862 * if swap is full reactivate this page so that
863 * we eventually cycle all pages through the
864 * inactive queue.
865 */
866
867 if (swapcluster_allocslots(&swc)) {
868 dirtyreacts++;
869 uvm_pageactivate(p);
870 mutex_exit(slock);
871 continue;
872 }
873
874 /*
875 * at this point, we're definitely going reuse this
876 * page. mark the page busy and delayed-free.
877 * we should remove the page from the page queues
878 * so we don't ever look at it again.
879 * adjust counters and such.
880 */
881
882 p->flags |= PG_BUSY;
883 UVM_PAGE_OWN(p, "scan_queue");
884
885 p->flags |= PG_PAGEOUT;
886 uvm_pagedequeue(p);
887
888 uvmexp.pgswapout++;
889 mutex_exit(&uvm_pageqlock);
890
891 /*
892 * add the new page to the cluster.
893 */
894
895 if (swapcluster_add(&swc, p)) {
896 p->flags &= ~(PG_BUSY|PG_PAGEOUT);
897 UVM_PAGE_OWN(p, NULL);
898 mutex_enter(&uvm_pageqlock);
899 dirtyreacts++;
900 uvm_pageactivate(p);
901 mutex_exit(slock);
902 continue;
903 }
904 mutex_exit(slock);
905
906 swapcluster_flush(&swc, false);
907 mutex_enter(&uvm_pageqlock);
908
909 /*
910 * the pageout is in progress. bump counters and set up
911 * for the next loop.
912 */
913
914 uvmexp.pdpending++;
915
916 #else /* defined(VMSWAP) */
917 uvm_pageactivate(p);
918 mutex_exit(slock);
919 #endif /* defined(VMSWAP) */
920 }
921
922 #if defined(VMSWAP)
923 mutex_exit(&uvm_pageqlock);
924 swapcluster_flush(&swc, true);
925 mutex_enter(&uvm_pageqlock);
926 #endif /* defined(VMSWAP) */
927 }
928
929 /*
930 * uvmpd_scan: scan the page queues and attempt to meet our targets.
931 *
932 * => called with pageq's locked
933 */
934
935 static void
936 uvmpd_scan(void)
937 {
938 int swap_shortage, pages_freed;
939 UVMHIST_FUNC("uvmpd_scan"); UVMHIST_CALLED(pdhist);
940
941 uvmexp.pdrevs++;
942
943 /*
944 * work on meeting our targets. first we work on our free target
945 * by converting inactive pages into free pages. then we work on
946 * meeting our inactive target by converting active pages to
947 * inactive ones.
948 */
949
950 UVMHIST_LOG(pdhist, " starting 'free' loop",0,0,0,0);
951
952 pages_freed = uvmexp.pdfreed;
953 uvmpd_scan_queue();
954 pages_freed = uvmexp.pdfreed - pages_freed;
955
956 /*
957 * detect if we're not going to be able to page anything out
958 * until we free some swap resources from active pages.
959 */
960
961 swap_shortage = 0;
962 if (uvmexp.free < uvmexp.freetarg &&
963 uvmexp.swpginuse >= uvmexp.swpgavail &&
964 !uvm_swapisfull() &&
965 pages_freed == 0) {
966 swap_shortage = uvmexp.freetarg - uvmexp.free;
967 }
968
969 uvmpdpol_balancequeue(swap_shortage);
970
971 /*
972 * if still below the minimum target, try unloading kernel
973 * modules.
974 */
975
976 if (uvmexp.free < uvmexp.freemin) {
977 module_thread_kick();
978 }
979 }
980
981 /*
982 * uvm_reclaimable: decide whether to wait for pagedaemon.
983 *
984 * => return true if it seems to be worth to do uvm_wait.
985 *
986 * XXX should be tunable.
987 * XXX should consider pools, etc?
988 */
989
990 bool
991 uvm_reclaimable(void)
992 {
993 int filepages;
994 int active, inactive;
995
996 /*
997 * if swap is not full, no problem.
998 */
999
1000 if (!uvm_swapisfull()) {
1001 return true;
1002 }
1003
1004 /*
1005 * file-backed pages can be reclaimed even when swap is full.
1006 * if we have more than 1/16 of pageable memory or 5MB, try to reclaim.
1007 *
1008 * XXX assume the worst case, ie. all wired pages are file-backed.
1009 *
1010 * XXX should consider about other reclaimable memory.
1011 * XXX ie. pools, traditional buffer cache.
1012 */
1013
1014 filepages = uvmexp.filepages + uvmexp.execpages - uvmexp.wired;
1015 uvm_estimatepageable(&active, &inactive);
1016 if (filepages >= MIN((active + inactive) >> 4,
1017 5 * 1024 * 1024 >> PAGE_SHIFT)) {
1018 return true;
1019 }
1020
1021 /*
1022 * kill the process, fail allocation, etc..
1023 */
1024
1025 return false;
1026 }
1027
1028 void
1029 uvm_estimatepageable(int *active, int *inactive)
1030 {
1031
1032 uvmpdpol_estimatepageable(active, inactive);
1033 }
1034
1035 void
1036 uvm_reclaim_init(void)
1037 {
1038
1039 /* Initialize UVM reclaim hooks. */
1040 mutex_init(&uvm_reclaim_lock, MUTEX_DEFAULT, IPL_NONE);
1041 SLIST_INIT(&uvm_reclaim_list);
1042 }
1043
1044 void
1045 uvm_reclaim_hook_add(struct uvm_reclaim_hook *hook)
1046 {
1047
1048 KASSERT(hook != NULL);
1049
1050 mutex_enter(&uvm_reclaim_lock);
1051 SLIST_INSERT_HEAD(&uvm_reclaim_list, hook, uvm_reclaim_next);
1052 mutex_exit(&uvm_reclaim_lock);
1053 }
1054
1055 void
1056 uvm_reclaim_hook_del(struct uvm_reclaim_hook *hook_entry)
1057 {
1058 struct uvm_reclaim_hook *hook;
1059
1060 KASSERT(hook_entry != NULL);
1061
1062 mutex_enter(&uvm_reclaim_lock);
1063 SLIST_FOREACH(hook, &uvm_reclaim_list, uvm_reclaim_next) {
1064 if (hook != hook_entry) {
1065 continue;
1066 }
1067
1068 SLIST_REMOVE(&uvm_reclaim_list, hook, uvm_reclaim_hook,
1069 uvm_reclaim_next);
1070 break;
1071 }
1072
1073 mutex_exit(&uvm_reclaim_lock);
1074 }
1075