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