vmscan.c 93 KB

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