vmscan.c 92 KB

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