uvm_pdaemon.c revision 1.103.2.1 1 /* $NetBSD: uvm_pdaemon.c,v 1.103.2.1 2011/11/02 21:54:01 yamt 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.103.2.1 2011/11/02 21:54:01 yamt 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 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
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 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
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 #if defined(VMSWAP)
678 swapcluster_init(&swc);
679 #endif /* defined(VMSWAP) */
680
681 dirtyreacts = 0;
682 lockownerfail = 0;
683 uvmpdpol_scaninit();
684
685 while (/* CONSTCOND */ 1) {
686
687 /*
688 * see if we've met the free target.
689 */
690
691 if (uvmexp.free + uvmexp.paging
692 #if defined(VMSWAP)
693 + swapcluster_nused(&swc)
694 #endif /* defined(VMSWAP) */
695 >= uvmexp.freetarg << 2 ||
696 dirtyreacts == UVMPD_NUMDIRTYREACTS) {
697 UVMHIST_LOG(pdhist," met free target: "
698 "exit loop", 0, 0, 0, 0);
699 break;
700 }
701
702 p = uvmpdpol_selectvictim();
703 if (p == NULL) {
704 break;
705 }
706 KASSERT(uvmpdpol_pageisqueued_p(p));
707 KASSERT(p->wire_count == 0);
708
709 /*
710 * we are below target and have a new page to consider.
711 */
712
713 anon = p->uanon;
714 uobj = p->uobject;
715
716 /*
717 * first we attempt to lock the object that this page
718 * belongs to. if our attempt fails we skip on to
719 * the next page (no harm done). it is important to
720 * "try" locking the object as we are locking in the
721 * wrong order (pageq -> object) and we don't want to
722 * deadlock.
723 *
724 * the only time we expect to see an ownerless page
725 * (i.e. a page with no uobject and !PQ_ANON) is if an
726 * anon has loaned a page from a uvm_object and the
727 * uvm_object has dropped the ownership. in that
728 * case, the anon can "take over" the loaned page
729 * and make it its own.
730 */
731
732 slock = uvmpd_trylockowner(p);
733 if (slock == NULL) {
734 /*
735 * yield cpu to make a chance for an LWP holding
736 * the lock run. otherwise we can busy-loop too long
737 * if the page queue is filled with a lot of pages
738 * from few objects.
739 */
740 lockownerfail++;
741 if (lockownerfail > UVMPD_NUMTRYLOCKOWNER) {
742 mutex_exit(&uvm_pageqlock);
743 /* XXX Better than yielding but inadequate. */
744 kpause("livelock", false, 1, NULL);
745 mutex_enter(&uvm_pageqlock);
746 lockownerfail = 0;
747 }
748 continue;
749 }
750 if (p->flags & PG_BUSY) {
751 mutex_exit(slock);
752 uvmexp.pdbusy++;
753 continue;
754 }
755
756 /* does the page belong to an object? */
757 if (uobj != NULL) {
758 uvmexp.pdobscan++;
759 } else {
760 #if defined(VMSWAP)
761 KASSERT(anon != NULL);
762 uvmexp.pdanscan++;
763 #else /* defined(VMSWAP) */
764 panic("%s: anon", __func__);
765 #endif /* defined(VMSWAP) */
766 }
767
768
769 /*
770 * we now have the object and the page queues locked.
771 * if the page is not swap-backed, call the object's
772 * pager to flush and free the page.
773 */
774
775 #if defined(READAHEAD_STATS)
776 if ((p->pqflags & PQ_READAHEAD) != 0) {
777 p->pqflags &= ~PQ_READAHEAD;
778 uvm_ra_miss.ev_count++;
779 }
780 #endif /* defined(READAHEAD_STATS) */
781
782 if ((p->pqflags & PQ_SWAPBACKED) == 0) {
783 KASSERT(uobj != NULL);
784 mutex_exit(&uvm_pageqlock);
785 (void) (uobj->pgops->pgo_put)(uobj, p->offset,
786 p->offset + PAGE_SIZE, PGO_CLEANIT|PGO_FREE);
787 mutex_enter(&uvm_pageqlock);
788 continue;
789 }
790
791 /*
792 * the page is swap-backed. remove all the permissions
793 * from the page so we can sync the modified info
794 * without any race conditions. if the page is clean
795 * we can free it now and continue.
796 */
797
798 pmap_page_protect(p, VM_PROT_NONE);
799 if (uvm_pagegetdirty(p) == UVM_PAGE_STATUS_UNKNOWN) {
800 if (pmap_clear_modify(p)) {
801 uvm_pagemarkdirty(p, UVM_PAGE_STATUS_DIRTY);
802 } else {
803 uvm_pagemarkdirty(p, UVM_PAGE_STATUS_CLEAN);
804 }
805 }
806 if (uvm_pagegetdirty(p) != UVM_PAGE_STATUS_DIRTY) {
807 int slot;
808 int pageidx;
809
810 pageidx = p->offset >> PAGE_SHIFT;
811 uvm_pagefree(p);
812 uvmexp.pdfreed++;
813
814 /*
815 * for anons, we need to remove the page
816 * from the anon ourselves. for aobjs,
817 * pagefree did that for us.
818 */
819
820 if (anon) {
821 KASSERT(anon->an_swslot != 0);
822 anon->an_page = NULL;
823 slot = anon->an_swslot;
824 } else {
825 slot = uao_find_swslot(uobj, pageidx);
826 }
827 mutex_exit(slock);
828
829 if (slot > 0) {
830 /* this page is now only in swap. */
831 mutex_enter(&uvm_swap_data_lock);
832 KASSERT(uvmexp.swpgonly < uvmexp.swpginuse);
833 uvmexp.swpgonly++;
834 mutex_exit(&uvm_swap_data_lock);
835 }
836 continue;
837 }
838
839 #if defined(VMSWAP)
840 /*
841 * this page is dirty, skip it if we'll have met our
842 * free target when all the current pageouts complete.
843 */
844
845 if (uvmexp.free + uvmexp.paging > uvmexp.freetarg << 2) {
846 mutex_exit(slock);
847 continue;
848 }
849
850 /*
851 * free any swap space allocated to the page since
852 * we'll have to write it again with its new data.
853 */
854
855 uvmpd_dropswap(p);
856
857 /*
858 * start new swap pageout cluster (if necessary).
859 *
860 * if swap is full reactivate this page so that
861 * we eventually cycle all pages through the
862 * inactive queue.
863 */
864
865 if (swapcluster_allocslots(&swc)) {
866 dirtyreacts++;
867 uvm_pageactivate(p);
868 mutex_exit(slock);
869 continue;
870 }
871
872 /*
873 * at this point, we're definitely going reuse this
874 * page. mark the page busy and delayed-free.
875 * we should remove the page from the page queues
876 * so we don't ever look at it again.
877 * adjust counters and such.
878 */
879
880 p->flags |= PG_BUSY;
881 UVM_PAGE_OWN(p, "scan_queue");
882
883 p->flags |= PG_PAGEOUT;
884 uvm_pagedequeue(p);
885
886 uvmexp.pgswapout++;
887 mutex_exit(&uvm_pageqlock);
888
889 /*
890 * add the new page to the cluster.
891 */
892
893 if (swapcluster_add(&swc, p)) {
894 p->flags &= ~(PG_BUSY|PG_PAGEOUT);
895 UVM_PAGE_OWN(p, NULL);
896 mutex_enter(&uvm_pageqlock);
897 dirtyreacts++;
898 uvm_pageactivate(p);
899 mutex_exit(slock);
900 continue;
901 }
902 mutex_exit(slock);
903
904 swapcluster_flush(&swc, false);
905 mutex_enter(&uvm_pageqlock);
906
907 /*
908 * the pageout is in progress. bump counters and set up
909 * for the next loop.
910 */
911
912 uvmexp.pdpending++;
913
914 #else /* defined(VMSWAP) */
915 uvm_pageactivate(p);
916 mutex_exit(slock);
917 #endif /* defined(VMSWAP) */
918 }
919
920 #if defined(VMSWAP)
921 mutex_exit(&uvm_pageqlock);
922 swapcluster_flush(&swc, true);
923 mutex_enter(&uvm_pageqlock);
924 #endif /* defined(VMSWAP) */
925 }
926
927 /*
928 * uvmpd_scan: scan the page queues and attempt to meet our targets.
929 *
930 * => called with pageq's locked
931 */
932
933 static void
934 uvmpd_scan(void)
935 {
936 int swap_shortage, pages_freed;
937 UVMHIST_FUNC("uvmpd_scan"); UVMHIST_CALLED(pdhist);
938
939 uvmexp.pdrevs++;
940
941 /*
942 * work on meeting our targets. first we work on our free target
943 * by converting inactive pages into free pages. then we work on
944 * meeting our inactive target by converting active pages to
945 * inactive ones.
946 */
947
948 UVMHIST_LOG(pdhist, " starting 'free' loop",0,0,0,0);
949
950 pages_freed = uvmexp.pdfreed;
951 uvmpd_scan_queue();
952 pages_freed = uvmexp.pdfreed - pages_freed;
953
954 /*
955 * detect if we're not going to be able to page anything out
956 * until we free some swap resources from active pages.
957 */
958
959 swap_shortage = 0;
960 if (uvmexp.free < uvmexp.freetarg &&
961 uvmexp.swpginuse >= uvmexp.swpgavail &&
962 !uvm_swapisfull() &&
963 pages_freed == 0) {
964 swap_shortage = uvmexp.freetarg - uvmexp.free;
965 }
966
967 uvmpdpol_balancequeue(swap_shortage);
968
969 /*
970 * if still below the minimum target, try unloading kernel
971 * modules.
972 */
973
974 if (uvmexp.free < uvmexp.freemin) {
975 module_thread_kick();
976 }
977 }
978
979 /*
980 * uvm_reclaimable: decide whether to wait for pagedaemon.
981 *
982 * => return true if it seems to be worth to do uvm_wait.
983 *
984 * XXX should be tunable.
985 * XXX should consider pools, etc?
986 */
987
988 bool
989 uvm_reclaimable(void)
990 {
991 int filepages;
992 int active, inactive;
993
994 /*
995 * if swap is not full, no problem.
996 */
997
998 if (!uvm_swapisfull()) {
999 return true;
1000 }
1001
1002 /*
1003 * file-backed pages can be reclaimed even when swap is full.
1004 * if we have more than 1/16 of pageable memory or 5MB, try to reclaim.
1005 *
1006 * XXX assume the worst case, ie. all wired pages are file-backed.
1007 *
1008 * XXX should consider about other reclaimable memory.
1009 * XXX ie. pools, traditional buffer cache.
1010 */
1011
1012 filepages = uvmexp.filepages + uvmexp.execpages - uvmexp.wired;
1013 uvm_estimatepageable(&active, &inactive);
1014 if (filepages >= MIN((active + inactive) >> 4,
1015 5 * 1024 * 1024 >> PAGE_SHIFT)) {
1016 return true;
1017 }
1018
1019 /*
1020 * kill the process, fail allocation, etc..
1021 */
1022
1023 return false;
1024 }
1025
1026 void
1027 uvm_estimatepageable(int *active, int *inactive)
1028 {
1029
1030 uvmpdpol_estimatepageable(active, inactive);
1031 }
1032
1033 void
1034 uvm_reclaim_init(void)
1035 {
1036
1037 /* Initialize UVM reclaim hooks. */
1038 mutex_init(&uvm_reclaim_lock, MUTEX_DEFAULT, IPL_NONE);
1039 SLIST_INIT(&uvm_reclaim_list);
1040 }
1041
1042 void
1043 uvm_reclaim_hook_add(struct uvm_reclaim_hook *hook)
1044 {
1045
1046 KASSERT(hook != NULL);
1047
1048 mutex_enter(&uvm_reclaim_lock);
1049 SLIST_INSERT_HEAD(&uvm_reclaim_list, hook, uvm_reclaim_next);
1050 mutex_exit(&uvm_reclaim_lock);
1051 }
1052
1053 void
1054 uvm_reclaim_hook_del(struct uvm_reclaim_hook *hook_entry)
1055 {
1056 struct uvm_reclaim_hook *hook;
1057
1058 KASSERT(hook_entry != NULL);
1059
1060 mutex_enter(&uvm_reclaim_lock);
1061 SLIST_FOREACH(hook, &uvm_reclaim_list, uvm_reclaim_next) {
1062 if (hook != hook_entry) {
1063 continue;
1064 }
1065
1066 SLIST_REMOVE(&uvm_reclaim_list, hook, uvm_reclaim_hook,
1067 uvm_reclaim_next);
1068 break;
1069 }
1070
1071 mutex_exit(&uvm_reclaim_lock);
1072 }
1073