vmscan.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. /*
  2. * linux/mm/vmscan.c
  3. *
  4. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  5. *
  6. * Swap reorganised 29.12.95, Stephen Tweedie.
  7. * kswapd added: 7.1.96 sct
  8. * Removed kswapd_ctl limits, and swap out as many pages as needed
  9. * to bring the system back to freepages.high: 2.4.97, Rik van Riel.
  10. * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com).
  11. * Multiqueue VM started 5.8.00, Rik van Riel.
  12. */
  13. #include <linux/mm.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/kernel_stat.h>
  17. #include <linux/swap.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/init.h>
  20. #include <linux/highmem.h>
  21. #include <linux/file.h>
  22. #include <linux/writeback.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/buffer_head.h> /* for try_to_release_page(),
  25. buffer_heads_over_limit */
  26. #include <linux/mm_inline.h>
  27. #include <linux/pagevec.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/rmap.h>
  30. #include <linux/topology.h>
  31. #include <linux/cpu.h>
  32. #include <linux/cpuset.h>
  33. #include <linux/notifier.h>
  34. #include <linux/rwsem.h>
  35. #include <linux/delay.h>
  36. #include <asm/tlbflush.h>
  37. #include <asm/div64.h>
  38. #include <linux/swapops.h>
  39. #include "internal.h"
  40. struct scan_control {
  41. /* Incremented by the number of inactive pages that were scanned */
  42. unsigned long nr_scanned;
  43. unsigned long nr_mapped; /* From page_state */
  44. /* This context's GFP mask */
  45. gfp_t gfp_mask;
  46. int may_writepage;
  47. /* Can pages be swapped as part of reclaim? */
  48. int may_swap;
  49. /* This context's SWAP_CLUSTER_MAX. If freeing memory for
  50. * suspend, we effectively ignore SWAP_CLUSTER_MAX.
  51. * In this context, it doesn't matter that we scan the
  52. * whole list at once. */
  53. int swap_cluster_max;
  54. int swappiness;
  55. };
  56. /*
  57. * The list of shrinker callbacks used by to apply pressure to
  58. * ageable caches.
  59. */
  60. struct shrinker {
  61. shrinker_t shrinker;
  62. struct list_head list;
  63. int seeks; /* seeks to recreate an obj */
  64. long nr; /* objs pending delete */
  65. };
  66. #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
  67. #ifdef ARCH_HAS_PREFETCH
  68. #define prefetch_prev_lru_page(_page, _base, _field) \
  69. do { \
  70. if ((_page)->lru.prev != _base) { \
  71. struct page *prev; \
  72. \
  73. prev = lru_to_page(&(_page->lru)); \
  74. prefetch(&prev->_field); \
  75. } \
  76. } while (0)
  77. #else
  78. #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
  79. #endif
  80. #ifdef ARCH_HAS_PREFETCHW
  81. #define prefetchw_prev_lru_page(_page, _base, _field) \
  82. do { \
  83. if ((_page)->lru.prev != _base) { \
  84. struct page *prev; \
  85. \
  86. prev = lru_to_page(&(_page->lru)); \
  87. prefetchw(&prev->_field); \
  88. } \
  89. } while (0)
  90. #else
  91. #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
  92. #endif
  93. /*
  94. * From 0 .. 100. Higher means more swappy.
  95. */
  96. int vm_swappiness = 60;
  97. 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. mod_page_state(slabs_scanned, this_scan);
  191. total_scan -= this_scan;
  192. cond_resched();
  193. }
  194. shrinker->nr += total_scan;
  195. }
  196. up_read(&shrinker_rwsem);
  197. return ret;
  198. }
  199. /* Called without lock on whether page is mapped, so answer is unstable */
  200. static inline int page_mapping_inuse(struct page *page)
  201. {
  202. struct address_space *mapping;
  203. /* Page is in somebody's page tables. */
  204. if (page_mapped(page))
  205. return 1;
  206. /* Be more reluctant to reclaim swapcache than pagecache */
  207. if (PageSwapCache(page))
  208. return 1;
  209. mapping = page_mapping(page);
  210. if (!mapping)
  211. return 0;
  212. /* File is mmap'd by somebody? */
  213. return mapping_mapped(mapping);
  214. }
  215. static inline int is_page_cache_freeable(struct page *page)
  216. {
  217. return page_count(page) - !!PagePrivate(page) == 2;
  218. }
  219. static int may_write_to_queue(struct backing_dev_info *bdi)
  220. {
  221. if (current->flags & PF_SWAPWRITE)
  222. return 1;
  223. if (!bdi_write_congested(bdi))
  224. return 1;
  225. if (bdi == current->backing_dev_info)
  226. return 1;
  227. return 0;
  228. }
  229. /*
  230. * We detected a synchronous write error writing a page out. Probably
  231. * -ENOSPC. We need to propagate that into the address_space for a subsequent
  232. * fsync(), msync() or close().
  233. *
  234. * The tricky part is that after writepage we cannot touch the mapping: nothing
  235. * prevents it from being freed up. But we have a ref on the page and once
  236. * that page is locked, the mapping is pinned.
  237. *
  238. * We're allowed to run sleeping lock_page() here because we know the caller has
  239. * __GFP_FS.
  240. */
  241. static void handle_write_error(struct address_space *mapping,
  242. struct page *page, int error)
  243. {
  244. lock_page(page);
  245. if (page_mapping(page) == mapping) {
  246. if (error == -ENOSPC)
  247. set_bit(AS_ENOSPC, &mapping->flags);
  248. else
  249. set_bit(AS_EIO, &mapping->flags);
  250. }
  251. unlock_page(page);
  252. }
  253. /* 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. mod_page_state(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. __mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
  581. __mod_page_state(kswapd_steal, nr_freed);
  582. } else
  583. __mod_page_state_zone(zone, pgscan_direct, nr_scan);
  584. __mod_page_state_zone(zone, pgsteal, 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 = (sc->nr_mapped * 100) / vm_total_pages;
  658. /*
  659. * Now decide how much we really want to unmap some pages. The
  660. * mapped ratio is downgraded - just because there's a lot of
  661. * mapped memory doesn't necessarily mean that page reclaim
  662. * isn't succeeding.
  663. *
  664. * The distress ratio is important - we don't want to start
  665. * going oom.
  666. *
  667. * A 100% value of vm_swappiness overrides this algorithm
  668. * altogether.
  669. */
  670. swap_tendency = mapped_ratio / 2 + distress + sc->swappiness;
  671. /*
  672. * Now use this metric to decide whether to start moving mapped
  673. * memory onto the inactive list.
  674. */
  675. if (swap_tendency >= 100)
  676. reclaim_mapped = 1;
  677. }
  678. lru_add_drain();
  679. spin_lock_irq(&zone->lru_lock);
  680. pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
  681. &l_hold, &pgscanned);
  682. zone->pages_scanned += pgscanned;
  683. zone->nr_active -= pgmoved;
  684. spin_unlock_irq(&zone->lru_lock);
  685. while (!list_empty(&l_hold)) {
  686. cond_resched();
  687. page = lru_to_page(&l_hold);
  688. list_del(&page->lru);
  689. if (page_mapped(page)) {
  690. if (!reclaim_mapped ||
  691. (total_swap_pages == 0 && PageAnon(page)) ||
  692. page_referenced(page, 0)) {
  693. list_add(&page->lru, &l_active);
  694. continue;
  695. }
  696. }
  697. list_add(&page->lru, &l_inactive);
  698. }
  699. pagevec_init(&pvec, 1);
  700. pgmoved = 0;
  701. spin_lock_irq(&zone->lru_lock);
  702. while (!list_empty(&l_inactive)) {
  703. page = lru_to_page(&l_inactive);
  704. prefetchw_prev_lru_page(page, &l_inactive, flags);
  705. BUG_ON(PageLRU(page));
  706. SetPageLRU(page);
  707. BUG_ON(!PageActive(page));
  708. ClearPageActive(page);
  709. list_move(&page->lru, &zone->inactive_list);
  710. pgmoved++;
  711. if (!pagevec_add(&pvec, page)) {
  712. zone->nr_inactive += pgmoved;
  713. spin_unlock_irq(&zone->lru_lock);
  714. pgdeactivate += pgmoved;
  715. pgmoved = 0;
  716. if (buffer_heads_over_limit)
  717. pagevec_strip(&pvec);
  718. __pagevec_release(&pvec);
  719. spin_lock_irq(&zone->lru_lock);
  720. }
  721. }
  722. zone->nr_inactive += pgmoved;
  723. pgdeactivate += pgmoved;
  724. if (buffer_heads_over_limit) {
  725. spin_unlock_irq(&zone->lru_lock);
  726. pagevec_strip(&pvec);
  727. spin_lock_irq(&zone->lru_lock);
  728. }
  729. pgmoved = 0;
  730. while (!list_empty(&l_active)) {
  731. page = lru_to_page(&l_active);
  732. prefetchw_prev_lru_page(page, &l_active, flags);
  733. BUG_ON(PageLRU(page));
  734. SetPageLRU(page);
  735. BUG_ON(!PageActive(page));
  736. list_move(&page->lru, &zone->active_list);
  737. pgmoved++;
  738. if (!pagevec_add(&pvec, page)) {
  739. zone->nr_active += pgmoved;
  740. pgmoved = 0;
  741. spin_unlock_irq(&zone->lru_lock);
  742. __pagevec_release(&pvec);
  743. spin_lock_irq(&zone->lru_lock);
  744. }
  745. }
  746. zone->nr_active += pgmoved;
  747. spin_unlock(&zone->lru_lock);
  748. __mod_page_state_zone(zone, pgrefill, pgscanned);
  749. __mod_page_state(pgdeactivate, pgdeactivate);
  750. local_irq_enable();
  751. pagevec_release(&pvec);
  752. }
  753. /*
  754. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  755. */
  756. static unsigned long shrink_zone(int priority, struct zone *zone,
  757. struct scan_control *sc)
  758. {
  759. unsigned long nr_active;
  760. unsigned long nr_inactive;
  761. unsigned long nr_to_scan;
  762. unsigned long nr_reclaimed = 0;
  763. atomic_inc(&zone->reclaim_in_progress);
  764. /*
  765. * Add one to `nr_to_scan' just to make sure that the kernel will
  766. * slowly sift through the active list.
  767. */
  768. zone->nr_scan_active += (zone->nr_active >> priority) + 1;
  769. nr_active = zone->nr_scan_active;
  770. if (nr_active >= sc->swap_cluster_max)
  771. zone->nr_scan_active = 0;
  772. else
  773. nr_active = 0;
  774. zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1;
  775. nr_inactive = zone->nr_scan_inactive;
  776. if (nr_inactive >= sc->swap_cluster_max)
  777. zone->nr_scan_inactive = 0;
  778. else
  779. nr_inactive = 0;
  780. while (nr_active || nr_inactive) {
  781. if (nr_active) {
  782. nr_to_scan = min(nr_active,
  783. (unsigned long)sc->swap_cluster_max);
  784. nr_active -= nr_to_scan;
  785. shrink_active_list(nr_to_scan, zone, sc);
  786. }
  787. if (nr_inactive) {
  788. nr_to_scan = min(nr_inactive,
  789. (unsigned long)sc->swap_cluster_max);
  790. nr_inactive -= nr_to_scan;
  791. nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
  792. sc);
  793. }
  794. }
  795. throttle_vm_writeout();
  796. atomic_dec(&zone->reclaim_in_progress);
  797. return nr_reclaimed;
  798. }
  799. /*
  800. * This is the direct reclaim path, for page-allocating processes. We only
  801. * try to reclaim pages from zones which will satisfy the caller's allocation
  802. * request.
  803. *
  804. * We reclaim from a zone even if that zone is over pages_high. Because:
  805. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  806. * allocation or
  807. * b) The zones may be over pages_high but they must go *over* pages_high to
  808. * satisfy the `incremental min' zone defense algorithm.
  809. *
  810. * Returns the number of reclaimed pages.
  811. *
  812. * If a zone is deemed to be full of pinned pages then just give it a light
  813. * scan then give up on it.
  814. */
  815. static unsigned long shrink_zones(int priority, struct zone **zones,
  816. struct scan_control *sc)
  817. {
  818. unsigned long nr_reclaimed = 0;
  819. int i;
  820. for (i = 0; zones[i] != NULL; i++) {
  821. struct zone *zone = zones[i];
  822. if (!populated_zone(zone))
  823. continue;
  824. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  825. continue;
  826. zone->temp_priority = priority;
  827. if (zone->prev_priority > priority)
  828. zone->prev_priority = priority;
  829. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  830. continue; /* Let kswapd poll it */
  831. nr_reclaimed += shrink_zone(priority, zone, sc);
  832. }
  833. return nr_reclaimed;
  834. }
  835. /*
  836. * This is the main entry point to direct page reclaim.
  837. *
  838. * If a full scan of the inactive list fails to free enough memory then we
  839. * are "out of memory" and something needs to be killed.
  840. *
  841. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  842. * high - the zone may be full of dirty or under-writeback pages, which this
  843. * caller can't do much about. We kick pdflush and take explicit naps in the
  844. * hope that some of these pages can be written. But if the allocating task
  845. * holds filesystem locks which prevent writeout this might not work, and the
  846. * allocation attempt will fail.
  847. */
  848. unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
  849. {
  850. int priority;
  851. int ret = 0;
  852. unsigned long total_scanned = 0;
  853. unsigned long nr_reclaimed = 0;
  854. struct reclaim_state *reclaim_state = current->reclaim_state;
  855. unsigned long lru_pages = 0;
  856. int i;
  857. struct scan_control sc = {
  858. .gfp_mask = gfp_mask,
  859. .may_writepage = !laptop_mode,
  860. .swap_cluster_max = SWAP_CLUSTER_MAX,
  861. .may_swap = 1,
  862. .swappiness = vm_swappiness,
  863. };
  864. inc_page_state(allocstall);
  865. for (i = 0; zones[i] != NULL; i++) {
  866. struct zone *zone = zones[i];
  867. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  868. continue;
  869. zone->temp_priority = DEF_PRIORITY;
  870. lru_pages += zone->nr_active + zone->nr_inactive;
  871. }
  872. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  873. sc.nr_mapped = read_page_state(nr_mapped);
  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. sc.nr_mapped = read_page_state(nr_mapped);
  953. inc_page_state(pageoutrun);
  954. for (i = 0; i < pgdat->nr_zones; i++) {
  955. struct zone *zone = pgdat->node_zones + i;
  956. zone->temp_priority = DEF_PRIORITY;
  957. }
  958. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  959. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  960. unsigned long lru_pages = 0;
  961. /* The swap token gets in the way of swapout... */
  962. if (!priority)
  963. disable_swap_token();
  964. all_zones_ok = 1;
  965. /*
  966. * Scan in the highmem->dma direction for the highest
  967. * zone which needs scanning
  968. */
  969. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  970. struct zone *zone = pgdat->node_zones + i;
  971. if (!populated_zone(zone))
  972. continue;
  973. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  974. continue;
  975. if (!zone_watermark_ok(zone, order, zone->pages_high,
  976. 0, 0)) {
  977. end_zone = i;
  978. goto scan;
  979. }
  980. }
  981. goto out;
  982. scan:
  983. for (i = 0; i <= end_zone; i++) {
  984. struct zone *zone = pgdat->node_zones + i;
  985. lru_pages += zone->nr_active + zone->nr_inactive;
  986. }
  987. /*
  988. * Now scan the zone in the dma->highmem direction, stopping
  989. * at the last zone which needs scanning.
  990. *
  991. * We do this because the page allocator works in the opposite
  992. * direction. This prevents the page allocator from allocating
  993. * pages behind kswapd's direction of progress, which would
  994. * cause too much scanning of the lower zones.
  995. */
  996. for (i = 0; i <= end_zone; i++) {
  997. struct zone *zone = pgdat->node_zones + i;
  998. int nr_slab;
  999. if (!populated_zone(zone))
  1000. continue;
  1001. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1002. continue;
  1003. if (!zone_watermark_ok(zone, order, zone->pages_high,
  1004. end_zone, 0))
  1005. all_zones_ok = 0;
  1006. zone->temp_priority = priority;
  1007. if (zone->prev_priority > priority)
  1008. zone->prev_priority = priority;
  1009. sc.nr_scanned = 0;
  1010. nr_reclaimed += shrink_zone(priority, zone, &sc);
  1011. reclaim_state->reclaimed_slab = 0;
  1012. nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
  1013. lru_pages);
  1014. nr_reclaimed += reclaim_state->reclaimed_slab;
  1015. total_scanned += sc.nr_scanned;
  1016. if (zone->all_unreclaimable)
  1017. continue;
  1018. if (nr_slab == 0 && zone->pages_scanned >=
  1019. (zone->nr_active + zone->nr_inactive) * 4)
  1020. zone->all_unreclaimable = 1;
  1021. /*
  1022. * If we've done a decent amount of scanning and
  1023. * the reclaim ratio is low, start doing writepage
  1024. * even in laptop mode
  1025. */
  1026. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  1027. total_scanned > nr_reclaimed + nr_reclaimed / 2)
  1028. sc.may_writepage = 1;
  1029. }
  1030. if (all_zones_ok)
  1031. break; /* kswapd: all done */
  1032. /*
  1033. * OK, kswapd is getting into trouble. Take a nap, then take
  1034. * another pass across the zones.
  1035. */
  1036. if (total_scanned && priority < DEF_PRIORITY - 2)
  1037. blk_congestion_wait(WRITE, HZ/10);
  1038. /*
  1039. * We do this so kswapd doesn't build up large priorities for
  1040. * example when it is freeing in parallel with allocators. It
  1041. * matches the direct reclaim path behaviour in terms of impact
  1042. * on zone->*_priority.
  1043. */
  1044. if (nr_reclaimed >= SWAP_CLUSTER_MAX)
  1045. break;
  1046. }
  1047. out:
  1048. for (i = 0; i < pgdat->nr_zones; i++) {
  1049. struct zone *zone = pgdat->node_zones + i;
  1050. zone->prev_priority = zone->temp_priority;
  1051. }
  1052. if (!all_zones_ok) {
  1053. cond_resched();
  1054. goto loop_again;
  1055. }
  1056. return nr_reclaimed;
  1057. }
  1058. /*
  1059. * The background pageout daemon, started as a kernel thread
  1060. * from the init process.
  1061. *
  1062. * This basically trickles out pages so that we have _some_
  1063. * free memory available even if there is no other activity
  1064. * that frees anything up. This is needed for things like routing
  1065. * etc, where we otherwise might have all activity going on in
  1066. * asynchronous contexts that cannot page things out.
  1067. *
  1068. * If there are applications that are active memory-allocators
  1069. * (most normal use), this basically shouldn't matter.
  1070. */
  1071. static int kswapd(void *p)
  1072. {
  1073. unsigned long order;
  1074. pg_data_t *pgdat = (pg_data_t*)p;
  1075. struct task_struct *tsk = current;
  1076. DEFINE_WAIT(wait);
  1077. struct reclaim_state reclaim_state = {
  1078. .reclaimed_slab = 0,
  1079. };
  1080. cpumask_t cpumask;
  1081. daemonize("kswapd%d", pgdat->node_id);
  1082. cpumask = node_to_cpumask(pgdat->node_id);
  1083. if (!cpus_empty(cpumask))
  1084. set_cpus_allowed(tsk, cpumask);
  1085. current->reclaim_state = &reclaim_state;
  1086. /*
  1087. * Tell the memory management that we're a "memory allocator",
  1088. * and that if we need more memory we should get access to it
  1089. * regardless (see "__alloc_pages()"). "kswapd" should
  1090. * never get caught in the normal page freeing logic.
  1091. *
  1092. * (Kswapd normally doesn't need memory anyway, but sometimes
  1093. * you need a small amount of memory in order to be able to
  1094. * page out something else, and this flag essentially protects
  1095. * us from recursively trying to free more memory as we're
  1096. * trying to free the first piece of memory in the first place).
  1097. */
  1098. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  1099. order = 0;
  1100. for ( ; ; ) {
  1101. unsigned long new_order;
  1102. try_to_freeze();
  1103. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  1104. new_order = pgdat->kswapd_max_order;
  1105. pgdat->kswapd_max_order = 0;
  1106. if (order < new_order) {
  1107. /*
  1108. * Don't sleep if someone wants a larger 'order'
  1109. * allocation
  1110. */
  1111. order = new_order;
  1112. } else {
  1113. schedule();
  1114. order = pgdat->kswapd_max_order;
  1115. }
  1116. finish_wait(&pgdat->kswapd_wait, &wait);
  1117. balance_pgdat(pgdat, order);
  1118. }
  1119. return 0;
  1120. }
  1121. /*
  1122. * A zone is low on free memory, so wake its kswapd task to service it.
  1123. */
  1124. void wakeup_kswapd(struct zone *zone, int order)
  1125. {
  1126. pg_data_t *pgdat;
  1127. if (!populated_zone(zone))
  1128. return;
  1129. pgdat = zone->zone_pgdat;
  1130. if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
  1131. return;
  1132. if (pgdat->kswapd_max_order < order)
  1133. pgdat->kswapd_max_order = order;
  1134. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  1135. return;
  1136. if (!waitqueue_active(&pgdat->kswapd_wait))
  1137. return;
  1138. wake_up_interruptible(&pgdat->kswapd_wait);
  1139. }
  1140. #ifdef CONFIG_PM
  1141. /*
  1142. * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
  1143. * from LRU lists system-wide, for given pass and priority, and returns the
  1144. * number of reclaimed pages
  1145. *
  1146. * For pass > 3 we also try to shrink the LRU lists that contain a few pages
  1147. */
  1148. static unsigned long shrink_all_zones(unsigned long nr_pages, int pass,
  1149. int prio, struct scan_control *sc)
  1150. {
  1151. struct zone *zone;
  1152. unsigned long nr_to_scan, ret = 0;
  1153. for_each_zone(zone) {
  1154. if (!populated_zone(zone))
  1155. continue;
  1156. if (zone->all_unreclaimable && prio != DEF_PRIORITY)
  1157. continue;
  1158. /* For pass = 0 we don't shrink the active list */
  1159. if (pass > 0) {
  1160. zone->nr_scan_active += (zone->nr_active >> prio) + 1;
  1161. if (zone->nr_scan_active >= nr_pages || pass > 3) {
  1162. zone->nr_scan_active = 0;
  1163. nr_to_scan = min(nr_pages, zone->nr_active);
  1164. shrink_active_list(nr_to_scan, zone, sc);
  1165. }
  1166. }
  1167. zone->nr_scan_inactive += (zone->nr_inactive >> prio) + 1;
  1168. if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
  1169. zone->nr_scan_inactive = 0;
  1170. nr_to_scan = min(nr_pages, zone->nr_inactive);
  1171. ret += shrink_inactive_list(nr_to_scan, zone, sc);
  1172. if (ret >= nr_pages)
  1173. return ret;
  1174. }
  1175. }
  1176. return ret;
  1177. }
  1178. /*
  1179. * Try to free `nr_pages' of memory, system-wide, and return the number of
  1180. * freed pages.
  1181. *
  1182. * Rather than trying to age LRUs the aim is to preserve the overall
  1183. * LRU order by reclaiming preferentially
  1184. * inactive > active > active referenced > active mapped
  1185. */
  1186. unsigned long shrink_all_memory(unsigned long nr_pages)
  1187. {
  1188. unsigned long lru_pages, nr_slab;
  1189. unsigned long ret = 0;
  1190. int pass;
  1191. struct reclaim_state reclaim_state;
  1192. struct zone *zone;
  1193. struct scan_control sc = {
  1194. .gfp_mask = GFP_KERNEL,
  1195. .may_swap = 0,
  1196. .swap_cluster_max = nr_pages,
  1197. .may_writepage = 1,
  1198. .swappiness = vm_swappiness,
  1199. };
  1200. current->reclaim_state = &reclaim_state;
  1201. lru_pages = 0;
  1202. for_each_zone(zone)
  1203. lru_pages += zone->nr_active + zone->nr_inactive;
  1204. nr_slab = read_page_state(nr_slab);
  1205. /* If slab caches are huge, it's better to hit them first */
  1206. while (nr_slab >= lru_pages) {
  1207. reclaim_state.reclaimed_slab = 0;
  1208. shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
  1209. if (!reclaim_state.reclaimed_slab)
  1210. break;
  1211. ret += reclaim_state.reclaimed_slab;
  1212. if (ret >= nr_pages)
  1213. goto out;
  1214. nr_slab -= reclaim_state.reclaimed_slab;
  1215. }
  1216. /*
  1217. * We try to shrink LRUs in 5 passes:
  1218. * 0 = Reclaim from inactive_list only
  1219. * 1 = Reclaim from active list but don't reclaim mapped
  1220. * 2 = 2nd pass of type 1
  1221. * 3 = Reclaim mapped (normal reclaim)
  1222. * 4 = 2nd pass of type 3
  1223. */
  1224. for (pass = 0; pass < 5; pass++) {
  1225. int prio;
  1226. /* Needed for shrinking slab caches later on */
  1227. if (!lru_pages)
  1228. for_each_zone(zone) {
  1229. lru_pages += zone->nr_active;
  1230. lru_pages += zone->nr_inactive;
  1231. }
  1232. /* Force reclaiming mapped pages in the passes #3 and #4 */
  1233. if (pass > 2) {
  1234. sc.may_swap = 1;
  1235. sc.swappiness = 100;
  1236. }
  1237. for (prio = DEF_PRIORITY; prio >= 0; prio--) {
  1238. unsigned long nr_to_scan = nr_pages - ret;
  1239. sc.nr_mapped = read_page_state(nr_mapped);
  1240. sc.nr_scanned = 0;
  1241. ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
  1242. if (ret >= nr_pages)
  1243. goto out;
  1244. reclaim_state.reclaimed_slab = 0;
  1245. shrink_slab(sc.nr_scanned, sc.gfp_mask, lru_pages);
  1246. ret += reclaim_state.reclaimed_slab;
  1247. if (ret >= nr_pages)
  1248. goto out;
  1249. if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
  1250. blk_congestion_wait(WRITE, HZ / 10);
  1251. }
  1252. lru_pages = 0;
  1253. }
  1254. /*
  1255. * If ret = 0, we could not shrink LRUs, but there may be something
  1256. * in slab caches
  1257. */
  1258. if (!ret)
  1259. do {
  1260. reclaim_state.reclaimed_slab = 0;
  1261. shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
  1262. ret += reclaim_state.reclaimed_slab;
  1263. } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
  1264. out:
  1265. current->reclaim_state = NULL;
  1266. return ret;
  1267. }
  1268. #endif
  1269. #ifdef CONFIG_HOTPLUG_CPU
  1270. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  1271. not required for correctness. So if the last cpu in a node goes
  1272. away, we get changed to run anywhere: as the first one comes back,
  1273. restore their cpu bindings. */
  1274. static int cpu_callback(struct notifier_block *nfb,
  1275. unsigned long action, void *hcpu)
  1276. {
  1277. pg_data_t *pgdat;
  1278. cpumask_t mask;
  1279. if (action == CPU_ONLINE) {
  1280. for_each_online_pgdat(pgdat) {
  1281. mask = node_to_cpumask(pgdat->node_id);
  1282. if (any_online_cpu(mask) != NR_CPUS)
  1283. /* One of our CPUs online: restore mask */
  1284. set_cpus_allowed(pgdat->kswapd, mask);
  1285. }
  1286. }
  1287. return NOTIFY_OK;
  1288. }
  1289. #endif /* CONFIG_HOTPLUG_CPU */
  1290. static int __init kswapd_init(void)
  1291. {
  1292. pg_data_t *pgdat;
  1293. swap_setup();
  1294. for_each_online_pgdat(pgdat) {
  1295. pid_t pid;
  1296. pid = kernel_thread(kswapd, pgdat, CLONE_KERNEL);
  1297. BUG_ON(pid < 0);
  1298. read_lock(&tasklist_lock);
  1299. pgdat->kswapd = find_task_by_pid(pid);
  1300. read_unlock(&tasklist_lock);
  1301. }
  1302. hotcpu_notifier(cpu_callback, 0);
  1303. return 0;
  1304. }
  1305. module_init(kswapd_init)
  1306. #ifdef CONFIG_NUMA
  1307. /*
  1308. * Zone reclaim mode
  1309. *
  1310. * If non-zero call zone_reclaim when the number of free pages falls below
  1311. * the watermarks.
  1312. *
  1313. * In the future we may add flags to the mode. However, the page allocator
  1314. * should only have to check that zone_reclaim_mode != 0 before calling
  1315. * zone_reclaim().
  1316. */
  1317. int zone_reclaim_mode __read_mostly;
  1318. #define RECLAIM_OFF 0
  1319. #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
  1320. #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
  1321. #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
  1322. #define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */
  1323. /*
  1324. * Mininum time between zone reclaim scans
  1325. */
  1326. int zone_reclaim_interval __read_mostly = 30*HZ;
  1327. /*
  1328. * Priority for ZONE_RECLAIM. This determines the fraction of pages
  1329. * of a node considered for each zone_reclaim. 4 scans 1/16th of
  1330. * a zone.
  1331. */
  1332. #define ZONE_RECLAIM_PRIORITY 4
  1333. /*
  1334. * Try to free up some pages from this zone through reclaim.
  1335. */
  1336. static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  1337. {
  1338. /* Minimum pages needed in order to stay on node */
  1339. const unsigned long nr_pages = 1 << order;
  1340. struct task_struct *p = current;
  1341. struct reclaim_state reclaim_state;
  1342. int priority;
  1343. unsigned long nr_reclaimed = 0;
  1344. struct scan_control sc = {
  1345. .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
  1346. .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP),
  1347. .nr_mapped = read_page_state(nr_mapped),
  1348. .swap_cluster_max = max_t(unsigned long, nr_pages,
  1349. SWAP_CLUSTER_MAX),
  1350. .gfp_mask = gfp_mask,
  1351. .swappiness = vm_swappiness,
  1352. };
  1353. disable_swap_token();
  1354. cond_resched();
  1355. /*
  1356. * We need to be able to allocate from the reserves for RECLAIM_SWAP
  1357. * and we also need to be able to write out pages for RECLAIM_WRITE
  1358. * and RECLAIM_SWAP.
  1359. */
  1360. p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
  1361. reclaim_state.reclaimed_slab = 0;
  1362. p->reclaim_state = &reclaim_state;
  1363. /*
  1364. * Free memory by calling shrink zone with increasing priorities
  1365. * until we have enough memory freed.
  1366. */
  1367. priority = ZONE_RECLAIM_PRIORITY;
  1368. do {
  1369. nr_reclaimed += shrink_zone(priority, zone, &sc);
  1370. priority--;
  1371. } while (priority >= 0 && nr_reclaimed < nr_pages);
  1372. if (nr_reclaimed < nr_pages && (zone_reclaim_mode & RECLAIM_SLAB)) {
  1373. /*
  1374. * shrink_slab() does not currently allow us to determine how
  1375. * many pages were freed in this zone. So we just shake the slab
  1376. * a bit and then go off node for this particular allocation
  1377. * despite possibly having freed enough memory to allocate in
  1378. * this zone. If we freed local memory then the next
  1379. * allocations will be local again.
  1380. *
  1381. * shrink_slab will free memory on all zones and may take
  1382. * a long time.
  1383. */
  1384. shrink_slab(sc.nr_scanned, gfp_mask, order);
  1385. }
  1386. p->reclaim_state = NULL;
  1387. current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
  1388. if (nr_reclaimed == 0) {
  1389. /*
  1390. * We were unable to reclaim enough pages to stay on node. We
  1391. * now allow off node accesses for a certain time period before
  1392. * trying again to reclaim pages from the local zone.
  1393. */
  1394. zone->last_unsuccessful_zone_reclaim = jiffies;
  1395. }
  1396. return nr_reclaimed >= nr_pages;
  1397. }
  1398. int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
  1399. {
  1400. cpumask_t mask;
  1401. int node_id;
  1402. /*
  1403. * Do not reclaim if there was a recent unsuccessful attempt at zone
  1404. * reclaim. In that case we let allocations go off node for the
  1405. * zone_reclaim_interval. Otherwise we would scan for each off-node
  1406. * page allocation.
  1407. */
  1408. if (time_before(jiffies,
  1409. zone->last_unsuccessful_zone_reclaim + zone_reclaim_interval))
  1410. return 0;
  1411. /*
  1412. * Avoid concurrent zone reclaims, do not reclaim in a zone that does
  1413. * not have reclaimable pages and if we should not delay the allocation
  1414. * then do not scan.
  1415. */
  1416. if (!(gfp_mask & __GFP_WAIT) ||
  1417. zone->all_unreclaimable ||
  1418. atomic_read(&zone->reclaim_in_progress) > 0 ||
  1419. (current->flags & PF_MEMALLOC))
  1420. return 0;
  1421. /*
  1422. * Only run zone reclaim on the local zone or on zones that do not
  1423. * have associated processors. This will favor the local processor
  1424. * over remote processors and spread off node memory allocations
  1425. * as wide as possible.
  1426. */
  1427. node_id = zone->zone_pgdat->node_id;
  1428. mask = node_to_cpumask(node_id);
  1429. if (!cpus_empty(mask) && node_id != numa_node_id())
  1430. return 0;
  1431. return __zone_reclaim(zone, gfp_mask, order);
  1432. }
  1433. #endif