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