vmscan.c 98 KB

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