vmscan.c 42 KB

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