vmscan.c 104 KB

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