vm_pageout.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430
  1. /*-
  2. * SPDX-License-Identifier: (BSD-4-Clause AND MIT-CMU)
  3. *
  4. * Copyright (c) 1991 Regents of the University of California.
  5. * All rights reserved.
  6. * Copyright (c) 1994 John S. Dyson
  7. * All rights reserved.
  8. * Copyright (c) 1994 David Greenman
  9. * All rights reserved.
  10. * Copyright (c) 2005 Yahoo! Technologies Norway AS
  11. * All rights reserved.
  12. *
  13. * This code is derived from software contributed to Berkeley by
  14. * The Mach Operating System project at Carnegie-Mellon University.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions
  18. * are met:
  19. * 1. Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. * 3. All advertising materials mentioning features or use of this software
  25. * must display the following acknowledgement:
  26. * This product includes software developed by the University of
  27. * California, Berkeley and its contributors.
  28. * 4. Neither the name of the University nor the names of its contributors
  29. * may be used to endorse or promote products derived from this software
  30. * without specific prior written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  33. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  36. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  37. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  38. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  40. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  41. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  42. * SUCH DAMAGE.
  43. *
  44. *
  45. * Copyright (c) 1987, 1990 Carnegie-Mellon University.
  46. * All rights reserved.
  47. *
  48. * Authors: Avadis Tevanian, Jr., Michael Wayne Young
  49. *
  50. * Permission to use, copy, modify and distribute this software and
  51. * its documentation is hereby granted, provided that both the copyright
  52. * notice and this permission notice appear in all copies of the
  53. * software, derivative works or modified versions, and any portions
  54. * thereof, and that both notices appear in supporting documentation.
  55. *
  56. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  57. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
  58. * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  59. *
  60. * Carnegie Mellon requests users of this software to return to
  61. *
  62. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  63. * School of Computer Science
  64. * Carnegie Mellon University
  65. * Pittsburgh PA 15213-3890
  66. *
  67. * any improvements or extensions that they make and grant Carnegie the
  68. * rights to redistribute these changes.
  69. */
  70. /*
  71. * The proverbial page-out daemon.
  72. */
  73. #include <sys/cdefs.h>
  74. #include "opt_vm.h"
  75. #include <sys/param.h>
  76. #include <sys/systm.h>
  77. #include <sys/kernel.h>
  78. #include <sys/blockcount.h>
  79. #include <sys/eventhandler.h>
  80. #include <sys/lock.h>
  81. #include <sys/mutex.h>
  82. #include <sys/proc.h>
  83. #include <sys/kthread.h>
  84. #include <sys/ktr.h>
  85. #include <sys/mount.h>
  86. #include <sys/racct.h>
  87. #include <sys/resourcevar.h>
  88. #include <sys/sched.h>
  89. #include <sys/sdt.h>
  90. #include <sys/signalvar.h>
  91. #include <sys/smp.h>
  92. #include <sys/time.h>
  93. #include <sys/vnode.h>
  94. #include <sys/vmmeter.h>
  95. #include <sys/rwlock.h>
  96. #include <sys/sx.h>
  97. #include <sys/sysctl.h>
  98. #include <vm/vm.h>
  99. #include <vm/vm_param.h>
  100. #include <vm/vm_object.h>
  101. #include <vm/vm_page.h>
  102. #include <vm/vm_map.h>
  103. #include <vm/vm_pageout.h>
  104. #include <vm/vm_pager.h>
  105. #include <vm/vm_phys.h>
  106. #include <vm/vm_pagequeue.h>
  107. #include <vm/swap_pager.h>
  108. #include <vm/vm_extern.h>
  109. #include <vm/uma.h>
  110. /*
  111. * System initialization
  112. */
  113. /* the kernel process "vm_pageout"*/
  114. static void vm_pageout(void);
  115. static void vm_pageout_init(void);
  116. static int vm_pageout_clean(vm_page_t m, int *numpagedout);
  117. static int vm_pageout_cluster(vm_page_t m);
  118. static void vm_pageout_mightbe_oom(struct vm_domain *vmd, int page_shortage,
  119. int starting_page_shortage);
  120. SYSINIT(pagedaemon_init, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, vm_pageout_init,
  121. NULL);
  122. struct proc *pageproc;
  123. static struct kproc_desc page_kp = {
  124. "pagedaemon",
  125. vm_pageout,
  126. &pageproc
  127. };
  128. SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start,
  129. &page_kp);
  130. SDT_PROVIDER_DEFINE(vm);
  131. SDT_PROBE_DEFINE(vm, , , vm__lowmem_scan);
  132. /* Pagedaemon activity rates, in subdivisions of one second. */
  133. #define VM_LAUNDER_RATE 10
  134. #define VM_INACT_SCAN_RATE 10
  135. static int swapdev_enabled;
  136. int vm_pageout_page_count = 32;
  137. static int vm_panic_on_oom = 0;
  138. SYSCTL_INT(_vm, OID_AUTO, panic_on_oom,
  139. CTLFLAG_RWTUN, &vm_panic_on_oom, 0,
  140. "Panic on the given number of out-of-memory errors instead of "
  141. "killing the largest process");
  142. static int vm_pageout_update_period;
  143. SYSCTL_INT(_vm, OID_AUTO, pageout_update_period,
  144. CTLFLAG_RWTUN, &vm_pageout_update_period, 0,
  145. "Maximum active LRU update period");
  146. static int pageout_cpus_per_thread = 16;
  147. SYSCTL_INT(_vm, OID_AUTO, pageout_cpus_per_thread, CTLFLAG_RDTUN,
  148. &pageout_cpus_per_thread, 0,
  149. "Number of CPUs per pagedaemon worker thread");
  150. static int lowmem_period = 10;
  151. SYSCTL_INT(_vm, OID_AUTO, lowmem_period, CTLFLAG_RWTUN, &lowmem_period, 0,
  152. "Low memory callback period");
  153. static int disable_swap_pageouts;
  154. SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
  155. CTLFLAG_RWTUN, &disable_swap_pageouts, 0,
  156. "Disallow swapout of dirty pages");
  157. static int pageout_lock_miss;
  158. SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
  159. CTLFLAG_RD, &pageout_lock_miss, 0,
  160. "vget() lock misses during pageout");
  161. static int vm_pageout_oom_seq = 12;
  162. SYSCTL_INT(_vm, OID_AUTO, pageout_oom_seq,
  163. CTLFLAG_RWTUN, &vm_pageout_oom_seq, 0,
  164. "back-to-back calls to oom detector to start OOM");
  165. static int act_scan_laundry_weight = 3;
  166. SYSCTL_INT(_vm, OID_AUTO, act_scan_laundry_weight, CTLFLAG_RWTUN,
  167. &act_scan_laundry_weight, 0,
  168. "weight given to clean vs. dirty pages in active queue scans");
  169. static u_int vm_background_launder_rate = 4096;
  170. SYSCTL_UINT(_vm, OID_AUTO, background_launder_rate, CTLFLAG_RWTUN,
  171. &vm_background_launder_rate, 0,
  172. "background laundering rate, in kilobytes per second");
  173. static u_int vm_background_launder_max = 20 * 1024;
  174. SYSCTL_UINT(_vm, OID_AUTO, background_launder_max, CTLFLAG_RWTUN,
  175. &vm_background_launder_max, 0,
  176. "background laundering cap, in kilobytes");
  177. u_long vm_page_max_user_wired;
  178. SYSCTL_ULONG(_vm, OID_AUTO, max_user_wired, CTLFLAG_RW,
  179. &vm_page_max_user_wired, 0,
  180. "system-wide limit to user-wired page count");
  181. static u_int isqrt(u_int num);
  182. static int vm_pageout_launder(struct vm_domain *vmd, int launder,
  183. bool in_shortfall);
  184. static void vm_pageout_laundry_worker(void *arg);
  185. struct scan_state {
  186. struct vm_batchqueue bq;
  187. struct vm_pagequeue *pq;
  188. vm_page_t marker;
  189. int maxscan;
  190. int scanned;
  191. };
  192. static void
  193. vm_pageout_init_scan(struct scan_state *ss, struct vm_pagequeue *pq,
  194. vm_page_t marker, vm_page_t after, int maxscan)
  195. {
  196. vm_pagequeue_assert_locked(pq);
  197. KASSERT((marker->a.flags & PGA_ENQUEUED) == 0,
  198. ("marker %p already enqueued", marker));
  199. if (after == NULL)
  200. TAILQ_INSERT_HEAD(&pq->pq_pl, marker, plinks.q);
  201. else
  202. TAILQ_INSERT_AFTER(&pq->pq_pl, after, marker, plinks.q);
  203. vm_page_aflag_set(marker, PGA_ENQUEUED);
  204. vm_batchqueue_init(&ss->bq);
  205. ss->pq = pq;
  206. ss->marker = marker;
  207. ss->maxscan = maxscan;
  208. ss->scanned = 0;
  209. vm_pagequeue_unlock(pq);
  210. }
  211. static void
  212. vm_pageout_end_scan(struct scan_state *ss)
  213. {
  214. struct vm_pagequeue *pq;
  215. pq = ss->pq;
  216. vm_pagequeue_assert_locked(pq);
  217. KASSERT((ss->marker->a.flags & PGA_ENQUEUED) != 0,
  218. ("marker %p not enqueued", ss->marker));
  219. TAILQ_REMOVE(&pq->pq_pl, ss->marker, plinks.q);
  220. vm_page_aflag_clear(ss->marker, PGA_ENQUEUED);
  221. pq->pq_pdpages += ss->scanned;
  222. }
  223. /*
  224. * Add a small number of queued pages to a batch queue for later processing
  225. * without the corresponding queue lock held. The caller must have enqueued a
  226. * marker page at the desired start point for the scan. Pages will be
  227. * physically dequeued if the caller so requests. Otherwise, the returned
  228. * batch may contain marker pages, and it is up to the caller to handle them.
  229. *
  230. * When processing the batch queue, vm_pageout_defer() must be used to
  231. * determine whether the page has been logically dequeued since the batch was
  232. * collected.
  233. */
  234. static __always_inline void
  235. vm_pageout_collect_batch(struct scan_state *ss, const bool dequeue)
  236. {
  237. struct vm_pagequeue *pq;
  238. vm_page_t m, marker, n;
  239. marker = ss->marker;
  240. pq = ss->pq;
  241. KASSERT((marker->a.flags & PGA_ENQUEUED) != 0,
  242. ("marker %p not enqueued", ss->marker));
  243. vm_pagequeue_lock(pq);
  244. for (m = TAILQ_NEXT(marker, plinks.q); m != NULL &&
  245. ss->scanned < ss->maxscan && ss->bq.bq_cnt < VM_BATCHQUEUE_SIZE;
  246. m = n, ss->scanned++) {
  247. n = TAILQ_NEXT(m, plinks.q);
  248. if ((m->flags & PG_MARKER) == 0) {
  249. KASSERT((m->a.flags & PGA_ENQUEUED) != 0,
  250. ("page %p not enqueued", m));
  251. KASSERT((m->flags & PG_FICTITIOUS) == 0,
  252. ("Fictitious page %p cannot be in page queue", m));
  253. KASSERT((m->oflags & VPO_UNMANAGED) == 0,
  254. ("Unmanaged page %p cannot be in page queue", m));
  255. } else if (dequeue)
  256. continue;
  257. (void)vm_batchqueue_insert(&ss->bq, m);
  258. if (dequeue) {
  259. TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
  260. vm_page_aflag_clear(m, PGA_ENQUEUED);
  261. }
  262. }
  263. TAILQ_REMOVE(&pq->pq_pl, marker, plinks.q);
  264. if (__predict_true(m != NULL))
  265. TAILQ_INSERT_BEFORE(m, marker, plinks.q);
  266. else
  267. TAILQ_INSERT_TAIL(&pq->pq_pl, marker, plinks.q);
  268. if (dequeue)
  269. vm_pagequeue_cnt_add(pq, -ss->bq.bq_cnt);
  270. vm_pagequeue_unlock(pq);
  271. }
  272. /*
  273. * Return the next page to be scanned, or NULL if the scan is complete.
  274. */
  275. static __always_inline vm_page_t
  276. vm_pageout_next(struct scan_state *ss, const bool dequeue)
  277. {
  278. if (ss->bq.bq_cnt == 0)
  279. vm_pageout_collect_batch(ss, dequeue);
  280. return (vm_batchqueue_pop(&ss->bq));
  281. }
  282. /*
  283. * Determine whether processing of a page should be deferred and ensure that any
  284. * outstanding queue operations are processed.
  285. */
  286. static __always_inline bool
  287. vm_pageout_defer(vm_page_t m, const uint8_t queue, const bool enqueued)
  288. {
  289. vm_page_astate_t as;
  290. as = vm_page_astate_load(m);
  291. if (__predict_false(as.queue != queue ||
  292. ((as.flags & PGA_ENQUEUED) != 0) != enqueued))
  293. return (true);
  294. if ((as.flags & PGA_QUEUE_OP_MASK) != 0) {
  295. vm_page_pqbatch_submit(m, queue);
  296. return (true);
  297. }
  298. return (false);
  299. }
  300. /*
  301. * Scan for pages at adjacent offsets within the given page's object that are
  302. * eligible for laundering, form a cluster of these pages and the given page,
  303. * and launder that cluster.
  304. */
  305. static int
  306. vm_pageout_cluster(vm_page_t m)
  307. {
  308. vm_object_t object;
  309. vm_page_t mc[2 * vm_pageout_page_count], p, pb, ps;
  310. vm_pindex_t pindex;
  311. int ib, is, page_base, pageout_count;
  312. object = m->object;
  313. VM_OBJECT_ASSERT_WLOCKED(object);
  314. pindex = m->pindex;
  315. vm_page_assert_xbusied(m);
  316. mc[vm_pageout_page_count] = pb = ps = m;
  317. pageout_count = 1;
  318. page_base = vm_pageout_page_count;
  319. ib = 1;
  320. is = 1;
  321. /*
  322. * We can cluster only if the page is not clean, busy, or held, and
  323. * the page is in the laundry queue.
  324. *
  325. * During heavy mmap/modification loads the pageout
  326. * daemon can really fragment the underlying file
  327. * due to flushing pages out of order and not trying to
  328. * align the clusters (which leaves sporadic out-of-order
  329. * holes). To solve this problem we do the reverse scan
  330. * first and attempt to align our cluster, then do a
  331. * forward scan if room remains.
  332. */
  333. more:
  334. while (ib != 0 && pageout_count < vm_pageout_page_count) {
  335. if (ib > pindex) {
  336. ib = 0;
  337. break;
  338. }
  339. if ((p = vm_page_prev(pb)) == NULL ||
  340. vm_page_tryxbusy(p) == 0) {
  341. ib = 0;
  342. break;
  343. }
  344. if (vm_page_wired(p)) {
  345. ib = 0;
  346. vm_page_xunbusy(p);
  347. break;
  348. }
  349. vm_page_test_dirty(p);
  350. if (p->dirty == 0) {
  351. ib = 0;
  352. vm_page_xunbusy(p);
  353. break;
  354. }
  355. if (!vm_page_in_laundry(p) || !vm_page_try_remove_write(p)) {
  356. vm_page_xunbusy(p);
  357. ib = 0;
  358. break;
  359. }
  360. mc[--page_base] = pb = p;
  361. ++pageout_count;
  362. ++ib;
  363. /*
  364. * We are at an alignment boundary. Stop here, and switch
  365. * directions. Do not clear ib.
  366. */
  367. if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
  368. break;
  369. }
  370. while (pageout_count < vm_pageout_page_count &&
  371. pindex + is < object->size) {
  372. if ((p = vm_page_next(ps)) == NULL ||
  373. vm_page_tryxbusy(p) == 0)
  374. break;
  375. if (vm_page_wired(p)) {
  376. vm_page_xunbusy(p);
  377. break;
  378. }
  379. vm_page_test_dirty(p);
  380. if (p->dirty == 0) {
  381. vm_page_xunbusy(p);
  382. break;
  383. }
  384. if (!vm_page_in_laundry(p) || !vm_page_try_remove_write(p)) {
  385. vm_page_xunbusy(p);
  386. break;
  387. }
  388. mc[page_base + pageout_count] = ps = p;
  389. ++pageout_count;
  390. ++is;
  391. }
  392. /*
  393. * If we exhausted our forward scan, continue with the reverse scan
  394. * when possible, even past an alignment boundary. This catches
  395. * boundary conditions.
  396. */
  397. if (ib != 0 && pageout_count < vm_pageout_page_count)
  398. goto more;
  399. return (vm_pageout_flush(&mc[page_base], pageout_count,
  400. VM_PAGER_PUT_NOREUSE, 0, NULL, NULL));
  401. }
  402. /*
  403. * vm_pageout_flush() - launder the given pages
  404. *
  405. * The given pages are laundered. Note that we setup for the start of
  406. * I/O ( i.e. busy the page ), mark it read-only, and bump the object
  407. * reference count all in here rather then in the parent. If we want
  408. * the parent to do more sophisticated things we may have to change
  409. * the ordering.
  410. *
  411. * Returned runlen is the count of pages between mreq and first
  412. * page after mreq with status VM_PAGER_AGAIN.
  413. * *eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL
  414. * for any page in runlen set.
  415. */
  416. int
  417. vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen,
  418. boolean_t *eio)
  419. {
  420. vm_object_t object = mc[0]->object;
  421. int pageout_status[count];
  422. int numpagedout = 0;
  423. int i, runlen;
  424. VM_OBJECT_ASSERT_WLOCKED(object);
  425. /*
  426. * Initiate I/O. Mark the pages shared busy and verify that they're
  427. * valid and read-only.
  428. *
  429. * We do not have to fixup the clean/dirty bits here... we can
  430. * allow the pager to do it after the I/O completes.
  431. *
  432. * NOTE! mc[i]->dirty may be partial or fragmented due to an
  433. * edge case with file fragments.
  434. */
  435. for (i = 0; i < count; i++) {
  436. KASSERT(vm_page_all_valid(mc[i]),
  437. ("vm_pageout_flush: partially invalid page %p index %d/%d",
  438. mc[i], i, count));
  439. KASSERT((mc[i]->a.flags & PGA_WRITEABLE) == 0,
  440. ("vm_pageout_flush: writeable page %p", mc[i]));
  441. vm_page_busy_downgrade(mc[i]);
  442. }
  443. vm_object_pip_add(object, count);
  444. vm_pager_put_pages(object, mc, count, flags, pageout_status);
  445. runlen = count - mreq;
  446. if (eio != NULL)
  447. *eio = FALSE;
  448. for (i = 0; i < count; i++) {
  449. vm_page_t mt = mc[i];
  450. KASSERT(pageout_status[i] == VM_PAGER_PEND ||
  451. !pmap_page_is_write_mapped(mt),
  452. ("vm_pageout_flush: page %p is not write protected", mt));
  453. switch (pageout_status[i]) {
  454. case VM_PAGER_OK:
  455. /*
  456. * The page may have moved since laundering started, in
  457. * which case it should be left alone.
  458. */
  459. if (vm_page_in_laundry(mt))
  460. vm_page_deactivate_noreuse(mt);
  461. /* FALLTHROUGH */
  462. case VM_PAGER_PEND:
  463. numpagedout++;
  464. break;
  465. case VM_PAGER_BAD:
  466. /*
  467. * The page is outside the object's range. We pretend
  468. * that the page out worked and clean the page, so the
  469. * changes will be lost if the page is reclaimed by
  470. * the page daemon.
  471. */
  472. vm_page_undirty(mt);
  473. if (vm_page_in_laundry(mt))
  474. vm_page_deactivate_noreuse(mt);
  475. break;
  476. case VM_PAGER_ERROR:
  477. case VM_PAGER_FAIL:
  478. /*
  479. * If the page couldn't be paged out to swap because the
  480. * pager wasn't able to find space, place the page in
  481. * the PQ_UNSWAPPABLE holding queue. This is an
  482. * optimization that prevents the page daemon from
  483. * wasting CPU cycles on pages that cannot be reclaimed
  484. * because no swap device is configured.
  485. *
  486. * Otherwise, reactivate the page so that it doesn't
  487. * clog the laundry and inactive queues. (We will try
  488. * paging it out again later.)
  489. */
  490. if ((object->flags & OBJ_SWAP) != 0 &&
  491. pageout_status[i] == VM_PAGER_FAIL) {
  492. vm_page_unswappable(mt);
  493. numpagedout++;
  494. } else
  495. vm_page_activate(mt);
  496. if (eio != NULL && i >= mreq && i - mreq < runlen)
  497. *eio = TRUE;
  498. break;
  499. case VM_PAGER_AGAIN:
  500. if (i >= mreq && i - mreq < runlen)
  501. runlen = i - mreq;
  502. break;
  503. }
  504. /*
  505. * If the operation is still going, leave the page busy to
  506. * block all other accesses. Also, leave the paging in
  507. * progress indicator set so that we don't attempt an object
  508. * collapse.
  509. */
  510. if (pageout_status[i] != VM_PAGER_PEND) {
  511. vm_object_pip_wakeup(object);
  512. vm_page_sunbusy(mt);
  513. }
  514. }
  515. if (prunlen != NULL)
  516. *prunlen = runlen;
  517. return (numpagedout);
  518. }
  519. static void
  520. vm_pageout_swapon(void *arg __unused, struct swdevt *sp __unused)
  521. {
  522. atomic_store_rel_int(&swapdev_enabled, 1);
  523. }
  524. static void
  525. vm_pageout_swapoff(void *arg __unused, struct swdevt *sp __unused)
  526. {
  527. if (swap_pager_nswapdev() == 1)
  528. atomic_store_rel_int(&swapdev_enabled, 0);
  529. }
  530. /*
  531. * Attempt to acquire all of the necessary locks to launder a page and
  532. * then call through the clustering layer to PUTPAGES. Wait a short
  533. * time for a vnode lock.
  534. *
  535. * Requires the page and object lock on entry, releases both before return.
  536. * Returns 0 on success and an errno otherwise.
  537. */
  538. static int
  539. vm_pageout_clean(vm_page_t m, int *numpagedout)
  540. {
  541. struct vnode *vp;
  542. struct mount *mp;
  543. vm_object_t object;
  544. vm_pindex_t pindex;
  545. int error;
  546. object = m->object;
  547. VM_OBJECT_ASSERT_WLOCKED(object);
  548. error = 0;
  549. vp = NULL;
  550. mp = NULL;
  551. /*
  552. * The object is already known NOT to be dead. It
  553. * is possible for the vget() to block the whole
  554. * pageout daemon, but the new low-memory handling
  555. * code should prevent it.
  556. *
  557. * We can't wait forever for the vnode lock, we might
  558. * deadlock due to a vn_read() getting stuck in
  559. * vm_wait while holding this vnode. We skip the
  560. * vnode if we can't get it in a reasonable amount
  561. * of time.
  562. */
  563. if (object->type == OBJT_VNODE) {
  564. vm_page_xunbusy(m);
  565. vp = object->handle;
  566. if (vp->v_type == VREG &&
  567. vn_start_write(vp, &mp, V_NOWAIT) != 0) {
  568. mp = NULL;
  569. error = EDEADLK;
  570. goto unlock_all;
  571. }
  572. KASSERT(mp != NULL,
  573. ("vp %p with NULL v_mount", vp));
  574. vm_object_reference_locked(object);
  575. pindex = m->pindex;
  576. VM_OBJECT_WUNLOCK(object);
  577. if (vget(vp, vn_lktype_write(NULL, vp) | LK_TIMELOCK) != 0) {
  578. vp = NULL;
  579. error = EDEADLK;
  580. goto unlock_mp;
  581. }
  582. VM_OBJECT_WLOCK(object);
  583. /*
  584. * Ensure that the object and vnode were not disassociated
  585. * while locks were dropped.
  586. */
  587. if (vp->v_object != object) {
  588. error = ENOENT;
  589. goto unlock_all;
  590. }
  591. /*
  592. * While the object was unlocked, the page may have been:
  593. * (1) moved to a different queue,
  594. * (2) reallocated to a different object,
  595. * (3) reallocated to a different offset, or
  596. * (4) cleaned.
  597. */
  598. if (!vm_page_in_laundry(m) || m->object != object ||
  599. m->pindex != pindex || m->dirty == 0) {
  600. error = ENXIO;
  601. goto unlock_all;
  602. }
  603. /*
  604. * The page may have been busied while the object lock was
  605. * released.
  606. */
  607. if (vm_page_tryxbusy(m) == 0) {
  608. error = EBUSY;
  609. goto unlock_all;
  610. }
  611. }
  612. /*
  613. * Remove all writeable mappings, failing if the page is wired.
  614. */
  615. if (!vm_page_try_remove_write(m)) {
  616. vm_page_xunbusy(m);
  617. error = EBUSY;
  618. goto unlock_all;
  619. }
  620. /*
  621. * If a page is dirty, then it is either being washed
  622. * (but not yet cleaned) or it is still in the
  623. * laundry. If it is still in the laundry, then we
  624. * start the cleaning operation.
  625. */
  626. if ((*numpagedout = vm_pageout_cluster(m)) == 0)
  627. error = EIO;
  628. unlock_all:
  629. VM_OBJECT_WUNLOCK(object);
  630. unlock_mp:
  631. if (mp != NULL) {
  632. if (vp != NULL)
  633. vput(vp);
  634. vm_object_deallocate(object);
  635. vn_finished_write(mp);
  636. }
  637. return (error);
  638. }
  639. /*
  640. * Attempt to launder the specified number of pages.
  641. *
  642. * Returns the number of pages successfully laundered.
  643. */
  644. static int
  645. vm_pageout_launder(struct vm_domain *vmd, int launder, bool in_shortfall)
  646. {
  647. struct scan_state ss;
  648. struct vm_pagequeue *pq;
  649. vm_object_t object;
  650. vm_page_t m, marker;
  651. vm_page_astate_t new, old;
  652. int act_delta, error, numpagedout, queue, refs, starting_target;
  653. int vnodes_skipped;
  654. bool pageout_ok;
  655. object = NULL;
  656. starting_target = launder;
  657. vnodes_skipped = 0;
  658. /*
  659. * Scan the laundry queues for pages eligible to be laundered. We stop
  660. * once the target number of dirty pages have been laundered, or once
  661. * we've reached the end of the queue. A single iteration of this loop
  662. * may cause more than one page to be laundered because of clustering.
  663. *
  664. * As an optimization, we avoid laundering from PQ_UNSWAPPABLE when no
  665. * swap devices are configured.
  666. */
  667. if (atomic_load_acq_int(&swapdev_enabled))
  668. queue = PQ_UNSWAPPABLE;
  669. else
  670. queue = PQ_LAUNDRY;
  671. scan:
  672. marker = &vmd->vmd_markers[queue];
  673. pq = &vmd->vmd_pagequeues[queue];
  674. vm_pagequeue_lock(pq);
  675. vm_pageout_init_scan(&ss, pq, marker, NULL, pq->pq_cnt);
  676. while (launder > 0 && (m = vm_pageout_next(&ss, false)) != NULL) {
  677. if (__predict_false((m->flags & PG_MARKER) != 0))
  678. continue;
  679. /*
  680. * Don't touch a page that was removed from the queue after the
  681. * page queue lock was released. Otherwise, ensure that any
  682. * pending queue operations, such as dequeues for wired pages,
  683. * are handled.
  684. */
  685. if (vm_pageout_defer(m, queue, true))
  686. continue;
  687. /*
  688. * Lock the page's object.
  689. */
  690. if (object == NULL || object != m->object) {
  691. if (object != NULL)
  692. VM_OBJECT_WUNLOCK(object);
  693. object = atomic_load_ptr(&m->object);
  694. if (__predict_false(object == NULL))
  695. /* The page is being freed by another thread. */
  696. continue;
  697. /* Depends on type-stability. */
  698. VM_OBJECT_WLOCK(object);
  699. if (__predict_false(m->object != object)) {
  700. VM_OBJECT_WUNLOCK(object);
  701. object = NULL;
  702. continue;
  703. }
  704. }
  705. if (vm_page_tryxbusy(m) == 0)
  706. continue;
  707. /*
  708. * Check for wirings now that we hold the object lock and have
  709. * exclusively busied the page. If the page is mapped, it may
  710. * still be wired by pmap lookups. The call to
  711. * vm_page_try_remove_all() below atomically checks for such
  712. * wirings and removes mappings. If the page is unmapped, the
  713. * wire count is guaranteed not to increase after this check.
  714. */
  715. if (__predict_false(vm_page_wired(m)))
  716. goto skip_page;
  717. /*
  718. * Invalid pages can be easily freed. They cannot be
  719. * mapped; vm_page_free() asserts this.
  720. */
  721. if (vm_page_none_valid(m))
  722. goto free_page;
  723. refs = object->ref_count != 0 ? pmap_ts_referenced(m) : 0;
  724. for (old = vm_page_astate_load(m);;) {
  725. /*
  726. * Check to see if the page has been removed from the
  727. * queue since the first such check. Leave it alone if
  728. * so, discarding any references collected by
  729. * pmap_ts_referenced().
  730. */
  731. if (__predict_false(_vm_page_queue(old) == PQ_NONE))
  732. goto skip_page;
  733. new = old;
  734. act_delta = refs;
  735. if ((old.flags & PGA_REFERENCED) != 0) {
  736. new.flags &= ~PGA_REFERENCED;
  737. act_delta++;
  738. }
  739. if (act_delta == 0) {
  740. ;
  741. } else if (object->ref_count != 0) {
  742. /*
  743. * Increase the activation count if the page was
  744. * referenced while in the laundry queue. This
  745. * makes it less likely that the page will be
  746. * returned prematurely to the laundry queue.
  747. */
  748. new.act_count += ACT_ADVANCE +
  749. act_delta;
  750. if (new.act_count > ACT_MAX)
  751. new.act_count = ACT_MAX;
  752. new.flags &= ~PGA_QUEUE_OP_MASK;
  753. new.flags |= PGA_REQUEUE;
  754. new.queue = PQ_ACTIVE;
  755. if (!vm_page_pqstate_commit(m, &old, new))
  756. continue;
  757. /*
  758. * If this was a background laundering, count
  759. * activated pages towards our target. The
  760. * purpose of background laundering is to ensure
  761. * that pages are eventually cycled through the
  762. * laundry queue, and an activation is a valid
  763. * way out.
  764. */
  765. if (!in_shortfall)
  766. launder--;
  767. VM_CNT_INC(v_reactivated);
  768. goto skip_page;
  769. } else if ((object->flags & OBJ_DEAD) == 0) {
  770. new.flags |= PGA_REQUEUE;
  771. if (!vm_page_pqstate_commit(m, &old, new))
  772. continue;
  773. goto skip_page;
  774. }
  775. break;
  776. }
  777. /*
  778. * If the page appears to be clean at the machine-independent
  779. * layer, then remove all of its mappings from the pmap in
  780. * anticipation of freeing it. If, however, any of the page's
  781. * mappings allow write access, then the page may still be
  782. * modified until the last of those mappings are removed.
  783. */
  784. if (object->ref_count != 0) {
  785. vm_page_test_dirty(m);
  786. if (m->dirty == 0 && !vm_page_try_remove_all(m))
  787. goto skip_page;
  788. }
  789. /*
  790. * Clean pages are freed, and dirty pages are paged out unless
  791. * they belong to a dead object. Requeueing dirty pages from
  792. * dead objects is pointless, as they are being paged out and
  793. * freed by the thread that destroyed the object.
  794. */
  795. if (m->dirty == 0) {
  796. free_page:
  797. /*
  798. * Now we are guaranteed that no other threads are
  799. * manipulating the page, check for a last-second
  800. * reference.
  801. */
  802. if (vm_pageout_defer(m, queue, true))
  803. goto skip_page;
  804. vm_page_free(m);
  805. VM_CNT_INC(v_dfree);
  806. } else if ((object->flags & OBJ_DEAD) == 0) {
  807. if ((object->flags & OBJ_SWAP) != 0)
  808. pageout_ok = disable_swap_pageouts == 0;
  809. else
  810. pageout_ok = true;
  811. if (!pageout_ok) {
  812. vm_page_launder(m);
  813. goto skip_page;
  814. }
  815. /*
  816. * Form a cluster with adjacent, dirty pages from the
  817. * same object, and page out that entire cluster.
  818. *
  819. * The adjacent, dirty pages must also be in the
  820. * laundry. However, their mappings are not checked
  821. * for new references. Consequently, a recently
  822. * referenced page may be paged out. However, that
  823. * page will not be prematurely reclaimed. After page
  824. * out, the page will be placed in the inactive queue,
  825. * where any new references will be detected and the
  826. * page reactivated.
  827. */
  828. error = vm_pageout_clean(m, &numpagedout);
  829. if (error == 0) {
  830. launder -= numpagedout;
  831. ss.scanned += numpagedout;
  832. } else if (error == EDEADLK) {
  833. pageout_lock_miss++;
  834. vnodes_skipped++;
  835. }
  836. object = NULL;
  837. } else {
  838. skip_page:
  839. vm_page_xunbusy(m);
  840. }
  841. }
  842. if (object != NULL) {
  843. VM_OBJECT_WUNLOCK(object);
  844. object = NULL;
  845. }
  846. vm_pagequeue_lock(pq);
  847. vm_pageout_end_scan(&ss);
  848. vm_pagequeue_unlock(pq);
  849. if (launder > 0 && queue == PQ_UNSWAPPABLE) {
  850. queue = PQ_LAUNDRY;
  851. goto scan;
  852. }
  853. /*
  854. * Wakeup the sync daemon if we skipped a vnode in a writeable object
  855. * and we didn't launder enough pages.
  856. */
  857. if (vnodes_skipped > 0 && launder > 0)
  858. (void)speedup_syncer();
  859. return (starting_target - launder);
  860. }
  861. /*
  862. * Compute the integer square root.
  863. */
  864. static u_int
  865. isqrt(u_int num)
  866. {
  867. u_int bit, root, tmp;
  868. bit = num != 0 ? (1u << ((fls(num) - 1) & ~1)) : 0;
  869. root = 0;
  870. while (bit != 0) {
  871. tmp = root + bit;
  872. root >>= 1;
  873. if (num >= tmp) {
  874. num -= tmp;
  875. root += bit;
  876. }
  877. bit >>= 2;
  878. }
  879. return (root);
  880. }
  881. /*
  882. * Perform the work of the laundry thread: periodically wake up and determine
  883. * whether any pages need to be laundered. If so, determine the number of pages
  884. * that need to be laundered, and launder them.
  885. */
  886. static void
  887. vm_pageout_laundry_worker(void *arg)
  888. {
  889. struct vm_domain *vmd;
  890. struct vm_pagequeue *pq;
  891. uint64_t nclean, ndirty, nfreed;
  892. int domain, last_target, launder, shortfall, shortfall_cycle, target;
  893. bool in_shortfall;
  894. domain = (uintptr_t)arg;
  895. vmd = VM_DOMAIN(domain);
  896. pq = &vmd->vmd_pagequeues[PQ_LAUNDRY];
  897. KASSERT(vmd->vmd_segs != 0, ("domain without segments"));
  898. shortfall = 0;
  899. in_shortfall = false;
  900. shortfall_cycle = 0;
  901. last_target = target = 0;
  902. nfreed = 0;
  903. /*
  904. * Calls to these handlers are serialized by the swap syscall lock.
  905. */
  906. (void)EVENTHANDLER_REGISTER(swapon, vm_pageout_swapon, vmd,
  907. EVENTHANDLER_PRI_ANY);
  908. (void)EVENTHANDLER_REGISTER(swapoff, vm_pageout_swapoff, vmd,
  909. EVENTHANDLER_PRI_ANY);
  910. /*
  911. * The pageout laundry worker is never done, so loop forever.
  912. */
  913. for (;;) {
  914. KASSERT(target >= 0, ("negative target %d", target));
  915. KASSERT(shortfall_cycle >= 0,
  916. ("negative cycle %d", shortfall_cycle));
  917. launder = 0;
  918. /*
  919. * First determine whether we need to launder pages to meet a
  920. * shortage of free pages.
  921. */
  922. if (shortfall > 0) {
  923. in_shortfall = true;
  924. shortfall_cycle = VM_LAUNDER_RATE / VM_INACT_SCAN_RATE;
  925. target = shortfall;
  926. } else if (!in_shortfall)
  927. goto trybackground;
  928. else if (shortfall_cycle == 0 || vm_laundry_target(vmd) <= 0) {
  929. /*
  930. * We recently entered shortfall and began laundering
  931. * pages. If we have completed that laundering run
  932. * (and we are no longer in shortfall) or we have met
  933. * our laundry target through other activity, then we
  934. * can stop laundering pages.
  935. */
  936. in_shortfall = false;
  937. target = 0;
  938. goto trybackground;
  939. }
  940. launder = target / shortfall_cycle--;
  941. goto dolaundry;
  942. /*
  943. * There's no immediate need to launder any pages; see if we
  944. * meet the conditions to perform background laundering:
  945. *
  946. * 1. The ratio of dirty to clean inactive pages exceeds the
  947. * background laundering threshold, or
  948. * 2. we haven't yet reached the target of the current
  949. * background laundering run.
  950. *
  951. * The background laundering threshold is not a constant.
  952. * Instead, it is a slowly growing function of the number of
  953. * clean pages freed by the page daemon since the last
  954. * background laundering. Thus, as the ratio of dirty to
  955. * clean inactive pages grows, the amount of memory pressure
  956. * required to trigger laundering decreases. We ensure
  957. * that the threshold is non-zero after an inactive queue
  958. * scan, even if that scan failed to free a single clean page.
  959. */
  960. trybackground:
  961. nclean = vmd->vmd_free_count +
  962. vmd->vmd_pagequeues[PQ_INACTIVE].pq_cnt;
  963. ndirty = vmd->vmd_pagequeues[PQ_LAUNDRY].pq_cnt;
  964. if (target == 0 && ndirty * isqrt(howmany(nfreed + 1,
  965. vmd->vmd_free_target - vmd->vmd_free_min)) >= nclean) {
  966. target = vmd->vmd_background_launder_target;
  967. }
  968. /*
  969. * We have a non-zero background laundering target. If we've
  970. * laundered up to our maximum without observing a page daemon
  971. * request, just stop. This is a safety belt that ensures we
  972. * don't launder an excessive amount if memory pressure is low
  973. * and the ratio of dirty to clean pages is large. Otherwise,
  974. * proceed at the background laundering rate.
  975. */
  976. if (target > 0) {
  977. if (nfreed > 0) {
  978. nfreed = 0;
  979. last_target = target;
  980. } else if (last_target - target >=
  981. vm_background_launder_max * PAGE_SIZE / 1024) {
  982. target = 0;
  983. }
  984. launder = vm_background_launder_rate * PAGE_SIZE / 1024;
  985. launder /= VM_LAUNDER_RATE;
  986. if (launder > target)
  987. launder = target;
  988. }
  989. dolaundry:
  990. if (launder > 0) {
  991. /*
  992. * Because of I/O clustering, the number of laundered
  993. * pages could exceed "target" by the maximum size of
  994. * a cluster minus one.
  995. */
  996. target -= min(vm_pageout_launder(vmd, launder,
  997. in_shortfall), target);
  998. pause("laundp", hz / VM_LAUNDER_RATE);
  999. }
  1000. /*
  1001. * If we're not currently laundering pages and the page daemon
  1002. * hasn't posted a new request, sleep until the page daemon
  1003. * kicks us.
  1004. */
  1005. vm_pagequeue_lock(pq);
  1006. if (target == 0 && vmd->vmd_laundry_request == VM_LAUNDRY_IDLE)
  1007. (void)mtx_sleep(&vmd->vmd_laundry_request,
  1008. vm_pagequeue_lockptr(pq), PVM, "launds", 0);
  1009. /*
  1010. * If the pagedaemon has indicated that it's in shortfall, start
  1011. * a shortfall laundering unless we're already in the middle of
  1012. * one. This may preempt a background laundering.
  1013. */
  1014. if (vmd->vmd_laundry_request == VM_LAUNDRY_SHORTFALL &&
  1015. (!in_shortfall || shortfall_cycle == 0)) {
  1016. shortfall = vm_laundry_target(vmd) +
  1017. vmd->vmd_pageout_deficit;
  1018. target = 0;
  1019. } else
  1020. shortfall = 0;
  1021. if (target == 0)
  1022. vmd->vmd_laundry_request = VM_LAUNDRY_IDLE;
  1023. nfreed += vmd->vmd_clean_pages_freed;
  1024. vmd->vmd_clean_pages_freed = 0;
  1025. vm_pagequeue_unlock(pq);
  1026. }
  1027. }
  1028. /*
  1029. * Compute the number of pages we want to try to move from the
  1030. * active queue to either the inactive or laundry queue.
  1031. *
  1032. * When scanning active pages during a shortage, we make clean pages
  1033. * count more heavily towards the page shortage than dirty pages.
  1034. * This is because dirty pages must be laundered before they can be
  1035. * reused and thus have less utility when attempting to quickly
  1036. * alleviate a free page shortage. However, this weighting also
  1037. * causes the scan to deactivate dirty pages more aggressively,
  1038. * improving the effectiveness of clustering.
  1039. */
  1040. static int
  1041. vm_pageout_active_target(struct vm_domain *vmd)
  1042. {
  1043. int shortage;
  1044. shortage = vmd->vmd_inactive_target + vm_paging_target(vmd) -
  1045. (vmd->vmd_pagequeues[PQ_INACTIVE].pq_cnt +
  1046. vmd->vmd_pagequeues[PQ_LAUNDRY].pq_cnt / act_scan_laundry_weight);
  1047. shortage *= act_scan_laundry_weight;
  1048. return (shortage);
  1049. }
  1050. /*
  1051. * Scan the active queue. If there is no shortage of inactive pages, scan a
  1052. * small portion of the queue in order to maintain quasi-LRU.
  1053. */
  1054. static void
  1055. vm_pageout_scan_active(struct vm_domain *vmd, int page_shortage)
  1056. {
  1057. struct scan_state ss;
  1058. vm_object_t object;
  1059. vm_page_t m, marker;
  1060. struct vm_pagequeue *pq;
  1061. vm_page_astate_t old, new;
  1062. long min_scan;
  1063. int act_delta, max_scan, ps_delta, refs, scan_tick;
  1064. uint8_t nqueue;
  1065. marker = &vmd->vmd_markers[PQ_ACTIVE];
  1066. pq = &vmd->vmd_pagequeues[PQ_ACTIVE];
  1067. vm_pagequeue_lock(pq);
  1068. /*
  1069. * If we're just idle polling attempt to visit every
  1070. * active page within 'update_period' seconds.
  1071. */
  1072. scan_tick = ticks;
  1073. if (vm_pageout_update_period != 0) {
  1074. min_scan = pq->pq_cnt;
  1075. min_scan *= scan_tick - vmd->vmd_last_active_scan;
  1076. min_scan /= hz * vm_pageout_update_period;
  1077. } else
  1078. min_scan = 0;
  1079. if (min_scan > 0 || (page_shortage > 0 && pq->pq_cnt > 0))
  1080. vmd->vmd_last_active_scan = scan_tick;
  1081. /*
  1082. * Scan the active queue for pages that can be deactivated. Update
  1083. * the per-page activity counter and use it to identify deactivation
  1084. * candidates. Held pages may be deactivated.
  1085. *
  1086. * To avoid requeuing each page that remains in the active queue, we
  1087. * implement the CLOCK algorithm. To keep the implementation of the
  1088. * enqueue operation consistent for all page queues, we use two hands,
  1089. * represented by marker pages. Scans begin at the first hand, which
  1090. * precedes the second hand in the queue. When the two hands meet,
  1091. * they are moved back to the head and tail of the queue, respectively,
  1092. * and scanning resumes.
  1093. */
  1094. max_scan = page_shortage > 0 ? pq->pq_cnt : min_scan;
  1095. act_scan:
  1096. vm_pageout_init_scan(&ss, pq, marker, &vmd->vmd_clock[0], max_scan);
  1097. while ((m = vm_pageout_next(&ss, false)) != NULL) {
  1098. if (__predict_false(m == &vmd->vmd_clock[1])) {
  1099. vm_pagequeue_lock(pq);
  1100. TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_clock[0], plinks.q);
  1101. TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_clock[1], plinks.q);
  1102. TAILQ_INSERT_HEAD(&pq->pq_pl, &vmd->vmd_clock[0],
  1103. plinks.q);
  1104. TAILQ_INSERT_TAIL(&pq->pq_pl, &vmd->vmd_clock[1],
  1105. plinks.q);
  1106. max_scan -= ss.scanned;
  1107. vm_pageout_end_scan(&ss);
  1108. goto act_scan;
  1109. }
  1110. if (__predict_false((m->flags & PG_MARKER) != 0))
  1111. continue;
  1112. /*
  1113. * Don't touch a page that was removed from the queue after the
  1114. * page queue lock was released. Otherwise, ensure that any
  1115. * pending queue operations, such as dequeues for wired pages,
  1116. * are handled.
  1117. */
  1118. if (vm_pageout_defer(m, PQ_ACTIVE, true))
  1119. continue;
  1120. /*
  1121. * A page's object pointer may be set to NULL before
  1122. * the object lock is acquired.
  1123. */
  1124. object = atomic_load_ptr(&m->object);
  1125. if (__predict_false(object == NULL))
  1126. /*
  1127. * The page has been removed from its object.
  1128. */
  1129. continue;
  1130. /* Deferred free of swap space. */
  1131. if ((m->a.flags & PGA_SWAP_FREE) != 0 &&
  1132. VM_OBJECT_TRYWLOCK(object)) {
  1133. if (m->object == object)
  1134. vm_pager_page_unswapped(m);
  1135. VM_OBJECT_WUNLOCK(object);
  1136. }
  1137. /*
  1138. * Check to see "how much" the page has been used.
  1139. *
  1140. * Test PGA_REFERENCED after calling pmap_ts_referenced() so
  1141. * that a reference from a concurrently destroyed mapping is
  1142. * observed here and now.
  1143. *
  1144. * Perform an unsynchronized object ref count check. While
  1145. * the page lock ensures that the page is not reallocated to
  1146. * another object, in particular, one with unmanaged mappings
  1147. * that cannot support pmap_ts_referenced(), two races are,
  1148. * nonetheless, possible:
  1149. * 1) The count was transitioning to zero, but we saw a non-
  1150. * zero value. pmap_ts_referenced() will return zero
  1151. * because the page is not mapped.
  1152. * 2) The count was transitioning to one, but we saw zero.
  1153. * This race delays the detection of a new reference. At
  1154. * worst, we will deactivate and reactivate the page.
  1155. */
  1156. refs = object->ref_count != 0 ? pmap_ts_referenced(m) : 0;
  1157. old = vm_page_astate_load(m);
  1158. do {
  1159. /*
  1160. * Check to see if the page has been removed from the
  1161. * queue since the first such check. Leave it alone if
  1162. * so, discarding any references collected by
  1163. * pmap_ts_referenced().
  1164. */
  1165. if (__predict_false(_vm_page_queue(old) == PQ_NONE)) {
  1166. ps_delta = 0;
  1167. break;
  1168. }
  1169. /*
  1170. * Advance or decay the act_count based on recent usage.
  1171. */
  1172. new = old;
  1173. act_delta = refs;
  1174. if ((old.flags & PGA_REFERENCED) != 0) {
  1175. new.flags &= ~PGA_REFERENCED;
  1176. act_delta++;
  1177. }
  1178. if (act_delta != 0) {
  1179. new.act_count += ACT_ADVANCE + act_delta;
  1180. if (new.act_count > ACT_MAX)
  1181. new.act_count = ACT_MAX;
  1182. } else {
  1183. new.act_count -= min(new.act_count,
  1184. ACT_DECLINE);
  1185. }
  1186. if (new.act_count > 0) {
  1187. /*
  1188. * Adjust the activation count and keep the page
  1189. * in the active queue. The count might be left
  1190. * unchanged if it is saturated. The page may
  1191. * have been moved to a different queue since we
  1192. * started the scan, in which case we move it
  1193. * back.
  1194. */
  1195. ps_delta = 0;
  1196. if (old.queue != PQ_ACTIVE) {
  1197. new.flags &= ~PGA_QUEUE_OP_MASK;
  1198. new.flags |= PGA_REQUEUE;
  1199. new.queue = PQ_ACTIVE;
  1200. }
  1201. } else {
  1202. /*
  1203. * When not short for inactive pages, let dirty
  1204. * pages go through the inactive queue before
  1205. * moving to the laundry queue. This gives them
  1206. * some extra time to be reactivated,
  1207. * potentially avoiding an expensive pageout.
  1208. * However, during a page shortage, the inactive
  1209. * queue is necessarily small, and so dirty
  1210. * pages would only spend a trivial amount of
  1211. * time in the inactive queue. Therefore, we
  1212. * might as well place them directly in the
  1213. * laundry queue to reduce queuing overhead.
  1214. *
  1215. * Calling vm_page_test_dirty() here would
  1216. * require acquisition of the object's write
  1217. * lock. However, during a page shortage,
  1218. * directing dirty pages into the laundry queue
  1219. * is only an optimization and not a
  1220. * requirement. Therefore, we simply rely on
  1221. * the opportunistic updates to the page's dirty
  1222. * field by the pmap.
  1223. */
  1224. if (page_shortage <= 0) {
  1225. nqueue = PQ_INACTIVE;
  1226. ps_delta = 0;
  1227. } else if (m->dirty == 0) {
  1228. nqueue = PQ_INACTIVE;
  1229. ps_delta = act_scan_laundry_weight;
  1230. } else {
  1231. nqueue = PQ_LAUNDRY;
  1232. ps_delta = 1;
  1233. }
  1234. new.flags &= ~PGA_QUEUE_OP_MASK;
  1235. new.flags |= PGA_REQUEUE;
  1236. new.queue = nqueue;
  1237. }
  1238. } while (!vm_page_pqstate_commit(m, &old, new));
  1239. page_shortage -= ps_delta;
  1240. }
  1241. vm_pagequeue_lock(pq);
  1242. TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_clock[0], plinks.q);
  1243. TAILQ_INSERT_AFTER(&pq->pq_pl, marker, &vmd->vmd_clock[0], plinks.q);
  1244. vm_pageout_end_scan(&ss);
  1245. vm_pagequeue_unlock(pq);
  1246. }
  1247. static int
  1248. vm_pageout_reinsert_inactive_page(struct vm_pagequeue *pq, vm_page_t marker,
  1249. vm_page_t m)
  1250. {
  1251. vm_page_astate_t as;
  1252. vm_pagequeue_assert_locked(pq);
  1253. as = vm_page_astate_load(m);
  1254. if (as.queue != PQ_INACTIVE || (as.flags & PGA_ENQUEUED) != 0)
  1255. return (0);
  1256. vm_page_aflag_set(m, PGA_ENQUEUED);
  1257. TAILQ_INSERT_BEFORE(marker, m, plinks.q);
  1258. return (1);
  1259. }
  1260. /*
  1261. * Re-add stuck pages to the inactive queue. We will examine them again
  1262. * during the next scan. If the queue state of a page has changed since
  1263. * it was physically removed from the page queue in
  1264. * vm_pageout_collect_batch(), don't do anything with that page.
  1265. */
  1266. static void
  1267. vm_pageout_reinsert_inactive(struct scan_state *ss, struct vm_batchqueue *bq,
  1268. vm_page_t m)
  1269. {
  1270. struct vm_pagequeue *pq;
  1271. vm_page_t marker;
  1272. int delta;
  1273. delta = 0;
  1274. marker = ss->marker;
  1275. pq = ss->pq;
  1276. if (m != NULL) {
  1277. if (vm_batchqueue_insert(bq, m) != 0)
  1278. return;
  1279. vm_pagequeue_lock(pq);
  1280. delta += vm_pageout_reinsert_inactive_page(pq, marker, m);
  1281. } else
  1282. vm_pagequeue_lock(pq);
  1283. while ((m = vm_batchqueue_pop(bq)) != NULL)
  1284. delta += vm_pageout_reinsert_inactive_page(pq, marker, m);
  1285. vm_pagequeue_cnt_add(pq, delta);
  1286. vm_pagequeue_unlock(pq);
  1287. vm_batchqueue_init(bq);
  1288. }
  1289. static void
  1290. vm_pageout_scan_inactive(struct vm_domain *vmd, int page_shortage)
  1291. {
  1292. struct timeval start, end;
  1293. struct scan_state ss;
  1294. struct vm_batchqueue rq;
  1295. struct vm_page marker_page;
  1296. vm_page_t m, marker;
  1297. struct vm_pagequeue *pq;
  1298. vm_object_t object;
  1299. vm_page_astate_t old, new;
  1300. int act_delta, addl_page_shortage, starting_page_shortage, refs;
  1301. object = NULL;
  1302. vm_batchqueue_init(&rq);
  1303. getmicrouptime(&start);
  1304. /*
  1305. * The addl_page_shortage is an estimate of the number of temporarily
  1306. * stuck pages in the inactive queue. In other words, the
  1307. * number of pages from the inactive count that should be
  1308. * discounted in setting the target for the active queue scan.
  1309. */
  1310. addl_page_shortage = 0;
  1311. /*
  1312. * Start scanning the inactive queue for pages that we can free. The
  1313. * scan will stop when we reach the target or we have scanned the
  1314. * entire queue. (Note that m->a.act_count is not used to make
  1315. * decisions for the inactive queue, only for the active queue.)
  1316. */
  1317. starting_page_shortage = page_shortage;
  1318. marker = &marker_page;
  1319. vm_page_init_marker(marker, PQ_INACTIVE, 0);
  1320. pq = &vmd->vmd_pagequeues[PQ_INACTIVE];
  1321. vm_pagequeue_lock(pq);
  1322. vm_pageout_init_scan(&ss, pq, marker, NULL, pq->pq_cnt);
  1323. while (page_shortage > 0) {
  1324. /*
  1325. * If we need to refill the scan batch queue, release any
  1326. * optimistically held object lock. This gives someone else a
  1327. * chance to grab the lock, and also avoids holding it while we
  1328. * do unrelated work.
  1329. */
  1330. if (object != NULL && vm_batchqueue_empty(&ss.bq)) {
  1331. VM_OBJECT_WUNLOCK(object);
  1332. object = NULL;
  1333. }
  1334. m = vm_pageout_next(&ss, true);
  1335. if (m == NULL)
  1336. break;
  1337. KASSERT((m->flags & PG_MARKER) == 0,
  1338. ("marker page %p was dequeued", m));
  1339. /*
  1340. * Don't touch a page that was removed from the queue after the
  1341. * page queue lock was released. Otherwise, ensure that any
  1342. * pending queue operations, such as dequeues for wired pages,
  1343. * are handled.
  1344. */
  1345. if (vm_pageout_defer(m, PQ_INACTIVE, false))
  1346. continue;
  1347. /*
  1348. * Lock the page's object.
  1349. */
  1350. if (object == NULL || object != m->object) {
  1351. if (object != NULL)
  1352. VM_OBJECT_WUNLOCK(object);
  1353. object = atomic_load_ptr(&m->object);
  1354. if (__predict_false(object == NULL))
  1355. /* The page is being freed by another thread. */
  1356. continue;
  1357. /* Depends on type-stability. */
  1358. VM_OBJECT_WLOCK(object);
  1359. if (__predict_false(m->object != object)) {
  1360. VM_OBJECT_WUNLOCK(object);
  1361. object = NULL;
  1362. goto reinsert;
  1363. }
  1364. }
  1365. if (vm_page_tryxbusy(m) == 0) {
  1366. /*
  1367. * Don't mess with busy pages. Leave them at
  1368. * the front of the queue. Most likely, they
  1369. * are being paged out and will leave the
  1370. * queue shortly after the scan finishes. So,
  1371. * they ought to be discounted from the
  1372. * inactive count.
  1373. */
  1374. addl_page_shortage++;
  1375. goto reinsert;
  1376. }
  1377. /* Deferred free of swap space. */
  1378. if ((m->a.flags & PGA_SWAP_FREE) != 0)
  1379. vm_pager_page_unswapped(m);
  1380. /*
  1381. * Check for wirings now that we hold the object lock and have
  1382. * exclusively busied the page. If the page is mapped, it may
  1383. * still be wired by pmap lookups. The call to
  1384. * vm_page_try_remove_all() below atomically checks for such
  1385. * wirings and removes mappings. If the page is unmapped, the
  1386. * wire count is guaranteed not to increase after this check.
  1387. */
  1388. if (__predict_false(vm_page_wired(m)))
  1389. goto skip_page;
  1390. /*
  1391. * Invalid pages can be easily freed. They cannot be
  1392. * mapped, vm_page_free() asserts this.
  1393. */
  1394. if (vm_page_none_valid(m))
  1395. goto free_page;
  1396. refs = object->ref_count != 0 ? pmap_ts_referenced(m) : 0;
  1397. for (old = vm_page_astate_load(m);;) {
  1398. /*
  1399. * Check to see if the page has been removed from the
  1400. * queue since the first such check. Leave it alone if
  1401. * so, discarding any references collected by
  1402. * pmap_ts_referenced().
  1403. */
  1404. if (__predict_false(_vm_page_queue(old) == PQ_NONE))
  1405. goto skip_page;
  1406. new = old;
  1407. act_delta = refs;
  1408. if ((old.flags & PGA_REFERENCED) != 0) {
  1409. new.flags &= ~PGA_REFERENCED;
  1410. act_delta++;
  1411. }
  1412. if (act_delta == 0) {
  1413. ;
  1414. } else if (object->ref_count != 0) {
  1415. /*
  1416. * Increase the activation count if the
  1417. * page was referenced while in the
  1418. * inactive queue. This makes it less
  1419. * likely that the page will be returned
  1420. * prematurely to the inactive queue.
  1421. */
  1422. new.act_count += ACT_ADVANCE +
  1423. act_delta;
  1424. if (new.act_count > ACT_MAX)
  1425. new.act_count = ACT_MAX;
  1426. new.flags &= ~PGA_QUEUE_OP_MASK;
  1427. new.flags |= PGA_REQUEUE;
  1428. new.queue = PQ_ACTIVE;
  1429. if (!vm_page_pqstate_commit(m, &old, new))
  1430. continue;
  1431. VM_CNT_INC(v_reactivated);
  1432. goto skip_page;
  1433. } else if ((object->flags & OBJ_DEAD) == 0) {
  1434. new.queue = PQ_INACTIVE;
  1435. new.flags |= PGA_REQUEUE;
  1436. if (!vm_page_pqstate_commit(m, &old, new))
  1437. continue;
  1438. goto skip_page;
  1439. }
  1440. break;
  1441. }
  1442. /*
  1443. * If the page appears to be clean at the machine-independent
  1444. * layer, then remove all of its mappings from the pmap in
  1445. * anticipation of freeing it. If, however, any of the page's
  1446. * mappings allow write access, then the page may still be
  1447. * modified until the last of those mappings are removed.
  1448. */
  1449. if (object->ref_count != 0) {
  1450. vm_page_test_dirty(m);
  1451. if (m->dirty == 0 && !vm_page_try_remove_all(m))
  1452. goto skip_page;
  1453. }
  1454. /*
  1455. * Clean pages can be freed, but dirty pages must be sent back
  1456. * to the laundry, unless they belong to a dead object.
  1457. * Requeueing dirty pages from dead objects is pointless, as
  1458. * they are being paged out and freed by the thread that
  1459. * destroyed the object.
  1460. */
  1461. if (m->dirty == 0) {
  1462. free_page:
  1463. /*
  1464. * Now we are guaranteed that no other threads are
  1465. * manipulating the page, check for a last-second
  1466. * reference that would save it from doom.
  1467. */
  1468. if (vm_pageout_defer(m, PQ_INACTIVE, false))
  1469. goto skip_page;
  1470. /*
  1471. * Because we dequeued the page and have already checked
  1472. * for pending dequeue and enqueue requests, we can
  1473. * safely disassociate the page from the inactive queue
  1474. * without holding the queue lock.
  1475. */
  1476. m->a.queue = PQ_NONE;
  1477. vm_page_free(m);
  1478. page_shortage--;
  1479. continue;
  1480. }
  1481. if ((object->flags & OBJ_DEAD) == 0)
  1482. vm_page_launder(m);
  1483. skip_page:
  1484. vm_page_xunbusy(m);
  1485. continue;
  1486. reinsert:
  1487. vm_pageout_reinsert_inactive(&ss, &rq, m);
  1488. }
  1489. if (object != NULL)
  1490. VM_OBJECT_WUNLOCK(object);
  1491. vm_pageout_reinsert_inactive(&ss, &rq, NULL);
  1492. vm_pageout_reinsert_inactive(&ss, &ss.bq, NULL);
  1493. vm_pagequeue_lock(pq);
  1494. vm_pageout_end_scan(&ss);
  1495. vm_pagequeue_unlock(pq);
  1496. /*
  1497. * Record the remaining shortage and the progress and rate it was made.
  1498. */
  1499. atomic_add_int(&vmd->vmd_addl_shortage, addl_page_shortage);
  1500. getmicrouptime(&end);
  1501. timevalsub(&end, &start);
  1502. atomic_add_int(&vmd->vmd_inactive_us,
  1503. end.tv_sec * 1000000 + end.tv_usec);
  1504. atomic_add_int(&vmd->vmd_inactive_freed,
  1505. starting_page_shortage - page_shortage);
  1506. }
  1507. /*
  1508. * Dispatch a number of inactive threads according to load and collect the
  1509. * results to present a coherent view of paging activity on this domain.
  1510. */
  1511. static int
  1512. vm_pageout_inactive_dispatch(struct vm_domain *vmd, int shortage)
  1513. {
  1514. u_int freed, pps, slop, threads, us;
  1515. vmd->vmd_inactive_shortage = shortage;
  1516. slop = 0;
  1517. /*
  1518. * If we have more work than we can do in a quarter of our interval, we
  1519. * fire off multiple threads to process it.
  1520. */
  1521. threads = vmd->vmd_inactive_threads;
  1522. if (threads > 1 && vmd->vmd_inactive_pps != 0 &&
  1523. shortage > vmd->vmd_inactive_pps / VM_INACT_SCAN_RATE / 4) {
  1524. vmd->vmd_inactive_shortage /= threads;
  1525. slop = shortage % threads;
  1526. vm_domain_pageout_lock(vmd);
  1527. blockcount_acquire(&vmd->vmd_inactive_starting, threads - 1);
  1528. blockcount_acquire(&vmd->vmd_inactive_running, threads - 1);
  1529. wakeup(&vmd->vmd_inactive_shortage);
  1530. vm_domain_pageout_unlock(vmd);
  1531. }
  1532. /* Run the local thread scan. */
  1533. vm_pageout_scan_inactive(vmd, vmd->vmd_inactive_shortage + slop);
  1534. /*
  1535. * Block until helper threads report results and then accumulate
  1536. * totals.
  1537. */
  1538. blockcount_wait(&vmd->vmd_inactive_running, NULL, "vmpoid", PVM);
  1539. freed = atomic_readandclear_int(&vmd->vmd_inactive_freed);
  1540. VM_CNT_ADD(v_dfree, freed);
  1541. /*
  1542. * Calculate the per-thread paging rate with an exponential decay of
  1543. * prior results. Careful to avoid integer rounding errors with large
  1544. * us values.
  1545. */
  1546. us = max(atomic_readandclear_int(&vmd->vmd_inactive_us), 1);
  1547. if (us > 1000000)
  1548. /* Keep rounding to tenths */
  1549. pps = (freed * 10) / ((us * 10) / 1000000);
  1550. else
  1551. pps = (1000000 / us) * freed;
  1552. vmd->vmd_inactive_pps = (vmd->vmd_inactive_pps / 2) + (pps / 2);
  1553. return (shortage - freed);
  1554. }
  1555. /*
  1556. * Attempt to reclaim the requested number of pages from the inactive queue.
  1557. * Returns true if the shortage was addressed.
  1558. */
  1559. static int
  1560. vm_pageout_inactive(struct vm_domain *vmd, int shortage, int *addl_shortage)
  1561. {
  1562. struct vm_pagequeue *pq;
  1563. u_int addl_page_shortage, deficit, page_shortage;
  1564. u_int starting_page_shortage;
  1565. /*
  1566. * vmd_pageout_deficit counts the number of pages requested in
  1567. * allocations that failed because of a free page shortage. We assume
  1568. * that the allocations will be reattempted and thus include the deficit
  1569. * in our scan target.
  1570. */
  1571. deficit = atomic_readandclear_int(&vmd->vmd_pageout_deficit);
  1572. starting_page_shortage = shortage + deficit;
  1573. /*
  1574. * Run the inactive scan on as many threads as is necessary.
  1575. */
  1576. page_shortage = vm_pageout_inactive_dispatch(vmd, starting_page_shortage);
  1577. addl_page_shortage = atomic_readandclear_int(&vmd->vmd_addl_shortage);
  1578. /*
  1579. * Wake up the laundry thread so that it can perform any needed
  1580. * laundering. If we didn't meet our target, we're in shortfall and
  1581. * need to launder more aggressively. If PQ_LAUNDRY is empty and no
  1582. * swap devices are configured, the laundry thread has no work to do, so
  1583. * don't bother waking it up.
  1584. *
  1585. * The laundry thread uses the number of inactive queue scans elapsed
  1586. * since the last laundering to determine whether to launder again, so
  1587. * keep count.
  1588. */
  1589. if (starting_page_shortage > 0) {
  1590. pq = &vmd->vmd_pagequeues[PQ_LAUNDRY];
  1591. vm_pagequeue_lock(pq);
  1592. if (vmd->vmd_laundry_request == VM_LAUNDRY_IDLE &&
  1593. (pq->pq_cnt > 0 || atomic_load_acq_int(&swapdev_enabled))) {
  1594. if (page_shortage > 0) {
  1595. vmd->vmd_laundry_request = VM_LAUNDRY_SHORTFALL;
  1596. VM_CNT_INC(v_pdshortfalls);
  1597. } else if (vmd->vmd_laundry_request !=
  1598. VM_LAUNDRY_SHORTFALL)
  1599. vmd->vmd_laundry_request =
  1600. VM_LAUNDRY_BACKGROUND;
  1601. wakeup(&vmd->vmd_laundry_request);
  1602. }
  1603. vmd->vmd_clean_pages_freed +=
  1604. starting_page_shortage - page_shortage;
  1605. vm_pagequeue_unlock(pq);
  1606. }
  1607. /*
  1608. * Wakeup the swapout daemon if we didn't free the targeted number of
  1609. * pages.
  1610. */
  1611. if (page_shortage > 0)
  1612. vm_swapout_run();
  1613. /*
  1614. * If the inactive queue scan fails repeatedly to meet its
  1615. * target, kill the largest process.
  1616. */
  1617. vm_pageout_mightbe_oom(vmd, page_shortage, starting_page_shortage);
  1618. /*
  1619. * Reclaim pages by swapping out idle processes, if configured to do so.
  1620. */
  1621. vm_swapout_run_idle();
  1622. /*
  1623. * See the description of addl_page_shortage above.
  1624. */
  1625. *addl_shortage = addl_page_shortage + deficit;
  1626. return (page_shortage <= 0);
  1627. }
  1628. static int vm_pageout_oom_vote;
  1629. /*
  1630. * The pagedaemon threads randlomly select one to perform the
  1631. * OOM. Trying to kill processes before all pagedaemons
  1632. * failed to reach free target is premature.
  1633. */
  1634. static void
  1635. vm_pageout_mightbe_oom(struct vm_domain *vmd, int page_shortage,
  1636. int starting_page_shortage)
  1637. {
  1638. int old_vote;
  1639. if (starting_page_shortage <= 0 || starting_page_shortage !=
  1640. page_shortage)
  1641. vmd->vmd_oom_seq = 0;
  1642. else
  1643. vmd->vmd_oom_seq++;
  1644. if (vmd->vmd_oom_seq < vm_pageout_oom_seq) {
  1645. if (vmd->vmd_oom) {
  1646. vmd->vmd_oom = FALSE;
  1647. atomic_subtract_int(&vm_pageout_oom_vote, 1);
  1648. }
  1649. return;
  1650. }
  1651. /*
  1652. * Do not follow the call sequence until OOM condition is
  1653. * cleared.
  1654. */
  1655. vmd->vmd_oom_seq = 0;
  1656. if (vmd->vmd_oom)
  1657. return;
  1658. vmd->vmd_oom = TRUE;
  1659. old_vote = atomic_fetchadd_int(&vm_pageout_oom_vote, 1);
  1660. if (old_vote != vm_ndomains - 1)
  1661. return;
  1662. /*
  1663. * The current pagedaemon thread is the last in the quorum to
  1664. * start OOM. Initiate the selection and signaling of the
  1665. * victim.
  1666. */
  1667. vm_pageout_oom(VM_OOM_MEM);
  1668. /*
  1669. * After one round of OOM terror, recall our vote. On the
  1670. * next pass, current pagedaemon would vote again if the low
  1671. * memory condition is still there, due to vmd_oom being
  1672. * false.
  1673. */
  1674. vmd->vmd_oom = FALSE;
  1675. atomic_subtract_int(&vm_pageout_oom_vote, 1);
  1676. }
  1677. /*
  1678. * The OOM killer is the page daemon's action of last resort when
  1679. * memory allocation requests have been stalled for a prolonged period
  1680. * of time because it cannot reclaim memory. This function computes
  1681. * the approximate number of physical pages that could be reclaimed if
  1682. * the specified address space is destroyed.
  1683. *
  1684. * Private, anonymous memory owned by the address space is the
  1685. * principal resource that we expect to recover after an OOM kill.
  1686. * Since the physical pages mapped by the address space's COW entries
  1687. * are typically shared pages, they are unlikely to be released and so
  1688. * they are not counted.
  1689. *
  1690. * To get to the point where the page daemon runs the OOM killer, its
  1691. * efforts to write-back vnode-backed pages may have stalled. This
  1692. * could be caused by a memory allocation deadlock in the write path
  1693. * that might be resolved by an OOM kill. Therefore, physical pages
  1694. * belonging to vnode-backed objects are counted, because they might
  1695. * be freed without being written out first if the address space holds
  1696. * the last reference to an unlinked vnode.
  1697. *
  1698. * Similarly, physical pages belonging to OBJT_PHYS objects are
  1699. * counted because the address space might hold the last reference to
  1700. * the object.
  1701. */
  1702. static long
  1703. vm_pageout_oom_pagecount(struct vmspace *vmspace)
  1704. {
  1705. vm_map_t map;
  1706. vm_map_entry_t entry;
  1707. vm_object_t obj;
  1708. long res;
  1709. map = &vmspace->vm_map;
  1710. KASSERT(!map->system_map, ("system map"));
  1711. sx_assert(&map->lock, SA_LOCKED);
  1712. res = 0;
  1713. VM_MAP_ENTRY_FOREACH(entry, map) {
  1714. if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
  1715. continue;
  1716. obj = entry->object.vm_object;
  1717. if (obj == NULL)
  1718. continue;
  1719. if ((entry->eflags & MAP_ENTRY_NEEDS_COPY) != 0 &&
  1720. obj->ref_count != 1)
  1721. continue;
  1722. if (obj->type == OBJT_PHYS || obj->type == OBJT_VNODE ||
  1723. (obj->flags & OBJ_SWAP) != 0)
  1724. res += obj->resident_page_count;
  1725. }
  1726. return (res);
  1727. }
  1728. static int vm_oom_ratelim_last;
  1729. static int vm_oom_pf_secs = 10;
  1730. SYSCTL_INT(_vm, OID_AUTO, oom_pf_secs, CTLFLAG_RWTUN, &vm_oom_pf_secs, 0,
  1731. "");
  1732. static struct mtx vm_oom_ratelim_mtx;
  1733. void
  1734. vm_pageout_oom(int shortage)
  1735. {
  1736. const char *reason;
  1737. struct proc *p, *bigproc;
  1738. vm_offset_t size, bigsize;
  1739. struct thread *td;
  1740. struct vmspace *vm;
  1741. int now;
  1742. bool breakout;
  1743. /*
  1744. * For OOM requests originating from vm_fault(), there is a high
  1745. * chance that a single large process faults simultaneously in
  1746. * several threads. Also, on an active system running many
  1747. * processes of middle-size, like buildworld, all of them
  1748. * could fault almost simultaneously as well.
  1749. *
  1750. * To avoid killing too many processes, rate-limit OOMs
  1751. * initiated by vm_fault() time-outs on the waits for free
  1752. * pages.
  1753. */
  1754. mtx_lock(&vm_oom_ratelim_mtx);
  1755. now = ticks;
  1756. if (shortage == VM_OOM_MEM_PF &&
  1757. (u_int)(now - vm_oom_ratelim_last) < hz * vm_oom_pf_secs) {
  1758. mtx_unlock(&vm_oom_ratelim_mtx);
  1759. return;
  1760. }
  1761. vm_oom_ratelim_last = now;
  1762. mtx_unlock(&vm_oom_ratelim_mtx);
  1763. /*
  1764. * We keep the process bigproc locked once we find it to keep anyone
  1765. * from messing with it; however, there is a possibility of
  1766. * deadlock if process B is bigproc and one of its child processes
  1767. * attempts to propagate a signal to B while we are waiting for A's
  1768. * lock while walking this list. To avoid this, we don't block on
  1769. * the process lock but just skip a process if it is already locked.
  1770. */
  1771. bigproc = NULL;
  1772. bigsize = 0;
  1773. sx_slock(&allproc_lock);
  1774. FOREACH_PROC_IN_SYSTEM(p) {
  1775. PROC_LOCK(p);
  1776. /*
  1777. * If this is a system, protected or killed process, skip it.
  1778. */
  1779. if (p->p_state != PRS_NORMAL || (p->p_flag & (P_INEXEC |
  1780. P_PROTECTED | P_SYSTEM | P_WEXIT)) != 0 ||
  1781. p->p_pid == 1 || P_KILLED(p) ||
  1782. (p->p_pid < 48 && swap_pager_avail != 0)) {
  1783. PROC_UNLOCK(p);
  1784. continue;
  1785. }
  1786. /*
  1787. * If the process is in a non-running type state,
  1788. * don't touch it. Check all the threads individually.
  1789. */
  1790. breakout = false;
  1791. FOREACH_THREAD_IN_PROC(p, td) {
  1792. thread_lock(td);
  1793. if (!TD_ON_RUNQ(td) &&
  1794. !TD_IS_RUNNING(td) &&
  1795. !TD_IS_SLEEPING(td) &&
  1796. !TD_IS_SUSPENDED(td) &&
  1797. !TD_IS_SWAPPED(td)) {
  1798. thread_unlock(td);
  1799. breakout = true;
  1800. break;
  1801. }
  1802. thread_unlock(td);
  1803. }
  1804. if (breakout) {
  1805. PROC_UNLOCK(p);
  1806. continue;
  1807. }
  1808. /*
  1809. * get the process size
  1810. */
  1811. vm = vmspace_acquire_ref(p);
  1812. if (vm == NULL) {
  1813. PROC_UNLOCK(p);
  1814. continue;
  1815. }
  1816. _PHOLD_LITE(p);
  1817. PROC_UNLOCK(p);
  1818. sx_sunlock(&allproc_lock);
  1819. if (!vm_map_trylock_read(&vm->vm_map)) {
  1820. vmspace_free(vm);
  1821. sx_slock(&allproc_lock);
  1822. PRELE(p);
  1823. continue;
  1824. }
  1825. size = vmspace_swap_count(vm);
  1826. if (shortage == VM_OOM_MEM || shortage == VM_OOM_MEM_PF)
  1827. size += vm_pageout_oom_pagecount(vm);
  1828. vm_map_unlock_read(&vm->vm_map);
  1829. vmspace_free(vm);
  1830. sx_slock(&allproc_lock);
  1831. /*
  1832. * If this process is bigger than the biggest one,
  1833. * remember it.
  1834. */
  1835. if (size > bigsize) {
  1836. if (bigproc != NULL)
  1837. PRELE(bigproc);
  1838. bigproc = p;
  1839. bigsize = size;
  1840. } else {
  1841. PRELE(p);
  1842. }
  1843. }
  1844. sx_sunlock(&allproc_lock);
  1845. if (bigproc != NULL) {
  1846. switch (shortage) {
  1847. case VM_OOM_MEM:
  1848. reason = "failed to reclaim memory";
  1849. break;
  1850. case VM_OOM_MEM_PF:
  1851. reason = "a thread waited too long to allocate a page";
  1852. break;
  1853. case VM_OOM_SWAPZ:
  1854. reason = "out of swap space";
  1855. break;
  1856. default:
  1857. panic("unknown OOM reason %d", shortage);
  1858. }
  1859. if (vm_panic_on_oom != 0 && --vm_panic_on_oom == 0)
  1860. panic("%s", reason);
  1861. PROC_LOCK(bigproc);
  1862. killproc(bigproc, reason);
  1863. sched_nice(bigproc, PRIO_MIN);
  1864. _PRELE(bigproc);
  1865. PROC_UNLOCK(bigproc);
  1866. }
  1867. }
  1868. /*
  1869. * Signal a free page shortage to subsystems that have registered an event
  1870. * handler. Reclaim memory from UMA in the event of a severe shortage.
  1871. * Return true if the free page count should be re-evaluated.
  1872. */
  1873. static bool
  1874. vm_pageout_lowmem(void)
  1875. {
  1876. static int lowmem_ticks = 0;
  1877. int last;
  1878. bool ret;
  1879. ret = false;
  1880. last = atomic_load_int(&lowmem_ticks);
  1881. while ((u_int)(ticks - last) / hz >= lowmem_period) {
  1882. if (atomic_fcmpset_int(&lowmem_ticks, &last, ticks) == 0)
  1883. continue;
  1884. /*
  1885. * Decrease registered cache sizes.
  1886. */
  1887. SDT_PROBE0(vm, , , vm__lowmem_scan);
  1888. EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_PAGES);
  1889. /*
  1890. * We do this explicitly after the caches have been
  1891. * drained above.
  1892. */
  1893. uma_reclaim(UMA_RECLAIM_TRIM);
  1894. ret = true;
  1895. break;
  1896. }
  1897. /*
  1898. * Kick off an asynchronous reclaim of cached memory if one of the
  1899. * page daemons is failing to keep up with demand. Use the "severe"
  1900. * threshold instead of "min" to ensure that we do not blow away the
  1901. * caches if a subset of the NUMA domains are depleted by kernel memory
  1902. * allocations; the domainset iterators automatically skip domains
  1903. * below the "min" threshold on the first pass.
  1904. *
  1905. * UMA reclaim worker has its own rate-limiting mechanism, so don't
  1906. * worry about kicking it too often.
  1907. */
  1908. if (vm_page_count_severe())
  1909. uma_reclaim_wakeup();
  1910. return (ret);
  1911. }
  1912. static void
  1913. vm_pageout_worker(void *arg)
  1914. {
  1915. struct vm_domain *vmd;
  1916. u_int ofree;
  1917. int addl_shortage, domain, shortage;
  1918. bool target_met;
  1919. domain = (uintptr_t)arg;
  1920. vmd = VM_DOMAIN(domain);
  1921. shortage = 0;
  1922. target_met = true;
  1923. /*
  1924. * XXXKIB It could be useful to bind pageout daemon threads to
  1925. * the cores belonging to the domain, from which vm_page_array
  1926. * is allocated.
  1927. */
  1928. KASSERT(vmd->vmd_segs != 0, ("domain without segments"));
  1929. vmd->vmd_last_active_scan = ticks;
  1930. /*
  1931. * The pageout daemon worker is never done, so loop forever.
  1932. */
  1933. while (TRUE) {
  1934. vm_domain_pageout_lock(vmd);
  1935. /*
  1936. * We need to clear wanted before we check the limits. This
  1937. * prevents races with wakers who will check wanted after they
  1938. * reach the limit.
  1939. */
  1940. atomic_store_int(&vmd->vmd_pageout_wanted, 0);
  1941. /*
  1942. * Might the page daemon need to run again?
  1943. */
  1944. if (vm_paging_needed(vmd, vmd->vmd_free_count)) {
  1945. /*
  1946. * Yes. If the scan failed to produce enough free
  1947. * pages, sleep uninterruptibly for some time in the
  1948. * hope that the laundry thread will clean some pages.
  1949. */
  1950. vm_domain_pageout_unlock(vmd);
  1951. if (!target_met)
  1952. pause("pwait", hz / VM_INACT_SCAN_RATE);
  1953. } else {
  1954. /*
  1955. * No, sleep until the next wakeup or until pages
  1956. * need to have their reference stats updated.
  1957. */
  1958. if (mtx_sleep(&vmd->vmd_pageout_wanted,
  1959. vm_domain_pageout_lockptr(vmd), PDROP | PVM,
  1960. "psleep", hz / VM_INACT_SCAN_RATE) == 0)
  1961. VM_CNT_INC(v_pdwakeups);
  1962. }
  1963. /* Prevent spurious wakeups by ensuring that wanted is set. */
  1964. atomic_store_int(&vmd->vmd_pageout_wanted, 1);
  1965. /*
  1966. * Use the controller to calculate how many pages to free in
  1967. * this interval, and scan the inactive queue. If the lowmem
  1968. * handlers appear to have freed up some pages, subtract the
  1969. * difference from the inactive queue scan target.
  1970. */
  1971. shortage = pidctrl_daemon(&vmd->vmd_pid, vmd->vmd_free_count);
  1972. if (shortage > 0) {
  1973. ofree = vmd->vmd_free_count;
  1974. if (vm_pageout_lowmem() && vmd->vmd_free_count > ofree)
  1975. shortage -= min(vmd->vmd_free_count - ofree,
  1976. (u_int)shortage);
  1977. target_met = vm_pageout_inactive(vmd, shortage,
  1978. &addl_shortage);
  1979. } else
  1980. addl_shortage = 0;
  1981. /*
  1982. * Scan the active queue. A positive value for shortage
  1983. * indicates that we must aggressively deactivate pages to avoid
  1984. * a shortfall.
  1985. */
  1986. shortage = vm_pageout_active_target(vmd) + addl_shortage;
  1987. vm_pageout_scan_active(vmd, shortage);
  1988. }
  1989. }
  1990. /*
  1991. * vm_pageout_helper runs additional pageout daemons in times of high paging
  1992. * activity.
  1993. */
  1994. static void
  1995. vm_pageout_helper(void *arg)
  1996. {
  1997. struct vm_domain *vmd;
  1998. int domain;
  1999. domain = (uintptr_t)arg;
  2000. vmd = VM_DOMAIN(domain);
  2001. vm_domain_pageout_lock(vmd);
  2002. for (;;) {
  2003. msleep(&vmd->vmd_inactive_shortage,
  2004. vm_domain_pageout_lockptr(vmd), PVM, "psleep", 0);
  2005. blockcount_release(&vmd->vmd_inactive_starting, 1);
  2006. vm_domain_pageout_unlock(vmd);
  2007. vm_pageout_scan_inactive(vmd, vmd->vmd_inactive_shortage);
  2008. vm_domain_pageout_lock(vmd);
  2009. /*
  2010. * Release the running count while the pageout lock is held to
  2011. * prevent wakeup races.
  2012. */
  2013. blockcount_release(&vmd->vmd_inactive_running, 1);
  2014. }
  2015. }
  2016. static int
  2017. get_pageout_threads_per_domain(const struct vm_domain *vmd)
  2018. {
  2019. unsigned total_pageout_threads, eligible_cpus, domain_cpus;
  2020. if (VM_DOMAIN_EMPTY(vmd->vmd_domain))
  2021. return (0);
  2022. /*
  2023. * Semi-arbitrarily constrain pagedaemon threads to less than half the
  2024. * total number of CPUs in the system as an upper limit.
  2025. */
  2026. if (pageout_cpus_per_thread < 2)
  2027. pageout_cpus_per_thread = 2;
  2028. else if (pageout_cpus_per_thread > mp_ncpus)
  2029. pageout_cpus_per_thread = mp_ncpus;
  2030. total_pageout_threads = howmany(mp_ncpus, pageout_cpus_per_thread);
  2031. domain_cpus = CPU_COUNT(&cpuset_domain[vmd->vmd_domain]);
  2032. /* Pagedaemons are not run in empty domains. */
  2033. eligible_cpus = mp_ncpus;
  2034. for (unsigned i = 0; i < vm_ndomains; i++)
  2035. if (VM_DOMAIN_EMPTY(i))
  2036. eligible_cpus -= CPU_COUNT(&cpuset_domain[i]);
  2037. /*
  2038. * Assign a portion of the total pageout threads to this domain
  2039. * corresponding to the fraction of pagedaemon-eligible CPUs in the
  2040. * domain. In asymmetric NUMA systems, domains with more CPUs may be
  2041. * allocated more threads than domains with fewer CPUs.
  2042. */
  2043. return (howmany(total_pageout_threads * domain_cpus, eligible_cpus));
  2044. }
  2045. /*
  2046. * Initialize basic pageout daemon settings. See the comment above the
  2047. * definition of vm_domain for some explanation of how these thresholds are
  2048. * used.
  2049. */
  2050. static void
  2051. vm_pageout_init_domain(int domain)
  2052. {
  2053. struct vm_domain *vmd;
  2054. struct sysctl_oid *oid;
  2055. vmd = VM_DOMAIN(domain);
  2056. vmd->vmd_interrupt_free_min = 2;
  2057. /*
  2058. * v_free_reserved needs to include enough for the largest
  2059. * swap pager structures plus enough for any pv_entry structs
  2060. * when paging.
  2061. */
  2062. vmd->vmd_pageout_free_min = 2 * MAXBSIZE / PAGE_SIZE +
  2063. vmd->vmd_interrupt_free_min;
  2064. vmd->vmd_free_reserved = vm_pageout_page_count +
  2065. vmd->vmd_pageout_free_min + vmd->vmd_page_count / 768;
  2066. vmd->vmd_free_min = vmd->vmd_page_count / 200;
  2067. vmd->vmd_free_severe = vmd->vmd_free_min / 2;
  2068. vmd->vmd_free_target = 4 * vmd->vmd_free_min + vmd->vmd_free_reserved;
  2069. vmd->vmd_free_min += vmd->vmd_free_reserved;
  2070. vmd->vmd_free_severe += vmd->vmd_free_reserved;
  2071. vmd->vmd_inactive_target = (3 * vmd->vmd_free_target) / 2;
  2072. if (vmd->vmd_inactive_target > vmd->vmd_free_count / 3)
  2073. vmd->vmd_inactive_target = vmd->vmd_free_count / 3;
  2074. /*
  2075. * Set the default wakeup threshold to be 10% below the paging
  2076. * target. This keeps the steady state out of shortfall.
  2077. */
  2078. vmd->vmd_pageout_wakeup_thresh = (vmd->vmd_free_target / 10) * 9;
  2079. /*
  2080. * Target amount of memory to move out of the laundry queue during a
  2081. * background laundering. This is proportional to the amount of system
  2082. * memory.
  2083. */
  2084. vmd->vmd_background_launder_target = (vmd->vmd_free_target -
  2085. vmd->vmd_free_min) / 10;
  2086. /* Initialize the pageout daemon pid controller. */
  2087. pidctrl_init(&vmd->vmd_pid, hz / VM_INACT_SCAN_RATE,
  2088. vmd->vmd_free_target, PIDCTRL_BOUND,
  2089. PIDCTRL_KPD, PIDCTRL_KID, PIDCTRL_KDD);
  2090. oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(vmd->vmd_oid), OID_AUTO,
  2091. "pidctrl", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
  2092. pidctrl_init_sysctl(&vmd->vmd_pid, SYSCTL_CHILDREN(oid));
  2093. vmd->vmd_inactive_threads = get_pageout_threads_per_domain(vmd);
  2094. }
  2095. static void
  2096. vm_pageout_init(void)
  2097. {
  2098. u_long freecount;
  2099. int i;
  2100. /*
  2101. * Initialize some paging parameters.
  2102. */
  2103. if (vm_cnt.v_page_count < 2000)
  2104. vm_pageout_page_count = 8;
  2105. freecount = 0;
  2106. for (i = 0; i < vm_ndomains; i++) {
  2107. struct vm_domain *vmd;
  2108. vm_pageout_init_domain(i);
  2109. vmd = VM_DOMAIN(i);
  2110. vm_cnt.v_free_reserved += vmd->vmd_free_reserved;
  2111. vm_cnt.v_free_target += vmd->vmd_free_target;
  2112. vm_cnt.v_free_min += vmd->vmd_free_min;
  2113. vm_cnt.v_inactive_target += vmd->vmd_inactive_target;
  2114. vm_cnt.v_pageout_free_min += vmd->vmd_pageout_free_min;
  2115. vm_cnt.v_interrupt_free_min += vmd->vmd_interrupt_free_min;
  2116. vm_cnt.v_free_severe += vmd->vmd_free_severe;
  2117. freecount += vmd->vmd_free_count;
  2118. }
  2119. /*
  2120. * Set interval in seconds for active scan. We want to visit each
  2121. * page at least once every ten minutes. This is to prevent worst
  2122. * case paging behaviors with stale active LRU.
  2123. */
  2124. if (vm_pageout_update_period == 0)
  2125. vm_pageout_update_period = 600;
  2126. /*
  2127. * Set the maximum number of user-wired virtual pages. Historically the
  2128. * main source of such pages was mlock(2) and mlockall(2). Hypervisors
  2129. * may also request user-wired memory.
  2130. */
  2131. if (vm_page_max_user_wired == 0)
  2132. vm_page_max_user_wired = 4 * freecount / 5;
  2133. }
  2134. /*
  2135. * vm_pageout is the high level pageout daemon.
  2136. */
  2137. static void
  2138. vm_pageout(void)
  2139. {
  2140. struct proc *p;
  2141. struct thread *td;
  2142. int error, first, i, j, pageout_threads;
  2143. p = curproc;
  2144. td = curthread;
  2145. mtx_init(&vm_oom_ratelim_mtx, "vmoomr", NULL, MTX_DEF);
  2146. swap_pager_swap_init();
  2147. for (first = -1, i = 0; i < vm_ndomains; i++) {
  2148. if (VM_DOMAIN_EMPTY(i)) {
  2149. if (bootverbose)
  2150. printf("domain %d empty; skipping pageout\n",
  2151. i);
  2152. continue;
  2153. }
  2154. if (first == -1)
  2155. first = i;
  2156. else {
  2157. error = kthread_add(vm_pageout_worker,
  2158. (void *)(uintptr_t)i, p, NULL, 0, 0, "dom%d", i);
  2159. if (error != 0)
  2160. panic("starting pageout for domain %d: %d\n",
  2161. i, error);
  2162. }
  2163. pageout_threads = VM_DOMAIN(i)->vmd_inactive_threads;
  2164. for (j = 0; j < pageout_threads - 1; j++) {
  2165. error = kthread_add(vm_pageout_helper,
  2166. (void *)(uintptr_t)i, p, NULL, 0, 0,
  2167. "dom%d helper%d", i, j);
  2168. if (error != 0)
  2169. panic("starting pageout helper %d for domain "
  2170. "%d: %d\n", j, i, error);
  2171. }
  2172. error = kthread_add(vm_pageout_laundry_worker,
  2173. (void *)(uintptr_t)i, p, NULL, 0, 0, "laundry: dom%d", i);
  2174. if (error != 0)
  2175. panic("starting laundry for domain %d: %d", i, error);
  2176. }
  2177. error = kthread_add(uma_reclaim_worker, NULL, p, NULL, 0, 0, "uma");
  2178. if (error != 0)
  2179. panic("starting uma_reclaim helper, error %d\n", error);
  2180. snprintf(td->td_name, sizeof(td->td_name), "dom%d", first);
  2181. vm_pageout_worker((void *)(uintptr_t)first);
  2182. }
  2183. /*
  2184. * Perform an advisory wakeup of the page daemon.
  2185. */
  2186. void
  2187. pagedaemon_wakeup(int domain)
  2188. {
  2189. struct vm_domain *vmd;
  2190. vmd = VM_DOMAIN(domain);
  2191. vm_domain_pageout_assert_unlocked(vmd);
  2192. if (curproc == pageproc)
  2193. return;
  2194. if (atomic_fetchadd_int(&vmd->vmd_pageout_wanted, 1) == 0) {
  2195. vm_domain_pageout_lock(vmd);
  2196. atomic_store_int(&vmd->vmd_pageout_wanted, 1);
  2197. wakeup(&vmd->vmd_pageout_wanted);
  2198. vm_domain_pageout_unlock(vmd);
  2199. }
  2200. }