]> git.karo-electronics.de Git - karo-tx-linux.git/blob - mm/swap.c
mm-move-lazy-free-pages-to-inactive-list-fix-fix
[karo-tx-linux.git] / mm / swap.c
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 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.
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/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/hugetlb.h>
35 #include <linux/page_idle.h>
36
37 #include "internal.h"
38
39 #define CREATE_TRACE_POINTS
40 #include <trace/events/pagemap.h>
41
42 /* How many pages do we try to swap or page in/out together? */
43 int page_cluster;
44
45 static DEFINE_PER_CPU(struct pagevec, lru_add_pvec);
46 static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
47 static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
48 static DEFINE_PER_CPU(struct pagevec, lru_deactivate_pvecs);
49
50 /*
51  * This path almost never happens for VM activity - pages are normally
52  * freed via pagevecs.  But it gets used by networking.
53  */
54 static void __page_cache_release(struct page *page)
55 {
56         if (PageLRU(page)) {
57                 struct zone *zone = page_zone(page);
58                 struct lruvec *lruvec;
59                 unsigned long flags;
60
61                 spin_lock_irqsave(&zone->lru_lock, flags);
62                 lruvec = mem_cgroup_page_lruvec(page, zone);
63                 VM_BUG_ON_PAGE(!PageLRU(page), page);
64                 __ClearPageLRU(page);
65                 del_page_from_lru_list(page, lruvec, page_off_lru(page));
66                 spin_unlock_irqrestore(&zone->lru_lock, flags);
67         }
68         mem_cgroup_uncharge(page);
69 }
70
71 static void __put_single_page(struct page *page)
72 {
73         __page_cache_release(page);
74         free_hot_cold_page(page, false);
75 }
76
77 static void __put_compound_page(struct page *page)
78 {
79         compound_page_dtor *dtor;
80
81         /*
82          * __page_cache_release() is supposed to be called for thp, not for
83          * hugetlb. This is because hugetlb page does never have PageLRU set
84          * (it's never listed to any LRU lists) and no memcg routines should
85          * be called for hugetlb (it has a separate hugetlb_cgroup.)
86          */
87         if (!PageHuge(page))
88                 __page_cache_release(page);
89         dtor = get_compound_page_dtor(page);
90         (*dtor)(page);
91 }
92
93 void __put_page(struct page *page)
94 {
95         if (unlikely(PageCompound(page)))
96                 __put_compound_page(page);
97         else
98                 __put_single_page(page);
99 }
100 EXPORT_SYMBOL(__put_page);
101
102 /**
103  * put_pages_list() - release a list of pages
104  * @pages: list of pages threaded on page->lru
105  *
106  * Release a list of pages which are strung together on page.lru.  Currently
107  * used by read_cache_pages() and related error recovery code.
108  */
109 void put_pages_list(struct list_head *pages)
110 {
111         while (!list_empty(pages)) {
112                 struct page *victim;
113
114                 victim = list_entry(pages->prev, struct page, lru);
115                 list_del(&victim->lru);
116                 page_cache_release(victim);
117         }
118 }
119 EXPORT_SYMBOL(put_pages_list);
120
121 /*
122  * get_kernel_pages() - pin kernel pages in memory
123  * @kiov:       An array of struct kvec structures
124  * @nr_segs:    number of segments to pin
125  * @write:      pinning for read/write, currently ignored
126  * @pages:      array that receives pointers to the pages pinned.
127  *              Should be at least nr_segs long.
128  *
129  * Returns number of pages pinned. This may be fewer than the number
130  * requested. If nr_pages is 0 or negative, returns 0. If no pages
131  * were pinned, returns -errno. Each page returned must be released
132  * with a put_page() call when it is finished with.
133  */
134 int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write,
135                 struct page **pages)
136 {
137         int seg;
138
139         for (seg = 0; seg < nr_segs; seg++) {
140                 if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE))
141                         return seg;
142
143                 pages[seg] = kmap_to_page(kiov[seg].iov_base);
144                 page_cache_get(pages[seg]);
145         }
146
147         return seg;
148 }
149 EXPORT_SYMBOL_GPL(get_kernel_pages);
150
151 /*
152  * get_kernel_page() - pin a kernel page in memory
153  * @start:      starting kernel address
154  * @write:      pinning for read/write, currently ignored
155  * @pages:      array that receives pointer to the page pinned.
156  *              Must be at least nr_segs long.
157  *
158  * Returns 1 if page is pinned. If the page was not pinned, returns
159  * -errno. The page returned must be released with a put_page() call
160  * when it is finished with.
161  */
162 int get_kernel_page(unsigned long start, int write, struct page **pages)
163 {
164         const struct kvec kiov = {
165                 .iov_base = (void *)start,
166                 .iov_len = PAGE_SIZE
167         };
168
169         return get_kernel_pages(&kiov, 1, write, pages);
170 }
171 EXPORT_SYMBOL_GPL(get_kernel_page);
172
173 static void pagevec_lru_move_fn(struct pagevec *pvec,
174         void (*move_fn)(struct page *page, struct lruvec *lruvec, void *arg),
175         void *arg)
176 {
177         int i;
178         struct zone *zone = NULL;
179         struct lruvec *lruvec;
180         unsigned long flags = 0;
181
182         for (i = 0; i < pagevec_count(pvec); i++) {
183                 struct page *page = pvec->pages[i];
184                 struct zone *pagezone = page_zone(page);
185
186                 if (pagezone != zone) {
187                         if (zone)
188                                 spin_unlock_irqrestore(&zone->lru_lock, flags);
189                         zone = pagezone;
190                         spin_lock_irqsave(&zone->lru_lock, flags);
191                 }
192
193                 lruvec = mem_cgroup_page_lruvec(page, zone);
194                 (*move_fn)(page, lruvec, arg);
195         }
196         if (zone)
197                 spin_unlock_irqrestore(&zone->lru_lock, flags);
198         release_pages(pvec->pages, pvec->nr, pvec->cold);
199         pagevec_reinit(pvec);
200 }
201
202 static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec,
203                                  void *arg)
204 {
205         int *pgmoved = arg;
206
207         if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
208                 enum lru_list lru = page_lru_base_type(page);
209                 list_move_tail(&page->lru, &lruvec->lists[lru]);
210                 (*pgmoved)++;
211         }
212 }
213
214 /*
215  * pagevec_move_tail() must be called with IRQ disabled.
216  * Otherwise this may cause nasty races.
217  */
218 static void pagevec_move_tail(struct pagevec *pvec)
219 {
220         int pgmoved = 0;
221
222         pagevec_lru_move_fn(pvec, pagevec_move_tail_fn, &pgmoved);
223         __count_vm_events(PGROTATED, pgmoved);
224 }
225
226 /*
227  * Writeback is about to end against a page which has been marked for immediate
228  * reclaim.  If it still appears to be reclaimable, move it to the tail of the
229  * inactive list.
230  */
231 void rotate_reclaimable_page(struct page *page)
232 {
233         if (!PageLocked(page) && !PageDirty(page) && !PageActive(page) &&
234             !PageUnevictable(page) && PageLRU(page)) {
235                 struct pagevec *pvec;
236                 unsigned long flags;
237
238                 page_cache_get(page);
239                 local_irq_save(flags);
240                 pvec = this_cpu_ptr(&lru_rotate_pvecs);
241                 if (!pagevec_add(pvec, page))
242                         pagevec_move_tail(pvec);
243                 local_irq_restore(flags);
244         }
245 }
246
247 static void update_page_reclaim_stat(struct lruvec *lruvec,
248                                      int file, int rotated)
249 {
250         struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
251
252         reclaim_stat->recent_scanned[file]++;
253         if (rotated)
254                 reclaim_stat->recent_rotated[file]++;
255 }
256
257 static void __activate_page(struct page *page, struct lruvec *lruvec,
258                             void *arg)
259 {
260         if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
261                 int file = page_is_file_cache(page);
262                 int lru = page_lru_base_type(page);
263
264                 del_page_from_lru_list(page, lruvec, lru);
265                 SetPageActive(page);
266                 lru += LRU_ACTIVE;
267                 add_page_to_lru_list(page, lruvec, lru);
268                 trace_mm_lru_activate(page);
269
270                 __count_vm_event(PGACTIVATE);
271                 update_page_reclaim_stat(lruvec, file, 1);
272         }
273 }
274
275 #ifdef CONFIG_SMP
276 static DEFINE_PER_CPU(struct pagevec, activate_page_pvecs);
277
278 static void activate_page_drain(int cpu)
279 {
280         struct pagevec *pvec = &per_cpu(activate_page_pvecs, cpu);
281
282         if (pagevec_count(pvec))
283                 pagevec_lru_move_fn(pvec, __activate_page, NULL);
284 }
285
286 static bool need_activate_page_drain(int cpu)
287 {
288         return pagevec_count(&per_cpu(activate_page_pvecs, cpu)) != 0;
289 }
290
291 void activate_page(struct page *page)
292 {
293         if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
294                 struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
295
296                 page_cache_get(page);
297                 if (!pagevec_add(pvec, page))
298                         pagevec_lru_move_fn(pvec, __activate_page, NULL);
299                 put_cpu_var(activate_page_pvecs);
300         }
301 }
302
303 #else
304 static inline void activate_page_drain(int cpu)
305 {
306 }
307
308 static bool need_activate_page_drain(int cpu)
309 {
310         return false;
311 }
312
313 void activate_page(struct page *page)
314 {
315         struct zone *zone = page_zone(page);
316
317         spin_lock_irq(&zone->lru_lock);
318         __activate_page(page, mem_cgroup_page_lruvec(page, zone), NULL);
319         spin_unlock_irq(&zone->lru_lock);
320 }
321 #endif
322
323 static void __lru_cache_activate_page(struct page *page)
324 {
325         struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
326         int i;
327
328         /*
329          * Search backwards on the optimistic assumption that the page being
330          * activated has just been added to this pagevec. Note that only
331          * the local pagevec is examined as a !PageLRU page could be in the
332          * process of being released, reclaimed, migrated or on a remote
333          * pagevec that is currently being drained. Furthermore, marking
334          * a remote pagevec's page PageActive potentially hits a race where
335          * a page is marked PageActive just after it is added to the inactive
336          * list causing accounting errors and BUG_ON checks to trigger.
337          */
338         for (i = pagevec_count(pvec) - 1; i >= 0; i--) {
339                 struct page *pagevec_page = pvec->pages[i];
340
341                 if (pagevec_page == page) {
342                         SetPageActive(page);
343                         break;
344                 }
345         }
346
347         put_cpu_var(lru_add_pvec);
348 }
349
350 /*
351  * Mark a page as having seen activity.
352  *
353  * inactive,unreferenced        ->      inactive,referenced
354  * inactive,referenced          ->      active,unreferenced
355  * active,unreferenced          ->      active,referenced
356  *
357  * When a newly allocated page is not yet visible, so safe for non-atomic ops,
358  * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
359  */
360 void mark_page_accessed(struct page *page)
361 {
362         page = compound_head(page);
363         if (!PageActive(page) && !PageUnevictable(page) &&
364                         PageReferenced(page)) {
365
366                 /*
367                  * If the page is on the LRU, queue it for activation via
368                  * activate_page_pvecs. Otherwise, assume the page is on a
369                  * pagevec, mark it active and it'll be moved to the active
370                  * LRU on the next drain.
371                  */
372                 if (PageLRU(page))
373                         activate_page(page);
374                 else
375                         __lru_cache_activate_page(page);
376                 ClearPageReferenced(page);
377                 if (page_is_file_cache(page))
378                         workingset_activation(page);
379         } else if (!PageReferenced(page)) {
380                 SetPageReferenced(page);
381         }
382         if (page_is_idle(page))
383                 clear_page_idle(page);
384 }
385 EXPORT_SYMBOL(mark_page_accessed);
386
387 static void __lru_cache_add(struct page *page)
388 {
389         struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
390
391         page_cache_get(page);
392         if (!pagevec_space(pvec))
393                 __pagevec_lru_add(pvec);
394         pagevec_add(pvec, page);
395         put_cpu_var(lru_add_pvec);
396 }
397
398 /**
399  * lru_cache_add: add a page to the page lists
400  * @page: the page to add
401  */
402 void lru_cache_add_anon(struct page *page)
403 {
404         if (PageActive(page))
405                 ClearPageActive(page);
406         __lru_cache_add(page);
407 }
408
409 void lru_cache_add_file(struct page *page)
410 {
411         if (PageActive(page))
412                 ClearPageActive(page);
413         __lru_cache_add(page);
414 }
415 EXPORT_SYMBOL(lru_cache_add_file);
416
417 /**
418  * lru_cache_add - add a page to a page list
419  * @page: the page to be added to the LRU.
420  *
421  * Queue the page for addition to the LRU via pagevec. The decision on whether
422  * to add the page to the [in]active [file|anon] list is deferred until the
423  * pagevec is drained. This gives a chance for the caller of lru_cache_add()
424  * have the page added to the active list using mark_page_accessed().
425  */
426 void lru_cache_add(struct page *page)
427 {
428         VM_BUG_ON_PAGE(PageActive(page) && PageUnevictable(page), page);
429         VM_BUG_ON_PAGE(PageLRU(page), page);
430         __lru_cache_add(page);
431 }
432
433 /**
434  * add_page_to_unevictable_list - add a page to the unevictable list
435  * @page:  the page to be added to the unevictable list
436  *
437  * Add page directly to its zone's unevictable list.  To avoid races with
438  * tasks that might be making the page evictable, through eg. munlock,
439  * munmap or exit, while it's not on the lru, we want to add the page
440  * while it's locked or otherwise "invisible" to other tasks.  This is
441  * difficult to do when using the pagevec cache, so bypass that.
442  */
443 void add_page_to_unevictable_list(struct page *page)
444 {
445         struct zone *zone = page_zone(page);
446         struct lruvec *lruvec;
447
448         spin_lock_irq(&zone->lru_lock);
449         lruvec = mem_cgroup_page_lruvec(page, zone);
450         ClearPageActive(page);
451         SetPageUnevictable(page);
452         SetPageLRU(page);
453         add_page_to_lru_list(page, lruvec, LRU_UNEVICTABLE);
454         spin_unlock_irq(&zone->lru_lock);
455 }
456
457 /**
458  * lru_cache_add_active_or_unevictable
459  * @page:  the page to be added to LRU
460  * @vma:   vma in which page is mapped for determining reclaimability
461  *
462  * Place @page on the active or unevictable LRU list, depending on its
463  * evictability.  Note that if the page is not evictable, it goes
464  * directly back onto it's zone's unevictable list, it does NOT use a
465  * per cpu pagevec.
466  */
467 void lru_cache_add_active_or_unevictable(struct page *page,
468                                          struct vm_area_struct *vma)
469 {
470         VM_BUG_ON_PAGE(PageLRU(page), page);
471
472         if (likely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED)) {
473                 SetPageActive(page);
474                 lru_cache_add(page);
475                 return;
476         }
477
478         if (!TestSetPageMlocked(page)) {
479                 /*
480                  * We use the irq-unsafe __mod_zone_page_stat because this
481                  * counter is not modified from interrupt context, and the pte
482                  * lock is held(spinlock), which implies preemption disabled.
483                  */
484                 __mod_zone_page_state(page_zone(page), NR_MLOCK,
485                                     hpage_nr_pages(page));
486                 count_vm_event(UNEVICTABLE_PGMLOCKED);
487         }
488         add_page_to_unevictable_list(page);
489 }
490
491 /*
492  * If the page can not be invalidated, it is moved to the
493  * inactive list to speed up its reclaim.  It is moved to the
494  * head of the list, rather than the tail, to give the flusher
495  * threads some time to write it out, as this is much more
496  * effective than the single-page writeout from reclaim.
497  *
498  * If the page isn't page_mapped and dirty/writeback, the page
499  * could reclaim asap using PG_reclaim.
500  *
501  * 1. active, mapped page -> none
502  * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
503  * 3. inactive, mapped page -> none
504  * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
505  * 5. inactive, clean -> inactive, tail
506  * 6. Others -> none
507  *
508  * In 4, why it moves inactive's head, the VM expects the page would
509  * be write it out by flusher threads as this is much more effective
510  * than the single-page writeout from reclaim.
511  */
512 static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec,
513                               void *arg)
514 {
515         int lru, file;
516         bool active;
517
518         if (!PageLRU(page))
519                 return;
520
521         if (PageUnevictable(page))
522                 return;
523
524         /* Some processes are using the page */
525         if (page_mapped(page))
526                 return;
527
528         active = PageActive(page);
529         file = page_is_file_cache(page);
530         lru = page_lru_base_type(page);
531
532         del_page_from_lru_list(page, lruvec, lru + active);
533         ClearPageActive(page);
534         ClearPageReferenced(page);
535         add_page_to_lru_list(page, lruvec, lru);
536
537         if (PageWriteback(page) || PageDirty(page)) {
538                 /*
539                  * PG_reclaim could be raced with end_page_writeback
540                  * It can make readahead confusing.  But race window
541                  * is _really_ small and  it's non-critical problem.
542                  */
543                 SetPageReclaim(page);
544         } else {
545                 /*
546                  * The page's writeback ends up during pagevec
547                  * We moves tha page into tail of inactive.
548                  */
549                 list_move_tail(&page->lru, &lruvec->lists[lru]);
550                 __count_vm_event(PGROTATED);
551         }
552
553         if (active)
554                 __count_vm_event(PGDEACTIVATE);
555         update_page_reclaim_stat(lruvec, file, 0);
556 }
557
558
559 static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec,
560                             void *arg)
561 {
562         if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) {
563                 int file = page_is_file_cache(page);
564                 int lru = page_lru_base_type(page);
565
566                 del_page_from_lru_list(page, lruvec, lru + LRU_ACTIVE);
567                 ClearPageActive(page);
568                 add_page_to_lru_list(page, lruvec, lru);
569
570                 __count_vm_event(PGDEACTIVATE);
571                 update_page_reclaim_stat(lruvec, file, 0);
572         }
573 }
574
575 /*
576  * Drain pages out of the cpu's pagevecs.
577  * Either "cpu" is the current CPU, and preemption has already been
578  * disabled; or "cpu" is being hot-unplugged, and is already dead.
579  */
580 void lru_add_drain_cpu(int cpu)
581 {
582         struct pagevec *pvec = &per_cpu(lru_add_pvec, cpu);
583
584         if (pagevec_count(pvec))
585                 __pagevec_lru_add(pvec);
586
587         pvec = &per_cpu(lru_rotate_pvecs, cpu);
588         if (pagevec_count(pvec)) {
589                 unsigned long flags;
590
591                 /* No harm done if a racing interrupt already did this */
592                 local_irq_save(flags);
593                 pagevec_move_tail(pvec);
594                 local_irq_restore(flags);
595         }
596
597         pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
598         if (pagevec_count(pvec))
599                 pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
600
601         pvec = &per_cpu(lru_deactivate_pvecs, cpu);
602         if (pagevec_count(pvec))
603                 pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
604
605         activate_page_drain(cpu);
606 }
607
608 /**
609  * deactivate_file_page - forcefully deactivate a file page
610  * @page: page to deactivate
611  *
612  * This function hints the VM that @page is a good reclaim candidate,
613  * for example if its invalidation fails due to the page being dirty
614  * or under writeback.
615  */
616 void deactivate_file_page(struct page *page)
617 {
618         /*
619          * In a workload with many unevictable page such as mprotect,
620          * unevictable page deactivation for accelerating reclaim is pointless.
621          */
622         if (PageUnevictable(page))
623                 return;
624
625         if (likely(get_page_unless_zero(page))) {
626                 struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
627
628                 if (!pagevec_add(pvec, page))
629                         pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
630                 put_cpu_var(lru_deactivate_file_pvecs);
631         }
632 }
633
634 /**
635  * deactivate_page - deactivate a page
636  * @page: page to deactivate
637  *
638  * deactivate_page() moves @page to the inactive list if @page was on the active
639  * list and was not an unevictable page.  This is done to accelerate the reclaim
640  * of @page.
641  */
642 void deactivate_page(struct page *page)
643 {
644         if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) {
645                 struct pagevec *pvec = &get_cpu_var(lru_deactivate_pvecs);
646
647                 page_cache_get(page);
648                 if (!pagevec_add(pvec, page))
649                         pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
650                 put_cpu_var(lru_deactivate_pvecs);
651         }
652 }
653
654 void lru_add_drain(void)
655 {
656         lru_add_drain_cpu(get_cpu());
657         put_cpu();
658 }
659
660 static void lru_add_drain_per_cpu(struct work_struct *dummy)
661 {
662         lru_add_drain();
663 }
664
665 static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
666
667 void lru_add_drain_all(void)
668 {
669         static DEFINE_MUTEX(lock);
670         static struct cpumask has_work;
671         int cpu;
672
673         mutex_lock(&lock);
674         get_online_cpus();
675         cpumask_clear(&has_work);
676
677         for_each_online_cpu(cpu) {
678                 struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
679
680                 if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
681                     pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
682                     pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
683                     pagevec_count(&per_cpu(lru_deactivate_pvecs, cpu)) ||
684                     need_activate_page_drain(cpu)) {
685                         INIT_WORK(work, lru_add_drain_per_cpu);
686                         schedule_work_on(cpu, work);
687                         cpumask_set_cpu(cpu, &has_work);
688                 }
689         }
690
691         for_each_cpu(cpu, &has_work)
692                 flush_work(&per_cpu(lru_add_drain_work, cpu));
693
694         put_online_cpus();
695         mutex_unlock(&lock);
696 }
697
698 /**
699  * release_pages - batched page_cache_release()
700  * @pages: array of pages to release
701  * @nr: number of pages
702  * @cold: whether the pages are cache cold
703  *
704  * Decrement the reference count on all the pages in @pages.  If it
705  * fell to zero, remove the page from the LRU and free it.
706  */
707 void release_pages(struct page **pages, int nr, bool cold)
708 {
709         int i;
710         LIST_HEAD(pages_to_free);
711         struct zone *zone = NULL;
712         struct lruvec *lruvec;
713         unsigned long uninitialized_var(flags);
714         unsigned int uninitialized_var(lock_batch);
715
716         for (i = 0; i < nr; i++) {
717                 struct page *page = pages[i];
718
719                 /*
720                  * Make sure the IRQ-safe lock-holding time does not get
721                  * excessive with a continuous string of pages from the
722                  * same zone. The lock is held only if zone != NULL.
723                  */
724                 if (zone && ++lock_batch == SWAP_CLUSTER_MAX) {
725                         spin_unlock_irqrestore(&zone->lru_lock, flags);
726                         zone = NULL;
727                 }
728
729                 page = compound_head(page);
730                 if (!put_page_testzero(page))
731                         continue;
732
733                 if (PageCompound(page)) {
734                         if (zone) {
735                                 spin_unlock_irqrestore(&zone->lru_lock, flags);
736                                 zone = NULL;
737                         }
738                         __put_compound_page(page);
739                         continue;
740                 }
741
742                 if (PageLRU(page)) {
743                         struct zone *pagezone = page_zone(page);
744
745                         if (pagezone != zone) {
746                                 if (zone)
747                                         spin_unlock_irqrestore(&zone->lru_lock,
748                                                                         flags);
749                                 lock_batch = 0;
750                                 zone = pagezone;
751                                 spin_lock_irqsave(&zone->lru_lock, flags);
752                         }
753
754                         lruvec = mem_cgroup_page_lruvec(page, zone);
755                         VM_BUG_ON_PAGE(!PageLRU(page), page);
756                         __ClearPageLRU(page);
757                         del_page_from_lru_list(page, lruvec, page_off_lru(page));
758                 }
759
760                 /* Clear Active bit in case of parallel mark_page_accessed */
761                 __ClearPageActive(page);
762
763                 list_add(&page->lru, &pages_to_free);
764         }
765         if (zone)
766                 spin_unlock_irqrestore(&zone->lru_lock, flags);
767
768         mem_cgroup_uncharge_list(&pages_to_free);
769         free_hot_cold_page_list(&pages_to_free, cold);
770 }
771 EXPORT_SYMBOL(release_pages);
772
773 /*
774  * The pages which we're about to release may be in the deferred lru-addition
775  * queues.  That would prevent them from really being freed right now.  That's
776  * OK from a correctness point of view but is inefficient - those pages may be
777  * cache-warm and we want to give them back to the page allocator ASAP.
778  *
779  * So __pagevec_release() will drain those queues here.  __pagevec_lru_add()
780  * and __pagevec_lru_add_active() call release_pages() directly to avoid
781  * mutual recursion.
782  */
783 void __pagevec_release(struct pagevec *pvec)
784 {
785         lru_add_drain();
786         release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
787         pagevec_reinit(pvec);
788 }
789 EXPORT_SYMBOL(__pagevec_release);
790
791 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
792 /* used by __split_huge_page_refcount() */
793 void lru_add_page_tail(struct page *page, struct page *page_tail,
794                        struct lruvec *lruvec, struct list_head *list)
795 {
796         const int file = 0;
797
798         VM_BUG_ON_PAGE(!PageHead(page), page);
799         VM_BUG_ON_PAGE(PageCompound(page_tail), page);
800         VM_BUG_ON_PAGE(PageLRU(page_tail), page);
801         VM_BUG_ON(NR_CPUS != 1 &&
802                   !spin_is_locked(&lruvec_zone(lruvec)->lru_lock));
803
804         if (!list)
805                 SetPageLRU(page_tail);
806
807         if (likely(PageLRU(page)))
808                 list_add_tail(&page_tail->lru, &page->lru);
809         else if (list) {
810                 /* page reclaim is reclaiming a huge page */
811                 get_page(page_tail);
812                 list_add_tail(&page_tail->lru, list);
813         } else {
814                 struct list_head *list_head;
815                 /*
816                  * Head page has not yet been counted, as an hpage,
817                  * so we must account for each subpage individually.
818                  *
819                  * Use the standard add function to put page_tail on the list,
820                  * but then correct its position so they all end up in order.
821                  */
822                 add_page_to_lru_list(page_tail, lruvec, page_lru(page_tail));
823                 list_head = page_tail->lru.prev;
824                 list_move_tail(&page_tail->lru, list_head);
825         }
826
827         if (!PageUnevictable(page))
828                 update_page_reclaim_stat(lruvec, file, PageActive(page_tail));
829 }
830 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
831
832 static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec,
833                                  void *arg)
834 {
835         int file = page_is_file_cache(page);
836         int active = PageActive(page);
837         enum lru_list lru = page_lru(page);
838
839         VM_BUG_ON_PAGE(PageLRU(page), page);
840
841         SetPageLRU(page);
842         add_page_to_lru_list(page, lruvec, lru);
843         update_page_reclaim_stat(lruvec, file, active);
844         trace_mm_lru_insertion(page, lru);
845 }
846
847 /*
848  * Add the passed pages to the LRU, then drop the caller's refcount
849  * on them.  Reinitialises the caller's pagevec.
850  */
851 void __pagevec_lru_add(struct pagevec *pvec)
852 {
853         pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL);
854 }
855 EXPORT_SYMBOL(__pagevec_lru_add);
856
857 /**
858  * pagevec_lookup_entries - gang pagecache lookup
859  * @pvec:       Where the resulting entries are placed
860  * @mapping:    The address_space to search
861  * @start:      The starting entry index
862  * @nr_entries: The maximum number of entries
863  * @indices:    The cache indices corresponding to the entries in @pvec
864  *
865  * pagevec_lookup_entries() will search for and return a group of up
866  * to @nr_entries pages and shadow entries in the mapping.  All
867  * entries are placed in @pvec.  pagevec_lookup_entries() takes a
868  * reference against actual pages in @pvec.
869  *
870  * The search returns a group of mapping-contiguous entries with
871  * ascending indexes.  There may be holes in the indices due to
872  * not-present entries.
873  *
874  * pagevec_lookup_entries() returns the number of entries which were
875  * found.
876  */
877 unsigned pagevec_lookup_entries(struct pagevec *pvec,
878                                 struct address_space *mapping,
879                                 pgoff_t start, unsigned nr_pages,
880                                 pgoff_t *indices)
881 {
882         pvec->nr = find_get_entries(mapping, start, nr_pages,
883                                     pvec->pages, indices);
884         return pagevec_count(pvec);
885 }
886
887 /**
888  * pagevec_remove_exceptionals - pagevec exceptionals pruning
889  * @pvec:       The pagevec to prune
890  *
891  * pagevec_lookup_entries() fills both pages and exceptional radix
892  * tree entries into the pagevec.  This function prunes all
893  * exceptionals from @pvec without leaving holes, so that it can be
894  * passed on to page-only pagevec operations.
895  */
896 void pagevec_remove_exceptionals(struct pagevec *pvec)
897 {
898         int i, j;
899
900         for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
901                 struct page *page = pvec->pages[i];
902                 if (!radix_tree_exceptional_entry(page))
903                         pvec->pages[j++] = page;
904         }
905         pvec->nr = j;
906 }
907
908 /**
909  * pagevec_lookup - gang pagecache lookup
910  * @pvec:       Where the resulting pages are placed
911  * @mapping:    The address_space to search
912  * @start:      The starting page index
913  * @nr_pages:   The maximum number of pages
914  *
915  * pagevec_lookup() will search for and return a group of up to @nr_pages pages
916  * in the mapping.  The pages are placed in @pvec.  pagevec_lookup() takes a
917  * reference against the pages in @pvec.
918  *
919  * The search returns a group of mapping-contiguous pages with ascending
920  * indexes.  There may be holes in the indices due to not-present pages.
921  *
922  * pagevec_lookup() returns the number of pages which were found.
923  */
924 unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
925                 pgoff_t start, unsigned nr_pages)
926 {
927         pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
928         return pagevec_count(pvec);
929 }
930 EXPORT_SYMBOL(pagevec_lookup);
931
932 unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
933                 pgoff_t *index, int tag, unsigned nr_pages)
934 {
935         pvec->nr = find_get_pages_tag(mapping, index, tag,
936                                         nr_pages, pvec->pages);
937         return pagevec_count(pvec);
938 }
939 EXPORT_SYMBOL(pagevec_lookup_tag);
940
941 /*
942  * Perform any setup for the swap system
943  */
944 void __init swap_setup(void)
945 {
946         unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
947 #ifdef CONFIG_SWAP
948         int i;
949
950         for (i = 0; i < MAX_SWAPFILES; i++)
951                 spin_lock_init(&swapper_spaces[i].tree_lock);
952 #endif
953
954         /* Use a smaller cluster for small-memory machines */
955         if (megs < 16)
956                 page_cluster = 2;
957         else
958                 page_cluster = 3;
959         /*
960          * Right now other parts of the system means that we
961          * _really_ don't want to cluster much more
962          */
963 }