vmscan.c 50 KB

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