vmscan.c 82 KB

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