vmscan.c 46 KB

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