vmscan.c 101 KB

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