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