vmscan.c 107 KB

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