uvm_pdaemon.c revision 1.103.2.2 1 /* $NetBSD: uvm_pdaemon.c,v 1.103.2.2 2011/11/18 00:57:34 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.2 2011/11/18 00:57:34 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 *lock;
420
421 KASSERT(mutex_owned(&uvm_pageqlock));
422 lock = uvm_page_getlock(pg);
423 KASSERT(lock != NULL);
424 if (!mutex_tryenter(lock)) {
425 return NULL;
426 }
427 if (uobj == NULL) {
428
429 /*
430 * set PQ_ANON if it isn't set already.
431 */
432
433 if ((pg->pqflags & PQ_ANON) == 0) {
434 KASSERT(pg->loan_count > 0);
435 pg->loan_count--;
436 pg->pqflags |= PQ_ANON;
437 /* anon now owns it */
438 }
439 }
440
441 return lock;
442 }
443
444 #if defined(VMSWAP)
445 struct swapcluster {
446 int swc_slot;
447 int swc_nallocated;
448 int swc_nused;
449 struct vm_page *swc_pages[howmany(MAXPHYS, MIN_PAGE_SIZE)];
450 };
451
452 static void
453 swapcluster_init(struct swapcluster *swc)
454 {
455
456 swc->swc_slot = 0;
457 swc->swc_nused = 0;
458 }
459
460 static int
461 swapcluster_allocslots(struct swapcluster *swc)
462 {
463 int slot;
464 int npages;
465
466 if (swc->swc_slot != 0) {
467 return 0;
468 }
469
470 /* Even with strange MAXPHYS, the shift
471 implicitly rounds down to a page. */
472 npages = MAXPHYS >> PAGE_SHIFT;
473 slot = uvm_swap_alloc(&npages, true);
474 if (slot == 0) {
475 return ENOMEM;
476 }
477 swc->swc_slot = slot;
478 swc->swc_nallocated = npages;
479 swc->swc_nused = 0;
480
481 return 0;
482 }
483
484 static int
485 swapcluster_add(struct swapcluster *swc, struct vm_page *pg)
486 {
487 int slot;
488 struct uvm_object *uobj;
489
490 KASSERT(swc->swc_slot != 0);
491 KASSERT(swc->swc_nused < swc->swc_nallocated);
492 KASSERT((pg->pqflags & PQ_SWAPBACKED) != 0);
493
494 slot = swc->swc_slot + swc->swc_nused;
495 uobj = pg->uobject;
496 if (uobj == NULL) {
497 KASSERT(mutex_owned(pg->uanon->an_lock));
498 pg->uanon->an_swslot = slot;
499 } else {
500 int result;
501
502 KASSERT(mutex_owned(uobj->vmobjlock));
503 result = uao_set_swslot(uobj, pg->offset >> PAGE_SHIFT, slot);
504 if (result == -1) {
505 return ENOMEM;
506 }
507 }
508 swc->swc_pages[swc->swc_nused] = pg;
509 swc->swc_nused++;
510
511 return 0;
512 }
513
514 static void
515 swapcluster_flush(struct swapcluster *swc, bool now)
516 {
517 int slot;
518 int nused;
519 int nallocated;
520 int error;
521
522 if (swc->swc_slot == 0) {
523 return;
524 }
525 KASSERT(swc->swc_nused <= swc->swc_nallocated);
526
527 slot = swc->swc_slot;
528 nused = swc->swc_nused;
529 nallocated = swc->swc_nallocated;
530
531 /*
532 * if this is the final pageout we could have a few
533 * unused swap blocks. if so, free them now.
534 */
535
536 if (nused < nallocated) {
537 if (!now) {
538 return;
539 }
540 uvm_swap_free(slot + nused, nallocated - nused);
541 }
542
543 /*
544 * now start the pageout.
545 */
546
547 if (nused > 0) {
548 uvmexp.pdpageouts++;
549 uvm_pageout_start(nused);
550 error = uvm_swap_put(slot, swc->swc_pages, nused, 0);
551 KASSERT(error == 0 || error == ENOMEM);
552 }
553
554 /*
555 * zero swslot to indicate that we are
556 * no longer building a swap-backed cluster.
557 */
558
559 swc->swc_slot = 0;
560 swc->swc_nused = 0;
561 }
562
563 static int
564 swapcluster_nused(struct swapcluster *swc)
565 {
566
567 return swc->swc_nused;
568 }
569
570 /*
571 * uvmpd_dropswap: free any swap allocated to this page.
572 *
573 * => called with owner locked.
574 * => return true if a page had an associated slot.
575 */
576
577 static bool
578 uvmpd_dropswap(struct vm_page *pg)
579 {
580 bool result = false;
581 struct vm_anon *anon = pg->uanon;
582
583 if ((pg->pqflags & PQ_ANON) && anon->an_swslot) {
584 uvm_swap_free(anon->an_swslot, 1);
585 anon->an_swslot = 0;
586 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
587 result = true;
588 } else if (pg->pqflags & PQ_AOBJ) {
589 int slot = uao_set_swslot(pg->uobject,
590 pg->offset >> PAGE_SHIFT, 0);
591 if (slot) {
592 uvm_swap_free(slot, 1);
593 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
594 result = true;
595 }
596 }
597
598 return result;
599 }
600
601 /*
602 * uvmpd_trydropswap: try to free any swap allocated to this page.
603 *
604 * => return true if a slot is successfully freed.
605 */
606
607 bool
608 uvmpd_trydropswap(struct vm_page *pg)
609 {
610 kmutex_t *slock;
611 bool result;
612
613 if ((pg->flags & PG_BUSY) != 0) {
614 return false;
615 }
616
617 /*
618 * lock the page's owner.
619 */
620
621 slock = uvmpd_trylockowner(pg);
622 if (slock == NULL) {
623 return false;
624 }
625
626 /*
627 * skip this page if it's busy.
628 */
629
630 if ((pg->flags & PG_BUSY) != 0) {
631 mutex_exit(slock);
632 return false;
633 }
634
635 result = uvmpd_dropswap(pg);
636
637 mutex_exit(slock);
638
639 return result;
640 }
641
642 #endif /* defined(VMSWAP) */
643
644 /*
645 * uvmpd_scan_queue: scan an replace candidate list for pages
646 * to clean or free.
647 *
648 * => called with page queues locked
649 * => we work on meeting our free target by converting inactive pages
650 * into free pages.
651 * => we handle the building of swap-backed clusters
652 */
653
654 static void
655 uvmpd_scan_queue(void)
656 {
657 struct vm_page *p;
658 struct uvm_object *uobj;
659 struct vm_anon *anon;
660 #if defined(VMSWAP)
661 struct swapcluster swc;
662 #endif /* defined(VMSWAP) */
663 int dirtyreacts;
664 int lockownerfail;
665 kmutex_t *slock;
666 UVMHIST_FUNC("uvmpd_scan_queue"); UVMHIST_CALLED(pdhist);
667
668 #if defined(VMSWAP)
669 swapcluster_init(&swc);
670 #endif /* defined(VMSWAP) */
671
672 dirtyreacts = 0;
673 lockownerfail = 0;
674 uvmpdpol_scaninit();
675
676 while (/* CONSTCOND */ 1) {
677
678 /*
679 * see if we've met the free target.
680 */
681
682 if (uvmexp.free + uvmexp.paging
683 #if defined(VMSWAP)
684 + swapcluster_nused(&swc)
685 #endif /* defined(VMSWAP) */
686 >= uvmexp.freetarg << 2 ||
687 dirtyreacts == UVMPD_NUMDIRTYREACTS) {
688 UVMHIST_LOG(pdhist," met free target: "
689 "exit loop", 0, 0, 0, 0);
690 break;
691 }
692
693 p = uvmpdpol_selectvictim();
694 if (p == NULL) {
695 break;
696 }
697 KASSERT(uvmpdpol_pageisqueued_p(p));
698 KASSERT(p->wire_count == 0);
699
700 /*
701 * we are below target and have a new page to consider.
702 */
703
704 anon = p->uanon;
705 uobj = p->uobject;
706
707 /*
708 * first we attempt to lock the object that this page
709 * belongs to. if our attempt fails we skip on to
710 * the next page (no harm done). it is important to
711 * "try" locking the object as we are locking in the
712 * wrong order (pageq -> object) and we don't want to
713 * deadlock.
714 *
715 * the only time we expect to see an ownerless page
716 * (i.e. a page with no uobject and !PQ_ANON) is if an
717 * anon has loaned a page from a uvm_object and the
718 * uvm_object has dropped the ownership. in that
719 * case, the anon can "take over" the loaned page
720 * and make it its own.
721 */
722
723 slock = uvmpd_trylockowner(p);
724 if (slock == NULL) {
725 /*
726 * yield cpu to make a chance for an LWP holding
727 * the lock run. otherwise we can busy-loop too long
728 * if the page queue is filled with a lot of pages
729 * from few objects.
730 */
731 lockownerfail++;
732 if (lockownerfail > UVMPD_NUMTRYLOCKOWNER) {
733 mutex_obj_pause(uvm_page_getlock(p),
734 &uvm_pageqlock);
735 lockownerfail = 0;
736 }
737 continue;
738 }
739 if (p->flags & PG_BUSY) {
740 mutex_exit(slock);
741 uvmexp.pdbusy++;
742 continue;
743 }
744
745 /* does the page belong to an object? */
746 if (uobj != NULL) {
747 uvmexp.pdobscan++;
748 } else {
749 #if defined(VMSWAP)
750 KASSERT(anon != NULL);
751 uvmexp.pdanscan++;
752 #else /* defined(VMSWAP) */
753 panic("%s: anon", __func__);
754 #endif /* defined(VMSWAP) */
755 }
756
757
758 /*
759 * we now have the object and the page queues locked.
760 * if the page is not swap-backed, call the object's
761 * pager to flush and free the page.
762 */
763
764 #if defined(READAHEAD_STATS)
765 if ((p->pqflags & PQ_READAHEAD) != 0) {
766 p->pqflags &= ~PQ_READAHEAD;
767 uvm_ra_miss.ev_count++;
768 }
769 #endif /* defined(READAHEAD_STATS) */
770
771 if ((p->pqflags & PQ_SWAPBACKED) == 0) {
772 KASSERT(uobj != NULL);
773 mutex_exit(&uvm_pageqlock);
774 (void) (uobj->pgops->pgo_put)(uobj, p->offset,
775 p->offset + PAGE_SIZE, PGO_CLEANIT|PGO_FREE);
776 mutex_enter(&uvm_pageqlock);
777 continue;
778 }
779
780 /*
781 * the page is swap-backed. remove all the permissions
782 * from the page so we can sync the modified info
783 * without any race conditions. if the page is clean
784 * we can free it now and continue.
785 */
786
787 pmap_page_protect(p, VM_PROT_NONE);
788 if (uvm_pagegetdirty(p) == UVM_PAGE_STATUS_UNKNOWN) {
789 if (pmap_clear_modify(p)) {
790 uvm_pagemarkdirty(p, UVM_PAGE_STATUS_DIRTY);
791 } else {
792 uvm_pagemarkdirty(p, UVM_PAGE_STATUS_CLEAN);
793 }
794 }
795 if (uvm_pagegetdirty(p) != UVM_PAGE_STATUS_DIRTY) {
796 int slot;
797 int pageidx;
798
799 pageidx = p->offset >> PAGE_SHIFT;
800 uvm_pagefree(p);
801 uvmexp.pdfreed++;
802
803 /*
804 * for anons, we need to remove the page
805 * from the anon ourselves. for aobjs,
806 * pagefree did that for us.
807 */
808
809 if (anon) {
810 KASSERT(anon->an_swslot != 0);
811 anon->an_page = NULL;
812 slot = anon->an_swslot;
813 } else {
814 slot = uao_find_swslot(uobj, pageidx);
815 }
816 mutex_exit(slock);
817
818 if (slot > 0) {
819 /* this page is now only in swap. */
820 mutex_enter(&uvm_swap_data_lock);
821 KASSERT(uvmexp.swpgonly < uvmexp.swpginuse);
822 uvmexp.swpgonly++;
823 mutex_exit(&uvm_swap_data_lock);
824 }
825 continue;
826 }
827
828 #if defined(VMSWAP)
829 /*
830 * this page is dirty, skip it if we'll have met our
831 * free target when all the current pageouts complete.
832 */
833
834 if (uvmexp.free + uvmexp.paging > uvmexp.freetarg << 2) {
835 mutex_exit(slock);
836 continue;
837 }
838
839 /*
840 * free any swap space allocated to the page since
841 * we'll have to write it again with its new data.
842 */
843
844 uvmpd_dropswap(p);
845
846 /*
847 * start new swap pageout cluster (if necessary).
848 *
849 * if swap is full reactivate this page so that
850 * we eventually cycle all pages through the
851 * inactive queue.
852 */
853
854 if (swapcluster_allocslots(&swc)) {
855 dirtyreacts++;
856 uvm_pageactivate(p);
857 mutex_exit(slock);
858 continue;
859 }
860
861 /*
862 * at this point, we're definitely going reuse this
863 * page. mark the page busy and delayed-free.
864 * we should remove the page from the page queues
865 * so we don't ever look at it again.
866 * adjust counters and such.
867 */
868
869 p->flags |= PG_BUSY;
870 UVM_PAGE_OWN(p, "scan_queue");
871
872 p->flags |= PG_PAGEOUT;
873 uvm_pagedequeue(p);
874
875 uvmexp.pgswapout++;
876 mutex_exit(&uvm_pageqlock);
877
878 /*
879 * add the new page to the cluster.
880 */
881
882 if (swapcluster_add(&swc, p)) {
883 p->flags &= ~(PG_BUSY|PG_PAGEOUT);
884 UVM_PAGE_OWN(p, NULL);
885 mutex_enter(&uvm_pageqlock);
886 dirtyreacts++;
887 uvm_pageactivate(p);
888 mutex_exit(slock);
889 continue;
890 }
891 mutex_exit(slock);
892
893 swapcluster_flush(&swc, false);
894 mutex_enter(&uvm_pageqlock);
895
896 /*
897 * the pageout is in progress. bump counters and set up
898 * for the next loop.
899 */
900
901 uvmexp.pdpending++;
902
903 #else /* defined(VMSWAP) */
904 uvm_pageactivate(p);
905 mutex_exit(slock);
906 #endif /* defined(VMSWAP) */
907 }
908
909 #if defined(VMSWAP)
910 mutex_exit(&uvm_pageqlock);
911 swapcluster_flush(&swc, true);
912 mutex_enter(&uvm_pageqlock);
913 #endif /* defined(VMSWAP) */
914 }
915
916 /*
917 * uvmpd_scan: scan the page queues and attempt to meet our targets.
918 *
919 * => called with pageq's locked
920 */
921
922 static void
923 uvmpd_scan(void)
924 {
925 int swap_shortage, pages_freed;
926 UVMHIST_FUNC("uvmpd_scan"); UVMHIST_CALLED(pdhist);
927
928 uvmexp.pdrevs++;
929
930 /*
931 * work on meeting our targets. first we work on our free target
932 * by converting inactive pages into free pages. then we work on
933 * meeting our inactive target by converting active pages to
934 * inactive ones.
935 */
936
937 UVMHIST_LOG(pdhist, " starting 'free' loop",0,0,0,0);
938
939 pages_freed = uvmexp.pdfreed;
940 uvmpd_scan_queue();
941 pages_freed = uvmexp.pdfreed - pages_freed;
942
943 /*
944 * detect if we're not going to be able to page anything out
945 * until we free some swap resources from active pages.
946 */
947
948 swap_shortage = 0;
949 if (uvmexp.free < uvmexp.freetarg &&
950 uvmexp.swpginuse >= uvmexp.swpgavail &&
951 !uvm_swapisfull() &&
952 pages_freed == 0) {
953 swap_shortage = uvmexp.freetarg - uvmexp.free;
954 }
955
956 uvmpdpol_balancequeue(swap_shortage);
957
958 /*
959 * if still below the minimum target, try unloading kernel
960 * modules.
961 */
962
963 if (uvmexp.free < uvmexp.freemin) {
964 module_thread_kick();
965 }
966 }
967
968 /*
969 * uvm_reclaimable: decide whether to wait for pagedaemon.
970 *
971 * => return true if it seems to be worth to do uvm_wait.
972 *
973 * XXX should be tunable.
974 * XXX should consider pools, etc?
975 */
976
977 bool
978 uvm_reclaimable(void)
979 {
980 int filepages;
981 int active, inactive;
982
983 /*
984 * if swap is not full, no problem.
985 */
986
987 if (!uvm_swapisfull()) {
988 return true;
989 }
990
991 /*
992 * file-backed pages can be reclaimed even when swap is full.
993 * if we have more than 1/16 of pageable memory or 5MB, try to reclaim.
994 *
995 * XXX assume the worst case, ie. all wired pages are file-backed.
996 *
997 * XXX should consider about other reclaimable memory.
998 * XXX ie. pools, traditional buffer cache.
999 */
1000
1001 filepages = uvmexp.filepages + uvmexp.execpages - uvmexp.wired;
1002 uvm_estimatepageable(&active, &inactive);
1003 if (filepages >= MIN((active + inactive) >> 4,
1004 5 * 1024 * 1024 >> PAGE_SHIFT)) {
1005 return true;
1006 }
1007
1008 /*
1009 * kill the process, fail allocation, etc..
1010 */
1011
1012 return false;
1013 }
1014
1015 void
1016 uvm_estimatepageable(int *active, int *inactive)
1017 {
1018
1019 uvmpdpol_estimatepageable(active, inactive);
1020 }
1021
1022 void
1023 uvm_reclaim_init(void)
1024 {
1025
1026 /* Initialize UVM reclaim hooks. */
1027 mutex_init(&uvm_reclaim_lock, MUTEX_DEFAULT, IPL_NONE);
1028 SLIST_INIT(&uvm_reclaim_list);
1029 }
1030
1031 void
1032 uvm_reclaim_hook_add(struct uvm_reclaim_hook *hook)
1033 {
1034
1035 KASSERT(hook != NULL);
1036
1037 mutex_enter(&uvm_reclaim_lock);
1038 SLIST_INSERT_HEAD(&uvm_reclaim_list, hook, uvm_reclaim_next);
1039 mutex_exit(&uvm_reclaim_lock);
1040 }
1041
1042 void
1043 uvm_reclaim_hook_del(struct uvm_reclaim_hook *hook_entry)
1044 {
1045 struct uvm_reclaim_hook *hook;
1046
1047 KASSERT(hook_entry != NULL);
1048
1049 mutex_enter(&uvm_reclaim_lock);
1050 SLIST_FOREACH(hook, &uvm_reclaim_list, uvm_reclaim_next) {
1051 if (hook != hook_entry) {
1052 continue;
1053 }
1054
1055 SLIST_REMOVE(&uvm_reclaim_list, hook, uvm_reclaim_hook,
1056 uvm_reclaim_next);
1057 break;
1058 }
1059
1060 mutex_exit(&uvm_reclaim_lock);
1061 }
1062