vmscan.c 98 KB

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