vmscan.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041
  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. return nr_reclaimed;
  1187. }
  1188. /*
  1189. * This moves pages from the active list to the inactive list.
  1190. *
  1191. * We move them the other way if the page is referenced by one or more
  1192. * processes, from rmap.
  1193. *
  1194. * If the pages are mostly unmapped, the processing is fast and it is
  1195. * appropriate to hold zone->lru_lock across the whole operation. But if
  1196. * the pages are mapped, the processing is slow (page_referenced()) so we
  1197. * should drop zone->lru_lock around each page. It's impossible to balance
  1198. * this, so instead we remove the pages from the LRU while processing them.
  1199. * It is safe to rely on PG_active against the non-LRU pages in here because
  1200. * nobody will play with that bit on a non-LRU page.
  1201. *
  1202. * The downside is that we have to touch page->_count against each page.
  1203. * But we had to alter page->flags anyway.
  1204. */
  1205. static void move_active_pages_to_lru(struct zone *zone,
  1206. struct list_head *list,
  1207. enum lru_list lru)
  1208. {
  1209. unsigned long pgmoved = 0;
  1210. struct pagevec pvec;
  1211. struct page *page;
  1212. pagevec_init(&pvec, 1);
  1213. while (!list_empty(list)) {
  1214. page = lru_to_page(list);
  1215. VM_BUG_ON(PageLRU(page));
  1216. SetPageLRU(page);
  1217. list_move(&page->lru, &zone->lru[lru].list);
  1218. mem_cgroup_add_lru_list(page, lru);
  1219. pgmoved++;
  1220. if (!pagevec_add(&pvec, page) || list_empty(list)) {
  1221. spin_unlock_irq(&zone->lru_lock);
  1222. if (buffer_heads_over_limit)
  1223. pagevec_strip(&pvec);
  1224. __pagevec_release(&pvec);
  1225. spin_lock_irq(&zone->lru_lock);
  1226. }
  1227. }
  1228. __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
  1229. if (!is_active_lru(lru))
  1230. __count_vm_events(PGDEACTIVATE, pgmoved);
  1231. }
  1232. static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
  1233. struct scan_control *sc, int priority, int file)
  1234. {
  1235. unsigned long nr_taken;
  1236. unsigned long pgscanned;
  1237. unsigned long vm_flags;
  1238. LIST_HEAD(l_hold); /* The pages which were snipped off */
  1239. LIST_HEAD(l_active);
  1240. LIST_HEAD(l_inactive);
  1241. struct page *page;
  1242. struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
  1243. unsigned long nr_rotated = 0;
  1244. lru_add_drain();
  1245. spin_lock_irq(&zone->lru_lock);
  1246. if (scanning_global_lru(sc)) {
  1247. nr_taken = isolate_pages_global(nr_pages, &l_hold,
  1248. &pgscanned, sc->order,
  1249. ISOLATE_ACTIVE, zone,
  1250. 1, file);
  1251. zone->pages_scanned += pgscanned;
  1252. } else {
  1253. nr_taken = mem_cgroup_isolate_pages(nr_pages, &l_hold,
  1254. &pgscanned, sc->order,
  1255. ISOLATE_ACTIVE, zone,
  1256. sc->mem_cgroup, 1, file);
  1257. /*
  1258. * mem_cgroup_isolate_pages() keeps track of
  1259. * scanned pages on its own.
  1260. */
  1261. }
  1262. reclaim_stat->recent_scanned[file] += nr_taken;
  1263. __count_zone_vm_events(PGREFILL, zone, pgscanned);
  1264. if (file)
  1265. __mod_zone_page_state(zone, NR_ACTIVE_FILE, -nr_taken);
  1266. else
  1267. __mod_zone_page_state(zone, NR_ACTIVE_ANON, -nr_taken);
  1268. __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
  1269. spin_unlock_irq(&zone->lru_lock);
  1270. while (!list_empty(&l_hold)) {
  1271. cond_resched();
  1272. page = lru_to_page(&l_hold);
  1273. list_del(&page->lru);
  1274. if (unlikely(!page_evictable(page, NULL))) {
  1275. putback_lru_page(page);
  1276. continue;
  1277. }
  1278. if (page_referenced(page, 0, sc->mem_cgroup, &vm_flags)) {
  1279. nr_rotated++;
  1280. /*
  1281. * Identify referenced, file-backed active pages and
  1282. * give them one more trip around the active list. So
  1283. * that executable code get better chances to stay in
  1284. * memory under moderate memory pressure. Anon pages
  1285. * are not likely to be evicted by use-once streaming
  1286. * IO, plus JVM can create lots of anon VM_EXEC pages,
  1287. * so we ignore them here.
  1288. */
  1289. if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
  1290. list_add(&page->lru, &l_active);
  1291. continue;
  1292. }
  1293. }
  1294. ClearPageActive(page); /* we are de-activating */
  1295. list_add(&page->lru, &l_inactive);
  1296. }
  1297. /*
  1298. * Move pages back to the lru list.
  1299. */
  1300. spin_lock_irq(&zone->lru_lock);
  1301. /*
  1302. * Count referenced pages from currently used mappings as rotated,
  1303. * even though only some of them are actually re-activated. This
  1304. * helps balance scan pressure between file and anonymous pages in
  1305. * get_scan_ratio.
  1306. */
  1307. reclaim_stat->recent_rotated[file] += nr_rotated;
  1308. move_active_pages_to_lru(zone, &l_active,
  1309. LRU_ACTIVE + file * LRU_FILE);
  1310. move_active_pages_to_lru(zone, &l_inactive,
  1311. LRU_BASE + file * LRU_FILE);
  1312. __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
  1313. spin_unlock_irq(&zone->lru_lock);
  1314. }
  1315. #ifdef CONFIG_SWAP
  1316. static int inactive_anon_is_low_global(struct zone *zone)
  1317. {
  1318. unsigned long active, inactive;
  1319. active = zone_page_state(zone, NR_ACTIVE_ANON);
  1320. inactive = zone_page_state(zone, NR_INACTIVE_ANON);
  1321. if (inactive * zone->inactive_ratio < active)
  1322. return 1;
  1323. return 0;
  1324. }
  1325. /**
  1326. * inactive_anon_is_low - check if anonymous pages need to be deactivated
  1327. * @zone: zone to check
  1328. * @sc: scan control of this context
  1329. *
  1330. * Returns true if the zone does not have enough inactive anon pages,
  1331. * meaning some active anon pages need to be deactivated.
  1332. */
  1333. static int inactive_anon_is_low(struct zone *zone, struct scan_control *sc)
  1334. {
  1335. int low;
  1336. /*
  1337. * If we don't have swap space, anonymous page deactivation
  1338. * is pointless.
  1339. */
  1340. if (!total_swap_pages)
  1341. return 0;
  1342. if (scanning_global_lru(sc))
  1343. low = inactive_anon_is_low_global(zone);
  1344. else
  1345. low = mem_cgroup_inactive_anon_is_low(sc->mem_cgroup);
  1346. return low;
  1347. }
  1348. #else
  1349. static inline int inactive_anon_is_low(struct zone *zone,
  1350. struct scan_control *sc)
  1351. {
  1352. return 0;
  1353. }
  1354. #endif
  1355. static int inactive_file_is_low_global(struct zone *zone)
  1356. {
  1357. unsigned long active, inactive;
  1358. active = zone_page_state(zone, NR_ACTIVE_FILE);
  1359. inactive = zone_page_state(zone, NR_INACTIVE_FILE);
  1360. return (active > inactive);
  1361. }
  1362. /**
  1363. * inactive_file_is_low - check if file pages need to be deactivated
  1364. * @zone: zone to check
  1365. * @sc: scan control of this context
  1366. *
  1367. * When the system is doing streaming IO, memory pressure here
  1368. * ensures that active file pages get deactivated, until more
  1369. * than half of the file pages are on the inactive list.
  1370. *
  1371. * Once we get to that situation, protect the system's working
  1372. * set from being evicted by disabling active file page aging.
  1373. *
  1374. * This uses a different ratio than the anonymous pages, because
  1375. * the page cache uses a use-once replacement algorithm.
  1376. */
  1377. static int inactive_file_is_low(struct zone *zone, struct scan_control *sc)
  1378. {
  1379. int low;
  1380. if (scanning_global_lru(sc))
  1381. low = inactive_file_is_low_global(zone);
  1382. else
  1383. low = mem_cgroup_inactive_file_is_low(sc->mem_cgroup);
  1384. return low;
  1385. }
  1386. static int inactive_list_is_low(struct zone *zone, struct scan_control *sc,
  1387. int file)
  1388. {
  1389. if (file)
  1390. return inactive_file_is_low(zone, sc);
  1391. else
  1392. return inactive_anon_is_low(zone, sc);
  1393. }
  1394. static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
  1395. struct zone *zone, struct scan_control *sc, int priority)
  1396. {
  1397. int file = is_file_lru(lru);
  1398. if (is_active_lru(lru)) {
  1399. if (inactive_list_is_low(zone, sc, file))
  1400. shrink_active_list(nr_to_scan, zone, sc, priority, file);
  1401. return 0;
  1402. }
  1403. return shrink_inactive_list(nr_to_scan, zone, sc, priority, file);
  1404. }
  1405. /*
  1406. * Smallish @nr_to_scan's are deposited in @nr_saved_scan,
  1407. * until we collected @swap_cluster_max pages to scan.
  1408. */
  1409. static unsigned long nr_scan_try_batch(unsigned long nr_to_scan,
  1410. unsigned long *nr_saved_scan)
  1411. {
  1412. unsigned long nr;
  1413. *nr_saved_scan += nr_to_scan;
  1414. nr = *nr_saved_scan;
  1415. if (nr >= SWAP_CLUSTER_MAX)
  1416. *nr_saved_scan = 0;
  1417. else
  1418. nr = 0;
  1419. return nr;
  1420. }
  1421. /*
  1422. * Determine how aggressively the anon and file LRU lists should be
  1423. * scanned. The relative value of each set of LRU lists is determined
  1424. * by looking at the fraction of the pages scanned we did rotate back
  1425. * onto the active list instead of evict.
  1426. *
  1427. * nr[0] = anon pages to scan; nr[1] = file pages to scan
  1428. */
  1429. static void get_scan_count(struct zone *zone, struct scan_control *sc,
  1430. unsigned long *nr, int priority)
  1431. {
  1432. unsigned long anon, file, free;
  1433. unsigned long anon_prio, file_prio;
  1434. unsigned long ap, fp;
  1435. struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc);
  1436. u64 fraction[2], denominator;
  1437. enum lru_list l;
  1438. int noswap = 0;
  1439. /* If we have no swap space, do not bother scanning anon pages. */
  1440. if (!sc->may_swap || (nr_swap_pages <= 0)) {
  1441. noswap = 1;
  1442. fraction[0] = 0;
  1443. fraction[1] = 1;
  1444. denominator = 1;
  1445. goto out;
  1446. }
  1447. anon = zone_nr_lru_pages(zone, sc, LRU_ACTIVE_ANON) +
  1448. zone_nr_lru_pages(zone, sc, LRU_INACTIVE_ANON);
  1449. file = zone_nr_lru_pages(zone, sc, LRU_ACTIVE_FILE) +
  1450. zone_nr_lru_pages(zone, sc, LRU_INACTIVE_FILE);
  1451. if (scanning_global_lru(sc)) {
  1452. free = zone_page_state(zone, NR_FREE_PAGES);
  1453. /* If we have very few page cache pages,
  1454. force-scan anon pages. */
  1455. if (unlikely(file + free <= high_wmark_pages(zone))) {
  1456. fraction[0] = 1;
  1457. fraction[1] = 0;
  1458. denominator = 1;
  1459. goto out;
  1460. }
  1461. }
  1462. /*
  1463. * With swappiness at 100, anonymous and file have the same priority.
  1464. * This scanning priority is essentially the inverse of IO cost.
  1465. */
  1466. anon_prio = sc->swappiness;
  1467. file_prio = 200 - sc->swappiness;
  1468. /*
  1469. * OK, so we have swap space and a fair amount of page cache
  1470. * pages. We use the recently rotated / recently scanned
  1471. * ratios to determine how valuable each cache is.
  1472. *
  1473. * Because workloads change over time (and to avoid overflow)
  1474. * we keep these statistics as a floating average, which ends
  1475. * up weighing recent references more than old ones.
  1476. *
  1477. * anon in [0], file in [1]
  1478. */
  1479. spin_lock_irq(&zone->lru_lock);
  1480. if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
  1481. reclaim_stat->recent_scanned[0] /= 2;
  1482. reclaim_stat->recent_rotated[0] /= 2;
  1483. }
  1484. if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
  1485. reclaim_stat->recent_scanned[1] /= 2;
  1486. reclaim_stat->recent_rotated[1] /= 2;
  1487. }
  1488. /*
  1489. * The amount of pressure on anon vs file pages is inversely
  1490. * proportional to the fraction of recently scanned pages on
  1491. * each list that were recently referenced and in active use.
  1492. */
  1493. ap = (anon_prio + 1) * (reclaim_stat->recent_scanned[0] + 1);
  1494. ap /= reclaim_stat->recent_rotated[0] + 1;
  1495. fp = (file_prio + 1) * (reclaim_stat->recent_scanned[1] + 1);
  1496. fp /= reclaim_stat->recent_rotated[1] + 1;
  1497. spin_unlock_irq(&zone->lru_lock);
  1498. fraction[0] = ap;
  1499. fraction[1] = fp;
  1500. denominator = ap + fp + 1;
  1501. out:
  1502. for_each_evictable_lru(l) {
  1503. int file = is_file_lru(l);
  1504. unsigned long scan;
  1505. scan = zone_nr_lru_pages(zone, sc, l);
  1506. if (priority || noswap) {
  1507. scan >>= priority;
  1508. scan = div64_u64(scan * fraction[file], denominator);
  1509. }
  1510. nr[l] = nr_scan_try_batch(scan,
  1511. &reclaim_stat->nr_saved_scan[l]);
  1512. }
  1513. }
  1514. static void set_lumpy_reclaim_mode(int priority, struct scan_control *sc)
  1515. {
  1516. /*
  1517. * If we need a large contiguous chunk of memory, or have
  1518. * trouble getting a small set of contiguous pages, we
  1519. * will reclaim both active and inactive pages.
  1520. */
  1521. if (sc->order > PAGE_ALLOC_COSTLY_ORDER)
  1522. sc->lumpy_reclaim_mode = 1;
  1523. else if (sc->order && priority < DEF_PRIORITY - 2)
  1524. sc->lumpy_reclaim_mode = 1;
  1525. else
  1526. sc->lumpy_reclaim_mode = 0;
  1527. }
  1528. /*
  1529. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  1530. */
  1531. static void shrink_zone(int priority, struct zone *zone,
  1532. struct scan_control *sc)
  1533. {
  1534. unsigned long nr[NR_LRU_LISTS];
  1535. unsigned long nr_to_scan;
  1536. enum lru_list l;
  1537. unsigned long nr_reclaimed = sc->nr_reclaimed;
  1538. unsigned long nr_to_reclaim = sc->nr_to_reclaim;
  1539. get_scan_count(zone, sc, nr, priority);
  1540. set_lumpy_reclaim_mode(priority, sc);
  1541. while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
  1542. nr[LRU_INACTIVE_FILE]) {
  1543. for_each_evictable_lru(l) {
  1544. if (nr[l]) {
  1545. nr_to_scan = min_t(unsigned long,
  1546. nr[l], SWAP_CLUSTER_MAX);
  1547. nr[l] -= nr_to_scan;
  1548. nr_reclaimed += shrink_list(l, nr_to_scan,
  1549. zone, sc, priority);
  1550. }
  1551. }
  1552. /*
  1553. * On large memory systems, scan >> priority can become
  1554. * really large. This is fine for the starting priority;
  1555. * we want to put equal scanning pressure on each zone.
  1556. * However, if the VM has a harder time of freeing pages,
  1557. * with multiple processes reclaiming pages, the total
  1558. * freeing target can get unreasonably large.
  1559. */
  1560. if (nr_reclaimed >= nr_to_reclaim && priority < DEF_PRIORITY)
  1561. break;
  1562. }
  1563. sc->nr_reclaimed = nr_reclaimed;
  1564. /*
  1565. * Even if we did not try to evict anon pages at all, we want to
  1566. * rebalance the anon lru active/inactive ratio.
  1567. */
  1568. if (inactive_anon_is_low(zone, sc))
  1569. shrink_active_list(SWAP_CLUSTER_MAX, zone, sc, priority, 0);
  1570. throttle_vm_writeout(sc->gfp_mask);
  1571. }
  1572. /*
  1573. * This is the direct reclaim path, for page-allocating processes. We only
  1574. * try to reclaim pages from zones which will satisfy the caller's allocation
  1575. * request.
  1576. *
  1577. * We reclaim from a zone even if that zone is over high_wmark_pages(zone).
  1578. * Because:
  1579. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  1580. * allocation or
  1581. * b) The target zone may be at high_wmark_pages(zone) but the lower zones
  1582. * must go *over* high_wmark_pages(zone) to satisfy the `incremental min'
  1583. * zone defense algorithm.
  1584. *
  1585. * If a zone is deemed to be full of pinned pages then just give it a light
  1586. * scan then give up on it.
  1587. */
  1588. static void shrink_zones(int priority, struct zonelist *zonelist,
  1589. struct scan_control *sc)
  1590. {
  1591. struct zoneref *z;
  1592. struct zone *zone;
  1593. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  1594. gfp_zone(sc->gfp_mask), sc->nodemask) {
  1595. if (!populated_zone(zone))
  1596. continue;
  1597. /*
  1598. * Take care memory controller reclaiming has small influence
  1599. * to global LRU.
  1600. */
  1601. if (scanning_global_lru(sc)) {
  1602. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1603. continue;
  1604. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1605. continue; /* Let kswapd poll it */
  1606. }
  1607. shrink_zone(priority, zone, sc);
  1608. }
  1609. }
  1610. static bool zone_reclaimable(struct zone *zone)
  1611. {
  1612. return zone->pages_scanned < zone_reclaimable_pages(zone) * 6;
  1613. }
  1614. /*
  1615. * As hibernation is going on, kswapd is freezed so that it can't mark
  1616. * the zone into all_unreclaimable. It can't handle OOM during hibernation.
  1617. * So let's check zone's unreclaimable in direct reclaim as well as kswapd.
  1618. */
  1619. static bool all_unreclaimable(struct zonelist *zonelist,
  1620. struct scan_control *sc)
  1621. {
  1622. struct zoneref *z;
  1623. struct zone *zone;
  1624. bool all_unreclaimable = true;
  1625. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  1626. gfp_zone(sc->gfp_mask), sc->nodemask) {
  1627. if (!populated_zone(zone))
  1628. continue;
  1629. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1630. continue;
  1631. if (zone_reclaimable(zone)) {
  1632. all_unreclaimable = false;
  1633. break;
  1634. }
  1635. }
  1636. return all_unreclaimable;
  1637. }
  1638. /*
  1639. * This is the main entry point to direct page reclaim.
  1640. *
  1641. * If a full scan of the inactive list fails to free enough memory then we
  1642. * are "out of memory" and something needs to be killed.
  1643. *
  1644. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  1645. * high - the zone may be full of dirty or under-writeback pages, which this
  1646. * caller can't do much about. We kick the writeback threads and take explicit
  1647. * naps in the hope that some of these pages can be written. But if the
  1648. * allocating task holds filesystem locks which prevent writeout this might not
  1649. * work, and the allocation attempt will fail.
  1650. *
  1651. * returns: 0, if no pages reclaimed
  1652. * else, the number of pages reclaimed
  1653. */
  1654. static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
  1655. struct scan_control *sc)
  1656. {
  1657. int priority;
  1658. unsigned long total_scanned = 0;
  1659. struct reclaim_state *reclaim_state = current->reclaim_state;
  1660. struct zoneref *z;
  1661. struct zone *zone;
  1662. unsigned long writeback_threshold;
  1663. get_mems_allowed();
  1664. delayacct_freepages_start();
  1665. if (scanning_global_lru(sc))
  1666. count_vm_event(ALLOCSTALL);
  1667. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1668. sc->nr_scanned = 0;
  1669. if (!priority)
  1670. disable_swap_token();
  1671. shrink_zones(priority, zonelist, sc);
  1672. /*
  1673. * Don't shrink slabs when reclaiming memory from
  1674. * over limit cgroups
  1675. */
  1676. if (scanning_global_lru(sc)) {
  1677. unsigned long lru_pages = 0;
  1678. for_each_zone_zonelist(zone, z, zonelist,
  1679. gfp_zone(sc->gfp_mask)) {
  1680. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1681. continue;
  1682. lru_pages += zone_reclaimable_pages(zone);
  1683. }
  1684. shrink_slab(sc->nr_scanned, sc->gfp_mask, lru_pages);
  1685. if (reclaim_state) {
  1686. sc->nr_reclaimed += reclaim_state->reclaimed_slab;
  1687. reclaim_state->reclaimed_slab = 0;
  1688. }
  1689. }
  1690. total_scanned += sc->nr_scanned;
  1691. if (sc->nr_reclaimed >= sc->nr_to_reclaim)
  1692. goto out;
  1693. /*
  1694. * Try to write back as many pages as we just scanned. This
  1695. * tends to cause slow streaming writers to write data to the
  1696. * disk smoothly, at the dirtying rate, which is nice. But
  1697. * that's undesirable in laptop mode, where we *want* lumpy
  1698. * writeout. So in laptop mode, write out the whole world.
  1699. */
  1700. writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2;
  1701. if (total_scanned > writeback_threshold) {
  1702. wakeup_flusher_threads(laptop_mode ? 0 : total_scanned);
  1703. sc->may_writepage = 1;
  1704. }
  1705. /* Take a nap, wait for some writeback to complete */
  1706. if (!sc->hibernation_mode && sc->nr_scanned &&
  1707. priority < DEF_PRIORITY - 2)
  1708. congestion_wait(BLK_RW_ASYNC, HZ/10);
  1709. }
  1710. out:
  1711. delayacct_freepages_end();
  1712. put_mems_allowed();
  1713. if (sc->nr_reclaimed)
  1714. return sc->nr_reclaimed;
  1715. /* top priority shrink_zones still had more to do? don't OOM, then */
  1716. if (scanning_global_lru(sc) && !all_unreclaimable(zonelist, sc))
  1717. return 1;
  1718. return 0;
  1719. }
  1720. unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
  1721. gfp_t gfp_mask, nodemask_t *nodemask)
  1722. {
  1723. unsigned long nr_reclaimed;
  1724. struct scan_control sc = {
  1725. .gfp_mask = gfp_mask,
  1726. .may_writepage = !laptop_mode,
  1727. .nr_to_reclaim = SWAP_CLUSTER_MAX,
  1728. .may_unmap = 1,
  1729. .may_swap = 1,
  1730. .swappiness = vm_swappiness,
  1731. .order = order,
  1732. .mem_cgroup = NULL,
  1733. .nodemask = nodemask,
  1734. };
  1735. trace_mm_vmscan_direct_reclaim_begin(order,
  1736. sc.may_writepage,
  1737. gfp_mask);
  1738. nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
  1739. trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
  1740. return nr_reclaimed;
  1741. }
  1742. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  1743. unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *mem,
  1744. gfp_t gfp_mask, bool noswap,
  1745. unsigned int swappiness,
  1746. struct zone *zone)
  1747. {
  1748. struct scan_control sc = {
  1749. .nr_to_reclaim = SWAP_CLUSTER_MAX,
  1750. .may_writepage = !laptop_mode,
  1751. .may_unmap = 1,
  1752. .may_swap = !noswap,
  1753. .swappiness = swappiness,
  1754. .order = 0,
  1755. .mem_cgroup = mem,
  1756. };
  1757. sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
  1758. (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
  1759. trace_mm_vmscan_memcg_softlimit_reclaim_begin(0,
  1760. sc.may_writepage,
  1761. sc.gfp_mask);
  1762. /*
  1763. * NOTE: Although we can get the priority field, using it
  1764. * here is not a good idea, since it limits the pages we can scan.
  1765. * if we don't reclaim here, the shrink_zone from balance_pgdat
  1766. * will pick up pages from other mem cgroup's as well. We hack
  1767. * the priority and make it zero.
  1768. */
  1769. shrink_zone(0, zone, &sc);
  1770. trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
  1771. return sc.nr_reclaimed;
  1772. }
  1773. unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont,
  1774. gfp_t gfp_mask,
  1775. bool noswap,
  1776. unsigned int swappiness)
  1777. {
  1778. struct zonelist *zonelist;
  1779. unsigned long nr_reclaimed;
  1780. struct scan_control sc = {
  1781. .may_writepage = !laptop_mode,
  1782. .may_unmap = 1,
  1783. .may_swap = !noswap,
  1784. .nr_to_reclaim = SWAP_CLUSTER_MAX,
  1785. .swappiness = swappiness,
  1786. .order = 0,
  1787. .mem_cgroup = mem_cont,
  1788. .nodemask = NULL, /* we don't care the placement */
  1789. };
  1790. sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
  1791. (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
  1792. zonelist = NODE_DATA(numa_node_id())->node_zonelists;
  1793. trace_mm_vmscan_memcg_reclaim_begin(0,
  1794. sc.may_writepage,
  1795. sc.gfp_mask);
  1796. nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
  1797. trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
  1798. return nr_reclaimed;
  1799. }
  1800. #endif
  1801. /* is kswapd sleeping prematurely? */
  1802. static int sleeping_prematurely(pg_data_t *pgdat, int order, long remaining)
  1803. {
  1804. int i;
  1805. /* If a direct reclaimer woke kswapd within HZ/10, it's premature */
  1806. if (remaining)
  1807. return 1;
  1808. /* If after HZ/10, a zone is below the high mark, it's premature */
  1809. for (i = 0; i < pgdat->nr_zones; i++) {
  1810. struct zone *zone = pgdat->node_zones + i;
  1811. if (!populated_zone(zone))
  1812. continue;
  1813. if (zone->all_unreclaimable)
  1814. continue;
  1815. if (!zone_watermark_ok(zone, order, high_wmark_pages(zone),
  1816. 0, 0))
  1817. return 1;
  1818. }
  1819. return 0;
  1820. }
  1821. /*
  1822. * For kswapd, balance_pgdat() will work across all this node's zones until
  1823. * they are all at high_wmark_pages(zone).
  1824. *
  1825. * Returns the number of pages which were actually freed.
  1826. *
  1827. * There is special handling here for zones which are full of pinned pages.
  1828. * This can happen if the pages are all mlocked, or if they are all used by
  1829. * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
  1830. * What we do is to detect the case where all pages in the zone have been
  1831. * scanned twice and there has been zero successful reclaim. Mark the zone as
  1832. * dead and from now on, only perform a short scan. Basically we're polling
  1833. * the zone for when the problem goes away.
  1834. *
  1835. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  1836. * zones which have free_pages > high_wmark_pages(zone), but once a zone is
  1837. * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the
  1838. * lower zones regardless of the number of free pages in the lower zones. This
  1839. * interoperates with the page allocator fallback scheme to ensure that aging
  1840. * of pages is balanced across the zones.
  1841. */
  1842. static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
  1843. {
  1844. int all_zones_ok;
  1845. int priority;
  1846. int i;
  1847. unsigned long total_scanned;
  1848. struct reclaim_state *reclaim_state = current->reclaim_state;
  1849. struct scan_control sc = {
  1850. .gfp_mask = GFP_KERNEL,
  1851. .may_unmap = 1,
  1852. .may_swap = 1,
  1853. /*
  1854. * kswapd doesn't want to be bailed out while reclaim. because
  1855. * we want to put equal scanning pressure on each zone.
  1856. */
  1857. .nr_to_reclaim = ULONG_MAX,
  1858. .swappiness = vm_swappiness,
  1859. .order = order,
  1860. .mem_cgroup = NULL,
  1861. };
  1862. loop_again:
  1863. total_scanned = 0;
  1864. sc.nr_reclaimed = 0;
  1865. sc.may_writepage = !laptop_mode;
  1866. count_vm_event(PAGEOUTRUN);
  1867. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1868. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  1869. unsigned long lru_pages = 0;
  1870. int has_under_min_watermark_zone = 0;
  1871. /* The swap token gets in the way of swapout... */
  1872. if (!priority)
  1873. disable_swap_token();
  1874. all_zones_ok = 1;
  1875. /*
  1876. * Scan in the highmem->dma direction for the highest
  1877. * zone which needs scanning
  1878. */
  1879. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  1880. struct zone *zone = pgdat->node_zones + i;
  1881. if (!populated_zone(zone))
  1882. continue;
  1883. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1884. continue;
  1885. /*
  1886. * Do some background aging of the anon list, to give
  1887. * pages a chance to be referenced before reclaiming.
  1888. */
  1889. if (inactive_anon_is_low(zone, &sc))
  1890. shrink_active_list(SWAP_CLUSTER_MAX, zone,
  1891. &sc, priority, 0);
  1892. if (!zone_watermark_ok(zone, order,
  1893. high_wmark_pages(zone), 0, 0)) {
  1894. end_zone = i;
  1895. break;
  1896. }
  1897. }
  1898. if (i < 0)
  1899. goto out;
  1900. for (i = 0; i <= end_zone; i++) {
  1901. struct zone *zone = pgdat->node_zones + i;
  1902. lru_pages += zone_reclaimable_pages(zone);
  1903. }
  1904. /*
  1905. * Now scan the zone in the dma->highmem direction, stopping
  1906. * at the last zone which needs scanning.
  1907. *
  1908. * We do this because the page allocator works in the opposite
  1909. * direction. This prevents the page allocator from allocating
  1910. * pages behind kswapd's direction of progress, which would
  1911. * cause too much scanning of the lower zones.
  1912. */
  1913. for (i = 0; i <= end_zone; i++) {
  1914. struct zone *zone = pgdat->node_zones + i;
  1915. int nr_slab;
  1916. if (!populated_zone(zone))
  1917. continue;
  1918. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1919. continue;
  1920. sc.nr_scanned = 0;
  1921. /*
  1922. * Call soft limit reclaim before calling shrink_zone.
  1923. * For now we ignore the return value
  1924. */
  1925. mem_cgroup_soft_limit_reclaim(zone, order, sc.gfp_mask);
  1926. /*
  1927. * We put equal pressure on every zone, unless one
  1928. * zone has way too many pages free already.
  1929. */
  1930. if (!zone_watermark_ok(zone, order,
  1931. 8*high_wmark_pages(zone), end_zone, 0))
  1932. shrink_zone(priority, zone, &sc);
  1933. reclaim_state->reclaimed_slab = 0;
  1934. nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
  1935. lru_pages);
  1936. sc.nr_reclaimed += reclaim_state->reclaimed_slab;
  1937. total_scanned += sc.nr_scanned;
  1938. if (zone->all_unreclaimable)
  1939. continue;
  1940. if (nr_slab == 0 && !zone_reclaimable(zone))
  1941. zone->all_unreclaimable = 1;
  1942. /*
  1943. * If we've done a decent amount of scanning and
  1944. * the reclaim ratio is low, start doing writepage
  1945. * even in laptop mode
  1946. */
  1947. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  1948. total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2)
  1949. sc.may_writepage = 1;
  1950. if (!zone_watermark_ok(zone, order,
  1951. high_wmark_pages(zone), end_zone, 0)) {
  1952. all_zones_ok = 0;
  1953. /*
  1954. * We are still under min water mark. This
  1955. * means that we have a GFP_ATOMIC allocation
  1956. * failure risk. Hurry up!
  1957. */
  1958. if (!zone_watermark_ok(zone, order,
  1959. min_wmark_pages(zone), end_zone, 0))
  1960. has_under_min_watermark_zone = 1;
  1961. }
  1962. }
  1963. if (all_zones_ok)
  1964. break; /* kswapd: all done */
  1965. /*
  1966. * OK, kswapd is getting into trouble. Take a nap, then take
  1967. * another pass across the zones.
  1968. */
  1969. if (total_scanned && (priority < DEF_PRIORITY - 2)) {
  1970. if (has_under_min_watermark_zone)
  1971. count_vm_event(KSWAPD_SKIP_CONGESTION_WAIT);
  1972. else
  1973. congestion_wait(BLK_RW_ASYNC, HZ/10);
  1974. }
  1975. /*
  1976. * We do this so kswapd doesn't build up large priorities for
  1977. * example when it is freeing in parallel with allocators. It
  1978. * matches the direct reclaim path behaviour in terms of impact
  1979. * on zone->*_priority.
  1980. */
  1981. if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX)
  1982. break;
  1983. }
  1984. out:
  1985. if (!all_zones_ok) {
  1986. cond_resched();
  1987. try_to_freeze();
  1988. /*
  1989. * Fragmentation may mean that the system cannot be
  1990. * rebalanced for high-order allocations in all zones.
  1991. * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX,
  1992. * it means the zones have been fully scanned and are still
  1993. * not balanced. For high-order allocations, there is
  1994. * little point trying all over again as kswapd may
  1995. * infinite loop.
  1996. *
  1997. * Instead, recheck all watermarks at order-0 as they
  1998. * are the most important. If watermarks are ok, kswapd will go
  1999. * back to sleep. High-order users can still perform direct
  2000. * reclaim if they wish.
  2001. */
  2002. if (sc.nr_reclaimed < SWAP_CLUSTER_MAX)
  2003. order = sc.order = 0;
  2004. goto loop_again;
  2005. }
  2006. return sc.nr_reclaimed;
  2007. }
  2008. /*
  2009. * The background pageout daemon, started as a kernel thread
  2010. * from the init process.
  2011. *
  2012. * This basically trickles out pages so that we have _some_
  2013. * free memory available even if there is no other activity
  2014. * that frees anything up. This is needed for things like routing
  2015. * etc, where we otherwise might have all activity going on in
  2016. * asynchronous contexts that cannot page things out.
  2017. *
  2018. * If there are applications that are active memory-allocators
  2019. * (most normal use), this basically shouldn't matter.
  2020. */
  2021. static int kswapd(void *p)
  2022. {
  2023. unsigned long order;
  2024. pg_data_t *pgdat = (pg_data_t*)p;
  2025. struct task_struct *tsk = current;
  2026. DEFINE_WAIT(wait);
  2027. struct reclaim_state reclaim_state = {
  2028. .reclaimed_slab = 0,
  2029. };
  2030. const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
  2031. lockdep_set_current_reclaim_state(GFP_KERNEL);
  2032. if (!cpumask_empty(cpumask))
  2033. set_cpus_allowed_ptr(tsk, cpumask);
  2034. current->reclaim_state = &reclaim_state;
  2035. /*
  2036. * Tell the memory management that we're a "memory allocator",
  2037. * and that if we need more memory we should get access to it
  2038. * regardless (see "__alloc_pages()"). "kswapd" should
  2039. * never get caught in the normal page freeing logic.
  2040. *
  2041. * (Kswapd normally doesn't need memory anyway, but sometimes
  2042. * you need a small amount of memory in order to be able to
  2043. * page out something else, and this flag essentially protects
  2044. * us from recursively trying to free more memory as we're
  2045. * trying to free the first piece of memory in the first place).
  2046. */
  2047. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  2048. set_freezable();
  2049. order = 0;
  2050. for ( ; ; ) {
  2051. unsigned long new_order;
  2052. int ret;
  2053. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  2054. new_order = pgdat->kswapd_max_order;
  2055. pgdat->kswapd_max_order = 0;
  2056. if (order < new_order) {
  2057. /*
  2058. * Don't sleep if someone wants a larger 'order'
  2059. * allocation
  2060. */
  2061. order = new_order;
  2062. } else {
  2063. if (!freezing(current) && !kthread_should_stop()) {
  2064. long remaining = 0;
  2065. /* Try to sleep for a short interval */
  2066. if (!sleeping_prematurely(pgdat, order, remaining)) {
  2067. remaining = schedule_timeout(HZ/10);
  2068. finish_wait(&pgdat->kswapd_wait, &wait);
  2069. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  2070. }
  2071. /*
  2072. * After a short sleep, check if it was a
  2073. * premature sleep. If not, then go fully
  2074. * to sleep until explicitly woken up
  2075. */
  2076. if (!sleeping_prematurely(pgdat, order, remaining)) {
  2077. trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
  2078. schedule();
  2079. } else {
  2080. if (remaining)
  2081. count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
  2082. else
  2083. count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
  2084. }
  2085. }
  2086. order = pgdat->kswapd_max_order;
  2087. }
  2088. finish_wait(&pgdat->kswapd_wait, &wait);
  2089. ret = try_to_freeze();
  2090. if (kthread_should_stop())
  2091. break;
  2092. /*
  2093. * We can speed up thawing tasks if we don't call balance_pgdat
  2094. * after returning from the refrigerator
  2095. */
  2096. if (!ret) {
  2097. trace_mm_vmscan_kswapd_wake(pgdat->node_id, order);
  2098. balance_pgdat(pgdat, order);
  2099. }
  2100. }
  2101. return 0;
  2102. }
  2103. /*
  2104. * A zone is low on free memory, so wake its kswapd task to service it.
  2105. */
  2106. void wakeup_kswapd(struct zone *zone, int order)
  2107. {
  2108. pg_data_t *pgdat;
  2109. if (!populated_zone(zone))
  2110. return;
  2111. pgdat = zone->zone_pgdat;
  2112. if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 0))
  2113. return;
  2114. if (pgdat->kswapd_max_order < order)
  2115. pgdat->kswapd_max_order = order;
  2116. trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order);
  2117. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  2118. return;
  2119. if (!waitqueue_active(&pgdat->kswapd_wait))
  2120. return;
  2121. wake_up_interruptible(&pgdat->kswapd_wait);
  2122. }
  2123. /*
  2124. * The reclaimable count would be mostly accurate.
  2125. * The less reclaimable pages may be
  2126. * - mlocked pages, which will be moved to unevictable list when encountered
  2127. * - mapped pages, which may require several travels to be reclaimed
  2128. * - dirty pages, which is not "instantly" reclaimable
  2129. */
  2130. unsigned long global_reclaimable_pages(void)
  2131. {
  2132. int nr;
  2133. nr = global_page_state(NR_ACTIVE_FILE) +
  2134. global_page_state(NR_INACTIVE_FILE);
  2135. if (nr_swap_pages > 0)
  2136. nr += global_page_state(NR_ACTIVE_ANON) +
  2137. global_page_state(NR_INACTIVE_ANON);
  2138. return nr;
  2139. }
  2140. unsigned long zone_reclaimable_pages(struct zone *zone)
  2141. {
  2142. int nr;
  2143. nr = zone_page_state(zone, NR_ACTIVE_FILE) +
  2144. zone_page_state(zone, NR_INACTIVE_FILE);
  2145. if (nr_swap_pages > 0)
  2146. nr += zone_page_state(zone, NR_ACTIVE_ANON) +
  2147. zone_page_state(zone, NR_INACTIVE_ANON);
  2148. return nr;
  2149. }
  2150. #ifdef CONFIG_HIBERNATION
  2151. /*
  2152. * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
  2153. * freed pages.
  2154. *
  2155. * Rather than trying to age LRUs the aim is to preserve the overall
  2156. * LRU order by reclaiming preferentially
  2157. * inactive > active > active referenced > active mapped
  2158. */
  2159. unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
  2160. {
  2161. struct reclaim_state reclaim_state;
  2162. struct scan_control sc = {
  2163. .gfp_mask = GFP_HIGHUSER_MOVABLE,
  2164. .may_swap = 1,
  2165. .may_unmap = 1,
  2166. .may_writepage = 1,
  2167. .nr_to_reclaim = nr_to_reclaim,
  2168. .hibernation_mode = 1,
  2169. .swappiness = vm_swappiness,
  2170. .order = 0,
  2171. };
  2172. struct zonelist * zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
  2173. struct task_struct *p = current;
  2174. unsigned long nr_reclaimed;
  2175. p->flags |= PF_MEMALLOC;
  2176. lockdep_set_current_reclaim_state(sc.gfp_mask);
  2177. reclaim_state.reclaimed_slab = 0;
  2178. p->reclaim_state = &reclaim_state;
  2179. nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
  2180. p->reclaim_state = NULL;
  2181. lockdep_clear_current_reclaim_state();
  2182. p->flags &= ~PF_MEMALLOC;
  2183. return nr_reclaimed;
  2184. }
  2185. #endif /* CONFIG_HIBERNATION */
  2186. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  2187. not required for correctness. So if the last cpu in a node goes
  2188. away, we get changed to run anywhere: as the first one comes back,
  2189. restore their cpu bindings. */
  2190. static int __devinit cpu_callback(struct notifier_block *nfb,
  2191. unsigned long action, void *hcpu)
  2192. {
  2193. int nid;
  2194. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
  2195. for_each_node_state(nid, N_HIGH_MEMORY) {
  2196. pg_data_t *pgdat = NODE_DATA(nid);
  2197. const struct cpumask *mask;
  2198. mask = cpumask_of_node(pgdat->node_id);
  2199. if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
  2200. /* One of our CPUs online: restore mask */
  2201. set_cpus_allowed_ptr(pgdat->kswapd, mask);
  2202. }
  2203. }
  2204. return NOTIFY_OK;
  2205. }
  2206. /*
  2207. * This kswapd start function will be called by init and node-hot-add.
  2208. * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
  2209. */
  2210. int kswapd_run(int nid)
  2211. {
  2212. pg_data_t *pgdat = NODE_DATA(nid);
  2213. int ret = 0;
  2214. if (pgdat->kswapd)
  2215. return 0;
  2216. pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
  2217. if (IS_ERR(pgdat->kswapd)) {
  2218. /* failure at boot is fatal */
  2219. BUG_ON(system_state == SYSTEM_BOOTING);
  2220. printk("Failed to start kswapd on node %d\n",nid);
  2221. ret = -1;
  2222. }
  2223. return ret;
  2224. }
  2225. /*
  2226. * Called by memory hotplug when all memory in a node is offlined.
  2227. */
  2228. void kswapd_stop(int nid)
  2229. {
  2230. struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
  2231. if (kswapd)
  2232. kthread_stop(kswapd);
  2233. }
  2234. static int __init kswapd_init(void)
  2235. {
  2236. int nid;
  2237. swap_setup();
  2238. for_each_node_state(nid, N_HIGH_MEMORY)
  2239. kswapd_run(nid);
  2240. hotcpu_notifier(cpu_callback, 0);
  2241. return 0;
  2242. }
  2243. module_init(kswapd_init)
  2244. #ifdef CONFIG_NUMA
  2245. /*
  2246. * Zone reclaim mode
  2247. *
  2248. * If non-zero call zone_reclaim when the number of free pages falls below
  2249. * the watermarks.
  2250. */
  2251. int zone_reclaim_mode __read_mostly;
  2252. #define RECLAIM_OFF 0
  2253. #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */
  2254. #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
  2255. #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
  2256. /*
  2257. * Priority for ZONE_RECLAIM. This determines the fraction of pages
  2258. * of a node considered for each zone_reclaim. 4 scans 1/16th of
  2259. * a zone.
  2260. */
  2261. #define ZONE_RECLAIM_PRIORITY 4
  2262. /*
  2263. * Percentage of pages in a zone that must be unmapped for zone_reclaim to
  2264. * occur.
  2265. */
  2266. int sysctl_min_unmapped_ratio = 1;
  2267. /*
  2268. * If the number of slab pages in a zone grows beyond this percentage then
  2269. * slab reclaim needs to occur.
  2270. */
  2271. int sysctl_min_slab_ratio = 5;
  2272. static inline unsigned long zone_unmapped_file_pages(struct zone *zone)
  2273. {
  2274. unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED);
  2275. unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) +
  2276. zone_page_state(zone, NR_ACTIVE_FILE);
  2277. /*
  2278. * It's possible for there to be more file mapped pages than
  2279. * accounted for by the pages on the file LRU lists because
  2280. * tmpfs pages accounted for as ANON can also be FILE_MAPPED
  2281. */
  2282. return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
  2283. }
  2284. /* Work out how many page cache pages we can reclaim in this reclaim_mode */
  2285. static long zone_pagecache_reclaimable(struct zone *zone)
  2286. {
  2287. long nr_pagecache_reclaimable;
  2288. long delta = 0;
  2289. /*
  2290. * If RECLAIM_SWAP is set, then all file pages are considered
  2291. * potentially reclaimable. Otherwise, we have to worry about
  2292. * pages like swapcache and zone_unmapped_file_pages() provides
  2293. * a better estimate
  2294. */
  2295. if (zone_reclaim_mode & RECLAIM_SWAP)
  2296. nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES);
  2297. else
  2298. nr_pagecache_reclaimable = zone_unmapped_file_pages(zone);
  2299. /* If we can't clean pages, remove dirty pages from consideration */
  2300. if (!(zone_reclaim_mode & RECLAIM_WRITE))
  2301. delta += zone_page_state(zone, NR_FILE_DIRTY);
  2302. /* Watch for any possible underflows due to delta */
  2303. if (unlikely(delta > nr_pagecache_reclaimable))
  2304. delta = nr_pagecache_reclaimable;
  2305. return nr_pagecache_reclaimable - delta;
  2306. }
  2307. /*
  2308. * Try to free up some pages from this zone through reclaim.
  2309. */
  2310. static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  2311. {
  2312. /* Minimum pages needed in order to stay on node */
  2313. const unsigned long nr_pages = 1 << order;
  2314. struct task_struct *p = current;
  2315. struct reclaim_state reclaim_state;
  2316. int priority;
  2317. struct scan_control sc = {
  2318. .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
  2319. .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP),
  2320. .may_swap = 1,
  2321. .nr_to_reclaim = max_t(unsigned long, nr_pages,
  2322. SWAP_CLUSTER_MAX),
  2323. .gfp_mask = gfp_mask,
  2324. .swappiness = vm_swappiness,
  2325. .order = order,
  2326. };
  2327. unsigned long nr_slab_pages0, nr_slab_pages1;
  2328. cond_resched();
  2329. /*
  2330. * We need to be able to allocate from the reserves for RECLAIM_SWAP
  2331. * and we also need to be able to write out pages for RECLAIM_WRITE
  2332. * and RECLAIM_SWAP.
  2333. */
  2334. p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
  2335. lockdep_set_current_reclaim_state(gfp_mask);
  2336. reclaim_state.reclaimed_slab = 0;
  2337. p->reclaim_state = &reclaim_state;
  2338. if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) {
  2339. /*
  2340. * Free memory by calling shrink zone with increasing
  2341. * priorities until we have enough memory freed.
  2342. */
  2343. priority = ZONE_RECLAIM_PRIORITY;
  2344. do {
  2345. shrink_zone(priority, zone, &sc);
  2346. priority--;
  2347. } while (priority >= 0 && sc.nr_reclaimed < nr_pages);
  2348. }
  2349. nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  2350. if (nr_slab_pages0 > zone->min_slab_pages) {
  2351. /*
  2352. * shrink_slab() does not currently allow us to determine how
  2353. * many pages were freed in this zone. So we take the current
  2354. * number of slab pages and shake the slab until it is reduced
  2355. * by the same nr_pages that we used for reclaiming unmapped
  2356. * pages.
  2357. *
  2358. * Note that shrink_slab will free memory on all zones and may
  2359. * take a long time.
  2360. */
  2361. for (;;) {
  2362. unsigned long lru_pages = zone_reclaimable_pages(zone);
  2363. /* No reclaimable slab or very low memory pressure */
  2364. if (!shrink_slab(sc.nr_scanned, gfp_mask, lru_pages))
  2365. break;
  2366. /* Freed enough memory */
  2367. nr_slab_pages1 = zone_page_state(zone,
  2368. NR_SLAB_RECLAIMABLE);
  2369. if (nr_slab_pages1 + nr_pages <= nr_slab_pages0)
  2370. break;
  2371. }
  2372. /*
  2373. * Update nr_reclaimed by the number of slab pages we
  2374. * reclaimed from this zone.
  2375. */
  2376. nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  2377. if (nr_slab_pages1 < nr_slab_pages0)
  2378. sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1;
  2379. }
  2380. p->reclaim_state = NULL;
  2381. current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
  2382. lockdep_clear_current_reclaim_state();
  2383. return sc.nr_reclaimed >= nr_pages;
  2384. }
  2385. int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  2386. {
  2387. int node_id;
  2388. int ret;
  2389. /*
  2390. * Zone reclaim reclaims unmapped file backed pages and
  2391. * slab pages if we are over the defined limits.
  2392. *
  2393. * A small portion of unmapped file backed pages is needed for
  2394. * file I/O otherwise pages read by file I/O will be immediately
  2395. * thrown out if the zone is overallocated. So we do not reclaim
  2396. * if less than a specified percentage of the zone is used by
  2397. * unmapped file backed pages.
  2398. */
  2399. if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages &&
  2400. zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages)
  2401. return ZONE_RECLAIM_FULL;
  2402. if (zone->all_unreclaimable)
  2403. return ZONE_RECLAIM_FULL;
  2404. /*
  2405. * Do not scan if the allocation should not be delayed.
  2406. */
  2407. if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
  2408. return ZONE_RECLAIM_NOSCAN;
  2409. /*
  2410. * Only run zone reclaim on the local zone or on zones that do not
  2411. * have associated processors. This will favor the local processor
  2412. * over remote processors and spread off node memory allocations
  2413. * as wide as possible.
  2414. */
  2415. node_id = zone_to_nid(zone);
  2416. if (node_state(node_id, N_CPU) && node_id != numa_node_id())
  2417. return ZONE_RECLAIM_NOSCAN;
  2418. if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
  2419. return ZONE_RECLAIM_NOSCAN;
  2420. ret = __zone_reclaim(zone, gfp_mask, order);
  2421. zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
  2422. if (!ret)
  2423. count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
  2424. return ret;
  2425. }
  2426. #endif
  2427. /*
  2428. * page_evictable - test whether a page is evictable
  2429. * @page: the page to test
  2430. * @vma: the VMA in which the page is or will be mapped, may be NULL
  2431. *
  2432. * Test whether page is evictable--i.e., should be placed on active/inactive
  2433. * lists vs unevictable list. The vma argument is !NULL when called from the
  2434. * fault path to determine how to instantate a new page.
  2435. *
  2436. * Reasons page might not be evictable:
  2437. * (1) page's mapping marked unevictable
  2438. * (2) page is part of an mlocked VMA
  2439. *
  2440. */
  2441. int page_evictable(struct page *page, struct vm_area_struct *vma)
  2442. {
  2443. if (mapping_unevictable(page_mapping(page)))
  2444. return 0;
  2445. if (PageMlocked(page) || (vma && is_mlocked_vma(vma, page)))
  2446. return 0;
  2447. return 1;
  2448. }
  2449. /**
  2450. * check_move_unevictable_page - check page for evictability and move to appropriate zone lru list
  2451. * @page: page to check evictability and move to appropriate lru list
  2452. * @zone: zone page is in
  2453. *
  2454. * Checks a page for evictability and moves the page to the appropriate
  2455. * zone lru list.
  2456. *
  2457. * Restrictions: zone->lru_lock must be held, page must be on LRU and must
  2458. * have PageUnevictable set.
  2459. */
  2460. static void check_move_unevictable_page(struct page *page, struct zone *zone)
  2461. {
  2462. VM_BUG_ON(PageActive(page));
  2463. retry:
  2464. ClearPageUnevictable(page);
  2465. if (page_evictable(page, NULL)) {
  2466. enum lru_list l = page_lru_base_type(page);
  2467. __dec_zone_state(zone, NR_UNEVICTABLE);
  2468. list_move(&page->lru, &zone->lru[l].list);
  2469. mem_cgroup_move_lists(page, LRU_UNEVICTABLE, l);
  2470. __inc_zone_state(zone, NR_INACTIVE_ANON + l);
  2471. __count_vm_event(UNEVICTABLE_PGRESCUED);
  2472. } else {
  2473. /*
  2474. * rotate unevictable list
  2475. */
  2476. SetPageUnevictable(page);
  2477. list_move(&page->lru, &zone->lru[LRU_UNEVICTABLE].list);
  2478. mem_cgroup_rotate_lru_list(page, LRU_UNEVICTABLE);
  2479. if (page_evictable(page, NULL))
  2480. goto retry;
  2481. }
  2482. }
  2483. /**
  2484. * scan_mapping_unevictable_pages - scan an address space for evictable pages
  2485. * @mapping: struct address_space to scan for evictable pages
  2486. *
  2487. * Scan all pages in mapping. Check unevictable pages for
  2488. * evictability and move them to the appropriate zone lru list.
  2489. */
  2490. void scan_mapping_unevictable_pages(struct address_space *mapping)
  2491. {
  2492. pgoff_t next = 0;
  2493. pgoff_t end = (i_size_read(mapping->host) + PAGE_CACHE_SIZE - 1) >>
  2494. PAGE_CACHE_SHIFT;
  2495. struct zone *zone;
  2496. struct pagevec pvec;
  2497. if (mapping->nrpages == 0)
  2498. return;
  2499. pagevec_init(&pvec, 0);
  2500. while (next < end &&
  2501. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  2502. int i;
  2503. int pg_scanned = 0;
  2504. zone = NULL;
  2505. for (i = 0; i < pagevec_count(&pvec); i++) {
  2506. struct page *page = pvec.pages[i];
  2507. pgoff_t page_index = page->index;
  2508. struct zone *pagezone = page_zone(page);
  2509. pg_scanned++;
  2510. if (page_index > next)
  2511. next = page_index;
  2512. next++;
  2513. if (pagezone != zone) {
  2514. if (zone)
  2515. spin_unlock_irq(&zone->lru_lock);
  2516. zone = pagezone;
  2517. spin_lock_irq(&zone->lru_lock);
  2518. }
  2519. if (PageLRU(page) && PageUnevictable(page))
  2520. check_move_unevictable_page(page, zone);
  2521. }
  2522. if (zone)
  2523. spin_unlock_irq(&zone->lru_lock);
  2524. pagevec_release(&pvec);
  2525. count_vm_events(UNEVICTABLE_PGSCANNED, pg_scanned);
  2526. }
  2527. }
  2528. /**
  2529. * scan_zone_unevictable_pages - check unevictable list for evictable pages
  2530. * @zone - zone of which to scan the unevictable list
  2531. *
  2532. * Scan @zone's unevictable LRU lists to check for pages that have become
  2533. * evictable. Move those that have to @zone's inactive list where they
  2534. * become candidates for reclaim, unless shrink_inactive_zone() decides
  2535. * to reactivate them. Pages that are still unevictable are rotated
  2536. * back onto @zone's unevictable list.
  2537. */
  2538. #define SCAN_UNEVICTABLE_BATCH_SIZE 16UL /* arbitrary lock hold batch size */
  2539. static void scan_zone_unevictable_pages(struct zone *zone)
  2540. {
  2541. struct list_head *l_unevictable = &zone->lru[LRU_UNEVICTABLE].list;
  2542. unsigned long scan;
  2543. unsigned long nr_to_scan = zone_page_state(zone, NR_UNEVICTABLE);
  2544. while (nr_to_scan > 0) {
  2545. unsigned long batch_size = min(nr_to_scan,
  2546. SCAN_UNEVICTABLE_BATCH_SIZE);
  2547. spin_lock_irq(&zone->lru_lock);
  2548. for (scan = 0; scan < batch_size; scan++) {
  2549. struct page *page = lru_to_page(l_unevictable);
  2550. if (!trylock_page(page))
  2551. continue;
  2552. prefetchw_prev_lru_page(page, l_unevictable, flags);
  2553. if (likely(PageLRU(page) && PageUnevictable(page)))
  2554. check_move_unevictable_page(page, zone);
  2555. unlock_page(page);
  2556. }
  2557. spin_unlock_irq(&zone->lru_lock);
  2558. nr_to_scan -= batch_size;
  2559. }
  2560. }
  2561. /**
  2562. * scan_all_zones_unevictable_pages - scan all unevictable lists for evictable pages
  2563. *
  2564. * A really big hammer: scan all zones' unevictable LRU lists to check for
  2565. * pages that have become evictable. Move those back to the zones'
  2566. * inactive list where they become candidates for reclaim.
  2567. * This occurs when, e.g., we have unswappable pages on the unevictable lists,
  2568. * and we add swap to the system. As such, it runs in the context of a task
  2569. * that has possibly/probably made some previously unevictable pages
  2570. * evictable.
  2571. */
  2572. static void scan_all_zones_unevictable_pages(void)
  2573. {
  2574. struct zone *zone;
  2575. for_each_zone(zone) {
  2576. scan_zone_unevictable_pages(zone);
  2577. }
  2578. }
  2579. /*
  2580. * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of
  2581. * all nodes' unevictable lists for evictable pages
  2582. */
  2583. unsigned long scan_unevictable_pages;
  2584. int scan_unevictable_handler(struct ctl_table *table, int write,
  2585. void __user *buffer,
  2586. size_t *length, loff_t *ppos)
  2587. {
  2588. proc_doulongvec_minmax(table, write, buffer, length, ppos);
  2589. if (write && *(unsigned long *)table->data)
  2590. scan_all_zones_unevictable_pages();
  2591. scan_unevictable_pages = 0;
  2592. return 0;
  2593. }
  2594. #ifdef CONFIG_NUMA
  2595. /*
  2596. * per node 'scan_unevictable_pages' attribute. On demand re-scan of
  2597. * a specified node's per zone unevictable lists for evictable pages.
  2598. */
  2599. static ssize_t read_scan_unevictable_node(struct sys_device *dev,
  2600. struct sysdev_attribute *attr,
  2601. char *buf)
  2602. {
  2603. return sprintf(buf, "0\n"); /* always zero; should fit... */
  2604. }
  2605. static ssize_t write_scan_unevictable_node(struct sys_device *dev,
  2606. struct sysdev_attribute *attr,
  2607. const char *buf, size_t count)
  2608. {
  2609. struct zone *node_zones = NODE_DATA(dev->id)->node_zones;
  2610. struct zone *zone;
  2611. unsigned long res;
  2612. unsigned long req = strict_strtoul(buf, 10, &res);
  2613. if (!req)
  2614. return 1; /* zero is no-op */
  2615. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  2616. if (!populated_zone(zone))
  2617. continue;
  2618. scan_zone_unevictable_pages(zone);
  2619. }
  2620. return 1;
  2621. }
  2622. static SYSDEV_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR,
  2623. read_scan_unevictable_node,
  2624. write_scan_unevictable_node);
  2625. int scan_unevictable_register_node(struct node *node)
  2626. {
  2627. return sysdev_create_file(&node->sysdev, &attr_scan_unevictable_pages);
  2628. }
  2629. void scan_unevictable_unregister_node(struct node *node)
  2630. {
  2631. sysdev_remove_file(&node->sysdev, &attr_scan_unevictable_pages);
  2632. }
  2633. #endif