uvm_pdaemon.c revision 1.12.2.3 1 /* $NetBSD: uvm_pdaemon.c,v 1.12.2.3 1999/04/09 04:46:43 chs Exp $ */
2
3 /*
4 * XXXCDC: "ROUGH DRAFT" QUALITY UVM PRE-RELEASE FILE!
5 * >>>USE AT YOUR OWN RISK, WORK IS NOT FINISHED<<<
6 */
7 /*
8 * Copyright (c) 1997 Charles D. Cranor and Washington University.
9 * Copyright (c) 1991, 1993, The Regents of the University of California.
10 *
11 * All rights reserved.
12 *
13 * This code is derived from software contributed to Berkeley by
14 * The Mach Operating System project at Carnegie-Mellon University.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by Charles D. Cranor,
27 * Washington University, the University of California, Berkeley and
28 * its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 *
45 * @(#)vm_pageout.c 8.5 (Berkeley) 2/14/94
46 * from: Id: uvm_pdaemon.c,v 1.1.2.32 1998/02/06 05:26:30 chs Exp
47 *
48 *
49 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
50 * All rights reserved.
51 *
52 * Permission to use, copy, modify and distribute this software and
53 * its documentation is hereby granted, provided that both the copyright
54 * notice and this permission notice appear in all copies of the
55 * software, derivative works or modified versions, and any portions
56 * thereof, and that both notices appear in supporting documentation.
57 *
58 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
59 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
60 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
61 *
62 * Carnegie Mellon requests users of this software to return to
63 *
64 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
65 * School of Computer Science
66 * Carnegie Mellon University
67 * Pittsburgh PA 15213-3890
68 *
69 * any improvements or extensions that they make and grant Carnegie the
70 * rights to redistribute these changes.
71 */
72
73 #include "opt_uvmhist.h"
74
75 /*
76 * uvm_pdaemon.c: the page daemon
77 */
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
85 #include <vm/vm.h>
86 #include <vm/vm_page.h>
87 #include <vm/vm_kern.h>
88
89 #include <uvm/uvm.h>
90
91 /*
92 * local prototypes
93 */
94
95 static void uvmpd_scan __P((void));
96 static boolean_t uvmpd_scan_inactive __P((struct pglist *));
97 static void uvmpd_tune __P((void));
98
99
100 /*
101 * uvm_wait: wait (sleep) for the page daemon to free some pages
102 *
103 * => should be called with all locks released
104 * => should _not_ be called by the page daemon (to avoid deadlock)
105 */
106
107 void
108 uvm_wait(wmsg)
109 char *wmsg;
110 {
111 int timo = 0;
112 int s = splbio();
113
114 /*
115 * check for page daemon going to sleep (waiting for itself)
116 */
117
118 if (curproc == uvm.pagedaemon_proc) {
119 /*
120 * now we have a problem: the pagedaemon wants to go to
121 * sleep until it frees more memory. but how can it
122 * free more memory if it is asleep? that is a deadlock.
123 * we have two options:
124 * [1] panic now
125 * [2] put a timeout on the sleep, thus causing the
126 * pagedaemon to only pause (rather than sleep forever)
127 *
128 * note that option [2] will only help us if we get lucky
129 * and some other process on the system breaks the deadlock
130 * by exiting or freeing memory (thus allowing the pagedaemon
131 * to continue). for now we panic if DEBUG is defined,
132 * otherwise we hope for the best with option [2] (better
133 * yet, this should never happen in the first place!).
134 */
135
136 printf("pagedaemon: deadlock detected!\n");
137 timo = hz >> 3; /* set timeout */
138 #if defined(DEBUG)
139 /* DEBUG: panic so we can debug it */
140 panic("pagedaemon deadlock");
141 #endif
142 }
143
144 simple_lock(&uvm.pagedaemon_lock);
145 wakeup(&uvm.pagedaemon); /* wake the daemon! */
146 UVM_UNLOCK_AND_WAIT(&uvmexp.free, &uvm.pagedaemon_lock, FALSE, wmsg,
147 timo);
148
149 splx(s);
150 }
151
152
153 /*
154 * uvmpd_tune: tune paging parameters
155 *
156 * => called when ever memory is added (or removed?) to the system
157 * => caller must call with page queues locked
158 */
159
160 static void
161 uvmpd_tune()
162 {
163 UVMHIST_FUNC("uvmpd_tune"); UVMHIST_CALLED(pdhist);
164
165 uvmexp.freemin = uvmexp.npages / 20;
166
167 /* between 16k and 256k */
168 /* XXX: what are these values good for? */
169 uvmexp.freemin = max(uvmexp.freemin, (16*1024) >> PAGE_SHIFT);
170 uvmexp.freemin = min(uvmexp.freemin, (256*1024) >> PAGE_SHIFT);
171
172 uvmexp.freetarg = (uvmexp.freemin * 4) / 3;
173 if (uvmexp.freetarg <= uvmexp.freemin)
174 uvmexp.freetarg = uvmexp.freemin + 1;
175
176 /* uvmexp.inactarg: computed in main daemon loop */
177
178 uvmexp.wiredmax = uvmexp.npages / 3;
179 UVMHIST_LOG(pdhist, "<- done, freemin=%d, freetarg=%d, wiredmax=%d",
180 uvmexp.freemin, uvmexp.freetarg, uvmexp.wiredmax, 0);
181 }
182
183 /*
184 * uvm_pageout: the main loop for the pagedaemon
185 */
186
187 void
188 uvm_pageout()
189 {
190 int npages = 0;
191 UVMHIST_FUNC("uvm_pageout"); UVMHIST_CALLED(pdhist);
192
193 UVMHIST_LOG(pdhist,"<starting uvm pagedaemon>", 0, 0, 0, 0);
194
195 /*
196 * ensure correct priority and set paging parameters...
197 */
198
199 uvm.pagedaemon_proc = curproc;
200 (void) spl0();
201 uvm_lock_pageq();
202 npages = uvmexp.npages;
203 uvmpd_tune();
204 uvm_unlock_pageq();
205
206 /*
207 * main loop
208 */
209 while (TRUE) {
210
211 simple_lock(&uvm.pagedaemon_lock);
212
213 UVMHIST_LOG(pdhist," <<SLEEPING>>",0,0,0,0);
214 UVM_UNLOCK_AND_WAIT(&uvm.pagedaemon,
215 &uvm.pagedaemon_lock, FALSE, "pgdaemon", 0);
216 UVMHIST_LOG(pdhist," <<WOKE UP>>",0,0,0,0);
217
218 simple_unlock(&uvm.pagedaemon_lock);
219
220 /* drain pool resources */
221 pool_drain(0);
222
223 /*
224 * now lock page queues and recompute inactive count
225 */
226 uvm_lock_pageq();
227
228 if (npages != uvmexp.npages) { /* check for new pages? */
229 npages = uvmexp.npages;
230 uvmpd_tune();
231 }
232
233 uvmexp.inactarg = (uvmexp.active + uvmexp.inactive) / 3;
234 if (uvmexp.inactarg <= uvmexp.freetarg)
235 uvmexp.inactarg = uvmexp.freetarg + 1;
236
237 UVMHIST_LOG(pdhist," free/ftarg=%d/%d, inact/itarg=%d/%d",
238 uvmexp.free, uvmexp.freetarg, uvmexp.inactive,
239 uvmexp.inactarg);
240
241 /*
242 * scan if needed
243 */
244 if (uvmexp.free + uvmexp.paging < uvmexp.freetarg ||
245 uvmexp.inactive < uvmexp.inactarg) {
246 uvmpd_scan();
247 }
248
249 /*
250 * if there's any free memory to be had,
251 * wake up any waiters.
252 */
253 wakeup(&uvmexp.free);
254
255 /*
256 * done scan. unlock page queues (the only lock we are holding)
257 */
258 uvm_unlock_pageq();
259 }
260 /*NOTREACHED*/
261 }
262
263
264 /*
265 * uvm_aiodone_daemon: main loop for the aiodone daemon.
266 */
267
268 void
269 uvm_aiodone_daemon()
270 {
271 int s;
272 struct uvm_aiodesc *aio, *nextaio;
273 UVMHIST_FUNC("uvm_aiodoned"); UVMHIST_CALLED(pdhist);
274
275 UVMHIST_LOG(pdhist,"<starting uvm aiodone daemon>", 0, 0, 0, 0);
276
277 for (;;) {
278
279 /*
280 * carefully attempt to go to sleep (without losing "wakeups"!).
281 * we need splbio because we want to make sure the aio_done list
282 * is totally empty before we go to sleep.
283 */
284
285 s = splbio();
286 simple_lock(&uvm.aiodoned_lock);
287
288 /*
289 * if we've got done aio's, then bypass the sleep
290 */
291
292 if (TAILQ_FIRST(&uvm.aio_done) == NULL) {
293 UVMHIST_LOG(pdhist," <<SLEEPING>>",0,0,0,0);
294 UVM_UNLOCK_AND_WAIT(&uvm.aiodoned,
295 &uvm.aiodoned_lock, FALSE, "aiodoned", 0);
296 UVMHIST_LOG(pdhist," <<WOKE UP>>",0,0,0,0);
297
298 /* relock aiodoned_lock, still at splbio */
299 simple_lock(&uvm.aiodoned_lock);
300 }
301
302 /*
303 * check for done aio structures
304 */
305
306 aio = TAILQ_FIRST(&uvm.aio_done);
307 if (aio) {
308 TAILQ_INIT(&uvm.aio_done);
309 }
310
311 simple_unlock(&uvm.aiodoned_lock);
312 splx(s);
313
314 /*
315 * process each i/o that's done.
316 */
317
318 for (/*null*/; aio != NULL ; aio = nextaio) {
319 uvmexp.paging -= aio->npages;
320 nextaio = TAILQ_NEXT(aio, aioq);
321 aio->aiodone(aio);
322 }
323 }
324 }
325
326
327
328 /*
329 * uvmpd_scan_inactive: the first loop of uvmpd_scan broken out into
330 * its own function for ease of reading.
331 *
332 * => called with page queues locked
333 * => we work on meeting our free target by converting inactive pages
334 * into free pages.
335 * => we handle the building of swap-backed clusters
336 * => we return TRUE if we are exiting because we met our target
337 */
338
339 static boolean_t
340 uvmpd_scan_inactive(pglst)
341 struct pglist *pglst;
342 {
343 boolean_t retval = FALSE; /* assume we haven't hit target */
344 int s, free, result;
345 struct vm_page *p, *nextpg;
346 struct uvm_object *uobj;
347 struct vm_page *pps[MAXBSIZE >> PAGE_SHIFT], **ppsp;
348 int npages;
349 struct vm_page *swpps[MAXBSIZE >> PAGE_SHIFT]; /* XXX: see below */
350 int swnpages, swcpages; /* XXX: see below */
351 int swslot, oldslot;
352 struct vm_anon *anon;
353 boolean_t swap_backed;
354 vaddr_t start;
355 UVMHIST_FUNC("uvmpd_scan_inactive"); UVMHIST_CALLED(pdhist);
356
357 /*
358 * note: we currently keep swap-backed pages on a seperate inactive
359 * list from object-backed pages. however, merging the two lists
360 * back together again hasn't been ruled out. thus, we keep our
361 * swap cluster in "swpps" rather than in pps (allows us to mix
362 * clustering types in the event of a mixed inactive queue).
363 */
364
365 /*
366 * swslot is non-zero if we are building a swap cluster. we want
367 * to stay in the loop while we have a page to scan or we have
368 * a swap-cluster to build.
369 */
370 swslot = 0;
371 swnpages = swcpages = 0;
372 free = 0;
373
374 for (p = TAILQ_FIRST(pglst); p != NULL || swslot != 0; p = nextpg) {
375
376 /*
377 * note that p can be NULL iff we have traversed the whole
378 * list and need to do one final swap-backed clustered pageout.
379 */
380 if (p) {
381 /*
382 * update our copy of "free" and see if we've met
383 * our target
384 */
385 s = splimp();
386 uvm_lock_fpageq();
387 free = uvmexp.free;
388 uvm_unlock_fpageq();
389 splx(s);
390
391 if (free >= uvmexp.freetarg) {
392 UVMHIST_LOG(pdhist," met free target: "
393 "exit loop", 0, 0, 0, 0);
394 retval = TRUE; /* hit the target! */
395
396 if (swslot == 0)
397 /* exit now if no swap-i/o pending */
398 break;
399
400 /* set p to null to signal final swap i/o */
401 p = NULL;
402 }
403 }
404
405 uobj = NULL; /* be safe and shut gcc up */
406 anon = NULL; /* be safe and shut gcc up */
407
408 if (p) { /* if (we have a new page to consider) */
409 /*
410 * we are below target and have a new page to consider.
411 */
412 uvmexp.pdscans++;
413 nextpg = TAILQ_NEXT(p, pageq);
414
415 #if 1
416 /*
417 * XXX
418 * commented this out because the only way a page
419 * can be referenced and still on the inactive queue
420 * if it is "referenced" by the process of
421 * paging it out. we shouldn't count these
422 * as real references, so this code must be a noop.
423 *
424 * XXX shouldn't this bit be in the *ACTIVE* queue
425 * scanning code?
426 */
427
428 /*
429 * move referenced pages back to active queue and
430 * skip to next page (unlikely to happen since
431 * inactive pages shouldn't have any valid mappings
432 * and we cleared reference before deactivating).
433 */
434 if (pmap_is_referenced(PMAP_PGARG(p))) {
435 uvm_pageactivate(p);
436 uvmexp.pdreact++;
437 continue;
438 }
439 #endif
440
441 /*
442 * first we attempt to lock the object that this page
443 * belongs to. if our attempt fails we skip on to
444 * the next page (no harm done). it is important to
445 * "try" locking the object as we are locking in the
446 * wrong order (pageq -> object) and we don't want to
447 * get deadlocked.
448 *
449 * the only time we exepct to see an ownerless page
450 * (i.e. a page with no uobject and !PQ_ANON) is if an
451 * anon has loaned a page from a uvm_object and the
452 * uvm_object has dropped the ownership. in that
453 * case, the anon can "take over" the loaned page
454 * and make it its own.
455 */
456
457 /* is page part of an anon or ownerless ? */
458 if ((p->pqflags & PQ_ANON) || p->uobject == NULL) {
459
460 anon = p->uanon;
461
462 #ifdef DIAGNOSTIC
463 /* to be on inactive q, page must be part
464 * of _something_ */
465 if (anon == NULL)
466 panic("pagedaemon: page with no anon "
467 "or object detected - loop 1");
468 #endif
469
470 if (!simple_lock_try(&anon->an_lock))
471 /* lock failed, skip this page */
472 continue;
473
474 /*
475 * if the page is ownerless, claim it in the
476 * name of "anon"!
477 */
478 if ((p->pqflags & PQ_ANON) == 0) {
479 #ifdef DIAGNOSTIC
480 if (p->loan_count < 1)
481 panic("pagedaemon: non-loaned "
482 "ownerless page detected -"
483 " loop 1");
484 #endif
485 p->loan_count--;
486 p->pqflags |= PQ_ANON; /* anon now owns it */
487 }
488
489 if (p->flags & PG_BUSY) {
490 simple_unlock(&anon->an_lock);
491 uvmexp.pdbusy++;
492 /* someone else owns page, skip it */
493 continue;
494 }
495
496 uvmexp.pdanscan++;
497
498 } else {
499
500 uobj = p->uobject;
501
502 if (!simple_lock_try(&uobj->vmobjlock))
503 /* lock failed, skip this page */
504 continue;
505
506 if (p->flags & PG_BUSY) {
507 simple_unlock(&uobj->vmobjlock);
508 uvmexp.pdbusy++;
509 /* someone else owns page, skip it */
510 continue;
511 }
512
513 uvmexp.pdobscan++;
514 }
515
516 /*
517 * we now have the object and the page queues locked.
518 * the page is not busy. if the page is clean we
519 * can free it now and continue.
520 */
521
522 if (p->flags & PG_CLEAN) {
523
524 if (p->pqflags & PQ_SWAPBACKED) {
525 /* this page now lives only in swap */
526 uvmexp.swpguniq++;
527 }
528
529 /* zap all mappings with pmap_page_protect... */
530 pmap_page_protect(PMAP_PGARG(p), VM_PROT_NONE);
531 uvm_pagefree(p);
532 uvmexp.pdfreed++;
533
534 if (anon) {
535 #ifdef DIAGNOSTIC
536 /*
537 * an anonymous page can only be clean
538 * if it has valid backing store.
539 */
540 if (anon->an_swslot == 0)
541 panic("pagedaemon: clean anon "
542 "page without backing store?");
543 #endif
544 /* remove from object */
545 anon->u.an_page = NULL;
546 simple_unlock(&anon->an_lock);
547 } else {
548 /* pagefree has already removed the
549 * page from the object */
550 simple_unlock(&uobj->vmobjlock);
551 }
552 continue;
553 }
554
555 /*
556 * this page is dirty, skip it if we'll have met our
557 * free target when all the current pageouts complete.
558 */
559
560 if (free + uvmexp.paging > uvmexp.freetarg << 2) {
561 if (anon) {
562 simple_unlock(&anon->an_lock);
563 } else {
564 simple_unlock(&uobj->vmobjlock);
565 }
566 continue;
567 }
568
569 /*
570 * the page we are looking at is dirty. we must
571 * clean it before it can be freed. to do this we
572 * first mark the page busy so that no one else will
573 * touch the page. we write protect all the mappings
574 * of the page so that no one touches it while it is
575 * in I/O.
576 */
577
578 swap_backed = ((p->pqflags & PQ_SWAPBACKED) != 0);
579 p->flags |= PG_BUSY; /* now we own it */
580 UVM_PAGE_OWN(p, "scan_inactive");
581 pmap_page_protect(PMAP_PGARG(p), VM_PROT_READ);
582 uvmexp.pgswapout++;
583
584 /*
585 * for swap-backed pages we need to (re)allocate
586 * swap space.
587 */
588 if (swap_backed) {
589
590 /*
591 * free old swap slot (if any)
592 */
593 if (anon) {
594 if (anon->an_swslot) {
595 uvm_swap_free(anon->an_swslot,
596 1);
597 anon->an_swslot = 0;
598 }
599 } else {
600 oldslot = uao_set_swslot(uobj,
601 p->offset >> PAGE_SHIFT, 0);
602
603 if (oldslot)
604 uvm_swap_free(oldslot, 1);
605 }
606
607 /*
608 * start new cluster (if necessary)
609 */
610 if (swslot == 0) {
611 /* want this much */
612 swnpages = MAXBSIZE >> PAGE_SHIFT;
613
614 swslot = uvm_swap_alloc(&swnpages,
615 TRUE);
616
617 if (swslot == 0) {
618 /* no swap? give up! */
619 p->flags &= ~PG_BUSY;
620 UVM_PAGE_OWN(p, NULL);
621 if (anon)
622 simple_unlock(
623 &anon->an_lock);
624 else
625 simple_unlock(
626 &uobj->vmobjlock);
627 continue;
628 }
629 swcpages = 0; /* cluster is empty */
630 }
631
632 /*
633 * add block to cluster
634 */
635 swpps[swcpages] = p;
636 uvmexp.pgswapout++;
637 if (anon)
638 anon->an_swslot = swslot + swcpages;
639 else
640 uao_set_swslot(uobj,
641 p->offset >> PAGE_SHIFT,
642 swslot + swcpages);
643 swcpages++;
644
645 /* done (swap-backed) */
646 }
647
648 /* end: if (p) ["if we have new page to consider"] */
649 } else {
650
651 /* if p == NULL we must be doing a last swap i/o */
652 swap_backed = TRUE;
653 }
654
655 /*
656 * now consider doing the pageout.
657 *
658 * for swap-backed pages, we do the pageout if we have either
659 * filled the cluster (in which case (swnpages == swcpages) or
660 * run out of pages (p == NULL).
661 *
662 * for object pages, we always do the pageout.
663 */
664 if (swap_backed) {
665
666 if (p) { /* if we just added a page to cluster */
667 if (anon)
668 simple_unlock(&anon->an_lock);
669 else
670 simple_unlock(&uobj->vmobjlock);
671
672 /* cluster not full yet? */
673 if (swcpages < swnpages)
674 continue;
675 }
676
677 /* starting I/O now... set up for it */
678 npages = swcpages;
679 ppsp = swpps;
680 /* for swap-backed pages only */
681 start = (vaddr_t) swslot;
682
683 /* if this is final pageout we could have a few
684 * extra swap blocks */
685 if (swcpages < swnpages) {
686 uvm_swap_free(swslot + swcpages,
687 (swnpages - swcpages));
688 }
689
690 } else {
691
692 /* normal object pageout */
693 ppsp = pps;
694 npages = sizeof(pps) / sizeof(struct vm_page *);
695 /* not looked at because PGO_ALLPAGES is set */
696 start = 0;
697
698 }
699
700 /*
701 * now do the pageout.
702 *
703 * for swap_backed pages we have already built the cluster.
704 * for !swap_backed pages, uvm_pager_put will call the object's
705 * "make put cluster" function to build a cluster on our behalf.
706 *
707 * we pass the PGO_PDFREECLUST flag to uvm_pager_put to instruct
708 * it to free the cluster pages for us on a successful I/O (it
709 * always does this for un-successful I/O requests). this
710 * allows us to do clustered pageout without having to deal
711 * with cluster pages at this level.
712 *
713 * note locking semantics of uvm_pager_put with PGO_PDFREECLUST:
714 * IN: locked: uobj (if !swap_backed), page queues
715 * OUT: locked: uobj (if !swap_backed && result !=VM_PAGER_PEND)
716 * !locked: pageqs, uobj (if swap_backed || VM_PAGER_PEND)
717 *
718 * [the bit about VM_PAGER_PEND saves us one lock-unlock pair]
719 */
720
721 /* locked: uobj (if !swap_backed), page queues */
722 uvmexp.pdpageouts++;
723 result = uvm_pager_put((swap_backed) ? NULL : uobj, p,
724 &ppsp, &npages, PGO_ALLPAGES|PGO_PDFREECLUST, start, 0);
725 /* locked: uobj (if !swap_backed && result != PEND) */
726 /* unlocked: pageqs, object (if swap_backed ||result == PEND) */
727
728 /*
729 * if we did i/o to swap, zero swslot to indicate that we are
730 * no longer building a swap-backed cluster.
731 */
732
733 if (swap_backed)
734 swslot = 0; /* done with this cluster */
735
736 /*
737 * first, we check for VM_PAGER_PEND which means that the
738 * async I/O is in progress and the async I/O done routine
739 * will clean up after us. in this case we move on to the
740 * next page.
741 *
742 * there is a very remote chance that the pending async i/o can
743 * finish _before_ we get here. if that happens, our page "p"
744 * may no longer be on the inactive queue. so we verify this
745 * when determining the next page (starting over at the head if
746 * we've lost our inactive page).
747 */
748
749 if (result == VM_PAGER_PEND) {
750 uvmexp.paging += npages;
751 uvm_lock_pageq(); /* relock page queues */
752 uvmexp.pdpending++;
753 if (p) {
754 if (p->pqflags & PQ_INACTIVE)
755 /* reload! */
756 nextpg = TAILQ_NEXT(p, pageq);
757 else
758 /* reload! */
759 nextpg = TAILQ_FIRST(pglst);
760 } else {
761 nextpg = NULL; /* done list */
762 }
763 continue;
764 }
765
766 /*
767 * clean up "p" if we have one
768 */
769
770 if (p) {
771 /*
772 * the I/O request to "p" is done and uvm_pager_put
773 * has freed any cluster pages it may have allocated
774 * during I/O. all that is left for us to do is
775 * clean up page "p" (which is still PG_BUSY).
776 *
777 * our result could be one of the following:
778 * VM_PAGER_OK: successful pageout
779 *
780 * VM_PAGER_AGAIN: tmp resource shortage, we skip
781 * to next page
782 * VM_PAGER_{FAIL,ERROR,BAD}: an error. we
783 * "reactivate" page to get it out of the way (it
784 * will eventually drift back into the inactive
785 * queue for a retry).
786 * VM_PAGER_UNLOCK: should never see this as it is
787 * only valid for "get" operations
788 */
789
790 /* relock p's object: page queues not lock yet, so
791 * no need for "try" */
792
793 /* !swap_backed case: already locked... */
794 if (swap_backed) {
795 if (anon)
796 simple_lock(&anon->an_lock);
797 else
798 simple_lock(&uobj->vmobjlock);
799 }
800
801 #ifdef DIAGNOSTIC
802 if (result == VM_PAGER_UNLOCK)
803 panic("pagedaemon: pageout returned "
804 "invalid 'unlock' code");
805 #endif
806
807 /* handle PG_WANTED now */
808 if (p->flags & PG_WANTED)
809 /* still holding object lock */
810 wakeup(p);
811
812 p->flags &= ~(PG_BUSY|PG_WANTED);
813 UVM_PAGE_OWN(p, NULL);
814
815 /* released during I/O? */
816 if (p->flags & PG_RELEASED) {
817 if (anon) {
818 /* remove page so we can get nextpg */
819 anon->u.an_page = NULL;
820
821 simple_unlock(&anon->an_lock);
822 uvm_anfree(anon); /* kills anon */
823 pmap_page_protect(PMAP_PGARG(p),
824 VM_PROT_NONE);
825 anon = NULL;
826 uvm_lock_pageq();
827 nextpg = TAILQ_NEXT(p, pageq);
828 /* free released page */
829 uvm_pagefree(p);
830
831 } else {
832
833 #ifdef DIAGNOSTIC
834 if (uobj->pgops->pgo_releasepg == NULL)
835 panic("pagedaemon: no "
836 "pgo_releasepg function");
837 #endif
838
839 /*
840 * pgo_releasepg nukes the page and
841 * gets "nextpg" for us. it returns
842 * with the page queues locked (when
843 * given nextpg ptr).
844 */
845 if (!uobj->pgops->pgo_releasepg(p,
846 &nextpg))
847 /* uobj died after release */
848 uobj = NULL;
849
850 /*
851 * lock page queues here so that they're
852 * always locked at the end of the loop.
853 */
854 uvm_lock_pageq();
855 }
856
857 } else { /* page was not released during I/O */
858
859 uvm_lock_pageq();
860 nextpg = TAILQ_NEXT(p, pageq);
861
862 if (result != VM_PAGER_OK) {
863
864 /* pageout was a failure... */
865 if (result != VM_PAGER_AGAIN)
866 uvm_pageactivate(p);
867 pmap_clear_reference(PMAP_PGARG(p));
868 /* XXXCDC: if (swap_backed) FREE p's
869 * swap block? */
870
871 } else {
872
873 /* pageout was a success... */
874 pmap_clear_reference(PMAP_PGARG(p));
875 pmap_clear_modify(PMAP_PGARG(p));
876 p->flags |= PG_CLEAN;
877 /* XXX: could free page here, but old
878 * pagedaemon does not */
879
880 }
881 }
882
883 /*
884 * drop object lock (if there is an object left). do
885 * a safety check of nextpg to make sure it is on the
886 * inactive queue (it should be since PG_BUSY pages on
887 * the inactive queue can't be re-queued [note: not
888 * true for active queue]).
889 */
890
891 if (anon)
892 simple_unlock(&anon->an_lock);
893 else if (uobj)
894 simple_unlock(&uobj->vmobjlock);
895
896 } /* if (p) */ else {
897
898 /* if p is null in this loop, make sure it stays null
899 * in next loop */
900 nextpg = NULL;
901
902 /*
903 * lock page queues here just so they're always locked
904 * at the end of the loop.
905 */
906 uvm_lock_pageq();
907 }
908
909 if (nextpg && (nextpg->pqflags & PQ_INACTIVE) == 0) {
910 printf("pagedaemon: invalid nextpg! reverting to "
911 "queue head\n");
912 nextpg = TAILQ_FIRST(pglst); /* reload! */
913 }
914
915 } /* end of "inactive" 'for' loop */
916 return (retval);
917 }
918
919 /*
920 * uvmpd_scan: scan the page queues and attempt to meet our targets.
921 *
922 * => called with pageq's locked
923 */
924
925 void
926 uvmpd_scan()
927 {
928 int s, free, inactive_shortage, swap_shortage;
929 struct vm_page *p, *nextpg;
930 struct uvm_object *uobj;
931 boolean_t got_it;
932 UVMHIST_FUNC("uvmpd_scan"); UVMHIST_CALLED(pdhist);
933
934 uvmexp.pdrevs++; /* counter */
935
936 #ifdef __GNUC__
937 uobj = NULL; /* XXX gcc */
938 #endif
939 /*
940 * get current "free" page count
941 */
942 s = splimp();
943 uvm_lock_fpageq();
944 free = uvmexp.free;
945 uvm_unlock_fpageq();
946 splx(s);
947
948 #ifndef __SWAP_BROKEN
949 /*
950 * swap out some processes if we are below our free target.
951 * we need to unlock the page queues for this.
952 */
953 if (free < uvmexp.freetarg) {
954
955 uvmexp.pdswout++;
956 UVMHIST_LOG(pdhist," free %d < target %d: swapout", free,
957 uvmexp.freetarg, 0, 0);
958 uvm_unlock_pageq();
959 uvm_swapout_threads();
960 pmap_update(); /* update so we can scan inactive q */
961 uvm_lock_pageq();
962
963 }
964 #endif
965
966 /*
967 * now we want to work on meeting our targets. first we work on our
968 * free target by converting inactive pages into free pages. then
969 * we work on meeting our inactive target by converting active pages
970 * to inactive ones.
971 */
972
973 UVMHIST_LOG(pdhist, " starting 'free' loop",0,0,0,0);
974
975 /*
976 * do loop #1! alternate starting queue between swap and object based
977 * on the low bit of uvmexp.pdrevs (which we bump by one each call).
978 */
979
980 got_it = FALSE;
981 if ((uvmexp.pdrevs & 1) != 0 && uvmexp.nswapdev != 0)
982 got_it = uvmpd_scan_inactive(&uvm.page_inactive_swp);
983 if (!got_it)
984 got_it = uvmpd_scan_inactive(&uvm.page_inactive_obj);
985 if (!got_it && (uvmexp.pdrevs & 1) == 0 && uvmexp.nswapdev != 0)
986 (void) uvmpd_scan_inactive(&uvm.page_inactive_swp);
987
988 /*
989 * we have done the scan to get free pages. now we work on meeting
990 * our inactive target. if we are still below the free target
991 * and we didn't start any pageouts in the inactive scan above
992 * (perhaps because we're out of swap space) and we've met
993 * the inactive target, then go ahead and deactivate some more
994 * pages anyway. meeting the free target is important enough
995 * that it's worth temporarily reducing the number of active pages.
996 */
997
998 inactive_shortage = uvmexp.inactarg - uvmexp.inactive;
999 if (free < uvmexp.freetarg && inactive_shortage <= 0 &&
1000 uvmexp.paging == 0) {
1001 inactive_shortage = 16;
1002 }
1003
1004 /*
1005 * detect if we're not going to be able to page anything out
1006 * until we free some swap resources from active pages.
1007 */
1008 swap_shortage = 0;
1009 if (uvmexp.free < uvmexp.freetarg &&
1010 uvmexp.swpginuse == uvmexp.swpages &&
1011 uvmexp.swpguniq < uvmexp.swpages &&
1012 uvmexp.paging == 0) {
1013 swap_shortage = uvmexp.freetarg - uvmexp.free;
1014 }
1015
1016 UVMHIST_LOG(pdhist, " loop 2: inactive_shortage=%d swap_shortage=%d",
1017 inactive_shortage, swap_shortage,0,0);
1018 for (p = TAILQ_FIRST(&uvm.page_active);
1019 p != NULL && (inactive_shortage > 0 || swap_shortage > 0);
1020 p = nextpg) {
1021
1022 nextpg = TAILQ_NEXT(p, pageq);
1023 if (p->flags & PG_BUSY)
1024 continue; /* quick check before trying to lock */
1025
1026 /*
1027 * lock the page's owner.
1028 */
1029 /* is page anon owned or ownerless? */
1030 if ((p->pqflags & PQ_ANON) || p->uobject == NULL) {
1031 #ifdef DIAGNOSTIC
1032 if (p->uanon == NULL)
1033 panic("pagedaemon: page with no anon or "
1034 "object detected - loop 2");
1035 #endif
1036 if (!simple_lock_try(&p->uanon->an_lock))
1037 continue;
1038
1039 /* take over the page? */
1040 if ((p->pqflags & PQ_ANON) == 0) {
1041 #ifdef DIAGNOSTIC
1042 if (p->loan_count < 1)
1043 panic("pagedaemon: non-loaned "
1044 "ownerless page detected - loop 2");
1045 #endif
1046 p->loan_count--;
1047 p->pqflags |= PQ_ANON;
1048 }
1049 } else {
1050 if (!simple_lock_try(&p->uobject->vmobjlock))
1051 continue;
1052 }
1053
1054 /*
1055 * skip this page if it's busy.
1056 */
1057 if ((p->flags & PG_BUSY) != 0) {
1058 if (p->pqflags & PQ_ANON)
1059 simple_unlock(&p->uanon->an_lock);
1060 else
1061 simple_unlock(&p->uobject->vmobjlock);
1062 continue;
1063 }
1064
1065 /*
1066 * free any swap allocated to this page
1067 * if there's a shortage of swap.
1068 */
1069 if (swap_shortage > 0) {
1070 if (p->pqflags & PQ_ANON && p->uanon->an_swslot) {
1071 uvm_swap_free(p->uanon->an_swslot, 1);
1072 p->uanon->an_swslot = 0;
1073 p->flags &= ~PG_CLEAN;
1074 swap_shortage--;
1075 }
1076 if (p->pqflags & PQ_AOBJ) {
1077 int slot = uao_set_swslot(p->uobject,
1078 p->offset >> PAGE_SHIFT, 0);
1079 if (slot) {
1080 uvm_swap_free(slot, 1);
1081 p->flags &= ~PG_CLEAN;
1082 swap_shortage--;
1083 }
1084 }
1085 }
1086
1087 /*
1088 * deactivate this page if there's a shortage of
1089 * inactive pages.
1090 */
1091 if (inactive_shortage > 0) {
1092 pmap_page_protect(PMAP_PGARG(p), VM_PROT_NONE);
1093 /* no need to check wire_count as pg is "active" */
1094 uvm_pagedeactivate(p);
1095 uvmexp.pddeact++;
1096 inactive_shortage--;
1097 }
1098
1099 if (p->pqflags & PQ_ANON)
1100 simple_unlock(&p->uanon->an_lock);
1101 else
1102 simple_unlock(&p->uobject->vmobjlock);
1103 }
1104 }
1105