swap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 opereation 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/module.h>
  26. #include <linux/percpu_counter.h>
  27. #include <linux/percpu.h>
  28. #include <linux/cpu.h>
  29. #include <linux/notifier.h>
  30. #include <linux/init.h>
  31. /* How many pages do we try to swap or page in/out together? */
  32. int page_cluster;
  33. #ifdef CONFIG_HUGETLB_PAGE
  34. void put_page(struct page *page)
  35. {
  36. if (unlikely(PageCompound(page))) {
  37. page = (struct page *)page_private(page);
  38. if (put_page_testzero(page)) {
  39. void (*dtor)(struct page *page);
  40. dtor = (void (*)(struct page *))page[1].mapping;
  41. (*dtor)(page);
  42. }
  43. return;
  44. }
  45. if (put_page_testzero(page))
  46. __page_cache_release(page);
  47. }
  48. EXPORT_SYMBOL(put_page);
  49. #endif
  50. /*
  51. * Writeback is about to end against a page which has been marked for immediate
  52. * reclaim. If it still appears to be reclaimable, move it to the tail of the
  53. * inactive list. The page still has PageWriteback set, which will pin it.
  54. *
  55. * We don't expect many pages to come through here, so don't bother batching
  56. * things up.
  57. *
  58. * To avoid placing the page at the tail of the LRU while PG_writeback is still
  59. * set, this function will clear PG_writeback before performing the page
  60. * motion. Do that inside the lru lock because once PG_writeback is cleared
  61. * we may not touch the page.
  62. *
  63. * Returns zero if it cleared PG_writeback.
  64. */
  65. int rotate_reclaimable_page(struct page *page)
  66. {
  67. struct zone *zone;
  68. unsigned long flags;
  69. if (PageLocked(page))
  70. return 1;
  71. if (PageDirty(page))
  72. return 1;
  73. if (PageActive(page))
  74. return 1;
  75. if (!PageLRU(page))
  76. return 1;
  77. zone = page_zone(page);
  78. spin_lock_irqsave(&zone->lru_lock, flags);
  79. if (PageLRU(page) && !PageActive(page)) {
  80. list_del(&page->lru);
  81. list_add_tail(&page->lru, &zone->inactive_list);
  82. inc_page_state(pgrotated);
  83. }
  84. if (!test_clear_page_writeback(page))
  85. BUG();
  86. spin_unlock_irqrestore(&zone->lru_lock, flags);
  87. return 0;
  88. }
  89. /*
  90. * FIXME: speed this up?
  91. */
  92. void fastcall activate_page(struct page *page)
  93. {
  94. struct zone *zone = page_zone(page);
  95. spin_lock_irq(&zone->lru_lock);
  96. if (PageLRU(page) && !PageActive(page)) {
  97. del_page_from_inactive_list(zone, page);
  98. SetPageActive(page);
  99. add_page_to_active_list(zone, page);
  100. inc_page_state(pgactivate);
  101. }
  102. spin_unlock_irq(&zone->lru_lock);
  103. }
  104. /*
  105. * Mark a page as having seen activity.
  106. *
  107. * inactive,unreferenced -> inactive,referenced
  108. * inactive,referenced -> active,unreferenced
  109. * active,unreferenced -> active,referenced
  110. */
  111. void fastcall mark_page_accessed(struct page *page)
  112. {
  113. if (!PageActive(page) && PageReferenced(page) && PageLRU(page)) {
  114. activate_page(page);
  115. ClearPageReferenced(page);
  116. } else if (!PageReferenced(page)) {
  117. SetPageReferenced(page);
  118. }
  119. }
  120. EXPORT_SYMBOL(mark_page_accessed);
  121. /**
  122. * lru_cache_add: add a page to the page lists
  123. * @page: the page to add
  124. */
  125. static DEFINE_PER_CPU(struct pagevec, lru_add_pvecs) = { 0, };
  126. static DEFINE_PER_CPU(struct pagevec, lru_add_active_pvecs) = { 0, };
  127. void fastcall lru_cache_add(struct page *page)
  128. {
  129. struct pagevec *pvec = &get_cpu_var(lru_add_pvecs);
  130. page_cache_get(page);
  131. if (!pagevec_add(pvec, page))
  132. __pagevec_lru_add(pvec);
  133. put_cpu_var(lru_add_pvecs);
  134. }
  135. void fastcall lru_cache_add_active(struct page *page)
  136. {
  137. struct pagevec *pvec = &get_cpu_var(lru_add_active_pvecs);
  138. page_cache_get(page);
  139. if (!pagevec_add(pvec, page))
  140. __pagevec_lru_add_active(pvec);
  141. put_cpu_var(lru_add_active_pvecs);
  142. }
  143. void lru_add_drain(void)
  144. {
  145. struct pagevec *pvec = &get_cpu_var(lru_add_pvecs);
  146. if (pagevec_count(pvec))
  147. __pagevec_lru_add(pvec);
  148. pvec = &__get_cpu_var(lru_add_active_pvecs);
  149. if (pagevec_count(pvec))
  150. __pagevec_lru_add_active(pvec);
  151. put_cpu_var(lru_add_pvecs);
  152. }
  153. /*
  154. * This path almost never happens for VM activity - pages are normally
  155. * freed via pagevecs. But it gets used by networking.
  156. */
  157. void fastcall __page_cache_release(struct page *page)
  158. {
  159. unsigned long flags;
  160. struct zone *zone = page_zone(page);
  161. spin_lock_irqsave(&zone->lru_lock, flags);
  162. if (TestClearPageLRU(page))
  163. del_page_from_lru(zone, page);
  164. if (page_count(page) != 0)
  165. page = NULL;
  166. spin_unlock_irqrestore(&zone->lru_lock, flags);
  167. if (page)
  168. free_hot_page(page);
  169. }
  170. EXPORT_SYMBOL(__page_cache_release);
  171. /*
  172. * Batched page_cache_release(). Decrement the reference count on all the
  173. * passed pages. If it fell to zero then remove the page from the LRU and
  174. * free it.
  175. *
  176. * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
  177. * for the remainder of the operation.
  178. *
  179. * The locking in this function is against shrink_cache(): we recheck the
  180. * page count inside the lock to see whether shrink_cache grabbed the page
  181. * via the LRU. If it did, give up: shrink_cache will free it.
  182. */
  183. void release_pages(struct page **pages, int nr, int cold)
  184. {
  185. int i;
  186. struct pagevec pages_to_free;
  187. struct zone *zone = NULL;
  188. pagevec_init(&pages_to_free, cold);
  189. for (i = 0; i < nr; i++) {
  190. struct page *page = pages[i];
  191. struct zone *pagezone;
  192. if (!put_page_testzero(page))
  193. continue;
  194. pagezone = page_zone(page);
  195. if (pagezone != zone) {
  196. if (zone)
  197. spin_unlock_irq(&zone->lru_lock);
  198. zone = pagezone;
  199. spin_lock_irq(&zone->lru_lock);
  200. }
  201. if (TestClearPageLRU(page))
  202. del_page_from_lru(zone, page);
  203. if (page_count(page) == 0) {
  204. if (!pagevec_add(&pages_to_free, page)) {
  205. spin_unlock_irq(&zone->lru_lock);
  206. __pagevec_free(&pages_to_free);
  207. pagevec_reinit(&pages_to_free);
  208. zone = NULL; /* No lock is held */
  209. }
  210. }
  211. }
  212. if (zone)
  213. spin_unlock_irq(&zone->lru_lock);
  214. pagevec_free(&pages_to_free);
  215. }
  216. /*
  217. * The pages which we're about to release may be in the deferred lru-addition
  218. * queues. That would prevent them from really being freed right now. That's
  219. * OK from a correctness point of view but is inefficient - those pages may be
  220. * cache-warm and we want to give them back to the page allocator ASAP.
  221. *
  222. * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
  223. * and __pagevec_lru_add_active() call release_pages() directly to avoid
  224. * mutual recursion.
  225. */
  226. void __pagevec_release(struct pagevec *pvec)
  227. {
  228. lru_add_drain();
  229. release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
  230. pagevec_reinit(pvec);
  231. }
  232. /*
  233. * pagevec_release() for pages which are known to not be on the LRU
  234. *
  235. * This function reinitialises the caller's pagevec.
  236. */
  237. void __pagevec_release_nonlru(struct pagevec *pvec)
  238. {
  239. int i;
  240. struct pagevec pages_to_free;
  241. pagevec_init(&pages_to_free, pvec->cold);
  242. for (i = 0; i < pagevec_count(pvec); i++) {
  243. struct page *page = pvec->pages[i];
  244. BUG_ON(PageLRU(page));
  245. if (put_page_testzero(page))
  246. pagevec_add(&pages_to_free, page);
  247. }
  248. pagevec_free(&pages_to_free);
  249. pagevec_reinit(pvec);
  250. }
  251. /*
  252. * Add the passed pages to the LRU, then drop the caller's refcount
  253. * on them. Reinitialises the caller's pagevec.
  254. */
  255. void __pagevec_lru_add(struct pagevec *pvec)
  256. {
  257. int i;
  258. struct zone *zone = NULL;
  259. for (i = 0; i < pagevec_count(pvec); i++) {
  260. struct page *page = pvec->pages[i];
  261. struct zone *pagezone = page_zone(page);
  262. if (pagezone != zone) {
  263. if (zone)
  264. spin_unlock_irq(&zone->lru_lock);
  265. zone = pagezone;
  266. spin_lock_irq(&zone->lru_lock);
  267. }
  268. if (TestSetPageLRU(page))
  269. BUG();
  270. add_page_to_inactive_list(zone, page);
  271. }
  272. if (zone)
  273. spin_unlock_irq(&zone->lru_lock);
  274. release_pages(pvec->pages, pvec->nr, pvec->cold);
  275. pagevec_reinit(pvec);
  276. }
  277. EXPORT_SYMBOL(__pagevec_lru_add);
  278. void __pagevec_lru_add_active(struct pagevec *pvec)
  279. {
  280. int i;
  281. struct zone *zone = NULL;
  282. for (i = 0; i < pagevec_count(pvec); i++) {
  283. struct page *page = pvec->pages[i];
  284. struct zone *pagezone = page_zone(page);
  285. if (pagezone != zone) {
  286. if (zone)
  287. spin_unlock_irq(&zone->lru_lock);
  288. zone = pagezone;
  289. spin_lock_irq(&zone->lru_lock);
  290. }
  291. if (TestSetPageLRU(page))
  292. BUG();
  293. if (TestSetPageActive(page))
  294. BUG();
  295. add_page_to_active_list(zone, page);
  296. }
  297. if (zone)
  298. spin_unlock_irq(&zone->lru_lock);
  299. release_pages(pvec->pages, pvec->nr, pvec->cold);
  300. pagevec_reinit(pvec);
  301. }
  302. /*
  303. * Try to drop buffers from the pages in a pagevec
  304. */
  305. void pagevec_strip(struct pagevec *pvec)
  306. {
  307. int i;
  308. for (i = 0; i < pagevec_count(pvec); i++) {
  309. struct page *page = pvec->pages[i];
  310. if (PagePrivate(page) && !TestSetPageLocked(page)) {
  311. try_to_release_page(page, 0);
  312. unlock_page(page);
  313. }
  314. }
  315. }
  316. /**
  317. * pagevec_lookup - gang pagecache lookup
  318. * @pvec: Where the resulting pages are placed
  319. * @mapping: The address_space to search
  320. * @start: The starting page index
  321. * @nr_pages: The maximum number of pages
  322. *
  323. * pagevec_lookup() will search for and return a group of up to @nr_pages pages
  324. * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
  325. * reference against the pages in @pvec.
  326. *
  327. * The search returns a group of mapping-contiguous pages with ascending
  328. * indexes. There may be holes in the indices due to not-present pages.
  329. *
  330. * pagevec_lookup() returns the number of pages which were found.
  331. */
  332. unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
  333. pgoff_t start, unsigned nr_pages)
  334. {
  335. pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
  336. return pagevec_count(pvec);
  337. }
  338. unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
  339. pgoff_t *index, int tag, unsigned nr_pages)
  340. {
  341. pvec->nr = find_get_pages_tag(mapping, index, tag,
  342. nr_pages, pvec->pages);
  343. return pagevec_count(pvec);
  344. }
  345. #ifdef CONFIG_SMP
  346. /*
  347. * We tolerate a little inaccuracy to avoid ping-ponging the counter between
  348. * CPUs
  349. */
  350. #define ACCT_THRESHOLD max(16, NR_CPUS * 2)
  351. static DEFINE_PER_CPU(long, committed_space) = 0;
  352. void vm_acct_memory(long pages)
  353. {
  354. long *local;
  355. preempt_disable();
  356. local = &__get_cpu_var(committed_space);
  357. *local += pages;
  358. if (*local > ACCT_THRESHOLD || *local < -ACCT_THRESHOLD) {
  359. atomic_add(*local, &vm_committed_space);
  360. *local = 0;
  361. }
  362. preempt_enable();
  363. }
  364. EXPORT_SYMBOL(vm_acct_memory);
  365. #ifdef CONFIG_HOTPLUG_CPU
  366. static void lru_drain_cache(unsigned int cpu)
  367. {
  368. struct pagevec *pvec = &per_cpu(lru_add_pvecs, cpu);
  369. /* CPU is dead, so no locking needed. */
  370. if (pagevec_count(pvec))
  371. __pagevec_lru_add(pvec);
  372. pvec = &per_cpu(lru_add_active_pvecs, cpu);
  373. if (pagevec_count(pvec))
  374. __pagevec_lru_add_active(pvec);
  375. }
  376. /* Drop the CPU's cached committed space back into the central pool. */
  377. static int cpu_swap_callback(struct notifier_block *nfb,
  378. unsigned long action,
  379. void *hcpu)
  380. {
  381. long *committed;
  382. committed = &per_cpu(committed_space, (long)hcpu);
  383. if (action == CPU_DEAD) {
  384. atomic_add(*committed, &vm_committed_space);
  385. *committed = 0;
  386. lru_drain_cache((long)hcpu);
  387. }
  388. return NOTIFY_OK;
  389. }
  390. #endif /* CONFIG_HOTPLUG_CPU */
  391. #endif /* CONFIG_SMP */
  392. #ifdef CONFIG_SMP
  393. void percpu_counter_mod(struct percpu_counter *fbc, long amount)
  394. {
  395. long count;
  396. long *pcount;
  397. int cpu = get_cpu();
  398. pcount = per_cpu_ptr(fbc->counters, cpu);
  399. count = *pcount + amount;
  400. if (count >= FBC_BATCH || count <= -FBC_BATCH) {
  401. spin_lock(&fbc->lock);
  402. fbc->count += count;
  403. spin_unlock(&fbc->lock);
  404. count = 0;
  405. }
  406. *pcount = count;
  407. put_cpu();
  408. }
  409. EXPORT_SYMBOL(percpu_counter_mod);
  410. #endif
  411. /*
  412. * Perform any setup for the swap system
  413. */
  414. void __init swap_setup(void)
  415. {
  416. unsigned long megs = num_physpages >> (20 - PAGE_SHIFT);
  417. /* Use a smaller cluster for small-memory machines */
  418. if (megs < 16)
  419. page_cluster = 2;
  420. else
  421. page_cluster = 3;
  422. /*
  423. * Right now other parts of the system means that we
  424. * _really_ don't want to cluster much more
  425. */
  426. hotcpu_notifier(cpu_swap_callback, 0);
  427. }