vmscan.c 44 KB

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