swap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. static void __lru_add_drain(int cpu)
  142. {
  143. struct pagevec *pvec = &per_cpu(lru_add_pvecs, cpu);
  144. /* CPU is dead, so no locking needed. */
  145. if (pagevec_count(pvec))
  146. __pagevec_lru_add(pvec);
  147. pvec = &per_cpu(lru_add_active_pvecs, cpu);
  148. if (pagevec_count(pvec))
  149. __pagevec_lru_add_active(pvec);
  150. }
  151. void lru_add_drain(void)
  152. {
  153. __lru_add_drain(get_cpu());
  154. put_cpu();
  155. }
  156. #ifdef CONFIG_NUMA
  157. static void lru_add_drain_per_cpu(void *dummy)
  158. {
  159. lru_add_drain();
  160. }
  161. /*
  162. * Returns 0 for success
  163. */
  164. int lru_add_drain_all(void)
  165. {
  166. return schedule_on_each_cpu(lru_add_drain_per_cpu, NULL);
  167. }
  168. #else
  169. /*
  170. * Returns 0 for success
  171. */
  172. int lru_add_drain_all(void)
  173. {
  174. lru_add_drain();
  175. return 0;
  176. }
  177. #endif
  178. /*
  179. * This path almost never happens for VM activity - pages are normally
  180. * freed via pagevecs. But it gets used by networking.
  181. */
  182. void fastcall __page_cache_release(struct page *page)
  183. {
  184. unsigned long flags;
  185. struct zone *zone = page_zone(page);
  186. spin_lock_irqsave(&zone->lru_lock, flags);
  187. if (TestClearPageLRU(page))
  188. del_page_from_lru(zone, page);
  189. if (page_count(page) != 0)
  190. page = NULL;
  191. spin_unlock_irqrestore(&zone->lru_lock, flags);
  192. if (page)
  193. free_hot_page(page);
  194. }
  195. EXPORT_SYMBOL(__page_cache_release);
  196. /*
  197. * Batched page_cache_release(). Decrement the reference count on all the
  198. * passed pages. If it fell to zero then remove the page from the LRU and
  199. * free it.
  200. *
  201. * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
  202. * for the remainder of the operation.
  203. *
  204. * The locking in this function is against shrink_cache(): we recheck the
  205. * page count inside the lock to see whether shrink_cache grabbed the page
  206. * via the LRU. If it did, give up: shrink_cache will free it.
  207. */
  208. void release_pages(struct page **pages, int nr, int cold)
  209. {
  210. int i;
  211. struct pagevec pages_to_free;
  212. struct zone *zone = NULL;
  213. pagevec_init(&pages_to_free, cold);
  214. for (i = 0; i < nr; i++) {
  215. struct page *page = pages[i];
  216. struct zone *pagezone;
  217. if (!put_page_testzero(page))
  218. continue;
  219. pagezone = page_zone(page);
  220. if (pagezone != zone) {
  221. if (zone)
  222. spin_unlock_irq(&zone->lru_lock);
  223. zone = pagezone;
  224. spin_lock_irq(&zone->lru_lock);
  225. }
  226. if (TestClearPageLRU(page))
  227. del_page_from_lru(zone, page);
  228. if (page_count(page) == 0) {
  229. if (!pagevec_add(&pages_to_free, page)) {
  230. spin_unlock_irq(&zone->lru_lock);
  231. __pagevec_free(&pages_to_free);
  232. pagevec_reinit(&pages_to_free);
  233. zone = NULL; /* No lock is held */
  234. }
  235. }
  236. }
  237. if (zone)
  238. spin_unlock_irq(&zone->lru_lock);
  239. pagevec_free(&pages_to_free);
  240. }
  241. /*
  242. * The pages which we're about to release may be in the deferred lru-addition
  243. * queues. That would prevent them from really being freed right now. That's
  244. * OK from a correctness point of view but is inefficient - those pages may be
  245. * cache-warm and we want to give them back to the page allocator ASAP.
  246. *
  247. * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
  248. * and __pagevec_lru_add_active() call release_pages() directly to avoid
  249. * mutual recursion.
  250. */
  251. void __pagevec_release(struct pagevec *pvec)
  252. {
  253. lru_add_drain();
  254. release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
  255. pagevec_reinit(pvec);
  256. }
  257. EXPORT_SYMBOL(__pagevec_release);
  258. /*
  259. * pagevec_release() for pages which are known to not be on the LRU
  260. *
  261. * This function reinitialises the caller's pagevec.
  262. */
  263. void __pagevec_release_nonlru(struct pagevec *pvec)
  264. {
  265. int i;
  266. struct pagevec pages_to_free;
  267. pagevec_init(&pages_to_free, pvec->cold);
  268. for (i = 0; i < pagevec_count(pvec); i++) {
  269. struct page *page = pvec->pages[i];
  270. BUG_ON(PageLRU(page));
  271. if (put_page_testzero(page))
  272. pagevec_add(&pages_to_free, page);
  273. }
  274. pagevec_free(&pages_to_free);
  275. pagevec_reinit(pvec);
  276. }
  277. /*
  278. * Add the passed pages to the LRU, then drop the caller's refcount
  279. * on them. Reinitialises the caller's pagevec.
  280. */
  281. void __pagevec_lru_add(struct pagevec *pvec)
  282. {
  283. int i;
  284. struct zone *zone = NULL;
  285. for (i = 0; i < pagevec_count(pvec); i++) {
  286. struct page *page = pvec->pages[i];
  287. struct zone *pagezone = page_zone(page);
  288. if (pagezone != zone) {
  289. if (zone)
  290. spin_unlock_irq(&zone->lru_lock);
  291. zone = pagezone;
  292. spin_lock_irq(&zone->lru_lock);
  293. }
  294. if (TestSetPageLRU(page))
  295. BUG();
  296. add_page_to_inactive_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. EXPORT_SYMBOL(__pagevec_lru_add);
  304. void __pagevec_lru_add_active(struct pagevec *pvec)
  305. {
  306. int i;
  307. struct zone *zone = NULL;
  308. for (i = 0; i < pagevec_count(pvec); i++) {
  309. struct page *page = pvec->pages[i];
  310. struct zone *pagezone = page_zone(page);
  311. if (pagezone != zone) {
  312. if (zone)
  313. spin_unlock_irq(&zone->lru_lock);
  314. zone = pagezone;
  315. spin_lock_irq(&zone->lru_lock);
  316. }
  317. if (TestSetPageLRU(page))
  318. BUG();
  319. if (TestSetPageActive(page))
  320. BUG();
  321. add_page_to_active_list(zone, page);
  322. }
  323. if (zone)
  324. spin_unlock_irq(&zone->lru_lock);
  325. release_pages(pvec->pages, pvec->nr, pvec->cold);
  326. pagevec_reinit(pvec);
  327. }
  328. /*
  329. * Try to drop buffers from the pages in a pagevec
  330. */
  331. void pagevec_strip(struct pagevec *pvec)
  332. {
  333. int i;
  334. for (i = 0; i < pagevec_count(pvec); i++) {
  335. struct page *page = pvec->pages[i];
  336. if (PagePrivate(page) && !TestSetPageLocked(page)) {
  337. try_to_release_page(page, 0);
  338. unlock_page(page);
  339. }
  340. }
  341. }
  342. /**
  343. * pagevec_lookup - gang pagecache lookup
  344. * @pvec: Where the resulting pages are placed
  345. * @mapping: The address_space to search
  346. * @start: The starting page index
  347. * @nr_pages: The maximum number of pages
  348. *
  349. * pagevec_lookup() will search for and return a group of up to @nr_pages pages
  350. * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
  351. * reference against the pages in @pvec.
  352. *
  353. * The search returns a group of mapping-contiguous pages with ascending
  354. * indexes. There may be holes in the indices due to not-present pages.
  355. *
  356. * pagevec_lookup() returns the number of pages which were found.
  357. */
  358. unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
  359. pgoff_t start, unsigned nr_pages)
  360. {
  361. pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
  362. return pagevec_count(pvec);
  363. }
  364. EXPORT_SYMBOL(pagevec_lookup);
  365. unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
  366. pgoff_t *index, int tag, unsigned nr_pages)
  367. {
  368. pvec->nr = find_get_pages_tag(mapping, index, tag,
  369. nr_pages, pvec->pages);
  370. return pagevec_count(pvec);
  371. }
  372. EXPORT_SYMBOL(pagevec_lookup_tag);
  373. #ifdef CONFIG_SMP
  374. /*
  375. * We tolerate a little inaccuracy to avoid ping-ponging the counter between
  376. * CPUs
  377. */
  378. #define ACCT_THRESHOLD max(16, NR_CPUS * 2)
  379. static DEFINE_PER_CPU(long, committed_space) = 0;
  380. void vm_acct_memory(long pages)
  381. {
  382. long *local;
  383. preempt_disable();
  384. local = &__get_cpu_var(committed_space);
  385. *local += pages;
  386. if (*local > ACCT_THRESHOLD || *local < -ACCT_THRESHOLD) {
  387. atomic_add(*local, &vm_committed_space);
  388. *local = 0;
  389. }
  390. preempt_enable();
  391. }
  392. #ifdef CONFIG_HOTPLUG_CPU
  393. /* Drop the CPU's cached committed space back into the central pool. */
  394. static int cpu_swap_callback(struct notifier_block *nfb,
  395. unsigned long action,
  396. void *hcpu)
  397. {
  398. long *committed;
  399. committed = &per_cpu(committed_space, (long)hcpu);
  400. if (action == CPU_DEAD) {
  401. atomic_add(*committed, &vm_committed_space);
  402. *committed = 0;
  403. __lru_add_drain((long)hcpu);
  404. }
  405. return NOTIFY_OK;
  406. }
  407. #endif /* CONFIG_HOTPLUG_CPU */
  408. #endif /* CONFIG_SMP */
  409. #ifdef CONFIG_SMP
  410. void percpu_counter_mod(struct percpu_counter *fbc, long amount)
  411. {
  412. long count;
  413. long *pcount;
  414. int cpu = get_cpu();
  415. pcount = per_cpu_ptr(fbc->counters, cpu);
  416. count = *pcount + amount;
  417. if (count >= FBC_BATCH || count <= -FBC_BATCH) {
  418. spin_lock(&fbc->lock);
  419. fbc->count += count;
  420. spin_unlock(&fbc->lock);
  421. count = 0;
  422. }
  423. *pcount = count;
  424. put_cpu();
  425. }
  426. EXPORT_SYMBOL(percpu_counter_mod);
  427. #endif
  428. /*
  429. * Perform any setup for the swap system
  430. */
  431. void __init swap_setup(void)
  432. {
  433. unsigned long megs = num_physpages >> (20 - PAGE_SHIFT);
  434. /* Use a smaller cluster for small-memory machines */
  435. if (megs < 16)
  436. page_cluster = 2;
  437. else
  438. page_cluster = 3;
  439. /*
  440. * Right now other parts of the system means that we
  441. * _really_ don't want to cluster much more
  442. */
  443. hotcpu_notifier(cpu_swap_callback, 0);
  444. }