swap.c 16 KB

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