vmscan.c 97 KB

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