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