swap.c 13 KB

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