vmscan.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  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. static int swap_page(struct page *page)
  537. {
  538. struct address_space *mapping = page_mapping(page);
  539. if (page_mapped(page) && mapping)
  540. if (try_to_unmap(page) != SWAP_SUCCESS)
  541. goto unlock_retry;
  542. if (PageDirty(page)) {
  543. /* Page is dirty, try to write it out here */
  544. switch(pageout(page, mapping)) {
  545. case PAGE_KEEP:
  546. case PAGE_ACTIVATE:
  547. goto unlock_retry;
  548. case PAGE_SUCCESS:
  549. goto retry;
  550. case PAGE_CLEAN:
  551. ; /* try to free the page below */
  552. }
  553. }
  554. if (PagePrivate(page)) {
  555. if (!try_to_release_page(page, GFP_KERNEL) ||
  556. (!mapping && page_count(page) == 1))
  557. goto unlock_retry;
  558. }
  559. if (remove_mapping(mapping, page)) {
  560. /* Success */
  561. unlock_page(page);
  562. return 0;
  563. }
  564. unlock_retry:
  565. unlock_page(page);
  566. retry:
  567. return -EAGAIN;
  568. }
  569. /*
  570. * migrate_pages
  571. *
  572. * Two lists are passed to this function. The first list
  573. * contains the pages isolated from the LRU to be migrated.
  574. * The second list contains new pages that the pages isolated
  575. * can be moved to. If the second list is NULL then all
  576. * pages are swapped out.
  577. *
  578. * The function returns after 10 attempts or if no pages
  579. * are movable anymore because t has become empty
  580. * or no retryable pages exist anymore.
  581. *
  582. * SIMPLIFIED VERSION: This implementation of migrate_pages
  583. * is only swapping out pages and never touches the second
  584. * list. The direct migration patchset
  585. * extends this function to avoid the use of swap.
  586. *
  587. * Return: Number of pages not migrated when "to" ran empty.
  588. */
  589. int migrate_pages(struct list_head *from, struct list_head *to,
  590. struct list_head *moved, struct list_head *failed)
  591. {
  592. int retry;
  593. int nr_failed = 0;
  594. int pass = 0;
  595. struct page *page;
  596. struct page *page2;
  597. int swapwrite = current->flags & PF_SWAPWRITE;
  598. int rc;
  599. if (!swapwrite)
  600. current->flags |= PF_SWAPWRITE;
  601. redo:
  602. retry = 0;
  603. list_for_each_entry_safe(page, page2, from, lru) {
  604. cond_resched();
  605. rc = 0;
  606. if (page_count(page) == 1)
  607. /* page was freed from under us. So we are done. */
  608. goto next;
  609. /*
  610. * Skip locked pages during the first two passes to give the
  611. * functions holding the lock time to release the page. Later we
  612. * use lock_page() to have a higher chance of acquiring the
  613. * lock.
  614. */
  615. rc = -EAGAIN;
  616. if (pass > 2)
  617. lock_page(page);
  618. else
  619. if (TestSetPageLocked(page))
  620. goto next;
  621. /*
  622. * Only wait on writeback if we have already done a pass where
  623. * we we may have triggered writeouts for lots of pages.
  624. */
  625. if (pass > 0) {
  626. wait_on_page_writeback(page);
  627. } else {
  628. if (PageWriteback(page))
  629. goto unlock_page;
  630. }
  631. /*
  632. * Anonymous pages must have swap cache references otherwise
  633. * the information contained in the page maps cannot be
  634. * preserved.
  635. */
  636. if (PageAnon(page) && !PageSwapCache(page)) {
  637. if (!add_to_swap(page, GFP_KERNEL)) {
  638. rc = -ENOMEM;
  639. goto unlock_page;
  640. }
  641. }
  642. /*
  643. * Page is properly locked and writeback is complete.
  644. * Try to migrate the page.
  645. */
  646. rc = swap_page(page);
  647. goto next;
  648. unlock_page:
  649. unlock_page(page);
  650. next:
  651. if (rc == -EAGAIN) {
  652. retry++;
  653. } else if (rc) {
  654. /* Permanent failure */
  655. list_move(&page->lru, failed);
  656. nr_failed++;
  657. } else {
  658. /* Success */
  659. list_move(&page->lru, moved);
  660. }
  661. }
  662. if (retry && pass++ < 10)
  663. goto redo;
  664. if (!swapwrite)
  665. current->flags &= ~PF_SWAPWRITE;
  666. return nr_failed + retry;
  667. }
  668. static void lru_add_drain_per_cpu(void *dummy)
  669. {
  670. lru_add_drain();
  671. }
  672. /*
  673. * Isolate one page from the LRU lists and put it on the
  674. * indicated list. Do necessary cache draining if the
  675. * page is not on the LRU lists yet.
  676. *
  677. * Result:
  678. * 0 = page not on LRU list
  679. * 1 = page removed from LRU list and added to the specified list.
  680. * -ENOENT = page is being freed elsewhere.
  681. */
  682. int isolate_lru_page(struct page *page)
  683. {
  684. int rc = 0;
  685. struct zone *zone = page_zone(page);
  686. redo:
  687. spin_lock_irq(&zone->lru_lock);
  688. rc = __isolate_lru_page(page);
  689. if (rc == 1) {
  690. if (PageActive(page))
  691. del_page_from_active_list(zone, page);
  692. else
  693. del_page_from_inactive_list(zone, page);
  694. }
  695. spin_unlock_irq(&zone->lru_lock);
  696. if (rc == 0) {
  697. /*
  698. * Maybe this page is still waiting for a cpu to drain it
  699. * from one of the lru lists?
  700. */
  701. rc = schedule_on_each_cpu(lru_add_drain_per_cpu, NULL);
  702. if (rc == 0 && PageLRU(page))
  703. goto redo;
  704. }
  705. return rc;
  706. }
  707. #endif
  708. /*
  709. * zone->lru_lock is heavily contended. Some of the functions that
  710. * shrink the lists perform better by taking out a batch of pages
  711. * and working on them outside the LRU lock.
  712. *
  713. * For pagecache intensive workloads, this function is the hottest
  714. * spot in the kernel (apart from copy_*_user functions).
  715. *
  716. * Appropriate locks must be held before calling this function.
  717. *
  718. * @nr_to_scan: The number of pages to look through on the list.
  719. * @src: The LRU list to pull pages off.
  720. * @dst: The temp list to put pages on to.
  721. * @scanned: The number of pages that were scanned.
  722. *
  723. * returns how many pages were moved onto *@dst.
  724. */
  725. static int isolate_lru_pages(int nr_to_scan, struct list_head *src,
  726. struct list_head *dst, int *scanned)
  727. {
  728. int nr_taken = 0;
  729. struct page *page;
  730. int scan = 0;
  731. while (scan++ < nr_to_scan && !list_empty(src)) {
  732. page = lru_to_page(src);
  733. prefetchw_prev_lru_page(page, src, flags);
  734. switch (__isolate_lru_page(page)) {
  735. case 1:
  736. /* Succeeded to isolate page */
  737. list_move(&page->lru, dst);
  738. nr_taken++;
  739. break;
  740. case -ENOENT:
  741. /* Not possible to isolate */
  742. list_move(&page->lru, src);
  743. break;
  744. default:
  745. BUG();
  746. }
  747. }
  748. *scanned = scan;
  749. return nr_taken;
  750. }
  751. /*
  752. * shrink_cache() adds the number of pages reclaimed to sc->nr_reclaimed
  753. */
  754. static void shrink_cache(struct zone *zone, struct scan_control *sc)
  755. {
  756. LIST_HEAD(page_list);
  757. struct pagevec pvec;
  758. int max_scan = sc->nr_to_scan;
  759. pagevec_init(&pvec, 1);
  760. lru_add_drain();
  761. spin_lock_irq(&zone->lru_lock);
  762. while (max_scan > 0) {
  763. struct page *page;
  764. int nr_taken;
  765. int nr_scan;
  766. int nr_freed;
  767. nr_taken = isolate_lru_pages(sc->swap_cluster_max,
  768. &zone->inactive_list,
  769. &page_list, &nr_scan);
  770. zone->nr_inactive -= nr_taken;
  771. zone->pages_scanned += nr_scan;
  772. spin_unlock_irq(&zone->lru_lock);
  773. if (nr_taken == 0)
  774. goto done;
  775. max_scan -= nr_scan;
  776. nr_freed = shrink_list(&page_list, sc);
  777. local_irq_disable();
  778. if (current_is_kswapd()) {
  779. __mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
  780. __mod_page_state(kswapd_steal, nr_freed);
  781. } else
  782. __mod_page_state_zone(zone, pgscan_direct, nr_scan);
  783. __mod_page_state_zone(zone, pgsteal, nr_freed);
  784. spin_lock(&zone->lru_lock);
  785. /*
  786. * Put back any unfreeable pages.
  787. */
  788. while (!list_empty(&page_list)) {
  789. page = lru_to_page(&page_list);
  790. if (TestSetPageLRU(page))
  791. BUG();
  792. list_del(&page->lru);
  793. if (PageActive(page))
  794. add_page_to_active_list(zone, page);
  795. else
  796. add_page_to_inactive_list(zone, page);
  797. if (!pagevec_add(&pvec, page)) {
  798. spin_unlock_irq(&zone->lru_lock);
  799. __pagevec_release(&pvec);
  800. spin_lock_irq(&zone->lru_lock);
  801. }
  802. }
  803. }
  804. spin_unlock_irq(&zone->lru_lock);
  805. done:
  806. pagevec_release(&pvec);
  807. }
  808. /*
  809. * This moves pages from the active list to the inactive list.
  810. *
  811. * We move them the other way if the page is referenced by one or more
  812. * processes, from rmap.
  813. *
  814. * If the pages are mostly unmapped, the processing is fast and it is
  815. * appropriate to hold zone->lru_lock across the whole operation. But if
  816. * the pages are mapped, the processing is slow (page_referenced()) so we
  817. * should drop zone->lru_lock around each page. It's impossible to balance
  818. * this, so instead we remove the pages from the LRU while processing them.
  819. * It is safe to rely on PG_active against the non-LRU pages in here because
  820. * nobody will play with that bit on a non-LRU page.
  821. *
  822. * The downside is that we have to touch page->_count against each page.
  823. * But we had to alter page->flags anyway.
  824. */
  825. static void
  826. refill_inactive_zone(struct zone *zone, struct scan_control *sc)
  827. {
  828. int pgmoved;
  829. int pgdeactivate = 0;
  830. int pgscanned;
  831. int nr_pages = sc->nr_to_scan;
  832. LIST_HEAD(l_hold); /* The pages which were snipped off */
  833. LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
  834. LIST_HEAD(l_active); /* Pages to go onto the active_list */
  835. struct page *page;
  836. struct pagevec pvec;
  837. int reclaim_mapped = 0;
  838. long mapped_ratio;
  839. long distress;
  840. long swap_tendency;
  841. lru_add_drain();
  842. spin_lock_irq(&zone->lru_lock);
  843. pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
  844. &l_hold, &pgscanned);
  845. zone->pages_scanned += pgscanned;
  846. zone->nr_active -= pgmoved;
  847. spin_unlock_irq(&zone->lru_lock);
  848. /*
  849. * `distress' is a measure of how much trouble we're having reclaiming
  850. * pages. 0 -> no problems. 100 -> great trouble.
  851. */
  852. distress = 100 >> zone->prev_priority;
  853. /*
  854. * The point of this algorithm is to decide when to start reclaiming
  855. * mapped memory instead of just pagecache. Work out how much memory
  856. * is mapped.
  857. */
  858. mapped_ratio = (sc->nr_mapped * 100) / total_memory;
  859. /*
  860. * Now decide how much we really want to unmap some pages. The mapped
  861. * ratio is downgraded - just because there's a lot of mapped memory
  862. * doesn't necessarily mean that page reclaim isn't succeeding.
  863. *
  864. * The distress ratio is important - we don't want to start going oom.
  865. *
  866. * A 100% value of vm_swappiness overrides this algorithm altogether.
  867. */
  868. swap_tendency = mapped_ratio / 2 + distress + vm_swappiness;
  869. /*
  870. * Now use this metric to decide whether to start moving mapped memory
  871. * onto the inactive list.
  872. */
  873. if (swap_tendency >= 100)
  874. reclaim_mapped = 1;
  875. while (!list_empty(&l_hold)) {
  876. cond_resched();
  877. page = lru_to_page(&l_hold);
  878. list_del(&page->lru);
  879. if (page_mapped(page)) {
  880. if (!reclaim_mapped ||
  881. (total_swap_pages == 0 && PageAnon(page)) ||
  882. page_referenced(page, 0)) {
  883. list_add(&page->lru, &l_active);
  884. continue;
  885. }
  886. }
  887. list_add(&page->lru, &l_inactive);
  888. }
  889. pagevec_init(&pvec, 1);
  890. pgmoved = 0;
  891. spin_lock_irq(&zone->lru_lock);
  892. while (!list_empty(&l_inactive)) {
  893. page = lru_to_page(&l_inactive);
  894. prefetchw_prev_lru_page(page, &l_inactive, flags);
  895. if (TestSetPageLRU(page))
  896. BUG();
  897. if (!TestClearPageActive(page))
  898. BUG();
  899. list_move(&page->lru, &zone->inactive_list);
  900. pgmoved++;
  901. if (!pagevec_add(&pvec, page)) {
  902. zone->nr_inactive += pgmoved;
  903. spin_unlock_irq(&zone->lru_lock);
  904. pgdeactivate += pgmoved;
  905. pgmoved = 0;
  906. if (buffer_heads_over_limit)
  907. pagevec_strip(&pvec);
  908. __pagevec_release(&pvec);
  909. spin_lock_irq(&zone->lru_lock);
  910. }
  911. }
  912. zone->nr_inactive += pgmoved;
  913. pgdeactivate += pgmoved;
  914. if (buffer_heads_over_limit) {
  915. spin_unlock_irq(&zone->lru_lock);
  916. pagevec_strip(&pvec);
  917. spin_lock_irq(&zone->lru_lock);
  918. }
  919. pgmoved = 0;
  920. while (!list_empty(&l_active)) {
  921. page = lru_to_page(&l_active);
  922. prefetchw_prev_lru_page(page, &l_active, flags);
  923. if (TestSetPageLRU(page))
  924. BUG();
  925. BUG_ON(!PageActive(page));
  926. list_move(&page->lru, &zone->active_list);
  927. pgmoved++;
  928. if (!pagevec_add(&pvec, page)) {
  929. zone->nr_active += pgmoved;
  930. pgmoved = 0;
  931. spin_unlock_irq(&zone->lru_lock);
  932. __pagevec_release(&pvec);
  933. spin_lock_irq(&zone->lru_lock);
  934. }
  935. }
  936. zone->nr_active += pgmoved;
  937. spin_unlock(&zone->lru_lock);
  938. __mod_page_state_zone(zone, pgrefill, pgscanned);
  939. __mod_page_state(pgdeactivate, pgdeactivate);
  940. local_irq_enable();
  941. pagevec_release(&pvec);
  942. }
  943. /*
  944. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  945. */
  946. static void
  947. shrink_zone(struct zone *zone, struct scan_control *sc)
  948. {
  949. unsigned long nr_active;
  950. unsigned long nr_inactive;
  951. atomic_inc(&zone->reclaim_in_progress);
  952. /*
  953. * Add one to `nr_to_scan' just to make sure that the kernel will
  954. * slowly sift through the active list.
  955. */
  956. zone->nr_scan_active += (zone->nr_active >> sc->priority) + 1;
  957. nr_active = zone->nr_scan_active;
  958. if (nr_active >= sc->swap_cluster_max)
  959. zone->nr_scan_active = 0;
  960. else
  961. nr_active = 0;
  962. zone->nr_scan_inactive += (zone->nr_inactive >> sc->priority) + 1;
  963. nr_inactive = zone->nr_scan_inactive;
  964. if (nr_inactive >= sc->swap_cluster_max)
  965. zone->nr_scan_inactive = 0;
  966. else
  967. nr_inactive = 0;
  968. while (nr_active || nr_inactive) {
  969. if (nr_active) {
  970. sc->nr_to_scan = min(nr_active,
  971. (unsigned long)sc->swap_cluster_max);
  972. nr_active -= sc->nr_to_scan;
  973. refill_inactive_zone(zone, sc);
  974. }
  975. if (nr_inactive) {
  976. sc->nr_to_scan = min(nr_inactive,
  977. (unsigned long)sc->swap_cluster_max);
  978. nr_inactive -= sc->nr_to_scan;
  979. shrink_cache(zone, sc);
  980. }
  981. }
  982. throttle_vm_writeout();
  983. atomic_dec(&zone->reclaim_in_progress);
  984. }
  985. /*
  986. * This is the direct reclaim path, for page-allocating processes. We only
  987. * try to reclaim pages from zones which will satisfy the caller's allocation
  988. * request.
  989. *
  990. * We reclaim from a zone even if that zone is over pages_high. Because:
  991. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  992. * allocation or
  993. * b) The zones may be over pages_high but they must go *over* pages_high to
  994. * satisfy the `incremental min' zone defense algorithm.
  995. *
  996. * Returns the number of reclaimed pages.
  997. *
  998. * If a zone is deemed to be full of pinned pages then just give it a light
  999. * scan then give up on it.
  1000. */
  1001. static void
  1002. shrink_caches(struct zone **zones, struct scan_control *sc)
  1003. {
  1004. int i;
  1005. for (i = 0; zones[i] != NULL; i++) {
  1006. struct zone *zone = zones[i];
  1007. if (!populated_zone(zone))
  1008. continue;
  1009. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  1010. continue;
  1011. zone->temp_priority = sc->priority;
  1012. if (zone->prev_priority > sc->priority)
  1013. zone->prev_priority = sc->priority;
  1014. if (zone->all_unreclaimable && sc->priority != DEF_PRIORITY)
  1015. continue; /* Let kswapd poll it */
  1016. shrink_zone(zone, sc);
  1017. }
  1018. }
  1019. /*
  1020. * This is the main entry point to direct page reclaim.
  1021. *
  1022. * If a full scan of the inactive list fails to free enough memory then we
  1023. * are "out of memory" and something needs to be killed.
  1024. *
  1025. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  1026. * high - the zone may be full of dirty or under-writeback pages, which this
  1027. * caller can't do much about. We kick pdflush and take explicit naps in the
  1028. * hope that some of these pages can be written. But if the allocating task
  1029. * holds filesystem locks which prevent writeout this might not work, and the
  1030. * allocation attempt will fail.
  1031. */
  1032. int try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
  1033. {
  1034. int priority;
  1035. int ret = 0;
  1036. int total_scanned = 0, total_reclaimed = 0;
  1037. struct reclaim_state *reclaim_state = current->reclaim_state;
  1038. struct scan_control sc;
  1039. unsigned long lru_pages = 0;
  1040. int i;
  1041. sc.gfp_mask = gfp_mask;
  1042. sc.may_writepage = 0;
  1043. inc_page_state(allocstall);
  1044. for (i = 0; zones[i] != NULL; i++) {
  1045. struct zone *zone = zones[i];
  1046. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  1047. continue;
  1048. zone->temp_priority = DEF_PRIORITY;
  1049. lru_pages += zone->nr_active + zone->nr_inactive;
  1050. }
  1051. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1052. sc.nr_mapped = read_page_state(nr_mapped);
  1053. sc.nr_scanned = 0;
  1054. sc.nr_reclaimed = 0;
  1055. sc.priority = priority;
  1056. sc.swap_cluster_max = SWAP_CLUSTER_MAX;
  1057. if (!priority)
  1058. disable_swap_token();
  1059. shrink_caches(zones, &sc);
  1060. shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
  1061. if (reclaim_state) {
  1062. sc.nr_reclaimed += reclaim_state->reclaimed_slab;
  1063. reclaim_state->reclaimed_slab = 0;
  1064. }
  1065. total_scanned += sc.nr_scanned;
  1066. total_reclaimed += sc.nr_reclaimed;
  1067. if (total_reclaimed >= sc.swap_cluster_max) {
  1068. ret = 1;
  1069. goto out;
  1070. }
  1071. /*
  1072. * Try to write back as many pages as we just scanned. This
  1073. * tends to cause slow streaming writers to write data to the
  1074. * disk smoothly, at the dirtying rate, which is nice. But
  1075. * that's undesirable in laptop mode, where we *want* lumpy
  1076. * writeout. So in laptop mode, write out the whole world.
  1077. */
  1078. if (total_scanned > sc.swap_cluster_max + sc.swap_cluster_max/2) {
  1079. wakeup_pdflush(laptop_mode ? 0 : total_scanned);
  1080. sc.may_writepage = 1;
  1081. }
  1082. /* Take a nap, wait for some writeback to complete */
  1083. if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
  1084. blk_congestion_wait(WRITE, HZ/10);
  1085. }
  1086. out:
  1087. for (i = 0; zones[i] != 0; i++) {
  1088. struct zone *zone = zones[i];
  1089. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  1090. continue;
  1091. zone->prev_priority = zone->temp_priority;
  1092. }
  1093. return ret;
  1094. }
  1095. /*
  1096. * For kswapd, balance_pgdat() will work across all this node's zones until
  1097. * they are all at pages_high.
  1098. *
  1099. * If `nr_pages' is non-zero then it is the number of pages which are to be
  1100. * reclaimed, regardless of the zone occupancies. This is a software suspend
  1101. * special.
  1102. *
  1103. * Returns the number of pages which were actually freed.
  1104. *
  1105. * There is special handling here for zones which are full of pinned pages.
  1106. * This can happen if the pages are all mlocked, or if they are all used by
  1107. * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
  1108. * What we do is to detect the case where all pages in the zone have been
  1109. * scanned twice and there has been zero successful reclaim. Mark the zone as
  1110. * dead and from now on, only perform a short scan. Basically we're polling
  1111. * the zone for when the problem goes away.
  1112. *
  1113. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  1114. * zones which have free_pages > pages_high, but once a zone is found to have
  1115. * free_pages <= pages_high, we scan that zone and the lower zones regardless
  1116. * of the number of free pages in the lower zones. This interoperates with
  1117. * the page allocator fallback scheme to ensure that aging of pages is balanced
  1118. * across the zones.
  1119. */
  1120. static int balance_pgdat(pg_data_t *pgdat, int nr_pages, int order)
  1121. {
  1122. int to_free = nr_pages;
  1123. int all_zones_ok;
  1124. int priority;
  1125. int i;
  1126. int total_scanned, total_reclaimed;
  1127. struct reclaim_state *reclaim_state = current->reclaim_state;
  1128. struct scan_control sc;
  1129. loop_again:
  1130. total_scanned = 0;
  1131. total_reclaimed = 0;
  1132. sc.gfp_mask = GFP_KERNEL;
  1133. sc.may_writepage = 0;
  1134. sc.nr_mapped = read_page_state(nr_mapped);
  1135. inc_page_state(pageoutrun);
  1136. for (i = 0; i < pgdat->nr_zones; i++) {
  1137. struct zone *zone = pgdat->node_zones + i;
  1138. zone->temp_priority = DEF_PRIORITY;
  1139. }
  1140. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  1141. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  1142. unsigned long lru_pages = 0;
  1143. /* The swap token gets in the way of swapout... */
  1144. if (!priority)
  1145. disable_swap_token();
  1146. all_zones_ok = 1;
  1147. if (nr_pages == 0) {
  1148. /*
  1149. * Scan in the highmem->dma direction for the highest
  1150. * zone which needs scanning
  1151. */
  1152. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  1153. struct zone *zone = pgdat->node_zones + i;
  1154. if (!populated_zone(zone))
  1155. continue;
  1156. if (zone->all_unreclaimable &&
  1157. priority != DEF_PRIORITY)
  1158. continue;
  1159. if (!zone_watermark_ok(zone, order,
  1160. zone->pages_high, 0, 0)) {
  1161. end_zone = i;
  1162. goto scan;
  1163. }
  1164. }
  1165. goto out;
  1166. } else {
  1167. end_zone = pgdat->nr_zones - 1;
  1168. }
  1169. scan:
  1170. for (i = 0; i <= end_zone; i++) {
  1171. struct zone *zone = pgdat->node_zones + i;
  1172. lru_pages += zone->nr_active + zone->nr_inactive;
  1173. }
  1174. /*
  1175. * Now scan the zone in the dma->highmem direction, stopping
  1176. * at the last zone which needs scanning.
  1177. *
  1178. * We do this because the page allocator works in the opposite
  1179. * direction. This prevents the page allocator from allocating
  1180. * pages behind kswapd's direction of progress, which would
  1181. * cause too much scanning of the lower zones.
  1182. */
  1183. for (i = 0; i <= end_zone; i++) {
  1184. struct zone *zone = pgdat->node_zones + i;
  1185. int nr_slab;
  1186. if (!populated_zone(zone))
  1187. continue;
  1188. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1189. continue;
  1190. if (nr_pages == 0) { /* Not software suspend */
  1191. if (!zone_watermark_ok(zone, order,
  1192. zone->pages_high, end_zone, 0))
  1193. all_zones_ok = 0;
  1194. }
  1195. zone->temp_priority = priority;
  1196. if (zone->prev_priority > priority)
  1197. zone->prev_priority = priority;
  1198. sc.nr_scanned = 0;
  1199. sc.nr_reclaimed = 0;
  1200. sc.priority = priority;
  1201. sc.swap_cluster_max = nr_pages? nr_pages : SWAP_CLUSTER_MAX;
  1202. atomic_inc(&zone->reclaim_in_progress);
  1203. shrink_zone(zone, &sc);
  1204. atomic_dec(&zone->reclaim_in_progress);
  1205. reclaim_state->reclaimed_slab = 0;
  1206. nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
  1207. lru_pages);
  1208. sc.nr_reclaimed += reclaim_state->reclaimed_slab;
  1209. total_reclaimed += sc.nr_reclaimed;
  1210. total_scanned += sc.nr_scanned;
  1211. if (zone->all_unreclaimable)
  1212. continue;
  1213. if (nr_slab == 0 && zone->pages_scanned >=
  1214. (zone->nr_active + zone->nr_inactive) * 4)
  1215. zone->all_unreclaimable = 1;
  1216. /*
  1217. * If we've done a decent amount of scanning and
  1218. * the reclaim ratio is low, start doing writepage
  1219. * even in laptop mode
  1220. */
  1221. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  1222. total_scanned > total_reclaimed+total_reclaimed/2)
  1223. sc.may_writepage = 1;
  1224. }
  1225. if (nr_pages && to_free > total_reclaimed)
  1226. continue; /* swsusp: need to do more work */
  1227. if (all_zones_ok)
  1228. break; /* kswapd: all done */
  1229. /*
  1230. * OK, kswapd is getting into trouble. Take a nap, then take
  1231. * another pass across the zones.
  1232. */
  1233. if (total_scanned && priority < DEF_PRIORITY - 2)
  1234. blk_congestion_wait(WRITE, HZ/10);
  1235. /*
  1236. * We do this so kswapd doesn't build up large priorities for
  1237. * example when it is freeing in parallel with allocators. It
  1238. * matches the direct reclaim path behaviour in terms of impact
  1239. * on zone->*_priority.
  1240. */
  1241. if ((total_reclaimed >= SWAP_CLUSTER_MAX) && (!nr_pages))
  1242. break;
  1243. }
  1244. out:
  1245. for (i = 0; i < pgdat->nr_zones; i++) {
  1246. struct zone *zone = pgdat->node_zones + i;
  1247. zone->prev_priority = zone->temp_priority;
  1248. }
  1249. if (!all_zones_ok) {
  1250. cond_resched();
  1251. goto loop_again;
  1252. }
  1253. return total_reclaimed;
  1254. }
  1255. /*
  1256. * The background pageout daemon, started as a kernel thread
  1257. * from the init process.
  1258. *
  1259. * This basically trickles out pages so that we have _some_
  1260. * free memory available even if there is no other activity
  1261. * that frees anything up. This is needed for things like routing
  1262. * etc, where we otherwise might have all activity going on in
  1263. * asynchronous contexts that cannot page things out.
  1264. *
  1265. * If there are applications that are active memory-allocators
  1266. * (most normal use), this basically shouldn't matter.
  1267. */
  1268. static int kswapd(void *p)
  1269. {
  1270. unsigned long order;
  1271. pg_data_t *pgdat = (pg_data_t*)p;
  1272. struct task_struct *tsk = current;
  1273. DEFINE_WAIT(wait);
  1274. struct reclaim_state reclaim_state = {
  1275. .reclaimed_slab = 0,
  1276. };
  1277. cpumask_t cpumask;
  1278. daemonize("kswapd%d", pgdat->node_id);
  1279. cpumask = node_to_cpumask(pgdat->node_id);
  1280. if (!cpus_empty(cpumask))
  1281. set_cpus_allowed(tsk, cpumask);
  1282. current->reclaim_state = &reclaim_state;
  1283. /*
  1284. * Tell the memory management that we're a "memory allocator",
  1285. * and that if we need more memory we should get access to it
  1286. * regardless (see "__alloc_pages()"). "kswapd" should
  1287. * never get caught in the normal page freeing logic.
  1288. *
  1289. * (Kswapd normally doesn't need memory anyway, but sometimes
  1290. * you need a small amount of memory in order to be able to
  1291. * page out something else, and this flag essentially protects
  1292. * us from recursively trying to free more memory as we're
  1293. * trying to free the first piece of memory in the first place).
  1294. */
  1295. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  1296. order = 0;
  1297. for ( ; ; ) {
  1298. unsigned long new_order;
  1299. try_to_freeze();
  1300. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  1301. new_order = pgdat->kswapd_max_order;
  1302. pgdat->kswapd_max_order = 0;
  1303. if (order < new_order) {
  1304. /*
  1305. * Don't sleep if someone wants a larger 'order'
  1306. * allocation
  1307. */
  1308. order = new_order;
  1309. } else {
  1310. schedule();
  1311. order = pgdat->kswapd_max_order;
  1312. }
  1313. finish_wait(&pgdat->kswapd_wait, &wait);
  1314. balance_pgdat(pgdat, 0, order);
  1315. }
  1316. return 0;
  1317. }
  1318. /*
  1319. * A zone is low on free memory, so wake its kswapd task to service it.
  1320. */
  1321. void wakeup_kswapd(struct zone *zone, int order)
  1322. {
  1323. pg_data_t *pgdat;
  1324. if (!populated_zone(zone))
  1325. return;
  1326. pgdat = zone->zone_pgdat;
  1327. if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
  1328. return;
  1329. if (pgdat->kswapd_max_order < order)
  1330. pgdat->kswapd_max_order = order;
  1331. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  1332. return;
  1333. if (!waitqueue_active(&pgdat->kswapd_wait))
  1334. return;
  1335. wake_up_interruptible(&pgdat->kswapd_wait);
  1336. }
  1337. #ifdef CONFIG_PM
  1338. /*
  1339. * Try to free `nr_pages' of memory, system-wide. Returns the number of freed
  1340. * pages.
  1341. */
  1342. int shrink_all_memory(int nr_pages)
  1343. {
  1344. pg_data_t *pgdat;
  1345. int nr_to_free = nr_pages;
  1346. int ret = 0;
  1347. struct reclaim_state reclaim_state = {
  1348. .reclaimed_slab = 0,
  1349. };
  1350. current->reclaim_state = &reclaim_state;
  1351. for_each_pgdat(pgdat) {
  1352. int freed;
  1353. freed = balance_pgdat(pgdat, nr_to_free, 0);
  1354. ret += freed;
  1355. nr_to_free -= freed;
  1356. if (nr_to_free <= 0)
  1357. break;
  1358. }
  1359. current->reclaim_state = NULL;
  1360. return ret;
  1361. }
  1362. #endif
  1363. #ifdef CONFIG_HOTPLUG_CPU
  1364. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  1365. not required for correctness. So if the last cpu in a node goes
  1366. away, we get changed to run anywhere: as the first one comes back,
  1367. restore their cpu bindings. */
  1368. static int __devinit cpu_callback(struct notifier_block *nfb,
  1369. unsigned long action,
  1370. void *hcpu)
  1371. {
  1372. pg_data_t *pgdat;
  1373. cpumask_t mask;
  1374. if (action == CPU_ONLINE) {
  1375. for_each_pgdat(pgdat) {
  1376. mask = node_to_cpumask(pgdat->node_id);
  1377. if (any_online_cpu(mask) != NR_CPUS)
  1378. /* One of our CPUs online: restore mask */
  1379. set_cpus_allowed(pgdat->kswapd, mask);
  1380. }
  1381. }
  1382. return NOTIFY_OK;
  1383. }
  1384. #endif /* CONFIG_HOTPLUG_CPU */
  1385. static int __init kswapd_init(void)
  1386. {
  1387. pg_data_t *pgdat;
  1388. swap_setup();
  1389. for_each_pgdat(pgdat)
  1390. pgdat->kswapd
  1391. = find_task_by_pid(kernel_thread(kswapd, pgdat, CLONE_KERNEL));
  1392. total_memory = nr_free_pagecache_pages();
  1393. hotcpu_notifier(cpu_callback, 0);
  1394. return 0;
  1395. }
  1396. module_init(kswapd_init)