swap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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, lru_add_pvecs) = { 0, };
  34. static DEFINE_PER_CPU(struct pagevec, lru_add_active_pvecs) = { 0, };
  35. static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs) = { 0, };
  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)) {
  106. list_move_tail(&page->lru, &zone->inactive_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. 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)) {
  143. del_page_from_inactive_list(zone, page);
  144. SetPageActive(page);
  145. add_page_to_active_list(zone, page);
  146. __count_vm_event(PGACTIVATE);
  147. mem_cgroup_move_lists(page, true);
  148. }
  149. spin_unlock_irq(&zone->lru_lock);
  150. }
  151. /*
  152. * Mark a page as having seen activity.
  153. *
  154. * inactive,unreferenced -> inactive,referenced
  155. * inactive,referenced -> active,unreferenced
  156. * active,unreferenced -> active,referenced
  157. */
  158. void mark_page_accessed(struct page *page)
  159. {
  160. if (!PageActive(page) && PageReferenced(page) && PageLRU(page)) {
  161. activate_page(page);
  162. ClearPageReferenced(page);
  163. } else if (!PageReferenced(page)) {
  164. SetPageReferenced(page);
  165. }
  166. }
  167. EXPORT_SYMBOL(mark_page_accessed);
  168. /**
  169. * lru_cache_add: add a page to the page lists
  170. * @page: the page to add
  171. */
  172. void lru_cache_add(struct page *page)
  173. {
  174. struct pagevec *pvec = &get_cpu_var(lru_add_pvecs);
  175. page_cache_get(page);
  176. if (!pagevec_add(pvec, page))
  177. __pagevec_lru_add(pvec);
  178. put_cpu_var(lru_add_pvecs);
  179. }
  180. void lru_cache_add_active(struct page *page)
  181. {
  182. struct pagevec *pvec = &get_cpu_var(lru_add_active_pvecs);
  183. page_cache_get(page);
  184. if (!pagevec_add(pvec, page))
  185. __pagevec_lru_add_active(pvec);
  186. put_cpu_var(lru_add_active_pvecs);
  187. }
  188. /*
  189. * Drain pages out of the cpu's pagevecs.
  190. * Either "cpu" is the current CPU, and preemption has already been
  191. * disabled; or "cpu" is being hot-unplugged, and is already dead.
  192. */
  193. static void drain_cpu_pagevecs(int cpu)
  194. {
  195. struct pagevec *pvec;
  196. pvec = &per_cpu(lru_add_pvecs, cpu);
  197. if (pagevec_count(pvec))
  198. __pagevec_lru_add(pvec);
  199. pvec = &per_cpu(lru_add_active_pvecs, cpu);
  200. if (pagevec_count(pvec))
  201. __pagevec_lru_add_active(pvec);
  202. pvec = &per_cpu(lru_rotate_pvecs, cpu);
  203. if (pagevec_count(pvec)) {
  204. unsigned long flags;
  205. /* No harm done if a racing interrupt already did this */
  206. local_irq_save(flags);
  207. pagevec_move_tail(pvec);
  208. local_irq_restore(flags);
  209. }
  210. }
  211. void lru_add_drain(void)
  212. {
  213. drain_cpu_pagevecs(get_cpu());
  214. put_cpu();
  215. }
  216. #ifdef CONFIG_NUMA
  217. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  218. {
  219. lru_add_drain();
  220. }
  221. /*
  222. * Returns 0 for success
  223. */
  224. int lru_add_drain_all(void)
  225. {
  226. return schedule_on_each_cpu(lru_add_drain_per_cpu);
  227. }
  228. #else
  229. /*
  230. * Returns 0 for success
  231. */
  232. int lru_add_drain_all(void)
  233. {
  234. lru_add_drain();
  235. return 0;
  236. }
  237. #endif
  238. /*
  239. * Batched page_cache_release(). Decrement the reference count on all the
  240. * passed pages. If it fell to zero then remove the page from the LRU and
  241. * free it.
  242. *
  243. * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
  244. * for the remainder of the operation.
  245. *
  246. * The locking in this function is against shrink_cache(): we recheck the
  247. * page count inside the lock to see whether shrink_cache grabbed the page
  248. * via the LRU. If it did, give up: shrink_cache will free it.
  249. */
  250. void release_pages(struct page **pages, int nr, int cold)
  251. {
  252. int i;
  253. struct pagevec pages_to_free;
  254. struct zone *zone = NULL;
  255. unsigned long uninitialized_var(flags);
  256. pagevec_init(&pages_to_free, cold);
  257. for (i = 0; i < nr; i++) {
  258. struct page *page = pages[i];
  259. if (unlikely(PageCompound(page))) {
  260. if (zone) {
  261. spin_unlock_irqrestore(&zone->lru_lock, flags);
  262. zone = NULL;
  263. }
  264. put_compound_page(page);
  265. continue;
  266. }
  267. if (!put_page_testzero(page))
  268. continue;
  269. if (PageLRU(page)) {
  270. struct zone *pagezone = page_zone(page);
  271. if (pagezone != zone) {
  272. if (zone)
  273. spin_unlock_irqrestore(&zone->lru_lock,
  274. flags);
  275. zone = pagezone;
  276. spin_lock_irqsave(&zone->lru_lock, flags);
  277. }
  278. VM_BUG_ON(!PageLRU(page));
  279. __ClearPageLRU(page);
  280. del_page_from_lru(zone, page);
  281. }
  282. if (!pagevec_add(&pages_to_free, page)) {
  283. if (zone) {
  284. spin_unlock_irqrestore(&zone->lru_lock, flags);
  285. zone = NULL;
  286. }
  287. __pagevec_free(&pages_to_free);
  288. pagevec_reinit(&pages_to_free);
  289. }
  290. }
  291. if (zone)
  292. spin_unlock_irqrestore(&zone->lru_lock, flags);
  293. pagevec_free(&pages_to_free);
  294. }
  295. /*
  296. * The pages which we're about to release may be in the deferred lru-addition
  297. * queues. That would prevent them from really being freed right now. That's
  298. * OK from a correctness point of view but is inefficient - those pages may be
  299. * cache-warm and we want to give them back to the page allocator ASAP.
  300. *
  301. * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
  302. * and __pagevec_lru_add_active() call release_pages() directly to avoid
  303. * mutual recursion.
  304. */
  305. void __pagevec_release(struct pagevec *pvec)
  306. {
  307. lru_add_drain();
  308. release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
  309. pagevec_reinit(pvec);
  310. }
  311. EXPORT_SYMBOL(__pagevec_release);
  312. /*
  313. * pagevec_release() for pages which are known to not be on the LRU
  314. *
  315. * This function reinitialises the caller's pagevec.
  316. */
  317. void __pagevec_release_nonlru(struct pagevec *pvec)
  318. {
  319. int i;
  320. struct pagevec pages_to_free;
  321. pagevec_init(&pages_to_free, pvec->cold);
  322. for (i = 0; i < pagevec_count(pvec); i++) {
  323. struct page *page = pvec->pages[i];
  324. VM_BUG_ON(PageLRU(page));
  325. if (put_page_testzero(page))
  326. pagevec_add(&pages_to_free, page);
  327. }
  328. pagevec_free(&pages_to_free);
  329. pagevec_reinit(pvec);
  330. }
  331. /*
  332. * Add the passed pages to the LRU, then drop the caller's refcount
  333. * on them. Reinitialises the caller's pagevec.
  334. */
  335. void __pagevec_lru_add(struct pagevec *pvec)
  336. {
  337. int i;
  338. struct zone *zone = NULL;
  339. for (i = 0; i < pagevec_count(pvec); i++) {
  340. struct page *page = pvec->pages[i];
  341. struct zone *pagezone = page_zone(page);
  342. if (pagezone != zone) {
  343. if (zone)
  344. spin_unlock_irq(&zone->lru_lock);
  345. zone = pagezone;
  346. spin_lock_irq(&zone->lru_lock);
  347. }
  348. VM_BUG_ON(PageLRU(page));
  349. SetPageLRU(page);
  350. add_page_to_inactive_list(zone, page);
  351. }
  352. if (zone)
  353. spin_unlock_irq(&zone->lru_lock);
  354. release_pages(pvec->pages, pvec->nr, pvec->cold);
  355. pagevec_reinit(pvec);
  356. }
  357. EXPORT_SYMBOL(__pagevec_lru_add);
  358. void __pagevec_lru_add_active(struct pagevec *pvec)
  359. {
  360. int i;
  361. struct zone *zone = NULL;
  362. for (i = 0; i < pagevec_count(pvec); i++) {
  363. struct page *page = pvec->pages[i];
  364. struct zone *pagezone = page_zone(page);
  365. if (pagezone != zone) {
  366. if (zone)
  367. spin_unlock_irq(&zone->lru_lock);
  368. zone = pagezone;
  369. spin_lock_irq(&zone->lru_lock);
  370. }
  371. VM_BUG_ON(PageLRU(page));
  372. SetPageLRU(page);
  373. VM_BUG_ON(PageActive(page));
  374. SetPageActive(page);
  375. add_page_to_active_list(zone, page);
  376. }
  377. if (zone)
  378. spin_unlock_irq(&zone->lru_lock);
  379. release_pages(pvec->pages, pvec->nr, pvec->cold);
  380. pagevec_reinit(pvec);
  381. }
  382. /*
  383. * Try to drop buffers from the pages in a pagevec
  384. */
  385. void pagevec_strip(struct pagevec *pvec)
  386. {
  387. int i;
  388. for (i = 0; i < pagevec_count(pvec); i++) {
  389. struct page *page = pvec->pages[i];
  390. if (PagePrivate(page) && !TestSetPageLocked(page)) {
  391. if (PagePrivate(page))
  392. try_to_release_page(page, 0);
  393. unlock_page(page);
  394. }
  395. }
  396. }
  397. /**
  398. * pagevec_lookup - gang pagecache lookup
  399. * @pvec: Where the resulting pages are placed
  400. * @mapping: The address_space to search
  401. * @start: The starting page index
  402. * @nr_pages: The maximum number of pages
  403. *
  404. * pagevec_lookup() will search for and return a group of up to @nr_pages pages
  405. * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
  406. * reference against the pages in @pvec.
  407. *
  408. * The search returns a group of mapping-contiguous pages with ascending
  409. * indexes. There may be holes in the indices due to not-present pages.
  410. *
  411. * pagevec_lookup() returns the number of pages which were found.
  412. */
  413. unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
  414. pgoff_t start, unsigned nr_pages)
  415. {
  416. pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
  417. return pagevec_count(pvec);
  418. }
  419. EXPORT_SYMBOL(pagevec_lookup);
  420. unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
  421. pgoff_t *index, int tag, unsigned nr_pages)
  422. {
  423. pvec->nr = find_get_pages_tag(mapping, index, tag,
  424. nr_pages, pvec->pages);
  425. return pagevec_count(pvec);
  426. }
  427. EXPORT_SYMBOL(pagevec_lookup_tag);
  428. #ifdef CONFIG_SMP
  429. /*
  430. * We tolerate a little inaccuracy to avoid ping-ponging the counter between
  431. * CPUs
  432. */
  433. #define ACCT_THRESHOLD max(16, NR_CPUS * 2)
  434. static DEFINE_PER_CPU(long, committed_space) = 0;
  435. void vm_acct_memory(long pages)
  436. {
  437. long *local;
  438. preempt_disable();
  439. local = &__get_cpu_var(committed_space);
  440. *local += pages;
  441. if (*local > ACCT_THRESHOLD || *local < -ACCT_THRESHOLD) {
  442. atomic_long_add(*local, &vm_committed_space);
  443. *local = 0;
  444. }
  445. preempt_enable();
  446. }
  447. #ifdef CONFIG_HOTPLUG_CPU
  448. /* Drop the CPU's cached committed space back into the central pool. */
  449. static int cpu_swap_callback(struct notifier_block *nfb,
  450. unsigned long action,
  451. void *hcpu)
  452. {
  453. long *committed;
  454. committed = &per_cpu(committed_space, (long)hcpu);
  455. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  456. atomic_long_add(*committed, &vm_committed_space);
  457. *committed = 0;
  458. drain_cpu_pagevecs((long)hcpu);
  459. }
  460. return NOTIFY_OK;
  461. }
  462. #endif /* CONFIG_HOTPLUG_CPU */
  463. #endif /* CONFIG_SMP */
  464. /*
  465. * Perform any setup for the swap system
  466. */
  467. void __init swap_setup(void)
  468. {
  469. unsigned long megs = num_physpages >> (20 - PAGE_SHIFT);
  470. #ifdef CONFIG_SWAP
  471. bdi_init(swapper_space.backing_dev_info);
  472. #endif
  473. /* Use a smaller cluster for small-memory machines */
  474. if (megs < 16)
  475. page_cluster = 2;
  476. else
  477. page_cluster = 3;
  478. /*
  479. * Right now other parts of the system means that we
  480. * _really_ don't want to cluster much more
  481. */
  482. #ifdef CONFIG_HOTPLUG_CPU
  483. hotcpu_notifier(cpu_swap_callback, 0);
  484. #endif
  485. }