vmscan.c 92 KB

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