vmscan.c 35 KB

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