swap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. * linux/mm/swap.c
  3. *
  4. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  5. */
  6. /*
  7. * This file contains the default values for the operation of the
  8. * Linux VM subsystem. Fine-tuning documentation can be found in
  9. * Documentation/sysctl/vm.txt.
  10. * Started 18.12.91
  11. * Swap aging added 23.2.95, Stephen Tweedie.
  12. * Buffermem limits added 12.3.98, Rik van Riel.
  13. */
  14. #include <linux/mm.h>
  15. #include <linux/sched.h>
  16. #include <linux/kernel_stat.h>
  17. #include <linux/swap.h>
  18. #include <linux/mman.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/pagevec.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/mm_inline.h>
  24. #include <linux/buffer_head.h> /* for try_to_release_page() */
  25. #include <linux/percpu_counter.h>
  26. #include <linux/percpu.h>
  27. #include <linux/cpu.h>
  28. #include <linux/notifier.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/memcontrol.h>
  31. /* How many pages do we try to swap or page in/out together? */
  32. int page_cluster;
  33. static DEFINE_PER_CPU(struct pagevec[NR_LRU_LISTS], lru_add_pvecs);
  34. static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
  35. /*
  36. * This path almost never happens for VM activity - pages are normally
  37. * freed via pagevecs. But it gets used by networking.
  38. */
  39. static void __page_cache_release(struct page *page)
  40. {
  41. if (PageLRU(page)) {
  42. unsigned long flags;
  43. struct zone *zone = page_zone(page);
  44. spin_lock_irqsave(&zone->lru_lock, flags);
  45. VM_BUG_ON(!PageLRU(page));
  46. __ClearPageLRU(page);
  47. del_page_from_lru(zone, page);
  48. spin_unlock_irqrestore(&zone->lru_lock, flags);
  49. }
  50. free_hot_page(page);
  51. }
  52. static void put_compound_page(struct page *page)
  53. {
  54. page = compound_head(page);
  55. if (put_page_testzero(page)) {
  56. compound_page_dtor *dtor;
  57. dtor = get_compound_page_dtor(page);
  58. (*dtor)(page);
  59. }
  60. }
  61. void put_page(struct page *page)
  62. {
  63. if (unlikely(PageCompound(page)))
  64. put_compound_page(page);
  65. else if (put_page_testzero(page))
  66. __page_cache_release(page);
  67. }
  68. EXPORT_SYMBOL(put_page);
  69. /**
  70. * put_pages_list() - release a list of pages
  71. * @pages: list of pages threaded on page->lru
  72. *
  73. * Release a list of pages which are strung together on page.lru. Currently
  74. * used by read_cache_pages() and related error recovery code.
  75. */
  76. void put_pages_list(struct list_head *pages)
  77. {
  78. while (!list_empty(pages)) {
  79. struct page *victim;
  80. victim = list_entry(pages->prev, struct page, lru);
  81. list_del(&victim->lru);
  82. page_cache_release(victim);
  83. }
  84. }
  85. EXPORT_SYMBOL(put_pages_list);
  86. /*
  87. * pagevec_move_tail() must be called with IRQ disabled.
  88. * Otherwise this may cause nasty races.
  89. */
  90. static void pagevec_move_tail(struct pagevec *pvec)
  91. {
  92. int i;
  93. int pgmoved = 0;
  94. struct zone *zone = NULL;
  95. for (i = 0; i < pagevec_count(pvec); i++) {
  96. struct page *page = pvec->pages[i];
  97. struct zone *pagezone = page_zone(page);
  98. if (pagezone != zone) {
  99. if (zone)
  100. spin_unlock(&zone->lru_lock);
  101. zone = pagezone;
  102. spin_lock(&zone->lru_lock);
  103. }
  104. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  105. int lru = page_is_file_cache(page);
  106. list_move_tail(&page->lru, &zone->lru[lru].list);
  107. pgmoved++;
  108. }
  109. }
  110. if (zone)
  111. spin_unlock(&zone->lru_lock);
  112. __count_vm_events(PGROTATED, pgmoved);
  113. release_pages(pvec->pages, pvec->nr, pvec->cold);
  114. pagevec_reinit(pvec);
  115. }
  116. /*
  117. * Writeback is about to end against a page which has been marked for immediate
  118. * reclaim. If it still appears to be reclaimable, move it to the tail of the
  119. * inactive list.
  120. */
  121. void rotate_reclaimable_page(struct page *page)
  122. {
  123. if (!PageLocked(page) && !PageDirty(page) && !PageActive(page) &&
  124. !PageUnevictable(page) && PageLRU(page)) {
  125. struct pagevec *pvec;
  126. unsigned long flags;
  127. page_cache_get(page);
  128. local_irq_save(flags);
  129. pvec = &__get_cpu_var(lru_rotate_pvecs);
  130. if (!pagevec_add(pvec, page))
  131. pagevec_move_tail(pvec);
  132. local_irq_restore(flags);
  133. }
  134. }
  135. /*
  136. * FIXME: speed this up?
  137. */
  138. void activate_page(struct page *page)
  139. {
  140. struct zone *zone = page_zone(page);
  141. spin_lock_irq(&zone->lru_lock);
  142. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  143. int file = page_is_file_cache(page);
  144. int lru = LRU_BASE + file;
  145. del_page_from_lru_list(zone, page, lru);
  146. SetPageActive(page);
  147. lru += LRU_ACTIVE;
  148. add_page_to_lru_list(zone, page, lru);
  149. __count_vm_event(PGACTIVATE);
  150. mem_cgroup_move_lists(page, lru);
  151. zone->recent_rotated[!!file]++;
  152. zone->recent_scanned[!!file]++;
  153. }
  154. spin_unlock_irq(&zone->lru_lock);
  155. }
  156. /*
  157. * Mark a page as having seen activity.
  158. *
  159. * inactive,unreferenced -> inactive,referenced
  160. * inactive,referenced -> active,unreferenced
  161. * active,unreferenced -> active,referenced
  162. */
  163. void mark_page_accessed(struct page *page)
  164. {
  165. if (!PageActive(page) && !PageUnevictable(page) &&
  166. PageReferenced(page) && PageLRU(page)) {
  167. activate_page(page);
  168. ClearPageReferenced(page);
  169. } else if (!PageReferenced(page)) {
  170. SetPageReferenced(page);
  171. }
  172. }
  173. EXPORT_SYMBOL(mark_page_accessed);
  174. void __lru_cache_add(struct page *page, enum lru_list lru)
  175. {
  176. struct pagevec *pvec = &get_cpu_var(lru_add_pvecs)[lru];
  177. page_cache_get(page);
  178. if (!pagevec_add(pvec, page))
  179. ____pagevec_lru_add(pvec, lru);
  180. put_cpu_var(lru_add_pvecs);
  181. }
  182. /**
  183. * lru_cache_add_lru - add a page to a page list
  184. * @page: the page to be added to the LRU.
  185. * @lru: the LRU list to which the page is added.
  186. */
  187. void lru_cache_add_lru(struct page *page, enum lru_list lru)
  188. {
  189. if (PageActive(page)) {
  190. VM_BUG_ON(PageUnevictable(page));
  191. ClearPageActive(page);
  192. } else if (PageUnevictable(page)) {
  193. VM_BUG_ON(PageActive(page));
  194. ClearPageUnevictable(page);
  195. }
  196. VM_BUG_ON(PageLRU(page) || PageActive(page) || PageUnevictable(page));
  197. __lru_cache_add(page, lru);
  198. }
  199. /**
  200. * add_page_to_unevictable_list - add a page to the unevictable list
  201. * @page: the page to be added to the unevictable list
  202. *
  203. * Add page directly to its zone's unevictable list. To avoid races with
  204. * tasks that might be making the page evictable, through eg. munlock,
  205. * munmap or exit, while it's not on the lru, we want to add the page
  206. * while it's locked or otherwise "invisible" to other tasks. This is
  207. * difficult to do when using the pagevec cache, so bypass that.
  208. */
  209. void add_page_to_unevictable_list(struct page *page)
  210. {
  211. struct zone *zone = page_zone(page);
  212. spin_lock_irq(&zone->lru_lock);
  213. SetPageUnevictable(page);
  214. SetPageLRU(page);
  215. add_page_to_lru_list(zone, page, LRU_UNEVICTABLE);
  216. spin_unlock_irq(&zone->lru_lock);
  217. }
  218. /*
  219. * Drain pages out of the cpu's pagevecs.
  220. * Either "cpu" is the current CPU, and preemption has already been
  221. * disabled; or "cpu" is being hot-unplugged, and is already dead.
  222. */
  223. static void drain_cpu_pagevecs(int cpu)
  224. {
  225. struct pagevec *pvecs = per_cpu(lru_add_pvecs, cpu);
  226. struct pagevec *pvec;
  227. int lru;
  228. for_each_lru(lru) {
  229. pvec = &pvecs[lru - LRU_BASE];
  230. if (pagevec_count(pvec))
  231. ____pagevec_lru_add(pvec, lru);
  232. }
  233. pvec = &per_cpu(lru_rotate_pvecs, cpu);
  234. if (pagevec_count(pvec)) {
  235. unsigned long flags;
  236. /* No harm done if a racing interrupt already did this */
  237. local_irq_save(flags);
  238. pagevec_move_tail(pvec);
  239. local_irq_restore(flags);
  240. }
  241. }
  242. void lru_add_drain(void)
  243. {
  244. drain_cpu_pagevecs(get_cpu());
  245. put_cpu();
  246. }
  247. #if defined(CONFIG_NUMA) || defined(CONFIG_UNEVICTABLE_LRU)
  248. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  249. {
  250. lru_add_drain();
  251. }
  252. /*
  253. * Returns 0 for success
  254. */
  255. int lru_add_drain_all(void)
  256. {
  257. return schedule_on_each_cpu(lru_add_drain_per_cpu);
  258. }
  259. #else
  260. /*
  261. * Returns 0 for success
  262. */
  263. int lru_add_drain_all(void)
  264. {
  265. lru_add_drain();
  266. return 0;
  267. }
  268. #endif
  269. /*
  270. * Batched page_cache_release(). Decrement the reference count on all the
  271. * passed pages. If it fell to zero then remove the page from the LRU and
  272. * free it.
  273. *
  274. * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
  275. * for the remainder of the operation.
  276. *
  277. * The locking in this function is against shrink_inactive_list(): we recheck
  278. * the page count inside the lock to see whether shrink_inactive_list()
  279. * grabbed the page via the LRU. If it did, give up: shrink_inactive_list()
  280. * will free it.
  281. */
  282. void release_pages(struct page **pages, int nr, int cold)
  283. {
  284. int i;
  285. struct pagevec pages_to_free;
  286. struct zone *zone = NULL;
  287. unsigned long uninitialized_var(flags);
  288. pagevec_init(&pages_to_free, cold);
  289. for (i = 0; i < nr; i++) {
  290. struct page *page = pages[i];
  291. if (unlikely(PageCompound(page))) {
  292. if (zone) {
  293. spin_unlock_irqrestore(&zone->lru_lock, flags);
  294. zone = NULL;
  295. }
  296. put_compound_page(page);
  297. continue;
  298. }
  299. if (!put_page_testzero(page))
  300. continue;
  301. if (PageLRU(page)) {
  302. struct zone *pagezone = page_zone(page);
  303. if (pagezone != zone) {
  304. if (zone)
  305. spin_unlock_irqrestore(&zone->lru_lock,
  306. flags);
  307. zone = pagezone;
  308. spin_lock_irqsave(&zone->lru_lock, flags);
  309. }
  310. VM_BUG_ON(!PageLRU(page));
  311. __ClearPageLRU(page);
  312. del_page_from_lru(zone, page);
  313. }
  314. if (!pagevec_add(&pages_to_free, page)) {
  315. if (zone) {
  316. spin_unlock_irqrestore(&zone->lru_lock, flags);
  317. zone = NULL;
  318. }
  319. __pagevec_free(&pages_to_free);
  320. pagevec_reinit(&pages_to_free);
  321. }
  322. }
  323. if (zone)
  324. spin_unlock_irqrestore(&zone->lru_lock, flags);
  325. pagevec_free(&pages_to_free);
  326. }
  327. /*
  328. * The pages which we're about to release may be in the deferred lru-addition
  329. * queues. That would prevent them from really being freed right now. That's
  330. * OK from a correctness point of view but is inefficient - those pages may be
  331. * cache-warm and we want to give them back to the page allocator ASAP.
  332. *
  333. * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
  334. * and __pagevec_lru_add_active() call release_pages() directly to avoid
  335. * mutual recursion.
  336. */
  337. void __pagevec_release(struct pagevec *pvec)
  338. {
  339. lru_add_drain();
  340. release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
  341. pagevec_reinit(pvec);
  342. }
  343. EXPORT_SYMBOL(__pagevec_release);
  344. /*
  345. * pagevec_release() for pages which are known to not be on the LRU
  346. *
  347. * This function reinitialises the caller's pagevec.
  348. */
  349. void __pagevec_release_nonlru(struct pagevec *pvec)
  350. {
  351. int i;
  352. struct pagevec pages_to_free;
  353. pagevec_init(&pages_to_free, pvec->cold);
  354. for (i = 0; i < pagevec_count(pvec); i++) {
  355. struct page *page = pvec->pages[i];
  356. VM_BUG_ON(PageLRU(page));
  357. if (put_page_testzero(page))
  358. pagevec_add(&pages_to_free, page);
  359. }
  360. pagevec_free(&pages_to_free);
  361. pagevec_reinit(pvec);
  362. }
  363. /*
  364. * Add the passed pages to the LRU, then drop the caller's refcount
  365. * on them. Reinitialises the caller's pagevec.
  366. */
  367. void ____pagevec_lru_add(struct pagevec *pvec, enum lru_list lru)
  368. {
  369. int i;
  370. struct zone *zone = NULL;
  371. VM_BUG_ON(is_unevictable_lru(lru));
  372. for (i = 0; i < pagevec_count(pvec); i++) {
  373. struct page *page = pvec->pages[i];
  374. struct zone *pagezone = page_zone(page);
  375. if (pagezone != zone) {
  376. if (zone)
  377. spin_unlock_irq(&zone->lru_lock);
  378. zone = pagezone;
  379. spin_lock_irq(&zone->lru_lock);
  380. }
  381. VM_BUG_ON(PageActive(page));
  382. VM_BUG_ON(PageUnevictable(page));
  383. VM_BUG_ON(PageLRU(page));
  384. SetPageLRU(page);
  385. if (is_active_lru(lru))
  386. SetPageActive(page);
  387. add_page_to_lru_list(zone, page, lru);
  388. }
  389. if (zone)
  390. spin_unlock_irq(&zone->lru_lock);
  391. release_pages(pvec->pages, pvec->nr, pvec->cold);
  392. pagevec_reinit(pvec);
  393. }
  394. EXPORT_SYMBOL(____pagevec_lru_add);
  395. /*
  396. * Try to drop buffers from the pages in a pagevec
  397. */
  398. void pagevec_strip(struct pagevec *pvec)
  399. {
  400. int i;
  401. for (i = 0; i < pagevec_count(pvec); i++) {
  402. struct page *page = pvec->pages[i];
  403. if (PagePrivate(page) && trylock_page(page)) {
  404. if (PagePrivate(page))
  405. try_to_release_page(page, 0);
  406. unlock_page(page);
  407. }
  408. }
  409. }
  410. /**
  411. * pagevec_swap_free - try to free swap space from the pages in a pagevec
  412. * @pvec: pagevec with swapcache pages to free the swap space of
  413. *
  414. * The caller needs to hold an extra reference to each page and
  415. * not hold the page lock on the pages. This function uses a
  416. * trylock on the page lock so it may not always free the swap
  417. * space associated with a page.
  418. */
  419. void pagevec_swap_free(struct pagevec *pvec)
  420. {
  421. int i;
  422. for (i = 0; i < pagevec_count(pvec); i++) {
  423. struct page *page = pvec->pages[i];
  424. if (PageSwapCache(page) && trylock_page(page)) {
  425. if (PageSwapCache(page))
  426. remove_exclusive_swap_page_ref(page);
  427. unlock_page(page);
  428. }
  429. }
  430. }
  431. /**
  432. * pagevec_lookup - gang pagecache lookup
  433. * @pvec: Where the resulting pages are placed
  434. * @mapping: The address_space to search
  435. * @start: The starting page index
  436. * @nr_pages: The maximum number of pages
  437. *
  438. * pagevec_lookup() will search for and return a group of up to @nr_pages pages
  439. * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
  440. * reference against the pages in @pvec.
  441. *
  442. * The search returns a group of mapping-contiguous pages with ascending
  443. * indexes. There may be holes in the indices due to not-present pages.
  444. *
  445. * pagevec_lookup() returns the number of pages which were found.
  446. */
  447. unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
  448. pgoff_t start, unsigned nr_pages)
  449. {
  450. pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
  451. return pagevec_count(pvec);
  452. }
  453. EXPORT_SYMBOL(pagevec_lookup);
  454. unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
  455. pgoff_t *index, int tag, unsigned nr_pages)
  456. {
  457. pvec->nr = find_get_pages_tag(mapping, index, tag,
  458. nr_pages, pvec->pages);
  459. return pagevec_count(pvec);
  460. }
  461. EXPORT_SYMBOL(pagevec_lookup_tag);
  462. #ifdef CONFIG_SMP
  463. /*
  464. * We tolerate a little inaccuracy to avoid ping-ponging the counter between
  465. * CPUs
  466. */
  467. #define ACCT_THRESHOLD max(16, NR_CPUS * 2)
  468. static DEFINE_PER_CPU(long, committed_space);
  469. void vm_acct_memory(long pages)
  470. {
  471. long *local;
  472. preempt_disable();
  473. local = &__get_cpu_var(committed_space);
  474. *local += pages;
  475. if (*local > ACCT_THRESHOLD || *local < -ACCT_THRESHOLD) {
  476. atomic_long_add(*local, &vm_committed_space);
  477. *local = 0;
  478. }
  479. preempt_enable();
  480. }
  481. #ifdef CONFIG_HOTPLUG_CPU
  482. /* Drop the CPU's cached committed space back into the central pool. */
  483. static int cpu_swap_callback(struct notifier_block *nfb,
  484. unsigned long action,
  485. void *hcpu)
  486. {
  487. long *committed;
  488. committed = &per_cpu(committed_space, (long)hcpu);
  489. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  490. atomic_long_add(*committed, &vm_committed_space);
  491. *committed = 0;
  492. drain_cpu_pagevecs((long)hcpu);
  493. }
  494. return NOTIFY_OK;
  495. }
  496. #endif /* CONFIG_HOTPLUG_CPU */
  497. #endif /* CONFIG_SMP */
  498. /*
  499. * Perform any setup for the swap system
  500. */
  501. void __init swap_setup(void)
  502. {
  503. unsigned long megs = num_physpages >> (20 - PAGE_SHIFT);
  504. #ifdef CONFIG_SWAP
  505. bdi_init(swapper_space.backing_dev_info);
  506. #endif
  507. /* Use a smaller cluster for small-memory machines */
  508. if (megs < 16)
  509. page_cluster = 2;
  510. else
  511. page_cluster = 3;
  512. /*
  513. * Right now other parts of the system means that we
  514. * _really_ don't want to cluster much more
  515. */
  516. #ifdef CONFIG_HOTPLUG_CPU
  517. hotcpu_notifier(cpu_swap_callback, 0);
  518. #endif
  519. }