swap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. free_hot_cold_page(page, 0);
  53. }
  54. static void put_compound_page(struct page *page)
  55. {
  56. page = compound_head(page);
  57. if (put_page_testzero(page)) {
  58. compound_page_dtor *dtor;
  59. dtor = get_compound_page_dtor(page);
  60. (*dtor)(page);
  61. }
  62. }
  63. void put_page(struct page *page)
  64. {
  65. if (unlikely(PageCompound(page)))
  66. put_compound_page(page);
  67. else if (put_page_testzero(page))
  68. __page_cache_release(page);
  69. }
  70. EXPORT_SYMBOL(put_page);
  71. /**
  72. * put_pages_list() - release a list of pages
  73. * @pages: list of pages threaded on page->lru
  74. *
  75. * Release a list of pages which are strung together on page.lru. Currently
  76. * used by read_cache_pages() and related error recovery code.
  77. */
  78. void put_pages_list(struct list_head *pages)
  79. {
  80. while (!list_empty(pages)) {
  81. struct page *victim;
  82. victim = list_entry(pages->prev, struct page, lru);
  83. list_del(&victim->lru);
  84. page_cache_release(victim);
  85. }
  86. }
  87. EXPORT_SYMBOL(put_pages_list);
  88. /*
  89. * pagevec_move_tail() must be called with IRQ disabled.
  90. * Otherwise this may cause nasty races.
  91. */
  92. static void pagevec_move_tail(struct pagevec *pvec)
  93. {
  94. int i;
  95. int pgmoved = 0;
  96. struct zone *zone = NULL;
  97. for (i = 0; i < pagevec_count(pvec); i++) {
  98. struct page *page = pvec->pages[i];
  99. struct zone *pagezone = page_zone(page);
  100. if (pagezone != zone) {
  101. if (zone)
  102. spin_unlock(&zone->lru_lock);
  103. zone = pagezone;
  104. spin_lock(&zone->lru_lock);
  105. }
  106. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  107. int lru = page_lru_base_type(page);
  108. list_move_tail(&page->lru, &zone->lru[lru].list);
  109. pgmoved++;
  110. }
  111. }
  112. if (zone)
  113. spin_unlock(&zone->lru_lock);
  114. __count_vm_events(PGROTATED, pgmoved);
  115. release_pages(pvec->pages, pvec->nr, pvec->cold);
  116. pagevec_reinit(pvec);
  117. }
  118. /*
  119. * Writeback is about to end against a page which has been marked for immediate
  120. * reclaim. If it still appears to be reclaimable, move it to the tail of the
  121. * inactive list.
  122. */
  123. void rotate_reclaimable_page(struct page *page)
  124. {
  125. if (!PageLocked(page) && !PageDirty(page) && !PageActive(page) &&
  126. !PageUnevictable(page) && PageLRU(page)) {
  127. struct pagevec *pvec;
  128. unsigned long flags;
  129. page_cache_get(page);
  130. local_irq_save(flags);
  131. pvec = &__get_cpu_var(lru_rotate_pvecs);
  132. if (!pagevec_add(pvec, page))
  133. pagevec_move_tail(pvec);
  134. local_irq_restore(flags);
  135. }
  136. }
  137. static void update_page_reclaim_stat(struct zone *zone, struct page *page,
  138. int file, int rotated)
  139. {
  140. struct zone_reclaim_stat *reclaim_stat = &zone->reclaim_stat;
  141. struct zone_reclaim_stat *memcg_reclaim_stat;
  142. memcg_reclaim_stat = mem_cgroup_get_reclaim_stat_from_page(page);
  143. reclaim_stat->recent_scanned[file]++;
  144. if (rotated)
  145. reclaim_stat->recent_rotated[file]++;
  146. if (!memcg_reclaim_stat)
  147. return;
  148. memcg_reclaim_stat->recent_scanned[file]++;
  149. if (rotated)
  150. memcg_reclaim_stat->recent_rotated[file]++;
  151. }
  152. /*
  153. * FIXME: speed this up?
  154. */
  155. void activate_page(struct page *page)
  156. {
  157. struct zone *zone = page_zone(page);
  158. spin_lock_irq(&zone->lru_lock);
  159. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  160. int file = page_is_file_cache(page);
  161. int lru = page_lru_base_type(page);
  162. del_page_from_lru_list(zone, page, lru);
  163. SetPageActive(page);
  164. lru += LRU_ACTIVE;
  165. add_page_to_lru_list(zone, page, lru);
  166. __count_vm_event(PGACTIVATE);
  167. update_page_reclaim_stat(zone, page, file, 1);
  168. }
  169. spin_unlock_irq(&zone->lru_lock);
  170. }
  171. /*
  172. * Mark a page as having seen activity.
  173. *
  174. * inactive,unreferenced -> inactive,referenced
  175. * inactive,referenced -> active,unreferenced
  176. * active,unreferenced -> active,referenced
  177. */
  178. void mark_page_accessed(struct page *page)
  179. {
  180. if (!PageActive(page) && !PageUnevictable(page) &&
  181. PageReferenced(page) && PageLRU(page)) {
  182. activate_page(page);
  183. ClearPageReferenced(page);
  184. } else if (!PageReferenced(page)) {
  185. SetPageReferenced(page);
  186. }
  187. }
  188. EXPORT_SYMBOL(mark_page_accessed);
  189. void __lru_cache_add(struct page *page, enum lru_list lru)
  190. {
  191. struct pagevec *pvec = &get_cpu_var(lru_add_pvecs)[lru];
  192. page_cache_get(page);
  193. if (!pagevec_add(pvec, page))
  194. ____pagevec_lru_add(pvec, lru);
  195. put_cpu_var(lru_add_pvecs);
  196. }
  197. EXPORT_SYMBOL(__lru_cache_add);
  198. /**
  199. * lru_cache_add_lru - add a page to a page list
  200. * @page: the page to be added to the LRU.
  201. * @lru: the LRU list to which the page is added.
  202. */
  203. void lru_cache_add_lru(struct page *page, enum lru_list lru)
  204. {
  205. if (PageActive(page)) {
  206. VM_BUG_ON(PageUnevictable(page));
  207. ClearPageActive(page);
  208. } else if (PageUnevictable(page)) {
  209. VM_BUG_ON(PageActive(page));
  210. ClearPageUnevictable(page);
  211. }
  212. VM_BUG_ON(PageLRU(page) || PageActive(page) || PageUnevictable(page));
  213. __lru_cache_add(page, lru);
  214. }
  215. /**
  216. * add_page_to_unevictable_list - add a page to the unevictable list
  217. * @page: the page to be added to the unevictable list
  218. *
  219. * Add page directly to its zone's unevictable list. To avoid races with
  220. * tasks that might be making the page evictable, through eg. munlock,
  221. * munmap or exit, while it's not on the lru, we want to add the page
  222. * while it's locked or otherwise "invisible" to other tasks. This is
  223. * difficult to do when using the pagevec cache, so bypass that.
  224. */
  225. void add_page_to_unevictable_list(struct page *page)
  226. {
  227. struct zone *zone = page_zone(page);
  228. spin_lock_irq(&zone->lru_lock);
  229. SetPageUnevictable(page);
  230. SetPageLRU(page);
  231. add_page_to_lru_list(zone, page, LRU_UNEVICTABLE);
  232. spin_unlock_irq(&zone->lru_lock);
  233. }
  234. /*
  235. * Drain pages out of the cpu's pagevecs.
  236. * Either "cpu" is the current CPU, and preemption has already been
  237. * disabled; or "cpu" is being hot-unplugged, and is already dead.
  238. */
  239. static void drain_cpu_pagevecs(int cpu)
  240. {
  241. struct pagevec *pvecs = per_cpu(lru_add_pvecs, cpu);
  242. struct pagevec *pvec;
  243. int lru;
  244. for_each_lru(lru) {
  245. pvec = &pvecs[lru - LRU_BASE];
  246. if (pagevec_count(pvec))
  247. ____pagevec_lru_add(pvec, lru);
  248. }
  249. pvec = &per_cpu(lru_rotate_pvecs, cpu);
  250. if (pagevec_count(pvec)) {
  251. unsigned long flags;
  252. /* No harm done if a racing interrupt already did this */
  253. local_irq_save(flags);
  254. pagevec_move_tail(pvec);
  255. local_irq_restore(flags);
  256. }
  257. }
  258. void lru_add_drain(void)
  259. {
  260. drain_cpu_pagevecs(get_cpu());
  261. put_cpu();
  262. }
  263. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  264. {
  265. lru_add_drain();
  266. }
  267. /*
  268. * Returns 0 for success
  269. */
  270. int lru_add_drain_all(void)
  271. {
  272. return schedule_on_each_cpu(lru_add_drain_per_cpu);
  273. }
  274. /*
  275. * Batched page_cache_release(). Decrement the reference count on all the
  276. * passed pages. If it fell to zero then remove the page from the LRU and
  277. * free it.
  278. *
  279. * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
  280. * for the remainder of the operation.
  281. *
  282. * The locking in this function is against shrink_inactive_list(): we recheck
  283. * the page count inside the lock to see whether shrink_inactive_list()
  284. * grabbed the page via the LRU. If it did, give up: shrink_inactive_list()
  285. * will free it.
  286. */
  287. void release_pages(struct page **pages, int nr, int cold)
  288. {
  289. int i;
  290. struct pagevec pages_to_free;
  291. struct zone *zone = NULL;
  292. unsigned long uninitialized_var(flags);
  293. pagevec_init(&pages_to_free, cold);
  294. for (i = 0; i < nr; i++) {
  295. struct page *page = pages[i];
  296. if (unlikely(PageCompound(page))) {
  297. if (zone) {
  298. spin_unlock_irqrestore(&zone->lru_lock, flags);
  299. zone = NULL;
  300. }
  301. put_compound_page(page);
  302. continue;
  303. }
  304. if (!put_page_testzero(page))
  305. continue;
  306. if (PageLRU(page)) {
  307. struct zone *pagezone = page_zone(page);
  308. if (pagezone != zone) {
  309. if (zone)
  310. spin_unlock_irqrestore(&zone->lru_lock,
  311. flags);
  312. zone = pagezone;
  313. spin_lock_irqsave(&zone->lru_lock, flags);
  314. }
  315. VM_BUG_ON(!PageLRU(page));
  316. __ClearPageLRU(page);
  317. del_page_from_lru(zone, page);
  318. }
  319. if (!pagevec_add(&pages_to_free, page)) {
  320. if (zone) {
  321. spin_unlock_irqrestore(&zone->lru_lock, flags);
  322. zone = NULL;
  323. }
  324. __pagevec_free(&pages_to_free);
  325. pagevec_reinit(&pages_to_free);
  326. }
  327. }
  328. if (zone)
  329. spin_unlock_irqrestore(&zone->lru_lock, flags);
  330. pagevec_free(&pages_to_free);
  331. }
  332. /*
  333. * The pages which we're about to release may be in the deferred lru-addition
  334. * queues. That would prevent them from really being freed right now. That's
  335. * OK from a correctness point of view but is inefficient - those pages may be
  336. * cache-warm and we want to give them back to the page allocator ASAP.
  337. *
  338. * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
  339. * and __pagevec_lru_add_active() call release_pages() directly to avoid
  340. * mutual recursion.
  341. */
  342. void __pagevec_release(struct pagevec *pvec)
  343. {
  344. lru_add_drain();
  345. release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
  346. pagevec_reinit(pvec);
  347. }
  348. EXPORT_SYMBOL(__pagevec_release);
  349. /*
  350. * Add the passed pages to the LRU, then drop the caller's refcount
  351. * on them. Reinitialises the caller's pagevec.
  352. */
  353. void ____pagevec_lru_add(struct pagevec *pvec, enum lru_list lru)
  354. {
  355. int i;
  356. struct zone *zone = NULL;
  357. VM_BUG_ON(is_unevictable_lru(lru));
  358. for (i = 0; i < pagevec_count(pvec); i++) {
  359. struct page *page = pvec->pages[i];
  360. struct zone *pagezone = page_zone(page);
  361. int file;
  362. int active;
  363. if (pagezone != zone) {
  364. if (zone)
  365. spin_unlock_irq(&zone->lru_lock);
  366. zone = pagezone;
  367. spin_lock_irq(&zone->lru_lock);
  368. }
  369. VM_BUG_ON(PageActive(page));
  370. VM_BUG_ON(PageUnevictable(page));
  371. VM_BUG_ON(PageLRU(page));
  372. SetPageLRU(page);
  373. active = is_active_lru(lru);
  374. file = is_file_lru(lru);
  375. if (active)
  376. SetPageActive(page);
  377. update_page_reclaim_stat(zone, page, file, active);
  378. add_page_to_lru_list(zone, page, lru);
  379. }
  380. if (zone)
  381. spin_unlock_irq(&zone->lru_lock);
  382. release_pages(pvec->pages, pvec->nr, pvec->cold);
  383. pagevec_reinit(pvec);
  384. }
  385. EXPORT_SYMBOL(____pagevec_lru_add);
  386. /*
  387. * Try to drop buffers from the pages in a pagevec
  388. */
  389. void pagevec_strip(struct pagevec *pvec)
  390. {
  391. int i;
  392. for (i = 0; i < pagevec_count(pvec); i++) {
  393. struct page *page = pvec->pages[i];
  394. if (page_has_private(page) && trylock_page(page)) {
  395. if (page_has_private(page))
  396. try_to_release_page(page, 0);
  397. unlock_page(page);
  398. }
  399. }
  400. }
  401. /**
  402. * pagevec_lookup - gang pagecache lookup
  403. * @pvec: Where the resulting pages are placed
  404. * @mapping: The address_space to search
  405. * @start: The starting page index
  406. * @nr_pages: The maximum number of pages
  407. *
  408. * pagevec_lookup() will search for and return a group of up to @nr_pages pages
  409. * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
  410. * reference against the pages in @pvec.
  411. *
  412. * The search returns a group of mapping-contiguous pages with ascending
  413. * indexes. There may be holes in the indices due to not-present pages.
  414. *
  415. * pagevec_lookup() returns the number of pages which were found.
  416. */
  417. unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
  418. pgoff_t start, unsigned nr_pages)
  419. {
  420. pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
  421. return pagevec_count(pvec);
  422. }
  423. EXPORT_SYMBOL(pagevec_lookup);
  424. unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
  425. pgoff_t *index, int tag, unsigned nr_pages)
  426. {
  427. pvec->nr = find_get_pages_tag(mapping, index, tag,
  428. nr_pages, pvec->pages);
  429. return pagevec_count(pvec);
  430. }
  431. EXPORT_SYMBOL(pagevec_lookup_tag);
  432. /*
  433. * Perform any setup for the swap system
  434. */
  435. void __init swap_setup(void)
  436. {
  437. unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
  438. #ifdef CONFIG_SWAP
  439. bdi_init(swapper_space.backing_dev_info);
  440. #endif
  441. /* Use a smaller cluster for small-memory machines */
  442. if (megs < 16)
  443. page_cluster = 2;
  444. else
  445. page_cluster = 3;
  446. /*
  447. * Right now other parts of the system means that we
  448. * _really_ don't want to cluster much more
  449. */
  450. }