vmscan.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009
  1. /*
  2. * linux/mm/vmscan.c
  3. *
  4. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  5. *
  6. * Swap reorganised 29.12.95, Stephen Tweedie.
  7. * kswapd added: 7.1.96 sct
  8. * Removed kswapd_ctl limits, and swap out as many pages as needed
  9. * to bring the system back to freepages.high: 2.4.97, Rik van Riel.
  10. * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com).
  11. * Multiqueue VM started 5.8.00, Rik van Riel.
  12. */
  13. #include <linux/mm.h>
  14. #include <linux/module.h>
  15. #include <linux/gfp.h>
  16. #include <linux/kernel_stat.h>
  17. #include <linux/swap.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/init.h>
  20. #include <linux/highmem.h>
  21. #include <linux/vmstat.h>
  22. #include <linux/file.h>
  23. #include <linux/writeback.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/buffer_head.h> /* for try_to_release_page(),
  26. buffer_heads_over_limit */
  27. #include <linux/mm_inline.h>
  28. #include <linux/pagevec.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/rmap.h>
  31. #include <linux/topology.h>
  32. #include <linux/cpu.h>
  33. #include <linux/cpuset.h>
  34. #include <linux/notifier.h>
  35. #include <linux/rwsem.h>
  36. #include <linux/delay.h>
  37. #include <linux/kthread.h>
  38. #include <linux/freezer.h>
  39. #include <linux/memcontrol.h>
  40. #include <linux/delayacct.h>
  41. #include <linux/sysctl.h>
  42. #include <asm/tlbflush.h>
  43. #include <asm/div64.h>
  44. #include <linux/swapops.h>
  45. #include "internal.h"
  46. #define CREATE_TRACE_POINTS
  47. #include <trace/events/vmscan.h>
  48. struct scan_control {
  49. /* Incremented by the number of inactive pages that were scanned */
  50. unsigned long nr_scanned;
  51. /* Number of pages freed so far during a call to shrink_zones() */
  52. unsigned long nr_reclaimed;
  53. /* How many pages shrink_list() should reclaim */
  54. unsigned long nr_to_reclaim;
  55. unsigned long hibernation_mode;
  56. /* This context's GFP mask */
  57. gfp_t gfp_mask;
  58. int may_writepage;
  59. /* Can mapped pages be reclaimed? */
  60. int may_unmap;
  61. /* Can pages be swapped as part of reclaim? */
  62. int may_swap;
  63. int swappiness;
  64. int order;
  65. /*
  66. * Intend to reclaim enough contenious memory rather than to reclaim
  67. * enough amount memory. I.e, it's the mode for high order allocation.
  68. */
  69. bool lumpy_reclaim_mode;
  70. /* Which cgroup do we reclaim from */
  71. struct mem_cgroup *mem_cgroup;
  72. /*
  73. * Nodemask of nodes allowed by the caller. If NULL, all nodes
  74. * are scanned.
  75. */
  76. nodemask_t *nodemask;
  77. };
  78. #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
  79. #ifdef ARCH_HAS_PREFETCH
  80. #define prefetch_prev_lru_page(_page, _base, _field) \
  81. do { \
  82. if ((_page)->lru.prev != _base) { \
  83. struct page *prev; \
  84. \
  85. prev = lru_to_page(&(_page->lru)); \
  86. prefetch(&prev->_field); \
  87. } \
  88. } while (0)
  89. #else
  90. #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
  91. #endif
  92. #ifdef ARCH_HAS_PREFETCHW
  93. #define prefetchw_prev_lru_page(_page, _base, _field) \
  94. do { \
  95. if ((_page)->lru.prev != _base) { \
  96. struct page *prev; \
  97. \
  98. prev = lru_to_page(&(_page->lru)); \
  99. prefetchw(&prev->_field); \
  100. } \
  101. } while (0)
  102. #else
  103. #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
  104. #endif
  105. /*
  106. * From 0 .. 100. Higher means more swappy.
  107. */
  108. int vm_swappiness = 60;
  109. long vm_total_pages; /* The total number of pages which the VM controls */
  110. static LIST_HEAD(shrinker_list);
  111. static DECLARE_RWSEM(shrinker_rwsem);
  112. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  113. #define scanning_global_lru(sc) (!(sc)->mem_cgroup)
  114. #else
  115. #define scanning_global_lru(sc) (1)
  116. #endif
  117. static struct zone_reclaim_stat *get_reclaim_stat(struct zone *zone,
  118. struct scan_control *sc)
  119. {
  120. if (!scanning_global_lru(sc))
  121. return mem_cgroup_get_reclaim_stat(sc->mem_cgroup, zone);
  122. return &zone->reclaim_stat;
  123. }
  124. static unsigned long zone_nr_lru_pages(struct zone *zone,
  125. struct scan_control *sc, enum lru_list lru)
  126. {
  127. if (!scanning_global_lru(sc))
  128. return mem_cgroup_zone_nr_pages(sc->mem_cgroup, zone, lru);
  129. return zone_page_state(zone, NR_LRU_BASE + lru);
  130. }
  131. /*
  132. * Add a shrinker callback to be called from the vm
  133. */
  134. void register_shrinker(struct shrinker *shrinker)
  135. {
  136. shrinker->nr = 0;
  137. down_write(&shrinker_rwsem);
  138. list_add_tail(&shrinker->list, &shrinker_list);
  139. up_write(&shrinker_rwsem);
  140. }
  141. EXPORT_SYMBOL(register_shrinker);
  142. /*
  143. * Remove one
  144. */
  145. void unregister_shrinker(struct shrinker *shrinker)
  146. {
  147. down_write(&shrinker_rwsem);
  148. list_del(&shrinker->list);
  149. up_write(&shrinker_rwsem);
  150. }
  151. EXPORT_SYMBOL(unregister_shrinker);
  152. #define SHRINK_BATCH 128
  153. /*
  154. * Call the shrink functions to age shrinkable caches
  155. *
  156. * Here we assume it costs one seek to replace a lru page and that it also
  157. * takes a seek to recreate a cache object. With this in mind we age equal
  158. * percentages of the lru and ageable caches. This should balance the seeks
  159. * generated by these structures.
  160. *
  161. * If the vm encountered mapped pages on the LRU it increase the pressure on
  162. * slab to avoid swapping.
  163. *
  164. * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
  165. *
  166. * `lru_pages' represents the number of on-LRU pages in all the zones which
  167. * are eligible for the caller's allocation attempt. It is used for balancing
  168. * slab reclaim versus page reclaim.
  169. *
  170. * Returns the number of slab objects which we shrunk.
  171. */
  172. unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
  173. unsigned long lru_pages)
  174. {
  175. struct shrinker *shrinker;
  176. unsigned long ret = 0;
  177. if (scanned == 0)
  178. scanned = SWAP_CLUSTER_MAX;
  179. if (!down_read_trylock(&shrinker_rwsem))
  180. return 1; /* Assume we'll be able to shrink next time */
  181. list_for_each_entry(shrinker, &shrinker_list, list) {
  182. unsigned long long delta;
  183. unsigned long total_scan;
  184. unsigned long max_pass;
  185. max_pass = (*shrinker->shrink)(shrinker, 0, gfp_mask);
  186. delta = (4 * scanned) / shrinker->seeks;
  187. delta *= max_pass;
  188. do_div(delta, lru_pages + 1);
  189. shrinker->nr += delta;
  190. if (shrinker->nr < 0) {
  191. printk(KERN_ERR "shrink_slab: %pF negative objects to "
  192. "delete nr=%ld\n",
  193. shrinker->shrink, shrinker->nr);
  194. shrinker->nr = max_pass;
  195. }
  196. /*
  197. * Avoid risking looping forever due to too large nr value:
  198. * never try to free more than twice the estimate number of
  199. * freeable entries.
  200. */
  201. if (shrinker->nr > max_pass * 2)
  202. shrinker->nr = max_pass * 2;
  203. total_scan = shrinker->nr;
  204. shrinker->nr = 0;
  205. while (total_scan >= SHRINK_BATCH) {
  206. long this_scan = SHRINK_BATCH;
  207. int shrink_ret;
  208. int nr_before;
  209. nr_before = (*shrinker->shrink)(shrinker, 0, gfp_mask);
  210. shrink_ret = (*shrinker->shrink)(shrinker, this_scan,
  211. gfp_mask);
  212. if (shrink_ret == -1)
  213. break;
  214. if (shrink_ret < nr_before)
  215. ret += nr_before - shrink_ret;
  216. count_vm_events(SLABS_SCANNED, this_scan);
  217. total_scan -= this_scan;
  218. cond_resched();
  219. }
  220. shrinker->nr += total_scan;
  221. }
  222. up_read(&shrinker_rwsem);
  223. return ret;
  224. }
  225. static inline int is_page_cache_freeable(struct page *page)
  226. {
  227. /*
  228. * A freeable page cache page is referenced only by the caller
  229. * that isolated the page, the page cache radix tree and
  230. * optional buffer heads at page->private.
  231. */
  232. return page_count(page) - page_has_private(page) == 2;
  233. }
  234. static int may_write_to_queue(struct backing_dev_info *bdi)
  235. {
  236. if (current->flags & PF_SWAPWRITE)
  237. return 1;
  238. if (!bdi_write_congested(bdi))
  239. return 1;
  240. if (bdi == current->backing_dev_info)
  241. return 1;
  242. return 0;
  243. }
  244. /*
  245. * We detected a synchronous write error writing a page out. Probably
  246. * -ENOSPC. We need to propagate that into the address_space for a subsequent
  247. * fsync(), msync() or close().
  248. *
  249. * The tricky part is that after writepage we cannot touch the mapping: nothing
  250. * prevents it from being freed up. But we have a ref on the page and once
  251. * that page is locked, the mapping is pinned.
  252. *
  253. * We're allowed to run sleeping lock_page() here because we know the caller has
  254. * __GFP_FS.
  255. */
  256. static void handle_write_error(struct address_space *mapping,
  257. struct page *page, int error)
  258. {
  259. lock_page_nosync(page);
  260. if (page_mapping(page) == mapping)
  261. mapping_set_error(mapping, error);
  262. unlock_page(page);
  263. }
  264. /* Request for sync pageout. */
  265. enum pageout_io {
  266. PAGEOUT_IO_ASYNC,
  267. PAGEOUT_IO_SYNC,
  268. };
  269. /* possible outcome of pageout() */
  270. typedef enum {
  271. /* failed to write page out, page is locked */
  272. PAGE_KEEP,
  273. /* move page to the active list, page is locked */
  274. PAGE_ACTIVATE,
  275. /* page has been sent to the disk successfully, page is unlocked */
  276. PAGE_SUCCESS,
  277. /* page is clean and locked */
  278. PAGE_CLEAN,
  279. } pageout_t;
  280. /*
  281. * pageout is called by shrink_page_list() for each dirty page.
  282. * Calls ->writepage().
  283. */
  284. static pageout_t pageout(struct page *page, struct address_space *mapping,
  285. enum pageout_io sync_writeback)
  286. {
  287. /*
  288. * If the page is dirty, only perform writeback if that write
  289. * will be non-blocking. To prevent this allocation from being
  290. * stalled by pagecache activity. But note that there may be
  291. * stalls if we need to run get_block(). We could test
  292. * PagePrivate for that.
  293. *
  294. * If this process is currently in __generic_file_aio_write() against
  295. * this page's queue, we can perform writeback even if that
  296. * will block.
  297. *
  298. * If the page is swapcache, write it back even if that would
  299. * block, for some throttling. This happens by accident, because
  300. * swap_backing_dev_info is bust: it doesn't reflect the
  301. * congestion state of the swapdevs. Easy to fix, if needed.
  302. */
  303. if (!is_page_cache_freeable(page))
  304. return PAGE_KEEP;
  305. if (!mapping) {
  306. /*
  307. * Some data journaling orphaned pages can have
  308. * page->mapping == NULL while being dirty with clean buffers.
  309. */
  310. if (page_has_private(page)) {
  311. if (try_to_free_buffers(page)) {
  312. ClearPageDirty(page);
  313. printk("%s: orphaned page\n", __func__);
  314. return PAGE_CLEAN;
  315. }
  316. }
  317. return PAGE_KEEP;
  318. }
  319. if (mapping->a_ops->writepage == NULL)
  320. return PAGE_ACTIVATE;
  321. if (!may_write_to_queue(mapping->backing_dev_info))
  322. return PAGE_KEEP;
  323. if (clear_page_dirty_for_io(page)) {
  324. int res;
  325. struct writeback_control wbc = {
  326. .sync_mode = WB_SYNC_NONE,
  327. .nr_to_write = SWAP_CLUSTER_MAX,
  328. .range_start = 0,
  329. .range_end = LLONG_MAX,
  330. .nonblocking = 1,
  331. .for_reclaim = 1,
  332. };
  333. SetPageReclaim(page);
  334. res = mapping->a_ops->writepage(page, &wbc);
  335. if (res < 0)
  336. handle_write_error(mapping, page, res);
  337. if (res == AOP_WRITEPAGE_ACTIVATE) {
  338. ClearPageReclaim(page);
  339. return PAGE_ACTIVATE;
  340. }
  341. /*
  342. * Wait on writeback if requested to. This happens when
  343. * direct reclaiming a large contiguous area and the
  344. * first attempt to free a range of pages fails.
  345. */
  346. if (PageWriteback(page) && sync_writeback == PAGEOUT_IO_SYNC)
  347. wait_on_page_writeback(page);
  348. if (!PageWriteback(page)) {
  349. /* synchronous write or broken a_ops? */
  350. ClearPageReclaim(page);
  351. }
  352. trace_mm_vmscan_writepage(page,
  353. trace_reclaim_flags(page, sync_writeback));
  354. inc_zone_page_state(page, NR_VMSCAN_WRITE);
  355. return PAGE_SUCCESS;
  356. }
  357. return PAGE_CLEAN;
  358. }
  359. /*
  360. * Same as remove_mapping, but if the page is removed from the mapping, it
  361. * gets returned with a refcount of 0.
  362. */
  363. static int __remove_mapping(struct address_space *mapping, struct page *page)
  364. {
  365. BUG_ON(!PageLocked(page));
  366. BUG_ON(mapping != page_mapping(page));
  367. spin_lock_irq(&mapping->tree_lock);
  368. /*
  369. * The non racy check for a busy page.
  370. *
  371. * Must be careful with the order of the tests. When someone has
  372. * a ref to the page, it may be possible that they dirty it then
  373. * drop the reference. So if PageDirty is tested before page_count
  374. * here, then the following race may occur:
  375. *
  376. * get_user_pages(&page);
  377. * [user mapping goes away]
  378. * write_to(page);
  379. * !PageDirty(page) [good]
  380. * SetPageDirty(page);
  381. * put_page(page);
  382. * !page_count(page) [good, discard it]
  383. *
  384. * [oops, our write_to data is lost]
  385. *
  386. * Reversing the order of the tests ensures such a situation cannot
  387. * escape unnoticed. The smp_rmb is needed to ensure the page->flags
  388. * load is not satisfied before that of page->_count.
  389. *
  390. * Note that if SetPageDirty is always performed via set_page_dirty,
  391. * and thus under tree_lock, then this ordering is not required.
  392. */
  393. if (!page_freeze_refs(page, 2))
  394. goto cannot_free;
  395. /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */
  396. if (unlikely(PageDirty(page))) {
  397. page_unfreeze_refs(page, 2);
  398. goto cannot_free;
  399. }
  400. if (PageSwapCache(page)) {
  401. swp_entry_t swap = { .val = page_private(page) };
  402. __delete_from_swap_cache(page);
  403. spin_unlock_irq(&mapping->tree_lock);
  404. swapcache_free(swap, page);
  405. } else {
  406. __remove_from_page_cache(page);
  407. spin_unlock_irq(&mapping->tree_lock);
  408. mem_cgroup_uncharge_cache_page(page);
  409. }
  410. return 1;
  411. cannot_free:
  412. spin_unlock_irq(&mapping->tree_lock);
  413. return 0;
  414. }
  415. /*
  416. * Attempt to detach a locked page from its ->mapping. If it is dirty or if
  417. * someone else has a ref on the page, abort and return 0. If it was
  418. * successfully detached, return 1. Assumes the caller has a single ref on
  419. * this page.
  420. */
  421. int remove_mapping(struct address_space *mapping, struct page *page)
  422. {
  423. if (__remove_mapping(mapping, page)) {
  424. /*
  425. * Unfreezing the refcount with 1 rather than 2 effectively
  426. * drops the pagecache ref for us without requiring another
  427. * atomic operation.
  428. */
  429. page_unfreeze_refs(page, 1);
  430. return 1;
  431. }
  432. return 0;
  433. }
  434. /**
  435. * putback_lru_page - put previously isolated page onto appropriate LRU list
  436. * @page: page to be put back to appropriate lru list
  437. *
  438. * Add previously isolated @page to appropriate LRU list.
  439. * Page may still be unevictable for other reasons.
  440. *
  441. * lru_lock must not be held, interrupts must be enabled.
  442. */
  443. void putback_lru_page(struct page *page)
  444. {
  445. int lru;
  446. int active = !!TestClearPageActive(page);
  447. int was_unevictable = PageUnevictable(page);
  448. VM_BUG_ON(PageLRU(page));
  449. redo:
  450. ClearPageUnevictable(page);
  451. if (page_evictable(page, NULL)) {
  452. /*
  453. * For evictable pages, we can use the cache.
  454. * In event of a race, worst case is we end up with an
  455. * unevictable page on [in]active list.
  456. * We know how to handle that.
  457. */
  458. lru = active + page_lru_base_type(page);
  459. lru_cache_add_lru(page, lru);
  460. } else {
  461. /*
  462. * Put unevictable pages directly on zone's unevictable
  463. * list.
  464. */
  465. lru = LRU_UNEVICTABLE;
  466. add_page_to_unevictable_list(page);
  467. /*
  468. * When racing with an mlock clearing (page is
  469. * unlocked), make sure that if the other thread does
  470. * not observe our setting of PG_lru and fails
  471. * isolation, we see PG_mlocked cleared below and move
  472. * the page back to the evictable list.
  473. *
  474. * The other side is TestClearPageMlocked().
  475. */
  476. smp_mb();
  477. }
  478. /*
  479. * page's status can change while we move it among lru. If an evictable
  480. * page is on unevictable list, it never be freed. To avoid that,
  481. * check after we added it to the list, again.
  482. */
  483. if (lru == LRU_UNEVICTABLE && page_evictable(page, NULL)) {
  484. if (!isolate_lru_page(page)) {
  485. put_page(page);
  486. goto redo;
  487. }
  488. /* This means someone else dropped this page from LRU
  489. * So, it will be freed or putback to LRU again. There is
  490. * nothing to do here.
  491. */
  492. }
  493. if (was_unevictable && lru != LRU_UNEVICTABLE)
  494. count_vm_event(UNEVICTABLE_PGRESCUED);
  495. else if (!was_unevictable && lru == LRU_UNEVICTABLE)
  496. count_vm_event(UNEVICTABLE_PGCULLED);
  497. put_page(page); /* drop ref from isolate */
  498. }
  499. enum page_references {
  500. PAGEREF_RECLAIM,
  501. PAGEREF_RECLAIM_CLEAN,
  502. PAGEREF_KEEP,
  503. PAGEREF_ACTIVATE,
  504. };
  505. static enum page_references page_check_references(struct page *page,
  506. struct scan_control *sc)
  507. {
  508. int referenced_ptes, referenced_page;
  509. unsigned long vm_flags;
  510. referenced_ptes = page_referenced(page, 1, sc->mem_cgroup, &vm_flags);
  511. referenced_page = TestClearPageReferenced(page);
  512. /* Lumpy reclaim - ignore references */
  513. if (sc->lumpy_reclaim_mode)
  514. return PAGEREF_RECLAIM;
  515. /*
  516. * Mlock lost the isolation race with us. Let try_to_unmap()
  517. * move the page to the unevictable list.
  518. */
  519. if (vm_flags & VM_LOCKED)
  520. return PAGEREF_RECLAIM;
  521. if (referenced_ptes) {
  522. if (PageAnon(page))
  523. return PAGEREF_ACTIVATE;
  524. /*
  525. * All mapped pages start out with page table
  526. * references from the instantiating fault, so we need
  527. * to look twice if a mapped file page is used more
  528. * than once.
  529. *
  530. * Mark it and spare it for another trip around the
  531. * inactive list. Another page table reference will
  532. * lead to its activation.
  533. *
  534. * Note: the mark is set for activated pages as well
  535. * so that recently deactivated but used pages are
  536. * quickly recovered.
  537. */
  538. SetPageReferenced(page);
  539. if (referenced_page)
  540. return PAGEREF_ACTIVATE;
  541. return PAGEREF_KEEP;
  542. }
  543. /* Reclaim if clean, defer dirty pages to writeback */
  544. if (referenced_page)
  545. return PAGEREF_RECLAIM_CLEAN;
  546. return PAGEREF_RECLAIM;
  547. }
  548. static noinline_for_stack void free_page_list(struct list_head *free_pages)
  549. {
  550. struct pagevec freed_pvec;
  551. struct page *page, *tmp;
  552. pagevec_init(&freed_pvec, 1);
  553. list_for_each_entry_safe(page, tmp, free_pages, lru) {
  554. list_del(&page->lru);
  555. if (!pagevec_add(&freed_pvec, page)) {
  556. __pagevec_free(&freed_pvec);
  557. pagevec_reinit(&freed_pvec);
  558. }
  559. }
  560. pagevec_free(&freed_pvec);
  561. }
  562. /*
  563. * shrink_page_list() returns the number of reclaimed pages
  564. */
  565. static unsigned long shrink_page_list(struct list_head *page_list,
  566. struct scan_control *sc,
  567. enum pageout_io sync_writeback)
  568. {
  569. LIST_HEAD(ret_pages);
  570. LIST_HEAD(free_pages);
  571. int pgactivate = 0;
  572. unsigned long nr_reclaimed = 0;
  573. cond_resched();
  574. while (!list_empty(page_list)) {
  575. enum page_references references;
  576. struct address_space *mapping;
  577. struct page *page;
  578. int may_enter_fs;
  579. cond_resched();
  580. page = lru_to_page(page_list);
  581. list_del(&page->lru);
  582. if (!trylock_page(page))
  583. goto keep;
  584. VM_BUG_ON(PageActive(page));
  585. sc->nr_scanned++;
  586. if (unlikely(!page_evictable(page, NULL)))
  587. goto cull_mlocked;
  588. if (!sc->may_unmap && page_mapped(page))
  589. goto keep_locked;
  590. /* Double the slab pressure for mapped and swapcache pages */
  591. if (page_mapped(page) || PageSwapCache(page))
  592. sc->nr_scanned++;
  593. may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
  594. (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
  595. if (PageWriteback(page)) {
  596. /*
  597. * Synchronous reclaim is performed in two passes,
  598. * first an asynchronous pass over the list to
  599. * start parallel writeback, and a second synchronous
  600. * pass to wait for the IO to complete. Wait here
  601. * for any page for which writeback has already
  602. * started.
  603. */
  604. if (sync_writeback == PAGEOUT_IO_SYNC && may_enter_fs)
  605. wait_on_page_writeback(page);
  606. else
  607. goto keep_locked;
  608. }
  609. references = page_check_references(page, sc);
  610. switch (references) {
  611. case PAGEREF_ACTIVATE:
  612. goto activate_locked;
  613. case PAGEREF_KEEP:
  614. goto keep_locked;
  615. case PAGEREF_RECLAIM:
  616. case PAGEREF_RECLAIM_CLEAN:
  617. ; /* try to reclaim the page below */
  618. }
  619. /*
  620. * Anonymous process memory has backing store?
  621. * Try to allocate it some swap space here.
  622. */
  623. if (PageAnon(page) && !PageSwapCache(page)) {
  624. if (!(sc->gfp_mask & __GFP_IO))
  625. goto keep_locked;
  626. if (!add_to_swap(page))
  627. goto activate_locked;
  628. may_enter_fs = 1;
  629. }
  630. mapping = page_mapping(page);
  631. /*
  632. * The page is mapped into the page tables of one or more
  633. * processes. Try to unmap it here.
  634. */
  635. if (page_mapped(page) && mapping) {
  636. switch (try_to_unmap(page, TTU_UNMAP)) {
  637. case SWAP_FAIL:
  638. goto activate_locked;
  639. case SWAP_AGAIN:
  640. goto keep_locked;
  641. case SWAP_MLOCK:
  642. goto cull_mlocked;
  643. case SWAP_SUCCESS:
  644. ; /* try to free the page below */
  645. }
  646. }
  647. if (PageDirty(page)) {
  648. if (references == PAGEREF_RECLAIM_CLEAN)
  649. goto keep_locked;
  650. if (!may_enter_fs)
  651. goto keep_locked;
  652. if (!sc->may_writepage)
  653. goto keep_locked;
  654. /* Page is dirty, try to write it out here */
  655. switch (pageout(page, mapping, sync_writeback)) {
  656. case PAGE_KEEP:
  657. goto keep_locked;
  658. case PAGE_ACTIVATE:
  659. goto activate_locked;
  660. case PAGE_SUCCESS:
  661. if (PageWriteback(page) || PageDirty(page))
  662. goto keep;
  663. /*
  664. * A synchronous write - probably a ramdisk. Go
  665. * ahead and try to reclaim the page.
  666. */
  667. if (!trylock_page(page))
  668. goto keep;
  669. if (PageDirty(page) || PageWriteback(page))
  670. goto keep_locked;
  671. mapping = page_mapping(page);
  672. case PAGE_CLEAN:
  673. ; /* try to free the page below */
  674. }
  675. }
  676. /*
  677. * If the page has buffers, try to free the buffer mappings
  678. * associated with this page. If we succeed we try to free
  679. * the page as well.
  680. *
  681. * We do this even if the page is PageDirty().
  682. * try_to_release_page() does not perform I/O, but it is
  683. * possible for a page to have PageDirty set, but it is actually
  684. * clean (all its buffers are clean). This happens if the
  685. * buffers were written out directly, with submit_bh(). ext3
  686. * will do this, as well as the blockdev mapping.
  687. * try_to_release_page() will discover that cleanness and will
  688. * drop the buffers and mark the page clean - it can be freed.
  689. *
  690. * Rarely, pages can have buffers and no ->mapping. These are
  691. * the pages which were not successfully invalidated in
  692. * truncate_complete_page(). We try to drop those buffers here
  693. * and if that worked, and the page is no longer mapped into
  694. * process address space (page_count == 1) it can be freed.
  695. * Otherwise, leave the page on the LRU so it is swappable.
  696. */
  697. if (page_has_private(page)) {
  698. if (!try_to_release_page(page, sc->gfp_mask))
  699. goto activate_locked;
  700. if (!mapping && page_count(page) == 1) {
  701. unlock_page(page);
  702. if (put_page_testzero(page))
  703. goto free_it;
  704. else {
  705. /*
  706. * rare race with speculative reference.
  707. * the speculative reference will free
  708. * this page shortly, so we may
  709. * increment nr_reclaimed here (and
  710. * leave it off the LRU).
  711. */
  712. nr_reclaimed++;
  713. continue;
  714. }
  715. }
  716. }
  717. if (!mapping || !__remove_mapping(mapping, page))
  718. goto keep_locked;
  719. /*
  720. * At this point, we have no other references and there is
  721. * no way to pick any more up (removed from LRU, removed
  722. * from pagecache). Can use non-atomic bitops now (and
  723. * we obviously don't have to worry about waking up a process
  724. * waiting on the page lock, because there are no references.
  725. */
  726. __clear_page_locked(page);
  727. free_it:
  728. nr_reclaimed++;
  729. /*
  730. * Is there need to periodically free_page_list? It would
  731. * appear not as the counts should be low
  732. */
  733. list_add(&page->lru, &free_pages);
  734. continue;
  735. cull_mlocked:
  736. if (PageSwapCache(page))
  737. try_to_free_swap(page);
  738. unlock_page(page);
  739. putback_lru_page(page);
  740. continue;
  741. activate_locked:
  742. /* Not a candidate for swapping, so reclaim swap space. */
  743. if (PageSwapCache(page) && vm_swap_full())
  744. try_to_free_swap(page);
  745. VM_BUG_ON(PageActive(page));
  746. SetPageActive(page);
  747. pgactivate++;
  748. keep_locked:
  749. unlock_page(page);
  750. keep:
  751. list_add(&page->lru, &ret_pages);
  752. VM_BUG_ON(PageLRU(page) || PageUnevictable(page));
  753. }
  754. free_page_list(&free_pages);
  755. list_splice(&ret_pages, page_list);
  756. count_vm_events(PGACTIVATE, pgactivate);
  757. return nr_reclaimed;
  758. }
  759. /*
  760. * Attempt to remove the specified page from its LRU. Only take this page
  761. * if it is of the appropriate PageActive status. Pages which are being
  762. * freed elsewhere are also ignored.
  763. *
  764. * page: page to consider
  765. * mode: one of the LRU isolation modes defined above
  766. *
  767. * returns 0 on success, -ve errno on failure.
  768. */
  769. int __isolate_lru_page(struct page *page, int mode, int file)
  770. {
  771. int ret = -EINVAL;
  772. /* Only take pages on the LRU. */
  773. if (!PageLRU(page))
  774. return ret;
  775. /*
  776. * When checking the active state, we need to be sure we are
  777. * dealing with comparible boolean values. Take the logical not
  778. * of each.
  779. */
  780. if (mode != ISOLATE_BOTH && (!PageActive(page) != !mode))
  781. return ret;
  782. if (mode != ISOLATE_BOTH && page_is_file_cache(page) != file)
  783. return ret;
  784. /*
  785. * When this function is being called for lumpy reclaim, we
  786. * initially look into all LRU pages, active, inactive and
  787. * unevictable; only give shrink_page_list evictable pages.
  788. */
  789. if (PageUnevictable(page))
  790. return ret;
  791. ret = -EBUSY;
  792. if (likely(get_page_unless_zero(page))) {
  793. /*
  794. * Be careful not to clear PageLRU until after we're
  795. * sure the page is not being freed elsewhere -- the
  796. * page release code relies on it.
  797. */
  798. ClearPageLRU(page);
  799. ret = 0;
  800. }
  801. return ret;
  802. }
  803. /*
  804. * zone->lru_lock is heavily contended. Some of the functions that
  805. * shrink the lists perform better by taking out a batch of pages
  806. * and working on them outside the LRU lock.
  807. *
  808. * For pagecache intensive workloads, this function is the hottest
  809. * spot in the kernel (apart from copy_*_user functions).
  810. *
  811. * Appropriate locks must be held before calling this function.
  812. *
  813. * @nr_to_scan: The number of pages to look through on the list.
  814. * @src: The LRU list to pull pages off.
  815. * @dst: The temp list to put pages on to.
  816. * @scanned: The number of pages that were scanned.
  817. * @order: The caller's attempted allocation order
  818. * @mode: One of the LRU isolation modes
  819. * @file: True [1] if isolating file [!anon] pages
  820. *
  821. * returns how many pages were moved onto *@dst.
  822. */
  823. static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
  824. struct list_head *src, struct list_head *dst,
  825. unsigned long *scanned, int order, int mode, int file)
  826. {
  827. unsigned long nr_taken = 0;
  828. unsigned long nr_lumpy_taken = 0;
  829. unsigned long nr_lumpy_dirty = 0;
  830. unsigned long nr_lumpy_failed = 0;
  831. unsigned long scan;
  832. for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
  833. struct page *page;
  834. unsigned long pfn;
  835. unsigned long end_pfn;
  836. unsigned long page_pfn;
  837. int zone_id;
  838. page = lru_to_page(src);
  839. prefetchw_prev_lru_page(page, src, flags);
  840. VM_BUG_ON(!PageLRU(page));
  841. switch (__isolate_lru_page(page, mode, file)) {
  842. case 0:
  843. list_move(&page->lru, dst);
  844. mem_cgroup_del_lru(page);
  845. nr_taken++;
  846. break;
  847. case -EBUSY:
  848. /* else it is being freed elsewhere */
  849. list_move(&page->lru, src);
  850. mem_cgroup_rotate_lru_list(page, page_lru(page));
  851. continue;
  852. default:
  853. BUG();
  854. }
  855. if (!order)
  856. continue;
  857. /*
  858. * Attempt to take all pages in the order aligned region
  859. * surrounding the tag page. Only take those pages of
  860. * the same active state as that tag page. We may safely
  861. * round the target page pfn down to the requested order
  862. * as the mem_map is guarenteed valid out to MAX_ORDER,
  863. * where that page is in a different zone we will detect
  864. * it from its zone id and abort this block scan.
  865. */
  866. zone_id = page_zone_id(page);
  867. page_pfn = page_to_pfn(page);
  868. pfn = page_pfn & ~((1 << order) - 1);
  869. end_pfn = pfn + (1 << order);
  870. for (; pfn < end_pfn; pfn++) {
  871. struct page *cursor_page;
  872. /* The target page is in the block, ignore it. */
  873. if (unlikely(pfn == page_pfn))
  874. continue;
  875. /* Avoid holes within the zone. */
  876. if (unlikely(!pfn_valid_within(pfn)))
  877. break;
  878. cursor_page = pfn_to_page(pfn);
  879. /* Check that we have not crossed a zone boundary. */
  880. if (unlikely(page_zone_id(cursor_page) != zone_id))
  881. continue;
  882. /*
  883. * If we don't have enough swap space, reclaiming of
  884. * anon page which don't already have a swap slot is
  885. * pointless.
  886. */
  887. if (nr_swap_pages <= 0 && PageAnon(cursor_page) &&
  888. !PageSwapCache(cursor_page))
  889. continue;
  890. if (__isolate_lru_page(cursor_page, mode, file) == 0) {
  891. list_move(&cursor_page->lru, dst);
  892. mem_cgroup_del_lru(cursor_page);
  893. nr_taken++;
  894. nr_lumpy_taken++;
  895. if (PageDirty(cursor_page))
  896. nr_lumpy_dirty++;
  897. scan++;
  898. } else {
  899. if (mode == ISOLATE_BOTH &&
  900. page_count(cursor_page))
  901. nr_lumpy_failed++;
  902. }
  903. }
  904. }
  905. *scanned = scan;
  906. trace_mm_vmscan_lru_isolate(order,
  907. nr_to_scan, scan,
  908. nr_taken,
  909. nr_lumpy_taken, nr_lumpy_dirty, nr_lumpy_failed,
  910. mode);
  911. return nr_taken;
  912. }
  913. static unsigned long isolate_pages_global(unsigned long nr,
  914. struct list_head *dst,
  915. unsigned long *scanned, int order,
  916. int mode, struct zone *z,
  917. int active, int file)
  918. {
  919. int lru = LRU_BASE;
  920. if (active)
  921. lru += LRU_ACTIVE;
  922. if (file)
  923. lru += LRU_FILE;
  924. return isolate_lru_pages(nr, &z->lru[lru].list, dst, scanned, order,
  925. mode, file);
  926. }
  927. /*
  928. * clear_active_flags() is a helper for shrink_active_list(), clearing
  929. * any active bits from the pages in the list.
  930. */
  931. static unsigned long clear_active_flags(struct list_head *page_list,
  932. unsigned int *count)
  933. {
  934. int nr_active = 0;
  935. int lru;
  936. struct page *page;
  937. list_for_each_entry(page, page_list, lru) {
  938. lru = page_lru_base_type(page);
  939. if (PageActive(page)) {
  940. lru += LRU_ACTIVE;
  941. ClearPageActive(page);
  942. nr_active++;
  943. }
  944. if (count)
  945. count[lru]++;
  946. }
  947. return nr_active;
  948. }
  949. /**
  950. * isolate_lru_page - tries to isolate a page from its LRU list
  951. * @page: page to isolate from its LRU list
  952. *
  953. * Isolates a @page from an LRU list, clears PageLRU and adjusts the
  954. * vmstat statistic corresponding to whatever LRU list the page was on.
  955. *
  956. * Returns 0 if the page was removed from an LRU list.
  957. * Returns -EBUSY if the page was not on an LRU list.
  958. *
  959. * The returned page will have PageLRU() cleared. If it was found on
  960. * the active list, it will have PageActive set. If it was found on
  961. * the unevictable list, it will have the PageUnevictable bit set. That flag
  962. * may need to be cleared by the caller before letting the page go.
  963. *
  964. * The vmstat statistic corresponding to the list on which the page was
  965. * found will be decremented.
  966. *
  967. * Restrictions:
  968. * (1) Must be called with an elevated refcount on the page. This is a
  969. * fundamentnal difference from isolate_lru_pages (which is called
  970. * without a stable reference).
  971. * (2) the lru_lock must not be held.
  972. * (3) interrupts must be enabled.
  973. */
  974. int isolate_lru_page(struct page *page)
  975. {
  976. int ret = -EBUSY;
  977. if (PageLRU(page)) {
  978. struct zone *zone = page_zone(page);
  979. spin_lock_irq(&zone->lru_lock);
  980. if (PageLRU(page) && get_page_unless_zero(page)) {
  981. int lru = page_lru(page);
  982. ret = 0;
  983. ClearPageLRU(page);
  984. del_page_from_lru_list(zone, page, lru);
  985. }
  986. spin_unlock_irq(&zone->lru_lock);
  987. }
  988. return ret;
  989. }
  990. /*
  991. * Are there way too many processes in the direct reclaim path already?
  992. */
  993. static int too_many_isolated(struct zone *zone, int file,
  994. struct scan_control *sc)
  995. {
  996. unsigned long inactive, isolated;
  997. if (current_is_kswapd())
  998. return 0;
  999. if (!scanning_global_lru(sc))
  1000. return 0;
  1001. if (file) {
  1002. inactive = zone_page_state(zone, NR_INACTIVE_FILE);
  1003. isolated = zone_page_state(zone, NR_ISOLATED_FILE);
  1004. } else {
  1005. inactive = zone_page_state(zone, NR_INACTIVE_ANON);
  1006. isolated = zone_page_state(zone, NR_ISOLATED_ANON);
  1007. }
  1008. return isolated > inactive;
  1009. }
  1010. /*
  1011. * TODO: Try merging with migrations version of putback_lru_pages
  1012. */
  1013. static noinline_for_stack void
  1014. putback_lru_pages(struct zone *zone, struct scan_control *sc,
  1015. unsigned long nr_anon, unsigned long nr_file,
  1016. struct list_head *page_list)
  1017. {
  1018. struct page *page;
  1019. struct pagevec pvec;
  1020. struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
  1021. pagevec_init(&pvec, 1);
  1022. /*
  1023. * Put back any unfreeable pages.
  1024. */
  1025. spin_lock(&zone->lru_lock);
  1026. while (!list_empty(page_list)) {
  1027. int lru;
  1028. page = lru_to_page(page_list);
  1029. VM_BUG_ON(PageLRU(page));
  1030. list_del(&page->lru);
  1031. if (unlikely(!page_evictable(page, NULL))) {
  1032. spin_unlock_irq(&zone->lru_lock);
  1033. putback_lru_page(page);
  1034. spin_lock_irq(&zone->lru_lock);
  1035. continue;
  1036. }
  1037. SetPageLRU(page);
  1038. lru = page_lru(page);
  1039. add_page_to_lru_list(zone, page, lru);
  1040. if (is_active_lru(lru)) {
  1041. int file = is_file_lru(lru);
  1042. reclaim_stat->recent_rotated[file]++;
  1043. }
  1044. if (!pagevec_add(&pvec, page)) {
  1045. spin_unlock_irq(&zone->lru_lock);
  1046. __pagevec_release(&pvec);
  1047. spin_lock_irq(&zone->lru_lock);
  1048. }
  1049. }
  1050. __mod_zone_page_state(zone, NR_ISOLATED_ANON, -nr_anon);
  1051. __mod_zone_page_state(zone, NR_ISOLATED_FILE, -nr_file);
  1052. spin_unlock_irq(&zone->lru_lock);
  1053. pagevec_release(&pvec);
  1054. }
  1055. static noinline_for_stack void update_isolated_counts(struct zone *zone,
  1056. struct scan_control *sc,
  1057. unsigned long *nr_anon,
  1058. unsigned long *nr_file,
  1059. struct list_head *isolated_list)
  1060. {
  1061. unsigned long nr_active;
  1062. unsigned int count[NR_LRU_LISTS] = { 0, };
  1063. struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
  1064. nr_active = clear_active_flags(isolated_list, count);
  1065. __count_vm_events(PGDEACTIVATE, nr_active);
  1066. __mod_zone_page_state(zone, NR_ACTIVE_FILE,
  1067. -count[LRU_ACTIVE_FILE]);
  1068. __mod_zone_page_state(zone, NR_INACTIVE_FILE,
  1069. -count[LRU_INACTIVE_FILE]);
  1070. __mod_zone_page_state(zone, NR_ACTIVE_ANON,
  1071. -count[LRU_ACTIVE_ANON]);
  1072. __mod_zone_page_state(zone, NR_INACTIVE_ANON,
  1073. -count[LRU_INACTIVE_ANON]);
  1074. *nr_anon = count[LRU_ACTIVE_ANON] + count[LRU_INACTIVE_ANON];
  1075. *nr_file = count[LRU_ACTIVE_FILE] + count[LRU_INACTIVE_FILE];
  1076. __mod_zone_page_state(zone, NR_ISOLATED_ANON, *nr_anon);
  1077. __mod_zone_page_state(zone, NR_ISOLATED_FILE, *nr_file);
  1078. reclaim_stat->recent_scanned[0] += *nr_anon;
  1079. reclaim_stat->recent_scanned[1] += *nr_file;
  1080. }
  1081. /*
  1082. * Returns true if the caller should wait to clean dirty/writeback pages.
  1083. *
  1084. * If we are direct reclaiming for contiguous pages and we do not reclaim
  1085. * everything in the list, try again and wait for writeback IO to complete.
  1086. * This will stall high-order allocations noticeably. Only do that when really
  1087. * need to free the pages under high memory pressure.
  1088. */
  1089. static inline bool should_reclaim_stall(unsigned long nr_taken,
  1090. unsigned long nr_freed,
  1091. int priority,
  1092. struct scan_control *sc)
  1093. {
  1094. int lumpy_stall_priority;
  1095. /* kswapd should not stall on sync IO */
  1096. if (current_is_kswapd())
  1097. return false;
  1098. /* Only stall on lumpy reclaim */
  1099. if (!sc->lumpy_reclaim_mode)
  1100. return false;
  1101. /* If we have relaimed everything on the isolated list, no stall */
  1102. if (nr_freed == nr_taken)
  1103. return false;
  1104. /*
  1105. * For high-order allocations, there are two stall thresholds.
  1106. * High-cost allocations stall immediately where as lower
  1107. * order allocations such as stacks require the scanning
  1108. * priority to be much higher before stalling.
  1109. */
  1110. if (sc->order > PAGE_ALLOC_COSTLY_ORDER)
  1111. lumpy_stall_priority = DEF_PRIORITY;
  1112. else
  1113. lumpy_stall_priority = DEF_PRIORITY / 3;
  1114. return priority <= lumpy_stall_priority;
  1115. }
  1116. /*
  1117. * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
  1118. * of reclaimed pages
  1119. */
  1120. static noinline_for_stack unsigned long
  1121. shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,
  1122. struct scan_control *sc, int priority, int file)
  1123. {
  1124. LIST_HEAD(page_list);
  1125. unsigned long nr_scanned;
  1126. unsigned long nr_reclaimed = 0;
  1127. unsigned long nr_taken;
  1128. unsigned long nr_active;
  1129. unsigned long nr_anon;
  1130. unsigned long nr_file;
  1131. while (unlikely(too_many_isolated(zone, file, sc))) {
  1132. congestion_wait(BLK_RW_ASYNC, HZ/10);
  1133. /* We are about to die and free our memory. Return now. */
  1134. if (fatal_signal_pending(current))
  1135. return SWAP_CLUSTER_MAX;
  1136. }
  1137. lru_add_drain();
  1138. spin_lock_irq(&zone->lru_lock);
  1139. if (scanning_global_lru(sc)) {
  1140. nr_taken = isolate_pages_global(nr_to_scan,
  1141. &page_list, &nr_scanned, sc->order,
  1142. sc->lumpy_reclaim_mode ?
  1143. ISOLATE_BOTH : ISOLATE_INACTIVE,
  1144. zone, 0, file);
  1145. zone->pages_scanned += nr_scanned;
  1146. if (current_is_kswapd())
  1147. __count_zone_vm_events(PGSCAN_KSWAPD, zone,
  1148. nr_scanned);
  1149. else
  1150. __count_zone_vm_events(PGSCAN_DIRECT, zone,
  1151. nr_scanned);
  1152. } else {
  1153. nr_taken = mem_cgroup_isolate_pages(nr_to_scan,
  1154. &page_list, &nr_scanned, sc->order,
  1155. sc->lumpy_reclaim_mode ?
  1156. ISOLATE_BOTH : ISOLATE_INACTIVE,
  1157. zone, sc->mem_cgroup,
  1158. 0, file);
  1159. /*
  1160. * mem_cgroup_isolate_pages() keeps track of
  1161. * scanned pages on its own.
  1162. */
  1163. }
  1164. if (nr_taken == 0) {
  1165. spin_unlock_irq(&zone->lru_lock);
  1166. return 0;
  1167. }
  1168. update_isolated_counts(zone, sc, &nr_anon, &nr_file, &page_list);
  1169. spin_unlock_irq(&zone->lru_lock);
  1170. nr_reclaimed = shrink_page_list(&page_list, sc, PAGEOUT_IO_ASYNC);
  1171. /* Check if we should syncronously wait for writeback */
  1172. if (should_reclaim_stall(nr_taken, nr_reclaimed, priority, sc)) {
  1173. congestion_wait(BLK_RW_ASYNC, HZ/10);
  1174. /*
  1175. * The attempt at page out may have made some
  1176. * of the pages active, mark them inactive again.
  1177. */
  1178. nr_active = clear_active_flags(&page_list, NULL);
  1179. count_vm_events(PGDEACTIVATE, nr_active);
  1180. nr_reclaimed += shrink_page_list(&page_list, sc, PAGEOUT_IO_SYNC);
  1181. }
  1182. local_irq_disable();
  1183. if (current_is_kswapd())
  1184. __count_vm_events(KSWAPD_STEAL, nr_reclaimed);
  1185. __count_zone_vm_events(PGSTEAL, zone, nr_reclaimed);
  1186. putback_lru_pages(zone, sc, nr_anon, nr_file, &page_list);
  1187. return nr_reclaimed;
  1188. }
  1189. /*
  1190. * This moves pages from the active list to the inactive list.
  1191. *
  1192. * We move them the other way if the page is referenced by one or more
  1193. * processes, from rmap.
  1194. *
  1195. * If the pages are mostly unmapped, the processing is fast and it is
  1196. * appropriate to hold zone->lru_lock across the whole operation. But if
  1197. * the pages are mapped, the processing is slow (page_referenced()) so we
  1198. * should drop zone->lru_lock around each page. It's impossible to balance
  1199. * this, so instead we remove the pages from the LRU while processing them.
  1200. * It is safe to rely on PG_active against the non-LRU pages in here because
  1201. * nobody will play with that bit on a non-LRU page.
  1202. *
  1203. * The downside is that we have to touch page->_count against each page.
  1204. * But we had to alter page->flags anyway.
  1205. */
  1206. static void move_active_pages_to_lru(struct zone *zone,
  1207. struct list_head *list,
  1208. enum lru_list lru)
  1209. {
  1210. unsigned long pgmoved = 0;
  1211. struct pagevec pvec;
  1212. struct page *page;
  1213. pagevec_init(&pvec, 1);
  1214. while (!list_empty(list)) {
  1215. page = lru_to_page(list);
  1216. VM_BUG_ON(PageLRU(page));
  1217. SetPageLRU(page);
  1218. list_move(&page->lru, &zone->lru[lru].list);
  1219. mem_cgroup_add_lru_list(page, lru);
  1220. pgmoved++;
  1221. if (!pagevec_add(&pvec, page) || list_empty(list)) {
  1222. spin_unlock_irq(&zone->lru_lock);
  1223. if (buffer_heads_over_limit)
  1224. pagevec_strip(&pvec);
  1225. __pagevec_release(&pvec);
  1226. spin_lock_irq(&zone->lru_lock);
  1227. }
  1228. }
  1229. __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
  1230. if (!is_active_lru(lru))
  1231. __count_vm_events(PGDEACTIVATE, pgmoved);
  1232. }
  1233. static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
  1234. struct scan_control *sc, int priority, int file)
  1235. {
  1236. unsigned long nr_taken;
  1237. unsigned long pgscanned;
  1238. unsigned long vm_flags;
  1239. LIST_HEAD(l_hold); /* The pages which were snipped off */
  1240. LIST_HEAD(l_active);
  1241. LIST_HEAD(l_inactive);
  1242. struct page *page;
  1243. struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
  1244. unsigned long nr_rotated = 0;
  1245. lru_add_drain();
  1246. spin_lock_irq(&zone->lru_lock);
  1247. if (scanning_global_lru(sc)) {
  1248. nr_taken = isolate_pages_global(nr_pages, &l_hold,
  1249. &pgscanned, sc->order,
  1250. ISOLATE_ACTIVE, zone,
  1251. 1, file);
  1252. zone->pages_scanned += pgscanned;
  1253. } else {
  1254. nr_taken = mem_cgroup_isolate_pages(nr_pages, &l_hold,
  1255. &pgscanned, sc->order,
  1256. ISOLATE_ACTIVE, zone,
  1257. sc->mem_cgroup, 1, file);
  1258. /*
  1259. * mem_cgroup_isolate_pages() keeps track of
  1260. * scanned pages on its own.
  1261. */
  1262. }
  1263. reclaim_stat->recent_scanned[file] += nr_taken;
  1264. __count_zone_vm_events(PGREFILL, zone, pgscanned);
  1265. if (file)
  1266. __mod_zone_page_state(zone, NR_ACTIVE_FILE, -nr_taken);
  1267. else
  1268. __mod_zone_page_state(zone, NR_ACTIVE_ANON, -nr_taken);
  1269. __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
  1270. spin_unlock_irq(&zone->lru_lock);
  1271. while (!list_empty(&l_hold)) {
  1272. cond_resched();
  1273. page = lru_to_page(&l_hold);
  1274. list_del(&page->lru);
  1275. if (unlikely(!page_evictable(page, NULL))) {
  1276. putback_lru_page(page);
  1277. continue;
  1278. }
  1279. if (page_referenced(page, 0, sc->mem_cgroup, &vm_flags)) {
  1280. nr_rotated++;
  1281. /*
  1282. * Identify referenced, file-backed active pages and
  1283. * give them one more trip around the active list. So
  1284. * that executable code get better chances to stay in
  1285. * memory under moderate memory pressure. Anon pages
  1286. * are not likely to be evicted by use-once streaming
  1287. * IO, plus JVM can create lots of anon VM_EXEC pages,
  1288. * so we ignore them here.
  1289. */
  1290. if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
  1291. list_add(&page->lru, &l_active);
  1292. continue;
  1293. }
  1294. }
  1295. ClearPageActive(page); /* we are de-activating */
  1296. list_add(&page->lru, &l_inactive);
  1297. }
  1298. /*
  1299. * Move pages back to the lru list.
  1300. */
  1301. spin_lock_irq(&zone->lru_lock);
  1302. /*
  1303. * Count referenced pages from currently used mappings as rotated,
  1304. * even though only some of them are actually re-activated. This
  1305. * helps balance scan pressure between file and anonymous pages in
  1306. * get_scan_ratio.
  1307. */
  1308. reclaim_stat->recent_rotated[file] += nr_rotated;
  1309. move_active_pages_to_lru(zone, &l_active,
  1310. LRU_ACTIVE + file * LRU_FILE);
  1311. move_active_pages_to_lru(zone, &l_inactive,
  1312. LRU_BASE + file * LRU_FILE);
  1313. __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
  1314. spin_unlock_irq(&zone->lru_lock);
  1315. }
  1316. static int inactive_anon_is_low_global(struct zone *zone)
  1317. {
  1318. unsigned long active, inactive;
  1319. active = zone_page_state(zone, NR_ACTIVE_ANON);
  1320. inactive = zone_page_state(zone, NR_INACTIVE_ANON);
  1321. if (inactive * zone->inactive_ratio < active)
  1322. return 1;
  1323. return 0;
  1324. }
  1325. /**
  1326. * inactive_anon_is_low - check if anonymous pages need to be deactivated
  1327. * @zone: zone to check
  1328. * @sc: scan control of this context
  1329. *
  1330. * Returns true if the zone does not have enough inactive anon pages,
  1331. * meaning some active anon pages need to be deactivated.
  1332. */
  1333. static int inactive_anon_is_low(struct zone *zone, struct scan_control *sc)
  1334. {
  1335. int low;
  1336. if (scanning_global_lru(sc))
  1337. low = inactive_anon_is_low_global(zone);
  1338. else
  1339. low = mem_cgroup_inactive_anon_is_low(sc->mem_cgroup);
  1340. return low;
  1341. }
  1342. static int inactive_file_is_low_global(struct zone *zone)
  1343. {
  1344. unsigned long active, inactive;
  1345. active = zone_page_state(zone, NR_ACTIVE_FILE);
  1346. inactive = zone_page_state(zone, NR_INACTIVE_FILE);
  1347. return (active > inactive);
  1348. }
  1349. /**
  1350. * inactive_file_is_low - check if file pages need to be deactivated
  1351. * @zone: zone to check
  1352. * @sc: scan control of this context
  1353. *
  1354. * When the system is doing streaming IO, memory pressure here
  1355. * ensures that active file pages get deactivated, until more
  1356. * than half of the file pages are on the inactive list.
  1357. *
  1358. * Once we get to that situation, protect the system's working
  1359. * set from being evicted by disabling active file page aging.
  1360. *
  1361. * This uses a different ratio than the anonymous pages, because
  1362. * the page cache uses a use-once replacement algorithm.
  1363. */
  1364. static int inactive_file_is_low(struct zone *zone, struct scan_control *sc)
  1365. {
  1366. int low;
  1367. if (scanning_global_lru(sc))
  1368. low = inactive_file_is_low_global(zone);
  1369. else
  1370. low = mem_cgroup_inactive_file_is_low(sc->mem_cgroup);
  1371. return low;
  1372. }
  1373. static int inactive_list_is_low(struct zone *zone, struct scan_control *sc,
  1374. int file)
  1375. {
  1376. if (file)
  1377. return inactive_file_is_low(zone, sc);
  1378. else
  1379. return inactive_anon_is_low(zone, sc);
  1380. }
  1381. static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
  1382. struct zone *zone, struct scan_control *sc, int priority)
  1383. {
  1384. int file = is_file_lru(lru);
  1385. if (is_active_lru(lru)) {
  1386. if (inactive_list_is_low(zone, sc, file))
  1387. shrink_active_list(nr_to_scan, zone, sc, priority, file);
  1388. return 0;
  1389. }
  1390. return shrink_inactive_list(nr_to_scan, zone, sc, priority, file);
  1391. }
  1392. /*
  1393. * Smallish @nr_to_scan's are deposited in @nr_saved_scan,
  1394. * until we collected @swap_cluster_max pages to scan.
  1395. */
  1396. static unsigned long nr_scan_try_batch(unsigned long nr_to_scan,
  1397. unsigned long *nr_saved_scan)
  1398. {
  1399. unsigned long nr;
  1400. *nr_saved_scan += nr_to_scan;
  1401. nr = *nr_saved_scan;
  1402. if (nr >= SWAP_CLUSTER_MAX)
  1403. *nr_saved_scan = 0;
  1404. else
  1405. nr = 0;
  1406. return nr;
  1407. }
  1408. /*
  1409. * Determine how aggressively the anon and file LRU lists should be
  1410. * scanned. The relative value of each set of LRU lists is determined
  1411. * by looking at the fraction of the pages scanned we did rotate back
  1412. * onto the active list instead of evict.
  1413. *
  1414. * nr[0] = anon pages to scan; nr[1] = file pages to scan
  1415. */
  1416. static void get_scan_count(struct zone *zone, struct scan_control *sc,
  1417. unsigned long *nr, int priority)
  1418. {
  1419. unsigned long anon, file, free;
  1420. unsigned long anon_prio, file_prio;
  1421. unsigned long ap, fp;
  1422. struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
  1423. u64 fraction[2], denominator;
  1424. enum lru_list l;
  1425. int noswap = 0;
  1426. /* If we have no swap space, do not bother scanning anon pages. */
  1427. if (!sc->may_swap || (nr_swap_pages <= 0)) {
  1428. noswap = 1;
  1429. fraction[0] = 0;
  1430. fraction[1] = 1;
  1431. denominator = 1;
  1432. goto out;
  1433. }
  1434. anon = zone_nr_lru_pages(zone, sc, LRU_ACTIVE_ANON) +
  1435. zone_nr_lru_pages(zone, sc, LRU_INACTIVE_ANON);
  1436. file = zone_nr_lru_pages(zone, sc, LRU_ACTIVE_FILE) +
  1437. zone_nr_lru_pages(zone, sc, LRU_INACTIVE_FILE);
  1438. if (scanning_global_lru(sc)) {
  1439. free = zone_page_state(zone, NR_FREE_PAGES);
  1440. /* If we have very few page cache pages,
  1441. force-scan anon pages. */
  1442. if (unlikely(file + free <= high_wmark_pages(zone))) {
  1443. fraction[0] = 1;
  1444. fraction[1] = 0;
  1445. denominator = 1;
  1446. goto out;
  1447. }
  1448. }
  1449. /*
  1450. * With swappiness at 100, anonymous and file have the same priority.
  1451. * This scanning priority is essentially the inverse of IO cost.
  1452. */
  1453. anon_prio = sc->swappiness;
  1454. file_prio = 200 - sc->swappiness;
  1455. /*
  1456. * OK, so we have swap space and a fair amount of page cache
  1457. * pages. We use the recently rotated / recently scanned
  1458. * ratios to determine how valuable each cache is.
  1459. *
  1460. * Because workloads change over time (and to avoid overflow)
  1461. * we keep these statistics as a floating average, which ends
  1462. * up weighing recent references more than old ones.
  1463. *
  1464. * anon in [0], file in [1]
  1465. */
  1466. spin_lock_irq(&zone->lru_lock);
  1467. if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
  1468. reclaim_stat->recent_scanned[0] /= 2;
  1469. reclaim_stat->recent_rotated[0] /= 2;
  1470. }
  1471. if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
  1472. reclaim_stat->recent_scanned[1] /= 2;
  1473. reclaim_stat->recent_rotated[1] /= 2;
  1474. }
  1475. /*
  1476. * The amount of pressure on anon vs file pages is inversely
  1477. * proportional to the fraction of recently scanned pages on
  1478. * each list that were recently referenced and in active use.
  1479. */
  1480. ap = (anon_prio + 1) * (reclaim_stat->recent_scanned[0] + 1);
  1481. ap /= reclaim_stat->recent_rotated[0] + 1;
  1482. fp = (file_prio + 1) * (reclaim_stat->recent_scanned[1] + 1);
  1483. fp /= reclaim_stat->recent_rotated[1] + 1;
  1484. spin_unlock_irq(&zone->lru_lock);
  1485. fraction[0] = ap;
  1486. fraction[1] = fp;
  1487. denominator = ap + fp + 1;
  1488. out:
  1489. for_each_evictable_lru(l) {
  1490. int file = is_file_lru(l);
  1491. unsigned long scan;
  1492. scan = zone_nr_lru_pages(zone, sc, l);
  1493. if (priority || noswap) {
  1494. scan >>= priority;
  1495. scan = div64_u64(scan * fraction[file], denominator);
  1496. }
  1497. nr[l] = nr_scan_try_batch(scan,
  1498. &reclaim_stat->nr_saved_scan[l]);
  1499. }
  1500. }
  1501. static void set_lumpy_reclaim_mode(int priority, struct scan_control *sc)
  1502. {
  1503. /*
  1504. * If we need a large contiguous chunk of memory, or have
  1505. * trouble getting a small set of contiguous pages, we
  1506. * will reclaim both active and inactive pages.
  1507. */
  1508. if (sc->order > PAGE_ALLOC_COSTLY_ORDER)
  1509. sc->lumpy_reclaim_mode = 1;
  1510. else if (sc->order && priority < DEF_PRIORITY - 2)
  1511. sc->lumpy_reclaim_mode = 1;
  1512. else
  1513. sc->lumpy_reclaim_mode = 0;
  1514. }
  1515. /*
  1516. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  1517. */
  1518. static void shrink_zone(int priority, struct zone *zone,
  1519. struct scan_control *sc)
  1520. {
  1521. unsigned long nr[NR_LRU_LISTS];
  1522. unsigned long nr_to_scan;
  1523. enum lru_list l;
  1524. unsigned long nr_reclaimed = sc->nr_reclaimed;
  1525. unsigned long nr_to_reclaim = sc->nr_to_reclaim;
  1526. get_scan_count(zone, sc, nr, priority);
  1527. set_lumpy_reclaim_mode(priority, sc);
  1528. while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
  1529. nr[LRU_INACTIVE_FILE]) {
  1530. for_each_evictable_lru(l) {
  1531. if (nr[l]) {
  1532. nr_to_scan = min_t(unsigned long,
  1533. nr[l], SWAP_CLUSTER_MAX);
  1534. nr[l] -= nr_to_scan;
  1535. nr_reclaimed += shrink_list(l, nr_to_scan,
  1536. zone, sc, priority);
  1537. }
  1538. }
  1539. /*
  1540. * On large memory systems, scan >> priority can become
  1541. * really large. This is fine for the starting priority;
  1542. * we want to put equal scanning pressure on each zone.
  1543. * However, if the VM has a harder time of freeing pages,
  1544. * with multiple processes reclaiming pages, the total
  1545. * freeing target can get unreasonably large.
  1546. */
  1547. if (nr_reclaimed >= nr_to_reclaim && priority < DEF_PRIORITY)
  1548. break;
  1549. }
  1550. sc->nr_reclaimed = nr_reclaimed;
  1551. /*
  1552. * Even if we did not try to evict anon pages at all, we want to
  1553. * rebalance the anon lru active/inactive ratio.
  1554. */
  1555. if (inactive_anon_is_low(zone, sc) && nr_swap_pages > 0)
  1556. shrink_active_list(SWAP_CLUSTER_MAX, zone, sc, priority, 0);
  1557. throttle_vm_writeout(sc->gfp_mask);
  1558. }
  1559. /*
  1560. * This is the direct reclaim path, for page-allocating processes. We only
  1561. * try to reclaim pages from zones which will satisfy the caller's allocation
  1562. * request.
  1563. *
  1564. * We reclaim from a zone even if that zone is over high_wmark_pages(zone).
  1565. * Because:
  1566. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  1567. * allocation or
  1568. * b) The target zone may be at high_wmark_pages(zone) but the lower zones
  1569. * must go *over* high_wmark_pages(zone) to satisfy the `incremental min'
  1570. * zone defense algorithm.
  1571. *
  1572. * If a zone is deemed to be full of pinned pages then just give it a light
  1573. * scan then give up on it.
  1574. */
  1575. static bool shrink_zones(int priority, struct zonelist *zonelist,
  1576. struct scan_control *sc)
  1577. {
  1578. struct zoneref *z;
  1579. struct zone *zone;
  1580. bool all_unreclaimable = true;
  1581. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  1582. gfp_zone(sc->gfp_mask), sc->nodemask) {
  1583. if (!populated_zone(zone))
  1584. continue;
  1585. /*
  1586. * Take care memory controller reclaiming has small influence
  1587. * to global LRU.
  1588. */
  1589. if (scanning_global_lru(sc)) {
  1590. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1591. continue;
  1592. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1593. continue; /* Let kswapd poll it */
  1594. }
  1595. shrink_zone(priority, zone, sc);
  1596. all_unreclaimable = false;
  1597. }
  1598. return all_unreclaimable;
  1599. }
  1600. /*
  1601. * This is the main entry point to direct page reclaim.
  1602. *
  1603. * If a full scan of the inactive list fails to free enough memory then we
  1604. * are "out of memory" and something needs to be killed.
  1605. *
  1606. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  1607. * high - the zone may be full of dirty or under-writeback pages, which this
  1608. * caller can't do much about. We kick the writeback threads and take explicit
  1609. * naps in the hope that some of these pages can be written. But if the
  1610. * allocating task holds filesystem locks which prevent writeout this might not
  1611. * work, and the allocation attempt will fail.
  1612. *
  1613. * returns: 0, if no pages reclaimed
  1614. * else, the number of pages reclaimed
  1615. */
  1616. static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
  1617. struct scan_control *sc)
  1618. {
  1619. int priority;
  1620. bool all_unreclaimable;
  1621. unsigned long total_scanned = 0;
  1622. struct reclaim_state *reclaim_state = current->reclaim_state;
  1623. struct zoneref *z;
  1624. struct zone *zone;
  1625. unsigned long writeback_threshold;
  1626. get_mems_allowed();
  1627. delayacct_freepages_start();
  1628. if (scanning_global_lru(sc))
  1629. count_vm_event(ALLOCSTALL);
  1630. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1631. sc->nr_scanned = 0;
  1632. if (!priority)
  1633. disable_swap_token();
  1634. all_unreclaimable = shrink_zones(priority, zonelist, sc);
  1635. /*
  1636. * Don't shrink slabs when reclaiming memory from
  1637. * over limit cgroups
  1638. */
  1639. if (scanning_global_lru(sc)) {
  1640. unsigned long lru_pages = 0;
  1641. for_each_zone_zonelist(zone, z, zonelist,
  1642. gfp_zone(sc->gfp_mask)) {
  1643. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1644. continue;
  1645. lru_pages += zone_reclaimable_pages(zone);
  1646. }
  1647. shrink_slab(sc->nr_scanned, sc->gfp_mask, lru_pages);
  1648. if (reclaim_state) {
  1649. sc->nr_reclaimed += reclaim_state->reclaimed_slab;
  1650. reclaim_state->reclaimed_slab = 0;
  1651. }
  1652. }
  1653. total_scanned += sc->nr_scanned;
  1654. if (sc->nr_reclaimed >= sc->nr_to_reclaim)
  1655. goto out;
  1656. /*
  1657. * Try to write back as many pages as we just scanned. This
  1658. * tends to cause slow streaming writers to write data to the
  1659. * disk smoothly, at the dirtying rate, which is nice. But
  1660. * that's undesirable in laptop mode, where we *want* lumpy
  1661. * writeout. So in laptop mode, write out the whole world.
  1662. */
  1663. writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2;
  1664. if (total_scanned > writeback_threshold) {
  1665. wakeup_flusher_threads(laptop_mode ? 0 : total_scanned);
  1666. sc->may_writepage = 1;
  1667. }
  1668. /* Take a nap, wait for some writeback to complete */
  1669. if (!sc->hibernation_mode && sc->nr_scanned &&
  1670. priority < DEF_PRIORITY - 2)
  1671. congestion_wait(BLK_RW_ASYNC, HZ/10);
  1672. }
  1673. out:
  1674. /*
  1675. * Now that we've scanned all the zones at this priority level, note
  1676. * that level within the zone so that the next thread which performs
  1677. * scanning of this zone will immediately start out at this priority
  1678. * level. This affects only the decision whether or not to bring
  1679. * mapped pages onto the inactive list.
  1680. */
  1681. if (priority < 0)
  1682. priority = 0;
  1683. delayacct_freepages_end();
  1684. put_mems_allowed();
  1685. if (sc->nr_reclaimed)
  1686. return sc->nr_reclaimed;
  1687. /* top priority shrink_zones still had more to do? don't OOM, then */
  1688. if (scanning_global_lru(sc) && !all_unreclaimable)
  1689. return 1;
  1690. return 0;
  1691. }
  1692. unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
  1693. gfp_t gfp_mask, nodemask_t *nodemask)
  1694. {
  1695. unsigned long nr_reclaimed;
  1696. struct scan_control sc = {
  1697. .gfp_mask = gfp_mask,
  1698. .may_writepage = !laptop_mode,
  1699. .nr_to_reclaim = SWAP_CLUSTER_MAX,
  1700. .may_unmap = 1,
  1701. .may_swap = 1,
  1702. .swappiness = vm_swappiness,
  1703. .order = order,
  1704. .mem_cgroup = NULL,
  1705. .nodemask = nodemask,
  1706. };
  1707. trace_mm_vmscan_direct_reclaim_begin(order,
  1708. sc.may_writepage,
  1709. gfp_mask);
  1710. nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
  1711. trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
  1712. return nr_reclaimed;
  1713. }
  1714. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  1715. unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *mem,
  1716. gfp_t gfp_mask, bool noswap,
  1717. unsigned int swappiness,
  1718. struct zone *zone)
  1719. {
  1720. struct scan_control sc = {
  1721. .nr_to_reclaim = SWAP_CLUSTER_MAX,
  1722. .may_writepage = !laptop_mode,
  1723. .may_unmap = 1,
  1724. .may_swap = !noswap,
  1725. .swappiness = swappiness,
  1726. .order = 0,
  1727. .mem_cgroup = mem,
  1728. };
  1729. sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
  1730. (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
  1731. trace_mm_vmscan_memcg_softlimit_reclaim_begin(0,
  1732. sc.may_writepage,
  1733. sc.gfp_mask);
  1734. /*
  1735. * NOTE: Although we can get the priority field, using it
  1736. * here is not a good idea, since it limits the pages we can scan.
  1737. * if we don't reclaim here, the shrink_zone from balance_pgdat
  1738. * will pick up pages from other mem cgroup's as well. We hack
  1739. * the priority and make it zero.
  1740. */
  1741. shrink_zone(0, zone, &sc);
  1742. trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
  1743. return sc.nr_reclaimed;
  1744. }
  1745. unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont,
  1746. gfp_t gfp_mask,
  1747. bool noswap,
  1748. unsigned int swappiness)
  1749. {
  1750. struct zonelist *zonelist;
  1751. unsigned long nr_reclaimed;
  1752. struct scan_control sc = {
  1753. .may_writepage = !laptop_mode,
  1754. .may_unmap = 1,
  1755. .may_swap = !noswap,
  1756. .nr_to_reclaim = SWAP_CLUSTER_MAX,
  1757. .swappiness = swappiness,
  1758. .order = 0,
  1759. .mem_cgroup = mem_cont,
  1760. .nodemask = NULL, /* we don't care the placement */
  1761. };
  1762. sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
  1763. (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
  1764. zonelist = NODE_DATA(numa_node_id())->node_zonelists;
  1765. trace_mm_vmscan_memcg_reclaim_begin(0,
  1766. sc.may_writepage,
  1767. sc.gfp_mask);
  1768. nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
  1769. trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
  1770. return nr_reclaimed;
  1771. }
  1772. #endif
  1773. /* is kswapd sleeping prematurely? */
  1774. static int sleeping_prematurely(pg_data_t *pgdat, int order, long remaining)
  1775. {
  1776. int i;
  1777. /* If a direct reclaimer woke kswapd within HZ/10, it's premature */
  1778. if (remaining)
  1779. return 1;
  1780. /* If after HZ/10, a zone is below the high mark, it's premature */
  1781. for (i = 0; i < pgdat->nr_zones; i++) {
  1782. struct zone *zone = pgdat->node_zones + i;
  1783. if (!populated_zone(zone))
  1784. continue;
  1785. if (zone->all_unreclaimable)
  1786. continue;
  1787. if (!zone_watermark_ok(zone, order, high_wmark_pages(zone),
  1788. 0, 0))
  1789. return 1;
  1790. }
  1791. return 0;
  1792. }
  1793. /*
  1794. * For kswapd, balance_pgdat() will work across all this node's zones until
  1795. * they are all at high_wmark_pages(zone).
  1796. *
  1797. * Returns the number of pages which were actually freed.
  1798. *
  1799. * There is special handling here for zones which are full of pinned pages.
  1800. * This can happen if the pages are all mlocked, or if they are all used by
  1801. * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
  1802. * What we do is to detect the case where all pages in the zone have been
  1803. * scanned twice and there has been zero successful reclaim. Mark the zone as
  1804. * dead and from now on, only perform a short scan. Basically we're polling
  1805. * the zone for when the problem goes away.
  1806. *
  1807. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  1808. * zones which have free_pages > high_wmark_pages(zone), but once a zone is
  1809. * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the
  1810. * lower zones regardless of the number of free pages in the lower zones. This
  1811. * interoperates with the page allocator fallback scheme to ensure that aging
  1812. * of pages is balanced across the zones.
  1813. */
  1814. static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
  1815. {
  1816. int all_zones_ok;
  1817. int priority;
  1818. int i;
  1819. unsigned long total_scanned;
  1820. struct reclaim_state *reclaim_state = current->reclaim_state;
  1821. struct scan_control sc = {
  1822. .gfp_mask = GFP_KERNEL,
  1823. .may_unmap = 1,
  1824. .may_swap = 1,
  1825. /*
  1826. * kswapd doesn't want to be bailed out while reclaim. because
  1827. * we want to put equal scanning pressure on each zone.
  1828. */
  1829. .nr_to_reclaim = ULONG_MAX,
  1830. .swappiness = vm_swappiness,
  1831. .order = order,
  1832. .mem_cgroup = NULL,
  1833. };
  1834. loop_again:
  1835. total_scanned = 0;
  1836. sc.nr_reclaimed = 0;
  1837. sc.may_writepage = !laptop_mode;
  1838. count_vm_event(PAGEOUTRUN);
  1839. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1840. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  1841. unsigned long lru_pages = 0;
  1842. int has_under_min_watermark_zone = 0;
  1843. /* The swap token gets in the way of swapout... */
  1844. if (!priority)
  1845. disable_swap_token();
  1846. all_zones_ok = 1;
  1847. /*
  1848. * Scan in the highmem->dma direction for the highest
  1849. * zone which needs scanning
  1850. */
  1851. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  1852. struct zone *zone = pgdat->node_zones + i;
  1853. if (!populated_zone(zone))
  1854. continue;
  1855. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1856. continue;
  1857. /*
  1858. * Do some background aging of the anon list, to give
  1859. * pages a chance to be referenced before reclaiming.
  1860. */
  1861. if (inactive_anon_is_low(zone, &sc))
  1862. shrink_active_list(SWAP_CLUSTER_MAX, zone,
  1863. &sc, priority, 0);
  1864. if (!zone_watermark_ok(zone, order,
  1865. high_wmark_pages(zone), 0, 0)) {
  1866. end_zone = i;
  1867. break;
  1868. }
  1869. }
  1870. if (i < 0)
  1871. goto out;
  1872. for (i = 0; i <= end_zone; i++) {
  1873. struct zone *zone = pgdat->node_zones + i;
  1874. lru_pages += zone_reclaimable_pages(zone);
  1875. }
  1876. /*
  1877. * Now scan the zone in the dma->highmem direction, stopping
  1878. * at the last zone which needs scanning.
  1879. *
  1880. * We do this because the page allocator works in the opposite
  1881. * direction. This prevents the page allocator from allocating
  1882. * pages behind kswapd's direction of progress, which would
  1883. * cause too much scanning of the lower zones.
  1884. */
  1885. for (i = 0; i <= end_zone; i++) {
  1886. struct zone *zone = pgdat->node_zones + i;
  1887. int nr_slab;
  1888. if (!populated_zone(zone))
  1889. continue;
  1890. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1891. continue;
  1892. sc.nr_scanned = 0;
  1893. /*
  1894. * Call soft limit reclaim before calling shrink_zone.
  1895. * For now we ignore the return value
  1896. */
  1897. mem_cgroup_soft_limit_reclaim(zone, order, sc.gfp_mask);
  1898. /*
  1899. * We put equal pressure on every zone, unless one
  1900. * zone has way too many pages free already.
  1901. */
  1902. if (!zone_watermark_ok(zone, order,
  1903. 8*high_wmark_pages(zone), end_zone, 0))
  1904. shrink_zone(priority, zone, &sc);
  1905. reclaim_state->reclaimed_slab = 0;
  1906. nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
  1907. lru_pages);
  1908. sc.nr_reclaimed += reclaim_state->reclaimed_slab;
  1909. total_scanned += sc.nr_scanned;
  1910. if (zone->all_unreclaimable)
  1911. continue;
  1912. if (nr_slab == 0 &&
  1913. zone->pages_scanned >= (zone_reclaimable_pages(zone) * 6))
  1914. zone->all_unreclaimable = 1;
  1915. /*
  1916. * If we've done a decent amount of scanning and
  1917. * the reclaim ratio is low, start doing writepage
  1918. * even in laptop mode
  1919. */
  1920. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  1921. total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2)
  1922. sc.may_writepage = 1;
  1923. if (!zone_watermark_ok(zone, order,
  1924. high_wmark_pages(zone), end_zone, 0)) {
  1925. all_zones_ok = 0;
  1926. /*
  1927. * We are still under min water mark. This
  1928. * means that we have a GFP_ATOMIC allocation
  1929. * failure risk. Hurry up!
  1930. */
  1931. if (!zone_watermark_ok(zone, order,
  1932. min_wmark_pages(zone), end_zone, 0))
  1933. has_under_min_watermark_zone = 1;
  1934. }
  1935. }
  1936. if (all_zones_ok)
  1937. break; /* kswapd: all done */
  1938. /*
  1939. * OK, kswapd is getting into trouble. Take a nap, then take
  1940. * another pass across the zones.
  1941. */
  1942. if (total_scanned && (priority < DEF_PRIORITY - 2)) {
  1943. if (has_under_min_watermark_zone)
  1944. count_vm_event(KSWAPD_SKIP_CONGESTION_WAIT);
  1945. else
  1946. congestion_wait(BLK_RW_ASYNC, HZ/10);
  1947. }
  1948. /*
  1949. * We do this so kswapd doesn't build up large priorities for
  1950. * example when it is freeing in parallel with allocators. It
  1951. * matches the direct reclaim path behaviour in terms of impact
  1952. * on zone->*_priority.
  1953. */
  1954. if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX)
  1955. break;
  1956. }
  1957. out:
  1958. if (!all_zones_ok) {
  1959. cond_resched();
  1960. try_to_freeze();
  1961. /*
  1962. * Fragmentation may mean that the system cannot be
  1963. * rebalanced for high-order allocations in all zones.
  1964. * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX,
  1965. * it means the zones have been fully scanned and are still
  1966. * not balanced. For high-order allocations, there is
  1967. * little point trying all over again as kswapd may
  1968. * infinite loop.
  1969. *
  1970. * Instead, recheck all watermarks at order-0 as they
  1971. * are the most important. If watermarks are ok, kswapd will go
  1972. * back to sleep. High-order users can still perform direct
  1973. * reclaim if they wish.
  1974. */
  1975. if (sc.nr_reclaimed < SWAP_CLUSTER_MAX)
  1976. order = sc.order = 0;
  1977. goto loop_again;
  1978. }
  1979. return sc.nr_reclaimed;
  1980. }
  1981. /*
  1982. * The background pageout daemon, started as a kernel thread
  1983. * from the init process.
  1984. *
  1985. * This basically trickles out pages so that we have _some_
  1986. * free memory available even if there is no other activity
  1987. * that frees anything up. This is needed for things like routing
  1988. * etc, where we otherwise might have all activity going on in
  1989. * asynchronous contexts that cannot page things out.
  1990. *
  1991. * If there are applications that are active memory-allocators
  1992. * (most normal use), this basically shouldn't matter.
  1993. */
  1994. static int kswapd(void *p)
  1995. {
  1996. unsigned long order;
  1997. pg_data_t *pgdat = (pg_data_t*)p;
  1998. struct task_struct *tsk = current;
  1999. DEFINE_WAIT(wait);
  2000. struct reclaim_state reclaim_state = {
  2001. .reclaimed_slab = 0,
  2002. };
  2003. const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
  2004. lockdep_set_current_reclaim_state(GFP_KERNEL);
  2005. if (!cpumask_empty(cpumask))
  2006. set_cpus_allowed_ptr(tsk, cpumask);
  2007. current->reclaim_state = &reclaim_state;
  2008. /*
  2009. * Tell the memory management that we're a "memory allocator",
  2010. * and that if we need more memory we should get access to it
  2011. * regardless (see "__alloc_pages()"). "kswapd" should
  2012. * never get caught in the normal page freeing logic.
  2013. *
  2014. * (Kswapd normally doesn't need memory anyway, but sometimes
  2015. * you need a small amount of memory in order to be able to
  2016. * page out something else, and this flag essentially protects
  2017. * us from recursively trying to free more memory as we're
  2018. * trying to free the first piece of memory in the first place).
  2019. */
  2020. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  2021. set_freezable();
  2022. order = 0;
  2023. for ( ; ; ) {
  2024. unsigned long new_order;
  2025. int ret;
  2026. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  2027. new_order = pgdat->kswapd_max_order;
  2028. pgdat->kswapd_max_order = 0;
  2029. if (order < new_order) {
  2030. /*
  2031. * Don't sleep if someone wants a larger 'order'
  2032. * allocation
  2033. */
  2034. order = new_order;
  2035. } else {
  2036. if (!freezing(current) && !kthread_should_stop()) {
  2037. long remaining = 0;
  2038. /* Try to sleep for a short interval */
  2039. if (!sleeping_prematurely(pgdat, order, remaining)) {
  2040. remaining = schedule_timeout(HZ/10);
  2041. finish_wait(&pgdat->kswapd_wait, &wait);
  2042. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  2043. }
  2044. /*
  2045. * After a short sleep, check if it was a
  2046. * premature sleep. If not, then go fully
  2047. * to sleep until explicitly woken up
  2048. */
  2049. if (!sleeping_prematurely(pgdat, order, remaining)) {
  2050. trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
  2051. schedule();
  2052. } else {
  2053. if (remaining)
  2054. count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
  2055. else
  2056. count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
  2057. }
  2058. }
  2059. order = pgdat->kswapd_max_order;
  2060. }
  2061. finish_wait(&pgdat->kswapd_wait, &wait);
  2062. ret = try_to_freeze();
  2063. if (kthread_should_stop())
  2064. break;
  2065. /*
  2066. * We can speed up thawing tasks if we don't call balance_pgdat
  2067. * after returning from the refrigerator
  2068. */
  2069. if (!ret) {
  2070. trace_mm_vmscan_kswapd_wake(pgdat->node_id, order);
  2071. balance_pgdat(pgdat, order);
  2072. }
  2073. }
  2074. return 0;
  2075. }
  2076. /*
  2077. * A zone is low on free memory, so wake its kswapd task to service it.
  2078. */
  2079. void wakeup_kswapd(struct zone *zone, int order)
  2080. {
  2081. pg_data_t *pgdat;
  2082. if (!populated_zone(zone))
  2083. return;
  2084. pgdat = zone->zone_pgdat;
  2085. if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 0))
  2086. return;
  2087. if (pgdat->kswapd_max_order < order)
  2088. pgdat->kswapd_max_order = order;
  2089. trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order);
  2090. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  2091. return;
  2092. if (!waitqueue_active(&pgdat->kswapd_wait))
  2093. return;
  2094. wake_up_interruptible(&pgdat->kswapd_wait);
  2095. }
  2096. /*
  2097. * The reclaimable count would be mostly accurate.
  2098. * The less reclaimable pages may be
  2099. * - mlocked pages, which will be moved to unevictable list when encountered
  2100. * - mapped pages, which may require several travels to be reclaimed
  2101. * - dirty pages, which is not "instantly" reclaimable
  2102. */
  2103. unsigned long global_reclaimable_pages(void)
  2104. {
  2105. int nr;
  2106. nr = global_page_state(NR_ACTIVE_FILE) +
  2107. global_page_state(NR_INACTIVE_FILE);
  2108. if (nr_swap_pages > 0)
  2109. nr += global_page_state(NR_ACTIVE_ANON) +
  2110. global_page_state(NR_INACTIVE_ANON);
  2111. return nr;
  2112. }
  2113. unsigned long zone_reclaimable_pages(struct zone *zone)
  2114. {
  2115. int nr;
  2116. nr = zone_page_state(zone, NR_ACTIVE_FILE) +
  2117. zone_page_state(zone, NR_INACTIVE_FILE);
  2118. if (nr_swap_pages > 0)
  2119. nr += zone_page_state(zone, NR_ACTIVE_ANON) +
  2120. zone_page_state(zone, NR_INACTIVE_ANON);
  2121. return nr;
  2122. }
  2123. #ifdef CONFIG_HIBERNATION
  2124. /*
  2125. * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
  2126. * freed pages.
  2127. *
  2128. * Rather than trying to age LRUs the aim is to preserve the overall
  2129. * LRU order by reclaiming preferentially
  2130. * inactive > active > active referenced > active mapped
  2131. */
  2132. unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
  2133. {
  2134. struct reclaim_state reclaim_state;
  2135. struct scan_control sc = {
  2136. .gfp_mask = GFP_HIGHUSER_MOVABLE,
  2137. .may_swap = 1,
  2138. .may_unmap = 1,
  2139. .may_writepage = 1,
  2140. .nr_to_reclaim = nr_to_reclaim,
  2141. .hibernation_mode = 1,
  2142. .swappiness = vm_swappiness,
  2143. .order = 0,
  2144. };
  2145. struct zonelist * zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
  2146. struct task_struct *p = current;
  2147. unsigned long nr_reclaimed;
  2148. p->flags |= PF_MEMALLOC;
  2149. lockdep_set_current_reclaim_state(sc.gfp_mask);
  2150. reclaim_state.reclaimed_slab = 0;
  2151. p->reclaim_state = &reclaim_state;
  2152. nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
  2153. p->reclaim_state = NULL;
  2154. lockdep_clear_current_reclaim_state();
  2155. p->flags &= ~PF_MEMALLOC;
  2156. return nr_reclaimed;
  2157. }
  2158. #endif /* CONFIG_HIBERNATION */
  2159. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  2160. not required for correctness. So if the last cpu in a node goes
  2161. away, we get changed to run anywhere: as the first one comes back,
  2162. restore their cpu bindings. */
  2163. static int __devinit cpu_callback(struct notifier_block *nfb,
  2164. unsigned long action, void *hcpu)
  2165. {
  2166. int nid;
  2167. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
  2168. for_each_node_state(nid, N_HIGH_MEMORY) {
  2169. pg_data_t *pgdat = NODE_DATA(nid);
  2170. const struct cpumask *mask;
  2171. mask = cpumask_of_node(pgdat->node_id);
  2172. if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
  2173. /* One of our CPUs online: restore mask */
  2174. set_cpus_allowed_ptr(pgdat->kswapd, mask);
  2175. }
  2176. }
  2177. return NOTIFY_OK;
  2178. }
  2179. /*
  2180. * This kswapd start function will be called by init and node-hot-add.
  2181. * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
  2182. */
  2183. int kswapd_run(int nid)
  2184. {
  2185. pg_data_t *pgdat = NODE_DATA(nid);
  2186. int ret = 0;
  2187. if (pgdat->kswapd)
  2188. return 0;
  2189. pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
  2190. if (IS_ERR(pgdat->kswapd)) {
  2191. /* failure at boot is fatal */
  2192. BUG_ON(system_state == SYSTEM_BOOTING);
  2193. printk("Failed to start kswapd on node %d\n",nid);
  2194. ret = -1;
  2195. }
  2196. return ret;
  2197. }
  2198. /*
  2199. * Called by memory hotplug when all memory in a node is offlined.
  2200. */
  2201. void kswapd_stop(int nid)
  2202. {
  2203. struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
  2204. if (kswapd)
  2205. kthread_stop(kswapd);
  2206. }
  2207. static int __init kswapd_init(void)
  2208. {
  2209. int nid;
  2210. swap_setup();
  2211. for_each_node_state(nid, N_HIGH_MEMORY)
  2212. kswapd_run(nid);
  2213. hotcpu_notifier(cpu_callback, 0);
  2214. return 0;
  2215. }
  2216. module_init(kswapd_init)
  2217. #ifdef CONFIG_NUMA
  2218. /*
  2219. * Zone reclaim mode
  2220. *
  2221. * If non-zero call zone_reclaim when the number of free pages falls below
  2222. * the watermarks.
  2223. */
  2224. int zone_reclaim_mode __read_mostly;
  2225. #define RECLAIM_OFF 0
  2226. #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */
  2227. #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
  2228. #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
  2229. /*
  2230. * Priority for ZONE_RECLAIM. This determines the fraction of pages
  2231. * of a node considered for each zone_reclaim. 4 scans 1/16th of
  2232. * a zone.
  2233. */
  2234. #define ZONE_RECLAIM_PRIORITY 4
  2235. /*
  2236. * Percentage of pages in a zone that must be unmapped for zone_reclaim to
  2237. * occur.
  2238. */
  2239. int sysctl_min_unmapped_ratio = 1;
  2240. /*
  2241. * If the number of slab pages in a zone grows beyond this percentage then
  2242. * slab reclaim needs to occur.
  2243. */
  2244. int sysctl_min_slab_ratio = 5;
  2245. static inline unsigned long zone_unmapped_file_pages(struct zone *zone)
  2246. {
  2247. unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED);
  2248. unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) +
  2249. zone_page_state(zone, NR_ACTIVE_FILE);
  2250. /*
  2251. * It's possible for there to be more file mapped pages than
  2252. * accounted for by the pages on the file LRU lists because
  2253. * tmpfs pages accounted for as ANON can also be FILE_MAPPED
  2254. */
  2255. return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
  2256. }
  2257. /* Work out how many page cache pages we can reclaim in this reclaim_mode */
  2258. static long zone_pagecache_reclaimable(struct zone *zone)
  2259. {
  2260. long nr_pagecache_reclaimable;
  2261. long delta = 0;
  2262. /*
  2263. * If RECLAIM_SWAP is set, then all file pages are considered
  2264. * potentially reclaimable. Otherwise, we have to worry about
  2265. * pages like swapcache and zone_unmapped_file_pages() provides
  2266. * a better estimate
  2267. */
  2268. if (zone_reclaim_mode & RECLAIM_SWAP)
  2269. nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES);
  2270. else
  2271. nr_pagecache_reclaimable = zone_unmapped_file_pages(zone);
  2272. /* If we can't clean pages, remove dirty pages from consideration */
  2273. if (!(zone_reclaim_mode & RECLAIM_WRITE))
  2274. delta += zone_page_state(zone, NR_FILE_DIRTY);
  2275. /* Watch for any possible underflows due to delta */
  2276. if (unlikely(delta > nr_pagecache_reclaimable))
  2277. delta = nr_pagecache_reclaimable;
  2278. return nr_pagecache_reclaimable - delta;
  2279. }
  2280. /*
  2281. * Try to free up some pages from this zone through reclaim.
  2282. */
  2283. static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  2284. {
  2285. /* Minimum pages needed in order to stay on node */
  2286. const unsigned long nr_pages = 1 << order;
  2287. struct task_struct *p = current;
  2288. struct reclaim_state reclaim_state;
  2289. int priority;
  2290. struct scan_control sc = {
  2291. .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
  2292. .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP),
  2293. .may_swap = 1,
  2294. .nr_to_reclaim = max_t(unsigned long, nr_pages,
  2295. SWAP_CLUSTER_MAX),
  2296. .gfp_mask = gfp_mask,
  2297. .swappiness = vm_swappiness,
  2298. .order = order,
  2299. };
  2300. unsigned long nr_slab_pages0, nr_slab_pages1;
  2301. cond_resched();
  2302. /*
  2303. * We need to be able to allocate from the reserves for RECLAIM_SWAP
  2304. * and we also need to be able to write out pages for RECLAIM_WRITE
  2305. * and RECLAIM_SWAP.
  2306. */
  2307. p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
  2308. lockdep_set_current_reclaim_state(gfp_mask);
  2309. reclaim_state.reclaimed_slab = 0;
  2310. p->reclaim_state = &reclaim_state;
  2311. if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) {
  2312. /*
  2313. * Free memory by calling shrink zone with increasing
  2314. * priorities until we have enough memory freed.
  2315. */
  2316. priority = ZONE_RECLAIM_PRIORITY;
  2317. do {
  2318. shrink_zone(priority, zone, &sc);
  2319. priority--;
  2320. } while (priority >= 0 && sc.nr_reclaimed < nr_pages);
  2321. }
  2322. nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  2323. if (nr_slab_pages0 > zone->min_slab_pages) {
  2324. /*
  2325. * shrink_slab() does not currently allow us to determine how
  2326. * many pages were freed in this zone. So we take the current
  2327. * number of slab pages and shake the slab until it is reduced
  2328. * by the same nr_pages that we used for reclaiming unmapped
  2329. * pages.
  2330. *
  2331. * Note that shrink_slab will free memory on all zones and may
  2332. * take a long time.
  2333. */
  2334. for (;;) {
  2335. unsigned long lru_pages = zone_reclaimable_pages(zone);
  2336. /* No reclaimable slab or very low memory pressure */
  2337. if (!shrink_slab(sc.nr_scanned, gfp_mask, lru_pages))
  2338. break;
  2339. /* Freed enough memory */
  2340. nr_slab_pages1 = zone_page_state(zone,
  2341. NR_SLAB_RECLAIMABLE);
  2342. if (nr_slab_pages1 + nr_pages <= nr_slab_pages0)
  2343. break;
  2344. }
  2345. /*
  2346. * Update nr_reclaimed by the number of slab pages we
  2347. * reclaimed from this zone.
  2348. */
  2349. nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  2350. if (nr_slab_pages1 < nr_slab_pages0)
  2351. sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1;
  2352. }
  2353. p->reclaim_state = NULL;
  2354. current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
  2355. lockdep_clear_current_reclaim_state();
  2356. return sc.nr_reclaimed >= nr_pages;
  2357. }
  2358. int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  2359. {
  2360. int node_id;
  2361. int ret;
  2362. /*
  2363. * Zone reclaim reclaims unmapped file backed pages and
  2364. * slab pages if we are over the defined limits.
  2365. *
  2366. * A small portion of unmapped file backed pages is needed for
  2367. * file I/O otherwise pages read by file I/O will be immediately
  2368. * thrown out if the zone is overallocated. So we do not reclaim
  2369. * if less than a specified percentage of the zone is used by
  2370. * unmapped file backed pages.
  2371. */
  2372. if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages &&
  2373. zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages)
  2374. return ZONE_RECLAIM_FULL;
  2375. if (zone->all_unreclaimable)
  2376. return ZONE_RECLAIM_FULL;
  2377. /*
  2378. * Do not scan if the allocation should not be delayed.
  2379. */
  2380. if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
  2381. return ZONE_RECLAIM_NOSCAN;
  2382. /*
  2383. * Only run zone reclaim on the local zone or on zones that do not
  2384. * have associated processors. This will favor the local processor
  2385. * over remote processors and spread off node memory allocations
  2386. * as wide as possible.
  2387. */
  2388. node_id = zone_to_nid(zone);
  2389. if (node_state(node_id, N_CPU) && node_id != numa_node_id())
  2390. return ZONE_RECLAIM_NOSCAN;
  2391. if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
  2392. return ZONE_RECLAIM_NOSCAN;
  2393. ret = __zone_reclaim(zone, gfp_mask, order);
  2394. zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
  2395. if (!ret)
  2396. count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
  2397. return ret;
  2398. }
  2399. #endif
  2400. /*
  2401. * page_evictable - test whether a page is evictable
  2402. * @page: the page to test
  2403. * @vma: the VMA in which the page is or will be mapped, may be NULL
  2404. *
  2405. * Test whether page is evictable--i.e., should be placed on active/inactive
  2406. * lists vs unevictable list. The vma argument is !NULL when called from the
  2407. * fault path to determine how to instantate a new page.
  2408. *
  2409. * Reasons page might not be evictable:
  2410. * (1) page's mapping marked unevictable
  2411. * (2) page is part of an mlocked VMA
  2412. *
  2413. */
  2414. int page_evictable(struct page *page, struct vm_area_struct *vma)
  2415. {
  2416. if (mapping_unevictable(page_mapping(page)))
  2417. return 0;
  2418. if (PageMlocked(page) || (vma && is_mlocked_vma(vma, page)))
  2419. return 0;
  2420. return 1;
  2421. }
  2422. /**
  2423. * check_move_unevictable_page - check page for evictability and move to appropriate zone lru list
  2424. * @page: page to check evictability and move to appropriate lru list
  2425. * @zone: zone page is in
  2426. *
  2427. * Checks a page for evictability and moves the page to the appropriate
  2428. * zone lru list.
  2429. *
  2430. * Restrictions: zone->lru_lock must be held, page must be on LRU and must
  2431. * have PageUnevictable set.
  2432. */
  2433. static void check_move_unevictable_page(struct page *page, struct zone *zone)
  2434. {
  2435. VM_BUG_ON(PageActive(page));
  2436. retry:
  2437. ClearPageUnevictable(page);
  2438. if (page_evictable(page, NULL)) {
  2439. enum lru_list l = page_lru_base_type(page);
  2440. __dec_zone_state(zone, NR_UNEVICTABLE);
  2441. list_move(&page->lru, &zone->lru[l].list);
  2442. mem_cgroup_move_lists(page, LRU_UNEVICTABLE, l);
  2443. __inc_zone_state(zone, NR_INACTIVE_ANON + l);
  2444. __count_vm_event(UNEVICTABLE_PGRESCUED);
  2445. } else {
  2446. /*
  2447. * rotate unevictable list
  2448. */
  2449. SetPageUnevictable(page);
  2450. list_move(&page->lru, &zone->lru[LRU_UNEVICTABLE].list);
  2451. mem_cgroup_rotate_lru_list(page, LRU_UNEVICTABLE);
  2452. if (page_evictable(page, NULL))
  2453. goto retry;
  2454. }
  2455. }
  2456. /**
  2457. * scan_mapping_unevictable_pages - scan an address space for evictable pages
  2458. * @mapping: struct address_space to scan for evictable pages
  2459. *
  2460. * Scan all pages in mapping. Check unevictable pages for
  2461. * evictability and move them to the appropriate zone lru list.
  2462. */
  2463. void scan_mapping_unevictable_pages(struct address_space *mapping)
  2464. {
  2465. pgoff_t next = 0;
  2466. pgoff_t end = (i_size_read(mapping->host) + PAGE_CACHE_SIZE - 1) >>
  2467. PAGE_CACHE_SHIFT;
  2468. struct zone *zone;
  2469. struct pagevec pvec;
  2470. if (mapping->nrpages == 0)
  2471. return;
  2472. pagevec_init(&pvec, 0);
  2473. while (next < end &&
  2474. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  2475. int i;
  2476. int pg_scanned = 0;
  2477. zone = NULL;
  2478. for (i = 0; i < pagevec_count(&pvec); i++) {
  2479. struct page *page = pvec.pages[i];
  2480. pgoff_t page_index = page->index;
  2481. struct zone *pagezone = page_zone(page);
  2482. pg_scanned++;
  2483. if (page_index > next)
  2484. next = page_index;
  2485. next++;
  2486. if (pagezone != zone) {
  2487. if (zone)
  2488. spin_unlock_irq(&zone->lru_lock);
  2489. zone = pagezone;
  2490. spin_lock_irq(&zone->lru_lock);
  2491. }
  2492. if (PageLRU(page) && PageUnevictable(page))
  2493. check_move_unevictable_page(page, zone);
  2494. }
  2495. if (zone)
  2496. spin_unlock_irq(&zone->lru_lock);
  2497. pagevec_release(&pvec);
  2498. count_vm_events(UNEVICTABLE_PGSCANNED, pg_scanned);
  2499. }
  2500. }
  2501. /**
  2502. * scan_zone_unevictable_pages - check unevictable list for evictable pages
  2503. * @zone - zone of which to scan the unevictable list
  2504. *
  2505. * Scan @zone's unevictable LRU lists to check for pages that have become
  2506. * evictable. Move those that have to @zone's inactive list where they
  2507. * become candidates for reclaim, unless shrink_inactive_zone() decides
  2508. * to reactivate them. Pages that are still unevictable are rotated
  2509. * back onto @zone's unevictable list.
  2510. */
  2511. #define SCAN_UNEVICTABLE_BATCH_SIZE 16UL /* arbitrary lock hold batch size */
  2512. static void scan_zone_unevictable_pages(struct zone *zone)
  2513. {
  2514. struct list_head *l_unevictable = &zone->lru[LRU_UNEVICTABLE].list;
  2515. unsigned long scan;
  2516. unsigned long nr_to_scan = zone_page_state(zone, NR_UNEVICTABLE);
  2517. while (nr_to_scan > 0) {
  2518. unsigned long batch_size = min(nr_to_scan,
  2519. SCAN_UNEVICTABLE_BATCH_SIZE);
  2520. spin_lock_irq(&zone->lru_lock);
  2521. for (scan = 0; scan < batch_size; scan++) {
  2522. struct page *page = lru_to_page(l_unevictable);
  2523. if (!trylock_page(page))
  2524. continue;
  2525. prefetchw_prev_lru_page(page, l_unevictable, flags);
  2526. if (likely(PageLRU(page) && PageUnevictable(page)))
  2527. check_move_unevictable_page(page, zone);
  2528. unlock_page(page);
  2529. }
  2530. spin_unlock_irq(&zone->lru_lock);
  2531. nr_to_scan -= batch_size;
  2532. }
  2533. }
  2534. /**
  2535. * scan_all_zones_unevictable_pages - scan all unevictable lists for evictable pages
  2536. *
  2537. * A really big hammer: scan all zones' unevictable LRU lists to check for
  2538. * pages that have become evictable. Move those back to the zones'
  2539. * inactive list where they become candidates for reclaim.
  2540. * This occurs when, e.g., we have unswappable pages on the unevictable lists,
  2541. * and we add swap to the system. As such, it runs in the context of a task
  2542. * that has possibly/probably made some previously unevictable pages
  2543. * evictable.
  2544. */
  2545. static void scan_all_zones_unevictable_pages(void)
  2546. {
  2547. struct zone *zone;
  2548. for_each_zone(zone) {
  2549. scan_zone_unevictable_pages(zone);
  2550. }
  2551. }
  2552. /*
  2553. * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of
  2554. * all nodes' unevictable lists for evictable pages
  2555. */
  2556. unsigned long scan_unevictable_pages;
  2557. int scan_unevictable_handler(struct ctl_table *table, int write,
  2558. void __user *buffer,
  2559. size_t *length, loff_t *ppos)
  2560. {
  2561. proc_doulongvec_minmax(table, write, buffer, length, ppos);
  2562. if (write && *(unsigned long *)table->data)
  2563. scan_all_zones_unevictable_pages();
  2564. scan_unevictable_pages = 0;
  2565. return 0;
  2566. }
  2567. /*
  2568. * per node 'scan_unevictable_pages' attribute. On demand re-scan of
  2569. * a specified node's per zone unevictable lists for evictable pages.
  2570. */
  2571. static ssize_t read_scan_unevictable_node(struct sys_device *dev,
  2572. struct sysdev_attribute *attr,
  2573. char *buf)
  2574. {
  2575. return sprintf(buf, "0\n"); /* always zero; should fit... */
  2576. }
  2577. static ssize_t write_scan_unevictable_node(struct sys_device *dev,
  2578. struct sysdev_attribute *attr,
  2579. const char *buf, size_t count)
  2580. {
  2581. struct zone *node_zones = NODE_DATA(dev->id)->node_zones;
  2582. struct zone *zone;
  2583. unsigned long res;
  2584. unsigned long req = strict_strtoul(buf, 10, &res);
  2585. if (!req)
  2586. return 1; /* zero is no-op */
  2587. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  2588. if (!populated_zone(zone))
  2589. continue;
  2590. scan_zone_unevictable_pages(zone);
  2591. }
  2592. return 1;
  2593. }
  2594. static SYSDEV_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR,
  2595. read_scan_unevictable_node,
  2596. write_scan_unevictable_node);
  2597. int scan_unevictable_register_node(struct node *node)
  2598. {
  2599. return sysdev_create_file(&node->sysdev, &attr_scan_unevictable_pages);
  2600. }
  2601. void scan_unevictable_unregister_node(struct node *node)
  2602. {
  2603. sysdev_remove_file(&node->sysdev, &attr_scan_unevictable_pages);
  2604. }