vmscan.c 97 KB

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