swap.c 12 KB

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