vmscan.c 100 KB

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