Home | History | Annotate | Line # | Download | only in uvm
uvm_pdaemon.c revision 1.134
      1 /*	$NetBSD: uvm_pdaemon.c,v 1.134 2023/09/10 15:01:11 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. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)vm_pageout.c        8.5 (Berkeley) 2/14/94
     37  * from: Id: uvm_pdaemon.c,v 1.1.2.32 1998/02/06 05:26:30 chs Exp
     38  *
     39  *
     40  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
     41  * All rights reserved.
     42  *
     43  * Permission to use, copy, modify and distribute this software and
     44  * its documentation is hereby granted, provided that both the copyright
     45  * notice and this permission notice appear in all copies of the
     46  * software, derivative works or modified versions, and any portions
     47  * thereof, and that both notices appear in supporting documentation.
     48  *
     49  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     50  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     51  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     52  *
     53  * Carnegie Mellon requests users of this software to return to
     54  *
     55  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     56  *  School of Computer Science
     57  *  Carnegie Mellon University
     58  *  Pittsburgh PA 15213-3890
     59  *
     60  * any improvements or extensions that they make and grant Carnegie the
     61  * rights to redistribute these changes.
     62  */
     63 
     64 /*
     65  * uvm_pdaemon.c: the page daemon
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: uvm_pdaemon.c,v 1.134 2023/09/10 15:01:11 ad Exp $");
     70 
     71 #include "opt_uvmhist.h"
     72 #include "opt_readahead.h"
     73 
     74 #define	__RWLOCK_PRIVATE
     75 
     76 #include <sys/param.h>
     77 #include <sys/proc.h>
     78 #include <sys/systm.h>
     79 #include <sys/kernel.h>
     80 #include <sys/pool.h>
     81 #include <sys/buf.h>
     82 #include <sys/module.h>
     83 #include <sys/atomic.h>
     84 #include <sys/kthread.h>
     85 
     86 #include <uvm/uvm.h>
     87 #include <uvm/uvm_pdpolicy.h>
     88 #include <uvm/uvm_pgflcache.h>
     89 
     90 #ifdef UVMHIST
     91 #ifndef UVMHIST_PDHIST_SIZE
     92 #define UVMHIST_PDHIST_SIZE 100
     93 #endif
     94 static struct kern_history_ent pdhistbuf[UVMHIST_PDHIST_SIZE];
     95 UVMHIST_DEFINE(pdhist) = UVMHIST_INITIALIZER(pdhisthist, pdhistbuf);
     96 #endif
     97 
     98 /*
     99  * UVMPD_NUMDIRTYREACTS is how many dirty pages the pagedaemon will reactivate
    100  * in a pass thru the inactive list when swap is full.  the value should be
    101  * "small"... if it's too large we'll cycle the active pages thru the inactive
    102  * queue too quickly to for them to be referenced and avoid being freed.
    103  */
    104 
    105 #define	UVMPD_NUMDIRTYREACTS	16
    106 
    107 /*
    108  * local prototypes
    109  */
    110 
    111 static void	uvmpd_scan(void);
    112 static void	uvmpd_scan_queue(void);
    113 static void	uvmpd_tune(void);
    114 static void	uvmpd_pool_drain_thread(void *);
    115 static void	uvmpd_pool_drain_wakeup(void);
    116 
    117 static unsigned int uvm_pagedaemon_waiters;
    118 
    119 /* State for the pool drainer thread */
    120 static kmutex_t uvmpd_lock __cacheline_aligned;
    121 static kcondvar_t uvmpd_pool_drain_cv;
    122 static bool uvmpd_pool_drain_run = false;
    123 
    124 /*
    125  * XXX hack to avoid hangs when large processes fork.
    126  */
    127 u_int uvm_extrapages;
    128 
    129 /*
    130  * uvm_wait: wait (sleep) for the page daemon to free some pages
    131  *
    132  * => should be called with all locks released
    133  * => should _not_ be called by the page daemon (to avoid deadlock)
    134  */
    135 
    136 void
    137 uvm_wait(const char *wmsg)
    138 {
    139 	int timo = 0;
    140 
    141 	if (uvm.pagedaemon_lwp == NULL)
    142 		panic("out of memory before the pagedaemon thread exists");
    143 
    144 	mutex_spin_enter(&uvmpd_lock);
    145 
    146 	/*
    147 	 * check for page daemon going to sleep (waiting for itself)
    148 	 */
    149 
    150 	if (curlwp == uvm.pagedaemon_lwp && uvmexp.paging == 0) {
    151 		/*
    152 		 * now we have a problem: the pagedaemon wants to go to
    153 		 * sleep until it frees more memory.   but how can it
    154 		 * free more memory if it is asleep?  that is a deadlock.
    155 		 * we have two options:
    156 		 *  [1] panic now
    157 		 *  [2] put a timeout on the sleep, thus causing the
    158 		 *      pagedaemon to only pause (rather than sleep forever)
    159 		 *
    160 		 * note that option [2] will only help us if we get lucky
    161 		 * and some other process on the system breaks the deadlock
    162 		 * by exiting or freeing memory (thus allowing the pagedaemon
    163 		 * to continue).  for now we panic if DEBUG is defined,
    164 		 * otherwise we hope for the best with option [2] (better
    165 		 * yet, this should never happen in the first place!).
    166 		 */
    167 
    168 		printf("pagedaemon: deadlock detected!\n");
    169 		timo = hz >> 3;		/* set timeout */
    170 #if defined(DEBUG)
    171 		/* DEBUG: panic so we can debug it */
    172 		panic("pagedaemon deadlock");
    173 #endif
    174 	}
    175 
    176 	uvm_pagedaemon_waiters++;
    177 	wakeup(&uvm.pagedaemon);		/* wake the daemon! */
    178 	UVM_UNLOCK_AND_WAIT(&uvmexp.free, &uvmpd_lock, false, wmsg, timo);
    179 }
    180 
    181 /*
    182  * uvm_kick_pdaemon: perform checks to determine if we need to
    183  * give the pagedaemon a nudge, and do so if necessary.
    184  */
    185 
    186 void
    187 uvm_kick_pdaemon(void)
    188 {
    189 	int fpages = uvm_availmem(false);
    190 
    191 	if (fpages + uvmexp.paging < uvmexp.freemin ||
    192 	    (fpages + uvmexp.paging < uvmexp.freetarg &&
    193 	     uvmpdpol_needsscan_p()) ||
    194 	     uvm_km_va_starved_p()) {
    195 	     	mutex_spin_enter(&uvmpd_lock);
    196 		wakeup(&uvm.pagedaemon);
    197 	     	mutex_spin_exit(&uvmpd_lock);
    198 	}
    199 }
    200 
    201 /*
    202  * uvmpd_tune: tune paging parameters
    203  *
    204  * => called when ever memory is added (or removed?) to the system
    205  */
    206 
    207 static void
    208 uvmpd_tune(void)
    209 {
    210 	int val;
    211 
    212 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pdhist);
    213 
    214 	/*
    215 	 * try to keep 0.5% of available RAM free, but limit to between
    216 	 * 128k and 1024k per-CPU.  XXX: what are these values good for?
    217 	 */
    218 	val = uvmexp.npages / 200;
    219 	val = MAX(val, (128*1024) >> PAGE_SHIFT);
    220 	val = MIN(val, (1024*1024) >> PAGE_SHIFT);
    221 	val *= ncpu;
    222 
    223 	/* Make sure there's always a user page free. */
    224 	if (val < uvmexp.reserve_kernel + 1)
    225 		val = uvmexp.reserve_kernel + 1;
    226 	uvmexp.freemin = val;
    227 
    228 	/* Calculate free target. */
    229 	val = (uvmexp.freemin * 4) / 3;
    230 	if (val <= uvmexp.freemin)
    231 		val = uvmexp.freemin + 1;
    232 	uvmexp.freetarg = val + atomic_swap_uint(&uvm_extrapages, 0);
    233 
    234 	uvmexp.wiredmax = uvmexp.npages / 3;
    235 	UVMHIST_LOG(pdhist, "<- done, freemin=%jd, freetarg=%jd, wiredmax=%jd",
    236 	      uvmexp.freemin, uvmexp.freetarg, uvmexp.wiredmax, 0);
    237 }
    238 
    239 /*
    240  * uvm_pageout: the main loop for the pagedaemon
    241  */
    242 
    243 void
    244 uvm_pageout(void *arg)
    245 {
    246 	int npages = 0;
    247 	int extrapages = 0;
    248 	int fpages;
    249 
    250 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pdhist);
    251 
    252 	UVMHIST_LOG(pdhist,"<starting uvm pagedaemon>", 0, 0, 0, 0);
    253 
    254 	mutex_init(&uvmpd_lock, MUTEX_DEFAULT, IPL_VM);
    255 	cv_init(&uvmpd_pool_drain_cv, "pooldrain");
    256 
    257 	/* Create the pool drainer kernel thread. */
    258 	if (kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL,
    259 	    uvmpd_pool_drain_thread, NULL, NULL, "pooldrain"))
    260 		panic("fork pooldrain");
    261 
    262 	/*
    263 	 * ensure correct priority and set paging parameters...
    264 	 */
    265 
    266 	uvm.pagedaemon_lwp = curlwp;
    267 	npages = uvmexp.npages;
    268 	uvmpd_tune();
    269 
    270 	/*
    271 	 * main loop
    272 	 */
    273 
    274 	for (;;) {
    275 		bool needsscan, needsfree, kmem_va_starved;
    276 
    277 		kmem_va_starved = uvm_km_va_starved_p();
    278 
    279 		mutex_spin_enter(&uvmpd_lock);
    280 		if ((uvm_pagedaemon_waiters == 0 || uvmexp.paging > 0) &&
    281 		    !kmem_va_starved) {
    282 			UVMHIST_LOG(pdhist,"  <<SLEEPING>>",0,0,0,0);
    283 			UVM_UNLOCK_AND_WAIT(&uvm.pagedaemon,
    284 			    &uvmpd_lock, false, "pgdaemon", 0);
    285 			uvmexp.pdwoke++;
    286 			UVMHIST_LOG(pdhist,"  <<WOKE UP>>",0,0,0,0);
    287 		} else {
    288 			mutex_spin_exit(&uvmpd_lock);
    289 		}
    290 
    291 		/*
    292 		 * now recompute inactive count
    293 		 */
    294 
    295 		if (npages != uvmexp.npages || extrapages != uvm_extrapages) {
    296 			npages = uvmexp.npages;
    297 			extrapages = uvm_extrapages;
    298 			uvmpd_tune();
    299 		}
    300 
    301 		uvmpdpol_tune();
    302 
    303 		/*
    304 		 * Estimate a hint.  Note that bufmem are returned to
    305 		 * system only when entire pool page is empty.
    306 		 */
    307 		fpages = uvm_availmem(false);
    308 		UVMHIST_LOG(pdhist,"  free/ftarg=%jd/%jd",
    309 		    fpages, uvmexp.freetarg, 0,0);
    310 
    311 		needsfree = fpages + uvmexp.paging < uvmexp.freetarg;
    312 		needsscan = needsfree || uvmpdpol_needsscan_p();
    313 
    314 		/*
    315 		 * scan if needed
    316 		 */
    317 		if (needsscan) {
    318 			uvmpd_scan();
    319 		}
    320 
    321 		/*
    322 		 * if there's any free memory to be had,
    323 		 * wake up any waiters.
    324 		 */
    325 		if (uvm_availmem(false) > uvmexp.reserve_kernel ||
    326 		    uvmexp.paging == 0) {
    327 			mutex_spin_enter(&uvmpd_lock);
    328 			wakeup(&uvmexp.free);
    329 			uvm_pagedaemon_waiters = 0;
    330 			mutex_spin_exit(&uvmpd_lock);
    331 		}
    332 
    333 		/*
    334 		 * scan done.  if we don't need free memory, we're done.
    335 		 */
    336 
    337 		if (!needsfree && !kmem_va_starved)
    338 			continue;
    339 
    340 		/*
    341 		 * kick the pool drainer thread.
    342 		 */
    343 
    344 		uvmpd_pool_drain_wakeup();
    345 	}
    346 	/*NOTREACHED*/
    347 }
    348 
    349 void
    350 uvm_pageout_start(int npages)
    351 {
    352 
    353 	atomic_add_int(&uvmexp.paging, npages);
    354 }
    355 
    356 void
    357 uvm_pageout_done(int npages)
    358 {
    359 
    360 	KASSERT(atomic_load_relaxed(&uvmexp.paging) >= npages);
    361 
    362 	if (npages == 0) {
    363 		return;
    364 	}
    365 
    366 	atomic_add_int(&uvmexp.paging, -npages);
    367 
    368 	/*
    369 	 * wake up either of pagedaemon or LWPs waiting for it.
    370 	 */
    371 
    372 	mutex_spin_enter(&uvmpd_lock);
    373 	if (uvm_availmem(false) <= uvmexp.reserve_kernel) {
    374 		wakeup(&uvm.pagedaemon);
    375 	} else if (uvm_pagedaemon_waiters != 0) {
    376 		wakeup(&uvmexp.free);
    377 		uvm_pagedaemon_waiters = 0;
    378 	}
    379 	mutex_spin_exit(&uvmpd_lock);
    380 }
    381 
    382 static krwlock_t *
    383 uvmpd_page_owner_lock(struct vm_page *pg)
    384 {
    385 	struct uvm_object *uobj = pg->uobject;
    386 	struct vm_anon *anon = pg->uanon;
    387 	krwlock_t *slock;
    388 
    389 	KASSERT(mutex_owned(&pg->interlock));
    390 
    391 #ifdef DEBUG
    392 	if (uobj == (void *)0xdeadbeef || anon == (void *)0xdeadbeef) {
    393 		return NULL;
    394 	}
    395 #endif
    396 	if (uobj != NULL) {
    397 		slock = uobj->vmobjlock;
    398 		KASSERTMSG(slock != NULL, "pg %p uobj %p, NULL lock", pg, uobj);
    399 	} else if (anon != NULL) {
    400 		slock = anon->an_lock;
    401 		KASSERTMSG(slock != NULL, "pg %p anon %p, NULL lock", pg, anon);
    402 	} else {
    403 		slock = NULL;
    404 	}
    405 	return slock;
    406 }
    407 
    408 /*
    409  * uvmpd_trylockowner: trylock the page's owner.
    410  *
    411  * => called with page interlock held.
    412  * => resolve orphaned O->A loaned page.
    413  * => return the locked mutex on success.  otherwise, return NULL.
    414  */
    415 
    416 krwlock_t *
    417 uvmpd_trylockowner(struct vm_page *pg)
    418 {
    419 	krwlock_t *slock, *heldslock = NULL;
    420 
    421 	KASSERT(mutex_owned(&pg->interlock));
    422 
    423 	slock = uvmpd_page_owner_lock(pg);
    424 	if (slock == NULL) {
    425 		/* Page may be in state of flux - ignore. */
    426 		mutex_exit(&pg->interlock);
    427 		return NULL;
    428 	}
    429 
    430 	if (rw_tryenter(slock, RW_WRITER)) {
    431 		goto success;
    432 	}
    433 
    434 	/*
    435 	 * The try-lock didn't work, so now do a blocking lock after
    436 	 * dropping the page interlock.  Prevent the owner lock from
    437 	 * being freed by taking a hold on it first.
    438 	 */
    439 
    440 	rw_obj_hold(slock);
    441 	mutex_exit(&pg->interlock);
    442 	rw_enter(slock, RW_WRITER);
    443 	heldslock = slock;
    444 
    445 	/*
    446 	 * Now we hold some owner lock.  Check if the lock we hold
    447 	 * is still the lock for the owner of the page.
    448 	 * If it is then return it, otherwise release it and return NULL.
    449 	 */
    450 
    451 	mutex_enter(&pg->interlock);
    452 	slock = uvmpd_page_owner_lock(pg);
    453 	if (heldslock != slock) {
    454 		rw_exit(heldslock);
    455 		slock = NULL;
    456 	} else {
    457 success:
    458 		/*
    459 		 * Set PG_ANON if it isn't set already.
    460 		 */
    461 		if (pg->uobject == NULL && (pg->flags & PG_ANON) == 0) {
    462 			KASSERT(pg->loan_count > 0);
    463 			pg->loan_count--;
    464 			pg->flags |= PG_ANON;
    465 			/* anon now owns it */
    466 		}
    467 	}
    468 	mutex_exit(&pg->interlock);
    469 	if (heldslock != NULL) {
    470 		rw_obj_free(heldslock);
    471 	}
    472 	return slock;
    473 }
    474 
    475 #if defined(VMSWAP)
    476 struct swapcluster {
    477 	int swc_slot;
    478 	int swc_nallocated;
    479 	int swc_nused;
    480 	struct vm_page *swc_pages[howmany(MAXPHYS, MIN_PAGE_SIZE)];
    481 };
    482 
    483 static void
    484 swapcluster_init(struct swapcluster *swc)
    485 {
    486 
    487 	swc->swc_slot = 0;
    488 	swc->swc_nused = 0;
    489 }
    490 
    491 static int
    492 swapcluster_allocslots(struct swapcluster *swc)
    493 {
    494 	int slot;
    495 	int npages;
    496 
    497 	if (swc->swc_slot != 0) {
    498 		return 0;
    499 	}
    500 
    501 	/* Even with strange MAXPHYS, the shift
    502 	   implicitly rounds down to a page. */
    503 	npages = MAXPHYS >> PAGE_SHIFT;
    504 	slot = uvm_swap_alloc(&npages, true);
    505 	if (slot == 0) {
    506 		return ENOMEM;
    507 	}
    508 	swc->swc_slot = slot;
    509 	swc->swc_nallocated = npages;
    510 	swc->swc_nused = 0;
    511 
    512 	return 0;
    513 }
    514 
    515 static int
    516 swapcluster_add(struct swapcluster *swc, struct vm_page *pg)
    517 {
    518 	int slot;
    519 	struct uvm_object *uobj;
    520 
    521 	KASSERT(swc->swc_slot != 0);
    522 	KASSERT(swc->swc_nused < swc->swc_nallocated);
    523 	KASSERT((pg->flags & PG_SWAPBACKED) != 0);
    524 
    525 	slot = swc->swc_slot + swc->swc_nused;
    526 	uobj = pg->uobject;
    527 	if (uobj == NULL) {
    528 		KASSERT(rw_write_held(pg->uanon->an_lock));
    529 		pg->uanon->an_swslot = slot;
    530 	} else {
    531 		int result;
    532 
    533 		KASSERT(rw_write_held(uobj->vmobjlock));
    534 		result = uao_set_swslot(uobj, pg->offset >> PAGE_SHIFT, slot);
    535 		if (result == -1) {
    536 			return ENOMEM;
    537 		}
    538 	}
    539 	swc->swc_pages[swc->swc_nused] = pg;
    540 	swc->swc_nused++;
    541 
    542 	return 0;
    543 }
    544 
    545 static void
    546 swapcluster_flush(struct swapcluster *swc, bool now)
    547 {
    548 	int slot;
    549 	int nused;
    550 	int nallocated;
    551 	int error __diagused;
    552 
    553 	if (swc->swc_slot == 0) {
    554 		return;
    555 	}
    556 	KASSERT(swc->swc_nused <= swc->swc_nallocated);
    557 
    558 	slot = swc->swc_slot;
    559 	nused = swc->swc_nused;
    560 	nallocated = swc->swc_nallocated;
    561 
    562 	/*
    563 	 * if this is the final pageout we could have a few
    564 	 * unused swap blocks.  if so, free them now.
    565 	 */
    566 
    567 	if (nused < nallocated) {
    568 		if (!now) {
    569 			return;
    570 		}
    571 		uvm_swap_free(slot + nused, nallocated - nused);
    572 	}
    573 
    574 	/*
    575 	 * now start the pageout.
    576 	 */
    577 
    578 	if (nused > 0) {
    579 		uvmexp.pdpageouts++;
    580 		uvm_pageout_start(nused);
    581 		error = uvm_swap_put(slot, swc->swc_pages, nused, 0);
    582 		KASSERT(error == 0 || error == ENOMEM);
    583 	}
    584 
    585 	/*
    586 	 * zero swslot to indicate that we are
    587 	 * no longer building a swap-backed cluster.
    588 	 */
    589 
    590 	swc->swc_slot = 0;
    591 	swc->swc_nused = 0;
    592 }
    593 
    594 static int
    595 swapcluster_nused(struct swapcluster *swc)
    596 {
    597 
    598 	return swc->swc_nused;
    599 }
    600 
    601 /*
    602  * uvmpd_dropswap: free any swap allocated to this page.
    603  *
    604  * => called with owner locked.
    605  * => return true if a page had an associated slot.
    606  */
    607 
    608 bool
    609 uvmpd_dropswap(struct vm_page *pg)
    610 {
    611 	bool result = false;
    612 	struct vm_anon *anon = pg->uanon;
    613 
    614 	if ((pg->flags & PG_ANON) && anon->an_swslot) {
    615 		uvm_swap_free(anon->an_swslot, 1);
    616 		anon->an_swslot = 0;
    617 		uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
    618 		result = true;
    619 	} else if (pg->flags & PG_AOBJ) {
    620 		int slot = uao_set_swslot(pg->uobject,
    621 		    pg->offset >> PAGE_SHIFT, 0);
    622 		if (slot) {
    623 			uvm_swap_free(slot, 1);
    624 			uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
    625 			result = true;
    626 		}
    627 	}
    628 
    629 	return result;
    630 }
    631 
    632 #endif /* defined(VMSWAP) */
    633 
    634 /*
    635  * uvmpd_scan_queue: scan an replace candidate list for pages
    636  * to clean or free.
    637  *
    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 	krwlock_t *slock;
    654 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pdhist);
    655 
    656 	/*
    657 	 * swslot is non-zero if we are building a swap cluster.  we want
    658 	 * to stay in the loop while we have a page to scan or we have
    659 	 * a swap-cluster to build.
    660 	 */
    661 
    662 #if defined(VMSWAP)
    663 	swapcluster_init(&swc);
    664 #endif /* defined(VMSWAP) */
    665 
    666 	dirtyreacts = 0;
    667 	uvmpdpol_scaninit();
    668 
    669 	while (/* CONSTCOND */ 1) {
    670 
    671 		/*
    672 		 * see if we've met the free target.
    673 		 */
    674 
    675 		if (uvm_availmem(false) + uvmexp.paging
    676 #if defined(VMSWAP)
    677 		    + swapcluster_nused(&swc)
    678 #endif /* defined(VMSWAP) */
    679 		    >= uvmexp.freetarg << 2 ||
    680 		    dirtyreacts == UVMPD_NUMDIRTYREACTS) {
    681 			UVMHIST_LOG(pdhist,"  met free target: "
    682 				    "exit loop", 0, 0, 0, 0);
    683 			break;
    684 		}
    685 
    686 		/*
    687 		 * first we have the pdpolicy select a victim page
    688 		 * and attempt to lock the object that the page
    689 		 * belongs to.  if our attempt fails we skip on to
    690 		 * the next page (no harm done).  it is important to
    691 		 * "try" locking the object as we are locking in the
    692 		 * wrong order (pageq -> object) and we don't want to
    693 		 * deadlock.
    694 		 *
    695 		 * the only time we expect to see an ownerless page
    696 		 * (i.e. a page with no uobject and !PG_ANON) is if an
    697 		 * anon has loaned a page from a uvm_object and the
    698 		 * uvm_object has dropped the ownership.  in that
    699 		 * case, the anon can "take over" the loaned page
    700 		 * and make it its own.
    701 		 */
    702 
    703 		p = uvmpdpol_selectvictim(&slock);
    704 		if (p == NULL) {
    705 			break;
    706 		}
    707 		KASSERT(uvmpdpol_pageisqueued_p(p));
    708 		KASSERT(uvm_page_owner_locked_p(p, true));
    709 		KASSERT(p->wire_count == 0);
    710 
    711 		/*
    712 		 * we are below target and have a new page to consider.
    713 		 */
    714 
    715 		anon = p->uanon;
    716 		uobj = p->uobject;
    717 
    718 		if (p->flags & PG_BUSY) {
    719 			rw_exit(slock);
    720 			uvmexp.pdbusy++;
    721 			continue;
    722 		}
    723 
    724 		/* does the page belong to an object? */
    725 		if (uobj != NULL) {
    726 			uvmexp.pdobscan++;
    727 		} else {
    728 #if defined(VMSWAP)
    729 			KASSERT(anon != NULL);
    730 			uvmexp.pdanscan++;
    731 #else /* defined(VMSWAP) */
    732 			panic("%s: anon", __func__);
    733 #endif /* defined(VMSWAP) */
    734 		}
    735 
    736 
    737 		/*
    738 		 * we now have the object locked.
    739 		 * if the page is not swap-backed, call the object's
    740 		 * pager to flush and free the page.
    741 		 */
    742 
    743 #if defined(READAHEAD_STATS)
    744 		if ((p->flags & PG_READAHEAD) != 0) {
    745 			p->flags &= ~PG_READAHEAD;
    746 			uvm_ra_miss.ev_count++;
    747 		}
    748 #endif /* defined(READAHEAD_STATS) */
    749 
    750 		if ((p->flags & PG_SWAPBACKED) == 0) {
    751 			KASSERT(uobj != NULL);
    752 			(void) (uobj->pgops->pgo_put)(uobj, p->offset,
    753 			    p->offset + PAGE_SIZE, PGO_CLEANIT|PGO_FREE);
    754 			continue;
    755 		}
    756 
    757 		/*
    758 		 * the page is swap-backed.  remove all the permissions
    759 		 * from the page so we can sync the modified info
    760 		 * without any race conditions.  if the page is clean
    761 		 * we can free it now and continue.
    762 		 */
    763 
    764 		pmap_page_protect(p, VM_PROT_NONE);
    765 		if (uvm_pagegetdirty(p) == UVM_PAGE_STATUS_UNKNOWN) {
    766 			if (pmap_clear_modify(p)) {
    767 				uvm_pagemarkdirty(p, UVM_PAGE_STATUS_DIRTY);
    768 			} else {
    769 				uvm_pagemarkdirty(p, UVM_PAGE_STATUS_CLEAN);
    770 			}
    771 		}
    772 		if (uvm_pagegetdirty(p) != UVM_PAGE_STATUS_DIRTY) {
    773 			int slot;
    774 			int pageidx;
    775 
    776 			pageidx = p->offset >> PAGE_SHIFT;
    777 			uvm_pagefree(p);
    778 			atomic_inc_uint(&uvmexp.pdfreed);
    779 
    780 			/*
    781 			 * for anons, we need to remove the page
    782 			 * from the anon ourselves.  for aobjs,
    783 			 * pagefree did that for us.
    784 			 */
    785 
    786 			if (anon) {
    787 				KASSERT(anon->an_swslot != 0);
    788 				anon->an_page = NULL;
    789 				slot = anon->an_swslot;
    790 			} else {
    791 				slot = uao_find_swslot(uobj, pageidx);
    792 			}
    793 			if (slot > 0) {
    794 				/* this page is now only in swap. */
    795 				KASSERT(uvmexp.swpgonly < uvmexp.swpginuse);
    796 				atomic_inc_uint(&uvmexp.swpgonly);
    797 			}
    798 			rw_exit(slock);
    799 			continue;
    800 		}
    801 
    802 #if defined(VMSWAP)
    803 		/*
    804 		 * this page is dirty, skip it if we'll have met our
    805 		 * free target when all the current pageouts complete.
    806 		 */
    807 
    808 		if (uvm_availmem(false) + uvmexp.paging >
    809 		    uvmexp.freetarg << 2) {
    810 			rw_exit(slock);
    811 			continue;
    812 		}
    813 
    814 		/*
    815 		 * free any swap space allocated to the page since
    816 		 * we'll have to write it again with its new data.
    817 		 */
    818 
    819 		uvmpd_dropswap(p);
    820 
    821 		/*
    822 		 * start new swap pageout cluster (if necessary).
    823 		 *
    824 		 * if swap is full reactivate this page so that
    825 		 * we eventually cycle all pages through the
    826 		 * inactive queue.
    827 		 */
    828 
    829 		if (swapcluster_allocslots(&swc)) {
    830 			dirtyreacts++;
    831 			uvm_pagelock(p);
    832 			uvm_pageactivate(p);
    833 			uvm_pageunlock(p);
    834 			rw_exit(slock);
    835 			continue;
    836 		}
    837 
    838 		/*
    839 		 * at this point, we're definitely going reuse this
    840 		 * page.  mark the page busy and delayed-free.
    841 		 * we should remove the page from the page queues
    842 		 * so we don't ever look at it again.
    843 		 * adjust counters and such.
    844 		 */
    845 
    846 		p->flags |= PG_BUSY;
    847 		UVM_PAGE_OWN(p, "scan_queue");
    848 		p->flags |= PG_PAGEOUT;
    849 		uvmexp.pgswapout++;
    850 
    851 		uvm_pagelock(p);
    852 		uvm_pagedequeue(p);
    853 		uvm_pageunlock(p);
    854 
    855 		/*
    856 		 * add the new page to the cluster.
    857 		 */
    858 
    859 		if (swapcluster_add(&swc, p)) {
    860 			p->flags &= ~(PG_BUSY|PG_PAGEOUT);
    861 			UVM_PAGE_OWN(p, NULL);
    862 			dirtyreacts++;
    863 			uvm_pagelock(p);
    864 			uvm_pageactivate(p);
    865 			uvm_pageunlock(p);
    866 			rw_exit(slock);
    867 			continue;
    868 		}
    869 		rw_exit(slock);
    870 
    871 		swapcluster_flush(&swc, false);
    872 
    873 		/*
    874 		 * the pageout is in progress.  bump counters and set up
    875 		 * for the next loop.
    876 		 */
    877 
    878 		atomic_inc_uint(&uvmexp.pdpending);
    879 
    880 #else /* defined(VMSWAP) */
    881 		uvm_pagelock(p);
    882 		uvm_pageactivate(p);
    883 		uvm_pageunlock(p);
    884 		rw_exit(slock);
    885 #endif /* defined(VMSWAP) */
    886 	}
    887 
    888 	uvmpdpol_scanfini();
    889 
    890 #if defined(VMSWAP)
    891 	swapcluster_flush(&swc, true);
    892 #endif /* defined(VMSWAP) */
    893 }
    894 
    895 /*
    896  * uvmpd_scan: scan the page queues and attempt to meet our targets.
    897  */
    898 
    899 static void
    900 uvmpd_scan(void)
    901 {
    902 	int swap_shortage, pages_freed, fpages;
    903 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pdhist);
    904 
    905 	uvmexp.pdrevs++;
    906 
    907 	/*
    908 	 * work on meeting our targets.   first we work on our free target
    909 	 * by converting inactive pages into free pages.  then we work on
    910 	 * meeting our inactive target by converting active pages to
    911 	 * inactive ones.
    912 	 */
    913 
    914 	UVMHIST_LOG(pdhist, "  starting 'free' loop",0,0,0,0);
    915 
    916 	pages_freed = uvmexp.pdfreed;
    917 	uvmpd_scan_queue();
    918 	pages_freed = uvmexp.pdfreed - pages_freed;
    919 
    920 	/*
    921 	 * detect if we're not going to be able to page anything out
    922 	 * until we free some swap resources from active pages.
    923 	 */
    924 
    925 	swap_shortage = 0;
    926 	fpages = uvm_availmem(false);
    927 	if (fpages < uvmexp.freetarg &&
    928 	    uvmexp.swpginuse >= uvmexp.swpgavail &&
    929 	    !uvm_swapisfull() &&
    930 	    pages_freed == 0) {
    931 		swap_shortage = uvmexp.freetarg - fpages;
    932 	}
    933 
    934 	uvmpdpol_balancequeue(swap_shortage);
    935 
    936 	/*
    937 	 * if still below the minimum target, try unloading kernel
    938 	 * modules.
    939 	 */
    940 
    941 	if (uvm_availmem(false) < uvmexp.freemin) {
    942 		module_thread_kick();
    943 	}
    944 }
    945 
    946 /*
    947  * uvm_reclaimable: decide whether to wait for pagedaemon.
    948  *
    949  * => return true if it seems to be worth to do uvm_wait.
    950  *
    951  * XXX should be tunable.
    952  * XXX should consider pools, etc?
    953  */
    954 
    955 bool
    956 uvm_reclaimable(void)
    957 {
    958 	int filepages;
    959 	int active, inactive;
    960 
    961 	/*
    962 	 * if swap is not full, no problem.
    963 	 */
    964 
    965 	if (!uvm_swapisfull()) {
    966 		return true;
    967 	}
    968 
    969 	/*
    970 	 * file-backed pages can be reclaimed even when swap is full.
    971 	 * if we have more than 1/16 of pageable memory or 5MB, try to reclaim.
    972 	 * NB: filepages calculation does not exclude EXECPAGES - intentional.
    973 	 *
    974 	 * XXX assume the worst case, ie. all wired pages are file-backed.
    975 	 *
    976 	 * XXX should consider about other reclaimable memory.
    977 	 * XXX ie. pools, traditional buffer cache.
    978 	 */
    979 
    980 	cpu_count_sync(false);
    981 	filepages = (int)(cpu_count_get(CPU_COUNT_FILECLEAN) +
    982 	    cpu_count_get(CPU_COUNT_FILEUNKNOWN) +
    983 	    cpu_count_get(CPU_COUNT_FILEDIRTY) - uvmexp.wired);
    984 	uvm_estimatepageable(&active, &inactive);
    985 	if (filepages >= MIN((active + inactive) >> 4,
    986 	    5 * 1024 * 1024 >> PAGE_SHIFT)) {
    987 		return true;
    988 	}
    989 
    990 	/*
    991 	 * kill the process, fail allocation, etc..
    992 	 */
    993 
    994 	return false;
    995 }
    996 
    997 void
    998 uvm_estimatepageable(int *active, int *inactive)
    999 {
   1000 
   1001 	uvmpdpol_estimatepageable(active, inactive);
   1002 }
   1003 
   1004 
   1005 /*
   1006  * Use a separate thread for draining pools.
   1007  * This work can't done from the main pagedaemon thread because
   1008  * some pool allocators need to take vm_map locks.
   1009  */
   1010 
   1011 static void
   1012 uvmpd_pool_drain_thread(void *arg)
   1013 {
   1014 	struct pool *firstpool, *curpool;
   1015 	int bufcnt, lastslept;
   1016 	bool cycled;
   1017 
   1018 	firstpool = NULL;
   1019 	cycled = true;
   1020 	for (;;) {
   1021 		/*
   1022 		 * sleep until awoken by the pagedaemon.
   1023 		 */
   1024 		mutex_enter(&uvmpd_lock);
   1025 		if (!uvmpd_pool_drain_run) {
   1026 			lastslept = getticks();
   1027 			cv_wait(&uvmpd_pool_drain_cv, &uvmpd_lock);
   1028 			if (getticks() != lastslept) {
   1029 				cycled = false;
   1030 				firstpool = NULL;
   1031 			}
   1032 		}
   1033 		uvmpd_pool_drain_run = false;
   1034 		mutex_exit(&uvmpd_lock);
   1035 
   1036 		/*
   1037 		 * rate limit draining, otherwise in desperate circumstances
   1038 		 * this can totally saturate the system with xcall activity.
   1039 		 */
   1040 		if (cycled) {
   1041 			kpause("uvmpdlmt", false, 1, NULL);
   1042 			cycled = false;
   1043 			firstpool = NULL;
   1044 		}
   1045 
   1046 		/*
   1047 		 * drain and temporarily disable the freelist cache.
   1048 		 */
   1049 		uvm_pgflcache_pause();
   1050 
   1051 		/*
   1052 		 * kill unused metadata buffers.
   1053 		 */
   1054 		bufcnt = uvmexp.freetarg - uvm_availmem(false);
   1055 		if (bufcnt < 0)
   1056 			bufcnt = 0;
   1057 
   1058 		mutex_enter(&bufcache_lock);
   1059 		buf_drain(bufcnt << PAGE_SHIFT);
   1060 		mutex_exit(&bufcache_lock);
   1061 
   1062 		/*
   1063 		 * drain a pool, and then re-enable the freelist cache.
   1064 		 */
   1065 		(void)pool_drain(&curpool);
   1066 		KASSERT(curpool != NULL);
   1067 		if (firstpool == NULL) {
   1068 			firstpool = curpool;
   1069 		} else if (firstpool == curpool) {
   1070 			cycled = true;
   1071 		}
   1072 		uvm_pgflcache_resume();
   1073 	}
   1074 	/*NOTREACHED*/
   1075 }
   1076 
   1077 static void
   1078 uvmpd_pool_drain_wakeup(void)
   1079 {
   1080 
   1081 	mutex_enter(&uvmpd_lock);
   1082 	uvmpd_pool_drain_run = true;
   1083 	cv_signal(&uvmpd_pool_drain_cv);
   1084 	mutex_exit(&uvmpd_lock);
   1085 }
   1086