4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
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.
12 * Swap aging added 23.2.95, Stephen Tweedie.
13 * Buffermem limits added 12.3.98, Rik van Riel.
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>
35 /* How many pages do we try to swap or page in/out together? */
38 #ifdef CONFIG_HUGETLB_PAGE
40 void put_page(struct page *page)
42 if (unlikely(PageCompound(page))) {
43 page = (struct page *)page->private;
44 if (put_page_testzero(page)) {
45 void (*dtor)(struct page *page);
47 dtor = (void (*)(struct page *))page[1].mapping;
52 if (!PageReserved(page) && put_page_testzero(page))
53 __page_cache_release(page);
55 EXPORT_SYMBOL(put_page);
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.
63 * We don't expect many pages to come through here, so don't bother batching
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.
71 * Returns zero if it cleared PG_writeback.
73 int rotate_reclaimable_page(struct page *page)
75 #ifdef CONFIG_CKRM_RES_MEM
76 struct ckrm_zone *ckrm_zone = page_ckrmzone(page);
77 struct zone *zone = ckrm_zone->zone;
79 struct zone *zone = page_zone(page);
92 spin_lock_irqsave(&zone->lru_lock, flags);
93 if (PageLRU(page) && !PageActive(page)) {
95 ckrm_add_tail_inactive(page);
96 inc_page_state(pgrotated);
98 if (!test_clear_page_writeback(page))
100 spin_unlock_irqrestore(&zone->lru_lock, flags);
105 * FIXME: speed this up?
107 void fastcall activate_page(struct page *page)
109 struct zone *zone = page_zone(page);
111 spin_lock_irq(&zone->lru_lock);
112 if (PageLRU(page) && !PageActive(page)) {
113 del_page_from_inactive_list(zone, page);
115 add_page_to_active_list(zone, page);
116 inc_page_state(pgactivate);
118 spin_unlock_irq(&zone->lru_lock);
122 * Mark a page as having seen activity.
124 * inactive,unreferenced -> inactive,referenced
125 * inactive,referenced -> active,unreferenced
126 * active,unreferenced -> active,referenced
128 void fastcall mark_page_accessed(struct page *page)
130 if (!PageActive(page) && PageReferenced(page) && PageLRU(page)) {
132 ClearPageReferenced(page);
133 } else if (!PageReferenced(page)) {
134 SetPageReferenced(page);
138 EXPORT_SYMBOL(mark_page_accessed);
141 * lru_cache_add: add a page to the page lists
142 * @page: the page to add
144 static DEFINE_PER_CPU(struct pagevec, lru_add_pvecs) = { 0, };
145 static DEFINE_PER_CPU(struct pagevec, lru_add_active_pvecs) = { 0, };
147 void fastcall lru_cache_add(struct page *page)
149 struct pagevec *pvec = &get_cpu_var(lru_add_pvecs);
151 page_cache_get(page);
152 if (!pagevec_add(pvec, page))
153 __pagevec_lru_add(pvec);
154 put_cpu_var(lru_add_pvecs);
157 void fastcall lru_cache_add_active(struct page *page)
159 struct pagevec *pvec = &get_cpu_var(lru_add_active_pvecs);
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);
167 void lru_add_drain(void)
169 struct pagevec *pvec = &get_cpu_var(lru_add_pvecs);
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);
180 * This path almost never happens for VM activity - pages are normally
181 * freed via pagevecs. But it gets used by networking.
183 void fastcall __page_cache_release(struct page *page)
186 struct zone *zone = page_zone(page);
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)
193 spin_unlock_irqrestore(&zone->lru_lock, flags);
198 EXPORT_SYMBOL(__page_cache_release);
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
205 * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
206 * for the remainder of the operation.
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.
212 void release_pages(struct page **pages, int nr, int cold)
215 struct pagevec pages_to_free;
216 struct zone *zone = NULL;
218 pagevec_init(&pages_to_free, cold);
219 for (i = 0; i < nr; i++) {
220 struct page *page = pages[i];
221 struct zone *pagezone;
223 if (PageReserved(page) || !put_page_testzero(page))
226 pagezone = page_zone(page);
227 if (pagezone != zone) {
229 spin_unlock_irq(&zone->lru_lock);
231 spin_lock_irq(&zone->lru_lock);
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 */
245 spin_unlock_irq(&zone->lru_lock);
247 pagevec_free(&pages_to_free);
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.
256 * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
257 * and __pagevec_lru_add_active() call release_pages() directly to avoid
260 void __pagevec_release(struct pagevec *pvec)
263 release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
264 pagevec_reinit(pvec);
268 * pagevec_release() for pages which are known to not be on the LRU
270 * This function reinitialises the caller's pagevec.
272 void __pagevec_release_nonlru(struct pagevec *pvec)
275 struct pagevec pages_to_free;
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];
282 BUG_ON(PageLRU(page));
283 if (put_page_testzero(page))
284 pagevec_add(&pages_to_free, page);
286 pagevec_free(&pages_to_free);
287 pagevec_reinit(pvec);
291 * Add the passed pages to the LRU, then drop the caller's refcount
292 * on them. Reinitialises the caller's pagevec.
294 void __pagevec_lru_add(struct pagevec *pvec)
297 struct zone *zone = NULL;
299 for (i = 0; i < pagevec_count(pvec); i++) {
300 struct page *page = pvec->pages[i];
301 struct zone *pagezone = page_zone(page);
303 if (pagezone != zone) {
305 spin_unlock_irq(&zone->lru_lock);
307 spin_lock_irq(&zone->lru_lock);
309 if (TestSetPageLRU(page))
311 add_page_to_inactive_list(zone, page);
314 spin_unlock_irq(&zone->lru_lock);
315 release_pages(pvec->pages, pvec->nr, pvec->cold);
316 pagevec_reinit(pvec);
319 EXPORT_SYMBOL(__pagevec_lru_add);
321 void __pagevec_lru_add_active(struct pagevec *pvec)
324 struct zone *zone = NULL;
326 for (i = 0; i < pagevec_count(pvec); i++) {
327 struct page *page = pvec->pages[i];
328 struct zone *pagezone = page_zone(page);
330 if (pagezone != zone) {
332 spin_unlock_irq(&zone->lru_lock);
334 spin_lock_irq(&zone->lru_lock);
336 if (TestSetPageLRU(page))
338 if (TestSetPageActive(page))
340 add_page_to_active_list(zone, page);
343 spin_unlock_irq(&zone->lru_lock);
344 release_pages(pvec->pages, pvec->nr, pvec->cold);
345 pagevec_reinit(pvec);
349 * Try to drop buffers from the pages in a pagevec
351 void pagevec_strip(struct pagevec *pvec)
355 for (i = 0; i < pagevec_count(pvec); i++) {
356 struct page *page = pvec->pages[i];
358 if (PagePrivate(page) && !TestSetPageLocked(page)) {
359 try_to_release_page(page, 0);
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
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.
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.
379 * pagevec_lookup() returns the number of pages which were found.
381 unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
382 pgoff_t start, unsigned nr_pages)
384 pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
385 return pagevec_count(pvec);
388 unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
389 pgoff_t *index, int tag, unsigned nr_pages)
391 pvec->nr = find_get_pages_tag(mapping, index, tag,
392 nr_pages, pvec->pages);
393 return pagevec_count(pvec);
399 * We tolerate a little inaccuracy to avoid ping-ponging the counter between
402 #define ACCT_THRESHOLD max(16, NR_CPUS * 2)
404 static DEFINE_PER_CPU(long, committed_space) = 0;
406 void vm_acct_memory(long pages)
411 local = &__get_cpu_var(committed_space);
413 if (*local > ACCT_THRESHOLD || *local < -ACCT_THRESHOLD) {
414 atomic_add(*local, &vm_committed_space);
419 EXPORT_SYMBOL(vm_acct_memory);
421 #ifdef CONFIG_HOTPLUG_CPU
422 static void lru_drain_cache(unsigned int cpu)
424 struct pagevec *pvec = &per_cpu(lru_add_pvecs, cpu);
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);
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,
441 committed = &per_cpu(committed_space, (long)hcpu);
442 if (action == CPU_DEAD) {
443 atomic_add(*committed, &vm_committed_space);
445 lru_drain_cache((long)hcpu);
449 #endif /* CONFIG_HOTPLUG_CPU */
450 #endif /* CONFIG_SMP */
453 void percpu_counter_mod(struct percpu_counter *fbc, long amount)
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);
464 spin_unlock(&fbc->lock);
470 EXPORT_SYMBOL(percpu_counter_mod);
474 * Perform any setup for the swap system
476 void __init swap_setup(void)
478 unsigned long megs = num_physpages >> (20 - PAGE_SHIFT);
480 /* Use a smaller cluster for small-memory machines */
486 * Right now other parts of the system means that we
487 * _really_ don't want to cluster much more
489 hotcpu_notifier(cpu_swap_callback, 0);