vmscan.c 88 KB

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