vmscan.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108
  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 <asm/tlbflush.h>
  41. #include <asm/div64.h>
  42. #include <linux/swapops.h>
  43. #include "internal.h"
  44. struct scan_control {
  45. /* Incremented by the number of inactive pages that were scanned */
  46. unsigned long nr_scanned;
  47. /* This context's GFP mask */
  48. gfp_t gfp_mask;
  49. int may_writepage;
  50. /* Can pages be swapped as part of reclaim? */
  51. int may_swap;
  52. /* This context's SWAP_CLUSTER_MAX. If freeing memory for
  53. * suspend, we effectively ignore SWAP_CLUSTER_MAX.
  54. * In this context, it doesn't matter that we scan the
  55. * whole list at once. */
  56. int swap_cluster_max;
  57. int swappiness;
  58. int all_unreclaimable;
  59. int order;
  60. /*
  61. * Pages that have (or should have) IO pending. If we run into
  62. * a lot of these, we're better off waiting a little for IO to
  63. * finish rather than scanning more pages in the VM.
  64. */
  65. int nr_io_pages;
  66. /* Which cgroup do we reclaim from */
  67. struct mem_cgroup *mem_cgroup;
  68. /* Pluggable isolate pages callback */
  69. unsigned long (*isolate_pages)(unsigned long nr, struct list_head *dst,
  70. unsigned long *scanned, int order, int mode,
  71. struct zone *z, struct mem_cgroup *mem_cont,
  72. int active);
  73. };
  74. #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
  75. #ifdef ARCH_HAS_PREFETCH
  76. #define prefetch_prev_lru_page(_page, _base, _field) \
  77. do { \
  78. if ((_page)->lru.prev != _base) { \
  79. struct page *prev; \
  80. \
  81. prev = lru_to_page(&(_page->lru)); \
  82. prefetch(&prev->_field); \
  83. } \
  84. } while (0)
  85. #else
  86. #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
  87. #endif
  88. #ifdef ARCH_HAS_PREFETCHW
  89. #define prefetchw_prev_lru_page(_page, _base, _field) \
  90. do { \
  91. if ((_page)->lru.prev != _base) { \
  92. struct page *prev; \
  93. \
  94. prev = lru_to_page(&(_page->lru)); \
  95. prefetchw(&prev->_field); \
  96. } \
  97. } while (0)
  98. #else
  99. #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
  100. #endif
  101. /*
  102. * From 0 .. 100. Higher means more swappy.
  103. */
  104. int vm_swappiness = 60;
  105. long vm_total_pages; /* The total number of pages which the VM controls */
  106. static LIST_HEAD(shrinker_list);
  107. static DECLARE_RWSEM(shrinker_rwsem);
  108. #ifdef CONFIG_CGROUP_MEM_CONT
  109. #define scan_global_lru(sc) (!(sc)->mem_cgroup)
  110. #else
  111. #define scan_global_lru(sc) (1)
  112. #endif
  113. /*
  114. * Add a shrinker callback to be called from the vm
  115. */
  116. void register_shrinker(struct shrinker *shrinker)
  117. {
  118. shrinker->nr = 0;
  119. down_write(&shrinker_rwsem);
  120. list_add_tail(&shrinker->list, &shrinker_list);
  121. up_write(&shrinker_rwsem);
  122. }
  123. EXPORT_SYMBOL(register_shrinker);
  124. /*
  125. * Remove one
  126. */
  127. void unregister_shrinker(struct shrinker *shrinker)
  128. {
  129. down_write(&shrinker_rwsem);
  130. list_del(&shrinker->list);
  131. up_write(&shrinker_rwsem);
  132. }
  133. EXPORT_SYMBOL(unregister_shrinker);
  134. #define SHRINK_BATCH 128
  135. /*
  136. * Call the shrink functions to age shrinkable caches
  137. *
  138. * Here we assume it costs one seek to replace a lru page and that it also
  139. * takes a seek to recreate a cache object. With this in mind we age equal
  140. * percentages of the lru and ageable caches. This should balance the seeks
  141. * generated by these structures.
  142. *
  143. * If the vm encountered mapped pages on the LRU it increase the pressure on
  144. * slab to avoid swapping.
  145. *
  146. * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
  147. *
  148. * `lru_pages' represents the number of on-LRU pages in all the zones which
  149. * are eligible for the caller's allocation attempt. It is used for balancing
  150. * slab reclaim versus page reclaim.
  151. *
  152. * Returns the number of slab objects which we shrunk.
  153. */
  154. unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
  155. unsigned long lru_pages)
  156. {
  157. struct shrinker *shrinker;
  158. unsigned long ret = 0;
  159. if (scanned == 0)
  160. scanned = SWAP_CLUSTER_MAX;
  161. if (!down_read_trylock(&shrinker_rwsem))
  162. return 1; /* Assume we'll be able to shrink next time */
  163. list_for_each_entry(shrinker, &shrinker_list, list) {
  164. unsigned long long delta;
  165. unsigned long total_scan;
  166. unsigned long max_pass = (*shrinker->shrink)(0, gfp_mask);
  167. delta = (4 * scanned) / shrinker->seeks;
  168. delta *= max_pass;
  169. do_div(delta, lru_pages + 1);
  170. shrinker->nr += delta;
  171. if (shrinker->nr < 0) {
  172. printk(KERN_ERR "%s: nr=%ld\n",
  173. __FUNCTION__, shrinker->nr);
  174. shrinker->nr = max_pass;
  175. }
  176. /*
  177. * Avoid risking looping forever due to too large nr value:
  178. * never try to free more than twice the estimate number of
  179. * freeable entries.
  180. */
  181. if (shrinker->nr > max_pass * 2)
  182. shrinker->nr = max_pass * 2;
  183. total_scan = shrinker->nr;
  184. shrinker->nr = 0;
  185. while (total_scan >= SHRINK_BATCH) {
  186. long this_scan = SHRINK_BATCH;
  187. int shrink_ret;
  188. int nr_before;
  189. nr_before = (*shrinker->shrink)(0, gfp_mask);
  190. shrink_ret = (*shrinker->shrink)(this_scan, gfp_mask);
  191. if (shrink_ret == -1)
  192. break;
  193. if (shrink_ret < nr_before)
  194. ret += nr_before - shrink_ret;
  195. count_vm_events(SLABS_SCANNED, this_scan);
  196. total_scan -= this_scan;
  197. cond_resched();
  198. }
  199. shrinker->nr += total_scan;
  200. }
  201. up_read(&shrinker_rwsem);
  202. return ret;
  203. }
  204. /* Called without lock on whether page is mapped, so answer is unstable */
  205. static inline int page_mapping_inuse(struct page *page)
  206. {
  207. struct address_space *mapping;
  208. /* Page is in somebody's page tables. */
  209. if (page_mapped(page))
  210. return 1;
  211. /* Be more reluctant to reclaim swapcache than pagecache */
  212. if (PageSwapCache(page))
  213. return 1;
  214. mapping = page_mapping(page);
  215. if (!mapping)
  216. return 0;
  217. /* File is mmap'd by somebody? */
  218. return mapping_mapped(mapping);
  219. }
  220. static inline int is_page_cache_freeable(struct page *page)
  221. {
  222. return page_count(page) - !!PagePrivate(page) == 2;
  223. }
  224. static int may_write_to_queue(struct backing_dev_info *bdi)
  225. {
  226. if (current->flags & PF_SWAPWRITE)
  227. return 1;
  228. if (!bdi_write_congested(bdi))
  229. return 1;
  230. if (bdi == current->backing_dev_info)
  231. return 1;
  232. return 0;
  233. }
  234. /*
  235. * We detected a synchronous write error writing a page out. Probably
  236. * -ENOSPC. We need to propagate that into the address_space for a subsequent
  237. * fsync(), msync() or close().
  238. *
  239. * The tricky part is that after writepage we cannot touch the mapping: nothing
  240. * prevents it from being freed up. But we have a ref on the page and once
  241. * that page is locked, the mapping is pinned.
  242. *
  243. * We're allowed to run sleeping lock_page() here because we know the caller has
  244. * __GFP_FS.
  245. */
  246. static void handle_write_error(struct address_space *mapping,
  247. struct page *page, int error)
  248. {
  249. lock_page(page);
  250. if (page_mapping(page) == mapping)
  251. mapping_set_error(mapping, error);
  252. unlock_page(page);
  253. }
  254. /* Request for sync pageout. */
  255. enum pageout_io {
  256. PAGEOUT_IO_ASYNC,
  257. PAGEOUT_IO_SYNC,
  258. };
  259. /* possible outcome of pageout() */
  260. typedef enum {
  261. /* failed to write page out, page is locked */
  262. PAGE_KEEP,
  263. /* move page to the active list, page is locked */
  264. PAGE_ACTIVATE,
  265. /* page has been sent to the disk successfully, page is unlocked */
  266. PAGE_SUCCESS,
  267. /* page is clean and locked */
  268. PAGE_CLEAN,
  269. } pageout_t;
  270. /*
  271. * pageout is called by shrink_page_list() for each dirty page.
  272. * Calls ->writepage().
  273. */
  274. static pageout_t pageout(struct page *page, struct address_space *mapping,
  275. enum pageout_io sync_writeback)
  276. {
  277. /*
  278. * If the page is dirty, only perform writeback if that write
  279. * will be non-blocking. To prevent this allocation from being
  280. * stalled by pagecache activity. But note that there may be
  281. * stalls if we need to run get_block(). We could test
  282. * PagePrivate for that.
  283. *
  284. * If this process is currently in generic_file_write() against
  285. * this page's queue, we can perform writeback even if that
  286. * will block.
  287. *
  288. * If the page is swapcache, write it back even if that would
  289. * block, for some throttling. This happens by accident, because
  290. * swap_backing_dev_info is bust: it doesn't reflect the
  291. * congestion state of the swapdevs. Easy to fix, if needed.
  292. * See swapfile.c:page_queue_congested().
  293. */
  294. if (!is_page_cache_freeable(page))
  295. return PAGE_KEEP;
  296. if (!mapping) {
  297. /*
  298. * Some data journaling orphaned pages can have
  299. * page->mapping == NULL while being dirty with clean buffers.
  300. */
  301. if (PagePrivate(page)) {
  302. if (try_to_free_buffers(page)) {
  303. ClearPageDirty(page);
  304. printk("%s: orphaned page\n", __FUNCTION__);
  305. return PAGE_CLEAN;
  306. }
  307. }
  308. return PAGE_KEEP;
  309. }
  310. if (mapping->a_ops->writepage == NULL)
  311. return PAGE_ACTIVATE;
  312. if (!may_write_to_queue(mapping->backing_dev_info))
  313. return PAGE_KEEP;
  314. if (clear_page_dirty_for_io(page)) {
  315. int res;
  316. struct writeback_control wbc = {
  317. .sync_mode = WB_SYNC_NONE,
  318. .nr_to_write = SWAP_CLUSTER_MAX,
  319. .range_start = 0,
  320. .range_end = LLONG_MAX,
  321. .nonblocking = 1,
  322. .for_reclaim = 1,
  323. };
  324. SetPageReclaim(page);
  325. res = mapping->a_ops->writepage(page, &wbc);
  326. if (res < 0)
  327. handle_write_error(mapping, page, res);
  328. if (res == AOP_WRITEPAGE_ACTIVATE) {
  329. ClearPageReclaim(page);
  330. return PAGE_ACTIVATE;
  331. }
  332. /*
  333. * Wait on writeback if requested to. This happens when
  334. * direct reclaiming a large contiguous area and the
  335. * first attempt to free a range of pages fails.
  336. */
  337. if (PageWriteback(page) && sync_writeback == PAGEOUT_IO_SYNC)
  338. wait_on_page_writeback(page);
  339. if (!PageWriteback(page)) {
  340. /* synchronous write or broken a_ops? */
  341. ClearPageReclaim(page);
  342. }
  343. inc_zone_page_state(page, NR_VMSCAN_WRITE);
  344. return PAGE_SUCCESS;
  345. }
  346. return PAGE_CLEAN;
  347. }
  348. /*
  349. * Attempt to detach a locked page from its ->mapping. If it is dirty or if
  350. * someone else has a ref on the page, abort and return 0. If it was
  351. * successfully detached, return 1. Assumes the caller has a single ref on
  352. * this page.
  353. */
  354. int remove_mapping(struct address_space *mapping, struct page *page)
  355. {
  356. BUG_ON(!PageLocked(page));
  357. BUG_ON(mapping != page_mapping(page));
  358. write_lock_irq(&mapping->tree_lock);
  359. /*
  360. * The non racy check for a busy page.
  361. *
  362. * Must be careful with the order of the tests. When someone has
  363. * a ref to the page, it may be possible that they dirty it then
  364. * drop the reference. So if PageDirty is tested before page_count
  365. * here, then the following race may occur:
  366. *
  367. * get_user_pages(&page);
  368. * [user mapping goes away]
  369. * write_to(page);
  370. * !PageDirty(page) [good]
  371. * SetPageDirty(page);
  372. * put_page(page);
  373. * !page_count(page) [good, discard it]
  374. *
  375. * [oops, our write_to data is lost]
  376. *
  377. * Reversing the order of the tests ensures such a situation cannot
  378. * escape unnoticed. The smp_rmb is needed to ensure the page->flags
  379. * load is not satisfied before that of page->_count.
  380. *
  381. * Note that if SetPageDirty is always performed via set_page_dirty,
  382. * and thus under tree_lock, then this ordering is not required.
  383. */
  384. if (unlikely(page_count(page) != 2))
  385. goto cannot_free;
  386. smp_rmb();
  387. if (unlikely(PageDirty(page)))
  388. goto cannot_free;
  389. if (PageSwapCache(page)) {
  390. swp_entry_t swap = { .val = page_private(page) };
  391. __delete_from_swap_cache(page);
  392. write_unlock_irq(&mapping->tree_lock);
  393. swap_free(swap);
  394. __put_page(page); /* The pagecache ref */
  395. return 1;
  396. }
  397. __remove_from_page_cache(page);
  398. write_unlock_irq(&mapping->tree_lock);
  399. __put_page(page);
  400. return 1;
  401. cannot_free:
  402. write_unlock_irq(&mapping->tree_lock);
  403. return 0;
  404. }
  405. /*
  406. * shrink_page_list() returns the number of reclaimed pages
  407. */
  408. static unsigned long shrink_page_list(struct list_head *page_list,
  409. struct scan_control *sc,
  410. enum pageout_io sync_writeback)
  411. {
  412. LIST_HEAD(ret_pages);
  413. struct pagevec freed_pvec;
  414. int pgactivate = 0;
  415. unsigned long nr_reclaimed = 0;
  416. cond_resched();
  417. pagevec_init(&freed_pvec, 1);
  418. while (!list_empty(page_list)) {
  419. struct address_space *mapping;
  420. struct page *page;
  421. int may_enter_fs;
  422. int referenced;
  423. cond_resched();
  424. page = lru_to_page(page_list);
  425. list_del(&page->lru);
  426. if (TestSetPageLocked(page))
  427. goto keep;
  428. VM_BUG_ON(PageActive(page));
  429. sc->nr_scanned++;
  430. if (!sc->may_swap && page_mapped(page))
  431. goto keep_locked;
  432. /* Double the slab pressure for mapped and swapcache pages */
  433. if (page_mapped(page) || PageSwapCache(page))
  434. sc->nr_scanned++;
  435. may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
  436. (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
  437. if (PageWriteback(page)) {
  438. /*
  439. * Synchronous reclaim is performed in two passes,
  440. * first an asynchronous pass over the list to
  441. * start parallel writeback, and a second synchronous
  442. * pass to wait for the IO to complete. Wait here
  443. * for any page for which writeback has already
  444. * started.
  445. */
  446. if (sync_writeback == PAGEOUT_IO_SYNC && may_enter_fs)
  447. wait_on_page_writeback(page);
  448. else {
  449. sc->nr_io_pages++;
  450. goto keep_locked;
  451. }
  452. }
  453. referenced = page_referenced(page, 1, sc->mem_cgroup);
  454. /* In active use or really unfreeable? Activate it. */
  455. if (sc->order <= PAGE_ALLOC_COSTLY_ORDER &&
  456. referenced && page_mapping_inuse(page))
  457. goto activate_locked;
  458. #ifdef CONFIG_SWAP
  459. /*
  460. * Anonymous process memory has backing store?
  461. * Try to allocate it some swap space here.
  462. */
  463. if (PageAnon(page) && !PageSwapCache(page))
  464. if (!add_to_swap(page, GFP_ATOMIC))
  465. goto activate_locked;
  466. #endif /* CONFIG_SWAP */
  467. mapping = page_mapping(page);
  468. /*
  469. * The page is mapped into the page tables of one or more
  470. * processes. Try to unmap it here.
  471. */
  472. if (page_mapped(page) && mapping) {
  473. switch (try_to_unmap(page, 0)) {
  474. case SWAP_FAIL:
  475. goto activate_locked;
  476. case SWAP_AGAIN:
  477. goto keep_locked;
  478. case SWAP_SUCCESS:
  479. ; /* try to free the page below */
  480. }
  481. }
  482. if (PageDirty(page)) {
  483. if (sc->order <= PAGE_ALLOC_COSTLY_ORDER && referenced)
  484. goto keep_locked;
  485. if (!may_enter_fs) {
  486. sc->nr_io_pages++;
  487. goto keep_locked;
  488. }
  489. if (!sc->may_writepage)
  490. goto keep_locked;
  491. /* Page is dirty, try to write it out here */
  492. switch (pageout(page, mapping, sync_writeback)) {
  493. case PAGE_KEEP:
  494. goto keep_locked;
  495. case PAGE_ACTIVATE:
  496. goto activate_locked;
  497. case PAGE_SUCCESS:
  498. if (PageWriteback(page) || PageDirty(page)) {
  499. sc->nr_io_pages++;
  500. goto keep;
  501. }
  502. /*
  503. * A synchronous write - probably a ramdisk. Go
  504. * ahead and try to reclaim the page.
  505. */
  506. if (TestSetPageLocked(page))
  507. goto keep;
  508. if (PageDirty(page) || PageWriteback(page))
  509. goto keep_locked;
  510. mapping = page_mapping(page);
  511. case PAGE_CLEAN:
  512. ; /* try to free the page below */
  513. }
  514. }
  515. /*
  516. * If the page has buffers, try to free the buffer mappings
  517. * associated with this page. If we succeed we try to free
  518. * the page as well.
  519. *
  520. * We do this even if the page is PageDirty().
  521. * try_to_release_page() does not perform I/O, but it is
  522. * possible for a page to have PageDirty set, but it is actually
  523. * clean (all its buffers are clean). This happens if the
  524. * buffers were written out directly, with submit_bh(). ext3
  525. * will do this, as well as the blockdev mapping.
  526. * try_to_release_page() will discover that cleanness and will
  527. * drop the buffers and mark the page clean - it can be freed.
  528. *
  529. * Rarely, pages can have buffers and no ->mapping. These are
  530. * the pages which were not successfully invalidated in
  531. * truncate_complete_page(). We try to drop those buffers here
  532. * and if that worked, and the page is no longer mapped into
  533. * process address space (page_count == 1) it can be freed.
  534. * Otherwise, leave the page on the LRU so it is swappable.
  535. */
  536. if (PagePrivate(page)) {
  537. if (!try_to_release_page(page, sc->gfp_mask))
  538. goto activate_locked;
  539. if (!mapping && page_count(page) == 1)
  540. goto free_it;
  541. }
  542. if (!mapping || !remove_mapping(mapping, page))
  543. goto keep_locked;
  544. free_it:
  545. unlock_page(page);
  546. nr_reclaimed++;
  547. if (!pagevec_add(&freed_pvec, page))
  548. __pagevec_release_nonlru(&freed_pvec);
  549. continue;
  550. activate_locked:
  551. SetPageActive(page);
  552. pgactivate++;
  553. keep_locked:
  554. unlock_page(page);
  555. keep:
  556. list_add(&page->lru, &ret_pages);
  557. VM_BUG_ON(PageLRU(page));
  558. }
  559. list_splice(&ret_pages, page_list);
  560. if (pagevec_count(&freed_pvec))
  561. __pagevec_release_nonlru(&freed_pvec);
  562. count_vm_events(PGACTIVATE, pgactivate);
  563. return nr_reclaimed;
  564. }
  565. /* LRU Isolation modes. */
  566. #define ISOLATE_INACTIVE 0 /* Isolate inactive pages. */
  567. #define ISOLATE_ACTIVE 1 /* Isolate active pages. */
  568. #define ISOLATE_BOTH 2 /* Isolate both active and inactive pages. */
  569. /*
  570. * Attempt to remove the specified page from its LRU. Only take this page
  571. * if it is of the appropriate PageActive status. Pages which are being
  572. * freed elsewhere are also ignored.
  573. *
  574. * page: page to consider
  575. * mode: one of the LRU isolation modes defined above
  576. *
  577. * returns 0 on success, -ve errno on failure.
  578. */
  579. int __isolate_lru_page(struct page *page, int mode)
  580. {
  581. int ret = -EINVAL;
  582. /* Only take pages on the LRU. */
  583. if (!PageLRU(page))
  584. return ret;
  585. /*
  586. * When checking the active state, we need to be sure we are
  587. * dealing with comparible boolean values. Take the logical not
  588. * of each.
  589. */
  590. if (mode != ISOLATE_BOTH && (!PageActive(page) != !mode))
  591. return ret;
  592. ret = -EBUSY;
  593. if (likely(get_page_unless_zero(page))) {
  594. /*
  595. * Be careful not to clear PageLRU until after we're
  596. * sure the page is not being freed elsewhere -- the
  597. * page release code relies on it.
  598. */
  599. ClearPageLRU(page);
  600. ret = 0;
  601. }
  602. return ret;
  603. }
  604. /*
  605. * zone->lru_lock is heavily contended. Some of the functions that
  606. * shrink the lists perform better by taking out a batch of pages
  607. * and working on them outside the LRU lock.
  608. *
  609. * For pagecache intensive workloads, this function is the hottest
  610. * spot in the kernel (apart from copy_*_user functions).
  611. *
  612. * Appropriate locks must be held before calling this function.
  613. *
  614. * @nr_to_scan: The number of pages to look through on the list.
  615. * @src: The LRU list to pull pages off.
  616. * @dst: The temp list to put pages on to.
  617. * @scanned: The number of pages that were scanned.
  618. * @order: The caller's attempted allocation order
  619. * @mode: One of the LRU isolation modes
  620. *
  621. * returns how many pages were moved onto *@dst.
  622. */
  623. static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
  624. struct list_head *src, struct list_head *dst,
  625. unsigned long *scanned, int order, int mode)
  626. {
  627. unsigned long nr_taken = 0;
  628. unsigned long scan;
  629. for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
  630. struct page *page;
  631. unsigned long pfn;
  632. unsigned long end_pfn;
  633. unsigned long page_pfn;
  634. int zone_id;
  635. page = lru_to_page(src);
  636. prefetchw_prev_lru_page(page, src, flags);
  637. VM_BUG_ON(!PageLRU(page));
  638. switch (__isolate_lru_page(page, mode)) {
  639. case 0:
  640. list_move(&page->lru, dst);
  641. nr_taken++;
  642. break;
  643. case -EBUSY:
  644. /* else it is being freed elsewhere */
  645. list_move(&page->lru, src);
  646. continue;
  647. default:
  648. BUG();
  649. }
  650. if (!order)
  651. continue;
  652. /*
  653. * Attempt to take all pages in the order aligned region
  654. * surrounding the tag page. Only take those pages of
  655. * the same active state as that tag page. We may safely
  656. * round the target page pfn down to the requested order
  657. * as the mem_map is guarenteed valid out to MAX_ORDER,
  658. * where that page is in a different zone we will detect
  659. * it from its zone id and abort this block scan.
  660. */
  661. zone_id = page_zone_id(page);
  662. page_pfn = page_to_pfn(page);
  663. pfn = page_pfn & ~((1 << order) - 1);
  664. end_pfn = pfn + (1 << order);
  665. for (; pfn < end_pfn; pfn++) {
  666. struct page *cursor_page;
  667. /* The target page is in the block, ignore it. */
  668. if (unlikely(pfn == page_pfn))
  669. continue;
  670. /* Avoid holes within the zone. */
  671. if (unlikely(!pfn_valid_within(pfn)))
  672. break;
  673. cursor_page = pfn_to_page(pfn);
  674. /* Check that we have not crossed a zone boundary. */
  675. if (unlikely(page_zone_id(cursor_page) != zone_id))
  676. continue;
  677. switch (__isolate_lru_page(cursor_page, mode)) {
  678. case 0:
  679. list_move(&cursor_page->lru, dst);
  680. nr_taken++;
  681. scan++;
  682. break;
  683. case -EBUSY:
  684. /* else it is being freed elsewhere */
  685. list_move(&cursor_page->lru, src);
  686. default:
  687. break;
  688. }
  689. }
  690. }
  691. *scanned = scan;
  692. return nr_taken;
  693. }
  694. static unsigned long isolate_pages_global(unsigned long nr,
  695. struct list_head *dst,
  696. unsigned long *scanned, int order,
  697. int mode, struct zone *z,
  698. struct mem_cgroup *mem_cont,
  699. int active)
  700. {
  701. if (active)
  702. return isolate_lru_pages(nr, &z->active_list, dst,
  703. scanned, order, mode);
  704. else
  705. return isolate_lru_pages(nr, &z->inactive_list, dst,
  706. scanned, order, mode);
  707. }
  708. /*
  709. * clear_active_flags() is a helper for shrink_active_list(), clearing
  710. * any active bits from the pages in the list.
  711. */
  712. static unsigned long clear_active_flags(struct list_head *page_list)
  713. {
  714. int nr_active = 0;
  715. struct page *page;
  716. list_for_each_entry(page, page_list, lru)
  717. if (PageActive(page)) {
  718. ClearPageActive(page);
  719. nr_active++;
  720. }
  721. return nr_active;
  722. }
  723. /*
  724. * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
  725. * of reclaimed pages
  726. */
  727. static unsigned long shrink_inactive_list(unsigned long max_scan,
  728. struct zone *zone, struct scan_control *sc)
  729. {
  730. LIST_HEAD(page_list);
  731. struct pagevec pvec;
  732. unsigned long nr_scanned = 0;
  733. unsigned long nr_reclaimed = 0;
  734. pagevec_init(&pvec, 1);
  735. lru_add_drain();
  736. spin_lock_irq(&zone->lru_lock);
  737. do {
  738. struct page *page;
  739. unsigned long nr_taken;
  740. unsigned long nr_scan;
  741. unsigned long nr_freed;
  742. unsigned long nr_active;
  743. nr_taken = sc->isolate_pages(sc->swap_cluster_max,
  744. &page_list, &nr_scan, sc->order,
  745. (sc->order > PAGE_ALLOC_COSTLY_ORDER)?
  746. ISOLATE_BOTH : ISOLATE_INACTIVE,
  747. zone, sc->mem_cgroup, 0);
  748. nr_active = clear_active_flags(&page_list);
  749. __count_vm_events(PGDEACTIVATE, nr_active);
  750. __mod_zone_page_state(zone, NR_ACTIVE, -nr_active);
  751. __mod_zone_page_state(zone, NR_INACTIVE,
  752. -(nr_taken - nr_active));
  753. if (scan_global_lru(sc))
  754. zone->pages_scanned += nr_scan;
  755. spin_unlock_irq(&zone->lru_lock);
  756. nr_scanned += nr_scan;
  757. nr_freed = shrink_page_list(&page_list, sc, PAGEOUT_IO_ASYNC);
  758. /*
  759. * If we are direct reclaiming for contiguous pages and we do
  760. * not reclaim everything in the list, try again and wait
  761. * for IO to complete. This will stall high-order allocations
  762. * but that should be acceptable to the caller
  763. */
  764. if (nr_freed < nr_taken && !current_is_kswapd() &&
  765. sc->order > PAGE_ALLOC_COSTLY_ORDER) {
  766. congestion_wait(WRITE, HZ/10);
  767. /*
  768. * The attempt at page out may have made some
  769. * of the pages active, mark them inactive again.
  770. */
  771. nr_active = clear_active_flags(&page_list);
  772. count_vm_events(PGDEACTIVATE, nr_active);
  773. nr_freed += shrink_page_list(&page_list, sc,
  774. PAGEOUT_IO_SYNC);
  775. }
  776. nr_reclaimed += nr_freed;
  777. local_irq_disable();
  778. if (current_is_kswapd()) {
  779. __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan);
  780. __count_vm_events(KSWAPD_STEAL, nr_freed);
  781. } else if (scan_global_lru(sc))
  782. __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan);
  783. __count_zone_vm_events(PGSTEAL, zone, nr_freed);
  784. if (nr_taken == 0)
  785. goto done;
  786. spin_lock(&zone->lru_lock);
  787. /*
  788. * Put back any unfreeable pages.
  789. */
  790. while (!list_empty(&page_list)) {
  791. page = lru_to_page(&page_list);
  792. VM_BUG_ON(PageLRU(page));
  793. SetPageLRU(page);
  794. list_del(&page->lru);
  795. if (PageActive(page))
  796. add_page_to_active_list(zone, page);
  797. else
  798. add_page_to_inactive_list(zone, page);
  799. if (!pagevec_add(&pvec, page)) {
  800. spin_unlock_irq(&zone->lru_lock);
  801. __pagevec_release(&pvec);
  802. spin_lock_irq(&zone->lru_lock);
  803. }
  804. }
  805. } while (nr_scanned < max_scan);
  806. spin_unlock(&zone->lru_lock);
  807. done:
  808. local_irq_enable();
  809. pagevec_release(&pvec);
  810. return nr_reclaimed;
  811. }
  812. /*
  813. * We are about to scan this zone at a certain priority level. If that priority
  814. * level is smaller (ie: more urgent) than the previous priority, then note
  815. * that priority level within the zone. This is done so that when the next
  816. * process comes in to scan this zone, it will immediately start out at this
  817. * priority level rather than having to build up its own scanning priority.
  818. * Here, this priority affects only the reclaim-mapped threshold.
  819. */
  820. static inline void note_zone_scanning_priority(struct zone *zone, int priority)
  821. {
  822. if (priority < zone->prev_priority)
  823. zone->prev_priority = priority;
  824. }
  825. static inline int zone_is_near_oom(struct zone *zone)
  826. {
  827. return zone->pages_scanned >= (zone_page_state(zone, NR_ACTIVE)
  828. + zone_page_state(zone, NR_INACTIVE))*3;
  829. }
  830. /*
  831. * Determine we should try to reclaim mapped pages.
  832. * This is called only when sc->mem_cgroup is NULL.
  833. */
  834. static int calc_reclaim_mapped(struct scan_control *sc, struct zone *zone,
  835. int priority)
  836. {
  837. long mapped_ratio;
  838. long distress;
  839. long swap_tendency;
  840. long imbalance;
  841. int reclaim_mapped = 0;
  842. int prev_priority;
  843. if (scan_global_lru(sc) && zone_is_near_oom(zone))
  844. return 1;
  845. /*
  846. * `distress' is a measure of how much trouble we're having
  847. * reclaiming pages. 0 -> no problems. 100 -> great trouble.
  848. */
  849. if (scan_global_lru(sc))
  850. prev_priority = zone->prev_priority;
  851. else
  852. prev_priority = mem_cgroup_get_reclaim_priority(sc->mem_cgroup);
  853. distress = 100 >> min(prev_priority, priority);
  854. /*
  855. * The point of this algorithm is to decide when to start
  856. * reclaiming mapped memory instead of just pagecache. Work out
  857. * how much memory
  858. * is mapped.
  859. */
  860. if (scan_global_lru(sc))
  861. mapped_ratio = ((global_page_state(NR_FILE_MAPPED) +
  862. global_page_state(NR_ANON_PAGES)) * 100) /
  863. vm_total_pages;
  864. else
  865. mapped_ratio = mem_cgroup_calc_mapped_ratio(sc->mem_cgroup);
  866. /*
  867. * Now decide how much we really want to unmap some pages. The
  868. * mapped ratio is downgraded - just because there's a lot of
  869. * mapped memory doesn't necessarily mean that page reclaim
  870. * isn't succeeding.
  871. *
  872. * The distress ratio is important - we don't want to start
  873. * going oom.
  874. *
  875. * A 100% value of vm_swappiness overrides this algorithm
  876. * altogether.
  877. */
  878. swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
  879. /*
  880. * If there's huge imbalance between active and inactive
  881. * (think active 100 times larger than inactive) we should
  882. * become more permissive, or the system will take too much
  883. * cpu before it start swapping during memory pressure.
  884. * Distress is about avoiding early-oom, this is about
  885. * making swappiness graceful despite setting it to low
  886. * values.
  887. *
  888. * Avoid div by zero with nr_inactive+1, and max resulting
  889. * value is vm_total_pages.
  890. */
  891. if (scan_global_lru(sc)) {
  892. imbalance = zone_page_state(zone, NR_ACTIVE);
  893. imbalance /= zone_page_state(zone, NR_INACTIVE) + 1;
  894. } else
  895. imbalance = mem_cgroup_reclaim_imbalance(sc->mem_cgroup);
  896. /*
  897. * Reduce the effect of imbalance if swappiness is low,
  898. * this means for a swappiness very low, the imbalance
  899. * must be much higher than 100 for this logic to make
  900. * the difference.
  901. *
  902. * Max temporary value is vm_total_pages*100.
  903. */
  904. imbalance *= (vm_swappiness + 1);
  905. imbalance /= 100;
  906. /*
  907. * If not much of the ram is mapped, makes the imbalance
  908. * less relevant, it's high priority we refill the inactive
  909. * list with mapped pages only in presence of high ratio of
  910. * mapped pages.
  911. *
  912. * Max temporary value is vm_total_pages*100.
  913. */
  914. imbalance *= mapped_ratio;
  915. imbalance /= 100;
  916. /* apply imbalance feedback to swap_tendency */
  917. swap_tendency += imbalance;
  918. /*
  919. * Now use this metric to decide whether to start moving mapped
  920. * memory onto the inactive list.
  921. */
  922. if (swap_tendency >= 100)
  923. reclaim_mapped = 1;
  924. return reclaim_mapped;
  925. }
  926. /*
  927. * This moves pages from the active list to the inactive list.
  928. *
  929. * We move them the other way if the page is referenced by one or more
  930. * processes, from rmap.
  931. *
  932. * If the pages are mostly unmapped, the processing is fast and it is
  933. * appropriate to hold zone->lru_lock across the whole operation. But if
  934. * the pages are mapped, the processing is slow (page_referenced()) so we
  935. * should drop zone->lru_lock around each page. It's impossible to balance
  936. * this, so instead we remove the pages from the LRU while processing them.
  937. * It is safe to rely on PG_active against the non-LRU pages in here because
  938. * nobody will play with that bit on a non-LRU page.
  939. *
  940. * The downside is that we have to touch page->_count against each page.
  941. * But we had to alter page->flags anyway.
  942. */
  943. static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
  944. struct scan_control *sc, int priority)
  945. {
  946. unsigned long pgmoved;
  947. int pgdeactivate = 0;
  948. unsigned long pgscanned;
  949. LIST_HEAD(l_hold); /* The pages which were snipped off */
  950. LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
  951. LIST_HEAD(l_active); /* Pages to go onto the active_list */
  952. struct page *page;
  953. struct pagevec pvec;
  954. int reclaim_mapped = 0;
  955. if (sc->may_swap)
  956. reclaim_mapped = calc_reclaim_mapped(sc, zone, priority);
  957. lru_add_drain();
  958. spin_lock_irq(&zone->lru_lock);
  959. pgmoved = sc->isolate_pages(nr_pages, &l_hold, &pgscanned, sc->order,
  960. ISOLATE_ACTIVE, zone,
  961. sc->mem_cgroup, 1);
  962. /*
  963. * zone->pages_scanned is used for detect zone's oom
  964. * mem_cgroup remembers nr_scan by itself.
  965. */
  966. if (scan_global_lru(sc))
  967. zone->pages_scanned += pgscanned;
  968. __mod_zone_page_state(zone, NR_ACTIVE, -pgmoved);
  969. spin_unlock_irq(&zone->lru_lock);
  970. while (!list_empty(&l_hold)) {
  971. cond_resched();
  972. page = lru_to_page(&l_hold);
  973. list_del(&page->lru);
  974. if (page_mapped(page)) {
  975. if (!reclaim_mapped ||
  976. (total_swap_pages == 0 && PageAnon(page)) ||
  977. page_referenced(page, 0, sc->mem_cgroup)) {
  978. list_add(&page->lru, &l_active);
  979. continue;
  980. }
  981. }
  982. list_add(&page->lru, &l_inactive);
  983. }
  984. pagevec_init(&pvec, 1);
  985. pgmoved = 0;
  986. spin_lock_irq(&zone->lru_lock);
  987. while (!list_empty(&l_inactive)) {
  988. page = lru_to_page(&l_inactive);
  989. prefetchw_prev_lru_page(page, &l_inactive, flags);
  990. VM_BUG_ON(PageLRU(page));
  991. SetPageLRU(page);
  992. VM_BUG_ON(!PageActive(page));
  993. ClearPageActive(page);
  994. list_move(&page->lru, &zone->inactive_list);
  995. mem_cgroup_move_lists(page_get_page_cgroup(page), false);
  996. pgmoved++;
  997. if (!pagevec_add(&pvec, page)) {
  998. __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
  999. spin_unlock_irq(&zone->lru_lock);
  1000. pgdeactivate += pgmoved;
  1001. pgmoved = 0;
  1002. if (buffer_heads_over_limit)
  1003. pagevec_strip(&pvec);
  1004. __pagevec_release(&pvec);
  1005. spin_lock_irq(&zone->lru_lock);
  1006. }
  1007. }
  1008. __mod_zone_page_state(zone, NR_INACTIVE, pgmoved);
  1009. pgdeactivate += pgmoved;
  1010. if (buffer_heads_over_limit) {
  1011. spin_unlock_irq(&zone->lru_lock);
  1012. pagevec_strip(&pvec);
  1013. spin_lock_irq(&zone->lru_lock);
  1014. }
  1015. pgmoved = 0;
  1016. while (!list_empty(&l_active)) {
  1017. page = lru_to_page(&l_active);
  1018. prefetchw_prev_lru_page(page, &l_active, flags);
  1019. VM_BUG_ON(PageLRU(page));
  1020. SetPageLRU(page);
  1021. VM_BUG_ON(!PageActive(page));
  1022. list_move(&page->lru, &zone->active_list);
  1023. mem_cgroup_move_lists(page_get_page_cgroup(page), true);
  1024. pgmoved++;
  1025. if (!pagevec_add(&pvec, page)) {
  1026. __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
  1027. pgmoved = 0;
  1028. spin_unlock_irq(&zone->lru_lock);
  1029. __pagevec_release(&pvec);
  1030. spin_lock_irq(&zone->lru_lock);
  1031. }
  1032. }
  1033. __mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
  1034. __count_zone_vm_events(PGREFILL, zone, pgscanned);
  1035. __count_vm_events(PGDEACTIVATE, pgdeactivate);
  1036. spin_unlock_irq(&zone->lru_lock);
  1037. pagevec_release(&pvec);
  1038. }
  1039. /*
  1040. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  1041. */
  1042. static unsigned long shrink_zone(int priority, struct zone *zone,
  1043. struct scan_control *sc)
  1044. {
  1045. unsigned long nr_active;
  1046. unsigned long nr_inactive;
  1047. unsigned long nr_to_scan;
  1048. unsigned long nr_reclaimed = 0;
  1049. if (scan_global_lru(sc)) {
  1050. /*
  1051. * Add one to nr_to_scan just to make sure that the kernel
  1052. * will slowly sift through the active list.
  1053. */
  1054. zone->nr_scan_active +=
  1055. (zone_page_state(zone, NR_ACTIVE) >> priority) + 1;
  1056. nr_active = zone->nr_scan_active;
  1057. zone->nr_scan_inactive +=
  1058. (zone_page_state(zone, NR_INACTIVE) >> priority) + 1;
  1059. nr_inactive = zone->nr_scan_inactive;
  1060. if (nr_inactive >= sc->swap_cluster_max)
  1061. zone->nr_scan_inactive = 0;
  1062. else
  1063. nr_inactive = 0;
  1064. if (nr_active >= sc->swap_cluster_max)
  1065. zone->nr_scan_active = 0;
  1066. else
  1067. nr_active = 0;
  1068. } else {
  1069. /*
  1070. * This reclaim occurs not because zone memory shortage but
  1071. * because memory controller hits its limit.
  1072. * Then, don't modify zone reclaim related data.
  1073. */
  1074. nr_active = mem_cgroup_calc_reclaim_active(sc->mem_cgroup,
  1075. zone, priority);
  1076. nr_inactive = mem_cgroup_calc_reclaim_inactive(sc->mem_cgroup,
  1077. zone, priority);
  1078. }
  1079. while (nr_active || nr_inactive) {
  1080. if (nr_active) {
  1081. nr_to_scan = min(nr_active,
  1082. (unsigned long)sc->swap_cluster_max);
  1083. nr_active -= nr_to_scan;
  1084. shrink_active_list(nr_to_scan, zone, sc, priority);
  1085. }
  1086. if (nr_inactive) {
  1087. nr_to_scan = min(nr_inactive,
  1088. (unsigned long)sc->swap_cluster_max);
  1089. nr_inactive -= nr_to_scan;
  1090. nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
  1091. sc);
  1092. }
  1093. }
  1094. throttle_vm_writeout(sc->gfp_mask);
  1095. return nr_reclaimed;
  1096. }
  1097. /*
  1098. * This is the direct reclaim path, for page-allocating processes. We only
  1099. * try to reclaim pages from zones which will satisfy the caller's allocation
  1100. * request.
  1101. *
  1102. * We reclaim from a zone even if that zone is over pages_high. Because:
  1103. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  1104. * allocation or
  1105. * b) The zones may be over pages_high but they must go *over* pages_high to
  1106. * satisfy the `incremental min' zone defense algorithm.
  1107. *
  1108. * Returns the number of reclaimed pages.
  1109. *
  1110. * If a zone is deemed to be full of pinned pages then just give it a light
  1111. * scan then give up on it.
  1112. */
  1113. static unsigned long shrink_zones(int priority, struct zone **zones,
  1114. struct scan_control *sc)
  1115. {
  1116. unsigned long nr_reclaimed = 0;
  1117. int i;
  1118. sc->all_unreclaimable = 1;
  1119. for (i = 0; zones[i] != NULL; i++) {
  1120. struct zone *zone = zones[i];
  1121. if (!populated_zone(zone))
  1122. continue;
  1123. /*
  1124. * Take care memory controller reclaiming has small influence
  1125. * to global LRU.
  1126. */
  1127. if (scan_global_lru(sc)) {
  1128. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1129. continue;
  1130. note_zone_scanning_priority(zone, priority);
  1131. if (zone_is_all_unreclaimable(zone) &&
  1132. priority != DEF_PRIORITY)
  1133. continue; /* Let kswapd poll it */
  1134. sc->all_unreclaimable = 0;
  1135. } else {
  1136. /*
  1137. * Ignore cpuset limitation here. We just want to reduce
  1138. * # of used pages by us regardless of memory shortage.
  1139. */
  1140. sc->all_unreclaimable = 0;
  1141. mem_cgroup_note_reclaim_priority(sc->mem_cgroup,
  1142. priority);
  1143. }
  1144. nr_reclaimed += shrink_zone(priority, zone, sc);
  1145. }
  1146. return nr_reclaimed;
  1147. }
  1148. /*
  1149. * This is the main entry point to direct page reclaim.
  1150. *
  1151. * If a full scan of the inactive list fails to free enough memory then we
  1152. * are "out of memory" and something needs to be killed.
  1153. *
  1154. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  1155. * high - the zone may be full of dirty or under-writeback pages, which this
  1156. * caller can't do much about. We kick pdflush and take explicit naps in the
  1157. * hope that some of these pages can be written. But if the allocating task
  1158. * holds filesystem locks which prevent writeout this might not work, and the
  1159. * allocation attempt will fail.
  1160. */
  1161. static unsigned long do_try_to_free_pages(struct zone **zones, gfp_t gfp_mask,
  1162. struct scan_control *sc)
  1163. {
  1164. int priority;
  1165. int ret = 0;
  1166. unsigned long total_scanned = 0;
  1167. unsigned long nr_reclaimed = 0;
  1168. struct reclaim_state *reclaim_state = current->reclaim_state;
  1169. unsigned long lru_pages = 0;
  1170. int i;
  1171. if (scan_global_lru(sc))
  1172. count_vm_event(ALLOCSTALL);
  1173. /*
  1174. * mem_cgroup will not do shrink_slab.
  1175. */
  1176. if (scan_global_lru(sc)) {
  1177. for (i = 0; zones[i] != NULL; i++) {
  1178. struct zone *zone = zones[i];
  1179. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1180. continue;
  1181. lru_pages += zone_page_state(zone, NR_ACTIVE)
  1182. + zone_page_state(zone, NR_INACTIVE);
  1183. }
  1184. }
  1185. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1186. sc->nr_scanned = 0;
  1187. sc->nr_io_pages = 0;
  1188. if (!priority)
  1189. disable_swap_token();
  1190. nr_reclaimed += shrink_zones(priority, zones, sc);
  1191. /*
  1192. * Don't shrink slabs when reclaiming memory from
  1193. * over limit cgroups
  1194. */
  1195. if (scan_global_lru(sc)) {
  1196. shrink_slab(sc->nr_scanned, gfp_mask, lru_pages);
  1197. if (reclaim_state) {
  1198. nr_reclaimed += reclaim_state->reclaimed_slab;
  1199. reclaim_state->reclaimed_slab = 0;
  1200. }
  1201. }
  1202. total_scanned += sc->nr_scanned;
  1203. if (nr_reclaimed >= sc->swap_cluster_max) {
  1204. ret = 1;
  1205. goto out;
  1206. }
  1207. /*
  1208. * Try to write back as many pages as we just scanned. This
  1209. * tends to cause slow streaming writers to write data to the
  1210. * disk smoothly, at the dirtying rate, which is nice. But
  1211. * that's undesirable in laptop mode, where we *want* lumpy
  1212. * writeout. So in laptop mode, write out the whole world.
  1213. */
  1214. if (total_scanned > sc->swap_cluster_max +
  1215. sc->swap_cluster_max / 2) {
  1216. wakeup_pdflush(laptop_mode ? 0 : total_scanned);
  1217. sc->may_writepage = 1;
  1218. }
  1219. /* Take a nap, wait for some writeback to complete */
  1220. if (sc->nr_scanned && priority < DEF_PRIORITY - 2 &&
  1221. sc->nr_io_pages > sc->swap_cluster_max)
  1222. congestion_wait(WRITE, HZ/10);
  1223. }
  1224. /* top priority shrink_caches still had more to do? don't OOM, then */
  1225. if (!sc->all_unreclaimable && scan_global_lru(sc))
  1226. ret = 1;
  1227. out:
  1228. /*
  1229. * Now that we've scanned all the zones at this priority level, note
  1230. * that level within the zone so that the next thread which performs
  1231. * scanning of this zone will immediately start out at this priority
  1232. * level. This affects only the decision whether or not to bring
  1233. * mapped pages onto the inactive list.
  1234. */
  1235. if (priority < 0)
  1236. priority = 0;
  1237. if (scan_global_lru(sc)) {
  1238. for (i = 0; zones[i] != NULL; i++) {
  1239. struct zone *zone = zones[i];
  1240. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1241. continue;
  1242. zone->prev_priority = priority;
  1243. }
  1244. } else
  1245. mem_cgroup_record_reclaim_priority(sc->mem_cgroup, priority);
  1246. return ret;
  1247. }
  1248. unsigned long try_to_free_pages(struct zone **zones, int order, gfp_t gfp_mask)
  1249. {
  1250. struct scan_control sc = {
  1251. .gfp_mask = gfp_mask,
  1252. .may_writepage = !laptop_mode,
  1253. .swap_cluster_max = SWAP_CLUSTER_MAX,
  1254. .may_swap = 1,
  1255. .swappiness = vm_swappiness,
  1256. .order = order,
  1257. .mem_cgroup = NULL,
  1258. .isolate_pages = isolate_pages_global,
  1259. };
  1260. return do_try_to_free_pages(zones, gfp_mask, &sc);
  1261. }
  1262. #ifdef CONFIG_CGROUP_MEM_CONT
  1263. unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont,
  1264. gfp_t gfp_mask)
  1265. {
  1266. struct scan_control sc = {
  1267. .gfp_mask = gfp_mask,
  1268. .may_writepage = !laptop_mode,
  1269. .may_swap = 1,
  1270. .swap_cluster_max = SWAP_CLUSTER_MAX,
  1271. .swappiness = vm_swappiness,
  1272. .order = 0,
  1273. .mem_cgroup = mem_cont,
  1274. .isolate_pages = mem_cgroup_isolate_pages,
  1275. };
  1276. struct zone **zones;
  1277. int target_zone = gfp_zone(GFP_HIGHUSER_MOVABLE);
  1278. zones = NODE_DATA(numa_node_id())->node_zonelists[target_zone].zones;
  1279. if (do_try_to_free_pages(zones, sc.gfp_mask, &sc))
  1280. return 1;
  1281. return 0;
  1282. }
  1283. #endif
  1284. /*
  1285. * For kswapd, balance_pgdat() will work across all this node's zones until
  1286. * they are all at pages_high.
  1287. *
  1288. * Returns the number of pages which were actually freed.
  1289. *
  1290. * There is special handling here for zones which are full of pinned pages.
  1291. * This can happen if the pages are all mlocked, or if they are all used by
  1292. * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
  1293. * What we do is to detect the case where all pages in the zone have been
  1294. * scanned twice and there has been zero successful reclaim. Mark the zone as
  1295. * dead and from now on, only perform a short scan. Basically we're polling
  1296. * the zone for when the problem goes away.
  1297. *
  1298. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  1299. * zones which have free_pages > pages_high, but once a zone is found to have
  1300. * free_pages <= pages_high, we scan that zone and the lower zones regardless
  1301. * of the number of free pages in the lower zones. This interoperates with
  1302. * the page allocator fallback scheme to ensure that aging of pages is balanced
  1303. * across the zones.
  1304. */
  1305. static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
  1306. {
  1307. int all_zones_ok;
  1308. int priority;
  1309. int i;
  1310. unsigned long total_scanned;
  1311. unsigned long nr_reclaimed;
  1312. struct reclaim_state *reclaim_state = current->reclaim_state;
  1313. struct scan_control sc = {
  1314. .gfp_mask = GFP_KERNEL,
  1315. .may_swap = 1,
  1316. .swap_cluster_max = SWAP_CLUSTER_MAX,
  1317. .swappiness = vm_swappiness,
  1318. .order = order,
  1319. .mem_cgroup = NULL,
  1320. .isolate_pages = isolate_pages_global,
  1321. };
  1322. /*
  1323. * temp_priority is used to remember the scanning priority at which
  1324. * this zone was successfully refilled to free_pages == pages_high.
  1325. */
  1326. int temp_priority[MAX_NR_ZONES];
  1327. loop_again:
  1328. total_scanned = 0;
  1329. nr_reclaimed = 0;
  1330. sc.may_writepage = !laptop_mode;
  1331. count_vm_event(PAGEOUTRUN);
  1332. for (i = 0; i < pgdat->nr_zones; i++)
  1333. temp_priority[i] = DEF_PRIORITY;
  1334. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1335. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  1336. unsigned long lru_pages = 0;
  1337. /* The swap token gets in the way of swapout... */
  1338. if (!priority)
  1339. disable_swap_token();
  1340. sc.nr_io_pages = 0;
  1341. all_zones_ok = 1;
  1342. /*
  1343. * Scan in the highmem->dma direction for the highest
  1344. * zone which needs scanning
  1345. */
  1346. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  1347. struct zone *zone = pgdat->node_zones + i;
  1348. if (!populated_zone(zone))
  1349. continue;
  1350. if (zone_is_all_unreclaimable(zone) &&
  1351. priority != DEF_PRIORITY)
  1352. continue;
  1353. if (!zone_watermark_ok(zone, order, zone->pages_high,
  1354. 0, 0)) {
  1355. end_zone = i;
  1356. break;
  1357. }
  1358. }
  1359. if (i < 0)
  1360. goto out;
  1361. for (i = 0; i <= end_zone; i++) {
  1362. struct zone *zone = pgdat->node_zones + i;
  1363. lru_pages += zone_page_state(zone, NR_ACTIVE)
  1364. + zone_page_state(zone, NR_INACTIVE);
  1365. }
  1366. /*
  1367. * Now scan the zone in the dma->highmem direction, stopping
  1368. * at the last zone which needs scanning.
  1369. *
  1370. * We do this because the page allocator works in the opposite
  1371. * direction. This prevents the page allocator from allocating
  1372. * pages behind kswapd's direction of progress, which would
  1373. * cause too much scanning of the lower zones.
  1374. */
  1375. for (i = 0; i <= end_zone; i++) {
  1376. struct zone *zone = pgdat->node_zones + i;
  1377. int nr_slab;
  1378. if (!populated_zone(zone))
  1379. continue;
  1380. if (zone_is_all_unreclaimable(zone) &&
  1381. priority != DEF_PRIORITY)
  1382. continue;
  1383. if (!zone_watermark_ok(zone, order, zone->pages_high,
  1384. end_zone, 0))
  1385. all_zones_ok = 0;
  1386. temp_priority[i] = priority;
  1387. sc.nr_scanned = 0;
  1388. note_zone_scanning_priority(zone, priority);
  1389. /*
  1390. * We put equal pressure on every zone, unless one
  1391. * zone has way too many pages free already.
  1392. */
  1393. if (!zone_watermark_ok(zone, order, 8*zone->pages_high,
  1394. end_zone, 0))
  1395. nr_reclaimed += shrink_zone(priority, zone, &sc);
  1396. reclaim_state->reclaimed_slab = 0;
  1397. nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
  1398. lru_pages);
  1399. nr_reclaimed += reclaim_state->reclaimed_slab;
  1400. total_scanned += sc.nr_scanned;
  1401. if (zone_is_all_unreclaimable(zone))
  1402. continue;
  1403. if (nr_slab == 0 && zone->pages_scanned >=
  1404. (zone_page_state(zone, NR_ACTIVE)
  1405. + zone_page_state(zone, NR_INACTIVE)) * 6)
  1406. zone_set_flag(zone,
  1407. ZONE_ALL_UNRECLAIMABLE);
  1408. /*
  1409. * If we've done a decent amount of scanning and
  1410. * the reclaim ratio is low, start doing writepage
  1411. * even in laptop mode
  1412. */
  1413. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  1414. total_scanned > nr_reclaimed + nr_reclaimed / 2)
  1415. sc.may_writepage = 1;
  1416. }
  1417. if (all_zones_ok)
  1418. break; /* kswapd: all done */
  1419. /*
  1420. * OK, kswapd is getting into trouble. Take a nap, then take
  1421. * another pass across the zones.
  1422. */
  1423. if (total_scanned && priority < DEF_PRIORITY - 2 &&
  1424. sc.nr_io_pages > sc.swap_cluster_max)
  1425. congestion_wait(WRITE, HZ/10);
  1426. /*
  1427. * We do this so kswapd doesn't build up large priorities for
  1428. * example when it is freeing in parallel with allocators. It
  1429. * matches the direct reclaim path behaviour in terms of impact
  1430. * on zone->*_priority.
  1431. */
  1432. if (nr_reclaimed >= SWAP_CLUSTER_MAX)
  1433. break;
  1434. }
  1435. out:
  1436. /*
  1437. * Note within each zone the priority level at which this zone was
  1438. * brought into a happy state. So that the next thread which scans this
  1439. * zone will start out at that priority level.
  1440. */
  1441. for (i = 0; i < pgdat->nr_zones; i++) {
  1442. struct zone *zone = pgdat->node_zones + i;
  1443. zone->prev_priority = temp_priority[i];
  1444. }
  1445. if (!all_zones_ok) {
  1446. cond_resched();
  1447. try_to_freeze();
  1448. goto loop_again;
  1449. }
  1450. return nr_reclaimed;
  1451. }
  1452. /*
  1453. * The background pageout daemon, started as a kernel thread
  1454. * from the init process.
  1455. *
  1456. * This basically trickles out pages so that we have _some_
  1457. * free memory available even if there is no other activity
  1458. * that frees anything up. This is needed for things like routing
  1459. * etc, where we otherwise might have all activity going on in
  1460. * asynchronous contexts that cannot page things out.
  1461. *
  1462. * If there are applications that are active memory-allocators
  1463. * (most normal use), this basically shouldn't matter.
  1464. */
  1465. static int kswapd(void *p)
  1466. {
  1467. unsigned long order;
  1468. pg_data_t *pgdat = (pg_data_t*)p;
  1469. struct task_struct *tsk = current;
  1470. DEFINE_WAIT(wait);
  1471. struct reclaim_state reclaim_state = {
  1472. .reclaimed_slab = 0,
  1473. };
  1474. cpumask_t cpumask;
  1475. cpumask = node_to_cpumask(pgdat->node_id);
  1476. if (!cpus_empty(cpumask))
  1477. set_cpus_allowed(tsk, cpumask);
  1478. current->reclaim_state = &reclaim_state;
  1479. /*
  1480. * Tell the memory management that we're a "memory allocator",
  1481. * and that if we need more memory we should get access to it
  1482. * regardless (see "__alloc_pages()"). "kswapd" should
  1483. * never get caught in the normal page freeing logic.
  1484. *
  1485. * (Kswapd normally doesn't need memory anyway, but sometimes
  1486. * you need a small amount of memory in order to be able to
  1487. * page out something else, and this flag essentially protects
  1488. * us from recursively trying to free more memory as we're
  1489. * trying to free the first piece of memory in the first place).
  1490. */
  1491. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  1492. set_freezable();
  1493. order = 0;
  1494. for ( ; ; ) {
  1495. unsigned long new_order;
  1496. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  1497. new_order = pgdat->kswapd_max_order;
  1498. pgdat->kswapd_max_order = 0;
  1499. if (order < new_order) {
  1500. /*
  1501. * Don't sleep if someone wants a larger 'order'
  1502. * allocation
  1503. */
  1504. order = new_order;
  1505. } else {
  1506. if (!freezing(current))
  1507. schedule();
  1508. order = pgdat->kswapd_max_order;
  1509. }
  1510. finish_wait(&pgdat->kswapd_wait, &wait);
  1511. if (!try_to_freeze()) {
  1512. /* We can speed up thawing tasks if we don't call
  1513. * balance_pgdat after returning from the refrigerator
  1514. */
  1515. balance_pgdat(pgdat, order);
  1516. }
  1517. }
  1518. return 0;
  1519. }
  1520. /*
  1521. * A zone is low on free memory, so wake its kswapd task to service it.
  1522. */
  1523. void wakeup_kswapd(struct zone *zone, int order)
  1524. {
  1525. pg_data_t *pgdat;
  1526. if (!populated_zone(zone))
  1527. return;
  1528. pgdat = zone->zone_pgdat;
  1529. if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
  1530. return;
  1531. if (pgdat->kswapd_max_order < order)
  1532. pgdat->kswapd_max_order = order;
  1533. if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
  1534. return;
  1535. if (!waitqueue_active(&pgdat->kswapd_wait))
  1536. return;
  1537. wake_up_interruptible(&pgdat->kswapd_wait);
  1538. }
  1539. #ifdef CONFIG_PM
  1540. /*
  1541. * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
  1542. * from LRU lists system-wide, for given pass and priority, and returns the
  1543. * number of reclaimed pages
  1544. *
  1545. * For pass > 3 we also try to shrink the LRU lists that contain a few pages
  1546. */
  1547. static unsigned long shrink_all_zones(unsigned long nr_pages, int prio,
  1548. int pass, struct scan_control *sc)
  1549. {
  1550. struct zone *zone;
  1551. unsigned long nr_to_scan, ret = 0;
  1552. for_each_zone(zone) {
  1553. if (!populated_zone(zone))
  1554. continue;
  1555. if (zone_is_all_unreclaimable(zone) && prio != DEF_PRIORITY)
  1556. continue;
  1557. /* For pass = 0 we don't shrink the active list */
  1558. if (pass > 0) {
  1559. zone->nr_scan_active +=
  1560. (zone_page_state(zone, NR_ACTIVE) >> prio) + 1;
  1561. if (zone->nr_scan_active >= nr_pages || pass > 3) {
  1562. zone->nr_scan_active = 0;
  1563. nr_to_scan = min(nr_pages,
  1564. zone_page_state(zone, NR_ACTIVE));
  1565. shrink_active_list(nr_to_scan, zone, sc, prio);
  1566. }
  1567. }
  1568. zone->nr_scan_inactive +=
  1569. (zone_page_state(zone, NR_INACTIVE) >> prio) + 1;
  1570. if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
  1571. zone->nr_scan_inactive = 0;
  1572. nr_to_scan = min(nr_pages,
  1573. zone_page_state(zone, NR_INACTIVE));
  1574. ret += shrink_inactive_list(nr_to_scan, zone, sc);
  1575. if (ret >= nr_pages)
  1576. return ret;
  1577. }
  1578. }
  1579. return ret;
  1580. }
  1581. static unsigned long count_lru_pages(void)
  1582. {
  1583. return global_page_state(NR_ACTIVE) + global_page_state(NR_INACTIVE);
  1584. }
  1585. /*
  1586. * Try to free `nr_pages' of memory, system-wide, and return the number of
  1587. * freed pages.
  1588. *
  1589. * Rather than trying to age LRUs the aim is to preserve the overall
  1590. * LRU order by reclaiming preferentially
  1591. * inactive > active > active referenced > active mapped
  1592. */
  1593. unsigned long shrink_all_memory(unsigned long nr_pages)
  1594. {
  1595. unsigned long lru_pages, nr_slab;
  1596. unsigned long ret = 0;
  1597. int pass;
  1598. struct reclaim_state reclaim_state;
  1599. struct scan_control sc = {
  1600. .gfp_mask = GFP_KERNEL,
  1601. .may_swap = 0,
  1602. .swap_cluster_max = nr_pages,
  1603. .may_writepage = 1,
  1604. .swappiness = vm_swappiness,
  1605. .isolate_pages = isolate_pages_global,
  1606. };
  1607. current->reclaim_state = &reclaim_state;
  1608. lru_pages = count_lru_pages();
  1609. nr_slab = global_page_state(NR_SLAB_RECLAIMABLE);
  1610. /* If slab caches are huge, it's better to hit them first */
  1611. while (nr_slab >= lru_pages) {
  1612. reclaim_state.reclaimed_slab = 0;
  1613. shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
  1614. if (!reclaim_state.reclaimed_slab)
  1615. break;
  1616. ret += reclaim_state.reclaimed_slab;
  1617. if (ret >= nr_pages)
  1618. goto out;
  1619. nr_slab -= reclaim_state.reclaimed_slab;
  1620. }
  1621. /*
  1622. * We try to shrink LRUs in 5 passes:
  1623. * 0 = Reclaim from inactive_list only
  1624. * 1 = Reclaim from active list but don't reclaim mapped
  1625. * 2 = 2nd pass of type 1
  1626. * 3 = Reclaim mapped (normal reclaim)
  1627. * 4 = 2nd pass of type 3
  1628. */
  1629. for (pass = 0; pass < 5; pass++) {
  1630. int prio;
  1631. /* Force reclaiming mapped pages in the passes #3 and #4 */
  1632. if (pass > 2) {
  1633. sc.may_swap = 1;
  1634. sc.swappiness = 100;
  1635. }
  1636. for (prio = DEF_PRIORITY; prio >= 0; prio--) {
  1637. unsigned long nr_to_scan = nr_pages - ret;
  1638. sc.nr_scanned = 0;
  1639. ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
  1640. if (ret >= nr_pages)
  1641. goto out;
  1642. reclaim_state.reclaimed_slab = 0;
  1643. shrink_slab(sc.nr_scanned, sc.gfp_mask,
  1644. count_lru_pages());
  1645. ret += reclaim_state.reclaimed_slab;
  1646. if (ret >= nr_pages)
  1647. goto out;
  1648. if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
  1649. congestion_wait(WRITE, HZ / 10);
  1650. }
  1651. }
  1652. /*
  1653. * If ret = 0, we could not shrink LRUs, but there may be something
  1654. * in slab caches
  1655. */
  1656. if (!ret) {
  1657. do {
  1658. reclaim_state.reclaimed_slab = 0;
  1659. shrink_slab(nr_pages, sc.gfp_mask, count_lru_pages());
  1660. ret += reclaim_state.reclaimed_slab;
  1661. } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
  1662. }
  1663. out:
  1664. current->reclaim_state = NULL;
  1665. return ret;
  1666. }
  1667. #endif
  1668. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  1669. not required for correctness. So if the last cpu in a node goes
  1670. away, we get changed to run anywhere: as the first one comes back,
  1671. restore their cpu bindings. */
  1672. static int __devinit cpu_callback(struct notifier_block *nfb,
  1673. unsigned long action, void *hcpu)
  1674. {
  1675. pg_data_t *pgdat;
  1676. cpumask_t mask;
  1677. int nid;
  1678. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
  1679. for_each_node_state(nid, N_HIGH_MEMORY) {
  1680. pgdat = NODE_DATA(nid);
  1681. mask = node_to_cpumask(pgdat->node_id);
  1682. if (any_online_cpu(mask) != NR_CPUS)
  1683. /* One of our CPUs online: restore mask */
  1684. set_cpus_allowed(pgdat->kswapd, mask);
  1685. }
  1686. }
  1687. return NOTIFY_OK;
  1688. }
  1689. /*
  1690. * This kswapd start function will be called by init and node-hot-add.
  1691. * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
  1692. */
  1693. int kswapd_run(int nid)
  1694. {
  1695. pg_data_t *pgdat = NODE_DATA(nid);
  1696. int ret = 0;
  1697. if (pgdat->kswapd)
  1698. return 0;
  1699. pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
  1700. if (IS_ERR(pgdat->kswapd)) {
  1701. /* failure at boot is fatal */
  1702. BUG_ON(system_state == SYSTEM_BOOTING);
  1703. printk("Failed to start kswapd on node %d\n",nid);
  1704. ret = -1;
  1705. }
  1706. return ret;
  1707. }
  1708. static int __init kswapd_init(void)
  1709. {
  1710. int nid;
  1711. swap_setup();
  1712. for_each_node_state(nid, N_HIGH_MEMORY)
  1713. kswapd_run(nid);
  1714. hotcpu_notifier(cpu_callback, 0);
  1715. return 0;
  1716. }
  1717. module_init(kswapd_init)
  1718. #ifdef CONFIG_NUMA
  1719. /*
  1720. * Zone reclaim mode
  1721. *
  1722. * If non-zero call zone_reclaim when the number of free pages falls below
  1723. * the watermarks.
  1724. */
  1725. int zone_reclaim_mode __read_mostly;
  1726. #define RECLAIM_OFF 0
  1727. #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
  1728. #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
  1729. #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
  1730. /*
  1731. * Priority for ZONE_RECLAIM. This determines the fraction of pages
  1732. * of a node considered for each zone_reclaim. 4 scans 1/16th of
  1733. * a zone.
  1734. */
  1735. #define ZONE_RECLAIM_PRIORITY 4
  1736. /*
  1737. * Percentage of pages in a zone that must be unmapped for zone_reclaim to
  1738. * occur.
  1739. */
  1740. int sysctl_min_unmapped_ratio = 1;
  1741. /*
  1742. * If the number of slab pages in a zone grows beyond this percentage then
  1743. * slab reclaim needs to occur.
  1744. */
  1745. int sysctl_min_slab_ratio = 5;
  1746. /*
  1747. * Try to free up some pages from this zone through reclaim.
  1748. */
  1749. static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  1750. {
  1751. /* Minimum pages needed in order to stay on node */
  1752. const unsigned long nr_pages = 1 << order;
  1753. struct task_struct *p = current;
  1754. struct reclaim_state reclaim_state;
  1755. int priority;
  1756. unsigned long nr_reclaimed = 0;
  1757. struct scan_control sc = {
  1758. .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
  1759. .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
  1760. .swap_cluster_max = max_t(unsigned long, nr_pages,
  1761. SWAP_CLUSTER_MAX),
  1762. .gfp_mask = gfp_mask,
  1763. .swappiness = vm_swappiness,
  1764. .isolate_pages = isolate_pages_global,
  1765. };
  1766. unsigned long slab_reclaimable;
  1767. disable_swap_token();
  1768. cond_resched();
  1769. /*
  1770. * We need to be able to allocate from the reserves for RECLAIM_SWAP
  1771. * and we also need to be able to write out pages for RECLAIM_WRITE
  1772. * and RECLAIM_SWAP.
  1773. */
  1774. p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
  1775. reclaim_state.reclaimed_slab = 0;
  1776. p->reclaim_state = &reclaim_state;
  1777. if (zone_page_state(zone, NR_FILE_PAGES) -
  1778. zone_page_state(zone, NR_FILE_MAPPED) >
  1779. zone->min_unmapped_pages) {
  1780. /*
  1781. * Free memory by calling shrink zone with increasing
  1782. * priorities until we have enough memory freed.
  1783. */
  1784. priority = ZONE_RECLAIM_PRIORITY;
  1785. do {
  1786. note_zone_scanning_priority(zone, priority);
  1787. nr_reclaimed += shrink_zone(priority, zone, &sc);
  1788. priority--;
  1789. } while (priority >= 0 && nr_reclaimed < nr_pages);
  1790. }
  1791. slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  1792. if (slab_reclaimable > zone->min_slab_pages) {
  1793. /*
  1794. * shrink_slab() does not currently allow us to determine how
  1795. * many pages were freed in this zone. So we take the current
  1796. * number of slab pages and shake the slab until it is reduced
  1797. * by the same nr_pages that we used for reclaiming unmapped
  1798. * pages.
  1799. *
  1800. * Note that shrink_slab will free memory on all zones and may
  1801. * take a long time.
  1802. */
  1803. while (shrink_slab(sc.nr_scanned, gfp_mask, order) &&
  1804. zone_page_state(zone, NR_SLAB_RECLAIMABLE) >
  1805. slab_reclaimable - nr_pages)
  1806. ;
  1807. /*
  1808. * Update nr_reclaimed by the number of slab pages we
  1809. * reclaimed from this zone.
  1810. */
  1811. nr_reclaimed += slab_reclaimable -
  1812. zone_page_state(zone, NR_SLAB_RECLAIMABLE);
  1813. }
  1814. p->reclaim_state = NULL;
  1815. current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
  1816. return nr_reclaimed >= nr_pages;
  1817. }
  1818. int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  1819. {
  1820. int node_id;
  1821. int ret;
  1822. /*
  1823. * Zone reclaim reclaims unmapped file backed pages and
  1824. * slab pages if we are over the defined limits.
  1825. *
  1826. * A small portion of unmapped file backed pages is needed for
  1827. * file I/O otherwise pages read by file I/O will be immediately
  1828. * thrown out if the zone is overallocated. So we do not reclaim
  1829. * if less than a specified percentage of the zone is used by
  1830. * unmapped file backed pages.
  1831. */
  1832. if (zone_page_state(zone, NR_FILE_PAGES) -
  1833. zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages
  1834. && zone_page_state(zone, NR_SLAB_RECLAIMABLE)
  1835. <= zone->min_slab_pages)
  1836. return 0;
  1837. if (zone_is_all_unreclaimable(zone))
  1838. return 0;
  1839. /*
  1840. * Do not scan if the allocation should not be delayed.
  1841. */
  1842. if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
  1843. return 0;
  1844. /*
  1845. * Only run zone reclaim on the local zone or on zones that do not
  1846. * have associated processors. This will favor the local processor
  1847. * over remote processors and spread off node memory allocations
  1848. * as wide as possible.
  1849. */
  1850. node_id = zone_to_nid(zone);
  1851. if (node_state(node_id, N_CPU) && node_id != numa_node_id())
  1852. return 0;
  1853. if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
  1854. return 0;
  1855. ret = __zone_reclaim(zone, gfp_mask, order);
  1856. zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
  1857. return ret;
  1858. }
  1859. #endif