vmscan.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619
  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/file.h>
  22. #include <linux/writeback.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/buffer_head.h> /* for try_to_release_page(),
  25. buffer_heads_over_limit */
  26. #include <linux/mm_inline.h>
  27. #include <linux/pagevec.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/rmap.h>
  30. #include <linux/topology.h>
  31. #include <linux/cpu.h>
  32. #include <linux/cpuset.h>
  33. #include <linux/notifier.h>
  34. #include <linux/rwsem.h>
  35. #include <linux/delay.h>
  36. #include <asm/tlbflush.h>
  37. #include <asm/div64.h>
  38. #include <linux/swapops.h>
  39. #include "internal.h"
  40. struct scan_control {
  41. /* Incremented by the number of inactive pages that were scanned */
  42. unsigned long nr_scanned;
  43. unsigned long nr_mapped; /* From page_state */
  44. /* This context's GFP mask */
  45. gfp_t gfp_mask;
  46. int may_writepage;
  47. /* Can pages be swapped as part of reclaim? */
  48. int may_swap;
  49. /* This context's SWAP_CLUSTER_MAX. If freeing memory for
  50. * suspend, we effectively ignore SWAP_CLUSTER_MAX.
  51. * In this context, it doesn't matter that we scan the
  52. * whole list at once. */
  53. int swap_cluster_max;
  54. int swappiness;
  55. };
  56. /*
  57. * The list of shrinker callbacks used by to apply pressure to
  58. * ageable caches.
  59. */
  60. struct shrinker {
  61. shrinker_t shrinker;
  62. struct list_head list;
  63. int seeks; /* seeks to recreate an obj */
  64. long nr; /* objs pending delete */
  65. };
  66. #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
  67. #ifdef ARCH_HAS_PREFETCH
  68. #define prefetch_prev_lru_page(_page, _base, _field) \
  69. do { \
  70. if ((_page)->lru.prev != _base) { \
  71. struct page *prev; \
  72. \
  73. prev = lru_to_page(&(_page->lru)); \
  74. prefetch(&prev->_field); \
  75. } \
  76. } while (0)
  77. #else
  78. #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
  79. #endif
  80. #ifdef ARCH_HAS_PREFETCHW
  81. #define prefetchw_prev_lru_page(_page, _base, _field) \
  82. do { \
  83. if ((_page)->lru.prev != _base) { \
  84. struct page *prev; \
  85. \
  86. prev = lru_to_page(&(_page->lru)); \
  87. prefetchw(&prev->_field); \
  88. } \
  89. } while (0)
  90. #else
  91. #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
  92. #endif
  93. /*
  94. * From 0 .. 100. Higher means more swappy.
  95. */
  96. int vm_swappiness = 60;
  97. static long total_memory;
  98. static LIST_HEAD(shrinker_list);
  99. static DECLARE_RWSEM(shrinker_rwsem);
  100. /*
  101. * Add a shrinker callback to be called from the vm
  102. */
  103. struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
  104. {
  105. struct shrinker *shrinker;
  106. shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
  107. if (shrinker) {
  108. shrinker->shrinker = theshrinker;
  109. shrinker->seeks = seeks;
  110. shrinker->nr = 0;
  111. down_write(&shrinker_rwsem);
  112. list_add_tail(&shrinker->list, &shrinker_list);
  113. up_write(&shrinker_rwsem);
  114. }
  115. return shrinker;
  116. }
  117. EXPORT_SYMBOL(set_shrinker);
  118. /*
  119. * Remove one
  120. */
  121. void remove_shrinker(struct shrinker *shrinker)
  122. {
  123. down_write(&shrinker_rwsem);
  124. list_del(&shrinker->list);
  125. up_write(&shrinker_rwsem);
  126. kfree(shrinker);
  127. }
  128. EXPORT_SYMBOL(remove_shrinker);
  129. #define SHRINK_BATCH 128
  130. /*
  131. * Call the shrink functions to age shrinkable caches
  132. *
  133. * Here we assume it costs one seek to replace a lru page and that it also
  134. * takes a seek to recreate a cache object. With this in mind we age equal
  135. * percentages of the lru and ageable caches. This should balance the seeks
  136. * generated by these structures.
  137. *
  138. * If the vm encounted mapped pages on the LRU it increase the pressure on
  139. * slab to avoid swapping.
  140. *
  141. * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
  142. *
  143. * `lru_pages' represents the number of on-LRU pages in all the zones which
  144. * are eligible for the caller's allocation attempt. It is used for balancing
  145. * slab reclaim versus page reclaim.
  146. *
  147. * Returns the number of slab objects which we shrunk.
  148. */
  149. unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
  150. unsigned long lru_pages)
  151. {
  152. struct shrinker *shrinker;
  153. unsigned long ret = 0;
  154. if (scanned == 0)
  155. scanned = SWAP_CLUSTER_MAX;
  156. if (!down_read_trylock(&shrinker_rwsem))
  157. return 1; /* Assume we'll be able to shrink next time */
  158. list_for_each_entry(shrinker, &shrinker_list, list) {
  159. unsigned long long delta;
  160. unsigned long total_scan;
  161. unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
  162. delta = (4 * scanned) / shrinker->seeks;
  163. delta *= max_pass;
  164. do_div(delta, lru_pages + 1);
  165. shrinker->nr += delta;
  166. if (shrinker->nr < 0) {
  167. printk(KERN_ERR "%s: nr=%ld\n",
  168. __FUNCTION__, shrinker->nr);
  169. shrinker->nr = max_pass;
  170. }
  171. /*
  172. * Avoid risking looping forever due to too large nr value:
  173. * never try to free more than twice the estimate number of
  174. * freeable entries.
  175. */
  176. if (shrinker->nr > max_pass * 2)
  177. shrinker->nr = max_pass * 2;
  178. total_scan = shrinker->nr;
  179. shrinker->nr = 0;
  180. while (total_scan >= SHRINK_BATCH) {
  181. long this_scan = SHRINK_BATCH;
  182. int shrink_ret;
  183. int nr_before;
  184. nr_before = (*shrinker->shrinker)(0, gfp_mask);
  185. shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
  186. if (shrink_ret == -1)
  187. break;
  188. if (shrink_ret < nr_before)
  189. ret += nr_before - shrink_ret;
  190. mod_page_state(slabs_scanned, this_scan);
  191. total_scan -= this_scan;
  192. cond_resched();
  193. }
  194. shrinker->nr += total_scan;
  195. }
  196. up_read(&shrinker_rwsem);
  197. return ret;
  198. }
  199. /* Called without lock on whether page is mapped, so answer is unstable */
  200. static inline int page_mapping_inuse(struct page *page)
  201. {
  202. struct address_space *mapping;
  203. /* Page is in somebody's page tables. */
  204. if (page_mapped(page))
  205. return 1;
  206. /* Be more reluctant to reclaim swapcache than pagecache */
  207. if (PageSwapCache(page))
  208. return 1;
  209. mapping = page_mapping(page);
  210. if (!mapping)
  211. return 0;
  212. /* File is mmap'd by somebody? */
  213. return mapping_mapped(mapping);
  214. }
  215. static inline int is_page_cache_freeable(struct page *page)
  216. {
  217. return page_count(page) - !!PagePrivate(page) == 2;
  218. }
  219. static int may_write_to_queue(struct backing_dev_info *bdi)
  220. {
  221. if (current->flags & PF_SWAPWRITE)
  222. return 1;
  223. if (!bdi_write_congested(bdi))
  224. return 1;
  225. if (bdi == current->backing_dev_info)
  226. return 1;
  227. return 0;
  228. }
  229. /*
  230. * We detected a synchronous write error writing a page out. Probably
  231. * -ENOSPC. We need to propagate that into the address_space for a subsequent
  232. * fsync(), msync() or close().
  233. *
  234. * The tricky part is that after writepage we cannot touch the mapping: nothing
  235. * prevents it from being freed up. But we have a ref on the page and once
  236. * that page is locked, the mapping is pinned.
  237. *
  238. * We're allowed to run sleeping lock_page() here because we know the caller has
  239. * __GFP_FS.
  240. */
  241. static void handle_write_error(struct address_space *mapping,
  242. struct page *page, int error)
  243. {
  244. lock_page(page);
  245. if (page_mapping(page) == mapping) {
  246. if (error == -ENOSPC)
  247. set_bit(AS_ENOSPC, &mapping->flags);
  248. else
  249. set_bit(AS_EIO, &mapping->flags);
  250. }
  251. unlock_page(page);
  252. }
  253. /*
  254. * pageout is called by shrink_page_list() for each dirty page.
  255. * Calls ->writepage().
  256. */
  257. pageout_t pageout(struct page *page, struct address_space *mapping)
  258. {
  259. /*
  260. * If the page is dirty, only perform writeback if that write
  261. * will be non-blocking. To prevent this allocation from being
  262. * stalled by pagecache activity. But note that there may be
  263. * stalls if we need to run get_block(). We could test
  264. * PagePrivate for that.
  265. *
  266. * If this process is currently in generic_file_write() against
  267. * this page's queue, we can perform writeback even if that
  268. * will block.
  269. *
  270. * If the page is swapcache, write it back even if that would
  271. * block, for some throttling. This happens by accident, because
  272. * swap_backing_dev_info is bust: it doesn't reflect the
  273. * congestion state of the swapdevs. Easy to fix, if needed.
  274. * See swapfile.c:page_queue_congested().
  275. */
  276. if (!is_page_cache_freeable(page))
  277. return PAGE_KEEP;
  278. if (!mapping) {
  279. /*
  280. * Some data journaling orphaned pages can have
  281. * page->mapping == NULL while being dirty with clean buffers.
  282. */
  283. if (PagePrivate(page)) {
  284. if (try_to_free_buffers(page)) {
  285. ClearPageDirty(page);
  286. printk("%s: orphaned page\n", __FUNCTION__);
  287. return PAGE_CLEAN;
  288. }
  289. }
  290. return PAGE_KEEP;
  291. }
  292. if (mapping->a_ops->writepage == NULL)
  293. return PAGE_ACTIVATE;
  294. if (!may_write_to_queue(mapping->backing_dev_info))
  295. return PAGE_KEEP;
  296. if (clear_page_dirty_for_io(page)) {
  297. int res;
  298. struct writeback_control wbc = {
  299. .sync_mode = WB_SYNC_NONE,
  300. .nr_to_write = SWAP_CLUSTER_MAX,
  301. .nonblocking = 1,
  302. .for_reclaim = 1,
  303. };
  304. SetPageReclaim(page);
  305. res = mapping->a_ops->writepage(page, &wbc);
  306. if (res < 0)
  307. handle_write_error(mapping, page, res);
  308. if (res == AOP_WRITEPAGE_ACTIVATE) {
  309. ClearPageReclaim(page);
  310. return PAGE_ACTIVATE;
  311. }
  312. if (!PageWriteback(page)) {
  313. /* synchronous write or broken a_ops? */
  314. ClearPageReclaim(page);
  315. }
  316. return PAGE_SUCCESS;
  317. }
  318. return PAGE_CLEAN;
  319. }
  320. int remove_mapping(struct address_space *mapping, struct page *page)
  321. {
  322. if (!mapping)
  323. return 0; /* truncate got there first */
  324. write_lock_irq(&mapping->tree_lock);
  325. /*
  326. * The non-racy check for busy page. It is critical to check
  327. * PageDirty _after_ making sure that the page is freeable and
  328. * not in use by anybody. (pagecache + us == 2)
  329. */
  330. if (unlikely(page_count(page) != 2))
  331. goto cannot_free;
  332. smp_rmb();
  333. if (unlikely(PageDirty(page)))
  334. goto cannot_free;
  335. if (PageSwapCache(page)) {
  336. swp_entry_t swap = { .val = page_private(page) };
  337. __delete_from_swap_cache(page);
  338. write_unlock_irq(&mapping->tree_lock);
  339. swap_free(swap);
  340. __put_page(page); /* The pagecache ref */
  341. return 1;
  342. }
  343. __remove_from_page_cache(page);
  344. write_unlock_irq(&mapping->tree_lock);
  345. __put_page(page);
  346. return 1;
  347. cannot_free:
  348. write_unlock_irq(&mapping->tree_lock);
  349. return 0;
  350. }
  351. /*
  352. * shrink_page_list() returns the number of reclaimed pages
  353. */
  354. static unsigned long shrink_page_list(struct list_head *page_list,
  355. struct scan_control *sc)
  356. {
  357. LIST_HEAD(ret_pages);
  358. struct pagevec freed_pvec;
  359. int pgactivate = 0;
  360. unsigned long nr_reclaimed = 0;
  361. cond_resched();
  362. pagevec_init(&freed_pvec, 1);
  363. while (!list_empty(page_list)) {
  364. struct address_space *mapping;
  365. struct page *page;
  366. int may_enter_fs;
  367. int referenced;
  368. cond_resched();
  369. page = lru_to_page(page_list);
  370. list_del(&page->lru);
  371. if (TestSetPageLocked(page))
  372. goto keep;
  373. BUG_ON(PageActive(page));
  374. sc->nr_scanned++;
  375. if (!sc->may_swap && page_mapped(page))
  376. goto keep_locked;
  377. /* Double the slab pressure for mapped and swapcache pages */
  378. if (page_mapped(page) || PageSwapCache(page))
  379. sc->nr_scanned++;
  380. if (PageWriteback(page))
  381. goto keep_locked;
  382. referenced = page_referenced(page, 1);
  383. /* In active use or really unfreeable? Activate it. */
  384. if (referenced && page_mapping_inuse(page))
  385. goto activate_locked;
  386. #ifdef CONFIG_SWAP
  387. /*
  388. * Anonymous process memory has backing store?
  389. * Try to allocate it some swap space here.
  390. */
  391. if (PageAnon(page) && !PageSwapCache(page))
  392. if (!add_to_swap(page, GFP_ATOMIC))
  393. goto activate_locked;
  394. #endif /* CONFIG_SWAP */
  395. mapping = page_mapping(page);
  396. may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
  397. (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
  398. /*
  399. * The page is mapped into the page tables of one or more
  400. * processes. Try to unmap it here.
  401. */
  402. if (page_mapped(page) && mapping) {
  403. switch (try_to_unmap(page, 0)) {
  404. case SWAP_FAIL:
  405. goto activate_locked;
  406. case SWAP_AGAIN:
  407. goto keep_locked;
  408. case SWAP_SUCCESS:
  409. ; /* try to free the page below */
  410. }
  411. }
  412. if (PageDirty(page)) {
  413. if (referenced)
  414. goto keep_locked;
  415. if (!may_enter_fs)
  416. goto keep_locked;
  417. if (!sc->may_writepage)
  418. goto keep_locked;
  419. /* Page is dirty, try to write it out here */
  420. switch(pageout(page, mapping)) {
  421. case PAGE_KEEP:
  422. goto keep_locked;
  423. case PAGE_ACTIVATE:
  424. goto activate_locked;
  425. case PAGE_SUCCESS:
  426. if (PageWriteback(page) || PageDirty(page))
  427. goto keep;
  428. /*
  429. * A synchronous write - probably a ramdisk. Go
  430. * ahead and try to reclaim the page.
  431. */
  432. if (TestSetPageLocked(page))
  433. goto keep;
  434. if (PageDirty(page) || PageWriteback(page))
  435. goto keep_locked;
  436. mapping = page_mapping(page);
  437. case PAGE_CLEAN:
  438. ; /* try to free the page below */
  439. }
  440. }
  441. /*
  442. * If the page has buffers, try to free the buffer mappings
  443. * associated with this page. If we succeed we try to free
  444. * the page as well.
  445. *
  446. * We do this even if the page is PageDirty().
  447. * try_to_release_page() does not perform I/O, but it is
  448. * possible for a page to have PageDirty set, but it is actually
  449. * clean (all its buffers are clean). This happens if the
  450. * buffers were written out directly, with submit_bh(). ext3
  451. * will do this, as well as the blockdev mapping.
  452. * try_to_release_page() will discover that cleanness and will
  453. * drop the buffers and mark the page clean - it can be freed.
  454. *
  455. * Rarely, pages can have buffers and no ->mapping. These are
  456. * the pages which were not successfully invalidated in
  457. * truncate_complete_page(). We try to drop those buffers here
  458. * and if that worked, and the page is no longer mapped into
  459. * process address space (page_count == 1) it can be freed.
  460. * Otherwise, leave the page on the LRU so it is swappable.
  461. */
  462. if (PagePrivate(page)) {
  463. if (!try_to_release_page(page, sc->gfp_mask))
  464. goto activate_locked;
  465. if (!mapping && page_count(page) == 1)
  466. goto free_it;
  467. }
  468. if (!remove_mapping(mapping, page))
  469. goto keep_locked;
  470. free_it:
  471. unlock_page(page);
  472. nr_reclaimed++;
  473. if (!pagevec_add(&freed_pvec, page))
  474. __pagevec_release_nonlru(&freed_pvec);
  475. continue;
  476. activate_locked:
  477. SetPageActive(page);
  478. pgactivate++;
  479. keep_locked:
  480. unlock_page(page);
  481. keep:
  482. list_add(&page->lru, &ret_pages);
  483. BUG_ON(PageLRU(page));
  484. }
  485. list_splice(&ret_pages, page_list);
  486. if (pagevec_count(&freed_pvec))
  487. __pagevec_release_nonlru(&freed_pvec);
  488. mod_page_state(pgactivate, pgactivate);
  489. return nr_reclaimed;
  490. }
  491. /*
  492. * zone->lru_lock is heavily contended. Some of the functions that
  493. * shrink the lists perform better by taking out a batch of pages
  494. * and working on them outside the LRU lock.
  495. *
  496. * For pagecache intensive workloads, this function is the hottest
  497. * spot in the kernel (apart from copy_*_user functions).
  498. *
  499. * Appropriate locks must be held before calling this function.
  500. *
  501. * @nr_to_scan: The number of pages to look through on the list.
  502. * @src: The LRU list to pull pages off.
  503. * @dst: The temp list to put pages on to.
  504. * @scanned: The number of pages that were scanned.
  505. *
  506. * returns how many pages were moved onto *@dst.
  507. */
  508. static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
  509. struct list_head *src, struct list_head *dst,
  510. unsigned long *scanned)
  511. {
  512. unsigned long nr_taken = 0;
  513. struct page *page;
  514. unsigned long scan;
  515. for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
  516. struct list_head *target;
  517. page = lru_to_page(src);
  518. prefetchw_prev_lru_page(page, src, flags);
  519. BUG_ON(!PageLRU(page));
  520. list_del(&page->lru);
  521. target = src;
  522. if (likely(get_page_unless_zero(page))) {
  523. /*
  524. * Be careful not to clear PageLRU until after we're
  525. * sure the page is not being freed elsewhere -- the
  526. * page release code relies on it.
  527. */
  528. ClearPageLRU(page);
  529. target = dst;
  530. nr_taken++;
  531. } /* else it is being freed elsewhere */
  532. list_add(&page->lru, target);
  533. }
  534. *scanned = scan;
  535. return nr_taken;
  536. }
  537. /*
  538. * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
  539. * of reclaimed pages
  540. */
  541. static unsigned long shrink_inactive_list(unsigned long max_scan,
  542. struct zone *zone, struct scan_control *sc)
  543. {
  544. LIST_HEAD(page_list);
  545. struct pagevec pvec;
  546. unsigned long nr_scanned = 0;
  547. unsigned long nr_reclaimed = 0;
  548. pagevec_init(&pvec, 1);
  549. lru_add_drain();
  550. spin_lock_irq(&zone->lru_lock);
  551. do {
  552. struct page *page;
  553. unsigned long nr_taken;
  554. unsigned long nr_scan;
  555. unsigned long nr_freed;
  556. nr_taken = isolate_lru_pages(sc->swap_cluster_max,
  557. &zone->inactive_list,
  558. &page_list, &nr_scan);
  559. zone->nr_inactive -= nr_taken;
  560. zone->pages_scanned += nr_scan;
  561. spin_unlock_irq(&zone->lru_lock);
  562. nr_scanned += nr_scan;
  563. nr_freed = shrink_page_list(&page_list, sc);
  564. nr_reclaimed += nr_freed;
  565. local_irq_disable();
  566. if (current_is_kswapd()) {
  567. __mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
  568. __mod_page_state(kswapd_steal, nr_freed);
  569. } else
  570. __mod_page_state_zone(zone, pgscan_direct, nr_scan);
  571. __mod_page_state_zone(zone, pgsteal, nr_freed);
  572. if (nr_taken == 0)
  573. goto done;
  574. spin_lock(&zone->lru_lock);
  575. /*
  576. * Put back any unfreeable pages.
  577. */
  578. while (!list_empty(&page_list)) {
  579. page = lru_to_page(&page_list);
  580. BUG_ON(PageLRU(page));
  581. SetPageLRU(page);
  582. list_del(&page->lru);
  583. if (PageActive(page))
  584. add_page_to_active_list(zone, page);
  585. else
  586. add_page_to_inactive_list(zone, page);
  587. if (!pagevec_add(&pvec, page)) {
  588. spin_unlock_irq(&zone->lru_lock);
  589. __pagevec_release(&pvec);
  590. spin_lock_irq(&zone->lru_lock);
  591. }
  592. }
  593. } while (nr_scanned < max_scan);
  594. spin_unlock(&zone->lru_lock);
  595. done:
  596. local_irq_enable();
  597. pagevec_release(&pvec);
  598. return nr_reclaimed;
  599. }
  600. /*
  601. * This moves pages from the active list to the inactive list.
  602. *
  603. * We move them the other way if the page is referenced by one or more
  604. * processes, from rmap.
  605. *
  606. * If the pages are mostly unmapped, the processing is fast and it is
  607. * appropriate to hold zone->lru_lock across the whole operation. But if
  608. * the pages are mapped, the processing is slow (page_referenced()) so we
  609. * should drop zone->lru_lock around each page. It's impossible to balance
  610. * this, so instead we remove the pages from the LRU while processing them.
  611. * It is safe to rely on PG_active against the non-LRU pages in here because
  612. * nobody will play with that bit on a non-LRU page.
  613. *
  614. * The downside is that we have to touch page->_count against each page.
  615. * But we had to alter page->flags anyway.
  616. */
  617. static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
  618. struct scan_control *sc)
  619. {
  620. unsigned long pgmoved;
  621. int pgdeactivate = 0;
  622. unsigned long pgscanned;
  623. LIST_HEAD(l_hold); /* The pages which were snipped off */
  624. LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
  625. LIST_HEAD(l_active); /* Pages to go onto the active_list */
  626. struct page *page;
  627. struct pagevec pvec;
  628. int reclaim_mapped = 0;
  629. if (sc->may_swap) {
  630. long mapped_ratio;
  631. long distress;
  632. long swap_tendency;
  633. /*
  634. * `distress' is a measure of how much trouble we're having
  635. * reclaiming pages. 0 -> no problems. 100 -> great trouble.
  636. */
  637. distress = 100 >> zone->prev_priority;
  638. /*
  639. * The point of this algorithm is to decide when to start
  640. * reclaiming mapped memory instead of just pagecache. Work out
  641. * how much memory
  642. * is mapped.
  643. */
  644. mapped_ratio = (sc->nr_mapped * 100) / total_memory;
  645. /*
  646. * Now decide how much we really want to unmap some pages. The
  647. * mapped ratio is downgraded - just because there's a lot of
  648. * mapped memory doesn't necessarily mean that page reclaim
  649. * isn't succeeding.
  650. *
  651. * The distress ratio is important - we don't want to start
  652. * going oom.
  653. *
  654. * A 100% value of vm_swappiness overrides this algorithm
  655. * altogether.
  656. */
  657. swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
  658. /*
  659. * Now use this metric to decide whether to start moving mapped
  660. * memory onto the inactive list.
  661. */
  662. if (swap_tendency >= 100)
  663. reclaim_mapped = 1;
  664. }
  665. lru_add_drain();
  666. spin_lock_irq(&zone->lru_lock);
  667. pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
  668. &l_hold, &pgscanned);
  669. zone->pages_scanned += pgscanned;
  670. zone->nr_active -= pgmoved;
  671. spin_unlock_irq(&zone->lru_lock);
  672. while (!list_empty(&l_hold)) {
  673. cond_resched();
  674. page = lru_to_page(&l_hold);
  675. list_del(&page->lru);
  676. if (page_mapped(page)) {
  677. if (!reclaim_mapped ||
  678. (total_swap_pages == 0 && PageAnon(page)) ||
  679. page_referenced(page, 0)) {
  680. list_add(&page->lru, &l_active);
  681. continue;
  682. }
  683. }
  684. list_add(&page->lru, &l_inactive);
  685. }
  686. pagevec_init(&pvec, 1);
  687. pgmoved = 0;
  688. spin_lock_irq(&zone->lru_lock);
  689. while (!list_empty(&l_inactive)) {
  690. page = lru_to_page(&l_inactive);
  691. prefetchw_prev_lru_page(page, &l_inactive, flags);
  692. BUG_ON(PageLRU(page));
  693. SetPageLRU(page);
  694. BUG_ON(!PageActive(page));
  695. ClearPageActive(page);
  696. list_move(&page->lru, &zone->inactive_list);
  697. pgmoved++;
  698. if (!pagevec_add(&pvec, page)) {
  699. zone->nr_inactive += pgmoved;
  700. spin_unlock_irq(&zone->lru_lock);
  701. pgdeactivate += pgmoved;
  702. pgmoved = 0;
  703. if (buffer_heads_over_limit)
  704. pagevec_strip(&pvec);
  705. __pagevec_release(&pvec);
  706. spin_lock_irq(&zone->lru_lock);
  707. }
  708. }
  709. zone->nr_inactive += pgmoved;
  710. pgdeactivate += pgmoved;
  711. if (buffer_heads_over_limit) {
  712. spin_unlock_irq(&zone->lru_lock);
  713. pagevec_strip(&pvec);
  714. spin_lock_irq(&zone->lru_lock);
  715. }
  716. pgmoved = 0;
  717. while (!list_empty(&l_active)) {
  718. page = lru_to_page(&l_active);
  719. prefetchw_prev_lru_page(page, &l_active, flags);
  720. BUG_ON(PageLRU(page));
  721. SetPageLRU(page);
  722. BUG_ON(!PageActive(page));
  723. list_move(&page->lru, &zone->active_list);
  724. pgmoved++;
  725. if (!pagevec_add(&pvec, page)) {
  726. zone->nr_active += pgmoved;
  727. pgmoved = 0;
  728. spin_unlock_irq(&zone->lru_lock);
  729. __pagevec_release(&pvec);
  730. spin_lock_irq(&zone->lru_lock);
  731. }
  732. }
  733. zone->nr_active += pgmoved;
  734. spin_unlock(&zone->lru_lock);
  735. __mod_page_state_zone(zone, pgrefill, pgscanned);
  736. __mod_page_state(pgdeactivate, pgdeactivate);
  737. local_irq_enable();
  738. pagevec_release(&pvec);
  739. }
  740. /*
  741. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  742. */
  743. static unsigned long shrink_zone(int priority, struct zone *zone,
  744. struct scan_control *sc)
  745. {
  746. unsigned long nr_active;
  747. unsigned long nr_inactive;
  748. unsigned long nr_to_scan;
  749. unsigned long nr_reclaimed = 0;
  750. atomic_inc(&zone->reclaim_in_progress);
  751. /*
  752. * Add one to `nr_to_scan' just to make sure that the kernel will
  753. * slowly sift through the active list.
  754. */
  755. zone->nr_scan_active += (zone->nr_active >> priority) + 1;
  756. nr_active = zone->nr_scan_active;
  757. if (nr_active >= sc->swap_cluster_max)
  758. zone->nr_scan_active = 0;
  759. else
  760. nr_active = 0;
  761. zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1;
  762. nr_inactive = zone->nr_scan_inactive;
  763. if (nr_inactive >= sc->swap_cluster_max)
  764. zone->nr_scan_inactive = 0;
  765. else
  766. nr_inactive = 0;
  767. while (nr_active || nr_inactive) {
  768. if (nr_active) {
  769. nr_to_scan = min(nr_active,
  770. (unsigned long)sc->swap_cluster_max);
  771. nr_active -= nr_to_scan;
  772. shrink_active_list(nr_to_scan, zone, sc);
  773. }
  774. if (nr_inactive) {
  775. nr_to_scan = min(nr_inactive,
  776. (unsigned long)sc->swap_cluster_max);
  777. nr_inactive -= nr_to_scan;
  778. nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
  779. sc);
  780. }
  781. }
  782. throttle_vm_writeout();
  783. atomic_dec(&zone->reclaim_in_progress);
  784. return nr_reclaimed;
  785. }
  786. /*
  787. * This is the direct reclaim path, for page-allocating processes. We only
  788. * try to reclaim pages from zones which will satisfy the caller's allocation
  789. * request.
  790. *
  791. * We reclaim from a zone even if that zone is over pages_high. Because:
  792. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  793. * allocation or
  794. * b) The zones may be over pages_high but they must go *over* pages_high to
  795. * satisfy the `incremental min' zone defense algorithm.
  796. *
  797. * Returns the number of reclaimed pages.
  798. *
  799. * If a zone is deemed to be full of pinned pages then just give it a light
  800. * scan then give up on it.
  801. */
  802. static unsigned long shrink_zones(int priority, struct zone **zones,
  803. struct scan_control *sc)
  804. {
  805. unsigned long nr_reclaimed = 0;
  806. int i;
  807. for (i = 0; zones[i] != NULL; i++) {
  808. struct zone *zone = zones[i];
  809. if (!populated_zone(zone))
  810. continue;
  811. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  812. continue;
  813. zone->temp_priority = priority;
  814. if (zone->prev_priority > priority)
  815. zone->prev_priority = priority;
  816. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  817. continue; /* Let kswapd poll it */
  818. nr_reclaimed += shrink_zone(priority, zone, sc);
  819. }
  820. return nr_reclaimed;
  821. }
  822. /*
  823. * This is the main entry point to direct page reclaim.
  824. *
  825. * If a full scan of the inactive list fails to free enough memory then we
  826. * are "out of memory" and something needs to be killed.
  827. *
  828. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  829. * high - the zone may be full of dirty or under-writeback pages, which this
  830. * caller can't do much about. We kick pdflush and take explicit naps in the
  831. * hope that some of these pages can be written. But if the allocating task
  832. * holds filesystem locks which prevent writeout this might not work, and the
  833. * allocation attempt will fail.
  834. */
  835. unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
  836. {
  837. int priority;
  838. int ret = 0;
  839. unsigned long total_scanned = 0;
  840. unsigned long nr_reclaimed = 0;
  841. struct reclaim_state *reclaim_state = current->reclaim_state;
  842. unsigned long lru_pages = 0;
  843. int i;
  844. struct scan_control sc = {
  845. .gfp_mask = gfp_mask,
  846. .may_writepage = !laptop_mode,
  847. .swap_cluster_max = SWAP_CLUSTER_MAX,
  848. .may_swap = 1,
  849. .swappiness = vm_swappiness,
  850. };
  851. inc_page_state(allocstall);
  852. for (i = 0; zones[i] != NULL; i++) {
  853. struct zone *zone = zones[i];
  854. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  855. continue;
  856. zone->temp_priority = DEF_PRIORITY;
  857. lru_pages += zone->nr_active + zone->nr_inactive;
  858. }
  859. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  860. sc.nr_mapped = read_page_state(nr_mapped);
  861. sc.nr_scanned = 0;
  862. if (!priority)
  863. disable_swap_token();
  864. nr_reclaimed += shrink_zones(priority, zones, &sc);
  865. shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
  866. if (reclaim_state) {
  867. nr_reclaimed += reclaim_state->reclaimed_slab;
  868. reclaim_state->reclaimed_slab = 0;
  869. }
  870. total_scanned += sc.nr_scanned;
  871. if (nr_reclaimed >= sc.swap_cluster_max) {
  872. ret = 1;
  873. goto out;
  874. }
  875. /*
  876. * Try to write back as many pages as we just scanned. This
  877. * tends to cause slow streaming writers to write data to the
  878. * disk smoothly, at the dirtying rate, which is nice. But
  879. * that's undesirable in laptop mode, where we *want* lumpy
  880. * writeout. So in laptop mode, write out the whole world.
  881. */
  882. if (total_scanned > sc.swap_cluster_max +
  883. sc.swap_cluster_max / 2) {
  884. wakeup_pdflush(laptop_mode ? 0 : total_scanned);
  885. sc.may_writepage = 1;
  886. }
  887. /* Take a nap, wait for some writeback to complete */
  888. if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
  889. blk_congestion_wait(WRITE, HZ/10);
  890. }
  891. out:
  892. for (i = 0; zones[i] != 0; i++) {
  893. struct zone *zone = zones[i];
  894. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  895. continue;
  896. zone->prev_priority = zone->temp_priority;
  897. }
  898. return ret;
  899. }
  900. /*
  901. * For kswapd, balance_pgdat() will work across all this node's zones until
  902. * they are all at pages_high.
  903. *
  904. * Returns the number of pages which were actually freed.
  905. *
  906. * There is special handling here for zones which are full of pinned pages.
  907. * This can happen if the pages are all mlocked, or if they are all used by
  908. * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
  909. * What we do is to detect the case where all pages in the zone have been
  910. * scanned twice and there has been zero successful reclaim. Mark the zone as
  911. * dead and from now on, only perform a short scan. Basically we're polling
  912. * the zone for when the problem goes away.
  913. *
  914. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  915. * zones which have free_pages > pages_high, but once a zone is found to have
  916. * free_pages <= pages_high, we scan that zone and the lower zones regardless
  917. * of the number of free pages in the lower zones. This interoperates with
  918. * the page allocator fallback scheme to ensure that aging of pages is balanced
  919. * across the zones.
  920. */
  921. static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
  922. {
  923. int all_zones_ok;
  924. int priority;
  925. int i;
  926. unsigned long total_scanned;
  927. unsigned long nr_reclaimed;
  928. struct reclaim_state *reclaim_state = current->reclaim_state;
  929. struct scan_control sc = {
  930. .gfp_mask = GFP_KERNEL,
  931. .may_swap = 1,
  932. .swap_cluster_max = SWAP_CLUSTER_MAX,
  933. .swappiness = vm_swappiness,
  934. };
  935. loop_again:
  936. total_scanned = 0;
  937. nr_reclaimed = 0;
  938. sc.may_writepage = !laptop_mode;
  939. sc.nr_mapped = read_page_state(nr_mapped);
  940. inc_page_state(pageoutrun);
  941. for (i = 0; i < pgdat->nr_zones; i++) {
  942. struct zone *zone = pgdat->node_zones + i;
  943. zone->temp_priority = DEF_PRIORITY;
  944. }
  945. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  946. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  947. unsigned long lru_pages = 0;
  948. /* The swap token gets in the way of swapout... */
  949. if (!priority)
  950. disable_swap_token();
  951. all_zones_ok = 1;
  952. /*
  953. * Scan in the highmem->dma direction for the highest
  954. * zone which needs scanning
  955. */
  956. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  957. struct zone *zone = pgdat->node_zones + i;
  958. if (!populated_zone(zone))
  959. continue;
  960. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  961. continue;
  962. if (!zone_watermark_ok(zone, order, zone->pages_high,
  963. 0, 0)) {
  964. end_zone = i;
  965. goto scan;
  966. }
  967. }
  968. goto out;
  969. scan:
  970. for (i = 0; i <= end_zone; i++) {
  971. struct zone *zone = pgdat->node_zones + i;
  972. lru_pages += zone->nr_active + zone->nr_inactive;
  973. }
  974. /*
  975. * Now scan the zone in the dma->highmem direction, stopping
  976. * at the last zone which needs scanning.
  977. *
  978. * We do this because the page allocator works in the opposite
  979. * direction. This prevents the page allocator from allocating
  980. * pages behind kswapd's direction of progress, which would
  981. * cause too much scanning of the lower zones.
  982. */
  983. for (i = 0; i <= end_zone; i++) {
  984. struct zone *zone = pgdat->node_zones + i;
  985. int nr_slab;
  986. if (!populated_zone(zone))
  987. continue;
  988. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  989. continue;
  990. if (!zone_watermark_ok(zone, order, zone->pages_high,
  991. end_zone, 0))
  992. all_zones_ok = 0;
  993. zone->temp_priority = priority;
  994. if (zone->prev_priority > priority)
  995. zone->prev_priority = priority;
  996. sc.nr_scanned = 0;
  997. nr_reclaimed += shrink_zone(priority, zone, &sc);
  998. reclaim_state->reclaimed_slab = 0;
  999. nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
  1000. lru_pages);
  1001. nr_reclaimed += reclaim_state->reclaimed_slab;
  1002. total_scanned += sc.nr_scanned;
  1003. if (zone->all_unreclaimable)
  1004. continue;
  1005. if (nr_slab == 0 && zone->pages_scanned >=
  1006. (zone->nr_active + zone->nr_inactive) * 4)
  1007. zone->all_unreclaimable = 1;
  1008. /*
  1009. * If we've done a decent amount of scanning and
  1010. * the reclaim ratio is low, start doing writepage
  1011. * even in laptop mode
  1012. */
  1013. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  1014. total_scanned > nr_reclaimed + nr_reclaimed / 2)
  1015. sc.may_writepage = 1;
  1016. }
  1017. if (all_zones_ok)
  1018. break; /* kswapd: all done */
  1019. /*
  1020. * OK, kswapd is getting into trouble. Take a nap, then take
  1021. * another pass across the zones.
  1022. */
  1023. if (total_scanned && priority < DEF_PRIORITY - 2)
  1024. blk_congestion_wait(WRITE, HZ/10);
  1025. /*
  1026. * We do this so kswapd doesn't build up large priorities for
  1027. * example when it is freeing in parallel with allocators. It
  1028. * matches the direct reclaim path behaviour in terms of impact
  1029. * on zone->*_priority.
  1030. */
  1031. if (nr_reclaimed >= SWAP_CLUSTER_MAX)
  1032. break;
  1033. }
  1034. out:
  1035. for (i = 0; i < pgdat->nr_zones; i++) {
  1036. struct zone *zone = pgdat->node_zones + i;
  1037. zone->prev_priority = zone->temp_priority;
  1038. }
  1039. if (!all_zones_ok) {
  1040. cond_resched();
  1041. goto loop_again;
  1042. }
  1043. return nr_reclaimed;
  1044. }
  1045. /*
  1046. * The background pageout daemon, started as a kernel thread
  1047. * from the init process.
  1048. *
  1049. * This basically trickles out pages so that we have _some_
  1050. * free memory available even if there is no other activity
  1051. * that frees anything up. This is needed for things like routing
  1052. * etc, where we otherwise might have all activity going on in
  1053. * asynchronous contexts that cannot page things out.
  1054. *
  1055. * If there are applications that are active memory-allocators
  1056. * (most normal use), this basically shouldn't matter.
  1057. */
  1058. static int kswapd(void *p)
  1059. {
  1060. unsigned long order;
  1061. pg_data_t *pgdat = (pg_data_t*)p;
  1062. struct task_struct *tsk = current;
  1063. DEFINE_WAIT(wait);
  1064. struct reclaim_state reclaim_state = {
  1065. .reclaimed_slab = 0,
  1066. };
  1067. cpumask_t cpumask;
  1068. daemonize("kswapd%d", pgdat->node_id);
  1069. cpumask = node_to_cpumask(pgdat->node_id);
  1070. if (!cpus_empty(cpumask))
  1071. set_cpus_allowed(tsk, cpumask);
  1072. current->reclaim_state = &reclaim_state;
  1073. /*
  1074. * Tell the memory management that we're a "memory allocator",
  1075. * and that if we need more memory we should get access to it
  1076. * regardless (see "__alloc_pages()"). "kswapd" should
  1077. * never get caught in the normal page freeing logic.
  1078. *
  1079. * (Kswapd normally doesn't need memory anyway, but sometimes
  1080. * you need a small amount of memory in order to be able to
  1081. * page out something else, and this flag essentially protects
  1082. * us from recursively trying to free more memory as we're
  1083. * trying to free the first piece of memory in the first place).
  1084. */
  1085. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  1086. order = 0;
  1087. for ( ; ; ) {
  1088. unsigned long new_order;
  1089. try_to_freeze();
  1090. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  1091. new_order = pgdat->kswapd_max_order;
  1092. pgdat->kswapd_max_order = 0;
  1093. if (order < new_order) {
  1094. /*
  1095. * Don't sleep if someone wants a larger 'order'
  1096. * allocation
  1097. */
  1098. order = new_order;
  1099. } else {
  1100. schedule();
  1101. order = pgdat->kswapd_max_order;
  1102. }
  1103. finish_wait(&pgdat->kswapd_wait, &wait);
  1104. balance_pgdat(pgdat, order);
  1105. }
  1106. return 0;
  1107. }
  1108. /*
  1109. * A zone is low on free memory, so wake its kswapd task to service it.
  1110. */
  1111. void wakeup_kswapd(struct zone *zone, int order)
  1112. {
  1113. pg_data_t *pgdat;
  1114. if (!populated_zone(zone))
  1115. return;
  1116. pgdat = zone->zone_pgdat;
  1117. if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
  1118. return;
  1119. if (pgdat->kswapd_max_order < order)
  1120. pgdat->kswapd_max_order = order;
  1121. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  1122. return;
  1123. if (!waitqueue_active(&pgdat->kswapd_wait))
  1124. return;
  1125. wake_up_interruptible(&pgdat->kswapd_wait);
  1126. }
  1127. #ifdef CONFIG_PM
  1128. /*
  1129. * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
  1130. * from LRU lists system-wide, for given pass and priority, and returns the
  1131. * number of reclaimed pages
  1132. *
  1133. * For pass > 3 we also try to shrink the LRU lists that contain a few pages
  1134. */
  1135. static unsigned long shrink_all_zones(unsigned long nr_pages, int pass,
  1136. int prio, struct scan_control *sc)
  1137. {
  1138. struct zone *zone;
  1139. unsigned long nr_to_scan, ret = 0;
  1140. for_each_zone(zone) {
  1141. if (!populated_zone(zone))
  1142. continue;
  1143. if (zone->all_unreclaimable && prio != DEF_PRIORITY)
  1144. continue;
  1145. /* For pass = 0 we don't shrink the active list */
  1146. if (pass > 0) {
  1147. zone->nr_scan_active += (zone->nr_active >> prio) + 1;
  1148. if (zone->nr_scan_active >= nr_pages || pass > 3) {
  1149. zone->nr_scan_active = 0;
  1150. nr_to_scan = min(nr_pages, zone->nr_active);
  1151. shrink_active_list(nr_to_scan, zone, sc);
  1152. }
  1153. }
  1154. zone->nr_scan_inactive += (zone->nr_inactive >> prio) + 1;
  1155. if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
  1156. zone->nr_scan_inactive = 0;
  1157. nr_to_scan = min(nr_pages, zone->nr_inactive);
  1158. ret += shrink_inactive_list(nr_to_scan, zone, sc);
  1159. if (ret >= nr_pages)
  1160. return ret;
  1161. }
  1162. }
  1163. return ret;
  1164. }
  1165. /*
  1166. * Try to free `nr_pages' of memory, system-wide, and return the number of
  1167. * freed pages.
  1168. *
  1169. * Rather than trying to age LRUs the aim is to preserve the overall
  1170. * LRU order by reclaiming preferentially
  1171. * inactive > active > active referenced > active mapped
  1172. */
  1173. unsigned long shrink_all_memory(unsigned long nr_pages)
  1174. {
  1175. unsigned long lru_pages, nr_slab;
  1176. unsigned long ret = 0;
  1177. int pass;
  1178. struct reclaim_state reclaim_state;
  1179. struct zone *zone;
  1180. struct scan_control sc = {
  1181. .gfp_mask = GFP_KERNEL,
  1182. .may_swap = 0,
  1183. .swap_cluster_max = nr_pages,
  1184. .may_writepage = 1,
  1185. .swappiness = vm_swappiness,
  1186. };
  1187. current->reclaim_state = &reclaim_state;
  1188. lru_pages = 0;
  1189. for_each_zone(zone)
  1190. lru_pages += zone->nr_active + zone->nr_inactive;
  1191. nr_slab = read_page_state(nr_slab);
  1192. /* If slab caches are huge, it's better to hit them first */
  1193. while (nr_slab >= lru_pages) {
  1194. reclaim_state.reclaimed_slab = 0;
  1195. shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
  1196. if (!reclaim_state.reclaimed_slab)
  1197. break;
  1198. ret += reclaim_state.reclaimed_slab;
  1199. if (ret >= nr_pages)
  1200. goto out;
  1201. nr_slab -= reclaim_state.reclaimed_slab;
  1202. }
  1203. /*
  1204. * We try to shrink LRUs in 5 passes:
  1205. * 0 = Reclaim from inactive_list only
  1206. * 1 = Reclaim from active list but don't reclaim mapped
  1207. * 2 = 2nd pass of type 1
  1208. * 3 = Reclaim mapped (normal reclaim)
  1209. * 4 = 2nd pass of type 3
  1210. */
  1211. for (pass = 0; pass < 5; pass++) {
  1212. int prio;
  1213. /* Needed for shrinking slab caches later on */
  1214. if (!lru_pages)
  1215. for_each_zone(zone) {
  1216. lru_pages += zone->nr_active;
  1217. lru_pages += zone->nr_inactive;
  1218. }
  1219. /* Force reclaiming mapped pages in the passes #3 and #4 */
  1220. if (pass > 2) {
  1221. sc.may_swap = 1;
  1222. sc.swappiness = 100;
  1223. }
  1224. for (prio = DEF_PRIORITY; prio >= 0; prio--) {
  1225. unsigned long nr_to_scan = nr_pages - ret;
  1226. sc.nr_mapped = read_page_state(nr_mapped);
  1227. sc.nr_scanned = 0;
  1228. ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
  1229. if (ret >= nr_pages)
  1230. goto out;
  1231. reclaim_state.reclaimed_slab = 0;
  1232. shrink_slab(sc.nr_scanned, sc.gfp_mask, lru_pages);
  1233. ret += reclaim_state.reclaimed_slab;
  1234. if (ret >= nr_pages)
  1235. goto out;
  1236. if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
  1237. blk_congestion_wait(WRITE, HZ / 10);
  1238. }
  1239. lru_pages = 0;
  1240. }
  1241. /*
  1242. * If ret = 0, we could not shrink LRUs, but there may be something
  1243. * in slab caches
  1244. */
  1245. if (!ret)
  1246. do {
  1247. reclaim_state.reclaimed_slab = 0;
  1248. shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
  1249. ret += reclaim_state.reclaimed_slab;
  1250. } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
  1251. out:
  1252. current->reclaim_state = NULL;
  1253. return ret;
  1254. }
  1255. #endif
  1256. #ifdef CONFIG_HOTPLUG_CPU
  1257. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  1258. not required for correctness. So if the last cpu in a node goes
  1259. away, we get changed to run anywhere: as the first one comes back,
  1260. restore their cpu bindings. */
  1261. static int cpu_callback(struct notifier_block *nfb,
  1262. unsigned long action, void *hcpu)
  1263. {
  1264. pg_data_t *pgdat;
  1265. cpumask_t mask;
  1266. if (action == CPU_ONLINE) {
  1267. for_each_online_pgdat(pgdat) {
  1268. mask = node_to_cpumask(pgdat->node_id);
  1269. if (any_online_cpu(mask) != NR_CPUS)
  1270. /* One of our CPUs online: restore mask */
  1271. set_cpus_allowed(pgdat->kswapd, mask);
  1272. }
  1273. }
  1274. return NOTIFY_OK;
  1275. }
  1276. #endif /* CONFIG_HOTPLUG_CPU */
  1277. static int __init kswapd_init(void)
  1278. {
  1279. pg_data_t *pgdat;
  1280. swap_setup();
  1281. for_each_online_pgdat(pgdat) {
  1282. pid_t pid;
  1283. pid = kernel_thread(kswapd, pgdat, CLONE_KERNEL);
  1284. BUG_ON(pid < 0);
  1285. read_lock(&tasklist_lock);
  1286. pgdat->kswapd = find_task_by_pid(pid);
  1287. read_unlock(&tasklist_lock);
  1288. }
  1289. total_memory = nr_free_pagecache_pages();
  1290. hotcpu_notifier(cpu_callback, 0);
  1291. return 0;
  1292. }
  1293. module_init(kswapd_init)
  1294. #ifdef CONFIG_NUMA
  1295. /*
  1296. * Zone reclaim mode
  1297. *
  1298. * If non-zero call zone_reclaim when the number of free pages falls below
  1299. * the watermarks.
  1300. *
  1301. * In the future we may add flags to the mode. However, the page allocator
  1302. * should only have to check that zone_reclaim_mode != 0 before calling
  1303. * zone_reclaim().
  1304. */
  1305. int zone_reclaim_mode __read_mostly;
  1306. #define RECLAIM_OFF 0
  1307. #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
  1308. #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
  1309. #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
  1310. #define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */
  1311. /*
  1312. * Mininum time between zone reclaim scans
  1313. */
  1314. int zone_reclaim_interval __read_mostly = 30*HZ;
  1315. /*
  1316. * Priority for ZONE_RECLAIM. This determines the fraction of pages
  1317. * of a node considered for each zone_reclaim. 4 scans 1/16th of
  1318. * a zone.
  1319. */
  1320. #define ZONE_RECLAIM_PRIORITY 4
  1321. /*
  1322. * Try to free up some pages from this zone through reclaim.
  1323. */
  1324. static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  1325. {
  1326. /* Minimum pages needed in order to stay on node */
  1327. const unsigned long nr_pages = 1 << order;
  1328. struct task_struct *p = current;
  1329. struct reclaim_state reclaim_state;
  1330. int priority;
  1331. unsigned long nr_reclaimed = 0;
  1332. struct scan_control sc = {
  1333. .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
  1334. .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
  1335. .nr_mapped = read_page_state(nr_mapped),
  1336. .swap_cluster_max = max_t(unsigned long, nr_pages,
  1337. SWAP_CLUSTER_MAX),
  1338. .gfp_mask = gfp_mask,
  1339. .swappiness = vm_swappiness,
  1340. };
  1341. disable_swap_token();
  1342. cond_resched();
  1343. /*
  1344. * We need to be able to allocate from the reserves for RECLAIM_SWAP
  1345. * and we also need to be able to write out pages for RECLAIM_WRITE
  1346. * and RECLAIM_SWAP.
  1347. */
  1348. p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
  1349. reclaim_state.reclaimed_slab = 0;
  1350. p->reclaim_state = &reclaim_state;
  1351. /*
  1352. * Free memory by calling shrink zone with increasing priorities
  1353. * until we have enough memory freed.
  1354. */
  1355. priority = ZONE_RECLAIM_PRIORITY;
  1356. do {
  1357. nr_reclaimed += shrink_zone(priority, zone, &sc);
  1358. priority--;
  1359. } while (priority >= 0 && nr_reclaimed < nr_pages);
  1360. if (nr_reclaimed < nr_pages && (zone_reclaim_mode & RECLAIM_SLAB)) {
  1361. /*
  1362. * shrink_slab() does not currently allow us to determine how
  1363. * many pages were freed in this zone. So we just shake the slab
  1364. * a bit and then go off node for this particular allocation
  1365. * despite possibly having freed enough memory to allocate in
  1366. * this zone. If we freed local memory then the next
  1367. * allocations will be local again.
  1368. *
  1369. * shrink_slab will free memory on all zones and may take
  1370. * a long time.
  1371. */
  1372. shrink_slab(sc.nr_scanned, gfp_mask, order);
  1373. }
  1374. p->reclaim_state = NULL;
  1375. current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
  1376. if (nr_reclaimed == 0) {
  1377. /*
  1378. * We were unable to reclaim enough pages to stay on node. We
  1379. * now allow off node accesses for a certain time period before
  1380. * trying again to reclaim pages from the local zone.
  1381. */
  1382. zone->last_unsuccessful_zone_reclaim = jiffies;
  1383. }
  1384. return nr_reclaimed >= nr_pages;
  1385. }
  1386. int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  1387. {
  1388. cpumask_t mask;
  1389. int node_id;
  1390. /*
  1391. * Do not reclaim if there was a recent unsuccessful attempt at zone
  1392. * reclaim. In that case we let allocations go off node for the
  1393. * zone_reclaim_interval. Otherwise we would scan for each off-node
  1394. * page allocation.
  1395. */
  1396. if (time_before(jiffies,
  1397. zone->last_unsuccessful_zone_reclaim + zone_reclaim_interval))
  1398. return 0;
  1399. /*
  1400. * Avoid concurrent zone reclaims, do not reclaim in a zone that does
  1401. * not have reclaimable pages and if we should not delay the allocation
  1402. * then do not scan.
  1403. */
  1404. if (!(gfp_mask & __GFP_WAIT) ||
  1405. zone->all_unreclaimable ||
  1406. atomic_read(&zone->reclaim_in_progress) > 0 ||
  1407. (current->flags & PF_MEMALLOC))
  1408. return 0;
  1409. /*
  1410. * Only run zone reclaim on the local zone or on zones that do not
  1411. * have associated processors. This will favor the local processor
  1412. * over remote processors and spread off node memory allocations
  1413. * as wide as possible.
  1414. */
  1415. node_id = zone->zone_pgdat->node_id;
  1416. mask = node_to_cpumask(node_id);
  1417. if (!cpus_empty(mask) && node_id != numa_node_id())
  1418. return 0;
  1419. return __zone_reclaim(zone, gfp_mask, order);
  1420. }
  1421. #endif