vmscan.c 107 KB

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