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