vmscan.c 98 KB

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