vmscan.c 98 KB

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