swap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. /**
  198. * lru_cache_add_lru - add a page to a page list
  199. * @page: the page to be added to the LRU.
  200. * @lru: the LRU list to which the page is added.
  201. */
  202. void lru_cache_add_lru(struct page *page, enum lru_list lru)
  203. {
  204. if (PageActive(page)) {
  205. VM_BUG_ON(PageUnevictable(page));
  206. ClearPageActive(page);
  207. } else if (PageUnevictable(page)) {
  208. VM_BUG_ON(PageActive(page));
  209. ClearPageUnevictable(page);
  210. }
  211. VM_BUG_ON(PageLRU(page) || PageActive(page) || PageUnevictable(page));
  212. __lru_cache_add(page, lru);
  213. }
  214. /**
  215. * add_page_to_unevictable_list - add a page to the unevictable list
  216. * @page: the page to be added to the unevictable list
  217. *
  218. * Add page directly to its zone's unevictable list. To avoid races with
  219. * tasks that might be making the page evictable, through eg. munlock,
  220. * munmap or exit, while it's not on the lru, we want to add the page
  221. * while it's locked or otherwise "invisible" to other tasks. This is
  222. * difficult to do when using the pagevec cache, so bypass that.
  223. */
  224. void add_page_to_unevictable_list(struct page *page)
  225. {
  226. struct zone *zone = page_zone(page);
  227. spin_lock_irq(&zone->lru_lock);
  228. SetPageUnevictable(page);
  229. SetPageLRU(page);
  230. add_page_to_lru_list(zone, page, LRU_UNEVICTABLE);
  231. spin_unlock_irq(&zone->lru_lock);
  232. }
  233. /*
  234. * Drain pages out of the cpu's pagevecs.
  235. * Either "cpu" is the current CPU, and preemption has already been
  236. * disabled; or "cpu" is being hot-unplugged, and is already dead.
  237. */
  238. static void drain_cpu_pagevecs(int cpu)
  239. {
  240. struct pagevec *pvecs = per_cpu(lru_add_pvecs, cpu);
  241. struct pagevec *pvec;
  242. int lru;
  243. for_each_lru(lru) {
  244. pvec = &pvecs[lru - LRU_BASE];
  245. if (pagevec_count(pvec))
  246. ____pagevec_lru_add(pvec, lru);
  247. }
  248. pvec = &per_cpu(lru_rotate_pvecs, cpu);
  249. if (pagevec_count(pvec)) {
  250. unsigned long flags;
  251. /* No harm done if a racing interrupt already did this */
  252. local_irq_save(flags);
  253. pagevec_move_tail(pvec);
  254. local_irq_restore(flags);
  255. }
  256. }
  257. void lru_add_drain(void)
  258. {
  259. drain_cpu_pagevecs(get_cpu());
  260. put_cpu();
  261. }
  262. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  263. {
  264. lru_add_drain();
  265. }
  266. /*
  267. * Returns 0 for success
  268. */
  269. int lru_add_drain_all(void)
  270. {
  271. return schedule_on_each_cpu(lru_add_drain_per_cpu);
  272. }
  273. /*
  274. * Batched page_cache_release(). Decrement the reference count on all the
  275. * passed pages. If it fell to zero then remove the page from the LRU and
  276. * free it.
  277. *
  278. * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
  279. * for the remainder of the operation.
  280. *
  281. * The locking in this function is against shrink_inactive_list(): we recheck
  282. * the page count inside the lock to see whether shrink_inactive_list()
  283. * grabbed the page via the LRU. If it did, give up: shrink_inactive_list()
  284. * will free it.
  285. */
  286. void release_pages(struct page **pages, int nr, int cold)
  287. {
  288. int i;
  289. struct pagevec pages_to_free;
  290. struct zone *zone = NULL;
  291. unsigned long uninitialized_var(flags);
  292. pagevec_init(&pages_to_free, cold);
  293. for (i = 0; i < nr; i++) {
  294. struct page *page = pages[i];
  295. if (unlikely(PageCompound(page))) {
  296. if (zone) {
  297. spin_unlock_irqrestore(&zone->lru_lock, flags);
  298. zone = NULL;
  299. }
  300. put_compound_page(page);
  301. continue;
  302. }
  303. if (!put_page_testzero(page))
  304. continue;
  305. if (PageLRU(page)) {
  306. struct zone *pagezone = page_zone(page);
  307. if (pagezone != zone) {
  308. if (zone)
  309. spin_unlock_irqrestore(&zone->lru_lock,
  310. flags);
  311. zone = pagezone;
  312. spin_lock_irqsave(&zone->lru_lock, flags);
  313. }
  314. VM_BUG_ON(!PageLRU(page));
  315. __ClearPageLRU(page);
  316. del_page_from_lru(zone, page);
  317. }
  318. if (!pagevec_add(&pages_to_free, page)) {
  319. if (zone) {
  320. spin_unlock_irqrestore(&zone->lru_lock, flags);
  321. zone = NULL;
  322. }
  323. __pagevec_free(&pages_to_free);
  324. pagevec_reinit(&pages_to_free);
  325. }
  326. }
  327. if (zone)
  328. spin_unlock_irqrestore(&zone->lru_lock, flags);
  329. pagevec_free(&pages_to_free);
  330. }
  331. /*
  332. * The pages which we're about to release may be in the deferred lru-addition
  333. * queues. That would prevent them from really being freed right now. That's
  334. * OK from a correctness point of view but is inefficient - those pages may be
  335. * cache-warm and we want to give them back to the page allocator ASAP.
  336. *
  337. * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
  338. * and __pagevec_lru_add_active() call release_pages() directly to avoid
  339. * mutual recursion.
  340. */
  341. void __pagevec_release(struct pagevec *pvec)
  342. {
  343. lru_add_drain();
  344. release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
  345. pagevec_reinit(pvec);
  346. }
  347. EXPORT_SYMBOL(__pagevec_release);
  348. /*
  349. * Add the passed pages to the LRU, then drop the caller's refcount
  350. * on them. Reinitialises the caller's pagevec.
  351. */
  352. void ____pagevec_lru_add(struct pagevec *pvec, enum lru_list lru)
  353. {
  354. int i;
  355. struct zone *zone = NULL;
  356. VM_BUG_ON(is_unevictable_lru(lru));
  357. for (i = 0; i < pagevec_count(pvec); i++) {
  358. struct page *page = pvec->pages[i];
  359. struct zone *pagezone = page_zone(page);
  360. int file;
  361. int active;
  362. if (pagezone != zone) {
  363. if (zone)
  364. spin_unlock_irq(&zone->lru_lock);
  365. zone = pagezone;
  366. spin_lock_irq(&zone->lru_lock);
  367. }
  368. VM_BUG_ON(PageActive(page));
  369. VM_BUG_ON(PageUnevictable(page));
  370. VM_BUG_ON(PageLRU(page));
  371. SetPageLRU(page);
  372. active = is_active_lru(lru);
  373. file = is_file_lru(lru);
  374. if (active)
  375. SetPageActive(page);
  376. update_page_reclaim_stat(zone, page, file, active);
  377. add_page_to_lru_list(zone, page, lru);
  378. }
  379. if (zone)
  380. spin_unlock_irq(&zone->lru_lock);
  381. release_pages(pvec->pages, pvec->nr, pvec->cold);
  382. pagevec_reinit(pvec);
  383. }
  384. EXPORT_SYMBOL(____pagevec_lru_add);
  385. /*
  386. * Try to drop buffers from the pages in a pagevec
  387. */
  388. void pagevec_strip(struct pagevec *pvec)
  389. {
  390. int i;
  391. for (i = 0; i < pagevec_count(pvec); i++) {
  392. struct page *page = pvec->pages[i];
  393. if (page_has_private(page) && trylock_page(page)) {
  394. if (page_has_private(page))
  395. try_to_release_page(page, 0);
  396. unlock_page(page);
  397. }
  398. }
  399. }
  400. /**
  401. * pagevec_lookup - gang pagecache lookup
  402. * @pvec: Where the resulting pages are placed
  403. * @mapping: The address_space to search
  404. * @start: The starting page index
  405. * @nr_pages: The maximum number of pages
  406. *
  407. * pagevec_lookup() will search for and return a group of up to @nr_pages pages
  408. * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
  409. * reference against the pages in @pvec.
  410. *
  411. * The search returns a group of mapping-contiguous pages with ascending
  412. * indexes. There may be holes in the indices due to not-present pages.
  413. *
  414. * pagevec_lookup() returns the number of pages which were found.
  415. */
  416. unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
  417. pgoff_t start, unsigned nr_pages)
  418. {
  419. pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
  420. return pagevec_count(pvec);
  421. }
  422. EXPORT_SYMBOL(pagevec_lookup);
  423. unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
  424. pgoff_t *index, int tag, unsigned nr_pages)
  425. {
  426. pvec->nr = find_get_pages_tag(mapping, index, tag,
  427. nr_pages, pvec->pages);
  428. return pagevec_count(pvec);
  429. }
  430. EXPORT_SYMBOL(pagevec_lookup_tag);
  431. /*
  432. * Perform any setup for the swap system
  433. */
  434. void __init swap_setup(void)
  435. {
  436. unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
  437. #ifdef CONFIG_SWAP
  438. bdi_init(swapper_space.backing_dev_info);
  439. #endif
  440. /* Use a smaller cluster for small-memory machines */
  441. if (megs < 16)
  442. page_cluster = 2;
  443. else
  444. page_cluster = 3;
  445. /*
  446. * Right now other parts of the system means that we
  447. * _really_ don't want to cluster much more
  448. */
  449. }