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 * linux/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? */
38 * Writeback is about to end against a page which has been marked for immediate
39 * reclaim. If it still appears to be reclaimable, move it to the tail of the
40 * inactive list. The page still has PageWriteback set, which will pin it.
42 * We don't expect many pages to come through here, so don't bother batching
45 * To avoid placing the page at the tail of the LRU while PG_writeback is still
46 * set, this function will clear PG_writeback before performing the page
47 * motion. Do that inside the lru lock because once PG_writeback is cleared
48 * we may not touch the page.
50 * Returns zero if it cleared PG_writeback.
52 int rotate_reclaimable_page(struct page
*page
)
66 zone
= page_zone(page
);
67 spin_lock_irqsave(&zone
->lru_lock
, flags
);
68 if (PageLRU(page
) && !PageActive(page
)) {
70 list_add_tail(&page
->lru
, &zone
->inactive_list
);
71 inc_page_state(pgrotated
);
73 if (!TestClearPageWriteback(page
))
75 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
80 * FIXME: speed this up?
82 void fastcall
activate_page(struct page
*page
)
84 struct zone
*zone
= page_zone(page
);
86 spin_lock_irq(&zone
->lru_lock
);
87 if (PageLRU(page
) && !PageActive(page
)) {
88 del_page_from_inactive_list(zone
, page
);
90 add_page_to_active_list(zone
, page
);
91 inc_page_state(pgactivate
);
93 spin_unlock_irq(&zone
->lru_lock
);
97 * Mark a page as having seen activity.
99 * inactive,unreferenced -> inactive,referenced
100 * inactive,referenced -> active,unreferenced
101 * active,unreferenced -> active,referenced
103 void fastcall
mark_page_accessed(struct page
*page
)
105 if (!PageActive(page
) && PageReferenced(page
) && PageLRU(page
)) {
107 ClearPageReferenced(page
);
108 } else if (!PageReferenced(page
)) {
109 SetPageReferenced(page
);
113 EXPORT_SYMBOL(mark_page_accessed
);
116 * lru_cache_add: add a page to the page lists
117 * @page: the page to add
119 static DEFINE_PER_CPU(struct pagevec
, lru_add_pvecs
) = { 0, };
120 static DEFINE_PER_CPU(struct pagevec
, lru_add_active_pvecs
) = { 0, };
122 void fastcall
lru_cache_add(struct page
*page
)
124 struct pagevec
*pvec
= &get_cpu_var(lru_add_pvecs
);
126 page_cache_get(page
);
127 if (!pagevec_add(pvec
, page
))
128 __pagevec_lru_add(pvec
);
129 put_cpu_var(lru_add_pvecs
);
132 void fastcall
lru_cache_add_active(struct page
*page
)
134 struct pagevec
*pvec
= &get_cpu_var(lru_add_active_pvecs
);
136 page_cache_get(page
);
137 if (!pagevec_add(pvec
, page
))
138 __pagevec_lru_add_active(pvec
);
139 put_cpu_var(lru_add_active_pvecs
);
142 void lru_add_drain(void)
144 struct pagevec
*pvec
= &get_cpu_var(lru_add_pvecs
);
146 if (pagevec_count(pvec
))
147 __pagevec_lru_add(pvec
);
148 pvec
= &__get_cpu_var(lru_add_active_pvecs
);
149 if (pagevec_count(pvec
))
150 __pagevec_lru_add_active(pvec
);
151 put_cpu_var(lru_add_pvecs
);
155 * This path almost never happens for VM activity - pages are normally
156 * freed via pagevecs. But it gets used by networking.
158 void fastcall
__page_cache_release(struct page
*page
)
161 struct zone
*zone
= page_zone(page
);
163 spin_lock_irqsave(&zone
->lru_lock
, flags
);
164 if (TestClearPageLRU(page
))
165 del_page_from_lru(zone
, page
);
166 if (page_count(page
) != 0)
168 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
173 EXPORT_SYMBOL(__page_cache_release
);
176 * Batched page_cache_release(). Decrement the reference count on all the
177 * passed pages. If it fell to zero then remove the page from the LRU and
180 * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
181 * for the remainder of the operation.
183 * The locking in this function is against shrink_cache(): we recheck the
184 * page count inside the lock to see whether shrink_cache grabbed the page
185 * via the LRU. If it did, give up: shrink_cache will free it.
187 void release_pages(struct page
**pages
, int nr
, int cold
)
190 struct pagevec pages_to_free
;
191 struct zone
*zone
= NULL
;
193 pagevec_init(&pages_to_free
, cold
);
194 for (i
= 0; i
< nr
; i
++) {
195 struct page
*page
= pages
[i
];
196 struct zone
*pagezone
;
198 if (PageReserved(page
) || !put_page_testzero(page
))
201 pagezone
= page_zone(page
);
202 if (pagezone
!= zone
) {
204 spin_unlock_irq(&zone
->lru_lock
);
206 spin_lock_irq(&zone
->lru_lock
);
208 if (TestClearPageLRU(page
))
209 del_page_from_lru(zone
, page
);
210 if (page_count(page
) == 0) {
211 if (!pagevec_add(&pages_to_free
, page
)) {
212 spin_unlock_irq(&zone
->lru_lock
);
213 __pagevec_free(&pages_to_free
);
214 pagevec_reinit(&pages_to_free
);
215 zone
= NULL
; /* No lock is held */
220 spin_unlock_irq(&zone
->lru_lock
);
222 pagevec_free(&pages_to_free
);
226 * The pages which we're about to release may be in the deferred lru-addition
227 * queues. That would prevent them from really being freed right now. That's
228 * OK from a correctness point of view but is inefficient - those pages may be
229 * cache-warm and we want to give them back to the page allocator ASAP.
231 * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
232 * and __pagevec_lru_add_active() call release_pages() directly to avoid
235 void __pagevec_release(struct pagevec
*pvec
)
238 release_pages(pvec
->pages
, pagevec_count(pvec
), pvec
->cold
);
239 pagevec_reinit(pvec
);
243 * pagevec_release() for pages which are known to not be on the LRU
245 * This function reinitialises the caller's pagevec.
247 void __pagevec_release_nonlru(struct pagevec
*pvec
)
250 struct pagevec pages_to_free
;
252 pagevec_init(&pages_to_free
, pvec
->cold
);
253 pages_to_free
.cold
= pvec
->cold
;
254 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
255 struct page
*page
= pvec
->pages
[i
];
257 BUG_ON(PageLRU(page
));
258 if (put_page_testzero(page
))
259 pagevec_add(&pages_to_free
, page
);
261 pagevec_free(&pages_to_free
);
262 pagevec_reinit(pvec
);
266 * Add the passed pages to the LRU, then drop the caller's refcount
267 * on them. Reinitialises the caller's pagevec.
269 void __pagevec_lru_add(struct pagevec
*pvec
)
272 struct zone
*zone
= NULL
;
274 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
275 struct page
*page
= pvec
->pages
[i
];
276 struct zone
*pagezone
= page_zone(page
);
278 if (pagezone
!= zone
) {
280 spin_unlock_irq(&zone
->lru_lock
);
282 spin_lock_irq(&zone
->lru_lock
);
284 if (TestSetPageLRU(page
))
286 add_page_to_inactive_list(zone
, page
);
289 spin_unlock_irq(&zone
->lru_lock
);
290 release_pages(pvec
->pages
, pvec
->nr
, pvec
->cold
);
291 pagevec_reinit(pvec
);
294 EXPORT_SYMBOL(__pagevec_lru_add
);
296 void __pagevec_lru_add_active(struct pagevec
*pvec
)
299 struct zone
*zone
= NULL
;
301 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
302 struct page
*page
= pvec
->pages
[i
];
303 struct zone
*pagezone
= page_zone(page
);
305 if (pagezone
!= zone
) {
307 spin_unlock_irq(&zone
->lru_lock
);
309 spin_lock_irq(&zone
->lru_lock
);
311 if (TestSetPageLRU(page
))
313 if (TestSetPageActive(page
))
315 add_page_to_active_list(zone
, page
);
318 spin_unlock_irq(&zone
->lru_lock
);
319 release_pages(pvec
->pages
, pvec
->nr
, pvec
->cold
);
320 pagevec_reinit(pvec
);
324 * Try to drop buffers from the pages in a pagevec
326 void pagevec_strip(struct pagevec
*pvec
)
330 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
331 struct page
*page
= pvec
->pages
[i
];
333 if (PagePrivate(page
) && !TestSetPageLocked(page
)) {
334 try_to_release_page(page
, 0);
341 * pagevec_lookup - gang pagecache lookup
342 * @pvec: Where the resulting pages are placed
343 * @mapping: The address_space to search
344 * @start: The starting page index
345 * @nr_pages: The maximum number of pages
347 * pagevec_lookup() will search for and return a group of up to @nr_pages pages
348 * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
349 * reference against the pages in @pvec.
351 * The search returns a group of mapping-contiguous pages with ascending
352 * indexes. There may be holes in the indices due to not-present pages.
354 * pagevec_lookup() returns the number of pages which were found.
356 unsigned int pagevec_lookup(struct pagevec
*pvec
, struct address_space
*mapping
,
357 pgoff_t start
, unsigned int nr_pages
)
359 pvec
->nr
= find_get_pages(mapping
, start
, nr_pages
, pvec
->pages
);
360 return pagevec_count(pvec
);
366 * We tolerate a little inaccuracy to avoid ping-ponging the counter between
369 #define ACCT_THRESHOLD max(16, NR_CPUS * 2)
371 static DEFINE_PER_CPU(long, committed_space
) = 0;
373 void vm_acct_memory(long pages
)
378 local
= &__get_cpu_var(committed_space
);
380 if (*local
> ACCT_THRESHOLD
|| *local
< -ACCT_THRESHOLD
) {
381 atomic_add(*local
, &vm_committed_space
);
386 EXPORT_SYMBOL(vm_acct_memory
);
388 #ifdef CONFIG_HOTPLUG_CPU
389 static void lru_drain_cache(unsigned int cpu
)
391 struct pagevec
*pvec
= &per_cpu(lru_add_pvecs
, cpu
);
393 /* CPU is dead, so no locking needed. */
394 if (pagevec_count(pvec
))
395 __pagevec_lru_add(pvec
);
396 pvec
= &per_cpu(lru_add_active_pvecs
, cpu
);
397 if (pagevec_count(pvec
))
398 __pagevec_lru_add_active(pvec
);
401 /* Drop the CPU's cached committed space back into the central pool. */
402 static int cpu_swap_callback(struct notifier_block
*nfb
,
403 unsigned long action
,
408 committed
= &per_cpu(committed_space
, (long)hcpu
);
409 if (action
== CPU_DEAD
) {
410 atomic_add(*committed
, &vm_committed_space
);
412 lru_drain_cache((long)hcpu
);
416 #endif /* CONFIG_HOTPLUG_CPU */
417 #endif /* CONFIG_SMP */
420 void percpu_counter_mod(struct percpu_counter
*fbc
, long amount
)
426 pcount
= per_cpu_ptr(fbc
->counters
, cpu
);
427 count
= *pcount
+ amount
;
428 if (count
>= FBC_BATCH
|| count
<= -FBC_BATCH
) {
429 spin_lock(&fbc
->lock
);
431 spin_unlock(&fbc
->lock
);
437 EXPORT_SYMBOL(percpu_counter_mod
);
441 * Perform any setup for the swap system
443 void __init
swap_setup(void)
445 unsigned long megs
= num_physpages
>> (20 - PAGE_SHIFT
);
447 /* Use a smaller cluster for small-memory machines */
453 * Right now other parts of the system means that we
454 * _really_ don't want to cluster much more
456 hotcpu_notifier(cpu_swap_callback
, 0);