vmscan.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  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 <linux/kthread.h>
  37. #include <asm/tlbflush.h>
  38. #include <asm/div64.h>
  39. #include <linux/swapops.h>
  40. #include "internal.h"
  41. struct scan_control {
  42. /* Incremented by the number of inactive pages that were scanned */
  43. unsigned long nr_scanned;
  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. long vm_total_pages; /* The total number of pages which the VM controls */
  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. count_vm_events(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. /* possible outcome of pageout() */
  254. typedef enum {
  255. /* failed to write page out, page is locked */
  256. PAGE_KEEP,
  257. /* move page to the active list, page is locked */
  258. PAGE_ACTIVATE,
  259. /* page has been sent to the disk successfully, page is unlocked */
  260. PAGE_SUCCESS,
  261. /* page is clean and locked */
  262. PAGE_CLEAN,
  263. } pageout_t;
  264. /*
  265. * pageout is called by shrink_page_list() for each dirty page.
  266. * Calls ->writepage().
  267. */
  268. static pageout_t pageout(struct page *page, struct address_space *mapping)
  269. {
  270. /*
  271. * If the page is dirty, only perform writeback if that write
  272. * will be non-blocking. To prevent this allocation from being
  273. * stalled by pagecache activity. But note that there may be
  274. * stalls if we need to run get_block(). We could test
  275. * PagePrivate for that.
  276. *
  277. * If this process is currently in generic_file_write() against
  278. * this page's queue, we can perform writeback even if that
  279. * will block.
  280. *
  281. * If the page is swapcache, write it back even if that would
  282. * block, for some throttling. This happens by accident, because
  283. * swap_backing_dev_info is bust: it doesn't reflect the
  284. * congestion state of the swapdevs. Easy to fix, if needed.
  285. * See swapfile.c:page_queue_congested().
  286. */
  287. if (!is_page_cache_freeable(page))
  288. return PAGE_KEEP;
  289. if (!mapping) {
  290. /*
  291. * Some data journaling orphaned pages can have
  292. * page->mapping == NULL while being dirty with clean buffers.
  293. */
  294. if (PagePrivate(page)) {
  295. if (try_to_free_buffers(page)) {
  296. ClearPageDirty(page);
  297. printk("%s: orphaned page\n", __FUNCTION__);
  298. return PAGE_CLEAN;
  299. }
  300. }
  301. return PAGE_KEEP;
  302. }
  303. if (mapping->a_ops->writepage == NULL)
  304. return PAGE_ACTIVATE;
  305. if (!may_write_to_queue(mapping->backing_dev_info))
  306. return PAGE_KEEP;
  307. if (clear_page_dirty_for_io(page)) {
  308. int res;
  309. struct writeback_control wbc = {
  310. .sync_mode = WB_SYNC_NONE,
  311. .nr_to_write = SWAP_CLUSTER_MAX,
  312. .range_start = 0,
  313. .range_end = LLONG_MAX,
  314. .nonblocking = 1,
  315. .for_reclaim = 1,
  316. };
  317. SetPageReclaim(page);
  318. res = mapping->a_ops->writepage(page, &wbc);
  319. if (res < 0)
  320. handle_write_error(mapping, page, res);
  321. if (res == AOP_WRITEPAGE_ACTIVATE) {
  322. ClearPageReclaim(page);
  323. return PAGE_ACTIVATE;
  324. }
  325. if (!PageWriteback(page)) {
  326. /* synchronous write or broken a_ops? */
  327. ClearPageReclaim(page);
  328. }
  329. return PAGE_SUCCESS;
  330. }
  331. return PAGE_CLEAN;
  332. }
  333. int remove_mapping(struct address_space *mapping, struct page *page)
  334. {
  335. if (!mapping)
  336. return 0; /* truncate got there first */
  337. write_lock_irq(&mapping->tree_lock);
  338. /*
  339. * The non-racy check for busy page. It is critical to check
  340. * PageDirty _after_ making sure that the page is freeable and
  341. * not in use by anybody. (pagecache + us == 2)
  342. */
  343. if (unlikely(page_count(page) != 2))
  344. goto cannot_free;
  345. smp_rmb();
  346. if (unlikely(PageDirty(page)))
  347. goto cannot_free;
  348. if (PageSwapCache(page)) {
  349. swp_entry_t swap = { .val = page_private(page) };
  350. __delete_from_swap_cache(page);
  351. write_unlock_irq(&mapping->tree_lock);
  352. swap_free(swap);
  353. __put_page(page); /* The pagecache ref */
  354. return 1;
  355. }
  356. __remove_from_page_cache(page);
  357. write_unlock_irq(&mapping->tree_lock);
  358. __put_page(page);
  359. return 1;
  360. cannot_free:
  361. write_unlock_irq(&mapping->tree_lock);
  362. return 0;
  363. }
  364. /*
  365. * shrink_page_list() returns the number of reclaimed pages
  366. */
  367. static unsigned long shrink_page_list(struct list_head *page_list,
  368. struct scan_control *sc)
  369. {
  370. LIST_HEAD(ret_pages);
  371. struct pagevec freed_pvec;
  372. int pgactivate = 0;
  373. unsigned long nr_reclaimed = 0;
  374. cond_resched();
  375. pagevec_init(&freed_pvec, 1);
  376. while (!list_empty(page_list)) {
  377. struct address_space *mapping;
  378. struct page *page;
  379. int may_enter_fs;
  380. int referenced;
  381. cond_resched();
  382. page = lru_to_page(page_list);
  383. list_del(&page->lru);
  384. if (TestSetPageLocked(page))
  385. goto keep;
  386. BUG_ON(PageActive(page));
  387. sc->nr_scanned++;
  388. if (!sc->may_swap && page_mapped(page))
  389. goto keep_locked;
  390. /* Double the slab pressure for mapped and swapcache pages */
  391. if (page_mapped(page) || PageSwapCache(page))
  392. sc->nr_scanned++;
  393. if (PageWriteback(page))
  394. goto keep_locked;
  395. referenced = page_referenced(page, 1);
  396. /* In active use or really unfreeable? Activate it. */
  397. if (referenced && page_mapping_inuse(page))
  398. goto activate_locked;
  399. #ifdef CONFIG_SWAP
  400. /*
  401. * Anonymous process memory has backing store?
  402. * Try to allocate it some swap space here.
  403. */
  404. if (PageAnon(page) && !PageSwapCache(page))
  405. if (!add_to_swap(page, GFP_ATOMIC))
  406. goto activate_locked;
  407. #endif /* CONFIG_SWAP */
  408. mapping = page_mapping(page);
  409. may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
  410. (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
  411. /*
  412. * The page is mapped into the page tables of one or more
  413. * processes. Try to unmap it here.
  414. */
  415. if (page_mapped(page) && mapping) {
  416. switch (try_to_unmap(page, 0)) {
  417. case SWAP_FAIL:
  418. goto activate_locked;
  419. case SWAP_AGAIN:
  420. goto keep_locked;
  421. case SWAP_SUCCESS:
  422. ; /* try to free the page below */
  423. }
  424. }
  425. if (PageDirty(page)) {
  426. if (referenced)
  427. goto keep_locked;
  428. if (!may_enter_fs)
  429. goto keep_locked;
  430. if (!sc->may_writepage)
  431. goto keep_locked;
  432. /* Page is dirty, try to write it out here */
  433. switch(pageout(page, mapping)) {
  434. case PAGE_KEEP:
  435. goto keep_locked;
  436. case PAGE_ACTIVATE:
  437. goto activate_locked;
  438. case PAGE_SUCCESS:
  439. if (PageWriteback(page) || PageDirty(page))
  440. goto keep;
  441. /*
  442. * A synchronous write - probably a ramdisk. Go
  443. * ahead and try to reclaim the page.
  444. */
  445. if (TestSetPageLocked(page))
  446. goto keep;
  447. if (PageDirty(page) || PageWriteback(page))
  448. goto keep_locked;
  449. mapping = page_mapping(page);
  450. case PAGE_CLEAN:
  451. ; /* try to free the page below */
  452. }
  453. }
  454. /*
  455. * If the page has buffers, try to free the buffer mappings
  456. * associated with this page. If we succeed we try to free
  457. * the page as well.
  458. *
  459. * We do this even if the page is PageDirty().
  460. * try_to_release_page() does not perform I/O, but it is
  461. * possible for a page to have PageDirty set, but it is actually
  462. * clean (all its buffers are clean). This happens if the
  463. * buffers were written out directly, with submit_bh(). ext3
  464. * will do this, as well as the blockdev mapping.
  465. * try_to_release_page() will discover that cleanness and will
  466. * drop the buffers and mark the page clean - it can be freed.
  467. *
  468. * Rarely, pages can have buffers and no ->mapping. These are
  469. * the pages which were not successfully invalidated in
  470. * truncate_complete_page(). We try to drop those buffers here
  471. * and if that worked, and the page is no longer mapped into
  472. * process address space (page_count == 1) it can be freed.
  473. * Otherwise, leave the page on the LRU so it is swappable.
  474. */
  475. if (PagePrivate(page)) {
  476. if (!try_to_release_page(page, sc->gfp_mask))
  477. goto activate_locked;
  478. if (!mapping && page_count(page) == 1)
  479. goto free_it;
  480. }
  481. if (!remove_mapping(mapping, page))
  482. goto keep_locked;
  483. free_it:
  484. unlock_page(page);
  485. nr_reclaimed++;
  486. if (!pagevec_add(&freed_pvec, page))
  487. __pagevec_release_nonlru(&freed_pvec);
  488. continue;
  489. activate_locked:
  490. SetPageActive(page);
  491. pgactivate++;
  492. keep_locked:
  493. unlock_page(page);
  494. keep:
  495. list_add(&page->lru, &ret_pages);
  496. BUG_ON(PageLRU(page));
  497. }
  498. list_splice(&ret_pages, page_list);
  499. if (pagevec_count(&freed_pvec))
  500. __pagevec_release_nonlru(&freed_pvec);
  501. count_vm_events(PGACTIVATE, pgactivate);
  502. return nr_reclaimed;
  503. }
  504. /*
  505. * zone->lru_lock is heavily contended. Some of the functions that
  506. * shrink the lists perform better by taking out a batch of pages
  507. * and working on them outside the LRU lock.
  508. *
  509. * For pagecache intensive workloads, this function is the hottest
  510. * spot in the kernel (apart from copy_*_user functions).
  511. *
  512. * Appropriate locks must be held before calling this function.
  513. *
  514. * @nr_to_scan: The number of pages to look through on the list.
  515. * @src: The LRU list to pull pages off.
  516. * @dst: The temp list to put pages on to.
  517. * @scanned: The number of pages that were scanned.
  518. *
  519. * returns how many pages were moved onto *@dst.
  520. */
  521. static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
  522. struct list_head *src, struct list_head *dst,
  523. unsigned long *scanned)
  524. {
  525. unsigned long nr_taken = 0;
  526. struct page *page;
  527. unsigned long scan;
  528. for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
  529. struct list_head *target;
  530. page = lru_to_page(src);
  531. prefetchw_prev_lru_page(page, src, flags);
  532. BUG_ON(!PageLRU(page));
  533. list_del(&page->lru);
  534. target = src;
  535. if (likely(get_page_unless_zero(page))) {
  536. /*
  537. * Be careful not to clear PageLRU until after we're
  538. * sure the page is not being freed elsewhere -- the
  539. * page release code relies on it.
  540. */
  541. ClearPageLRU(page);
  542. target = dst;
  543. nr_taken++;
  544. } /* else it is being freed elsewhere */
  545. list_add(&page->lru, target);
  546. }
  547. *scanned = scan;
  548. return nr_taken;
  549. }
  550. /*
  551. * shrink_inactive_list() is a helper for shrink_zone(). It returns the number
  552. * of reclaimed pages
  553. */
  554. static unsigned long shrink_inactive_list(unsigned long max_scan,
  555. struct zone *zone, struct scan_control *sc)
  556. {
  557. LIST_HEAD(page_list);
  558. struct pagevec pvec;
  559. unsigned long nr_scanned = 0;
  560. unsigned long nr_reclaimed = 0;
  561. pagevec_init(&pvec, 1);
  562. lru_add_drain();
  563. spin_lock_irq(&zone->lru_lock);
  564. do {
  565. struct page *page;
  566. unsigned long nr_taken;
  567. unsigned long nr_scan;
  568. unsigned long nr_freed;
  569. nr_taken = isolate_lru_pages(sc->swap_cluster_max,
  570. &zone->inactive_list,
  571. &page_list, &nr_scan);
  572. zone->nr_inactive -= nr_taken;
  573. zone->pages_scanned += nr_scan;
  574. spin_unlock_irq(&zone->lru_lock);
  575. nr_scanned += nr_scan;
  576. nr_freed = shrink_page_list(&page_list, sc);
  577. nr_reclaimed += nr_freed;
  578. local_irq_disable();
  579. if (current_is_kswapd()) {
  580. __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan);
  581. __count_vm_events(KSWAPD_STEAL, nr_freed);
  582. } else
  583. __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan);
  584. __count_vm_events(PGACTIVATE, nr_freed);
  585. if (nr_taken == 0)
  586. goto done;
  587. spin_lock(&zone->lru_lock);
  588. /*
  589. * Put back any unfreeable pages.
  590. */
  591. while (!list_empty(&page_list)) {
  592. page = lru_to_page(&page_list);
  593. BUG_ON(PageLRU(page));
  594. SetPageLRU(page);
  595. list_del(&page->lru);
  596. if (PageActive(page))
  597. add_page_to_active_list(zone, page);
  598. else
  599. add_page_to_inactive_list(zone, page);
  600. if (!pagevec_add(&pvec, page)) {
  601. spin_unlock_irq(&zone->lru_lock);
  602. __pagevec_release(&pvec);
  603. spin_lock_irq(&zone->lru_lock);
  604. }
  605. }
  606. } while (nr_scanned < max_scan);
  607. spin_unlock(&zone->lru_lock);
  608. done:
  609. local_irq_enable();
  610. pagevec_release(&pvec);
  611. return nr_reclaimed;
  612. }
  613. /*
  614. * This moves pages from the active list to the inactive list.
  615. *
  616. * We move them the other way if the page is referenced by one or more
  617. * processes, from rmap.
  618. *
  619. * If the pages are mostly unmapped, the processing is fast and it is
  620. * appropriate to hold zone->lru_lock across the whole operation. But if
  621. * the pages are mapped, the processing is slow (page_referenced()) so we
  622. * should drop zone->lru_lock around each page. It's impossible to balance
  623. * this, so instead we remove the pages from the LRU while processing them.
  624. * It is safe to rely on PG_active against the non-LRU pages in here because
  625. * nobody will play with that bit on a non-LRU page.
  626. *
  627. * The downside is that we have to touch page->_count against each page.
  628. * But we had to alter page->flags anyway.
  629. */
  630. static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
  631. struct scan_control *sc)
  632. {
  633. unsigned long pgmoved;
  634. int pgdeactivate = 0;
  635. unsigned long pgscanned;
  636. LIST_HEAD(l_hold); /* The pages which were snipped off */
  637. LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
  638. LIST_HEAD(l_active); /* Pages to go onto the active_list */
  639. struct page *page;
  640. struct pagevec pvec;
  641. int reclaim_mapped = 0;
  642. if (sc->may_swap) {
  643. long mapped_ratio;
  644. long distress;
  645. long swap_tendency;
  646. /*
  647. * `distress' is a measure of how much trouble we're having
  648. * reclaiming pages. 0 -> no problems. 100 -> great trouble.
  649. */
  650. distress = 100 >> zone->prev_priority;
  651. /*
  652. * The point of this algorithm is to decide when to start
  653. * reclaiming mapped memory instead of just pagecache. Work out
  654. * how much memory
  655. * is mapped.
  656. */
  657. mapped_ratio = ((global_page_state(NR_FILE_MAPPED) +
  658. global_page_state(NR_ANON_PAGES)) * 100) /
  659. vm_total_pages;
  660. /*
  661. * Now decide how much we really want to unmap some pages. The
  662. * mapped ratio is downgraded - just because there's a lot of
  663. * mapped memory doesn't necessarily mean that page reclaim
  664. * isn't succeeding.
  665. *
  666. * The distress ratio is important - we don't want to start
  667. * going oom.
  668. *
  669. * A 100% value of vm_swappiness overrides this algorithm
  670. * altogether.
  671. */
  672. swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
  673. /*
  674. * Now use this metric to decide whether to start moving mapped
  675. * memory onto the inactive list.
  676. */
  677. if (swap_tendency >= 100)
  678. reclaim_mapped = 1;
  679. }
  680. lru_add_drain();
  681. spin_lock_irq(&zone->lru_lock);
  682. pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
  683. &l_hold, &pgscanned);
  684. zone->pages_scanned += pgscanned;
  685. zone->nr_active -= pgmoved;
  686. spin_unlock_irq(&zone->lru_lock);
  687. while (!list_empty(&l_hold)) {
  688. cond_resched();
  689. page = lru_to_page(&l_hold);
  690. list_del(&page->lru);
  691. if (page_mapped(page)) {
  692. if (!reclaim_mapped ||
  693. (total_swap_pages == 0 && PageAnon(page)) ||
  694. page_referenced(page, 0)) {
  695. list_add(&page->lru, &l_active);
  696. continue;
  697. }
  698. }
  699. list_add(&page->lru, &l_inactive);
  700. }
  701. pagevec_init(&pvec, 1);
  702. pgmoved = 0;
  703. spin_lock_irq(&zone->lru_lock);
  704. while (!list_empty(&l_inactive)) {
  705. page = lru_to_page(&l_inactive);
  706. prefetchw_prev_lru_page(page, &l_inactive, flags);
  707. BUG_ON(PageLRU(page));
  708. SetPageLRU(page);
  709. BUG_ON(!PageActive(page));
  710. ClearPageActive(page);
  711. list_move(&page->lru, &zone->inactive_list);
  712. pgmoved++;
  713. if (!pagevec_add(&pvec, page)) {
  714. zone->nr_inactive += pgmoved;
  715. spin_unlock_irq(&zone->lru_lock);
  716. pgdeactivate += pgmoved;
  717. pgmoved = 0;
  718. if (buffer_heads_over_limit)
  719. pagevec_strip(&pvec);
  720. __pagevec_release(&pvec);
  721. spin_lock_irq(&zone->lru_lock);
  722. }
  723. }
  724. zone->nr_inactive += pgmoved;
  725. pgdeactivate += pgmoved;
  726. if (buffer_heads_over_limit) {
  727. spin_unlock_irq(&zone->lru_lock);
  728. pagevec_strip(&pvec);
  729. spin_lock_irq(&zone->lru_lock);
  730. }
  731. pgmoved = 0;
  732. while (!list_empty(&l_active)) {
  733. page = lru_to_page(&l_active);
  734. prefetchw_prev_lru_page(page, &l_active, flags);
  735. BUG_ON(PageLRU(page));
  736. SetPageLRU(page);
  737. BUG_ON(!PageActive(page));
  738. list_move(&page->lru, &zone->active_list);
  739. pgmoved++;
  740. if (!pagevec_add(&pvec, page)) {
  741. zone->nr_active += pgmoved;
  742. pgmoved = 0;
  743. spin_unlock_irq(&zone->lru_lock);
  744. __pagevec_release(&pvec);
  745. spin_lock_irq(&zone->lru_lock);
  746. }
  747. }
  748. zone->nr_active += pgmoved;
  749. __count_zone_vm_events(PGREFILL, zone, pgscanned);
  750. __count_vm_events(PGDEACTIVATE, pgdeactivate);
  751. spin_unlock_irq(&zone->lru_lock);
  752. pagevec_release(&pvec);
  753. }
  754. /*
  755. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  756. */
  757. static unsigned long shrink_zone(int priority, struct zone *zone,
  758. struct scan_control *sc)
  759. {
  760. unsigned long nr_active;
  761. unsigned long nr_inactive;
  762. unsigned long nr_to_scan;
  763. unsigned long nr_reclaimed = 0;
  764. atomic_inc(&zone->reclaim_in_progress);
  765. /*
  766. * Add one to `nr_to_scan' just to make sure that the kernel will
  767. * slowly sift through the active list.
  768. */
  769. zone->nr_scan_active += (zone->nr_active >> priority) + 1;
  770. nr_active = zone->nr_scan_active;
  771. if (nr_active >= sc->swap_cluster_max)
  772. zone->nr_scan_active = 0;
  773. else
  774. nr_active = 0;
  775. zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1;
  776. nr_inactive = zone->nr_scan_inactive;
  777. if (nr_inactive >= sc->swap_cluster_max)
  778. zone->nr_scan_inactive = 0;
  779. else
  780. nr_inactive = 0;
  781. while (nr_active || nr_inactive) {
  782. if (nr_active) {
  783. nr_to_scan = min(nr_active,
  784. (unsigned long)sc->swap_cluster_max);
  785. nr_active -= nr_to_scan;
  786. shrink_active_list(nr_to_scan, zone, sc);
  787. }
  788. if (nr_inactive) {
  789. nr_to_scan = min(nr_inactive,
  790. (unsigned long)sc->swap_cluster_max);
  791. nr_inactive -= nr_to_scan;
  792. nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
  793. sc);
  794. }
  795. }
  796. throttle_vm_writeout();
  797. atomic_dec(&zone->reclaim_in_progress);
  798. return nr_reclaimed;
  799. }
  800. /*
  801. * This is the direct reclaim path, for page-allocating processes. We only
  802. * try to reclaim pages from zones which will satisfy the caller's allocation
  803. * request.
  804. *
  805. * We reclaim from a zone even if that zone is over pages_high. Because:
  806. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  807. * allocation or
  808. * b) The zones may be over pages_high but they must go *over* pages_high to
  809. * satisfy the `incremental min' zone defense algorithm.
  810. *
  811. * Returns the number of reclaimed pages.
  812. *
  813. * If a zone is deemed to be full of pinned pages then just give it a light
  814. * scan then give up on it.
  815. */
  816. static unsigned long shrink_zones(int priority, struct zone **zones,
  817. struct scan_control *sc)
  818. {
  819. unsigned long nr_reclaimed = 0;
  820. int i;
  821. for (i = 0; zones[i] != NULL; i++) {
  822. struct zone *zone = zones[i];
  823. if (!populated_zone(zone))
  824. continue;
  825. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  826. continue;
  827. zone->temp_priority = priority;
  828. if (zone->prev_priority > priority)
  829. zone->prev_priority = priority;
  830. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  831. continue; /* Let kswapd poll it */
  832. nr_reclaimed += shrink_zone(priority, zone, sc);
  833. }
  834. return nr_reclaimed;
  835. }
  836. /*
  837. * This is the main entry point to direct page reclaim.
  838. *
  839. * If a full scan of the inactive list fails to free enough memory then we
  840. * are "out of memory" and something needs to be killed.
  841. *
  842. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  843. * high - the zone may be full of dirty or under-writeback pages, which this
  844. * caller can't do much about. We kick pdflush and take explicit naps in the
  845. * hope that some of these pages can be written. But if the allocating task
  846. * holds filesystem locks which prevent writeout this might not work, and the
  847. * allocation attempt will fail.
  848. */
  849. unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
  850. {
  851. int priority;
  852. int ret = 0;
  853. unsigned long total_scanned = 0;
  854. unsigned long nr_reclaimed = 0;
  855. struct reclaim_state *reclaim_state = current->reclaim_state;
  856. unsigned long lru_pages = 0;
  857. int i;
  858. struct scan_control sc = {
  859. .gfp_mask = gfp_mask,
  860. .may_writepage = !laptop_mode,
  861. .swap_cluster_max = SWAP_CLUSTER_MAX,
  862. .may_swap = 1,
  863. .swappiness = vm_swappiness,
  864. };
  865. count_vm_event(ALLOCSTALL);
  866. for (i = 0; zones[i] != NULL; i++) {
  867. struct zone *zone = zones[i];
  868. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  869. continue;
  870. zone->temp_priority = DEF_PRIORITY;
  871. lru_pages += zone->nr_active + zone->nr_inactive;
  872. }
  873. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  874. sc.nr_scanned = 0;
  875. if (!priority)
  876. disable_swap_token();
  877. nr_reclaimed += shrink_zones(priority, zones, &sc);
  878. shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
  879. if (reclaim_state) {
  880. nr_reclaimed += reclaim_state->reclaimed_slab;
  881. reclaim_state->reclaimed_slab = 0;
  882. }
  883. total_scanned += sc.nr_scanned;
  884. if (nr_reclaimed >= sc.swap_cluster_max) {
  885. ret = 1;
  886. goto out;
  887. }
  888. /*
  889. * Try to write back as many pages as we just scanned. This
  890. * tends to cause slow streaming writers to write data to the
  891. * disk smoothly, at the dirtying rate, which is nice. But
  892. * that's undesirable in laptop mode, where we *want* lumpy
  893. * writeout. So in laptop mode, write out the whole world.
  894. */
  895. if (total_scanned > sc.swap_cluster_max +
  896. sc.swap_cluster_max / 2) {
  897. wakeup_pdflush(laptop_mode ? 0 : total_scanned);
  898. sc.may_writepage = 1;
  899. }
  900. /* Take a nap, wait for some writeback to complete */
  901. if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
  902. blk_congestion_wait(WRITE, HZ/10);
  903. }
  904. out:
  905. for (i = 0; zones[i] != 0; i++) {
  906. struct zone *zone = zones[i];
  907. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  908. continue;
  909. zone->prev_priority = zone->temp_priority;
  910. }
  911. return ret;
  912. }
  913. /*
  914. * For kswapd, balance_pgdat() will work across all this node's zones until
  915. * they are all at pages_high.
  916. *
  917. * Returns the number of pages which were actually freed.
  918. *
  919. * There is special handling here for zones which are full of pinned pages.
  920. * This can happen if the pages are all mlocked, or if they are all used by
  921. * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
  922. * What we do is to detect the case where all pages in the zone have been
  923. * scanned twice and there has been zero successful reclaim. Mark the zone as
  924. * dead and from now on, only perform a short scan. Basically we're polling
  925. * the zone for when the problem goes away.
  926. *
  927. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  928. * zones which have free_pages > pages_high, but once a zone is found to have
  929. * free_pages <= pages_high, we scan that zone and the lower zones regardless
  930. * of the number of free pages in the lower zones. This interoperates with
  931. * the page allocator fallback scheme to ensure that aging of pages is balanced
  932. * across the zones.
  933. */
  934. static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
  935. {
  936. int all_zones_ok;
  937. int priority;
  938. int i;
  939. unsigned long total_scanned;
  940. unsigned long nr_reclaimed;
  941. struct reclaim_state *reclaim_state = current->reclaim_state;
  942. struct scan_control sc = {
  943. .gfp_mask = GFP_KERNEL,
  944. .may_swap = 1,
  945. .swap_cluster_max = SWAP_CLUSTER_MAX,
  946. .swappiness = vm_swappiness,
  947. };
  948. loop_again:
  949. total_scanned = 0;
  950. nr_reclaimed = 0;
  951. sc.may_writepage = !laptop_mode;
  952. count_vm_event(PAGEOUTRUN);
  953. for (i = 0; i < pgdat->nr_zones; i++) {
  954. struct zone *zone = pgdat->node_zones + i;
  955. zone->temp_priority = DEF_PRIORITY;
  956. }
  957. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  958. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  959. unsigned long lru_pages = 0;
  960. /* The swap token gets in the way of swapout... */
  961. if (!priority)
  962. disable_swap_token();
  963. all_zones_ok = 1;
  964. /*
  965. * Scan in the highmem->dma direction for the highest
  966. * zone which needs scanning
  967. */
  968. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  969. struct zone *zone = pgdat->node_zones + i;
  970. if (!populated_zone(zone))
  971. continue;
  972. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  973. continue;
  974. if (!zone_watermark_ok(zone, order, zone->pages_high,
  975. 0, 0)) {
  976. end_zone = i;
  977. goto scan;
  978. }
  979. }
  980. goto out;
  981. scan:
  982. for (i = 0; i <= end_zone; i++) {
  983. struct zone *zone = pgdat->node_zones + i;
  984. lru_pages += zone->nr_active + zone->nr_inactive;
  985. }
  986. /*
  987. * Now scan the zone in the dma->highmem direction, stopping
  988. * at the last zone which needs scanning.
  989. *
  990. * We do this because the page allocator works in the opposite
  991. * direction. This prevents the page allocator from allocating
  992. * pages behind kswapd's direction of progress, which would
  993. * cause too much scanning of the lower zones.
  994. */
  995. for (i = 0; i <= end_zone; i++) {
  996. struct zone *zone = pgdat->node_zones + i;
  997. int nr_slab;
  998. if (!populated_zone(zone))
  999. continue;
  1000. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1001. continue;
  1002. if (!zone_watermark_ok(zone, order, zone->pages_high,
  1003. end_zone, 0))
  1004. all_zones_ok = 0;
  1005. zone->temp_priority = priority;
  1006. if (zone->prev_priority > priority)
  1007. zone->prev_priority = priority;
  1008. sc.nr_scanned = 0;
  1009. nr_reclaimed += shrink_zone(priority, zone, &sc);
  1010. reclaim_state->reclaimed_slab = 0;
  1011. nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
  1012. lru_pages);
  1013. nr_reclaimed += reclaim_state->reclaimed_slab;
  1014. total_scanned += sc.nr_scanned;
  1015. if (zone->all_unreclaimable)
  1016. continue;
  1017. if (nr_slab == 0 && zone->pages_scanned >=
  1018. (zone->nr_active + zone->nr_inactive) * 4)
  1019. zone->all_unreclaimable = 1;
  1020. /*
  1021. * If we've done a decent amount of scanning and
  1022. * the reclaim ratio is low, start doing writepage
  1023. * even in laptop mode
  1024. */
  1025. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  1026. total_scanned > nr_reclaimed + nr_reclaimed / 2)
  1027. sc.may_writepage = 1;
  1028. }
  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)
  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. cpumask = node_to_cpumask(pgdat->node_id);
  1081. if (!cpus_empty(cpumask))
  1082. set_cpus_allowed(tsk, cpumask);
  1083. current->reclaim_state = &reclaim_state;
  1084. /*
  1085. * Tell the memory management that we're a "memory allocator",
  1086. * and that if we need more memory we should get access to it
  1087. * regardless (see "__alloc_pages()"). "kswapd" should
  1088. * never get caught in the normal page freeing logic.
  1089. *
  1090. * (Kswapd normally doesn't need memory anyway, but sometimes
  1091. * you need a small amount of memory in order to be able to
  1092. * page out something else, and this flag essentially protects
  1093. * us from recursively trying to free more memory as we're
  1094. * trying to free the first piece of memory in the first place).
  1095. */
  1096. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  1097. order = 0;
  1098. for ( ; ; ) {
  1099. unsigned long new_order;
  1100. try_to_freeze();
  1101. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  1102. new_order = pgdat->kswapd_max_order;
  1103. pgdat->kswapd_max_order = 0;
  1104. if (order < new_order) {
  1105. /*
  1106. * Don't sleep if someone wants a larger 'order'
  1107. * allocation
  1108. */
  1109. order = new_order;
  1110. } else {
  1111. schedule();
  1112. order = pgdat->kswapd_max_order;
  1113. }
  1114. finish_wait(&pgdat->kswapd_wait, &wait);
  1115. balance_pgdat(pgdat, order);
  1116. }
  1117. return 0;
  1118. }
  1119. /*
  1120. * A zone is low on free memory, so wake its kswapd task to service it.
  1121. */
  1122. void wakeup_kswapd(struct zone *zone, int order)
  1123. {
  1124. pg_data_t *pgdat;
  1125. if (!populated_zone(zone))
  1126. return;
  1127. pgdat = zone->zone_pgdat;
  1128. if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
  1129. return;
  1130. if (pgdat->kswapd_max_order < order)
  1131. pgdat->kswapd_max_order = order;
  1132. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  1133. return;
  1134. if (!waitqueue_active(&pgdat->kswapd_wait))
  1135. return;
  1136. wake_up_interruptible(&pgdat->kswapd_wait);
  1137. }
  1138. #ifdef CONFIG_PM
  1139. /*
  1140. * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
  1141. * from LRU lists system-wide, for given pass and priority, and returns the
  1142. * number of reclaimed pages
  1143. *
  1144. * For pass > 3 we also try to shrink the LRU lists that contain a few pages
  1145. */
  1146. static unsigned long shrink_all_zones(unsigned long nr_pages, int pass,
  1147. int prio, struct scan_control *sc)
  1148. {
  1149. struct zone *zone;
  1150. unsigned long nr_to_scan, ret = 0;
  1151. for_each_zone(zone) {
  1152. if (!populated_zone(zone))
  1153. continue;
  1154. if (zone->all_unreclaimable && prio != DEF_PRIORITY)
  1155. continue;
  1156. /* For pass = 0 we don't shrink the active list */
  1157. if (pass > 0) {
  1158. zone->nr_scan_active += (zone->nr_active >> prio) + 1;
  1159. if (zone->nr_scan_active >= nr_pages || pass > 3) {
  1160. zone->nr_scan_active = 0;
  1161. nr_to_scan = min(nr_pages, zone->nr_active);
  1162. shrink_active_list(nr_to_scan, zone, sc);
  1163. }
  1164. }
  1165. zone->nr_scan_inactive += (zone->nr_inactive >> prio) + 1;
  1166. if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
  1167. zone->nr_scan_inactive = 0;
  1168. nr_to_scan = min(nr_pages, zone->nr_inactive);
  1169. ret += shrink_inactive_list(nr_to_scan, zone, sc);
  1170. if (ret >= nr_pages)
  1171. return ret;
  1172. }
  1173. }
  1174. return ret;
  1175. }
  1176. /*
  1177. * Try to free `nr_pages' of memory, system-wide, and return the number of
  1178. * freed pages.
  1179. *
  1180. * Rather than trying to age LRUs the aim is to preserve the overall
  1181. * LRU order by reclaiming preferentially
  1182. * inactive > active > active referenced > active mapped
  1183. */
  1184. unsigned long shrink_all_memory(unsigned long nr_pages)
  1185. {
  1186. unsigned long lru_pages, nr_slab;
  1187. unsigned long ret = 0;
  1188. int pass;
  1189. struct reclaim_state reclaim_state;
  1190. struct zone *zone;
  1191. struct scan_control sc = {
  1192. .gfp_mask = GFP_KERNEL,
  1193. .may_swap = 0,
  1194. .swap_cluster_max = nr_pages,
  1195. .may_writepage = 1,
  1196. .swappiness = vm_swappiness,
  1197. };
  1198. current->reclaim_state = &reclaim_state;
  1199. lru_pages = 0;
  1200. for_each_zone(zone)
  1201. lru_pages += zone->nr_active + zone->nr_inactive;
  1202. nr_slab = global_page_state(NR_SLAB);
  1203. /* If slab caches are huge, it's better to hit them first */
  1204. while (nr_slab >= lru_pages) {
  1205. reclaim_state.reclaimed_slab = 0;
  1206. shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
  1207. if (!reclaim_state.reclaimed_slab)
  1208. break;
  1209. ret += reclaim_state.reclaimed_slab;
  1210. if (ret >= nr_pages)
  1211. goto out;
  1212. nr_slab -= reclaim_state.reclaimed_slab;
  1213. }
  1214. /*
  1215. * We try to shrink LRUs in 5 passes:
  1216. * 0 = Reclaim from inactive_list only
  1217. * 1 = Reclaim from active list but don't reclaim mapped
  1218. * 2 = 2nd pass of type 1
  1219. * 3 = Reclaim mapped (normal reclaim)
  1220. * 4 = 2nd pass of type 3
  1221. */
  1222. for (pass = 0; pass < 5; pass++) {
  1223. int prio;
  1224. /* Needed for shrinking slab caches later on */
  1225. if (!lru_pages)
  1226. for_each_zone(zone) {
  1227. lru_pages += zone->nr_active;
  1228. lru_pages += zone->nr_inactive;
  1229. }
  1230. /* Force reclaiming mapped pages in the passes #3 and #4 */
  1231. if (pass > 2) {
  1232. sc.may_swap = 1;
  1233. sc.swappiness = 100;
  1234. }
  1235. for (prio = DEF_PRIORITY; prio >= 0; prio--) {
  1236. unsigned long nr_to_scan = nr_pages - ret;
  1237. sc.nr_scanned = 0;
  1238. ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
  1239. if (ret >= nr_pages)
  1240. goto out;
  1241. reclaim_state.reclaimed_slab = 0;
  1242. shrink_slab(sc.nr_scanned, sc.gfp_mask, lru_pages);
  1243. ret += reclaim_state.reclaimed_slab;
  1244. if (ret >= nr_pages)
  1245. goto out;
  1246. if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
  1247. blk_congestion_wait(WRITE, HZ / 10);
  1248. }
  1249. lru_pages = 0;
  1250. }
  1251. /*
  1252. * If ret = 0, we could not shrink LRUs, but there may be something
  1253. * in slab caches
  1254. */
  1255. if (!ret)
  1256. do {
  1257. reclaim_state.reclaimed_slab = 0;
  1258. shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
  1259. ret += reclaim_state.reclaimed_slab;
  1260. } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
  1261. out:
  1262. current->reclaim_state = NULL;
  1263. return ret;
  1264. }
  1265. #endif
  1266. #ifdef CONFIG_HOTPLUG_CPU
  1267. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  1268. not required for correctness. So if the last cpu in a node goes
  1269. away, we get changed to run anywhere: as the first one comes back,
  1270. restore their cpu bindings. */
  1271. static int __devinit cpu_callback(struct notifier_block *nfb,
  1272. unsigned long action, void *hcpu)
  1273. {
  1274. pg_data_t *pgdat;
  1275. cpumask_t mask;
  1276. if (action == CPU_ONLINE) {
  1277. for_each_online_pgdat(pgdat) {
  1278. mask = node_to_cpumask(pgdat->node_id);
  1279. if (any_online_cpu(mask) != NR_CPUS)
  1280. /* One of our CPUs online: restore mask */
  1281. set_cpus_allowed(pgdat->kswapd, mask);
  1282. }
  1283. }
  1284. return NOTIFY_OK;
  1285. }
  1286. #endif /* CONFIG_HOTPLUG_CPU */
  1287. /*
  1288. * This kswapd start function will be called by init and node-hot-add.
  1289. * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
  1290. */
  1291. int kswapd_run(int nid)
  1292. {
  1293. pg_data_t *pgdat = NODE_DATA(nid);
  1294. int ret = 0;
  1295. if (pgdat->kswapd)
  1296. return 0;
  1297. pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
  1298. if (IS_ERR(pgdat->kswapd)) {
  1299. /* failure at boot is fatal */
  1300. BUG_ON(system_state == SYSTEM_BOOTING);
  1301. printk("Failed to start kswapd on node %d\n",nid);
  1302. ret = -1;
  1303. }
  1304. return ret;
  1305. }
  1306. static int __init kswapd_init(void)
  1307. {
  1308. int nid;
  1309. swap_setup();
  1310. for_each_online_node(nid)
  1311. kswapd_run(nid);
  1312. hotcpu_notifier(cpu_callback, 0);
  1313. return 0;
  1314. }
  1315. module_init(kswapd_init)
  1316. #ifdef CONFIG_NUMA
  1317. /*
  1318. * Zone reclaim mode
  1319. *
  1320. * If non-zero call zone_reclaim when the number of free pages falls below
  1321. * the watermarks.
  1322. *
  1323. * In the future we may add flags to the mode. However, the page allocator
  1324. * should only have to check that zone_reclaim_mode != 0 before calling
  1325. * zone_reclaim().
  1326. */
  1327. int zone_reclaim_mode __read_mostly;
  1328. #define RECLAIM_OFF 0
  1329. #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
  1330. #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
  1331. #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
  1332. #define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */
  1333. /*
  1334. * Priority for ZONE_RECLAIM. This determines the fraction of pages
  1335. * of a node considered for each zone_reclaim. 4 scans 1/16th of
  1336. * a zone.
  1337. */
  1338. #define ZONE_RECLAIM_PRIORITY 4
  1339. /*
  1340. * Try to free up some pages from this zone through reclaim.
  1341. */
  1342. static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  1343. {
  1344. /* Minimum pages needed in order to stay on node */
  1345. const unsigned long nr_pages = 1 << order;
  1346. struct task_struct *p = current;
  1347. struct reclaim_state reclaim_state;
  1348. int priority;
  1349. unsigned long nr_reclaimed = 0;
  1350. struct scan_control sc = {
  1351. .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
  1352. .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
  1353. .swap_cluster_max = max_t(unsigned long, nr_pages,
  1354. SWAP_CLUSTER_MAX),
  1355. .gfp_mask = gfp_mask,
  1356. .swappiness = vm_swappiness,
  1357. };
  1358. disable_swap_token();
  1359. cond_resched();
  1360. /*
  1361. * We need to be able to allocate from the reserves for RECLAIM_SWAP
  1362. * and we also need to be able to write out pages for RECLAIM_WRITE
  1363. * and RECLAIM_SWAP.
  1364. */
  1365. p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
  1366. reclaim_state.reclaimed_slab = 0;
  1367. p->reclaim_state = &reclaim_state;
  1368. /*
  1369. * Free memory by calling shrink zone with increasing priorities
  1370. * until we have enough memory freed.
  1371. */
  1372. priority = ZONE_RECLAIM_PRIORITY;
  1373. do {
  1374. nr_reclaimed += shrink_zone(priority, zone, &sc);
  1375. priority--;
  1376. } while (priority >= 0 && nr_reclaimed < nr_pages);
  1377. if (nr_reclaimed < nr_pages && (zone_reclaim_mode & RECLAIM_SLAB)) {
  1378. /*
  1379. * shrink_slab() does not currently allow us to determine how
  1380. * many pages were freed in this zone. So we just shake the slab
  1381. * a bit and then go off node for this particular allocation
  1382. * despite possibly having freed enough memory to allocate in
  1383. * this zone. If we freed local memory then the next
  1384. * allocations will be local again.
  1385. *
  1386. * shrink_slab will free memory on all zones and may take
  1387. * a long time.
  1388. */
  1389. shrink_slab(sc.nr_scanned, gfp_mask, order);
  1390. }
  1391. p->reclaim_state = NULL;
  1392. current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
  1393. return nr_reclaimed >= nr_pages;
  1394. }
  1395. int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  1396. {
  1397. cpumask_t mask;
  1398. int node_id;
  1399. /*
  1400. * Do not reclaim if there are not enough reclaimable pages in this
  1401. * zone that would satify this allocations.
  1402. *
  1403. * All unmapped pagecache pages are reclaimable.
  1404. *
  1405. * Both counters may be temporarily off a bit so we use
  1406. * SWAP_CLUSTER_MAX as the boundary. It may also be good to
  1407. * leave a few frequently used unmapped pagecache pages around.
  1408. */
  1409. if (zone_page_state(zone, NR_FILE_PAGES) -
  1410. zone_page_state(zone, NR_FILE_MAPPED) < SWAP_CLUSTER_MAX)
  1411. return 0;
  1412. /*
  1413. * Avoid concurrent zone reclaims, do not reclaim in a zone that does
  1414. * not have reclaimable pages and if we should not delay the allocation
  1415. * then do not scan.
  1416. */
  1417. if (!(gfp_mask & __GFP_WAIT) ||
  1418. zone->all_unreclaimable ||
  1419. atomic_read(&zone->reclaim_in_progress) > 0 ||
  1420. (current->flags & PF_MEMALLOC))
  1421. return 0;
  1422. /*
  1423. * Only run zone reclaim on the local zone or on zones that do not
  1424. * have associated processors. This will favor the local processor
  1425. * over remote processors and spread off node memory allocations
  1426. * as wide as possible.
  1427. */
  1428. node_id = zone->zone_pgdat->node_id;
  1429. mask = node_to_cpumask(node_id);
  1430. if (!cpus_empty(mask) && node_id != numa_node_id())
  1431. return 0;
  1432. return __zone_reclaim(zone, gfp_mask, order);
  1433. }
  1434. #endif