[PATCH] selinux: require AUDIT
[linux-block.git] / mm / swap.c
CommitLineData
1da177e4
LT
1/*
2 * linux/mm/swap.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 */
6
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.
14 */
15
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>
33
34/* How many pages do we try to swap or page in/out together? */
35int page_cluster;
36
1da177e4
LT
37void put_page(struct page *page)
38{
39 if (unlikely(PageCompound(page))) {
4c21e2f2 40 page = (struct page *)page_private(page);
1da177e4
LT
41 if (put_page_testzero(page)) {
42 void (*dtor)(struct page *page);
43
44 dtor = (void (*)(struct page *))page[1].mapping;
45 (*dtor)(page);
46 }
47 return;
48 }
b5810039 49 if (put_page_testzero(page))
1da177e4
LT
50 __page_cache_release(page);
51}
52EXPORT_SYMBOL(put_page);
1da177e4
LT
53
54/*
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.
58 *
59 * We don't expect many pages to come through here, so don't bother batching
60 * things up.
61 *
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.
66 *
67 * Returns zero if it cleared PG_writeback.
68 */
69int rotate_reclaimable_page(struct page *page)
70{
71 struct zone *zone;
72 unsigned long flags;
73
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;
82
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);
89 }
90 if (!test_clear_page_writeback(page))
91 BUG();
92 spin_unlock_irqrestore(&zone->lru_lock, flags);
93 return 0;
94}
95
96/*
97 * FIXME: speed this up?
98 */
99void fastcall activate_page(struct page *page)
100{
101 struct zone *zone = page_zone(page);
102
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);
109 }
110 spin_unlock_irq(&zone->lru_lock);
111}
112
113/*
114 * Mark a page as having seen activity.
115 *
116 * inactive,unreferenced -> inactive,referenced
117 * inactive,referenced -> active,unreferenced
118 * active,unreferenced -> active,referenced
119 */
120void fastcall mark_page_accessed(struct page *page)
121{
122 if (!PageActive(page) && PageReferenced(page) && PageLRU(page)) {
123 activate_page(page);
124 ClearPageReferenced(page);
125 } else if (!PageReferenced(page)) {
126 SetPageReferenced(page);
127 }
128}
129
130EXPORT_SYMBOL(mark_page_accessed);
131
132/**
133 * lru_cache_add: add a page to the page lists
134 * @page: the page to add
135 */
136static DEFINE_PER_CPU(struct pagevec, lru_add_pvecs) = { 0, };
137static DEFINE_PER_CPU(struct pagevec, lru_add_active_pvecs) = { 0, };
138
139void fastcall lru_cache_add(struct page *page)
140{
141 struct pagevec *pvec = &get_cpu_var(lru_add_pvecs);
142
143 page_cache_get(page);
144 if (!pagevec_add(pvec, page))
145 __pagevec_lru_add(pvec);
146 put_cpu_var(lru_add_pvecs);
147}
148
149void fastcall lru_cache_add_active(struct page *page)
150{
151 struct pagevec *pvec = &get_cpu_var(lru_add_active_pvecs);
152
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);
157}
158
80bfed90 159static void __lru_add_drain(int cpu)
1da177e4 160{
80bfed90 161 struct pagevec *pvec = &per_cpu(lru_add_pvecs, cpu);
1da177e4 162
80bfed90 163 /* CPU is dead, so no locking needed. */
1da177e4
LT
164 if (pagevec_count(pvec))
165 __pagevec_lru_add(pvec);
80bfed90 166 pvec = &per_cpu(lru_add_active_pvecs, cpu);
1da177e4
LT
167 if (pagevec_count(pvec))
168 __pagevec_lru_add_active(pvec);
80bfed90
AM
169}
170
171void lru_add_drain(void)
172{
173 __lru_add_drain(get_cpu());
174 put_cpu();
1da177e4
LT
175}
176
053837fc
NP
177#ifdef CONFIG_NUMA
178static void lru_add_drain_per_cpu(void *dummy)
179{
180 lru_add_drain();
181}
182
183/*
184 * Returns 0 for success
185 */
186int lru_add_drain_all(void)
187{
188 return schedule_on_each_cpu(lru_add_drain_per_cpu, NULL);
189}
190
191#else
192
193/*
194 * Returns 0 for success
195 */
196int lru_add_drain_all(void)
197{
198 lru_add_drain();
199 return 0;
200}
201#endif
202
1da177e4
LT
203/*
204 * This path almost never happens for VM activity - pages are normally
205 * freed via pagevecs. But it gets used by networking.
206 */
207void fastcall __page_cache_release(struct page *page)
208{
209 unsigned long flags;
210 struct zone *zone = page_zone(page);
211
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)
216 page = NULL;
217 spin_unlock_irqrestore(&zone->lru_lock, flags);
218 if (page)
219 free_hot_page(page);
220}
221
222EXPORT_SYMBOL(__page_cache_release);
223
224/*
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
227 * free it.
228 *
229 * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
230 * for the remainder of the operation.
231 *
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.
235 */
236void release_pages(struct page **pages, int nr, int cold)
237{
238 int i;
239 struct pagevec pages_to_free;
240 struct zone *zone = NULL;
241
242 pagevec_init(&pages_to_free, cold);
243 for (i = 0; i < nr; i++) {
244 struct page *page = pages[i];
245 struct zone *pagezone;
246
b5810039 247 if (!put_page_testzero(page))
1da177e4
LT
248 continue;
249
250 pagezone = page_zone(page);
251 if (pagezone != zone) {
252 if (zone)
253 spin_unlock_irq(&zone->lru_lock);
254 zone = pagezone;
255 spin_lock_irq(&zone->lru_lock);
256 }
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 */
265 }
266 }
267 }
268 if (zone)
269 spin_unlock_irq(&zone->lru_lock);
270
271 pagevec_free(&pages_to_free);
272}
273
274/*
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.
279 *
280 * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
281 * and __pagevec_lru_add_active() call release_pages() directly to avoid
282 * mutual recursion.
283 */
284void __pagevec_release(struct pagevec *pvec)
285{
286 lru_add_drain();
287 release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
288 pagevec_reinit(pvec);
289}
290
7f285701
SF
291EXPORT_SYMBOL(__pagevec_release);
292
1da177e4
LT
293/*
294 * pagevec_release() for pages which are known to not be on the LRU
295 *
296 * This function reinitialises the caller's pagevec.
297 */
298void __pagevec_release_nonlru(struct pagevec *pvec)
299{
300 int i;
301 struct pagevec pages_to_free;
302
303 pagevec_init(&pages_to_free, pvec->cold);
1da177e4
LT
304 for (i = 0; i < pagevec_count(pvec); i++) {
305 struct page *page = pvec->pages[i];
306
307 BUG_ON(PageLRU(page));
308 if (put_page_testzero(page))
309 pagevec_add(&pages_to_free, page);
310 }
311 pagevec_free(&pages_to_free);
312 pagevec_reinit(pvec);
313}
314
315/*
316 * Add the passed pages to the LRU, then drop the caller's refcount
317 * on them. Reinitialises the caller's pagevec.
318 */
319void __pagevec_lru_add(struct pagevec *pvec)
320{
321 int i;
322 struct zone *zone = NULL;
323
324 for (i = 0; i < pagevec_count(pvec); i++) {
325 struct page *page = pvec->pages[i];
326 struct zone *pagezone = page_zone(page);
327
328 if (pagezone != zone) {
329 if (zone)
330 spin_unlock_irq(&zone->lru_lock);
331 zone = pagezone;
332 spin_lock_irq(&zone->lru_lock);
333 }
334 if (TestSetPageLRU(page))
335 BUG();
336 add_page_to_inactive_list(zone, page);
337 }
338 if (zone)
339 spin_unlock_irq(&zone->lru_lock);
340 release_pages(pvec->pages, pvec->nr, pvec->cold);
341 pagevec_reinit(pvec);
342}
343
344EXPORT_SYMBOL(__pagevec_lru_add);
345
346void __pagevec_lru_add_active(struct pagevec *pvec)
347{
348 int i;
349 struct zone *zone = NULL;
350
351 for (i = 0; i < pagevec_count(pvec); i++) {
352 struct page *page = pvec->pages[i];
353 struct zone *pagezone = page_zone(page);
354
355 if (pagezone != zone) {
356 if (zone)
357 spin_unlock_irq(&zone->lru_lock);
358 zone = pagezone;
359 spin_lock_irq(&zone->lru_lock);
360 }
361 if (TestSetPageLRU(page))
362 BUG();
363 if (TestSetPageActive(page))
364 BUG();
365 add_page_to_active_list(zone, page);
366 }
367 if (zone)
368 spin_unlock_irq(&zone->lru_lock);
369 release_pages(pvec->pages, pvec->nr, pvec->cold);
370 pagevec_reinit(pvec);
371}
372
373/*
374 * Try to drop buffers from the pages in a pagevec
375 */
376void pagevec_strip(struct pagevec *pvec)
377{
378 int i;
379
380 for (i = 0; i < pagevec_count(pvec); i++) {
381 struct page *page = pvec->pages[i];
382
383 if (PagePrivate(page) && !TestSetPageLocked(page)) {
384 try_to_release_page(page, 0);
385 unlock_page(page);
386 }
387 }
388}
389
390/**
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
396 *
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.
400 *
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.
403 *
404 * pagevec_lookup() returns the number of pages which were found.
405 */
406unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
407 pgoff_t start, unsigned nr_pages)
408{
409 pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
410 return pagevec_count(pvec);
411}
412
78539fdf
CH
413EXPORT_SYMBOL(pagevec_lookup);
414
1da177e4
LT
415unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
416 pgoff_t *index, int tag, unsigned nr_pages)
417{
418 pvec->nr = find_get_pages_tag(mapping, index, tag,
419 nr_pages, pvec->pages);
420 return pagevec_count(pvec);
421}
422
7f285701 423EXPORT_SYMBOL(pagevec_lookup_tag);
1da177e4
LT
424
425#ifdef CONFIG_SMP
426/*
427 * We tolerate a little inaccuracy to avoid ping-ponging the counter between
428 * CPUs
429 */
430#define ACCT_THRESHOLD max(16, NR_CPUS * 2)
431
432static DEFINE_PER_CPU(long, committed_space) = 0;
433
434void vm_acct_memory(long pages)
435{
436 long *local;
437
438 preempt_disable();
439 local = &__get_cpu_var(committed_space);
440 *local += pages;
441 if (*local > ACCT_THRESHOLD || *local < -ACCT_THRESHOLD) {
442 atomic_add(*local, &vm_committed_space);
443 *local = 0;
444 }
445 preempt_enable();
446}
1da177e4
LT
447
448#ifdef CONFIG_HOTPLUG_CPU
1da177e4
LT
449
450/* Drop the CPU's cached committed space back into the central pool. */
451static int cpu_swap_callback(struct notifier_block *nfb,
452 unsigned long action,
453 void *hcpu)
454{
455 long *committed;
456
457 committed = &per_cpu(committed_space, (long)hcpu);
458 if (action == CPU_DEAD) {
459 atomic_add(*committed, &vm_committed_space);
460 *committed = 0;
80bfed90 461 __lru_add_drain((long)hcpu);
1da177e4
LT
462 }
463 return NOTIFY_OK;
464}
465#endif /* CONFIG_HOTPLUG_CPU */
466#endif /* CONFIG_SMP */
467
468#ifdef CONFIG_SMP
469void percpu_counter_mod(struct percpu_counter *fbc, long amount)
470{
471 long count;
472 long *pcount;
473 int cpu = get_cpu();
474
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);
479 fbc->count += count;
480 spin_unlock(&fbc->lock);
481 count = 0;
482 }
483 *pcount = count;
484 put_cpu();
485}
486EXPORT_SYMBOL(percpu_counter_mod);
487#endif
488
489/*
490 * Perform any setup for the swap system
491 */
492void __init swap_setup(void)
493{
494 unsigned long megs = num_physpages >> (20 - PAGE_SHIFT);
495
496 /* Use a smaller cluster for small-memory machines */
497 if (megs < 16)
498 page_cluster = 2;
499 else
500 page_cluster = 3;
501 /*
502 * Right now other parts of the system means that we
503 * _really_ don't want to cluster much more
504 */
505 hotcpu_notifier(cpu_swap_callback, 0);
506}