vmscan.c 103 KB

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