4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
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.
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/export.h>
25 #include <linux/mm_inline.h>
26 #include <linux/percpu_counter.h>
27 #include <linux/memremap.h>
28 #include <linux/percpu.h>
29 #include <linux/cpu.h>
30 #include <linux/notifier.h>
31 #include <linux/backing-dev.h>
32 #include <linux/memremap.h>
33 #include <linux/memcontrol.h>
34 #include <linux/gfp.h>
35 #include <linux/uio.h>
36 #include <linux/hugetlb.h>
37 #include <linux/page_idle.h>
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/pagemap.h>
44 /* How many pages do we try to swap or page in/out together? */
47 static DEFINE_PER_CPU(struct pagevec, lru_add_pvec);
48 static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
49 static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
50 static DEFINE_PER_CPU(struct pagevec, lru_lazyfree_pvecs);
52 static DEFINE_PER_CPU(struct pagevec, activate_page_pvecs);
56 * This path almost never happens for VM activity - pages are normally
57 * freed via pagevecs. But it gets used by networking.
59 static void __page_cache_release(struct page *page)
62 struct zone *zone = page_zone(page);
63 struct lruvec *lruvec;
66 spin_lock_irqsave(zone_lru_lock(zone), flags);
67 lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat);
68 VM_BUG_ON_PAGE(!PageLRU(page), page);
70 del_page_from_lru_list(page, lruvec, page_off_lru(page));
71 spin_unlock_irqrestore(zone_lru_lock(zone), flags);
73 __ClearPageWaiters(page);
74 mem_cgroup_uncharge(page);
77 static void __put_single_page(struct page *page)
79 __page_cache_release(page);
80 free_unref_page(page);
83 static void __put_compound_page(struct page *page)
85 compound_page_dtor *dtor;
88 * __page_cache_release() is supposed to be called for thp, not for
89 * hugetlb. This is because hugetlb page does never have PageLRU set
90 * (it's never listed to any LRU lists) and no memcg routines should
91 * be called for hugetlb (it has a separate hugetlb_cgroup.)
94 __page_cache_release(page);
95 dtor = get_compound_page_dtor(page);
99 void __put_page(struct page *page)
101 if (is_zone_device_page(page)) {
102 put_dev_pagemap(page->pgmap);
105 * The page belongs to the device that created pgmap. Do
106 * not return it to page allocator.
111 if (unlikely(PageCompound(page)))
112 __put_compound_page(page);
114 __put_single_page(page);
116 EXPORT_SYMBOL(__put_page);
119 * put_pages_list() - release a list of pages
120 * @pages: list of pages threaded on page->lru
122 * Release a list of pages which are strung together on page.lru. Currently
123 * used by read_cache_pages() and related error recovery code.
125 void put_pages_list(struct list_head *pages)
127 while (!list_empty(pages)) {
130 victim = list_entry(pages->prev, struct page, lru);
131 list_del(&victim->lru);
135 EXPORT_SYMBOL(put_pages_list);
138 * get_kernel_pages() - pin kernel pages in memory
139 * @kiov: An array of struct kvec structures
140 * @nr_segs: number of segments to pin
141 * @write: pinning for read/write, currently ignored
142 * @pages: array that receives pointers to the pages pinned.
143 * Should be at least nr_segs long.
145 * Returns number of pages pinned. This may be fewer than the number
146 * requested. If nr_pages is 0 or negative, returns 0. If no pages
147 * were pinned, returns -errno. Each page returned must be released
148 * with a put_page() call when it is finished with.
150 int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write,
155 for (seg = 0; seg < nr_segs; seg++) {
156 if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE))
159 pages[seg] = kmap_to_page(kiov[seg].iov_base);
160 get_page(pages[seg]);
165 EXPORT_SYMBOL_GPL(get_kernel_pages);
168 * get_kernel_page() - pin a kernel page in memory
169 * @start: starting kernel address
170 * @write: pinning for read/write, currently ignored
171 * @pages: array that receives pointer to the page pinned.
172 * Must be at least nr_segs long.
174 * Returns 1 if page is pinned. If the page was not pinned, returns
175 * -errno. The page returned must be released with a put_page() call
176 * when it is finished with.
178 int get_kernel_page(unsigned long start, int write, struct page **pages)
180 const struct kvec kiov = {
181 .iov_base = (void *)start,
185 return get_kernel_pages(&kiov, 1, write, pages);
187 EXPORT_SYMBOL_GPL(get_kernel_page);
189 static void pagevec_lru_move_fn(struct pagevec *pvec,
190 void (*move_fn)(struct page *page, struct lruvec *lruvec, void *arg),
194 struct pglist_data *pgdat = NULL;
195 struct lruvec *lruvec;
196 unsigned long flags = 0;
198 for (i = 0; i < pagevec_count(pvec); i++) {
199 struct page *page = pvec->pages[i];
200 struct pglist_data *pagepgdat = page_pgdat(page);
202 if (pagepgdat != pgdat) {
204 spin_unlock_irqrestore(&pgdat->lru_lock, flags);
206 spin_lock_irqsave(&pgdat->lru_lock, flags);
209 lruvec = mem_cgroup_page_lruvec(page, pgdat);
210 (*move_fn)(page, lruvec, arg);
213 spin_unlock_irqrestore(&pgdat->lru_lock, flags);
214 release_pages(pvec->pages, pvec->nr);
215 pagevec_reinit(pvec);
218 static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec,
223 if (PageLRU(page) && !PageUnevictable(page)) {
224 del_page_from_lru_list(page, lruvec, page_lru(page));
225 ClearPageActive(page);
226 add_page_to_lru_list_tail(page, lruvec, page_lru(page));
232 * pagevec_move_tail() must be called with IRQ disabled.
233 * Otherwise this may cause nasty races.
235 static void pagevec_move_tail(struct pagevec *pvec)
239 pagevec_lru_move_fn(pvec, pagevec_move_tail_fn, &pgmoved);
240 __count_vm_events(PGROTATED, pgmoved);
244 * Writeback is about to end against a page which has been marked for immediate
245 * reclaim. If it still appears to be reclaimable, move it to the tail of the
248 void rotate_reclaimable_page(struct page *page)
250 if (!PageLocked(page) && !PageDirty(page) &&
251 !PageUnevictable(page) && PageLRU(page)) {
252 struct pagevec *pvec;
256 local_irq_save(flags);
257 pvec = this_cpu_ptr(&lru_rotate_pvecs);
258 if (!pagevec_add(pvec, page) || PageCompound(page))
259 pagevec_move_tail(pvec);
260 local_irq_restore(flags);
264 static void update_page_reclaim_stat(struct lruvec *lruvec,
265 int file, int rotated)
267 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
269 reclaim_stat->recent_scanned[file]++;
271 reclaim_stat->recent_rotated[file]++;
274 static void __activate_page(struct page *page, struct lruvec *lruvec,
277 if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
278 int file = page_is_file_cache(page);
279 int lru = page_lru_base_type(page);
281 del_page_from_lru_list(page, lruvec, lru);
284 add_page_to_lru_list(page, lruvec, lru);
285 trace_mm_lru_activate(page);
287 __count_vm_event(PGACTIVATE);
288 update_page_reclaim_stat(lruvec, file, 1);
293 static void activate_page_drain(int cpu)
295 struct pagevec *pvec = &per_cpu(activate_page_pvecs, cpu);
297 if (pagevec_count(pvec))
298 pagevec_lru_move_fn(pvec, __activate_page, NULL);
301 static bool need_activate_page_drain(int cpu)
303 return pagevec_count(&per_cpu(activate_page_pvecs, cpu)) != 0;
306 void activate_page(struct page *page)
308 page = compound_head(page);
309 if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
310 struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
313 if (!pagevec_add(pvec, page) || PageCompound(page))
314 pagevec_lru_move_fn(pvec, __activate_page, NULL);
315 put_cpu_var(activate_page_pvecs);
320 static inline void activate_page_drain(int cpu)
324 void activate_page(struct page *page)
326 struct zone *zone = page_zone(page);
328 page = compound_head(page);
329 spin_lock_irq(zone_lru_lock(zone));
330 __activate_page(page, mem_cgroup_page_lruvec(page, zone->zone_pgdat), NULL);
331 spin_unlock_irq(zone_lru_lock(zone));
335 static void __lru_cache_activate_page(struct page *page)
337 struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
341 * Search backwards on the optimistic assumption that the page being
342 * activated has just been added to this pagevec. Note that only
343 * the local pagevec is examined as a !PageLRU page could be in the
344 * process of being released, reclaimed, migrated or on a remote
345 * pagevec that is currently being drained. Furthermore, marking
346 * a remote pagevec's page PageActive potentially hits a race where
347 * a page is marked PageActive just after it is added to the inactive
348 * list causing accounting errors and BUG_ON checks to trigger.
350 for (i = pagevec_count(pvec) - 1; i >= 0; i--) {
351 struct page *pagevec_page = pvec->pages[i];
353 if (pagevec_page == page) {
359 put_cpu_var(lru_add_pvec);
363 * Mark a page as having seen activity.
365 * inactive,unreferenced -> inactive,referenced
366 * inactive,referenced -> active,unreferenced
367 * active,unreferenced -> active,referenced
369 * When a newly allocated page is not yet visible, so safe for non-atomic ops,
370 * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
372 void mark_page_accessed(struct page *page)
374 page = compound_head(page);
375 if (!PageActive(page) && !PageUnevictable(page) &&
376 PageReferenced(page)) {
379 * If the page is on the LRU, queue it for activation via
380 * activate_page_pvecs. Otherwise, assume the page is on a
381 * pagevec, mark it active and it'll be moved to the active
382 * LRU on the next drain.
387 __lru_cache_activate_page(page);
388 ClearPageReferenced(page);
389 if (page_is_file_cache(page))
390 workingset_activation(page);
391 } else if (!PageReferenced(page)) {
392 SetPageReferenced(page);
394 if (page_is_idle(page))
395 clear_page_idle(page);
397 EXPORT_SYMBOL(mark_page_accessed);
399 static void __lru_cache_add(struct page *page)
401 struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
404 if (!pagevec_add(pvec, page) || PageCompound(page))
405 __pagevec_lru_add(pvec);
406 put_cpu_var(lru_add_pvec);
410 * lru_cache_add_anon - add a page to the page lists
411 * @page: the page to add
413 void lru_cache_add_anon(struct page *page)
415 if (PageActive(page))
416 ClearPageActive(page);
417 __lru_cache_add(page);
420 void lru_cache_add_file(struct page *page)
422 if (PageActive(page))
423 ClearPageActive(page);
424 __lru_cache_add(page);
426 EXPORT_SYMBOL(lru_cache_add_file);
429 * lru_cache_add - add a page to a page list
430 * @page: the page to be added to the LRU.
432 * Queue the page for addition to the LRU via pagevec. The decision on whether
433 * to add the page to the [in]active [file|anon] list is deferred until the
434 * pagevec is drained. This gives a chance for the caller of lru_cache_add()
435 * have the page added to the active list using mark_page_accessed().
437 void lru_cache_add(struct page *page)
439 VM_BUG_ON_PAGE(PageActive(page) && PageUnevictable(page), page);
440 VM_BUG_ON_PAGE(PageLRU(page), page);
441 __lru_cache_add(page);
445 * lru_cache_add_active_or_unevictable
446 * @page: the page to be added to LRU
447 * @vma: vma in which page is mapped for determining reclaimability
449 * Place @page on the active or unevictable LRU list, depending on its
450 * evictability. Note that if the page is not evictable, it goes
451 * directly back onto it's zone's unevictable list, it does NOT use a
454 void lru_cache_add_active_or_unevictable(struct page *page,
455 struct vm_area_struct *vma)
457 VM_BUG_ON_PAGE(PageLRU(page), page);
459 if (likely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED))
461 else if (!TestSetPageMlocked(page)) {
463 * We use the irq-unsafe __mod_zone_page_stat because this
464 * counter is not modified from interrupt context, and the pte
465 * lock is held(spinlock), which implies preemption disabled.
467 __mod_zone_page_state(page_zone(page), NR_MLOCK,
468 hpage_nr_pages(page));
469 count_vm_event(UNEVICTABLE_PGMLOCKED);
475 * If the page can not be invalidated, it is moved to the
476 * inactive list to speed up its reclaim. It is moved to the
477 * head of the list, rather than the tail, to give the flusher
478 * threads some time to write it out, as this is much more
479 * effective than the single-page writeout from reclaim.
481 * If the page isn't page_mapped and dirty/writeback, the page
482 * could reclaim asap using PG_reclaim.
484 * 1. active, mapped page -> none
485 * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
486 * 3. inactive, mapped page -> none
487 * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
488 * 5. inactive, clean -> inactive, tail
491 * In 4, why it moves inactive's head, the VM expects the page would
492 * be write it out by flusher threads as this is much more effective
493 * than the single-page writeout from reclaim.
495 static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec,
504 if (PageUnevictable(page))
507 /* Some processes are using the page */
508 if (page_mapped(page))
511 active = PageActive(page);
512 file = page_is_file_cache(page);
513 lru = page_lru_base_type(page);
515 del_page_from_lru_list(page, lruvec, lru + active);
516 ClearPageActive(page);
517 ClearPageReferenced(page);
518 add_page_to_lru_list(page, lruvec, lru);
520 if (PageWriteback(page) || PageDirty(page)) {
522 * PG_reclaim could be raced with end_page_writeback
523 * It can make readahead confusing. But race window
524 * is _really_ small and it's non-critical problem.
526 SetPageReclaim(page);
529 * The page's writeback ends up during pagevec
530 * We moves tha page into tail of inactive.
532 list_move_tail(&page->lru, &lruvec->lists[lru]);
533 __count_vm_event(PGROTATED);
537 __count_vm_event(PGDEACTIVATE);
538 update_page_reclaim_stat(lruvec, file, 0);
542 static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec,
545 if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
546 !PageSwapCache(page) && !PageUnevictable(page)) {
547 bool active = PageActive(page);
549 del_page_from_lru_list(page, lruvec,
550 LRU_INACTIVE_ANON + active);
551 ClearPageActive(page);
552 ClearPageReferenced(page);
554 * lazyfree pages are clean anonymous pages. They have
555 * SwapBacked flag cleared to distinguish normal anonymous
558 ClearPageSwapBacked(page);
559 add_page_to_lru_list(page, lruvec, LRU_INACTIVE_FILE);
561 __count_vm_events(PGLAZYFREE, hpage_nr_pages(page));
562 count_memcg_page_event(page, PGLAZYFREE);
563 update_page_reclaim_stat(lruvec, 1, 0);
568 * Drain pages out of the cpu's pagevecs.
569 * Either "cpu" is the current CPU, and preemption has already been
570 * disabled; or "cpu" is being hot-unplugged, and is already dead.
572 void lru_add_drain_cpu(int cpu)
574 struct pagevec *pvec = &per_cpu(lru_add_pvec, cpu);
576 if (pagevec_count(pvec))
577 __pagevec_lru_add(pvec);
579 pvec = &per_cpu(lru_rotate_pvecs, cpu);
580 if (pagevec_count(pvec)) {
583 /* No harm done if a racing interrupt already did this */
584 local_irq_save(flags);
585 pagevec_move_tail(pvec);
586 local_irq_restore(flags);
589 pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
590 if (pagevec_count(pvec))
591 pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
593 pvec = &per_cpu(lru_lazyfree_pvecs, cpu);
594 if (pagevec_count(pvec))
595 pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL);
597 activate_page_drain(cpu);
601 * deactivate_file_page - forcefully deactivate a file page
602 * @page: page to deactivate
604 * This function hints the VM that @page is a good reclaim candidate,
605 * for example if its invalidation fails due to the page being dirty
606 * or under writeback.
608 void deactivate_file_page(struct page *page)
611 * In a workload with many unevictable page such as mprotect,
612 * unevictable page deactivation for accelerating reclaim is pointless.
614 if (PageUnevictable(page))
617 if (likely(get_page_unless_zero(page))) {
618 struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
620 if (!pagevec_add(pvec, page) || PageCompound(page))
621 pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
622 put_cpu_var(lru_deactivate_file_pvecs);
627 * mark_page_lazyfree - make an anon page lazyfree
628 * @page: page to deactivate
630 * mark_page_lazyfree() moves @page to the inactive file list.
631 * This is done to accelerate the reclaim of @page.
633 void mark_page_lazyfree(struct page *page)
635 if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
636 !PageSwapCache(page) && !PageUnevictable(page)) {
637 struct pagevec *pvec = &get_cpu_var(lru_lazyfree_pvecs);
640 if (!pagevec_add(pvec, page) || PageCompound(page))
641 pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL);
642 put_cpu_var(lru_lazyfree_pvecs);
646 void lru_add_drain(void)
648 lru_add_drain_cpu(get_cpu());
654 static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
656 static void lru_add_drain_per_cpu(struct work_struct *dummy)
662 * Doesn't need any cpu hotplug locking because we do rely on per-cpu
663 * kworkers being shut down before our page_alloc_cpu_dead callback is
664 * executed on the offlined cpu.
665 * Calling this function with cpu hotplug locks held can actually lead
666 * to obscure indirect dependencies via WQ context.
668 void lru_add_drain_all(void)
670 static DEFINE_MUTEX(lock);
671 static struct cpumask has_work;
675 * Make sure nobody triggers this path before mm_percpu_wq is fully
678 if (WARN_ON(!mm_percpu_wq))
682 cpumask_clear(&has_work);
684 for_each_online_cpu(cpu) {
685 struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
687 if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
688 pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
689 pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
690 pagevec_count(&per_cpu(lru_lazyfree_pvecs, cpu)) ||
691 need_activate_page_drain(cpu)) {
692 INIT_WORK(work, lru_add_drain_per_cpu);
693 queue_work_on(cpu, mm_percpu_wq, work);
694 cpumask_set_cpu(cpu, &has_work);
698 for_each_cpu(cpu, &has_work)
699 flush_work(&per_cpu(lru_add_drain_work, cpu));
704 void lru_add_drain_all(void)
711 * release_pages - batched put_page()
712 * @pages: array of pages to release
713 * @nr: number of pages
715 * Decrement the reference count on all the pages in @pages. If it
716 * fell to zero, remove the page from the LRU and free it.
718 void release_pages(struct page **pages, int nr)
721 LIST_HEAD(pages_to_free);
722 struct pglist_data *locked_pgdat = NULL;
723 struct lruvec *lruvec;
724 unsigned long uninitialized_var(flags);
725 unsigned int uninitialized_var(lock_batch);
727 for (i = 0; i < nr; i++) {
728 struct page *page = pages[i];
731 * Make sure the IRQ-safe lock-holding time does not get
732 * excessive with a continuous string of pages from the
733 * same pgdat. The lock is held only if pgdat != NULL.
735 if (locked_pgdat && ++lock_batch == SWAP_CLUSTER_MAX) {
736 spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
740 if (is_huge_zero_page(page))
743 if (is_zone_device_page(page)) {
745 spin_unlock_irqrestore(&locked_pgdat->lru_lock,
750 * ZONE_DEVICE pages that return 'false' from
751 * put_devmap_managed_page() do not require special
752 * processing, and instead, expect a call to
753 * put_page_testzero().
755 if (put_devmap_managed_page(page))
759 page = compound_head(page);
760 if (!put_page_testzero(page))
763 if (PageCompound(page)) {
765 spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
768 __put_compound_page(page);
773 struct pglist_data *pgdat = page_pgdat(page);
775 if (pgdat != locked_pgdat) {
777 spin_unlock_irqrestore(&locked_pgdat->lru_lock,
780 locked_pgdat = pgdat;
781 spin_lock_irqsave(&locked_pgdat->lru_lock, flags);
784 lruvec = mem_cgroup_page_lruvec(page, locked_pgdat);
785 VM_BUG_ON_PAGE(!PageLRU(page), page);
786 __ClearPageLRU(page);
787 del_page_from_lru_list(page, lruvec, page_off_lru(page));
790 /* Clear Active bit in case of parallel mark_page_accessed */
791 __ClearPageActive(page);
792 __ClearPageWaiters(page);
794 list_add(&page->lru, &pages_to_free);
797 spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
799 mem_cgroup_uncharge_list(&pages_to_free);
800 free_unref_page_list(&pages_to_free);
802 EXPORT_SYMBOL(release_pages);
805 * The pages which we're about to release may be in the deferred lru-addition
806 * queues. That would prevent them from really being freed right now. That's
807 * OK from a correctness point of view but is inefficient - those pages may be
808 * cache-warm and we want to give them back to the page allocator ASAP.
810 * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
811 * and __pagevec_lru_add_active() call release_pages() directly to avoid
814 void __pagevec_release(struct pagevec *pvec)
816 if (!pvec->percpu_pvec_drained) {
818 pvec->percpu_pvec_drained = true;
820 release_pages(pvec->pages, pagevec_count(pvec));
821 pagevec_reinit(pvec);
823 EXPORT_SYMBOL(__pagevec_release);
825 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
826 /* used by __split_huge_page_refcount() */
827 void lru_add_page_tail(struct page *page, struct page *page_tail,
828 struct lruvec *lruvec, struct list_head *list)
832 VM_BUG_ON_PAGE(!PageHead(page), page);
833 VM_BUG_ON_PAGE(PageCompound(page_tail), page);
834 VM_BUG_ON_PAGE(PageLRU(page_tail), page);
835 VM_BUG_ON(NR_CPUS != 1 &&
836 !spin_is_locked(&lruvec_pgdat(lruvec)->lru_lock));
839 SetPageLRU(page_tail);
841 if (likely(PageLRU(page)))
842 list_add_tail(&page_tail->lru, &page->lru);
844 /* page reclaim is reclaiming a huge page */
846 list_add_tail(&page_tail->lru, list);
848 struct list_head *list_head;
850 * Head page has not yet been counted, as an hpage,
851 * so we must account for each subpage individually.
853 * Use the standard add function to put page_tail on the list,
854 * but then correct its position so they all end up in order.
856 add_page_to_lru_list(page_tail, lruvec, page_lru(page_tail));
857 list_head = page_tail->lru.prev;
858 list_move_tail(&page_tail->lru, list_head);
861 if (!PageUnevictable(page))
862 update_page_reclaim_stat(lruvec, file, PageActive(page_tail));
864 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
866 static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec,
870 int was_unevictable = TestClearPageUnevictable(page);
872 VM_BUG_ON_PAGE(PageLRU(page), page);
876 * Page becomes evictable in two ways:
877 * 1) Within LRU lock [munlock_vma_pages() and __munlock_pagevec()].
878 * 2) Before acquiring LRU lock to put the page to correct LRU and then
879 * a) do PageLRU check with lock [check_move_unevictable_pages]
880 * b) do PageLRU check before lock [clear_page_mlock]
882 * (1) & (2a) are ok as LRU lock will serialize them. For (2b), we need
883 * following strict ordering:
885 * #0: __pagevec_lru_add_fn #1: clear_page_mlock
887 * SetPageLRU() TestClearPageMlocked()
888 * smp_mb() // explicit ordering // above provides strict
890 * PageMlocked() PageLRU()
893 * if '#1' does not observe setting of PG_lru by '#0' and fails
894 * isolation, the explicit barrier will make sure that page_evictable
895 * check will put the page in correct LRU. Without smp_mb(), SetPageLRU
896 * can be reordered after PageMlocked check and can make '#1' to fail
897 * the isolation of the page whose Mlocked bit is cleared (#0 is also
898 * looking at the same page) and the evictable page will be stranded
899 * in an unevictable LRU.
903 if (page_evictable(page)) {
904 lru = page_lru(page);
905 update_page_reclaim_stat(lruvec, page_is_file_cache(page),
908 count_vm_event(UNEVICTABLE_PGRESCUED);
910 lru = LRU_UNEVICTABLE;
911 ClearPageActive(page);
912 SetPageUnevictable(page);
913 if (!was_unevictable)
914 count_vm_event(UNEVICTABLE_PGCULLED);
917 add_page_to_lru_list(page, lruvec, lru);
918 trace_mm_lru_insertion(page, lru);
922 * Add the passed pages to the LRU, then drop the caller's refcount
923 * on them. Reinitialises the caller's pagevec.
925 void __pagevec_lru_add(struct pagevec *pvec)
927 pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL);
929 EXPORT_SYMBOL(__pagevec_lru_add);
932 * pagevec_lookup_entries - gang pagecache lookup
933 * @pvec: Where the resulting entries are placed
934 * @mapping: The address_space to search
935 * @start: The starting entry index
936 * @nr_entries: The maximum number of pages
937 * @indices: The cache indices corresponding to the entries in @pvec
939 * pagevec_lookup_entries() will search for and return a group of up
940 * to @nr_pages pages and shadow entries in the mapping. All
941 * entries are placed in @pvec. pagevec_lookup_entries() takes a
942 * reference against actual pages in @pvec.
944 * The search returns a group of mapping-contiguous entries with
945 * ascending indexes. There may be holes in the indices due to
946 * not-present entries.
948 * pagevec_lookup_entries() returns the number of entries which were
951 unsigned pagevec_lookup_entries(struct pagevec *pvec,
952 struct address_space *mapping,
953 pgoff_t start, unsigned nr_entries,
956 pvec->nr = find_get_entries(mapping, start, nr_entries,
957 pvec->pages, indices);
958 return pagevec_count(pvec);
962 * pagevec_remove_exceptionals - pagevec exceptionals pruning
963 * @pvec: The pagevec to prune
965 * pagevec_lookup_entries() fills both pages and exceptional radix
966 * tree entries into the pagevec. This function prunes all
967 * exceptionals from @pvec without leaving holes, so that it can be
968 * passed on to page-only pagevec operations.
970 void pagevec_remove_exceptionals(struct pagevec *pvec)
974 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
975 struct page *page = pvec->pages[i];
976 if (!radix_tree_exceptional_entry(page))
977 pvec->pages[j++] = page;
983 * pagevec_lookup_range - gang pagecache lookup
984 * @pvec: Where the resulting pages are placed
985 * @mapping: The address_space to search
986 * @start: The starting page index
987 * @end: The final page index
989 * pagevec_lookup_range() will search for & return a group of up to PAGEVEC_SIZE
990 * pages in the mapping starting from index @start and upto index @end
991 * (inclusive). The pages are placed in @pvec. pagevec_lookup() takes a
992 * reference against the pages in @pvec.
994 * The search returns a group of mapping-contiguous pages with ascending
995 * indexes. There may be holes in the indices due to not-present pages. We
996 * also update @start to index the next page for the traversal.
998 * pagevec_lookup_range() returns the number of pages which were found. If this
999 * number is smaller than PAGEVEC_SIZE, the end of specified range has been
1002 unsigned pagevec_lookup_range(struct pagevec *pvec,
1003 struct address_space *mapping, pgoff_t *start, pgoff_t end)
1005 pvec->nr = find_get_pages_range(mapping, start, end, PAGEVEC_SIZE,
1007 return pagevec_count(pvec);
1009 EXPORT_SYMBOL(pagevec_lookup_range);
1011 unsigned pagevec_lookup_range_tag(struct pagevec *pvec,
1012 struct address_space *mapping, pgoff_t *index, pgoff_t end,
1015 pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
1016 PAGEVEC_SIZE, pvec->pages);
1017 return pagevec_count(pvec);
1019 EXPORT_SYMBOL(pagevec_lookup_range_tag);
1021 unsigned pagevec_lookup_range_nr_tag(struct pagevec *pvec,
1022 struct address_space *mapping, pgoff_t *index, pgoff_t end,
1023 int tag, unsigned max_pages)
1025 pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
1026 min_t(unsigned int, max_pages, PAGEVEC_SIZE), pvec->pages);
1027 return pagevec_count(pvec);
1029 EXPORT_SYMBOL(pagevec_lookup_range_nr_tag);
1031 * Perform any setup for the swap system
1033 void __init swap_setup(void)
1035 unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
1037 /* Use a smaller cluster for small-memory machines */
1043 * Right now other parts of the system means that we
1044 * _really_ don't want to cluster much more