vmscan.c 96 KB

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