vmscan.c 97 KB

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