vmscan.c 84 KB

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