Lynx framebuffers multidomain implementation.
[linux/elbrus.git] / mm / swap.c
blob9a9d4c7da2cc8a224596fbcfa69923629cd5f01a
1 /*
2 * linux/mm/swap.c
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 */
7 /*
8 * This file contains the default values for the operation 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.
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/export.h>
25 #include <linux/mm_inline.h>
26 #include <linux/percpu_counter.h>
27 #include <linux/percpu.h>
28 #include <linux/cpu.h>
29 #include <linux/notifier.h>
30 #include <linux/backing-dev.h>
31 #include <linux/memcontrol.h>
32 #include <linux/gfp.h>
33 #include <linux/uio.h>
34 #include <linux/locallock.h>
36 #include "internal.h"
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/pagemap.h>
41 /* How many pages do we try to swap or page in/out together? */
42 int page_cluster;
44 static DEFINE_PER_CPU(struct pagevec, lru_add_pvec);
45 static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
46 static DEFINE_PER_CPU(struct pagevec, lru_deactivate_pvecs);
48 static DEFINE_LOCAL_IRQ_LOCK(rotate_lock);
49 static DEFINE_LOCAL_IRQ_LOCK(swapvec_lock);
52 * This path almost never happens for VM activity - pages are normally
53 * freed via pagevecs. But it gets used by networking.
55 static void __page_cache_release(struct page *page)
57 if (PageLRU(page)) {
58 struct zone *zone = page_zone(page);
59 struct lruvec *lruvec;
60 unsigned long flags;
62 spin_lock_irqsave(&zone->lru_lock, flags);
63 lruvec = mem_cgroup_page_lruvec(page, zone);
64 VM_BUG_ON_PAGE(!PageLRU(page), page);
65 __ClearPageLRU(page);
66 del_page_from_lru_list(page, lruvec, page_off_lru(page));
67 spin_unlock_irqrestore(&zone->lru_lock, flags);
71 static void __put_single_page(struct page *page)
73 __page_cache_release(page);
74 free_hot_cold_page(page, false);
77 static void __put_compound_page(struct page *page)
79 compound_page_dtor *dtor;
81 __page_cache_release(page);
82 dtor = get_compound_page_dtor(page);
83 (*dtor)(page);
86 static void put_compound_page(struct page *page)
88 struct page *page_head;
90 if (likely(!PageTail(page))) {
91 if (put_page_testzero(page)) {
93 * By the time all refcounts have been released
94 * split_huge_page cannot run anymore from under us.
96 if (PageHead(page))
97 __put_compound_page(page);
98 else
99 __put_single_page(page);
101 return;
104 /* __split_huge_page_refcount can run under us */
105 page_head = compound_head(page);
108 * THP can not break up slab pages so avoid taking
109 * compound_lock() and skip the tail page refcounting (in
110 * _mapcount) too. Slab performs non-atomic bit ops on
111 * page->flags for better performance. In particular
112 * slab_unlock() in slub used to be a hot path. It is still
113 * hot on arches that do not support
114 * this_cpu_cmpxchg_double().
116 * If "page" is part of a slab or hugetlbfs page it cannot be
117 * splitted and the head page cannot change from under us. And
118 * if "page" is part of a THP page under splitting, if the
119 * head page pointed by the THP tail isn't a THP head anymore,
120 * we'll find PageTail clear after smp_rmb() and we'll treat
121 * it as a single page.
123 if (!__compound_tail_refcounted(page_head)) {
125 * If "page" is a THP tail, we must read the tail page
126 * flags after the head page flags. The
127 * split_huge_page side enforces write memory barriers
128 * between clearing PageTail and before the head page
129 * can be freed and reallocated.
131 smp_rmb();
132 if (likely(PageTail(page))) {
134 * __split_huge_page_refcount cannot race
135 * here.
137 VM_BUG_ON_PAGE(!PageHead(page_head), page_head);
138 VM_BUG_ON_PAGE(page_mapcount(page) != 0, page);
139 if (put_page_testzero(page_head)) {
141 * If this is the tail of a slab
142 * compound page, the tail pin must
143 * not be the last reference held on
144 * the page, because the PG_slab
145 * cannot be cleared before all tail
146 * pins (which skips the _mapcount
147 * tail refcounting) have been
148 * released. For hugetlbfs the tail
149 * pin may be the last reference on
150 * the page instead, because
151 * PageHeadHuge will not go away until
152 * the compound page enters the buddy
153 * allocator.
155 VM_BUG_ON_PAGE(PageSlab(page_head), page_head);
156 __put_compound_page(page_head);
158 return;
159 } else
161 * __split_huge_page_refcount run before us,
162 * "page" was a THP tail. The split page_head
163 * has been freed and reallocated as slab or
164 * hugetlbfs page of smaller order (only
165 * possible if reallocated as slab on x86).
167 goto out_put_single;
170 if (likely(page != page_head && get_page_unless_zero(page_head))) {
171 unsigned long flags;
174 * page_head wasn't a dangling pointer but it may not
175 * be a head page anymore by the time we obtain the
176 * lock. That is ok as long as it can't be freed from
177 * under us.
179 flags = compound_lock_irqsave(page_head);
180 if (unlikely(!PageTail(page))) {
181 /* __split_huge_page_refcount run before us */
182 compound_unlock_irqrestore(page_head, flags);
183 if (put_page_testzero(page_head)) {
185 * The head page may have been freed
186 * and reallocated as a compound page
187 * of smaller order and then freed
188 * again. All we know is that it
189 * cannot have become: a THP page, a
190 * compound page of higher order, a
191 * tail page. That is because we
192 * still hold the refcount of the
193 * split THP tail and page_head was
194 * the THP head before the split.
196 if (PageHead(page_head))
197 __put_compound_page(page_head);
198 else
199 __put_single_page(page_head);
201 out_put_single:
202 if (put_page_testzero(page))
203 __put_single_page(page);
204 return;
206 VM_BUG_ON_PAGE(page_head != page->first_page, page);
208 * We can release the refcount taken by
209 * get_page_unless_zero() now that
210 * __split_huge_page_refcount() is blocked on the
211 * compound_lock.
213 if (put_page_testzero(page_head))
214 VM_BUG_ON_PAGE(1, page_head);
215 /* __split_huge_page_refcount will wait now */
216 VM_BUG_ON_PAGE(page_mapcount(page) <= 0, page);
217 atomic_dec(&page->_mapcount);
218 VM_BUG_ON_PAGE(atomic_read(&page_head->_count) <= 0, page_head);
219 VM_BUG_ON_PAGE(atomic_read(&page->_count) != 0, page);
220 compound_unlock_irqrestore(page_head, flags);
222 if (put_page_testzero(page_head)) {
223 if (PageHead(page_head))
224 __put_compound_page(page_head);
225 else
226 __put_single_page(page_head);
228 } else {
229 /* page_head is a dangling pointer */
230 VM_BUG_ON_PAGE(PageTail(page), page);
231 goto out_put_single;
235 void put_page(struct page *page)
237 if (unlikely(PageCompound(page)))
238 put_compound_page(page);
239 else if (put_page_testzero(page))
240 __put_single_page(page);
242 EXPORT_SYMBOL(put_page);
245 * This function is exported but must not be called by anything other
246 * than get_page(). It implements the slow path of get_page().
248 bool __get_page_tail(struct page *page)
251 * This takes care of get_page() if run on a tail page
252 * returned by one of the get_user_pages/follow_page variants.
253 * get_user_pages/follow_page itself doesn't need the compound
254 * lock because it runs __get_page_tail_foll() under the
255 * proper PT lock that already serializes against
256 * split_huge_page().
258 unsigned long flags;
259 bool got;
260 struct page *page_head = compound_head(page);
262 /* Ref to put_compound_page() comment. */
263 if (!__compound_tail_refcounted(page_head)) {
264 smp_rmb();
265 if (likely(PageTail(page))) {
267 * This is a hugetlbfs page or a slab
268 * page. __split_huge_page_refcount
269 * cannot race here.
271 VM_BUG_ON_PAGE(!PageHead(page_head), page_head);
272 __get_page_tail_foll(page, true);
273 return true;
274 } else {
276 * __split_huge_page_refcount run
277 * before us, "page" was a THP
278 * tail. The split page_head has been
279 * freed and reallocated as slab or
280 * hugetlbfs page of smaller order
281 * (only possible if reallocated as
282 * slab on x86).
284 return false;
288 got = false;
289 if (likely(page != page_head && get_page_unless_zero(page_head))) {
291 * page_head wasn't a dangling pointer but it
292 * may not be a head page anymore by the time
293 * we obtain the lock. That is ok as long as it
294 * can't be freed from under us.
296 flags = compound_lock_irqsave(page_head);
297 /* here __split_huge_page_refcount won't run anymore */
298 if (likely(PageTail(page))) {
299 __get_page_tail_foll(page, false);
300 got = true;
302 compound_unlock_irqrestore(page_head, flags);
303 if (unlikely(!got))
304 put_page(page_head);
306 return got;
308 EXPORT_SYMBOL(__get_page_tail);
311 * put_pages_list() - release a list of pages
312 * @pages: list of pages threaded on page->lru
314 * Release a list of pages which are strung together on page.lru. Currently
315 * used by read_cache_pages() and related error recovery code.
317 void put_pages_list(struct list_head *pages)
319 while (!list_empty(pages)) {
320 struct page *victim;
322 victim = list_entry(pages->prev, struct page, lru);
323 list_del(&victim->lru);
324 page_cache_release(victim);
327 EXPORT_SYMBOL(put_pages_list);
330 * get_kernel_pages() - pin kernel pages in memory
331 * @kiov: An array of struct kvec structures
332 * @nr_segs: number of segments to pin
333 * @write: pinning for read/write, currently ignored
334 * @pages: array that receives pointers to the pages pinned.
335 * Should be at least nr_segs long.
337 * Returns number of pages pinned. This may be fewer than the number
338 * requested. If nr_pages is 0 or negative, returns 0. If no pages
339 * were pinned, returns -errno. Each page returned must be released
340 * with a put_page() call when it is finished with.
342 int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write,
343 struct page **pages)
345 int seg;
347 for (seg = 0; seg < nr_segs; seg++) {
348 if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE))
349 return seg;
351 pages[seg] = kmap_to_page(kiov[seg].iov_base);
352 page_cache_get(pages[seg]);
355 return seg;
357 EXPORT_SYMBOL_GPL(get_kernel_pages);
360 * get_kernel_page() - pin a kernel page in memory
361 * @start: starting kernel address
362 * @write: pinning for read/write, currently ignored
363 * @pages: array that receives pointer to the page pinned.
364 * Must be at least nr_segs long.
366 * Returns 1 if page is pinned. If the page was not pinned, returns
367 * -errno. The page returned must be released with a put_page() call
368 * when it is finished with.
370 int get_kernel_page(unsigned long start, int write, struct page **pages)
372 const struct kvec kiov = {
373 .iov_base = (void *)start,
374 .iov_len = PAGE_SIZE
377 return get_kernel_pages(&kiov, 1, write, pages);
379 EXPORT_SYMBOL_GPL(get_kernel_page);
381 static void pagevec_lru_move_fn(struct pagevec *pvec,
382 void (*move_fn)(struct page *page, struct lruvec *lruvec, void *arg),
383 void *arg)
385 int i;
386 struct zone *zone = NULL;
387 struct lruvec *lruvec;
388 unsigned long flags = 0;
390 for (i = 0; i < pagevec_count(pvec); i++) {
391 struct page *page = pvec->pages[i];
392 struct zone *pagezone = page_zone(page);
394 if (pagezone != zone) {
395 if (zone)
396 spin_unlock_irqrestore(&zone->lru_lock, flags);
397 zone = pagezone;
398 spin_lock_irqsave(&zone->lru_lock, flags);
401 lruvec = mem_cgroup_page_lruvec(page, zone);
402 (*move_fn)(page, lruvec, arg);
404 if (zone)
405 spin_unlock_irqrestore(&zone->lru_lock, flags);
406 release_pages(pvec->pages, pvec->nr, pvec->cold);
407 pagevec_reinit(pvec);
410 static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec,
411 void *arg)
413 int *pgmoved = arg;
415 if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
416 enum lru_list lru = page_lru_base_type(page);
417 list_move_tail(&page->lru, &lruvec->lists[lru]);
418 (*pgmoved)++;
423 * pagevec_move_tail() must be called with IRQ disabled.
424 * Otherwise this may cause nasty races.
426 static void pagevec_move_tail(struct pagevec *pvec)
428 int pgmoved = 0;
430 pagevec_lru_move_fn(pvec, pagevec_move_tail_fn, &pgmoved);
431 __count_vm_events(PGROTATED, pgmoved);
435 * Writeback is about to end against a page which has been marked for immediate
436 * reclaim. If it still appears to be reclaimable, move it to the tail of the
437 * inactive list.
439 void rotate_reclaimable_page(struct page *page)
441 if (!PageLocked(page) && !PageDirty(page) && !PageActive(page) &&
442 !PageUnevictable(page) && PageLRU(page)) {
443 struct pagevec *pvec;
444 unsigned long flags;
446 page_cache_get(page);
447 local_lock_irqsave(rotate_lock, flags);
448 pvec = &__get_cpu_var(lru_rotate_pvecs);
449 if (!pagevec_add(pvec, page))
450 pagevec_move_tail(pvec);
451 local_unlock_irqrestore(rotate_lock, flags);
455 static void update_page_reclaim_stat(struct lruvec *lruvec,
456 int file, int rotated)
458 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
460 reclaim_stat->recent_scanned[file]++;
461 if (rotated)
462 reclaim_stat->recent_rotated[file]++;
465 static void __activate_page(struct page *page, struct lruvec *lruvec,
466 void *arg)
468 if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
469 int file = page_is_file_cache(page);
470 int lru = page_lru_base_type(page);
472 del_page_from_lru_list(page, lruvec, lru);
473 SetPageActive(page);
474 lru += LRU_ACTIVE;
475 add_page_to_lru_list(page, lruvec, lru);
476 trace_mm_lru_activate(page);
478 __count_vm_event(PGACTIVATE);
479 update_page_reclaim_stat(lruvec, file, 1);
483 #ifdef CONFIG_SMP
484 static DEFINE_PER_CPU(struct pagevec, activate_page_pvecs);
486 static void activate_page_drain(int cpu)
488 struct pagevec *pvec = &per_cpu(activate_page_pvecs, cpu);
490 if (pagevec_count(pvec))
491 pagevec_lru_move_fn(pvec, __activate_page, NULL);
494 static bool need_activate_page_drain(int cpu)
496 return pagevec_count(&per_cpu(activate_page_pvecs, cpu)) != 0;
499 void activate_page(struct page *page)
501 if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
502 struct pagevec *pvec = &get_locked_var(swapvec_lock,
503 activate_page_pvecs);
505 page_cache_get(page);
506 if (!pagevec_add(pvec, page))
507 pagevec_lru_move_fn(pvec, __activate_page, NULL);
508 put_locked_var(swapvec_lock, activate_page_pvecs);
512 #else
513 static inline void activate_page_drain(int cpu)
517 static bool need_activate_page_drain(int cpu)
519 return false;
522 void activate_page(struct page *page)
524 struct zone *zone = page_zone(page);
526 spin_lock_irq(&zone->lru_lock);
527 __activate_page(page, mem_cgroup_page_lruvec(page, zone), NULL);
528 spin_unlock_irq(&zone->lru_lock);
530 #endif
532 static void __lru_cache_activate_page(struct page *page)
534 struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
535 int i;
538 * Search backwards on the optimistic assumption that the page being
539 * activated has just been added to this pagevec. Note that only
540 * the local pagevec is examined as a !PageLRU page could be in the
541 * process of being released, reclaimed, migrated or on a remote
542 * pagevec that is currently being drained. Furthermore, marking
543 * a remote pagevec's page PageActive potentially hits a race where
544 * a page is marked PageActive just after it is added to the inactive
545 * list causing accounting errors and BUG_ON checks to trigger.
547 for (i = pagevec_count(pvec) - 1; i >= 0; i--) {
548 struct page *pagevec_page = pvec->pages[i];
550 if (pagevec_page == page) {
551 SetPageActive(page);
552 break;
556 put_locked_var(swapvec_lock, lru_add_pvec);
560 * Mark a page as having seen activity.
562 * inactive,unreferenced -> inactive,referenced
563 * inactive,referenced -> active,unreferenced
564 * active,unreferenced -> active,referenced
566 void mark_page_accessed(struct page *page)
568 if (!PageActive(page) && !PageUnevictable(page) &&
569 PageReferenced(page)) {
572 * If the page is on the LRU, queue it for activation via
573 * activate_page_pvecs. Otherwise, assume the page is on a
574 * pagevec, mark it active and it'll be moved to the active
575 * LRU on the next drain.
577 if (PageLRU(page))
578 activate_page(page);
579 else
580 __lru_cache_activate_page(page);
581 ClearPageReferenced(page);
582 } else if (!PageReferenced(page)) {
583 SetPageReferenced(page);
586 EXPORT_SYMBOL(mark_page_accessed);
589 * Used to mark_page_accessed(page) that is not visible yet and when it is
590 * still safe to use non-atomic ops
592 void init_page_accessed(struct page *page)
594 if (!PageReferenced(page))
595 __SetPageReferenced(page);
597 EXPORT_SYMBOL(init_page_accessed);
599 static void __lru_cache_add(struct page *page)
601 struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
603 page_cache_get(page);
604 if (!pagevec_space(pvec))
605 __pagevec_lru_add(pvec);
606 pagevec_add(pvec, page);
607 put_locked_var(swapvec_lock, lru_add_pvec);
611 * lru_cache_add: add a page to the page lists
612 * @page: the page to add
614 void lru_cache_add_anon(struct page *page)
616 if (PageActive(page))
617 ClearPageActive(page);
618 __lru_cache_add(page);
621 void lru_cache_add_file(struct page *page)
623 if (PageActive(page))
624 ClearPageActive(page);
625 __lru_cache_add(page);
627 EXPORT_SYMBOL(lru_cache_add_file);
630 * lru_cache_add - add a page to a page list
631 * @page: the page to be added to the LRU.
633 * Queue the page for addition to the LRU via pagevec. The decision on whether
634 * to add the page to the [in]active [file|anon] list is deferred until the
635 * pagevec is drained. This gives a chance for the caller of lru_cache_add()
636 * have the page added to the active list using mark_page_accessed().
638 void lru_cache_add(struct page *page)
640 VM_BUG_ON_PAGE(PageActive(page) && PageUnevictable(page), page);
641 VM_BUG_ON_PAGE(PageLRU(page), page);
642 __lru_cache_add(page);
646 * add_page_to_unevictable_list - add a page to the unevictable list
647 * @page: the page to be added to the unevictable list
649 * Add page directly to its zone's unevictable list. To avoid races with
650 * tasks that might be making the page evictable, through eg. munlock,
651 * munmap or exit, while it's not on the lru, we want to add the page
652 * while it's locked or otherwise "invisible" to other tasks. This is
653 * difficult to do when using the pagevec cache, so bypass that.
655 void add_page_to_unevictable_list(struct page *page)
657 struct zone *zone = page_zone(page);
658 struct lruvec *lruvec;
660 spin_lock_irq(&zone->lru_lock);
661 lruvec = mem_cgroup_page_lruvec(page, zone);
662 ClearPageActive(page);
663 SetPageUnevictable(page);
664 SetPageLRU(page);
665 add_page_to_lru_list(page, lruvec, LRU_UNEVICTABLE);
666 spin_unlock_irq(&zone->lru_lock);
670 * If the page can not be invalidated, it is moved to the
671 * inactive list to speed up its reclaim. It is moved to the
672 * head of the list, rather than the tail, to give the flusher
673 * threads some time to write it out, as this is much more
674 * effective than the single-page writeout from reclaim.
676 * If the page isn't page_mapped and dirty/writeback, the page
677 * could reclaim asap using PG_reclaim.
679 * 1. active, mapped page -> none
680 * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
681 * 3. inactive, mapped page -> none
682 * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
683 * 5. inactive, clean -> inactive, tail
684 * 6. Others -> none
686 * In 4, why it moves inactive's head, the VM expects the page would
687 * be write it out by flusher threads as this is much more effective
688 * than the single-page writeout from reclaim.
690 static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec,
691 void *arg)
693 int lru, file;
694 bool active;
696 if (!PageLRU(page))
697 return;
699 if (PageUnevictable(page))
700 return;
702 /* Some processes are using the page */
703 if (page_mapped(page))
704 return;
706 active = PageActive(page);
707 file = page_is_file_cache(page);
708 lru = page_lru_base_type(page);
710 del_page_from_lru_list(page, lruvec, lru + active);
711 ClearPageActive(page);
712 ClearPageReferenced(page);
713 add_page_to_lru_list(page, lruvec, lru);
715 if (PageWriteback(page) || PageDirty(page)) {
717 * PG_reclaim could be raced with end_page_writeback
718 * It can make readahead confusing. But race window
719 * is _really_ small and it's non-critical problem.
721 SetPageReclaim(page);
722 } else {
724 * The page's writeback ends up during pagevec
725 * We moves tha page into tail of inactive.
727 list_move_tail(&page->lru, &lruvec->lists[lru]);
728 __count_vm_event(PGROTATED);
731 if (active)
732 __count_vm_event(PGDEACTIVATE);
733 update_page_reclaim_stat(lruvec, file, 0);
737 * Drain pages out of the cpu's pagevecs.
738 * Either "cpu" is the current CPU, and preemption has already been
739 * disabled; or "cpu" is being hot-unplugged, and is already dead.
741 void lru_add_drain_cpu(int cpu)
743 struct pagevec *pvec = &per_cpu(lru_add_pvec, cpu);
745 if (pagevec_count(pvec))
746 __pagevec_lru_add(pvec);
748 pvec = &per_cpu(lru_rotate_pvecs, cpu);
749 if (pagevec_count(pvec)) {
750 unsigned long flags;
752 /* No harm done if a racing interrupt already did this */
753 local_lock_irqsave(rotate_lock, flags);
754 pagevec_move_tail(pvec);
755 local_unlock_irqrestore(rotate_lock, flags);
758 pvec = &per_cpu(lru_deactivate_pvecs, cpu);
759 if (pagevec_count(pvec))
760 pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
762 activate_page_drain(cpu);
766 * deactivate_page - forcefully deactivate a page
767 * @page: page to deactivate
769 * This function hints the VM that @page is a good reclaim candidate,
770 * for example if its invalidation fails due to the page being dirty
771 * or under writeback.
773 void deactivate_page(struct page *page)
776 * In a workload with many unevictable page such as mprotect, unevictable
777 * page deactivation for accelerating reclaim is pointless.
779 if (PageUnevictable(page))
780 return;
782 if (likely(get_page_unless_zero(page))) {
783 struct pagevec *pvec = &get_locked_var(swapvec_lock,
784 lru_deactivate_pvecs);
786 if (!pagevec_add(pvec, page))
787 pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
788 put_locked_var(swapvec_lock, lru_deactivate_pvecs);
792 void lru_add_drain(void)
794 lru_add_drain_cpu(local_lock_cpu(swapvec_lock));
795 local_unlock_cpu(swapvec_lock);
798 static void lru_add_drain_per_cpu(struct work_struct *dummy)
800 lru_add_drain();
803 static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
805 void lru_add_drain_all(void)
807 static DEFINE_MUTEX(lock);
808 static struct cpumask has_work;
809 int cpu;
811 mutex_lock(&lock);
812 get_online_cpus();
813 cpumask_clear(&has_work);
815 for_each_online_cpu(cpu) {
816 struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
818 if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
819 pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
820 pagevec_count(&per_cpu(lru_deactivate_pvecs, cpu)) ||
821 need_activate_page_drain(cpu)) {
822 INIT_WORK(work, lru_add_drain_per_cpu);
823 schedule_work_on(cpu, work);
824 cpumask_set_cpu(cpu, &has_work);
828 for_each_cpu(cpu, &has_work)
829 flush_work(&per_cpu(lru_add_drain_work, cpu));
831 put_online_cpus();
832 mutex_unlock(&lock);
836 * Batched page_cache_release(). Decrement the reference count on all the
837 * passed pages. If it fell to zero then remove the page from the LRU and
838 * free it.
840 * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
841 * for the remainder of the operation.
843 * The locking in this function is against shrink_inactive_list(): we recheck
844 * the page count inside the lock to see whether shrink_inactive_list()
845 * grabbed the page via the LRU. If it did, give up: shrink_inactive_list()
846 * will free it.
848 void release_pages(struct page **pages, int nr, bool cold)
850 int i;
851 LIST_HEAD(pages_to_free);
852 struct zone *zone = NULL;
853 struct lruvec *lruvec;
854 unsigned long uninitialized_var(flags);
856 for (i = 0; i < nr; i++) {
857 struct page *page = pages[i];
859 if (unlikely(PageCompound(page))) {
860 if (zone) {
861 spin_unlock_irqrestore(&zone->lru_lock, flags);
862 zone = NULL;
864 put_compound_page(page);
865 continue;
868 if (!put_page_testzero(page))
869 continue;
871 if (PageLRU(page)) {
872 struct zone *pagezone = page_zone(page);
874 if (pagezone != zone) {
875 if (zone)
876 spin_unlock_irqrestore(&zone->lru_lock,
877 flags);
878 zone = pagezone;
879 spin_lock_irqsave(&zone->lru_lock, flags);
882 lruvec = mem_cgroup_page_lruvec(page, zone);
883 VM_BUG_ON_PAGE(!PageLRU(page), page);
884 __ClearPageLRU(page);
885 del_page_from_lru_list(page, lruvec, page_off_lru(page));
888 /* Clear Active bit in case of parallel mark_page_accessed */
889 __ClearPageActive(page);
891 list_add(&page->lru, &pages_to_free);
893 if (zone)
894 spin_unlock_irqrestore(&zone->lru_lock, flags);
896 free_hot_cold_page_list(&pages_to_free, cold);
898 EXPORT_SYMBOL(release_pages);
901 * The pages which we're about to release may be in the deferred lru-addition
902 * queues. That would prevent them from really being freed right now. That's
903 * OK from a correctness point of view but is inefficient - those pages may be
904 * cache-warm and we want to give them back to the page allocator ASAP.
906 * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
907 * and __pagevec_lru_add_active() call release_pages() directly to avoid
908 * mutual recursion.
910 void __pagevec_release(struct pagevec *pvec)
912 lru_add_drain();
913 release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
914 pagevec_reinit(pvec);
916 EXPORT_SYMBOL(__pagevec_release);
918 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
919 /* used by __split_huge_page_refcount() */
920 void lru_add_page_tail(struct page *page, struct page *page_tail,
921 struct lruvec *lruvec, struct list_head *list)
923 const int file = 0;
925 VM_BUG_ON_PAGE(!PageHead(page), page);
926 VM_BUG_ON_PAGE(PageCompound(page_tail), page);
927 VM_BUG_ON_PAGE(PageLRU(page_tail), page);
928 VM_BUG_ON(NR_CPUS != 1 &&
929 !spin_is_locked(&lruvec_zone(lruvec)->lru_lock));
931 if (!list)
932 SetPageLRU(page_tail);
934 if (likely(PageLRU(page)))
935 list_add_tail(&page_tail->lru, &page->lru);
936 else if (list) {
937 /* page reclaim is reclaiming a huge page */
938 get_page(page_tail);
939 list_add_tail(&page_tail->lru, list);
940 } else {
941 struct list_head *list_head;
943 * Head page has not yet been counted, as an hpage,
944 * so we must account for each subpage individually.
946 * Use the standard add function to put page_tail on the list,
947 * but then correct its position so they all end up in order.
949 add_page_to_lru_list(page_tail, lruvec, page_lru(page_tail));
950 list_head = page_tail->lru.prev;
951 list_move_tail(&page_tail->lru, list_head);
954 if (!PageUnevictable(page))
955 update_page_reclaim_stat(lruvec, file, PageActive(page_tail));
957 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
959 static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec,
960 void *arg)
962 int file = page_is_file_cache(page);
963 int active = PageActive(page);
964 enum lru_list lru = page_lru(page);
966 VM_BUG_ON_PAGE(PageLRU(page), page);
968 SetPageLRU(page);
969 add_page_to_lru_list(page, lruvec, lru);
970 update_page_reclaim_stat(lruvec, file, active);
971 trace_mm_lru_insertion(page, lru);
975 * Add the passed pages to the LRU, then drop the caller's refcount
976 * on them. Reinitialises the caller's pagevec.
978 void __pagevec_lru_add(struct pagevec *pvec)
980 pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL);
982 EXPORT_SYMBOL(__pagevec_lru_add);
985 * pagevec_lookup_entries - gang pagecache lookup
986 * @pvec: Where the resulting entries are placed
987 * @mapping: The address_space to search
988 * @start: The starting entry index
989 * @nr_entries: The maximum number of entries
990 * @indices: The cache indices corresponding to the entries in @pvec
992 * pagevec_lookup_entries() will search for and return a group of up
993 * to @nr_entries pages and shadow entries in the mapping. All
994 * entries are placed in @pvec. pagevec_lookup_entries() takes a
995 * reference against actual pages in @pvec.
997 * The search returns a group of mapping-contiguous entries with
998 * ascending indexes. There may be holes in the indices due to
999 * not-present entries.
1001 * pagevec_lookup_entries() returns the number of entries which were
1002 * found.
1004 unsigned pagevec_lookup_entries(struct pagevec *pvec,
1005 struct address_space *mapping,
1006 pgoff_t start, unsigned nr_pages,
1007 pgoff_t *indices)
1009 pvec->nr = find_get_entries(mapping, start, nr_pages,
1010 pvec->pages, indices);
1011 return pagevec_count(pvec);
1015 * pagevec_remove_exceptionals - pagevec exceptionals pruning
1016 * @pvec: The pagevec to prune
1018 * pagevec_lookup_entries() fills both pages and exceptional radix
1019 * tree entries into the pagevec. This function prunes all
1020 * exceptionals from @pvec without leaving holes, so that it can be
1021 * passed on to page-only pagevec operations.
1023 void pagevec_remove_exceptionals(struct pagevec *pvec)
1025 int i, j;
1027 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
1028 struct page *page = pvec->pages[i];
1029 if (!radix_tree_exceptional_entry(page))
1030 pvec->pages[j++] = page;
1032 pvec->nr = j;
1036 * pagevec_lookup - gang pagecache lookup
1037 * @pvec: Where the resulting pages are placed
1038 * @mapping: The address_space to search
1039 * @start: The starting page index
1040 * @nr_pages: The maximum number of pages
1042 * pagevec_lookup() will search for and return a group of up to @nr_pages pages
1043 * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
1044 * reference against the pages in @pvec.
1046 * The search returns a group of mapping-contiguous pages with ascending
1047 * indexes. There may be holes in the indices due to not-present pages.
1049 * pagevec_lookup() returns the number of pages which were found.
1051 unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
1052 pgoff_t start, unsigned nr_pages)
1054 pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
1055 return pagevec_count(pvec);
1057 EXPORT_SYMBOL(pagevec_lookup);
1059 unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
1060 pgoff_t *index, int tag, unsigned nr_pages)
1062 pvec->nr = find_get_pages_tag(mapping, index, tag,
1063 nr_pages, pvec->pages);
1064 return pagevec_count(pvec);
1066 EXPORT_SYMBOL(pagevec_lookup_tag);
1069 * Perform any setup for the swap system
1071 void __init swap_setup(void)
1073 unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
1074 #ifdef CONFIG_SWAP
1075 int i;
1077 if (bdi_init(swapper_spaces[0].backing_dev_info))
1078 panic("Failed to init swap bdi");
1079 for (i = 0; i < MAX_SWAPFILES; i++) {
1080 spin_lock_init(&swapper_spaces[i].tree_lock);
1081 INIT_LIST_HEAD(&swapper_spaces[i].i_mmap_nonlinear);
1083 #endif
1085 /* Use a smaller cluster for small-memory machines */
1086 if (megs < 16)
1087 page_cluster = 2;
1088 else
1089 page_cluster = 3;
1091 * Right now other parts of the system means that we
1092 * _really_ don't want to cluster much more