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