vmscan.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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. /*
  331. * shrink_list adds the number of reclaimed pages to sc->nr_reclaimed
  332. */
  333. static int shrink_list(struct list_head *page_list, struct scan_control *sc)
  334. {
  335. LIST_HEAD(ret_pages);
  336. struct pagevec freed_pvec;
  337. int pgactivate = 0;
  338. int reclaimed = 0;
  339. cond_resched();
  340. pagevec_init(&freed_pvec, 1);
  341. while (!list_empty(page_list)) {
  342. struct address_space *mapping;
  343. struct page *page;
  344. int may_enter_fs;
  345. int referenced;
  346. cond_resched();
  347. page = lru_to_page(page_list);
  348. list_del(&page->lru);
  349. if (TestSetPageLocked(page))
  350. goto keep;
  351. BUG_ON(PageActive(page));
  352. sc->nr_scanned++;
  353. /* Double the slab pressure for mapped and swapcache pages */
  354. if (page_mapped(page) || PageSwapCache(page))
  355. sc->nr_scanned++;
  356. if (PageWriteback(page))
  357. goto keep_locked;
  358. referenced = page_referenced(page, 1);
  359. /* In active use or really unfreeable? Activate it. */
  360. if (referenced && page_mapping_inuse(page))
  361. goto activate_locked;
  362. #ifdef CONFIG_SWAP
  363. /*
  364. * Anonymous process memory has backing store?
  365. * Try to allocate it some swap space here.
  366. */
  367. if (PageAnon(page) && !PageSwapCache(page)) {
  368. if (!add_to_swap(page))
  369. goto activate_locked;
  370. }
  371. #endif /* CONFIG_SWAP */
  372. mapping = page_mapping(page);
  373. may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
  374. (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
  375. /*
  376. * The page is mapped into the page tables of one or more
  377. * processes. Try to unmap it here.
  378. */
  379. if (page_mapped(page) && mapping) {
  380. switch (try_to_unmap(page)) {
  381. case SWAP_FAIL:
  382. goto activate_locked;
  383. case SWAP_AGAIN:
  384. goto keep_locked;
  385. case SWAP_SUCCESS:
  386. ; /* try to free the page below */
  387. }
  388. }
  389. if (PageDirty(page)) {
  390. if (referenced)
  391. goto keep_locked;
  392. if (!may_enter_fs)
  393. goto keep_locked;
  394. if (laptop_mode && !sc->may_writepage)
  395. goto keep_locked;
  396. /* Page is dirty, try to write it out here */
  397. switch(pageout(page, mapping)) {
  398. case PAGE_KEEP:
  399. goto keep_locked;
  400. case PAGE_ACTIVATE:
  401. goto activate_locked;
  402. case PAGE_SUCCESS:
  403. if (PageWriteback(page) || PageDirty(page))
  404. goto keep;
  405. /*
  406. * A synchronous write - probably a ramdisk. Go
  407. * ahead and try to reclaim the page.
  408. */
  409. if (TestSetPageLocked(page))
  410. goto keep;
  411. if (PageDirty(page) || PageWriteback(page))
  412. goto keep_locked;
  413. mapping = page_mapping(page);
  414. case PAGE_CLEAN:
  415. ; /* try to free the page below */
  416. }
  417. }
  418. /*
  419. * If the page has buffers, try to free the buffer mappings
  420. * associated with this page. If we succeed we try to free
  421. * the page as well.
  422. *
  423. * We do this even if the page is PageDirty().
  424. * try_to_release_page() does not perform I/O, but it is
  425. * possible for a page to have PageDirty set, but it is actually
  426. * clean (all its buffers are clean). This happens if the
  427. * buffers were written out directly, with submit_bh(). ext3
  428. * will do this, as well as the blockdev mapping.
  429. * try_to_release_page() will discover that cleanness and will
  430. * drop the buffers and mark the page clean - it can be freed.
  431. *
  432. * Rarely, pages can have buffers and no ->mapping. These are
  433. * the pages which were not successfully invalidated in
  434. * truncate_complete_page(). We try to drop those buffers here
  435. * and if that worked, and the page is no longer mapped into
  436. * process address space (page_count == 1) it can be freed.
  437. * Otherwise, leave the page on the LRU so it is swappable.
  438. */
  439. if (PagePrivate(page)) {
  440. if (!try_to_release_page(page, sc->gfp_mask))
  441. goto activate_locked;
  442. if (!mapping && page_count(page) == 1)
  443. goto free_it;
  444. }
  445. if (!mapping)
  446. goto keep_locked; /* truncate got there first */
  447. write_lock_irq(&mapping->tree_lock);
  448. /*
  449. * The non-racy check for busy page. It is critical to check
  450. * PageDirty _after_ making sure that the page is freeable and
  451. * not in use by anybody. (pagecache + us == 2)
  452. */
  453. if (unlikely(page_count(page) != 2))
  454. goto cannot_free;
  455. smp_rmb();
  456. if (unlikely(PageDirty(page)))
  457. goto cannot_free;
  458. #ifdef CONFIG_SWAP
  459. if (PageSwapCache(page)) {
  460. swp_entry_t swap = { .val = page_private(page) };
  461. __delete_from_swap_cache(page);
  462. write_unlock_irq(&mapping->tree_lock);
  463. swap_free(swap);
  464. __put_page(page); /* The pagecache ref */
  465. goto free_it;
  466. }
  467. #endif /* CONFIG_SWAP */
  468. __remove_from_page_cache(page);
  469. write_unlock_irq(&mapping->tree_lock);
  470. __put_page(page);
  471. free_it:
  472. unlock_page(page);
  473. reclaimed++;
  474. if (!pagevec_add(&freed_pvec, page))
  475. __pagevec_release_nonlru(&freed_pvec);
  476. continue;
  477. cannot_free:
  478. write_unlock_irq(&mapping->tree_lock);
  479. goto keep_locked;
  480. activate_locked:
  481. SetPageActive(page);
  482. pgactivate++;
  483. keep_locked:
  484. unlock_page(page);
  485. keep:
  486. list_add(&page->lru, &ret_pages);
  487. BUG_ON(PageLRU(page));
  488. }
  489. list_splice(&ret_pages, page_list);
  490. if (pagevec_count(&freed_pvec))
  491. __pagevec_release_nonlru(&freed_pvec);
  492. mod_page_state(pgactivate, pgactivate);
  493. sc->nr_reclaimed += reclaimed;
  494. return reclaimed;
  495. }
  496. /*
  497. * zone->lru_lock is heavily contended. Some of the functions that
  498. * shrink the lists perform better by taking out a batch of pages
  499. * and working on them outside the LRU lock.
  500. *
  501. * For pagecache intensive workloads, this function is the hottest
  502. * spot in the kernel (apart from copy_*_user functions).
  503. *
  504. * Appropriate locks must be held before calling this function.
  505. *
  506. * @nr_to_scan: The number of pages to look through on the list.
  507. * @src: The LRU list to pull pages off.
  508. * @dst: The temp list to put pages on to.
  509. * @scanned: The number of pages that were scanned.
  510. *
  511. * returns how many pages were moved onto *@dst.
  512. */
  513. static int isolate_lru_pages(int nr_to_scan, struct list_head *src,
  514. struct list_head *dst, int *scanned)
  515. {
  516. int nr_taken = 0;
  517. struct page *page;
  518. int scan = 0;
  519. while (scan++ < nr_to_scan && !list_empty(src)) {
  520. page = lru_to_page(src);
  521. prefetchw_prev_lru_page(page, src, flags);
  522. switch (__isolate_lru_page(page)) {
  523. case 1:
  524. /* Succeeded to isolate page */
  525. list_move(&page->lru, dst);
  526. nr_taken++;
  527. break;
  528. case -ENOENT:
  529. /* Not possible to isolate */
  530. list_move(&page->lru, src);
  531. break;
  532. default:
  533. BUG();
  534. }
  535. }
  536. *scanned = scan;
  537. return nr_taken;
  538. }
  539. static void lru_add_drain_per_cpu(void *dummy)
  540. {
  541. lru_add_drain();
  542. }
  543. /*
  544. * Isolate one page from the LRU lists and put it on the
  545. * indicated list. Do necessary cache draining if the
  546. * page is not on the LRU lists yet.
  547. *
  548. * Result:
  549. * 0 = page not on LRU list
  550. * 1 = page removed from LRU list and added to the specified list.
  551. * -ENOENT = page is being freed elsewhere.
  552. */
  553. int isolate_lru_page(struct page *page)
  554. {
  555. int rc = 0;
  556. struct zone *zone = page_zone(page);
  557. redo:
  558. spin_lock_irq(&zone->lru_lock);
  559. rc = __isolate_lru_page(page);
  560. if (rc == 1) {
  561. if (PageActive(page))
  562. del_page_from_active_list(zone, page);
  563. else
  564. del_page_from_inactive_list(zone, page);
  565. }
  566. spin_unlock_irq(&zone->lru_lock);
  567. if (rc == 0) {
  568. /*
  569. * Maybe this page is still waiting for a cpu to drain it
  570. * from one of the lru lists?
  571. */
  572. rc = schedule_on_each_cpu(lru_add_drain_per_cpu, NULL);
  573. if (rc == 0 && PageLRU(page))
  574. goto redo;
  575. }
  576. return rc;
  577. }
  578. /*
  579. * shrink_cache() adds the number of pages reclaimed to sc->nr_reclaimed
  580. */
  581. static void shrink_cache(struct zone *zone, struct scan_control *sc)
  582. {
  583. LIST_HEAD(page_list);
  584. struct pagevec pvec;
  585. int max_scan = sc->nr_to_scan;
  586. pagevec_init(&pvec, 1);
  587. lru_add_drain();
  588. spin_lock_irq(&zone->lru_lock);
  589. while (max_scan > 0) {
  590. struct page *page;
  591. int nr_taken;
  592. int nr_scan;
  593. int nr_freed;
  594. nr_taken = isolate_lru_pages(sc->swap_cluster_max,
  595. &zone->inactive_list,
  596. &page_list, &nr_scan);
  597. zone->nr_inactive -= nr_taken;
  598. zone->pages_scanned += nr_scan;
  599. spin_unlock_irq(&zone->lru_lock);
  600. if (nr_taken == 0)
  601. goto done;
  602. max_scan -= nr_scan;
  603. nr_freed = shrink_list(&page_list, sc);
  604. local_irq_disable();
  605. if (current_is_kswapd()) {
  606. __mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
  607. __mod_page_state(kswapd_steal, nr_freed);
  608. } else
  609. __mod_page_state_zone(zone, pgscan_direct, nr_scan);
  610. __mod_page_state_zone(zone, pgsteal, nr_freed);
  611. spin_lock(&zone->lru_lock);
  612. /*
  613. * Put back any unfreeable pages.
  614. */
  615. while (!list_empty(&page_list)) {
  616. page = lru_to_page(&page_list);
  617. if (TestSetPageLRU(page))
  618. BUG();
  619. list_del(&page->lru);
  620. if (PageActive(page))
  621. add_page_to_active_list(zone, page);
  622. else
  623. add_page_to_inactive_list(zone, page);
  624. if (!pagevec_add(&pvec, page)) {
  625. spin_unlock_irq(&zone->lru_lock);
  626. __pagevec_release(&pvec);
  627. spin_lock_irq(&zone->lru_lock);
  628. }
  629. }
  630. }
  631. spin_unlock_irq(&zone->lru_lock);
  632. done:
  633. pagevec_release(&pvec);
  634. }
  635. static inline void move_to_lru(struct page *page)
  636. {
  637. list_del(&page->lru);
  638. if (PageActive(page)) {
  639. /*
  640. * lru_cache_add_active checks that
  641. * the PG_active bit is off.
  642. */
  643. ClearPageActive(page);
  644. lru_cache_add_active(page);
  645. } else {
  646. lru_cache_add(page);
  647. }
  648. put_page(page);
  649. }
  650. /*
  651. * Add isolated pages on the list back to the LRU
  652. *
  653. * returns the number of pages put back.
  654. */
  655. int putback_lru_pages(struct list_head *l)
  656. {
  657. struct page *page;
  658. struct page *page2;
  659. int count = 0;
  660. list_for_each_entry_safe(page, page2, l, lru) {
  661. move_to_lru(page);
  662. count++;
  663. }
  664. return count;
  665. }
  666. /*
  667. * This moves pages from the active list to the inactive list.
  668. *
  669. * We move them the other way if the page is referenced by one or more
  670. * processes, from rmap.
  671. *
  672. * If the pages are mostly unmapped, the processing is fast and it is
  673. * appropriate to hold zone->lru_lock across the whole operation. But if
  674. * the pages are mapped, the processing is slow (page_referenced()) so we
  675. * should drop zone->lru_lock around each page. It's impossible to balance
  676. * this, so instead we remove the pages from the LRU while processing them.
  677. * It is safe to rely on PG_active against the non-LRU pages in here because
  678. * nobody will play with that bit on a non-LRU page.
  679. *
  680. * The downside is that we have to touch page->_count against each page.
  681. * But we had to alter page->flags anyway.
  682. */
  683. static void
  684. refill_inactive_zone(struct zone *zone, struct scan_control *sc)
  685. {
  686. int pgmoved;
  687. int pgdeactivate = 0;
  688. int pgscanned;
  689. int nr_pages = sc->nr_to_scan;
  690. LIST_HEAD(l_hold); /* The pages which were snipped off */
  691. LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
  692. LIST_HEAD(l_active); /* Pages to go onto the active_list */
  693. struct page *page;
  694. struct pagevec pvec;
  695. int reclaim_mapped = 0;
  696. long mapped_ratio;
  697. long distress;
  698. long swap_tendency;
  699. lru_add_drain();
  700. spin_lock_irq(&zone->lru_lock);
  701. pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
  702. &l_hold, &pgscanned);
  703. zone->pages_scanned += pgscanned;
  704. zone->nr_active -= pgmoved;
  705. spin_unlock_irq(&zone->lru_lock);
  706. /*
  707. * `distress' is a measure of how much trouble we're having reclaiming
  708. * pages. 0 -> no problems. 100 -> great trouble.
  709. */
  710. distress = 100 >> zone->prev_priority;
  711. /*
  712. * The point of this algorithm is to decide when to start reclaiming
  713. * mapped memory instead of just pagecache. Work out how much memory
  714. * is mapped.
  715. */
  716. mapped_ratio = (sc->nr_mapped * 100) / total_memory;
  717. /*
  718. * Now decide how much we really want to unmap some pages. The mapped
  719. * ratio is downgraded - just because there's a lot of mapped memory
  720. * doesn't necessarily mean that page reclaim isn't succeeding.
  721. *
  722. * The distress ratio is important - we don't want to start going oom.
  723. *
  724. * A 100% value of vm_swappiness overrides this algorithm altogether.
  725. */
  726. swap_tendency = mapped_ratio / 2 + distress + vm_swappiness;
  727. /*
  728. * Now use this metric to decide whether to start moving mapped memory
  729. * onto the inactive list.
  730. */
  731. if (swap_tendency >= 100)
  732. reclaim_mapped = 1;
  733. while (!list_empty(&l_hold)) {
  734. cond_resched();
  735. page = lru_to_page(&l_hold);
  736. list_del(&page->lru);
  737. if (page_mapped(page)) {
  738. if (!reclaim_mapped ||
  739. (total_swap_pages == 0 && PageAnon(page)) ||
  740. page_referenced(page, 0)) {
  741. list_add(&page->lru, &l_active);
  742. continue;
  743. }
  744. }
  745. list_add(&page->lru, &l_inactive);
  746. }
  747. pagevec_init(&pvec, 1);
  748. pgmoved = 0;
  749. spin_lock_irq(&zone->lru_lock);
  750. while (!list_empty(&l_inactive)) {
  751. page = lru_to_page(&l_inactive);
  752. prefetchw_prev_lru_page(page, &l_inactive, flags);
  753. if (TestSetPageLRU(page))
  754. BUG();
  755. if (!TestClearPageActive(page))
  756. BUG();
  757. list_move(&page->lru, &zone->inactive_list);
  758. pgmoved++;
  759. if (!pagevec_add(&pvec, page)) {
  760. zone->nr_inactive += pgmoved;
  761. spin_unlock_irq(&zone->lru_lock);
  762. pgdeactivate += pgmoved;
  763. pgmoved = 0;
  764. if (buffer_heads_over_limit)
  765. pagevec_strip(&pvec);
  766. __pagevec_release(&pvec);
  767. spin_lock_irq(&zone->lru_lock);
  768. }
  769. }
  770. zone->nr_inactive += pgmoved;
  771. pgdeactivate += pgmoved;
  772. if (buffer_heads_over_limit) {
  773. spin_unlock_irq(&zone->lru_lock);
  774. pagevec_strip(&pvec);
  775. spin_lock_irq(&zone->lru_lock);
  776. }
  777. pgmoved = 0;
  778. while (!list_empty(&l_active)) {
  779. page = lru_to_page(&l_active);
  780. prefetchw_prev_lru_page(page, &l_active, flags);
  781. if (TestSetPageLRU(page))
  782. BUG();
  783. BUG_ON(!PageActive(page));
  784. list_move(&page->lru, &zone->active_list);
  785. pgmoved++;
  786. if (!pagevec_add(&pvec, page)) {
  787. zone->nr_active += pgmoved;
  788. pgmoved = 0;
  789. spin_unlock_irq(&zone->lru_lock);
  790. __pagevec_release(&pvec);
  791. spin_lock_irq(&zone->lru_lock);
  792. }
  793. }
  794. zone->nr_active += pgmoved;
  795. spin_unlock(&zone->lru_lock);
  796. __mod_page_state_zone(zone, pgrefill, pgscanned);
  797. __mod_page_state(pgdeactivate, pgdeactivate);
  798. local_irq_enable();
  799. pagevec_release(&pvec);
  800. }
  801. /*
  802. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  803. */
  804. static void
  805. shrink_zone(struct zone *zone, struct scan_control *sc)
  806. {
  807. unsigned long nr_active;
  808. unsigned long nr_inactive;
  809. atomic_inc(&zone->reclaim_in_progress);
  810. /*
  811. * Add one to `nr_to_scan' just to make sure that the kernel will
  812. * slowly sift through the active list.
  813. */
  814. zone->nr_scan_active += (zone->nr_active >> sc->priority) + 1;
  815. nr_active = zone->nr_scan_active;
  816. if (nr_active >= sc->swap_cluster_max)
  817. zone->nr_scan_active = 0;
  818. else
  819. nr_active = 0;
  820. zone->nr_scan_inactive += (zone->nr_inactive >> sc->priority) + 1;
  821. nr_inactive = zone->nr_scan_inactive;
  822. if (nr_inactive >= sc->swap_cluster_max)
  823. zone->nr_scan_inactive = 0;
  824. else
  825. nr_inactive = 0;
  826. while (nr_active || nr_inactive) {
  827. if (nr_active) {
  828. sc->nr_to_scan = min(nr_active,
  829. (unsigned long)sc->swap_cluster_max);
  830. nr_active -= sc->nr_to_scan;
  831. refill_inactive_zone(zone, sc);
  832. }
  833. if (nr_inactive) {
  834. sc->nr_to_scan = min(nr_inactive,
  835. (unsigned long)sc->swap_cluster_max);
  836. nr_inactive -= sc->nr_to_scan;
  837. shrink_cache(zone, sc);
  838. }
  839. }
  840. throttle_vm_writeout();
  841. atomic_dec(&zone->reclaim_in_progress);
  842. }
  843. /*
  844. * This is the direct reclaim path, for page-allocating processes. We only
  845. * try to reclaim pages from zones which will satisfy the caller's allocation
  846. * request.
  847. *
  848. * We reclaim from a zone even if that zone is over pages_high. Because:
  849. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  850. * allocation or
  851. * b) The zones may be over pages_high but they must go *over* pages_high to
  852. * satisfy the `incremental min' zone defense algorithm.
  853. *
  854. * Returns the number of reclaimed pages.
  855. *
  856. * If a zone is deemed to be full of pinned pages then just give it a light
  857. * scan then give up on it.
  858. */
  859. static void
  860. shrink_caches(struct zone **zones, struct scan_control *sc)
  861. {
  862. int i;
  863. for (i = 0; zones[i] != NULL; i++) {
  864. struct zone *zone = zones[i];
  865. if (!populated_zone(zone))
  866. continue;
  867. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  868. continue;
  869. zone->temp_priority = sc->priority;
  870. if (zone->prev_priority > sc->priority)
  871. zone->prev_priority = sc->priority;
  872. if (zone->all_unreclaimable && sc->priority != DEF_PRIORITY)
  873. continue; /* Let kswapd poll it */
  874. shrink_zone(zone, sc);
  875. }
  876. }
  877. /*
  878. * This is the main entry point to direct page reclaim.
  879. *
  880. * If a full scan of the inactive list fails to free enough memory then we
  881. * are "out of memory" and something needs to be killed.
  882. *
  883. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  884. * high - the zone may be full of dirty or under-writeback pages, which this
  885. * caller can't do much about. We kick pdflush and take explicit naps in the
  886. * hope that some of these pages can be written. But if the allocating task
  887. * holds filesystem locks which prevent writeout this might not work, and the
  888. * allocation attempt will fail.
  889. */
  890. int try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
  891. {
  892. int priority;
  893. int ret = 0;
  894. int total_scanned = 0, total_reclaimed = 0;
  895. struct reclaim_state *reclaim_state = current->reclaim_state;
  896. struct scan_control sc;
  897. unsigned long lru_pages = 0;
  898. int i;
  899. sc.gfp_mask = gfp_mask;
  900. sc.may_writepage = 0;
  901. inc_page_state(allocstall);
  902. for (i = 0; zones[i] != NULL; i++) {
  903. struct zone *zone = zones[i];
  904. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  905. continue;
  906. zone->temp_priority = DEF_PRIORITY;
  907. lru_pages += zone->nr_active + zone->nr_inactive;
  908. }
  909. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  910. sc.nr_mapped = read_page_state(nr_mapped);
  911. sc.nr_scanned = 0;
  912. sc.nr_reclaimed = 0;
  913. sc.priority = priority;
  914. sc.swap_cluster_max = SWAP_CLUSTER_MAX;
  915. if (!priority)
  916. disable_swap_token();
  917. shrink_caches(zones, &sc);
  918. shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
  919. if (reclaim_state) {
  920. sc.nr_reclaimed += reclaim_state->reclaimed_slab;
  921. reclaim_state->reclaimed_slab = 0;
  922. }
  923. total_scanned += sc.nr_scanned;
  924. total_reclaimed += sc.nr_reclaimed;
  925. if (total_reclaimed >= sc.swap_cluster_max) {
  926. ret = 1;
  927. goto out;
  928. }
  929. /*
  930. * Try to write back as many pages as we just scanned. This
  931. * tends to cause slow streaming writers to write data to the
  932. * disk smoothly, at the dirtying rate, which is nice. But
  933. * that's undesirable in laptop mode, where we *want* lumpy
  934. * writeout. So in laptop mode, write out the whole world.
  935. */
  936. if (total_scanned > sc.swap_cluster_max + sc.swap_cluster_max/2) {
  937. wakeup_pdflush(laptop_mode ? 0 : total_scanned);
  938. sc.may_writepage = 1;
  939. }
  940. /* Take a nap, wait for some writeback to complete */
  941. if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
  942. blk_congestion_wait(WRITE, HZ/10);
  943. }
  944. out:
  945. for (i = 0; zones[i] != 0; i++) {
  946. struct zone *zone = zones[i];
  947. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  948. continue;
  949. zone->prev_priority = zone->temp_priority;
  950. }
  951. return ret;
  952. }
  953. /*
  954. * For kswapd, balance_pgdat() will work across all this node's zones until
  955. * they are all at pages_high.
  956. *
  957. * If `nr_pages' is non-zero then it is the number of pages which are to be
  958. * reclaimed, regardless of the zone occupancies. This is a software suspend
  959. * special.
  960. *
  961. * Returns the number of pages which were actually freed.
  962. *
  963. * There is special handling here for zones which are full of pinned pages.
  964. * This can happen if the pages are all mlocked, or if they are all used by
  965. * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
  966. * What we do is to detect the case where all pages in the zone have been
  967. * scanned twice and there has been zero successful reclaim. Mark the zone as
  968. * dead and from now on, only perform a short scan. Basically we're polling
  969. * the zone for when the problem goes away.
  970. *
  971. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  972. * zones which have free_pages > pages_high, but once a zone is found to have
  973. * free_pages <= pages_high, we scan that zone and the lower zones regardless
  974. * of the number of free pages in the lower zones. This interoperates with
  975. * the page allocator fallback scheme to ensure that aging of pages is balanced
  976. * across the zones.
  977. */
  978. static int balance_pgdat(pg_data_t *pgdat, int nr_pages, int order)
  979. {
  980. int to_free = nr_pages;
  981. int all_zones_ok;
  982. int priority;
  983. int i;
  984. int total_scanned, total_reclaimed;
  985. struct reclaim_state *reclaim_state = current->reclaim_state;
  986. struct scan_control sc;
  987. loop_again:
  988. total_scanned = 0;
  989. total_reclaimed = 0;
  990. sc.gfp_mask = GFP_KERNEL;
  991. sc.may_writepage = 0;
  992. sc.nr_mapped = read_page_state(nr_mapped);
  993. inc_page_state(pageoutrun);
  994. for (i = 0; i < pgdat->nr_zones; i++) {
  995. struct zone *zone = pgdat->node_zones + i;
  996. zone->temp_priority = DEF_PRIORITY;
  997. }
  998. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  999. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  1000. unsigned long lru_pages = 0;
  1001. /* The swap token gets in the way of swapout... */
  1002. if (!priority)
  1003. disable_swap_token();
  1004. all_zones_ok = 1;
  1005. if (nr_pages == 0) {
  1006. /*
  1007. * Scan in the highmem->dma direction for the highest
  1008. * zone which needs scanning
  1009. */
  1010. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  1011. struct zone *zone = pgdat->node_zones + i;
  1012. if (!populated_zone(zone))
  1013. continue;
  1014. if (zone->all_unreclaimable &&
  1015. priority != DEF_PRIORITY)
  1016. continue;
  1017. if (!zone_watermark_ok(zone, order,
  1018. zone->pages_high, 0, 0)) {
  1019. end_zone = i;
  1020. goto scan;
  1021. }
  1022. }
  1023. goto out;
  1024. } else {
  1025. end_zone = pgdat->nr_zones - 1;
  1026. }
  1027. scan:
  1028. for (i = 0; i <= end_zone; i++) {
  1029. struct zone *zone = pgdat->node_zones + i;
  1030. lru_pages += zone->nr_active + zone->nr_inactive;
  1031. }
  1032. /*
  1033. * Now scan the zone in the dma->highmem direction, stopping
  1034. * at the last zone which needs scanning.
  1035. *
  1036. * We do this because the page allocator works in the opposite
  1037. * direction. This prevents the page allocator from allocating
  1038. * pages behind kswapd's direction of progress, which would
  1039. * cause too much scanning of the lower zones.
  1040. */
  1041. for (i = 0; i <= end_zone; i++) {
  1042. struct zone *zone = pgdat->node_zones + i;
  1043. int nr_slab;
  1044. if (!populated_zone(zone))
  1045. continue;
  1046. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  1047. continue;
  1048. if (nr_pages == 0) { /* Not software suspend */
  1049. if (!zone_watermark_ok(zone, order,
  1050. zone->pages_high, end_zone, 0))
  1051. all_zones_ok = 0;
  1052. }
  1053. zone->temp_priority = priority;
  1054. if (zone->prev_priority > priority)
  1055. zone->prev_priority = priority;
  1056. sc.nr_scanned = 0;
  1057. sc.nr_reclaimed = 0;
  1058. sc.priority = priority;
  1059. sc.swap_cluster_max = nr_pages? nr_pages : SWAP_CLUSTER_MAX;
  1060. atomic_inc(&zone->reclaim_in_progress);
  1061. shrink_zone(zone, &sc);
  1062. atomic_dec(&zone->reclaim_in_progress);
  1063. reclaim_state->reclaimed_slab = 0;
  1064. nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
  1065. lru_pages);
  1066. sc.nr_reclaimed += reclaim_state->reclaimed_slab;
  1067. total_reclaimed += sc.nr_reclaimed;
  1068. total_scanned += sc.nr_scanned;
  1069. if (zone->all_unreclaimable)
  1070. continue;
  1071. if (nr_slab == 0 && zone->pages_scanned >=
  1072. (zone->nr_active + zone->nr_inactive) * 4)
  1073. zone->all_unreclaimable = 1;
  1074. /*
  1075. * If we've done a decent amount of scanning and
  1076. * the reclaim ratio is low, start doing writepage
  1077. * even in laptop mode
  1078. */
  1079. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  1080. total_scanned > total_reclaimed+total_reclaimed/2)
  1081. sc.may_writepage = 1;
  1082. }
  1083. if (nr_pages && to_free > total_reclaimed)
  1084. continue; /* swsusp: need to do more work */
  1085. if (all_zones_ok)
  1086. break; /* kswapd: all done */
  1087. /*
  1088. * OK, kswapd is getting into trouble. Take a nap, then take
  1089. * another pass across the zones.
  1090. */
  1091. if (total_scanned && priority < DEF_PRIORITY - 2)
  1092. blk_congestion_wait(WRITE, HZ/10);
  1093. /*
  1094. * We do this so kswapd doesn't build up large priorities for
  1095. * example when it is freeing in parallel with allocators. It
  1096. * matches the direct reclaim path behaviour in terms of impact
  1097. * on zone->*_priority.
  1098. */
  1099. if ((total_reclaimed >= SWAP_CLUSTER_MAX) && (!nr_pages))
  1100. break;
  1101. }
  1102. out:
  1103. for (i = 0; i < pgdat->nr_zones; i++) {
  1104. struct zone *zone = pgdat->node_zones + i;
  1105. zone->prev_priority = zone->temp_priority;
  1106. }
  1107. if (!all_zones_ok) {
  1108. cond_resched();
  1109. goto loop_again;
  1110. }
  1111. return total_reclaimed;
  1112. }
  1113. /*
  1114. * The background pageout daemon, started as a kernel thread
  1115. * from the init process.
  1116. *
  1117. * This basically trickles out pages so that we have _some_
  1118. * free memory available even if there is no other activity
  1119. * that frees anything up. This is needed for things like routing
  1120. * etc, where we otherwise might have all activity going on in
  1121. * asynchronous contexts that cannot page things out.
  1122. *
  1123. * If there are applications that are active memory-allocators
  1124. * (most normal use), this basically shouldn't matter.
  1125. */
  1126. static int kswapd(void *p)
  1127. {
  1128. unsigned long order;
  1129. pg_data_t *pgdat = (pg_data_t*)p;
  1130. struct task_struct *tsk = current;
  1131. DEFINE_WAIT(wait);
  1132. struct reclaim_state reclaim_state = {
  1133. .reclaimed_slab = 0,
  1134. };
  1135. cpumask_t cpumask;
  1136. daemonize("kswapd%d", pgdat->node_id);
  1137. cpumask = node_to_cpumask(pgdat->node_id);
  1138. if (!cpus_empty(cpumask))
  1139. set_cpus_allowed(tsk, cpumask);
  1140. current->reclaim_state = &reclaim_state;
  1141. /*
  1142. * Tell the memory management that we're a "memory allocator",
  1143. * and that if we need more memory we should get access to it
  1144. * regardless (see "__alloc_pages()"). "kswapd" should
  1145. * never get caught in the normal page freeing logic.
  1146. *
  1147. * (Kswapd normally doesn't need memory anyway, but sometimes
  1148. * you need a small amount of memory in order to be able to
  1149. * page out something else, and this flag essentially protects
  1150. * us from recursively trying to free more memory as we're
  1151. * trying to free the first piece of memory in the first place).
  1152. */
  1153. tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  1154. order = 0;
  1155. for ( ; ; ) {
  1156. unsigned long new_order;
  1157. try_to_freeze();
  1158. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  1159. new_order = pgdat->kswapd_max_order;
  1160. pgdat->kswapd_max_order = 0;
  1161. if (order < new_order) {
  1162. /*
  1163. * Don't sleep if someone wants a larger 'order'
  1164. * allocation
  1165. */
  1166. order = new_order;
  1167. } else {
  1168. schedule();
  1169. order = pgdat->kswapd_max_order;
  1170. }
  1171. finish_wait(&pgdat->kswapd_wait, &wait);
  1172. balance_pgdat(pgdat, 0, order);
  1173. }
  1174. return 0;
  1175. }
  1176. /*
  1177. * A zone is low on free memory, so wake its kswapd task to service it.
  1178. */
  1179. void wakeup_kswapd(struct zone *zone, int order)
  1180. {
  1181. pg_data_t *pgdat;
  1182. if (!populated_zone(zone))
  1183. return;
  1184. pgdat = zone->zone_pgdat;
  1185. if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
  1186. return;
  1187. if (pgdat->kswapd_max_order < order)
  1188. pgdat->kswapd_max_order = order;
  1189. if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
  1190. return;
  1191. if (!waitqueue_active(&pgdat->kswapd_wait))
  1192. return;
  1193. wake_up_interruptible(&pgdat->kswapd_wait);
  1194. }
  1195. #ifdef CONFIG_PM
  1196. /*
  1197. * Try to free `nr_pages' of memory, system-wide. Returns the number of freed
  1198. * pages.
  1199. */
  1200. int shrink_all_memory(int nr_pages)
  1201. {
  1202. pg_data_t *pgdat;
  1203. int nr_to_free = nr_pages;
  1204. int ret = 0;
  1205. struct reclaim_state reclaim_state = {
  1206. .reclaimed_slab = 0,
  1207. };
  1208. current->reclaim_state = &reclaim_state;
  1209. for_each_pgdat(pgdat) {
  1210. int freed;
  1211. freed = balance_pgdat(pgdat, nr_to_free, 0);
  1212. ret += freed;
  1213. nr_to_free -= freed;
  1214. if (nr_to_free <= 0)
  1215. break;
  1216. }
  1217. current->reclaim_state = NULL;
  1218. return ret;
  1219. }
  1220. #endif
  1221. #ifdef CONFIG_HOTPLUG_CPU
  1222. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  1223. not required for correctness. So if the last cpu in a node goes
  1224. away, we get changed to run anywhere: as the first one comes back,
  1225. restore their cpu bindings. */
  1226. static int __devinit cpu_callback(struct notifier_block *nfb,
  1227. unsigned long action,
  1228. void *hcpu)
  1229. {
  1230. pg_data_t *pgdat;
  1231. cpumask_t mask;
  1232. if (action == CPU_ONLINE) {
  1233. for_each_pgdat(pgdat) {
  1234. mask = node_to_cpumask(pgdat->node_id);
  1235. if (any_online_cpu(mask) != NR_CPUS)
  1236. /* One of our CPUs online: restore mask */
  1237. set_cpus_allowed(pgdat->kswapd, mask);
  1238. }
  1239. }
  1240. return NOTIFY_OK;
  1241. }
  1242. #endif /* CONFIG_HOTPLUG_CPU */
  1243. static int __init kswapd_init(void)
  1244. {
  1245. pg_data_t *pgdat;
  1246. swap_setup();
  1247. for_each_pgdat(pgdat)
  1248. pgdat->kswapd
  1249. = find_task_by_pid(kernel_thread(kswapd, pgdat, CLONE_KERNEL));
  1250. total_memory = nr_free_pagecache_pages();
  1251. hotcpu_notifier(cpu_callback, 0);
  1252. return 0;
  1253. }
  1254. module_init(kswapd_init)