vmscan.c 96 KB

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