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. count_vm_events(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. count_vm_events(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. __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan);
  581. __count_vm_events(KSWAPD_STEAL, nr_freed);
  582. } else
  583. __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan);
  584. __count_vm_events(PGACTIVATE, 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. __count_zone_vm_events(PGREFILL, zone, pgscanned);
  750. __count_vm_events(PGDEACTIVATE, pgdeactivate);
  751. spin_unlock_irq(&zone->lru_lock);
  752. pagevec_release(&pvec);
  753. }
  754. /*
  755. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  756. */
  757. static unsigned long shrink_zone(int priority, struct zone *zone,
  758. struct scan_control *sc)
  759. {
  760. unsigned long nr_active;
  761. unsigned long nr_inactive;
  762. unsigned long nr_to_scan;
  763. unsigned long nr_reclaimed = 0;
  764. atomic_inc(&zone->reclaim_in_progress);
  765. /*
  766. * Add one to `nr_to_scan' just to make sure that the kernel will
  767. * slowly sift through the active list.
  768. */
  769. zone->nr_scan_active += (zone->nr_active >> priority) + 1;
  770. nr_active = zone->nr_scan_active;
  771. if (nr_active >= sc->swap_cluster_max)
  772. zone->nr_scan_active = 0;
  773. else
  774. nr_active = 0;
  775. zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1;
  776. nr_inactive = zone->nr_scan_inactive;
  777. if (nr_inactive >= sc->swap_cluster_max)
  778. zone->nr_scan_inactive = 0;
  779. else
  780. nr_inactive = 0;
  781. while (nr_active || nr_inactive) {
  782. if (nr_active) {
  783. nr_to_scan = min(nr_active,
  784. (unsigned long)sc->swap_cluster_max);
  785. nr_active -= nr_to_scan;
  786. shrink_active_list(nr_to_scan, zone, sc);
  787. }
  788. if (nr_inactive) {
  789. nr_to_scan = min(nr_inactive,
  790. (unsigned long)sc->swap_cluster_max);
  791. nr_inactive -= nr_to_scan;
  792. nr_reclaimed += shrink_inactive_list(nr_to_scan, zone,
  793. sc);
  794. }
  795. }
  796. throttle_vm_writeout();
  797. atomic_dec(&zone->reclaim_in_progress);
  798. return nr_reclaimed;
  799. }
  800. /*
  801. * This is the direct reclaim path, for page-allocating processes. We only
  802. * try to reclaim pages from zones which will satisfy the caller's allocation
  803. * request.
  804. *
  805. * We reclaim from a zone even if that zone is over pages_high. Because:
  806. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  807. * allocation or
  808. * b) The zones may be over pages_high but they must go *over* pages_high to
  809. * satisfy the `incremental min' zone defense algorithm.
  810. *
  811. * Returns the number of reclaimed pages.
  812. *
  813. * If a zone is deemed to be full of pinned pages then just give it a light
  814. * scan then give up on it.
  815. */
  816. static unsigned long shrink_zones(int priority, struct zone **zones,
  817. struct scan_control *sc)
  818. {
  819. unsigned long nr_reclaimed = 0;
  820. int i;
  821. for (i = 0; zones[i] != NULL; i++) {
  822. struct zone *zone = zones[i];
  823. if (!populated_zone(zone))
  824. continue;
  825. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  826. continue;
  827. zone->temp_priority = priority;
  828. if (zone->prev_priority > priority)
  829. zone->prev_priority = priority;
  830. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  831. continue; /* Let kswapd poll it */
  832. nr_reclaimed += shrink_zone(priority, zone, sc);
  833. }
  834. return nr_reclaimed;
  835. }
  836. /*
  837. * This is the main entry point to direct page reclaim.
  838. *
  839. * If a full scan of the inactive list fails to free enough memory then we
  840. * are "out of memory" and something needs to be killed.
  841. *
  842. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  843. * high - the zone may be full of dirty or under-writeback pages, which this
  844. * caller can't do much about. We kick pdflush and take explicit naps in the
  845. * hope that some of these pages can be written. But if the allocating task
  846. * holds filesystem locks which prevent writeout this might not work, and the
  847. * allocation attempt will fail.
  848. */
  849. unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
  850. {
  851. int priority;
  852. int ret = 0;
  853. unsigned long total_scanned = 0;
  854. unsigned long nr_reclaimed = 0;
  855. struct reclaim_state *reclaim_state = current->reclaim_state;
  856. unsigned long lru_pages = 0;
  857. int i;
  858. struct scan_control sc = {
  859. .gfp_mask = gfp_mask,
  860. .may_writepage = !laptop_mode,
  861. .swap_cluster_max = SWAP_CLUSTER_MAX,
  862. .may_swap = 1,
  863. .swappiness = vm_swappiness,
  864. };
  865. count_vm_event(ALLOCSTALL);
  866. for (i = 0; zones[i] != NULL; i++) {
  867. struct zone *zone = zones[i];
  868. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  869. continue;
  870. zone->temp_priority = DEF_PRIORITY;
  871. lru_pages += zone->nr_active + zone->nr_inactive;
  872. }
  873. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  874. sc.nr_scanned = 0;
  875. if (!priority)
  876. disable_swap_token();
  877. nr_reclaimed += shrink_zones(priority, zones, &sc);
  878. shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
  879. if (reclaim_state) {
  880. nr_reclaimed += reclaim_state->reclaimed_slab;
  881. reclaim_state->reclaimed_slab = 0;
  882. }
  883. total_scanned += sc.nr_scanned;
  884. if (nr_reclaimed >= sc.swap_cluster_max) {
  885. ret = 1;
  886. goto out;
  887. }
  888. /*
  889. * Try to write back as many pages as we just scanned. This
  890. * tends to cause slow streaming writers to write data to the
  891. * disk smoothly, at the dirtying rate, which is nice. But
  892. * that's undesirable in laptop mode, where we *want* lumpy
  893. * writeout. So in laptop mode, write out the whole world.
  894. */
  895. if (total_scanned > sc.swap_cluster_max +
  896. sc.swap_cluster_max / 2) {
  897. wakeup_pdflush(laptop_mode ? 0 : total_scanned);
  898. sc.may_writepage = 1;
  899. }
  900. /* Take a nap, wait for some writeback to complete */
  901. if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
  902. blk_congestion_wait(WRITE, HZ/10);
  903. }
  904. out:
  905. for (i = 0; zones[i] != 0; i++) {
  906. struct zone *zone = zones[i];
  907. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  908. continue;
  909. zone->prev_priority = zone->temp_priority;
  910. }
  911. return ret;
  912. }
  913. /*
  914. * For kswapd, balance_pgdat() will work across all this node's zones until
  915. * they are all at pages_high.
  916. *
  917. * Returns the number of pages which were actually freed.
  918. *
  919. * There is special handling here for zones which are full of pinned pages.
  920. * This can happen if the pages are all mlocked, or if they are all used by
  921. * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
  922. * What we do is to detect the case where all pages in the zone have been
  923. * scanned twice and there has been zero successful reclaim. Mark the zone as
  924. * dead and from now on, only perform a short scan. Basically we're polling
  925. * the zone for when the problem goes away.
  926. *
  927. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  928. * zones which have free_pages > pages_high, but once a zone is found to have
  929. * free_pages <= pages_high, we scan that zone and the lower zones regardless
  930. * of the number of free pages in the lower zones. This interoperates with
  931. * the page allocator fallback scheme to ensure that aging of pages is balanced
  932. * across the zones.
  933. */
  934. static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
  935. {
  936. int all_zones_ok;
  937. int priority;
  938. int i;
  939. unsigned long total_scanned;
  940. unsigned long nr_reclaimed;
  941. struct reclaim_state *reclaim_state = current->reclaim_state;
  942. struct scan_control sc = {
  943. .gfp_mask = GFP_KERNEL,
  944. .may_swap = 1,
  945. .swap_cluster_max = SWAP_CLUSTER_MAX,
  946. .swappiness = vm_swappiness,
  947. };
  948. loop_again:
  949. total_scanned = 0;
  950. nr_reclaimed = 0;
  951. sc.may_writepage = !laptop_mode;
  952. count_vm_event(PAGEOUTRUN);
  953. for (i = 0; i < pgdat->nr_zones; i++) {
  954. struct zone *zone = pgdat->node_zones + i;
  955. zone->temp_priority = DEF_PRIORITY;
  956. }
  957. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  958. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  959. unsigned long lru_pages = 0;
  960. /* The swap token gets in the way of swapout... */
  961. if (!priority)
  962. disable_swap_token();
  963. all_zones_ok = 1;
  964. /*
  965. * Scan in the highmem->dma direction for the highest
  966. * zone which needs scanning
  967. */
  968. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  969. struct zone *zone = pgdat->node_zones + i;
  970. if (!populated_zone(zone))
  971. continue;
  972. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  973. continue;
  974. if (!zone_watermark_ok(zone, order, zone->pages_high,
  975. 0, 0)) {
  976. end_zone = i;
  977. goto scan;
  978. }
  979. }
  980. goto out;
  981. scan:
  982. for (i = 0; i <= end_zone; i++) {
  983. struct zone *zone = pgdat->node_zones + i;
  984. lru_pages += zone->nr_active + zone->nr_inactive;
  985. }
  986. /*
  987. * Now scan the zone in the dma->highmem direction, stopping
  988. * at the last zone which needs scanning.
  989. *
  990. * We do this because the page allocator works in the opposite
  991. * direction. This prevents the page allocator from allocating
  992. * pages behind kswapd's direction of progress, which would
  993. * cause too much scanning of the lower zones.
  994. */
  995. for (i = 0; i <= end_zone; i++) {
  996. struct zone *zone = pgdat->node_zones + i;
  997. int nr_slab;
  998. if (!populated_zone(zone))
  999. continue;
  1000. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1001. continue;
  1002. if (!zone_watermark_ok(zone, order, zone->pages_high,
  1003. end_zone, 0))
  1004. all_zones_ok = 0;
  1005. zone->temp_priority = priority;
  1006. if (zone->prev_priority > priority)
  1007. zone->prev_priority = priority;
  1008. sc.nr_scanned = 0;
  1009. nr_reclaimed += shrink_zone(priority, zone, &sc);
  1010. reclaim_state->reclaimed_slab = 0;
  1011. nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
  1012. lru_pages);
  1013. nr_reclaimed += reclaim_state->reclaimed_slab;
  1014. total_scanned += sc.nr_scanned;
  1015. if (zone->all_unreclaimable)
  1016. continue;
  1017. if (nr_slab == 0 && zone->pages_scanned >=
  1018. (zone->nr_active + zone->nr_inactive) * 4)
  1019. zone->all_unreclaimable = 1;
  1020. /*
  1021. * If we've done a decent amount of scanning and
  1022. * the reclaim ratio is low, start doing writepage
  1023. * even in laptop mode
  1024. */
  1025. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  1026. total_scanned > nr_reclaimed + nr_reclaimed / 2)
  1027. sc.may_writepage = 1;
  1028. }
  1029. if (all_zones_ok)
  1030. break; /* kswapd: all done */
  1031. /*
  1032. * OK, kswapd is getting into trouble. Take a nap, then take
  1033. * another pass across the zones.
  1034. */
  1035. if (total_scanned && priority < DEF_PRIORITY - 2)
  1036. blk_congestion_wait(WRITE, HZ/10);
  1037. /*
  1038. * We do this so kswapd doesn't build up large priorities for
  1039. * example when it is freeing in parallel with allocators. It
  1040. * matches the direct reclaim path behaviour in terms of impact
  1041. * on zone->*_priority.
  1042. */
  1043. if (nr_reclaimed >= SWAP_CLUSTER_MAX)
  1044. break;
  1045. }
  1046. out:
  1047. for (i = 0; i < pgdat->nr_zones; i++) {
  1048. struct zone *zone = pgdat->node_zones + i;
  1049. zone->prev_priority = zone->temp_priority;
  1050. }
  1051. if (!all_zones_ok) {
  1052. cond_resched();
  1053. goto loop_again;
  1054. }
  1055. return nr_reclaimed;
  1056. }
  1057. /*
  1058. * The background pageout daemon, started as a kernel thread
  1059. * from the init process.
  1060. *
  1061. * This basically trickles out pages so that we have _some_
  1062. * free memory available even if there is no other activity
  1063. * that frees anything up. This is needed for things like routing
  1064. * etc, where we otherwise might have all activity going on in
  1065. * asynchronous contexts that cannot page things out.
  1066. *
  1067. * If there are applications that are active memory-allocators
  1068. * (most normal use), this basically shouldn't matter.
  1069. */
  1070. static int kswapd(void *p)
  1071. {
  1072. unsigned long order;
  1073. pg_data_t *pgdat = (pg_data_t*)p;
  1074. struct task_struct *tsk = current;
  1075. DEFINE_WAIT(wait);
  1076. struct reclaim_state reclaim_state = {
  1077. .reclaimed_slab = 0,
  1078. };
  1079. cpumask_t cpumask;
  1080. cpumask = node_to_cpumask(pgdat->node_id);
  1081. if (!cpus_empty(cpumask))
  1082. set_cpus_allowed(tsk, cpumask);
  1083. current->reclaim_state = &reclaim_state;
  1084. /*
  1085. * Tell the memory management that we're a "memory allocator",
  1086. * and that if we need more memory we should get access to it
  1087. * regardless (see "__alloc_pages()"). "kswapd" should
  1088. * never get caught in the normal page freeing logic.
  1089. *
  1090. * (Kswapd normally doesn't need memory anyway, but sometimes
  1091. * you need a small amount of memory in order to be able to
  1092. * page out something else, and this flag essentially protects
  1093. * us from recursively trying to free more memory as we're
  1094. * trying to free the first piece of memory in the first place).
  1095. */
  1096. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  1097. order = 0;
  1098. for ( ; ; ) {
  1099. unsigned long new_order;
  1100. try_to_freeze();
  1101. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  1102. new_order = pgdat->kswapd_max_order;
  1103. pgdat->kswapd_max_order = 0;
  1104. if (order < new_order) {
  1105. /*
  1106. * Don't sleep if someone wants a larger 'order'
  1107. * allocation
  1108. */
  1109. order = new_order;
  1110. } else {
  1111. schedule();
  1112. order = pgdat->kswapd_max_order;
  1113. }
  1114. finish_wait(&pgdat->kswapd_wait, &wait);
  1115. balance_pgdat(pgdat, order);
  1116. }
  1117. return 0;
  1118. }
  1119. /*
  1120. * A zone is low on free memory, so wake its kswapd task to service it.
  1121. */
  1122. void wakeup_kswapd(struct zone *zone, int order)
  1123. {
  1124. pg_data_t *pgdat;
  1125. if (!populated_zone(zone))
  1126. return;
  1127. pgdat = zone->zone_pgdat;
  1128. if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
  1129. return;
  1130. if (pgdat->kswapd_max_order < order)
  1131. pgdat->kswapd_max_order = order;
  1132. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  1133. return;
  1134. if (!waitqueue_active(&pgdat->kswapd_wait))
  1135. return;
  1136. wake_up_interruptible(&pgdat->kswapd_wait);
  1137. }
  1138. #ifdef CONFIG_PM
  1139. /*
  1140. * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages
  1141. * from LRU lists system-wide, for given pass and priority, and returns the
  1142. * number of reclaimed pages
  1143. *
  1144. * For pass > 3 we also try to shrink the LRU lists that contain a few pages
  1145. */
  1146. static unsigned long shrink_all_zones(unsigned long nr_pages, int pass,
  1147. int prio, struct scan_control *sc)
  1148. {
  1149. struct zone *zone;
  1150. unsigned long nr_to_scan, ret = 0;
  1151. for_each_zone(zone) {
  1152. if (!populated_zone(zone))
  1153. continue;
  1154. if (zone->all_unreclaimable && prio != DEF_PRIORITY)
  1155. continue;
  1156. /* For pass = 0 we don't shrink the active list */
  1157. if (pass > 0) {
  1158. zone->nr_scan_active += (zone->nr_active >> prio) + 1;
  1159. if (zone->nr_scan_active >= nr_pages || pass > 3) {
  1160. zone->nr_scan_active = 0;
  1161. nr_to_scan = min(nr_pages, zone->nr_active);
  1162. shrink_active_list(nr_to_scan, zone, sc);
  1163. }
  1164. }
  1165. zone->nr_scan_inactive += (zone->nr_inactive >> prio) + 1;
  1166. if (zone->nr_scan_inactive >= nr_pages || pass > 3) {
  1167. zone->nr_scan_inactive = 0;
  1168. nr_to_scan = min(nr_pages, zone->nr_inactive);
  1169. ret += shrink_inactive_list(nr_to_scan, zone, sc);
  1170. if (ret >= nr_pages)
  1171. return ret;
  1172. }
  1173. }
  1174. return ret;
  1175. }
  1176. /*
  1177. * Try to free `nr_pages' of memory, system-wide, and return the number of
  1178. * freed pages.
  1179. *
  1180. * Rather than trying to age LRUs the aim is to preserve the overall
  1181. * LRU order by reclaiming preferentially
  1182. * inactive > active > active referenced > active mapped
  1183. */
  1184. unsigned long shrink_all_memory(unsigned long nr_pages)
  1185. {
  1186. unsigned long lru_pages, nr_slab;
  1187. unsigned long ret = 0;
  1188. int pass;
  1189. struct reclaim_state reclaim_state;
  1190. struct zone *zone;
  1191. struct scan_control sc = {
  1192. .gfp_mask = GFP_KERNEL,
  1193. .may_swap = 0,
  1194. .swap_cluster_max = nr_pages,
  1195. .may_writepage = 1,
  1196. .swappiness = vm_swappiness,
  1197. };
  1198. current->reclaim_state = &reclaim_state;
  1199. lru_pages = 0;
  1200. for_each_zone(zone)
  1201. lru_pages += zone->nr_active + zone->nr_inactive;
  1202. nr_slab = global_page_state(NR_SLAB);
  1203. /* If slab caches are huge, it's better to hit them first */
  1204. while (nr_slab >= lru_pages) {
  1205. reclaim_state.reclaimed_slab = 0;
  1206. shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
  1207. if (!reclaim_state.reclaimed_slab)
  1208. break;
  1209. ret += reclaim_state.reclaimed_slab;
  1210. if (ret >= nr_pages)
  1211. goto out;
  1212. nr_slab -= reclaim_state.reclaimed_slab;
  1213. }
  1214. /*
  1215. * We try to shrink LRUs in 5 passes:
  1216. * 0 = Reclaim from inactive_list only
  1217. * 1 = Reclaim from active list but don't reclaim mapped
  1218. * 2 = 2nd pass of type 1
  1219. * 3 = Reclaim mapped (normal reclaim)
  1220. * 4 = 2nd pass of type 3
  1221. */
  1222. for (pass = 0; pass < 5; pass++) {
  1223. int prio;
  1224. /* Needed for shrinking slab caches later on */
  1225. if (!lru_pages)
  1226. for_each_zone(zone) {
  1227. lru_pages += zone->nr_active;
  1228. lru_pages += zone->nr_inactive;
  1229. }
  1230. /* Force reclaiming mapped pages in the passes #3 and #4 */
  1231. if (pass > 2) {
  1232. sc.may_swap = 1;
  1233. sc.swappiness = 100;
  1234. }
  1235. for (prio = DEF_PRIORITY; prio >= 0; prio--) {
  1236. unsigned long nr_to_scan = nr_pages - ret;
  1237. sc.nr_scanned = 0;
  1238. ret += shrink_all_zones(nr_to_scan, prio, pass, &sc);
  1239. if (ret >= nr_pages)
  1240. goto out;
  1241. reclaim_state.reclaimed_slab = 0;
  1242. shrink_slab(sc.nr_scanned, sc.gfp_mask, lru_pages);
  1243. ret += reclaim_state.reclaimed_slab;
  1244. if (ret >= nr_pages)
  1245. goto out;
  1246. if (sc.nr_scanned && prio < DEF_PRIORITY - 2)
  1247. blk_congestion_wait(WRITE, HZ / 10);
  1248. }
  1249. lru_pages = 0;
  1250. }
  1251. /*
  1252. * If ret = 0, we could not shrink LRUs, but there may be something
  1253. * in slab caches
  1254. */
  1255. if (!ret)
  1256. do {
  1257. reclaim_state.reclaimed_slab = 0;
  1258. shrink_slab(nr_pages, sc.gfp_mask, lru_pages);
  1259. ret += reclaim_state.reclaimed_slab;
  1260. } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0);
  1261. out:
  1262. current->reclaim_state = NULL;
  1263. return ret;
  1264. }
  1265. #endif
  1266. #ifdef CONFIG_HOTPLUG_CPU
  1267. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  1268. not required for correctness. So if the last cpu in a node goes
  1269. away, we get changed to run anywhere: as the first one comes back,
  1270. restore their cpu bindings. */
  1271. static int __devinit cpu_callback(struct notifier_block *nfb,
  1272. unsigned long action, void *hcpu)
  1273. {
  1274. pg_data_t *pgdat;
  1275. cpumask_t mask;
  1276. if (action == CPU_ONLINE) {
  1277. for_each_online_pgdat(pgdat) {
  1278. mask = node_to_cpumask(pgdat->node_id);
  1279. if (any_online_cpu(mask) != NR_CPUS)
  1280. /* One of our CPUs online: restore mask */
  1281. set_cpus_allowed(pgdat->kswapd, mask);
  1282. }
  1283. }
  1284. return NOTIFY_OK;
  1285. }
  1286. #endif /* CONFIG_HOTPLUG_CPU */
  1287. /*
  1288. * This kswapd start function will be called by init and node-hot-add.
  1289. * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
  1290. */
  1291. int kswapd_run(int nid)
  1292. {
  1293. pg_data_t *pgdat = NODE_DATA(nid);
  1294. int ret = 0;
  1295. if (pgdat->kswapd)
  1296. return 0;
  1297. pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
  1298. if (IS_ERR(pgdat->kswapd)) {
  1299. /* failure at boot is fatal */
  1300. BUG_ON(system_state == SYSTEM_BOOTING);
  1301. printk("Failed to start kswapd on node %d\n",nid);
  1302. ret = -1;
  1303. }
  1304. return ret;
  1305. }
  1306. static int __init kswapd_init(void)
  1307. {
  1308. int nid;
  1309. swap_setup();
  1310. for_each_online_node(nid)
  1311. kswapd_run(nid);
  1312. hotcpu_notifier(cpu_callback, 0);
  1313. return 0;
  1314. }
  1315. module_init(kswapd_init)
  1316. #ifdef CONFIG_NUMA
  1317. /*
  1318. * Zone reclaim mode
  1319. *
  1320. * If non-zero call zone_reclaim when the number of free pages falls below
  1321. * the watermarks.
  1322. */
  1323. int zone_reclaim_mode __read_mostly;
  1324. #define RECLAIM_OFF 0
  1325. #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */
  1326. #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
  1327. #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */
  1328. #define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */
  1329. /*
  1330. * Priority for ZONE_RECLAIM. This determines the fraction of pages
  1331. * of a node considered for each zone_reclaim. 4 scans 1/16th of
  1332. * a zone.
  1333. */
  1334. #define ZONE_RECLAIM_PRIORITY 4
  1335. /*
  1336. * Percentage of pages in a zone that must be unmapped for zone_reclaim to
  1337. * occur.
  1338. */
  1339. int sysctl_min_unmapped_ratio = 1;
  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. * Zone reclaim reclaims unmapped file backed pages.
  1402. *
  1403. * A small portion of unmapped file backed pages is needed for
  1404. * file I/O otherwise pages read by file I/O will be immediately
  1405. * thrown out if the zone is overallocated. So we do not reclaim
  1406. * if less than a specified percentage of the zone is used by
  1407. * unmapped file backed pages.
  1408. */
  1409. if (zone_page_state(zone, NR_FILE_PAGES) -
  1410. zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_ratio)
  1411. return 0;
  1412. /*
  1413. * Avoid concurrent zone reclaims, do not reclaim in a zone that does
  1414. * not have reclaimable pages and if we should not delay the allocation
  1415. * then do not scan.
  1416. */
  1417. if (!(gfp_mask & __GFP_WAIT) ||
  1418. zone->all_unreclaimable ||
  1419. atomic_read(&zone->reclaim_in_progress) > 0 ||
  1420. (current->flags & PF_MEMALLOC))
  1421. return 0;
  1422. /*
  1423. * Only run zone reclaim on the local zone or on zones that do not
  1424. * have associated processors. This will favor the local processor
  1425. * over remote processors and spread off node memory allocations
  1426. * as wide as possible.
  1427. */
  1428. node_id = zone->zone_pgdat->node_id;
  1429. mask = node_to_cpumask(node_id);
  1430. if (!cpus_empty(mask) && node_id != numa_node_id())
  1431. return 0;
  1432. return __zone_reclaim(zone, gfp_mask, order);
  1433. }
  1434. #endif