[PATCH] x86_64: Bug correction in populate_memnodemap()
[linux-2.6/mini2440.git] / mm / swap.c
blob73d351439ef6c9cc34cf9d1ad4887692a7d935b4
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 void put_page(struct page *page)
39 if (unlikely(PageCompound(page))) {
40 page = (struct page *)page_private(page);
41 if (put_page_testzero(page)) {
42 void (*dtor)(struct page *page);
44 dtor = (void (*)(struct page *))page[1].mapping;
45 (*dtor)(page);
47 return;
49 if (put_page_testzero(page))
50 __page_cache_release(page);
52 EXPORT_SYMBOL(put_page);
55 * Writeback is about to end against a page which has been marked for immediate
56 * reclaim. If it still appears to be reclaimable, move it to the tail of the
57 * inactive list. The page still has PageWriteback set, which will pin it.
59 * We don't expect many pages to come through here, so don't bother batching
60 * things up.
62 * To avoid placing the page at the tail of the LRU while PG_writeback is still
63 * set, this function will clear PG_writeback before performing the page
64 * motion. Do that inside the lru lock because once PG_writeback is cleared
65 * we may not touch the page.
67 * Returns zero if it cleared PG_writeback.
69 int rotate_reclaimable_page(struct page *page)
71 struct zone *zone;
72 unsigned long flags;
74 if (PageLocked(page))
75 return 1;
76 if (PageDirty(page))
77 return 1;
78 if (PageActive(page))
79 return 1;
80 if (!PageLRU(page))
81 return 1;
83 zone = page_zone(page);
84 spin_lock_irqsave(&zone->lru_lock, flags);
85 if (PageLRU(page) && !PageActive(page)) {
86 list_del(&page->lru);
87 list_add_tail(&page->lru, &zone->inactive_list);
88 inc_page_state(pgrotated);
90 if (!test_clear_page_writeback(page))
91 BUG();
92 spin_unlock_irqrestore(&zone->lru_lock, flags);
93 return 0;
97 * FIXME: speed this up?
99 void fastcall activate_page(struct page *page)
101 struct zone *zone = page_zone(page);
103 spin_lock_irq(&zone->lru_lock);
104 if (PageLRU(page) && !PageActive(page)) {
105 del_page_from_inactive_list(zone, page);
106 SetPageActive(page);
107 add_page_to_active_list(zone, page);
108 inc_page_state(pgactivate);
110 spin_unlock_irq(&zone->lru_lock);
114 * Mark a page as having seen activity.
116 * inactive,unreferenced -> inactive,referenced
117 * inactive,referenced -> active,unreferenced
118 * active,unreferenced -> active,referenced
120 void fastcall mark_page_accessed(struct page *page)
122 if (!PageActive(page) && PageReferenced(page) && PageLRU(page)) {
123 activate_page(page);
124 ClearPageReferenced(page);
125 } else if (!PageReferenced(page)) {
126 SetPageReferenced(page);
130 EXPORT_SYMBOL(mark_page_accessed);
133 * lru_cache_add: add a page to the page lists
134 * @page: the page to add
136 static DEFINE_PER_CPU(struct pagevec, lru_add_pvecs) = { 0, };
137 static DEFINE_PER_CPU(struct pagevec, lru_add_active_pvecs) = { 0, };
139 void fastcall lru_cache_add(struct page *page)
141 struct pagevec *pvec = &get_cpu_var(lru_add_pvecs);
143 page_cache_get(page);
144 if (!pagevec_add(pvec, page))
145 __pagevec_lru_add(pvec);
146 put_cpu_var(lru_add_pvecs);
149 void fastcall lru_cache_add_active(struct page *page)
151 struct pagevec *pvec = &get_cpu_var(lru_add_active_pvecs);
153 page_cache_get(page);
154 if (!pagevec_add(pvec, page))
155 __pagevec_lru_add_active(pvec);
156 put_cpu_var(lru_add_active_pvecs);
159 void lru_add_drain(void)
161 struct pagevec *pvec = &get_cpu_var(lru_add_pvecs);
163 if (pagevec_count(pvec))
164 __pagevec_lru_add(pvec);
165 pvec = &__get_cpu_var(lru_add_active_pvecs);
166 if (pagevec_count(pvec))
167 __pagevec_lru_add_active(pvec);
168 put_cpu_var(lru_add_pvecs);
172 * This path almost never happens for VM activity - pages are normally
173 * freed via pagevecs. But it gets used by networking.
175 void fastcall __page_cache_release(struct page *page)
177 unsigned long flags;
178 struct zone *zone = page_zone(page);
180 spin_lock_irqsave(&zone->lru_lock, flags);
181 if (TestClearPageLRU(page))
182 del_page_from_lru(zone, page);
183 if (page_count(page) != 0)
184 page = NULL;
185 spin_unlock_irqrestore(&zone->lru_lock, flags);
186 if (page)
187 free_hot_page(page);
190 EXPORT_SYMBOL(__page_cache_release);
193 * Batched page_cache_release(). Decrement the reference count on all the
194 * passed pages. If it fell to zero then remove the page from the LRU and
195 * free it.
197 * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
198 * for the remainder of the operation.
200 * The locking in this function is against shrink_cache(): we recheck the
201 * page count inside the lock to see whether shrink_cache grabbed the page
202 * via the LRU. If it did, give up: shrink_cache will free it.
204 void release_pages(struct page **pages, int nr, int cold)
206 int i;
207 struct pagevec pages_to_free;
208 struct zone *zone = NULL;
210 pagevec_init(&pages_to_free, cold);
211 for (i = 0; i < nr; i++) {
212 struct page *page = pages[i];
213 struct zone *pagezone;
215 if (!put_page_testzero(page))
216 continue;
218 pagezone = page_zone(page);
219 if (pagezone != zone) {
220 if (zone)
221 spin_unlock_irq(&zone->lru_lock);
222 zone = pagezone;
223 spin_lock_irq(&zone->lru_lock);
225 if (TestClearPageLRU(page))
226 del_page_from_lru(zone, page);
227 if (page_count(page) == 0) {
228 if (!pagevec_add(&pages_to_free, page)) {
229 spin_unlock_irq(&zone->lru_lock);
230 __pagevec_free(&pages_to_free);
231 pagevec_reinit(&pages_to_free);
232 zone = NULL; /* No lock is held */
236 if (zone)
237 spin_unlock_irq(&zone->lru_lock);
239 pagevec_free(&pages_to_free);
243 * The pages which we're about to release may be in the deferred lru-addition
244 * queues. That would prevent them from really being freed right now. That's
245 * OK from a correctness point of view but is inefficient - those pages may be
246 * cache-warm and we want to give them back to the page allocator ASAP.
248 * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
249 * and __pagevec_lru_add_active() call release_pages() directly to avoid
250 * mutual recursion.
252 void __pagevec_release(struct pagevec *pvec)
254 lru_add_drain();
255 release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
256 pagevec_reinit(pvec);
259 EXPORT_SYMBOL(__pagevec_release);
262 * pagevec_release() for pages which are known to not be on the LRU
264 * This function reinitialises the caller's pagevec.
266 void __pagevec_release_nonlru(struct pagevec *pvec)
268 int i;
269 struct pagevec pages_to_free;
271 pagevec_init(&pages_to_free, pvec->cold);
272 for (i = 0; i < pagevec_count(pvec); i++) {
273 struct page *page = pvec->pages[i];
275 BUG_ON(PageLRU(page));
276 if (put_page_testzero(page))
277 pagevec_add(&pages_to_free, page);
279 pagevec_free(&pages_to_free);
280 pagevec_reinit(pvec);
284 * Add the passed pages to the LRU, then drop the caller's refcount
285 * on them. Reinitialises the caller's pagevec.
287 void __pagevec_lru_add(struct pagevec *pvec)
289 int i;
290 struct zone *zone = NULL;
292 for (i = 0; i < pagevec_count(pvec); i++) {
293 struct page *page = pvec->pages[i];
294 struct zone *pagezone = page_zone(page);
296 if (pagezone != zone) {
297 if (zone)
298 spin_unlock_irq(&zone->lru_lock);
299 zone = pagezone;
300 spin_lock_irq(&zone->lru_lock);
302 if (TestSetPageLRU(page))
303 BUG();
304 add_page_to_inactive_list(zone, page);
306 if (zone)
307 spin_unlock_irq(&zone->lru_lock);
308 release_pages(pvec->pages, pvec->nr, pvec->cold);
309 pagevec_reinit(pvec);
312 EXPORT_SYMBOL(__pagevec_lru_add);
314 void __pagevec_lru_add_active(struct pagevec *pvec)
316 int i;
317 struct zone *zone = NULL;
319 for (i = 0; i < pagevec_count(pvec); i++) {
320 struct page *page = pvec->pages[i];
321 struct zone *pagezone = page_zone(page);
323 if (pagezone != zone) {
324 if (zone)
325 spin_unlock_irq(&zone->lru_lock);
326 zone = pagezone;
327 spin_lock_irq(&zone->lru_lock);
329 if (TestSetPageLRU(page))
330 BUG();
331 if (TestSetPageActive(page))
332 BUG();
333 add_page_to_active_list(zone, page);
335 if (zone)
336 spin_unlock_irq(&zone->lru_lock);
337 release_pages(pvec->pages, pvec->nr, pvec->cold);
338 pagevec_reinit(pvec);
342 * Try to drop buffers from the pages in a pagevec
344 void pagevec_strip(struct pagevec *pvec)
346 int i;
348 for (i = 0; i < pagevec_count(pvec); i++) {
349 struct page *page = pvec->pages[i];
351 if (PagePrivate(page) && !TestSetPageLocked(page)) {
352 try_to_release_page(page, 0);
353 unlock_page(page);
359 * pagevec_lookup - gang pagecache lookup
360 * @pvec: Where the resulting pages are placed
361 * @mapping: The address_space to search
362 * @start: The starting page index
363 * @nr_pages: The maximum number of pages
365 * pagevec_lookup() will search for and return a group of up to @nr_pages pages
366 * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
367 * reference against the pages in @pvec.
369 * The search returns a group of mapping-contiguous pages with ascending
370 * indexes. There may be holes in the indices due to not-present pages.
372 * pagevec_lookup() returns the number of pages which were found.
374 unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
375 pgoff_t start, unsigned nr_pages)
377 pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
378 return pagevec_count(pvec);
381 unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
382 pgoff_t *index, int tag, unsigned nr_pages)
384 pvec->nr = find_get_pages_tag(mapping, index, tag,
385 nr_pages, pvec->pages);
386 return pagevec_count(pvec);
389 EXPORT_SYMBOL(pagevec_lookup_tag);
391 #ifdef CONFIG_SMP
393 * We tolerate a little inaccuracy to avoid ping-ponging the counter between
394 * CPUs
396 #define ACCT_THRESHOLD max(16, NR_CPUS * 2)
398 static DEFINE_PER_CPU(long, committed_space) = 0;
400 void vm_acct_memory(long pages)
402 long *local;
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;
411 preempt_enable();
414 #ifdef CONFIG_HOTPLUG_CPU
415 static void lru_drain_cache(unsigned int cpu)
417 struct pagevec *pvec = &per_cpu(lru_add_pvecs, cpu);
419 /* CPU is dead, so no locking needed. */
420 if (pagevec_count(pvec))
421 __pagevec_lru_add(pvec);
422 pvec = &per_cpu(lru_add_active_pvecs, cpu);
423 if (pagevec_count(pvec))
424 __pagevec_lru_add_active(pvec);
427 /* Drop the CPU's cached committed space back into the central pool. */
428 static int cpu_swap_callback(struct notifier_block *nfb,
429 unsigned long action,
430 void *hcpu)
432 long *committed;
434 committed = &per_cpu(committed_space, (long)hcpu);
435 if (action == CPU_DEAD) {
436 atomic_add(*committed, &vm_committed_space);
437 *committed = 0;
438 lru_drain_cache((long)hcpu);
440 return NOTIFY_OK;
442 #endif /* CONFIG_HOTPLUG_CPU */
443 #endif /* CONFIG_SMP */
445 #ifdef CONFIG_SMP
446 void percpu_counter_mod(struct percpu_counter *fbc, long amount)
448 long count;
449 long *pcount;
450 int cpu = get_cpu();
452 pcount = per_cpu_ptr(fbc->counters, cpu);
453 count = *pcount + amount;
454 if (count >= FBC_BATCH || count <= -FBC_BATCH) {
455 spin_lock(&fbc->lock);
456 fbc->count += count;
457 spin_unlock(&fbc->lock);
458 count = 0;
460 *pcount = count;
461 put_cpu();
463 EXPORT_SYMBOL(percpu_counter_mod);
464 #endif
467 * Perform any setup for the swap system
469 void __init swap_setup(void)
471 unsigned long megs = num_physpages >> (20 - PAGE_SHIFT);
473 /* Use a smaller cluster for small-memory machines */
474 if (megs < 16)
475 page_cluster = 2;
476 else
477 page_cluster = 3;
479 * Right now other parts of the system means that we
480 * _really_ don't want to cluster much more
482 hotcpu_notifier(cpu_swap_callback, 0);