vmscan.c 41 KB

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