~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/mm/swap.c

Version: ~ [ linux-5.9-rc6 ] ~ [ linux-5.8.10 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.66 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.146 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.198 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.236 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.236 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp