vmscan.c 95 KB

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