swap.c 13 KB

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