vmscan.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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. /* How many pages shrink_cache() should reclaim */
  58. int nr_to_reclaim;
  59. /* Ask shrink_caches, or shrink_zone to scan at this priority */
  60. unsigned int priority;
  61. /* This context's GFP mask */
  62. unsigned int gfp_mask;
  63. int may_writepage;
  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. static int shrink_slab(unsigned long scanned, unsigned int gfp_mask,
  164. unsigned long lru_pages)
  165. {
  166. struct shrinker *shrinker;
  167. int ret = 0;
  168. if (scanned == 0)
  169. scanned = SWAP_CLUSTER_MAX;
  170. if (!down_read_trylock(&shrinker_rwsem))
  171. return 1; /* Assume we'll be able to shrink next time */
  172. list_for_each_entry(shrinker, &shrinker_list, list) {
  173. unsigned long long delta;
  174. unsigned long total_scan;
  175. delta = (4 * scanned) / shrinker->seeks;
  176. delta *= (*shrinker->shrinker)(0, gfp_mask);
  177. do_div(delta, lru_pages + 1);
  178. shrinker->nr += delta;
  179. if (shrinker->nr < 0)
  180. shrinker->nr = LONG_MAX; /* It wrapped! */
  181. total_scan = shrinker->nr;
  182. shrinker->nr = 0;
  183. while (total_scan >= SHRINK_BATCH) {
  184. long this_scan = SHRINK_BATCH;
  185. int shrink_ret;
  186. int nr_before;
  187. nr_before = (*shrinker->shrinker)(0, gfp_mask);
  188. shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
  189. if (shrink_ret == -1)
  190. break;
  191. if (shrink_ret < nr_before)
  192. ret += nr_before - shrink_ret;
  193. mod_page_state(slabs_scanned, this_scan);
  194. total_scan -= this_scan;
  195. cond_resched();
  196. }
  197. shrinker->nr += total_scan;
  198. }
  199. up_read(&shrinker_rwsem);
  200. return ret;
  201. }
  202. /* Called without lock on whether page is mapped, so answer is unstable */
  203. static inline int page_mapping_inuse(struct page *page)
  204. {
  205. struct address_space *mapping;
  206. /* Page is in somebody's page tables. */
  207. if (page_mapped(page))
  208. return 1;
  209. /* Be more reluctant to reclaim swapcache than pagecache */
  210. if (PageSwapCache(page))
  211. return 1;
  212. mapping = page_mapping(page);
  213. if (!mapping)
  214. return 0;
  215. /* File is mmap'd by somebody? */
  216. return mapping_mapped(mapping);
  217. }
  218. static inline int is_page_cache_freeable(struct page *page)
  219. {
  220. return page_count(page) - !!PagePrivate(page) == 2;
  221. }
  222. static int may_write_to_queue(struct backing_dev_info *bdi)
  223. {
  224. if (current_is_kswapd())
  225. return 1;
  226. if (current_is_pdflush()) /* This is unlikely, but why not... */
  227. return 1;
  228. if (!bdi_write_congested(bdi))
  229. return 1;
  230. if (bdi == current->backing_dev_info)
  231. return 1;
  232. return 0;
  233. }
  234. /*
  235. * We detected a synchronous write error writing a page out. Probably
  236. * -ENOSPC. We need to propagate that into the address_space for a subsequent
  237. * fsync(), msync() or close().
  238. *
  239. * The tricky part is that after writepage we cannot touch the mapping: nothing
  240. * prevents it from being freed up. But we have a ref on the page and once
  241. * that page is locked, the mapping is pinned.
  242. *
  243. * We're allowed to run sleeping lock_page() here because we know the caller has
  244. * __GFP_FS.
  245. */
  246. static void handle_write_error(struct address_space *mapping,
  247. struct page *page, int error)
  248. {
  249. lock_page(page);
  250. if (page_mapping(page) == mapping) {
  251. if (error == -ENOSPC)
  252. set_bit(AS_ENOSPC, &mapping->flags);
  253. else
  254. set_bit(AS_EIO, &mapping->flags);
  255. }
  256. unlock_page(page);
  257. }
  258. /*
  259. * pageout is called by shrink_list() for each dirty page. Calls ->writepage().
  260. */
  261. static pageout_t pageout(struct page *page, struct address_space *mapping)
  262. {
  263. /*
  264. * If the page is dirty, only perform writeback if that write
  265. * will be non-blocking. To prevent this allocation from being
  266. * stalled by pagecache activity. But note that there may be
  267. * stalls if we need to run get_block(). We could test
  268. * PagePrivate for that.
  269. *
  270. * If this process is currently in generic_file_write() against
  271. * this page's queue, we can perform writeback even if that
  272. * will block.
  273. *
  274. * If the page is swapcache, write it back even if that would
  275. * block, for some throttling. This happens by accident, because
  276. * swap_backing_dev_info is bust: it doesn't reflect the
  277. * congestion state of the swapdevs. Easy to fix, if needed.
  278. * See swapfile.c:page_queue_congested().
  279. */
  280. if (!is_page_cache_freeable(page))
  281. return PAGE_KEEP;
  282. if (!mapping) {
  283. /*
  284. * Some data journaling orphaned pages can have
  285. * page->mapping == NULL while being dirty with clean buffers.
  286. */
  287. if (PagePrivate(page)) {
  288. if (try_to_free_buffers(page)) {
  289. ClearPageDirty(page);
  290. printk("%s: orphaned page\n", __FUNCTION__);
  291. return PAGE_CLEAN;
  292. }
  293. }
  294. return PAGE_KEEP;
  295. }
  296. if (mapping->a_ops->writepage == NULL)
  297. return PAGE_ACTIVATE;
  298. if (!may_write_to_queue(mapping->backing_dev_info))
  299. return PAGE_KEEP;
  300. if (clear_page_dirty_for_io(page)) {
  301. int res;
  302. struct writeback_control wbc = {
  303. .sync_mode = WB_SYNC_NONE,
  304. .nr_to_write = SWAP_CLUSTER_MAX,
  305. .nonblocking = 1,
  306. .for_reclaim = 1,
  307. };
  308. SetPageReclaim(page);
  309. res = mapping->a_ops->writepage(page, &wbc);
  310. if (res < 0)
  311. handle_write_error(mapping, page, res);
  312. if (res == WRITEPAGE_ACTIVATE) {
  313. ClearPageReclaim(page);
  314. return PAGE_ACTIVATE;
  315. }
  316. if (!PageWriteback(page)) {
  317. /* synchronous write or broken a_ops? */
  318. ClearPageReclaim(page);
  319. }
  320. return PAGE_SUCCESS;
  321. }
  322. return PAGE_CLEAN;
  323. }
  324. /*
  325. * shrink_list adds the number of reclaimed pages to sc->nr_reclaimed
  326. */
  327. static int shrink_list(struct list_head *page_list, struct scan_control *sc)
  328. {
  329. LIST_HEAD(ret_pages);
  330. struct pagevec freed_pvec;
  331. int pgactivate = 0;
  332. int reclaimed = 0;
  333. cond_resched();
  334. pagevec_init(&freed_pvec, 1);
  335. while (!list_empty(page_list)) {
  336. struct address_space *mapping;
  337. struct page *page;
  338. int may_enter_fs;
  339. int referenced;
  340. cond_resched();
  341. page = lru_to_page(page_list);
  342. list_del(&page->lru);
  343. if (TestSetPageLocked(page))
  344. goto keep;
  345. BUG_ON(PageActive(page));
  346. sc->nr_scanned++;
  347. /* Double the slab pressure for mapped and swapcache pages */
  348. if (page_mapped(page) || PageSwapCache(page))
  349. sc->nr_scanned++;
  350. if (PageWriteback(page))
  351. goto keep_locked;
  352. referenced = page_referenced(page, 1, sc->priority <= 0);
  353. /* In active use or really unfreeable? Activate it. */
  354. if (referenced && page_mapping_inuse(page))
  355. goto activate_locked;
  356. #ifdef CONFIG_SWAP
  357. /*
  358. * Anonymous process memory has backing store?
  359. * Try to allocate it some swap space here.
  360. */
  361. if (PageAnon(page) && !PageSwapCache(page)) {
  362. if (!add_to_swap(page))
  363. goto activate_locked;
  364. }
  365. #endif /* CONFIG_SWAP */
  366. mapping = page_mapping(page);
  367. may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
  368. (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
  369. /*
  370. * The page is mapped into the page tables of one or more
  371. * processes. Try to unmap it here.
  372. */
  373. if (page_mapped(page) && mapping) {
  374. switch (try_to_unmap(page)) {
  375. case SWAP_FAIL:
  376. goto activate_locked;
  377. case SWAP_AGAIN:
  378. goto keep_locked;
  379. case SWAP_SUCCESS:
  380. ; /* try to free the page below */
  381. }
  382. }
  383. if (PageDirty(page)) {
  384. if (referenced)
  385. goto keep_locked;
  386. if (!may_enter_fs)
  387. goto keep_locked;
  388. if (laptop_mode && !sc->may_writepage)
  389. goto keep_locked;
  390. /* Page is dirty, try to write it out here */
  391. switch(pageout(page, mapping)) {
  392. case PAGE_KEEP:
  393. goto keep_locked;
  394. case PAGE_ACTIVATE:
  395. goto activate_locked;
  396. case PAGE_SUCCESS:
  397. if (PageWriteback(page) || PageDirty(page))
  398. goto keep;
  399. /*
  400. * A synchronous write - probably a ramdisk. Go
  401. * ahead and try to reclaim the page.
  402. */
  403. if (TestSetPageLocked(page))
  404. goto keep;
  405. if (PageDirty(page) || PageWriteback(page))
  406. goto keep_locked;
  407. mapping = page_mapping(page);
  408. case PAGE_CLEAN:
  409. ; /* try to free the page below */
  410. }
  411. }
  412. /*
  413. * If the page has buffers, try to free the buffer mappings
  414. * associated with this page. If we succeed we try to free
  415. * the page as well.
  416. *
  417. * We do this even if the page is PageDirty().
  418. * try_to_release_page() does not perform I/O, but it is
  419. * possible for a page to have PageDirty set, but it is actually
  420. * clean (all its buffers are clean). This happens if the
  421. * buffers were written out directly, with submit_bh(). ext3
  422. * will do this, as well as the blockdev mapping.
  423. * try_to_release_page() will discover that cleanness and will
  424. * drop the buffers and mark the page clean - it can be freed.
  425. *
  426. * Rarely, pages can have buffers and no ->mapping. These are
  427. * the pages which were not successfully invalidated in
  428. * truncate_complete_page(). We try to drop those buffers here
  429. * and if that worked, and the page is no longer mapped into
  430. * process address space (page_count == 1) it can be freed.
  431. * Otherwise, leave the page on the LRU so it is swappable.
  432. */
  433. if (PagePrivate(page)) {
  434. if (!try_to_release_page(page, sc->gfp_mask))
  435. goto activate_locked;
  436. if (!mapping && page_count(page) == 1)
  437. goto free_it;
  438. }
  439. if (!mapping)
  440. goto keep_locked; /* truncate got there first */
  441. write_lock_irq(&mapping->tree_lock);
  442. /*
  443. * The non-racy check for busy page. It is critical to check
  444. * PageDirty _after_ making sure that the page is freeable and
  445. * not in use by anybody. (pagecache + us == 2)
  446. */
  447. if (page_count(page) != 2 || PageDirty(page)) {
  448. write_unlock_irq(&mapping->tree_lock);
  449. goto keep_locked;
  450. }
  451. #ifdef CONFIG_SWAP
  452. if (PageSwapCache(page)) {
  453. swp_entry_t swap = { .val = page->private };
  454. __delete_from_swap_cache(page);
  455. write_unlock_irq(&mapping->tree_lock);
  456. swap_free(swap);
  457. __put_page(page); /* The pagecache ref */
  458. goto free_it;
  459. }
  460. #endif /* CONFIG_SWAP */
  461. __remove_from_page_cache(page);
  462. write_unlock_irq(&mapping->tree_lock);
  463. __put_page(page);
  464. free_it:
  465. unlock_page(page);
  466. reclaimed++;
  467. if (!pagevec_add(&freed_pvec, page))
  468. __pagevec_release_nonlru(&freed_pvec);
  469. continue;
  470. activate_locked:
  471. SetPageActive(page);
  472. pgactivate++;
  473. keep_locked:
  474. unlock_page(page);
  475. keep:
  476. list_add(&page->lru, &ret_pages);
  477. BUG_ON(PageLRU(page));
  478. }
  479. list_splice(&ret_pages, page_list);
  480. if (pagevec_count(&freed_pvec))
  481. __pagevec_release_nonlru(&freed_pvec);
  482. mod_page_state(pgactivate, pgactivate);
  483. sc->nr_reclaimed += reclaimed;
  484. return reclaimed;
  485. }
  486. /*
  487. * zone->lru_lock is heavily contended. Some of the functions that
  488. * shrink the lists perform better by taking out a batch of pages
  489. * and working on them outside the LRU lock.
  490. *
  491. * For pagecache intensive workloads, this function is the hottest
  492. * spot in the kernel (apart from copy_*_user functions).
  493. *
  494. * Appropriate locks must be held before calling this function.
  495. *
  496. * @nr_to_scan: The number of pages to look through on the list.
  497. * @src: The LRU list to pull pages off.
  498. * @dst: The temp list to put pages on to.
  499. * @scanned: The number of pages that were scanned.
  500. *
  501. * returns how many pages were moved onto *@dst.
  502. */
  503. static int isolate_lru_pages(int nr_to_scan, struct list_head *src,
  504. struct list_head *dst, int *scanned)
  505. {
  506. int nr_taken = 0;
  507. struct page *page;
  508. int scan = 0;
  509. while (scan++ < nr_to_scan && !list_empty(src)) {
  510. page = lru_to_page(src);
  511. prefetchw_prev_lru_page(page, src, flags);
  512. if (!TestClearPageLRU(page))
  513. BUG();
  514. list_del(&page->lru);
  515. if (get_page_testone(page)) {
  516. /*
  517. * It is being freed elsewhere
  518. */
  519. __put_page(page);
  520. SetPageLRU(page);
  521. list_add(&page->lru, src);
  522. continue;
  523. } else {
  524. list_add(&page->lru, dst);
  525. nr_taken++;
  526. }
  527. }
  528. *scanned = scan;
  529. return nr_taken;
  530. }
  531. /*
  532. * shrink_cache() adds the number of pages reclaimed to sc->nr_reclaimed
  533. */
  534. static void shrink_cache(struct zone *zone, struct scan_control *sc)
  535. {
  536. LIST_HEAD(page_list);
  537. struct pagevec pvec;
  538. int max_scan = sc->nr_to_scan;
  539. pagevec_init(&pvec, 1);
  540. lru_add_drain();
  541. spin_lock_irq(&zone->lru_lock);
  542. while (max_scan > 0) {
  543. struct page *page;
  544. int nr_taken;
  545. int nr_scan;
  546. int nr_freed;
  547. nr_taken = isolate_lru_pages(sc->swap_cluster_max,
  548. &zone->inactive_list,
  549. &page_list, &nr_scan);
  550. zone->nr_inactive -= nr_taken;
  551. zone->pages_scanned += nr_scan;
  552. spin_unlock_irq(&zone->lru_lock);
  553. if (nr_taken == 0)
  554. goto done;
  555. max_scan -= nr_scan;
  556. if (current_is_kswapd())
  557. mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
  558. else
  559. mod_page_state_zone(zone, pgscan_direct, nr_scan);
  560. nr_freed = shrink_list(&page_list, sc);
  561. if (current_is_kswapd())
  562. mod_page_state(kswapd_steal, nr_freed);
  563. mod_page_state_zone(zone, pgsteal, nr_freed);
  564. sc->nr_to_reclaim -= nr_freed;
  565. spin_lock_irq(&zone->lru_lock);
  566. /*
  567. * Put back any unfreeable pages.
  568. */
  569. while (!list_empty(&page_list)) {
  570. page = lru_to_page(&page_list);
  571. if (TestSetPageLRU(page))
  572. BUG();
  573. list_del(&page->lru);
  574. if (PageActive(page))
  575. add_page_to_active_list(zone, page);
  576. else
  577. add_page_to_inactive_list(zone, page);
  578. if (!pagevec_add(&pvec, page)) {
  579. spin_unlock_irq(&zone->lru_lock);
  580. __pagevec_release(&pvec);
  581. spin_lock_irq(&zone->lru_lock);
  582. }
  583. }
  584. }
  585. spin_unlock_irq(&zone->lru_lock);
  586. done:
  587. pagevec_release(&pvec);
  588. }
  589. /*
  590. * This moves pages from the active list to the inactive list.
  591. *
  592. * We move them the other way if the page is referenced by one or more
  593. * processes, from rmap.
  594. *
  595. * If the pages are mostly unmapped, the processing is fast and it is
  596. * appropriate to hold zone->lru_lock across the whole operation. But if
  597. * the pages are mapped, the processing is slow (page_referenced()) so we
  598. * should drop zone->lru_lock around each page. It's impossible to balance
  599. * this, so instead we remove the pages from the LRU while processing them.
  600. * It is safe to rely on PG_active against the non-LRU pages in here because
  601. * nobody will play with that bit on a non-LRU page.
  602. *
  603. * The downside is that we have to touch page->_count against each page.
  604. * But we had to alter page->flags anyway.
  605. */
  606. static void
  607. refill_inactive_zone(struct zone *zone, struct scan_control *sc)
  608. {
  609. int pgmoved;
  610. int pgdeactivate = 0;
  611. int pgscanned;
  612. int nr_pages = sc->nr_to_scan;
  613. LIST_HEAD(l_hold); /* The pages which were snipped off */
  614. LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */
  615. LIST_HEAD(l_active); /* Pages to go onto the active_list */
  616. struct page *page;
  617. struct pagevec pvec;
  618. int reclaim_mapped = 0;
  619. long mapped_ratio;
  620. long distress;
  621. long swap_tendency;
  622. lru_add_drain();
  623. spin_lock_irq(&zone->lru_lock);
  624. pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
  625. &l_hold, &pgscanned);
  626. zone->pages_scanned += pgscanned;
  627. zone->nr_active -= pgmoved;
  628. spin_unlock_irq(&zone->lru_lock);
  629. /*
  630. * `distress' is a measure of how much trouble we're having reclaiming
  631. * pages. 0 -> no problems. 100 -> great trouble.
  632. */
  633. distress = 100 >> zone->prev_priority;
  634. /*
  635. * The point of this algorithm is to decide when to start reclaiming
  636. * mapped memory instead of just pagecache. Work out how much memory
  637. * is mapped.
  638. */
  639. mapped_ratio = (sc->nr_mapped * 100) / total_memory;
  640. /*
  641. * Now decide how much we really want to unmap some pages. The mapped
  642. * ratio is downgraded - just because there's a lot of mapped memory
  643. * doesn't necessarily mean that page reclaim isn't succeeding.
  644. *
  645. * The distress ratio is important - we don't want to start going oom.
  646. *
  647. * A 100% value of vm_swappiness overrides this algorithm altogether.
  648. */
  649. swap_tendency = mapped_ratio / 2 + distress + vm_swappiness;
  650. /*
  651. * Now use this metric to decide whether to start moving mapped memory
  652. * onto the inactive list.
  653. */
  654. if (swap_tendency >= 100)
  655. reclaim_mapped = 1;
  656. while (!list_empty(&l_hold)) {
  657. cond_resched();
  658. page = lru_to_page(&l_hold);
  659. list_del(&page->lru);
  660. if (page_mapped(page)) {
  661. if (!reclaim_mapped ||
  662. (total_swap_pages == 0 && PageAnon(page)) ||
  663. page_referenced(page, 0, sc->priority <= 0)) {
  664. list_add(&page->lru, &l_active);
  665. continue;
  666. }
  667. }
  668. list_add(&page->lru, &l_inactive);
  669. }
  670. pagevec_init(&pvec, 1);
  671. pgmoved = 0;
  672. spin_lock_irq(&zone->lru_lock);
  673. while (!list_empty(&l_inactive)) {
  674. page = lru_to_page(&l_inactive);
  675. prefetchw_prev_lru_page(page, &l_inactive, flags);
  676. if (TestSetPageLRU(page))
  677. BUG();
  678. if (!TestClearPageActive(page))
  679. BUG();
  680. list_move(&page->lru, &zone->inactive_list);
  681. pgmoved++;
  682. if (!pagevec_add(&pvec, page)) {
  683. zone->nr_inactive += pgmoved;
  684. spin_unlock_irq(&zone->lru_lock);
  685. pgdeactivate += pgmoved;
  686. pgmoved = 0;
  687. if (buffer_heads_over_limit)
  688. pagevec_strip(&pvec);
  689. __pagevec_release(&pvec);
  690. spin_lock_irq(&zone->lru_lock);
  691. }
  692. }
  693. zone->nr_inactive += pgmoved;
  694. pgdeactivate += pgmoved;
  695. if (buffer_heads_over_limit) {
  696. spin_unlock_irq(&zone->lru_lock);
  697. pagevec_strip(&pvec);
  698. spin_lock_irq(&zone->lru_lock);
  699. }
  700. pgmoved = 0;
  701. while (!list_empty(&l_active)) {
  702. page = lru_to_page(&l_active);
  703. prefetchw_prev_lru_page(page, &l_active, flags);
  704. if (TestSetPageLRU(page))
  705. BUG();
  706. BUG_ON(!PageActive(page));
  707. list_move(&page->lru, &zone->active_list);
  708. pgmoved++;
  709. if (!pagevec_add(&pvec, page)) {
  710. zone->nr_active += pgmoved;
  711. pgmoved = 0;
  712. spin_unlock_irq(&zone->lru_lock);
  713. __pagevec_release(&pvec);
  714. spin_lock_irq(&zone->lru_lock);
  715. }
  716. }
  717. zone->nr_active += pgmoved;
  718. spin_unlock_irq(&zone->lru_lock);
  719. pagevec_release(&pvec);
  720. mod_page_state_zone(zone, pgrefill, pgscanned);
  721. mod_page_state(pgdeactivate, pgdeactivate);
  722. }
  723. /*
  724. * This is a basic per-zone page freer. Used by both kswapd and direct reclaim.
  725. */
  726. static void
  727. shrink_zone(struct zone *zone, struct scan_control *sc)
  728. {
  729. unsigned long nr_active;
  730. unsigned long nr_inactive;
  731. /*
  732. * Add one to `nr_to_scan' just to make sure that the kernel will
  733. * slowly sift through the active list.
  734. */
  735. zone->nr_scan_active += (zone->nr_active >> sc->priority) + 1;
  736. nr_active = zone->nr_scan_active;
  737. if (nr_active >= sc->swap_cluster_max)
  738. zone->nr_scan_active = 0;
  739. else
  740. nr_active = 0;
  741. zone->nr_scan_inactive += (zone->nr_inactive >> sc->priority) + 1;
  742. nr_inactive = zone->nr_scan_inactive;
  743. if (nr_inactive >= sc->swap_cluster_max)
  744. zone->nr_scan_inactive = 0;
  745. else
  746. nr_inactive = 0;
  747. sc->nr_to_reclaim = sc->swap_cluster_max;
  748. while (nr_active || nr_inactive) {
  749. if (nr_active) {
  750. sc->nr_to_scan = min(nr_active,
  751. (unsigned long)sc->swap_cluster_max);
  752. nr_active -= sc->nr_to_scan;
  753. refill_inactive_zone(zone, sc);
  754. }
  755. if (nr_inactive) {
  756. sc->nr_to_scan = min(nr_inactive,
  757. (unsigned long)sc->swap_cluster_max);
  758. nr_inactive -= sc->nr_to_scan;
  759. shrink_cache(zone, sc);
  760. if (sc->nr_to_reclaim <= 0)
  761. break;
  762. }
  763. }
  764. throttle_vm_writeout();
  765. }
  766. /*
  767. * This is the direct reclaim path, for page-allocating processes. We only
  768. * try to reclaim pages from zones which will satisfy the caller's allocation
  769. * request.
  770. *
  771. * We reclaim from a zone even if that zone is over pages_high. Because:
  772. * a) The caller may be trying to free *extra* pages to satisfy a higher-order
  773. * allocation or
  774. * b) The zones may be over pages_high but they must go *over* pages_high to
  775. * satisfy the `incremental min' zone defense algorithm.
  776. *
  777. * Returns the number of reclaimed pages.
  778. *
  779. * If a zone is deemed to be full of pinned pages then just give it a light
  780. * scan then give up on it.
  781. */
  782. static void
  783. shrink_caches(struct zone **zones, struct scan_control *sc)
  784. {
  785. int i;
  786. for (i = 0; zones[i] != NULL; i++) {
  787. struct zone *zone = zones[i];
  788. if (zone->present_pages == 0)
  789. continue;
  790. if (!cpuset_zone_allowed(zone))
  791. continue;
  792. zone->temp_priority = sc->priority;
  793. if (zone->prev_priority > sc->priority)
  794. zone->prev_priority = sc->priority;
  795. if (zone->all_unreclaimable && sc->priority != DEF_PRIORITY)
  796. continue; /* Let kswapd poll it */
  797. shrink_zone(zone, sc);
  798. }
  799. }
  800. /*
  801. * This is the main entry point to direct page reclaim.
  802. *
  803. * If a full scan of the inactive list fails to free enough memory then we
  804. * are "out of memory" and something needs to be killed.
  805. *
  806. * If the caller is !__GFP_FS then the probability of a failure is reasonably
  807. * high - the zone may be full of dirty or under-writeback pages, which this
  808. * caller can't do much about. We kick pdflush and take explicit naps in the
  809. * hope that some of these pages can be written. But if the allocating task
  810. * holds filesystem locks which prevent writeout this might not work, and the
  811. * allocation attempt will fail.
  812. */
  813. int try_to_free_pages(struct zone **zones,
  814. unsigned int gfp_mask, unsigned int order)
  815. {
  816. int priority;
  817. int ret = 0;
  818. int total_scanned = 0, total_reclaimed = 0;
  819. struct reclaim_state *reclaim_state = current->reclaim_state;
  820. struct scan_control sc;
  821. unsigned long lru_pages = 0;
  822. int i;
  823. sc.gfp_mask = gfp_mask;
  824. sc.may_writepage = 0;
  825. inc_page_state(allocstall);
  826. for (i = 0; zones[i] != NULL; i++) {
  827. struct zone *zone = zones[i];
  828. if (!cpuset_zone_allowed(zone))
  829. continue;
  830. zone->temp_priority = DEF_PRIORITY;
  831. lru_pages += zone->nr_active + zone->nr_inactive;
  832. }
  833. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  834. sc.nr_mapped = read_page_state(nr_mapped);
  835. sc.nr_scanned = 0;
  836. sc.nr_reclaimed = 0;
  837. sc.priority = priority;
  838. sc.swap_cluster_max = SWAP_CLUSTER_MAX;
  839. shrink_caches(zones, &sc);
  840. shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
  841. if (reclaim_state) {
  842. sc.nr_reclaimed += reclaim_state->reclaimed_slab;
  843. reclaim_state->reclaimed_slab = 0;
  844. }
  845. total_scanned += sc.nr_scanned;
  846. total_reclaimed += sc.nr_reclaimed;
  847. if (total_reclaimed >= sc.swap_cluster_max) {
  848. ret = 1;
  849. goto out;
  850. }
  851. /*
  852. * Try to write back as many pages as we just scanned. This
  853. * tends to cause slow streaming writers to write data to the
  854. * disk smoothly, at the dirtying rate, which is nice. But
  855. * that's undesirable in laptop mode, where we *want* lumpy
  856. * writeout. So in laptop mode, write out the whole world.
  857. */
  858. if (total_scanned > sc.swap_cluster_max + sc.swap_cluster_max/2) {
  859. wakeup_bdflush(laptop_mode ? 0 : total_scanned);
  860. sc.may_writepage = 1;
  861. }
  862. /* Take a nap, wait for some writeback to complete */
  863. if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
  864. blk_congestion_wait(WRITE, HZ/10);
  865. }
  866. out:
  867. for (i = 0; zones[i] != 0; i++) {
  868. struct zone *zone = zones[i];
  869. if (!cpuset_zone_allowed(zone))
  870. continue;
  871. zone->prev_priority = zone->temp_priority;
  872. }
  873. return ret;
  874. }
  875. /*
  876. * For kswapd, balance_pgdat() will work across all this node's zones until
  877. * they are all at pages_high.
  878. *
  879. * If `nr_pages' is non-zero then it is the number of pages which are to be
  880. * reclaimed, regardless of the zone occupancies. This is a software suspend
  881. * special.
  882. *
  883. * Returns the number of pages which were actually freed.
  884. *
  885. * There is special handling here for zones which are full of pinned pages.
  886. * This can happen if the pages are all mlocked, or if they are all used by
  887. * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb.
  888. * What we do is to detect the case where all pages in the zone have been
  889. * scanned twice and there has been zero successful reclaim. Mark the zone as
  890. * dead and from now on, only perform a short scan. Basically we're polling
  891. * the zone for when the problem goes away.
  892. *
  893. * kswapd scans the zones in the highmem->normal->dma direction. It skips
  894. * zones which have free_pages > pages_high, but once a zone is found to have
  895. * free_pages <= pages_high, we scan that zone and the lower zones regardless
  896. * of the number of free pages in the lower zones. This interoperates with
  897. * the page allocator fallback scheme to ensure that aging of pages is balanced
  898. * across the zones.
  899. */
  900. static int balance_pgdat(pg_data_t *pgdat, int nr_pages, int order)
  901. {
  902. int to_free = nr_pages;
  903. int all_zones_ok;
  904. int priority;
  905. int i;
  906. int total_scanned, total_reclaimed;
  907. struct reclaim_state *reclaim_state = current->reclaim_state;
  908. struct scan_control sc;
  909. loop_again:
  910. total_scanned = 0;
  911. total_reclaimed = 0;
  912. sc.gfp_mask = GFP_KERNEL;
  913. sc.may_writepage = 0;
  914. sc.nr_mapped = read_page_state(nr_mapped);
  915. inc_page_state(pageoutrun);
  916. for (i = 0; i < pgdat->nr_zones; i++) {
  917. struct zone *zone = pgdat->node_zones + i;
  918. zone->temp_priority = DEF_PRIORITY;
  919. }
  920. for (priority = DEF_PRIORITY; priority >= 0; priority--) {
  921. int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */
  922. unsigned long lru_pages = 0;
  923. all_zones_ok = 1;
  924. if (nr_pages == 0) {
  925. /*
  926. * Scan in the highmem->dma direction for the highest
  927. * zone which needs scanning
  928. */
  929. for (i = pgdat->nr_zones - 1; i >= 0; i--) {
  930. struct zone *zone = pgdat->node_zones + i;
  931. if (zone->present_pages == 0)
  932. continue;
  933. if (zone->all_unreclaimable &&
  934. priority != DEF_PRIORITY)
  935. continue;
  936. if (!zone_watermark_ok(zone, order,
  937. zone->pages_high, 0, 0, 0)) {
  938. end_zone = i;
  939. goto scan;
  940. }
  941. }
  942. goto out;
  943. } else {
  944. end_zone = pgdat->nr_zones - 1;
  945. }
  946. scan:
  947. for (i = 0; i <= end_zone; i++) {
  948. struct zone *zone = pgdat->node_zones + i;
  949. lru_pages += zone->nr_active + zone->nr_inactive;
  950. }
  951. /*
  952. * Now scan the zone in the dma->highmem direction, stopping
  953. * at the last zone which needs scanning.
  954. *
  955. * We do this because the page allocator works in the opposite
  956. * direction. This prevents the page allocator from allocating
  957. * pages behind kswapd's direction of progress, which would
  958. * cause too much scanning of the lower zones.
  959. */
  960. for (i = 0; i <= end_zone; i++) {
  961. struct zone *zone = pgdat->node_zones + i;
  962. int nr_slab;
  963. if (zone->present_pages == 0)
  964. continue;
  965. if (zone->all_unreclaimable && priority != DEF_PRIORITY)
  966. continue;
  967. if (nr_pages == 0) { /* Not software suspend */
  968. if (!zone_watermark_ok(zone, order,
  969. zone->pages_high, end_zone, 0, 0))
  970. all_zones_ok = 0;
  971. }
  972. zone->temp_priority = priority;
  973. if (zone->prev_priority > priority)
  974. zone->prev_priority = priority;
  975. sc.nr_scanned = 0;
  976. sc.nr_reclaimed = 0;
  977. sc.priority = priority;
  978. sc.swap_cluster_max = nr_pages? nr_pages : SWAP_CLUSTER_MAX;
  979. shrink_zone(zone, &sc);
  980. reclaim_state->reclaimed_slab = 0;
  981. nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
  982. lru_pages);
  983. sc.nr_reclaimed += reclaim_state->reclaimed_slab;
  984. total_reclaimed += sc.nr_reclaimed;
  985. total_scanned += sc.nr_scanned;
  986. if (zone->all_unreclaimable)
  987. continue;
  988. if (nr_slab == 0 && zone->pages_scanned >=
  989. (zone->nr_active + zone->nr_inactive) * 4)
  990. zone->all_unreclaimable = 1;
  991. /*
  992. * If we've done a decent amount of scanning and
  993. * the reclaim ratio is low, start doing writepage
  994. * even in laptop mode
  995. */
  996. if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
  997. total_scanned > total_reclaimed+total_reclaimed/2)
  998. sc.may_writepage = 1;
  999. }
  1000. if (nr_pages && to_free > total_reclaimed)
  1001. continue; /* swsusp: need to do more work */
  1002. if (all_zones_ok)
  1003. break; /* kswapd: all done */
  1004. /*
  1005. * OK, kswapd is getting into trouble. Take a nap, then take
  1006. * another pass across the zones.
  1007. */
  1008. if (total_scanned && priority < DEF_PRIORITY - 2)
  1009. blk_congestion_wait(WRITE, HZ/10);
  1010. /*
  1011. * We do this so kswapd doesn't build up large priorities for
  1012. * example when it is freeing in parallel with allocators. It
  1013. * matches the direct reclaim path behaviour in terms of impact
  1014. * on zone->*_priority.
  1015. */
  1016. if ((total_reclaimed >= SWAP_CLUSTER_MAX) && (!nr_pages))
  1017. break;
  1018. }
  1019. out:
  1020. for (i = 0; i < pgdat->nr_zones; i++) {
  1021. struct zone *zone = pgdat->node_zones + i;
  1022. zone->prev_priority = zone->temp_priority;
  1023. }
  1024. if (!all_zones_ok) {
  1025. cond_resched();
  1026. goto loop_again;
  1027. }
  1028. return total_reclaimed;
  1029. }
  1030. /*
  1031. * The background pageout daemon, started as a kernel thread
  1032. * from the init process.
  1033. *
  1034. * This basically trickles out pages so that we have _some_
  1035. * free memory available even if there is no other activity
  1036. * that frees anything up. This is needed for things like routing
  1037. * etc, where we otherwise might have all activity going on in
  1038. * asynchronous contexts that cannot page things out.
  1039. *
  1040. * If there are applications that are active memory-allocators
  1041. * (most normal use), this basically shouldn't matter.
  1042. */
  1043. static int kswapd(void *p)
  1044. {
  1045. unsigned long order;
  1046. pg_data_t *pgdat = (pg_data_t*)p;
  1047. struct task_struct *tsk = current;
  1048. DEFINE_WAIT(wait);
  1049. struct reclaim_state reclaim_state = {
  1050. .reclaimed_slab = 0,
  1051. };
  1052. cpumask_t cpumask;
  1053. daemonize("kswapd%d", pgdat->node_id);
  1054. cpumask = node_to_cpumask(pgdat->node_id);
  1055. if (!cpus_empty(cpumask))
  1056. set_cpus_allowed(tsk, cpumask);
  1057. current->reclaim_state = &reclaim_state;
  1058. /*
  1059. * Tell the memory management that we're a "memory allocator",
  1060. * and that if we need more memory we should get access to it
  1061. * regardless (see "__alloc_pages()"). "kswapd" should
  1062. * never get caught in the normal page freeing logic.
  1063. *
  1064. * (Kswapd normally doesn't need memory anyway, but sometimes
  1065. * you need a small amount of memory in order to be able to
  1066. * page out something else, and this flag essentially protects
  1067. * us from recursively trying to free more memory as we're
  1068. * trying to free the first piece of memory in the first place).
  1069. */
  1070. tsk->flags |= PF_MEMALLOC|PF_KSWAPD;
  1071. order = 0;
  1072. for ( ; ; ) {
  1073. unsigned long new_order;
  1074. if (current->flags & PF_FREEZE)
  1075. refrigerator(PF_FREEZE);
  1076. prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  1077. new_order = pgdat->kswapd_max_order;
  1078. pgdat->kswapd_max_order = 0;
  1079. if (order < new_order) {
  1080. /*
  1081. * Don't sleep if someone wants a larger 'order'
  1082. * allocation
  1083. */
  1084. order = new_order;
  1085. } else {
  1086. schedule();
  1087. order = pgdat->kswapd_max_order;
  1088. }
  1089. finish_wait(&pgdat->kswapd_wait, &wait);
  1090. balance_pgdat(pgdat, 0, order);
  1091. }
  1092. return 0;
  1093. }
  1094. /*
  1095. * A zone is low on free memory, so wake its kswapd task to service it.
  1096. */
  1097. void wakeup_kswapd(struct zone *zone, int order)
  1098. {
  1099. pg_data_t *pgdat;
  1100. if (zone->present_pages == 0)
  1101. return;
  1102. pgdat = zone->zone_pgdat;
  1103. if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0, 0))
  1104. return;
  1105. if (pgdat->kswapd_max_order < order)
  1106. pgdat->kswapd_max_order = order;
  1107. if (!cpuset_zone_allowed(zone))
  1108. return;
  1109. if (!waitqueue_active(&zone->zone_pgdat->kswapd_wait))
  1110. return;
  1111. wake_up_interruptible(&zone->zone_pgdat->kswapd_wait);
  1112. }
  1113. #ifdef CONFIG_PM
  1114. /*
  1115. * Try to free `nr_pages' of memory, system-wide. Returns the number of freed
  1116. * pages.
  1117. */
  1118. int shrink_all_memory(int nr_pages)
  1119. {
  1120. pg_data_t *pgdat;
  1121. int nr_to_free = nr_pages;
  1122. int ret = 0;
  1123. struct reclaim_state reclaim_state = {
  1124. .reclaimed_slab = 0,
  1125. };
  1126. current->reclaim_state = &reclaim_state;
  1127. for_each_pgdat(pgdat) {
  1128. int freed;
  1129. freed = balance_pgdat(pgdat, nr_to_free, 0);
  1130. ret += freed;
  1131. nr_to_free -= freed;
  1132. if (nr_to_free <= 0)
  1133. break;
  1134. }
  1135. current->reclaim_state = NULL;
  1136. return ret;
  1137. }
  1138. #endif
  1139. #ifdef CONFIG_HOTPLUG_CPU
  1140. /* It's optimal to keep kswapds on the same CPUs as their memory, but
  1141. not required for correctness. So if the last cpu in a node goes
  1142. away, we get changed to run anywhere: as the first one comes back,
  1143. restore their cpu bindings. */
  1144. static int __devinit cpu_callback(struct notifier_block *nfb,
  1145. unsigned long action,
  1146. void *hcpu)
  1147. {
  1148. pg_data_t *pgdat;
  1149. cpumask_t mask;
  1150. if (action == CPU_ONLINE) {
  1151. for_each_pgdat(pgdat) {
  1152. mask = node_to_cpumask(pgdat->node_id);
  1153. if (any_online_cpu(mask) != NR_CPUS)
  1154. /* One of our CPUs online: restore mask */
  1155. set_cpus_allowed(pgdat->kswapd, mask);
  1156. }
  1157. }
  1158. return NOTIFY_OK;
  1159. }
  1160. #endif /* CONFIG_HOTPLUG_CPU */
  1161. static int __init kswapd_init(void)
  1162. {
  1163. pg_data_t *pgdat;
  1164. swap_setup();
  1165. for_each_pgdat(pgdat)
  1166. pgdat->kswapd
  1167. = find_task_by_pid(kernel_thread(kswapd, pgdat, CLONE_KERNEL));
  1168. total_memory = nr_free_pagecache_pages();
  1169. hotcpu_notifier(cpu_callback, 0);
  1170. return 0;
  1171. }
  1172. module_init(kswapd_init)