]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - mm/swap.c
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/percpu_counter.h>
28 #include <linux/percpu.h>
29 #include <linux/cpu.h>
30 #include <linux/notifier.h>
32 /* How many pages do we try to swap or page in/out together? */
36 * This path almost never happens for VM activity - pages are normally
37 * freed via pagevecs. But it gets used by networking.
39 static void fastcall
__page_cache_release(struct page
*page
)
43 struct zone
*zone
= page_zone(page
);
45 spin_lock_irqsave(&zone
->lru_lock
, flags
);
46 VM_BUG_ON(!PageLRU(page
));
48 del_page_from_lru(zone
, page
);
49 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
54 static void put_compound_page(struct page
*page
)
56 page
= compound_head(page
);
57 if (put_page_testzero(page
)) {
58 compound_page_dtor
*dtor
;
60 dtor
= get_compound_page_dtor(page
);
65 void put_page(struct page
*page
)
67 if (unlikely(PageCompound(page
)))
68 put_compound_page(page
);
69 else if (put_page_testzero(page
))
70 __page_cache_release(page
);
72 EXPORT_SYMBOL(put_page
);
75 * put_pages_list(): release a list of pages
77 * Release a list of pages which are strung together on page.lru. Currently
78 * used by read_cache_pages() and related error recovery code.
80 * @pages: list of pages threaded on page->lru
82 void put_pages_list(struct list_head
*pages
)
84 while (!list_empty(pages
)) {
87 victim
= list_entry(pages
->prev
, struct page
, lru
);
88 list_del(&victim
->lru
);
89 page_cache_release(victim
);
92 EXPORT_SYMBOL(put_pages_list
);
95 * Writeback is about to end against a page which has been marked for immediate
96 * reclaim. If it still appears to be reclaimable, move it to the tail of the
97 * inactive list. The page still has PageWriteback set, which will pin it.
99 * We don't expect many pages to come through here, so don't bother batching
102 * To avoid placing the page at the tail of the LRU while PG_writeback is still
103 * set, this function will clear PG_writeback before performing the page
104 * motion. Do that inside the lru lock because once PG_writeback is cleared
105 * we may not touch the page.
107 * Returns zero if it cleared PG_writeback.
109 int rotate_reclaimable_page(struct page
*page
)
114 if (PageLocked(page
))
118 if (PageActive(page
))
123 zone
= page_zone(page
);
124 spin_lock_irqsave(&zone
->lru_lock
, flags
);
125 if (PageLRU(page
) && !PageActive(page
)) {
126 list_move_tail(&page
->lru
, &zone
->inactive_list
);
127 __count_vm_event(PGROTATED
);
129 if (!test_clear_page_writeback(page
))
131 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
136 * FIXME: speed this up?
138 void fastcall
activate_page(struct page
*page
)
140 struct zone
*zone
= page_zone(page
);
142 spin_lock_irq(&zone
->lru_lock
);
143 if (PageLRU(page
) && !PageActive(page
)) {
144 del_page_from_inactive_list(zone
, page
);
146 add_page_to_active_list(zone
, page
);
147 __count_vm_event(PGACTIVATE
);
149 spin_unlock_irq(&zone
->lru_lock
);
153 * Mark a page as having seen activity.
155 * inactive,unreferenced -> inactive,referenced
156 * inactive,referenced -> active,unreferenced
157 * active,unreferenced -> active,referenced
159 void fastcall
mark_page_accessed(struct page
*page
)
161 if (!PageActive(page
) && PageReferenced(page
) && PageLRU(page
)) {
163 ClearPageReferenced(page
);
164 } else if (!PageReferenced(page
)) {
165 SetPageReferenced(page
);
169 EXPORT_SYMBOL(mark_page_accessed
);
172 * lru_cache_add: add a page to the page lists
173 * @page: the page to add
175 static DEFINE_PER_CPU(struct pagevec
, lru_add_pvecs
) = { 0, };
176 static DEFINE_PER_CPU(struct pagevec
, lru_add_active_pvecs
) = { 0, };
178 void fastcall
lru_cache_add(struct page
*page
)
180 struct pagevec
*pvec
= &get_cpu_var(lru_add_pvecs
);
182 page_cache_get(page
);
183 if (!pagevec_add(pvec
, page
))
184 __pagevec_lru_add(pvec
);
185 put_cpu_var(lru_add_pvecs
);
188 void fastcall
lru_cache_add_active(struct page
*page
)
190 struct pagevec
*pvec
= &get_cpu_var(lru_add_active_pvecs
);
192 page_cache_get(page
);
193 if (!pagevec_add(pvec
, page
))
194 __pagevec_lru_add_active(pvec
);
195 put_cpu_var(lru_add_active_pvecs
);
198 static void __lru_add_drain(int cpu
)
200 struct pagevec
*pvec
= &per_cpu(lru_add_pvecs
, cpu
);
202 /* CPU is dead, so no locking needed. */
203 if (pagevec_count(pvec
))
204 __pagevec_lru_add(pvec
);
205 pvec
= &per_cpu(lru_add_active_pvecs
, cpu
);
206 if (pagevec_count(pvec
))
207 __pagevec_lru_add_active(pvec
);
210 void lru_add_drain(void)
212 __lru_add_drain(get_cpu());
217 static void lru_add_drain_per_cpu(struct work_struct
*dummy
)
223 * Returns 0 for success
225 int lru_add_drain_all(void)
227 return schedule_on_each_cpu(lru_add_drain_per_cpu
);
233 * Returns 0 for success
235 int lru_add_drain_all(void)
243 * Batched page_cache_release(). Decrement the reference count on all the
244 * passed pages. If it fell to zero then remove the page from the LRU and
247 * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
248 * for the remainder of the operation.
250 * The locking in this function is against shrink_cache(): we recheck the
251 * page count inside the lock to see whether shrink_cache grabbed the page
252 * via the LRU. If it did, give up: shrink_cache will free it.
254 void release_pages(struct page
**pages
, int nr
, int cold
)
257 struct pagevec pages_to_free
;
258 struct zone
*zone
= NULL
;
260 pagevec_init(&pages_to_free
, cold
);
261 for (i
= 0; i
< nr
; i
++) {
262 struct page
*page
= pages
[i
];
264 if (unlikely(PageCompound(page
))) {
266 spin_unlock_irq(&zone
->lru_lock
);
269 put_compound_page(page
);
273 if (!put_page_testzero(page
))
277 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 VM_BUG_ON(!PageLRU(page
));
285 __ClearPageLRU(page
);
286 del_page_from_lru(zone
, page
);
289 if (!pagevec_add(&pages_to_free
, page
)) {
291 spin_unlock_irq(&zone
->lru_lock
);
294 __pagevec_free(&pages_to_free
);
295 pagevec_reinit(&pages_to_free
);
299 spin_unlock_irq(&zone
->lru_lock
);
301 pagevec_free(&pages_to_free
);
305 * The pages which we're about to release may be in the deferred lru-addition
306 * queues. That would prevent them from really being freed right now. That's
307 * OK from a correctness point of view but is inefficient - those pages may be
308 * cache-warm and we want to give them back to the page allocator ASAP.
310 * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
311 * and __pagevec_lru_add_active() call release_pages() directly to avoid
314 void __pagevec_release(struct pagevec
*pvec
)
317 release_pages(pvec
->pages
, pagevec_count(pvec
), pvec
->cold
);
318 pagevec_reinit(pvec
);
321 EXPORT_SYMBOL(__pagevec_release
);
324 * pagevec_release() for pages which are known to not be on the LRU
326 * This function reinitialises the caller's pagevec.
328 void __pagevec_release_nonlru(struct pagevec
*pvec
)
331 struct pagevec pages_to_free
;
333 pagevec_init(&pages_to_free
, pvec
->cold
);
334 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
335 struct page
*page
= pvec
->pages
[i
];
337 VM_BUG_ON(PageLRU(page
));
338 if (put_page_testzero(page
))
339 pagevec_add(&pages_to_free
, page
);
341 pagevec_free(&pages_to_free
);
342 pagevec_reinit(pvec
);
346 * Add the passed pages to the LRU, then drop the caller's refcount
347 * on them. Reinitialises the caller's pagevec.
349 void __pagevec_lru_add(struct pagevec
*pvec
)
352 struct zone
*zone
= NULL
;
354 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
355 struct page
*page
= pvec
->pages
[i
];
356 struct zone
*pagezone
= page_zone(page
);
358 if (pagezone
!= zone
) {
360 spin_unlock_irq(&zone
->lru_lock
);
362 spin_lock_irq(&zone
->lru_lock
);
364 VM_BUG_ON(PageLRU(page
));
366 add_page_to_inactive_list(zone
, page
);
369 spin_unlock_irq(&zone
->lru_lock
);
370 release_pages(pvec
->pages
, pvec
->nr
, pvec
->cold
);
371 pagevec_reinit(pvec
);
374 EXPORT_SYMBOL(__pagevec_lru_add
);
376 void __pagevec_lru_add_active(struct pagevec
*pvec
)
379 struct zone
*zone
= NULL
;
381 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
382 struct page
*page
= pvec
->pages
[i
];
383 struct zone
*pagezone
= page_zone(page
);
385 if (pagezone
!= zone
) {
387 spin_unlock_irq(&zone
->lru_lock
);
389 spin_lock_irq(&zone
->lru_lock
);
391 VM_BUG_ON(PageLRU(page
));
393 VM_BUG_ON(PageActive(page
));
395 add_page_to_active_list(zone
, page
);
398 spin_unlock_irq(&zone
->lru_lock
);
399 release_pages(pvec
->pages
, pvec
->nr
, pvec
->cold
);
400 pagevec_reinit(pvec
);
404 * Try to drop buffers from the pages in a pagevec
406 void pagevec_strip(struct pagevec
*pvec
)
410 for (i
= 0; i
< pagevec_count(pvec
); i
++) {
411 struct page
*page
= pvec
->pages
[i
];
413 if (PagePrivate(page
) && !TestSetPageLocked(page
)) {
414 if (PagePrivate(page
))
415 try_to_release_page(page
, 0);
422 * pagevec_lookup - gang pagecache lookup
423 * @pvec: Where the resulting pages are placed
424 * @mapping: The address_space to search
425 * @start: The starting page index
426 * @nr_pages: The maximum number of pages
428 * pagevec_lookup() will search for and return a group of up to @nr_pages pages
429 * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
430 * reference against the pages in @pvec.
432 * The search returns a group of mapping-contiguous pages with ascending
433 * indexes. There may be holes in the indices due to not-present pages.
435 * pagevec_lookup() returns the number of pages which were found.
437 unsigned pagevec_lookup(struct pagevec
*pvec
, struct address_space
*mapping
,
438 pgoff_t start
, unsigned nr_pages
)
440 pvec
->nr
= find_get_pages(mapping
, start
, nr_pages
, pvec
->pages
);
441 return pagevec_count(pvec
);
444 EXPORT_SYMBOL(pagevec_lookup
);
446 unsigned pagevec_lookup_tag(struct pagevec
*pvec
, struct address_space
*mapping
,
447 pgoff_t
*index
, int tag
, unsigned nr_pages
)
449 pvec
->nr
= find_get_pages_tag(mapping
, index
, tag
,
450 nr_pages
, pvec
->pages
);
451 return pagevec_count(pvec
);
454 EXPORT_SYMBOL(pagevec_lookup_tag
);
458 * We tolerate a little inaccuracy to avoid ping-ponging the counter between
461 #define ACCT_THRESHOLD max(16, NR_CPUS * 2)
463 static DEFINE_PER_CPU(long, committed_space
) = 0;
465 void vm_acct_memory(long pages
)
470 local
= &__get_cpu_var(committed_space
);
472 if (*local
> ACCT_THRESHOLD
|| *local
< -ACCT_THRESHOLD
) {
473 atomic_add(*local
, &vm_committed_space
);
479 #ifdef CONFIG_HOTPLUG_CPU
481 /* Drop the CPU's cached committed space back into the central pool. */
482 static int cpu_swap_callback(struct notifier_block
*nfb
,
483 unsigned long action
,
488 committed
= &per_cpu(committed_space
, (long)hcpu
);
489 if (action
== CPU_DEAD
|| action
== CPU_DEAD_FROZEN
) {
490 atomic_add(*committed
, &vm_committed_space
);
492 __lru_add_drain((long)hcpu
);
496 #endif /* CONFIG_HOTPLUG_CPU */
497 #endif /* CONFIG_SMP */
500 * Perform any setup for the swap system
502 void __init
swap_setup(void)
504 unsigned long megs
= num_physpages
>> (20 - PAGE_SHIFT
);
506 /* Use a smaller cluster for small-memory machines */
512 * Right now other parts of the system means that we
513 * _really_ don't want to cluster much more
515 #ifdef CONFIG_HOTPLUG_CPU
516 hotcpu_notifier(cpu_swap_callback
, 0);