vmscan.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624
  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. switch (try_to_munlock(page)) {
  546. case SWAP_FAIL: /* shouldn't happen */
  547. case SWAP_AGAIN:
  548. goto keep_locked;
  549. case SWAP_MLOCK:
  550. goto cull_mlocked;
  551. case SWAP_SUCCESS:
  552. ; /* fall thru'; add to swap cache */
  553. }
  554. if (!add_to_swap(page, GFP_ATOMIC))
  555. goto activate_locked;
  556. }
  557. #endif /* CONFIG_SWAP */
  558. mapping = page_mapping(page);
  559. /*
  560. * The page is mapped into the page tables of one or more
  561. * processes. Try to unmap it here.
  562. */
  563. if (page_mapped(page) && mapping) {
  564. switch (try_to_unmap(page, 0)) {
  565. case SWAP_FAIL:
  566. goto activate_locked;
  567. case SWAP_AGAIN:
  568. goto keep_locked;
  569. case SWAP_MLOCK:
  570. goto cull_mlocked;
  571. case SWAP_SUCCESS:
  572. ; /* try to free the page below */
  573. }
  574. }
  575. if (PageDirty(page)) {
  576. if (sc->order <= PAGE_ALLOC_COSTLY_ORDER && referenced)
  577. goto keep_locked;
  578. if (!may_enter_fs)
  579. goto keep_locked;
  580. if (!sc->may_writepage)
  581. goto keep_locked;
  582. /* Page is dirty, try to write it out here */
  583. switch (pageout(page, mapping, sync_writeback)) {
  584. case PAGE_KEEP:
  585. goto keep_locked;
  586. case PAGE_ACTIVATE:
  587. goto activate_locked;
  588. case PAGE_SUCCESS:
  589. if (PageWriteback(page) || PageDirty(page))
  590. goto keep;
  591. /*
  592. * A synchronous write - probably a ramdisk. Go
  593. * ahead and try to reclaim the page.
  594. */
  595. if (!trylock_page(page))
  596. goto keep;
  597. if (PageDirty(page) || PageWriteback(page))
  598. goto keep_locked;
  599. mapping = page_mapping(page);
  600. case PAGE_CLEAN:
  601. ; /* try to free the page below */
  602. }
  603. }
  604. /*
  605. * If the page has buffers, try to free the buffer mappings
  606. * associated with this page. If we succeed we try to free
  607. * the page as well.
  608. *
  609. * We do this even if the page is PageDirty().
  610. * try_to_release_page() does not perform I/O, but it is
  611. * possible for a page to have PageDirty set, but it is actually
  612. * clean (all its buffers are clean). This happens if the
  613. * buffers were written out directly, with submit_bh(). ext3
  614. * will do this, as well as the blockdev mapping.
  615. * try_to_release_page() will discover that cleanness and will
  616. * drop the buffers and mark the page clean - it can be freed.
  617. *
  618. * Rarely, pages can have buffers and no ->mapping. These are
  619. * the pages which were not successfully invalidated in
  620. * truncate_complete_page(). We try to drop those buffers here
  621. * and if that worked, and the page is no longer mapped into
  622. * process address space (page_count == 1) it can be freed.
  623. * Otherwise, leave the page on the LRU so it is swappable.
  624. */
  625. if (PagePrivate(page)) {
  626. if (!try_to_release_page(page, sc->gfp_mask))
  627. goto activate_locked;
  628. if (!mapping && page_count(page) == 1) {
  629. unlock_page(page);
  630. if (put_page_testzero(page))
  631. goto free_it;
  632. else {
  633. /*
  634. * rare race with speculative reference.
  635. * the speculative reference will free
  636. * this page shortly, so we may
  637. * increment nr_reclaimed here (and
  638. * leave it off the LRU).
  639. */
  640. nr_reclaimed++;
  641. continue;
  642. }
  643. }
  644. }
  645. if (!mapping || !__remove_mapping(mapping, page))
  646. goto keep_locked;
  647. /*
  648. * At this point, we have no other references and there is
  649. * no way to pick any more up (removed from LRU, removed
  650. * from pagecache). Can use non-atomic bitops now (and
  651. * we obviously don't have to worry about waking up a process
  652. * waiting on the page lock, because there are no references.
  653. */
  654. __clear_page_locked(page);
  655. free_it:
  656. nr_reclaimed++;
  657. if (!pagevec_add(&freed_pvec, page)) {
  658. __pagevec_free(&freed_pvec);
  659. pagevec_reinit(&freed_pvec);
  660. }
  661. continue;
  662. cull_mlocked:
  663. unlock_page(page);
  664. putback_lru_page(page);
  665. continue;
  666. activate_locked:
  667. /* Not a candidate for swapping, so reclaim swap space. */
  668. if (PageSwapCache(page) && vm_swap_full())
  669. remove_exclusive_swap_page_ref(page);
  670. VM_BUG_ON(PageActive(page));
  671. SetPageActive(page);
  672. pgactivate++;
  673. keep_locked:
  674. unlock_page(page);
  675. keep:
  676. list_add(&page->lru, &ret_pages);
  677. VM_BUG_ON(PageLRU(page) || PageUnevictable(page));
  678. }
  679. list_splice(&ret_pages, page_list);
  680. if (pagevec_count(&freed_pvec))
  681. __pagevec_free(&freed_pvec);
  682. count_vm_events(PGACTIVATE, pgactivate);
  683. return nr_reclaimed;
  684. }
  685. /* LRU Isolation modes. */
  686. #define ISOLATE_INACTIVE 0 /* Isolate inactive pages. */
  687. #define ISOLATE_ACTIVE 1 /* Isolate active pages. */
  688. #define ISOLATE_BOTH 2 /* Isolate both active and inactive pages. */
  689. /*
  690. * Attempt to remove the specified page from its LRU. Only take this page
  691. * if it is of the appropriate PageActive status. Pages which are being
  692. * freed elsewhere are also ignored.
  693. *
  694. * page: page to consider
  695. * mode: one of the LRU isolation modes defined above
  696. *
  697. * returns 0 on success, -ve errno on failure.
  698. */
  699. int __isolate_lru_page(struct page *page, int mode, int file)
  700. {
  701. int ret = -EINVAL;
  702. /* Only take pages on the LRU. */
  703. if (!PageLRU(page))
  704. return ret;
  705. /*
  706. * When checking the active state, we need to be sure we are
  707. * dealing with comparible boolean values. Take the logical not
  708. * of each.
  709. */
  710. if (mode != ISOLATE_BOTH && (!PageActive(page) != !mode))
  711. return ret;
  712. if (mode != ISOLATE_BOTH && (!page_is_file_cache(page) != !file))
  713. return ret;
  714. /*
  715. * When this function is being called for lumpy reclaim, we
  716. * initially look into all LRU pages, active, inactive and
  717. * unevictable; only give shrink_page_list evictable pages.
  718. */
  719. if (PageUnevictable(page))
  720. return ret;
  721. ret = -EBUSY;
  722. if (likely(get_page_unless_zero(page))) {
  723. /*
  724. * Be careful not to clear PageLRU until after we're
  725. * sure the page is not being freed elsewhere -- the
  726. * page release code relies on it.
  727. */
  728. ClearPageLRU(page);
  729. ret = 0;
  730. }
  731. return ret;
  732. }
  733. /*
  734. * zone->lru_lock is heavily contended. Some of the functions that
  735. * shrink the lists perform better by taking out a batch of pages
  736. * and working on them outside the LRU lock.
  737. *
  738. * For pagecache intensive workloads, this function is the hottest
  739. * spot in the kernel (apart from copy_*_user functions).
  740. *
  741. * Appropriate locks must be held before calling this function.
  742. *
  743. * @nr_to_scan: The number of pages to look through on the list.
  744. * @src: The LRU list to pull pages off.
  745. * @dst: The temp list to put pages on to.
  746. * @scanned: The number of pages that were scanned.
  747. * @order: The caller's attempted allocation order
  748. * @mode: One of the LRU isolation modes
  749. * @file: True [1] if isolating file [!anon] pages
  750. *
  751. * returns how many pages were moved onto *@dst.
  752. */
  753. static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
  754. struct list_head *src, struct list_head *dst,
  755. unsigned long *scanned, int order, int mode, int file)
  756. {
  757. unsigned long nr_taken = 0;
  758. unsigned long scan;
  759. for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
  760. struct page *page;
  761. unsigned long pfn;
  762. unsigned long end_pfn;
  763. unsigned long page_pfn;
  764. int zone_id;
  765. page = lru_to_page(src);
  766. prefetchw_prev_lru_page(page, src, flags);
  767. VM_BUG_ON(!PageLRU(page));
  768. switch (__isolate_lru_page(page, mode, file)) {
  769. case 0:
  770. list_move(&page->lru, dst);
  771. nr_taken++;
  772. break;
  773. case -EBUSY:
  774. /* else it is being freed elsewhere */
  775. list_move(&page->lru, src);
  776. continue;
  777. default:
  778. BUG();
  779. }
  780. if (!order)
  781. continue;
  782. /*
  783. * Attempt to take all pages in the order aligned region
  784. * surrounding the tag page. Only take those pages of
  785. * the same active state as that tag page. We may safely
  786. * round the target page pfn down to the requested order
  787. * as the mem_map is guarenteed valid out to MAX_ORDER,
  788. * where that page is in a different zone we will detect
  789. * it from its zone id and abort this block scan.
  790. */
  791. zone_id = page_zone_id(page);
  792. page_pfn = page_to_pfn(page);
  793. pfn = page_pfn & ~((1 << order) - 1);
  794. end_pfn = pfn + (1 << order);
  795. for (; pfn < end_pfn; pfn++) {
  796. struct page *cursor_page;
  797. /* The target page is in the block, ignore it. */
  798. if (unlikely(pfn == page_pfn))
  799. continue;
  800. /* Avoid holes within the zone. */
  801. if (unlikely(!pfn_valid_within(pfn)))
  802. break;
  803. cursor_page = pfn_to_page(pfn);
  804. /* Check that we have not crossed a zone boundary. */
  805. if (unlikely(page_zone_id(cursor_page) != zone_id))
  806. continue;
  807. switch (__isolate_lru_page(cursor_page, mode, file)) {
  808. case 0:
  809. list_move(&cursor_page->lru, dst);
  810. nr_taken++;
  811. scan++;
  812. break;
  813. case -EBUSY:
  814. /* else it is being freed elsewhere */
  815. list_move(&cursor_page->lru, src);
  816. default:
  817. break; /* ! on LRU or wrong list */
  818. }
  819. }
  820. }
  821. *scanned = scan;
  822. return nr_taken;
  823. }
  824. static unsigned long isolate_pages_global(unsigned long nr,
  825. struct list_head *dst,
  826. unsigned long *scanned, int order,
  827. int mode, struct zone *z,
  828. struct mem_cgroup *mem_cont,
  829. int active, int file)
  830. {
  831. int lru = LRU_BASE;
  832. if (active)
  833. lru += LRU_ACTIVE;
  834. if (file)
  835. lru += LRU_FILE;
  836. return isolate_lru_pages(nr, &z->lru[lru].list, dst, scanned, order,
  837. mode, !!file);
  838. }
  839. /*
  840. * clear_active_flags() is a helper for shrink_active_list(), clearing
  841. * any active bits from the pages in the list.
  842. */
  843. static unsigned long clear_active_flags(struct list_head *page_list,
  844. unsigned int *count)
  845. {
  846. int nr_active = 0;
  847. int lru;
  848. struct page *page;
  849. list_for_each_entry(page, page_list, lru) {
  850. lru = page_is_file_cache(page);
  851. if (PageActive(page)) {
  852. lru += LRU_ACTIVE;
  853. ClearPageActive(page);
  854. nr_active++;
  855. }
  856. count[lru]++;
  857. }
  858. return nr_active;
  859. }
  860. /**
  861. * isolate_lru_page - tries to isolate a page from its LRU list
  862. * @page: page to isolate from its LRU list
  863. *
  864. * Isolates a @page from an LRU list, clears PageLRU and adjusts the
  865. * vmstat statistic corresponding to whatever LRU list the page was on.
  866. *
  867. * Returns 0 if the page was removed from an LRU list.
  868. * Returns -EBUSY if the page was not on an LRU list.
  869. *
  870. * The returned page will have PageLRU() cleared. If it was found on
  871. * the active list, it will have PageActive set. If it was found on
  872. * the unevictable list, it will have the PageUnevictable bit set. That flag
  873. * may need to be cleared by the caller before letting the page go.
  874. *
  875. * The vmstat statistic corresponding to the list on which the page was
  876. * found will be decremented.
  877. *
  878. * Restrictions:
  879. * (1) Must be called with an elevated refcount on the page. This is a
  880. * fundamentnal difference from isolate_lru_pages (which is called
  881. * without a stable reference).
  882. * (2) the lru_lock must not be held.
  883. * (3) interrupts must be enabled.
  884. */
  885. int isolate_lru_page(struct page *page)
  886. {
  887. int ret = -EBUSY;
  888. if (PageLRU(page)) {
  889. struct zone *zone = page_zone(page);
  890. spin_lock_irq(&zone->lru_lock);
  891. if (PageLRU(page) && get_page_unless_zero(page)) {
  892. int lru = page_lru(page);
  893. ret = 0;
  894. ClearPageLRU(page);
  895. del_page_from_lru_list(zone, page, lru);
  896. }
  897. spin_unlock_irq(&zone->lru_lock);
  898. }
  899. return ret;
  900. }
  901. /*
  902. * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
  903. * of reclaimed pages
  904. */
  905. static unsigned long shrink_inactive_list(unsigned long max_scan,
  906. struct zone *zone, struct scan_control *sc,
  907. int priority, int file)
  908. {
  909. LIST_HEAD(page_list);
  910. struct pagevec pvec;
  911. unsigned long nr_scanned = 0;
  912. unsigned long nr_reclaimed = 0;
  913. pagevec_init(&pvec, 1);
  914. lru_add_drain();
  915. spin_lock_irq(&zone->lru_lock);
  916. do {
  917. struct page *page;
  918. unsigned long nr_taken;
  919. unsigned long nr_scan;
  920. unsigned long nr_freed;
  921. unsigned long nr_active;
  922. unsigned int count[NR_LRU_LISTS] = { 0, };
  923. int mode = ISOLATE_INACTIVE;
  924. /*
  925. * If we need a large contiguous chunk of memory, or have
  926. * trouble getting a small set of contiguous pages, we
  927. * will reclaim both active and inactive pages.
  928. *
  929. * We use the same threshold as pageout congestion_wait below.
  930. */
  931. if (sc->order > PAGE_ALLOC_COSTLY_ORDER)
  932. mode = ISOLATE_BOTH;
  933. else if (sc->order && priority < DEF_PRIORITY - 2)
  934. mode = ISOLATE_BOTH;
  935. nr_taken = sc->isolate_pages(sc->swap_cluster_max,
  936. &page_list, &nr_scan, sc->order, mode,
  937. zone, sc->mem_cgroup, 0, file);
  938. nr_active = clear_active_flags(&page_list, count);
  939. __count_vm_events(PGDEACTIVATE, nr_active);
  940. __mod_zone_page_state(zone, NR_ACTIVE_FILE,
  941. -count[LRU_ACTIVE_FILE]);
  942. __mod_zone_page_state(zone, NR_INACTIVE_FILE,
  943. -count[LRU_INACTIVE_FILE]);
  944. __mod_zone_page_state(zone, NR_ACTIVE_ANON,
  945. -count[LRU_ACTIVE_ANON]);
  946. __mod_zone_page_state(zone, NR_INACTIVE_ANON,
  947. -count[LRU_INACTIVE_ANON]);
  948. if (scan_global_lru(sc)) {
  949. zone->pages_scanned += nr_scan;
  950. zone->recent_scanned[0] += count[LRU_INACTIVE_ANON];
  951. zone->recent_scanned[0] += count[LRU_ACTIVE_ANON];
  952. zone->recent_scanned[1] += count[LRU_INACTIVE_FILE];
  953. zone->recent_scanned[1] += count[LRU_ACTIVE_FILE];
  954. }
  955. spin_unlock_irq(&zone->lru_lock);
  956. nr_scanned += nr_scan;
  957. nr_freed = shrink_page_list(&page_list, sc, PAGEOUT_IO_ASYNC);
  958. /*
  959. * If we are direct reclaiming for contiguous pages and we do
  960. * not reclaim everything in the list, try again and wait
  961. * for IO to complete. This will stall high-order allocations
  962. * but that should be acceptable to the caller
  963. */
  964. if (nr_freed < nr_taken && !current_is_kswapd() &&
  965. sc->order > PAGE_ALLOC_COSTLY_ORDER) {
  966. congestion_wait(WRITE, HZ/10);
  967. /*
  968. * The attempt at page out may have made some
  969. * of the pages active, mark them inactive again.
  970. */
  971. nr_active = clear_active_flags(&page_list, count);
  972. count_vm_events(PGDEACTIVATE, nr_active);
  973. nr_freed += shrink_page_list(&page_list, sc,
  974. PAGEOUT_IO_SYNC);
  975. }
  976. nr_reclaimed += nr_freed;
  977. local_irq_disable();
  978. if (current_is_kswapd()) {
  979. __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan);
  980. __count_vm_events(KSWAPD_STEAL, nr_freed);
  981. } else if (scan_global_lru(sc))
  982. __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan);
  983. __count_zone_vm_events(PGSTEAL, zone, nr_freed);
  984. if (nr_taken == 0)
  985. goto done;
  986. spin_lock(&zone->lru_lock);
  987. /*
  988. * Put back any unfreeable pages.
  989. */
  990. while (!list_empty(&page_list)) {
  991. int lru;
  992. page = lru_to_page(&page_list);
  993. VM_BUG_ON(PageLRU(page));
  994. list_del(&page->lru);
  995. if (unlikely(!page_evictable(page, NULL))) {
  996. spin_unlock_irq(&zone->lru_lock);
  997. putback_lru_page(page);
  998. spin_lock_irq(&zone->lru_lock);
  999. continue;
  1000. }
  1001. SetPageLRU(page);
  1002. lru = page_lru(page);
  1003. add_page_to_lru_list(zone, page, lru);
  1004. mem_cgroup_move_lists(page, lru);
  1005. if (PageActive(page) && scan_global_lru(sc)) {
  1006. int file = !!page_is_file_cache(page);
  1007. zone->recent_rotated[file]++;
  1008. }
  1009. if (!pagevec_add(&pvec, page)) {
  1010. spin_unlock_irq(&zone->lru_lock);
  1011. __pagevec_release(&pvec);
  1012. spin_lock_irq(&zone->lru_lock);
  1013. }
  1014. }
  1015. } while (nr_scanned < max_scan);
  1016. spin_unlock(&zone->lru_lock);
  1017. done:
  1018. local_irq_enable();
  1019. pagevec_release(&pvec);
  1020. return nr_reclaimed;
  1021. }
  1022. /*
  1023. * We are about to scan this zone at a certain priority level. If that priority
  1024. * level is smaller (ie: more urgent) than the previous priority, then note
  1025. * that priority level within the zone. This is done so that when the next
  1026. * process comes in to scan this zone, it will immediately start out at this
  1027. * priority level rather than having to build up its own scanning priority.
  1028. * Here, this priority affects only the reclaim-mapped threshold.
  1029. */
  1030. static inline void note_zone_scanning_priority(struct zone *zone, int priority)
  1031. {
  1032. if (priority < zone->prev_priority)
  1033. zone->prev_priority = priority;
  1034. }
  1035. static inline int zone_is_near_oom(struct zone *zone)
  1036. {
  1037. return zone->pages_scanned >= (zone_lru_pages(zone) * 3);
  1038. }
  1039. /*
  1040. * This moves pages from the active list to the inactive list.
  1041. *
  1042. * We move them the other way if the page is referenced by one or more
  1043. * processes, from rmap.
  1044. *
  1045. * If the pages are mostly unmapped, the processing is fast and it is
  1046. * appropriate to hold zone->lru_lock across the whole operation. But if
  1047. * the pages are mapped, the processing is slow (page_referenced()) so we
  1048. * should drop zone->lru_lock around each page. It's impossible to balance
  1049. * this, so instead we remove the pages from the LRU while processing them.
  1050. * It is safe to rely on PG_active against the non-LRU pages in here because
  1051. * nobody will play with that bit on a non-LRU page.
  1052. *
  1053. * The downside is that we have to touch page->_count against each page.
  1054. * But we had to alter page->flags anyway.
  1055. */
  1056. static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
  1057. struct scan_control *sc, int priority, int file)
  1058. {
  1059. unsigned long pgmoved;
  1060. int pgdeactivate = 0;
  1061. unsigned long pgscanned;
  1062. LIST_HEAD(l_hold); /* The pages which were snipped off */
  1063. LIST_HEAD(l_inactive);
  1064. struct page *page;
  1065. struct pagevec pvec;
  1066. enum lru_list lru;
  1067. lru_add_drain();
  1068. spin_lock_irq(&zone->lru_lock);
  1069. pgmoved = sc->isolate_pages(nr_pages, &l_hold, &pgscanned, sc->order,
  1070. ISOLATE_ACTIVE, zone,
  1071. sc->mem_cgroup, 1, file);
  1072. /*
  1073. * zone->pages_scanned is used for detect zone's oom
  1074. * mem_cgroup remembers nr_scan by itself.
  1075. */
  1076. if (scan_global_lru(sc)) {
  1077. zone->pages_scanned += pgscanned;
  1078. zone->recent_scanned[!!file] += pgmoved;
  1079. }
  1080. if (file)
  1081. __mod_zone_page_state(zone, NR_ACTIVE_FILE, -pgmoved);
  1082. else
  1083. __mod_zone_page_state(zone, NR_ACTIVE_ANON, -pgmoved);
  1084. spin_unlock_irq(&zone->lru_lock);
  1085. pgmoved = 0;
  1086. while (!list_empty(&l_hold)) {
  1087. cond_resched();
  1088. page = lru_to_page(&l_hold);
  1089. list_del(&page->lru);
  1090. if (unlikely(!page_evictable(page, NULL))) {
  1091. putback_lru_page(page);
  1092. continue;
  1093. }
  1094. /* page_referenced clears PageReferenced */
  1095. if (page_mapping_inuse(page) &&
  1096. page_referenced(page, 0, sc->mem_cgroup))
  1097. pgmoved++;
  1098. list_add(&page->lru, &l_inactive);
  1099. }
  1100. /*
  1101. * Count referenced pages from currently used mappings as
  1102. * rotated, even though they are moved to the inactive list.
  1103. * This helps balance scan pressure between file and anonymous
  1104. * pages in get_scan_ratio.
  1105. */
  1106. zone->recent_rotated[!!file] += pgmoved;
  1107. /*
  1108. * Move the pages to the [file or anon] inactive list.
  1109. */
  1110. pagevec_init(&pvec, 1);
  1111. pgmoved = 0;
  1112. lru = LRU_BASE + file * LRU_FILE;
  1113. spin_lock_irq(&zone->lru_lock);
  1114. while (!list_empty(&l_inactive)) {
  1115. page = lru_to_page(&l_inactive);
  1116. prefetchw_prev_lru_page(page, &l_inactive, flags);
  1117. VM_BUG_ON(PageLRU(page));
  1118. SetPageLRU(page);
  1119. VM_BUG_ON(!PageActive(page));
  1120. ClearPageActive(page);
  1121. list_move(&page->lru, &zone->lru[lru].list);
  1122. mem_cgroup_move_lists(page, lru);
  1123. pgmoved++;
  1124. if (!pagevec_add(&pvec, page)) {
  1125. __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
  1126. spin_unlock_irq(&zone->lru_lock);
  1127. pgdeactivate += pgmoved;
  1128. pgmoved = 0;
  1129. if (buffer_heads_over_limit)
  1130. pagevec_strip(&pvec);
  1131. __pagevec_release(&pvec);
  1132. spin_lock_irq(&zone->lru_lock);
  1133. }
  1134. }
  1135. __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
  1136. pgdeactivate += pgmoved;
  1137. if (buffer_heads_over_limit) {
  1138. spin_unlock_irq(&zone->lru_lock);
  1139. pagevec_strip(&pvec);
  1140. spin_lock_irq(&zone->lru_lock);
  1141. }
  1142. __count_zone_vm_events(PGREFILL, zone, pgscanned);
  1143. __count_vm_events(PGDEACTIVATE, pgdeactivate);
  1144. spin_unlock_irq(&zone->lru_lock);
  1145. if (vm_swap_full())
  1146. pagevec_swap_free(&pvec);
  1147. pagevec_release(&pvec);
  1148. }
  1149. static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
  1150. struct zone *zone, struct scan_control *sc, int priority)
  1151. {
  1152. int file = is_file_lru(lru);
  1153. if (lru == LRU_ACTIVE_FILE) {
  1154. shrink_active_list(nr_to_scan, zone, sc, priority, file);
  1155. return 0;
  1156. }
  1157. if (lru == LRU_ACTIVE_ANON &&
  1158. (!scan_global_lru(sc) || inactive_anon_is_low(zone))) {
  1159. shrink_active_list(nr_to_scan, zone, sc, priority, file);
  1160. return 0;
  1161. }
  1162. return shrink_inactive_list(nr_to_scan, zone, sc, priority, file);
  1163. }
  1164. /*
  1165. * Determine how aggressively the anon and file LRU lists should be
  1166. * scanned. The relative value of each set of LRU lists is determined
  1167. * by looking at the fraction of the pages scanned we did rotate back
  1168. * onto the active list instead of evict.
  1169. *
  1170. * percent[0] specifies how much pressure to put on ram/swap backed
  1171. * memory, while percent[1] determines pressure on the file LRUs.
  1172. */
  1173. static void get_scan_ratio(struct zone *zone, struct scan_control *sc,
  1174. unsigned long *percent)
  1175. {
  1176. unsigned long anon, file, free;
  1177. unsigned long anon_prio, file_prio;
  1178. unsigned long ap, fp;
  1179. anon = zone_page_state(zone, NR_ACTIVE_ANON) +
  1180. zone_page_state(zone, NR_INACTIVE_ANON);
  1181. file = zone_page_state(zone, NR_ACTIVE_FILE) +
  1182. zone_page_state(zone, NR_INACTIVE_FILE);
  1183. free = zone_page_state(zone, NR_FREE_PAGES);
  1184. /* If we have no swap space, do not bother scanning anon pages. */
  1185. if (nr_swap_pages <= 0) {
  1186. percent[0] = 0;
  1187. percent[1] = 100;
  1188. return;
  1189. }
  1190. /* If we have very few page cache pages, force-scan anon pages. */
  1191. if (unlikely(file + free <= zone->pages_high)) {
  1192. percent[0] = 100;
  1193. percent[1] = 0;
  1194. return;
  1195. }
  1196. /*
  1197. * OK, so we have swap space and a fair amount of page cache
  1198. * pages. We use the recently rotated / recently scanned
  1199. * ratios to determine how valuable each cache is.
  1200. *
  1201. * Because workloads change over time (and to avoid overflow)
  1202. * we keep these statistics as a floating average, which ends
  1203. * up weighing recent references more than old ones.
  1204. *
  1205. * anon in [0], file in [1]
  1206. */
  1207. if (unlikely(zone->recent_scanned[0] > anon / 4)) {
  1208. spin_lock_irq(&zone->lru_lock);
  1209. zone->recent_scanned[0] /= 2;
  1210. zone->recent_rotated[0] /= 2;
  1211. spin_unlock_irq(&zone->lru_lock);
  1212. }
  1213. if (unlikely(zone->recent_scanned[1] > file / 4)) {
  1214. spin_lock_irq(&zone->lru_lock);
  1215. zone->recent_scanned[1] /= 2;
  1216. zone->recent_rotated[1] /= 2;
  1217. spin_unlock_irq(&zone->lru_lock);
  1218. }
  1219. /*
  1220. * With swappiness at 100, anonymous and file have the same priority.
  1221. * This scanning priority is essentially the inverse of IO cost.
  1222. */
  1223. anon_prio = sc->swappiness;
  1224. file_prio = 200 - sc->swappiness;
  1225. /*
  1226. * anon recent_rotated[0]
  1227. * %anon = 100 * ----------- / ----------------- * IO cost
  1228. * anon + file rotate_sum
  1229. */
  1230. ap = (anon_prio + 1) * (zone->recent_scanned[0] + 1);
  1231. ap /= zone->recent_rotated[0] + 1;
  1232. fp = (file_prio + 1) * (zone->recent_scanned[1] + 1);
  1233. fp /= zone->recent_rotated[1] + 1;
  1234. /* Normalize to percentages */
  1235. percent[0] = 100 * ap / (ap + fp + 1);
  1236. percent[1] = 100 - percent[0];
  1237. }
  1238. /*
  1239. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  1240. */
  1241. static unsigned long shrink_zone(int priority, struct zone *zone,
  1242. struct scan_control *sc)
  1243. {
  1244. unsigned long nr[NR_LRU_LISTS];
  1245. unsigned long nr_to_scan;
  1246. unsigned long nr_reclaimed = 0;
  1247. unsigned long percent[2]; /* anon @ 0; file @ 1 */
  1248. enum lru_list l;
  1249. get_scan_ratio(zone, sc, percent);
  1250. for_each_evictable_lru(l) {
  1251. if (scan_global_lru(sc)) {
  1252. int file = is_file_lru(l);
  1253. int scan;
  1254. scan = zone_page_state(zone, NR_LRU_BASE + l);
  1255. if (priority) {
  1256. scan >>= priority;
  1257. scan = (scan * percent[file]) / 100;
  1258. }
  1259. zone->lru[l].nr_scan += scan;
  1260. nr[l] = zone->lru[l].nr_scan;
  1261. if (nr[l] >= sc->swap_cluster_max)
  1262. zone->lru[l].nr_scan = 0;
  1263. else
  1264. nr[l] = 0;
  1265. } else {
  1266. /*
  1267. * This reclaim occurs not because zone memory shortage
  1268. * but because memory controller hits its limit.
  1269. * Don't modify zone reclaim related data.
  1270. */
  1271. nr[l] = mem_cgroup_calc_reclaim(sc->mem_cgroup, zone,
  1272. priority, l);
  1273. }
  1274. }
  1275. while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
  1276. nr[LRU_INACTIVE_FILE]) {
  1277. for_each_evictable_lru(l) {
  1278. if (nr[l]) {
  1279. nr_to_scan = min(nr[l],
  1280. (unsigned long)sc->swap_cluster_max);
  1281. nr[l] -= nr_to_scan;
  1282. nr_reclaimed += shrink_list(l, nr_to_scan,
  1283. zone, sc, priority);
  1284. }
  1285. }
  1286. }
  1287. /*
  1288. * Even if we did not try to evict anon pages at all, we want to
  1289. * rebalance the anon lru active/inactive ratio.
  1290. */
  1291. if (!scan_global_lru(sc) || inactive_anon_is_low(zone))
  1292. shrink_active_list(SWAP_CLUSTER_MAX, zone, sc, priority, 0);
  1293. else if (!scan_global_lru(sc))
  1294. shrink_active_list(SWAP_CLUSTER_MAX, zone, sc, priority, 0);
  1295. throttle_vm_writeout(sc->gfp_mask);
  1296. return nr_reclaimed;
  1297. }
  1298. /*
  1299. * This is the direct reclaim path, for page-allocating processes. We only
  1300. * try to reclaim pages from zones which will satisfy the caller's allocation
  1301. * request.
  1302. *
  1303. * We reclaim from a zone even if that zone is over pages_high. Because:
  1304. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  1305. * allocation or
  1306. * b) The zones may be over pages_high but they must go *over* pages_high to
  1307. * satisfy the `incremental min' zone defense algorithm.
  1308. *
  1309. * Returns the number of reclaimed pages.
  1310. *
  1311. * If a zone is deemed to be full of pinned pages then just give it a light
  1312. * scan then give up on it.
  1313. */
  1314. static unsigned long shrink_zones(int priority, struct zonelist *zonelist,
  1315. struct scan_control *sc)
  1316. {
  1317. enum zone_type high_zoneidx = gfp_zone(sc->gfp_mask);
  1318. unsigned long nr_reclaimed = 0;
  1319. struct zoneref *z;
  1320. struct zone *zone;
  1321. sc->all_unreclaimable = 1;
  1322. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  1323. if (!populated_zone(zone))
  1324. continue;
  1325. /*
  1326. * Take care memory controller reclaiming has small influence
  1327. * to global LRU.
  1328. */
  1329. if (scan_global_lru(sc)) {
  1330. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1331. continue;
  1332. note_zone_scanning_priority(zone, priority);
  1333. if (zone_is_all_unreclaimable(zone) &&
  1334. priority != DEF_PRIORITY)
  1335. continue; /* Let kswapd poll it */
  1336. sc->all_unreclaimable = 0;
  1337. } else {
  1338. /*
  1339. * Ignore cpuset limitation here. We just want to reduce
  1340. * # of used pages by us regardless of memory shortage.
  1341. */
  1342. sc->all_unreclaimable = 0;
  1343. mem_cgroup_note_reclaim_priority(sc->mem_cgroup,
  1344. priority);
  1345. }
  1346. nr_reclaimed += shrink_zone(priority, zone, sc);
  1347. }
  1348. return nr_reclaimed;
  1349. }
  1350. /*
  1351. * This is the main entry point to direct page reclaim.
  1352. *
  1353. * If a full scan of the inactive list fails to free enough memory then we
  1354. * are "out of memory" and something needs to be killed.
  1355. *
  1356. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  1357. * high - the zone may be full of dirty or under-writeback pages, which this
  1358. * caller can't do much about. We kick pdflush and take explicit naps in the
  1359. * hope that some of these pages can be written. But if the allocating task
  1360. * holds filesystem locks which prevent writeout this might not work, and the
  1361. * allocation attempt will fail.
  1362. *
  1363. * returns: 0, if no pages reclaimed
  1364. * else, the number of pages reclaimed
  1365. */
  1366. static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
  1367. struct scan_control *sc)
  1368. {
  1369. int priority;
  1370. unsigned long ret = 0;
  1371. unsigned long total_scanned = 0;
  1372. unsigned long nr_reclaimed = 0;
  1373. struct reclaim_state *reclaim_state = current->reclaim_state;
  1374. unsigned long lru_pages = 0;
  1375. struct zoneref *z;
  1376. struct zone *zone;
  1377. enum zone_type high_zoneidx = gfp_zone(sc->gfp_mask);
  1378. delayacct_freepages_start();
  1379. if (scan_global_lru(sc))
  1380. count_vm_event(ALLOCSTALL);
  1381. /*
  1382. * mem_cgroup will not do shrink_slab.
  1383. */
  1384. if (scan_global_lru(sc)) {
  1385. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  1386. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1387. continue;
  1388. lru_pages += zone_lru_pages(zone);
  1389. }
  1390. }
  1391. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1392. sc->nr_scanned = 0;
  1393. if (!priority)
  1394. disable_swap_token();
  1395. nr_reclaimed += shrink_zones(priority, zonelist, sc);
  1396. /*
  1397. * Don't shrink slabs when reclaiming memory from
  1398. * over limit cgroups
  1399. */
  1400. if (scan_global_lru(sc)) {
  1401. shrink_slab(sc->nr_scanned, sc->gfp_mask, lru_pages);
  1402. if (reclaim_state) {
  1403. nr_reclaimed += reclaim_state->reclaimed_slab;
  1404. reclaim_state->reclaimed_slab = 0;
  1405. }
  1406. }
  1407. total_scanned += sc->nr_scanned;
  1408. if (nr_reclaimed >= sc->swap_cluster_max) {
  1409. ret = nr_reclaimed;
  1410. goto out;
  1411. }
  1412. /*
  1413. * Try to write back as many pages as we just scanned. This
  1414. * tends to cause slow streaming writers to write data to the
  1415. * disk smoothly, at the dirtying rate, which is nice. But
  1416. * that's undesirable in laptop mode, where we *want* lumpy
  1417. * writeout. So in laptop mode, write out the whole world.
  1418. */
  1419. if (total_scanned > sc->swap_cluster_max +
  1420. sc->swap_cluster_max / 2) {
  1421. wakeup_pdflush(laptop_mode ? 0 : total_scanned);
  1422. sc->may_writepage = 1;
  1423. }
  1424. /* Take a nap, wait for some writeback to complete */
  1425. if (sc->nr_scanned && priority < DEF_PRIORITY - 2)
  1426. congestion_wait(WRITE, HZ/10);
  1427. }
  1428. /* top priority shrink_zones still had more to do? don't OOM, then */
  1429. if (!sc->all_unreclaimable && scan_global_lru(sc))
  1430. ret = nr_reclaimed;
  1431. out:
  1432. /*
  1433. * Now that we've scanned all the zones at this priority level, note
  1434. * that level within the zone so that the next thread which performs
  1435. * scanning of this zone will immediately start out at this priority
  1436. * level. This affects only the decision whether or not to bring
  1437. * mapped pages onto the inactive list.
  1438. */
  1439. if (priority < 0)
  1440. priority = 0;
  1441. if (scan_global_lru(sc)) {
  1442. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  1443. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1444. continue;
  1445. zone->prev_priority = priority;
  1446. }
  1447. } else
  1448. mem_cgroup_record_reclaim_priority(sc->mem_cgroup, priority);
  1449. delayacct_freepages_end();
  1450. return ret;
  1451. }
  1452. unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
  1453. gfp_t gfp_mask)
  1454. {
  1455. struct scan_control sc = {
  1456. .gfp_mask = gfp_mask,
  1457. .may_writepage = !laptop_mode,
  1458. .swap_cluster_max = SWAP_CLUSTER_MAX,
  1459. .may_swap = 1,
  1460. .swappiness = vm_swappiness,
  1461. .order = order,
  1462. .mem_cgroup = NULL,
  1463. .isolate_pages = isolate_pages_global,
  1464. };
  1465. return do_try_to_free_pages(zonelist, &sc);
  1466. }
  1467. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  1468. unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont,
  1469. gfp_t gfp_mask)
  1470. {
  1471. struct scan_control sc = {
  1472. .may_writepage = !laptop_mode,
  1473. .may_swap = 1,
  1474. .swap_cluster_max = SWAP_CLUSTER_MAX,
  1475. .swappiness = vm_swappiness,
  1476. .order = 0,
  1477. .mem_cgroup = mem_cont,
  1478. .isolate_pages = mem_cgroup_isolate_pages,
  1479. };
  1480. struct zonelist *zonelist;
  1481. sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
  1482. (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
  1483. zonelist = NODE_DATA(numa_node_id())->node_zonelists;
  1484. return do_try_to_free_pages(zonelist, &sc);
  1485. }
  1486. #endif
  1487. /*
  1488. * For kswapd, balance_pgdat() will work across all this node's zones until
  1489. * they are all at pages_high.
  1490. *
  1491. * Returns the number of pages which were actually freed.
  1492. *
  1493. * There is special handling here for zones which are full of pinned pages.
  1494. * This can happen if the pages are all mlocked, or if they are all used by
  1495. * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
  1496. * What we do is to detect the case where all pages in the zone have been
  1497. * scanned twice and there has been zero successful reclaim. Mark the zone as
  1498. * dead and from now on, only perform a short scan. Basically we're polling
  1499. * the zone for when the problem goes away.
  1500. *
  1501. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  1502. * zones which have free_pages > pages_high, but once a zone is found to have
  1503. * free_pages <= pages_high, we scan that zone and the lower zones regardless
  1504. * of the number of free pages in the lower zones. This interoperates with
  1505. * the page allocator fallback scheme to ensure that aging of pages is balanced
  1506. * across the zones.
  1507. */
  1508. static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
  1509. {
  1510. int all_zones_ok;
  1511. int priority;
  1512. int i;
  1513. unsigned long total_scanned;
  1514. unsigned long nr_reclaimed;
  1515. struct reclaim_state *reclaim_state = current->reclaim_state;
  1516. struct scan_control sc = {
  1517. .gfp_mask = GFP_KERNEL,
  1518. .may_swap = 1,
  1519. .swap_cluster_max = SWAP_CLUSTER_MAX,
  1520. .swappiness = vm_swappiness,
  1521. .order = order,
  1522. .mem_cgroup = NULL,
  1523. .isolate_pages = isolate_pages_global,
  1524. };
  1525. /*
  1526. * temp_priority is used to remember the scanning priority at which
  1527. * this zone was successfully refilled to free_pages == pages_high.
  1528. */
  1529. int temp_priority[MAX_NR_ZONES];
  1530. loop_again:
  1531. total_scanned = 0;
  1532. nr_reclaimed = 0;
  1533. sc.may_writepage = !laptop_mode;
  1534. count_vm_event(PAGEOUTRUN);
  1535. for (i = 0; i < pgdat->nr_zones; i++)
  1536. temp_priority[i] = DEF_PRIORITY;
  1537. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1538. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  1539. unsigned long lru_pages = 0;
  1540. /* The swap token gets in the way of swapout... */
  1541. if (!priority)
  1542. disable_swap_token();
  1543. all_zones_ok = 1;
  1544. /*
  1545. * Scan in the highmem->dma direction for the highest
  1546. * zone which needs scanning
  1547. */
  1548. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  1549. struct zone *zone = pgdat->node_zones + i;
  1550. if (!populated_zone(zone))
  1551. continue;
  1552. if (zone_is_all_unreclaimable(zone) &&
  1553. priority != DEF_PRIORITY)
  1554. continue;
  1555. /*
  1556. * Do some background aging of the anon list, to give
  1557. * pages a chance to be referenced before reclaiming.
  1558. */
  1559. if (inactive_anon_is_low(zone))
  1560. shrink_active_list(SWAP_CLUSTER_MAX, zone,
  1561. &sc, priority, 0);
  1562. if (!zone_watermark_ok(zone, order, zone->pages_high,
  1563. 0, 0)) {
  1564. end_zone = i;
  1565. break;
  1566. }
  1567. }
  1568. if (i < 0)
  1569. goto out;
  1570. for (i = 0; i <= end_zone; i++) {
  1571. struct zone *zone = pgdat->node_zones + i;
  1572. lru_pages += zone_lru_pages(zone);
  1573. }
  1574. /*
  1575. * Now scan the zone in the dma->highmem direction, stopping
  1576. * at the last zone which needs scanning.
  1577. *
  1578. * We do this because the page allocator works in the opposite
  1579. * direction. This prevents the page allocator from allocating
  1580. * pages behind kswapd's direction of progress, which would
  1581. * cause too much scanning of the lower zones.
  1582. */
  1583. for (i = 0; i <= end_zone; i++) {
  1584. struct zone *zone = pgdat->node_zones + i;
  1585. int nr_slab;
  1586. if (!populated_zone(zone))
  1587. continue;
  1588. if (zone_is_all_unreclaimable(zone) &&
  1589. priority != DEF_PRIORITY)
  1590. continue;
  1591. if (!zone_watermark_ok(zone, order, zone->pages_high,
  1592. end_zone, 0))
  1593. all_zones_ok = 0;
  1594. temp_priority[i] = priority;
  1595. sc.nr_scanned = 0;
  1596. note_zone_scanning_priority(zone, priority);
  1597. /*
  1598. * We put equal pressure on every zone, unless one
  1599. * zone has way too many pages free already.
  1600. */
  1601. if (!zone_watermark_ok(zone, order, 8*zone->pages_high,
  1602. end_zone, 0))
  1603. nr_reclaimed += shrink_zone(priority, zone, &sc);
  1604. reclaim_state->reclaimed_slab = 0;
  1605. nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
  1606. lru_pages);
  1607. nr_reclaimed += reclaim_state->reclaimed_slab;
  1608. total_scanned += sc.nr_scanned;
  1609. if (zone_is_all_unreclaimable(zone))
  1610. continue;
  1611. if (nr_slab == 0 && zone->pages_scanned >=
  1612. (zone_lru_pages(zone) * 6))
  1613. zone_set_flag(zone,
  1614. ZONE_ALL_UNRECLAIMABLE);
  1615. /*
  1616. * If we've done a decent amount of scanning and
  1617. * the reclaim ratio is low, start doing writepage
  1618. * even in laptop mode
  1619. */
  1620. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  1621. total_scanned > nr_reclaimed + nr_reclaimed / 2)
  1622. sc.may_writepage = 1;
  1623. }
  1624. if (all_zones_ok)
  1625. break; /* kswapd: all done */
  1626. /*
  1627. * OK, kswapd is getting into trouble. Take a nap, then take
  1628. * another pass across the zones.
  1629. */
  1630. if (total_scanned && priority < DEF_PRIORITY - 2)
  1631. congestion_wait(WRITE, HZ/10);
  1632. /*
  1633. * We do this so kswapd doesn't build up large priorities for
  1634. * example when it is freeing in parallel with allocators. It
  1635. * matches the direct reclaim path behaviour in terms of impact
  1636. * on zone->*_priority.
  1637. */
  1638. if (nr_reclaimed >= SWAP_CLUSTER_MAX)
  1639. break;
  1640. }
  1641. out:
  1642. /*
  1643. * Note within each zone the priority level at which this zone was
  1644. * brought into a happy state. So that the next thread which scans this
  1645. * zone will start out at that priority level.
  1646. */
  1647. for (i = 0; i < pgdat->nr_zones; i++) {
  1648. struct zone *zone = pgdat->node_zones + i;
  1649. zone->prev_priority = temp_priority[i];
  1650. }
  1651. if (!all_zones_ok) {
  1652. cond_resched();
  1653. try_to_freeze();
  1654. goto loop_again;
  1655. }
  1656. return nr_reclaimed;
  1657. }
  1658. /*
  1659. * The background pageout daemon, started as a kernel thread
  1660. * from the init process.
  1661. *
  1662. * This basically trickles out pages so that we have _some_
  1663. * free memory available even if there is no other activity
  1664. * that frees anything up. This is needed for things like routing
  1665. * etc, where we otherwise might have all activity going on in
  1666. * asynchronous contexts that cannot page things out.
  1667. *
  1668. * If there are applications that are active memory-allocators
  1669. * (most normal use), this basically shouldn't matter.
  1670. */
  1671. static int kswapd(void *p)
  1672. {
  1673. unsigned long order;
  1674. pg_data_t *pgdat = (pg_data_t*)p;
  1675. struct task_struct *tsk = current;
  1676. DEFINE_WAIT(wait);
  1677. struct reclaim_state reclaim_state = {
  1678. .reclaimed_slab = 0,
  1679. };
  1680. node_to_cpumask_ptr(cpumask, pgdat->node_id);
  1681. if (!cpus_empty(*cpumask))
  1682. set_cpus_allowed_ptr(tsk, cpumask);
  1683. current->reclaim_state = &reclaim_state;
  1684. /*
  1685. * Tell the memory management that we're a "memory allocator",
  1686. * and that if we need more memory we should get access to it
  1687. * regardless (see "__alloc_pages()"). "kswapd" should
  1688. * never get caught in the normal page freeing logic.
  1689. *
  1690. * (Kswapd normally doesn't need memory anyway, but sometimes
  1691. * you need a small amount of memory in order to be able to
  1692. * page out something else, and this flag essentially protects
  1693. * us from recursively trying to free more memory as we're
  1694. * trying to free the first piece of memory in the first place).
  1695. */
  1696. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  1697. set_freezable();
  1698. order = 0;
  1699. for ( ; ; ) {
  1700. unsigned long new_order;
  1701. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  1702. new_order = pgdat->kswapd_max_order;
  1703. pgdat->kswapd_max_order = 0;
  1704. if (order < new_order) {
  1705. /*
  1706. * Don't sleep if someone wants a larger 'order'
  1707. * allocation
  1708. */
  1709. order = new_order;
  1710. } else {
  1711. if (!freezing(current))
  1712. schedule();
  1713. order = pgdat->kswapd_max_order;
  1714. }
  1715. finish_wait(&pgdat->kswapd_wait, &wait);
  1716. if (!try_to_freeze()) {
  1717. /* We can speed up thawing tasks if we don't call
  1718. * balance_pgdat after returning from the refrigerator
  1719. */
  1720. balance_pgdat(pgdat, order);
  1721. }
  1722. }
  1723. return 0;
  1724. }
  1725. /*
  1726. * A zone is low on free memory, so wake its kswapd task to service it.
  1727. */
  1728. void wakeup_kswapd(struct zone *zone, int order)
  1729. {
  1730. pg_data_t *pgdat;
  1731. if (!populated_zone(zone))
  1732. return;
  1733. pgdat = zone->zone_pgdat;
  1734. if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
  1735. return;
  1736. if (pgdat->kswapd_max_order < order)
  1737. pgdat->kswapd_max_order = order;
  1738. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1739. return;
  1740. if (!waitqueue_active(&pgdat->kswapd_wait))
  1741. return;
  1742. wake_up_interruptible(&pgdat->kswapd_wait);
  1743. }
  1744. unsigned long global_lru_pages(void)
  1745. {
  1746. return global_page_state(NR_ACTIVE_ANON)
  1747. + global_page_state(NR_ACTIVE_FILE)
  1748. + global_page_state(NR_INACTIVE_ANON)
  1749. + global_page_state(NR_INACTIVE_FILE);
  1750. }
  1751. #ifdef CONFIG_PM
  1752. /*
  1753. * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
  1754. * from LRU lists system-wide, for given pass and priority, and returns the
  1755. * number of reclaimed pages
  1756. *
  1757. * For pass > 3 we also try to shrink the LRU lists that contain a few pages
  1758. */
  1759. static unsigned long shrink_all_zones(unsigned long nr_pages, int prio,
  1760. int pass, struct scan_control *sc)
  1761. {
  1762. struct zone *zone;
  1763. unsigned long nr_to_scan, ret = 0;
  1764. enum lru_list l;
  1765. for_each_zone(zone) {
  1766. if (!populated_zone(zone))
  1767. continue;
  1768. if (zone_is_all_unreclaimable(zone) && prio != DEF_PRIORITY)
  1769. continue;
  1770. for_each_evictable_lru(l) {
  1771. /* For pass = 0, we don't shrink the active list */
  1772. if (pass == 0 &&
  1773. (l == LRU_ACTIVE || l == LRU_ACTIVE_FILE))
  1774. continue;
  1775. zone->lru[l].nr_scan +=
  1776. (zone_page_state(zone, NR_LRU_BASE + l)
  1777. >> prio) + 1;
  1778. if (zone->lru[l].nr_scan >= nr_pages || pass > 3) {
  1779. zone->lru[l].nr_scan = 0;
  1780. nr_to_scan = min(nr_pages,
  1781. zone_page_state(zone,
  1782. NR_LRU_BASE + l));
  1783. ret += shrink_list(l, nr_to_scan, zone,
  1784. sc, prio);
  1785. if (ret >= nr_pages)
  1786. return ret;
  1787. }
  1788. }
  1789. }
  1790. return ret;
  1791. }
  1792. /*
  1793. * Try to free `nr_pages' of memory, system-wide, and return the number of
  1794. * freed pages.
  1795. *
  1796. * Rather than trying to age LRUs the aim is to preserve the overall
  1797. * LRU order by reclaiming preferentially
  1798. * inactive > active > active referenced > active mapped
  1799. */
  1800. unsigned long shrink_all_memory(unsigned long nr_pages)
  1801. {
  1802. unsigned long lru_pages, nr_slab;
  1803. unsigned long ret = 0;
  1804. int pass;
  1805. struct reclaim_state reclaim_state;
  1806. struct scan_control sc = {
  1807. .gfp_mask = GFP_KERNEL,
  1808. .may_swap = 0,
  1809. .swap_cluster_max = nr_pages,
  1810. .may_writepage = 1,
  1811. .swappiness = vm_swappiness,
  1812. .isolate_pages = isolate_pages_global,
  1813. };
  1814. current->reclaim_state = &reclaim_state;
  1815. lru_pages = global_lru_pages();
  1816. nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
  1817. /* If slab caches are huge, it's better to hit them first */
  1818. while (nr_slab >= lru_pages) {
  1819. reclaim_state.reclaimed_slab = 0;
  1820. shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
  1821. if (!reclaim_state.reclaimed_slab)
  1822. break;
  1823. ret += reclaim_state.reclaimed_slab;
  1824. if (ret >= nr_pages)
  1825. goto out;
  1826. nr_slab -= reclaim_state.reclaimed_slab;
  1827. }
  1828. /*
  1829. * We try to shrink LRUs in 5 passes:
  1830. * 0 = Reclaim from inactive_list only
  1831. * 1 = Reclaim from active list but don't reclaim mapped
  1832. * 2 = 2nd pass of type 1
  1833. * 3 = Reclaim mapped (normal reclaim)
  1834. * 4 = 2nd pass of type 3
  1835. */
  1836. for (pass = 0; pass < 5; pass++) {
  1837. int prio;
  1838. /* Force reclaiming mapped pages in the passes #3 and #4 */
  1839. if (pass > 2) {
  1840. sc.may_swap = 1;
  1841. sc.swappiness = 100;
  1842. }
  1843. for (prio = DEF_PRIORITY; prio >= 0; prio--) {
  1844. unsigned long nr_to_scan = nr_pages - ret;
  1845. sc.nr_scanned = 0;
  1846. ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
  1847. if (ret >= nr_pages)
  1848. goto out;
  1849. reclaim_state.reclaimed_slab = 0;
  1850. shrink_slab(sc.nr_scanned, sc.gfp_mask,
  1851. global_lru_pages());
  1852. ret += reclaim_state.reclaimed_slab;
  1853. if (ret >= nr_pages)
  1854. goto out;
  1855. if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
  1856. congestion_wait(WRITE, HZ / 10);
  1857. }
  1858. }
  1859. /*
  1860. * If ret = 0, we could not shrink LRUs, but there may be something
  1861. * in slab caches
  1862. */
  1863. if (!ret) {
  1864. do {
  1865. reclaim_state.reclaimed_slab = 0;
  1866. shrink_slab(nr_pages, sc.gfp_mask, global_lru_pages());
  1867. ret += reclaim_state.reclaimed_slab;
  1868. } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
  1869. }
  1870. out:
  1871. current->reclaim_state = NULL;
  1872. return ret;
  1873. }
  1874. #endif
  1875. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  1876. not required for correctness. So if the last cpu in a node goes
  1877. away, we get changed to run anywhere: as the first one comes back,
  1878. restore their cpu bindings. */
  1879. static int __devinit cpu_callback(struct notifier_block *nfb,
  1880. unsigned long action, void *hcpu)
  1881. {
  1882. int nid;
  1883. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
  1884. for_each_node_state(nid, N_HIGH_MEMORY) {
  1885. pg_data_t *pgdat = NODE_DATA(nid);
  1886. node_to_cpumask_ptr(mask, pgdat->node_id);
  1887. if (any_online_cpu(*mask) < nr_cpu_ids)
  1888. /* One of our CPUs online: restore mask */
  1889. set_cpus_allowed_ptr(pgdat->kswapd, mask);
  1890. }
  1891. }
  1892. return NOTIFY_OK;
  1893. }
  1894. /*
  1895. * This kswapd start function will be called by init and node-hot-add.
  1896. * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
  1897. */
  1898. int kswapd_run(int nid)
  1899. {
  1900. pg_data_t *pgdat = NODE_DATA(nid);
  1901. int ret = 0;
  1902. if (pgdat->kswapd)
  1903. return 0;
  1904. pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
  1905. if (IS_ERR(pgdat->kswapd)) {
  1906. /* failure at boot is fatal */
  1907. BUG_ON(system_state == SYSTEM_BOOTING);
  1908. printk("Failed to start kswapd on node %d\n",nid);
  1909. ret = -1;
  1910. }
  1911. return ret;
  1912. }
  1913. static int __init kswapd_init(void)
  1914. {
  1915. int nid;
  1916. swap_setup();
  1917. for_each_node_state(nid, N_HIGH_MEMORY)
  1918. kswapd_run(nid);
  1919. hotcpu_notifier(cpu_callback, 0);
  1920. return 0;
  1921. }
  1922. module_init(kswapd_init)
  1923. #ifdef CONFIG_NUMA
  1924. /*
  1925. * Zone reclaim mode
  1926. *
  1927. * If non-zero call zone_reclaim when the number of free pages falls below
  1928. * the watermarks.
  1929. */
  1930. int zone_reclaim_mode __read_mostly;
  1931. #define RECLAIM_OFF 0
  1932. #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */
  1933. #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
  1934. #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
  1935. /*
  1936. * Priority for ZONE_RECLAIM. This determines the fraction of pages
  1937. * of a node considered for each zone_reclaim. 4 scans 1/16th of
  1938. * a zone.
  1939. */
  1940. #define ZONE_RECLAIM_PRIORITY 4
  1941. /*
  1942. * Percentage of pages in a zone that must be unmapped for zone_reclaim to
  1943. * occur.
  1944. */
  1945. int sysctl_min_unmapped_ratio = 1;
  1946. /*
  1947. * If the number of slab pages in a zone grows beyond this percentage then
  1948. * slab reclaim needs to occur.
  1949. */
  1950. int sysctl_min_slab_ratio = 5;
  1951. /*
  1952. * Try to free up some pages from this zone through reclaim.
  1953. */
  1954. static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  1955. {
  1956. /* Minimum pages needed in order to stay on node */
  1957. const unsigned long nr_pages = 1 << order;
  1958. struct task_struct *p = current;
  1959. struct reclaim_state reclaim_state;
  1960. int priority;
  1961. unsigned long nr_reclaimed = 0;
  1962. struct scan_control sc = {
  1963. .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
  1964. .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
  1965. .swap_cluster_max = max_t(unsigned long, nr_pages,
  1966. SWAP_CLUSTER_MAX),
  1967. .gfp_mask = gfp_mask,
  1968. .swappiness = vm_swappiness,
  1969. .isolate_pages = isolate_pages_global,
  1970. };
  1971. unsigned long slab_reclaimable;
  1972. disable_swap_token();
  1973. cond_resched();
  1974. /*
  1975. * We need to be able to allocate from the reserves for RECLAIM_SWAP
  1976. * and we also need to be able to write out pages for RECLAIM_WRITE
  1977. * and RECLAIM_SWAP.
  1978. */
  1979. p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
  1980. reclaim_state.reclaimed_slab = 0;
  1981. p->reclaim_state = &reclaim_state;
  1982. if (zone_page_state(zone, NR_FILE_PAGES) -
  1983. zone_page_state(zone, NR_FILE_MAPPED) >
  1984. zone->min_unmapped_pages) {
  1985. /*
  1986. * Free memory by calling shrink zone with increasing
  1987. * priorities until we have enough memory freed.
  1988. */
  1989. priority = ZONE_RECLAIM_PRIORITY;
  1990. do {
  1991. note_zone_scanning_priority(zone, priority);
  1992. nr_reclaimed += shrink_zone(priority, zone, &sc);
  1993. priority--;
  1994. } while (priority >= 0 && nr_reclaimed < nr_pages);
  1995. }
  1996. slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  1997. if (slab_reclaimable > zone->min_slab_pages) {
  1998. /*
  1999. * shrink_slab() does not currently allow us to determine how
  2000. * many pages were freed in this zone. So we take the current
  2001. * number of slab pages and shake the slab until it is reduced
  2002. * by the same nr_pages that we used for reclaiming unmapped
  2003. * pages.
  2004. *
  2005. * Note that shrink_slab will free memory on all zones and may
  2006. * take a long time.
  2007. */
  2008. while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
  2009. zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
  2010. slab_reclaimable - nr_pages)
  2011. ;
  2012. /*
  2013. * Update nr_reclaimed by the number of slab pages we
  2014. * reclaimed from this zone.
  2015. */
  2016. nr_reclaimed += slab_reclaimable -
  2017. zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  2018. }
  2019. p->reclaim_state = NULL;
  2020. current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
  2021. return nr_reclaimed >= nr_pages;
  2022. }
  2023. int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  2024. {
  2025. int node_id;
  2026. int ret;
  2027. /*
  2028. * Zone reclaim reclaims unmapped file backed pages and
  2029. * slab pages if we are over the defined limits.
  2030. *
  2031. * A small portion of unmapped file backed pages is needed for
  2032. * file I/O otherwise pages read by file I/O will be immediately
  2033. * thrown out if the zone is overallocated. So we do not reclaim
  2034. * if less than a specified percentage of the zone is used by
  2035. * unmapped file backed pages.
  2036. */
  2037. if (zone_page_state(zone, NR_FILE_PAGES) -
  2038. zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
  2039. && zone_page_state(zone, NR_SLAB_RECLAIMABLE)
  2040. <= zone->min_slab_pages)
  2041. return 0;
  2042. if (zone_is_all_unreclaimable(zone))
  2043. return 0;
  2044. /*
  2045. * Do not scan if the allocation should not be delayed.
  2046. */
  2047. if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
  2048. return 0;
  2049. /*
  2050. * Only run zone reclaim on the local zone or on zones that do not
  2051. * have associated processors. This will favor the local processor
  2052. * over remote processors and spread off node memory allocations
  2053. * as wide as possible.
  2054. */
  2055. node_id = zone_to_nid(zone);
  2056. if (node_state(node_id, N_CPU) && node_id != numa_node_id())
  2057. return 0;
  2058. if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
  2059. return 0;
  2060. ret = __zone_reclaim(zone, gfp_mask, order);
  2061. zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
  2062. return ret;
  2063. }
  2064. #endif
  2065. #ifdef CONFIG_UNEVICTABLE_LRU
  2066. /*
  2067. * page_evictable - test whether a page is evictable
  2068. * @page: the page to test
  2069. * @vma: the VMA in which the page is or will be mapped, may be NULL
  2070. *
  2071. * Test whether page is evictable--i.e., should be placed on active/inactive
  2072. * lists vs unevictable list. The vma argument is !NULL when called from the
  2073. * fault path to determine how to instantate a new page.
  2074. *
  2075. * Reasons page might not be evictable:
  2076. * (1) page's mapping marked unevictable
  2077. * (2) page is part of an mlocked VMA
  2078. *
  2079. */
  2080. int page_evictable(struct page *page, struct vm_area_struct *vma)
  2081. {
  2082. if (mapping_unevictable(page_mapping(page)))
  2083. return 0;
  2084. if (PageMlocked(page) || (vma && is_mlocked_vma(vma, page)))
  2085. return 0;
  2086. return 1;
  2087. }
  2088. static void show_page_path(struct page *page)
  2089. {
  2090. char buf[256];
  2091. if (page_is_file_cache(page)) {
  2092. struct address_space *mapping = page->mapping;
  2093. struct dentry *dentry;
  2094. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  2095. spin_lock(&mapping->i_mmap_lock);
  2096. dentry = d_find_alias(mapping->host);
  2097. printk(KERN_INFO "rescued: %s %lu\n",
  2098. dentry_path(dentry, buf, 256), pgoff);
  2099. spin_unlock(&mapping->i_mmap_lock);
  2100. } else {
  2101. #if defined(CONFIG_MM_OWNER) && defined(CONFIG_MMU)
  2102. struct anon_vma *anon_vma;
  2103. struct vm_area_struct *vma;
  2104. anon_vma = page_lock_anon_vma(page);
  2105. if (!anon_vma)
  2106. return;
  2107. list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
  2108. printk(KERN_INFO "rescued: anon %s\n",
  2109. vma->vm_mm->owner->comm);
  2110. break;
  2111. }
  2112. page_unlock_anon_vma(anon_vma);
  2113. #endif
  2114. }
  2115. }
  2116. /**
  2117. * check_move_unevictable_page - check page for evictability and move to appropriate zone lru list
  2118. * @page: page to check evictability and move to appropriate lru list
  2119. * @zone: zone page is in
  2120. *
  2121. * Checks a page for evictability and moves the page to the appropriate
  2122. * zone lru list.
  2123. *
  2124. * Restrictions: zone->lru_lock must be held, page must be on LRU and must
  2125. * have PageUnevictable set.
  2126. */
  2127. static void check_move_unevictable_page(struct page *page, struct zone *zone)
  2128. {
  2129. VM_BUG_ON(PageActive(page));
  2130. retry:
  2131. ClearPageUnevictable(page);
  2132. if (page_evictable(page, NULL)) {
  2133. enum lru_list l = LRU_INACTIVE_ANON + page_is_file_cache(page);
  2134. show_page_path(page);
  2135. __dec_zone_state(zone, NR_UNEVICTABLE);
  2136. list_move(&page->lru, &zone->lru[l].list);
  2137. __inc_zone_state(zone, NR_INACTIVE_ANON + l);
  2138. __count_vm_event(UNEVICTABLE_PGRESCUED);
  2139. } else {
  2140. /*
  2141. * rotate unevictable list
  2142. */
  2143. SetPageUnevictable(page);
  2144. list_move(&page->lru, &zone->lru[LRU_UNEVICTABLE].list);
  2145. if (page_evictable(page, NULL))
  2146. goto retry;
  2147. }
  2148. }
  2149. /**
  2150. * scan_mapping_unevictable_pages - scan an address space for evictable pages
  2151. * @mapping: struct address_space to scan for evictable pages
  2152. *
  2153. * Scan all pages in mapping. Check unevictable pages for
  2154. * evictability and move them to the appropriate zone lru list.
  2155. */
  2156. void scan_mapping_unevictable_pages(struct address_space *mapping)
  2157. {
  2158. pgoff_t next = 0;
  2159. pgoff_t end = (i_size_read(mapping->host) + PAGE_CACHE_SIZE - 1) >>
  2160. PAGE_CACHE_SHIFT;
  2161. struct zone *zone;
  2162. struct pagevec pvec;
  2163. if (mapping->nrpages == 0)
  2164. return;
  2165. pagevec_init(&pvec, 0);
  2166. while (next < end &&
  2167. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  2168. int i;
  2169. int pg_scanned = 0;
  2170. zone = NULL;
  2171. for (i = 0; i < pagevec_count(&pvec); i++) {
  2172. struct page *page = pvec.pages[i];
  2173. pgoff_t page_index = page->index;
  2174. struct zone *pagezone = page_zone(page);
  2175. pg_scanned++;
  2176. if (page_index > next)
  2177. next = page_index;
  2178. next++;
  2179. if (pagezone != zone) {
  2180. if (zone)
  2181. spin_unlock_irq(&zone->lru_lock);
  2182. zone = pagezone;
  2183. spin_lock_irq(&zone->lru_lock);
  2184. }
  2185. if (PageLRU(page) && PageUnevictable(page))
  2186. check_move_unevictable_page(page, zone);
  2187. }
  2188. if (zone)
  2189. spin_unlock_irq(&zone->lru_lock);
  2190. pagevec_release(&pvec);
  2191. count_vm_events(UNEVICTABLE_PGSCANNED, pg_scanned);
  2192. }
  2193. }
  2194. /**
  2195. * scan_zone_unevictable_pages - check unevictable list for evictable pages
  2196. * @zone - zone of which to scan the unevictable list
  2197. *
  2198. * Scan @zone's unevictable LRU lists to check for pages that have become
  2199. * evictable. Move those that have to @zone's inactive list where they
  2200. * become candidates for reclaim, unless shrink_inactive_zone() decides
  2201. * to reactivate them. Pages that are still unevictable are rotated
  2202. * back onto @zone's unevictable list.
  2203. */
  2204. #define SCAN_UNEVICTABLE_BATCH_SIZE 16UL /* arbitrary lock hold batch size */
  2205. void scan_zone_unevictable_pages(struct zone *zone)
  2206. {
  2207. struct list_head *l_unevictable = &zone->lru[LRU_UNEVICTABLE].list;
  2208. unsigned long scan;
  2209. unsigned long nr_to_scan = zone_page_state(zone, NR_UNEVICTABLE);
  2210. while (nr_to_scan > 0) {
  2211. unsigned long batch_size = min(nr_to_scan,
  2212. SCAN_UNEVICTABLE_BATCH_SIZE);
  2213. spin_lock_irq(&zone->lru_lock);
  2214. for (scan = 0; scan < batch_size; scan++) {
  2215. struct page *page = lru_to_page(l_unevictable);
  2216. if (!trylock_page(page))
  2217. continue;
  2218. prefetchw_prev_lru_page(page, l_unevictable, flags);
  2219. if (likely(PageLRU(page) && PageUnevictable(page)))
  2220. check_move_unevictable_page(page, zone);
  2221. unlock_page(page);
  2222. }
  2223. spin_unlock_irq(&zone->lru_lock);
  2224. nr_to_scan -= batch_size;
  2225. }
  2226. }
  2227. /**
  2228. * scan_all_zones_unevictable_pages - scan all unevictable lists for evictable pages
  2229. *
  2230. * A really big hammer: scan all zones' unevictable LRU lists to check for
  2231. * pages that have become evictable. Move those back to the zones'
  2232. * inactive list where they become candidates for reclaim.
  2233. * This occurs when, e.g., we have unswappable pages on the unevictable lists,
  2234. * and we add swap to the system. As such, it runs in the context of a task
  2235. * that has possibly/probably made some previously unevictable pages
  2236. * evictable.
  2237. */
  2238. void scan_all_zones_unevictable_pages(void)
  2239. {
  2240. struct zone *zone;
  2241. for_each_zone(zone) {
  2242. scan_zone_unevictable_pages(zone);
  2243. }
  2244. }
  2245. /*
  2246. * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of
  2247. * all nodes' unevictable lists for evictable pages
  2248. */
  2249. unsigned long scan_unevictable_pages;
  2250. int scan_unevictable_handler(struct ctl_table *table, int write,
  2251. struct file *file, void __user *buffer,
  2252. size_t *length, loff_t *ppos)
  2253. {
  2254. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  2255. if (write && *(unsigned long *)table->data)
  2256. scan_all_zones_unevictable_pages();
  2257. scan_unevictable_pages = 0;
  2258. return 0;
  2259. }
  2260. /*
  2261. * per node 'scan_unevictable_pages' attribute. On demand re-scan of
  2262. * a specified node's per zone unevictable lists for evictable pages.
  2263. */
  2264. static ssize_t read_scan_unevictable_node(struct sys_device *dev,
  2265. struct sysdev_attribute *attr,
  2266. char *buf)
  2267. {
  2268. return sprintf(buf, "0\n"); /* always zero; should fit... */
  2269. }
  2270. static ssize_t write_scan_unevictable_node(struct sys_device *dev,
  2271. struct sysdev_attribute *attr,
  2272. const char *buf, size_t count)
  2273. {
  2274. struct zone *node_zones = NODE_DATA(dev->id)->node_zones;
  2275. struct zone *zone;
  2276. unsigned long res;
  2277. unsigned long req = strict_strtoul(buf, 10, &res);
  2278. if (!req)
  2279. return 1; /* zero is no-op */
  2280. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  2281. if (!populated_zone(zone))
  2282. continue;
  2283. scan_zone_unevictable_pages(zone);
  2284. }
  2285. return 1;
  2286. }
  2287. static SYSDEV_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR,
  2288. read_scan_unevictable_node,
  2289. write_scan_unevictable_node);
  2290. int scan_unevictable_register_node(struct node *node)
  2291. {
  2292. return sysdev_create_file(&node->sysdev, &attr_scan_unevictable_pages);
  2293. }
  2294. void scan_unevictable_unregister_node(struct node *node)
  2295. {
  2296. sysdev_remove_file(&node->sysdev, &attr_scan_unevictable_pages);
  2297. }
  2298. #endif