vmscan.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603
  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. /*
  1032. * This moves pages from the active list to the inactive list.
  1033. *
  1034. * We move them the other way if the page is referenced by one or more
  1035. * processes, from rmap.
  1036. *
  1037. * If the pages are mostly unmapped, the processing is fast and it is
  1038. * appropriate to hold zone->lru_lock across the whole operation. But if
  1039. * the pages are mapped, the processing is slow (page_referenced()) so we
  1040. * should drop zone->lru_lock around each page. It's impossible to balance
  1041. * this, so instead we remove the pages from the LRU while processing them.
  1042. * It is safe to rely on PG_active against the non-LRU pages in here because
  1043. * nobody will play with that bit on a non-LRU page.
  1044. *
  1045. * The downside is that we have to touch page->_count against each page.
  1046. * But we had to alter page->flags anyway.
  1047. */
  1048. static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
  1049. struct scan_control *sc, int priority, int file)
  1050. {
  1051. unsigned long pgmoved;
  1052. int pgdeactivate = 0;
  1053. unsigned long pgscanned;
  1054. LIST_HEAD(l_hold); /* The pages which were snipped off */
  1055. LIST_HEAD(l_inactive);
  1056. struct page *page;
  1057. struct pagevec pvec;
  1058. enum lru_list lru;
  1059. lru_add_drain();
  1060. spin_lock_irq(&zone->lru_lock);
  1061. pgmoved = sc->isolate_pages(nr_pages, &l_hold, &pgscanned, sc->order,
  1062. ISOLATE_ACTIVE, zone,
  1063. sc->mem_cgroup, 1, file);
  1064. /*
  1065. * zone->pages_scanned is used for detect zone's oom
  1066. * mem_cgroup remembers nr_scan by itself.
  1067. */
  1068. if (scan_global_lru(sc)) {
  1069. zone->pages_scanned += pgscanned;
  1070. zone->recent_scanned[!!file] += pgmoved;
  1071. }
  1072. if (file)
  1073. __mod_zone_page_state(zone, NR_ACTIVE_FILE, -pgmoved);
  1074. else
  1075. __mod_zone_page_state(zone, NR_ACTIVE_ANON, -pgmoved);
  1076. spin_unlock_irq(&zone->lru_lock);
  1077. pgmoved = 0;
  1078. while (!list_empty(&l_hold)) {
  1079. cond_resched();
  1080. page = lru_to_page(&l_hold);
  1081. list_del(&page->lru);
  1082. if (unlikely(!page_evictable(page, NULL))) {
  1083. putback_lru_page(page);
  1084. continue;
  1085. }
  1086. /* page_referenced clears PageReferenced */
  1087. if (page_mapping_inuse(page) &&
  1088. page_referenced(page, 0, sc->mem_cgroup))
  1089. pgmoved++;
  1090. list_add(&page->lru, &l_inactive);
  1091. }
  1092. /*
  1093. * Move the pages to the [file or anon] inactive list.
  1094. */
  1095. pagevec_init(&pvec, 1);
  1096. pgmoved = 0;
  1097. lru = LRU_BASE + file * LRU_FILE;
  1098. spin_lock_irq(&zone->lru_lock);
  1099. /*
  1100. * Count referenced pages from currently used mappings as
  1101. * rotated, even though they are moved to the inactive list.
  1102. * This helps balance scan pressure between file and anonymous
  1103. * pages in get_scan_ratio.
  1104. */
  1105. if (scan_global_lru(sc))
  1106. zone->recent_rotated[!!file] += pgmoved;
  1107. while (!list_empty(&l_inactive)) {
  1108. page = lru_to_page(&l_inactive);
  1109. prefetchw_prev_lru_page(page, &l_inactive, flags);
  1110. VM_BUG_ON(PageLRU(page));
  1111. SetPageLRU(page);
  1112. VM_BUG_ON(!PageActive(page));
  1113. ClearPageActive(page);
  1114. list_move(&page->lru, &zone->lru[lru].list);
  1115. mem_cgroup_move_lists(page, lru);
  1116. pgmoved++;
  1117. if (!pagevec_add(&pvec, page)) {
  1118. __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
  1119. spin_unlock_irq(&zone->lru_lock);
  1120. pgdeactivate += pgmoved;
  1121. pgmoved = 0;
  1122. if (buffer_heads_over_limit)
  1123. pagevec_strip(&pvec);
  1124. __pagevec_release(&pvec);
  1125. spin_lock_irq(&zone->lru_lock);
  1126. }
  1127. }
  1128. __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
  1129. pgdeactivate += pgmoved;
  1130. if (buffer_heads_over_limit) {
  1131. spin_unlock_irq(&zone->lru_lock);
  1132. pagevec_strip(&pvec);
  1133. spin_lock_irq(&zone->lru_lock);
  1134. }
  1135. __count_zone_vm_events(PGREFILL, zone, pgscanned);
  1136. __count_vm_events(PGDEACTIVATE, pgdeactivate);
  1137. spin_unlock_irq(&zone->lru_lock);
  1138. if (vm_swap_full())
  1139. pagevec_swap_free(&pvec);
  1140. pagevec_release(&pvec);
  1141. }
  1142. static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
  1143. struct zone *zone, struct scan_control *sc, int priority)
  1144. {
  1145. int file = is_file_lru(lru);
  1146. if (lru == LRU_ACTIVE_FILE) {
  1147. shrink_active_list(nr_to_scan, zone, sc, priority, file);
  1148. return 0;
  1149. }
  1150. if (lru == LRU_ACTIVE_ANON &&
  1151. (!scan_global_lru(sc) || inactive_anon_is_low(zone))) {
  1152. shrink_active_list(nr_to_scan, zone, sc, priority, file);
  1153. return 0;
  1154. }
  1155. return shrink_inactive_list(nr_to_scan, zone, sc, priority, file);
  1156. }
  1157. /*
  1158. * Determine how aggressively the anon and file LRU lists should be
  1159. * scanned. The relative value of each set of LRU lists is determined
  1160. * by looking at the fraction of the pages scanned we did rotate back
  1161. * onto the active list instead of evict.
  1162. *
  1163. * percent[0] specifies how much pressure to put on ram/swap backed
  1164. * memory, while percent[1] determines pressure on the file LRUs.
  1165. */
  1166. static void get_scan_ratio(struct zone *zone, struct scan_control *sc,
  1167. unsigned long *percent)
  1168. {
  1169. unsigned long anon, file, free;
  1170. unsigned long anon_prio, file_prio;
  1171. unsigned long ap, fp;
  1172. /* If we have no swap space, do not bother scanning anon pages. */
  1173. if (nr_swap_pages <= 0) {
  1174. percent[0] = 0;
  1175. percent[1] = 100;
  1176. return;
  1177. }
  1178. anon = zone_page_state(zone, NR_ACTIVE_ANON) +
  1179. zone_page_state(zone, NR_INACTIVE_ANON);
  1180. file = zone_page_state(zone, NR_ACTIVE_FILE) +
  1181. zone_page_state(zone, NR_INACTIVE_FILE);
  1182. free = zone_page_state(zone, NR_FREE_PAGES);
  1183. /* If we have very few page cache pages, force-scan anon pages. */
  1184. if (unlikely(file + free <= zone->pages_high)) {
  1185. percent[0] = 100;
  1186. percent[1] = 0;
  1187. return;
  1188. }
  1189. /*
  1190. * OK, so we have swap space and a fair amount of page cache
  1191. * pages. We use the recently rotated / recently scanned
  1192. * ratios to determine how valuable each cache is.
  1193. *
  1194. * Because workloads change over time (and to avoid overflow)
  1195. * we keep these statistics as a floating average, which ends
  1196. * up weighing recent references more than old ones.
  1197. *
  1198. * anon in [0], file in [1]
  1199. */
  1200. if (unlikely(zone->recent_scanned[0] > anon / 4)) {
  1201. spin_lock_irq(&zone->lru_lock);
  1202. zone->recent_scanned[0] /= 2;
  1203. zone->recent_rotated[0] /= 2;
  1204. spin_unlock_irq(&zone->lru_lock);
  1205. }
  1206. if (unlikely(zone->recent_scanned[1] > file / 4)) {
  1207. spin_lock_irq(&zone->lru_lock);
  1208. zone->recent_scanned[1] /= 2;
  1209. zone->recent_rotated[1] /= 2;
  1210. spin_unlock_irq(&zone->lru_lock);
  1211. }
  1212. /*
  1213. * With swappiness at 100, anonymous and file have the same priority.
  1214. * This scanning priority is essentially the inverse of IO cost.
  1215. */
  1216. anon_prio = sc->swappiness;
  1217. file_prio = 200 - sc->swappiness;
  1218. /*
  1219. * The amount of pressure on anon vs file pages is inversely
  1220. * proportional to the fraction of recently scanned pages on
  1221. * each list that were recently referenced and in active use.
  1222. */
  1223. ap = (anon_prio + 1) * (zone->recent_scanned[0] + 1);
  1224. ap /= zone->recent_rotated[0] + 1;
  1225. fp = (file_prio + 1) * (zone->recent_scanned[1] + 1);
  1226. fp /= zone->recent_rotated[1] + 1;
  1227. /* Normalize to percentages */
  1228. percent[0] = 100 * ap / (ap + fp + 1);
  1229. percent[1] = 100 - percent[0];
  1230. }
  1231. /*
  1232. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  1233. */
  1234. static void shrink_zone(int priority, struct zone *zone,
  1235. struct scan_control *sc)
  1236. {
  1237. unsigned long nr[NR_LRU_LISTS];
  1238. unsigned long nr_to_scan;
  1239. unsigned long percent[2]; /* anon @ 0; file @ 1 */
  1240. enum lru_list l;
  1241. unsigned long nr_reclaimed = sc->nr_reclaimed;
  1242. unsigned long swap_cluster_max = sc->swap_cluster_max;
  1243. get_scan_ratio(zone, sc, percent);
  1244. for_each_evictable_lru(l) {
  1245. if (scan_global_lru(sc)) {
  1246. int file = is_file_lru(l);
  1247. int scan;
  1248. scan = zone_page_state(zone, NR_LRU_BASE + l);
  1249. if (priority) {
  1250. scan >>= priority;
  1251. scan = (scan * percent[file]) / 100;
  1252. }
  1253. zone->lru[l].nr_scan += scan;
  1254. nr[l] = zone->lru[l].nr_scan;
  1255. if (nr[l] >= swap_cluster_max)
  1256. zone->lru[l].nr_scan = 0;
  1257. else
  1258. nr[l] = 0;
  1259. } else {
  1260. /*
  1261. * This reclaim occurs not because zone memory shortage
  1262. * but because memory controller hits its limit.
  1263. * Don't modify zone reclaim related data.
  1264. */
  1265. nr[l] = mem_cgroup_calc_reclaim(sc->mem_cgroup, zone,
  1266. priority, l);
  1267. }
  1268. }
  1269. while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
  1270. nr[LRU_INACTIVE_FILE]) {
  1271. for_each_evictable_lru(l) {
  1272. if (nr[l]) {
  1273. nr_to_scan = min(nr[l], swap_cluster_max);
  1274. nr[l] -= nr_to_scan;
  1275. nr_reclaimed += shrink_list(l, nr_to_scan,
  1276. zone, sc, priority);
  1277. }
  1278. }
  1279. /*
  1280. * On large memory systems, scan >> priority can become
  1281. * really large. This is fine for the starting priority;
  1282. * we want to put equal scanning pressure on each zone.
  1283. * However, if the VM has a harder time of freeing pages,
  1284. * with multiple processes reclaiming pages, the total
  1285. * freeing target can get unreasonably large.
  1286. */
  1287. if (nr_reclaimed > swap_cluster_max &&
  1288. priority < DEF_PRIORITY && !current_is_kswapd())
  1289. break;
  1290. }
  1291. sc->nr_reclaimed = nr_reclaimed;
  1292. /*
  1293. * Even if we did not try to evict anon pages at all, we want to
  1294. * rebalance the anon lru active/inactive ratio.
  1295. */
  1296. if (!scan_global_lru(sc) || inactive_anon_is_low(zone))
  1297. shrink_active_list(SWAP_CLUSTER_MAX, zone, sc, priority, 0);
  1298. else if (!scan_global_lru(sc))
  1299. shrink_active_list(SWAP_CLUSTER_MAX, zone, sc, priority, 0);
  1300. throttle_vm_writeout(sc->gfp_mask);
  1301. }
  1302. /*
  1303. * This is the direct reclaim path, for page-allocating processes. We only
  1304. * try to reclaim pages from zones which will satisfy the caller's allocation
  1305. * request.
  1306. *
  1307. * We reclaim from a zone even if that zone is over pages_high. Because:
  1308. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  1309. * allocation or
  1310. * b) The zones may be over pages_high but they must go *over* pages_high to
  1311. * satisfy the `incremental min' zone defense algorithm.
  1312. *
  1313. * If a zone is deemed to be full of pinned pages then just give it a light
  1314. * scan then give up on it.
  1315. */
  1316. static void shrink_zones(int priority, struct zonelist *zonelist,
  1317. struct scan_control *sc)
  1318. {
  1319. enum zone_type high_zoneidx = gfp_zone(sc->gfp_mask);
  1320. struct zoneref *z;
  1321. struct zone *zone;
  1322. sc->all_unreclaimable = 1;
  1323. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  1324. if (!populated_zone(zone))
  1325. continue;
  1326. /*
  1327. * Take care memory controller reclaiming has small influence
  1328. * to global LRU.
  1329. */
  1330. if (scan_global_lru(sc)) {
  1331. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1332. continue;
  1333. note_zone_scanning_priority(zone, priority);
  1334. if (zone_is_all_unreclaimable(zone) &&
  1335. priority != DEF_PRIORITY)
  1336. continue; /* Let kswapd poll it */
  1337. sc->all_unreclaimable = 0;
  1338. } else {
  1339. /*
  1340. * Ignore cpuset limitation here. We just want to reduce
  1341. * # of used pages by us regardless of memory shortage.
  1342. */
  1343. sc->all_unreclaimable = 0;
  1344. mem_cgroup_note_reclaim_priority(sc->mem_cgroup,
  1345. priority);
  1346. }
  1347. shrink_zone(priority, zone, sc);
  1348. }
  1349. }
  1350. /*
  1351. * This is the main entry point to direct page reclaim.
  1352. *
  1353. * If a full scan of the inactive list fails to free enough memory then we
  1354. * are "out of memory" and something needs to be killed.
  1355. *
  1356. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  1357. * high - the zone may be full of dirty or under-writeback pages, which this
  1358. * caller can't do much about. We kick pdflush and take explicit naps in the
  1359. * hope that some of these pages can be written. But if the allocating task
  1360. * holds filesystem locks which prevent writeout this might not work, and the
  1361. * allocation attempt will fail.
  1362. *
  1363. * returns: 0, if no pages reclaimed
  1364. * else, the number of pages reclaimed
  1365. */
  1366. static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
  1367. struct scan_control *sc)
  1368. {
  1369. int priority;
  1370. unsigned long ret = 0;
  1371. unsigned long total_scanned = 0;
  1372. struct reclaim_state *reclaim_state = current->reclaim_state;
  1373. unsigned long lru_pages = 0;
  1374. struct zoneref *z;
  1375. struct zone *zone;
  1376. enum zone_type high_zoneidx = gfp_zone(sc->gfp_mask);
  1377. delayacct_freepages_start();
  1378. if (scan_global_lru(sc))
  1379. count_vm_event(ALLOCSTALL);
  1380. /*
  1381. * mem_cgroup will not do shrink_slab.
  1382. */
  1383. if (scan_global_lru(sc)) {
  1384. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  1385. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1386. continue;
  1387. lru_pages += zone_lru_pages(zone);
  1388. }
  1389. }
  1390. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1391. sc->nr_scanned = 0;
  1392. if (!priority)
  1393. disable_swap_token();
  1394. shrink_zones(priority, zonelist, sc);
  1395. /*
  1396. * Don't shrink slabs when reclaiming memory from
  1397. * over limit cgroups
  1398. */
  1399. if (scan_global_lru(sc)) {
  1400. shrink_slab(sc->nr_scanned, sc->gfp_mask, lru_pages);
  1401. if (reclaim_state) {
  1402. sc->nr_reclaimed += reclaim_state->reclaimed_slab;
  1403. reclaim_state->reclaimed_slab = 0;
  1404. }
  1405. }
  1406. total_scanned += sc->nr_scanned;
  1407. if (sc->nr_reclaimed >= sc->swap_cluster_max) {
  1408. ret = sc->nr_reclaimed;
  1409. goto out;
  1410. }
  1411. /*
  1412. * Try to write back as many pages as we just scanned. This
  1413. * tends to cause slow streaming writers to write data to the
  1414. * disk smoothly, at the dirtying rate, which is nice. But
  1415. * that's undesirable in laptop mode, where we *want* lumpy
  1416. * writeout. So in laptop mode, write out the whole world.
  1417. */
  1418. if (total_scanned > sc->swap_cluster_max +
  1419. sc->swap_cluster_max / 2) {
  1420. wakeup_pdflush(laptop_mode ? 0 : total_scanned);
  1421. sc->may_writepage = 1;
  1422. }
  1423. /* Take a nap, wait for some writeback to complete */
  1424. if (sc->nr_scanned && priority < DEF_PRIORITY - 2)
  1425. congestion_wait(WRITE, HZ/10);
  1426. }
  1427. /* top priority shrink_zones still had more to do? don't OOM, then */
  1428. if (!sc->all_unreclaimable && scan_global_lru(sc))
  1429. ret = sc->nr_reclaimed;
  1430. out:
  1431. /*
  1432. * Now that we've scanned all the zones at this priority level, note
  1433. * that level within the zone so that the next thread which performs
  1434. * scanning of this zone will immediately start out at this priority
  1435. * level. This affects only the decision whether or not to bring
  1436. * mapped pages onto the inactive list.
  1437. */
  1438. if (priority < 0)
  1439. priority = 0;
  1440. if (scan_global_lru(sc)) {
  1441. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  1442. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1443. continue;
  1444. zone->prev_priority = priority;
  1445. }
  1446. } else
  1447. mem_cgroup_record_reclaim_priority(sc->mem_cgroup, priority);
  1448. delayacct_freepages_end();
  1449. return ret;
  1450. }
  1451. unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
  1452. gfp_t gfp_mask)
  1453. {
  1454. struct scan_control sc = {
  1455. .gfp_mask = gfp_mask,
  1456. .may_writepage = !laptop_mode,
  1457. .swap_cluster_max = SWAP_CLUSTER_MAX,
  1458. .may_swap = 1,
  1459. .swappiness = vm_swappiness,
  1460. .order = order,
  1461. .mem_cgroup = NULL,
  1462. .isolate_pages = isolate_pages_global,
  1463. };
  1464. return do_try_to_free_pages(zonelist, &sc);
  1465. }
  1466. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  1467. unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont,
  1468. gfp_t gfp_mask)
  1469. {
  1470. struct scan_control sc = {
  1471. .may_writepage = !laptop_mode,
  1472. .may_swap = 1,
  1473. .swap_cluster_max = SWAP_CLUSTER_MAX,
  1474. .swappiness = vm_swappiness,
  1475. .order = 0,
  1476. .mem_cgroup = mem_cont,
  1477. .isolate_pages = mem_cgroup_isolate_pages,
  1478. };
  1479. struct zonelist *zonelist;
  1480. sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
  1481. (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
  1482. zonelist = NODE_DATA(numa_node_id())->node_zonelists;
  1483. return do_try_to_free_pages(zonelist, &sc);
  1484. }
  1485. #endif
  1486. /*
  1487. * For kswapd, balance_pgdat() will work across all this node's zones until
  1488. * they are all at pages_high.
  1489. *
  1490. * Returns the number of pages which were actually freed.
  1491. *
  1492. * There is special handling here for zones which are full of pinned pages.
  1493. * This can happen if the pages are all mlocked, or if they are all used by
  1494. * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
  1495. * What we do is to detect the case where all pages in the zone have been
  1496. * scanned twice and there has been zero successful reclaim. Mark the zone as
  1497. * dead and from now on, only perform a short scan. Basically we're polling
  1498. * the zone for when the problem goes away.
  1499. *
  1500. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  1501. * zones which have free_pages > pages_high, but once a zone is found to have
  1502. * free_pages <= pages_high, we scan that zone and the lower zones regardless
  1503. * of the number of free pages in the lower zones. This interoperates with
  1504. * the page allocator fallback scheme to ensure that aging of pages is balanced
  1505. * across the zones.
  1506. */
  1507. static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
  1508. {
  1509. int all_zones_ok;
  1510. int priority;
  1511. int i;
  1512. unsigned long total_scanned;
  1513. struct reclaim_state *reclaim_state = current->reclaim_state;
  1514. struct scan_control sc = {
  1515. .gfp_mask = GFP_KERNEL,
  1516. .may_swap = 1,
  1517. .swap_cluster_max = SWAP_CLUSTER_MAX,
  1518. .swappiness = vm_swappiness,
  1519. .order = order,
  1520. .mem_cgroup = NULL,
  1521. .isolate_pages = isolate_pages_global,
  1522. };
  1523. /*
  1524. * temp_priority is used to remember the scanning priority at which
  1525. * this zone was successfully refilled to free_pages == pages_high.
  1526. */
  1527. int temp_priority[MAX_NR_ZONES];
  1528. loop_again:
  1529. total_scanned = 0;
  1530. sc.nr_reclaimed = 0;
  1531. sc.may_writepage = !laptop_mode;
  1532. count_vm_event(PAGEOUTRUN);
  1533. for (i = 0; i < pgdat->nr_zones; i++)
  1534. temp_priority[i] = DEF_PRIORITY;
  1535. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1536. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  1537. unsigned long lru_pages = 0;
  1538. /* The swap token gets in the way of swapout... */
  1539. if (!priority)
  1540. disable_swap_token();
  1541. all_zones_ok = 1;
  1542. /*
  1543. * Scan in the highmem->dma direction for the highest
  1544. * zone which needs scanning
  1545. */
  1546. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  1547. struct zone *zone = pgdat->node_zones + i;
  1548. if (!populated_zone(zone))
  1549. continue;
  1550. if (zone_is_all_unreclaimable(zone) &&
  1551. priority != DEF_PRIORITY)
  1552. continue;
  1553. /*
  1554. * Do some background aging of the anon list, to give
  1555. * pages a chance to be referenced before reclaiming.
  1556. */
  1557. if (inactive_anon_is_low(zone))
  1558. shrink_active_list(SWAP_CLUSTER_MAX, zone,
  1559. &sc, priority, 0);
  1560. if (!zone_watermark_ok(zone, order, zone->pages_high,
  1561. 0, 0)) {
  1562. end_zone = i;
  1563. break;
  1564. }
  1565. }
  1566. if (i < 0)
  1567. goto out;
  1568. for (i = 0; i <= end_zone; i++) {
  1569. struct zone *zone = pgdat->node_zones + i;
  1570. lru_pages += zone_lru_pages(zone);
  1571. }
  1572. /*
  1573. * Now scan the zone in the dma->highmem direction, stopping
  1574. * at the last zone which needs scanning.
  1575. *
  1576. * We do this because the page allocator works in the opposite
  1577. * direction. This prevents the page allocator from allocating
  1578. * pages behind kswapd's direction of progress, which would
  1579. * cause too much scanning of the lower zones.
  1580. */
  1581. for (i = 0; i <= end_zone; i++) {
  1582. struct zone *zone = pgdat->node_zones + i;
  1583. int nr_slab;
  1584. if (!populated_zone(zone))
  1585. continue;
  1586. if (zone_is_all_unreclaimable(zone) &&
  1587. priority != DEF_PRIORITY)
  1588. continue;
  1589. if (!zone_watermark_ok(zone, order, zone->pages_high,
  1590. end_zone, 0))
  1591. all_zones_ok = 0;
  1592. temp_priority[i] = priority;
  1593. sc.nr_scanned = 0;
  1594. note_zone_scanning_priority(zone, priority);
  1595. /*
  1596. * We put equal pressure on every zone, unless one
  1597. * zone has way too many pages free already.
  1598. */
  1599. if (!zone_watermark_ok(zone, order, 8*zone->pages_high,
  1600. end_zone, 0))
  1601. shrink_zone(priority, zone, &sc);
  1602. reclaim_state->reclaimed_slab = 0;
  1603. nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
  1604. lru_pages);
  1605. sc.nr_reclaimed += reclaim_state->reclaimed_slab;
  1606. total_scanned += sc.nr_scanned;
  1607. if (zone_is_all_unreclaimable(zone))
  1608. continue;
  1609. if (nr_slab == 0 && zone->pages_scanned >=
  1610. (zone_lru_pages(zone) * 6))
  1611. zone_set_flag(zone,
  1612. ZONE_ALL_UNRECLAIMABLE);
  1613. /*
  1614. * If we've done a decent amount of scanning and
  1615. * the reclaim ratio is low, start doing writepage
  1616. * even in laptop mode
  1617. */
  1618. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  1619. total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2)
  1620. sc.may_writepage = 1;
  1621. }
  1622. if (all_zones_ok)
  1623. break; /* kswapd: all done */
  1624. /*
  1625. * OK, kswapd is getting into trouble. Take a nap, then take
  1626. * another pass across the zones.
  1627. */
  1628. if (total_scanned && priority < DEF_PRIORITY - 2)
  1629. congestion_wait(WRITE, HZ/10);
  1630. /*
  1631. * We do this so kswapd doesn't build up large priorities for
  1632. * example when it is freeing in parallel with allocators. It
  1633. * matches the direct reclaim path behaviour in terms of impact
  1634. * on zone->*_priority.
  1635. */
  1636. if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX)
  1637. break;
  1638. }
  1639. out:
  1640. /*
  1641. * Note within each zone the priority level at which this zone was
  1642. * brought into a happy state. So that the next thread which scans this
  1643. * zone will start out at that priority level.
  1644. */
  1645. for (i = 0; i < pgdat->nr_zones; i++) {
  1646. struct zone *zone = pgdat->node_zones + i;
  1647. zone->prev_priority = temp_priority[i];
  1648. }
  1649. if (!all_zones_ok) {
  1650. cond_resched();
  1651. try_to_freeze();
  1652. /*
  1653. * Fragmentation may mean that the system cannot be
  1654. * rebalanced for high-order allocations in all zones.
  1655. * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX,
  1656. * it means the zones have been fully scanned and are still
  1657. * not balanced. For high-order allocations, there is
  1658. * little point trying all over again as kswapd may
  1659. * infinite loop.
  1660. *
  1661. * Instead, recheck all watermarks at order-0 as they
  1662. * are the most important. If watermarks are ok, kswapd will go
  1663. * back to sleep. High-order users can still perform direct
  1664. * reclaim if they wish.
  1665. */
  1666. if (sc.nr_reclaimed < SWAP_CLUSTER_MAX)
  1667. order = sc.order = 0;
  1668. goto loop_again;
  1669. }
  1670. return sc.nr_reclaimed;
  1671. }
  1672. /*
  1673. * The background pageout daemon, started as a kernel thread
  1674. * from the init process.
  1675. *
  1676. * This basically trickles out pages so that we have _some_
  1677. * free memory available even if there is no other activity
  1678. * that frees anything up. This is needed for things like routing
  1679. * etc, where we otherwise might have all activity going on in
  1680. * asynchronous contexts that cannot page things out.
  1681. *
  1682. * If there are applications that are active memory-allocators
  1683. * (most normal use), this basically shouldn't matter.
  1684. */
  1685. static int kswapd(void *p)
  1686. {
  1687. unsigned long order;
  1688. pg_data_t *pgdat = (pg_data_t*)p;
  1689. struct task_struct *tsk = current;
  1690. DEFINE_WAIT(wait);
  1691. struct reclaim_state reclaim_state = {
  1692. .reclaimed_slab = 0,
  1693. };
  1694. node_to_cpumask_ptr(cpumask, pgdat->node_id);
  1695. if (!cpumask_empty(cpumask))
  1696. set_cpus_allowed_ptr(tsk, cpumask);
  1697. current->reclaim_state = &reclaim_state;
  1698. /*
  1699. * Tell the memory management that we're a "memory allocator",
  1700. * and that if we need more memory we should get access to it
  1701. * regardless (see "__alloc_pages()"). "kswapd" should
  1702. * never get caught in the normal page freeing logic.
  1703. *
  1704. * (Kswapd normally doesn't need memory anyway, but sometimes
  1705. * you need a small amount of memory in order to be able to
  1706. * page out something else, and this flag essentially protects
  1707. * us from recursively trying to free more memory as we're
  1708. * trying to free the first piece of memory in the first place).
  1709. */
  1710. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  1711. set_freezable();
  1712. order = 0;
  1713. for ( ; ; ) {
  1714. unsigned long new_order;
  1715. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  1716. new_order = pgdat->kswapd_max_order;
  1717. pgdat->kswapd_max_order = 0;
  1718. if (order < new_order) {
  1719. /*
  1720. * Don't sleep if someone wants a larger 'order'
  1721. * allocation
  1722. */
  1723. order = new_order;
  1724. } else {
  1725. if (!freezing(current))
  1726. schedule();
  1727. order = pgdat->kswapd_max_order;
  1728. }
  1729. finish_wait(&pgdat->kswapd_wait, &wait);
  1730. if (!try_to_freeze()) {
  1731. /* We can speed up thawing tasks if we don't call
  1732. * balance_pgdat after returning from the refrigerator
  1733. */
  1734. balance_pgdat(pgdat, order);
  1735. }
  1736. }
  1737. return 0;
  1738. }
  1739. /*
  1740. * A zone is low on free memory, so wake its kswapd task to service it.
  1741. */
  1742. void wakeup_kswapd(struct zone *zone, int order)
  1743. {
  1744. pg_data_t *pgdat;
  1745. if (!populated_zone(zone))
  1746. return;
  1747. pgdat = zone->zone_pgdat;
  1748. if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
  1749. return;
  1750. if (pgdat->kswapd_max_order < order)
  1751. pgdat->kswapd_max_order = order;
  1752. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1753. return;
  1754. if (!waitqueue_active(&pgdat->kswapd_wait))
  1755. return;
  1756. wake_up_interruptible(&pgdat->kswapd_wait);
  1757. }
  1758. unsigned long global_lru_pages(void)
  1759. {
  1760. return global_page_state(NR_ACTIVE_ANON)
  1761. + global_page_state(NR_ACTIVE_FILE)
  1762. + global_page_state(NR_INACTIVE_ANON)
  1763. + global_page_state(NR_INACTIVE_FILE);
  1764. }
  1765. #ifdef CONFIG_PM
  1766. /*
  1767. * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
  1768. * from LRU lists system-wide, for given pass and priority, and returns the
  1769. * number of reclaimed pages
  1770. *
  1771. * For pass > 3 we also try to shrink the LRU lists that contain a few pages
  1772. */
  1773. static unsigned long shrink_all_zones(unsigned long nr_pages, int prio,
  1774. int pass, struct scan_control *sc)
  1775. {
  1776. struct zone *zone;
  1777. unsigned long nr_to_scan, ret = 0;
  1778. enum lru_list l;
  1779. for_each_zone(zone) {
  1780. if (!populated_zone(zone))
  1781. continue;
  1782. if (zone_is_all_unreclaimable(zone) && prio != DEF_PRIORITY)
  1783. continue;
  1784. for_each_evictable_lru(l) {
  1785. /* For pass = 0, we don't shrink the active list */
  1786. if (pass == 0 &&
  1787. (l == LRU_ACTIVE || l == LRU_ACTIVE_FILE))
  1788. continue;
  1789. zone->lru[l].nr_scan +=
  1790. (zone_page_state(zone, NR_LRU_BASE + l)
  1791. >> prio) + 1;
  1792. if (zone->lru[l].nr_scan >= nr_pages || pass > 3) {
  1793. zone->lru[l].nr_scan = 0;
  1794. nr_to_scan = min(nr_pages,
  1795. zone_page_state(zone,
  1796. NR_LRU_BASE + l));
  1797. ret += shrink_list(l, nr_to_scan, zone,
  1798. sc, prio);
  1799. if (ret >= nr_pages)
  1800. return ret;
  1801. }
  1802. }
  1803. }
  1804. return ret;
  1805. }
  1806. /*
  1807. * Try to free `nr_pages' of memory, system-wide, and return the number of
  1808. * freed pages.
  1809. *
  1810. * Rather than trying to age LRUs the aim is to preserve the overall
  1811. * LRU order by reclaiming preferentially
  1812. * inactive > active > active referenced > active mapped
  1813. */
  1814. unsigned long shrink_all_memory(unsigned long nr_pages)
  1815. {
  1816. unsigned long lru_pages, nr_slab;
  1817. unsigned long ret = 0;
  1818. int pass;
  1819. struct reclaim_state reclaim_state;
  1820. struct scan_control sc = {
  1821. .gfp_mask = GFP_KERNEL,
  1822. .may_swap = 0,
  1823. .swap_cluster_max = nr_pages,
  1824. .may_writepage = 1,
  1825. .swappiness = vm_swappiness,
  1826. .isolate_pages = isolate_pages_global,
  1827. };
  1828. current->reclaim_state = &reclaim_state;
  1829. lru_pages = global_lru_pages();
  1830. nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
  1831. /* If slab caches are huge, it's better to hit them first */
  1832. while (nr_slab >= lru_pages) {
  1833. reclaim_state.reclaimed_slab = 0;
  1834. shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
  1835. if (!reclaim_state.reclaimed_slab)
  1836. break;
  1837. ret += reclaim_state.reclaimed_slab;
  1838. if (ret >= nr_pages)
  1839. goto out;
  1840. nr_slab -= reclaim_state.reclaimed_slab;
  1841. }
  1842. /*
  1843. * We try to shrink LRUs in 5 passes:
  1844. * 0 = Reclaim from inactive_list only
  1845. * 1 = Reclaim from active list but don't reclaim mapped
  1846. * 2 = 2nd pass of type 1
  1847. * 3 = Reclaim mapped (normal reclaim)
  1848. * 4 = 2nd pass of type 3
  1849. */
  1850. for (pass = 0; pass < 5; pass++) {
  1851. int prio;
  1852. /* Force reclaiming mapped pages in the passes #3 and #4 */
  1853. if (pass > 2) {
  1854. sc.may_swap = 1;
  1855. sc.swappiness = 100;
  1856. }
  1857. for (prio = DEF_PRIORITY; prio >= 0; prio--) {
  1858. unsigned long nr_to_scan = nr_pages - ret;
  1859. sc.nr_scanned = 0;
  1860. ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
  1861. if (ret >= nr_pages)
  1862. goto out;
  1863. reclaim_state.reclaimed_slab = 0;
  1864. shrink_slab(sc.nr_scanned, sc.gfp_mask,
  1865. global_lru_pages());
  1866. ret += reclaim_state.reclaimed_slab;
  1867. if (ret >= nr_pages)
  1868. goto out;
  1869. if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
  1870. congestion_wait(WRITE, HZ / 10);
  1871. }
  1872. }
  1873. /*
  1874. * If ret = 0, we could not shrink LRUs, but there may be something
  1875. * in slab caches
  1876. */
  1877. if (!ret) {
  1878. do {
  1879. reclaim_state.reclaimed_slab = 0;
  1880. shrink_slab(nr_pages, sc.gfp_mask, global_lru_pages());
  1881. ret += reclaim_state.reclaimed_slab;
  1882. } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
  1883. }
  1884. out:
  1885. current->reclaim_state = NULL;
  1886. return ret;
  1887. }
  1888. #endif
  1889. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  1890. not required for correctness. So if the last cpu in a node goes
  1891. away, we get changed to run anywhere: as the first one comes back,
  1892. restore their cpu bindings. */
  1893. static int __devinit cpu_callback(struct notifier_block *nfb,
  1894. unsigned long action, void *hcpu)
  1895. {
  1896. int nid;
  1897. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
  1898. for_each_node_state(nid, N_HIGH_MEMORY) {
  1899. pg_data_t *pgdat = NODE_DATA(nid);
  1900. node_to_cpumask_ptr(mask, pgdat->node_id);
  1901. if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
  1902. /* One of our CPUs online: restore mask */
  1903. set_cpus_allowed_ptr(pgdat->kswapd, mask);
  1904. }
  1905. }
  1906. return NOTIFY_OK;
  1907. }
  1908. /*
  1909. * This kswapd start function will be called by init and node-hot-add.
  1910. * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
  1911. */
  1912. int kswapd_run(int nid)
  1913. {
  1914. pg_data_t *pgdat = NODE_DATA(nid);
  1915. int ret = 0;
  1916. if (pgdat->kswapd)
  1917. return 0;
  1918. pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
  1919. if (IS_ERR(pgdat->kswapd)) {
  1920. /* failure at boot is fatal */
  1921. BUG_ON(system_state == SYSTEM_BOOTING);
  1922. printk("Failed to start kswapd on node %d\n",nid);
  1923. ret = -1;
  1924. }
  1925. return ret;
  1926. }
  1927. static int __init kswapd_init(void)
  1928. {
  1929. int nid;
  1930. swap_setup();
  1931. for_each_node_state(nid, N_HIGH_MEMORY)
  1932. kswapd_run(nid);
  1933. hotcpu_notifier(cpu_callback, 0);
  1934. return 0;
  1935. }
  1936. module_init(kswapd_init)
  1937. #ifdef CONFIG_NUMA
  1938. /*
  1939. * Zone reclaim mode
  1940. *
  1941. * If non-zero call zone_reclaim when the number of free pages falls below
  1942. * the watermarks.
  1943. */
  1944. int zone_reclaim_mode __read_mostly;
  1945. #define RECLAIM_OFF 0
  1946. #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */
  1947. #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
  1948. #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
  1949. /*
  1950. * Priority for ZONE_RECLAIM. This determines the fraction of pages
  1951. * of a node considered for each zone_reclaim. 4 scans 1/16th of
  1952. * a zone.
  1953. */
  1954. #define ZONE_RECLAIM_PRIORITY 4
  1955. /*
  1956. * Percentage of pages in a zone that must be unmapped for zone_reclaim to
  1957. * occur.
  1958. */
  1959. int sysctl_min_unmapped_ratio = 1;
  1960. /*
  1961. * If the number of slab pages in a zone grows beyond this percentage then
  1962. * slab reclaim needs to occur.
  1963. */
  1964. int sysctl_min_slab_ratio = 5;
  1965. /*
  1966. * Try to free up some pages from this zone through reclaim.
  1967. */
  1968. static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  1969. {
  1970. /* Minimum pages needed in order to stay on node */
  1971. const unsigned long nr_pages = 1 << order;
  1972. struct task_struct *p = current;
  1973. struct reclaim_state reclaim_state;
  1974. int priority;
  1975. struct scan_control sc = {
  1976. .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
  1977. .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
  1978. .swap_cluster_max = max_t(unsigned long, nr_pages,
  1979. SWAP_CLUSTER_MAX),
  1980. .gfp_mask = gfp_mask,
  1981. .swappiness = vm_swappiness,
  1982. .isolate_pages = isolate_pages_global,
  1983. };
  1984. unsigned long slab_reclaimable;
  1985. disable_swap_token();
  1986. cond_resched();
  1987. /*
  1988. * We need to be able to allocate from the reserves for RECLAIM_SWAP
  1989. * and we also need to be able to write out pages for RECLAIM_WRITE
  1990. * and RECLAIM_SWAP.
  1991. */
  1992. p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
  1993. reclaim_state.reclaimed_slab = 0;
  1994. p->reclaim_state = &reclaim_state;
  1995. if (zone_page_state(zone, NR_FILE_PAGES) -
  1996. zone_page_state(zone, NR_FILE_MAPPED) >
  1997. zone->min_unmapped_pages) {
  1998. /*
  1999. * Free memory by calling shrink zone with increasing
  2000. * priorities until we have enough memory freed.
  2001. */
  2002. priority = ZONE_RECLAIM_PRIORITY;
  2003. do {
  2004. note_zone_scanning_priority(zone, priority);
  2005. shrink_zone(priority, zone, &sc);
  2006. priority--;
  2007. } while (priority >= 0 && sc.nr_reclaimed < nr_pages);
  2008. }
  2009. slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  2010. if (slab_reclaimable > zone->min_slab_pages) {
  2011. /*
  2012. * shrink_slab() does not currently allow us to determine how
  2013. * many pages were freed in this zone. So we take the current
  2014. * number of slab pages and shake the slab until it is reduced
  2015. * by the same nr_pages that we used for reclaiming unmapped
  2016. * pages.
  2017. *
  2018. * Note that shrink_slab will free memory on all zones and may
  2019. * take a long time.
  2020. */
  2021. while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
  2022. zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
  2023. slab_reclaimable - nr_pages)
  2024. ;
  2025. /*
  2026. * Update nr_reclaimed by the number of slab pages we
  2027. * reclaimed from this zone.
  2028. */
  2029. sc.nr_reclaimed += slab_reclaimable -
  2030. zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  2031. }
  2032. p->reclaim_state = NULL;
  2033. current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
  2034. return sc.nr_reclaimed >= nr_pages;
  2035. }
  2036. int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  2037. {
  2038. int node_id;
  2039. int ret;
  2040. /*
  2041. * Zone reclaim reclaims unmapped file backed pages and
  2042. * slab pages if we are over the defined limits.
  2043. *
  2044. * A small portion of unmapped file backed pages is needed for
  2045. * file I/O otherwise pages read by file I/O will be immediately
  2046. * thrown out if the zone is overallocated. So we do not reclaim
  2047. * if less than a specified percentage of the zone is used by
  2048. * unmapped file backed pages.
  2049. */
  2050. if (zone_page_state(zone, NR_FILE_PAGES) -
  2051. zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
  2052. && zone_page_state(zone, NR_SLAB_RECLAIMABLE)
  2053. <= zone->min_slab_pages)
  2054. return 0;
  2055. if (zone_is_all_unreclaimable(zone))
  2056. return 0;
  2057. /*
  2058. * Do not scan if the allocation should not be delayed.
  2059. */
  2060. if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
  2061. return 0;
  2062. /*
  2063. * Only run zone reclaim on the local zone or on zones that do not
  2064. * have associated processors. This will favor the local processor
  2065. * over remote processors and spread off node memory allocations
  2066. * as wide as possible.
  2067. */
  2068. node_id = zone_to_nid(zone);
  2069. if (node_state(node_id, N_CPU) && node_id != numa_node_id())
  2070. return 0;
  2071. if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
  2072. return 0;
  2073. ret = __zone_reclaim(zone, gfp_mask, order);
  2074. zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
  2075. return ret;
  2076. }
  2077. #endif
  2078. #ifdef CONFIG_UNEVICTABLE_LRU
  2079. /*
  2080. * page_evictable - test whether a page is evictable
  2081. * @page: the page to test
  2082. * @vma: the VMA in which the page is or will be mapped, may be NULL
  2083. *
  2084. * Test whether page is evictable--i.e., should be placed on active/inactive
  2085. * lists vs unevictable list. The vma argument is !NULL when called from the
  2086. * fault path to determine how to instantate a new page.
  2087. *
  2088. * Reasons page might not be evictable:
  2089. * (1) page's mapping marked unevictable
  2090. * (2) page is part of an mlocked VMA
  2091. *
  2092. */
  2093. int page_evictable(struct page *page, struct vm_area_struct *vma)
  2094. {
  2095. if (mapping_unevictable(page_mapping(page)))
  2096. return 0;
  2097. if (PageMlocked(page) || (vma && is_mlocked_vma(vma, page)))
  2098. return 0;
  2099. return 1;
  2100. }
  2101. /**
  2102. * check_move_unevictable_page - check page for evictability and move to appropriate zone lru list
  2103. * @page: page to check evictability and move to appropriate lru list
  2104. * @zone: zone page is in
  2105. *
  2106. * Checks a page for evictability and moves the page to the appropriate
  2107. * zone lru list.
  2108. *
  2109. * Restrictions: zone->lru_lock must be held, page must be on LRU and must
  2110. * have PageUnevictable set.
  2111. */
  2112. static void check_move_unevictable_page(struct page *page, struct zone *zone)
  2113. {
  2114. VM_BUG_ON(PageActive(page));
  2115. retry:
  2116. ClearPageUnevictable(page);
  2117. if (page_evictable(page, NULL)) {
  2118. enum lru_list l = LRU_INACTIVE_ANON + page_is_file_cache(page);
  2119. __dec_zone_state(zone, NR_UNEVICTABLE);
  2120. list_move(&page->lru, &zone->lru[l].list);
  2121. __inc_zone_state(zone, NR_INACTIVE_ANON + l);
  2122. __count_vm_event(UNEVICTABLE_PGRESCUED);
  2123. } else {
  2124. /*
  2125. * rotate unevictable list
  2126. */
  2127. SetPageUnevictable(page);
  2128. list_move(&page->lru, &zone->lru[LRU_UNEVICTABLE].list);
  2129. if (page_evictable(page, NULL))
  2130. goto retry;
  2131. }
  2132. }
  2133. /**
  2134. * scan_mapping_unevictable_pages - scan an address space for evictable pages
  2135. * @mapping: struct address_space to scan for evictable pages
  2136. *
  2137. * Scan all pages in mapping. Check unevictable pages for
  2138. * evictability and move them to the appropriate zone lru list.
  2139. */
  2140. void scan_mapping_unevictable_pages(struct address_space *mapping)
  2141. {
  2142. pgoff_t next = 0;
  2143. pgoff_t end = (i_size_read(mapping->host) + PAGE_CACHE_SIZE - 1) >>
  2144. PAGE_CACHE_SHIFT;
  2145. struct zone *zone;
  2146. struct pagevec pvec;
  2147. if (mapping->nrpages == 0)
  2148. return;
  2149. pagevec_init(&pvec, 0);
  2150. while (next < end &&
  2151. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  2152. int i;
  2153. int pg_scanned = 0;
  2154. zone = NULL;
  2155. for (i = 0; i < pagevec_count(&pvec); i++) {
  2156. struct page *page = pvec.pages[i];
  2157. pgoff_t page_index = page->index;
  2158. struct zone *pagezone = page_zone(page);
  2159. pg_scanned++;
  2160. if (page_index > next)
  2161. next = page_index;
  2162. next++;
  2163. if (pagezone != zone) {
  2164. if (zone)
  2165. spin_unlock_irq(&zone->lru_lock);
  2166. zone = pagezone;
  2167. spin_lock_irq(&zone->lru_lock);
  2168. }
  2169. if (PageLRU(page) && PageUnevictable(page))
  2170. check_move_unevictable_page(page, zone);
  2171. }
  2172. if (zone)
  2173. spin_unlock_irq(&zone->lru_lock);
  2174. pagevec_release(&pvec);
  2175. count_vm_events(UNEVICTABLE_PGSCANNED, pg_scanned);
  2176. }
  2177. }
  2178. /**
  2179. * scan_zone_unevictable_pages - check unevictable list for evictable pages
  2180. * @zone - zone of which to scan the unevictable list
  2181. *
  2182. * Scan @zone's unevictable LRU lists to check for pages that have become
  2183. * evictable. Move those that have to @zone's inactive list where they
  2184. * become candidates for reclaim, unless shrink_inactive_zone() decides
  2185. * to reactivate them. Pages that are still unevictable are rotated
  2186. * back onto @zone's unevictable list.
  2187. */
  2188. #define SCAN_UNEVICTABLE_BATCH_SIZE 16UL /* arbitrary lock hold batch size */
  2189. static void scan_zone_unevictable_pages(struct zone *zone)
  2190. {
  2191. struct list_head *l_unevictable = &zone->lru[LRU_UNEVICTABLE].list;
  2192. unsigned long scan;
  2193. unsigned long nr_to_scan = zone_page_state(zone, NR_UNEVICTABLE);
  2194. while (nr_to_scan > 0) {
  2195. unsigned long batch_size = min(nr_to_scan,
  2196. SCAN_UNEVICTABLE_BATCH_SIZE);
  2197. spin_lock_irq(&zone->lru_lock);
  2198. for (scan = 0; scan < batch_size; scan++) {
  2199. struct page *page = lru_to_page(l_unevictable);
  2200. if (!trylock_page(page))
  2201. continue;
  2202. prefetchw_prev_lru_page(page, l_unevictable, flags);
  2203. if (likely(PageLRU(page) && PageUnevictable(page)))
  2204. check_move_unevictable_page(page, zone);
  2205. unlock_page(page);
  2206. }
  2207. spin_unlock_irq(&zone->lru_lock);
  2208. nr_to_scan -= batch_size;
  2209. }
  2210. }
  2211. /**
  2212. * scan_all_zones_unevictable_pages - scan all unevictable lists for evictable pages
  2213. *
  2214. * A really big hammer: scan all zones' unevictable LRU lists to check for
  2215. * pages that have become evictable. Move those back to the zones'
  2216. * inactive list where they become candidates for reclaim.
  2217. * This occurs when, e.g., we have unswappable pages on the unevictable lists,
  2218. * and we add swap to the system. As such, it runs in the context of a task
  2219. * that has possibly/probably made some previously unevictable pages
  2220. * evictable.
  2221. */
  2222. static void scan_all_zones_unevictable_pages(void)
  2223. {
  2224. struct zone *zone;
  2225. for_each_zone(zone) {
  2226. scan_zone_unevictable_pages(zone);
  2227. }
  2228. }
  2229. /*
  2230. * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of
  2231. * all nodes' unevictable lists for evictable pages
  2232. */
  2233. unsigned long scan_unevictable_pages;
  2234. int scan_unevictable_handler(struct ctl_table *table, int write,
  2235. struct file *file, void __user *buffer,
  2236. size_t *length, loff_t *ppos)
  2237. {
  2238. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  2239. if (write && *(unsigned long *)table->data)
  2240. scan_all_zones_unevictable_pages();
  2241. scan_unevictable_pages = 0;
  2242. return 0;
  2243. }
  2244. /*
  2245. * per node 'scan_unevictable_pages' attribute. On demand re-scan of
  2246. * a specified node's per zone unevictable lists for evictable pages.
  2247. */
  2248. static ssize_t read_scan_unevictable_node(struct sys_device *dev,
  2249. struct sysdev_attribute *attr,
  2250. char *buf)
  2251. {
  2252. return sprintf(buf, "0\n"); /* always zero; should fit... */
  2253. }
  2254. static ssize_t write_scan_unevictable_node(struct sys_device *dev,
  2255. struct sysdev_attribute *attr,
  2256. const char *buf, size_t count)
  2257. {
  2258. struct zone *node_zones = NODE_DATA(dev->id)->node_zones;
  2259. struct zone *zone;
  2260. unsigned long res;
  2261. unsigned long req = strict_strtoul(buf, 10, &res);
  2262. if (!req)
  2263. return 1; /* zero is no-op */
  2264. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  2265. if (!populated_zone(zone))
  2266. continue;
  2267. scan_zone_unevictable_pages(zone);
  2268. }
  2269. return 1;
  2270. }
  2271. static SYSDEV_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR,
  2272. read_scan_unevictable_node,
  2273. write_scan_unevictable_node);
  2274. int scan_unevictable_register_node(struct node *node)
  2275. {
  2276. return sysdev_create_file(&node->sysdev, &attr_scan_unevictable_pages);
  2277. }
  2278. void scan_unevictable_unregister_node(struct node *node)
  2279. {
  2280. sysdev_remove_file(&node->sysdev, &attr_scan_unevictable_pages);
  2281. }
  2282. #endif