[PATCH] Makefile typo
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / swap.c
blob8fd095c4ae51b00b314e732ac9ea5547a5cd26b2
1 /*
2 * linux/mm/swap.c
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 */
7 /*
8 * This file contains the default values for the opereation of the
9 * Linux VM subsystem. Fine-tuning documentation can be found in
10 * Documentation/sysctl/vm.txt.
11 * Started 18.12.91
12 * Swap aging added 23.2.95, Stephen Tweedie.
13 * Buffermem limits added 12.3.98, Rik van Riel.
16 #include <linux/mm.h>
17 #include <linux/sched.h>
18 #include <linux/kernel_stat.h>
19 #include <linux/swap.h>
20 #include <linux/mman.h>
21 #include <linux/pagemap.h>
22 #include <linux/pagevec.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/mm_inline.h>
26 #include <linux/buffer_head.h> /* for try_to_release_page() */
27 #include <linux/module.h>
28 #include <linux/percpu_counter.h>
29 #include <linux/percpu.h>
30 #include <linux/cpu.h>
31 #include <linux/notifier.h>
32 #include <linux/init.h>
34 /* How many pages do we try to swap or page in/out together? */
35 int page_cluster;
37 static void put_compound_page(struct page *page)
39 page = (struct page *)page_private(page);
40 if (put_page_testzero(page)) {
41 void (*dtor)(struct page *page);
43 dtor = (void (*)(struct page *))page[1].lru.next;
44 (*dtor)(page);
48 void put_page(struct page *page)
50 if (unlikely(PageCompound(page)))
51 put_compound_page(page);
52 else if (put_page_testzero(page))
53 __page_cache_release(page);
55 EXPORT_SYMBOL(put_page);
58 * Writeback is about to end against a page which has been marked for immediate
59 * reclaim. If it still appears to be reclaimable, move it to the tail of the
60 * inactive list. The page still has PageWriteback set, which will pin it.
62 * We don't expect many pages to come through here, so don't bother batching
63 * things up.
65 * To avoid placing the page at the tail of the LRU while PG_writeback is still
66 * set, this function will clear PG_writeback before performing the page
67 * motion. Do that inside the lru lock because once PG_writeback is cleared
68 * we may not touch the page.
70 * Returns zero if it cleared PG_writeback.
72 int rotate_reclaimable_page(struct page *page)
74 struct zone *zone;
75 unsigned long flags;
77 if (PageLocked(page))
78 return 1;
79 if (PageDirty(page))
80 return 1;
81 if (PageActive(page))
82 return 1;
83 if (!PageLRU(page))
84 return 1;
86 zone = page_zone(page);
87 spin_lock_irqsave(&zone->lru_lock, flags);
88 if (PageLRU(page) && !PageActive(page)) {
89 list_move_tail(&page->lru, &zone->inactive_list);
90 __count_vm_event(PGROTATED);
92 if (!test_clear_page_writeback(page))
93 BUG();
94 spin_unlock_irqrestore(&zone->lru_lock, flags);
95 return 0;
99 * FIXME: speed this up?
101 void fastcall activate_page(struct page *page)
103 struct zone *zone = page_zone(page);
105 spin_lock_irq(&zone->lru_lock);
106 if (PageLRU(page) && !PageActive(page)) {
107 del_page_from_inactive_list(zone, page);
108 SetPageActive(page);
109 add_page_to_active_list(zone, page);
110 __count_vm_event(PGACTIVATE);
112 spin_unlock_irq(&zone->lru_lock);
116 * Mark a page as having seen activity.
118 * inactive,unreferenced -> inactive,referenced
119 * inactive,referenced -> active,unreferenced
120 * active,unreferenced -> active,referenced
122 void fastcall mark_page_accessed(struct page *page)
124 if (!PageActive(page) && PageReferenced(page) && PageLRU(page)) {
125 activate_page(page);
126 ClearPageReferenced(page);
127 } else if (!PageReferenced(page)) {
128 SetPageReferenced(page);
132 EXPORT_SYMBOL(mark_page_accessed);
135 * lru_cache_add: add a page to the page lists
136 * @page: the page to add
138 static DEFINE_PER_CPU(struct pagevec, lru_add_pvecs) = { 0, };
139 static DEFINE_PER_CPU(struct pagevec, lru_add_active_pvecs) = { 0, };
141 void fastcall lru_cache_add(struct page *page)
143 struct pagevec *pvec = &get_cpu_var(lru_add_pvecs);
145 page_cache_get(page);
146 if (!pagevec_add(pvec, page))
147 __pagevec_lru_add(pvec);
148 put_cpu_var(lru_add_pvecs);
151 void fastcall lru_cache_add_active(struct page *page)
153 struct pagevec *pvec = &get_cpu_var(lru_add_active_pvecs);
155 page_cache_get(page);
156 if (!pagevec_add(pvec, page))
157 __pagevec_lru_add_active(pvec);
158 put_cpu_var(lru_add_active_pvecs);
161 static void __lru_add_drain(int cpu)
163 struct pagevec *pvec = &per_cpu(lru_add_pvecs, cpu);
165 /* CPU is dead, so no locking needed. */
166 if (pagevec_count(pvec))
167 __pagevec_lru_add(pvec);
168 pvec = &per_cpu(lru_add_active_pvecs, cpu);
169 if (pagevec_count(pvec))
170 __pagevec_lru_add_active(pvec);
173 void lru_add_drain(void)
175 __lru_add_drain(get_cpu());
176 put_cpu();
179 #ifdef CONFIG_NUMA
180 static void lru_add_drain_per_cpu(void *dummy)
182 lru_add_drain();
186 * Returns 0 for success
188 int lru_add_drain_all(void)
190 return schedule_on_each_cpu(lru_add_drain_per_cpu, NULL);
193 #else
196 * Returns 0 for success
198 int lru_add_drain_all(void)
200 lru_add_drain();
201 return 0;
203 #endif
206 * This path almost never happens for VM activity - pages are normally
207 * freed via pagevecs. But it gets used by networking.
209 void fastcall __page_cache_release(struct page *page)
211 if (PageLRU(page)) {
212 unsigned long flags;
213 struct zone *zone = page_zone(page);
215 spin_lock_irqsave(&zone->lru_lock, flags);
216 BUG_ON(!PageLRU(page));
217 __ClearPageLRU(page);
218 del_page_from_lru(zone, page);
219 spin_unlock_irqrestore(&zone->lru_lock, flags);
221 free_hot_page(page);
223 EXPORT_SYMBOL(__page_cache_release);
226 * Batched page_cache_release(). Decrement the reference count on all the
227 * passed pages. If it fell to zero then remove the page from the LRU and
228 * free it.
230 * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
231 * for the remainder of the operation.
233 * The locking in this function is against shrink_cache(): we recheck the
234 * page count inside the lock to see whether shrink_cache grabbed the page
235 * via the LRU. If it did, give up: shrink_cache will free it.
237 void release_pages(struct page **pages, int nr, int cold)
239 int i;
240 struct pagevec pages_to_free;
241 struct zone *zone = NULL;
243 pagevec_init(&pages_to_free, cold);
244 for (i = 0; i < nr; i++) {
245 struct page *page = pages[i];
247 if (unlikely(PageCompound(page))) {
248 if (zone) {
249 spin_unlock_irq(&zone->lru_lock);
250 zone = NULL;
252 put_compound_page(page);
253 continue;
256 if (!put_page_testzero(page))
257 continue;
259 if (PageLRU(page)) {
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);
267 BUG_ON(!PageLRU(page));
268 __ClearPageLRU(page);
269 del_page_from_lru(zone, page);
272 if (!pagevec_add(&pages_to_free, page)) {
273 if (zone) {
274 spin_unlock_irq(&zone->lru_lock);
275 zone = NULL;
277 __pagevec_free(&pages_to_free);
278 pagevec_reinit(&pages_to_free);
281 if (zone)
282 spin_unlock_irq(&zone->lru_lock);
284 pagevec_free(&pages_to_free);
288 * The pages which we're about to release may be in the deferred lru-addition
289 * queues. That would prevent them from really being freed right now. That's
290 * OK from a correctness point of view but is inefficient - those pages may be
291 * cache-warm and we want to give them back to the page allocator ASAP.
293 * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
294 * and __pagevec_lru_add_active() call release_pages() directly to avoid
295 * mutual recursion.
297 void __pagevec_release(struct pagevec *pvec)
299 lru_add_drain();
300 release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
301 pagevec_reinit(pvec);
304 EXPORT_SYMBOL(__pagevec_release);
307 * pagevec_release() for pages which are known to not be on the LRU
309 * This function reinitialises the caller's pagevec.
311 void __pagevec_release_nonlru(struct pagevec *pvec)
313 int i;
314 struct pagevec pages_to_free;
316 pagevec_init(&pages_to_free, pvec->cold);
317 for (i = 0; i < pagevec_count(pvec); i++) {
318 struct page *page = pvec->pages[i];
320 BUG_ON(PageLRU(page));
321 if (put_page_testzero(page))
322 pagevec_add(&pages_to_free, page);
324 pagevec_free(&pages_to_free);
325 pagevec_reinit(pvec);
329 * Add the passed pages to the LRU, then drop the caller's refcount
330 * on them. Reinitialises the caller's pagevec.
332 void __pagevec_lru_add(struct pagevec *pvec)
334 int i;
335 struct zone *zone = NULL;
337 for (i = 0; i < pagevec_count(pvec); i++) {
338 struct page *page = pvec->pages[i];
339 struct zone *pagezone = page_zone(page);
341 if (pagezone != zone) {
342 if (zone)
343 spin_unlock_irq(&zone->lru_lock);
344 zone = pagezone;
345 spin_lock_irq(&zone->lru_lock);
347 BUG_ON(PageLRU(page));
348 SetPageLRU(page);
349 add_page_to_inactive_list(zone, page);
351 if (zone)
352 spin_unlock_irq(&zone->lru_lock);
353 release_pages(pvec->pages, pvec->nr, pvec->cold);
354 pagevec_reinit(pvec);
357 EXPORT_SYMBOL(__pagevec_lru_add);
359 void __pagevec_lru_add_active(struct pagevec *pvec)
361 int i;
362 struct zone *zone = NULL;
364 for (i = 0; i < pagevec_count(pvec); i++) {
365 struct page *page = pvec->pages[i];
366 struct zone *pagezone = page_zone(page);
368 if (pagezone != zone) {
369 if (zone)
370 spin_unlock_irq(&zone->lru_lock);
371 zone = pagezone;
372 spin_lock_irq(&zone->lru_lock);
374 BUG_ON(PageLRU(page));
375 SetPageLRU(page);
376 BUG_ON(PageActive(page));
377 SetPageActive(page);
378 add_page_to_active_list(zone, page);
380 if (zone)
381 spin_unlock_irq(&zone->lru_lock);
382 release_pages(pvec->pages, pvec->nr, pvec->cold);
383 pagevec_reinit(pvec);
387 * Try to drop buffers from the pages in a pagevec
389 void pagevec_strip(struct pagevec *pvec)
391 int i;
393 for (i = 0; i < pagevec_count(pvec); i++) {
394 struct page *page = pvec->pages[i];
396 if (PagePrivate(page) && !TestSetPageLocked(page)) {
397 if (PagePrivate(page))
398 try_to_release_page(page, 0);
399 unlock_page(page);
405 * pagevec_lookup - gang pagecache lookup
406 * @pvec: Where the resulting pages are placed
407 * @mapping: The address_space to search
408 * @start: The starting page index
409 * @nr_pages: The maximum number of pages
411 * pagevec_lookup() will search for and return a group of up to @nr_pages pages
412 * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
413 * reference against the pages in @pvec.
415 * The search returns a group of mapping-contiguous pages with ascending
416 * indexes. There may be holes in the indices due to not-present pages.
418 * pagevec_lookup() returns the number of pages which were found.
420 unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
421 pgoff_t start, unsigned nr_pages)
423 pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
424 return pagevec_count(pvec);
427 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)
432 pvec->nr = find_get_pages_tag(mapping, index, tag,
433 nr_pages, pvec->pages);
434 return pagevec_count(pvec);
437 EXPORT_SYMBOL(pagevec_lookup_tag);
439 #ifdef CONFIG_SMP
441 * We tolerate a little inaccuracy to avoid ping-ponging the counter between
442 * CPUs
444 #define ACCT_THRESHOLD max(16, NR_CPUS * 2)
446 static DEFINE_PER_CPU(long, committed_space) = 0;
448 void vm_acct_memory(long pages)
450 long *local;
452 preempt_disable();
453 local = &__get_cpu_var(committed_space);
454 *local += pages;
455 if (*local > ACCT_THRESHOLD || *local < -ACCT_THRESHOLD) {
456 atomic_add(*local, &vm_committed_space);
457 *local = 0;
459 preempt_enable();
462 #ifdef CONFIG_HOTPLUG_CPU
464 /* Drop the CPU's cached committed space back into the central pool. */
465 static int cpu_swap_callback(struct notifier_block *nfb,
466 unsigned long action,
467 void *hcpu)
469 long *committed;
471 committed = &per_cpu(committed_space, (long)hcpu);
472 if (action == CPU_DEAD) {
473 atomic_add(*committed, &vm_committed_space);
474 *committed = 0;
475 __lru_add_drain((long)hcpu);
477 return NOTIFY_OK;
479 #endif /* CONFIG_HOTPLUG_CPU */
480 #endif /* CONFIG_SMP */
483 * Perform any setup for the swap system
485 void __init swap_setup(void)
487 unsigned long megs = num_physpages >> (20 - PAGE_SHIFT);
489 /* Use a smaller cluster for small-memory machines */
490 if (megs < 16)
491 page_cluster = 2;
492 else
493 page_cluster = 3;
495 * Right now other parts of the system means that we
496 * _really_ don't want to cluster much more
498 hotcpu_notifier(cpu_swap_callback, 0);