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>
31 /* How many pages do we try to swap or page in/out together? */
35 * Writeback is about to end against a page which has been marked for immediate
36 * reclaim. If it still appears to be reclaimable, move it to the tail of the
37 * inactive list. The page still has PageWriteback set, which will pin it.
39 * We don't expect many pages to come through here, so don't bother batching
42 * To avoid placing the page at the tail of the LRU while PG_writeback is still
43 * set, this function will clear PG_writeback before performing the page
44 * motion. Do that inside the lru lock because once PG_writeback is cleared
45 * we may not touch the page.
47 * Returns zero if it cleared PG_writeback.
49 int rotate_reclaimable_page(struct page
*page
)
63 zone
= page_zone(page
);
64 spin_lock_irqsave(&zone
->lru_lock
, flags
);
65 if (PageLRU(page
) && !PageActive(page
)) {
67 list_add_tail(&page
->lru
, &zone
->inactive_list
);
68 inc_page_state(pgrotated
);
70 if (!TestClearPageWriteback(page
))
72 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
77 * FIXME: speed this up?
79 void activate_page(struct page
*page
)
81 struct zone
*zone
= page_zone(page
);
83 spin_lock_irq(&zone
->lru_lock
);
84 if (PageLRU(page
) && !PageActive(page
)) {
85 del_page_from_inactive_list(zone
, page
);
87 add_page_to_active_list(zone
, page
);
88 inc_page_state(pgactivate
);
90 spin_unlock_irq(&zone
->lru_lock
);
94 * Mark a page as having seen activity.
96 * inactive,unreferenced -> inactive,referenced
97 * inactive,referenced -> active,unreferenced
98 * active,unreferenced -> active,referenced
100 void mark_page_accessed(struct page
*page
)
102 if (!PageActive(page
) && PageReferenced(page
) && PageLRU(page
)) {
104 ClearPageReferenced(page
);
105 } else if (!PageReferenced(page
)) {
106 SetPageReferenced(page
);
110 EXPORT_SYMBOL(mark_page_accessed
);
113 * lru_cache_add: add a page to the page lists
114 * @page: the page to add
116 static DEFINE_PER_CPU(struct pagevec
, lru_add_pvecs
) = { 0, };
117 static DEFINE_PER_CPU(struct pagevec
, lru_add_active_pvecs
) = { 0, };
119 void lru_cache_add(struct page
*page
)
121 struct pagevec
*pvec
= &get_cpu_var(lru_add_pvecs
);
123 page_cache_get(page
);
124 if (!pagevec_add(pvec
, page
))
125 __pagevec_lru_add(pvec
);
126 put_cpu_var(lru_add_pvecs
);
129 void lru_cache_add_active(struct page
*page
)
131 struct pagevec
*pvec
= &get_cpu_var(lru_add_active_pvecs
);
133 page_cache_get(page
);
134 if (!pagevec_add(pvec
, page
))
135 __pagevec_lru_add_active(pvec
);
136 put_cpu_var(lru_add_active_pvecs
);
139 void lru_add_drain(void)
141 struct pagevec
*pvec
= &get_cpu_var(lru_add_pvecs
);
143 if (pagevec_count(pvec
))
144 __pagevec_lru_add(pvec
);
145 pvec
= &__get_cpu_var(lru_add_active_pvecs
);
146 if (pagevec_count(pvec
))
147 __pagevec_lru_add_active(pvec
);
148 put_cpu_var(lru_add_pvecs
);
152 * This path almost never happens for VM activity - pages are normally
153 * freed via pagevecs. But it gets used by networking.
155 void __page_cache_release(struct page
*page
)
158 struct zone
*zone
= page_zone(page
);
160 spin_lock_irqsave(&zone
->lru_lock
, flags
);
161 if (TestClearPageLRU(page
))
162 del_page_from_lru(zone
, page
);
163 if (page_count(page
) != 0)
165 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
170 EXPORT_SYMBOL(__page_cache_release
);
173 * Batched page_cache_release(). Decrement the reference count on all the
174 * passed pages. If it fell to zero then remove the page from the LRU and
177 * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
178 * for the remainder of the operation.
180 * The locking in this function is against shrink_cache(): we recheck the
181 * page count inside the lock to see whether shrink_cache grabbed the page
182 * via the LRU. If it did, give up: shrink_cache will free it.
184 void release_pages(struct page
**pages
, int nr
, int cold
)
187 struct pagevec pages_to_free
;
188 struct zone
*zone
= NULL
;
190 pagevec_init(&pages_to_free
, cold
);
191 for (i
= 0; i
< nr
; i
++) {
192 struct page
*page
= pages
[i
];
193 struct zone
*pagezone
;
195 if (PageReserved(page
) || !put_page_testzero(page
))
198 pagezone
= page_zone(page
);
199 if (pagezone
!= zone
) {
201 spin_unlock_irq(&zone
->lru_lock
);
203 spin_lock_irq(&zone
->lru_lock
);
205 if (TestClearPageLRU(page
))
206 del_page_from_lru(zone
, page
);
207 if (page_count(page
) == 0) {
208 if (!pagevec_add(&pages_to_free
, page
)) {
209 spin_unlock_irq(&zone
->lru_lock
);
210 __pagevec_free(&pages_to_free
);
211 pagevec_reinit(&pages_to_free
);
212 zone
= NULL
; /* No lock is held */
217 spin_unlock_irq(&zone
->lru_lock
);
219 pagevec_free(&pages_to_free
);
223 * The pages which we're about to release may be in the deferred lru-addition
224 * queues. That would prevent them from really being freed right now. That's
225 * OK from a correctness point of view but is inefficient - those pages may be
226 * cache-warm and we want to give them back to the page allocator ASAP.
228 * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
229 * and __pagevec_lru_add_active() call release_pages() directly to avoid
232 void __pagevec_release(struct pagevec
*pvec
)
235 release_pages(pvec
->pages
, pagevec_count(pvec
), pvec
->cold
);
236 pagevec_reinit(pvec
);
240 * pagevec_release() for pages which are known to not be on the LRU
242 * This function reinitialises the caller's pagevec.
244 void __pagevec_release_nonlru(struct pagevec
*pvec
)
247 struct pagevec pages_to_free
;
249 pagevec_init(&pages_to_free
, pvec
->cold
);
250 pages_to_free
.cold
= pvec
->cold
;
251 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
252 struct page
*page
= pvec
->pages
[i
];
254 BUG_ON(PageLRU(page
));
255 if (put_page_testzero(page
))
256 pagevec_add(&pages_to_free
, page
);
258 pagevec_free(&pages_to_free
);
259 pagevec_reinit(pvec
);
263 * Add the passed pages to the LRU, then drop the caller's refcount
264 * on them. Reinitialises the caller's pagevec.
266 void __pagevec_lru_add(struct pagevec
*pvec
)
269 struct zone
*zone
= NULL
;
271 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
272 struct page
*page
= pvec
->pages
[i
];
273 struct zone
*pagezone
= page_zone(page
);
275 if (pagezone
!= zone
) {
277 spin_unlock_irq(&zone
->lru_lock
);
279 spin_lock_irq(&zone
->lru_lock
);
281 if (TestSetPageLRU(page
))
283 add_page_to_inactive_list(zone
, page
);
286 spin_unlock_irq(&zone
->lru_lock
);
287 release_pages(pvec
->pages
, pvec
->nr
, pvec
->cold
);
288 pagevec_reinit(pvec
);
291 EXPORT_SYMBOL(__pagevec_lru_add
);
293 void __pagevec_lru_add_active(struct pagevec
*pvec
)
296 struct zone
*zone
= NULL
;
298 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
299 struct page
*page
= pvec
->pages
[i
];
300 struct zone
*pagezone
= page_zone(page
);
302 if (pagezone
!= zone
) {
304 spin_unlock_irq(&zone
->lru_lock
);
306 spin_lock_irq(&zone
->lru_lock
);
308 if (TestSetPageLRU(page
))
310 if (TestSetPageActive(page
))
312 add_page_to_active_list(zone
, page
);
315 spin_unlock_irq(&zone
->lru_lock
);
316 release_pages(pvec
->pages
, pvec
->nr
, pvec
->cold
);
317 pagevec_reinit(pvec
);
321 * Try to drop buffers from the pages in a pagevec
323 void pagevec_strip(struct pagevec
*pvec
)
327 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
328 struct page
*page
= pvec
->pages
[i
];
330 if (PagePrivate(page
) && !TestSetPageLocked(page
)) {
331 try_to_release_page(page
, 0);
338 * pagevec_lookup - gang pagecache lookup
339 * @pvec: Where the resulting pages are placed
340 * @mapping: The address_space to search
341 * @start: The starting page index
342 * @nr_pages: The maximum number of pages
344 * pagevec_lookup() will search for and return a group of up to @nr_pages pages
345 * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
346 * reference against the pages in @pvec.
348 * The search returns a group of mapping-contiguous pages with ascending
349 * indexes. There may be holes in the indices due to not-present pages.
351 * pagevec_lookup() returns the number of pages which were found.
353 unsigned int pagevec_lookup(struct pagevec
*pvec
, struct address_space
*mapping
,
354 pgoff_t start
, unsigned int nr_pages
)
356 pvec
->nr
= find_get_pages(mapping
, start
, nr_pages
, pvec
->pages
);
357 return pagevec_count(pvec
);
363 * We tolerate a little inaccuracy to avoid ping-ponging the counter between
366 #define ACCT_THRESHOLD max(16, NR_CPUS * 2)
368 static DEFINE_PER_CPU(long, committed_space
) = 0;
370 void vm_acct_memory(long pages
)
375 local
= &__get_cpu_var(committed_space
);
377 if (*local
> ACCT_THRESHOLD
|| *local
< -ACCT_THRESHOLD
) {
378 atomic_add(*local
, &vm_committed_space
);
383 EXPORT_SYMBOL(vm_acct_memory
);
387 void percpu_counter_mod(struct percpu_counter
*fbc
, long amount
)
393 pcount
= per_cpu_ptr(fbc
->counters
, cpu
);
394 count
= *pcount
+ amount
;
395 if (count
>= FBC_BATCH
|| count
<= -FBC_BATCH
) {
396 spin_lock(&fbc
->lock
);
398 spin_unlock(&fbc
->lock
);
404 EXPORT_SYMBOL(percpu_counter_mod
);
408 * Perform any setup for the swap system
410 void __init
swap_setup(void)
412 unsigned long megs
= num_physpages
>> (20 - PAGE_SHIFT
);
414 /* Use a smaller cluster for small-memory machines */
420 * Right now other parts of the system means that we
421 * _really_ don't want to cluster much more