vmscan.c 102 KB

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