swap.c 13 KB

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