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>
34 /* How many pages do we try to swap or page in/out together? */
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
;
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
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
)
83 zone
= page_zone(page
);
84 spin_lock_irqsave(&zone
->lru_lock
, flags
);
85 if (PageLRU(page
) && !PageActive(page
)) {
87 list_add_tail(&page
->lru
, &zone
->inactive_list
);
88 inc_page_state(pgrotated
);
90 if (!test_clear_page_writeback(page
))
92 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
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
);
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
)) {
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 static void __lru_add_drain(int cpu
)
161 struct pagevec
*pvec
= &per_cpu(lru_add_pvecs
, cpu
);
163 /* CPU is dead, so no locking needed. */
164 if (pagevec_count(pvec
))
165 __pagevec_lru_add(pvec
);
166 pvec
= &per_cpu(lru_add_active_pvecs
, cpu
);
167 if (pagevec_count(pvec
))
168 __pagevec_lru_add_active(pvec
);
171 void lru_add_drain(void)
173 __lru_add_drain(get_cpu());
178 static void lru_add_drain_per_cpu(void *dummy
)
184 * Returns 0 for success
186 int lru_add_drain_all(void)
188 return schedule_on_each_cpu(lru_add_drain_per_cpu
, NULL
);
194 * Returns 0 for success
196 int lru_add_drain_all(void)
204 * This path almost never happens for VM activity - pages are normally
205 * freed via pagevecs. But it gets used by networking.
207 void fastcall
__page_cache_release(struct page
*page
)
210 struct zone
*zone
= page_zone(page
);
212 spin_lock_irqsave(&zone
->lru_lock
, flags
);
213 if (TestClearPageLRU(page
))
214 del_page_from_lru(zone
, page
);
215 if (page_count(page
) != 0)
217 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
222 EXPORT_SYMBOL(__page_cache_release
);
225 * Batched page_cache_release(). Decrement the reference count on all the
226 * passed pages. If it fell to zero then remove the page from the LRU and
229 * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
230 * for the remainder of the operation.
232 * The locking in this function is against shrink_cache(): we recheck the
233 * page count inside the lock to see whether shrink_cache grabbed the page
234 * via the LRU. If it did, give up: shrink_cache will free it.
236 void release_pages(struct page
**pages
, int nr
, int cold
)
239 struct pagevec pages_to_free
;
240 struct zone
*zone
= NULL
;
242 pagevec_init(&pages_to_free
, cold
);
243 for (i
= 0; i
< nr
; i
++) {
244 struct page
*page
= pages
[i
];
245 struct zone
*pagezone
;
247 if (!put_page_testzero(page
))
250 pagezone
= page_zone(page
);
251 if (pagezone
!= zone
) {
253 spin_unlock_irq(&zone
->lru_lock
);
255 spin_lock_irq(&zone
->lru_lock
);
257 if (TestClearPageLRU(page
))
258 del_page_from_lru(zone
, page
);
259 if (page_count(page
) == 0) {
260 if (!pagevec_add(&pages_to_free
, page
)) {
261 spin_unlock_irq(&zone
->lru_lock
);
262 __pagevec_free(&pages_to_free
);
263 pagevec_reinit(&pages_to_free
);
264 zone
= NULL
; /* No lock is held */
269 spin_unlock_irq(&zone
->lru_lock
);
271 pagevec_free(&pages_to_free
);
275 * The pages which we're about to release may be in the deferred lru-addition
276 * queues. That would prevent them from really being freed right now. That's
277 * OK from a correctness point of view but is inefficient - those pages may be
278 * cache-warm and we want to give them back to the page allocator ASAP.
280 * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
281 * and __pagevec_lru_add_active() call release_pages() directly to avoid
284 void __pagevec_release(struct pagevec
*pvec
)
287 release_pages(pvec
->pages
, pagevec_count(pvec
), pvec
->cold
);
288 pagevec_reinit(pvec
);
291 EXPORT_SYMBOL(__pagevec_release
);
294 * pagevec_release() for pages which are known to not be on the LRU
296 * This function reinitialises the caller's pagevec.
298 void __pagevec_release_nonlru(struct pagevec
*pvec
)
301 struct pagevec pages_to_free
;
303 pagevec_init(&pages_to_free
, pvec
->cold
);
304 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
305 struct page
*page
= pvec
->pages
[i
];
307 BUG_ON(PageLRU(page
));
308 if (put_page_testzero(page
))
309 pagevec_add(&pages_to_free
, page
);
311 pagevec_free(&pages_to_free
);
312 pagevec_reinit(pvec
);
316 * Add the passed pages to the LRU, then drop the caller's refcount
317 * on them. Reinitialises the caller's pagevec.
319 void __pagevec_lru_add(struct pagevec
*pvec
)
322 struct zone
*zone
= NULL
;
324 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
325 struct page
*page
= pvec
->pages
[i
];
326 struct zone
*pagezone
= page_zone(page
);
328 if (pagezone
!= zone
) {
330 spin_unlock_irq(&zone
->lru_lock
);
332 spin_lock_irq(&zone
->lru_lock
);
334 if (TestSetPageLRU(page
))
336 add_page_to_inactive_list(zone
, page
);
339 spin_unlock_irq(&zone
->lru_lock
);
340 release_pages(pvec
->pages
, pvec
->nr
, pvec
->cold
);
341 pagevec_reinit(pvec
);
344 EXPORT_SYMBOL(__pagevec_lru_add
);
346 void __pagevec_lru_add_active(struct pagevec
*pvec
)
349 struct zone
*zone
= NULL
;
351 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
352 struct page
*page
= pvec
->pages
[i
];
353 struct zone
*pagezone
= page_zone(page
);
355 if (pagezone
!= zone
) {
357 spin_unlock_irq(&zone
->lru_lock
);
359 spin_lock_irq(&zone
->lru_lock
);
361 if (TestSetPageLRU(page
))
363 if (TestSetPageActive(page
))
365 add_page_to_active_list(zone
, page
);
368 spin_unlock_irq(&zone
->lru_lock
);
369 release_pages(pvec
->pages
, pvec
->nr
, pvec
->cold
);
370 pagevec_reinit(pvec
);
374 * Try to drop buffers from the pages in a pagevec
376 void pagevec_strip(struct pagevec
*pvec
)
380 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
381 struct page
*page
= pvec
->pages
[i
];
383 if (PagePrivate(page
) && !TestSetPageLocked(page
)) {
384 try_to_release_page(page
, 0);
391 * pagevec_lookup - gang pagecache lookup
392 * @pvec: Where the resulting pages are placed
393 * @mapping: The address_space to search
394 * @start: The starting page index
395 * @nr_pages: The maximum number of pages
397 * pagevec_lookup() will search for and return a group of up to @nr_pages pages
398 * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
399 * reference against the pages in @pvec.
401 * The search returns a group of mapping-contiguous pages with ascending
402 * indexes. There may be holes in the indices due to not-present pages.
404 * pagevec_lookup() returns the number of pages which were found.
406 unsigned pagevec_lookup(struct pagevec
*pvec
, struct address_space
*mapping
,
407 pgoff_t start
, unsigned nr_pages
)
409 pvec
->nr
= find_get_pages(mapping
, start
, nr_pages
, pvec
->pages
);
410 return pagevec_count(pvec
);
413 EXPORT_SYMBOL(pagevec_lookup
);
415 unsigned pagevec_lookup_tag(struct pagevec
*pvec
, struct address_space
*mapping
,
416 pgoff_t
*index
, int tag
, unsigned nr_pages
)
418 pvec
->nr
= find_get_pages_tag(mapping
, index
, tag
,
419 nr_pages
, pvec
->pages
);
420 return pagevec_count(pvec
);
423 EXPORT_SYMBOL(pagevec_lookup_tag
);
427 * We tolerate a little inaccuracy to avoid ping-ponging the counter between
430 #define ACCT_THRESHOLD max(16, NR_CPUS * 2)
432 static DEFINE_PER_CPU(long, committed_space
) = 0;
434 void vm_acct_memory(long pages
)
439 local
= &__get_cpu_var(committed_space
);
441 if (*local
> ACCT_THRESHOLD
|| *local
< -ACCT_THRESHOLD
) {
442 atomic_add(*local
, &vm_committed_space
);
448 #ifdef CONFIG_HOTPLUG_CPU
450 /* Drop the CPU's cached committed space back into the central pool. */
451 static int cpu_swap_callback(struct notifier_block
*nfb
,
452 unsigned long action
,
457 committed
= &per_cpu(committed_space
, (long)hcpu
);
458 if (action
== CPU_DEAD
) {
459 atomic_add(*committed
, &vm_committed_space
);
461 __lru_add_drain((long)hcpu
);
465 #endif /* CONFIG_HOTPLUG_CPU */
466 #endif /* CONFIG_SMP */
469 void percpu_counter_mod(struct percpu_counter
*fbc
, long amount
)
475 pcount
= per_cpu_ptr(fbc
->counters
, cpu
);
476 count
= *pcount
+ amount
;
477 if (count
>= FBC_BATCH
|| count
<= -FBC_BATCH
) {
478 spin_lock(&fbc
->lock
);
480 spin_unlock(&fbc
->lock
);
486 EXPORT_SYMBOL(percpu_counter_mod
);
490 * Perform any setup for the swap system
492 void __init
swap_setup(void)
494 unsigned long megs
= num_physpages
>> (20 - PAGE_SHIFT
);
496 /* Use a smaller cluster for small-memory machines */
502 * Right now other parts of the system means that we
503 * _really_ don't want to cluster much more
505 hotcpu_notifier(cpu_swap_callback
, 0);