vmscan.c 44 KB

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