vmscan.c 44 KB

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