vmscan.c 107 KB

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