vmscan.c 37 KB

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