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