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> 4426aa2d19SDave Hansen #include <linux/migrate.h> 45873b4771SKeika Kobayashi #include <linux/delayacct.h> 46af936a16SLee Schermerhorn #include <linux/sysctl.h> 47929bea7cSKOSAKI Motohiro #include <linux/oom.h> 4864e3d12fSKuo-Hsin Yang #include <linux/pagevec.h> 49268bb0ceSLinus Torvalds #include <linux/prefetch.h> 50b1de0d13SMitchel Humpherys #include <linux/printk.h> 51f9fe48beSRoss Zwisler #include <linux/dax.h> 52eb414681SJohannes Weiner #include <linux/psi.h> 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds #include <asm/tlbflush.h> 551da177e4SLinus Torvalds #include <asm/div64.h> 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds #include <linux/swapops.h> 58117aad1eSRafael Aquini #include <linux/balloon_compaction.h> 591da177e4SLinus Torvalds 600f8053a5SNick Piggin #include "internal.h" 610f8053a5SNick Piggin 6233906bc5SMel Gorman #define CREATE_TRACE_POINTS 6333906bc5SMel Gorman #include <trace/events/vmscan.h> 6433906bc5SMel Gorman 651da177e4SLinus Torvalds struct scan_control { 6622fba335SKOSAKI Motohiro /* How many pages shrink_list() should reclaim */ 6722fba335SKOSAKI Motohiro unsigned long nr_to_reclaim; 6822fba335SKOSAKI Motohiro 69ee814fe2SJohannes Weiner /* 70ee814fe2SJohannes Weiner * Nodemask of nodes allowed by the caller. If NULL, all nodes 71ee814fe2SJohannes Weiner * are scanned. 72ee814fe2SJohannes Weiner */ 73ee814fe2SJohannes Weiner nodemask_t *nodemask; 749e3b2f8cSKonstantin Khlebnikov 755f53e762SKOSAKI Motohiro /* 76f16015fbSJohannes Weiner * The memory cgroup that hit its limit and as a result is the 77f16015fbSJohannes Weiner * primary target of this reclaim invocation. 78f16015fbSJohannes Weiner */ 79f16015fbSJohannes Weiner struct mem_cgroup *target_mem_cgroup; 8066e1707bSBalbir Singh 817cf111bcSJohannes Weiner /* 827cf111bcSJohannes Weiner * Scan pressure balancing between anon and file LRUs 837cf111bcSJohannes Weiner */ 847cf111bcSJohannes Weiner unsigned long anon_cost; 857cf111bcSJohannes Weiner unsigned long file_cost; 867cf111bcSJohannes Weiner 87b91ac374SJohannes Weiner /* Can active pages be deactivated as part of reclaim? */ 88b91ac374SJohannes Weiner #define DEACTIVATE_ANON 1 89b91ac374SJohannes Weiner #define DEACTIVATE_FILE 2 90b91ac374SJohannes Weiner unsigned int may_deactivate:2; 91b91ac374SJohannes Weiner unsigned int force_deactivate:1; 92b91ac374SJohannes Weiner unsigned int skipped_deactivate:1; 93b91ac374SJohannes Weiner 941276ad68SJohannes Weiner /* Writepage batching in laptop mode; RECLAIM_WRITE */ 95ee814fe2SJohannes Weiner unsigned int may_writepage:1; 96ee814fe2SJohannes Weiner 97ee814fe2SJohannes Weiner /* Can mapped pages be reclaimed? */ 98ee814fe2SJohannes Weiner unsigned int may_unmap:1; 99ee814fe2SJohannes Weiner 100ee814fe2SJohannes Weiner /* Can pages be swapped as part of reclaim? */ 101ee814fe2SJohannes Weiner unsigned int may_swap:1; 102ee814fe2SJohannes Weiner 103d6622f63SYisheng Xie /* 104f56ce412SJohannes Weiner * Cgroup memory below memory.low is protected as long as we 105f56ce412SJohannes Weiner * don't threaten to OOM. If any cgroup is reclaimed at 106f56ce412SJohannes Weiner * reduced force or passed over entirely due to its memory.low 107f56ce412SJohannes Weiner * setting (memcg_low_skipped), and nothing is reclaimed as a 108f56ce412SJohannes Weiner * result, then go back for one more cycle that reclaims the protected 109f56ce412SJohannes Weiner * memory (memcg_low_reclaim) to avert OOM. 110d6622f63SYisheng Xie */ 111d6622f63SYisheng Xie unsigned int memcg_low_reclaim:1; 112d6622f63SYisheng Xie unsigned int memcg_low_skipped:1; 113241994edSJohannes Weiner 114ee814fe2SJohannes Weiner unsigned int hibernation_mode:1; 115ee814fe2SJohannes Weiner 116ee814fe2SJohannes Weiner /* One of the zones is ready for compaction */ 117ee814fe2SJohannes Weiner unsigned int compaction_ready:1; 118ee814fe2SJohannes Weiner 119b91ac374SJohannes Weiner /* There is easily reclaimable cold cache in the current node */ 120b91ac374SJohannes Weiner unsigned int cache_trim_mode:1; 121b91ac374SJohannes Weiner 12253138ceaSJohannes Weiner /* The file pages on the current node are dangerously low */ 12353138ceaSJohannes Weiner unsigned int file_is_tiny:1; 12453138ceaSJohannes Weiner 12526aa2d19SDave Hansen /* Always discard instead of demoting to lower tier memory */ 12626aa2d19SDave Hansen unsigned int no_demotion:1; 12726aa2d19SDave Hansen 128bb451fdfSGreg Thelen /* Allocation order */ 129bb451fdfSGreg Thelen s8 order; 130bb451fdfSGreg Thelen 131bb451fdfSGreg Thelen /* Scan (total_size >> priority) pages at once */ 132bb451fdfSGreg Thelen s8 priority; 133bb451fdfSGreg Thelen 134bb451fdfSGreg Thelen /* The highest zone to isolate pages for reclaim from */ 135bb451fdfSGreg Thelen s8 reclaim_idx; 136bb451fdfSGreg Thelen 137bb451fdfSGreg Thelen /* This context's GFP mask */ 138bb451fdfSGreg Thelen gfp_t gfp_mask; 139bb451fdfSGreg Thelen 140ee814fe2SJohannes Weiner /* Incremented by the number of inactive pages that were scanned */ 141ee814fe2SJohannes Weiner unsigned long nr_scanned; 142ee814fe2SJohannes Weiner 143ee814fe2SJohannes Weiner /* Number of pages freed so far during a call to shrink_zones() */ 144ee814fe2SJohannes Weiner unsigned long nr_reclaimed; 145d108c772SAndrey Ryabinin 146d108c772SAndrey Ryabinin struct { 147d108c772SAndrey Ryabinin unsigned int dirty; 148d108c772SAndrey Ryabinin unsigned int unqueued_dirty; 149d108c772SAndrey Ryabinin unsigned int congested; 150d108c772SAndrey Ryabinin unsigned int writeback; 151d108c772SAndrey Ryabinin unsigned int immediate; 152d108c772SAndrey Ryabinin unsigned int file_taken; 153d108c772SAndrey Ryabinin unsigned int taken; 154d108c772SAndrey Ryabinin } nr; 155e5ca8071SYafang Shao 156e5ca8071SYafang Shao /* for recording the reclaimed slab by now */ 157e5ca8071SYafang Shao struct reclaim_state reclaim_state; 1581da177e4SLinus Torvalds }; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1611da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1621da177e4SLinus Torvalds do { \ 1631da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1641da177e4SLinus Torvalds struct page *prev; \ 1651da177e4SLinus Torvalds \ 1661da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1671da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1681da177e4SLinus Torvalds } \ 1691da177e4SLinus Torvalds } while (0) 1701da177e4SLinus Torvalds #else 1711da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1721da177e4SLinus Torvalds #endif 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds /* 175c843966cSJohannes Weiner * From 0 .. 200. Higher means more swappy. 1761da177e4SLinus Torvalds */ 1771da177e4SLinus Torvalds int vm_swappiness = 60; 1781da177e4SLinus Torvalds 1790a432dcbSYang Shi static void set_task_reclaim_state(struct task_struct *task, 1800a432dcbSYang Shi struct reclaim_state *rs) 1810a432dcbSYang Shi { 1820a432dcbSYang Shi /* Check for an overwrite */ 1830a432dcbSYang Shi WARN_ON_ONCE(rs && task->reclaim_state); 1840a432dcbSYang Shi 1850a432dcbSYang Shi /* Check for the nulling of an already-nulled member */ 1860a432dcbSYang Shi WARN_ON_ONCE(!rs && !task->reclaim_state); 1870a432dcbSYang Shi 1880a432dcbSYang Shi task->reclaim_state = rs; 1890a432dcbSYang Shi } 1900a432dcbSYang Shi 1911da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1921da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1931da177e4SLinus Torvalds 1940a432dcbSYang Shi #ifdef CONFIG_MEMCG 195a2fb1261SYang Shi static int shrinker_nr_max; 1962bfd3637SYang Shi 1973c6f17e6SYang Shi /* The shrinker_info is expanded in a batch of BITS_PER_LONG */ 198a2fb1261SYang Shi static inline int shrinker_map_size(int nr_items) 199a2fb1261SYang Shi { 200a2fb1261SYang Shi return (DIV_ROUND_UP(nr_items, BITS_PER_LONG) * sizeof(unsigned long)); 201a2fb1261SYang Shi } 2022bfd3637SYang Shi 2033c6f17e6SYang Shi static inline int shrinker_defer_size(int nr_items) 2043c6f17e6SYang Shi { 2053c6f17e6SYang Shi return (round_up(nr_items, BITS_PER_LONG) * sizeof(atomic_long_t)); 2063c6f17e6SYang Shi } 2073c6f17e6SYang Shi 208468ab843SYang Shi static struct shrinker_info *shrinker_info_protected(struct mem_cgroup *memcg, 209468ab843SYang Shi int nid) 210468ab843SYang Shi { 211468ab843SYang Shi return rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_info, 212468ab843SYang Shi lockdep_is_held(&shrinker_rwsem)); 213468ab843SYang Shi } 214468ab843SYang Shi 215e4262c4fSYang Shi static int expand_one_shrinker_info(struct mem_cgroup *memcg, 2163c6f17e6SYang Shi int map_size, int defer_size, 2173c6f17e6SYang Shi int old_map_size, int old_defer_size) 2182bfd3637SYang Shi { 219e4262c4fSYang Shi struct shrinker_info *new, *old; 2202bfd3637SYang Shi struct mem_cgroup_per_node *pn; 2212bfd3637SYang Shi int nid; 2223c6f17e6SYang Shi int size = map_size + defer_size; 2232bfd3637SYang Shi 2242bfd3637SYang Shi for_each_node(nid) { 2252bfd3637SYang Shi pn = memcg->nodeinfo[nid]; 226468ab843SYang Shi old = shrinker_info_protected(memcg, nid); 2272bfd3637SYang Shi /* Not yet online memcg */ 2282bfd3637SYang Shi if (!old) 2292bfd3637SYang Shi return 0; 2302bfd3637SYang Shi 2312bfd3637SYang Shi new = kvmalloc_node(sizeof(*new) + size, GFP_KERNEL, nid); 2322bfd3637SYang Shi if (!new) 2332bfd3637SYang Shi return -ENOMEM; 2342bfd3637SYang Shi 2353c6f17e6SYang Shi new->nr_deferred = (atomic_long_t *)(new + 1); 2363c6f17e6SYang Shi new->map = (void *)new->nr_deferred + defer_size; 2373c6f17e6SYang Shi 2383c6f17e6SYang Shi /* map: set all old bits, clear all new bits */ 2393c6f17e6SYang Shi memset(new->map, (int)0xff, old_map_size); 2403c6f17e6SYang Shi memset((void *)new->map + old_map_size, 0, map_size - old_map_size); 2413c6f17e6SYang Shi /* nr_deferred: copy old values, clear all new values */ 2423c6f17e6SYang Shi memcpy(new->nr_deferred, old->nr_deferred, old_defer_size); 2433c6f17e6SYang Shi memset((void *)new->nr_deferred + old_defer_size, 0, 2443c6f17e6SYang Shi defer_size - old_defer_size); 2452bfd3637SYang Shi 246e4262c4fSYang Shi rcu_assign_pointer(pn->shrinker_info, new); 24772673e86SYang Shi kvfree_rcu(old, rcu); 2482bfd3637SYang Shi } 2492bfd3637SYang Shi 2502bfd3637SYang Shi return 0; 2512bfd3637SYang Shi } 2522bfd3637SYang Shi 253e4262c4fSYang Shi void free_shrinker_info(struct mem_cgroup *memcg) 2542bfd3637SYang Shi { 2552bfd3637SYang Shi struct mem_cgroup_per_node *pn; 256e4262c4fSYang Shi struct shrinker_info *info; 2572bfd3637SYang Shi int nid; 2582bfd3637SYang Shi 2592bfd3637SYang Shi for_each_node(nid) { 2602bfd3637SYang Shi pn = memcg->nodeinfo[nid]; 261e4262c4fSYang Shi info = rcu_dereference_protected(pn->shrinker_info, true); 262e4262c4fSYang Shi kvfree(info); 263e4262c4fSYang Shi rcu_assign_pointer(pn->shrinker_info, NULL); 2642bfd3637SYang Shi } 2652bfd3637SYang Shi } 2662bfd3637SYang Shi 267e4262c4fSYang Shi int alloc_shrinker_info(struct mem_cgroup *memcg) 2682bfd3637SYang Shi { 269e4262c4fSYang Shi struct shrinker_info *info; 2702bfd3637SYang Shi int nid, size, ret = 0; 2713c6f17e6SYang Shi int map_size, defer_size = 0; 2722bfd3637SYang Shi 273d27cf2aaSYang Shi down_write(&shrinker_rwsem); 2743c6f17e6SYang Shi map_size = shrinker_map_size(shrinker_nr_max); 2753c6f17e6SYang Shi defer_size = shrinker_defer_size(shrinker_nr_max); 2763c6f17e6SYang Shi size = map_size + defer_size; 2772bfd3637SYang Shi for_each_node(nid) { 278e4262c4fSYang Shi info = kvzalloc_node(sizeof(*info) + size, GFP_KERNEL, nid); 279e4262c4fSYang Shi if (!info) { 280e4262c4fSYang Shi free_shrinker_info(memcg); 2812bfd3637SYang Shi ret = -ENOMEM; 2822bfd3637SYang Shi break; 2832bfd3637SYang Shi } 2843c6f17e6SYang Shi info->nr_deferred = (atomic_long_t *)(info + 1); 2853c6f17e6SYang Shi info->map = (void *)info->nr_deferred + defer_size; 286e4262c4fSYang Shi rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, info); 2872bfd3637SYang Shi } 288d27cf2aaSYang Shi up_write(&shrinker_rwsem); 2892bfd3637SYang Shi 2902bfd3637SYang Shi return ret; 2912bfd3637SYang Shi } 2922bfd3637SYang Shi 2933c6f17e6SYang Shi static inline bool need_expand(int nr_max) 2943c6f17e6SYang Shi { 2953c6f17e6SYang Shi return round_up(nr_max, BITS_PER_LONG) > 2963c6f17e6SYang Shi round_up(shrinker_nr_max, BITS_PER_LONG); 2973c6f17e6SYang Shi } 2983c6f17e6SYang Shi 299e4262c4fSYang Shi static int expand_shrinker_info(int new_id) 3002bfd3637SYang Shi { 3013c6f17e6SYang Shi int ret = 0; 302a2fb1261SYang Shi int new_nr_max = new_id + 1; 3033c6f17e6SYang Shi int map_size, defer_size = 0; 3043c6f17e6SYang Shi int old_map_size, old_defer_size = 0; 3052bfd3637SYang Shi struct mem_cgroup *memcg; 3062bfd3637SYang Shi 3073c6f17e6SYang Shi if (!need_expand(new_nr_max)) 308a2fb1261SYang Shi goto out; 3092bfd3637SYang Shi 3102bfd3637SYang Shi if (!root_mem_cgroup) 311d27cf2aaSYang Shi goto out; 312d27cf2aaSYang Shi 313d27cf2aaSYang Shi lockdep_assert_held(&shrinker_rwsem); 3142bfd3637SYang Shi 3153c6f17e6SYang Shi map_size = shrinker_map_size(new_nr_max); 3163c6f17e6SYang Shi defer_size = shrinker_defer_size(new_nr_max); 3173c6f17e6SYang Shi old_map_size = shrinker_map_size(shrinker_nr_max); 3183c6f17e6SYang Shi old_defer_size = shrinker_defer_size(shrinker_nr_max); 3193c6f17e6SYang Shi 3202bfd3637SYang Shi memcg = mem_cgroup_iter(NULL, NULL, NULL); 3212bfd3637SYang Shi do { 3223c6f17e6SYang Shi ret = expand_one_shrinker_info(memcg, map_size, defer_size, 3233c6f17e6SYang Shi old_map_size, old_defer_size); 3242bfd3637SYang Shi if (ret) { 3252bfd3637SYang Shi mem_cgroup_iter_break(NULL, memcg); 326d27cf2aaSYang Shi goto out; 3272bfd3637SYang Shi } 3282bfd3637SYang Shi } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 329d27cf2aaSYang Shi out: 3302bfd3637SYang Shi if (!ret) 331a2fb1261SYang Shi shrinker_nr_max = new_nr_max; 332d27cf2aaSYang Shi 3332bfd3637SYang Shi return ret; 3342bfd3637SYang Shi } 3352bfd3637SYang Shi 3362bfd3637SYang Shi void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id) 3372bfd3637SYang Shi { 3382bfd3637SYang Shi if (shrinker_id >= 0 && memcg && !mem_cgroup_is_root(memcg)) { 339e4262c4fSYang Shi struct shrinker_info *info; 3402bfd3637SYang Shi 3412bfd3637SYang Shi rcu_read_lock(); 342e4262c4fSYang Shi info = rcu_dereference(memcg->nodeinfo[nid]->shrinker_info); 3432bfd3637SYang Shi /* Pairs with smp mb in shrink_slab() */ 3442bfd3637SYang Shi smp_mb__before_atomic(); 345e4262c4fSYang Shi set_bit(shrinker_id, info->map); 3462bfd3637SYang Shi rcu_read_unlock(); 3472bfd3637SYang Shi } 3482bfd3637SYang Shi } 3492bfd3637SYang Shi 350b4c2b231SKirill Tkhai static DEFINE_IDR(shrinker_idr); 351b4c2b231SKirill Tkhai 352b4c2b231SKirill Tkhai static int prealloc_memcg_shrinker(struct shrinker *shrinker) 353b4c2b231SKirill Tkhai { 354b4c2b231SKirill Tkhai int id, ret = -ENOMEM; 355b4c2b231SKirill Tkhai 356476b30a0SYang Shi if (mem_cgroup_disabled()) 357476b30a0SYang Shi return -ENOSYS; 358476b30a0SYang Shi 359b4c2b231SKirill Tkhai down_write(&shrinker_rwsem); 360b4c2b231SKirill Tkhai /* This may call shrinker, so it must use down_read_trylock() */ 36141ca668aSYang Shi id = idr_alloc(&shrinker_idr, shrinker, 0, 0, GFP_KERNEL); 362b4c2b231SKirill Tkhai if (id < 0) 363b4c2b231SKirill Tkhai goto unlock; 364b4c2b231SKirill Tkhai 3650a4465d3SKirill Tkhai if (id >= shrinker_nr_max) { 366e4262c4fSYang Shi if (expand_shrinker_info(id)) { 3670a4465d3SKirill Tkhai idr_remove(&shrinker_idr, id); 3680a4465d3SKirill Tkhai goto unlock; 3690a4465d3SKirill Tkhai } 3700a4465d3SKirill Tkhai } 371b4c2b231SKirill Tkhai shrinker->id = id; 372b4c2b231SKirill Tkhai ret = 0; 373b4c2b231SKirill Tkhai unlock: 374b4c2b231SKirill Tkhai up_write(&shrinker_rwsem); 375b4c2b231SKirill Tkhai return ret; 376b4c2b231SKirill Tkhai } 377b4c2b231SKirill Tkhai 378b4c2b231SKirill Tkhai static void unregister_memcg_shrinker(struct shrinker *shrinker) 379b4c2b231SKirill Tkhai { 380b4c2b231SKirill Tkhai int id = shrinker->id; 381b4c2b231SKirill Tkhai 382b4c2b231SKirill Tkhai BUG_ON(id < 0); 383b4c2b231SKirill Tkhai 38441ca668aSYang Shi lockdep_assert_held(&shrinker_rwsem); 38541ca668aSYang Shi 386b4c2b231SKirill Tkhai idr_remove(&shrinker_idr, id); 387b4c2b231SKirill Tkhai } 388b4c2b231SKirill Tkhai 38986750830SYang Shi static long xchg_nr_deferred_memcg(int nid, struct shrinker *shrinker, 39086750830SYang Shi struct mem_cgroup *memcg) 39186750830SYang Shi { 39286750830SYang Shi struct shrinker_info *info; 39386750830SYang Shi 39486750830SYang Shi info = shrinker_info_protected(memcg, nid); 39586750830SYang Shi return atomic_long_xchg(&info->nr_deferred[shrinker->id], 0); 39686750830SYang Shi } 39786750830SYang Shi 39886750830SYang Shi static long add_nr_deferred_memcg(long nr, int nid, struct shrinker *shrinker, 39986750830SYang Shi struct mem_cgroup *memcg) 40086750830SYang Shi { 40186750830SYang Shi struct shrinker_info *info; 40286750830SYang Shi 40386750830SYang Shi info = shrinker_info_protected(memcg, nid); 40486750830SYang Shi return atomic_long_add_return(nr, &info->nr_deferred[shrinker->id]); 40586750830SYang Shi } 40686750830SYang Shi 407a178015cSYang Shi void reparent_shrinker_deferred(struct mem_cgroup *memcg) 408a178015cSYang Shi { 409a178015cSYang Shi int i, nid; 410a178015cSYang Shi long nr; 411a178015cSYang Shi struct mem_cgroup *parent; 412a178015cSYang Shi struct shrinker_info *child_info, *parent_info; 413a178015cSYang Shi 414a178015cSYang Shi parent = parent_mem_cgroup(memcg); 415a178015cSYang Shi if (!parent) 416a178015cSYang Shi parent = root_mem_cgroup; 417a178015cSYang Shi 418a178015cSYang Shi /* Prevent from concurrent shrinker_info expand */ 419a178015cSYang Shi down_read(&shrinker_rwsem); 420a178015cSYang Shi for_each_node(nid) { 421a178015cSYang Shi child_info = shrinker_info_protected(memcg, nid); 422a178015cSYang Shi parent_info = shrinker_info_protected(parent, nid); 423a178015cSYang Shi for (i = 0; i < shrinker_nr_max; i++) { 424a178015cSYang Shi nr = atomic_long_read(&child_info->nr_deferred[i]); 425a178015cSYang Shi atomic_long_add(nr, &parent_info->nr_deferred[i]); 426a178015cSYang Shi } 427a178015cSYang Shi } 428a178015cSYang Shi up_read(&shrinker_rwsem); 429a178015cSYang Shi } 430a178015cSYang Shi 431b5ead35eSJohannes Weiner static bool cgroup_reclaim(struct scan_control *sc) 43289b5fae5SJohannes Weiner { 433b5ead35eSJohannes Weiner return sc->target_mem_cgroup; 43489b5fae5SJohannes Weiner } 43597c9341fSTejun Heo 43697c9341fSTejun Heo /** 437b5ead35eSJohannes Weiner * writeback_throttling_sane - is the usual dirty throttling mechanism available? 43897c9341fSTejun Heo * @sc: scan_control in question 43997c9341fSTejun Heo * 44097c9341fSTejun Heo * The normal page dirty throttling mechanism in balance_dirty_pages() is 44197c9341fSTejun Heo * completely broken with the legacy memcg and direct stalling in 44297c9341fSTejun Heo * shrink_page_list() is used for throttling instead, which lacks all the 44397c9341fSTejun Heo * niceties such as fairness, adaptive pausing, bandwidth proportional 44497c9341fSTejun Heo * allocation and configurability. 44597c9341fSTejun Heo * 44697c9341fSTejun Heo * This function tests whether the vmscan currently in progress can assume 44797c9341fSTejun Heo * that the normal dirty throttling mechanism is operational. 44897c9341fSTejun Heo */ 449b5ead35eSJohannes Weiner static bool writeback_throttling_sane(struct scan_control *sc) 45097c9341fSTejun Heo { 451b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 45297c9341fSTejun Heo return true; 45397c9341fSTejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 45469234aceSLinus Torvalds if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) 45597c9341fSTejun Heo return true; 45697c9341fSTejun Heo #endif 45797c9341fSTejun Heo return false; 45897c9341fSTejun Heo } 45991a45470SKAMEZAWA Hiroyuki #else 4600a432dcbSYang Shi static int prealloc_memcg_shrinker(struct shrinker *shrinker) 4610a432dcbSYang Shi { 462476b30a0SYang Shi return -ENOSYS; 4630a432dcbSYang Shi } 4640a432dcbSYang Shi 4650a432dcbSYang Shi static void unregister_memcg_shrinker(struct shrinker *shrinker) 4660a432dcbSYang Shi { 4670a432dcbSYang Shi } 4680a432dcbSYang Shi 46986750830SYang Shi static long xchg_nr_deferred_memcg(int nid, struct shrinker *shrinker, 47086750830SYang Shi struct mem_cgroup *memcg) 47186750830SYang Shi { 47286750830SYang Shi return 0; 47386750830SYang Shi } 47486750830SYang Shi 47586750830SYang Shi static long add_nr_deferred_memcg(long nr, int nid, struct shrinker *shrinker, 47686750830SYang Shi struct mem_cgroup *memcg) 47786750830SYang Shi { 47886750830SYang Shi return 0; 47986750830SYang Shi } 48086750830SYang Shi 481b5ead35eSJohannes Weiner static bool cgroup_reclaim(struct scan_control *sc) 48289b5fae5SJohannes Weiner { 483b5ead35eSJohannes Weiner return false; 48489b5fae5SJohannes Weiner } 48597c9341fSTejun Heo 486b5ead35eSJohannes Weiner static bool writeback_throttling_sane(struct scan_control *sc) 48797c9341fSTejun Heo { 48897c9341fSTejun Heo return true; 48997c9341fSTejun Heo } 49091a45470SKAMEZAWA Hiroyuki #endif 49191a45470SKAMEZAWA Hiroyuki 49286750830SYang Shi static long xchg_nr_deferred(struct shrinker *shrinker, 49386750830SYang Shi struct shrink_control *sc) 49486750830SYang Shi { 49586750830SYang Shi int nid = sc->nid; 49686750830SYang Shi 49786750830SYang Shi if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) 49886750830SYang Shi nid = 0; 49986750830SYang Shi 50086750830SYang Shi if (sc->memcg && 50186750830SYang Shi (shrinker->flags & SHRINKER_MEMCG_AWARE)) 50286750830SYang Shi return xchg_nr_deferred_memcg(nid, shrinker, 50386750830SYang Shi sc->memcg); 50486750830SYang Shi 50586750830SYang Shi return atomic_long_xchg(&shrinker->nr_deferred[nid], 0); 50686750830SYang Shi } 50786750830SYang Shi 50886750830SYang Shi 50986750830SYang Shi static long add_nr_deferred(long nr, struct shrinker *shrinker, 51086750830SYang Shi struct shrink_control *sc) 51186750830SYang Shi { 51286750830SYang Shi int nid = sc->nid; 51386750830SYang Shi 51486750830SYang Shi if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) 51586750830SYang Shi nid = 0; 51686750830SYang Shi 51786750830SYang Shi if (sc->memcg && 51886750830SYang Shi (shrinker->flags & SHRINKER_MEMCG_AWARE)) 51986750830SYang Shi return add_nr_deferred_memcg(nr, nid, shrinker, 52086750830SYang Shi sc->memcg); 52186750830SYang Shi 52286750830SYang Shi return atomic_long_add_return(nr, &shrinker->nr_deferred[nid]); 52386750830SYang Shi } 52486750830SYang Shi 52526aa2d19SDave Hansen static bool can_demote(int nid, struct scan_control *sc) 52626aa2d19SDave Hansen { 52726aa2d19SDave Hansen if (sc->no_demotion) 52826aa2d19SDave Hansen return false; 52926aa2d19SDave Hansen if (next_demotion_node(nid) == NUMA_NO_NODE) 53026aa2d19SDave Hansen return false; 53126aa2d19SDave Hansen 53226aa2d19SDave Hansen // FIXME: actually enable this later in the series 53326aa2d19SDave Hansen return false; 53426aa2d19SDave Hansen } 53526aa2d19SDave Hansen 5365a1c84b4SMel Gorman /* 5375a1c84b4SMel Gorman * This misses isolated pages which are not accounted for to save counters. 5385a1c84b4SMel Gorman * As the data only determines if reclaim or compaction continues, it is 5395a1c84b4SMel Gorman * not expected that isolated pages will be a dominating factor. 5405a1c84b4SMel Gorman */ 5415a1c84b4SMel Gorman unsigned long zone_reclaimable_pages(struct zone *zone) 5425a1c84b4SMel Gorman { 5435a1c84b4SMel Gorman unsigned long nr; 5445a1c84b4SMel Gorman 5455a1c84b4SMel Gorman nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) + 5465a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE); 5475a1c84b4SMel Gorman if (get_nr_swap_pages() > 0) 5485a1c84b4SMel Gorman nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) + 5495a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON); 5505a1c84b4SMel Gorman 5515a1c84b4SMel Gorman return nr; 5525a1c84b4SMel Gorman } 5535a1c84b4SMel Gorman 554fd538803SMichal Hocko /** 555fd538803SMichal Hocko * lruvec_lru_size - Returns the number of pages on the given LRU list. 556fd538803SMichal Hocko * @lruvec: lru vector 557fd538803SMichal Hocko * @lru: lru to use 558fd538803SMichal Hocko * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list) 559fd538803SMichal Hocko */ 5602091339dSYu Zhao static unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, 5612091339dSYu Zhao int zone_idx) 562c9f299d9SKOSAKI Motohiro { 563de3b0150SJohannes Weiner unsigned long size = 0; 564fd538803SMichal Hocko int zid; 565a3d8e054SKOSAKI Motohiro 566de3b0150SJohannes Weiner for (zid = 0; zid <= zone_idx && zid < MAX_NR_ZONES; zid++) { 567fd538803SMichal Hocko struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; 568fd538803SMichal Hocko 569fd538803SMichal Hocko if (!managed_zone(zone)) 570fd538803SMichal Hocko continue; 571fd538803SMichal Hocko 572fd538803SMichal Hocko if (!mem_cgroup_disabled()) 573de3b0150SJohannes Weiner size += mem_cgroup_get_zone_lru_size(lruvec, lru, zid); 574fd538803SMichal Hocko else 575de3b0150SJohannes Weiner size += zone_page_state(zone, NR_ZONE_LRU_BASE + lru); 576c9f299d9SKOSAKI Motohiro } 577de3b0150SJohannes Weiner return size; 578b4536f0cSMichal Hocko } 579b4536f0cSMichal Hocko 5801da177e4SLinus Torvalds /* 5811d3d4437SGlauber Costa * Add a shrinker callback to be called from the vm. 5821da177e4SLinus Torvalds */ 5838e04944fSTetsuo Handa int prealloc_shrinker(struct shrinker *shrinker) 5841da177e4SLinus Torvalds { 585476b30a0SYang Shi unsigned int size; 586476b30a0SYang Shi int err; 5871d3d4437SGlauber Costa 588476b30a0SYang Shi if (shrinker->flags & SHRINKER_MEMCG_AWARE) { 589476b30a0SYang Shi err = prealloc_memcg_shrinker(shrinker); 590476b30a0SYang Shi if (err != -ENOSYS) 591476b30a0SYang Shi return err; 592476b30a0SYang Shi 593476b30a0SYang Shi shrinker->flags &= ~SHRINKER_MEMCG_AWARE; 594476b30a0SYang Shi } 595476b30a0SYang Shi 596476b30a0SYang Shi size = sizeof(*shrinker->nr_deferred); 5971d3d4437SGlauber Costa if (shrinker->flags & SHRINKER_NUMA_AWARE) 5981d3d4437SGlauber Costa size *= nr_node_ids; 5991d3d4437SGlauber Costa 6001d3d4437SGlauber Costa shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); 6011d3d4437SGlauber Costa if (!shrinker->nr_deferred) 6021d3d4437SGlauber Costa return -ENOMEM; 603b4c2b231SKirill Tkhai 6048e04944fSTetsuo Handa return 0; 6058e04944fSTetsuo Handa } 6061d3d4437SGlauber Costa 6078e04944fSTetsuo Handa void free_prealloced_shrinker(struct shrinker *shrinker) 6088e04944fSTetsuo Handa { 60941ca668aSYang Shi if (shrinker->flags & SHRINKER_MEMCG_AWARE) { 61041ca668aSYang Shi down_write(&shrinker_rwsem); 611b4c2b231SKirill Tkhai unregister_memcg_shrinker(shrinker); 61241ca668aSYang Shi up_write(&shrinker_rwsem); 613476b30a0SYang Shi return; 61441ca668aSYang Shi } 615b4c2b231SKirill Tkhai 6168e04944fSTetsuo Handa kfree(shrinker->nr_deferred); 6178e04944fSTetsuo Handa shrinker->nr_deferred = NULL; 6188e04944fSTetsuo Handa } 6198e04944fSTetsuo Handa 6208e04944fSTetsuo Handa void register_shrinker_prepared(struct shrinker *shrinker) 6218e04944fSTetsuo Handa { 6221da177e4SLinus Torvalds down_write(&shrinker_rwsem); 6231da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 62441ca668aSYang Shi shrinker->flags |= SHRINKER_REGISTERED; 6251da177e4SLinus Torvalds up_write(&shrinker_rwsem); 6268e04944fSTetsuo Handa } 6278e04944fSTetsuo Handa 6288e04944fSTetsuo Handa int register_shrinker(struct shrinker *shrinker) 6298e04944fSTetsuo Handa { 6308e04944fSTetsuo Handa int err = prealloc_shrinker(shrinker); 6318e04944fSTetsuo Handa 6328e04944fSTetsuo Handa if (err) 6338e04944fSTetsuo Handa return err; 6348e04944fSTetsuo Handa register_shrinker_prepared(shrinker); 6351d3d4437SGlauber Costa return 0; 6361da177e4SLinus Torvalds } 6378e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds /* 6401da177e4SLinus Torvalds * Remove one 6411da177e4SLinus Torvalds */ 6428e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 6431da177e4SLinus Torvalds { 64441ca668aSYang Shi if (!(shrinker->flags & SHRINKER_REGISTERED)) 645bb422a73STetsuo Handa return; 64641ca668aSYang Shi 6471da177e4SLinus Torvalds down_write(&shrinker_rwsem); 6481da177e4SLinus Torvalds list_del(&shrinker->list); 64941ca668aSYang Shi shrinker->flags &= ~SHRINKER_REGISTERED; 65041ca668aSYang Shi if (shrinker->flags & SHRINKER_MEMCG_AWARE) 65141ca668aSYang Shi unregister_memcg_shrinker(shrinker); 6521da177e4SLinus Torvalds up_write(&shrinker_rwsem); 65341ca668aSYang Shi 654ae393321SAndrew Vagin kfree(shrinker->nr_deferred); 655bb422a73STetsuo Handa shrinker->nr_deferred = NULL; 6561da177e4SLinus Torvalds } 6578e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds #define SHRINK_BATCH 128 6601d3d4437SGlauber Costa 661cb731d6cSVladimir Davydov static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, 6629092c71bSJosef Bacik struct shrinker *shrinker, int priority) 6631da177e4SLinus Torvalds { 66424f7c6b9SDave Chinner unsigned long freed = 0; 6651da177e4SLinus Torvalds unsigned long long delta; 666635697c6SKonstantin Khlebnikov long total_scan; 667d5bc5fd3SVladimir Davydov long freeable; 668acf92b48SDave Chinner long nr; 669acf92b48SDave Chinner long new_nr; 670e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 671e9299f50SDave Chinner : SHRINK_BATCH; 6725f33a080SShaohua Li long scanned = 0, next_deferred; 6731da177e4SLinus Torvalds 674d5bc5fd3SVladimir Davydov freeable = shrinker->count_objects(shrinker, shrinkctl); 6759b996468SKirill Tkhai if (freeable == 0 || freeable == SHRINK_EMPTY) 6769b996468SKirill Tkhai return freeable; 677635697c6SKonstantin Khlebnikov 678acf92b48SDave Chinner /* 679acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 680acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 681acf92b48SDave Chinner * don't also do this scanning work. 682acf92b48SDave Chinner */ 68386750830SYang Shi nr = xchg_nr_deferred(shrinker, shrinkctl); 684acf92b48SDave Chinner 6854b85afbdSJohannes Weiner if (shrinker->seeks) { 6869092c71bSJosef Bacik delta = freeable >> priority; 6879092c71bSJosef Bacik delta *= 4; 6889092c71bSJosef Bacik do_div(delta, shrinker->seeks); 6894b85afbdSJohannes Weiner } else { 6904b85afbdSJohannes Weiner /* 6914b85afbdSJohannes Weiner * These objects don't require any IO to create. Trim 6924b85afbdSJohannes Weiner * them aggressively under memory pressure to keep 6934b85afbdSJohannes Weiner * them from causing refetches in the IO caches. 6944b85afbdSJohannes Weiner */ 6954b85afbdSJohannes Weiner delta = freeable / 2; 6964b85afbdSJohannes Weiner } 697172b06c3SRoman Gushchin 69818bb473eSYang Shi total_scan = nr >> priority; 699acf92b48SDave Chinner total_scan += delta; 70018bb473eSYang Shi total_scan = min(total_scan, (2 * freeable)); 7011da177e4SLinus Torvalds 70224f7c6b9SDave Chinner trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, 7039092c71bSJosef Bacik freeable, delta, total_scan, priority); 70409576073SDave Chinner 7050b1fb40aSVladimir Davydov /* 7060b1fb40aSVladimir Davydov * Normally, we should not scan less than batch_size objects in one 7070b1fb40aSVladimir Davydov * pass to avoid too frequent shrinker calls, but if the slab has less 7080b1fb40aSVladimir Davydov * than batch_size objects in total and we are really tight on memory, 7090b1fb40aSVladimir Davydov * we will try to reclaim all available objects, otherwise we can end 7100b1fb40aSVladimir Davydov * up failing allocations although there are plenty of reclaimable 7110b1fb40aSVladimir Davydov * objects spread over several slabs with usage less than the 7120b1fb40aSVladimir Davydov * batch_size. 7130b1fb40aSVladimir Davydov * 7140b1fb40aSVladimir Davydov * We detect the "tight on memory" situations by looking at the total 7150b1fb40aSVladimir Davydov * number of objects we want to scan (total_scan). If it is greater 716d5bc5fd3SVladimir Davydov * than the total number of objects on slab (freeable), we must be 7170b1fb40aSVladimir Davydov * scanning at high prio and therefore should try to reclaim as much as 7180b1fb40aSVladimir Davydov * possible. 7190b1fb40aSVladimir Davydov */ 7200b1fb40aSVladimir Davydov while (total_scan >= batch_size || 721d5bc5fd3SVladimir Davydov total_scan >= freeable) { 72224f7c6b9SDave Chinner unsigned long ret; 7230b1fb40aSVladimir Davydov unsigned long nr_to_scan = min(batch_size, total_scan); 7241da177e4SLinus Torvalds 7250b1fb40aSVladimir Davydov shrinkctl->nr_to_scan = nr_to_scan; 726d460acb5SChris Wilson shrinkctl->nr_scanned = nr_to_scan; 72724f7c6b9SDave Chinner ret = shrinker->scan_objects(shrinker, shrinkctl); 72824f7c6b9SDave Chinner if (ret == SHRINK_STOP) 7291da177e4SLinus Torvalds break; 73024f7c6b9SDave Chinner freed += ret; 73124f7c6b9SDave Chinner 732d460acb5SChris Wilson count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned); 733d460acb5SChris Wilson total_scan -= shrinkctl->nr_scanned; 734d460acb5SChris Wilson scanned += shrinkctl->nr_scanned; 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds cond_resched(); 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds 73918bb473eSYang Shi /* 74018bb473eSYang Shi * The deferred work is increased by any new work (delta) that wasn't 74118bb473eSYang Shi * done, decreased by old deferred work that was done now. 74218bb473eSYang Shi * 74318bb473eSYang Shi * And it is capped to two times of the freeable items. 74418bb473eSYang Shi */ 74518bb473eSYang Shi next_deferred = max_t(long, (nr + delta - scanned), 0); 74618bb473eSYang Shi next_deferred = min(next_deferred, (2 * freeable)); 74718bb473eSYang Shi 748acf92b48SDave Chinner /* 749acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 75086750830SYang Shi * manner that handles concurrent updates. 751acf92b48SDave Chinner */ 75286750830SYang Shi new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl); 753acf92b48SDave Chinner 7548efb4b59SYang Shi trace_mm_shrink_slab_end(shrinker, shrinkctl->nid, freed, nr, new_nr, total_scan); 7551d3d4437SGlauber Costa return freed; 7561d3d4437SGlauber Costa } 7571d3d4437SGlauber Costa 7580a432dcbSYang Shi #ifdef CONFIG_MEMCG 759b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 760b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 761b0dedc49SKirill Tkhai { 762e4262c4fSYang Shi struct shrinker_info *info; 763b8e57efaSKirill Tkhai unsigned long ret, freed = 0; 764b8e57efaSKirill Tkhai int i; 765b0dedc49SKirill Tkhai 7660a432dcbSYang Shi if (!mem_cgroup_online(memcg)) 767b0dedc49SKirill Tkhai return 0; 768b0dedc49SKirill Tkhai 769b0dedc49SKirill Tkhai if (!down_read_trylock(&shrinker_rwsem)) 770b0dedc49SKirill Tkhai return 0; 771b0dedc49SKirill Tkhai 772468ab843SYang Shi info = shrinker_info_protected(memcg, nid); 773e4262c4fSYang Shi if (unlikely(!info)) 774b0dedc49SKirill Tkhai goto unlock; 775b0dedc49SKirill Tkhai 776e4262c4fSYang Shi for_each_set_bit(i, info->map, shrinker_nr_max) { 777b0dedc49SKirill Tkhai struct shrink_control sc = { 778b0dedc49SKirill Tkhai .gfp_mask = gfp_mask, 779b0dedc49SKirill Tkhai .nid = nid, 780b0dedc49SKirill Tkhai .memcg = memcg, 781b0dedc49SKirill Tkhai }; 782b0dedc49SKirill Tkhai struct shrinker *shrinker; 783b0dedc49SKirill Tkhai 784b0dedc49SKirill Tkhai shrinker = idr_find(&shrinker_idr, i); 78541ca668aSYang Shi if (unlikely(!shrinker || !(shrinker->flags & SHRINKER_REGISTERED))) { 7867e010df5SKirill Tkhai if (!shrinker) 787e4262c4fSYang Shi clear_bit(i, info->map); 788b0dedc49SKirill Tkhai continue; 789b0dedc49SKirill Tkhai } 790b0dedc49SKirill Tkhai 7910a432dcbSYang Shi /* Call non-slab shrinkers even though kmem is disabled */ 7920a432dcbSYang Shi if (!memcg_kmem_enabled() && 7930a432dcbSYang Shi !(shrinker->flags & SHRINKER_NONSLAB)) 7940a432dcbSYang Shi continue; 7950a432dcbSYang Shi 796b0dedc49SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 797f90280d6SKirill Tkhai if (ret == SHRINK_EMPTY) { 798e4262c4fSYang Shi clear_bit(i, info->map); 799f90280d6SKirill Tkhai /* 800f90280d6SKirill Tkhai * After the shrinker reported that it had no objects to 801f90280d6SKirill Tkhai * free, but before we cleared the corresponding bit in 802f90280d6SKirill Tkhai * the memcg shrinker map, a new object might have been 803f90280d6SKirill Tkhai * added. To make sure, we have the bit set in this 804f90280d6SKirill Tkhai * case, we invoke the shrinker one more time and reset 805f90280d6SKirill Tkhai * the bit if it reports that it is not empty anymore. 806f90280d6SKirill Tkhai * The memory barrier here pairs with the barrier in 8072bfd3637SYang Shi * set_shrinker_bit(): 808f90280d6SKirill Tkhai * 809f90280d6SKirill Tkhai * list_lru_add() shrink_slab_memcg() 810f90280d6SKirill Tkhai * list_add_tail() clear_bit() 811f90280d6SKirill Tkhai * <MB> <MB> 812f90280d6SKirill Tkhai * set_bit() do_shrink_slab() 813f90280d6SKirill Tkhai */ 814f90280d6SKirill Tkhai smp_mb__after_atomic(); 815f90280d6SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 8169b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 8179b996468SKirill Tkhai ret = 0; 818f90280d6SKirill Tkhai else 8192bfd3637SYang Shi set_shrinker_bit(memcg, nid, i); 820f90280d6SKirill Tkhai } 821b0dedc49SKirill Tkhai freed += ret; 822b0dedc49SKirill Tkhai 823b0dedc49SKirill Tkhai if (rwsem_is_contended(&shrinker_rwsem)) { 824b0dedc49SKirill Tkhai freed = freed ? : 1; 825b0dedc49SKirill Tkhai break; 826b0dedc49SKirill Tkhai } 827b0dedc49SKirill Tkhai } 828b0dedc49SKirill Tkhai unlock: 829b0dedc49SKirill Tkhai up_read(&shrinker_rwsem); 830b0dedc49SKirill Tkhai return freed; 831b0dedc49SKirill Tkhai } 8320a432dcbSYang Shi #else /* CONFIG_MEMCG */ 833b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 834b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 835b0dedc49SKirill Tkhai { 836b0dedc49SKirill Tkhai return 0; 837b0dedc49SKirill Tkhai } 8380a432dcbSYang Shi #endif /* CONFIG_MEMCG */ 839b0dedc49SKirill Tkhai 8406b4f7799SJohannes Weiner /** 841cb731d6cSVladimir Davydov * shrink_slab - shrink slab caches 8426b4f7799SJohannes Weiner * @gfp_mask: allocation context 8436b4f7799SJohannes Weiner * @nid: node whose slab caches to target 844cb731d6cSVladimir Davydov * @memcg: memory cgroup whose slab caches to target 8459092c71bSJosef Bacik * @priority: the reclaim priority 8461d3d4437SGlauber Costa * 8476b4f7799SJohannes Weiner * Call the shrink functions to age shrinkable caches. 8481d3d4437SGlauber Costa * 8496b4f7799SJohannes Weiner * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, 8506b4f7799SJohannes Weiner * unaware shrinkers will receive a node id of 0 instead. 8511d3d4437SGlauber Costa * 852aeed1d32SVladimir Davydov * @memcg specifies the memory cgroup to target. Unaware shrinkers 853aeed1d32SVladimir Davydov * are called only if it is the root cgroup. 854cb731d6cSVladimir Davydov * 8559092c71bSJosef Bacik * @priority is sc->priority, we take the number of objects and >> by priority 8569092c71bSJosef Bacik * in order to get the scan target. 8571d3d4437SGlauber Costa * 8586b4f7799SJohannes Weiner * Returns the number of reclaimed slab objects. 8591d3d4437SGlauber Costa */ 860cb731d6cSVladimir Davydov static unsigned long shrink_slab(gfp_t gfp_mask, int nid, 861cb731d6cSVladimir Davydov struct mem_cgroup *memcg, 8629092c71bSJosef Bacik int priority) 8631d3d4437SGlauber Costa { 864b8e57efaSKirill Tkhai unsigned long ret, freed = 0; 8651d3d4437SGlauber Costa struct shrinker *shrinker; 8661d3d4437SGlauber Costa 867fa1e512fSYang Shi /* 868fa1e512fSYang Shi * The root memcg might be allocated even though memcg is disabled 869fa1e512fSYang Shi * via "cgroup_disable=memory" boot parameter. This could make 870fa1e512fSYang Shi * mem_cgroup_is_root() return false, then just run memcg slab 871fa1e512fSYang Shi * shrink, but skip global shrink. This may result in premature 872fa1e512fSYang Shi * oom. 873fa1e512fSYang Shi */ 874fa1e512fSYang Shi if (!mem_cgroup_disabled() && !mem_cgroup_is_root(memcg)) 875b0dedc49SKirill Tkhai return shrink_slab_memcg(gfp_mask, nid, memcg, priority); 876cb731d6cSVladimir Davydov 877e830c63aSTetsuo Handa if (!down_read_trylock(&shrinker_rwsem)) 8781d3d4437SGlauber Costa goto out; 8791d3d4437SGlauber Costa 8801d3d4437SGlauber Costa list_for_each_entry(shrinker, &shrinker_list, list) { 8816b4f7799SJohannes Weiner struct shrink_control sc = { 8826b4f7799SJohannes Weiner .gfp_mask = gfp_mask, 8836b4f7799SJohannes Weiner .nid = nid, 884cb731d6cSVladimir Davydov .memcg = memcg, 8856b4f7799SJohannes Weiner }; 8866b4f7799SJohannes Weiner 8879b996468SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 8889b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 8899b996468SKirill Tkhai ret = 0; 8909b996468SKirill Tkhai freed += ret; 891e496612cSMinchan Kim /* 892e496612cSMinchan Kim * Bail out if someone want to register a new shrinker to 89355b65a57SEthon Paul * prevent the registration from being stalled for long periods 894e496612cSMinchan Kim * by parallel ongoing shrinking. 895e496612cSMinchan Kim */ 896e496612cSMinchan Kim if (rwsem_is_contended(&shrinker_rwsem)) { 897e496612cSMinchan Kim freed = freed ? : 1; 898e496612cSMinchan Kim break; 899e496612cSMinchan Kim } 900ec97097bSVladimir Davydov } 9011d3d4437SGlauber Costa 9021da177e4SLinus Torvalds up_read(&shrinker_rwsem); 903f06590bdSMinchan Kim out: 904f06590bdSMinchan Kim cond_resched(); 90524f7c6b9SDave Chinner return freed; 9061da177e4SLinus Torvalds } 9071da177e4SLinus Torvalds 908cb731d6cSVladimir Davydov void drop_slab_node(int nid) 909cb731d6cSVladimir Davydov { 910cb731d6cSVladimir Davydov unsigned long freed; 911cb731d6cSVladimir Davydov 912cb731d6cSVladimir Davydov do { 913cb731d6cSVladimir Davydov struct mem_cgroup *memcg = NULL; 914cb731d6cSVladimir Davydov 915069c411dSChunxin Zang if (fatal_signal_pending(current)) 916069c411dSChunxin Zang return; 917069c411dSChunxin Zang 918cb731d6cSVladimir Davydov freed = 0; 919aeed1d32SVladimir Davydov memcg = mem_cgroup_iter(NULL, NULL, NULL); 920cb731d6cSVladimir Davydov do { 9219092c71bSJosef Bacik freed += shrink_slab(GFP_KERNEL, nid, memcg, 0); 922cb731d6cSVladimir Davydov } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 923cb731d6cSVladimir Davydov } while (freed > 10); 924cb731d6cSVladimir Davydov } 925cb731d6cSVladimir Davydov 926cb731d6cSVladimir Davydov void drop_slab(void) 927cb731d6cSVladimir Davydov { 928cb731d6cSVladimir Davydov int nid; 929cb731d6cSVladimir Davydov 930cb731d6cSVladimir Davydov for_each_online_node(nid) 931cb731d6cSVladimir Davydov drop_slab_node(nid); 932cb731d6cSVladimir Davydov } 933cb731d6cSVladimir Davydov 9341da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 9351da177e4SLinus Torvalds { 936ceddc3a5SJohannes Weiner /* 937ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 93867891fffSMatthew Wilcox * that isolated the page, the page cache and optional buffer 93967891fffSMatthew Wilcox * heads at page->private. 940ceddc3a5SJohannes Weiner */ 9413efe62e4SMatthew Wilcox (Oracle) int page_cache_pins = thp_nr_pages(page); 94267891fffSMatthew Wilcox return page_count(page) - page_has_private(page) == 1 + page_cache_pins; 9431da177e4SLinus Torvalds } 9441da177e4SLinus Torvalds 945cb16556dSYang Shi static int may_write_to_inode(struct inode *inode) 9461da177e4SLinus Torvalds { 947930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 9481da177e4SLinus Torvalds return 1; 949703c2708STejun Heo if (!inode_write_congested(inode)) 9501da177e4SLinus Torvalds return 1; 951703c2708STejun Heo if (inode_to_bdi(inode) == current->backing_dev_info) 9521da177e4SLinus Torvalds return 1; 9531da177e4SLinus Torvalds return 0; 9541da177e4SLinus Torvalds } 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds /* 9571da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 9581da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 9591da177e4SLinus Torvalds * fsync(), msync() or close(). 9601da177e4SLinus Torvalds * 9611da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 9621da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 9631da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 9641da177e4SLinus Torvalds * 9651da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 9661da177e4SLinus Torvalds * __GFP_FS. 9671da177e4SLinus Torvalds */ 9681da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 9691da177e4SLinus Torvalds struct page *page, int error) 9701da177e4SLinus Torvalds { 9717eaceaccSJens Axboe lock_page(page); 9723e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 9733e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 9741da177e4SLinus Torvalds unlock_page(page); 9751da177e4SLinus Torvalds } 9761da177e4SLinus Torvalds 97704e62a29SChristoph Lameter /* possible outcome of pageout() */ 97804e62a29SChristoph Lameter typedef enum { 97904e62a29SChristoph Lameter /* failed to write page out, page is locked */ 98004e62a29SChristoph Lameter PAGE_KEEP, 98104e62a29SChristoph Lameter /* move page to the active list, page is locked */ 98204e62a29SChristoph Lameter PAGE_ACTIVATE, 98304e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 98404e62a29SChristoph Lameter PAGE_SUCCESS, 98504e62a29SChristoph Lameter /* page is clean and locked */ 98604e62a29SChristoph Lameter PAGE_CLEAN, 98704e62a29SChristoph Lameter } pageout_t; 98804e62a29SChristoph Lameter 9891da177e4SLinus Torvalds /* 9901742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 9911742f19fSAndrew Morton * Calls ->writepage(). 9921da177e4SLinus Torvalds */ 993cb16556dSYang Shi static pageout_t pageout(struct page *page, struct address_space *mapping) 9941da177e4SLinus Torvalds { 9951da177e4SLinus Torvalds /* 9961da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 9971da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 9981da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 9991da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 10001da177e4SLinus Torvalds * PagePrivate for that. 10011da177e4SLinus Torvalds * 10028174202bSAl Viro * If this process is currently in __generic_file_write_iter() against 10031da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 10041da177e4SLinus Torvalds * will block. 10051da177e4SLinus Torvalds * 10061da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 10071da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 10081da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 10091da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 10101da177e4SLinus Torvalds */ 10111da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 10121da177e4SLinus Torvalds return PAGE_KEEP; 10131da177e4SLinus Torvalds if (!mapping) { 10141da177e4SLinus Torvalds /* 10151da177e4SLinus Torvalds * Some data journaling orphaned pages can have 10161da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 10171da177e4SLinus Torvalds */ 1018266cf658SDavid Howells if (page_has_private(page)) { 10191da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 10201da177e4SLinus Torvalds ClearPageDirty(page); 1021b1de0d13SMitchel Humpherys pr_info("%s: orphaned page\n", __func__); 10221da177e4SLinus Torvalds return PAGE_CLEAN; 10231da177e4SLinus Torvalds } 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds return PAGE_KEEP; 10261da177e4SLinus Torvalds } 10271da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 10281da177e4SLinus Torvalds return PAGE_ACTIVATE; 1029cb16556dSYang Shi if (!may_write_to_inode(mapping->host)) 10301da177e4SLinus Torvalds return PAGE_KEEP; 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 10331da177e4SLinus Torvalds int res; 10341da177e4SLinus Torvalds struct writeback_control wbc = { 10351da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 10361da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 1037111ebb6eSOGAWA Hirofumi .range_start = 0, 1038111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 10391da177e4SLinus Torvalds .for_reclaim = 1, 10401da177e4SLinus Torvalds }; 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds SetPageReclaim(page); 10431da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 10441da177e4SLinus Torvalds if (res < 0) 10451da177e4SLinus Torvalds handle_write_error(mapping, page, res); 1046994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 10471da177e4SLinus Torvalds ClearPageReclaim(page); 10481da177e4SLinus Torvalds return PAGE_ACTIVATE; 10491da177e4SLinus Torvalds } 1050c661b078SAndy Whitcroft 10511da177e4SLinus Torvalds if (!PageWriteback(page)) { 10521da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 10531da177e4SLinus Torvalds ClearPageReclaim(page); 10541da177e4SLinus Torvalds } 10553aa23851Syalin wang trace_mm_vmscan_writepage(page); 1056c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_WRITE); 10571da177e4SLinus Torvalds return PAGE_SUCCESS; 10581da177e4SLinus Torvalds } 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds return PAGE_CLEAN; 10611da177e4SLinus Torvalds } 10621da177e4SLinus Torvalds 1063a649fd92SAndrew Morton /* 1064e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 1065e286781dSNick Piggin * gets returned with a refcount of 0. 1066a649fd92SAndrew Morton */ 1067a528910eSJohannes Weiner static int __remove_mapping(struct address_space *mapping, struct page *page, 1068b910718aSJohannes Weiner bool reclaimed, struct mem_cgroup *target_memcg) 106949d2e9ccSChristoph Lameter { 1070bd4c82c2SHuang Ying int refcount; 1071aae466b0SJoonsoo Kim void *shadow = NULL; 1072c4843a75SGreg Thelen 107328e4d965SNick Piggin BUG_ON(!PageLocked(page)); 107428e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 107549d2e9ccSChristoph Lameter 107630472509SJohannes Weiner xa_lock_irq(&mapping->i_pages); 107749d2e9ccSChristoph Lameter /* 10780fd0e6b0SNick Piggin * The non racy check for a busy page. 10790fd0e6b0SNick Piggin * 10800fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 10810fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 10820fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 10830fd0e6b0SNick Piggin * here, then the following race may occur: 10840fd0e6b0SNick Piggin * 10850fd0e6b0SNick Piggin * get_user_pages(&page); 10860fd0e6b0SNick Piggin * [user mapping goes away] 10870fd0e6b0SNick Piggin * write_to(page); 10880fd0e6b0SNick Piggin * !PageDirty(page) [good] 10890fd0e6b0SNick Piggin * SetPageDirty(page); 10900fd0e6b0SNick Piggin * put_page(page); 10910fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 10920fd0e6b0SNick Piggin * 10930fd0e6b0SNick Piggin * [oops, our write_to data is lost] 10940fd0e6b0SNick Piggin * 10950fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 10960fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 10970139aa7bSJoonsoo Kim * load is not satisfied before that of page->_refcount. 10980fd0e6b0SNick Piggin * 10990fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 1100b93b0163SMatthew Wilcox * and thus under the i_pages lock, then this ordering is not required. 110149d2e9ccSChristoph Lameter */ 1102906d278dSWilliam Kucharski refcount = 1 + compound_nr(page); 1103bd4c82c2SHuang Ying if (!page_ref_freeze(page, refcount)) 110449d2e9ccSChristoph Lameter goto cannot_free; 11051c4c3b99SJiang Biao /* note: atomic_cmpxchg in page_ref_freeze provides the smp_rmb */ 1106e286781dSNick Piggin if (unlikely(PageDirty(page))) { 1107bd4c82c2SHuang Ying page_ref_unfreeze(page, refcount); 110849d2e9ccSChristoph Lameter goto cannot_free; 1109e286781dSNick Piggin } 111049d2e9ccSChristoph Lameter 111149d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 111249d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 11130a31bc97SJohannes Weiner mem_cgroup_swapout(page, swap); 1114aae466b0SJoonsoo Kim if (reclaimed && !mapping_exiting(mapping)) 1115aae466b0SJoonsoo Kim shadow = workingset_eviction(page, target_memcg); 1116aae466b0SJoonsoo Kim __delete_from_swap_cache(page, swap, shadow); 111730472509SJohannes Weiner xa_unlock_irq(&mapping->i_pages); 111875f6d6d2SMinchan Kim put_swap_page(page, swap); 1119e286781dSNick Piggin } else { 11206072d13cSLinus Torvalds void (*freepage)(struct page *); 11216072d13cSLinus Torvalds 11226072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 1123a528910eSJohannes Weiner /* 1124a528910eSJohannes Weiner * Remember a shadow entry for reclaimed file cache in 1125a528910eSJohannes Weiner * order to detect refaults, thus thrashing, later on. 1126a528910eSJohannes Weiner * 1127a528910eSJohannes Weiner * But don't store shadows in an address space that is 1128238c3046Sdylan-meiners * already exiting. This is not just an optimization, 1129a528910eSJohannes Weiner * inode reclaim needs to empty out the radix tree or 1130a528910eSJohannes Weiner * the nodes are lost. Don't plant shadows behind its 1131a528910eSJohannes Weiner * back. 1132f9fe48beSRoss Zwisler * 1133f9fe48beSRoss Zwisler * We also don't store shadows for DAX mappings because the 1134f9fe48beSRoss Zwisler * only page cache pages found in these are zero pages 1135f9fe48beSRoss Zwisler * covering holes, and because we don't want to mix DAX 1136f9fe48beSRoss Zwisler * exceptional entries and shadow exceptional entries in the 1137b93b0163SMatthew Wilcox * same address_space. 1138a528910eSJohannes Weiner */ 11399de4f22aSHuang Ying if (reclaimed && page_is_file_lru(page) && 1140f9fe48beSRoss Zwisler !mapping_exiting(mapping) && !dax_mapping(mapping)) 1141b910718aSJohannes Weiner shadow = workingset_eviction(page, target_memcg); 114262cccb8cSJohannes Weiner __delete_from_page_cache(page, shadow); 114330472509SJohannes Weiner xa_unlock_irq(&mapping->i_pages); 11446072d13cSLinus Torvalds 11456072d13cSLinus Torvalds if (freepage != NULL) 11466072d13cSLinus Torvalds freepage(page); 1147e286781dSNick Piggin } 1148e286781dSNick Piggin 114949d2e9ccSChristoph Lameter return 1; 115049d2e9ccSChristoph Lameter 115149d2e9ccSChristoph Lameter cannot_free: 115230472509SJohannes Weiner xa_unlock_irq(&mapping->i_pages); 115349d2e9ccSChristoph Lameter return 0; 115449d2e9ccSChristoph Lameter } 115549d2e9ccSChristoph Lameter 11561da177e4SLinus Torvalds /* 1157e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 1158e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 1159e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 1160e286781dSNick Piggin * this page. 1161e286781dSNick Piggin */ 1162e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 1163e286781dSNick Piggin { 1164b910718aSJohannes Weiner if (__remove_mapping(mapping, page, false, NULL)) { 1165e286781dSNick Piggin /* 1166e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 1167e286781dSNick Piggin * drops the pagecache ref for us without requiring another 1168e286781dSNick Piggin * atomic operation. 1169e286781dSNick Piggin */ 1170fe896d18SJoonsoo Kim page_ref_unfreeze(page, 1); 1171e286781dSNick Piggin return 1; 1172e286781dSNick Piggin } 1173e286781dSNick Piggin return 0; 1174e286781dSNick Piggin } 1175e286781dSNick Piggin 1176894bc310SLee Schermerhorn /** 1177894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 1178894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 1179894bc310SLee Schermerhorn * 1180894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 1181894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 1182894bc310SLee Schermerhorn * 1183894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 1184894bc310SLee Schermerhorn */ 1185894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 1186894bc310SLee Schermerhorn { 1187c53954a0SMel Gorman lru_cache_add(page); 1188894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 1189894bc310SLee Schermerhorn } 1190894bc310SLee Schermerhorn 1191dfc8d636SJohannes Weiner enum page_references { 1192dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 1193dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 119464574746SJohannes Weiner PAGEREF_KEEP, 1195dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 1196dfc8d636SJohannes Weiner }; 1197dfc8d636SJohannes Weiner 1198dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 1199dfc8d636SJohannes Weiner struct scan_control *sc) 1200dfc8d636SJohannes Weiner { 120164574746SJohannes Weiner int referenced_ptes, referenced_page; 1202dfc8d636SJohannes Weiner unsigned long vm_flags; 1203dfc8d636SJohannes Weiner 1204c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 1205c3ac9a8aSJohannes Weiner &vm_flags); 120664574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 1207dfc8d636SJohannes Weiner 1208dfc8d636SJohannes Weiner /* 1209dfc8d636SJohannes Weiner * Mlock lost the isolation race with us. Let try_to_unmap() 1210dfc8d636SJohannes Weiner * move the page to the unevictable list. 1211dfc8d636SJohannes Weiner */ 1212dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 1213dfc8d636SJohannes Weiner return PAGEREF_RECLAIM; 1214dfc8d636SJohannes Weiner 121564574746SJohannes Weiner if (referenced_ptes) { 121664574746SJohannes Weiner /* 121764574746SJohannes Weiner * All mapped pages start out with page table 121864574746SJohannes Weiner * references from the instantiating fault, so we need 121964574746SJohannes Weiner * to look twice if a mapped file page is used more 122064574746SJohannes Weiner * than once. 122164574746SJohannes Weiner * 122264574746SJohannes Weiner * Mark it and spare it for another trip around the 122364574746SJohannes Weiner * inactive list. Another page table reference will 122464574746SJohannes Weiner * lead to its activation. 122564574746SJohannes Weiner * 122664574746SJohannes Weiner * Note: the mark is set for activated pages as well 122764574746SJohannes Weiner * so that recently deactivated but used pages are 122864574746SJohannes Weiner * quickly recovered. 122964574746SJohannes Weiner */ 123064574746SJohannes Weiner SetPageReferenced(page); 123164574746SJohannes Weiner 123234dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 1233dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 1234dfc8d636SJohannes Weiner 1235c909e993SKonstantin Khlebnikov /* 1236c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 1237c909e993SKonstantin Khlebnikov */ 1238b518154eSJoonsoo Kim if ((vm_flags & VM_EXEC) && !PageSwapBacked(page)) 1239c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 1240c909e993SKonstantin Khlebnikov 124164574746SJohannes Weiner return PAGEREF_KEEP; 124264574746SJohannes Weiner } 124364574746SJohannes Weiner 1244dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 12452e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 1246dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 124764574746SJohannes Weiner 124864574746SJohannes Weiner return PAGEREF_RECLAIM; 1249dfc8d636SJohannes Weiner } 1250dfc8d636SJohannes Weiner 1251e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 1252e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page, 1253e2be15f6SMel Gorman bool *dirty, bool *writeback) 1254e2be15f6SMel Gorman { 1255b4597226SMel Gorman struct address_space *mapping; 1256b4597226SMel Gorman 1257e2be15f6SMel Gorman /* 1258e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 1259e2be15f6SMel Gorman * from reclaim context. Do not stall reclaim based on them 1260e2be15f6SMel Gorman */ 12619de4f22aSHuang Ying if (!page_is_file_lru(page) || 1262802a3a92SShaohua Li (PageAnon(page) && !PageSwapBacked(page))) { 1263e2be15f6SMel Gorman *dirty = false; 1264e2be15f6SMel Gorman *writeback = false; 1265e2be15f6SMel Gorman return; 1266e2be15f6SMel Gorman } 1267e2be15f6SMel Gorman 1268e2be15f6SMel Gorman /* By default assume that the page flags are accurate */ 1269e2be15f6SMel Gorman *dirty = PageDirty(page); 1270e2be15f6SMel Gorman *writeback = PageWriteback(page); 1271b4597226SMel Gorman 1272b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 1273b4597226SMel Gorman if (!page_has_private(page)) 1274b4597226SMel Gorman return; 1275b4597226SMel Gorman 1276b4597226SMel Gorman mapping = page_mapping(page); 1277b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 1278b4597226SMel Gorman mapping->a_ops->is_dirty_writeback(page, dirty, writeback); 1279e2be15f6SMel Gorman } 1280e2be15f6SMel Gorman 128126aa2d19SDave Hansen static struct page *alloc_demote_page(struct page *page, unsigned long node) 128226aa2d19SDave Hansen { 128326aa2d19SDave Hansen struct migration_target_control mtc = { 128426aa2d19SDave Hansen /* 128526aa2d19SDave Hansen * Allocate from 'node', or fail quickly and quietly. 128626aa2d19SDave Hansen * When this happens, 'page' will likely just be discarded 128726aa2d19SDave Hansen * instead of migrated. 128826aa2d19SDave Hansen */ 128926aa2d19SDave Hansen .gfp_mask = (GFP_HIGHUSER_MOVABLE & ~__GFP_RECLAIM) | 129026aa2d19SDave Hansen __GFP_THISNODE | __GFP_NOWARN | 129126aa2d19SDave Hansen __GFP_NOMEMALLOC | GFP_NOWAIT, 129226aa2d19SDave Hansen .nid = node 129326aa2d19SDave Hansen }; 129426aa2d19SDave Hansen 129526aa2d19SDave Hansen return alloc_migration_target(page, (unsigned long)&mtc); 129626aa2d19SDave Hansen } 129726aa2d19SDave Hansen 129826aa2d19SDave Hansen /* 129926aa2d19SDave Hansen * Take pages on @demote_list and attempt to demote them to 130026aa2d19SDave Hansen * another node. Pages which are not demoted are left on 130126aa2d19SDave Hansen * @demote_pages. 130226aa2d19SDave Hansen */ 130326aa2d19SDave Hansen static unsigned int demote_page_list(struct list_head *demote_pages, 130426aa2d19SDave Hansen struct pglist_data *pgdat) 130526aa2d19SDave Hansen { 130626aa2d19SDave Hansen int target_nid = next_demotion_node(pgdat->node_id); 130726aa2d19SDave Hansen unsigned int nr_succeeded; 130826aa2d19SDave Hansen int err; 130926aa2d19SDave Hansen 131026aa2d19SDave Hansen if (list_empty(demote_pages)) 131126aa2d19SDave Hansen return 0; 131226aa2d19SDave Hansen 131326aa2d19SDave Hansen if (target_nid == NUMA_NO_NODE) 131426aa2d19SDave Hansen return 0; 131526aa2d19SDave Hansen 131626aa2d19SDave Hansen /* Demotion ignores all cpuset and mempolicy settings */ 131726aa2d19SDave Hansen err = migrate_pages(demote_pages, alloc_demote_page, NULL, 131826aa2d19SDave Hansen target_nid, MIGRATE_ASYNC, MR_DEMOTION, 131926aa2d19SDave Hansen &nr_succeeded); 132026aa2d19SDave Hansen 1321*668e4147SYang Shi if (current_is_kswapd()) 1322*668e4147SYang Shi __count_vm_events(PGDEMOTE_KSWAPD, nr_succeeded); 1323*668e4147SYang Shi else 1324*668e4147SYang Shi __count_vm_events(PGDEMOTE_DIRECT, nr_succeeded); 1325*668e4147SYang Shi 132626aa2d19SDave Hansen return nr_succeeded; 132726aa2d19SDave Hansen } 132826aa2d19SDave Hansen 1329e286781dSNick Piggin /* 13301742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 13311da177e4SLinus Torvalds */ 1332730ec8c0SManinder Singh static unsigned int shrink_page_list(struct list_head *page_list, 1333599d0c95SMel Gorman struct pglist_data *pgdat, 1334f84f6e2bSMel Gorman struct scan_control *sc, 13353c710c1aSMichal Hocko struct reclaim_stat *stat, 13368940b34aSMinchan Kim bool ignore_references) 13371da177e4SLinus Torvalds { 13381da177e4SLinus Torvalds LIST_HEAD(ret_pages); 1339abe4c3b5SMel Gorman LIST_HEAD(free_pages); 134026aa2d19SDave Hansen LIST_HEAD(demote_pages); 1341730ec8c0SManinder Singh unsigned int nr_reclaimed = 0; 1342730ec8c0SManinder Singh unsigned int pgactivate = 0; 134326aa2d19SDave Hansen bool do_demote_pass; 13441da177e4SLinus Torvalds 1345060f005fSKirill Tkhai memset(stat, 0, sizeof(*stat)); 13461da177e4SLinus Torvalds cond_resched(); 134726aa2d19SDave Hansen do_demote_pass = can_demote(pgdat->node_id, sc); 13481da177e4SLinus Torvalds 134926aa2d19SDave Hansen retry: 13501da177e4SLinus Torvalds while (!list_empty(page_list)) { 13511da177e4SLinus Torvalds struct address_space *mapping; 13521da177e4SLinus Torvalds struct page *page; 13538940b34aSMinchan Kim enum page_references references = PAGEREF_RECLAIM; 13544b793062SKirill Tkhai bool dirty, writeback, may_enter_fs; 135598879b3bSYang Shi unsigned int nr_pages; 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds cond_resched(); 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds page = lru_to_page(page_list); 13601da177e4SLinus Torvalds list_del(&page->lru); 13611da177e4SLinus Torvalds 1362529ae9aaSNick Piggin if (!trylock_page(page)) 13631da177e4SLinus Torvalds goto keep; 13641da177e4SLinus Torvalds 1365309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 13661da177e4SLinus Torvalds 1367d8c6546bSMatthew Wilcox (Oracle) nr_pages = compound_nr(page); 136898879b3bSYang Shi 136998879b3bSYang Shi /* Account the number of base pages even though THP */ 137098879b3bSYang Shi sc->nr_scanned += nr_pages; 137180e43426SChristoph Lameter 137239b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) 1373ad6b6704SMinchan Kim goto activate_locked; 1374894bc310SLee Schermerhorn 1375a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 137680e43426SChristoph Lameter goto keep_locked; 137780e43426SChristoph Lameter 1378c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 1379c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 1380c661b078SAndy Whitcroft 1381e62e384eSMichal Hocko /* 1382894befecSAndrey Ryabinin * The number of dirty pages determines if a node is marked 1383e2be15f6SMel Gorman * reclaim_congested which affects wait_iff_congested. kswapd 1384e2be15f6SMel Gorman * will stall and start writing pages if the tail of the LRU 1385e2be15f6SMel Gorman * is all dirty unqueued pages. 1386e2be15f6SMel Gorman */ 1387e2be15f6SMel Gorman page_check_dirty_writeback(page, &dirty, &writeback); 1388e2be15f6SMel Gorman if (dirty || writeback) 1389060f005fSKirill Tkhai stat->nr_dirty++; 1390e2be15f6SMel Gorman 1391e2be15f6SMel Gorman if (dirty && !writeback) 1392060f005fSKirill Tkhai stat->nr_unqueued_dirty++; 1393e2be15f6SMel Gorman 1394d04e8acdSMel Gorman /* 1395d04e8acdSMel Gorman * Treat this page as congested if the underlying BDI is or if 1396d04e8acdSMel Gorman * pages are cycling through the LRU so quickly that the 1397d04e8acdSMel Gorman * pages marked for immediate reclaim are making it to the 1398d04e8acdSMel Gorman * end of the LRU a second time. 1399d04e8acdSMel Gorman */ 1400e2be15f6SMel Gorman mapping = page_mapping(page); 14011da58ee2SJamie Liu if (((dirty || writeback) && mapping && 1402703c2708STejun Heo inode_write_congested(mapping->host)) || 1403d04e8acdSMel Gorman (writeback && PageReclaim(page))) 1404060f005fSKirill Tkhai stat->nr_congested++; 1405e2be15f6SMel Gorman 1406e2be15f6SMel Gorman /* 1407283aba9fSMel Gorman * If a page at the tail of the LRU is under writeback, there 1408283aba9fSMel Gorman * are three cases to consider. 1409e62e384eSMichal Hocko * 1410283aba9fSMel Gorman * 1) If reclaim is encountering an excessive number of pages 1411283aba9fSMel Gorman * under writeback and this page is both under writeback and 1412283aba9fSMel Gorman * PageReclaim then it indicates that pages are being queued 1413283aba9fSMel Gorman * for IO but are being recycled through the LRU before the 1414283aba9fSMel Gorman * IO can complete. Waiting on the page itself risks an 1415283aba9fSMel Gorman * indefinite stall if it is impossible to writeback the 1416283aba9fSMel Gorman * page due to IO error or disconnected storage so instead 1417b1a6f21eSMel Gorman * note that the LRU is being scanned too quickly and the 1418b1a6f21eSMel Gorman * caller can stall after page list has been processed. 1419c3b94f44SHugh Dickins * 142097c9341fSTejun Heo * 2) Global or new memcg reclaim encounters a page that is 1421ecf5fc6eSMichal Hocko * not marked for immediate reclaim, or the caller does not 1422ecf5fc6eSMichal Hocko * have __GFP_FS (or __GFP_IO if it's simply going to swap, 1423ecf5fc6eSMichal Hocko * not to fs). In this case mark the page for immediate 142497c9341fSTejun Heo * reclaim and continue scanning. 1425283aba9fSMel Gorman * 1426ecf5fc6eSMichal Hocko * Require may_enter_fs because we would wait on fs, which 1427ecf5fc6eSMichal Hocko * may not have submitted IO yet. And the loop driver might 1428283aba9fSMel Gorman * enter reclaim, and deadlock if it waits on a page for 1429283aba9fSMel Gorman * which it is needed to do the write (loop masks off 1430283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 1431283aba9fSMel Gorman * would probably show more reasons. 1432283aba9fSMel Gorman * 14337fadc820SHugh Dickins * 3) Legacy memcg encounters a page that is already marked 1434283aba9fSMel Gorman * PageReclaim. memcg does not have any dirty pages 1435283aba9fSMel Gorman * throttling so we could easily OOM just because too many 1436283aba9fSMel Gorman * pages are in writeback and there is nothing else to 1437283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 1438c55e8d03SJohannes Weiner * 1439c55e8d03SJohannes Weiner * In cases 1) and 2) we activate the pages to get them out of 1440c55e8d03SJohannes Weiner * the way while we continue scanning for clean pages on the 1441c55e8d03SJohannes Weiner * inactive list and refilling from the active list. The 1442c55e8d03SJohannes Weiner * observation here is that waiting for disk writes is more 1443c55e8d03SJohannes Weiner * expensive than potentially causing reloads down the line. 1444c55e8d03SJohannes Weiner * Since they're marked for immediate reclaim, they won't put 1445c55e8d03SJohannes Weiner * memory pressure on the cache working set any longer than it 1446c55e8d03SJohannes Weiner * takes to write them to disk. 1447e62e384eSMichal Hocko */ 1448283aba9fSMel Gorman if (PageWriteback(page)) { 1449283aba9fSMel Gorman /* Case 1 above */ 1450283aba9fSMel Gorman if (current_is_kswapd() && 1451283aba9fSMel Gorman PageReclaim(page) && 1452599d0c95SMel Gorman test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { 1453060f005fSKirill Tkhai stat->nr_immediate++; 1454c55e8d03SJohannes Weiner goto activate_locked; 1455283aba9fSMel Gorman 1456283aba9fSMel Gorman /* Case 2 above */ 1457b5ead35eSJohannes Weiner } else if (writeback_throttling_sane(sc) || 1458ecf5fc6eSMichal Hocko !PageReclaim(page) || !may_enter_fs) { 1459c3b94f44SHugh Dickins /* 1460c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 1461c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 1462c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 1463c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 1464c3b94f44SHugh Dickins * enough to care. What we do want is for this 1465c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 1466c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 1467c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 1468c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 1469c3b94f44SHugh Dickins */ 1470c3b94f44SHugh Dickins SetPageReclaim(page); 1471060f005fSKirill Tkhai stat->nr_writeback++; 1472c55e8d03SJohannes Weiner goto activate_locked; 1473283aba9fSMel Gorman 1474283aba9fSMel Gorman /* Case 3 above */ 1475283aba9fSMel Gorman } else { 14767fadc820SHugh Dickins unlock_page(page); 1477c3b94f44SHugh Dickins wait_on_page_writeback(page); 14787fadc820SHugh Dickins /* then go back and try same page again */ 14797fadc820SHugh Dickins list_add_tail(&page->lru, page_list); 14807fadc820SHugh Dickins continue; 1481e62e384eSMichal Hocko } 1482283aba9fSMel Gorman } 14831da177e4SLinus Torvalds 14848940b34aSMinchan Kim if (!ignore_references) 14856a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 148602c6de8dSMinchan Kim 1487dfc8d636SJohannes Weiner switch (references) { 1488dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 14891da177e4SLinus Torvalds goto activate_locked; 149064574746SJohannes Weiner case PAGEREF_KEEP: 149198879b3bSYang Shi stat->nr_ref_keep += nr_pages; 149264574746SJohannes Weiner goto keep_locked; 1493dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 1494dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 1495dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 1496dfc8d636SJohannes Weiner } 14971da177e4SLinus Torvalds 14981da177e4SLinus Torvalds /* 149926aa2d19SDave Hansen * Before reclaiming the page, try to relocate 150026aa2d19SDave Hansen * its contents to another node. 150126aa2d19SDave Hansen */ 150226aa2d19SDave Hansen if (do_demote_pass && 150326aa2d19SDave Hansen (thp_migration_supported() || !PageTransHuge(page))) { 150426aa2d19SDave Hansen list_add(&page->lru, &demote_pages); 150526aa2d19SDave Hansen unlock_page(page); 150626aa2d19SDave Hansen continue; 150726aa2d19SDave Hansen } 150826aa2d19SDave Hansen 150926aa2d19SDave Hansen /* 15101da177e4SLinus Torvalds * Anonymous process memory has backing store? 15111da177e4SLinus Torvalds * Try to allocate it some swap space here. 1512802a3a92SShaohua Li * Lazyfree page could be freed directly 15131da177e4SLinus Torvalds */ 1514bd4c82c2SHuang Ying if (PageAnon(page) && PageSwapBacked(page)) { 1515bd4c82c2SHuang Ying if (!PageSwapCache(page)) { 151663eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 151763eb6b93SHugh Dickins goto keep_locked; 1518feb889fbSLinus Torvalds if (page_maybe_dma_pinned(page)) 1519feb889fbSLinus Torvalds goto keep_locked; 1520747552b1SHuang Ying if (PageTransHuge(page)) { 1521b8f593cdSHuang Ying /* cannot split THP, skip it */ 1522747552b1SHuang Ying if (!can_split_huge_page(page, NULL)) 1523b8f593cdSHuang Ying goto activate_locked; 1524747552b1SHuang Ying /* 1525747552b1SHuang Ying * Split pages without a PMD map right 1526747552b1SHuang Ying * away. Chances are some or all of the 1527747552b1SHuang Ying * tail pages can be freed without IO. 1528747552b1SHuang Ying */ 1529747552b1SHuang Ying if (!compound_mapcount(page) && 1530bd4c82c2SHuang Ying split_huge_page_to_list(page, 1531bd4c82c2SHuang Ying page_list)) 1532747552b1SHuang Ying goto activate_locked; 1533747552b1SHuang Ying } 15340f074658SMinchan Kim if (!add_to_swap(page)) { 15350f074658SMinchan Kim if (!PageTransHuge(page)) 153698879b3bSYang Shi goto activate_locked_split; 1537bd4c82c2SHuang Ying /* Fallback to swap normal pages */ 1538bd4c82c2SHuang Ying if (split_huge_page_to_list(page, 1539bd4c82c2SHuang Ying page_list)) 15400f074658SMinchan Kim goto activate_locked; 1541fe490cc0SHuang Ying #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1542fe490cc0SHuang Ying count_vm_event(THP_SWPOUT_FALLBACK); 1543fe490cc0SHuang Ying #endif 15440f074658SMinchan Kim if (!add_to_swap(page)) 154598879b3bSYang Shi goto activate_locked_split; 15460f074658SMinchan Kim } 15470f074658SMinchan Kim 15484b793062SKirill Tkhai may_enter_fs = true; 15491da177e4SLinus Torvalds 1550e2be15f6SMel Gorman /* Adding to swap updated mapping */ 15511da177e4SLinus Torvalds mapping = page_mapping(page); 1552bd4c82c2SHuang Ying } 15537751b2daSKirill A. Shutemov } else if (unlikely(PageTransHuge(page))) { 15547751b2daSKirill A. Shutemov /* Split file THP */ 15557751b2daSKirill A. Shutemov if (split_huge_page_to_list(page, page_list)) 15567751b2daSKirill A. Shutemov goto keep_locked; 1557e2be15f6SMel Gorman } 15581da177e4SLinus Torvalds 15591da177e4SLinus Torvalds /* 156098879b3bSYang Shi * THP may get split above, need minus tail pages and update 156198879b3bSYang Shi * nr_pages to avoid accounting tail pages twice. 156298879b3bSYang Shi * 156398879b3bSYang Shi * The tail pages that are added into swap cache successfully 156498879b3bSYang Shi * reach here. 156598879b3bSYang Shi */ 156698879b3bSYang Shi if ((nr_pages > 1) && !PageTransHuge(page)) { 156798879b3bSYang Shi sc->nr_scanned -= (nr_pages - 1); 156898879b3bSYang Shi nr_pages = 1; 156998879b3bSYang Shi } 157098879b3bSYang Shi 157198879b3bSYang Shi /* 15721da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 15731da177e4SLinus Torvalds * processes. Try to unmap it here. 15741da177e4SLinus Torvalds */ 1575802a3a92SShaohua Li if (page_mapped(page)) { 1576013339dfSShakeel Butt enum ttu_flags flags = TTU_BATCH_FLUSH; 15771f318a9bSJaewon Kim bool was_swapbacked = PageSwapBacked(page); 1578bd4c82c2SHuang Ying 1579bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page))) 1580bd4c82c2SHuang Ying flags |= TTU_SPLIT_HUGE_PMD; 15811f318a9bSJaewon Kim 15821fb08ac6SYang Shi try_to_unmap(page, flags); 15831fb08ac6SYang Shi if (page_mapped(page)) { 158498879b3bSYang Shi stat->nr_unmap_fail += nr_pages; 15851f318a9bSJaewon Kim if (!was_swapbacked && PageSwapBacked(page)) 15861f318a9bSJaewon Kim stat->nr_lazyfree_fail += nr_pages; 15871da177e4SLinus Torvalds goto activate_locked; 15881da177e4SLinus Torvalds } 15891da177e4SLinus Torvalds } 15901da177e4SLinus Torvalds 15911da177e4SLinus Torvalds if (PageDirty(page)) { 1592ee72886dSMel Gorman /* 15934eda4823SJohannes Weiner * Only kswapd can writeback filesystem pages 15944eda4823SJohannes Weiner * to avoid risk of stack overflow. But avoid 15954eda4823SJohannes Weiner * injecting inefficient single-page IO into 15964eda4823SJohannes Weiner * flusher writeback as much as possible: only 15974eda4823SJohannes Weiner * write pages when we've encountered many 15984eda4823SJohannes Weiner * dirty pages, and when we've already scanned 15994eda4823SJohannes Weiner * the rest of the LRU for clean pages and see 16004eda4823SJohannes Weiner * the same dirty pages again (PageReclaim). 1601ee72886dSMel Gorman */ 16029de4f22aSHuang Ying if (page_is_file_lru(page) && 16034eda4823SJohannes Weiner (!current_is_kswapd() || !PageReclaim(page) || 1604599d0c95SMel Gorman !test_bit(PGDAT_DIRTY, &pgdat->flags))) { 160549ea7eb6SMel Gorman /* 160649ea7eb6SMel Gorman * Immediately reclaim when written back. 160749ea7eb6SMel Gorman * Similar in principal to deactivate_page() 160849ea7eb6SMel Gorman * except we already have the page isolated 160949ea7eb6SMel Gorman * and know it's dirty 161049ea7eb6SMel Gorman */ 1611c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_IMMEDIATE); 161249ea7eb6SMel Gorman SetPageReclaim(page); 161349ea7eb6SMel Gorman 1614c55e8d03SJohannes Weiner goto activate_locked; 1615ee72886dSMel Gorman } 1616ee72886dSMel Gorman 1617dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 16181da177e4SLinus Torvalds goto keep_locked; 16194dd4b920SAndrew Morton if (!may_enter_fs) 16201da177e4SLinus Torvalds goto keep_locked; 162152a8363eSChristoph Lameter if (!sc->may_writepage) 16221da177e4SLinus Torvalds goto keep_locked; 16231da177e4SLinus Torvalds 1624d950c947SMel Gorman /* 1625d950c947SMel Gorman * Page is dirty. Flush the TLB if a writable entry 1626d950c947SMel Gorman * potentially exists to avoid CPU writes after IO 1627d950c947SMel Gorman * starts and then write it out here. 1628d950c947SMel Gorman */ 1629d950c947SMel Gorman try_to_unmap_flush_dirty(); 1630cb16556dSYang Shi switch (pageout(page, mapping)) { 16311da177e4SLinus Torvalds case PAGE_KEEP: 16321da177e4SLinus Torvalds goto keep_locked; 16331da177e4SLinus Torvalds case PAGE_ACTIVATE: 16341da177e4SLinus Torvalds goto activate_locked; 16351da177e4SLinus Torvalds case PAGE_SUCCESS: 16366c357848SMatthew Wilcox (Oracle) stat->nr_pageout += thp_nr_pages(page); 163796f8bf4fSJohannes Weiner 16387d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 163941ac1999SMel Gorman goto keep; 16407d3579e8SKOSAKI Motohiro if (PageDirty(page)) 16411da177e4SLinus Torvalds goto keep; 16427d3579e8SKOSAKI Motohiro 16431da177e4SLinus Torvalds /* 16441da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 16451da177e4SLinus Torvalds * ahead and try to reclaim the page. 16461da177e4SLinus Torvalds */ 1647529ae9aaSNick Piggin if (!trylock_page(page)) 16481da177e4SLinus Torvalds goto keep; 16491da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 16501da177e4SLinus Torvalds goto keep_locked; 16511da177e4SLinus Torvalds mapping = page_mapping(page); 165201359eb2SGustavo A. R. Silva fallthrough; 16531da177e4SLinus Torvalds case PAGE_CLEAN: 16541da177e4SLinus Torvalds ; /* try to free the page below */ 16551da177e4SLinus Torvalds } 16561da177e4SLinus Torvalds } 16571da177e4SLinus Torvalds 16581da177e4SLinus Torvalds /* 16591da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 16601da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 16611da177e4SLinus Torvalds * the page as well. 16621da177e4SLinus Torvalds * 16631da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 16641da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 16651da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 16661da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 16671da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 16681da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 16691da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 16701da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 16711da177e4SLinus Torvalds * 16721da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 16731da177e4SLinus Torvalds * the pages which were not successfully invalidated in 1674d12b8951SYang Shi * truncate_cleanup_page(). We try to drop those buffers here 16751da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 16761da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 16771da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 16781da177e4SLinus Torvalds */ 1679266cf658SDavid Howells if (page_has_private(page)) { 16801da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 16811da177e4SLinus Torvalds goto activate_locked; 1682e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 1683e286781dSNick Piggin unlock_page(page); 1684e286781dSNick Piggin if (put_page_testzero(page)) 16851da177e4SLinus Torvalds goto free_it; 1686e286781dSNick Piggin else { 1687e286781dSNick Piggin /* 1688e286781dSNick Piggin * rare race with speculative reference. 1689e286781dSNick Piggin * the speculative reference will free 1690e286781dSNick Piggin * this page shortly, so we may 1691e286781dSNick Piggin * increment nr_reclaimed here (and 1692e286781dSNick Piggin * leave it off the LRU). 1693e286781dSNick Piggin */ 1694e286781dSNick Piggin nr_reclaimed++; 1695e286781dSNick Piggin continue; 1696e286781dSNick Piggin } 1697e286781dSNick Piggin } 16981da177e4SLinus Torvalds } 16991da177e4SLinus Torvalds 1700802a3a92SShaohua Li if (PageAnon(page) && !PageSwapBacked(page)) { 1701802a3a92SShaohua Li /* follow __remove_mapping for reference */ 1702802a3a92SShaohua Li if (!page_ref_freeze(page, 1)) 170349d2e9ccSChristoph Lameter goto keep_locked; 1704802a3a92SShaohua Li if (PageDirty(page)) { 1705802a3a92SShaohua Li page_ref_unfreeze(page, 1); 1706802a3a92SShaohua Li goto keep_locked; 1707802a3a92SShaohua Li } 17081da177e4SLinus Torvalds 1709802a3a92SShaohua Li count_vm_event(PGLAZYFREED); 17102262185cSRoman Gushchin count_memcg_page_event(page, PGLAZYFREED); 1711b910718aSJohannes Weiner } else if (!mapping || !__remove_mapping(mapping, page, true, 1712b910718aSJohannes Weiner sc->target_mem_cgroup)) 1713802a3a92SShaohua Li goto keep_locked; 17149a1ea439SHugh Dickins 17159a1ea439SHugh Dickins unlock_page(page); 1716e286781dSNick Piggin free_it: 171798879b3bSYang Shi /* 171898879b3bSYang Shi * THP may get swapped out in a whole, need account 171998879b3bSYang Shi * all base pages. 172098879b3bSYang Shi */ 172198879b3bSYang Shi nr_reclaimed += nr_pages; 1722abe4c3b5SMel Gorman 1723abe4c3b5SMel Gorman /* 1724abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1725abe4c3b5SMel Gorman * appear not as the counts should be low 1726abe4c3b5SMel Gorman */ 17277ae88534SYang Shi if (unlikely(PageTransHuge(page))) 1728ff45fc3cSMatthew Wilcox (Oracle) destroy_compound_page(page); 17297ae88534SYang Shi else 1730abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 17311da177e4SLinus Torvalds continue; 17321da177e4SLinus Torvalds 173398879b3bSYang Shi activate_locked_split: 173498879b3bSYang Shi /* 173598879b3bSYang Shi * The tail pages that are failed to add into swap cache 173698879b3bSYang Shi * reach here. Fixup nr_scanned and nr_pages. 173798879b3bSYang Shi */ 173898879b3bSYang Shi if (nr_pages > 1) { 173998879b3bSYang Shi sc->nr_scanned -= (nr_pages - 1); 174098879b3bSYang Shi nr_pages = 1; 174198879b3bSYang Shi } 17421da177e4SLinus Torvalds activate_locked: 174368a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 1744ad6b6704SMinchan Kim if (PageSwapCache(page) && (mem_cgroup_swap_full(page) || 1745ad6b6704SMinchan Kim PageMlocked(page))) 1746a2c43eedSHugh Dickins try_to_free_swap(page); 1747309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 1748ad6b6704SMinchan Kim if (!PageMlocked(page)) { 17499de4f22aSHuang Ying int type = page_is_file_lru(page); 17501da177e4SLinus Torvalds SetPageActive(page); 175198879b3bSYang Shi stat->nr_activate[type] += nr_pages; 17522262185cSRoman Gushchin count_memcg_page_event(page, PGACTIVATE); 1753ad6b6704SMinchan Kim } 17541da177e4SLinus Torvalds keep_locked: 17551da177e4SLinus Torvalds unlock_page(page); 17561da177e4SLinus Torvalds keep: 17571da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 1758309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); 17591da177e4SLinus Torvalds } 176026aa2d19SDave Hansen /* 'page_list' is always empty here */ 176126aa2d19SDave Hansen 176226aa2d19SDave Hansen /* Migrate pages selected for demotion */ 176326aa2d19SDave Hansen nr_reclaimed += demote_page_list(&demote_pages, pgdat); 176426aa2d19SDave Hansen /* Pages that could not be demoted are still in @demote_pages */ 176526aa2d19SDave Hansen if (!list_empty(&demote_pages)) { 176626aa2d19SDave Hansen /* Pages which failed to demoted go back on @page_list for retry: */ 176726aa2d19SDave Hansen list_splice_init(&demote_pages, page_list); 176826aa2d19SDave Hansen do_demote_pass = false; 176926aa2d19SDave Hansen goto retry; 177026aa2d19SDave Hansen } 1771abe4c3b5SMel Gorman 177298879b3bSYang Shi pgactivate = stat->nr_activate[0] + stat->nr_activate[1]; 177398879b3bSYang Shi 1774747db954SJohannes Weiner mem_cgroup_uncharge_list(&free_pages); 177572b252aeSMel Gorman try_to_unmap_flush(); 17762d4894b5SMel Gorman free_unref_page_list(&free_pages); 1777abe4c3b5SMel Gorman 17781da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1779886cf190SKirill Tkhai count_vm_events(PGACTIVATE, pgactivate); 17800a31bc97SJohannes Weiner 178105ff5137SAndrew Morton return nr_reclaimed; 17821da177e4SLinus Torvalds } 17831da177e4SLinus Torvalds 1784730ec8c0SManinder Singh unsigned int reclaim_clean_pages_from_list(struct zone *zone, 178502c6de8dSMinchan Kim struct list_head *page_list) 178602c6de8dSMinchan Kim { 178702c6de8dSMinchan Kim struct scan_control sc = { 178802c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 178902c6de8dSMinchan Kim .priority = DEF_PRIORITY, 179002c6de8dSMinchan Kim .may_unmap = 1, 179102c6de8dSMinchan Kim }; 17921f318a9bSJaewon Kim struct reclaim_stat stat; 1793730ec8c0SManinder Singh unsigned int nr_reclaimed; 179402c6de8dSMinchan Kim struct page *page, *next; 179502c6de8dSMinchan Kim LIST_HEAD(clean_pages); 17962d2b8d2bSYu Zhao unsigned int noreclaim_flag; 179702c6de8dSMinchan Kim 179802c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 1799ae37c7ffSOscar Salvador if (!PageHuge(page) && page_is_file_lru(page) && 1800ae37c7ffSOscar Salvador !PageDirty(page) && !__PageMovable(page) && 1801ae37c7ffSOscar Salvador !PageUnevictable(page)) { 180202c6de8dSMinchan Kim ClearPageActive(page); 180302c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 180402c6de8dSMinchan Kim } 180502c6de8dSMinchan Kim } 180602c6de8dSMinchan Kim 18072d2b8d2bSYu Zhao /* 18082d2b8d2bSYu Zhao * We should be safe here since we are only dealing with file pages and 18092d2b8d2bSYu Zhao * we are not kswapd and therefore cannot write dirty file pages. But 18102d2b8d2bSYu Zhao * call memalloc_noreclaim_save() anyway, just in case these conditions 18112d2b8d2bSYu Zhao * change in the future. 18122d2b8d2bSYu Zhao */ 18132d2b8d2bSYu Zhao noreclaim_flag = memalloc_noreclaim_save(); 18141f318a9bSJaewon Kim nr_reclaimed = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc, 1815013339dfSShakeel Butt &stat, true); 18162d2b8d2bSYu Zhao memalloc_noreclaim_restore(noreclaim_flag); 18172d2b8d2bSYu Zhao 181802c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 18192da9f630SNicholas Piggin mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, 18202da9f630SNicholas Piggin -(long)nr_reclaimed); 18211f318a9bSJaewon Kim /* 18221f318a9bSJaewon Kim * Since lazyfree pages are isolated from file LRU from the beginning, 18231f318a9bSJaewon Kim * they will rotate back to anonymous LRU in the end if it failed to 18241f318a9bSJaewon Kim * discard so isolated count will be mismatched. 18251f318a9bSJaewon Kim * Compensate the isolated count for both LRU lists. 18261f318a9bSJaewon Kim */ 18271f318a9bSJaewon Kim mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON, 18281f318a9bSJaewon Kim stat.nr_lazyfree_fail); 18291f318a9bSJaewon Kim mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, 18302da9f630SNicholas Piggin -(long)stat.nr_lazyfree_fail); 18311f318a9bSJaewon Kim return nr_reclaimed; 183202c6de8dSMinchan Kim } 183302c6de8dSMinchan Kim 18345ad333ebSAndy Whitcroft /* 18355ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 18365ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 18375ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 18385ad333ebSAndy Whitcroft * 18395ad333ebSAndy Whitcroft * page: page to consider 18405ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 18415ad333ebSAndy Whitcroft * 1842c2135f7cSAlex Shi * returns true on success, false on failure. 18435ad333ebSAndy Whitcroft */ 1844c2135f7cSAlex Shi bool __isolate_lru_page_prepare(struct page *page, isolate_mode_t mode) 18455ad333ebSAndy Whitcroft { 18465ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 18475ad333ebSAndy Whitcroft if (!PageLRU(page)) 1848c2135f7cSAlex Shi return false; 18495ad333ebSAndy Whitcroft 1850e46a2879SMinchan Kim /* Compaction should not handle unevictable pages but CMA can do so */ 1851e46a2879SMinchan Kim if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) 1852c2135f7cSAlex Shi return false; 1853894bc310SLee Schermerhorn 1854c8244935SMel Gorman /* 1855c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 1856c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 1857c8244935SMel Gorman * blocking - clean pages for the most part. 1858c8244935SMel Gorman * 1859c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 1860c8244935SMel Gorman * that it is possible to migrate without blocking 1861c8244935SMel Gorman */ 18621276ad68SJohannes Weiner if (mode & ISOLATE_ASYNC_MIGRATE) { 1863c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 1864c8244935SMel Gorman if (PageWriteback(page)) 1865c2135f7cSAlex Shi return false; 186639deaf85SMinchan Kim 1867c8244935SMel Gorman if (PageDirty(page)) { 1868c8244935SMel Gorman struct address_space *mapping; 186969d763fcSMel Gorman bool migrate_dirty; 1870c8244935SMel Gorman 1871c8244935SMel Gorman /* 1872c8244935SMel Gorman * Only pages without mappings or that have a 1873c8244935SMel Gorman * ->migratepage callback are possible to migrate 187469d763fcSMel Gorman * without blocking. However, we can be racing with 187569d763fcSMel Gorman * truncation so it's necessary to lock the page 187669d763fcSMel Gorman * to stabilise the mapping as truncation holds 187769d763fcSMel Gorman * the page lock until after the page is removed 187869d763fcSMel Gorman * from the page cache. 1879c8244935SMel Gorman */ 188069d763fcSMel Gorman if (!trylock_page(page)) 1881c2135f7cSAlex Shi return false; 188269d763fcSMel Gorman 1883c8244935SMel Gorman mapping = page_mapping(page); 1884145e1a71SHugh Dickins migrate_dirty = !mapping || mapping->a_ops->migratepage; 188569d763fcSMel Gorman unlock_page(page); 188669d763fcSMel Gorman if (!migrate_dirty) 1887c2135f7cSAlex Shi return false; 1888c8244935SMel Gorman } 1889c8244935SMel Gorman } 1890c8244935SMel Gorman 1891f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 1892c2135f7cSAlex Shi return false; 1893f80c0673SMinchan Kim 1894c2135f7cSAlex Shi return true; 18955ad333ebSAndy Whitcroft } 18965ad333ebSAndy Whitcroft 18977ee36a14SMel Gorman /* 18987ee36a14SMel Gorman * Update LRU sizes after isolating pages. The LRU size updates must 189955b65a57SEthon Paul * be complete before mem_cgroup_update_lru_size due to a sanity check. 19007ee36a14SMel Gorman */ 19017ee36a14SMel Gorman static __always_inline void update_lru_sizes(struct lruvec *lruvec, 1902b4536f0cSMichal Hocko enum lru_list lru, unsigned long *nr_zone_taken) 19037ee36a14SMel Gorman { 19047ee36a14SMel Gorman int zid; 19057ee36a14SMel Gorman 19067ee36a14SMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 19077ee36a14SMel Gorman if (!nr_zone_taken[zid]) 19087ee36a14SMel Gorman continue; 19097ee36a14SMel Gorman 1910a892cb6bSWei Yang update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 19117ee36a14SMel Gorman } 19127ee36a14SMel Gorman 19137ee36a14SMel Gorman } 19147ee36a14SMel Gorman 1915f611fab7SMel Gorman /* 191615b44736SHugh Dickins * Isolating page from the lruvec to fill in @dst list by nr_to_scan times. 191715b44736SHugh Dickins * 191815b44736SHugh Dickins * lruvec->lru_lock is heavily contended. Some of the functions that 19191da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 19201da177e4SLinus Torvalds * and working on them outside the LRU lock. 19211da177e4SLinus Torvalds * 19221da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 19231da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 19241da177e4SLinus Torvalds * 192515b44736SHugh Dickins * Lru_lock must be held before calling this function. 19261da177e4SLinus Torvalds * 1927791b48b6SMinchan Kim * @nr_to_scan: The number of eligible pages to look through on the list. 19285dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 19291da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1930f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1931fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 19323cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 19331da177e4SLinus Torvalds * 19341da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 19351da177e4SLinus Torvalds */ 193669e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 19375dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1938fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 1939a9e7c39fSKirill Tkhai enum lru_list lru) 19401da177e4SLinus Torvalds { 194175b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 194269e05944SAndrew Morton unsigned long nr_taken = 0; 1943599d0c95SMel Gorman unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; 19447cc30fcfSMel Gorman unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; 19453db65812SJohannes Weiner unsigned long skipped = 0; 1946791b48b6SMinchan Kim unsigned long scan, total_scan, nr_pages; 1947b2e18757SMel Gorman LIST_HEAD(pages_skipped); 1948a9e7c39fSKirill Tkhai isolate_mode_t mode = (sc->may_unmap ? 0 : ISOLATE_UNMAPPED); 19491da177e4SLinus Torvalds 195098879b3bSYang Shi total_scan = 0; 1951791b48b6SMinchan Kim scan = 0; 195298879b3bSYang Shi while (scan < nr_to_scan && !list_empty(src)) { 19535ad333ebSAndy Whitcroft struct page *page; 19545ad333ebSAndy Whitcroft 19551da177e4SLinus Torvalds page = lru_to_page(src); 19561da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 19571da177e4SLinus Torvalds 1958d8c6546bSMatthew Wilcox (Oracle) nr_pages = compound_nr(page); 195998879b3bSYang Shi total_scan += nr_pages; 196098879b3bSYang Shi 1961b2e18757SMel Gorman if (page_zonenum(page) > sc->reclaim_idx) { 1962b2e18757SMel Gorman list_move(&page->lru, &pages_skipped); 196398879b3bSYang Shi nr_skipped[page_zonenum(page)] += nr_pages; 1964b2e18757SMel Gorman continue; 1965b2e18757SMel Gorman } 1966b2e18757SMel Gorman 1967791b48b6SMinchan Kim /* 1968791b48b6SMinchan Kim * Do not count skipped pages because that makes the function 1969791b48b6SMinchan Kim * return with no isolated pages if the LRU mostly contains 1970791b48b6SMinchan Kim * ineligible pages. This causes the VM to not reclaim any 1971791b48b6SMinchan Kim * pages, triggering a premature OOM. 197298879b3bSYang Shi * 197398879b3bSYang Shi * Account all tail pages of THP. This would not cause 197498879b3bSYang Shi * premature OOM since __isolate_lru_page() returns -EBUSY 197598879b3bSYang Shi * only when the page is being freed somewhere else. 1976791b48b6SMinchan Kim */ 197798879b3bSYang Shi scan += nr_pages; 1978c2135f7cSAlex Shi if (!__isolate_lru_page_prepare(page, mode)) { 1979c2135f7cSAlex Shi /* It is being freed elsewhere */ 1980c2135f7cSAlex Shi list_move(&page->lru, src); 1981c2135f7cSAlex Shi continue; 1982c2135f7cSAlex Shi } 19839df41314SAlex Shi /* 19849df41314SAlex Shi * Be careful not to clear PageLRU until after we're 19859df41314SAlex Shi * sure the page is not being freed elsewhere -- the 19869df41314SAlex Shi * page release code relies on it. 19879df41314SAlex Shi */ 1988c2135f7cSAlex Shi if (unlikely(!get_page_unless_zero(page))) { 1989c2135f7cSAlex Shi list_move(&page->lru, src); 1990c2135f7cSAlex Shi continue; 1991c2135f7cSAlex Shi } 19929df41314SAlex Shi 19939df41314SAlex Shi if (!TestClearPageLRU(page)) { 1994c2135f7cSAlex Shi /* Another thread is already isolating this page */ 19959df41314SAlex Shi put_page(page); 1996c2135f7cSAlex Shi list_move(&page->lru, src); 1997c2135f7cSAlex Shi continue; 19989df41314SAlex Shi } 19999df41314SAlex Shi 2000599d0c95SMel Gorman nr_taken += nr_pages; 2001599d0c95SMel Gorman nr_zone_taken[page_zonenum(page)] += nr_pages; 20025ad333ebSAndy Whitcroft list_move(&page->lru, dst); 20035ad333ebSAndy Whitcroft } 20041da177e4SLinus Torvalds 2005b2e18757SMel Gorman /* 2006b2e18757SMel Gorman * Splice any skipped pages to the start of the LRU list. Note that 2007b2e18757SMel Gorman * this disrupts the LRU order when reclaiming for lower zones but 2008b2e18757SMel Gorman * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX 2009b2e18757SMel Gorman * scanning would soon rescan the same pages to skip and put the 2010b2e18757SMel Gorman * system at risk of premature OOM. 2011b2e18757SMel Gorman */ 20127cc30fcfSMel Gorman if (!list_empty(&pages_skipped)) { 20137cc30fcfSMel Gorman int zid; 20147cc30fcfSMel Gorman 20153db65812SJohannes Weiner list_splice(&pages_skipped, src); 20167cc30fcfSMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 20177cc30fcfSMel Gorman if (!nr_skipped[zid]) 20187cc30fcfSMel Gorman continue; 20197cc30fcfSMel Gorman 20207cc30fcfSMel Gorman __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); 20211265e3a6SMichal Hocko skipped += nr_skipped[zid]; 20227cc30fcfSMel Gorman } 20237cc30fcfSMel Gorman } 2024791b48b6SMinchan Kim *nr_scanned = total_scan; 20251265e3a6SMichal Hocko trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, 2026791b48b6SMinchan Kim total_scan, skipped, nr_taken, mode, lru); 2027b4536f0cSMichal Hocko update_lru_sizes(lruvec, lru, nr_zone_taken); 20281da177e4SLinus Torvalds return nr_taken; 20291da177e4SLinus Torvalds } 20301da177e4SLinus Torvalds 203162695a84SNick Piggin /** 203262695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 203362695a84SNick Piggin * @page: page to isolate from its LRU list 203462695a84SNick Piggin * 203562695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 203662695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 203762695a84SNick Piggin * 203862695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 203962695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 204062695a84SNick Piggin * 204162695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 2042894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 2043894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 2044894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 204562695a84SNick Piggin * 204662695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 204762695a84SNick Piggin * found will be decremented. 204862695a84SNick Piggin * 204962695a84SNick Piggin * Restrictions: 2050a5d09bedSMike Rapoport * 205162695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 205201c4776bSHui Su * fundamental difference from isolate_lru_pages (which is called 205362695a84SNick Piggin * without a stable reference). 205462695a84SNick Piggin * (2) the lru_lock must not be held. 205562695a84SNick Piggin * (3) interrupts must be enabled. 205662695a84SNick Piggin */ 205762695a84SNick Piggin int isolate_lru_page(struct page *page) 205862695a84SNick Piggin { 205962695a84SNick Piggin int ret = -EBUSY; 206062695a84SNick Piggin 2061309381feSSasha Levin VM_BUG_ON_PAGE(!page_count(page), page); 2062cf2a82eeSKirill A. Shutemov WARN_RATELIMIT(PageTail(page), "trying to isolate tail page"); 20630c917313SKonstantin Khlebnikov 2064d25b5bd8SAlex Shi if (TestClearPageLRU(page)) { 2065fa9add64SHugh Dickins struct lruvec *lruvec; 206662695a84SNick Piggin 20670c917313SKonstantin Khlebnikov get_page(page); 20686168d0daSAlex Shi lruvec = lock_page_lruvec_irq(page); 206946ae6b2cSYu Zhao del_page_from_lru_list(page, lruvec); 20706168d0daSAlex Shi unlock_page_lruvec_irq(lruvec); 2071fa9add64SHugh Dickins ret = 0; 207262695a84SNick Piggin } 2073d25b5bd8SAlex Shi 207462695a84SNick Piggin return ret; 207562695a84SNick Piggin } 207662695a84SNick Piggin 20775ad333ebSAndy Whitcroft /* 2078d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 2079178821b8SXianting Tian * then get rescheduled. When there are massive number of tasks doing page 2080d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 2081d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 2082d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 208335cd7815SRik van Riel */ 2084599d0c95SMel Gorman static int too_many_isolated(struct pglist_data *pgdat, int file, 208535cd7815SRik van Riel struct scan_control *sc) 208635cd7815SRik van Riel { 208735cd7815SRik van Riel unsigned long inactive, isolated; 208835cd7815SRik van Riel 208935cd7815SRik van Riel if (current_is_kswapd()) 209035cd7815SRik van Riel return 0; 209135cd7815SRik van Riel 2092b5ead35eSJohannes Weiner if (!writeback_throttling_sane(sc)) 209335cd7815SRik van Riel return 0; 209435cd7815SRik van Riel 209535cd7815SRik van Riel if (file) { 2096599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_FILE); 2097599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_FILE); 209835cd7815SRik van Riel } else { 2099599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_ANON); 2100599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_ANON); 210135cd7815SRik van Riel } 210235cd7815SRik van Riel 21033cf23841SFengguang Wu /* 21043cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 21053cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 21063cf23841SFengguang Wu * deadlock. 21073cf23841SFengguang Wu */ 2108d0164adcSMel Gorman if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) 21093cf23841SFengguang Wu inactive >>= 3; 21103cf23841SFengguang Wu 211135cd7815SRik van Riel return isolated > inactive; 211235cd7815SRik van Riel } 211335cd7815SRik van Riel 2114a222f341SKirill Tkhai /* 211515b44736SHugh Dickins * move_pages_to_lru() moves pages from private @list to appropriate LRU list. 211615b44736SHugh Dickins * On return, @list is reused as a list of pages to be freed by the caller. 2117a222f341SKirill Tkhai * 2118a222f341SKirill Tkhai * Returns the number of pages moved to the given lruvec. 2119a222f341SKirill Tkhai */ 21209ef56b78SMuchun Song static unsigned int move_pages_to_lru(struct lruvec *lruvec, 2121a222f341SKirill Tkhai struct list_head *list) 212266635629SMel Gorman { 2123a222f341SKirill Tkhai int nr_pages, nr_moved = 0; 21243f79768fSHugh Dickins LIST_HEAD(pages_to_free); 2125a222f341SKirill Tkhai struct page *page; 212666635629SMel Gorman 2127a222f341SKirill Tkhai while (!list_empty(list)) { 2128a222f341SKirill Tkhai page = lru_to_page(list); 2129309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 2130a222f341SKirill Tkhai list_del(&page->lru); 21313d06afabSAlex Shi if (unlikely(!page_evictable(page))) { 21326168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 213366635629SMel Gorman putback_lru_page(page); 21346168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 213566635629SMel Gorman continue; 213666635629SMel Gorman } 2137fa9add64SHugh Dickins 21383d06afabSAlex Shi /* 21393d06afabSAlex Shi * The SetPageLRU needs to be kept here for list integrity. 21403d06afabSAlex Shi * Otherwise: 21413d06afabSAlex Shi * #0 move_pages_to_lru #1 release_pages 21423d06afabSAlex Shi * if !put_page_testzero 21433d06afabSAlex Shi * if (put_page_testzero()) 21443d06afabSAlex Shi * !PageLRU //skip lru_lock 21453d06afabSAlex Shi * SetPageLRU() 21463d06afabSAlex Shi * list_add(&page->lru,) 21473d06afabSAlex Shi * list_add(&page->lru,) 21483d06afabSAlex Shi */ 21497a608572SLinus Torvalds SetPageLRU(page); 2150a222f341SKirill Tkhai 21513d06afabSAlex Shi if (unlikely(put_page_testzero(page))) { 215287560179SYu Zhao __clear_page_lru_flags(page); 21532bcf8879SHugh Dickins 21542bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 21556168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 2156ff45fc3cSMatthew Wilcox (Oracle) destroy_compound_page(page); 21576168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 21582bcf8879SHugh Dickins } else 21592bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 21603d06afabSAlex Shi 21613d06afabSAlex Shi continue; 21623d06afabSAlex Shi } 21633d06afabSAlex Shi 2164afca9157SAlex Shi /* 2165afca9157SAlex Shi * All pages were isolated from the same lruvec (and isolation 2166afca9157SAlex Shi * inhibits memcg migration). 2167afca9157SAlex Shi */ 21687467c391SMuchun Song VM_BUG_ON_PAGE(!page_matches_lruvec(page, lruvec), page); 21693a9c9788SYu Zhao add_page_to_lru_list(page, lruvec); 21703d06afabSAlex Shi nr_pages = thp_nr_pages(page); 2171a222f341SKirill Tkhai nr_moved += nr_pages; 217231d8fcacSJohannes Weiner if (PageActive(page)) 217331d8fcacSJohannes Weiner workingset_age_nonresident(lruvec, nr_pages); 217466635629SMel Gorman } 217566635629SMel Gorman 21763f79768fSHugh Dickins /* 21773f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 21783f79768fSHugh Dickins */ 2179a222f341SKirill Tkhai list_splice(&pages_to_free, list); 2180a222f341SKirill Tkhai 2181a222f341SKirill Tkhai return nr_moved; 218266635629SMel Gorman } 218366635629SMel Gorman 218466635629SMel Gorman /* 2185399ba0b9SNeilBrown * If a kernel thread (such as nfsd for loop-back mounts) services 2186a37b0715SNeilBrown * a backing device by writing to the page cache it sets PF_LOCAL_THROTTLE. 2187399ba0b9SNeilBrown * In that case we should only throttle if the backing device it is 2188399ba0b9SNeilBrown * writing to is congested. In other cases it is safe to throttle. 2189399ba0b9SNeilBrown */ 2190399ba0b9SNeilBrown static int current_may_throttle(void) 2191399ba0b9SNeilBrown { 2192a37b0715SNeilBrown return !(current->flags & PF_LOCAL_THROTTLE) || 2193399ba0b9SNeilBrown current->backing_dev_info == NULL || 2194399ba0b9SNeilBrown bdi_write_congested(current->backing_dev_info); 2195399ba0b9SNeilBrown } 2196399ba0b9SNeilBrown 2197399ba0b9SNeilBrown /* 2198b2e18757SMel Gorman * shrink_inactive_list() is a helper for shrink_node(). It returns the number 21991742f19fSAndrew Morton * of reclaimed pages 22001da177e4SLinus Torvalds */ 22019ef56b78SMuchun Song static unsigned long 22021a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 22039e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 22041da177e4SLinus Torvalds { 22051da177e4SLinus Torvalds LIST_HEAD(page_list); 2206e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 2207730ec8c0SManinder Singh unsigned int nr_reclaimed = 0; 2208e247dbceSKOSAKI Motohiro unsigned long nr_taken; 2209060f005fSKirill Tkhai struct reclaim_stat stat; 2210497a6c1bSJohannes Weiner bool file = is_file_lru(lru); 2211f46b7912SKirill Tkhai enum vm_event_item item; 2212599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 2213db73ee0dSMichal Hocko bool stalled = false; 221478dc583dSKOSAKI Motohiro 2215599d0c95SMel Gorman while (unlikely(too_many_isolated(pgdat, file, sc))) { 2216db73ee0dSMichal Hocko if (stalled) 2217db73ee0dSMichal Hocko return 0; 2218db73ee0dSMichal Hocko 2219db73ee0dSMichal Hocko /* wait a bit for the reclaimer. */ 2220db73ee0dSMichal Hocko msleep(100); 2221db73ee0dSMichal Hocko stalled = true; 222235cd7815SRik van Riel 222335cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 222435cd7815SRik van Riel if (fatal_signal_pending(current)) 222535cd7815SRik van Riel return SWAP_CLUSTER_MAX; 222635cd7815SRik van Riel } 222735cd7815SRik van Riel 22281da177e4SLinus Torvalds lru_add_drain(); 2229f80c0673SMinchan Kim 22306168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 22311da177e4SLinus Torvalds 22325dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 2233a9e7c39fSKirill Tkhai &nr_scanned, sc, lru); 223495d918fcSKonstantin Khlebnikov 2235599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 2236f46b7912SKirill Tkhai item = current_is_kswapd() ? PGSCAN_KSWAPD : PGSCAN_DIRECT; 2237b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 2238f46b7912SKirill Tkhai __count_vm_events(item, nr_scanned); 2239f46b7912SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned); 2240497a6c1bSJohannes Weiner __count_vm_events(PGSCAN_ANON + file, nr_scanned); 2241497a6c1bSJohannes Weiner 22426168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 2243d563c050SHillf Danton 2244d563c050SHillf Danton if (nr_taken == 0) 224566635629SMel Gorman return 0; 2246b35ea17bSKOSAKI Motohiro 2247013339dfSShakeel Butt nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, &stat, false); 2248c661b078SAndy Whitcroft 22496168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 2250497a6c1bSJohannes Weiner move_pages_to_lru(lruvec, &page_list); 2251497a6c1bSJohannes Weiner 2252497a6c1bSJohannes Weiner __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 2253f46b7912SKirill Tkhai item = current_is_kswapd() ? PGSTEAL_KSWAPD : PGSTEAL_DIRECT; 2254b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 2255f46b7912SKirill Tkhai __count_vm_events(item, nr_reclaimed); 2256f46b7912SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed); 2257497a6c1bSJohannes Weiner __count_vm_events(PGSTEAL_ANON + file, nr_reclaimed); 22586168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 22593f79768fSHugh Dickins 226075cc3c91SAlex Shi lru_note_cost(lruvec, file, stat.nr_pageout); 2261747db954SJohannes Weiner mem_cgroup_uncharge_list(&page_list); 22622d4894b5SMel Gorman free_unref_page_list(&page_list); 2263e11da5b4SMel Gorman 226492df3a72SMel Gorman /* 22651c610d5fSAndrey Ryabinin * If dirty pages are scanned that are not queued for IO, it 22661c610d5fSAndrey Ryabinin * implies that flushers are not doing their job. This can 22671c610d5fSAndrey Ryabinin * happen when memory pressure pushes dirty pages to the end of 22681c610d5fSAndrey Ryabinin * the LRU before the dirty limits are breached and the dirty 22691c610d5fSAndrey Ryabinin * data has expired. It can also happen when the proportion of 22701c610d5fSAndrey Ryabinin * dirty pages grows not through writes but through memory 22711c610d5fSAndrey Ryabinin * pressure reclaiming all the clean cache. And in some cases, 22721c610d5fSAndrey Ryabinin * the flushers simply cannot keep up with the allocation 22731c610d5fSAndrey Ryabinin * rate. Nudge the flusher threads in case they are asleep. 22741c610d5fSAndrey Ryabinin */ 22751c610d5fSAndrey Ryabinin if (stat.nr_unqueued_dirty == nr_taken) 22761c610d5fSAndrey Ryabinin wakeup_flusher_threads(WB_REASON_VMSCAN); 22771c610d5fSAndrey Ryabinin 2278d108c772SAndrey Ryabinin sc->nr.dirty += stat.nr_dirty; 2279d108c772SAndrey Ryabinin sc->nr.congested += stat.nr_congested; 2280d108c772SAndrey Ryabinin sc->nr.unqueued_dirty += stat.nr_unqueued_dirty; 2281d108c772SAndrey Ryabinin sc->nr.writeback += stat.nr_writeback; 2282d108c772SAndrey Ryabinin sc->nr.immediate += stat.nr_immediate; 2283d108c772SAndrey Ryabinin sc->nr.taken += nr_taken; 2284d108c772SAndrey Ryabinin if (file) 2285d108c772SAndrey Ryabinin sc->nr.file_taken += nr_taken; 22868e950282SMel Gorman 2287599d0c95SMel Gorman trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, 2288d51d1e64SSteven Rostedt nr_scanned, nr_reclaimed, &stat, sc->priority, file); 228905ff5137SAndrew Morton return nr_reclaimed; 22901da177e4SLinus Torvalds } 22911da177e4SLinus Torvalds 229215b44736SHugh Dickins /* 229315b44736SHugh Dickins * shrink_active_list() moves pages from the active LRU to the inactive LRU. 229415b44736SHugh Dickins * 229515b44736SHugh Dickins * We move them the other way if the page is referenced by one or more 229615b44736SHugh Dickins * processes. 229715b44736SHugh Dickins * 229815b44736SHugh Dickins * If the pages are mostly unmapped, the processing is fast and it is 229915b44736SHugh Dickins * appropriate to hold lru_lock across the whole operation. But if 230015b44736SHugh Dickins * the pages are mapped, the processing is slow (page_referenced()), so 230115b44736SHugh Dickins * we should drop lru_lock around each page. It's impossible to balance 230215b44736SHugh Dickins * this, so instead we remove the pages from the LRU while processing them. 230315b44736SHugh Dickins * It is safe to rely on PG_active against the non-LRU pages in here because 230415b44736SHugh Dickins * nobody will play with that bit on a non-LRU page. 230515b44736SHugh Dickins * 230615b44736SHugh Dickins * The downside is that we have to touch page->_refcount against each page. 230715b44736SHugh Dickins * But we had to alter page->flags anyway. 230815b44736SHugh Dickins */ 2309f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 23101a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 2311f16015fbSJohannes Weiner struct scan_control *sc, 23129e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 23131cfb419bSKAMEZAWA Hiroyuki { 231444c241f1SKOSAKI Motohiro unsigned long nr_taken; 2315f626012dSHugh Dickins unsigned long nr_scanned; 23166fe6b7e3SWu Fengguang unsigned long vm_flags; 23171cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 23188cab4754SWu Fengguang LIST_HEAD(l_active); 2319b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 23201cfb419bSKAMEZAWA Hiroyuki struct page *page; 23219d998b4fSMichal Hocko unsigned nr_deactivate, nr_activate; 23229d998b4fSMichal Hocko unsigned nr_rotated = 0; 23233cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 2324599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 23251cfb419bSKAMEZAWA Hiroyuki 23261da177e4SLinus Torvalds lru_add_drain(); 2327f80c0673SMinchan Kim 23286168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 2329925b7673SJohannes Weiner 23305dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 2331a9e7c39fSKirill Tkhai &nr_scanned, sc, lru); 233289b5fae5SJohannes Weiner 2333599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 23341cfb419bSKAMEZAWA Hiroyuki 2335912c0572SShakeel Butt if (!cgroup_reclaim(sc)) 2336599d0c95SMel Gorman __count_vm_events(PGREFILL, nr_scanned); 23372fa2690cSYafang Shao __count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned); 23389d5e6a9fSHugh Dickins 23396168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 23401da177e4SLinus Torvalds 23411da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 23421da177e4SLinus Torvalds cond_resched(); 23431da177e4SLinus Torvalds page = lru_to_page(&l_hold); 23441da177e4SLinus Torvalds list_del(&page->lru); 23457e9cd484SRik van Riel 234639b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 2347894bc310SLee Schermerhorn putback_lru_page(page); 2348894bc310SLee Schermerhorn continue; 2349894bc310SLee Schermerhorn } 2350894bc310SLee Schermerhorn 2351cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 2352cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 2353cc715d99SMel Gorman if (page_has_private(page)) 2354cc715d99SMel Gorman try_to_release_page(page, 0); 2355cc715d99SMel Gorman unlock_page(page); 2356cc715d99SMel Gorman } 2357cc715d99SMel Gorman } 2358cc715d99SMel Gorman 2359c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 2360c3ac9a8aSJohannes Weiner &vm_flags)) { 23618cab4754SWu Fengguang /* 23628cab4754SWu Fengguang * Identify referenced, file-backed active pages and 23638cab4754SWu Fengguang * give them one more trip around the active list. So 23648cab4754SWu Fengguang * that executable code get better chances to stay in 23658cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 23668cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 23678cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 23688cab4754SWu Fengguang * so we ignore them here. 23698cab4754SWu Fengguang */ 23709de4f22aSHuang Ying if ((vm_flags & VM_EXEC) && page_is_file_lru(page)) { 23716c357848SMatthew Wilcox (Oracle) nr_rotated += thp_nr_pages(page); 23728cab4754SWu Fengguang list_add(&page->lru, &l_active); 23738cab4754SWu Fengguang continue; 23748cab4754SWu Fengguang } 23758cab4754SWu Fengguang } 23767e9cd484SRik van Riel 23775205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 23781899ad18SJohannes Weiner SetPageWorkingset(page); 23791da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 23801da177e4SLinus Torvalds } 23811da177e4SLinus Torvalds 2382b555749aSAndrew Morton /* 23838cab4754SWu Fengguang * Move pages back to the lru list. 2384b555749aSAndrew Morton */ 23856168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 2386556adecbSRik van Riel 2387a222f341SKirill Tkhai nr_activate = move_pages_to_lru(lruvec, &l_active); 2388a222f341SKirill Tkhai nr_deactivate = move_pages_to_lru(lruvec, &l_inactive); 2389f372d89eSKirill Tkhai /* Keep all free pages in l_active list */ 2390f372d89eSKirill Tkhai list_splice(&l_inactive, &l_active); 23919851ac13SKirill Tkhai 23929851ac13SKirill Tkhai __count_vm_events(PGDEACTIVATE, nr_deactivate); 23939851ac13SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate); 23949851ac13SKirill Tkhai 2395599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 23966168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 23972bcf8879SHugh Dickins 2398f372d89eSKirill Tkhai mem_cgroup_uncharge_list(&l_active); 2399f372d89eSKirill Tkhai free_unref_page_list(&l_active); 24009d998b4fSMichal Hocko trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate, 24019d998b4fSMichal Hocko nr_deactivate, nr_rotated, sc->priority, file); 24021da177e4SLinus Torvalds } 24031da177e4SLinus Torvalds 24041a4e58ccSMinchan Kim unsigned long reclaim_pages(struct list_head *page_list) 24051a4e58ccSMinchan Kim { 2406f661d007SYang Shi int nid = NUMA_NO_NODE; 2407730ec8c0SManinder Singh unsigned int nr_reclaimed = 0; 24081a4e58ccSMinchan Kim LIST_HEAD(node_page_list); 24091a4e58ccSMinchan Kim struct reclaim_stat dummy_stat; 24101a4e58ccSMinchan Kim struct page *page; 24112d2b8d2bSYu Zhao unsigned int noreclaim_flag; 24121a4e58ccSMinchan Kim struct scan_control sc = { 24131a4e58ccSMinchan Kim .gfp_mask = GFP_KERNEL, 24141a4e58ccSMinchan Kim .priority = DEF_PRIORITY, 24151a4e58ccSMinchan Kim .may_writepage = 1, 24161a4e58ccSMinchan Kim .may_unmap = 1, 24171a4e58ccSMinchan Kim .may_swap = 1, 241826aa2d19SDave Hansen .no_demotion = 1, 24191a4e58ccSMinchan Kim }; 24201a4e58ccSMinchan Kim 24212d2b8d2bSYu Zhao noreclaim_flag = memalloc_noreclaim_save(); 24222d2b8d2bSYu Zhao 24231a4e58ccSMinchan Kim while (!list_empty(page_list)) { 24241a4e58ccSMinchan Kim page = lru_to_page(page_list); 2425f661d007SYang Shi if (nid == NUMA_NO_NODE) { 24261a4e58ccSMinchan Kim nid = page_to_nid(page); 24271a4e58ccSMinchan Kim INIT_LIST_HEAD(&node_page_list); 24281a4e58ccSMinchan Kim } 24291a4e58ccSMinchan Kim 24301a4e58ccSMinchan Kim if (nid == page_to_nid(page)) { 24311a4e58ccSMinchan Kim ClearPageActive(page); 24321a4e58ccSMinchan Kim list_move(&page->lru, &node_page_list); 24331a4e58ccSMinchan Kim continue; 24341a4e58ccSMinchan Kim } 24351a4e58ccSMinchan Kim 24361a4e58ccSMinchan Kim nr_reclaimed += shrink_page_list(&node_page_list, 24371a4e58ccSMinchan Kim NODE_DATA(nid), 2438013339dfSShakeel Butt &sc, &dummy_stat, false); 24391a4e58ccSMinchan Kim while (!list_empty(&node_page_list)) { 24401a4e58ccSMinchan Kim page = lru_to_page(&node_page_list); 24411a4e58ccSMinchan Kim list_del(&page->lru); 24421a4e58ccSMinchan Kim putback_lru_page(page); 24431a4e58ccSMinchan Kim } 24441a4e58ccSMinchan Kim 2445f661d007SYang Shi nid = NUMA_NO_NODE; 24461a4e58ccSMinchan Kim } 24471a4e58ccSMinchan Kim 24481a4e58ccSMinchan Kim if (!list_empty(&node_page_list)) { 24491a4e58ccSMinchan Kim nr_reclaimed += shrink_page_list(&node_page_list, 24501a4e58ccSMinchan Kim NODE_DATA(nid), 2451013339dfSShakeel Butt &sc, &dummy_stat, false); 24521a4e58ccSMinchan Kim while (!list_empty(&node_page_list)) { 24531a4e58ccSMinchan Kim page = lru_to_page(&node_page_list); 24541a4e58ccSMinchan Kim list_del(&page->lru); 24551a4e58ccSMinchan Kim putback_lru_page(page); 24561a4e58ccSMinchan Kim } 24571a4e58ccSMinchan Kim } 24581a4e58ccSMinchan Kim 24592d2b8d2bSYu Zhao memalloc_noreclaim_restore(noreclaim_flag); 24602d2b8d2bSYu Zhao 24611a4e58ccSMinchan Kim return nr_reclaimed; 24621a4e58ccSMinchan Kim } 24631a4e58ccSMinchan Kim 2464b91ac374SJohannes Weiner static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 2465b91ac374SJohannes Weiner struct lruvec *lruvec, struct scan_control *sc) 2466b91ac374SJohannes Weiner { 2467b91ac374SJohannes Weiner if (is_active_lru(lru)) { 2468b91ac374SJohannes Weiner if (sc->may_deactivate & (1 << is_file_lru(lru))) 2469b91ac374SJohannes Weiner shrink_active_list(nr_to_scan, lruvec, sc, lru); 2470b91ac374SJohannes Weiner else 2471b91ac374SJohannes Weiner sc->skipped_deactivate = 1; 2472b91ac374SJohannes Weiner return 0; 2473b91ac374SJohannes Weiner } 2474b91ac374SJohannes Weiner 2475b91ac374SJohannes Weiner return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 2476b91ac374SJohannes Weiner } 2477b91ac374SJohannes Weiner 247859dc76b0SRik van Riel /* 247959dc76b0SRik van Riel * The inactive anon list should be small enough that the VM never has 248059dc76b0SRik van Riel * to do too much work. 248114797e23SKOSAKI Motohiro * 248259dc76b0SRik van Riel * The inactive file list should be small enough to leave most memory 248359dc76b0SRik van Riel * to the established workingset on the scan-resistant active list, 248459dc76b0SRik van Riel * but large enough to avoid thrashing the aggregate readahead window. 248559dc76b0SRik van Riel * 248659dc76b0SRik van Riel * Both inactive lists should also be large enough that each inactive 248759dc76b0SRik van Riel * page has a chance to be referenced again before it is reclaimed. 248859dc76b0SRik van Riel * 24892a2e4885SJohannes Weiner * If that fails and refaulting is observed, the inactive list grows. 24902a2e4885SJohannes Weiner * 249159dc76b0SRik van Riel * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages 24923a50d14dSAndrey Ryabinin * on this LRU, maintained by the pageout code. An inactive_ratio 249359dc76b0SRik van Riel * of 3 means 3:1 or 25% of the pages are kept on the inactive list. 249459dc76b0SRik van Riel * 249559dc76b0SRik van Riel * total target max 249659dc76b0SRik van Riel * memory ratio inactive 249759dc76b0SRik van Riel * ------------------------------------- 249859dc76b0SRik van Riel * 10MB 1 5MB 249959dc76b0SRik van Riel * 100MB 1 50MB 250059dc76b0SRik van Riel * 1GB 3 250MB 250159dc76b0SRik van Riel * 10GB 10 0.9GB 250259dc76b0SRik van Riel * 100GB 31 3GB 250359dc76b0SRik van Riel * 1TB 101 10GB 250459dc76b0SRik van Riel * 10TB 320 32GB 250514797e23SKOSAKI Motohiro */ 2506b91ac374SJohannes Weiner static bool inactive_is_low(struct lruvec *lruvec, enum lru_list inactive_lru) 250714797e23SKOSAKI Motohiro { 2508b91ac374SJohannes Weiner enum lru_list active_lru = inactive_lru + LRU_ACTIVE; 25092a2e4885SJohannes Weiner unsigned long inactive, active; 25102a2e4885SJohannes Weiner unsigned long inactive_ratio; 251159dc76b0SRik van Riel unsigned long gb; 251259dc76b0SRik van Riel 2513b91ac374SJohannes Weiner inactive = lruvec_page_state(lruvec, NR_LRU_BASE + inactive_lru); 2514b91ac374SJohannes Weiner active = lruvec_page_state(lruvec, NR_LRU_BASE + active_lru); 2515f8d1a311SMel Gorman 251659dc76b0SRik van Riel gb = (inactive + active) >> (30 - PAGE_SHIFT); 25174002570cSJoonsoo Kim if (gb) 251859dc76b0SRik van Riel inactive_ratio = int_sqrt(10 * gb); 2519b39415b2SRik van Riel else 252059dc76b0SRik van Riel inactive_ratio = 1; 2521fd538803SMichal Hocko 252259dc76b0SRik van Riel return inactive * inactive_ratio < active; 2523b39415b2SRik van Riel } 2524b39415b2SRik van Riel 25259a265114SJohannes Weiner enum scan_balance { 25269a265114SJohannes Weiner SCAN_EQUAL, 25279a265114SJohannes Weiner SCAN_FRACT, 25289a265114SJohannes Weiner SCAN_ANON, 25299a265114SJohannes Weiner SCAN_FILE, 25309a265114SJohannes Weiner }; 25319a265114SJohannes Weiner 25321da177e4SLinus Torvalds /* 25334f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 25344f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 25354f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 25364f98a2feSRik van Riel * onto the active list instead of evict. 25374f98a2feSRik van Riel * 2538be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 2539be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 25404f98a2feSRik van Riel */ 2541afaf07a6SJohannes Weiner static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, 2542afaf07a6SJohannes Weiner unsigned long *nr) 25434f98a2feSRik van Riel { 2544afaf07a6SJohannes Weiner struct mem_cgroup *memcg = lruvec_memcg(lruvec); 2545d483a5ddSJohannes Weiner unsigned long anon_cost, file_cost, total_cost; 254633377678SVladimir Davydov int swappiness = mem_cgroup_swappiness(memcg); 2547ed017373SYu Zhao u64 fraction[ANON_AND_FILE]; 25489a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 25499a265114SJohannes Weiner enum scan_balance scan_balance; 25509a265114SJohannes Weiner unsigned long ap, fp; 25519a265114SJohannes Weiner enum lru_list lru; 255276a33fc3SShaohua Li 255376a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 2554d8b38438SVladimir Davydov if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) { 25559a265114SJohannes Weiner scan_balance = SCAN_FILE; 255676a33fc3SShaohua Li goto out; 255776a33fc3SShaohua Li } 25584f98a2feSRik van Riel 255910316b31SJohannes Weiner /* 256010316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 256110316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 256210316b31SJohannes Weiner * disable swapping for individual groups completely when 256310316b31SJohannes Weiner * using the memory controller's swap limit feature would be 256410316b31SJohannes Weiner * too expensive. 256510316b31SJohannes Weiner */ 2566b5ead35eSJohannes Weiner if (cgroup_reclaim(sc) && !swappiness) { 25679a265114SJohannes Weiner scan_balance = SCAN_FILE; 256810316b31SJohannes Weiner goto out; 256910316b31SJohannes Weiner } 257010316b31SJohannes Weiner 257110316b31SJohannes Weiner /* 257210316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 257310316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 257410316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 257510316b31SJohannes Weiner */ 257602695175SJohannes Weiner if (!sc->priority && swappiness) { 25779a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 257810316b31SJohannes Weiner goto out; 257910316b31SJohannes Weiner } 258010316b31SJohannes Weiner 258111d16c25SJohannes Weiner /* 258253138ceaSJohannes Weiner * If the system is almost out of file pages, force-scan anon. 258362376251SJohannes Weiner */ 2584b91ac374SJohannes Weiner if (sc->file_is_tiny) { 258562376251SJohannes Weiner scan_balance = SCAN_ANON; 258662376251SJohannes Weiner goto out; 258762376251SJohannes Weiner } 258862376251SJohannes Weiner 258962376251SJohannes Weiner /* 2590b91ac374SJohannes Weiner * If there is enough inactive page cache, we do not reclaim 2591b91ac374SJohannes Weiner * anything from the anonymous working right now. 2592e9868505SRik van Riel */ 2593b91ac374SJohannes Weiner if (sc->cache_trim_mode) { 25949a265114SJohannes Weiner scan_balance = SCAN_FILE; 2595e9868505SRik van Riel goto out; 25964f98a2feSRik van Riel } 25974f98a2feSRik van Riel 25989a265114SJohannes Weiner scan_balance = SCAN_FRACT; 25994f98a2feSRik van Riel /* 2600314b57fbSJohannes Weiner * Calculate the pressure balance between anon and file pages. 2601314b57fbSJohannes Weiner * 2602314b57fbSJohannes Weiner * The amount of pressure we put on each LRU is inversely 2603314b57fbSJohannes Weiner * proportional to the cost of reclaiming each list, as 2604314b57fbSJohannes Weiner * determined by the share of pages that are refaulting, times 2605314b57fbSJohannes Weiner * the relative IO cost of bringing back a swapped out 2606314b57fbSJohannes Weiner * anonymous page vs reloading a filesystem page (swappiness). 2607314b57fbSJohannes Weiner * 2608d483a5ddSJohannes Weiner * Although we limit that influence to ensure no list gets 2609d483a5ddSJohannes Weiner * left behind completely: at least a third of the pressure is 2610d483a5ddSJohannes Weiner * applied, before swappiness. 2611d483a5ddSJohannes Weiner * 2612314b57fbSJohannes Weiner * With swappiness at 100, anon and file have equal IO cost. 261358c37f6eSKOSAKI Motohiro */ 2614d483a5ddSJohannes Weiner total_cost = sc->anon_cost + sc->file_cost; 2615d483a5ddSJohannes Weiner anon_cost = total_cost + sc->anon_cost; 2616d483a5ddSJohannes Weiner file_cost = total_cost + sc->file_cost; 2617d483a5ddSJohannes Weiner total_cost = anon_cost + file_cost; 261858c37f6eSKOSAKI Motohiro 2619d483a5ddSJohannes Weiner ap = swappiness * (total_cost + 1); 2620d483a5ddSJohannes Weiner ap /= anon_cost + 1; 26214f98a2feSRik van Riel 2622d483a5ddSJohannes Weiner fp = (200 - swappiness) * (total_cost + 1); 2623d483a5ddSJohannes Weiner fp /= file_cost + 1; 26244f98a2feSRik van Riel 262576a33fc3SShaohua Li fraction[0] = ap; 262676a33fc3SShaohua Li fraction[1] = fp; 2627a4fe1631SJohannes Weiner denominator = ap + fp; 262876a33fc3SShaohua Li out: 26294111304dSHugh Dickins for_each_evictable_lru(lru) { 26304111304dSHugh Dickins int file = is_file_lru(lru); 26319783aa99SChris Down unsigned long lruvec_size; 2632f56ce412SJohannes Weiner unsigned long low, min; 263376a33fc3SShaohua Li unsigned long scan; 263476a33fc3SShaohua Li 26359783aa99SChris Down lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); 2636f56ce412SJohannes Weiner mem_cgroup_protection(sc->target_mem_cgroup, memcg, 2637f56ce412SJohannes Weiner &min, &low); 26389783aa99SChris Down 2639f56ce412SJohannes Weiner if (min || low) { 26409783aa99SChris Down /* 26419783aa99SChris Down * Scale a cgroup's reclaim pressure by proportioning 26429783aa99SChris Down * its current usage to its memory.low or memory.min 26439783aa99SChris Down * setting. 26449783aa99SChris Down * 26459783aa99SChris Down * This is important, as otherwise scanning aggression 26469783aa99SChris Down * becomes extremely binary -- from nothing as we 26479783aa99SChris Down * approach the memory protection threshold, to totally 26489783aa99SChris Down * nominal as we exceed it. This results in requiring 26499783aa99SChris Down * setting extremely liberal protection thresholds. It 26509783aa99SChris Down * also means we simply get no protection at all if we 26519783aa99SChris Down * set it too low, which is not ideal. 26521bc63fb1SChris Down * 26531bc63fb1SChris Down * If there is any protection in place, we reduce scan 26541bc63fb1SChris Down * pressure by how much of the total memory used is 26551bc63fb1SChris Down * within protection thresholds. 26569783aa99SChris Down * 26579de7ca46SChris Down * There is one special case: in the first reclaim pass, 26589de7ca46SChris Down * we skip over all groups that are within their low 26599de7ca46SChris Down * protection. If that fails to reclaim enough pages to 26609de7ca46SChris Down * satisfy the reclaim goal, we come back and override 26619de7ca46SChris Down * the best-effort low protection. However, we still 26629de7ca46SChris Down * ideally want to honor how well-behaved groups are in 26639de7ca46SChris Down * that case instead of simply punishing them all 26649de7ca46SChris Down * equally. As such, we reclaim them based on how much 26651bc63fb1SChris Down * memory they are using, reducing the scan pressure 26661bc63fb1SChris Down * again by how much of the total memory used is under 26671bc63fb1SChris Down * hard protection. 26689783aa99SChris Down */ 26691bc63fb1SChris Down unsigned long cgroup_size = mem_cgroup_size(memcg); 2670f56ce412SJohannes Weiner unsigned long protection; 2671f56ce412SJohannes Weiner 2672f56ce412SJohannes Weiner /* memory.low scaling, make sure we retry before OOM */ 2673f56ce412SJohannes Weiner if (!sc->memcg_low_reclaim && low > min) { 2674f56ce412SJohannes Weiner protection = low; 2675f56ce412SJohannes Weiner sc->memcg_low_skipped = 1; 2676f56ce412SJohannes Weiner } else { 2677f56ce412SJohannes Weiner protection = min; 2678f56ce412SJohannes Weiner } 26791bc63fb1SChris Down 26801bc63fb1SChris Down /* Avoid TOCTOU with earlier protection check */ 26811bc63fb1SChris Down cgroup_size = max(cgroup_size, protection); 26821bc63fb1SChris Down 26831bc63fb1SChris Down scan = lruvec_size - lruvec_size * protection / 26841bc63fb1SChris Down cgroup_size; 26859783aa99SChris Down 26869783aa99SChris Down /* 26871bc63fb1SChris Down * Minimally target SWAP_CLUSTER_MAX pages to keep 268855b65a57SEthon Paul * reclaim moving forwards, avoiding decrementing 26899de7ca46SChris Down * sc->priority further than desirable. 26909783aa99SChris Down */ 26911bc63fb1SChris Down scan = max(scan, SWAP_CLUSTER_MAX); 26929783aa99SChris Down } else { 26939783aa99SChris Down scan = lruvec_size; 26949783aa99SChris Down } 26959783aa99SChris Down 26969783aa99SChris Down scan >>= sc->priority; 26979783aa99SChris Down 2698688035f7SJohannes Weiner /* 2699688035f7SJohannes Weiner * If the cgroup's already been deleted, make sure to 2700688035f7SJohannes Weiner * scrape out the remaining cache. 2701688035f7SJohannes Weiner */ 2702688035f7SJohannes Weiner if (!scan && !mem_cgroup_online(memcg)) 27039783aa99SChris Down scan = min(lruvec_size, SWAP_CLUSTER_MAX); 27049a265114SJohannes Weiner 27059a265114SJohannes Weiner switch (scan_balance) { 27069a265114SJohannes Weiner case SCAN_EQUAL: 27079a265114SJohannes Weiner /* Scan lists relative to size */ 27089a265114SJohannes Weiner break; 27099a265114SJohannes Weiner case SCAN_FRACT: 27109a265114SJohannes Weiner /* 27119a265114SJohannes Weiner * Scan types proportional to swappiness and 27129a265114SJohannes Weiner * their relative recent reclaim efficiency. 271376073c64SGavin Shan * Make sure we don't miss the last page on 271476073c64SGavin Shan * the offlined memory cgroups because of a 271576073c64SGavin Shan * round-off error. 27169a265114SJohannes Weiner */ 271776073c64SGavin Shan scan = mem_cgroup_online(memcg) ? 271876073c64SGavin Shan div64_u64(scan * fraction[file], denominator) : 271976073c64SGavin Shan DIV64_U64_ROUND_UP(scan * fraction[file], 27206f04f48dSSuleiman Souhlal denominator); 27219a265114SJohannes Weiner break; 27229a265114SJohannes Weiner case SCAN_FILE: 27239a265114SJohannes Weiner case SCAN_ANON: 27249a265114SJohannes Weiner /* Scan one type exclusively */ 2725e072bff6SMateusz Nosek if ((scan_balance == SCAN_FILE) != file) 27269a265114SJohannes Weiner scan = 0; 27279a265114SJohannes Weiner break; 27289a265114SJohannes Weiner default: 27299a265114SJohannes Weiner /* Look ma, no brain */ 27309a265114SJohannes Weiner BUG(); 27319a265114SJohannes Weiner } 27326b4f7799SJohannes Weiner 27334111304dSHugh Dickins nr[lru] = scan; 273476a33fc3SShaohua Li } 27356e08a369SWu Fengguang } 27364f98a2feSRik van Riel 2737afaf07a6SJohannes Weiner static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) 27389b4f98cdSJohannes Weiner { 27399b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 2740e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 27419b4f98cdSJohannes Weiner unsigned long nr_to_scan; 27429b4f98cdSJohannes Weiner enum lru_list lru; 27439b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 27449b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 27459b4f98cdSJohannes Weiner struct blk_plug plug; 27461a501907SMel Gorman bool scan_adjusted; 27479b4f98cdSJohannes Weiner 2748afaf07a6SJohannes Weiner get_scan_count(lruvec, sc, nr); 27499b4f98cdSJohannes Weiner 2750e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 2751e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 2752e82e0561SMel Gorman 27531a501907SMel Gorman /* 27541a501907SMel Gorman * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal 27551a501907SMel Gorman * event that can occur when there is little memory pressure e.g. 27561a501907SMel Gorman * multiple streaming readers/writers. Hence, we do not abort scanning 27571a501907SMel Gorman * when the requested number of pages are reclaimed when scanning at 27581a501907SMel Gorman * DEF_PRIORITY on the assumption that the fact we are direct 27591a501907SMel Gorman * reclaiming implies that kswapd is not keeping up and it is best to 27601a501907SMel Gorman * do a batch of work at once. For memcg reclaim one check is made to 27611a501907SMel Gorman * abort proportional reclaim if either the file or anon lru has already 27621a501907SMel Gorman * dropped to zero at the first pass. 27631a501907SMel Gorman */ 2764b5ead35eSJohannes Weiner scan_adjusted = (!cgroup_reclaim(sc) && !current_is_kswapd() && 27651a501907SMel Gorman sc->priority == DEF_PRIORITY); 27661a501907SMel Gorman 27679b4f98cdSJohannes Weiner blk_start_plug(&plug); 27689b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 27699b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 2770e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 2771e82e0561SMel Gorman unsigned long nr_scanned; 2772e82e0561SMel Gorman 27739b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 27749b4f98cdSJohannes Weiner if (nr[lru]) { 27759b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 27769b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 27779b4f98cdSJohannes Weiner 27789b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 27793b991208SJohannes Weiner lruvec, sc); 27809b4f98cdSJohannes Weiner } 27819b4f98cdSJohannes Weiner } 2782e82e0561SMel Gorman 2783bd041733SMichal Hocko cond_resched(); 2784bd041733SMichal Hocko 2785e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 2786e82e0561SMel Gorman continue; 2787e82e0561SMel Gorman 27889b4f98cdSJohannes Weiner /* 2789e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 27901a501907SMel Gorman * requested. Ensure that the anon and file LRUs are scanned 2791e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 2792e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 2793e82e0561SMel Gorman * proportional to the original scan target. 2794e82e0561SMel Gorman */ 2795e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 2796e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 2797e82e0561SMel Gorman 27981a501907SMel Gorman /* 27991a501907SMel Gorman * It's just vindictive to attack the larger once the smaller 28001a501907SMel Gorman * has gone to zero. And given the way we stop scanning the 28011a501907SMel Gorman * smaller below, this makes sure that we only make one nudge 28021a501907SMel Gorman * towards proportionality once we've got nr_to_reclaim. 28031a501907SMel Gorman */ 28041a501907SMel Gorman if (!nr_file || !nr_anon) 28051a501907SMel Gorman break; 28061a501907SMel Gorman 2807e82e0561SMel Gorman if (nr_file > nr_anon) { 2808e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 2809e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 2810e82e0561SMel Gorman lru = LRU_BASE; 2811e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 2812e82e0561SMel Gorman } else { 2813e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 2814e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 2815e82e0561SMel Gorman lru = LRU_FILE; 2816e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 2817e82e0561SMel Gorman } 2818e82e0561SMel Gorman 2819e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 2820e82e0561SMel Gorman nr[lru] = 0; 2821e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 2822e82e0561SMel Gorman 2823e82e0561SMel Gorman /* 2824e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 2825e82e0561SMel Gorman * scan target and the percentage scanning already complete 2826e82e0561SMel Gorman */ 2827e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 2828e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2829e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2830e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2831e82e0561SMel Gorman 2832e82e0561SMel Gorman lru += LRU_ACTIVE; 2833e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2834e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2835e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2836e82e0561SMel Gorman 2837e82e0561SMel Gorman scan_adjusted = true; 28389b4f98cdSJohannes Weiner } 28399b4f98cdSJohannes Weiner blk_finish_plug(&plug); 28409b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 28419b4f98cdSJohannes Weiner 28429b4f98cdSJohannes Weiner /* 28439b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 28449b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 28459b4f98cdSJohannes Weiner */ 2846b91ac374SJohannes Weiner if (total_swap_pages && inactive_is_low(lruvec, LRU_INACTIVE_ANON)) 28479b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 28489b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 28499b4f98cdSJohannes Weiner } 28509b4f98cdSJohannes Weiner 285123b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 28529e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 285323b9da55SMel Gorman { 2854d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 285523b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 28569e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 285723b9da55SMel Gorman return true; 285823b9da55SMel Gorman 285923b9da55SMel Gorman return false; 286023b9da55SMel Gorman } 286123b9da55SMel Gorman 28624f98a2feSRik van Riel /* 286323b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 286423b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 286523b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 2866df3a45f9SQiwu Chen * calls try_to_compact_pages() that it will have enough free pages to succeed. 286723b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 28683e7d3449SMel Gorman */ 2869a9dd0a83SMel Gorman static inline bool should_continue_reclaim(struct pglist_data *pgdat, 28703e7d3449SMel Gorman unsigned long nr_reclaimed, 28713e7d3449SMel Gorman struct scan_control *sc) 28723e7d3449SMel Gorman { 28733e7d3449SMel Gorman unsigned long pages_for_compaction; 28743e7d3449SMel Gorman unsigned long inactive_lru_pages; 2875a9dd0a83SMel Gorman int z; 28763e7d3449SMel Gorman 28773e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 28789e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 28793e7d3449SMel Gorman return false; 28803e7d3449SMel Gorman 28813e7d3449SMel Gorman /* 28825ee04716SVlastimil Babka * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX 28835ee04716SVlastimil Babka * number of pages that were scanned. This will return to the caller 28845ee04716SVlastimil Babka * with the risk reclaim/compaction and the resulting allocation attempt 28855ee04716SVlastimil Babka * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL 28865ee04716SVlastimil Babka * allocations through requiring that the full LRU list has been scanned 28875ee04716SVlastimil Babka * first, by assuming that zero delta of sc->nr_scanned means full LRU 28885ee04716SVlastimil Babka * scan, but that approximation was wrong, and there were corner cases 28895ee04716SVlastimil Babka * where always a non-zero amount of pages were scanned. 28902876592fSMel Gorman */ 28912876592fSMel Gorman if (!nr_reclaimed) 28922876592fSMel Gorman return false; 28933e7d3449SMel Gorman 28943e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 2895a9dd0a83SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 2896a9dd0a83SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 28976aa303deSMel Gorman if (!managed_zone(zone)) 2898a9dd0a83SMel Gorman continue; 2899a9dd0a83SMel Gorman 2900a9dd0a83SMel Gorman switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) { 2901cf378319SVlastimil Babka case COMPACT_SUCCESS: 29023e7d3449SMel Gorman case COMPACT_CONTINUE: 29033e7d3449SMel Gorman return false; 29043e7d3449SMel Gorman default: 2905a9dd0a83SMel Gorman /* check next zone */ 2906a9dd0a83SMel Gorman ; 29073e7d3449SMel Gorman } 29083e7d3449SMel Gorman } 29091c6c1597SHillf Danton 29101c6c1597SHillf Danton /* 29111c6c1597SHillf Danton * If we have not reclaimed enough pages for compaction and the 29121c6c1597SHillf Danton * inactive lists are large enough, continue reclaiming 29131c6c1597SHillf Danton */ 29141c6c1597SHillf Danton pages_for_compaction = compact_gap(sc->order); 29151c6c1597SHillf Danton inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); 29161c6c1597SHillf Danton if (get_nr_swap_pages() > 0) 29171c6c1597SHillf Danton inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); 29181c6c1597SHillf Danton 29195ee04716SVlastimil Babka return inactive_lru_pages > pages_for_compaction; 2920a9dd0a83SMel Gorman } 29213e7d3449SMel Gorman 29220f6a5cffSJohannes Weiner static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc) 2923f16015fbSJohannes Weiner { 29240f6a5cffSJohannes Weiner struct mem_cgroup *target_memcg = sc->target_mem_cgroup; 2925694fbc0fSAndrew Morton struct mem_cgroup *memcg; 2926d108c772SAndrey Ryabinin 29270f6a5cffSJohannes Weiner memcg = mem_cgroup_iter(target_memcg, NULL, NULL); 2928694fbc0fSAndrew Morton do { 2929afaf07a6SJohannes Weiner struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); 29308e8ae645SJohannes Weiner unsigned long reclaimed; 2931cb731d6cSVladimir Davydov unsigned long scanned; 29329b4f98cdSJohannes Weiner 2933e3336cabSXunlei Pang /* 2934e3336cabSXunlei Pang * This loop can become CPU-bound when target memcgs 2935e3336cabSXunlei Pang * aren't eligible for reclaim - either because they 2936e3336cabSXunlei Pang * don't have any reclaimable pages, or because their 2937e3336cabSXunlei Pang * memory is explicitly protected. Avoid soft lockups. 2938e3336cabSXunlei Pang */ 2939e3336cabSXunlei Pang cond_resched(); 2940e3336cabSXunlei Pang 294145c7f7e1SChris Down mem_cgroup_calculate_protection(target_memcg, memcg); 294245c7f7e1SChris Down 294345c7f7e1SChris Down if (mem_cgroup_below_min(memcg)) { 2944bf8d5d52SRoman Gushchin /* 2945bf8d5d52SRoman Gushchin * Hard protection. 2946bf8d5d52SRoman Gushchin * If there is no reclaimable memory, OOM. 2947bf8d5d52SRoman Gushchin */ 2948bf8d5d52SRoman Gushchin continue; 294945c7f7e1SChris Down } else if (mem_cgroup_below_low(memcg)) { 2950bf8d5d52SRoman Gushchin /* 2951bf8d5d52SRoman Gushchin * Soft protection. 2952bf8d5d52SRoman Gushchin * Respect the protection only as long as 2953bf8d5d52SRoman Gushchin * there is an unprotected supply 2954bf8d5d52SRoman Gushchin * of reclaimable memory from other cgroups. 2955bf8d5d52SRoman Gushchin */ 2956d6622f63SYisheng Xie if (!sc->memcg_low_reclaim) { 2957d6622f63SYisheng Xie sc->memcg_low_skipped = 1; 2958241994edSJohannes Weiner continue; 2959d6622f63SYisheng Xie } 2960e27be240SJohannes Weiner memcg_memory_event(memcg, MEMCG_LOW); 2961241994edSJohannes Weiner } 2962241994edSJohannes Weiner 29638e8ae645SJohannes Weiner reclaimed = sc->nr_reclaimed; 2964cb731d6cSVladimir Davydov scanned = sc->nr_scanned; 2965afaf07a6SJohannes Weiner 2966afaf07a6SJohannes Weiner shrink_lruvec(lruvec, sc); 2967f9be23d6SKonstantin Khlebnikov 296828360f39SMel Gorman shrink_slab(sc->gfp_mask, pgdat->node_id, memcg, 296928360f39SMel Gorman sc->priority); 2970cb731d6cSVladimir Davydov 29718e8ae645SJohannes Weiner /* Record the group's reclaim efficiency */ 29728e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, memcg, false, 29738e8ae645SJohannes Weiner sc->nr_scanned - scanned, 29748e8ae645SJohannes Weiner sc->nr_reclaimed - reclaimed); 29758e8ae645SJohannes Weiner 29760f6a5cffSJohannes Weiner } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL))); 29770f6a5cffSJohannes Weiner } 29780f6a5cffSJohannes Weiner 29796c9e0907SLiu Song static void shrink_node(pg_data_t *pgdat, struct scan_control *sc) 29800f6a5cffSJohannes Weiner { 29810f6a5cffSJohannes Weiner struct reclaim_state *reclaim_state = current->reclaim_state; 29820f6a5cffSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 29831b05117dSJohannes Weiner struct lruvec *target_lruvec; 29840f6a5cffSJohannes Weiner bool reclaimable = false; 2985b91ac374SJohannes Weiner unsigned long file; 29860f6a5cffSJohannes Weiner 29871b05117dSJohannes Weiner target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat); 29881b05117dSJohannes Weiner 29890f6a5cffSJohannes Weiner again: 2990aa48e47eSShakeel Butt /* 2991aa48e47eSShakeel Butt * Flush the memory cgroup stats, so that we read accurate per-memcg 2992aa48e47eSShakeel Butt * lruvec stats for heuristics. 2993aa48e47eSShakeel Butt */ 2994aa48e47eSShakeel Butt mem_cgroup_flush_stats(); 2995aa48e47eSShakeel Butt 29960f6a5cffSJohannes Weiner memset(&sc->nr, 0, sizeof(sc->nr)); 29970f6a5cffSJohannes Weiner 29980f6a5cffSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 29990f6a5cffSJohannes Weiner nr_scanned = sc->nr_scanned; 30000f6a5cffSJohannes Weiner 300153138ceaSJohannes Weiner /* 30027cf111bcSJohannes Weiner * Determine the scan balance between anon and file LRUs. 30037cf111bcSJohannes Weiner */ 30046168d0daSAlex Shi spin_lock_irq(&target_lruvec->lru_lock); 30057cf111bcSJohannes Weiner sc->anon_cost = target_lruvec->anon_cost; 30067cf111bcSJohannes Weiner sc->file_cost = target_lruvec->file_cost; 30076168d0daSAlex Shi spin_unlock_irq(&target_lruvec->lru_lock); 30087cf111bcSJohannes Weiner 30097cf111bcSJohannes Weiner /* 3010b91ac374SJohannes Weiner * Target desirable inactive:active list ratios for the anon 3011b91ac374SJohannes Weiner * and file LRU lists. 3012b91ac374SJohannes Weiner */ 3013b91ac374SJohannes Weiner if (!sc->force_deactivate) { 3014b91ac374SJohannes Weiner unsigned long refaults; 3015b91ac374SJohannes Weiner 3016170b04b7SJoonsoo Kim refaults = lruvec_page_state(target_lruvec, 3017170b04b7SJoonsoo Kim WORKINGSET_ACTIVATE_ANON); 3018170b04b7SJoonsoo Kim if (refaults != target_lruvec->refaults[0] || 3019170b04b7SJoonsoo Kim inactive_is_low(target_lruvec, LRU_INACTIVE_ANON)) 3020b91ac374SJohannes Weiner sc->may_deactivate |= DEACTIVATE_ANON; 3021b91ac374SJohannes Weiner else 3022b91ac374SJohannes Weiner sc->may_deactivate &= ~DEACTIVATE_ANON; 3023b91ac374SJohannes Weiner 3024b91ac374SJohannes Weiner /* 3025b91ac374SJohannes Weiner * When refaults are being observed, it means a new 3026b91ac374SJohannes Weiner * workingset is being established. Deactivate to get 3027b91ac374SJohannes Weiner * rid of any stale active pages quickly. 3028b91ac374SJohannes Weiner */ 3029b91ac374SJohannes Weiner refaults = lruvec_page_state(target_lruvec, 3030170b04b7SJoonsoo Kim WORKINGSET_ACTIVATE_FILE); 3031170b04b7SJoonsoo Kim if (refaults != target_lruvec->refaults[1] || 3032b91ac374SJohannes Weiner inactive_is_low(target_lruvec, LRU_INACTIVE_FILE)) 3033b91ac374SJohannes Weiner sc->may_deactivate |= DEACTIVATE_FILE; 3034b91ac374SJohannes Weiner else 3035b91ac374SJohannes Weiner sc->may_deactivate &= ~DEACTIVATE_FILE; 3036b91ac374SJohannes Weiner } else 3037b91ac374SJohannes Weiner sc->may_deactivate = DEACTIVATE_ANON | DEACTIVATE_FILE; 3038b91ac374SJohannes Weiner 3039b91ac374SJohannes Weiner /* 3040b91ac374SJohannes Weiner * If we have plenty of inactive file pages that aren't 3041b91ac374SJohannes Weiner * thrashing, try to reclaim those first before touching 3042b91ac374SJohannes Weiner * anonymous pages. 3043b91ac374SJohannes Weiner */ 3044b91ac374SJohannes Weiner file = lruvec_page_state(target_lruvec, NR_INACTIVE_FILE); 3045b91ac374SJohannes Weiner if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE)) 3046b91ac374SJohannes Weiner sc->cache_trim_mode = 1; 3047b91ac374SJohannes Weiner else 3048b91ac374SJohannes Weiner sc->cache_trim_mode = 0; 3049b91ac374SJohannes Weiner 3050b91ac374SJohannes Weiner /* 305153138ceaSJohannes Weiner * Prevent the reclaimer from falling into the cache trap: as 305253138ceaSJohannes Weiner * cache pages start out inactive, every cache fault will tip 305353138ceaSJohannes Weiner * the scan balance towards the file LRU. And as the file LRU 305453138ceaSJohannes Weiner * shrinks, so does the window for rotation from references. 305553138ceaSJohannes Weiner * This means we have a runaway feedback loop where a tiny 305653138ceaSJohannes Weiner * thrashing file LRU becomes infinitely more attractive than 305753138ceaSJohannes Weiner * anon pages. Try to detect this based on file LRU size. 305853138ceaSJohannes Weiner */ 305953138ceaSJohannes Weiner if (!cgroup_reclaim(sc)) { 306053138ceaSJohannes Weiner unsigned long total_high_wmark = 0; 3061b91ac374SJohannes Weiner unsigned long free, anon; 3062b91ac374SJohannes Weiner int z; 306353138ceaSJohannes Weiner 306453138ceaSJohannes Weiner free = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); 306553138ceaSJohannes Weiner file = node_page_state(pgdat, NR_ACTIVE_FILE) + 306653138ceaSJohannes Weiner node_page_state(pgdat, NR_INACTIVE_FILE); 306753138ceaSJohannes Weiner 306853138ceaSJohannes Weiner for (z = 0; z < MAX_NR_ZONES; z++) { 306953138ceaSJohannes Weiner struct zone *zone = &pgdat->node_zones[z]; 307053138ceaSJohannes Weiner if (!managed_zone(zone)) 307153138ceaSJohannes Weiner continue; 307253138ceaSJohannes Weiner 307353138ceaSJohannes Weiner total_high_wmark += high_wmark_pages(zone); 307453138ceaSJohannes Weiner } 307553138ceaSJohannes Weiner 3076b91ac374SJohannes Weiner /* 3077b91ac374SJohannes Weiner * Consider anon: if that's low too, this isn't a 3078b91ac374SJohannes Weiner * runaway file reclaim problem, but rather just 3079b91ac374SJohannes Weiner * extreme pressure. Reclaim as per usual then. 3080b91ac374SJohannes Weiner */ 3081b91ac374SJohannes Weiner anon = node_page_state(pgdat, NR_INACTIVE_ANON); 3082b91ac374SJohannes Weiner 3083b91ac374SJohannes Weiner sc->file_is_tiny = 3084b91ac374SJohannes Weiner file + free <= total_high_wmark && 3085b91ac374SJohannes Weiner !(sc->may_deactivate & DEACTIVATE_ANON) && 3086b91ac374SJohannes Weiner anon >> sc->priority; 308753138ceaSJohannes Weiner } 308853138ceaSJohannes Weiner 30890f6a5cffSJohannes Weiner shrink_node_memcgs(pgdat, sc); 309070ddf637SAnton Vorontsov 30916b4f7799SJohannes Weiner if (reclaim_state) { 3092cb731d6cSVladimir Davydov sc->nr_reclaimed += reclaim_state->reclaimed_slab; 30936b4f7799SJohannes Weiner reclaim_state->reclaimed_slab = 0; 30946b4f7799SJohannes Weiner } 30956b4f7799SJohannes Weiner 30968e8ae645SJohannes Weiner /* Record the subtree's reclaim efficiency */ 30971b05117dSJohannes Weiner vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, 309870ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 309970ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 310070ddf637SAnton Vorontsov 31012344d7e4SJohannes Weiner if (sc->nr_reclaimed - nr_reclaimed) 31022344d7e4SJohannes Weiner reclaimable = true; 31032344d7e4SJohannes Weiner 3104e3c1ac58SAndrey Ryabinin if (current_is_kswapd()) { 3105d108c772SAndrey Ryabinin /* 3106e3c1ac58SAndrey Ryabinin * If reclaim is isolating dirty pages under writeback, 3107e3c1ac58SAndrey Ryabinin * it implies that the long-lived page allocation rate 3108e3c1ac58SAndrey Ryabinin * is exceeding the page laundering rate. Either the 3109e3c1ac58SAndrey Ryabinin * global limits are not being effective at throttling 3110e3c1ac58SAndrey Ryabinin * processes due to the page distribution throughout 3111e3c1ac58SAndrey Ryabinin * zones or there is heavy usage of a slow backing 3112e3c1ac58SAndrey Ryabinin * device. The only option is to throttle from reclaim 3113e3c1ac58SAndrey Ryabinin * context which is not ideal as there is no guarantee 3114d108c772SAndrey Ryabinin * the dirtying process is throttled in the same way 3115d108c772SAndrey Ryabinin * balance_dirty_pages() manages. 3116d108c772SAndrey Ryabinin * 3117e3c1ac58SAndrey Ryabinin * Once a node is flagged PGDAT_WRITEBACK, kswapd will 3118e3c1ac58SAndrey Ryabinin * count the number of pages under pages flagged for 3119e3c1ac58SAndrey Ryabinin * immediate reclaim and stall if any are encountered 3120e3c1ac58SAndrey Ryabinin * in the nr_immediate check below. 3121d108c772SAndrey Ryabinin */ 3122d108c772SAndrey Ryabinin if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken) 3123d108c772SAndrey Ryabinin set_bit(PGDAT_WRITEBACK, &pgdat->flags); 3124d108c772SAndrey Ryabinin 3125d108c772SAndrey Ryabinin /* Allow kswapd to start writing pages during reclaim.*/ 3126d108c772SAndrey Ryabinin if (sc->nr.unqueued_dirty == sc->nr.file_taken) 3127d108c772SAndrey Ryabinin set_bit(PGDAT_DIRTY, &pgdat->flags); 3128d108c772SAndrey Ryabinin 3129d108c772SAndrey Ryabinin /* 31301eba09c1SRandy Dunlap * If kswapd scans pages marked for immediate 3131d108c772SAndrey Ryabinin * reclaim and under writeback (nr_immediate), it 3132d108c772SAndrey Ryabinin * implies that pages are cycling through the LRU 3133d108c772SAndrey Ryabinin * faster than they are written so also forcibly stall. 3134d108c772SAndrey Ryabinin */ 3135d108c772SAndrey Ryabinin if (sc->nr.immediate) 3136d108c772SAndrey Ryabinin congestion_wait(BLK_RW_ASYNC, HZ/10); 3137d108c772SAndrey Ryabinin } 3138d108c772SAndrey Ryabinin 3139d108c772SAndrey Ryabinin /* 31401b05117dSJohannes Weiner * Tag a node/memcg as congested if all the dirty pages 31411b05117dSJohannes Weiner * scanned were backed by a congested BDI and 31421b05117dSJohannes Weiner * wait_iff_congested will stall. 31431b05117dSJohannes Weiner * 3144e3c1ac58SAndrey Ryabinin * Legacy memcg will stall in page writeback so avoid forcibly 3145e3c1ac58SAndrey Ryabinin * stalling in wait_iff_congested(). 3146e3c1ac58SAndrey Ryabinin */ 31471b05117dSJohannes Weiner if ((current_is_kswapd() || 31481b05117dSJohannes Weiner (cgroup_reclaim(sc) && writeback_throttling_sane(sc))) && 3149e3c1ac58SAndrey Ryabinin sc->nr.dirty && sc->nr.dirty == sc->nr.congested) 31501b05117dSJohannes Weiner set_bit(LRUVEC_CONGESTED, &target_lruvec->flags); 3151e3c1ac58SAndrey Ryabinin 3152e3c1ac58SAndrey Ryabinin /* 3153d108c772SAndrey Ryabinin * Stall direct reclaim for IO completions if underlying BDIs 3154d108c772SAndrey Ryabinin * and node is congested. Allow kswapd to continue until it 3155d108c772SAndrey Ryabinin * starts encountering unqueued dirty pages or cycling through 3156d108c772SAndrey Ryabinin * the LRU too quickly. 3157d108c772SAndrey Ryabinin */ 31581b05117dSJohannes Weiner if (!current_is_kswapd() && current_may_throttle() && 31591b05117dSJohannes Weiner !sc->hibernation_mode && 31601b05117dSJohannes Weiner test_bit(LRUVEC_CONGESTED, &target_lruvec->flags)) 3161e3c1ac58SAndrey Ryabinin wait_iff_congested(BLK_RW_ASYNC, HZ/10); 3162d108c772SAndrey Ryabinin 3163d2af3397SJohannes Weiner if (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, 3164d2af3397SJohannes Weiner sc)) 3165d2af3397SJohannes Weiner goto again; 31662344d7e4SJohannes Weiner 3167c73322d0SJohannes Weiner /* 3168c73322d0SJohannes Weiner * Kswapd gives up on balancing particular nodes after too 3169c73322d0SJohannes Weiner * many failures to reclaim anything from them and goes to 3170c73322d0SJohannes Weiner * sleep. On reclaim progress, reset the failure counter. A 3171c73322d0SJohannes Weiner * successful direct reclaim run will revive a dormant kswapd. 3172c73322d0SJohannes Weiner */ 3173c73322d0SJohannes Weiner if (reclaimable) 3174c73322d0SJohannes Weiner pgdat->kswapd_failures = 0; 3175f16015fbSJohannes Weiner } 3176f16015fbSJohannes Weiner 317753853e2dSVlastimil Babka /* 3178fdd4c614SVlastimil Babka * Returns true if compaction should go ahead for a costly-order request, or 3179fdd4c614SVlastimil Babka * the allocation would already succeed without compaction. Return false if we 3180fdd4c614SVlastimil Babka * should reclaim first. 318153853e2dSVlastimil Babka */ 31824f588331SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 3183fe4b1b24SMel Gorman { 318431483b6aSMel Gorman unsigned long watermark; 3185fdd4c614SVlastimil Babka enum compact_result suitable; 3186fe4b1b24SMel Gorman 3187fdd4c614SVlastimil Babka suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx); 3188fdd4c614SVlastimil Babka if (suitable == COMPACT_SUCCESS) 3189fdd4c614SVlastimil Babka /* Allocation should succeed already. Don't reclaim. */ 3190fdd4c614SVlastimil Babka return true; 3191fdd4c614SVlastimil Babka if (suitable == COMPACT_SKIPPED) 3192fdd4c614SVlastimil Babka /* Compaction cannot yet proceed. Do reclaim. */ 3193fe4b1b24SMel Gorman return false; 3194fe4b1b24SMel Gorman 3195fdd4c614SVlastimil Babka /* 3196fdd4c614SVlastimil Babka * Compaction is already possible, but it takes time to run and there 3197fdd4c614SVlastimil Babka * are potentially other callers using the pages just freed. So proceed 3198fdd4c614SVlastimil Babka * with reclaim to make a buffer of free pages available to give 3199fdd4c614SVlastimil Babka * compaction a reasonable chance of completing and allocating the page. 3200fdd4c614SVlastimil Babka * Note that we won't actually reclaim the whole buffer in one attempt 3201fdd4c614SVlastimil Babka * as the target watermark in should_continue_reclaim() is lower. But if 3202fdd4c614SVlastimil Babka * we are already above the high+gap watermark, don't reclaim at all. 3203fdd4c614SVlastimil Babka */ 3204fdd4c614SVlastimil Babka watermark = high_wmark_pages(zone) + compact_gap(sc->order); 3205fdd4c614SVlastimil Babka 3206fdd4c614SVlastimil Babka return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); 3207fe4b1b24SMel Gorman } 3208fe4b1b24SMel Gorman 32091da177e4SLinus Torvalds /* 32101da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 32111da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 32121da177e4SLinus Torvalds * request. 32131da177e4SLinus Torvalds * 32141da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 32151da177e4SLinus Torvalds * scan then give up on it. 32161da177e4SLinus Torvalds */ 32170a0337e0SMichal Hocko static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 32181da177e4SLinus Torvalds { 3219dd1a239fSMel Gorman struct zoneref *z; 322054a6eb5cSMel Gorman struct zone *zone; 32210608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 32220608f43dSAndrew Morton unsigned long nr_soft_scanned; 3223619d0d76SWeijie Yang gfp_t orig_mask; 322479dafcdcSMel Gorman pg_data_t *last_pgdat = NULL; 32251cfb419bSKAMEZAWA Hiroyuki 3226cc715d99SMel Gorman /* 3227cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 3228cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 3229cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 3230cc715d99SMel Gorman */ 3231619d0d76SWeijie Yang orig_mask = sc->gfp_mask; 3232b2e18757SMel Gorman if (buffer_heads_over_limit) { 3233cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 32344f588331SMel Gorman sc->reclaim_idx = gfp_zone(sc->gfp_mask); 3235b2e18757SMel Gorman } 3236cc715d99SMel Gorman 3237d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 3238b2e18757SMel Gorman sc->reclaim_idx, sc->nodemask) { 3239b2e18757SMel Gorman /* 32401cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 32411cfb419bSKAMEZAWA Hiroyuki * to global LRU. 32421cfb419bSKAMEZAWA Hiroyuki */ 3243b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) { 3244344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, 3245344736f2SVladimir Davydov GFP_KERNEL | __GFP_HARDWALL)) 32461da177e4SLinus Torvalds continue; 324765ec02cbSVladimir Davydov 3248e0887c19SRik van Riel /* 3249e0c23279SMel Gorman * If we already have plenty of memory free for 3250e0c23279SMel Gorman * compaction in this zone, don't free any more. 3251e0c23279SMel Gorman * Even though compaction is invoked for any 3252e0c23279SMel Gorman * non-zero order, only frequent costly order 3253e0c23279SMel Gorman * reclamation is disruptive enough to become a 3254c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 3255c7cfa37bSCopot Alexandru * page allocations. 3256e0887c19SRik van Riel */ 32570b06496aSJohannes Weiner if (IS_ENABLED(CONFIG_COMPACTION) && 32580b06496aSJohannes Weiner sc->order > PAGE_ALLOC_COSTLY_ORDER && 32594f588331SMel Gorman compaction_ready(zone, sc)) { 32600b06496aSJohannes Weiner sc->compaction_ready = true; 3261e0887c19SRik van Riel continue; 3262e0887c19SRik van Riel } 32630b06496aSJohannes Weiner 32640608f43dSAndrew Morton /* 326579dafcdcSMel Gorman * Shrink each node in the zonelist once. If the 326679dafcdcSMel Gorman * zonelist is ordered by zone (not the default) then a 326779dafcdcSMel Gorman * node may be shrunk multiple times but in that case 326879dafcdcSMel Gorman * the user prefers lower zones being preserved. 326979dafcdcSMel Gorman */ 327079dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 327179dafcdcSMel Gorman continue; 327279dafcdcSMel Gorman 327379dafcdcSMel Gorman /* 32740608f43dSAndrew Morton * This steals pages from memory cgroups over softlimit 32750608f43dSAndrew Morton * and returns the number of reclaimed pages and 32760608f43dSAndrew Morton * scanned pages. This works for global memory pressure 32770608f43dSAndrew Morton * and balancing, not for a memcg's limit. 32780608f43dSAndrew Morton */ 32790608f43dSAndrew Morton nr_soft_scanned = 0; 3280ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, 32810608f43dSAndrew Morton sc->order, sc->gfp_mask, 32820608f43dSAndrew Morton &nr_soft_scanned); 32830608f43dSAndrew Morton sc->nr_reclaimed += nr_soft_reclaimed; 32840608f43dSAndrew Morton sc->nr_scanned += nr_soft_scanned; 3285ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 3286ac34a1a3SKAMEZAWA Hiroyuki } 3287d149e3b2SYing Han 328879dafcdcSMel Gorman /* See comment about same check for global reclaim above */ 328979dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 329079dafcdcSMel Gorman continue; 329179dafcdcSMel Gorman last_pgdat = zone->zone_pgdat; 3292970a39a3SMel Gorman shrink_node(zone->zone_pgdat, sc); 32931da177e4SLinus Torvalds } 3294e0c23279SMel Gorman 329565ec02cbSVladimir Davydov /* 3296619d0d76SWeijie Yang * Restore to original mask to avoid the impact on the caller if we 3297619d0d76SWeijie Yang * promoted it to __GFP_HIGHMEM. 3298619d0d76SWeijie Yang */ 3299619d0d76SWeijie Yang sc->gfp_mask = orig_mask; 33001da177e4SLinus Torvalds } 33011da177e4SLinus Torvalds 3302b910718aSJohannes Weiner static void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat) 33032a2e4885SJohannes Weiner { 3304b910718aSJohannes Weiner struct lruvec *target_lruvec; 33052a2e4885SJohannes Weiner unsigned long refaults; 33062a2e4885SJohannes Weiner 3307b910718aSJohannes Weiner target_lruvec = mem_cgroup_lruvec(target_memcg, pgdat); 3308170b04b7SJoonsoo Kim refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_ANON); 3309170b04b7SJoonsoo Kim target_lruvec->refaults[0] = refaults; 3310170b04b7SJoonsoo Kim refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_FILE); 3311170b04b7SJoonsoo Kim target_lruvec->refaults[1] = refaults; 33122a2e4885SJohannes Weiner } 33132a2e4885SJohannes Weiner 33141da177e4SLinus Torvalds /* 33151da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 33161da177e4SLinus Torvalds * 33171da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 33181da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 33191da177e4SLinus Torvalds * 33201da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 33211da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 33225b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 33235b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 33245b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 33255b0830cbSJens Axboe * work, and the allocation attempt will fail. 3326a41f24eaSNishanth Aravamudan * 3327a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 3328a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 33291da177e4SLinus Torvalds */ 3330dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 33313115cd91SVladimir Davydov struct scan_control *sc) 33321da177e4SLinus Torvalds { 3333241994edSJohannes Weiner int initial_priority = sc->priority; 33342a2e4885SJohannes Weiner pg_data_t *last_pgdat; 33352a2e4885SJohannes Weiner struct zoneref *z; 33362a2e4885SJohannes Weiner struct zone *zone; 3337241994edSJohannes Weiner retry: 3338873b4771SKeika Kobayashi delayacct_freepages_start(); 3339873b4771SKeika Kobayashi 3340b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 33417cc30fcfSMel Gorman __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); 33421da177e4SLinus Torvalds 33439e3b2f8cSKonstantin Khlebnikov do { 334470ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 334570ddf637SAnton Vorontsov sc->priority); 334666e1707bSBalbir Singh sc->nr_scanned = 0; 33470a0337e0SMichal Hocko shrink_zones(zonelist, sc); 3348e0c23279SMel Gorman 3349bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 33500b06496aSJohannes Weiner break; 33510b06496aSJohannes Weiner 33520b06496aSJohannes Weiner if (sc->compaction_ready) 33530b06496aSJohannes Weiner break; 33541da177e4SLinus Torvalds 33551da177e4SLinus Torvalds /* 33560e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 33570e50ce3bSMinchan Kim * writepage even in laptop mode. 33580e50ce3bSMinchan Kim */ 33590e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 33600e50ce3bSMinchan Kim sc->may_writepage = 1; 33610b06496aSJohannes Weiner } while (--sc->priority >= 0); 3362bb21c7ceSKOSAKI Motohiro 33632a2e4885SJohannes Weiner last_pgdat = NULL; 33642a2e4885SJohannes Weiner for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx, 33652a2e4885SJohannes Weiner sc->nodemask) { 33662a2e4885SJohannes Weiner if (zone->zone_pgdat == last_pgdat) 33672a2e4885SJohannes Weiner continue; 33682a2e4885SJohannes Weiner last_pgdat = zone->zone_pgdat; 33691b05117dSJohannes Weiner 33702a2e4885SJohannes Weiner snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat); 33711b05117dSJohannes Weiner 33721b05117dSJohannes Weiner if (cgroup_reclaim(sc)) { 33731b05117dSJohannes Weiner struct lruvec *lruvec; 33741b05117dSJohannes Weiner 33751b05117dSJohannes Weiner lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, 33761b05117dSJohannes Weiner zone->zone_pgdat); 33771b05117dSJohannes Weiner clear_bit(LRUVEC_CONGESTED, &lruvec->flags); 33781b05117dSJohannes Weiner } 33792a2e4885SJohannes Weiner } 33802a2e4885SJohannes Weiner 3381873b4771SKeika Kobayashi delayacct_freepages_end(); 3382873b4771SKeika Kobayashi 3383bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 3384bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 3385bb21c7ceSKOSAKI Motohiro 33860cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 33870b06496aSJohannes Weiner if (sc->compaction_ready) 33887335084dSMel Gorman return 1; 33897335084dSMel Gorman 3390b91ac374SJohannes Weiner /* 3391b91ac374SJohannes Weiner * We make inactive:active ratio decisions based on the node's 3392b91ac374SJohannes Weiner * composition of memory, but a restrictive reclaim_idx or a 3393b91ac374SJohannes Weiner * memory.low cgroup setting can exempt large amounts of 3394b91ac374SJohannes Weiner * memory from reclaim. Neither of which are very common, so 3395b91ac374SJohannes Weiner * instead of doing costly eligibility calculations of the 3396b91ac374SJohannes Weiner * entire cgroup subtree up front, we assume the estimates are 3397b91ac374SJohannes Weiner * good, and retry with forcible deactivation if that fails. 3398b91ac374SJohannes Weiner */ 3399b91ac374SJohannes Weiner if (sc->skipped_deactivate) { 3400b91ac374SJohannes Weiner sc->priority = initial_priority; 3401b91ac374SJohannes Weiner sc->force_deactivate = 1; 3402b91ac374SJohannes Weiner sc->skipped_deactivate = 0; 3403b91ac374SJohannes Weiner goto retry; 3404b91ac374SJohannes Weiner } 3405b91ac374SJohannes Weiner 3406241994edSJohannes Weiner /* Untapped cgroup reserves? Don't OOM, retry. */ 3407d6622f63SYisheng Xie if (sc->memcg_low_skipped) { 3408241994edSJohannes Weiner sc->priority = initial_priority; 3409b91ac374SJohannes Weiner sc->force_deactivate = 0; 3410d6622f63SYisheng Xie sc->memcg_low_reclaim = 1; 3411d6622f63SYisheng Xie sc->memcg_low_skipped = 0; 3412241994edSJohannes Weiner goto retry; 3413241994edSJohannes Weiner } 3414241994edSJohannes Weiner 3415bb21c7ceSKOSAKI Motohiro return 0; 34161da177e4SLinus Torvalds } 34171da177e4SLinus Torvalds 3418c73322d0SJohannes Weiner static bool allow_direct_reclaim(pg_data_t *pgdat) 34195515061dSMel Gorman { 34205515061dSMel Gorman struct zone *zone; 34215515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 34225515061dSMel Gorman unsigned long free_pages = 0; 34235515061dSMel Gorman int i; 34245515061dSMel Gorman bool wmark_ok; 34255515061dSMel Gorman 3426c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3427c73322d0SJohannes Weiner return true; 3428c73322d0SJohannes Weiner 34295515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 34305515061dSMel Gorman zone = &pgdat->node_zones[i]; 3431d450abd8SJohannes Weiner if (!managed_zone(zone)) 3432d450abd8SJohannes Weiner continue; 3433d450abd8SJohannes Weiner 3434d450abd8SJohannes Weiner if (!zone_reclaimable_pages(zone)) 3435675becceSMel Gorman continue; 3436675becceSMel Gorman 34375515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 34385515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 34395515061dSMel Gorman } 34405515061dSMel Gorman 3441675becceSMel Gorman /* If there are no reserves (unexpected config) then do not throttle */ 3442675becceSMel Gorman if (!pfmemalloc_reserve) 3443675becceSMel Gorman return true; 3444675becceSMel Gorman 34455515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 34465515061dSMel Gorman 34475515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 34485515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 344997a225e6SJoonsoo Kim if (READ_ONCE(pgdat->kswapd_highest_zoneidx) > ZONE_NORMAL) 345097a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, ZONE_NORMAL); 34515644e1fbSQian Cai 34525515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 34535515061dSMel Gorman } 34545515061dSMel Gorman 34555515061dSMel Gorman return wmark_ok; 34565515061dSMel Gorman } 34575515061dSMel Gorman 34585515061dSMel Gorman /* 34595515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 34605515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 34615515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 346250694c28SMel Gorman * when the low watermark is reached. 346350694c28SMel Gorman * 346450694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 346550694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 34665515061dSMel Gorman */ 346750694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 34685515061dSMel Gorman nodemask_t *nodemask) 34695515061dSMel Gorman { 3470675becceSMel Gorman struct zoneref *z; 34715515061dSMel Gorman struct zone *zone; 3472675becceSMel Gorman pg_data_t *pgdat = NULL; 34735515061dSMel Gorman 34745515061dSMel Gorman /* 34755515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 34765515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 34775515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 34785515061dSMel Gorman * committing a transaction where throttling it could forcing other 34795515061dSMel Gorman * processes to block on log_wait_commit(). 34805515061dSMel Gorman */ 34815515061dSMel Gorman if (current->flags & PF_KTHREAD) 348250694c28SMel Gorman goto out; 348350694c28SMel Gorman 348450694c28SMel Gorman /* 348550694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 348650694c28SMel Gorman * It should return quickly so it can exit and free its memory 348750694c28SMel Gorman */ 348850694c28SMel Gorman if (fatal_signal_pending(current)) 348950694c28SMel Gorman goto out; 34905515061dSMel Gorman 3491675becceSMel Gorman /* 3492675becceSMel Gorman * Check if the pfmemalloc reserves are ok by finding the first node 3493675becceSMel Gorman * with a usable ZONE_NORMAL or lower zone. The expectation is that 3494675becceSMel Gorman * GFP_KERNEL will be required for allocating network buffers when 3495675becceSMel Gorman * swapping over the network so ZONE_HIGHMEM is unusable. 3496675becceSMel Gorman * 3497675becceSMel Gorman * Throttling is based on the first usable node and throttled processes 3498675becceSMel Gorman * wait on a queue until kswapd makes progress and wakes them. There 3499675becceSMel Gorman * is an affinity then between processes waking up and where reclaim 3500675becceSMel Gorman * progress has been made assuming the process wakes on the same node. 3501675becceSMel Gorman * More importantly, processes running on remote nodes will not compete 3502675becceSMel Gorman * for remote pfmemalloc reserves and processes on different nodes 3503675becceSMel Gorman * should make reasonable progress. 3504675becceSMel Gorman */ 3505675becceSMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 350617636faaSMichael S. Tsirkin gfp_zone(gfp_mask), nodemask) { 3507675becceSMel Gorman if (zone_idx(zone) > ZONE_NORMAL) 3508675becceSMel Gorman continue; 3509675becceSMel Gorman 3510675becceSMel Gorman /* Throttle based on the first usable node */ 35115515061dSMel Gorman pgdat = zone->zone_pgdat; 3512c73322d0SJohannes Weiner if (allow_direct_reclaim(pgdat)) 351350694c28SMel Gorman goto out; 3514675becceSMel Gorman break; 3515675becceSMel Gorman } 3516675becceSMel Gorman 3517675becceSMel Gorman /* If no zone was usable by the allocation flags then do not throttle */ 3518675becceSMel Gorman if (!pgdat) 3519675becceSMel Gorman goto out; 35205515061dSMel Gorman 352168243e76SMel Gorman /* Account for the throttling */ 352268243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 352368243e76SMel Gorman 35245515061dSMel Gorman /* 35255515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 35265515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 35275515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 35285515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 35295515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 35305515061dSMel Gorman * second before continuing. 35315515061dSMel Gorman */ 35325515061dSMel Gorman if (!(gfp_mask & __GFP_FS)) { 35335515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 3534c73322d0SJohannes Weiner allow_direct_reclaim(pgdat), HZ); 353550694c28SMel Gorman 353650694c28SMel Gorman goto check_pending; 35375515061dSMel Gorman } 35385515061dSMel Gorman 35395515061dSMel Gorman /* Throttle until kswapd wakes the process */ 35405515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 3541c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)); 354250694c28SMel Gorman 354350694c28SMel Gorman check_pending: 354450694c28SMel Gorman if (fatal_signal_pending(current)) 354550694c28SMel Gorman return true; 354650694c28SMel Gorman 354750694c28SMel Gorman out: 354850694c28SMel Gorman return false; 35495515061dSMel Gorman } 35505515061dSMel Gorman 3551dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 3552327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 355366e1707bSBalbir Singh { 355433906bc5SMel Gorman unsigned long nr_reclaimed; 355566e1707bSBalbir Singh struct scan_control sc = { 355622fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3557f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 3558b2e18757SMel Gorman .reclaim_idx = gfp_zone(gfp_mask), 3559ee814fe2SJohannes Weiner .order = order, 3560ee814fe2SJohannes Weiner .nodemask = nodemask, 3561ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 3562ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3563a6dc60f8SJohannes Weiner .may_unmap = 1, 35642e2e4259SKOSAKI Motohiro .may_swap = 1, 356566e1707bSBalbir Singh }; 356666e1707bSBalbir Singh 35675515061dSMel Gorman /* 3568bb451fdfSGreg Thelen * scan_control uses s8 fields for order, priority, and reclaim_idx. 3569bb451fdfSGreg Thelen * Confirm they are large enough for max values. 3570bb451fdfSGreg Thelen */ 3571bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_ORDER > S8_MAX); 3572bb451fdfSGreg Thelen BUILD_BUG_ON(DEF_PRIORITY > S8_MAX); 3573bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX); 3574bb451fdfSGreg Thelen 3575bb451fdfSGreg Thelen /* 357650694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 357750694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 357850694c28SMel Gorman * point. 35795515061dSMel Gorman */ 3580f2f43e56SNick Desaulniers if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask)) 35815515061dSMel Gorman return 1; 35825515061dSMel Gorman 35831732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 35843481c37fSYafang Shao trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask); 358533906bc5SMel Gorman 35863115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 358733906bc5SMel Gorman 358833906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 35891732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 359033906bc5SMel Gorman 359133906bc5SMel Gorman return nr_reclaimed; 359266e1707bSBalbir Singh } 359366e1707bSBalbir Singh 3594c255a458SAndrew Morton #ifdef CONFIG_MEMCG 359566e1707bSBalbir Singh 3596d2e5fb92SMichal Hocko /* Only used by soft limit reclaim. Do not reuse for anything else. */ 3597a9dd0a83SMel Gorman unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, 35984e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 3599ef8f2327SMel Gorman pg_data_t *pgdat, 36000ae5e89cSYing Han unsigned long *nr_scanned) 36014e416953SBalbir Singh { 3602afaf07a6SJohannes Weiner struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); 36034e416953SBalbir Singh struct scan_control sc = { 3604b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3605ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 36064e416953SBalbir Singh .may_writepage = !laptop_mode, 36074e416953SBalbir Singh .may_unmap = 1, 3608b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 36094e416953SBalbir Singh .may_swap = !noswap, 36104e416953SBalbir Singh }; 36110ae5e89cSYing Han 3612d2e5fb92SMichal Hocko WARN_ON_ONCE(!current->reclaim_state); 3613d2e5fb92SMichal Hocko 36144e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 36154e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 3616bdce6d9eSKOSAKI Motohiro 36179e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 36183481c37fSYafang Shao sc.gfp_mask); 3619bdce6d9eSKOSAKI Motohiro 36204e416953SBalbir Singh /* 36214e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 36224e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 3623a9dd0a83SMel Gorman * if we don't reclaim here, the shrink_node from balance_pgdat 36244e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 36254e416953SBalbir Singh * the priority and make it zero. 36264e416953SBalbir Singh */ 3627afaf07a6SJohannes Weiner shrink_lruvec(lruvec, &sc); 3628bdce6d9eSKOSAKI Motohiro 3629bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 3630bdce6d9eSKOSAKI Motohiro 36310ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 36320308f7cfSYafang Shao 36334e416953SBalbir Singh return sc.nr_reclaimed; 36344e416953SBalbir Singh } 36354e416953SBalbir Singh 363672835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 3637b70a2a21SJohannes Weiner unsigned long nr_pages, 36388c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 3639b70a2a21SJohannes Weiner bool may_swap) 364066e1707bSBalbir Singh { 3641bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 3642499118e9SVlastimil Babka unsigned int noreclaim_flag; 364366e1707bSBalbir Singh struct scan_control sc = { 3644b70a2a21SJohannes Weiner .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 36457dea19f9SMichal Hocko .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | 3646ee814fe2SJohannes Weiner (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 3647b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 3648ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 3649ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 365066e1707bSBalbir Singh .may_writepage = !laptop_mode, 3651a6dc60f8SJohannes Weiner .may_unmap = 1, 3652b70a2a21SJohannes Weiner .may_swap = may_swap, 3653a09ed5e0SYing Han }; 3654fa40d1eeSShakeel Butt /* 3655fa40d1eeSShakeel Butt * Traverse the ZONELIST_FALLBACK zonelist of the current node to put 3656fa40d1eeSShakeel Butt * equal pressure on all the nodes. This is based on the assumption that 3657fa40d1eeSShakeel Butt * the reclaim does not bail out early. 3658fa40d1eeSShakeel Butt */ 3659fa40d1eeSShakeel Butt struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 366066e1707bSBalbir Singh 36611732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 36623481c37fSYafang Shao trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask); 3663499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 3664eb414681SJohannes Weiner 36653115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3666eb414681SJohannes Weiner 3667499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 3668bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 36691732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 3670bdce6d9eSKOSAKI Motohiro 3671bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 367266e1707bSBalbir Singh } 367366e1707bSBalbir Singh #endif 367466e1707bSBalbir Singh 36751d82de61SMel Gorman static void age_active_anon(struct pglist_data *pgdat, 3676ef8f2327SMel Gorman struct scan_control *sc) 3677f16015fbSJohannes Weiner { 3678b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 3679b91ac374SJohannes Weiner struct lruvec *lruvec; 3680b95a2f2dSJohannes Weiner 3681b95a2f2dSJohannes Weiner if (!total_swap_pages) 3682b95a2f2dSJohannes Weiner return; 3683b95a2f2dSJohannes Weiner 3684b91ac374SJohannes Weiner lruvec = mem_cgroup_lruvec(NULL, pgdat); 3685b91ac374SJohannes Weiner if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON)) 3686b91ac374SJohannes Weiner return; 3687b91ac374SJohannes Weiner 3688b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 3689b95a2f2dSJohannes Weiner do { 3690b91ac374SJohannes Weiner lruvec = mem_cgroup_lruvec(memcg, pgdat); 36911a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 36929e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 3693b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 3694b95a2f2dSJohannes Weiner } while (memcg); 3695f16015fbSJohannes Weiner } 3696f16015fbSJohannes Weiner 369797a225e6SJoonsoo Kim static bool pgdat_watermark_boosted(pg_data_t *pgdat, int highest_zoneidx) 36981c30844dSMel Gorman { 36991c30844dSMel Gorman int i; 37001c30844dSMel Gorman struct zone *zone; 37011c30844dSMel Gorman 37021c30844dSMel Gorman /* 37031c30844dSMel Gorman * Check for watermark boosts top-down as the higher zones 37041c30844dSMel Gorman * are more likely to be boosted. Both watermarks and boosts 37051eba09c1SRandy Dunlap * should not be checked at the same time as reclaim would 37061c30844dSMel Gorman * start prematurely when there is no boosting and a lower 37071c30844dSMel Gorman * zone is balanced. 37081c30844dSMel Gorman */ 370997a225e6SJoonsoo Kim for (i = highest_zoneidx; i >= 0; i--) { 37101c30844dSMel Gorman zone = pgdat->node_zones + i; 37111c30844dSMel Gorman if (!managed_zone(zone)) 37121c30844dSMel Gorman continue; 37131c30844dSMel Gorman 37141c30844dSMel Gorman if (zone->watermark_boost) 37151c30844dSMel Gorman return true; 37161c30844dSMel Gorman } 37171c30844dSMel Gorman 37181c30844dSMel Gorman return false; 37191c30844dSMel Gorman } 37201c30844dSMel Gorman 3721e716f2ebSMel Gorman /* 3722e716f2ebSMel Gorman * Returns true if there is an eligible zone balanced for the request order 372397a225e6SJoonsoo Kim * and highest_zoneidx 3724e716f2ebSMel Gorman */ 372597a225e6SJoonsoo Kim static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx) 372660cefed4SJohannes Weiner { 3727e716f2ebSMel Gorman int i; 3728e716f2ebSMel Gorman unsigned long mark = -1; 3729e716f2ebSMel Gorman struct zone *zone; 373060cefed4SJohannes Weiner 37311c30844dSMel Gorman /* 37321c30844dSMel Gorman * Check watermarks bottom-up as lower zones are more likely to 37331c30844dSMel Gorman * meet watermarks. 37341c30844dSMel Gorman */ 373597a225e6SJoonsoo Kim for (i = 0; i <= highest_zoneidx; i++) { 3736e716f2ebSMel Gorman zone = pgdat->node_zones + i; 37376256c6b4SMel Gorman 3738e716f2ebSMel Gorman if (!managed_zone(zone)) 3739e716f2ebSMel Gorman continue; 3740e716f2ebSMel Gorman 3741e716f2ebSMel Gorman mark = high_wmark_pages(zone); 374297a225e6SJoonsoo Kim if (zone_watermark_ok_safe(zone, order, mark, highest_zoneidx)) 37436256c6b4SMel Gorman return true; 374460cefed4SJohannes Weiner } 374560cefed4SJohannes Weiner 3746e716f2ebSMel Gorman /* 374797a225e6SJoonsoo Kim * If a node has no populated zone within highest_zoneidx, it does not 3748e716f2ebSMel Gorman * need balancing by definition. This can happen if a zone-restricted 3749e716f2ebSMel Gorman * allocation tries to wake a remote kswapd. 3750e716f2ebSMel Gorman */ 3751e716f2ebSMel Gorman if (mark == -1) 3752e716f2ebSMel Gorman return true; 3753e716f2ebSMel Gorman 3754e716f2ebSMel Gorman return false; 3755e716f2ebSMel Gorman } 3756e716f2ebSMel Gorman 3757631b6e08SMel Gorman /* Clear pgdat state for congested, dirty or under writeback. */ 3758631b6e08SMel Gorman static void clear_pgdat_congested(pg_data_t *pgdat) 3759631b6e08SMel Gorman { 37601b05117dSJohannes Weiner struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat); 37611b05117dSJohannes Weiner 37621b05117dSJohannes Weiner clear_bit(LRUVEC_CONGESTED, &lruvec->flags); 3763631b6e08SMel Gorman clear_bit(PGDAT_DIRTY, &pgdat->flags); 3764631b6e08SMel Gorman clear_bit(PGDAT_WRITEBACK, &pgdat->flags); 3765631b6e08SMel Gorman } 3766631b6e08SMel Gorman 37671741c877SMel Gorman /* 37685515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 37695515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 37705515061dSMel Gorman * 37715515061dSMel Gorman * Returns true if kswapd is ready to sleep 37725515061dSMel Gorman */ 377397a225e6SJoonsoo Kim static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, 377497a225e6SJoonsoo Kim int highest_zoneidx) 3775f50de2d3SMel Gorman { 37765515061dSMel Gorman /* 37779e5e3661SVlastimil Babka * The throttled processes are normally woken up in balance_pgdat() as 3778c73322d0SJohannes Weiner * soon as allow_direct_reclaim() is true. But there is a potential 37799e5e3661SVlastimil Babka * race between when kswapd checks the watermarks and a process gets 37809e5e3661SVlastimil Babka * throttled. There is also a potential race if processes get 37819e5e3661SVlastimil Babka * throttled, kswapd wakes, a large process exits thereby balancing the 37829e5e3661SVlastimil Babka * zones, which causes kswapd to exit balance_pgdat() before reaching 37839e5e3661SVlastimil Babka * the wake up checks. If kswapd is going to sleep, no process should 37849e5e3661SVlastimil Babka * be sleeping on pfmemalloc_wait, so wake them now if necessary. If 37859e5e3661SVlastimil Babka * the wake up is premature, processes will wake kswapd and get 37869e5e3661SVlastimil Babka * throttled again. The difference from wake ups in balance_pgdat() is 37879e5e3661SVlastimil Babka * that here we are under prepare_to_wait(). 37885515061dSMel Gorman */ 37899e5e3661SVlastimil Babka if (waitqueue_active(&pgdat->pfmemalloc_wait)) 37909e5e3661SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 3791f50de2d3SMel Gorman 3792c73322d0SJohannes Weiner /* Hopeless node, leave it to direct reclaim */ 3793c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3794c73322d0SJohannes Weiner return true; 3795c73322d0SJohannes Weiner 379697a225e6SJoonsoo Kim if (pgdat_balanced(pgdat, order, highest_zoneidx)) { 3797631b6e08SMel Gorman clear_pgdat_congested(pgdat); 3798333b0a45SShantanu Goel return true; 37991d82de61SMel Gorman } 38001d82de61SMel Gorman 3801333b0a45SShantanu Goel return false; 3802f50de2d3SMel Gorman } 3803f50de2d3SMel Gorman 38041da177e4SLinus Torvalds /* 38051d82de61SMel Gorman * kswapd shrinks a node of pages that are at or below the highest usable 38061d82de61SMel Gorman * zone that is currently unbalanced. 3807b8e83b94SMel Gorman * 3808b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 3809283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 3810283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 381175485363SMel Gorman */ 38121d82de61SMel Gorman static bool kswapd_shrink_node(pg_data_t *pgdat, 3813accf6242SVlastimil Babka struct scan_control *sc) 381475485363SMel Gorman { 38151d82de61SMel Gorman struct zone *zone; 38161d82de61SMel Gorman int z; 381775485363SMel Gorman 38181d82de61SMel Gorman /* Reclaim a number of pages proportional to the number of zones */ 38191d82de61SMel Gorman sc->nr_to_reclaim = 0; 3820970a39a3SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 38211d82de61SMel Gorman zone = pgdat->node_zones + z; 38226aa303deSMel Gorman if (!managed_zone(zone)) 38231d82de61SMel Gorman continue; 38247c954f6dSMel Gorman 38251d82de61SMel Gorman sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); 38267c954f6dSMel Gorman } 38277c954f6dSMel Gorman 38281d82de61SMel Gorman /* 38291d82de61SMel Gorman * Historically care was taken to put equal pressure on all zones but 38301d82de61SMel Gorman * now pressure is applied based on node LRU order. 38311d82de61SMel Gorman */ 3832970a39a3SMel Gorman shrink_node(pgdat, sc); 38331d82de61SMel Gorman 38341d82de61SMel Gorman /* 38351d82de61SMel Gorman * Fragmentation may mean that the system cannot be rebalanced for 38361d82de61SMel Gorman * high-order allocations. If twice the allocation size has been 38371d82de61SMel Gorman * reclaimed then recheck watermarks only at order-0 to prevent 38381d82de61SMel Gorman * excessive reclaim. Assume that a process requested a high-order 38391d82de61SMel Gorman * can direct reclaim/compact. 38401d82de61SMel Gorman */ 38419861a62cSVlastimil Babka if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order)) 38421d82de61SMel Gorman sc->order = 0; 38431d82de61SMel Gorman 3844b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 384575485363SMel Gorman } 384675485363SMel Gorman 3847c49c2c47SMel Gorman /* Page allocator PCP high watermark is lowered if reclaim is active. */ 3848c49c2c47SMel Gorman static inline void 3849c49c2c47SMel Gorman update_reclaim_active(pg_data_t *pgdat, int highest_zoneidx, bool active) 3850c49c2c47SMel Gorman { 3851c49c2c47SMel Gorman int i; 3852c49c2c47SMel Gorman struct zone *zone; 3853c49c2c47SMel Gorman 3854c49c2c47SMel Gorman for (i = 0; i <= highest_zoneidx; i++) { 3855c49c2c47SMel Gorman zone = pgdat->node_zones + i; 3856c49c2c47SMel Gorman 3857c49c2c47SMel Gorman if (!managed_zone(zone)) 3858c49c2c47SMel Gorman continue; 3859c49c2c47SMel Gorman 3860c49c2c47SMel Gorman if (active) 3861c49c2c47SMel Gorman set_bit(ZONE_RECLAIM_ACTIVE, &zone->flags); 3862c49c2c47SMel Gorman else 3863c49c2c47SMel Gorman clear_bit(ZONE_RECLAIM_ACTIVE, &zone->flags); 3864c49c2c47SMel Gorman } 3865c49c2c47SMel Gorman } 3866c49c2c47SMel Gorman 3867c49c2c47SMel Gorman static inline void 3868c49c2c47SMel Gorman set_reclaim_active(pg_data_t *pgdat, int highest_zoneidx) 3869c49c2c47SMel Gorman { 3870c49c2c47SMel Gorman update_reclaim_active(pgdat, highest_zoneidx, true); 3871c49c2c47SMel Gorman } 3872c49c2c47SMel Gorman 3873c49c2c47SMel Gorman static inline void 3874c49c2c47SMel Gorman clear_reclaim_active(pg_data_t *pgdat, int highest_zoneidx) 3875c49c2c47SMel Gorman { 3876c49c2c47SMel Gorman update_reclaim_active(pgdat, highest_zoneidx, false); 3877c49c2c47SMel Gorman } 3878c49c2c47SMel Gorman 387975485363SMel Gorman /* 38801d82de61SMel Gorman * For kswapd, balance_pgdat() will reclaim pages across a node from zones 38811d82de61SMel Gorman * that are eligible for use by the caller until at least one zone is 38821d82de61SMel Gorman * balanced. 38831da177e4SLinus Torvalds * 38841d82de61SMel Gorman * Returns the order kswapd finished reclaiming at. 38851da177e4SLinus Torvalds * 38861da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 388741858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 38888bb4e7a2SWei Yang * found to have free_pages <= high_wmark_pages(zone), any page in that zone 38891d82de61SMel Gorman * or lower is eligible for reclaim until at least one usable zone is 38901d82de61SMel Gorman * balanced. 38911da177e4SLinus Torvalds */ 389297a225e6SJoonsoo Kim static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx) 38931da177e4SLinus Torvalds { 38941da177e4SLinus Torvalds int i; 38950608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 38960608f43dSAndrew Morton unsigned long nr_soft_scanned; 3897eb414681SJohannes Weiner unsigned long pflags; 38981c30844dSMel Gorman unsigned long nr_boost_reclaim; 38991c30844dSMel Gorman unsigned long zone_boosts[MAX_NR_ZONES] = { 0, }; 39001c30844dSMel Gorman bool boosted; 39011d82de61SMel Gorman struct zone *zone; 3902179e9639SAndrew Morton struct scan_control sc = { 3903179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 3904ee814fe2SJohannes Weiner .order = order, 3905a6dc60f8SJohannes Weiner .may_unmap = 1, 3906179e9639SAndrew Morton }; 390793781325SOmar Sandoval 39081732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 3909eb414681SJohannes Weiner psi_memstall_enter(&pflags); 39104f3eaf45SMatthew Wilcox (Oracle) __fs_reclaim_acquire(_THIS_IP_); 391193781325SOmar Sandoval 3912f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 39131da177e4SLinus Torvalds 39141c30844dSMel Gorman /* 39151c30844dSMel Gorman * Account for the reclaim boost. Note that the zone boost is left in 39161c30844dSMel Gorman * place so that parallel allocations that are near the watermark will 39171c30844dSMel Gorman * stall or direct reclaim until kswapd is finished. 39181c30844dSMel Gorman */ 39191c30844dSMel Gorman nr_boost_reclaim = 0; 392097a225e6SJoonsoo Kim for (i = 0; i <= highest_zoneidx; i++) { 39211c30844dSMel Gorman zone = pgdat->node_zones + i; 39221c30844dSMel Gorman if (!managed_zone(zone)) 39231c30844dSMel Gorman continue; 39241c30844dSMel Gorman 39251c30844dSMel Gorman nr_boost_reclaim += zone->watermark_boost; 39261c30844dSMel Gorman zone_boosts[i] = zone->watermark_boost; 39271c30844dSMel Gorman } 39281c30844dSMel Gorman boosted = nr_boost_reclaim; 39291c30844dSMel Gorman 39301c30844dSMel Gorman restart: 3931c49c2c47SMel Gorman set_reclaim_active(pgdat, highest_zoneidx); 39321c30844dSMel Gorman sc.priority = DEF_PRIORITY; 39339e3b2f8cSKonstantin Khlebnikov do { 3934c73322d0SJohannes Weiner unsigned long nr_reclaimed = sc.nr_reclaimed; 3935b8e83b94SMel Gorman bool raise_priority = true; 39361c30844dSMel Gorman bool balanced; 393793781325SOmar Sandoval bool ret; 3938b8e83b94SMel Gorman 393997a225e6SJoonsoo Kim sc.reclaim_idx = highest_zoneidx; 39401da177e4SLinus Torvalds 394186c79f6bSMel Gorman /* 394284c7a777SMel Gorman * If the number of buffer_heads exceeds the maximum allowed 394384c7a777SMel Gorman * then consider reclaiming from all zones. This has a dual 394484c7a777SMel Gorman * purpose -- on 64-bit systems it is expected that 394584c7a777SMel Gorman * buffer_heads are stripped during active rotation. On 32-bit 394684c7a777SMel Gorman * systems, highmem pages can pin lowmem memory and shrinking 394784c7a777SMel Gorman * buffers can relieve lowmem pressure. Reclaim may still not 394884c7a777SMel Gorman * go ahead if all eligible zones for the original allocation 394984c7a777SMel Gorman * request are balanced to avoid excessive reclaim from kswapd. 395086c79f6bSMel Gorman */ 395186c79f6bSMel Gorman if (buffer_heads_over_limit) { 395286c79f6bSMel Gorman for (i = MAX_NR_ZONES - 1; i >= 0; i--) { 395386c79f6bSMel Gorman zone = pgdat->node_zones + i; 39546aa303deSMel Gorman if (!managed_zone(zone)) 395586c79f6bSMel Gorman continue; 395686c79f6bSMel Gorman 3957970a39a3SMel Gorman sc.reclaim_idx = i; 395886c79f6bSMel Gorman break; 395986c79f6bSMel Gorman } 396086c79f6bSMel Gorman } 396186c79f6bSMel Gorman 396286c79f6bSMel Gorman /* 39631c30844dSMel Gorman * If the pgdat is imbalanced then ignore boosting and preserve 39641c30844dSMel Gorman * the watermarks for a later time and restart. Note that the 39651c30844dSMel Gorman * zone watermarks will be still reset at the end of balancing 39661c30844dSMel Gorman * on the grounds that the normal reclaim should be enough to 39671c30844dSMel Gorman * re-evaluate if boosting is required when kswapd next wakes. 396886c79f6bSMel Gorman */ 396997a225e6SJoonsoo Kim balanced = pgdat_balanced(pgdat, sc.order, highest_zoneidx); 39701c30844dSMel Gorman if (!balanced && nr_boost_reclaim) { 39711c30844dSMel Gorman nr_boost_reclaim = 0; 39721c30844dSMel Gorman goto restart; 39731c30844dSMel Gorman } 39741c30844dSMel Gorman 39751c30844dSMel Gorman /* 39761c30844dSMel Gorman * If boosting is not active then only reclaim if there are no 39771c30844dSMel Gorman * eligible zones. Note that sc.reclaim_idx is not used as 39781c30844dSMel Gorman * buffer_heads_over_limit may have adjusted it. 39791c30844dSMel Gorman */ 39801c30844dSMel Gorman if (!nr_boost_reclaim && balanced) 39811da177e4SLinus Torvalds goto out; 3982e1dbeda6SAndrew Morton 39831c30844dSMel Gorman /* Limit the priority of boosting to avoid reclaim writeback */ 39841c30844dSMel Gorman if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2) 39851c30844dSMel Gorman raise_priority = false; 39861c30844dSMel Gorman 39871c30844dSMel Gorman /* 39881c30844dSMel Gorman * Do not writeback or swap pages for boosted reclaim. The 39891c30844dSMel Gorman * intent is to relieve pressure not issue sub-optimal IO 39901c30844dSMel Gorman * from reclaim context. If no pages are reclaimed, the 39911c30844dSMel Gorman * reclaim will be aborted. 39921c30844dSMel Gorman */ 39931c30844dSMel Gorman sc.may_writepage = !laptop_mode && !nr_boost_reclaim; 39941c30844dSMel Gorman sc.may_swap = !nr_boost_reclaim; 39951c30844dSMel Gorman 39961da177e4SLinus Torvalds /* 39971d82de61SMel Gorman * Do some background aging of the anon list, to give 39981d82de61SMel Gorman * pages a chance to be referenced before reclaiming. All 39991d82de61SMel Gorman * pages are rotated regardless of classzone as this is 40001d82de61SMel Gorman * about consistent aging. 40011d82de61SMel Gorman */ 4002ef8f2327SMel Gorman age_active_anon(pgdat, &sc); 40031d82de61SMel Gorman 40041d82de61SMel Gorman /* 4005b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 4006b7ea3c41SMel Gorman * even in laptop mode. 4007b7ea3c41SMel Gorman */ 4008047d72c3SJohannes Weiner if (sc.priority < DEF_PRIORITY - 2) 4009b7ea3c41SMel Gorman sc.may_writepage = 1; 4010b7ea3c41SMel Gorman 40111d82de61SMel Gorman /* Call soft limit reclaim before calling shrink_node. */ 40121da177e4SLinus Torvalds sc.nr_scanned = 0; 40130608f43dSAndrew Morton nr_soft_scanned = 0; 4014ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, 40151d82de61SMel Gorman sc.gfp_mask, &nr_soft_scanned); 40160608f43dSAndrew Morton sc.nr_reclaimed += nr_soft_reclaimed; 40170608f43dSAndrew Morton 401832a4330dSRik van Riel /* 40191d82de61SMel Gorman * There should be no need to raise the scanning priority if 40201d82de61SMel Gorman * enough pages are already being scanned that that high 40211d82de61SMel Gorman * watermark would be met at 100% efficiency. 402232a4330dSRik van Riel */ 4023970a39a3SMel Gorman if (kswapd_shrink_node(pgdat, &sc)) 4024b8e83b94SMel Gorman raise_priority = false; 4025d7868daeSMel Gorman 40265515061dSMel Gorman /* 40275515061dSMel Gorman * If the low watermark is met there is no need for processes 40285515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 40295515061dSMel Gorman * able to safely make forward progress. Wake them 40305515061dSMel Gorman */ 40315515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 4032c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)) 4033cfc51155SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 40345515061dSMel Gorman 4035b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 40364f3eaf45SMatthew Wilcox (Oracle) __fs_reclaim_release(_THIS_IP_); 403793781325SOmar Sandoval ret = try_to_freeze(); 40384f3eaf45SMatthew Wilcox (Oracle) __fs_reclaim_acquire(_THIS_IP_); 403993781325SOmar Sandoval if (ret || kthread_should_stop()) 4040b8e83b94SMel Gorman break; 4041b8e83b94SMel Gorman 4042b8e83b94SMel Gorman /* 4043b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 4044b8e83b94SMel Gorman * progress in reclaiming pages 4045b8e83b94SMel Gorman */ 4046c73322d0SJohannes Weiner nr_reclaimed = sc.nr_reclaimed - nr_reclaimed; 40471c30844dSMel Gorman nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed); 40481c30844dSMel Gorman 40491c30844dSMel Gorman /* 40501c30844dSMel Gorman * If reclaim made no progress for a boost, stop reclaim as 40511c30844dSMel Gorman * IO cannot be queued and it could be an infinite loop in 40521c30844dSMel Gorman * extreme circumstances. 40531c30844dSMel Gorman */ 40541c30844dSMel Gorman if (nr_boost_reclaim && !nr_reclaimed) 40551c30844dSMel Gorman break; 40561c30844dSMel Gorman 4057c73322d0SJohannes Weiner if (raise_priority || !nr_reclaimed) 4058b8e83b94SMel Gorman sc.priority--; 40591d82de61SMel Gorman } while (sc.priority >= 1); 40601da177e4SLinus Torvalds 4061c73322d0SJohannes Weiner if (!sc.nr_reclaimed) 4062c73322d0SJohannes Weiner pgdat->kswapd_failures++; 4063c73322d0SJohannes Weiner 4064b8e83b94SMel Gorman out: 4065c49c2c47SMel Gorman clear_reclaim_active(pgdat, highest_zoneidx); 4066c49c2c47SMel Gorman 40671c30844dSMel Gorman /* If reclaim was boosted, account for the reclaim done in this pass */ 40681c30844dSMel Gorman if (boosted) { 40691c30844dSMel Gorman unsigned long flags; 40701c30844dSMel Gorman 407197a225e6SJoonsoo Kim for (i = 0; i <= highest_zoneidx; i++) { 40721c30844dSMel Gorman if (!zone_boosts[i]) 40731c30844dSMel Gorman continue; 40741c30844dSMel Gorman 40751c30844dSMel Gorman /* Increments are under the zone lock */ 40761c30844dSMel Gorman zone = pgdat->node_zones + i; 40771c30844dSMel Gorman spin_lock_irqsave(&zone->lock, flags); 40781c30844dSMel Gorman zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]); 40791c30844dSMel Gorman spin_unlock_irqrestore(&zone->lock, flags); 40801c30844dSMel Gorman } 40811c30844dSMel Gorman 40821c30844dSMel Gorman /* 40831c30844dSMel Gorman * As there is now likely space, wakeup kcompact to defragment 40841c30844dSMel Gorman * pageblocks. 40851c30844dSMel Gorman */ 408697a225e6SJoonsoo Kim wakeup_kcompactd(pgdat, pageblock_order, highest_zoneidx); 40871c30844dSMel Gorman } 40881c30844dSMel Gorman 40892a2e4885SJohannes Weiner snapshot_refaults(NULL, pgdat); 40904f3eaf45SMatthew Wilcox (Oracle) __fs_reclaim_release(_THIS_IP_); 4091eb414681SJohannes Weiner psi_memstall_leave(&pflags); 40921732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 4093e5ca8071SYafang Shao 40940abdee2bSMel Gorman /* 40951d82de61SMel Gorman * Return the order kswapd stopped reclaiming at as 40961d82de61SMel Gorman * prepare_kswapd_sleep() takes it into account. If another caller 40971d82de61SMel Gorman * entered the allocator slow path while kswapd was awake, order will 40981d82de61SMel Gorman * remain at the higher level. 40990abdee2bSMel Gorman */ 41001d82de61SMel Gorman return sc.order; 41011da177e4SLinus Torvalds } 41021da177e4SLinus Torvalds 4103e716f2ebSMel Gorman /* 410497a225e6SJoonsoo Kim * The pgdat->kswapd_highest_zoneidx is used to pass the highest zone index to 410597a225e6SJoonsoo Kim * be reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is 410697a225e6SJoonsoo Kim * not a valid index then either kswapd runs for first time or kswapd couldn't 410797a225e6SJoonsoo Kim * sleep after previous reclaim attempt (node is still unbalanced). In that 410897a225e6SJoonsoo Kim * case return the zone index of the previous kswapd reclaim cycle. 4109e716f2ebSMel Gorman */ 411097a225e6SJoonsoo Kim static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat, 411197a225e6SJoonsoo Kim enum zone_type prev_highest_zoneidx) 4112e716f2ebSMel Gorman { 411397a225e6SJoonsoo Kim enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); 41145644e1fbSQian Cai 411597a225e6SJoonsoo Kim return curr_idx == MAX_NR_ZONES ? prev_highest_zoneidx : curr_idx; 4116e716f2ebSMel Gorman } 4117e716f2ebSMel Gorman 411838087d9bSMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, 411997a225e6SJoonsoo Kim unsigned int highest_zoneidx) 4120f0bc0a60SKOSAKI Motohiro { 4121f0bc0a60SKOSAKI Motohiro long remaining = 0; 4122f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 4123f0bc0a60SKOSAKI Motohiro 4124f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 4125f0bc0a60SKOSAKI Motohiro return; 4126f0bc0a60SKOSAKI Motohiro 4127f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 4128f0bc0a60SKOSAKI Motohiro 4129333b0a45SShantanu Goel /* 4130333b0a45SShantanu Goel * Try to sleep for a short interval. Note that kcompactd will only be 4131333b0a45SShantanu Goel * woken if it is possible to sleep for a short interval. This is 4132333b0a45SShantanu Goel * deliberate on the assumption that if reclaim cannot keep an 4133333b0a45SShantanu Goel * eligible zone balanced that it's also unlikely that compaction will 4134333b0a45SShantanu Goel * succeed. 4135333b0a45SShantanu Goel */ 413697a225e6SJoonsoo Kim if (prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { 4137fd901c95SVlastimil Babka /* 4138fd901c95SVlastimil Babka * Compaction records what page blocks it recently failed to 4139fd901c95SVlastimil Babka * isolate pages from and skips them in the future scanning. 4140fd901c95SVlastimil Babka * When kswapd is going to sleep, it is reasonable to assume 4141fd901c95SVlastimil Babka * that pages and compaction may succeed so reset the cache. 4142fd901c95SVlastimil Babka */ 4143fd901c95SVlastimil Babka reset_isolation_suitable(pgdat); 4144fd901c95SVlastimil Babka 4145fd901c95SVlastimil Babka /* 4146fd901c95SVlastimil Babka * We have freed the memory, now we should compact it to make 4147fd901c95SVlastimil Babka * allocation of the requested order possible. 4148fd901c95SVlastimil Babka */ 414997a225e6SJoonsoo Kim wakeup_kcompactd(pgdat, alloc_order, highest_zoneidx); 4150fd901c95SVlastimil Babka 4151f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 415238087d9bSMel Gorman 415338087d9bSMel Gorman /* 415497a225e6SJoonsoo Kim * If woken prematurely then reset kswapd_highest_zoneidx and 415538087d9bSMel Gorman * order. The values will either be from a wakeup request or 415638087d9bSMel Gorman * the previous request that slept prematurely. 415738087d9bSMel Gorman */ 415838087d9bSMel Gorman if (remaining) { 415997a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, 416097a225e6SJoonsoo Kim kswapd_highest_zoneidx(pgdat, 416197a225e6SJoonsoo Kim highest_zoneidx)); 41625644e1fbSQian Cai 41635644e1fbSQian Cai if (READ_ONCE(pgdat->kswapd_order) < reclaim_order) 41645644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, reclaim_order); 416538087d9bSMel Gorman } 416638087d9bSMel Gorman 4167f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 4168f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 4169f0bc0a60SKOSAKI Motohiro } 4170f0bc0a60SKOSAKI Motohiro 4171f0bc0a60SKOSAKI Motohiro /* 4172f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 4173f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 4174f0bc0a60SKOSAKI Motohiro */ 4175d9f21d42SMel Gorman if (!remaining && 417697a225e6SJoonsoo Kim prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { 4177f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 4178f0bc0a60SKOSAKI Motohiro 4179f0bc0a60SKOSAKI Motohiro /* 4180f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 4181f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 4182f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 4183f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 4184f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 4185f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 4186f0bc0a60SKOSAKI Motohiro */ 4187f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 41881c7e7f6cSAaditya Kumar 41891c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 4190f0bc0a60SKOSAKI Motohiro schedule(); 41911c7e7f6cSAaditya Kumar 4192f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 4193f0bc0a60SKOSAKI Motohiro } else { 4194f0bc0a60SKOSAKI Motohiro if (remaining) 4195f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 4196f0bc0a60SKOSAKI Motohiro else 4197f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 4198f0bc0a60SKOSAKI Motohiro } 4199f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 4200f0bc0a60SKOSAKI Motohiro } 4201f0bc0a60SKOSAKI Motohiro 42021da177e4SLinus Torvalds /* 42031da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 42041da177e4SLinus Torvalds * from the init process. 42051da177e4SLinus Torvalds * 42061da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 42071da177e4SLinus Torvalds * free memory available even if there is no other activity 42081da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 42091da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 42101da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 42111da177e4SLinus Torvalds * 42121da177e4SLinus Torvalds * If there are applications that are active memory-allocators 42131da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 42141da177e4SLinus Torvalds */ 42151da177e4SLinus Torvalds static int kswapd(void *p) 42161da177e4SLinus Torvalds { 4217e716f2ebSMel Gorman unsigned int alloc_order, reclaim_order; 421897a225e6SJoonsoo Kim unsigned int highest_zoneidx = MAX_NR_ZONES - 1; 42191da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t *)p; 42201da177e4SLinus Torvalds struct task_struct *tsk = current; 4221a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 42221da177e4SLinus Torvalds 4223174596a0SRusty Russell if (!cpumask_empty(cpumask)) 4224c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 42251da177e4SLinus Torvalds 42261da177e4SLinus Torvalds /* 42271da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 42281da177e4SLinus Torvalds * and that if we need more memory we should get access to it 42291da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 42301da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 42311da177e4SLinus Torvalds * 42321da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 42331da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 42341da177e4SLinus Torvalds * page out something else, and this flag essentially protects 42351da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 42361da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 42371da177e4SLinus Torvalds */ 4238930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 423983144186SRafael J. Wysocki set_freezable(); 42401da177e4SLinus Torvalds 42415644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, 0); 424297a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); 42431da177e4SLinus Torvalds for ( ; ; ) { 42446f6313d4SJeff Liu bool ret; 42453e1d1d28SChristoph Lameter 42465644e1fbSQian Cai alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order); 424797a225e6SJoonsoo Kim highest_zoneidx = kswapd_highest_zoneidx(pgdat, 424897a225e6SJoonsoo Kim highest_zoneidx); 4249e716f2ebSMel Gorman 425038087d9bSMel Gorman kswapd_try_sleep: 425138087d9bSMel Gorman kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, 425297a225e6SJoonsoo Kim highest_zoneidx); 4253215ddd66SMel Gorman 425497a225e6SJoonsoo Kim /* Read the new order and highest_zoneidx */ 42552b47a24cSLukas Bulwahn alloc_order = READ_ONCE(pgdat->kswapd_order); 425697a225e6SJoonsoo Kim highest_zoneidx = kswapd_highest_zoneidx(pgdat, 425797a225e6SJoonsoo Kim highest_zoneidx); 42585644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, 0); 425997a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); 42601da177e4SLinus Torvalds 42618fe23e05SDavid Rientjes ret = try_to_freeze(); 42628fe23e05SDavid Rientjes if (kthread_should_stop()) 42638fe23e05SDavid Rientjes break; 42648fe23e05SDavid Rientjes 42658fe23e05SDavid Rientjes /* 42668fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 42678fe23e05SDavid Rientjes * after returning from the refrigerator 4268b1296cc4SRafael J. Wysocki */ 426938087d9bSMel Gorman if (ret) 427038087d9bSMel Gorman continue; 42711d82de61SMel Gorman 427238087d9bSMel Gorman /* 427338087d9bSMel Gorman * Reclaim begins at the requested order but if a high-order 427438087d9bSMel Gorman * reclaim fails then kswapd falls back to reclaiming for 427538087d9bSMel Gorman * order-0. If that happens, kswapd will consider sleeping 427638087d9bSMel Gorman * for the order it finished reclaiming at (reclaim_order) 427738087d9bSMel Gorman * but kcompactd is woken to compact for the original 427838087d9bSMel Gorman * request (alloc_order). 427938087d9bSMel Gorman */ 428097a225e6SJoonsoo Kim trace_mm_vmscan_kswapd_wake(pgdat->node_id, highest_zoneidx, 4281e5146b12SMel Gorman alloc_order); 428297a225e6SJoonsoo Kim reclaim_order = balance_pgdat(pgdat, alloc_order, 428397a225e6SJoonsoo Kim highest_zoneidx); 428438087d9bSMel Gorman if (reclaim_order < alloc_order) 428538087d9bSMel Gorman goto kswapd_try_sleep; 428633906bc5SMel Gorman } 4287b0a8cc58STakamori Yamaguchi 428871abdc15SJohannes Weiner tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); 428971abdc15SJohannes Weiner 42901da177e4SLinus Torvalds return 0; 42911da177e4SLinus Torvalds } 42921da177e4SLinus Torvalds 42931da177e4SLinus Torvalds /* 42945ecd9d40SDavid Rientjes * A zone is low on free memory or too fragmented for high-order memory. If 42955ecd9d40SDavid Rientjes * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's 42965ecd9d40SDavid Rientjes * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim 42975ecd9d40SDavid Rientjes * has failed or is not needed, still wake up kcompactd if only compaction is 42985ecd9d40SDavid Rientjes * needed. 42991da177e4SLinus Torvalds */ 43005ecd9d40SDavid Rientjes void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, 430197a225e6SJoonsoo Kim enum zone_type highest_zoneidx) 43021da177e4SLinus Torvalds { 43031da177e4SLinus Torvalds pg_data_t *pgdat; 43045644e1fbSQian Cai enum zone_type curr_idx; 43051da177e4SLinus Torvalds 43066aa303deSMel Gorman if (!managed_zone(zone)) 43071da177e4SLinus Torvalds return; 43081da177e4SLinus Torvalds 43095ecd9d40SDavid Rientjes if (!cpuset_zone_allowed(zone, gfp_flags)) 43101da177e4SLinus Torvalds return; 4311dffcac2cSShakeel Butt 43125644e1fbSQian Cai pgdat = zone->zone_pgdat; 431397a225e6SJoonsoo Kim curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); 43145644e1fbSQian Cai 431597a225e6SJoonsoo Kim if (curr_idx == MAX_NR_ZONES || curr_idx < highest_zoneidx) 431697a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, highest_zoneidx); 43175644e1fbSQian Cai 43185644e1fbSQian Cai if (READ_ONCE(pgdat->kswapd_order) < order) 43195644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, order); 43205644e1fbSQian Cai 43218d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 43221da177e4SLinus Torvalds return; 4323e1a55637SMel Gorman 43245ecd9d40SDavid Rientjes /* Hopeless node, leave it to direct reclaim if possible */ 43255ecd9d40SDavid Rientjes if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES || 432697a225e6SJoonsoo Kim (pgdat_balanced(pgdat, order, highest_zoneidx) && 432797a225e6SJoonsoo Kim !pgdat_watermark_boosted(pgdat, highest_zoneidx))) { 43285ecd9d40SDavid Rientjes /* 43295ecd9d40SDavid Rientjes * There may be plenty of free memory available, but it's too 43305ecd9d40SDavid Rientjes * fragmented for high-order allocations. Wake up kcompactd 43315ecd9d40SDavid Rientjes * and rely on compaction_suitable() to determine if it's 43325ecd9d40SDavid Rientjes * needed. If it fails, it will defer subsequent attempts to 43335ecd9d40SDavid Rientjes * ratelimit its work. 43345ecd9d40SDavid Rientjes */ 43355ecd9d40SDavid Rientjes if (!(gfp_flags & __GFP_DIRECT_RECLAIM)) 433697a225e6SJoonsoo Kim wakeup_kcompactd(pgdat, order, highest_zoneidx); 4337c73322d0SJohannes Weiner return; 43385ecd9d40SDavid Rientjes } 4339c73322d0SJohannes Weiner 434097a225e6SJoonsoo Kim trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, highest_zoneidx, order, 43415ecd9d40SDavid Rientjes gfp_flags); 43428d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 43431da177e4SLinus Torvalds } 43441da177e4SLinus Torvalds 4345c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 43461da177e4SLinus Torvalds /* 43477b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 4348d6277db4SRafael J. Wysocki * freed pages. 4349d6277db4SRafael J. Wysocki * 4350d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 4351d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 4352d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 43531da177e4SLinus Torvalds */ 43547b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 43551da177e4SLinus Torvalds { 4356d6277db4SRafael J. Wysocki struct scan_control sc = { 43577b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 4358ee814fe2SJohannes Weiner .gfp_mask = GFP_HIGHUSER_MOVABLE, 4359b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 43609e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 4361ee814fe2SJohannes Weiner .may_writepage = 1, 4362ee814fe2SJohannes Weiner .may_unmap = 1, 4363ee814fe2SJohannes Weiner .may_swap = 1, 4364ee814fe2SJohannes Weiner .hibernation_mode = 1, 43651da177e4SLinus Torvalds }; 43667b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 43677b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 4368499118e9SVlastimil Babka unsigned int noreclaim_flag; 43691da177e4SLinus Torvalds 4370d92a8cfcSPeter Zijlstra fs_reclaim_acquire(sc.gfp_mask); 437193781325SOmar Sandoval noreclaim_flag = memalloc_noreclaim_save(); 43721732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 4373d6277db4SRafael J. Wysocki 43743115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 4375d6277db4SRafael J. Wysocki 43761732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 4377499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 437893781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 4379d6277db4SRafael J. Wysocki 43807b51755cSKOSAKI Motohiro return nr_reclaimed; 43811da177e4SLinus Torvalds } 4382c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 43831da177e4SLinus Torvalds 43843218ae14SYasunori Goto /* 43853218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 43863218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 43873218ae14SYasunori Goto */ 43883218ae14SYasunori Goto int kswapd_run(int nid) 43893218ae14SYasunori Goto { 43903218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 43913218ae14SYasunori Goto int ret = 0; 43923218ae14SYasunori Goto 43933218ae14SYasunori Goto if (pgdat->kswapd) 43943218ae14SYasunori Goto return 0; 43953218ae14SYasunori Goto 43963218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 43973218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 43983218ae14SYasunori Goto /* failure at boot is fatal */ 4399c6202adfSThomas Gleixner BUG_ON(system_state < SYSTEM_RUNNING); 4400d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 4401d5dc0ad9SGavin Shan ret = PTR_ERR(pgdat->kswapd); 4402d72515b8SXishi Qiu pgdat->kswapd = NULL; 44033218ae14SYasunori Goto } 44043218ae14SYasunori Goto return ret; 44053218ae14SYasunori Goto } 44063218ae14SYasunori Goto 44078fe23e05SDavid Rientjes /* 4408d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 4409bfc8c901SVladimir Davydov * hold mem_hotplug_begin/end(). 44108fe23e05SDavid Rientjes */ 44118fe23e05SDavid Rientjes void kswapd_stop(int nid) 44128fe23e05SDavid Rientjes { 44138fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 44148fe23e05SDavid Rientjes 4415d8adde17SJiang Liu if (kswapd) { 44168fe23e05SDavid Rientjes kthread_stop(kswapd); 4417d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 4418d8adde17SJiang Liu } 44198fe23e05SDavid Rientjes } 44208fe23e05SDavid Rientjes 44211da177e4SLinus Torvalds static int __init kswapd_init(void) 44221da177e4SLinus Torvalds { 44236b700b5bSWei Yang int nid; 442469e05944SAndrew Morton 44251da177e4SLinus Torvalds swap_setup(); 442648fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 44273218ae14SYasunori Goto kswapd_run(nid); 44281da177e4SLinus Torvalds return 0; 44291da177e4SLinus Torvalds } 44301da177e4SLinus Torvalds 44311da177e4SLinus Torvalds module_init(kswapd_init) 44329eeff239SChristoph Lameter 44339eeff239SChristoph Lameter #ifdef CONFIG_NUMA 44349eeff239SChristoph Lameter /* 4435a5f5f91dSMel Gorman * Node reclaim mode 44369eeff239SChristoph Lameter * 4437a5f5f91dSMel Gorman * If non-zero call node_reclaim when the number of free pages falls below 44389eeff239SChristoph Lameter * the watermarks. 44399eeff239SChristoph Lameter */ 4440a5f5f91dSMel Gorman int node_reclaim_mode __read_mostly; 44419eeff239SChristoph Lameter 444251998364SDave Hansen /* 4443a5f5f91dSMel Gorman * Priority for NODE_RECLAIM. This determines the fraction of pages 4444a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 4445a92f7126SChristoph Lameter * a zone. 4446a92f7126SChristoph Lameter */ 4447a5f5f91dSMel Gorman #define NODE_RECLAIM_PRIORITY 4 4448a92f7126SChristoph Lameter 44499eeff239SChristoph Lameter /* 4450a5f5f91dSMel Gorman * Percentage of pages in a zone that must be unmapped for node_reclaim to 44519614634fSChristoph Lameter * occur. 44529614634fSChristoph Lameter */ 44539614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 44549614634fSChristoph Lameter 44559614634fSChristoph Lameter /* 44560ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 44570ff38490SChristoph Lameter * slab reclaim needs to occur. 44580ff38490SChristoph Lameter */ 44590ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 44600ff38490SChristoph Lameter 446111fb9989SMel Gorman static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) 446290afa5deSMel Gorman { 446311fb9989SMel Gorman unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); 446411fb9989SMel Gorman unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + 446511fb9989SMel Gorman node_page_state(pgdat, NR_ACTIVE_FILE); 446690afa5deSMel Gorman 446790afa5deSMel Gorman /* 446890afa5deSMel Gorman * It's possible for there to be more file mapped pages than 446990afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 447090afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 447190afa5deSMel Gorman */ 447290afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 447390afa5deSMel Gorman } 447490afa5deSMel Gorman 447590afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 4476a5f5f91dSMel Gorman static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) 447790afa5deSMel Gorman { 4478d031a157SAlexandru Moise unsigned long nr_pagecache_reclaimable; 4479d031a157SAlexandru Moise unsigned long delta = 0; 448090afa5deSMel Gorman 448190afa5deSMel Gorman /* 448295bbc0c7SZhihui Zhang * If RECLAIM_UNMAP is set, then all file pages are considered 448390afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 448411fb9989SMel Gorman * pages like swapcache and node_unmapped_file_pages() provides 448590afa5deSMel Gorman * a better estimate 448690afa5deSMel Gorman */ 4487a5f5f91dSMel Gorman if (node_reclaim_mode & RECLAIM_UNMAP) 4488a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); 448990afa5deSMel Gorman else 4490a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); 449190afa5deSMel Gorman 449290afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 4493a5f5f91dSMel Gorman if (!(node_reclaim_mode & RECLAIM_WRITE)) 4494a5f5f91dSMel Gorman delta += node_page_state(pgdat, NR_FILE_DIRTY); 449590afa5deSMel Gorman 449690afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 449790afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 449890afa5deSMel Gorman delta = nr_pagecache_reclaimable; 449990afa5deSMel Gorman 450090afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 450190afa5deSMel Gorman } 450290afa5deSMel Gorman 45030ff38490SChristoph Lameter /* 4504a5f5f91dSMel Gorman * Try to free up some pages from this node through reclaim. 45059eeff239SChristoph Lameter */ 4506a5f5f91dSMel Gorman static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 45079eeff239SChristoph Lameter { 45087fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 450969e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 45109eeff239SChristoph Lameter struct task_struct *p = current; 4511499118e9SVlastimil Babka unsigned int noreclaim_flag; 4512179e9639SAndrew Morton struct scan_control sc = { 451362b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 4514f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 4515bd2f6199SJohannes Weiner .order = order, 4516a5f5f91dSMel Gorman .priority = NODE_RECLAIM_PRIORITY, 4517a5f5f91dSMel Gorman .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), 4518a5f5f91dSMel Gorman .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), 4519ee814fe2SJohannes Weiner .may_swap = 1, 4520f2f43e56SNick Desaulniers .reclaim_idx = gfp_zone(gfp_mask), 4521179e9639SAndrew Morton }; 452257f29762SJohannes Weiner unsigned long pflags; 45239eeff239SChristoph Lameter 4524132bb8cfSYafang Shao trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order, 4525132bb8cfSYafang Shao sc.gfp_mask); 4526132bb8cfSYafang Shao 45279eeff239SChristoph Lameter cond_resched(); 452857f29762SJohannes Weiner psi_memstall_enter(&pflags); 452993781325SOmar Sandoval fs_reclaim_acquire(sc.gfp_mask); 4530d4f7796eSChristoph Lameter /* 453195bbc0c7SZhihui Zhang * We need to be able to allocate from the reserves for RECLAIM_UNMAP 4532d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 453395bbc0c7SZhihui Zhang * and RECLAIM_UNMAP. 4534d4f7796eSChristoph Lameter */ 4535499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 4536499118e9SVlastimil Babka p->flags |= PF_SWAPWRITE; 45371732d2b0SAndrew Morton set_task_reclaim_state(p, &sc.reclaim_state); 4538c84db23cSChristoph Lameter 4539a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) { 4540a92f7126SChristoph Lameter /* 4541894befecSAndrey Ryabinin * Free memory by calling shrink node with increasing 45420ff38490SChristoph Lameter * priorities until we have enough memory freed. 4543a92f7126SChristoph Lameter */ 4544a92f7126SChristoph Lameter do { 4545970a39a3SMel Gorman shrink_node(pgdat, &sc); 45469e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 45470ff38490SChristoph Lameter } 4548a92f7126SChristoph Lameter 45491732d2b0SAndrew Morton set_task_reclaim_state(p, NULL); 4550499118e9SVlastimil Babka current->flags &= ~PF_SWAPWRITE; 4551499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 455293781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 455357f29762SJohannes Weiner psi_memstall_leave(&pflags); 4554132bb8cfSYafang Shao 4555132bb8cfSYafang Shao trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed); 4556132bb8cfSYafang Shao 4557a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 45589eeff239SChristoph Lameter } 4559179e9639SAndrew Morton 4560a5f5f91dSMel Gorman int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 4561179e9639SAndrew Morton { 4562d773ed6bSDavid Rientjes int ret; 4563179e9639SAndrew Morton 4564179e9639SAndrew Morton /* 4565a5f5f91dSMel Gorman * Node reclaim reclaims unmapped file backed pages and 45660ff38490SChristoph Lameter * slab pages if we are over the defined limits. 456734aa1330SChristoph Lameter * 45689614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 45699614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 4570a5f5f91dSMel Gorman * thrown out if the node is overallocated. So we do not reclaim 4571a5f5f91dSMel Gorman * if less than a specified percentage of the node is used by 45729614634fSChristoph Lameter * unmapped file backed pages. 4573179e9639SAndrew Morton */ 4574a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && 4575d42f3245SRoman Gushchin node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) <= 4576d42f3245SRoman Gushchin pgdat->min_slab_pages) 4577a5f5f91dSMel Gorman return NODE_RECLAIM_FULL; 4578179e9639SAndrew Morton 4579179e9639SAndrew Morton /* 4580d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 4581179e9639SAndrew Morton */ 4582d0164adcSMel Gorman if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) 4583a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4584179e9639SAndrew Morton 4585179e9639SAndrew Morton /* 4586a5f5f91dSMel Gorman * Only run node reclaim on the local node or on nodes that do not 4587179e9639SAndrew Morton * have associated processors. This will favor the local processor 4588179e9639SAndrew Morton * over remote processors and spread off node memory allocations 4589179e9639SAndrew Morton * as wide as possible. 4590179e9639SAndrew Morton */ 4591a5f5f91dSMel Gorman if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) 4592a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4593d773ed6bSDavid Rientjes 4594a5f5f91dSMel Gorman if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) 4595a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4596fa5e084eSMel Gorman 4597a5f5f91dSMel Gorman ret = __node_reclaim(pgdat, gfp_mask, order); 4598a5f5f91dSMel Gorman clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); 4599d773ed6bSDavid Rientjes 460024cf7251SMel Gorman if (!ret) 460124cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 460224cf7251SMel Gorman 4603d773ed6bSDavid Rientjes return ret; 4604179e9639SAndrew Morton } 46059eeff239SChristoph Lameter #endif 4606894bc310SLee Schermerhorn 460789e004eaSLee Schermerhorn /** 460864e3d12fSKuo-Hsin Yang * check_move_unevictable_pages - check pages for evictability and move to 460964e3d12fSKuo-Hsin Yang * appropriate zone lru list 461064e3d12fSKuo-Hsin Yang * @pvec: pagevec with lru pages to check 461189e004eaSLee Schermerhorn * 461264e3d12fSKuo-Hsin Yang * Checks pages for evictability, if an evictable page is in the unevictable 461364e3d12fSKuo-Hsin Yang * lru list, moves it to the appropriate evictable lru list. This function 461464e3d12fSKuo-Hsin Yang * should be only used for lru pages. 461589e004eaSLee Schermerhorn */ 461664e3d12fSKuo-Hsin Yang void check_move_unevictable_pages(struct pagevec *pvec) 461789e004eaSLee Schermerhorn { 46186168d0daSAlex Shi struct lruvec *lruvec = NULL; 461924513264SHugh Dickins int pgscanned = 0; 462024513264SHugh Dickins int pgrescued = 0; 462189e004eaSLee Schermerhorn int i; 462289e004eaSLee Schermerhorn 462364e3d12fSKuo-Hsin Yang for (i = 0; i < pvec->nr; i++) { 462464e3d12fSKuo-Hsin Yang struct page *page = pvec->pages[i]; 46258d8869caSHugh Dickins int nr_pages; 462689e004eaSLee Schermerhorn 46278d8869caSHugh Dickins if (PageTransTail(page)) 46288d8869caSHugh Dickins continue; 46298d8869caSHugh Dickins 46308d8869caSHugh Dickins nr_pages = thp_nr_pages(page); 46318d8869caSHugh Dickins pgscanned += nr_pages; 46328d8869caSHugh Dickins 4633d25b5bd8SAlex Shi /* block memcg migration during page moving between lru */ 4634d25b5bd8SAlex Shi if (!TestClearPageLRU(page)) 4635d25b5bd8SAlex Shi continue; 4636d25b5bd8SAlex Shi 46372a5e4e34SAlexander Duyck lruvec = relock_page_lruvec_irq(page, lruvec); 4638d25b5bd8SAlex Shi if (page_evictable(page) && PageUnevictable(page)) { 463946ae6b2cSYu Zhao del_page_from_lru_list(page, lruvec); 464024513264SHugh Dickins ClearPageUnevictable(page); 46413a9c9788SYu Zhao add_page_to_lru_list(page, lruvec); 46428d8869caSHugh Dickins pgrescued += nr_pages; 464389e004eaSLee Schermerhorn } 4644d25b5bd8SAlex Shi SetPageLRU(page); 464589e004eaSLee Schermerhorn } 464624513264SHugh Dickins 46476168d0daSAlex Shi if (lruvec) { 464824513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 464924513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 46506168d0daSAlex Shi unlock_page_lruvec_irq(lruvec); 4651d25b5bd8SAlex Shi } else if (pgscanned) { 4652d25b5bd8SAlex Shi count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 465324513264SHugh Dickins } 465485046579SHugh Dickins } 465564e3d12fSKuo-Hsin Yang EXPORT_SYMBOL_GPL(check_move_unevictable_pages); 4656