vmscan.c 107 KB

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