vmscan.c 71 KB

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