vmscan.c 103 KB

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