vmscan.c 41 KB

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