vmscan.c 70 KB

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