vmscan.c 93 KB

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