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