swap.c 15 KB

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