vmscan.c 40 KB

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