vmscan.c 82 KB

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