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