vmscan.c 104 KB

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