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