swap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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;
  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 (!PageReserved(page) && 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 (PageReserved(page) || !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. pages_to_free.cold = pvec->cold;
  243. for (i = 0; i < pagevec_count(pvec); i++) {
  244. struct page *page = pvec->pages[i];
  245. BUG_ON(PageLRU(page));
  246. if (put_page_testzero(page))
  247. pagevec_add(&pages_to_free, page);
  248. }
  249. pagevec_free(&pages_to_free);
  250. pagevec_reinit(pvec);
  251. }
  252. /*
  253. * Add the passed pages to the LRU, then drop the caller's refcount
  254. * on them. Reinitialises the caller's pagevec.
  255. */
  256. void __pagevec_lru_add(struct pagevec *pvec)
  257. {
  258. int i;
  259. struct zone *zone = NULL;
  260. for (i = 0; i < pagevec_count(pvec); i++) {
  261. struct page *page = pvec->pages[i];
  262. struct zone *pagezone = page_zone(page);
  263. if (pagezone != zone) {
  264. if (zone)
  265. spin_unlock_irq(&zone->lru_lock);
  266. zone = pagezone;
  267. spin_lock_irq(&zone->lru_lock);
  268. }
  269. if (TestSetPageLRU(page))
  270. BUG();
  271. add_page_to_inactive_list(zone, page);
  272. }
  273. if (zone)
  274. spin_unlock_irq(&zone->lru_lock);
  275. release_pages(pvec->pages, pvec->nr, pvec->cold);
  276. pagevec_reinit(pvec);
  277. }
  278. EXPORT_SYMBOL(__pagevec_lru_add);
  279. void __pagevec_lru_add_active(struct pagevec *pvec)
  280. {
  281. int i;
  282. struct zone *zone = NULL;
  283. for (i = 0; i < pagevec_count(pvec); i++) {
  284. struct page *page = pvec->pages[i];
  285. struct zone *pagezone = page_zone(page);
  286. if (pagezone != zone) {
  287. if (zone)
  288. spin_unlock_irq(&zone->lru_lock);
  289. zone = pagezone;
  290. spin_lock_irq(&zone->lru_lock);
  291. }
  292. if (TestSetPageLRU(page))
  293. BUG();
  294. if (TestSetPageActive(page))
  295. BUG();
  296. add_page_to_active_list(zone, page);
  297. }
  298. if (zone)
  299. spin_unlock_irq(&zone->lru_lock);
  300. release_pages(pvec->pages, pvec->nr, pvec->cold);
  301. pagevec_reinit(pvec);
  302. }
  303. /*
  304. * Try to drop buffers from the pages in a pagevec
  305. */
  306. void pagevec_strip(struct pagevec *pvec)
  307. {
  308. int i;
  309. for (i = 0; i < pagevec_count(pvec); i++) {
  310. struct page *page = pvec->pages[i];
  311. if (PagePrivate(page) && !TestSetPageLocked(page)) {
  312. try_to_release_page(page, 0);
  313. unlock_page(page);
  314. }
  315. }
  316. }
  317. /**
  318. * pagevec_lookup - gang pagecache lookup
  319. * @pvec: Where the resulting pages are placed
  320. * @mapping: The address_space to search
  321. * @start: The starting page index
  322. * @nr_pages: The maximum number of pages
  323. *
  324. * pagevec_lookup() will search for and return a group of up to @nr_pages pages
  325. * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
  326. * reference against the pages in @pvec.
  327. *
  328. * The search returns a group of mapping-contiguous pages with ascending
  329. * indexes. There may be holes in the indices due to not-present pages.
  330. *
  331. * pagevec_lookup() returns the number of pages which were found.
  332. */
  333. unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
  334. pgoff_t start, unsigned nr_pages)
  335. {
  336. pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
  337. return pagevec_count(pvec);
  338. }
  339. unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
  340. pgoff_t *index, int tag, unsigned nr_pages)
  341. {
  342. pvec->nr = find_get_pages_tag(mapping, index, tag,
  343. nr_pages, pvec->pages);
  344. return pagevec_count(pvec);
  345. }
  346. #ifdef CONFIG_SMP
  347. /*
  348. * We tolerate a little inaccuracy to avoid ping-ponging the counter between
  349. * CPUs
  350. */
  351. #define ACCT_THRESHOLD max(16, NR_CPUS * 2)
  352. static DEFINE_PER_CPU(long, committed_space) = 0;
  353. void vm_acct_memory(long pages)
  354. {
  355. long *local;
  356. preempt_disable();
  357. local = &__get_cpu_var(committed_space);
  358. *local += pages;
  359. if (*local > ACCT_THRESHOLD || *local < -ACCT_THRESHOLD) {
  360. atomic_add(*local, &vm_committed_space);
  361. *local = 0;
  362. }
  363. preempt_enable();
  364. }
  365. EXPORT_SYMBOL(vm_acct_memory);
  366. #ifdef CONFIG_HOTPLUG_CPU
  367. static void lru_drain_cache(unsigned int cpu)
  368. {
  369. struct pagevec *pvec = &per_cpu(lru_add_pvecs, cpu);
  370. /* CPU is dead, so no locking needed. */
  371. if (pagevec_count(pvec))
  372. __pagevec_lru_add(pvec);
  373. pvec = &per_cpu(lru_add_active_pvecs, cpu);
  374. if (pagevec_count(pvec))
  375. __pagevec_lru_add_active(pvec);
  376. }
  377. /* Drop the CPU's cached committed space back into the central pool. */
  378. static int cpu_swap_callback(struct notifier_block *nfb,
  379. unsigned long action,
  380. void *hcpu)
  381. {
  382. long *committed;
  383. committed = &per_cpu(committed_space, (long)hcpu);
  384. if (action == CPU_DEAD) {
  385. atomic_add(*committed, &vm_committed_space);
  386. *committed = 0;
  387. lru_drain_cache((long)hcpu);
  388. }
  389. return NOTIFY_OK;
  390. }
  391. #endif /* CONFIG_HOTPLUG_CPU */
  392. #endif /* CONFIG_SMP */
  393. #ifdef CONFIG_SMP
  394. void percpu_counter_mod(struct percpu_counter *fbc, long amount)
  395. {
  396. long count;
  397. long *pcount;
  398. int cpu = get_cpu();
  399. pcount = per_cpu_ptr(fbc->counters, cpu);
  400. count = *pcount + amount;
  401. if (count >= FBC_BATCH || count <= -FBC_BATCH) {
  402. spin_lock(&fbc->lock);
  403. fbc->count += count;
  404. spin_unlock(&fbc->lock);
  405. count = 0;
  406. }
  407. *pcount = count;
  408. put_cpu();
  409. }
  410. EXPORT_SYMBOL(percpu_counter_mod);
  411. #endif
  412. /*
  413. * Perform any setup for the swap system
  414. */
  415. void __init swap_setup(void)
  416. {
  417. unsigned long megs = num_physpages >> (20 - PAGE_SHIFT);
  418. /* Use a smaller cluster for small-memory machines */
  419. if (megs < 16)
  420. page_cluster = 2;
  421. else
  422. page_cluster = 3;
  423. /*
  424. * Right now other parts of the system means that we
  425. * _really_ don't want to cluster much more
  426. */
  427. hotcpu_notifier(cpu_swap_callback, 0);
  428. }