vmscan.c 41 KB

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