vmscan.c 92 KB

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