vmscan.c 47 KB

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