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