1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Swap reorganised 29.12.95, Stephen Tweedie. 61da177e4SLinus Torvalds * kswapd added: 7.1.96 sct 71da177e4SLinus Torvalds * Removed kswapd_ctl limits, and swap out as many pages as needed 81da177e4SLinus Torvalds * to bring the system back to freepages.high: 2.4.97, Rik van Riel. 91da177e4SLinus Torvalds * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com). 101da177e4SLinus Torvalds * Multiqueue VM started 5.8.00, Rik van Riel. 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 13b1de0d13SMitchel Humpherys #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14b1de0d13SMitchel Humpherys 151da177e4SLinus Torvalds #include <linux/mm.h> 165b3cc15aSIngo Molnar #include <linux/sched/mm.h> 171da177e4SLinus Torvalds #include <linux/module.h> 185a0e3ad6STejun Heo #include <linux/gfp.h> 191da177e4SLinus Torvalds #include <linux/kernel_stat.h> 201da177e4SLinus Torvalds #include <linux/swap.h> 211da177e4SLinus Torvalds #include <linux/pagemap.h> 221da177e4SLinus Torvalds #include <linux/init.h> 231da177e4SLinus Torvalds #include <linux/highmem.h> 2470ddf637SAnton Vorontsov #include <linux/vmpressure.h> 25e129b5c2SAndrew Morton #include <linux/vmstat.h> 261da177e4SLinus Torvalds #include <linux/file.h> 271da177e4SLinus Torvalds #include <linux/writeback.h> 281da177e4SLinus Torvalds #include <linux/blkdev.h> 291da177e4SLinus Torvalds #include <linux/buffer_head.h> /* for try_to_release_page(), 301da177e4SLinus Torvalds buffer_heads_over_limit */ 311da177e4SLinus Torvalds #include <linux/mm_inline.h> 321da177e4SLinus Torvalds #include <linux/backing-dev.h> 331da177e4SLinus Torvalds #include <linux/rmap.h> 341da177e4SLinus Torvalds #include <linux/topology.h> 351da177e4SLinus Torvalds #include <linux/cpu.h> 361da177e4SLinus Torvalds #include <linux/cpuset.h> 373e7d3449SMel Gorman #include <linux/compaction.h> 381da177e4SLinus Torvalds #include <linux/notifier.h> 391da177e4SLinus Torvalds #include <linux/rwsem.h> 40248a0301SRafael J. Wysocki #include <linux/delay.h> 413218ae14SYasunori Goto #include <linux/kthread.h> 427dfb7103SNigel Cunningham #include <linux/freezer.h> 4366e1707bSBalbir Singh #include <linux/memcontrol.h> 44873b4771SKeika Kobayashi #include <linux/delayacct.h> 45af936a16SLee Schermerhorn #include <linux/sysctl.h> 46929bea7cSKOSAKI Motohiro #include <linux/oom.h> 4764e3d12fSKuo-Hsin Yang #include <linux/pagevec.h> 48268bb0ceSLinus Torvalds #include <linux/prefetch.h> 49b1de0d13SMitchel Humpherys #include <linux/printk.h> 50f9fe48beSRoss Zwisler #include <linux/dax.h> 51eb414681SJohannes Weiner #include <linux/psi.h> 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds #include <asm/tlbflush.h> 541da177e4SLinus Torvalds #include <asm/div64.h> 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds #include <linux/swapops.h> 57117aad1eSRafael Aquini #include <linux/balloon_compaction.h> 581da177e4SLinus Torvalds 590f8053a5SNick Piggin #include "internal.h" 600f8053a5SNick Piggin 6133906bc5SMel Gorman #define CREATE_TRACE_POINTS 6233906bc5SMel Gorman #include <trace/events/vmscan.h> 6333906bc5SMel Gorman 641da177e4SLinus Torvalds struct scan_control { 6522fba335SKOSAKI Motohiro /* How many pages shrink_list() should reclaim */ 6622fba335SKOSAKI Motohiro unsigned long nr_to_reclaim; 6722fba335SKOSAKI Motohiro 68ee814fe2SJohannes Weiner /* 69ee814fe2SJohannes Weiner * Nodemask of nodes allowed by the caller. If NULL, all nodes 70ee814fe2SJohannes Weiner * are scanned. 71ee814fe2SJohannes Weiner */ 72ee814fe2SJohannes Weiner nodemask_t *nodemask; 739e3b2f8cSKonstantin Khlebnikov 745f53e762SKOSAKI Motohiro /* 75f16015fbSJohannes Weiner * The memory cgroup that hit its limit and as a result is the 76f16015fbSJohannes Weiner * primary target of this reclaim invocation. 77f16015fbSJohannes Weiner */ 78f16015fbSJohannes Weiner struct mem_cgroup *target_mem_cgroup; 7966e1707bSBalbir Singh 807cf111bcSJohannes Weiner /* 817cf111bcSJohannes Weiner * Scan pressure balancing between anon and file LRUs 827cf111bcSJohannes Weiner */ 837cf111bcSJohannes Weiner unsigned long anon_cost; 847cf111bcSJohannes Weiner unsigned long file_cost; 857cf111bcSJohannes Weiner 86b91ac374SJohannes Weiner /* Can active pages be deactivated as part of reclaim? */ 87b91ac374SJohannes Weiner #define DEACTIVATE_ANON 1 88b91ac374SJohannes Weiner #define DEACTIVATE_FILE 2 89b91ac374SJohannes Weiner unsigned int may_deactivate:2; 90b91ac374SJohannes Weiner unsigned int force_deactivate:1; 91b91ac374SJohannes Weiner unsigned int skipped_deactivate:1; 92b91ac374SJohannes Weiner 931276ad68SJohannes Weiner /* Writepage batching in laptop mode; RECLAIM_WRITE */ 94ee814fe2SJohannes Weiner unsigned int may_writepage:1; 95ee814fe2SJohannes Weiner 96ee814fe2SJohannes Weiner /* Can mapped pages be reclaimed? */ 97ee814fe2SJohannes Weiner unsigned int may_unmap:1; 98ee814fe2SJohannes Weiner 99ee814fe2SJohannes Weiner /* Can pages be swapped as part of reclaim? */ 100ee814fe2SJohannes Weiner unsigned int may_swap:1; 101ee814fe2SJohannes Weiner 102d6622f63SYisheng Xie /* 103d6622f63SYisheng Xie * Cgroups are not reclaimed below their configured memory.low, 104d6622f63SYisheng Xie * unless we threaten to OOM. If any cgroups are skipped due to 105d6622f63SYisheng Xie * memory.low and nothing was reclaimed, go back for memory.low. 106d6622f63SYisheng Xie */ 107d6622f63SYisheng Xie unsigned int memcg_low_reclaim:1; 108d6622f63SYisheng Xie unsigned int memcg_low_skipped:1; 109241994edSJohannes Weiner 110ee814fe2SJohannes Weiner unsigned int hibernation_mode:1; 111ee814fe2SJohannes Weiner 112ee814fe2SJohannes Weiner /* One of the zones is ready for compaction */ 113ee814fe2SJohannes Weiner unsigned int compaction_ready:1; 114ee814fe2SJohannes Weiner 115b91ac374SJohannes Weiner /* There is easily reclaimable cold cache in the current node */ 116b91ac374SJohannes Weiner unsigned int cache_trim_mode:1; 117b91ac374SJohannes Weiner 11853138ceaSJohannes Weiner /* The file pages on the current node are dangerously low */ 11953138ceaSJohannes Weiner unsigned int file_is_tiny:1; 12053138ceaSJohannes Weiner 121bb451fdfSGreg Thelen /* Allocation order */ 122bb451fdfSGreg Thelen s8 order; 123bb451fdfSGreg Thelen 124bb451fdfSGreg Thelen /* Scan (total_size >> priority) pages at once */ 125bb451fdfSGreg Thelen s8 priority; 126bb451fdfSGreg Thelen 127bb451fdfSGreg Thelen /* The highest zone to isolate pages for reclaim from */ 128bb451fdfSGreg Thelen s8 reclaim_idx; 129bb451fdfSGreg Thelen 130bb451fdfSGreg Thelen /* This context's GFP mask */ 131bb451fdfSGreg Thelen gfp_t gfp_mask; 132bb451fdfSGreg Thelen 133ee814fe2SJohannes Weiner /* Incremented by the number of inactive pages that were scanned */ 134ee814fe2SJohannes Weiner unsigned long nr_scanned; 135ee814fe2SJohannes Weiner 136ee814fe2SJohannes Weiner /* Number of pages freed so far during a call to shrink_zones() */ 137ee814fe2SJohannes Weiner unsigned long nr_reclaimed; 138d108c772SAndrey Ryabinin 139d108c772SAndrey Ryabinin struct { 140d108c772SAndrey Ryabinin unsigned int dirty; 141d108c772SAndrey Ryabinin unsigned int unqueued_dirty; 142d108c772SAndrey Ryabinin unsigned int congested; 143d108c772SAndrey Ryabinin unsigned int writeback; 144d108c772SAndrey Ryabinin unsigned int immediate; 145d108c772SAndrey Ryabinin unsigned int file_taken; 146d108c772SAndrey Ryabinin unsigned int taken; 147d108c772SAndrey Ryabinin } nr; 148e5ca8071SYafang Shao 149e5ca8071SYafang Shao /* for recording the reclaimed slab by now */ 150e5ca8071SYafang Shao struct reclaim_state reclaim_state; 1511da177e4SLinus Torvalds }; 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1541da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1551da177e4SLinus Torvalds do { \ 1561da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1571da177e4SLinus Torvalds struct page *prev; \ 1581da177e4SLinus Torvalds \ 1591da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1601da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1611da177e4SLinus Torvalds } \ 1621da177e4SLinus Torvalds } while (0) 1631da177e4SLinus Torvalds #else 1641da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1651da177e4SLinus Torvalds #endif 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds /* 168c843966cSJohannes Weiner * From 0 .. 200. Higher means more swappy. 1691da177e4SLinus Torvalds */ 1701da177e4SLinus Torvalds int vm_swappiness = 60; 1711da177e4SLinus Torvalds 1720a432dcbSYang Shi static void set_task_reclaim_state(struct task_struct *task, 1730a432dcbSYang Shi struct reclaim_state *rs) 1740a432dcbSYang Shi { 1750a432dcbSYang Shi /* Check for an overwrite */ 1760a432dcbSYang Shi WARN_ON_ONCE(rs && task->reclaim_state); 1770a432dcbSYang Shi 1780a432dcbSYang Shi /* Check for the nulling of an already-nulled member */ 1790a432dcbSYang Shi WARN_ON_ONCE(!rs && !task->reclaim_state); 1800a432dcbSYang Shi 1810a432dcbSYang Shi task->reclaim_state = rs; 1820a432dcbSYang Shi } 1830a432dcbSYang Shi 1841da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1851da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1861da177e4SLinus Torvalds 1870a432dcbSYang Shi #ifdef CONFIG_MEMCG 188a2fb1261SYang Shi static int shrinker_nr_max; 1892bfd3637SYang Shi 190*3c6f17e6SYang Shi /* The shrinker_info is expanded in a batch of BITS_PER_LONG */ 191a2fb1261SYang Shi static inline int shrinker_map_size(int nr_items) 192a2fb1261SYang Shi { 193a2fb1261SYang Shi return (DIV_ROUND_UP(nr_items, BITS_PER_LONG) * sizeof(unsigned long)); 194a2fb1261SYang Shi } 1952bfd3637SYang Shi 196*3c6f17e6SYang Shi static inline int shrinker_defer_size(int nr_items) 197*3c6f17e6SYang Shi { 198*3c6f17e6SYang Shi return (round_up(nr_items, BITS_PER_LONG) * sizeof(atomic_long_t)); 199*3c6f17e6SYang Shi } 200*3c6f17e6SYang Shi 201468ab843SYang Shi static struct shrinker_info *shrinker_info_protected(struct mem_cgroup *memcg, 202468ab843SYang Shi int nid) 203468ab843SYang Shi { 204468ab843SYang Shi return rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_info, 205468ab843SYang Shi lockdep_is_held(&shrinker_rwsem)); 206468ab843SYang Shi } 207468ab843SYang Shi 208e4262c4fSYang Shi static int expand_one_shrinker_info(struct mem_cgroup *memcg, 209*3c6f17e6SYang Shi int map_size, int defer_size, 210*3c6f17e6SYang Shi int old_map_size, int old_defer_size) 2112bfd3637SYang Shi { 212e4262c4fSYang Shi struct shrinker_info *new, *old; 2132bfd3637SYang Shi struct mem_cgroup_per_node *pn; 2142bfd3637SYang Shi int nid; 215*3c6f17e6SYang Shi int size = map_size + defer_size; 2162bfd3637SYang Shi 2172bfd3637SYang Shi for_each_node(nid) { 2182bfd3637SYang Shi pn = memcg->nodeinfo[nid]; 219468ab843SYang Shi old = shrinker_info_protected(memcg, nid); 2202bfd3637SYang Shi /* Not yet online memcg */ 2212bfd3637SYang Shi if (!old) 2222bfd3637SYang Shi return 0; 2232bfd3637SYang Shi 2242bfd3637SYang Shi new = kvmalloc_node(sizeof(*new) + size, GFP_KERNEL, nid); 2252bfd3637SYang Shi if (!new) 2262bfd3637SYang Shi return -ENOMEM; 2272bfd3637SYang Shi 228*3c6f17e6SYang Shi new->nr_deferred = (atomic_long_t *)(new + 1); 229*3c6f17e6SYang Shi new->map = (void *)new->nr_deferred + defer_size; 230*3c6f17e6SYang Shi 231*3c6f17e6SYang Shi /* map: set all old bits, clear all new bits */ 232*3c6f17e6SYang Shi memset(new->map, (int)0xff, old_map_size); 233*3c6f17e6SYang Shi memset((void *)new->map + old_map_size, 0, map_size - old_map_size); 234*3c6f17e6SYang Shi /* nr_deferred: copy old values, clear all new values */ 235*3c6f17e6SYang Shi memcpy(new->nr_deferred, old->nr_deferred, old_defer_size); 236*3c6f17e6SYang Shi memset((void *)new->nr_deferred + old_defer_size, 0, 237*3c6f17e6SYang Shi defer_size - old_defer_size); 2382bfd3637SYang Shi 239e4262c4fSYang Shi rcu_assign_pointer(pn->shrinker_info, new); 24072673e86SYang Shi kvfree_rcu(old, rcu); 2412bfd3637SYang Shi } 2422bfd3637SYang Shi 2432bfd3637SYang Shi return 0; 2442bfd3637SYang Shi } 2452bfd3637SYang Shi 246e4262c4fSYang Shi void free_shrinker_info(struct mem_cgroup *memcg) 2472bfd3637SYang Shi { 2482bfd3637SYang Shi struct mem_cgroup_per_node *pn; 249e4262c4fSYang Shi struct shrinker_info *info; 2502bfd3637SYang Shi int nid; 2512bfd3637SYang Shi 2522bfd3637SYang Shi for_each_node(nid) { 2532bfd3637SYang Shi pn = memcg->nodeinfo[nid]; 254e4262c4fSYang Shi info = rcu_dereference_protected(pn->shrinker_info, true); 255e4262c4fSYang Shi kvfree(info); 256e4262c4fSYang Shi rcu_assign_pointer(pn->shrinker_info, NULL); 2572bfd3637SYang Shi } 2582bfd3637SYang Shi } 2592bfd3637SYang Shi 260e4262c4fSYang Shi int alloc_shrinker_info(struct mem_cgroup *memcg) 2612bfd3637SYang Shi { 262e4262c4fSYang Shi struct shrinker_info *info; 2632bfd3637SYang Shi int nid, size, ret = 0; 264*3c6f17e6SYang Shi int map_size, defer_size = 0; 2652bfd3637SYang Shi 266d27cf2aaSYang Shi down_write(&shrinker_rwsem); 267*3c6f17e6SYang Shi map_size = shrinker_map_size(shrinker_nr_max); 268*3c6f17e6SYang Shi defer_size = shrinker_defer_size(shrinker_nr_max); 269*3c6f17e6SYang Shi size = map_size + defer_size; 2702bfd3637SYang Shi for_each_node(nid) { 271e4262c4fSYang Shi info = kvzalloc_node(sizeof(*info) + size, GFP_KERNEL, nid); 272e4262c4fSYang Shi if (!info) { 273e4262c4fSYang Shi free_shrinker_info(memcg); 2742bfd3637SYang Shi ret = -ENOMEM; 2752bfd3637SYang Shi break; 2762bfd3637SYang Shi } 277*3c6f17e6SYang Shi info->nr_deferred = (atomic_long_t *)(info + 1); 278*3c6f17e6SYang Shi info->map = (void *)info->nr_deferred + defer_size; 279e4262c4fSYang Shi rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, info); 2802bfd3637SYang Shi } 281d27cf2aaSYang Shi up_write(&shrinker_rwsem); 2822bfd3637SYang Shi 2832bfd3637SYang Shi return ret; 2842bfd3637SYang Shi } 2852bfd3637SYang Shi 286*3c6f17e6SYang Shi static inline bool need_expand(int nr_max) 287*3c6f17e6SYang Shi { 288*3c6f17e6SYang Shi return round_up(nr_max, BITS_PER_LONG) > 289*3c6f17e6SYang Shi round_up(shrinker_nr_max, BITS_PER_LONG); 290*3c6f17e6SYang Shi } 291*3c6f17e6SYang Shi 292e4262c4fSYang Shi static int expand_shrinker_info(int new_id) 2932bfd3637SYang Shi { 294*3c6f17e6SYang Shi int ret = 0; 295a2fb1261SYang Shi int new_nr_max = new_id + 1; 296*3c6f17e6SYang Shi int map_size, defer_size = 0; 297*3c6f17e6SYang Shi int old_map_size, old_defer_size = 0; 2982bfd3637SYang Shi struct mem_cgroup *memcg; 2992bfd3637SYang Shi 300*3c6f17e6SYang Shi if (!need_expand(new_nr_max)) 301a2fb1261SYang Shi goto out; 3022bfd3637SYang Shi 3032bfd3637SYang Shi if (!root_mem_cgroup) 304d27cf2aaSYang Shi goto out; 305d27cf2aaSYang Shi 306d27cf2aaSYang Shi lockdep_assert_held(&shrinker_rwsem); 3072bfd3637SYang Shi 308*3c6f17e6SYang Shi map_size = shrinker_map_size(new_nr_max); 309*3c6f17e6SYang Shi defer_size = shrinker_defer_size(new_nr_max); 310*3c6f17e6SYang Shi old_map_size = shrinker_map_size(shrinker_nr_max); 311*3c6f17e6SYang Shi old_defer_size = shrinker_defer_size(shrinker_nr_max); 312*3c6f17e6SYang Shi 3132bfd3637SYang Shi memcg = mem_cgroup_iter(NULL, NULL, NULL); 3142bfd3637SYang Shi do { 315*3c6f17e6SYang Shi ret = expand_one_shrinker_info(memcg, map_size, defer_size, 316*3c6f17e6SYang Shi old_map_size, old_defer_size); 3172bfd3637SYang Shi if (ret) { 3182bfd3637SYang Shi mem_cgroup_iter_break(NULL, memcg); 319d27cf2aaSYang Shi goto out; 3202bfd3637SYang Shi } 3212bfd3637SYang Shi } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 322d27cf2aaSYang Shi out: 3232bfd3637SYang Shi if (!ret) 324a2fb1261SYang Shi shrinker_nr_max = new_nr_max; 325d27cf2aaSYang Shi 3262bfd3637SYang Shi return ret; 3272bfd3637SYang Shi } 3282bfd3637SYang Shi 3292bfd3637SYang Shi void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id) 3302bfd3637SYang Shi { 3312bfd3637SYang Shi if (shrinker_id >= 0 && memcg && !mem_cgroup_is_root(memcg)) { 332e4262c4fSYang Shi struct shrinker_info *info; 3332bfd3637SYang Shi 3342bfd3637SYang Shi rcu_read_lock(); 335e4262c4fSYang Shi info = rcu_dereference(memcg->nodeinfo[nid]->shrinker_info); 3362bfd3637SYang Shi /* Pairs with smp mb in shrink_slab() */ 3372bfd3637SYang Shi smp_mb__before_atomic(); 338e4262c4fSYang Shi set_bit(shrinker_id, info->map); 3392bfd3637SYang Shi rcu_read_unlock(); 3402bfd3637SYang Shi } 3412bfd3637SYang Shi } 3422bfd3637SYang Shi 343b4c2b231SKirill Tkhai static DEFINE_IDR(shrinker_idr); 344b4c2b231SKirill Tkhai 345b4c2b231SKirill Tkhai static int prealloc_memcg_shrinker(struct shrinker *shrinker) 346b4c2b231SKirill Tkhai { 347b4c2b231SKirill Tkhai int id, ret = -ENOMEM; 348b4c2b231SKirill Tkhai 349b4c2b231SKirill Tkhai down_write(&shrinker_rwsem); 350b4c2b231SKirill Tkhai /* This may call shrinker, so it must use down_read_trylock() */ 35141ca668aSYang Shi id = idr_alloc(&shrinker_idr, shrinker, 0, 0, GFP_KERNEL); 352b4c2b231SKirill Tkhai if (id < 0) 353b4c2b231SKirill Tkhai goto unlock; 354b4c2b231SKirill Tkhai 3550a4465d3SKirill Tkhai if (id >= shrinker_nr_max) { 356e4262c4fSYang Shi if (expand_shrinker_info(id)) { 3570a4465d3SKirill Tkhai idr_remove(&shrinker_idr, id); 3580a4465d3SKirill Tkhai goto unlock; 3590a4465d3SKirill Tkhai } 3600a4465d3SKirill Tkhai } 361b4c2b231SKirill Tkhai shrinker->id = id; 362b4c2b231SKirill Tkhai ret = 0; 363b4c2b231SKirill Tkhai unlock: 364b4c2b231SKirill Tkhai up_write(&shrinker_rwsem); 365b4c2b231SKirill Tkhai return ret; 366b4c2b231SKirill Tkhai } 367b4c2b231SKirill Tkhai 368b4c2b231SKirill Tkhai static void unregister_memcg_shrinker(struct shrinker *shrinker) 369b4c2b231SKirill Tkhai { 370b4c2b231SKirill Tkhai int id = shrinker->id; 371b4c2b231SKirill Tkhai 372b4c2b231SKirill Tkhai BUG_ON(id < 0); 373b4c2b231SKirill Tkhai 37441ca668aSYang Shi lockdep_assert_held(&shrinker_rwsem); 37541ca668aSYang Shi 376b4c2b231SKirill Tkhai idr_remove(&shrinker_idr, id); 377b4c2b231SKirill Tkhai } 378b4c2b231SKirill Tkhai 379b5ead35eSJohannes Weiner static bool cgroup_reclaim(struct scan_control *sc) 38089b5fae5SJohannes Weiner { 381b5ead35eSJohannes Weiner return sc->target_mem_cgroup; 38289b5fae5SJohannes Weiner } 38397c9341fSTejun Heo 38497c9341fSTejun Heo /** 385b5ead35eSJohannes Weiner * writeback_throttling_sane - is the usual dirty throttling mechanism available? 38697c9341fSTejun Heo * @sc: scan_control in question 38797c9341fSTejun Heo * 38897c9341fSTejun Heo * The normal page dirty throttling mechanism in balance_dirty_pages() is 38997c9341fSTejun Heo * completely broken with the legacy memcg and direct stalling in 39097c9341fSTejun Heo * shrink_page_list() is used for throttling instead, which lacks all the 39197c9341fSTejun Heo * niceties such as fairness, adaptive pausing, bandwidth proportional 39297c9341fSTejun Heo * allocation and configurability. 39397c9341fSTejun Heo * 39497c9341fSTejun Heo * This function tests whether the vmscan currently in progress can assume 39597c9341fSTejun Heo * that the normal dirty throttling mechanism is operational. 39697c9341fSTejun Heo */ 397b5ead35eSJohannes Weiner static bool writeback_throttling_sane(struct scan_control *sc) 39897c9341fSTejun Heo { 399b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 40097c9341fSTejun Heo return true; 40197c9341fSTejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 40269234aceSLinus Torvalds if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) 40397c9341fSTejun Heo return true; 40497c9341fSTejun Heo #endif 40597c9341fSTejun Heo return false; 40697c9341fSTejun Heo } 40791a45470SKAMEZAWA Hiroyuki #else 4080a432dcbSYang Shi static int prealloc_memcg_shrinker(struct shrinker *shrinker) 4090a432dcbSYang Shi { 4100a432dcbSYang Shi return 0; 4110a432dcbSYang Shi } 4120a432dcbSYang Shi 4130a432dcbSYang Shi static void unregister_memcg_shrinker(struct shrinker *shrinker) 4140a432dcbSYang Shi { 4150a432dcbSYang Shi } 4160a432dcbSYang Shi 417b5ead35eSJohannes Weiner static bool cgroup_reclaim(struct scan_control *sc) 41889b5fae5SJohannes Weiner { 419b5ead35eSJohannes Weiner return false; 42089b5fae5SJohannes Weiner } 42197c9341fSTejun Heo 422b5ead35eSJohannes Weiner static bool writeback_throttling_sane(struct scan_control *sc) 42397c9341fSTejun Heo { 42497c9341fSTejun Heo return true; 42597c9341fSTejun Heo } 42691a45470SKAMEZAWA Hiroyuki #endif 42791a45470SKAMEZAWA Hiroyuki 4285a1c84b4SMel Gorman /* 4295a1c84b4SMel Gorman * This misses isolated pages which are not accounted for to save counters. 4305a1c84b4SMel Gorman * As the data only determines if reclaim or compaction continues, it is 4315a1c84b4SMel Gorman * not expected that isolated pages will be a dominating factor. 4325a1c84b4SMel Gorman */ 4335a1c84b4SMel Gorman unsigned long zone_reclaimable_pages(struct zone *zone) 4345a1c84b4SMel Gorman { 4355a1c84b4SMel Gorman unsigned long nr; 4365a1c84b4SMel Gorman 4375a1c84b4SMel Gorman nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) + 4385a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE); 4395a1c84b4SMel Gorman if (get_nr_swap_pages() > 0) 4405a1c84b4SMel Gorman nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) + 4415a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON); 4425a1c84b4SMel Gorman 4435a1c84b4SMel Gorman return nr; 4445a1c84b4SMel Gorman } 4455a1c84b4SMel Gorman 446fd538803SMichal Hocko /** 447fd538803SMichal Hocko * lruvec_lru_size - Returns the number of pages on the given LRU list. 448fd538803SMichal Hocko * @lruvec: lru vector 449fd538803SMichal Hocko * @lru: lru to use 450fd538803SMichal Hocko * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list) 451fd538803SMichal Hocko */ 4522091339dSYu Zhao static unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, 4532091339dSYu Zhao int zone_idx) 454c9f299d9SKOSAKI Motohiro { 455de3b0150SJohannes Weiner unsigned long size = 0; 456fd538803SMichal Hocko int zid; 457a3d8e054SKOSAKI Motohiro 458de3b0150SJohannes Weiner for (zid = 0; zid <= zone_idx && zid < MAX_NR_ZONES; zid++) { 459fd538803SMichal Hocko struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; 460fd538803SMichal Hocko 461fd538803SMichal Hocko if (!managed_zone(zone)) 462fd538803SMichal Hocko continue; 463fd538803SMichal Hocko 464fd538803SMichal Hocko if (!mem_cgroup_disabled()) 465de3b0150SJohannes Weiner size += mem_cgroup_get_zone_lru_size(lruvec, lru, zid); 466fd538803SMichal Hocko else 467de3b0150SJohannes Weiner size += zone_page_state(zone, NR_ZONE_LRU_BASE + lru); 468c9f299d9SKOSAKI Motohiro } 469de3b0150SJohannes Weiner return size; 470b4536f0cSMichal Hocko } 471b4536f0cSMichal Hocko 4721da177e4SLinus Torvalds /* 4731d3d4437SGlauber Costa * Add a shrinker callback to be called from the vm. 4741da177e4SLinus Torvalds */ 4758e04944fSTetsuo Handa int prealloc_shrinker(struct shrinker *shrinker) 4761da177e4SLinus Torvalds { 477b9726c26SAlexey Dobriyan unsigned int size = sizeof(*shrinker->nr_deferred); 4781d3d4437SGlauber Costa 4791d3d4437SGlauber Costa if (shrinker->flags & SHRINKER_NUMA_AWARE) 4801d3d4437SGlauber Costa size *= nr_node_ids; 4811d3d4437SGlauber Costa 4821d3d4437SGlauber Costa shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); 4831d3d4437SGlauber Costa if (!shrinker->nr_deferred) 4841d3d4437SGlauber Costa return -ENOMEM; 485b4c2b231SKirill Tkhai 486b4c2b231SKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) { 487b4c2b231SKirill Tkhai if (prealloc_memcg_shrinker(shrinker)) 488b4c2b231SKirill Tkhai goto free_deferred; 489b4c2b231SKirill Tkhai } 490b4c2b231SKirill Tkhai 4918e04944fSTetsuo Handa return 0; 492b4c2b231SKirill Tkhai 493b4c2b231SKirill Tkhai free_deferred: 494b4c2b231SKirill Tkhai kfree(shrinker->nr_deferred); 495b4c2b231SKirill Tkhai shrinker->nr_deferred = NULL; 496b4c2b231SKirill Tkhai return -ENOMEM; 4978e04944fSTetsuo Handa } 4981d3d4437SGlauber Costa 4998e04944fSTetsuo Handa void free_prealloced_shrinker(struct shrinker *shrinker) 5008e04944fSTetsuo Handa { 501b4c2b231SKirill Tkhai if (!shrinker->nr_deferred) 502b4c2b231SKirill Tkhai return; 503b4c2b231SKirill Tkhai 50441ca668aSYang Shi if (shrinker->flags & SHRINKER_MEMCG_AWARE) { 50541ca668aSYang Shi down_write(&shrinker_rwsem); 506b4c2b231SKirill Tkhai unregister_memcg_shrinker(shrinker); 50741ca668aSYang Shi up_write(&shrinker_rwsem); 50841ca668aSYang Shi } 509b4c2b231SKirill Tkhai 5108e04944fSTetsuo Handa kfree(shrinker->nr_deferred); 5118e04944fSTetsuo Handa shrinker->nr_deferred = NULL; 5128e04944fSTetsuo Handa } 5138e04944fSTetsuo Handa 5148e04944fSTetsuo Handa void register_shrinker_prepared(struct shrinker *shrinker) 5158e04944fSTetsuo Handa { 5161da177e4SLinus Torvalds down_write(&shrinker_rwsem); 5171da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 51841ca668aSYang Shi shrinker->flags |= SHRINKER_REGISTERED; 5191da177e4SLinus Torvalds up_write(&shrinker_rwsem); 5208e04944fSTetsuo Handa } 5218e04944fSTetsuo Handa 5228e04944fSTetsuo Handa int register_shrinker(struct shrinker *shrinker) 5238e04944fSTetsuo Handa { 5248e04944fSTetsuo Handa int err = prealloc_shrinker(shrinker); 5258e04944fSTetsuo Handa 5268e04944fSTetsuo Handa if (err) 5278e04944fSTetsuo Handa return err; 5288e04944fSTetsuo Handa register_shrinker_prepared(shrinker); 5291d3d4437SGlauber Costa return 0; 5301da177e4SLinus Torvalds } 5318e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds /* 5341da177e4SLinus Torvalds * Remove one 5351da177e4SLinus Torvalds */ 5368e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 5371da177e4SLinus Torvalds { 53841ca668aSYang Shi if (!(shrinker->flags & SHRINKER_REGISTERED)) 539bb422a73STetsuo Handa return; 54041ca668aSYang Shi 5411da177e4SLinus Torvalds down_write(&shrinker_rwsem); 5421da177e4SLinus Torvalds list_del(&shrinker->list); 54341ca668aSYang Shi shrinker->flags &= ~SHRINKER_REGISTERED; 54441ca668aSYang Shi if (shrinker->flags & SHRINKER_MEMCG_AWARE) 54541ca668aSYang Shi unregister_memcg_shrinker(shrinker); 5461da177e4SLinus Torvalds up_write(&shrinker_rwsem); 54741ca668aSYang Shi 548ae393321SAndrew Vagin kfree(shrinker->nr_deferred); 549bb422a73STetsuo Handa shrinker->nr_deferred = NULL; 5501da177e4SLinus Torvalds } 5518e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds #define SHRINK_BATCH 128 5541d3d4437SGlauber Costa 555cb731d6cSVladimir Davydov static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, 5569092c71bSJosef Bacik struct shrinker *shrinker, int priority) 5571da177e4SLinus Torvalds { 55824f7c6b9SDave Chinner unsigned long freed = 0; 5591da177e4SLinus Torvalds unsigned long long delta; 560635697c6SKonstantin Khlebnikov long total_scan; 561d5bc5fd3SVladimir Davydov long freeable; 562acf92b48SDave Chinner long nr; 563acf92b48SDave Chinner long new_nr; 5641d3d4437SGlauber Costa int nid = shrinkctl->nid; 565e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 566e9299f50SDave Chinner : SHRINK_BATCH; 5675f33a080SShaohua Li long scanned = 0, next_deferred; 5681da177e4SLinus Torvalds 569ac7fb3adSKirill Tkhai if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) 570ac7fb3adSKirill Tkhai nid = 0; 571ac7fb3adSKirill Tkhai 572d5bc5fd3SVladimir Davydov freeable = shrinker->count_objects(shrinker, shrinkctl); 5739b996468SKirill Tkhai if (freeable == 0 || freeable == SHRINK_EMPTY) 5749b996468SKirill Tkhai return freeable; 575635697c6SKonstantin Khlebnikov 576acf92b48SDave Chinner /* 577acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 578acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 579acf92b48SDave Chinner * don't also do this scanning work. 580acf92b48SDave Chinner */ 5811d3d4437SGlauber Costa nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0); 582acf92b48SDave Chinner 583acf92b48SDave Chinner total_scan = nr; 5844b85afbdSJohannes Weiner if (shrinker->seeks) { 5859092c71bSJosef Bacik delta = freeable >> priority; 5869092c71bSJosef Bacik delta *= 4; 5879092c71bSJosef Bacik do_div(delta, shrinker->seeks); 5884b85afbdSJohannes Weiner } else { 5894b85afbdSJohannes Weiner /* 5904b85afbdSJohannes Weiner * These objects don't require any IO to create. Trim 5914b85afbdSJohannes Weiner * them aggressively under memory pressure to keep 5924b85afbdSJohannes Weiner * them from causing refetches in the IO caches. 5934b85afbdSJohannes Weiner */ 5944b85afbdSJohannes Weiner delta = freeable / 2; 5954b85afbdSJohannes Weiner } 596172b06c3SRoman Gushchin 597acf92b48SDave Chinner total_scan += delta; 598acf92b48SDave Chinner if (total_scan < 0) { 599d75f773cSSakari Ailus pr_err("shrink_slab: %pS negative objects to delete nr=%ld\n", 600a0b02131SDave Chinner shrinker->scan_objects, total_scan); 601d5bc5fd3SVladimir Davydov total_scan = freeable; 6025f33a080SShaohua Li next_deferred = nr; 6035f33a080SShaohua Li } else 6045f33a080SShaohua Li next_deferred = total_scan; 605ea164d73SAndrea Arcangeli 606ea164d73SAndrea Arcangeli /* 6073567b59aSDave Chinner * We need to avoid excessive windup on filesystem shrinkers 6083567b59aSDave Chinner * due to large numbers of GFP_NOFS allocations causing the 6093567b59aSDave Chinner * shrinkers to return -1 all the time. This results in a large 6103567b59aSDave Chinner * nr being built up so when a shrink that can do some work 6113567b59aSDave Chinner * comes along it empties the entire cache due to nr >>> 612d5bc5fd3SVladimir Davydov * freeable. This is bad for sustaining a working set in 6133567b59aSDave Chinner * memory. 6143567b59aSDave Chinner * 6153567b59aSDave Chinner * Hence only allow the shrinker to scan the entire cache when 6163567b59aSDave Chinner * a large delta change is calculated directly. 6173567b59aSDave Chinner */ 618d5bc5fd3SVladimir Davydov if (delta < freeable / 4) 619d5bc5fd3SVladimir Davydov total_scan = min(total_scan, freeable / 2); 6203567b59aSDave Chinner 6213567b59aSDave Chinner /* 622ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 623ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 624ea164d73SAndrea Arcangeli * freeable entries. 625ea164d73SAndrea Arcangeli */ 626d5bc5fd3SVladimir Davydov if (total_scan > freeable * 2) 627d5bc5fd3SVladimir Davydov total_scan = freeable * 2; 6281da177e4SLinus Torvalds 62924f7c6b9SDave Chinner trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, 6309092c71bSJosef Bacik freeable, delta, total_scan, priority); 63109576073SDave Chinner 6320b1fb40aSVladimir Davydov /* 6330b1fb40aSVladimir Davydov * Normally, we should not scan less than batch_size objects in one 6340b1fb40aSVladimir Davydov * pass to avoid too frequent shrinker calls, but if the slab has less 6350b1fb40aSVladimir Davydov * than batch_size objects in total and we are really tight on memory, 6360b1fb40aSVladimir Davydov * we will try to reclaim all available objects, otherwise we can end 6370b1fb40aSVladimir Davydov * up failing allocations although there are plenty of reclaimable 6380b1fb40aSVladimir Davydov * objects spread over several slabs with usage less than the 6390b1fb40aSVladimir Davydov * batch_size. 6400b1fb40aSVladimir Davydov * 6410b1fb40aSVladimir Davydov * We detect the "tight on memory" situations by looking at the total 6420b1fb40aSVladimir Davydov * number of objects we want to scan (total_scan). If it is greater 643d5bc5fd3SVladimir Davydov * than the total number of objects on slab (freeable), we must be 6440b1fb40aSVladimir Davydov * scanning at high prio and therefore should try to reclaim as much as 6450b1fb40aSVladimir Davydov * possible. 6460b1fb40aSVladimir Davydov */ 6470b1fb40aSVladimir Davydov while (total_scan >= batch_size || 648d5bc5fd3SVladimir Davydov total_scan >= freeable) { 64924f7c6b9SDave Chinner unsigned long ret; 6500b1fb40aSVladimir Davydov unsigned long nr_to_scan = min(batch_size, total_scan); 6511da177e4SLinus Torvalds 6520b1fb40aSVladimir Davydov shrinkctl->nr_to_scan = nr_to_scan; 653d460acb5SChris Wilson shrinkctl->nr_scanned = nr_to_scan; 65424f7c6b9SDave Chinner ret = shrinker->scan_objects(shrinker, shrinkctl); 65524f7c6b9SDave Chinner if (ret == SHRINK_STOP) 6561da177e4SLinus Torvalds break; 65724f7c6b9SDave Chinner freed += ret; 65824f7c6b9SDave Chinner 659d460acb5SChris Wilson count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned); 660d460acb5SChris Wilson total_scan -= shrinkctl->nr_scanned; 661d460acb5SChris Wilson scanned += shrinkctl->nr_scanned; 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds cond_resched(); 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds 6665f33a080SShaohua Li if (next_deferred >= scanned) 6675f33a080SShaohua Li next_deferred -= scanned; 6685f33a080SShaohua Li else 6695f33a080SShaohua Li next_deferred = 0; 670acf92b48SDave Chinner /* 671acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 672acf92b48SDave Chinner * manner that handles concurrent updates. If we exhausted the 673acf92b48SDave Chinner * scan, there is no need to do an update. 674acf92b48SDave Chinner */ 6755f33a080SShaohua Li if (next_deferred > 0) 6765f33a080SShaohua Li new_nr = atomic_long_add_return(next_deferred, 6771d3d4437SGlauber Costa &shrinker->nr_deferred[nid]); 67883aeeadaSKonstantin Khlebnikov else 6791d3d4437SGlauber Costa new_nr = atomic_long_read(&shrinker->nr_deferred[nid]); 680acf92b48SDave Chinner 6818efb4b59SYang Shi trace_mm_shrink_slab_end(shrinker, shrinkctl->nid, freed, nr, new_nr, total_scan); 6821d3d4437SGlauber Costa return freed; 6831d3d4437SGlauber Costa } 6841d3d4437SGlauber Costa 6850a432dcbSYang Shi #ifdef CONFIG_MEMCG 686b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 687b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 688b0dedc49SKirill Tkhai { 689e4262c4fSYang Shi struct shrinker_info *info; 690b8e57efaSKirill Tkhai unsigned long ret, freed = 0; 691b8e57efaSKirill Tkhai int i; 692b0dedc49SKirill Tkhai 6930a432dcbSYang Shi if (!mem_cgroup_online(memcg)) 694b0dedc49SKirill Tkhai return 0; 695b0dedc49SKirill Tkhai 696b0dedc49SKirill Tkhai if (!down_read_trylock(&shrinker_rwsem)) 697b0dedc49SKirill Tkhai return 0; 698b0dedc49SKirill Tkhai 699468ab843SYang Shi info = shrinker_info_protected(memcg, nid); 700e4262c4fSYang Shi if (unlikely(!info)) 701b0dedc49SKirill Tkhai goto unlock; 702b0dedc49SKirill Tkhai 703e4262c4fSYang Shi for_each_set_bit(i, info->map, shrinker_nr_max) { 704b0dedc49SKirill Tkhai struct shrink_control sc = { 705b0dedc49SKirill Tkhai .gfp_mask = gfp_mask, 706b0dedc49SKirill Tkhai .nid = nid, 707b0dedc49SKirill Tkhai .memcg = memcg, 708b0dedc49SKirill Tkhai }; 709b0dedc49SKirill Tkhai struct shrinker *shrinker; 710b0dedc49SKirill Tkhai 711b0dedc49SKirill Tkhai shrinker = idr_find(&shrinker_idr, i); 71241ca668aSYang Shi if (unlikely(!shrinker || !(shrinker->flags & SHRINKER_REGISTERED))) { 7137e010df5SKirill Tkhai if (!shrinker) 714e4262c4fSYang Shi clear_bit(i, info->map); 715b0dedc49SKirill Tkhai continue; 716b0dedc49SKirill Tkhai } 717b0dedc49SKirill Tkhai 7180a432dcbSYang Shi /* Call non-slab shrinkers even though kmem is disabled */ 7190a432dcbSYang Shi if (!memcg_kmem_enabled() && 7200a432dcbSYang Shi !(shrinker->flags & SHRINKER_NONSLAB)) 7210a432dcbSYang Shi continue; 7220a432dcbSYang Shi 723b0dedc49SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 724f90280d6SKirill Tkhai if (ret == SHRINK_EMPTY) { 725e4262c4fSYang Shi clear_bit(i, info->map); 726f90280d6SKirill Tkhai /* 727f90280d6SKirill Tkhai * After the shrinker reported that it had no objects to 728f90280d6SKirill Tkhai * free, but before we cleared the corresponding bit in 729f90280d6SKirill Tkhai * the memcg shrinker map, a new object might have been 730f90280d6SKirill Tkhai * added. To make sure, we have the bit set in this 731f90280d6SKirill Tkhai * case, we invoke the shrinker one more time and reset 732f90280d6SKirill Tkhai * the bit if it reports that it is not empty anymore. 733f90280d6SKirill Tkhai * The memory barrier here pairs with the barrier in 7342bfd3637SYang Shi * set_shrinker_bit(): 735f90280d6SKirill Tkhai * 736f90280d6SKirill Tkhai * list_lru_add() shrink_slab_memcg() 737f90280d6SKirill Tkhai * list_add_tail() clear_bit() 738f90280d6SKirill Tkhai * <MB> <MB> 739f90280d6SKirill Tkhai * set_bit() do_shrink_slab() 740f90280d6SKirill Tkhai */ 741f90280d6SKirill Tkhai smp_mb__after_atomic(); 742f90280d6SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 7439b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 7449b996468SKirill Tkhai ret = 0; 745f90280d6SKirill Tkhai else 7462bfd3637SYang Shi set_shrinker_bit(memcg, nid, i); 747f90280d6SKirill Tkhai } 748b0dedc49SKirill Tkhai freed += ret; 749b0dedc49SKirill Tkhai 750b0dedc49SKirill Tkhai if (rwsem_is_contended(&shrinker_rwsem)) { 751b0dedc49SKirill Tkhai freed = freed ? : 1; 752b0dedc49SKirill Tkhai break; 753b0dedc49SKirill Tkhai } 754b0dedc49SKirill Tkhai } 755b0dedc49SKirill Tkhai unlock: 756b0dedc49SKirill Tkhai up_read(&shrinker_rwsem); 757b0dedc49SKirill Tkhai return freed; 758b0dedc49SKirill Tkhai } 7590a432dcbSYang Shi #else /* CONFIG_MEMCG */ 760b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 761b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 762b0dedc49SKirill Tkhai { 763b0dedc49SKirill Tkhai return 0; 764b0dedc49SKirill Tkhai } 7650a432dcbSYang Shi #endif /* CONFIG_MEMCG */ 766b0dedc49SKirill Tkhai 7676b4f7799SJohannes Weiner /** 768cb731d6cSVladimir Davydov * shrink_slab - shrink slab caches 7696b4f7799SJohannes Weiner * @gfp_mask: allocation context 7706b4f7799SJohannes Weiner * @nid: node whose slab caches to target 771cb731d6cSVladimir Davydov * @memcg: memory cgroup whose slab caches to target 7729092c71bSJosef Bacik * @priority: the reclaim priority 7731d3d4437SGlauber Costa * 7746b4f7799SJohannes Weiner * Call the shrink functions to age shrinkable caches. 7751d3d4437SGlauber Costa * 7766b4f7799SJohannes Weiner * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, 7776b4f7799SJohannes Weiner * unaware shrinkers will receive a node id of 0 instead. 7781d3d4437SGlauber Costa * 779aeed1d32SVladimir Davydov * @memcg specifies the memory cgroup to target. Unaware shrinkers 780aeed1d32SVladimir Davydov * are called only if it is the root cgroup. 781cb731d6cSVladimir Davydov * 7829092c71bSJosef Bacik * @priority is sc->priority, we take the number of objects and >> by priority 7839092c71bSJosef Bacik * in order to get the scan target. 7841d3d4437SGlauber Costa * 7856b4f7799SJohannes Weiner * Returns the number of reclaimed slab objects. 7861d3d4437SGlauber Costa */ 787cb731d6cSVladimir Davydov static unsigned long shrink_slab(gfp_t gfp_mask, int nid, 788cb731d6cSVladimir Davydov struct mem_cgroup *memcg, 7899092c71bSJosef Bacik int priority) 7901d3d4437SGlauber Costa { 791b8e57efaSKirill Tkhai unsigned long ret, freed = 0; 7921d3d4437SGlauber Costa struct shrinker *shrinker; 7931d3d4437SGlauber Costa 794fa1e512fSYang Shi /* 795fa1e512fSYang Shi * The root memcg might be allocated even though memcg is disabled 796fa1e512fSYang Shi * via "cgroup_disable=memory" boot parameter. This could make 797fa1e512fSYang Shi * mem_cgroup_is_root() return false, then just run memcg slab 798fa1e512fSYang Shi * shrink, but skip global shrink. This may result in premature 799fa1e512fSYang Shi * oom. 800fa1e512fSYang Shi */ 801fa1e512fSYang Shi if (!mem_cgroup_disabled() && !mem_cgroup_is_root(memcg)) 802b0dedc49SKirill Tkhai return shrink_slab_memcg(gfp_mask, nid, memcg, priority); 803cb731d6cSVladimir Davydov 804e830c63aSTetsuo Handa if (!down_read_trylock(&shrinker_rwsem)) 8051d3d4437SGlauber Costa goto out; 8061d3d4437SGlauber Costa 8071d3d4437SGlauber Costa list_for_each_entry(shrinker, &shrinker_list, list) { 8086b4f7799SJohannes Weiner struct shrink_control sc = { 8096b4f7799SJohannes Weiner .gfp_mask = gfp_mask, 8106b4f7799SJohannes Weiner .nid = nid, 811cb731d6cSVladimir Davydov .memcg = memcg, 8126b4f7799SJohannes Weiner }; 8136b4f7799SJohannes Weiner 8149b996468SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 8159b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 8169b996468SKirill Tkhai ret = 0; 8179b996468SKirill Tkhai freed += ret; 818e496612cSMinchan Kim /* 819e496612cSMinchan Kim * Bail out if someone want to register a new shrinker to 82055b65a57SEthon Paul * prevent the registration from being stalled for long periods 821e496612cSMinchan Kim * by parallel ongoing shrinking. 822e496612cSMinchan Kim */ 823e496612cSMinchan Kim if (rwsem_is_contended(&shrinker_rwsem)) { 824e496612cSMinchan Kim freed = freed ? : 1; 825e496612cSMinchan Kim break; 826e496612cSMinchan Kim } 827ec97097bSVladimir Davydov } 8281d3d4437SGlauber Costa 8291da177e4SLinus Torvalds up_read(&shrinker_rwsem); 830f06590bdSMinchan Kim out: 831f06590bdSMinchan Kim cond_resched(); 83224f7c6b9SDave Chinner return freed; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 835cb731d6cSVladimir Davydov void drop_slab_node(int nid) 836cb731d6cSVladimir Davydov { 837cb731d6cSVladimir Davydov unsigned long freed; 838cb731d6cSVladimir Davydov 839cb731d6cSVladimir Davydov do { 840cb731d6cSVladimir Davydov struct mem_cgroup *memcg = NULL; 841cb731d6cSVladimir Davydov 842069c411dSChunxin Zang if (fatal_signal_pending(current)) 843069c411dSChunxin Zang return; 844069c411dSChunxin Zang 845cb731d6cSVladimir Davydov freed = 0; 846aeed1d32SVladimir Davydov memcg = mem_cgroup_iter(NULL, NULL, NULL); 847cb731d6cSVladimir Davydov do { 8489092c71bSJosef Bacik freed += shrink_slab(GFP_KERNEL, nid, memcg, 0); 849cb731d6cSVladimir Davydov } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 850cb731d6cSVladimir Davydov } while (freed > 10); 851cb731d6cSVladimir Davydov } 852cb731d6cSVladimir Davydov 853cb731d6cSVladimir Davydov void drop_slab(void) 854cb731d6cSVladimir Davydov { 855cb731d6cSVladimir Davydov int nid; 856cb731d6cSVladimir Davydov 857cb731d6cSVladimir Davydov for_each_online_node(nid) 858cb731d6cSVladimir Davydov drop_slab_node(nid); 859cb731d6cSVladimir Davydov } 860cb731d6cSVladimir Davydov 8611da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 8621da177e4SLinus Torvalds { 863ceddc3a5SJohannes Weiner /* 864ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 86567891fffSMatthew Wilcox * that isolated the page, the page cache and optional buffer 86667891fffSMatthew Wilcox * heads at page->private. 867ceddc3a5SJohannes Weiner */ 8683efe62e4SMatthew Wilcox (Oracle) int page_cache_pins = thp_nr_pages(page); 86967891fffSMatthew Wilcox return page_count(page) - page_has_private(page) == 1 + page_cache_pins; 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds 872cb16556dSYang Shi static int may_write_to_inode(struct inode *inode) 8731da177e4SLinus Torvalds { 874930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 8751da177e4SLinus Torvalds return 1; 876703c2708STejun Heo if (!inode_write_congested(inode)) 8771da177e4SLinus Torvalds return 1; 878703c2708STejun Heo if (inode_to_bdi(inode) == current->backing_dev_info) 8791da177e4SLinus Torvalds return 1; 8801da177e4SLinus Torvalds return 0; 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds /* 8841da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 8851da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 8861da177e4SLinus Torvalds * fsync(), msync() or close(). 8871da177e4SLinus Torvalds * 8881da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 8891da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 8901da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 8911da177e4SLinus Torvalds * 8921da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 8931da177e4SLinus Torvalds * __GFP_FS. 8941da177e4SLinus Torvalds */ 8951da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 8961da177e4SLinus Torvalds struct page *page, int error) 8971da177e4SLinus Torvalds { 8987eaceaccSJens Axboe lock_page(page); 8993e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 9003e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 9011da177e4SLinus Torvalds unlock_page(page); 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds 90404e62a29SChristoph Lameter /* possible outcome of pageout() */ 90504e62a29SChristoph Lameter typedef enum { 90604e62a29SChristoph Lameter /* failed to write page out, page is locked */ 90704e62a29SChristoph Lameter PAGE_KEEP, 90804e62a29SChristoph Lameter /* move page to the active list, page is locked */ 90904e62a29SChristoph Lameter PAGE_ACTIVATE, 91004e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 91104e62a29SChristoph Lameter PAGE_SUCCESS, 91204e62a29SChristoph Lameter /* page is clean and locked */ 91304e62a29SChristoph Lameter PAGE_CLEAN, 91404e62a29SChristoph Lameter } pageout_t; 91504e62a29SChristoph Lameter 9161da177e4SLinus Torvalds /* 9171742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 9181742f19fSAndrew Morton * Calls ->writepage(). 9191da177e4SLinus Torvalds */ 920cb16556dSYang Shi static pageout_t pageout(struct page *page, struct address_space *mapping) 9211da177e4SLinus Torvalds { 9221da177e4SLinus Torvalds /* 9231da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 9241da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 9251da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 9261da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 9271da177e4SLinus Torvalds * PagePrivate for that. 9281da177e4SLinus Torvalds * 9298174202bSAl Viro * If this process is currently in __generic_file_write_iter() against 9301da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 9311da177e4SLinus Torvalds * will block. 9321da177e4SLinus Torvalds * 9331da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 9341da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 9351da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 9361da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 9371da177e4SLinus Torvalds */ 9381da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 9391da177e4SLinus Torvalds return PAGE_KEEP; 9401da177e4SLinus Torvalds if (!mapping) { 9411da177e4SLinus Torvalds /* 9421da177e4SLinus Torvalds * Some data journaling orphaned pages can have 9431da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 9441da177e4SLinus Torvalds */ 945266cf658SDavid Howells if (page_has_private(page)) { 9461da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 9471da177e4SLinus Torvalds ClearPageDirty(page); 948b1de0d13SMitchel Humpherys pr_info("%s: orphaned page\n", __func__); 9491da177e4SLinus Torvalds return PAGE_CLEAN; 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds return PAGE_KEEP; 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 9551da177e4SLinus Torvalds return PAGE_ACTIVATE; 956cb16556dSYang Shi if (!may_write_to_inode(mapping->host)) 9571da177e4SLinus Torvalds return PAGE_KEEP; 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 9601da177e4SLinus Torvalds int res; 9611da177e4SLinus Torvalds struct writeback_control wbc = { 9621da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 9631da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 964111ebb6eSOGAWA Hirofumi .range_start = 0, 965111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 9661da177e4SLinus Torvalds .for_reclaim = 1, 9671da177e4SLinus Torvalds }; 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds SetPageReclaim(page); 9701da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 9711da177e4SLinus Torvalds if (res < 0) 9721da177e4SLinus Torvalds handle_write_error(mapping, page, res); 973994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 9741da177e4SLinus Torvalds ClearPageReclaim(page); 9751da177e4SLinus Torvalds return PAGE_ACTIVATE; 9761da177e4SLinus Torvalds } 977c661b078SAndy Whitcroft 9781da177e4SLinus Torvalds if (!PageWriteback(page)) { 9791da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 9801da177e4SLinus Torvalds ClearPageReclaim(page); 9811da177e4SLinus Torvalds } 9823aa23851Syalin wang trace_mm_vmscan_writepage(page); 983c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_WRITE); 9841da177e4SLinus Torvalds return PAGE_SUCCESS; 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds return PAGE_CLEAN; 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds 990a649fd92SAndrew Morton /* 991e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 992e286781dSNick Piggin * gets returned with a refcount of 0. 993a649fd92SAndrew Morton */ 994a528910eSJohannes Weiner static int __remove_mapping(struct address_space *mapping, struct page *page, 995b910718aSJohannes Weiner bool reclaimed, struct mem_cgroup *target_memcg) 99649d2e9ccSChristoph Lameter { 997c4843a75SGreg Thelen unsigned long flags; 998bd4c82c2SHuang Ying int refcount; 999aae466b0SJoonsoo Kim void *shadow = NULL; 1000c4843a75SGreg Thelen 100128e4d965SNick Piggin BUG_ON(!PageLocked(page)); 100228e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 100349d2e9ccSChristoph Lameter 1004b93b0163SMatthew Wilcox xa_lock_irqsave(&mapping->i_pages, flags); 100549d2e9ccSChristoph Lameter /* 10060fd0e6b0SNick Piggin * The non racy check for a busy page. 10070fd0e6b0SNick Piggin * 10080fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 10090fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 10100fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 10110fd0e6b0SNick Piggin * here, then the following race may occur: 10120fd0e6b0SNick Piggin * 10130fd0e6b0SNick Piggin * get_user_pages(&page); 10140fd0e6b0SNick Piggin * [user mapping goes away] 10150fd0e6b0SNick Piggin * write_to(page); 10160fd0e6b0SNick Piggin * !PageDirty(page) [good] 10170fd0e6b0SNick Piggin * SetPageDirty(page); 10180fd0e6b0SNick Piggin * put_page(page); 10190fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 10200fd0e6b0SNick Piggin * 10210fd0e6b0SNick Piggin * [oops, our write_to data is lost] 10220fd0e6b0SNick Piggin * 10230fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 10240fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 10250139aa7bSJoonsoo Kim * load is not satisfied before that of page->_refcount. 10260fd0e6b0SNick Piggin * 10270fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 1028b93b0163SMatthew Wilcox * and thus under the i_pages lock, then this ordering is not required. 102949d2e9ccSChristoph Lameter */ 1030906d278dSWilliam Kucharski refcount = 1 + compound_nr(page); 1031bd4c82c2SHuang Ying if (!page_ref_freeze(page, refcount)) 103249d2e9ccSChristoph Lameter goto cannot_free; 10331c4c3b99SJiang Biao /* note: atomic_cmpxchg in page_ref_freeze provides the smp_rmb */ 1034e286781dSNick Piggin if (unlikely(PageDirty(page))) { 1035bd4c82c2SHuang Ying page_ref_unfreeze(page, refcount); 103649d2e9ccSChristoph Lameter goto cannot_free; 1037e286781dSNick Piggin } 103849d2e9ccSChristoph Lameter 103949d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 104049d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 10410a31bc97SJohannes Weiner mem_cgroup_swapout(page, swap); 1042aae466b0SJoonsoo Kim if (reclaimed && !mapping_exiting(mapping)) 1043aae466b0SJoonsoo Kim shadow = workingset_eviction(page, target_memcg); 1044aae466b0SJoonsoo Kim __delete_from_swap_cache(page, swap, shadow); 1045b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 104675f6d6d2SMinchan Kim put_swap_page(page, swap); 1047e286781dSNick Piggin } else { 10486072d13cSLinus Torvalds void (*freepage)(struct page *); 10496072d13cSLinus Torvalds 10506072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 1051a528910eSJohannes Weiner /* 1052a528910eSJohannes Weiner * Remember a shadow entry for reclaimed file cache in 1053a528910eSJohannes Weiner * order to detect refaults, thus thrashing, later on. 1054a528910eSJohannes Weiner * 1055a528910eSJohannes Weiner * But don't store shadows in an address space that is 1056238c3046Sdylan-meiners * already exiting. This is not just an optimization, 1057a528910eSJohannes Weiner * inode reclaim needs to empty out the radix tree or 1058a528910eSJohannes Weiner * the nodes are lost. Don't plant shadows behind its 1059a528910eSJohannes Weiner * back. 1060f9fe48beSRoss Zwisler * 1061f9fe48beSRoss Zwisler * We also don't store shadows for DAX mappings because the 1062f9fe48beSRoss Zwisler * only page cache pages found in these are zero pages 1063f9fe48beSRoss Zwisler * covering holes, and because we don't want to mix DAX 1064f9fe48beSRoss Zwisler * exceptional entries and shadow exceptional entries in the 1065b93b0163SMatthew Wilcox * same address_space. 1066a528910eSJohannes Weiner */ 10679de4f22aSHuang Ying if (reclaimed && page_is_file_lru(page) && 1068f9fe48beSRoss Zwisler !mapping_exiting(mapping) && !dax_mapping(mapping)) 1069b910718aSJohannes Weiner shadow = workingset_eviction(page, target_memcg); 107062cccb8cSJohannes Weiner __delete_from_page_cache(page, shadow); 1071b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 10726072d13cSLinus Torvalds 10736072d13cSLinus Torvalds if (freepage != NULL) 10746072d13cSLinus Torvalds freepage(page); 1075e286781dSNick Piggin } 1076e286781dSNick Piggin 107749d2e9ccSChristoph Lameter return 1; 107849d2e9ccSChristoph Lameter 107949d2e9ccSChristoph Lameter cannot_free: 1080b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 108149d2e9ccSChristoph Lameter return 0; 108249d2e9ccSChristoph Lameter } 108349d2e9ccSChristoph Lameter 10841da177e4SLinus Torvalds /* 1085e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 1086e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 1087e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 1088e286781dSNick Piggin * this page. 1089e286781dSNick Piggin */ 1090e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 1091e286781dSNick Piggin { 1092b910718aSJohannes Weiner if (__remove_mapping(mapping, page, false, NULL)) { 1093e286781dSNick Piggin /* 1094e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 1095e286781dSNick Piggin * drops the pagecache ref for us without requiring another 1096e286781dSNick Piggin * atomic operation. 1097e286781dSNick Piggin */ 1098fe896d18SJoonsoo Kim page_ref_unfreeze(page, 1); 1099e286781dSNick Piggin return 1; 1100e286781dSNick Piggin } 1101e286781dSNick Piggin return 0; 1102e286781dSNick Piggin } 1103e286781dSNick Piggin 1104894bc310SLee Schermerhorn /** 1105894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 1106894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 1107894bc310SLee Schermerhorn * 1108894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 1109894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 1110894bc310SLee Schermerhorn * 1111894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 1112894bc310SLee Schermerhorn */ 1113894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 1114894bc310SLee Schermerhorn { 1115c53954a0SMel Gorman lru_cache_add(page); 1116894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 1117894bc310SLee Schermerhorn } 1118894bc310SLee Schermerhorn 1119dfc8d636SJohannes Weiner enum page_references { 1120dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 1121dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 112264574746SJohannes Weiner PAGEREF_KEEP, 1123dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 1124dfc8d636SJohannes Weiner }; 1125dfc8d636SJohannes Weiner 1126dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 1127dfc8d636SJohannes Weiner struct scan_control *sc) 1128dfc8d636SJohannes Weiner { 112964574746SJohannes Weiner int referenced_ptes, referenced_page; 1130dfc8d636SJohannes Weiner unsigned long vm_flags; 1131dfc8d636SJohannes Weiner 1132c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 1133c3ac9a8aSJohannes Weiner &vm_flags); 113464574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 1135dfc8d636SJohannes Weiner 1136dfc8d636SJohannes Weiner /* 1137dfc8d636SJohannes Weiner * Mlock lost the isolation race with us. Let try_to_unmap() 1138dfc8d636SJohannes Weiner * move the page to the unevictable list. 1139dfc8d636SJohannes Weiner */ 1140dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 1141dfc8d636SJohannes Weiner return PAGEREF_RECLAIM; 1142dfc8d636SJohannes Weiner 114364574746SJohannes Weiner if (referenced_ptes) { 114464574746SJohannes Weiner /* 114564574746SJohannes Weiner * All mapped pages start out with page table 114664574746SJohannes Weiner * references from the instantiating fault, so we need 114764574746SJohannes Weiner * to look twice if a mapped file page is used more 114864574746SJohannes Weiner * than once. 114964574746SJohannes Weiner * 115064574746SJohannes Weiner * Mark it and spare it for another trip around the 115164574746SJohannes Weiner * inactive list. Another page table reference will 115264574746SJohannes Weiner * lead to its activation. 115364574746SJohannes Weiner * 115464574746SJohannes Weiner * Note: the mark is set for activated pages as well 115564574746SJohannes Weiner * so that recently deactivated but used pages are 115664574746SJohannes Weiner * quickly recovered. 115764574746SJohannes Weiner */ 115864574746SJohannes Weiner SetPageReferenced(page); 115964574746SJohannes Weiner 116034dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 1161dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 1162dfc8d636SJohannes Weiner 1163c909e993SKonstantin Khlebnikov /* 1164c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 1165c909e993SKonstantin Khlebnikov */ 1166b518154eSJoonsoo Kim if ((vm_flags & VM_EXEC) && !PageSwapBacked(page)) 1167c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 1168c909e993SKonstantin Khlebnikov 116964574746SJohannes Weiner return PAGEREF_KEEP; 117064574746SJohannes Weiner } 117164574746SJohannes Weiner 1172dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 11732e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 1174dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 117564574746SJohannes Weiner 117664574746SJohannes Weiner return PAGEREF_RECLAIM; 1177dfc8d636SJohannes Weiner } 1178dfc8d636SJohannes Weiner 1179e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 1180e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page, 1181e2be15f6SMel Gorman bool *dirty, bool *writeback) 1182e2be15f6SMel Gorman { 1183b4597226SMel Gorman struct address_space *mapping; 1184b4597226SMel Gorman 1185e2be15f6SMel Gorman /* 1186e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 1187e2be15f6SMel Gorman * from reclaim context. Do not stall reclaim based on them 1188e2be15f6SMel Gorman */ 11899de4f22aSHuang Ying if (!page_is_file_lru(page) || 1190802a3a92SShaohua Li (PageAnon(page) && !PageSwapBacked(page))) { 1191e2be15f6SMel Gorman *dirty = false; 1192e2be15f6SMel Gorman *writeback = false; 1193e2be15f6SMel Gorman return; 1194e2be15f6SMel Gorman } 1195e2be15f6SMel Gorman 1196e2be15f6SMel Gorman /* By default assume that the page flags are accurate */ 1197e2be15f6SMel Gorman *dirty = PageDirty(page); 1198e2be15f6SMel Gorman *writeback = PageWriteback(page); 1199b4597226SMel Gorman 1200b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 1201b4597226SMel Gorman if (!page_has_private(page)) 1202b4597226SMel Gorman return; 1203b4597226SMel Gorman 1204b4597226SMel Gorman mapping = page_mapping(page); 1205b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 1206b4597226SMel Gorman mapping->a_ops->is_dirty_writeback(page, dirty, writeback); 1207e2be15f6SMel Gorman } 1208e2be15f6SMel Gorman 1209e286781dSNick Piggin /* 12101742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 12111da177e4SLinus Torvalds */ 1212730ec8c0SManinder Singh static unsigned int shrink_page_list(struct list_head *page_list, 1213599d0c95SMel Gorman struct pglist_data *pgdat, 1214f84f6e2bSMel Gorman struct scan_control *sc, 12153c710c1aSMichal Hocko struct reclaim_stat *stat, 12168940b34aSMinchan Kim bool ignore_references) 12171da177e4SLinus Torvalds { 12181da177e4SLinus Torvalds LIST_HEAD(ret_pages); 1219abe4c3b5SMel Gorman LIST_HEAD(free_pages); 1220730ec8c0SManinder Singh unsigned int nr_reclaimed = 0; 1221730ec8c0SManinder Singh unsigned int pgactivate = 0; 12221da177e4SLinus Torvalds 1223060f005fSKirill Tkhai memset(stat, 0, sizeof(*stat)); 12241da177e4SLinus Torvalds cond_resched(); 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds while (!list_empty(page_list)) { 12271da177e4SLinus Torvalds struct address_space *mapping; 12281da177e4SLinus Torvalds struct page *page; 12298940b34aSMinchan Kim enum page_references references = PAGEREF_RECLAIM; 12304b793062SKirill Tkhai bool dirty, writeback, may_enter_fs; 123198879b3bSYang Shi unsigned int nr_pages; 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds cond_resched(); 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds page = lru_to_page(page_list); 12361da177e4SLinus Torvalds list_del(&page->lru); 12371da177e4SLinus Torvalds 1238529ae9aaSNick Piggin if (!trylock_page(page)) 12391da177e4SLinus Torvalds goto keep; 12401da177e4SLinus Torvalds 1241309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 12421da177e4SLinus Torvalds 1243d8c6546bSMatthew Wilcox (Oracle) nr_pages = compound_nr(page); 124498879b3bSYang Shi 124598879b3bSYang Shi /* Account the number of base pages even though THP */ 124698879b3bSYang Shi sc->nr_scanned += nr_pages; 124780e43426SChristoph Lameter 124839b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) 1249ad6b6704SMinchan Kim goto activate_locked; 1250894bc310SLee Schermerhorn 1251a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 125280e43426SChristoph Lameter goto keep_locked; 125380e43426SChristoph Lameter 1254c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 1255c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 1256c661b078SAndy Whitcroft 1257e62e384eSMichal Hocko /* 1258894befecSAndrey Ryabinin * The number of dirty pages determines if a node is marked 1259e2be15f6SMel Gorman * reclaim_congested which affects wait_iff_congested. kswapd 1260e2be15f6SMel Gorman * will stall and start writing pages if the tail of the LRU 1261e2be15f6SMel Gorman * is all dirty unqueued pages. 1262e2be15f6SMel Gorman */ 1263e2be15f6SMel Gorman page_check_dirty_writeback(page, &dirty, &writeback); 1264e2be15f6SMel Gorman if (dirty || writeback) 1265060f005fSKirill Tkhai stat->nr_dirty++; 1266e2be15f6SMel Gorman 1267e2be15f6SMel Gorman if (dirty && !writeback) 1268060f005fSKirill Tkhai stat->nr_unqueued_dirty++; 1269e2be15f6SMel Gorman 1270d04e8acdSMel Gorman /* 1271d04e8acdSMel Gorman * Treat this page as congested if the underlying BDI is or if 1272d04e8acdSMel Gorman * pages are cycling through the LRU so quickly that the 1273d04e8acdSMel Gorman * pages marked for immediate reclaim are making it to the 1274d04e8acdSMel Gorman * end of the LRU a second time. 1275d04e8acdSMel Gorman */ 1276e2be15f6SMel Gorman mapping = page_mapping(page); 12771da58ee2SJamie Liu if (((dirty || writeback) && mapping && 1278703c2708STejun Heo inode_write_congested(mapping->host)) || 1279d04e8acdSMel Gorman (writeback && PageReclaim(page))) 1280060f005fSKirill Tkhai stat->nr_congested++; 1281e2be15f6SMel Gorman 1282e2be15f6SMel Gorman /* 1283283aba9fSMel Gorman * If a page at the tail of the LRU is under writeback, there 1284283aba9fSMel Gorman * are three cases to consider. 1285e62e384eSMichal Hocko * 1286283aba9fSMel Gorman * 1) If reclaim is encountering an excessive number of pages 1287283aba9fSMel Gorman * under writeback and this page is both under writeback and 1288283aba9fSMel Gorman * PageReclaim then it indicates that pages are being queued 1289283aba9fSMel Gorman * for IO but are being recycled through the LRU before the 1290283aba9fSMel Gorman * IO can complete. Waiting on the page itself risks an 1291283aba9fSMel Gorman * indefinite stall if it is impossible to writeback the 1292283aba9fSMel Gorman * page due to IO error or disconnected storage so instead 1293b1a6f21eSMel Gorman * note that the LRU is being scanned too quickly and the 1294b1a6f21eSMel Gorman * caller can stall after page list has been processed. 1295c3b94f44SHugh Dickins * 129697c9341fSTejun Heo * 2) Global or new memcg reclaim encounters a page that is 1297ecf5fc6eSMichal Hocko * not marked for immediate reclaim, or the caller does not 1298ecf5fc6eSMichal Hocko * have __GFP_FS (or __GFP_IO if it's simply going to swap, 1299ecf5fc6eSMichal Hocko * not to fs). In this case mark the page for immediate 130097c9341fSTejun Heo * reclaim and continue scanning. 1301283aba9fSMel Gorman * 1302ecf5fc6eSMichal Hocko * Require may_enter_fs because we would wait on fs, which 1303ecf5fc6eSMichal Hocko * may not have submitted IO yet. And the loop driver might 1304283aba9fSMel Gorman * enter reclaim, and deadlock if it waits on a page for 1305283aba9fSMel Gorman * which it is needed to do the write (loop masks off 1306283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 1307283aba9fSMel Gorman * would probably show more reasons. 1308283aba9fSMel Gorman * 13097fadc820SHugh Dickins * 3) Legacy memcg encounters a page that is already marked 1310283aba9fSMel Gorman * PageReclaim. memcg does not have any dirty pages 1311283aba9fSMel Gorman * throttling so we could easily OOM just because too many 1312283aba9fSMel Gorman * pages are in writeback and there is nothing else to 1313283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 1314c55e8d03SJohannes Weiner * 1315c55e8d03SJohannes Weiner * In cases 1) and 2) we activate the pages to get them out of 1316c55e8d03SJohannes Weiner * the way while we continue scanning for clean pages on the 1317c55e8d03SJohannes Weiner * inactive list and refilling from the active list. The 1318c55e8d03SJohannes Weiner * observation here is that waiting for disk writes is more 1319c55e8d03SJohannes Weiner * expensive than potentially causing reloads down the line. 1320c55e8d03SJohannes Weiner * Since they're marked for immediate reclaim, they won't put 1321c55e8d03SJohannes Weiner * memory pressure on the cache working set any longer than it 1322c55e8d03SJohannes Weiner * takes to write them to disk. 1323e62e384eSMichal Hocko */ 1324283aba9fSMel Gorman if (PageWriteback(page)) { 1325283aba9fSMel Gorman /* Case 1 above */ 1326283aba9fSMel Gorman if (current_is_kswapd() && 1327283aba9fSMel Gorman PageReclaim(page) && 1328599d0c95SMel Gorman test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { 1329060f005fSKirill Tkhai stat->nr_immediate++; 1330c55e8d03SJohannes Weiner goto activate_locked; 1331283aba9fSMel Gorman 1332283aba9fSMel Gorman /* Case 2 above */ 1333b5ead35eSJohannes Weiner } else if (writeback_throttling_sane(sc) || 1334ecf5fc6eSMichal Hocko !PageReclaim(page) || !may_enter_fs) { 1335c3b94f44SHugh Dickins /* 1336c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 1337c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 1338c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 1339c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 1340c3b94f44SHugh Dickins * enough to care. What we do want is for this 1341c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 1342c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 1343c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 1344c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 1345c3b94f44SHugh Dickins */ 1346c3b94f44SHugh Dickins SetPageReclaim(page); 1347060f005fSKirill Tkhai stat->nr_writeback++; 1348c55e8d03SJohannes Weiner goto activate_locked; 1349283aba9fSMel Gorman 1350283aba9fSMel Gorman /* Case 3 above */ 1351283aba9fSMel Gorman } else { 13527fadc820SHugh Dickins unlock_page(page); 1353c3b94f44SHugh Dickins wait_on_page_writeback(page); 13547fadc820SHugh Dickins /* then go back and try same page again */ 13557fadc820SHugh Dickins list_add_tail(&page->lru, page_list); 13567fadc820SHugh Dickins continue; 1357e62e384eSMichal Hocko } 1358283aba9fSMel Gorman } 13591da177e4SLinus Torvalds 13608940b34aSMinchan Kim if (!ignore_references) 13616a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 136202c6de8dSMinchan Kim 1363dfc8d636SJohannes Weiner switch (references) { 1364dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 13651da177e4SLinus Torvalds goto activate_locked; 136664574746SJohannes Weiner case PAGEREF_KEEP: 136798879b3bSYang Shi stat->nr_ref_keep += nr_pages; 136864574746SJohannes Weiner goto keep_locked; 1369dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 1370dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 1371dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 1372dfc8d636SJohannes Weiner } 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds /* 13751da177e4SLinus Torvalds * Anonymous process memory has backing store? 13761da177e4SLinus Torvalds * Try to allocate it some swap space here. 1377802a3a92SShaohua Li * Lazyfree page could be freed directly 13781da177e4SLinus Torvalds */ 1379bd4c82c2SHuang Ying if (PageAnon(page) && PageSwapBacked(page)) { 1380bd4c82c2SHuang Ying if (!PageSwapCache(page)) { 138163eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 138263eb6b93SHugh Dickins goto keep_locked; 1383feb889fbSLinus Torvalds if (page_maybe_dma_pinned(page)) 1384feb889fbSLinus Torvalds goto keep_locked; 1385747552b1SHuang Ying if (PageTransHuge(page)) { 1386b8f593cdSHuang Ying /* cannot split THP, skip it */ 1387747552b1SHuang Ying if (!can_split_huge_page(page, NULL)) 1388b8f593cdSHuang Ying goto activate_locked; 1389747552b1SHuang Ying /* 1390747552b1SHuang Ying * Split pages without a PMD map right 1391747552b1SHuang Ying * away. Chances are some or all of the 1392747552b1SHuang Ying * tail pages can be freed without IO. 1393747552b1SHuang Ying */ 1394747552b1SHuang Ying if (!compound_mapcount(page) && 1395bd4c82c2SHuang Ying split_huge_page_to_list(page, 1396bd4c82c2SHuang Ying page_list)) 1397747552b1SHuang Ying goto activate_locked; 1398747552b1SHuang Ying } 13990f074658SMinchan Kim if (!add_to_swap(page)) { 14000f074658SMinchan Kim if (!PageTransHuge(page)) 140198879b3bSYang Shi goto activate_locked_split; 1402bd4c82c2SHuang Ying /* Fallback to swap normal pages */ 1403bd4c82c2SHuang Ying if (split_huge_page_to_list(page, 1404bd4c82c2SHuang Ying page_list)) 14050f074658SMinchan Kim goto activate_locked; 1406fe490cc0SHuang Ying #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1407fe490cc0SHuang Ying count_vm_event(THP_SWPOUT_FALLBACK); 1408fe490cc0SHuang Ying #endif 14090f074658SMinchan Kim if (!add_to_swap(page)) 141098879b3bSYang Shi goto activate_locked_split; 14110f074658SMinchan Kim } 14120f074658SMinchan Kim 14134b793062SKirill Tkhai may_enter_fs = true; 14141da177e4SLinus Torvalds 1415e2be15f6SMel Gorman /* Adding to swap updated mapping */ 14161da177e4SLinus Torvalds mapping = page_mapping(page); 1417bd4c82c2SHuang Ying } 14187751b2daSKirill A. Shutemov } else if (unlikely(PageTransHuge(page))) { 14197751b2daSKirill A. Shutemov /* Split file THP */ 14207751b2daSKirill A. Shutemov if (split_huge_page_to_list(page, page_list)) 14217751b2daSKirill A. Shutemov goto keep_locked; 1422e2be15f6SMel Gorman } 14231da177e4SLinus Torvalds 14241da177e4SLinus Torvalds /* 142598879b3bSYang Shi * THP may get split above, need minus tail pages and update 142698879b3bSYang Shi * nr_pages to avoid accounting tail pages twice. 142798879b3bSYang Shi * 142898879b3bSYang Shi * The tail pages that are added into swap cache successfully 142998879b3bSYang Shi * reach here. 143098879b3bSYang Shi */ 143198879b3bSYang Shi if ((nr_pages > 1) && !PageTransHuge(page)) { 143298879b3bSYang Shi sc->nr_scanned -= (nr_pages - 1); 143398879b3bSYang Shi nr_pages = 1; 143498879b3bSYang Shi } 143598879b3bSYang Shi 143698879b3bSYang Shi /* 14371da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 14381da177e4SLinus Torvalds * processes. Try to unmap it here. 14391da177e4SLinus Torvalds */ 1440802a3a92SShaohua Li if (page_mapped(page)) { 1441013339dfSShakeel Butt enum ttu_flags flags = TTU_BATCH_FLUSH; 14421f318a9bSJaewon Kim bool was_swapbacked = PageSwapBacked(page); 1443bd4c82c2SHuang Ying 1444bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page))) 1445bd4c82c2SHuang Ying flags |= TTU_SPLIT_HUGE_PMD; 14461f318a9bSJaewon Kim 1447bd4c82c2SHuang Ying if (!try_to_unmap(page, flags)) { 144898879b3bSYang Shi stat->nr_unmap_fail += nr_pages; 14491f318a9bSJaewon Kim if (!was_swapbacked && PageSwapBacked(page)) 14501f318a9bSJaewon Kim stat->nr_lazyfree_fail += nr_pages; 14511da177e4SLinus Torvalds goto activate_locked; 14521da177e4SLinus Torvalds } 14531da177e4SLinus Torvalds } 14541da177e4SLinus Torvalds 14551da177e4SLinus Torvalds if (PageDirty(page)) { 1456ee72886dSMel Gorman /* 14574eda4823SJohannes Weiner * Only kswapd can writeback filesystem pages 14584eda4823SJohannes Weiner * to avoid risk of stack overflow. But avoid 14594eda4823SJohannes Weiner * injecting inefficient single-page IO into 14604eda4823SJohannes Weiner * flusher writeback as much as possible: only 14614eda4823SJohannes Weiner * write pages when we've encountered many 14624eda4823SJohannes Weiner * dirty pages, and when we've already scanned 14634eda4823SJohannes Weiner * the rest of the LRU for clean pages and see 14644eda4823SJohannes Weiner * the same dirty pages again (PageReclaim). 1465ee72886dSMel Gorman */ 14669de4f22aSHuang Ying if (page_is_file_lru(page) && 14674eda4823SJohannes Weiner (!current_is_kswapd() || !PageReclaim(page) || 1468599d0c95SMel Gorman !test_bit(PGDAT_DIRTY, &pgdat->flags))) { 146949ea7eb6SMel Gorman /* 147049ea7eb6SMel Gorman * Immediately reclaim when written back. 147149ea7eb6SMel Gorman * Similar in principal to deactivate_page() 147249ea7eb6SMel Gorman * except we already have the page isolated 147349ea7eb6SMel Gorman * and know it's dirty 147449ea7eb6SMel Gorman */ 1475c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_IMMEDIATE); 147649ea7eb6SMel Gorman SetPageReclaim(page); 147749ea7eb6SMel Gorman 1478c55e8d03SJohannes Weiner goto activate_locked; 1479ee72886dSMel Gorman } 1480ee72886dSMel Gorman 1481dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 14821da177e4SLinus Torvalds goto keep_locked; 14834dd4b920SAndrew Morton if (!may_enter_fs) 14841da177e4SLinus Torvalds goto keep_locked; 148552a8363eSChristoph Lameter if (!sc->may_writepage) 14861da177e4SLinus Torvalds goto keep_locked; 14871da177e4SLinus Torvalds 1488d950c947SMel Gorman /* 1489d950c947SMel Gorman * Page is dirty. Flush the TLB if a writable entry 1490d950c947SMel Gorman * potentially exists to avoid CPU writes after IO 1491d950c947SMel Gorman * starts and then write it out here. 1492d950c947SMel Gorman */ 1493d950c947SMel Gorman try_to_unmap_flush_dirty(); 1494cb16556dSYang Shi switch (pageout(page, mapping)) { 14951da177e4SLinus Torvalds case PAGE_KEEP: 14961da177e4SLinus Torvalds goto keep_locked; 14971da177e4SLinus Torvalds case PAGE_ACTIVATE: 14981da177e4SLinus Torvalds goto activate_locked; 14991da177e4SLinus Torvalds case PAGE_SUCCESS: 15006c357848SMatthew Wilcox (Oracle) stat->nr_pageout += thp_nr_pages(page); 150196f8bf4fSJohannes Weiner 15027d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 150341ac1999SMel Gorman goto keep; 15047d3579e8SKOSAKI Motohiro if (PageDirty(page)) 15051da177e4SLinus Torvalds goto keep; 15067d3579e8SKOSAKI Motohiro 15071da177e4SLinus Torvalds /* 15081da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 15091da177e4SLinus Torvalds * ahead and try to reclaim the page. 15101da177e4SLinus Torvalds */ 1511529ae9aaSNick Piggin if (!trylock_page(page)) 15121da177e4SLinus Torvalds goto keep; 15131da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 15141da177e4SLinus Torvalds goto keep_locked; 15151da177e4SLinus Torvalds mapping = page_mapping(page); 151601359eb2SGustavo A. R. Silva fallthrough; 15171da177e4SLinus Torvalds case PAGE_CLEAN: 15181da177e4SLinus Torvalds ; /* try to free the page below */ 15191da177e4SLinus Torvalds } 15201da177e4SLinus Torvalds } 15211da177e4SLinus Torvalds 15221da177e4SLinus Torvalds /* 15231da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 15241da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 15251da177e4SLinus Torvalds * the page as well. 15261da177e4SLinus Torvalds * 15271da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 15281da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 15291da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 15301da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 15311da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 15321da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 15331da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 15341da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 15351da177e4SLinus Torvalds * 15361da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 15371da177e4SLinus Torvalds * the pages which were not successfully invalidated in 1538d12b8951SYang Shi * truncate_cleanup_page(). We try to drop those buffers here 15391da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 15401da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 15411da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 15421da177e4SLinus Torvalds */ 1543266cf658SDavid Howells if (page_has_private(page)) { 15441da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 15451da177e4SLinus Torvalds goto activate_locked; 1546e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 1547e286781dSNick Piggin unlock_page(page); 1548e286781dSNick Piggin if (put_page_testzero(page)) 15491da177e4SLinus Torvalds goto free_it; 1550e286781dSNick Piggin else { 1551e286781dSNick Piggin /* 1552e286781dSNick Piggin * rare race with speculative reference. 1553e286781dSNick Piggin * the speculative reference will free 1554e286781dSNick Piggin * this page shortly, so we may 1555e286781dSNick Piggin * increment nr_reclaimed here (and 1556e286781dSNick Piggin * leave it off the LRU). 1557e286781dSNick Piggin */ 1558e286781dSNick Piggin nr_reclaimed++; 1559e286781dSNick Piggin continue; 1560e286781dSNick Piggin } 1561e286781dSNick Piggin } 15621da177e4SLinus Torvalds } 15631da177e4SLinus Torvalds 1564802a3a92SShaohua Li if (PageAnon(page) && !PageSwapBacked(page)) { 1565802a3a92SShaohua Li /* follow __remove_mapping for reference */ 1566802a3a92SShaohua Li if (!page_ref_freeze(page, 1)) 156749d2e9ccSChristoph Lameter goto keep_locked; 1568802a3a92SShaohua Li if (PageDirty(page)) { 1569802a3a92SShaohua Li page_ref_unfreeze(page, 1); 1570802a3a92SShaohua Li goto keep_locked; 1571802a3a92SShaohua Li } 15721da177e4SLinus Torvalds 1573802a3a92SShaohua Li count_vm_event(PGLAZYFREED); 15742262185cSRoman Gushchin count_memcg_page_event(page, PGLAZYFREED); 1575b910718aSJohannes Weiner } else if (!mapping || !__remove_mapping(mapping, page, true, 1576b910718aSJohannes Weiner sc->target_mem_cgroup)) 1577802a3a92SShaohua Li goto keep_locked; 15789a1ea439SHugh Dickins 15799a1ea439SHugh Dickins unlock_page(page); 1580e286781dSNick Piggin free_it: 158198879b3bSYang Shi /* 158298879b3bSYang Shi * THP may get swapped out in a whole, need account 158398879b3bSYang Shi * all base pages. 158498879b3bSYang Shi */ 158598879b3bSYang Shi nr_reclaimed += nr_pages; 1586abe4c3b5SMel Gorman 1587abe4c3b5SMel Gorman /* 1588abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1589abe4c3b5SMel Gorman * appear not as the counts should be low 1590abe4c3b5SMel Gorman */ 15917ae88534SYang Shi if (unlikely(PageTransHuge(page))) 1592ff45fc3cSMatthew Wilcox (Oracle) destroy_compound_page(page); 15937ae88534SYang Shi else 1594abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 15951da177e4SLinus Torvalds continue; 15961da177e4SLinus Torvalds 159798879b3bSYang Shi activate_locked_split: 159898879b3bSYang Shi /* 159998879b3bSYang Shi * The tail pages that are failed to add into swap cache 160098879b3bSYang Shi * reach here. Fixup nr_scanned and nr_pages. 160198879b3bSYang Shi */ 160298879b3bSYang Shi if (nr_pages > 1) { 160398879b3bSYang Shi sc->nr_scanned -= (nr_pages - 1); 160498879b3bSYang Shi nr_pages = 1; 160598879b3bSYang Shi } 16061da177e4SLinus Torvalds activate_locked: 160768a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 1608ad6b6704SMinchan Kim if (PageSwapCache(page) && (mem_cgroup_swap_full(page) || 1609ad6b6704SMinchan Kim PageMlocked(page))) 1610a2c43eedSHugh Dickins try_to_free_swap(page); 1611309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 1612ad6b6704SMinchan Kim if (!PageMlocked(page)) { 16139de4f22aSHuang Ying int type = page_is_file_lru(page); 16141da177e4SLinus Torvalds SetPageActive(page); 161598879b3bSYang Shi stat->nr_activate[type] += nr_pages; 16162262185cSRoman Gushchin count_memcg_page_event(page, PGACTIVATE); 1617ad6b6704SMinchan Kim } 16181da177e4SLinus Torvalds keep_locked: 16191da177e4SLinus Torvalds unlock_page(page); 16201da177e4SLinus Torvalds keep: 16211da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 1622309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); 16231da177e4SLinus Torvalds } 1624abe4c3b5SMel Gorman 162598879b3bSYang Shi pgactivate = stat->nr_activate[0] + stat->nr_activate[1]; 162698879b3bSYang Shi 1627747db954SJohannes Weiner mem_cgroup_uncharge_list(&free_pages); 162872b252aeSMel Gorman try_to_unmap_flush(); 16292d4894b5SMel Gorman free_unref_page_list(&free_pages); 1630abe4c3b5SMel Gorman 16311da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1632886cf190SKirill Tkhai count_vm_events(PGACTIVATE, pgactivate); 16330a31bc97SJohannes Weiner 163405ff5137SAndrew Morton return nr_reclaimed; 16351da177e4SLinus Torvalds } 16361da177e4SLinus Torvalds 1637730ec8c0SManinder Singh unsigned int reclaim_clean_pages_from_list(struct zone *zone, 163802c6de8dSMinchan Kim struct list_head *page_list) 163902c6de8dSMinchan Kim { 164002c6de8dSMinchan Kim struct scan_control sc = { 164102c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 164202c6de8dSMinchan Kim .priority = DEF_PRIORITY, 164302c6de8dSMinchan Kim .may_unmap = 1, 164402c6de8dSMinchan Kim }; 16451f318a9bSJaewon Kim struct reclaim_stat stat; 1646730ec8c0SManinder Singh unsigned int nr_reclaimed; 164702c6de8dSMinchan Kim struct page *page, *next; 164802c6de8dSMinchan Kim LIST_HEAD(clean_pages); 164902c6de8dSMinchan Kim 165002c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 1651ae37c7ffSOscar Salvador if (!PageHuge(page) && page_is_file_lru(page) && 1652ae37c7ffSOscar Salvador !PageDirty(page) && !__PageMovable(page) && 1653ae37c7ffSOscar Salvador !PageUnevictable(page)) { 165402c6de8dSMinchan Kim ClearPageActive(page); 165502c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 165602c6de8dSMinchan Kim } 165702c6de8dSMinchan Kim } 165802c6de8dSMinchan Kim 16591f318a9bSJaewon Kim nr_reclaimed = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc, 1660013339dfSShakeel Butt &stat, true); 166102c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 16622da9f630SNicholas Piggin mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, 16632da9f630SNicholas Piggin -(long)nr_reclaimed); 16641f318a9bSJaewon Kim /* 16651f318a9bSJaewon Kim * Since lazyfree pages are isolated from file LRU from the beginning, 16661f318a9bSJaewon Kim * they will rotate back to anonymous LRU in the end if it failed to 16671f318a9bSJaewon Kim * discard so isolated count will be mismatched. 16681f318a9bSJaewon Kim * Compensate the isolated count for both LRU lists. 16691f318a9bSJaewon Kim */ 16701f318a9bSJaewon Kim mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON, 16711f318a9bSJaewon Kim stat.nr_lazyfree_fail); 16721f318a9bSJaewon Kim mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, 16732da9f630SNicholas Piggin -(long)stat.nr_lazyfree_fail); 16741f318a9bSJaewon Kim return nr_reclaimed; 167502c6de8dSMinchan Kim } 167602c6de8dSMinchan Kim 16775ad333ebSAndy Whitcroft /* 16785ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 16795ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 16805ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 16815ad333ebSAndy Whitcroft * 16825ad333ebSAndy Whitcroft * page: page to consider 16835ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 16845ad333ebSAndy Whitcroft * 1685c2135f7cSAlex Shi * returns true on success, false on failure. 16865ad333ebSAndy Whitcroft */ 1687c2135f7cSAlex Shi bool __isolate_lru_page_prepare(struct page *page, isolate_mode_t mode) 16885ad333ebSAndy Whitcroft { 16895ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 16905ad333ebSAndy Whitcroft if (!PageLRU(page)) 1691c2135f7cSAlex Shi return false; 16925ad333ebSAndy Whitcroft 1693e46a2879SMinchan Kim /* Compaction should not handle unevictable pages but CMA can do so */ 1694e46a2879SMinchan Kim if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) 1695c2135f7cSAlex Shi return false; 1696894bc310SLee Schermerhorn 1697c8244935SMel Gorman /* 1698c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 1699c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 1700c8244935SMel Gorman * blocking - clean pages for the most part. 1701c8244935SMel Gorman * 1702c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 1703c8244935SMel Gorman * that it is possible to migrate without blocking 1704c8244935SMel Gorman */ 17051276ad68SJohannes Weiner if (mode & ISOLATE_ASYNC_MIGRATE) { 1706c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 1707c8244935SMel Gorman if (PageWriteback(page)) 1708c2135f7cSAlex Shi return false; 170939deaf85SMinchan Kim 1710c8244935SMel Gorman if (PageDirty(page)) { 1711c8244935SMel Gorman struct address_space *mapping; 171269d763fcSMel Gorman bool migrate_dirty; 1713c8244935SMel Gorman 1714c8244935SMel Gorman /* 1715c8244935SMel Gorman * Only pages without mappings or that have a 1716c8244935SMel Gorman * ->migratepage callback are possible to migrate 171769d763fcSMel Gorman * without blocking. However, we can be racing with 171869d763fcSMel Gorman * truncation so it's necessary to lock the page 171969d763fcSMel Gorman * to stabilise the mapping as truncation holds 172069d763fcSMel Gorman * the page lock until after the page is removed 172169d763fcSMel Gorman * from the page cache. 1722c8244935SMel Gorman */ 172369d763fcSMel Gorman if (!trylock_page(page)) 1724c2135f7cSAlex Shi return false; 172569d763fcSMel Gorman 1726c8244935SMel Gorman mapping = page_mapping(page); 1727145e1a71SHugh Dickins migrate_dirty = !mapping || mapping->a_ops->migratepage; 172869d763fcSMel Gorman unlock_page(page); 172969d763fcSMel Gorman if (!migrate_dirty) 1730c2135f7cSAlex Shi return false; 1731c8244935SMel Gorman } 1732c8244935SMel Gorman } 1733c8244935SMel Gorman 1734f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 1735c2135f7cSAlex Shi return false; 1736f80c0673SMinchan Kim 1737c2135f7cSAlex Shi return true; 17385ad333ebSAndy Whitcroft } 17395ad333ebSAndy Whitcroft 17407ee36a14SMel Gorman /* 17417ee36a14SMel Gorman * Update LRU sizes after isolating pages. The LRU size updates must 174255b65a57SEthon Paul * be complete before mem_cgroup_update_lru_size due to a sanity check. 17437ee36a14SMel Gorman */ 17447ee36a14SMel Gorman static __always_inline void update_lru_sizes(struct lruvec *lruvec, 1745b4536f0cSMichal Hocko enum lru_list lru, unsigned long *nr_zone_taken) 17467ee36a14SMel Gorman { 17477ee36a14SMel Gorman int zid; 17487ee36a14SMel Gorman 17497ee36a14SMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 17507ee36a14SMel Gorman if (!nr_zone_taken[zid]) 17517ee36a14SMel Gorman continue; 17527ee36a14SMel Gorman 1753a892cb6bSWei Yang update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 17547ee36a14SMel Gorman } 17557ee36a14SMel Gorman 17567ee36a14SMel Gorman } 17577ee36a14SMel Gorman 1758f4b7e272SAndrey Ryabinin /** 175915b44736SHugh Dickins * Isolating page from the lruvec to fill in @dst list by nr_to_scan times. 176015b44736SHugh Dickins * 176115b44736SHugh Dickins * lruvec->lru_lock is heavily contended. Some of the functions that 17621da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 17631da177e4SLinus Torvalds * and working on them outside the LRU lock. 17641da177e4SLinus Torvalds * 17651da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 17661da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 17671da177e4SLinus Torvalds * 176815b44736SHugh Dickins * Lru_lock must be held before calling this function. 17691da177e4SLinus Torvalds * 1770791b48b6SMinchan Kim * @nr_to_scan: The number of eligible pages to look through on the list. 17715dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 17721da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1773f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1774fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 17753cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 17761da177e4SLinus Torvalds * 17771da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 17781da177e4SLinus Torvalds */ 177969e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 17805dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1781fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 1782a9e7c39fSKirill Tkhai enum lru_list lru) 17831da177e4SLinus Torvalds { 178475b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 178569e05944SAndrew Morton unsigned long nr_taken = 0; 1786599d0c95SMel Gorman unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; 17877cc30fcfSMel Gorman unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; 17883db65812SJohannes Weiner unsigned long skipped = 0; 1789791b48b6SMinchan Kim unsigned long scan, total_scan, nr_pages; 1790b2e18757SMel Gorman LIST_HEAD(pages_skipped); 1791a9e7c39fSKirill Tkhai isolate_mode_t mode = (sc->may_unmap ? 0 : ISOLATE_UNMAPPED); 17921da177e4SLinus Torvalds 179398879b3bSYang Shi total_scan = 0; 1794791b48b6SMinchan Kim scan = 0; 179598879b3bSYang Shi while (scan < nr_to_scan && !list_empty(src)) { 17965ad333ebSAndy Whitcroft struct page *page; 17975ad333ebSAndy Whitcroft 17981da177e4SLinus Torvalds page = lru_to_page(src); 17991da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 18001da177e4SLinus Torvalds 1801d8c6546bSMatthew Wilcox (Oracle) nr_pages = compound_nr(page); 180298879b3bSYang Shi total_scan += nr_pages; 180398879b3bSYang Shi 1804b2e18757SMel Gorman if (page_zonenum(page) > sc->reclaim_idx) { 1805b2e18757SMel Gorman list_move(&page->lru, &pages_skipped); 180698879b3bSYang Shi nr_skipped[page_zonenum(page)] += nr_pages; 1807b2e18757SMel Gorman continue; 1808b2e18757SMel Gorman } 1809b2e18757SMel Gorman 1810791b48b6SMinchan Kim /* 1811791b48b6SMinchan Kim * Do not count skipped pages because that makes the function 1812791b48b6SMinchan Kim * return with no isolated pages if the LRU mostly contains 1813791b48b6SMinchan Kim * ineligible pages. This causes the VM to not reclaim any 1814791b48b6SMinchan Kim * pages, triggering a premature OOM. 181598879b3bSYang Shi * 181698879b3bSYang Shi * Account all tail pages of THP. This would not cause 181798879b3bSYang Shi * premature OOM since __isolate_lru_page() returns -EBUSY 181898879b3bSYang Shi * only when the page is being freed somewhere else. 1819791b48b6SMinchan Kim */ 182098879b3bSYang Shi scan += nr_pages; 1821c2135f7cSAlex Shi if (!__isolate_lru_page_prepare(page, mode)) { 1822c2135f7cSAlex Shi /* It is being freed elsewhere */ 1823c2135f7cSAlex Shi list_move(&page->lru, src); 1824c2135f7cSAlex Shi continue; 1825c2135f7cSAlex Shi } 18269df41314SAlex Shi /* 18279df41314SAlex Shi * Be careful not to clear PageLRU until after we're 18289df41314SAlex Shi * sure the page is not being freed elsewhere -- the 18299df41314SAlex Shi * page release code relies on it. 18309df41314SAlex Shi */ 1831c2135f7cSAlex Shi if (unlikely(!get_page_unless_zero(page))) { 1832c2135f7cSAlex Shi list_move(&page->lru, src); 1833c2135f7cSAlex Shi continue; 1834c2135f7cSAlex Shi } 18359df41314SAlex Shi 18369df41314SAlex Shi if (!TestClearPageLRU(page)) { 1837c2135f7cSAlex Shi /* Another thread is already isolating this page */ 18389df41314SAlex Shi put_page(page); 1839c2135f7cSAlex Shi list_move(&page->lru, src); 1840c2135f7cSAlex Shi continue; 18419df41314SAlex Shi } 18429df41314SAlex Shi 1843599d0c95SMel Gorman nr_taken += nr_pages; 1844599d0c95SMel Gorman nr_zone_taken[page_zonenum(page)] += nr_pages; 18455ad333ebSAndy Whitcroft list_move(&page->lru, dst); 18465ad333ebSAndy Whitcroft } 18471da177e4SLinus Torvalds 1848b2e18757SMel Gorman /* 1849b2e18757SMel Gorman * Splice any skipped pages to the start of the LRU list. Note that 1850b2e18757SMel Gorman * this disrupts the LRU order when reclaiming for lower zones but 1851b2e18757SMel Gorman * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX 1852b2e18757SMel Gorman * scanning would soon rescan the same pages to skip and put the 1853b2e18757SMel Gorman * system at risk of premature OOM. 1854b2e18757SMel Gorman */ 18557cc30fcfSMel Gorman if (!list_empty(&pages_skipped)) { 18567cc30fcfSMel Gorman int zid; 18577cc30fcfSMel Gorman 18583db65812SJohannes Weiner list_splice(&pages_skipped, src); 18597cc30fcfSMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 18607cc30fcfSMel Gorman if (!nr_skipped[zid]) 18617cc30fcfSMel Gorman continue; 18627cc30fcfSMel Gorman 18637cc30fcfSMel Gorman __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); 18641265e3a6SMichal Hocko skipped += nr_skipped[zid]; 18657cc30fcfSMel Gorman } 18667cc30fcfSMel Gorman } 1867791b48b6SMinchan Kim *nr_scanned = total_scan; 18681265e3a6SMichal Hocko trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, 1869791b48b6SMinchan Kim total_scan, skipped, nr_taken, mode, lru); 1870b4536f0cSMichal Hocko update_lru_sizes(lruvec, lru, nr_zone_taken); 18711da177e4SLinus Torvalds return nr_taken; 18721da177e4SLinus Torvalds } 18731da177e4SLinus Torvalds 187462695a84SNick Piggin /** 187562695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 187662695a84SNick Piggin * @page: page to isolate from its LRU list 187762695a84SNick Piggin * 187862695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 187962695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 188062695a84SNick Piggin * 188162695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 188262695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 188362695a84SNick Piggin * 188462695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1885894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1886894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1887894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 188862695a84SNick Piggin * 188962695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 189062695a84SNick Piggin * found will be decremented. 189162695a84SNick Piggin * 189262695a84SNick Piggin * Restrictions: 1893a5d09bedSMike Rapoport * 189462695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 189501c4776bSHui Su * fundamental difference from isolate_lru_pages (which is called 189662695a84SNick Piggin * without a stable reference). 189762695a84SNick Piggin * (2) the lru_lock must not be held. 189862695a84SNick Piggin * (3) interrupts must be enabled. 189962695a84SNick Piggin */ 190062695a84SNick Piggin int isolate_lru_page(struct page *page) 190162695a84SNick Piggin { 190262695a84SNick Piggin int ret = -EBUSY; 190362695a84SNick Piggin 1904309381feSSasha Levin VM_BUG_ON_PAGE(!page_count(page), page); 1905cf2a82eeSKirill A. Shutemov WARN_RATELIMIT(PageTail(page), "trying to isolate tail page"); 19060c917313SKonstantin Khlebnikov 1907d25b5bd8SAlex Shi if (TestClearPageLRU(page)) { 1908fa9add64SHugh Dickins struct lruvec *lruvec; 190962695a84SNick Piggin 19100c917313SKonstantin Khlebnikov get_page(page); 19116168d0daSAlex Shi lruvec = lock_page_lruvec_irq(page); 191246ae6b2cSYu Zhao del_page_from_lru_list(page, lruvec); 19136168d0daSAlex Shi unlock_page_lruvec_irq(lruvec); 1914fa9add64SHugh Dickins ret = 0; 191562695a84SNick Piggin } 1916d25b5bd8SAlex Shi 191762695a84SNick Piggin return ret; 191862695a84SNick Piggin } 191962695a84SNick Piggin 19205ad333ebSAndy Whitcroft /* 1921d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 1922178821b8SXianting Tian * then get rescheduled. When there are massive number of tasks doing page 1923d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 1924d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 1925d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 192635cd7815SRik van Riel */ 1927599d0c95SMel Gorman static int too_many_isolated(struct pglist_data *pgdat, int file, 192835cd7815SRik van Riel struct scan_control *sc) 192935cd7815SRik van Riel { 193035cd7815SRik van Riel unsigned long inactive, isolated; 193135cd7815SRik van Riel 193235cd7815SRik van Riel if (current_is_kswapd()) 193335cd7815SRik van Riel return 0; 193435cd7815SRik van Riel 1935b5ead35eSJohannes Weiner if (!writeback_throttling_sane(sc)) 193635cd7815SRik van Riel return 0; 193735cd7815SRik van Riel 193835cd7815SRik van Riel if (file) { 1939599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_FILE); 1940599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_FILE); 194135cd7815SRik van Riel } else { 1942599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_ANON); 1943599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_ANON); 194435cd7815SRik van Riel } 194535cd7815SRik van Riel 19463cf23841SFengguang Wu /* 19473cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 19483cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 19493cf23841SFengguang Wu * deadlock. 19503cf23841SFengguang Wu */ 1951d0164adcSMel Gorman if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) 19523cf23841SFengguang Wu inactive >>= 3; 19533cf23841SFengguang Wu 195435cd7815SRik van Riel return isolated > inactive; 195535cd7815SRik van Riel } 195635cd7815SRik van Riel 1957a222f341SKirill Tkhai /* 195815b44736SHugh Dickins * move_pages_to_lru() moves pages from private @list to appropriate LRU list. 195915b44736SHugh Dickins * On return, @list is reused as a list of pages to be freed by the caller. 1960a222f341SKirill Tkhai * 1961a222f341SKirill Tkhai * Returns the number of pages moved to the given lruvec. 1962a222f341SKirill Tkhai */ 1963a222f341SKirill Tkhai static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec, 1964a222f341SKirill Tkhai struct list_head *list) 196566635629SMel Gorman { 1966a222f341SKirill Tkhai int nr_pages, nr_moved = 0; 19673f79768fSHugh Dickins LIST_HEAD(pages_to_free); 1968a222f341SKirill Tkhai struct page *page; 196966635629SMel Gorman 1970a222f341SKirill Tkhai while (!list_empty(list)) { 1971a222f341SKirill Tkhai page = lru_to_page(list); 1972309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 1973a222f341SKirill Tkhai list_del(&page->lru); 19743d06afabSAlex Shi if (unlikely(!page_evictable(page))) { 19756168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 197666635629SMel Gorman putback_lru_page(page); 19776168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 197866635629SMel Gorman continue; 197966635629SMel Gorman } 1980fa9add64SHugh Dickins 19813d06afabSAlex Shi /* 19823d06afabSAlex Shi * The SetPageLRU needs to be kept here for list integrity. 19833d06afabSAlex Shi * Otherwise: 19843d06afabSAlex Shi * #0 move_pages_to_lru #1 release_pages 19853d06afabSAlex Shi * if !put_page_testzero 19863d06afabSAlex Shi * if (put_page_testzero()) 19873d06afabSAlex Shi * !PageLRU //skip lru_lock 19883d06afabSAlex Shi * SetPageLRU() 19893d06afabSAlex Shi * list_add(&page->lru,) 19903d06afabSAlex Shi * list_add(&page->lru,) 19913d06afabSAlex Shi */ 19927a608572SLinus Torvalds SetPageLRU(page); 1993a222f341SKirill Tkhai 19943d06afabSAlex Shi if (unlikely(put_page_testzero(page))) { 199587560179SYu Zhao __clear_page_lru_flags(page); 19962bcf8879SHugh Dickins 19972bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 19986168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 1999ff45fc3cSMatthew Wilcox (Oracle) destroy_compound_page(page); 20006168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 20012bcf8879SHugh Dickins } else 20022bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 20033d06afabSAlex Shi 20043d06afabSAlex Shi continue; 20053d06afabSAlex Shi } 20063d06afabSAlex Shi 2007afca9157SAlex Shi /* 2008afca9157SAlex Shi * All pages were isolated from the same lruvec (and isolation 2009afca9157SAlex Shi * inhibits memcg migration). 2010afca9157SAlex Shi */ 20112a5e4e34SAlexander Duyck VM_BUG_ON_PAGE(!lruvec_holds_page_lru_lock(page, lruvec), page); 20123a9c9788SYu Zhao add_page_to_lru_list(page, lruvec); 20133d06afabSAlex Shi nr_pages = thp_nr_pages(page); 2014a222f341SKirill Tkhai nr_moved += nr_pages; 201531d8fcacSJohannes Weiner if (PageActive(page)) 201631d8fcacSJohannes Weiner workingset_age_nonresident(lruvec, nr_pages); 201766635629SMel Gorman } 201866635629SMel Gorman 20193f79768fSHugh Dickins /* 20203f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 20213f79768fSHugh Dickins */ 2022a222f341SKirill Tkhai list_splice(&pages_to_free, list); 2023a222f341SKirill Tkhai 2024a222f341SKirill Tkhai return nr_moved; 202566635629SMel Gorman } 202666635629SMel Gorman 202766635629SMel Gorman /* 2028399ba0b9SNeilBrown * If a kernel thread (such as nfsd for loop-back mounts) services 2029a37b0715SNeilBrown * a backing device by writing to the page cache it sets PF_LOCAL_THROTTLE. 2030399ba0b9SNeilBrown * In that case we should only throttle if the backing device it is 2031399ba0b9SNeilBrown * writing to is congested. In other cases it is safe to throttle. 2032399ba0b9SNeilBrown */ 2033399ba0b9SNeilBrown static int current_may_throttle(void) 2034399ba0b9SNeilBrown { 2035a37b0715SNeilBrown return !(current->flags & PF_LOCAL_THROTTLE) || 2036399ba0b9SNeilBrown current->backing_dev_info == NULL || 2037399ba0b9SNeilBrown bdi_write_congested(current->backing_dev_info); 2038399ba0b9SNeilBrown } 2039399ba0b9SNeilBrown 2040399ba0b9SNeilBrown /* 2041b2e18757SMel Gorman * shrink_inactive_list() is a helper for shrink_node(). It returns the number 20421742f19fSAndrew Morton * of reclaimed pages 20431da177e4SLinus Torvalds */ 204466635629SMel Gorman static noinline_for_stack unsigned long 20451a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 20469e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 20471da177e4SLinus Torvalds { 20481da177e4SLinus Torvalds LIST_HEAD(page_list); 2049e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 2050730ec8c0SManinder Singh unsigned int nr_reclaimed = 0; 2051e247dbceSKOSAKI Motohiro unsigned long nr_taken; 2052060f005fSKirill Tkhai struct reclaim_stat stat; 2053497a6c1bSJohannes Weiner bool file = is_file_lru(lru); 2054f46b7912SKirill Tkhai enum vm_event_item item; 2055599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 2056db73ee0dSMichal Hocko bool stalled = false; 205778dc583dSKOSAKI Motohiro 2058599d0c95SMel Gorman while (unlikely(too_many_isolated(pgdat, file, sc))) { 2059db73ee0dSMichal Hocko if (stalled) 2060db73ee0dSMichal Hocko return 0; 2061db73ee0dSMichal Hocko 2062db73ee0dSMichal Hocko /* wait a bit for the reclaimer. */ 2063db73ee0dSMichal Hocko msleep(100); 2064db73ee0dSMichal Hocko stalled = true; 206535cd7815SRik van Riel 206635cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 206735cd7815SRik van Riel if (fatal_signal_pending(current)) 206835cd7815SRik van Riel return SWAP_CLUSTER_MAX; 206935cd7815SRik van Riel } 207035cd7815SRik van Riel 20711da177e4SLinus Torvalds lru_add_drain(); 2072f80c0673SMinchan Kim 20736168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 20741da177e4SLinus Torvalds 20755dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 2076a9e7c39fSKirill Tkhai &nr_scanned, sc, lru); 207795d918fcSKonstantin Khlebnikov 2078599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 2079f46b7912SKirill Tkhai item = current_is_kswapd() ? PGSCAN_KSWAPD : PGSCAN_DIRECT; 2080b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 2081f46b7912SKirill Tkhai __count_vm_events(item, nr_scanned); 2082f46b7912SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned); 2083497a6c1bSJohannes Weiner __count_vm_events(PGSCAN_ANON + file, nr_scanned); 2084497a6c1bSJohannes Weiner 20856168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 2086d563c050SHillf Danton 2087d563c050SHillf Danton if (nr_taken == 0) 208866635629SMel Gorman return 0; 2089b35ea17bSKOSAKI Motohiro 2090013339dfSShakeel Butt nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, &stat, false); 2091c661b078SAndy Whitcroft 20926168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 2093497a6c1bSJohannes Weiner move_pages_to_lru(lruvec, &page_list); 2094497a6c1bSJohannes Weiner 2095497a6c1bSJohannes Weiner __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 2096f46b7912SKirill Tkhai item = current_is_kswapd() ? PGSTEAL_KSWAPD : PGSTEAL_DIRECT; 2097b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 2098f46b7912SKirill Tkhai __count_vm_events(item, nr_reclaimed); 2099f46b7912SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed); 2100497a6c1bSJohannes Weiner __count_vm_events(PGSTEAL_ANON + file, nr_reclaimed); 21016168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 21023f79768fSHugh Dickins 210375cc3c91SAlex Shi lru_note_cost(lruvec, file, stat.nr_pageout); 2104747db954SJohannes Weiner mem_cgroup_uncharge_list(&page_list); 21052d4894b5SMel Gorman free_unref_page_list(&page_list); 2106e11da5b4SMel Gorman 210792df3a72SMel Gorman /* 21081c610d5fSAndrey Ryabinin * If dirty pages are scanned that are not queued for IO, it 21091c610d5fSAndrey Ryabinin * implies that flushers are not doing their job. This can 21101c610d5fSAndrey Ryabinin * happen when memory pressure pushes dirty pages to the end of 21111c610d5fSAndrey Ryabinin * the LRU before the dirty limits are breached and the dirty 21121c610d5fSAndrey Ryabinin * data has expired. It can also happen when the proportion of 21131c610d5fSAndrey Ryabinin * dirty pages grows not through writes but through memory 21141c610d5fSAndrey Ryabinin * pressure reclaiming all the clean cache. And in some cases, 21151c610d5fSAndrey Ryabinin * the flushers simply cannot keep up with the allocation 21161c610d5fSAndrey Ryabinin * rate. Nudge the flusher threads in case they are asleep. 21171c610d5fSAndrey Ryabinin */ 21181c610d5fSAndrey Ryabinin if (stat.nr_unqueued_dirty == nr_taken) 21191c610d5fSAndrey Ryabinin wakeup_flusher_threads(WB_REASON_VMSCAN); 21201c610d5fSAndrey Ryabinin 2121d108c772SAndrey Ryabinin sc->nr.dirty += stat.nr_dirty; 2122d108c772SAndrey Ryabinin sc->nr.congested += stat.nr_congested; 2123d108c772SAndrey Ryabinin sc->nr.unqueued_dirty += stat.nr_unqueued_dirty; 2124d108c772SAndrey Ryabinin sc->nr.writeback += stat.nr_writeback; 2125d108c772SAndrey Ryabinin sc->nr.immediate += stat.nr_immediate; 2126d108c772SAndrey Ryabinin sc->nr.taken += nr_taken; 2127d108c772SAndrey Ryabinin if (file) 2128d108c772SAndrey Ryabinin sc->nr.file_taken += nr_taken; 21298e950282SMel Gorman 2130599d0c95SMel Gorman trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, 2131d51d1e64SSteven Rostedt nr_scanned, nr_reclaimed, &stat, sc->priority, file); 213205ff5137SAndrew Morton return nr_reclaimed; 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds 213515b44736SHugh Dickins /* 213615b44736SHugh Dickins * shrink_active_list() moves pages from the active LRU to the inactive LRU. 213715b44736SHugh Dickins * 213815b44736SHugh Dickins * We move them the other way if the page is referenced by one or more 213915b44736SHugh Dickins * processes. 214015b44736SHugh Dickins * 214115b44736SHugh Dickins * If the pages are mostly unmapped, the processing is fast and it is 214215b44736SHugh Dickins * appropriate to hold lru_lock across the whole operation. But if 214315b44736SHugh Dickins * the pages are mapped, the processing is slow (page_referenced()), so 214415b44736SHugh Dickins * we should drop lru_lock around each page. It's impossible to balance 214515b44736SHugh Dickins * this, so instead we remove the pages from the LRU while processing them. 214615b44736SHugh Dickins * It is safe to rely on PG_active against the non-LRU pages in here because 214715b44736SHugh Dickins * nobody will play with that bit on a non-LRU page. 214815b44736SHugh Dickins * 214915b44736SHugh Dickins * The downside is that we have to touch page->_refcount against each page. 215015b44736SHugh Dickins * But we had to alter page->flags anyway. 215115b44736SHugh Dickins */ 2152f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 21531a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 2154f16015fbSJohannes Weiner struct scan_control *sc, 21559e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 21561cfb419bSKAMEZAWA Hiroyuki { 215744c241f1SKOSAKI Motohiro unsigned long nr_taken; 2158f626012dSHugh Dickins unsigned long nr_scanned; 21596fe6b7e3SWu Fengguang unsigned long vm_flags; 21601cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 21618cab4754SWu Fengguang LIST_HEAD(l_active); 2162b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 21631cfb419bSKAMEZAWA Hiroyuki struct page *page; 21649d998b4fSMichal Hocko unsigned nr_deactivate, nr_activate; 21659d998b4fSMichal Hocko unsigned nr_rotated = 0; 21663cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 2167599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 21681cfb419bSKAMEZAWA Hiroyuki 21691da177e4SLinus Torvalds lru_add_drain(); 2170f80c0673SMinchan Kim 21716168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 2172925b7673SJohannes Weiner 21735dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 2174a9e7c39fSKirill Tkhai &nr_scanned, sc, lru); 217589b5fae5SJohannes Weiner 2176599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 21771cfb419bSKAMEZAWA Hiroyuki 2178912c0572SShakeel Butt if (!cgroup_reclaim(sc)) 2179599d0c95SMel Gorman __count_vm_events(PGREFILL, nr_scanned); 21802fa2690cSYafang Shao __count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned); 21819d5e6a9fSHugh Dickins 21826168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 21831da177e4SLinus Torvalds 21841da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 21851da177e4SLinus Torvalds cond_resched(); 21861da177e4SLinus Torvalds page = lru_to_page(&l_hold); 21871da177e4SLinus Torvalds list_del(&page->lru); 21887e9cd484SRik van Riel 218939b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 2190894bc310SLee Schermerhorn putback_lru_page(page); 2191894bc310SLee Schermerhorn continue; 2192894bc310SLee Schermerhorn } 2193894bc310SLee Schermerhorn 2194cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 2195cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 2196cc715d99SMel Gorman if (page_has_private(page)) 2197cc715d99SMel Gorman try_to_release_page(page, 0); 2198cc715d99SMel Gorman unlock_page(page); 2199cc715d99SMel Gorman } 2200cc715d99SMel Gorman } 2201cc715d99SMel Gorman 2202c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 2203c3ac9a8aSJohannes Weiner &vm_flags)) { 22048cab4754SWu Fengguang /* 22058cab4754SWu Fengguang * Identify referenced, file-backed active pages and 22068cab4754SWu Fengguang * give them one more trip around the active list. So 22078cab4754SWu Fengguang * that executable code get better chances to stay in 22088cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 22098cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 22108cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 22118cab4754SWu Fengguang * so we ignore them here. 22128cab4754SWu Fengguang */ 22139de4f22aSHuang Ying if ((vm_flags & VM_EXEC) && page_is_file_lru(page)) { 22146c357848SMatthew Wilcox (Oracle) nr_rotated += thp_nr_pages(page); 22158cab4754SWu Fengguang list_add(&page->lru, &l_active); 22168cab4754SWu Fengguang continue; 22178cab4754SWu Fengguang } 22188cab4754SWu Fengguang } 22197e9cd484SRik van Riel 22205205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 22211899ad18SJohannes Weiner SetPageWorkingset(page); 22221da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 22231da177e4SLinus Torvalds } 22241da177e4SLinus Torvalds 2225b555749aSAndrew Morton /* 22268cab4754SWu Fengguang * Move pages back to the lru list. 2227b555749aSAndrew Morton */ 22286168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 2229556adecbSRik van Riel 2230a222f341SKirill Tkhai nr_activate = move_pages_to_lru(lruvec, &l_active); 2231a222f341SKirill Tkhai nr_deactivate = move_pages_to_lru(lruvec, &l_inactive); 2232f372d89eSKirill Tkhai /* Keep all free pages in l_active list */ 2233f372d89eSKirill Tkhai list_splice(&l_inactive, &l_active); 22349851ac13SKirill Tkhai 22359851ac13SKirill Tkhai __count_vm_events(PGDEACTIVATE, nr_deactivate); 22369851ac13SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate); 22379851ac13SKirill Tkhai 2238599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 22396168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 22402bcf8879SHugh Dickins 2241f372d89eSKirill Tkhai mem_cgroup_uncharge_list(&l_active); 2242f372d89eSKirill Tkhai free_unref_page_list(&l_active); 22439d998b4fSMichal Hocko trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate, 22449d998b4fSMichal Hocko nr_deactivate, nr_rotated, sc->priority, file); 22451da177e4SLinus Torvalds } 22461da177e4SLinus Torvalds 22471a4e58ccSMinchan Kim unsigned long reclaim_pages(struct list_head *page_list) 22481a4e58ccSMinchan Kim { 2249f661d007SYang Shi int nid = NUMA_NO_NODE; 2250730ec8c0SManinder Singh unsigned int nr_reclaimed = 0; 22511a4e58ccSMinchan Kim LIST_HEAD(node_page_list); 22521a4e58ccSMinchan Kim struct reclaim_stat dummy_stat; 22531a4e58ccSMinchan Kim struct page *page; 22541a4e58ccSMinchan Kim struct scan_control sc = { 22551a4e58ccSMinchan Kim .gfp_mask = GFP_KERNEL, 22561a4e58ccSMinchan Kim .priority = DEF_PRIORITY, 22571a4e58ccSMinchan Kim .may_writepage = 1, 22581a4e58ccSMinchan Kim .may_unmap = 1, 22591a4e58ccSMinchan Kim .may_swap = 1, 22601a4e58ccSMinchan Kim }; 22611a4e58ccSMinchan Kim 22621a4e58ccSMinchan Kim while (!list_empty(page_list)) { 22631a4e58ccSMinchan Kim page = lru_to_page(page_list); 2264f661d007SYang Shi if (nid == NUMA_NO_NODE) { 22651a4e58ccSMinchan Kim nid = page_to_nid(page); 22661a4e58ccSMinchan Kim INIT_LIST_HEAD(&node_page_list); 22671a4e58ccSMinchan Kim } 22681a4e58ccSMinchan Kim 22691a4e58ccSMinchan Kim if (nid == page_to_nid(page)) { 22701a4e58ccSMinchan Kim ClearPageActive(page); 22711a4e58ccSMinchan Kim list_move(&page->lru, &node_page_list); 22721a4e58ccSMinchan Kim continue; 22731a4e58ccSMinchan Kim } 22741a4e58ccSMinchan Kim 22751a4e58ccSMinchan Kim nr_reclaimed += shrink_page_list(&node_page_list, 22761a4e58ccSMinchan Kim NODE_DATA(nid), 2277013339dfSShakeel Butt &sc, &dummy_stat, false); 22781a4e58ccSMinchan Kim while (!list_empty(&node_page_list)) { 22791a4e58ccSMinchan Kim page = lru_to_page(&node_page_list); 22801a4e58ccSMinchan Kim list_del(&page->lru); 22811a4e58ccSMinchan Kim putback_lru_page(page); 22821a4e58ccSMinchan Kim } 22831a4e58ccSMinchan Kim 2284f661d007SYang Shi nid = NUMA_NO_NODE; 22851a4e58ccSMinchan Kim } 22861a4e58ccSMinchan Kim 22871a4e58ccSMinchan Kim if (!list_empty(&node_page_list)) { 22881a4e58ccSMinchan Kim nr_reclaimed += shrink_page_list(&node_page_list, 22891a4e58ccSMinchan Kim NODE_DATA(nid), 2290013339dfSShakeel Butt &sc, &dummy_stat, false); 22911a4e58ccSMinchan Kim while (!list_empty(&node_page_list)) { 22921a4e58ccSMinchan Kim page = lru_to_page(&node_page_list); 22931a4e58ccSMinchan Kim list_del(&page->lru); 22941a4e58ccSMinchan Kim putback_lru_page(page); 22951a4e58ccSMinchan Kim } 22961a4e58ccSMinchan Kim } 22971a4e58ccSMinchan Kim 22981a4e58ccSMinchan Kim return nr_reclaimed; 22991a4e58ccSMinchan Kim } 23001a4e58ccSMinchan Kim 2301b91ac374SJohannes Weiner static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 2302b91ac374SJohannes Weiner struct lruvec *lruvec, struct scan_control *sc) 2303b91ac374SJohannes Weiner { 2304b91ac374SJohannes Weiner if (is_active_lru(lru)) { 2305b91ac374SJohannes Weiner if (sc->may_deactivate & (1 << is_file_lru(lru))) 2306b91ac374SJohannes Weiner shrink_active_list(nr_to_scan, lruvec, sc, lru); 2307b91ac374SJohannes Weiner else 2308b91ac374SJohannes Weiner sc->skipped_deactivate = 1; 2309b91ac374SJohannes Weiner return 0; 2310b91ac374SJohannes Weiner } 2311b91ac374SJohannes Weiner 2312b91ac374SJohannes Weiner return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 2313b91ac374SJohannes Weiner } 2314b91ac374SJohannes Weiner 231559dc76b0SRik van Riel /* 231659dc76b0SRik van Riel * The inactive anon list should be small enough that the VM never has 231759dc76b0SRik van Riel * to do too much work. 231814797e23SKOSAKI Motohiro * 231959dc76b0SRik van Riel * The inactive file list should be small enough to leave most memory 232059dc76b0SRik van Riel * to the established workingset on the scan-resistant active list, 232159dc76b0SRik van Riel * but large enough to avoid thrashing the aggregate readahead window. 232259dc76b0SRik van Riel * 232359dc76b0SRik van Riel * Both inactive lists should also be large enough that each inactive 232459dc76b0SRik van Riel * page has a chance to be referenced again before it is reclaimed. 232559dc76b0SRik van Riel * 23262a2e4885SJohannes Weiner * If that fails and refaulting is observed, the inactive list grows. 23272a2e4885SJohannes Weiner * 232859dc76b0SRik van Riel * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages 23293a50d14dSAndrey Ryabinin * on this LRU, maintained by the pageout code. An inactive_ratio 233059dc76b0SRik van Riel * of 3 means 3:1 or 25% of the pages are kept on the inactive list. 233159dc76b0SRik van Riel * 233259dc76b0SRik van Riel * total target max 233359dc76b0SRik van Riel * memory ratio inactive 233459dc76b0SRik van Riel * ------------------------------------- 233559dc76b0SRik van Riel * 10MB 1 5MB 233659dc76b0SRik van Riel * 100MB 1 50MB 233759dc76b0SRik van Riel * 1GB 3 250MB 233859dc76b0SRik van Riel * 10GB 10 0.9GB 233959dc76b0SRik van Riel * 100GB 31 3GB 234059dc76b0SRik van Riel * 1TB 101 10GB 234159dc76b0SRik van Riel * 10TB 320 32GB 234214797e23SKOSAKI Motohiro */ 2343b91ac374SJohannes Weiner static bool inactive_is_low(struct lruvec *lruvec, enum lru_list inactive_lru) 234414797e23SKOSAKI Motohiro { 2345b91ac374SJohannes Weiner enum lru_list active_lru = inactive_lru + LRU_ACTIVE; 23462a2e4885SJohannes Weiner unsigned long inactive, active; 23472a2e4885SJohannes Weiner unsigned long inactive_ratio; 234859dc76b0SRik van Riel unsigned long gb; 234959dc76b0SRik van Riel 2350b91ac374SJohannes Weiner inactive = lruvec_page_state(lruvec, NR_LRU_BASE + inactive_lru); 2351b91ac374SJohannes Weiner active = lruvec_page_state(lruvec, NR_LRU_BASE + active_lru); 2352f8d1a311SMel Gorman 235359dc76b0SRik van Riel gb = (inactive + active) >> (30 - PAGE_SHIFT); 23544002570cSJoonsoo Kim if (gb) 235559dc76b0SRik van Riel inactive_ratio = int_sqrt(10 * gb); 2356b39415b2SRik van Riel else 235759dc76b0SRik van Riel inactive_ratio = 1; 2358fd538803SMichal Hocko 235959dc76b0SRik van Riel return inactive * inactive_ratio < active; 2360b39415b2SRik van Riel } 2361b39415b2SRik van Riel 23629a265114SJohannes Weiner enum scan_balance { 23639a265114SJohannes Weiner SCAN_EQUAL, 23649a265114SJohannes Weiner SCAN_FRACT, 23659a265114SJohannes Weiner SCAN_ANON, 23669a265114SJohannes Weiner SCAN_FILE, 23679a265114SJohannes Weiner }; 23689a265114SJohannes Weiner 23691da177e4SLinus Torvalds /* 23704f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 23714f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 23724f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 23734f98a2feSRik van Riel * onto the active list instead of evict. 23744f98a2feSRik van Riel * 2375be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 2376be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 23774f98a2feSRik van Riel */ 2378afaf07a6SJohannes Weiner static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, 2379afaf07a6SJohannes Weiner unsigned long *nr) 23804f98a2feSRik van Riel { 2381afaf07a6SJohannes Weiner struct mem_cgroup *memcg = lruvec_memcg(lruvec); 2382d483a5ddSJohannes Weiner unsigned long anon_cost, file_cost, total_cost; 238333377678SVladimir Davydov int swappiness = mem_cgroup_swappiness(memcg); 2384ed017373SYu Zhao u64 fraction[ANON_AND_FILE]; 23859a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 23869a265114SJohannes Weiner enum scan_balance scan_balance; 23879a265114SJohannes Weiner unsigned long ap, fp; 23889a265114SJohannes Weiner enum lru_list lru; 238976a33fc3SShaohua Li 239076a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 2391d8b38438SVladimir Davydov if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) { 23929a265114SJohannes Weiner scan_balance = SCAN_FILE; 239376a33fc3SShaohua Li goto out; 239476a33fc3SShaohua Li } 23954f98a2feSRik van Riel 239610316b31SJohannes Weiner /* 239710316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 239810316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 239910316b31SJohannes Weiner * disable swapping for individual groups completely when 240010316b31SJohannes Weiner * using the memory controller's swap limit feature would be 240110316b31SJohannes Weiner * too expensive. 240210316b31SJohannes Weiner */ 2403b5ead35eSJohannes Weiner if (cgroup_reclaim(sc) && !swappiness) { 24049a265114SJohannes Weiner scan_balance = SCAN_FILE; 240510316b31SJohannes Weiner goto out; 240610316b31SJohannes Weiner } 240710316b31SJohannes Weiner 240810316b31SJohannes Weiner /* 240910316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 241010316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 241110316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 241210316b31SJohannes Weiner */ 241302695175SJohannes Weiner if (!sc->priority && swappiness) { 24149a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 241510316b31SJohannes Weiner goto out; 241610316b31SJohannes Weiner } 241710316b31SJohannes Weiner 241811d16c25SJohannes Weiner /* 241953138ceaSJohannes Weiner * If the system is almost out of file pages, force-scan anon. 242062376251SJohannes Weiner */ 2421b91ac374SJohannes Weiner if (sc->file_is_tiny) { 242262376251SJohannes Weiner scan_balance = SCAN_ANON; 242362376251SJohannes Weiner goto out; 242462376251SJohannes Weiner } 242562376251SJohannes Weiner 242662376251SJohannes Weiner /* 2427b91ac374SJohannes Weiner * If there is enough inactive page cache, we do not reclaim 2428b91ac374SJohannes Weiner * anything from the anonymous working right now. 2429e9868505SRik van Riel */ 2430b91ac374SJohannes Weiner if (sc->cache_trim_mode) { 24319a265114SJohannes Weiner scan_balance = SCAN_FILE; 2432e9868505SRik van Riel goto out; 24334f98a2feSRik van Riel } 24344f98a2feSRik van Riel 24359a265114SJohannes Weiner scan_balance = SCAN_FRACT; 24364f98a2feSRik van Riel /* 2437314b57fbSJohannes Weiner * Calculate the pressure balance between anon and file pages. 2438314b57fbSJohannes Weiner * 2439314b57fbSJohannes Weiner * The amount of pressure we put on each LRU is inversely 2440314b57fbSJohannes Weiner * proportional to the cost of reclaiming each list, as 2441314b57fbSJohannes Weiner * determined by the share of pages that are refaulting, times 2442314b57fbSJohannes Weiner * the relative IO cost of bringing back a swapped out 2443314b57fbSJohannes Weiner * anonymous page vs reloading a filesystem page (swappiness). 2444314b57fbSJohannes Weiner * 2445d483a5ddSJohannes Weiner * Although we limit that influence to ensure no list gets 2446d483a5ddSJohannes Weiner * left behind completely: at least a third of the pressure is 2447d483a5ddSJohannes Weiner * applied, before swappiness. 2448d483a5ddSJohannes Weiner * 2449314b57fbSJohannes Weiner * With swappiness at 100, anon and file have equal IO cost. 245058c37f6eSKOSAKI Motohiro */ 2451d483a5ddSJohannes Weiner total_cost = sc->anon_cost + sc->file_cost; 2452d483a5ddSJohannes Weiner anon_cost = total_cost + sc->anon_cost; 2453d483a5ddSJohannes Weiner file_cost = total_cost + sc->file_cost; 2454d483a5ddSJohannes Weiner total_cost = anon_cost + file_cost; 245558c37f6eSKOSAKI Motohiro 2456d483a5ddSJohannes Weiner ap = swappiness * (total_cost + 1); 2457d483a5ddSJohannes Weiner ap /= anon_cost + 1; 24584f98a2feSRik van Riel 2459d483a5ddSJohannes Weiner fp = (200 - swappiness) * (total_cost + 1); 2460d483a5ddSJohannes Weiner fp /= file_cost + 1; 24614f98a2feSRik van Riel 246276a33fc3SShaohua Li fraction[0] = ap; 246376a33fc3SShaohua Li fraction[1] = fp; 2464a4fe1631SJohannes Weiner denominator = ap + fp; 246576a33fc3SShaohua Li out: 24664111304dSHugh Dickins for_each_evictable_lru(lru) { 24674111304dSHugh Dickins int file = is_file_lru(lru); 24689783aa99SChris Down unsigned long lruvec_size; 246976a33fc3SShaohua Li unsigned long scan; 24701bc63fb1SChris Down unsigned long protection; 247176a33fc3SShaohua Li 24729783aa99SChris Down lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); 247322f7496fSYafang Shao protection = mem_cgroup_protection(sc->target_mem_cgroup, 247422f7496fSYafang Shao memcg, 24751bc63fb1SChris Down sc->memcg_low_reclaim); 24769783aa99SChris Down 24771bc63fb1SChris Down if (protection) { 24789783aa99SChris Down /* 24799783aa99SChris Down * Scale a cgroup's reclaim pressure by proportioning 24809783aa99SChris Down * its current usage to its memory.low or memory.min 24819783aa99SChris Down * setting. 24829783aa99SChris Down * 24839783aa99SChris Down * This is important, as otherwise scanning aggression 24849783aa99SChris Down * becomes extremely binary -- from nothing as we 24859783aa99SChris Down * approach the memory protection threshold, to totally 24869783aa99SChris Down * nominal as we exceed it. This results in requiring 24879783aa99SChris Down * setting extremely liberal protection thresholds. It 24889783aa99SChris Down * also means we simply get no protection at all if we 24899783aa99SChris Down * set it too low, which is not ideal. 24901bc63fb1SChris Down * 24911bc63fb1SChris Down * If there is any protection in place, we reduce scan 24921bc63fb1SChris Down * pressure by how much of the total memory used is 24931bc63fb1SChris Down * within protection thresholds. 24949783aa99SChris Down * 24959de7ca46SChris Down * There is one special case: in the first reclaim pass, 24969de7ca46SChris Down * we skip over all groups that are within their low 24979de7ca46SChris Down * protection. If that fails to reclaim enough pages to 24989de7ca46SChris Down * satisfy the reclaim goal, we come back and override 24999de7ca46SChris Down * the best-effort low protection. However, we still 25009de7ca46SChris Down * ideally want to honor how well-behaved groups are in 25019de7ca46SChris Down * that case instead of simply punishing them all 25029de7ca46SChris Down * equally. As such, we reclaim them based on how much 25031bc63fb1SChris Down * memory they are using, reducing the scan pressure 25041bc63fb1SChris Down * again by how much of the total memory used is under 25051bc63fb1SChris Down * hard protection. 25069783aa99SChris Down */ 25071bc63fb1SChris Down unsigned long cgroup_size = mem_cgroup_size(memcg); 25081bc63fb1SChris Down 25091bc63fb1SChris Down /* Avoid TOCTOU with earlier protection check */ 25101bc63fb1SChris Down cgroup_size = max(cgroup_size, protection); 25111bc63fb1SChris Down 25121bc63fb1SChris Down scan = lruvec_size - lruvec_size * protection / 25131bc63fb1SChris Down cgroup_size; 25149783aa99SChris Down 25159783aa99SChris Down /* 25161bc63fb1SChris Down * Minimally target SWAP_CLUSTER_MAX pages to keep 251755b65a57SEthon Paul * reclaim moving forwards, avoiding decrementing 25189de7ca46SChris Down * sc->priority further than desirable. 25199783aa99SChris Down */ 25201bc63fb1SChris Down scan = max(scan, SWAP_CLUSTER_MAX); 25219783aa99SChris Down } else { 25229783aa99SChris Down scan = lruvec_size; 25239783aa99SChris Down } 25249783aa99SChris Down 25259783aa99SChris Down scan >>= sc->priority; 25269783aa99SChris Down 2527688035f7SJohannes Weiner /* 2528688035f7SJohannes Weiner * If the cgroup's already been deleted, make sure to 2529688035f7SJohannes Weiner * scrape out the remaining cache. 2530688035f7SJohannes Weiner */ 2531688035f7SJohannes Weiner if (!scan && !mem_cgroup_online(memcg)) 25329783aa99SChris Down scan = min(lruvec_size, SWAP_CLUSTER_MAX); 25339a265114SJohannes Weiner 25349a265114SJohannes Weiner switch (scan_balance) { 25359a265114SJohannes Weiner case SCAN_EQUAL: 25369a265114SJohannes Weiner /* Scan lists relative to size */ 25379a265114SJohannes Weiner break; 25389a265114SJohannes Weiner case SCAN_FRACT: 25399a265114SJohannes Weiner /* 25409a265114SJohannes Weiner * Scan types proportional to swappiness and 25419a265114SJohannes Weiner * their relative recent reclaim efficiency. 254276073c64SGavin Shan * Make sure we don't miss the last page on 254376073c64SGavin Shan * the offlined memory cgroups because of a 254476073c64SGavin Shan * round-off error. 25459a265114SJohannes Weiner */ 254676073c64SGavin Shan scan = mem_cgroup_online(memcg) ? 254776073c64SGavin Shan div64_u64(scan * fraction[file], denominator) : 254876073c64SGavin Shan DIV64_U64_ROUND_UP(scan * fraction[file], 25496f04f48dSSuleiman Souhlal denominator); 25509a265114SJohannes Weiner break; 25519a265114SJohannes Weiner case SCAN_FILE: 25529a265114SJohannes Weiner case SCAN_ANON: 25539a265114SJohannes Weiner /* Scan one type exclusively */ 2554e072bff6SMateusz Nosek if ((scan_balance == SCAN_FILE) != file) 25559a265114SJohannes Weiner scan = 0; 25569a265114SJohannes Weiner break; 25579a265114SJohannes Weiner default: 25589a265114SJohannes Weiner /* Look ma, no brain */ 25599a265114SJohannes Weiner BUG(); 25609a265114SJohannes Weiner } 25616b4f7799SJohannes Weiner 25624111304dSHugh Dickins nr[lru] = scan; 256376a33fc3SShaohua Li } 25646e08a369SWu Fengguang } 25654f98a2feSRik van Riel 2566afaf07a6SJohannes Weiner static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) 25679b4f98cdSJohannes Weiner { 25689b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 2569e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 25709b4f98cdSJohannes Weiner unsigned long nr_to_scan; 25719b4f98cdSJohannes Weiner enum lru_list lru; 25729b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 25739b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 25749b4f98cdSJohannes Weiner struct blk_plug plug; 25751a501907SMel Gorman bool scan_adjusted; 25769b4f98cdSJohannes Weiner 2577afaf07a6SJohannes Weiner get_scan_count(lruvec, sc, nr); 25789b4f98cdSJohannes Weiner 2579e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 2580e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 2581e82e0561SMel Gorman 25821a501907SMel Gorman /* 25831a501907SMel Gorman * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal 25841a501907SMel Gorman * event that can occur when there is little memory pressure e.g. 25851a501907SMel Gorman * multiple streaming readers/writers. Hence, we do not abort scanning 25861a501907SMel Gorman * when the requested number of pages are reclaimed when scanning at 25871a501907SMel Gorman * DEF_PRIORITY on the assumption that the fact we are direct 25881a501907SMel Gorman * reclaiming implies that kswapd is not keeping up and it is best to 25891a501907SMel Gorman * do a batch of work at once. For memcg reclaim one check is made to 25901a501907SMel Gorman * abort proportional reclaim if either the file or anon lru has already 25911a501907SMel Gorman * dropped to zero at the first pass. 25921a501907SMel Gorman */ 2593b5ead35eSJohannes Weiner scan_adjusted = (!cgroup_reclaim(sc) && !current_is_kswapd() && 25941a501907SMel Gorman sc->priority == DEF_PRIORITY); 25951a501907SMel Gorman 25969b4f98cdSJohannes Weiner blk_start_plug(&plug); 25979b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 25989b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 2599e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 2600e82e0561SMel Gorman unsigned long nr_scanned; 2601e82e0561SMel Gorman 26029b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 26039b4f98cdSJohannes Weiner if (nr[lru]) { 26049b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 26059b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 26069b4f98cdSJohannes Weiner 26079b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 26083b991208SJohannes Weiner lruvec, sc); 26099b4f98cdSJohannes Weiner } 26109b4f98cdSJohannes Weiner } 2611e82e0561SMel Gorman 2612bd041733SMichal Hocko cond_resched(); 2613bd041733SMichal Hocko 2614e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 2615e82e0561SMel Gorman continue; 2616e82e0561SMel Gorman 26179b4f98cdSJohannes Weiner /* 2618e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 26191a501907SMel Gorman * requested. Ensure that the anon and file LRUs are scanned 2620e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 2621e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 2622e82e0561SMel Gorman * proportional to the original scan target. 2623e82e0561SMel Gorman */ 2624e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 2625e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 2626e82e0561SMel Gorman 26271a501907SMel Gorman /* 26281a501907SMel Gorman * It's just vindictive to attack the larger once the smaller 26291a501907SMel Gorman * has gone to zero. And given the way we stop scanning the 26301a501907SMel Gorman * smaller below, this makes sure that we only make one nudge 26311a501907SMel Gorman * towards proportionality once we've got nr_to_reclaim. 26321a501907SMel Gorman */ 26331a501907SMel Gorman if (!nr_file || !nr_anon) 26341a501907SMel Gorman break; 26351a501907SMel Gorman 2636e82e0561SMel Gorman if (nr_file > nr_anon) { 2637e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 2638e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 2639e82e0561SMel Gorman lru = LRU_BASE; 2640e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 2641e82e0561SMel Gorman } else { 2642e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 2643e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 2644e82e0561SMel Gorman lru = LRU_FILE; 2645e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 2646e82e0561SMel Gorman } 2647e82e0561SMel Gorman 2648e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 2649e82e0561SMel Gorman nr[lru] = 0; 2650e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 2651e82e0561SMel Gorman 2652e82e0561SMel Gorman /* 2653e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 2654e82e0561SMel Gorman * scan target and the percentage scanning already complete 2655e82e0561SMel Gorman */ 2656e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 2657e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2658e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2659e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2660e82e0561SMel Gorman 2661e82e0561SMel Gorman lru += LRU_ACTIVE; 2662e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2663e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2664e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2665e82e0561SMel Gorman 2666e82e0561SMel Gorman scan_adjusted = true; 26679b4f98cdSJohannes Weiner } 26689b4f98cdSJohannes Weiner blk_finish_plug(&plug); 26699b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 26709b4f98cdSJohannes Weiner 26719b4f98cdSJohannes Weiner /* 26729b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 26739b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 26749b4f98cdSJohannes Weiner */ 2675b91ac374SJohannes Weiner if (total_swap_pages && inactive_is_low(lruvec, LRU_INACTIVE_ANON)) 26769b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 26779b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 26789b4f98cdSJohannes Weiner } 26799b4f98cdSJohannes Weiner 268023b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 26819e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 268223b9da55SMel Gorman { 2683d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 268423b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 26859e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 268623b9da55SMel Gorman return true; 268723b9da55SMel Gorman 268823b9da55SMel Gorman return false; 268923b9da55SMel Gorman } 269023b9da55SMel Gorman 26914f98a2feSRik van Riel /* 269223b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 269323b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 269423b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 2695df3a45f9SQiwu Chen * calls try_to_compact_pages() that it will have enough free pages to succeed. 269623b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 26973e7d3449SMel Gorman */ 2698a9dd0a83SMel Gorman static inline bool should_continue_reclaim(struct pglist_data *pgdat, 26993e7d3449SMel Gorman unsigned long nr_reclaimed, 27003e7d3449SMel Gorman struct scan_control *sc) 27013e7d3449SMel Gorman { 27023e7d3449SMel Gorman unsigned long pages_for_compaction; 27033e7d3449SMel Gorman unsigned long inactive_lru_pages; 2704a9dd0a83SMel Gorman int z; 27053e7d3449SMel Gorman 27063e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 27079e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 27083e7d3449SMel Gorman return false; 27093e7d3449SMel Gorman 27103e7d3449SMel Gorman /* 27115ee04716SVlastimil Babka * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX 27125ee04716SVlastimil Babka * number of pages that were scanned. This will return to the caller 27135ee04716SVlastimil Babka * with the risk reclaim/compaction and the resulting allocation attempt 27145ee04716SVlastimil Babka * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL 27155ee04716SVlastimil Babka * allocations through requiring that the full LRU list has been scanned 27165ee04716SVlastimil Babka * first, by assuming that zero delta of sc->nr_scanned means full LRU 27175ee04716SVlastimil Babka * scan, but that approximation was wrong, and there were corner cases 27185ee04716SVlastimil Babka * where always a non-zero amount of pages were scanned. 27192876592fSMel Gorman */ 27202876592fSMel Gorman if (!nr_reclaimed) 27212876592fSMel Gorman return false; 27223e7d3449SMel Gorman 27233e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 2724a9dd0a83SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 2725a9dd0a83SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 27266aa303deSMel Gorman if (!managed_zone(zone)) 2727a9dd0a83SMel Gorman continue; 2728a9dd0a83SMel Gorman 2729a9dd0a83SMel Gorman switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) { 2730cf378319SVlastimil Babka case COMPACT_SUCCESS: 27313e7d3449SMel Gorman case COMPACT_CONTINUE: 27323e7d3449SMel Gorman return false; 27333e7d3449SMel Gorman default: 2734a9dd0a83SMel Gorman /* check next zone */ 2735a9dd0a83SMel Gorman ; 27363e7d3449SMel Gorman } 27373e7d3449SMel Gorman } 27381c6c1597SHillf Danton 27391c6c1597SHillf Danton /* 27401c6c1597SHillf Danton * If we have not reclaimed enough pages for compaction and the 27411c6c1597SHillf Danton * inactive lists are large enough, continue reclaiming 27421c6c1597SHillf Danton */ 27431c6c1597SHillf Danton pages_for_compaction = compact_gap(sc->order); 27441c6c1597SHillf Danton inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); 27451c6c1597SHillf Danton if (get_nr_swap_pages() > 0) 27461c6c1597SHillf Danton inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); 27471c6c1597SHillf Danton 27485ee04716SVlastimil Babka return inactive_lru_pages > pages_for_compaction; 2749a9dd0a83SMel Gorman } 27503e7d3449SMel Gorman 27510f6a5cffSJohannes Weiner static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc) 2752f16015fbSJohannes Weiner { 27530f6a5cffSJohannes Weiner struct mem_cgroup *target_memcg = sc->target_mem_cgroup; 2754694fbc0fSAndrew Morton struct mem_cgroup *memcg; 2755d108c772SAndrey Ryabinin 27560f6a5cffSJohannes Weiner memcg = mem_cgroup_iter(target_memcg, NULL, NULL); 2757694fbc0fSAndrew Morton do { 2758afaf07a6SJohannes Weiner struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); 27598e8ae645SJohannes Weiner unsigned long reclaimed; 2760cb731d6cSVladimir Davydov unsigned long scanned; 27619b4f98cdSJohannes Weiner 2762e3336cabSXunlei Pang /* 2763e3336cabSXunlei Pang * This loop can become CPU-bound when target memcgs 2764e3336cabSXunlei Pang * aren't eligible for reclaim - either because they 2765e3336cabSXunlei Pang * don't have any reclaimable pages, or because their 2766e3336cabSXunlei Pang * memory is explicitly protected. Avoid soft lockups. 2767e3336cabSXunlei Pang */ 2768e3336cabSXunlei Pang cond_resched(); 2769e3336cabSXunlei Pang 277045c7f7e1SChris Down mem_cgroup_calculate_protection(target_memcg, memcg); 277145c7f7e1SChris Down 277245c7f7e1SChris Down if (mem_cgroup_below_min(memcg)) { 2773bf8d5d52SRoman Gushchin /* 2774bf8d5d52SRoman Gushchin * Hard protection. 2775bf8d5d52SRoman Gushchin * If there is no reclaimable memory, OOM. 2776bf8d5d52SRoman Gushchin */ 2777bf8d5d52SRoman Gushchin continue; 277845c7f7e1SChris Down } else if (mem_cgroup_below_low(memcg)) { 2779bf8d5d52SRoman Gushchin /* 2780bf8d5d52SRoman Gushchin * Soft protection. 2781bf8d5d52SRoman Gushchin * Respect the protection only as long as 2782bf8d5d52SRoman Gushchin * there is an unprotected supply 2783bf8d5d52SRoman Gushchin * of reclaimable memory from other cgroups. 2784bf8d5d52SRoman Gushchin */ 2785d6622f63SYisheng Xie if (!sc->memcg_low_reclaim) { 2786d6622f63SYisheng Xie sc->memcg_low_skipped = 1; 2787241994edSJohannes Weiner continue; 2788d6622f63SYisheng Xie } 2789e27be240SJohannes Weiner memcg_memory_event(memcg, MEMCG_LOW); 2790241994edSJohannes Weiner } 2791241994edSJohannes Weiner 27928e8ae645SJohannes Weiner reclaimed = sc->nr_reclaimed; 2793cb731d6cSVladimir Davydov scanned = sc->nr_scanned; 2794afaf07a6SJohannes Weiner 2795afaf07a6SJohannes Weiner shrink_lruvec(lruvec, sc); 2796f9be23d6SKonstantin Khlebnikov 279728360f39SMel Gorman shrink_slab(sc->gfp_mask, pgdat->node_id, memcg, 279828360f39SMel Gorman sc->priority); 2799cb731d6cSVladimir Davydov 28008e8ae645SJohannes Weiner /* Record the group's reclaim efficiency */ 28018e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, memcg, false, 28028e8ae645SJohannes Weiner sc->nr_scanned - scanned, 28038e8ae645SJohannes Weiner sc->nr_reclaimed - reclaimed); 28048e8ae645SJohannes Weiner 28050f6a5cffSJohannes Weiner } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL))); 28060f6a5cffSJohannes Weiner } 28070f6a5cffSJohannes Weiner 28086c9e0907SLiu Song static void shrink_node(pg_data_t *pgdat, struct scan_control *sc) 28090f6a5cffSJohannes Weiner { 28100f6a5cffSJohannes Weiner struct reclaim_state *reclaim_state = current->reclaim_state; 28110f6a5cffSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 28121b05117dSJohannes Weiner struct lruvec *target_lruvec; 28130f6a5cffSJohannes Weiner bool reclaimable = false; 2814b91ac374SJohannes Weiner unsigned long file; 28150f6a5cffSJohannes Weiner 28161b05117dSJohannes Weiner target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat); 28171b05117dSJohannes Weiner 28180f6a5cffSJohannes Weiner again: 28190f6a5cffSJohannes Weiner memset(&sc->nr, 0, sizeof(sc->nr)); 28200f6a5cffSJohannes Weiner 28210f6a5cffSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 28220f6a5cffSJohannes Weiner nr_scanned = sc->nr_scanned; 28230f6a5cffSJohannes Weiner 282453138ceaSJohannes Weiner /* 28257cf111bcSJohannes Weiner * Determine the scan balance between anon and file LRUs. 28267cf111bcSJohannes Weiner */ 28276168d0daSAlex Shi spin_lock_irq(&target_lruvec->lru_lock); 28287cf111bcSJohannes Weiner sc->anon_cost = target_lruvec->anon_cost; 28297cf111bcSJohannes Weiner sc->file_cost = target_lruvec->file_cost; 28306168d0daSAlex Shi spin_unlock_irq(&target_lruvec->lru_lock); 28317cf111bcSJohannes Weiner 28327cf111bcSJohannes Weiner /* 2833b91ac374SJohannes Weiner * Target desirable inactive:active list ratios for the anon 2834b91ac374SJohannes Weiner * and file LRU lists. 2835b91ac374SJohannes Weiner */ 2836b91ac374SJohannes Weiner if (!sc->force_deactivate) { 2837b91ac374SJohannes Weiner unsigned long refaults; 2838b91ac374SJohannes Weiner 2839170b04b7SJoonsoo Kim refaults = lruvec_page_state(target_lruvec, 2840170b04b7SJoonsoo Kim WORKINGSET_ACTIVATE_ANON); 2841170b04b7SJoonsoo Kim if (refaults != target_lruvec->refaults[0] || 2842170b04b7SJoonsoo Kim inactive_is_low(target_lruvec, LRU_INACTIVE_ANON)) 2843b91ac374SJohannes Weiner sc->may_deactivate |= DEACTIVATE_ANON; 2844b91ac374SJohannes Weiner else 2845b91ac374SJohannes Weiner sc->may_deactivate &= ~DEACTIVATE_ANON; 2846b91ac374SJohannes Weiner 2847b91ac374SJohannes Weiner /* 2848b91ac374SJohannes Weiner * When refaults are being observed, it means a new 2849b91ac374SJohannes Weiner * workingset is being established. Deactivate to get 2850b91ac374SJohannes Weiner * rid of any stale active pages quickly. 2851b91ac374SJohannes Weiner */ 2852b91ac374SJohannes Weiner refaults = lruvec_page_state(target_lruvec, 2853170b04b7SJoonsoo Kim WORKINGSET_ACTIVATE_FILE); 2854170b04b7SJoonsoo Kim if (refaults != target_lruvec->refaults[1] || 2855b91ac374SJohannes Weiner inactive_is_low(target_lruvec, LRU_INACTIVE_FILE)) 2856b91ac374SJohannes Weiner sc->may_deactivate |= DEACTIVATE_FILE; 2857b91ac374SJohannes Weiner else 2858b91ac374SJohannes Weiner sc->may_deactivate &= ~DEACTIVATE_FILE; 2859b91ac374SJohannes Weiner } else 2860b91ac374SJohannes Weiner sc->may_deactivate = DEACTIVATE_ANON | DEACTIVATE_FILE; 2861b91ac374SJohannes Weiner 2862b91ac374SJohannes Weiner /* 2863b91ac374SJohannes Weiner * If we have plenty of inactive file pages that aren't 2864b91ac374SJohannes Weiner * thrashing, try to reclaim those first before touching 2865b91ac374SJohannes Weiner * anonymous pages. 2866b91ac374SJohannes Weiner */ 2867b91ac374SJohannes Weiner file = lruvec_page_state(target_lruvec, NR_INACTIVE_FILE); 2868b91ac374SJohannes Weiner if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE)) 2869b91ac374SJohannes Weiner sc->cache_trim_mode = 1; 2870b91ac374SJohannes Weiner else 2871b91ac374SJohannes Weiner sc->cache_trim_mode = 0; 2872b91ac374SJohannes Weiner 2873b91ac374SJohannes Weiner /* 287453138ceaSJohannes Weiner * Prevent the reclaimer from falling into the cache trap: as 287553138ceaSJohannes Weiner * cache pages start out inactive, every cache fault will tip 287653138ceaSJohannes Weiner * the scan balance towards the file LRU. And as the file LRU 287753138ceaSJohannes Weiner * shrinks, so does the window for rotation from references. 287853138ceaSJohannes Weiner * This means we have a runaway feedback loop where a tiny 287953138ceaSJohannes Weiner * thrashing file LRU becomes infinitely more attractive than 288053138ceaSJohannes Weiner * anon pages. Try to detect this based on file LRU size. 288153138ceaSJohannes Weiner */ 288253138ceaSJohannes Weiner if (!cgroup_reclaim(sc)) { 288353138ceaSJohannes Weiner unsigned long total_high_wmark = 0; 2884b91ac374SJohannes Weiner unsigned long free, anon; 2885b91ac374SJohannes Weiner int z; 288653138ceaSJohannes Weiner 288753138ceaSJohannes Weiner free = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); 288853138ceaSJohannes Weiner file = node_page_state(pgdat, NR_ACTIVE_FILE) + 288953138ceaSJohannes Weiner node_page_state(pgdat, NR_INACTIVE_FILE); 289053138ceaSJohannes Weiner 289153138ceaSJohannes Weiner for (z = 0; z < MAX_NR_ZONES; z++) { 289253138ceaSJohannes Weiner struct zone *zone = &pgdat->node_zones[z]; 289353138ceaSJohannes Weiner if (!managed_zone(zone)) 289453138ceaSJohannes Weiner continue; 289553138ceaSJohannes Weiner 289653138ceaSJohannes Weiner total_high_wmark += high_wmark_pages(zone); 289753138ceaSJohannes Weiner } 289853138ceaSJohannes Weiner 2899b91ac374SJohannes Weiner /* 2900b91ac374SJohannes Weiner * Consider anon: if that's low too, this isn't a 2901b91ac374SJohannes Weiner * runaway file reclaim problem, but rather just 2902b91ac374SJohannes Weiner * extreme pressure. Reclaim as per usual then. 2903b91ac374SJohannes Weiner */ 2904b91ac374SJohannes Weiner anon = node_page_state(pgdat, NR_INACTIVE_ANON); 2905b91ac374SJohannes Weiner 2906b91ac374SJohannes Weiner sc->file_is_tiny = 2907b91ac374SJohannes Weiner file + free <= total_high_wmark && 2908b91ac374SJohannes Weiner !(sc->may_deactivate & DEACTIVATE_ANON) && 2909b91ac374SJohannes Weiner anon >> sc->priority; 291053138ceaSJohannes Weiner } 291153138ceaSJohannes Weiner 29120f6a5cffSJohannes Weiner shrink_node_memcgs(pgdat, sc); 291370ddf637SAnton Vorontsov 29146b4f7799SJohannes Weiner if (reclaim_state) { 2915cb731d6cSVladimir Davydov sc->nr_reclaimed += reclaim_state->reclaimed_slab; 29166b4f7799SJohannes Weiner reclaim_state->reclaimed_slab = 0; 29176b4f7799SJohannes Weiner } 29186b4f7799SJohannes Weiner 29198e8ae645SJohannes Weiner /* Record the subtree's reclaim efficiency */ 29201b05117dSJohannes Weiner vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, 292170ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 292270ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 292370ddf637SAnton Vorontsov 29242344d7e4SJohannes Weiner if (sc->nr_reclaimed - nr_reclaimed) 29252344d7e4SJohannes Weiner reclaimable = true; 29262344d7e4SJohannes Weiner 2927e3c1ac58SAndrey Ryabinin if (current_is_kswapd()) { 2928d108c772SAndrey Ryabinin /* 2929e3c1ac58SAndrey Ryabinin * If reclaim is isolating dirty pages under writeback, 2930e3c1ac58SAndrey Ryabinin * it implies that the long-lived page allocation rate 2931e3c1ac58SAndrey Ryabinin * is exceeding the page laundering rate. Either the 2932e3c1ac58SAndrey Ryabinin * global limits are not being effective at throttling 2933e3c1ac58SAndrey Ryabinin * processes due to the page distribution throughout 2934e3c1ac58SAndrey Ryabinin * zones or there is heavy usage of a slow backing 2935e3c1ac58SAndrey Ryabinin * device. The only option is to throttle from reclaim 2936e3c1ac58SAndrey Ryabinin * context which is not ideal as there is no guarantee 2937d108c772SAndrey Ryabinin * the dirtying process is throttled in the same way 2938d108c772SAndrey Ryabinin * balance_dirty_pages() manages. 2939d108c772SAndrey Ryabinin * 2940e3c1ac58SAndrey Ryabinin * Once a node is flagged PGDAT_WRITEBACK, kswapd will 2941e3c1ac58SAndrey Ryabinin * count the number of pages under pages flagged for 2942e3c1ac58SAndrey Ryabinin * immediate reclaim and stall if any are encountered 2943e3c1ac58SAndrey Ryabinin * in the nr_immediate check below. 2944d108c772SAndrey Ryabinin */ 2945d108c772SAndrey Ryabinin if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken) 2946d108c772SAndrey Ryabinin set_bit(PGDAT_WRITEBACK, &pgdat->flags); 2947d108c772SAndrey Ryabinin 2948d108c772SAndrey Ryabinin /* Allow kswapd to start writing pages during reclaim.*/ 2949d108c772SAndrey Ryabinin if (sc->nr.unqueued_dirty == sc->nr.file_taken) 2950d108c772SAndrey Ryabinin set_bit(PGDAT_DIRTY, &pgdat->flags); 2951d108c772SAndrey Ryabinin 2952d108c772SAndrey Ryabinin /* 29531eba09c1SRandy Dunlap * If kswapd scans pages marked for immediate 2954d108c772SAndrey Ryabinin * reclaim and under writeback (nr_immediate), it 2955d108c772SAndrey Ryabinin * implies that pages are cycling through the LRU 2956d108c772SAndrey Ryabinin * faster than they are written so also forcibly stall. 2957d108c772SAndrey Ryabinin */ 2958d108c772SAndrey Ryabinin if (sc->nr.immediate) 2959d108c772SAndrey Ryabinin congestion_wait(BLK_RW_ASYNC, HZ/10); 2960d108c772SAndrey Ryabinin } 2961d108c772SAndrey Ryabinin 2962d108c772SAndrey Ryabinin /* 29631b05117dSJohannes Weiner * Tag a node/memcg as congested if all the dirty pages 29641b05117dSJohannes Weiner * scanned were backed by a congested BDI and 29651b05117dSJohannes Weiner * wait_iff_congested will stall. 29661b05117dSJohannes Weiner * 2967e3c1ac58SAndrey Ryabinin * Legacy memcg will stall in page writeback so avoid forcibly 2968e3c1ac58SAndrey Ryabinin * stalling in wait_iff_congested(). 2969e3c1ac58SAndrey Ryabinin */ 29701b05117dSJohannes Weiner if ((current_is_kswapd() || 29711b05117dSJohannes Weiner (cgroup_reclaim(sc) && writeback_throttling_sane(sc))) && 2972e3c1ac58SAndrey Ryabinin sc->nr.dirty && sc->nr.dirty == sc->nr.congested) 29731b05117dSJohannes Weiner set_bit(LRUVEC_CONGESTED, &target_lruvec->flags); 2974e3c1ac58SAndrey Ryabinin 2975e3c1ac58SAndrey Ryabinin /* 2976d108c772SAndrey Ryabinin * Stall direct reclaim for IO completions if underlying BDIs 2977d108c772SAndrey Ryabinin * and node is congested. Allow kswapd to continue until it 2978d108c772SAndrey Ryabinin * starts encountering unqueued dirty pages or cycling through 2979d108c772SAndrey Ryabinin * the LRU too quickly. 2980d108c772SAndrey Ryabinin */ 29811b05117dSJohannes Weiner if (!current_is_kswapd() && current_may_throttle() && 29821b05117dSJohannes Weiner !sc->hibernation_mode && 29831b05117dSJohannes Weiner test_bit(LRUVEC_CONGESTED, &target_lruvec->flags)) 2984e3c1ac58SAndrey Ryabinin wait_iff_congested(BLK_RW_ASYNC, HZ/10); 2985d108c772SAndrey Ryabinin 2986d2af3397SJohannes Weiner if (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, 2987d2af3397SJohannes Weiner sc)) 2988d2af3397SJohannes Weiner goto again; 29892344d7e4SJohannes Weiner 2990c73322d0SJohannes Weiner /* 2991c73322d0SJohannes Weiner * Kswapd gives up on balancing particular nodes after too 2992c73322d0SJohannes Weiner * many failures to reclaim anything from them and goes to 2993c73322d0SJohannes Weiner * sleep. On reclaim progress, reset the failure counter. A 2994c73322d0SJohannes Weiner * successful direct reclaim run will revive a dormant kswapd. 2995c73322d0SJohannes Weiner */ 2996c73322d0SJohannes Weiner if (reclaimable) 2997c73322d0SJohannes Weiner pgdat->kswapd_failures = 0; 2998f16015fbSJohannes Weiner } 2999f16015fbSJohannes Weiner 300053853e2dSVlastimil Babka /* 3001fdd4c614SVlastimil Babka * Returns true if compaction should go ahead for a costly-order request, or 3002fdd4c614SVlastimil Babka * the allocation would already succeed without compaction. Return false if we 3003fdd4c614SVlastimil Babka * should reclaim first. 300453853e2dSVlastimil Babka */ 30054f588331SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 3006fe4b1b24SMel Gorman { 300731483b6aSMel Gorman unsigned long watermark; 3008fdd4c614SVlastimil Babka enum compact_result suitable; 3009fe4b1b24SMel Gorman 3010fdd4c614SVlastimil Babka suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx); 3011fdd4c614SVlastimil Babka if (suitable == COMPACT_SUCCESS) 3012fdd4c614SVlastimil Babka /* Allocation should succeed already. Don't reclaim. */ 3013fdd4c614SVlastimil Babka return true; 3014fdd4c614SVlastimil Babka if (suitable == COMPACT_SKIPPED) 3015fdd4c614SVlastimil Babka /* Compaction cannot yet proceed. Do reclaim. */ 3016fe4b1b24SMel Gorman return false; 3017fe4b1b24SMel Gorman 3018fdd4c614SVlastimil Babka /* 3019fdd4c614SVlastimil Babka * Compaction is already possible, but it takes time to run and there 3020fdd4c614SVlastimil Babka * are potentially other callers using the pages just freed. So proceed 3021fdd4c614SVlastimil Babka * with reclaim to make a buffer of free pages available to give 3022fdd4c614SVlastimil Babka * compaction a reasonable chance of completing and allocating the page. 3023fdd4c614SVlastimil Babka * Note that we won't actually reclaim the whole buffer in one attempt 3024fdd4c614SVlastimil Babka * as the target watermark in should_continue_reclaim() is lower. But if 3025fdd4c614SVlastimil Babka * we are already above the high+gap watermark, don't reclaim at all. 3026fdd4c614SVlastimil Babka */ 3027fdd4c614SVlastimil Babka watermark = high_wmark_pages(zone) + compact_gap(sc->order); 3028fdd4c614SVlastimil Babka 3029fdd4c614SVlastimil Babka return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); 3030fe4b1b24SMel Gorman } 3031fe4b1b24SMel Gorman 30321da177e4SLinus Torvalds /* 30331da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 30341da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 30351da177e4SLinus Torvalds * request. 30361da177e4SLinus Torvalds * 30371da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 30381da177e4SLinus Torvalds * scan then give up on it. 30391da177e4SLinus Torvalds */ 30400a0337e0SMichal Hocko static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 30411da177e4SLinus Torvalds { 3042dd1a239fSMel Gorman struct zoneref *z; 304354a6eb5cSMel Gorman struct zone *zone; 30440608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 30450608f43dSAndrew Morton unsigned long nr_soft_scanned; 3046619d0d76SWeijie Yang gfp_t orig_mask; 304779dafcdcSMel Gorman pg_data_t *last_pgdat = NULL; 30481cfb419bSKAMEZAWA Hiroyuki 3049cc715d99SMel Gorman /* 3050cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 3051cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 3052cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 3053cc715d99SMel Gorman */ 3054619d0d76SWeijie Yang orig_mask = sc->gfp_mask; 3055b2e18757SMel Gorman if (buffer_heads_over_limit) { 3056cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 30574f588331SMel Gorman sc->reclaim_idx = gfp_zone(sc->gfp_mask); 3058b2e18757SMel Gorman } 3059cc715d99SMel Gorman 3060d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 3061b2e18757SMel Gorman sc->reclaim_idx, sc->nodemask) { 3062b2e18757SMel Gorman /* 30631cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 30641cfb419bSKAMEZAWA Hiroyuki * to global LRU. 30651cfb419bSKAMEZAWA Hiroyuki */ 3066b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) { 3067344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, 3068344736f2SVladimir Davydov GFP_KERNEL | __GFP_HARDWALL)) 30691da177e4SLinus Torvalds continue; 307065ec02cbSVladimir Davydov 3071e0887c19SRik van Riel /* 3072e0c23279SMel Gorman * If we already have plenty of memory free for 3073e0c23279SMel Gorman * compaction in this zone, don't free any more. 3074e0c23279SMel Gorman * Even though compaction is invoked for any 3075e0c23279SMel Gorman * non-zero order, only frequent costly order 3076e0c23279SMel Gorman * reclamation is disruptive enough to become a 3077c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 3078c7cfa37bSCopot Alexandru * page allocations. 3079e0887c19SRik van Riel */ 30800b06496aSJohannes Weiner if (IS_ENABLED(CONFIG_COMPACTION) && 30810b06496aSJohannes Weiner sc->order > PAGE_ALLOC_COSTLY_ORDER && 30824f588331SMel Gorman compaction_ready(zone, sc)) { 30830b06496aSJohannes Weiner sc->compaction_ready = true; 3084e0887c19SRik van Riel continue; 3085e0887c19SRik van Riel } 30860b06496aSJohannes Weiner 30870608f43dSAndrew Morton /* 308879dafcdcSMel Gorman * Shrink each node in the zonelist once. If the 308979dafcdcSMel Gorman * zonelist is ordered by zone (not the default) then a 309079dafcdcSMel Gorman * node may be shrunk multiple times but in that case 309179dafcdcSMel Gorman * the user prefers lower zones being preserved. 309279dafcdcSMel Gorman */ 309379dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 309479dafcdcSMel Gorman continue; 309579dafcdcSMel Gorman 309679dafcdcSMel Gorman /* 30970608f43dSAndrew Morton * This steals pages from memory cgroups over softlimit 30980608f43dSAndrew Morton * and returns the number of reclaimed pages and 30990608f43dSAndrew Morton * scanned pages. This works for global memory pressure 31000608f43dSAndrew Morton * and balancing, not for a memcg's limit. 31010608f43dSAndrew Morton */ 31020608f43dSAndrew Morton nr_soft_scanned = 0; 3103ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, 31040608f43dSAndrew Morton sc->order, sc->gfp_mask, 31050608f43dSAndrew Morton &nr_soft_scanned); 31060608f43dSAndrew Morton sc->nr_reclaimed += nr_soft_reclaimed; 31070608f43dSAndrew Morton sc->nr_scanned += nr_soft_scanned; 3108ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 3109ac34a1a3SKAMEZAWA Hiroyuki } 3110d149e3b2SYing Han 311179dafcdcSMel Gorman /* See comment about same check for global reclaim above */ 311279dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 311379dafcdcSMel Gorman continue; 311479dafcdcSMel Gorman last_pgdat = zone->zone_pgdat; 3115970a39a3SMel Gorman shrink_node(zone->zone_pgdat, sc); 31161da177e4SLinus Torvalds } 3117e0c23279SMel Gorman 311865ec02cbSVladimir Davydov /* 3119619d0d76SWeijie Yang * Restore to original mask to avoid the impact on the caller if we 3120619d0d76SWeijie Yang * promoted it to __GFP_HIGHMEM. 3121619d0d76SWeijie Yang */ 3122619d0d76SWeijie Yang sc->gfp_mask = orig_mask; 31231da177e4SLinus Torvalds } 31241da177e4SLinus Torvalds 3125b910718aSJohannes Weiner static void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat) 31262a2e4885SJohannes Weiner { 3127b910718aSJohannes Weiner struct lruvec *target_lruvec; 31282a2e4885SJohannes Weiner unsigned long refaults; 31292a2e4885SJohannes Weiner 3130b910718aSJohannes Weiner target_lruvec = mem_cgroup_lruvec(target_memcg, pgdat); 3131170b04b7SJoonsoo Kim refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_ANON); 3132170b04b7SJoonsoo Kim target_lruvec->refaults[0] = refaults; 3133170b04b7SJoonsoo Kim refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_FILE); 3134170b04b7SJoonsoo Kim target_lruvec->refaults[1] = refaults; 31352a2e4885SJohannes Weiner } 31362a2e4885SJohannes Weiner 31371da177e4SLinus Torvalds /* 31381da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 31391da177e4SLinus Torvalds * 31401da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 31411da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 31421da177e4SLinus Torvalds * 31431da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 31441da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 31455b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 31465b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 31475b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 31485b0830cbSJens Axboe * work, and the allocation attempt will fail. 3149a41f24eaSNishanth Aravamudan * 3150a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 3151a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 31521da177e4SLinus Torvalds */ 3153dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 31543115cd91SVladimir Davydov struct scan_control *sc) 31551da177e4SLinus Torvalds { 3156241994edSJohannes Weiner int initial_priority = sc->priority; 31572a2e4885SJohannes Weiner pg_data_t *last_pgdat; 31582a2e4885SJohannes Weiner struct zoneref *z; 31592a2e4885SJohannes Weiner struct zone *zone; 3160241994edSJohannes Weiner retry: 3161873b4771SKeika Kobayashi delayacct_freepages_start(); 3162873b4771SKeika Kobayashi 3163b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 31647cc30fcfSMel Gorman __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); 31651da177e4SLinus Torvalds 31669e3b2f8cSKonstantin Khlebnikov do { 316770ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 316870ddf637SAnton Vorontsov sc->priority); 316966e1707bSBalbir Singh sc->nr_scanned = 0; 31700a0337e0SMichal Hocko shrink_zones(zonelist, sc); 3171e0c23279SMel Gorman 3172bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 31730b06496aSJohannes Weiner break; 31740b06496aSJohannes Weiner 31750b06496aSJohannes Weiner if (sc->compaction_ready) 31760b06496aSJohannes Weiner break; 31771da177e4SLinus Torvalds 31781da177e4SLinus Torvalds /* 31790e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 31800e50ce3bSMinchan Kim * writepage even in laptop mode. 31810e50ce3bSMinchan Kim */ 31820e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 31830e50ce3bSMinchan Kim sc->may_writepage = 1; 31840b06496aSJohannes Weiner } while (--sc->priority >= 0); 3185bb21c7ceSKOSAKI Motohiro 31862a2e4885SJohannes Weiner last_pgdat = NULL; 31872a2e4885SJohannes Weiner for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx, 31882a2e4885SJohannes Weiner sc->nodemask) { 31892a2e4885SJohannes Weiner if (zone->zone_pgdat == last_pgdat) 31902a2e4885SJohannes Weiner continue; 31912a2e4885SJohannes Weiner last_pgdat = zone->zone_pgdat; 31921b05117dSJohannes Weiner 31932a2e4885SJohannes Weiner snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat); 31941b05117dSJohannes Weiner 31951b05117dSJohannes Weiner if (cgroup_reclaim(sc)) { 31961b05117dSJohannes Weiner struct lruvec *lruvec; 31971b05117dSJohannes Weiner 31981b05117dSJohannes Weiner lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, 31991b05117dSJohannes Weiner zone->zone_pgdat); 32001b05117dSJohannes Weiner clear_bit(LRUVEC_CONGESTED, &lruvec->flags); 32011b05117dSJohannes Weiner } 32022a2e4885SJohannes Weiner } 32032a2e4885SJohannes Weiner 3204873b4771SKeika Kobayashi delayacct_freepages_end(); 3205873b4771SKeika Kobayashi 3206bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 3207bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 3208bb21c7ceSKOSAKI Motohiro 32090cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 32100b06496aSJohannes Weiner if (sc->compaction_ready) 32117335084dSMel Gorman return 1; 32127335084dSMel Gorman 3213b91ac374SJohannes Weiner /* 3214b91ac374SJohannes Weiner * We make inactive:active ratio decisions based on the node's 3215b91ac374SJohannes Weiner * composition of memory, but a restrictive reclaim_idx or a 3216b91ac374SJohannes Weiner * memory.low cgroup setting can exempt large amounts of 3217b91ac374SJohannes Weiner * memory from reclaim. Neither of which are very common, so 3218b91ac374SJohannes Weiner * instead of doing costly eligibility calculations of the 3219b91ac374SJohannes Weiner * entire cgroup subtree up front, we assume the estimates are 3220b91ac374SJohannes Weiner * good, and retry with forcible deactivation if that fails. 3221b91ac374SJohannes Weiner */ 3222b91ac374SJohannes Weiner if (sc->skipped_deactivate) { 3223b91ac374SJohannes Weiner sc->priority = initial_priority; 3224b91ac374SJohannes Weiner sc->force_deactivate = 1; 3225b91ac374SJohannes Weiner sc->skipped_deactivate = 0; 3226b91ac374SJohannes Weiner goto retry; 3227b91ac374SJohannes Weiner } 3228b91ac374SJohannes Weiner 3229241994edSJohannes Weiner /* Untapped cgroup reserves? Don't OOM, retry. */ 3230d6622f63SYisheng Xie if (sc->memcg_low_skipped) { 3231241994edSJohannes Weiner sc->priority = initial_priority; 3232b91ac374SJohannes Weiner sc->force_deactivate = 0; 3233d6622f63SYisheng Xie sc->memcg_low_reclaim = 1; 3234d6622f63SYisheng Xie sc->memcg_low_skipped = 0; 3235241994edSJohannes Weiner goto retry; 3236241994edSJohannes Weiner } 3237241994edSJohannes Weiner 3238bb21c7ceSKOSAKI Motohiro return 0; 32391da177e4SLinus Torvalds } 32401da177e4SLinus Torvalds 3241c73322d0SJohannes Weiner static bool allow_direct_reclaim(pg_data_t *pgdat) 32425515061dSMel Gorman { 32435515061dSMel Gorman struct zone *zone; 32445515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 32455515061dSMel Gorman unsigned long free_pages = 0; 32465515061dSMel Gorman int i; 32475515061dSMel Gorman bool wmark_ok; 32485515061dSMel Gorman 3249c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3250c73322d0SJohannes Weiner return true; 3251c73322d0SJohannes Weiner 32525515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 32535515061dSMel Gorman zone = &pgdat->node_zones[i]; 3254d450abd8SJohannes Weiner if (!managed_zone(zone)) 3255d450abd8SJohannes Weiner continue; 3256d450abd8SJohannes Weiner 3257d450abd8SJohannes Weiner if (!zone_reclaimable_pages(zone)) 3258675becceSMel Gorman continue; 3259675becceSMel Gorman 32605515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 32615515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 32625515061dSMel Gorman } 32635515061dSMel Gorman 3264675becceSMel Gorman /* If there are no reserves (unexpected config) then do not throttle */ 3265675becceSMel Gorman if (!pfmemalloc_reserve) 3266675becceSMel Gorman return true; 3267675becceSMel Gorman 32685515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 32695515061dSMel Gorman 32705515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 32715515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 327297a225e6SJoonsoo Kim if (READ_ONCE(pgdat->kswapd_highest_zoneidx) > ZONE_NORMAL) 327397a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, ZONE_NORMAL); 32745644e1fbSQian Cai 32755515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 32765515061dSMel Gorman } 32775515061dSMel Gorman 32785515061dSMel Gorman return wmark_ok; 32795515061dSMel Gorman } 32805515061dSMel Gorman 32815515061dSMel Gorman /* 32825515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 32835515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 32845515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 328550694c28SMel Gorman * when the low watermark is reached. 328650694c28SMel Gorman * 328750694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 328850694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 32895515061dSMel Gorman */ 329050694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 32915515061dSMel Gorman nodemask_t *nodemask) 32925515061dSMel Gorman { 3293675becceSMel Gorman struct zoneref *z; 32945515061dSMel Gorman struct zone *zone; 3295675becceSMel Gorman pg_data_t *pgdat = NULL; 32965515061dSMel Gorman 32975515061dSMel Gorman /* 32985515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 32995515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 33005515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 33015515061dSMel Gorman * committing a transaction where throttling it could forcing other 33025515061dSMel Gorman * processes to block on log_wait_commit(). 33035515061dSMel Gorman */ 33045515061dSMel Gorman if (current->flags & PF_KTHREAD) 330550694c28SMel Gorman goto out; 330650694c28SMel Gorman 330750694c28SMel Gorman /* 330850694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 330950694c28SMel Gorman * It should return quickly so it can exit and free its memory 331050694c28SMel Gorman */ 331150694c28SMel Gorman if (fatal_signal_pending(current)) 331250694c28SMel Gorman goto out; 33135515061dSMel Gorman 3314675becceSMel Gorman /* 3315675becceSMel Gorman * Check if the pfmemalloc reserves are ok by finding the first node 3316675becceSMel Gorman * with a usable ZONE_NORMAL or lower zone. The expectation is that 3317675becceSMel Gorman * GFP_KERNEL will be required for allocating network buffers when 3318675becceSMel Gorman * swapping over the network so ZONE_HIGHMEM is unusable. 3319675becceSMel Gorman * 3320675becceSMel Gorman * Throttling is based on the first usable node and throttled processes 3321675becceSMel Gorman * wait on a queue until kswapd makes progress and wakes them. There 3322675becceSMel Gorman * is an affinity then between processes waking up and where reclaim 3323675becceSMel Gorman * progress has been made assuming the process wakes on the same node. 3324675becceSMel Gorman * More importantly, processes running on remote nodes will not compete 3325675becceSMel Gorman * for remote pfmemalloc reserves and processes on different nodes 3326675becceSMel Gorman * should make reasonable progress. 3327675becceSMel Gorman */ 3328675becceSMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 332917636faaSMichael S. Tsirkin gfp_zone(gfp_mask), nodemask) { 3330675becceSMel Gorman if (zone_idx(zone) > ZONE_NORMAL) 3331675becceSMel Gorman continue; 3332675becceSMel Gorman 3333675becceSMel Gorman /* Throttle based on the first usable node */ 33345515061dSMel Gorman pgdat = zone->zone_pgdat; 3335c73322d0SJohannes Weiner if (allow_direct_reclaim(pgdat)) 333650694c28SMel Gorman goto out; 3337675becceSMel Gorman break; 3338675becceSMel Gorman } 3339675becceSMel Gorman 3340675becceSMel Gorman /* If no zone was usable by the allocation flags then do not throttle */ 3341675becceSMel Gorman if (!pgdat) 3342675becceSMel Gorman goto out; 33435515061dSMel Gorman 334468243e76SMel Gorman /* Account for the throttling */ 334568243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 334668243e76SMel Gorman 33475515061dSMel Gorman /* 33485515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 33495515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 33505515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 33515515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 33525515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 33535515061dSMel Gorman * second before continuing. 33545515061dSMel Gorman */ 33555515061dSMel Gorman if (!(gfp_mask & __GFP_FS)) { 33565515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 3357c73322d0SJohannes Weiner allow_direct_reclaim(pgdat), HZ); 335850694c28SMel Gorman 335950694c28SMel Gorman goto check_pending; 33605515061dSMel Gorman } 33615515061dSMel Gorman 33625515061dSMel Gorman /* Throttle until kswapd wakes the process */ 33635515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 3364c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)); 336550694c28SMel Gorman 336650694c28SMel Gorman check_pending: 336750694c28SMel Gorman if (fatal_signal_pending(current)) 336850694c28SMel Gorman return true; 336950694c28SMel Gorman 337050694c28SMel Gorman out: 337150694c28SMel Gorman return false; 33725515061dSMel Gorman } 33735515061dSMel Gorman 3374dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 3375327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 337666e1707bSBalbir Singh { 337733906bc5SMel Gorman unsigned long nr_reclaimed; 337866e1707bSBalbir Singh struct scan_control sc = { 337922fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3380f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 3381b2e18757SMel Gorman .reclaim_idx = gfp_zone(gfp_mask), 3382ee814fe2SJohannes Weiner .order = order, 3383ee814fe2SJohannes Weiner .nodemask = nodemask, 3384ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 3385ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3386a6dc60f8SJohannes Weiner .may_unmap = 1, 33872e2e4259SKOSAKI Motohiro .may_swap = 1, 338866e1707bSBalbir Singh }; 338966e1707bSBalbir Singh 33905515061dSMel Gorman /* 3391bb451fdfSGreg Thelen * scan_control uses s8 fields for order, priority, and reclaim_idx. 3392bb451fdfSGreg Thelen * Confirm they are large enough for max values. 3393bb451fdfSGreg Thelen */ 3394bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_ORDER > S8_MAX); 3395bb451fdfSGreg Thelen BUILD_BUG_ON(DEF_PRIORITY > S8_MAX); 3396bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX); 3397bb451fdfSGreg Thelen 3398bb451fdfSGreg Thelen /* 339950694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 340050694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 340150694c28SMel Gorman * point. 34025515061dSMel Gorman */ 3403f2f43e56SNick Desaulniers if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask)) 34045515061dSMel Gorman return 1; 34055515061dSMel Gorman 34061732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 34073481c37fSYafang Shao trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask); 340833906bc5SMel Gorman 34093115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 341033906bc5SMel Gorman 341133906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 34121732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 341333906bc5SMel Gorman 341433906bc5SMel Gorman return nr_reclaimed; 341566e1707bSBalbir Singh } 341666e1707bSBalbir Singh 3417c255a458SAndrew Morton #ifdef CONFIG_MEMCG 341866e1707bSBalbir Singh 3419d2e5fb92SMichal Hocko /* Only used by soft limit reclaim. Do not reuse for anything else. */ 3420a9dd0a83SMel Gorman unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, 34214e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 3422ef8f2327SMel Gorman pg_data_t *pgdat, 34230ae5e89cSYing Han unsigned long *nr_scanned) 34244e416953SBalbir Singh { 3425afaf07a6SJohannes Weiner struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); 34264e416953SBalbir Singh struct scan_control sc = { 3427b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3428ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 34294e416953SBalbir Singh .may_writepage = !laptop_mode, 34304e416953SBalbir Singh .may_unmap = 1, 3431b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 34324e416953SBalbir Singh .may_swap = !noswap, 34334e416953SBalbir Singh }; 34340ae5e89cSYing Han 3435d2e5fb92SMichal Hocko WARN_ON_ONCE(!current->reclaim_state); 3436d2e5fb92SMichal Hocko 34374e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 34384e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 3439bdce6d9eSKOSAKI Motohiro 34409e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 34413481c37fSYafang Shao sc.gfp_mask); 3442bdce6d9eSKOSAKI Motohiro 34434e416953SBalbir Singh /* 34444e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 34454e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 3446a9dd0a83SMel Gorman * if we don't reclaim here, the shrink_node from balance_pgdat 34474e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 34484e416953SBalbir Singh * the priority and make it zero. 34494e416953SBalbir Singh */ 3450afaf07a6SJohannes Weiner shrink_lruvec(lruvec, &sc); 3451bdce6d9eSKOSAKI Motohiro 3452bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 3453bdce6d9eSKOSAKI Motohiro 34540ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 34550308f7cfSYafang Shao 34564e416953SBalbir Singh return sc.nr_reclaimed; 34574e416953SBalbir Singh } 34584e416953SBalbir Singh 345972835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 3460b70a2a21SJohannes Weiner unsigned long nr_pages, 34618c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 3462b70a2a21SJohannes Weiner bool may_swap) 346366e1707bSBalbir Singh { 3464bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 3465499118e9SVlastimil Babka unsigned int noreclaim_flag; 346666e1707bSBalbir Singh struct scan_control sc = { 3467b70a2a21SJohannes Weiner .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 34687dea19f9SMichal Hocko .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | 3469ee814fe2SJohannes Weiner (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 3470b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 3471ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 3472ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 347366e1707bSBalbir Singh .may_writepage = !laptop_mode, 3474a6dc60f8SJohannes Weiner .may_unmap = 1, 3475b70a2a21SJohannes Weiner .may_swap = may_swap, 3476a09ed5e0SYing Han }; 3477fa40d1eeSShakeel Butt /* 3478fa40d1eeSShakeel Butt * Traverse the ZONELIST_FALLBACK zonelist of the current node to put 3479fa40d1eeSShakeel Butt * equal pressure on all the nodes. This is based on the assumption that 3480fa40d1eeSShakeel Butt * the reclaim does not bail out early. 3481fa40d1eeSShakeel Butt */ 3482fa40d1eeSShakeel Butt struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 348366e1707bSBalbir Singh 34841732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 34853481c37fSYafang Shao trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask); 3486499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 3487eb414681SJohannes Weiner 34883115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3489eb414681SJohannes Weiner 3490499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 3491bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 34921732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 3493bdce6d9eSKOSAKI Motohiro 3494bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 349566e1707bSBalbir Singh } 349666e1707bSBalbir Singh #endif 349766e1707bSBalbir Singh 34981d82de61SMel Gorman static void age_active_anon(struct pglist_data *pgdat, 3499ef8f2327SMel Gorman struct scan_control *sc) 3500f16015fbSJohannes Weiner { 3501b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 3502b91ac374SJohannes Weiner struct lruvec *lruvec; 3503b95a2f2dSJohannes Weiner 3504b95a2f2dSJohannes Weiner if (!total_swap_pages) 3505b95a2f2dSJohannes Weiner return; 3506b95a2f2dSJohannes Weiner 3507b91ac374SJohannes Weiner lruvec = mem_cgroup_lruvec(NULL, pgdat); 3508b91ac374SJohannes Weiner if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON)) 3509b91ac374SJohannes Weiner return; 3510b91ac374SJohannes Weiner 3511b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 3512b95a2f2dSJohannes Weiner do { 3513b91ac374SJohannes Weiner lruvec = mem_cgroup_lruvec(memcg, pgdat); 35141a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 35159e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 3516b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 3517b95a2f2dSJohannes Weiner } while (memcg); 3518f16015fbSJohannes Weiner } 3519f16015fbSJohannes Weiner 352097a225e6SJoonsoo Kim static bool pgdat_watermark_boosted(pg_data_t *pgdat, int highest_zoneidx) 35211c30844dSMel Gorman { 35221c30844dSMel Gorman int i; 35231c30844dSMel Gorman struct zone *zone; 35241c30844dSMel Gorman 35251c30844dSMel Gorman /* 35261c30844dSMel Gorman * Check for watermark boosts top-down as the higher zones 35271c30844dSMel Gorman * are more likely to be boosted. Both watermarks and boosts 35281eba09c1SRandy Dunlap * should not be checked at the same time as reclaim would 35291c30844dSMel Gorman * start prematurely when there is no boosting and a lower 35301c30844dSMel Gorman * zone is balanced. 35311c30844dSMel Gorman */ 353297a225e6SJoonsoo Kim for (i = highest_zoneidx; i >= 0; i--) { 35331c30844dSMel Gorman zone = pgdat->node_zones + i; 35341c30844dSMel Gorman if (!managed_zone(zone)) 35351c30844dSMel Gorman continue; 35361c30844dSMel Gorman 35371c30844dSMel Gorman if (zone->watermark_boost) 35381c30844dSMel Gorman return true; 35391c30844dSMel Gorman } 35401c30844dSMel Gorman 35411c30844dSMel Gorman return false; 35421c30844dSMel Gorman } 35431c30844dSMel Gorman 3544e716f2ebSMel Gorman /* 3545e716f2ebSMel Gorman * Returns true if there is an eligible zone balanced for the request order 354697a225e6SJoonsoo Kim * and highest_zoneidx 3547e716f2ebSMel Gorman */ 354897a225e6SJoonsoo Kim static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx) 354960cefed4SJohannes Weiner { 3550e716f2ebSMel Gorman int i; 3551e716f2ebSMel Gorman unsigned long mark = -1; 3552e716f2ebSMel Gorman struct zone *zone; 355360cefed4SJohannes Weiner 35541c30844dSMel Gorman /* 35551c30844dSMel Gorman * Check watermarks bottom-up as lower zones are more likely to 35561c30844dSMel Gorman * meet watermarks. 35571c30844dSMel Gorman */ 355897a225e6SJoonsoo Kim for (i = 0; i <= highest_zoneidx; i++) { 3559e716f2ebSMel Gorman zone = pgdat->node_zones + i; 35606256c6b4SMel Gorman 3561e716f2ebSMel Gorman if (!managed_zone(zone)) 3562e716f2ebSMel Gorman continue; 3563e716f2ebSMel Gorman 3564e716f2ebSMel Gorman mark = high_wmark_pages(zone); 356597a225e6SJoonsoo Kim if (zone_watermark_ok_safe(zone, order, mark, highest_zoneidx)) 35666256c6b4SMel Gorman return true; 356760cefed4SJohannes Weiner } 356860cefed4SJohannes Weiner 3569e716f2ebSMel Gorman /* 357097a225e6SJoonsoo Kim * If a node has no populated zone within highest_zoneidx, it does not 3571e716f2ebSMel Gorman * need balancing by definition. This can happen if a zone-restricted 3572e716f2ebSMel Gorman * allocation tries to wake a remote kswapd. 3573e716f2ebSMel Gorman */ 3574e716f2ebSMel Gorman if (mark == -1) 3575e716f2ebSMel Gorman return true; 3576e716f2ebSMel Gorman 3577e716f2ebSMel Gorman return false; 3578e716f2ebSMel Gorman } 3579e716f2ebSMel Gorman 3580631b6e08SMel Gorman /* Clear pgdat state for congested, dirty or under writeback. */ 3581631b6e08SMel Gorman static void clear_pgdat_congested(pg_data_t *pgdat) 3582631b6e08SMel Gorman { 35831b05117dSJohannes Weiner struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat); 35841b05117dSJohannes Weiner 35851b05117dSJohannes Weiner clear_bit(LRUVEC_CONGESTED, &lruvec->flags); 3586631b6e08SMel Gorman clear_bit(PGDAT_DIRTY, &pgdat->flags); 3587631b6e08SMel Gorman clear_bit(PGDAT_WRITEBACK, &pgdat->flags); 3588631b6e08SMel Gorman } 3589631b6e08SMel Gorman 35901741c877SMel Gorman /* 35915515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 35925515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 35935515061dSMel Gorman * 35945515061dSMel Gorman * Returns true if kswapd is ready to sleep 35955515061dSMel Gorman */ 359697a225e6SJoonsoo Kim static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, 359797a225e6SJoonsoo Kim int highest_zoneidx) 3598f50de2d3SMel Gorman { 35995515061dSMel Gorman /* 36009e5e3661SVlastimil Babka * The throttled processes are normally woken up in balance_pgdat() as 3601c73322d0SJohannes Weiner * soon as allow_direct_reclaim() is true. But there is a potential 36029e5e3661SVlastimil Babka * race between when kswapd checks the watermarks and a process gets 36039e5e3661SVlastimil Babka * throttled. There is also a potential race if processes get 36049e5e3661SVlastimil Babka * throttled, kswapd wakes, a large process exits thereby balancing the 36059e5e3661SVlastimil Babka * zones, which causes kswapd to exit balance_pgdat() before reaching 36069e5e3661SVlastimil Babka * the wake up checks. If kswapd is going to sleep, no process should 36079e5e3661SVlastimil Babka * be sleeping on pfmemalloc_wait, so wake them now if necessary. If 36089e5e3661SVlastimil Babka * the wake up is premature, processes will wake kswapd and get 36099e5e3661SVlastimil Babka * throttled again. The difference from wake ups in balance_pgdat() is 36109e5e3661SVlastimil Babka * that here we are under prepare_to_wait(). 36115515061dSMel Gorman */ 36129e5e3661SVlastimil Babka if (waitqueue_active(&pgdat->pfmemalloc_wait)) 36139e5e3661SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 3614f50de2d3SMel Gorman 3615c73322d0SJohannes Weiner /* Hopeless node, leave it to direct reclaim */ 3616c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3617c73322d0SJohannes Weiner return true; 3618c73322d0SJohannes Weiner 361997a225e6SJoonsoo Kim if (pgdat_balanced(pgdat, order, highest_zoneidx)) { 3620631b6e08SMel Gorman clear_pgdat_congested(pgdat); 3621333b0a45SShantanu Goel return true; 36221d82de61SMel Gorman } 36231d82de61SMel Gorman 3624333b0a45SShantanu Goel return false; 3625f50de2d3SMel Gorman } 3626f50de2d3SMel Gorman 36271da177e4SLinus Torvalds /* 36281d82de61SMel Gorman * kswapd shrinks a node of pages that are at or below the highest usable 36291d82de61SMel Gorman * zone that is currently unbalanced. 3630b8e83b94SMel Gorman * 3631b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 3632283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 3633283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 363475485363SMel Gorman */ 36351d82de61SMel Gorman static bool kswapd_shrink_node(pg_data_t *pgdat, 3636accf6242SVlastimil Babka struct scan_control *sc) 363775485363SMel Gorman { 36381d82de61SMel Gorman struct zone *zone; 36391d82de61SMel Gorman int z; 364075485363SMel Gorman 36411d82de61SMel Gorman /* Reclaim a number of pages proportional to the number of zones */ 36421d82de61SMel Gorman sc->nr_to_reclaim = 0; 3643970a39a3SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 36441d82de61SMel Gorman zone = pgdat->node_zones + z; 36456aa303deSMel Gorman if (!managed_zone(zone)) 36461d82de61SMel Gorman continue; 36477c954f6dSMel Gorman 36481d82de61SMel Gorman sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); 36497c954f6dSMel Gorman } 36507c954f6dSMel Gorman 36511d82de61SMel Gorman /* 36521d82de61SMel Gorman * Historically care was taken to put equal pressure on all zones but 36531d82de61SMel Gorman * now pressure is applied based on node LRU order. 36541d82de61SMel Gorman */ 3655970a39a3SMel Gorman shrink_node(pgdat, sc); 36561d82de61SMel Gorman 36571d82de61SMel Gorman /* 36581d82de61SMel Gorman * Fragmentation may mean that the system cannot be rebalanced for 36591d82de61SMel Gorman * high-order allocations. If twice the allocation size has been 36601d82de61SMel Gorman * reclaimed then recheck watermarks only at order-0 to prevent 36611d82de61SMel Gorman * excessive reclaim. Assume that a process requested a high-order 36621d82de61SMel Gorman * can direct reclaim/compact. 36631d82de61SMel Gorman */ 36649861a62cSVlastimil Babka if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order)) 36651d82de61SMel Gorman sc->order = 0; 36661d82de61SMel Gorman 3667b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 366875485363SMel Gorman } 366975485363SMel Gorman 367075485363SMel Gorman /* 36711d82de61SMel Gorman * For kswapd, balance_pgdat() will reclaim pages across a node from zones 36721d82de61SMel Gorman * that are eligible for use by the caller until at least one zone is 36731d82de61SMel Gorman * balanced. 36741da177e4SLinus Torvalds * 36751d82de61SMel Gorman * Returns the order kswapd finished reclaiming at. 36761da177e4SLinus Torvalds * 36771da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 367841858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 36798bb4e7a2SWei Yang * found to have free_pages <= high_wmark_pages(zone), any page in that zone 36801d82de61SMel Gorman * or lower is eligible for reclaim until at least one usable zone is 36811d82de61SMel Gorman * balanced. 36821da177e4SLinus Torvalds */ 368397a225e6SJoonsoo Kim static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx) 36841da177e4SLinus Torvalds { 36851da177e4SLinus Torvalds int i; 36860608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 36870608f43dSAndrew Morton unsigned long nr_soft_scanned; 3688eb414681SJohannes Weiner unsigned long pflags; 36891c30844dSMel Gorman unsigned long nr_boost_reclaim; 36901c30844dSMel Gorman unsigned long zone_boosts[MAX_NR_ZONES] = { 0, }; 36911c30844dSMel Gorman bool boosted; 36921d82de61SMel Gorman struct zone *zone; 3693179e9639SAndrew Morton struct scan_control sc = { 3694179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 3695ee814fe2SJohannes Weiner .order = order, 3696a6dc60f8SJohannes Weiner .may_unmap = 1, 3697179e9639SAndrew Morton }; 369893781325SOmar Sandoval 36991732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 3700eb414681SJohannes Weiner psi_memstall_enter(&pflags); 370193781325SOmar Sandoval __fs_reclaim_acquire(); 370293781325SOmar Sandoval 3703f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 37041da177e4SLinus Torvalds 37051c30844dSMel Gorman /* 37061c30844dSMel Gorman * Account for the reclaim boost. Note that the zone boost is left in 37071c30844dSMel Gorman * place so that parallel allocations that are near the watermark will 37081c30844dSMel Gorman * stall or direct reclaim until kswapd is finished. 37091c30844dSMel Gorman */ 37101c30844dSMel Gorman nr_boost_reclaim = 0; 371197a225e6SJoonsoo Kim for (i = 0; i <= highest_zoneidx; i++) { 37121c30844dSMel Gorman zone = pgdat->node_zones + i; 37131c30844dSMel Gorman if (!managed_zone(zone)) 37141c30844dSMel Gorman continue; 37151c30844dSMel Gorman 37161c30844dSMel Gorman nr_boost_reclaim += zone->watermark_boost; 37171c30844dSMel Gorman zone_boosts[i] = zone->watermark_boost; 37181c30844dSMel Gorman } 37191c30844dSMel Gorman boosted = nr_boost_reclaim; 37201c30844dSMel Gorman 37211c30844dSMel Gorman restart: 37221c30844dSMel Gorman sc.priority = DEF_PRIORITY; 37239e3b2f8cSKonstantin Khlebnikov do { 3724c73322d0SJohannes Weiner unsigned long nr_reclaimed = sc.nr_reclaimed; 3725b8e83b94SMel Gorman bool raise_priority = true; 37261c30844dSMel Gorman bool balanced; 372793781325SOmar Sandoval bool ret; 3728b8e83b94SMel Gorman 372997a225e6SJoonsoo Kim sc.reclaim_idx = highest_zoneidx; 37301da177e4SLinus Torvalds 373186c79f6bSMel Gorman /* 373284c7a777SMel Gorman * If the number of buffer_heads exceeds the maximum allowed 373384c7a777SMel Gorman * then consider reclaiming from all zones. This has a dual 373484c7a777SMel Gorman * purpose -- on 64-bit systems it is expected that 373584c7a777SMel Gorman * buffer_heads are stripped during active rotation. On 32-bit 373684c7a777SMel Gorman * systems, highmem pages can pin lowmem memory and shrinking 373784c7a777SMel Gorman * buffers can relieve lowmem pressure. Reclaim may still not 373884c7a777SMel Gorman * go ahead if all eligible zones for the original allocation 373984c7a777SMel Gorman * request are balanced to avoid excessive reclaim from kswapd. 374086c79f6bSMel Gorman */ 374186c79f6bSMel Gorman if (buffer_heads_over_limit) { 374286c79f6bSMel Gorman for (i = MAX_NR_ZONES - 1; i >= 0; i--) { 374386c79f6bSMel Gorman zone = pgdat->node_zones + i; 37446aa303deSMel Gorman if (!managed_zone(zone)) 374586c79f6bSMel Gorman continue; 374686c79f6bSMel Gorman 3747970a39a3SMel Gorman sc.reclaim_idx = i; 374886c79f6bSMel Gorman break; 374986c79f6bSMel Gorman } 375086c79f6bSMel Gorman } 375186c79f6bSMel Gorman 375286c79f6bSMel Gorman /* 37531c30844dSMel Gorman * If the pgdat is imbalanced then ignore boosting and preserve 37541c30844dSMel Gorman * the watermarks for a later time and restart. Note that the 37551c30844dSMel Gorman * zone watermarks will be still reset at the end of balancing 37561c30844dSMel Gorman * on the grounds that the normal reclaim should be enough to 37571c30844dSMel Gorman * re-evaluate if boosting is required when kswapd next wakes. 375886c79f6bSMel Gorman */ 375997a225e6SJoonsoo Kim balanced = pgdat_balanced(pgdat, sc.order, highest_zoneidx); 37601c30844dSMel Gorman if (!balanced && nr_boost_reclaim) { 37611c30844dSMel Gorman nr_boost_reclaim = 0; 37621c30844dSMel Gorman goto restart; 37631c30844dSMel Gorman } 37641c30844dSMel Gorman 37651c30844dSMel Gorman /* 37661c30844dSMel Gorman * If boosting is not active then only reclaim if there are no 37671c30844dSMel Gorman * eligible zones. Note that sc.reclaim_idx is not used as 37681c30844dSMel Gorman * buffer_heads_over_limit may have adjusted it. 37691c30844dSMel Gorman */ 37701c30844dSMel Gorman if (!nr_boost_reclaim && balanced) 37711da177e4SLinus Torvalds goto out; 3772e1dbeda6SAndrew Morton 37731c30844dSMel Gorman /* Limit the priority of boosting to avoid reclaim writeback */ 37741c30844dSMel Gorman if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2) 37751c30844dSMel Gorman raise_priority = false; 37761c30844dSMel Gorman 37771c30844dSMel Gorman /* 37781c30844dSMel Gorman * Do not writeback or swap pages for boosted reclaim. The 37791c30844dSMel Gorman * intent is to relieve pressure not issue sub-optimal IO 37801c30844dSMel Gorman * from reclaim context. If no pages are reclaimed, the 37811c30844dSMel Gorman * reclaim will be aborted. 37821c30844dSMel Gorman */ 37831c30844dSMel Gorman sc.may_writepage = !laptop_mode && !nr_boost_reclaim; 37841c30844dSMel Gorman sc.may_swap = !nr_boost_reclaim; 37851c30844dSMel Gorman 37861da177e4SLinus Torvalds /* 37871d82de61SMel Gorman * Do some background aging of the anon list, to give 37881d82de61SMel Gorman * pages a chance to be referenced before reclaiming. All 37891d82de61SMel Gorman * pages are rotated regardless of classzone as this is 37901d82de61SMel Gorman * about consistent aging. 37911d82de61SMel Gorman */ 3792ef8f2327SMel Gorman age_active_anon(pgdat, &sc); 37931d82de61SMel Gorman 37941d82de61SMel Gorman /* 3795b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 3796b7ea3c41SMel Gorman * even in laptop mode. 3797b7ea3c41SMel Gorman */ 3798047d72c3SJohannes Weiner if (sc.priority < DEF_PRIORITY - 2) 3799b7ea3c41SMel Gorman sc.may_writepage = 1; 3800b7ea3c41SMel Gorman 38011d82de61SMel Gorman /* Call soft limit reclaim before calling shrink_node. */ 38021da177e4SLinus Torvalds sc.nr_scanned = 0; 38030608f43dSAndrew Morton nr_soft_scanned = 0; 3804ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, 38051d82de61SMel Gorman sc.gfp_mask, &nr_soft_scanned); 38060608f43dSAndrew Morton sc.nr_reclaimed += nr_soft_reclaimed; 38070608f43dSAndrew Morton 380832a4330dSRik van Riel /* 38091d82de61SMel Gorman * There should be no need to raise the scanning priority if 38101d82de61SMel Gorman * enough pages are already being scanned that that high 38111d82de61SMel Gorman * watermark would be met at 100% efficiency. 381232a4330dSRik van Riel */ 3813970a39a3SMel Gorman if (kswapd_shrink_node(pgdat, &sc)) 3814b8e83b94SMel Gorman raise_priority = false; 3815d7868daeSMel Gorman 38165515061dSMel Gorman /* 38175515061dSMel Gorman * If the low watermark is met there is no need for processes 38185515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 38195515061dSMel Gorman * able to safely make forward progress. Wake them 38205515061dSMel Gorman */ 38215515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 3822c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)) 3823cfc51155SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 38245515061dSMel Gorman 3825b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 382693781325SOmar Sandoval __fs_reclaim_release(); 382793781325SOmar Sandoval ret = try_to_freeze(); 382893781325SOmar Sandoval __fs_reclaim_acquire(); 382993781325SOmar Sandoval if (ret || kthread_should_stop()) 3830b8e83b94SMel Gorman break; 3831b8e83b94SMel Gorman 3832b8e83b94SMel Gorman /* 3833b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 3834b8e83b94SMel Gorman * progress in reclaiming pages 3835b8e83b94SMel Gorman */ 3836c73322d0SJohannes Weiner nr_reclaimed = sc.nr_reclaimed - nr_reclaimed; 38371c30844dSMel Gorman nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed); 38381c30844dSMel Gorman 38391c30844dSMel Gorman /* 38401c30844dSMel Gorman * If reclaim made no progress for a boost, stop reclaim as 38411c30844dSMel Gorman * IO cannot be queued and it could be an infinite loop in 38421c30844dSMel Gorman * extreme circumstances. 38431c30844dSMel Gorman */ 38441c30844dSMel Gorman if (nr_boost_reclaim && !nr_reclaimed) 38451c30844dSMel Gorman break; 38461c30844dSMel Gorman 3847c73322d0SJohannes Weiner if (raise_priority || !nr_reclaimed) 3848b8e83b94SMel Gorman sc.priority--; 38491d82de61SMel Gorman } while (sc.priority >= 1); 38501da177e4SLinus Torvalds 3851c73322d0SJohannes Weiner if (!sc.nr_reclaimed) 3852c73322d0SJohannes Weiner pgdat->kswapd_failures++; 3853c73322d0SJohannes Weiner 3854b8e83b94SMel Gorman out: 38551c30844dSMel Gorman /* If reclaim was boosted, account for the reclaim done in this pass */ 38561c30844dSMel Gorman if (boosted) { 38571c30844dSMel Gorman unsigned long flags; 38581c30844dSMel Gorman 385997a225e6SJoonsoo Kim for (i = 0; i <= highest_zoneidx; i++) { 38601c30844dSMel Gorman if (!zone_boosts[i]) 38611c30844dSMel Gorman continue; 38621c30844dSMel Gorman 38631c30844dSMel Gorman /* Increments are under the zone lock */ 38641c30844dSMel Gorman zone = pgdat->node_zones + i; 38651c30844dSMel Gorman spin_lock_irqsave(&zone->lock, flags); 38661c30844dSMel Gorman zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]); 38671c30844dSMel Gorman spin_unlock_irqrestore(&zone->lock, flags); 38681c30844dSMel Gorman } 38691c30844dSMel Gorman 38701c30844dSMel Gorman /* 38711c30844dSMel Gorman * As there is now likely space, wakeup kcompact to defragment 38721c30844dSMel Gorman * pageblocks. 38731c30844dSMel Gorman */ 387497a225e6SJoonsoo Kim wakeup_kcompactd(pgdat, pageblock_order, highest_zoneidx); 38751c30844dSMel Gorman } 38761c30844dSMel Gorman 38772a2e4885SJohannes Weiner snapshot_refaults(NULL, pgdat); 387893781325SOmar Sandoval __fs_reclaim_release(); 3879eb414681SJohannes Weiner psi_memstall_leave(&pflags); 38801732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 3881e5ca8071SYafang Shao 38820abdee2bSMel Gorman /* 38831d82de61SMel Gorman * Return the order kswapd stopped reclaiming at as 38841d82de61SMel Gorman * prepare_kswapd_sleep() takes it into account. If another caller 38851d82de61SMel Gorman * entered the allocator slow path while kswapd was awake, order will 38861d82de61SMel Gorman * remain at the higher level. 38870abdee2bSMel Gorman */ 38881d82de61SMel Gorman return sc.order; 38891da177e4SLinus Torvalds } 38901da177e4SLinus Torvalds 3891e716f2ebSMel Gorman /* 389297a225e6SJoonsoo Kim * The pgdat->kswapd_highest_zoneidx is used to pass the highest zone index to 389397a225e6SJoonsoo Kim * be reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is 389497a225e6SJoonsoo Kim * not a valid index then either kswapd runs for first time or kswapd couldn't 389597a225e6SJoonsoo Kim * sleep after previous reclaim attempt (node is still unbalanced). In that 389697a225e6SJoonsoo Kim * case return the zone index of the previous kswapd reclaim cycle. 3897e716f2ebSMel Gorman */ 389897a225e6SJoonsoo Kim static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat, 389997a225e6SJoonsoo Kim enum zone_type prev_highest_zoneidx) 3900e716f2ebSMel Gorman { 390197a225e6SJoonsoo Kim enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); 39025644e1fbSQian Cai 390397a225e6SJoonsoo Kim return curr_idx == MAX_NR_ZONES ? prev_highest_zoneidx : curr_idx; 3904e716f2ebSMel Gorman } 3905e716f2ebSMel Gorman 390638087d9bSMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, 390797a225e6SJoonsoo Kim unsigned int highest_zoneidx) 3908f0bc0a60SKOSAKI Motohiro { 3909f0bc0a60SKOSAKI Motohiro long remaining = 0; 3910f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 3911f0bc0a60SKOSAKI Motohiro 3912f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 3913f0bc0a60SKOSAKI Motohiro return; 3914f0bc0a60SKOSAKI Motohiro 3915f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3916f0bc0a60SKOSAKI Motohiro 3917333b0a45SShantanu Goel /* 3918333b0a45SShantanu Goel * Try to sleep for a short interval. Note that kcompactd will only be 3919333b0a45SShantanu Goel * woken if it is possible to sleep for a short interval. This is 3920333b0a45SShantanu Goel * deliberate on the assumption that if reclaim cannot keep an 3921333b0a45SShantanu Goel * eligible zone balanced that it's also unlikely that compaction will 3922333b0a45SShantanu Goel * succeed. 3923333b0a45SShantanu Goel */ 392497a225e6SJoonsoo Kim if (prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { 3925fd901c95SVlastimil Babka /* 3926fd901c95SVlastimil Babka * Compaction records what page blocks it recently failed to 3927fd901c95SVlastimil Babka * isolate pages from and skips them in the future scanning. 3928fd901c95SVlastimil Babka * When kswapd is going to sleep, it is reasonable to assume 3929fd901c95SVlastimil Babka * that pages and compaction may succeed so reset the cache. 3930fd901c95SVlastimil Babka */ 3931fd901c95SVlastimil Babka reset_isolation_suitable(pgdat); 3932fd901c95SVlastimil Babka 3933fd901c95SVlastimil Babka /* 3934fd901c95SVlastimil Babka * We have freed the memory, now we should compact it to make 3935fd901c95SVlastimil Babka * allocation of the requested order possible. 3936fd901c95SVlastimil Babka */ 393797a225e6SJoonsoo Kim wakeup_kcompactd(pgdat, alloc_order, highest_zoneidx); 3938fd901c95SVlastimil Babka 3939f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 394038087d9bSMel Gorman 394138087d9bSMel Gorman /* 394297a225e6SJoonsoo Kim * If woken prematurely then reset kswapd_highest_zoneidx and 394338087d9bSMel Gorman * order. The values will either be from a wakeup request or 394438087d9bSMel Gorman * the previous request that slept prematurely. 394538087d9bSMel Gorman */ 394638087d9bSMel Gorman if (remaining) { 394797a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, 394897a225e6SJoonsoo Kim kswapd_highest_zoneidx(pgdat, 394997a225e6SJoonsoo Kim highest_zoneidx)); 39505644e1fbSQian Cai 39515644e1fbSQian Cai if (READ_ONCE(pgdat->kswapd_order) < reclaim_order) 39525644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, reclaim_order); 395338087d9bSMel Gorman } 395438087d9bSMel Gorman 3955f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3956f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3957f0bc0a60SKOSAKI Motohiro } 3958f0bc0a60SKOSAKI Motohiro 3959f0bc0a60SKOSAKI Motohiro /* 3960f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 3961f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 3962f0bc0a60SKOSAKI Motohiro */ 3963d9f21d42SMel Gorman if (!remaining && 396497a225e6SJoonsoo Kim prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { 3965f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 3966f0bc0a60SKOSAKI Motohiro 3967f0bc0a60SKOSAKI Motohiro /* 3968f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 3969f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 3970f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 3971f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 3972f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 3973f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 3974f0bc0a60SKOSAKI Motohiro */ 3975f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 39761c7e7f6cSAaditya Kumar 39771c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 3978f0bc0a60SKOSAKI Motohiro schedule(); 39791c7e7f6cSAaditya Kumar 3980f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 3981f0bc0a60SKOSAKI Motohiro } else { 3982f0bc0a60SKOSAKI Motohiro if (remaining) 3983f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 3984f0bc0a60SKOSAKI Motohiro else 3985f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 3986f0bc0a60SKOSAKI Motohiro } 3987f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3988f0bc0a60SKOSAKI Motohiro } 3989f0bc0a60SKOSAKI Motohiro 39901da177e4SLinus Torvalds /* 39911da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 39921da177e4SLinus Torvalds * from the init process. 39931da177e4SLinus Torvalds * 39941da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 39951da177e4SLinus Torvalds * free memory available even if there is no other activity 39961da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 39971da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 39981da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 39991da177e4SLinus Torvalds * 40001da177e4SLinus Torvalds * If there are applications that are active memory-allocators 40011da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 40021da177e4SLinus Torvalds */ 40031da177e4SLinus Torvalds static int kswapd(void *p) 40041da177e4SLinus Torvalds { 4005e716f2ebSMel Gorman unsigned int alloc_order, reclaim_order; 400697a225e6SJoonsoo Kim unsigned int highest_zoneidx = MAX_NR_ZONES - 1; 40071da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 40081da177e4SLinus Torvalds struct task_struct *tsk = current; 4009a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 40101da177e4SLinus Torvalds 4011174596a0SRusty Russell if (!cpumask_empty(cpumask)) 4012c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 40131da177e4SLinus Torvalds 40141da177e4SLinus Torvalds /* 40151da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 40161da177e4SLinus Torvalds * and that if we need more memory we should get access to it 40171da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 40181da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 40191da177e4SLinus Torvalds * 40201da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 40211da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 40221da177e4SLinus Torvalds * page out something else, and this flag essentially protects 40231da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 40241da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 40251da177e4SLinus Torvalds */ 4026930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 402783144186SRafael J. Wysocki set_freezable(); 40281da177e4SLinus Torvalds 40295644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, 0); 403097a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); 40311da177e4SLinus Torvalds for ( ; ; ) { 40326f6313d4SJeff Liu bool ret; 40333e1d1d28SChristoph Lameter 40345644e1fbSQian Cai alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order); 403597a225e6SJoonsoo Kim highest_zoneidx = kswapd_highest_zoneidx(pgdat, 403697a225e6SJoonsoo Kim highest_zoneidx); 4037e716f2ebSMel Gorman 403838087d9bSMel Gorman kswapd_try_sleep: 403938087d9bSMel Gorman kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, 404097a225e6SJoonsoo Kim highest_zoneidx); 4041215ddd66SMel Gorman 404297a225e6SJoonsoo Kim /* Read the new order and highest_zoneidx */ 40432b47a24cSLukas Bulwahn alloc_order = READ_ONCE(pgdat->kswapd_order); 404497a225e6SJoonsoo Kim highest_zoneidx = kswapd_highest_zoneidx(pgdat, 404597a225e6SJoonsoo Kim highest_zoneidx); 40465644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, 0); 404797a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); 40481da177e4SLinus Torvalds 40498fe23e05SDavid Rientjes ret = try_to_freeze(); 40508fe23e05SDavid Rientjes if (kthread_should_stop()) 40518fe23e05SDavid Rientjes break; 40528fe23e05SDavid Rientjes 40538fe23e05SDavid Rientjes /* 40548fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 40558fe23e05SDavid Rientjes * after returning from the refrigerator 4056b1296cc4SRafael J. Wysocki */ 405738087d9bSMel Gorman if (ret) 405838087d9bSMel Gorman continue; 40591d82de61SMel Gorman 406038087d9bSMel Gorman /* 406138087d9bSMel Gorman * Reclaim begins at the requested order but if a high-order 406238087d9bSMel Gorman * reclaim fails then kswapd falls back to reclaiming for 406338087d9bSMel Gorman * order-0. If that happens, kswapd will consider sleeping 406438087d9bSMel Gorman * for the order it finished reclaiming at (reclaim_order) 406538087d9bSMel Gorman * but kcompactd is woken to compact for the original 406638087d9bSMel Gorman * request (alloc_order). 406738087d9bSMel Gorman */ 406897a225e6SJoonsoo Kim trace_mm_vmscan_kswapd_wake(pgdat->node_id, highest_zoneidx, 4069e5146b12SMel Gorman alloc_order); 407097a225e6SJoonsoo Kim reclaim_order = balance_pgdat(pgdat, alloc_order, 407197a225e6SJoonsoo Kim highest_zoneidx); 407238087d9bSMel Gorman if (reclaim_order < alloc_order) 407338087d9bSMel Gorman goto kswapd_try_sleep; 407433906bc5SMel Gorman } 4075b0a8cc58STakamori Yamaguchi 407671abdc15SJohannes Weiner tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); 407771abdc15SJohannes Weiner 40781da177e4SLinus Torvalds return 0; 40791da177e4SLinus Torvalds } 40801da177e4SLinus Torvalds 40811da177e4SLinus Torvalds /* 40825ecd9d40SDavid Rientjes * A zone is low on free memory or too fragmented for high-order memory. If 40835ecd9d40SDavid Rientjes * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's 40845ecd9d40SDavid Rientjes * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim 40855ecd9d40SDavid Rientjes * has failed or is not needed, still wake up kcompactd if only compaction is 40865ecd9d40SDavid Rientjes * needed. 40871da177e4SLinus Torvalds */ 40885ecd9d40SDavid Rientjes void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, 408997a225e6SJoonsoo Kim enum zone_type highest_zoneidx) 40901da177e4SLinus Torvalds { 40911da177e4SLinus Torvalds pg_data_t *pgdat; 40925644e1fbSQian Cai enum zone_type curr_idx; 40931da177e4SLinus Torvalds 40946aa303deSMel Gorman if (!managed_zone(zone)) 40951da177e4SLinus Torvalds return; 40961da177e4SLinus Torvalds 40975ecd9d40SDavid Rientjes if (!cpuset_zone_allowed(zone, gfp_flags)) 40981da177e4SLinus Torvalds return; 4099dffcac2cSShakeel Butt 41005644e1fbSQian Cai pgdat = zone->zone_pgdat; 410197a225e6SJoonsoo Kim curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); 41025644e1fbSQian Cai 410397a225e6SJoonsoo Kim if (curr_idx == MAX_NR_ZONES || curr_idx < highest_zoneidx) 410497a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, highest_zoneidx); 41055644e1fbSQian Cai 41065644e1fbSQian Cai if (READ_ONCE(pgdat->kswapd_order) < order) 41075644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, order); 41085644e1fbSQian Cai 41098d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 41101da177e4SLinus Torvalds return; 4111e1a55637SMel Gorman 41125ecd9d40SDavid Rientjes /* Hopeless node, leave it to direct reclaim if possible */ 41135ecd9d40SDavid Rientjes if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES || 411497a225e6SJoonsoo Kim (pgdat_balanced(pgdat, order, highest_zoneidx) && 411597a225e6SJoonsoo Kim !pgdat_watermark_boosted(pgdat, highest_zoneidx))) { 41165ecd9d40SDavid Rientjes /* 41175ecd9d40SDavid Rientjes * There may be plenty of free memory available, but it's too 41185ecd9d40SDavid Rientjes * fragmented for high-order allocations. Wake up kcompactd 41195ecd9d40SDavid Rientjes * and rely on compaction_suitable() to determine if it's 41205ecd9d40SDavid Rientjes * needed. If it fails, it will defer subsequent attempts to 41215ecd9d40SDavid Rientjes * ratelimit its work. 41225ecd9d40SDavid Rientjes */ 41235ecd9d40SDavid Rientjes if (!(gfp_flags & __GFP_DIRECT_RECLAIM)) 412497a225e6SJoonsoo Kim wakeup_kcompactd(pgdat, order, highest_zoneidx); 4125c73322d0SJohannes Weiner return; 41265ecd9d40SDavid Rientjes } 4127c73322d0SJohannes Weiner 412897a225e6SJoonsoo Kim trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, highest_zoneidx, order, 41295ecd9d40SDavid Rientjes gfp_flags); 41308d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 41311da177e4SLinus Torvalds } 41321da177e4SLinus Torvalds 4133c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 41341da177e4SLinus Torvalds /* 41357b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 4136d6277db4SRafael J. Wysocki * freed pages. 4137d6277db4SRafael J. Wysocki * 4138d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 4139d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 4140d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 41411da177e4SLinus Torvalds */ 41427b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 41431da177e4SLinus Torvalds { 4144d6277db4SRafael J. Wysocki struct scan_control sc = { 41457b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 4146ee814fe2SJohannes Weiner .gfp_mask = GFP_HIGHUSER_MOVABLE, 4147b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 41489e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 4149ee814fe2SJohannes Weiner .may_writepage = 1, 4150ee814fe2SJohannes Weiner .may_unmap = 1, 4151ee814fe2SJohannes Weiner .may_swap = 1, 4152ee814fe2SJohannes Weiner .hibernation_mode = 1, 41531da177e4SLinus Torvalds }; 41547b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 41557b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 4156499118e9SVlastimil Babka unsigned int noreclaim_flag; 41571da177e4SLinus Torvalds 4158d92a8cfcSPeter Zijlstra fs_reclaim_acquire(sc.gfp_mask); 415993781325SOmar Sandoval noreclaim_flag = memalloc_noreclaim_save(); 41601732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 4161d6277db4SRafael J. Wysocki 41623115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 4163d6277db4SRafael J. Wysocki 41641732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 4165499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 416693781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 4167d6277db4SRafael J. Wysocki 41687b51755cSKOSAKI Motohiro return nr_reclaimed; 41691da177e4SLinus Torvalds } 4170c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 41711da177e4SLinus Torvalds 41723218ae14SYasunori Goto /* 41733218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 41743218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 41753218ae14SYasunori Goto */ 41763218ae14SYasunori Goto int kswapd_run(int nid) 41773218ae14SYasunori Goto { 41783218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 41793218ae14SYasunori Goto int ret = 0; 41803218ae14SYasunori Goto 41813218ae14SYasunori Goto if (pgdat->kswapd) 41823218ae14SYasunori Goto return 0; 41833218ae14SYasunori Goto 41843218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 41853218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 41863218ae14SYasunori Goto /* failure at boot is fatal */ 4187c6202adfSThomas Gleixner BUG_ON(system_state < SYSTEM_RUNNING); 4188d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 4189d5dc0ad9SGavin Shan ret = PTR_ERR(pgdat->kswapd); 4190d72515b8SXishi Qiu pgdat->kswapd = NULL; 41913218ae14SYasunori Goto } 41923218ae14SYasunori Goto return ret; 41933218ae14SYasunori Goto } 41943218ae14SYasunori Goto 41958fe23e05SDavid Rientjes /* 4196d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 4197bfc8c901SVladimir Davydov * hold mem_hotplug_begin/end(). 41988fe23e05SDavid Rientjes */ 41998fe23e05SDavid Rientjes void kswapd_stop(int nid) 42008fe23e05SDavid Rientjes { 42018fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 42028fe23e05SDavid Rientjes 4203d8adde17SJiang Liu if (kswapd) { 42048fe23e05SDavid Rientjes kthread_stop(kswapd); 4205d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 4206d8adde17SJiang Liu } 42078fe23e05SDavid Rientjes } 42088fe23e05SDavid Rientjes 42091da177e4SLinus Torvalds static int __init kswapd_init(void) 42101da177e4SLinus Torvalds { 42116b700b5bSWei Yang int nid; 421269e05944SAndrew Morton 42131da177e4SLinus Torvalds swap_setup(); 421448fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 42153218ae14SYasunori Goto kswapd_run(nid); 42161da177e4SLinus Torvalds return 0; 42171da177e4SLinus Torvalds } 42181da177e4SLinus Torvalds 42191da177e4SLinus Torvalds module_init(kswapd_init) 42209eeff239SChristoph Lameter 42219eeff239SChristoph Lameter #ifdef CONFIG_NUMA 42229eeff239SChristoph Lameter /* 4223a5f5f91dSMel Gorman * Node reclaim mode 42249eeff239SChristoph Lameter * 4225a5f5f91dSMel Gorman * If non-zero call node_reclaim when the number of free pages falls below 42269eeff239SChristoph Lameter * the watermarks. 42279eeff239SChristoph Lameter */ 4228a5f5f91dSMel Gorman int node_reclaim_mode __read_mostly; 42299eeff239SChristoph Lameter 423051998364SDave Hansen /* 4231a5f5f91dSMel Gorman * Priority for NODE_RECLAIM. This determines the fraction of pages 4232a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 4233a92f7126SChristoph Lameter * a zone. 4234a92f7126SChristoph Lameter */ 4235a5f5f91dSMel Gorman #define NODE_RECLAIM_PRIORITY 4 4236a92f7126SChristoph Lameter 42379eeff239SChristoph Lameter /* 4238a5f5f91dSMel Gorman * Percentage of pages in a zone that must be unmapped for node_reclaim to 42399614634fSChristoph Lameter * occur. 42409614634fSChristoph Lameter */ 42419614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 42429614634fSChristoph Lameter 42439614634fSChristoph Lameter /* 42440ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 42450ff38490SChristoph Lameter * slab reclaim needs to occur. 42460ff38490SChristoph Lameter */ 42470ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 42480ff38490SChristoph Lameter 424911fb9989SMel Gorman static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) 425090afa5deSMel Gorman { 425111fb9989SMel Gorman unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); 425211fb9989SMel Gorman unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + 425311fb9989SMel Gorman node_page_state(pgdat, NR_ACTIVE_FILE); 425490afa5deSMel Gorman 425590afa5deSMel Gorman /* 425690afa5deSMel Gorman * It's possible for there to be more file mapped pages than 425790afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 425890afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 425990afa5deSMel Gorman */ 426090afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 426190afa5deSMel Gorman } 426290afa5deSMel Gorman 426390afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 4264a5f5f91dSMel Gorman static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) 426590afa5deSMel Gorman { 4266d031a157SAlexandru Moise unsigned long nr_pagecache_reclaimable; 4267d031a157SAlexandru Moise unsigned long delta = 0; 426890afa5deSMel Gorman 426990afa5deSMel Gorman /* 427095bbc0c7SZhihui Zhang * If RECLAIM_UNMAP is set, then all file pages are considered 427190afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 427211fb9989SMel Gorman * pages like swapcache and node_unmapped_file_pages() provides 427390afa5deSMel Gorman * a better estimate 427490afa5deSMel Gorman */ 4275a5f5f91dSMel Gorman if (node_reclaim_mode & RECLAIM_UNMAP) 4276a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); 427790afa5deSMel Gorman else 4278a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); 427990afa5deSMel Gorman 428090afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 4281a5f5f91dSMel Gorman if (!(node_reclaim_mode & RECLAIM_WRITE)) 4282a5f5f91dSMel Gorman delta += node_page_state(pgdat, NR_FILE_DIRTY); 428390afa5deSMel Gorman 428490afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 428590afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 428690afa5deSMel Gorman delta = nr_pagecache_reclaimable; 428790afa5deSMel Gorman 428890afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 428990afa5deSMel Gorman } 429090afa5deSMel Gorman 42910ff38490SChristoph Lameter /* 4292a5f5f91dSMel Gorman * Try to free up some pages from this node through reclaim. 42939eeff239SChristoph Lameter */ 4294a5f5f91dSMel Gorman static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 42959eeff239SChristoph Lameter { 42967fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 429769e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 42989eeff239SChristoph Lameter struct task_struct *p = current; 4299499118e9SVlastimil Babka unsigned int noreclaim_flag; 4300179e9639SAndrew Morton struct scan_control sc = { 430162b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 4302f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 4303bd2f6199SJohannes Weiner .order = order, 4304a5f5f91dSMel Gorman .priority = NODE_RECLAIM_PRIORITY, 4305a5f5f91dSMel Gorman .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), 4306a5f5f91dSMel Gorman .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), 4307ee814fe2SJohannes Weiner .may_swap = 1, 4308f2f43e56SNick Desaulniers .reclaim_idx = gfp_zone(gfp_mask), 4309179e9639SAndrew Morton }; 43109eeff239SChristoph Lameter 4311132bb8cfSYafang Shao trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order, 4312132bb8cfSYafang Shao sc.gfp_mask); 4313132bb8cfSYafang Shao 43149eeff239SChristoph Lameter cond_resched(); 431593781325SOmar Sandoval fs_reclaim_acquire(sc.gfp_mask); 4316d4f7796eSChristoph Lameter /* 431795bbc0c7SZhihui Zhang * We need to be able to allocate from the reserves for RECLAIM_UNMAP 4318d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 431995bbc0c7SZhihui Zhang * and RECLAIM_UNMAP. 4320d4f7796eSChristoph Lameter */ 4321499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 4322499118e9SVlastimil Babka p->flags |= PF_SWAPWRITE; 43231732d2b0SAndrew Morton set_task_reclaim_state(p, &sc.reclaim_state); 4324c84db23cSChristoph Lameter 4325a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) { 4326a92f7126SChristoph Lameter /* 4327894befecSAndrey Ryabinin * Free memory by calling shrink node with increasing 43280ff38490SChristoph Lameter * priorities until we have enough memory freed. 4329a92f7126SChristoph Lameter */ 4330a92f7126SChristoph Lameter do { 4331970a39a3SMel Gorman shrink_node(pgdat, &sc); 43329e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 43330ff38490SChristoph Lameter } 4334a92f7126SChristoph Lameter 43351732d2b0SAndrew Morton set_task_reclaim_state(p, NULL); 4336499118e9SVlastimil Babka current->flags &= ~PF_SWAPWRITE; 4337499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 433893781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 4339132bb8cfSYafang Shao 4340132bb8cfSYafang Shao trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed); 4341132bb8cfSYafang Shao 4342a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 43439eeff239SChristoph Lameter } 4344179e9639SAndrew Morton 4345a5f5f91dSMel Gorman int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 4346179e9639SAndrew Morton { 4347d773ed6bSDavid Rientjes int ret; 4348179e9639SAndrew Morton 4349179e9639SAndrew Morton /* 4350a5f5f91dSMel Gorman * Node reclaim reclaims unmapped file backed pages and 43510ff38490SChristoph Lameter * slab pages if we are over the defined limits. 435234aa1330SChristoph Lameter * 43539614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 43549614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 4355a5f5f91dSMel Gorman * thrown out if the node is overallocated. So we do not reclaim 4356a5f5f91dSMel Gorman * if less than a specified percentage of the node is used by 43579614634fSChristoph Lameter * unmapped file backed pages. 4358179e9639SAndrew Morton */ 4359a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && 4360d42f3245SRoman Gushchin node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) <= 4361d42f3245SRoman Gushchin pgdat->min_slab_pages) 4362a5f5f91dSMel Gorman return NODE_RECLAIM_FULL; 4363179e9639SAndrew Morton 4364179e9639SAndrew Morton /* 4365d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 4366179e9639SAndrew Morton */ 4367d0164adcSMel Gorman if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) 4368a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4369179e9639SAndrew Morton 4370179e9639SAndrew Morton /* 4371a5f5f91dSMel Gorman * Only run node reclaim on the local node or on nodes that do not 4372179e9639SAndrew Morton * have associated processors. This will favor the local processor 4373179e9639SAndrew Morton * over remote processors and spread off node memory allocations 4374179e9639SAndrew Morton * as wide as possible. 4375179e9639SAndrew Morton */ 4376a5f5f91dSMel Gorman if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) 4377a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4378d773ed6bSDavid Rientjes 4379a5f5f91dSMel Gorman if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) 4380a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4381fa5e084eSMel Gorman 4382a5f5f91dSMel Gorman ret = __node_reclaim(pgdat, gfp_mask, order); 4383a5f5f91dSMel Gorman clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); 4384d773ed6bSDavid Rientjes 438524cf7251SMel Gorman if (!ret) 438624cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 438724cf7251SMel Gorman 4388d773ed6bSDavid Rientjes return ret; 4389179e9639SAndrew Morton } 43909eeff239SChristoph Lameter #endif 4391894bc310SLee Schermerhorn 439289e004eaSLee Schermerhorn /** 439364e3d12fSKuo-Hsin Yang * check_move_unevictable_pages - check pages for evictability and move to 439464e3d12fSKuo-Hsin Yang * appropriate zone lru list 439564e3d12fSKuo-Hsin Yang * @pvec: pagevec with lru pages to check 439689e004eaSLee Schermerhorn * 439764e3d12fSKuo-Hsin Yang * Checks pages for evictability, if an evictable page is in the unevictable 439864e3d12fSKuo-Hsin Yang * lru list, moves it to the appropriate evictable lru list. This function 439964e3d12fSKuo-Hsin Yang * should be only used for lru pages. 440089e004eaSLee Schermerhorn */ 440164e3d12fSKuo-Hsin Yang void check_move_unevictable_pages(struct pagevec *pvec) 440289e004eaSLee Schermerhorn { 44036168d0daSAlex Shi struct lruvec *lruvec = NULL; 440424513264SHugh Dickins int pgscanned = 0; 440524513264SHugh Dickins int pgrescued = 0; 440689e004eaSLee Schermerhorn int i; 440789e004eaSLee Schermerhorn 440864e3d12fSKuo-Hsin Yang for (i = 0; i < pvec->nr; i++) { 440964e3d12fSKuo-Hsin Yang struct page *page = pvec->pages[i]; 44108d8869caSHugh Dickins int nr_pages; 441189e004eaSLee Schermerhorn 44128d8869caSHugh Dickins if (PageTransTail(page)) 44138d8869caSHugh Dickins continue; 44148d8869caSHugh Dickins 44158d8869caSHugh Dickins nr_pages = thp_nr_pages(page); 44168d8869caSHugh Dickins pgscanned += nr_pages; 44178d8869caSHugh Dickins 4418d25b5bd8SAlex Shi /* block memcg migration during page moving between lru */ 4419d25b5bd8SAlex Shi if (!TestClearPageLRU(page)) 4420d25b5bd8SAlex Shi continue; 4421d25b5bd8SAlex Shi 44222a5e4e34SAlexander Duyck lruvec = relock_page_lruvec_irq(page, lruvec); 4423d25b5bd8SAlex Shi if (page_evictable(page) && PageUnevictable(page)) { 442446ae6b2cSYu Zhao del_page_from_lru_list(page, lruvec); 442524513264SHugh Dickins ClearPageUnevictable(page); 44263a9c9788SYu Zhao add_page_to_lru_list(page, lruvec); 44278d8869caSHugh Dickins pgrescued += nr_pages; 442889e004eaSLee Schermerhorn } 4429d25b5bd8SAlex Shi SetPageLRU(page); 443089e004eaSLee Schermerhorn } 443124513264SHugh Dickins 44326168d0daSAlex Shi if (lruvec) { 443324513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 443424513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 44356168d0daSAlex Shi unlock_page_lruvec_irq(lruvec); 4436d25b5bd8SAlex Shi } else if (pgscanned) { 4437d25b5bd8SAlex Shi count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 443824513264SHugh Dickins } 443985046579SHugh Dickins } 444064e3d12fSKuo-Hsin Yang EXPORT_SYMBOL_GPL(check_move_unevictable_pages); 4441