uvm_pdaemon.c revision 1.92 1 /* $NetBSD: uvm_pdaemon.c,v 1.92 2008/02/29 20:35:23 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 * Copyright (c) 1991, 1993, The Regents of the University of California.
6 *
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * The Mach Operating System project at Carnegie-Mellon University.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Charles D. Cranor,
23 * Washington University, the University of California, Berkeley and
24 * its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 * @(#)vm_pageout.c 8.5 (Berkeley) 2/14/94
42 * from: Id: uvm_pdaemon.c,v 1.1.2.32 1998/02/06 05:26:30 chs Exp
43 *
44 *
45 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46 * All rights reserved.
47 *
48 * Permission to use, copy, modify and distribute this software and
49 * its documentation is hereby granted, provided that both the copyright
50 * notice and this permission notice appear in all copies of the
51 * software, derivative works or modified versions, and any portions
52 * thereof, and that both notices appear in supporting documentation.
53 *
54 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
55 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
56 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
57 *
58 * Carnegie Mellon requests users of this software to return to
59 *
60 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
61 * School of Computer Science
62 * Carnegie Mellon University
63 * Pittsburgh PA 15213-3890
64 *
65 * any improvements or extensions that they make and grant Carnegie the
66 * rights to redistribute these changes.
67 */
68
69 /*
70 * uvm_pdaemon.c: the page daemon
71 */
72
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: uvm_pdaemon.c,v 1.92 2008/02/29 20:35:23 yamt Exp $");
75
76 #include "opt_uvmhist.h"
77 #include "opt_readahead.h"
78
79 #include <sys/param.h>
80 #include <sys/proc.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
83 #include <sys/pool.h>
84 #include <sys/buf.h>
85
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 * complete draining the pools.
332 */
333 pool_drain_end(pp, where);
334 }
335 /*NOTREACHED*/
336 }
337
338
339 /*
340 * uvm_aiodone_worker: a workqueue callback for the aiodone daemon.
341 */
342
343 void
344 uvm_aiodone_worker(struct work *wk, void *dummy)
345 {
346 struct buf *bp = (void *)wk;
347
348 KASSERT(&bp->b_work == wk);
349
350 /*
351 * process an i/o that's done.
352 */
353
354 (*bp->b_iodone)(bp);
355 }
356
357 void
358 uvm_pageout_start(int npages)
359 {
360
361 mutex_spin_enter(&uvm_fpageqlock);
362 uvmexp.paging += npages;
363 mutex_spin_exit(&uvm_fpageqlock);
364 }
365
366 void
367 uvm_pageout_done(int npages)
368 {
369
370 mutex_spin_enter(&uvm_fpageqlock);
371 KASSERT(uvmexp.paging >= npages);
372 uvmexp.paging -= npages;
373
374 /*
375 * wake up either of pagedaemon or LWPs waiting for it.
376 */
377
378 if (uvmexp.free <= uvmexp.reserve_kernel) {
379 wakeup(&uvm.pagedaemon);
380 } else {
381 wakeup(&uvmexp.free);
382 uvm_pagedaemon_waiters = 0;
383 }
384 mutex_spin_exit(&uvm_fpageqlock);
385 }
386
387 /*
388 * uvmpd_trylockowner: trylock the page's owner.
389 *
390 * => called with pageq locked.
391 * => resolve orphaned O->A loaned page.
392 * => return the locked mutex on success. otherwise, return NULL.
393 */
394
395 kmutex_t *
396 uvmpd_trylockowner(struct vm_page *pg)
397 {
398 struct uvm_object *uobj = pg->uobject;
399 kmutex_t *slock;
400
401 KASSERT(mutex_owned(&uvm_pageqlock));
402
403 if (uobj != NULL) {
404 slock = &uobj->vmobjlock;
405 } else {
406 struct vm_anon *anon = pg->uanon;
407
408 KASSERT(anon != NULL);
409 slock = &anon->an_lock;
410 }
411
412 if (!mutex_tryenter(slock)) {
413 return NULL;
414 }
415
416 if (uobj == NULL) {
417
418 /*
419 * set PQ_ANON if it isn't set already.
420 */
421
422 if ((pg->pqflags & PQ_ANON) == 0) {
423 KASSERT(pg->loan_count > 0);
424 pg->loan_count--;
425 pg->pqflags |= PQ_ANON;
426 /* anon now owns it */
427 }
428 }
429
430 return slock;
431 }
432
433 #if defined(VMSWAP)
434 struct swapcluster {
435 int swc_slot;
436 int swc_nallocated;
437 int swc_nused;
438 struct vm_page *swc_pages[howmany(MAXPHYS, MIN_PAGE_SIZE)];
439 };
440
441 static void
442 swapcluster_init(struct swapcluster *swc)
443 {
444
445 swc->swc_slot = 0;
446 swc->swc_nused = 0;
447 }
448
449 static int
450 swapcluster_allocslots(struct swapcluster *swc)
451 {
452 int slot;
453 int npages;
454
455 if (swc->swc_slot != 0) {
456 return 0;
457 }
458
459 /* Even with strange MAXPHYS, the shift
460 implicitly rounds down to a page. */
461 npages = MAXPHYS >> PAGE_SHIFT;
462 slot = uvm_swap_alloc(&npages, true);
463 if (slot == 0) {
464 return ENOMEM;
465 }
466 swc->swc_slot = slot;
467 swc->swc_nallocated = npages;
468 swc->swc_nused = 0;
469
470 return 0;
471 }
472
473 static int
474 swapcluster_add(struct swapcluster *swc, struct vm_page *pg)
475 {
476 int slot;
477 struct uvm_object *uobj;
478
479 KASSERT(swc->swc_slot != 0);
480 KASSERT(swc->swc_nused < swc->swc_nallocated);
481 KASSERT((pg->pqflags & PQ_SWAPBACKED) != 0);
482
483 slot = swc->swc_slot + swc->swc_nused;
484 uobj = pg->uobject;
485 if (uobj == NULL) {
486 KASSERT(mutex_owned(&pg->uanon->an_lock));
487 pg->uanon->an_swslot = slot;
488 } else {
489 int result;
490
491 KASSERT(mutex_owned(&uobj->vmobjlock));
492 result = uao_set_swslot(uobj, pg->offset >> PAGE_SHIFT, slot);
493 if (result == -1) {
494 return ENOMEM;
495 }
496 }
497 swc->swc_pages[swc->swc_nused] = pg;
498 swc->swc_nused++;
499
500 return 0;
501 }
502
503 static void
504 swapcluster_flush(struct swapcluster *swc, bool now)
505 {
506 int slot;
507 int nused;
508 int nallocated;
509 int error;
510
511 if (swc->swc_slot == 0) {
512 return;
513 }
514 KASSERT(swc->swc_nused <= swc->swc_nallocated);
515
516 slot = swc->swc_slot;
517 nused = swc->swc_nused;
518 nallocated = swc->swc_nallocated;
519
520 /*
521 * if this is the final pageout we could have a few
522 * unused swap blocks. if so, free them now.
523 */
524
525 if (nused < nallocated) {
526 if (!now) {
527 return;
528 }
529 uvm_swap_free(slot + nused, nallocated - nused);
530 }
531
532 /*
533 * now start the pageout.
534 */
535
536 if (nused > 0) {
537 uvmexp.pdpageouts++;
538 uvm_pageout_start(nused);
539 error = uvm_swap_put(slot, swc->swc_pages, nused, 0);
540 KASSERT(error == 0 || error == ENOMEM);
541 }
542
543 /*
544 * zero swslot to indicate that we are
545 * no longer building a swap-backed cluster.
546 */
547
548 swc->swc_slot = 0;
549 swc->swc_nused = 0;
550 }
551
552 static int
553 swapcluster_nused(struct swapcluster *swc)
554 {
555
556 return swc->swc_nused;
557 }
558
559 /*
560 * uvmpd_dropswap: free any swap allocated to this page.
561 *
562 * => called with owner locked.
563 * => return true if a page had an associated slot.
564 */
565
566 static bool
567 uvmpd_dropswap(struct vm_page *pg)
568 {
569 bool result = false;
570 struct vm_anon *anon = pg->uanon;
571
572 if ((pg->pqflags & PQ_ANON) && anon->an_swslot) {
573 uvm_swap_free(anon->an_swslot, 1);
574 anon->an_swslot = 0;
575 pg->flags &= ~PG_CLEAN;
576 result = true;
577 } else if (pg->pqflags & PQ_AOBJ) {
578 int slot = uao_set_swslot(pg->uobject,
579 pg->offset >> PAGE_SHIFT, 0);
580 if (slot) {
581 uvm_swap_free(slot, 1);
582 pg->flags &= ~PG_CLEAN;
583 result = true;
584 }
585 }
586
587 return result;
588 }
589
590 /*
591 * uvmpd_trydropswap: try to free any swap allocated to this page.
592 *
593 * => return true if a slot is successfully freed.
594 */
595
596 bool
597 uvmpd_trydropswap(struct vm_page *pg)
598 {
599 kmutex_t *slock;
600 bool result;
601
602 if ((pg->flags & PG_BUSY) != 0) {
603 return false;
604 }
605
606 /*
607 * lock the page's owner.
608 */
609
610 slock = uvmpd_trylockowner(pg);
611 if (slock == NULL) {
612 return false;
613 }
614
615 /*
616 * skip this page if it's busy.
617 */
618
619 if ((pg->flags & PG_BUSY) != 0) {
620 mutex_exit(slock);
621 return false;
622 }
623
624 result = uvmpd_dropswap(pg);
625
626 mutex_exit(slock);
627
628 return result;
629 }
630
631 #endif /* defined(VMSWAP) */
632
633 /*
634 * uvmpd_scan_queue: scan an replace candidate list for pages
635 * to clean or free.
636 *
637 * => called with page queues locked
638 * => we work on meeting our free target by converting inactive pages
639 * into free pages.
640 * => we handle the building of swap-backed clusters
641 */
642
643 static void
644 uvmpd_scan_queue(void)
645 {
646 struct vm_page *p;
647 struct uvm_object *uobj;
648 struct vm_anon *anon;
649 #if defined(VMSWAP)
650 struct swapcluster swc;
651 #endif /* defined(VMSWAP) */
652 int dirtyreacts;
653 int lockownerfail;
654 kmutex_t *slock;
655 UVMHIST_FUNC("uvmpd_scan_queue"); UVMHIST_CALLED(pdhist);
656
657 /*
658 * swslot is non-zero if we are building a swap cluster. we want
659 * to stay in the loop while we have a page to scan or we have
660 * a swap-cluster to build.
661 */
662
663 #if defined(VMSWAP)
664 swapcluster_init(&swc);
665 #endif /* defined(VMSWAP) */
666
667 dirtyreacts = 0;
668 lockownerfail = 0;
669 uvmpdpol_scaninit();
670
671 while (/* CONSTCOND */ 1) {
672
673 /*
674 * see if we've met the free target.
675 */
676
677 if (uvmexp.free + uvmexp.paging
678 #if defined(VMSWAP)
679 + swapcluster_nused(&swc)
680 #endif /* defined(VMSWAP) */
681 >= uvmexp.freetarg << 2 ||
682 dirtyreacts == UVMPD_NUMDIRTYREACTS) {
683 UVMHIST_LOG(pdhist," met free target: "
684 "exit loop", 0, 0, 0, 0);
685 break;
686 }
687
688 p = uvmpdpol_selectvictim();
689 if (p == NULL) {
690 break;
691 }
692 KASSERT(uvmpdpol_pageisqueued_p(p));
693 KASSERT(p->wire_count == 0);
694
695 /*
696 * we are below target and have a new page to consider.
697 */
698
699 anon = p->uanon;
700 uobj = p->uobject;
701
702 /*
703 * first we attempt to lock the object that this page
704 * belongs to. if our attempt fails we skip on to
705 * the next page (no harm done). it is important to
706 * "try" locking the object as we are locking in the
707 * wrong order (pageq -> object) and we don't want to
708 * deadlock.
709 *
710 * the only time we expect to see an ownerless page
711 * (i.e. a page with no uobject and !PQ_ANON) is if an
712 * anon has loaned a page from a uvm_object and the
713 * uvm_object has dropped the ownership. in that
714 * case, the anon can "take over" the loaned page
715 * and make it its own.
716 */
717
718 slock = uvmpd_trylockowner(p);
719 if (slock == NULL) {
720 /*
721 * yield cpu to make a chance for an LWP holding
722 * the lock run. otherwise we can busy-loop too long
723 * if the page queue is filled with a lot of pages
724 * from few objects.
725 */
726 lockownerfail++;
727 if (lockownerfail > UVMPD_NUMTRYLOCKOWNER) {
728 mutex_exit(&uvm_pageqlock);
729 /* XXX Better than yielding but inadequate. */
730 kpause("livelock", false, 1, NULL);
731 mutex_enter(&uvm_pageqlock);
732 lockownerfail = 0;
733 }
734 continue;
735 }
736 if (p->flags & PG_BUSY) {
737 mutex_exit(slock);
738 uvmexp.pdbusy++;
739 continue;
740 }
741
742 /* does the page belong to an object? */
743 if (uobj != NULL) {
744 uvmexp.pdobscan++;
745 } else {
746 #if defined(VMSWAP)
747 KASSERT(anon != NULL);
748 uvmexp.pdanscan++;
749 #else /* defined(VMSWAP) */
750 panic("%s: anon", __func__);
751 #endif /* defined(VMSWAP) */
752 }
753
754
755 /*
756 * we now have the object and the page queues locked.
757 * if the page is not swap-backed, call the object's
758 * pager to flush and free the page.
759 */
760
761 #if defined(READAHEAD_STATS)
762 if ((p->pqflags & PQ_READAHEAD) != 0) {
763 p->pqflags &= ~PQ_READAHEAD;
764 uvm_ra_miss.ev_count++;
765 }
766 #endif /* defined(READAHEAD_STATS) */
767
768 if ((p->pqflags & PQ_SWAPBACKED) == 0) {
769 KASSERT(uobj != NULL);
770 mutex_exit(&uvm_pageqlock);
771 (void) (uobj->pgops->pgo_put)(uobj, p->offset,
772 p->offset + PAGE_SIZE, PGO_CLEANIT|PGO_FREE);
773 mutex_enter(&uvm_pageqlock);
774 continue;
775 }
776
777 /*
778 * the page is swap-backed. remove all the permissions
779 * from the page so we can sync the modified info
780 * without any race conditions. if the page is clean
781 * we can free it now and continue.
782 */
783
784 pmap_page_protect(p, VM_PROT_NONE);
785 if ((p->flags & PG_CLEAN) && pmap_clear_modify(p)) {
786 p->flags &= ~(PG_CLEAN);
787 }
788 if (p->flags & PG_CLEAN) {
789 int slot;
790 int pageidx;
791
792 pageidx = p->offset >> PAGE_SHIFT;
793 uvm_pagefree(p);
794 uvmexp.pdfreed++;
795
796 /*
797 * for anons, we need to remove the page
798 * from the anon ourselves. for aobjs,
799 * pagefree did that for us.
800 */
801
802 if (anon) {
803 KASSERT(anon->an_swslot != 0);
804 anon->an_page = NULL;
805 slot = anon->an_swslot;
806 } else {
807 slot = uao_find_swslot(uobj, pageidx);
808 }
809 mutex_exit(slock);
810
811 if (slot > 0) {
812 /* this page is now only in swap. */
813 mutex_enter(&uvm_swap_data_lock);
814 KASSERT(uvmexp.swpgonly < uvmexp.swpginuse);
815 uvmexp.swpgonly++;
816 mutex_exit(&uvm_swap_data_lock);
817 }
818 continue;
819 }
820
821 #if defined(VMSWAP)
822 /*
823 * this page is dirty, skip it if we'll have met our
824 * free target when all the current pageouts complete.
825 */
826
827 if (uvmexp.free + uvmexp.paging > uvmexp.freetarg << 2) {
828 mutex_exit(slock);
829 continue;
830 }
831
832 /*
833 * free any swap space allocated to the page since
834 * we'll have to write it again with its new data.
835 */
836
837 uvmpd_dropswap(p);
838
839 /*
840 * if all pages in swap are only in swap,
841 * the swap space is full and we can't page out
842 * any more swap-backed pages. reactivate this page
843 * so that we eventually cycle all pages through
844 * the inactive queue.
845 */
846
847 if (uvm_swapisfull()) {
848 dirtyreacts++;
849 uvm_pageactivate(p);
850 mutex_exit(slock);
851 continue;
852 }
853
854 /*
855 * start new swap pageout cluster (if necessary).
856 */
857
858 if (swapcluster_allocslots(&swc)) {
859 mutex_exit(slock);
860 dirtyreacts++; /* XXX */
861 continue;
862 }
863
864 /*
865 * at this point, we're definitely going reuse this
866 * page. mark the page busy and delayed-free.
867 * we should remove the page from the page queues
868 * so we don't ever look at it again.
869 * adjust counters and such.
870 */
871
872 p->flags |= PG_BUSY;
873 UVM_PAGE_OWN(p, "scan_queue");
874
875 p->flags |= PG_PAGEOUT;
876 uvm_pagedequeue(p);
877
878 uvmexp.pgswapout++;
879 mutex_exit(&uvm_pageqlock);
880
881 /*
882 * add the new page to the cluster.
883 */
884
885 if (swapcluster_add(&swc, p)) {
886 p->flags &= ~(PG_BUSY|PG_PAGEOUT);
887 UVM_PAGE_OWN(p, NULL);
888 mutex_enter(&uvm_pageqlock);
889 dirtyreacts++;
890 uvm_pageactivate(p);
891 mutex_exit(slock);
892 continue;
893 }
894 mutex_exit(slock);
895
896 swapcluster_flush(&swc, false);
897 mutex_enter(&uvm_pageqlock);
898
899 /*
900 * the pageout is in progress. bump counters and set up
901 * for the next loop.
902 */
903
904 uvmexp.pdpending++;
905
906 #else /* defined(VMSWAP) */
907 uvm_pageactivate(p);
908 mutex_exit(slock);
909 #endif /* defined(VMSWAP) */
910 }
911
912 #if defined(VMSWAP)
913 mutex_exit(&uvm_pageqlock);
914 swapcluster_flush(&swc, true);
915 mutex_enter(&uvm_pageqlock);
916 #endif /* defined(VMSWAP) */
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 static void
926 uvmpd_scan(void)
927 {
928 int swap_shortage, pages_freed;
929 UVMHIST_FUNC("uvmpd_scan"); UVMHIST_CALLED(pdhist);
930
931 uvmexp.pdrevs++;
932
933 #ifndef __SWAP_BROKEN
934
935 /*
936 * swap out some processes if we are below our free target.
937 * we need to unlock the page queues for this.
938 */
939
940 if (uvmexp.free < uvmexp.freetarg && uvmexp.nswapdev != 0 &&
941 uvm.swapout_enabled) {
942 uvmexp.pdswout++;
943 UVMHIST_LOG(pdhist," free %d < target %d: swapout",
944 uvmexp.free, uvmexp.freetarg, 0, 0);
945 mutex_exit(&uvm_pageqlock);
946 uvm_swapout_threads();
947 mutex_enter(&uvm_pageqlock);
948
949 }
950 #endif
951
952 /*
953 * now we want to work on meeting our targets. first we work on our
954 * free target by converting inactive pages into free pages. then
955 * we work on meeting our inactive target by converting active pages
956 * to inactive ones.
957 */
958
959 UVMHIST_LOG(pdhist, " starting 'free' loop",0,0,0,0);
960
961 pages_freed = uvmexp.pdfreed;
962 uvmpd_scan_queue();
963 pages_freed = uvmexp.pdfreed - pages_freed;
964
965 /*
966 * detect if we're not going to be able to page anything out
967 * until we free some swap resources from active pages.
968 */
969
970 swap_shortage = 0;
971 if (uvmexp.free < uvmexp.freetarg &&
972 uvmexp.swpginuse >= uvmexp.swpgavail &&
973 !uvm_swapisfull() &&
974 pages_freed == 0) {
975 swap_shortage = uvmexp.freetarg - uvmexp.free;
976 }
977
978 uvmpdpol_balancequeue(swap_shortage);
979 }
980
981 /*
982 * uvm_reclaimable: decide whether to wait for pagedaemon.
983 *
984 * => return true if it seems to be worth to do uvm_wait.
985 *
986 * XXX should be tunable.
987 * XXX should consider pools, etc?
988 */
989
990 bool
991 uvm_reclaimable(void)
992 {
993 int filepages;
994 int active, inactive;
995
996 /*
997 * if swap is not full, no problem.
998 */
999
1000 if (!uvm_swapisfull()) {
1001 return true;
1002 }
1003
1004 /*
1005 * file-backed pages can be reclaimed even when swap is full.
1006 * if we have more than 1/16 of pageable memory or 5MB, try to reclaim.
1007 *
1008 * XXX assume the worst case, ie. all wired pages are file-backed.
1009 *
1010 * XXX should consider about other reclaimable memory.
1011 * XXX ie. pools, traditional buffer cache.
1012 */
1013
1014 filepages = uvmexp.filepages + uvmexp.execpages - uvmexp.wired;
1015 uvm_estimatepageable(&active, &inactive);
1016 if (filepages >= MIN((active + inactive) >> 4,
1017 5 * 1024 * 1024 >> PAGE_SHIFT)) {
1018 return true;
1019 }
1020
1021 /*
1022 * kill the process, fail allocation, etc..
1023 */
1024
1025 return false;
1026 }
1027
1028 void
1029 uvm_estimatepageable(int *active, int *inactive)
1030 {
1031
1032 uvmpdpol_estimatepageable(active, inactive);
1033 }
1034