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 { 52720b51af1SHuang Ying if (!numa_demotion_enabled) 52820b51af1SHuang Ying return false; 5293a235693SDave Hansen if (sc) { 5303a235693SDave Hansen if (sc->no_demotion) 53126aa2d19SDave Hansen return false; 5323a235693SDave Hansen /* It is pointless to do demotion in memcg reclaim */ 5333a235693SDave Hansen if (cgroup_reclaim(sc)) 5343a235693SDave Hansen return false; 5353a235693SDave Hansen } 53626aa2d19SDave Hansen if (next_demotion_node(nid) == NUMA_NO_NODE) 53726aa2d19SDave Hansen return false; 53826aa2d19SDave Hansen 53920b51af1SHuang Ying return true; 54026aa2d19SDave Hansen } 54126aa2d19SDave Hansen 542a2a36488SKeith Busch static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg, 543a2a36488SKeith Busch int nid, 544a2a36488SKeith Busch struct scan_control *sc) 545a2a36488SKeith Busch { 546a2a36488SKeith Busch if (memcg == NULL) { 547a2a36488SKeith Busch /* 548a2a36488SKeith Busch * For non-memcg reclaim, is there 549a2a36488SKeith Busch * space in any swap device? 550a2a36488SKeith Busch */ 551a2a36488SKeith Busch if (get_nr_swap_pages() > 0) 552a2a36488SKeith Busch return true; 553a2a36488SKeith Busch } else { 554a2a36488SKeith Busch /* Is the memcg below its swap limit? */ 555a2a36488SKeith Busch if (mem_cgroup_get_nr_swap_pages(memcg) > 0) 556a2a36488SKeith Busch return true; 557a2a36488SKeith Busch } 558a2a36488SKeith Busch 559a2a36488SKeith Busch /* 560a2a36488SKeith Busch * The page can not be swapped. 561a2a36488SKeith Busch * 562a2a36488SKeith Busch * Can it be reclaimed from this node via demotion? 563a2a36488SKeith Busch */ 564a2a36488SKeith Busch return can_demote(nid, sc); 565a2a36488SKeith Busch } 566a2a36488SKeith Busch 5675a1c84b4SMel Gorman /* 5685a1c84b4SMel Gorman * This misses isolated pages which are not accounted for to save counters. 5695a1c84b4SMel Gorman * As the data only determines if reclaim or compaction continues, it is 5705a1c84b4SMel Gorman * not expected that isolated pages will be a dominating factor. 5715a1c84b4SMel Gorman */ 5725a1c84b4SMel Gorman unsigned long zone_reclaimable_pages(struct zone *zone) 5735a1c84b4SMel Gorman { 5745a1c84b4SMel Gorman unsigned long nr; 5755a1c84b4SMel Gorman 5765a1c84b4SMel Gorman nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) + 5775a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE); 578a2a36488SKeith Busch if (can_reclaim_anon_pages(NULL, zone_to_nid(zone), NULL)) 5795a1c84b4SMel Gorman nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) + 5805a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON); 5815a1c84b4SMel Gorman 5825a1c84b4SMel Gorman return nr; 5835a1c84b4SMel Gorman } 5845a1c84b4SMel Gorman 585fd538803SMichal Hocko /** 586fd538803SMichal Hocko * lruvec_lru_size - Returns the number of pages on the given LRU list. 587fd538803SMichal Hocko * @lruvec: lru vector 588fd538803SMichal Hocko * @lru: lru to use 589fd538803SMichal Hocko * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list) 590fd538803SMichal Hocko */ 5912091339dSYu Zhao static unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, 5922091339dSYu Zhao int zone_idx) 593c9f299d9SKOSAKI Motohiro { 594de3b0150SJohannes Weiner unsigned long size = 0; 595fd538803SMichal Hocko int zid; 596a3d8e054SKOSAKI Motohiro 597de3b0150SJohannes Weiner for (zid = 0; zid <= zone_idx && zid < MAX_NR_ZONES; zid++) { 598fd538803SMichal Hocko struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; 599fd538803SMichal Hocko 600fd538803SMichal Hocko if (!managed_zone(zone)) 601fd538803SMichal Hocko continue; 602fd538803SMichal Hocko 603fd538803SMichal Hocko if (!mem_cgroup_disabled()) 604de3b0150SJohannes Weiner size += mem_cgroup_get_zone_lru_size(lruvec, lru, zid); 605fd538803SMichal Hocko else 606de3b0150SJohannes Weiner size += zone_page_state(zone, NR_ZONE_LRU_BASE + lru); 607c9f299d9SKOSAKI Motohiro } 608de3b0150SJohannes Weiner return size; 609b4536f0cSMichal Hocko } 610b4536f0cSMichal Hocko 6111da177e4SLinus Torvalds /* 6121d3d4437SGlauber Costa * Add a shrinker callback to be called from the vm. 6131da177e4SLinus Torvalds */ 6148e04944fSTetsuo Handa int prealloc_shrinker(struct shrinker *shrinker) 6151da177e4SLinus Torvalds { 616476b30a0SYang Shi unsigned int size; 617476b30a0SYang Shi int err; 6181d3d4437SGlauber Costa 619476b30a0SYang Shi if (shrinker->flags & SHRINKER_MEMCG_AWARE) { 620476b30a0SYang Shi err = prealloc_memcg_shrinker(shrinker); 621476b30a0SYang Shi if (err != -ENOSYS) 622476b30a0SYang Shi return err; 623476b30a0SYang Shi 624476b30a0SYang Shi shrinker->flags &= ~SHRINKER_MEMCG_AWARE; 625476b30a0SYang Shi } 626476b30a0SYang Shi 627476b30a0SYang Shi size = sizeof(*shrinker->nr_deferred); 6281d3d4437SGlauber Costa if (shrinker->flags & SHRINKER_NUMA_AWARE) 6291d3d4437SGlauber Costa size *= nr_node_ids; 6301d3d4437SGlauber Costa 6311d3d4437SGlauber Costa shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); 6321d3d4437SGlauber Costa if (!shrinker->nr_deferred) 6331d3d4437SGlauber Costa return -ENOMEM; 634b4c2b231SKirill Tkhai 6358e04944fSTetsuo Handa return 0; 6368e04944fSTetsuo Handa } 6371d3d4437SGlauber Costa 6388e04944fSTetsuo Handa void free_prealloced_shrinker(struct shrinker *shrinker) 6398e04944fSTetsuo Handa { 64041ca668aSYang Shi if (shrinker->flags & SHRINKER_MEMCG_AWARE) { 64141ca668aSYang Shi down_write(&shrinker_rwsem); 642b4c2b231SKirill Tkhai unregister_memcg_shrinker(shrinker); 64341ca668aSYang Shi up_write(&shrinker_rwsem); 644476b30a0SYang Shi return; 64541ca668aSYang Shi } 646b4c2b231SKirill Tkhai 6478e04944fSTetsuo Handa kfree(shrinker->nr_deferred); 6488e04944fSTetsuo Handa shrinker->nr_deferred = NULL; 6498e04944fSTetsuo Handa } 6508e04944fSTetsuo Handa 6518e04944fSTetsuo Handa void register_shrinker_prepared(struct shrinker *shrinker) 6528e04944fSTetsuo Handa { 6531da177e4SLinus Torvalds down_write(&shrinker_rwsem); 6541da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 65541ca668aSYang Shi shrinker->flags |= SHRINKER_REGISTERED; 6561da177e4SLinus Torvalds up_write(&shrinker_rwsem); 6578e04944fSTetsuo Handa } 6588e04944fSTetsuo Handa 6598e04944fSTetsuo Handa int register_shrinker(struct shrinker *shrinker) 6608e04944fSTetsuo Handa { 6618e04944fSTetsuo Handa int err = prealloc_shrinker(shrinker); 6628e04944fSTetsuo Handa 6638e04944fSTetsuo Handa if (err) 6648e04944fSTetsuo Handa return err; 6658e04944fSTetsuo Handa register_shrinker_prepared(shrinker); 6661d3d4437SGlauber Costa return 0; 6671da177e4SLinus Torvalds } 6688e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds /* 6711da177e4SLinus Torvalds * Remove one 6721da177e4SLinus Torvalds */ 6738e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 6741da177e4SLinus Torvalds { 67541ca668aSYang Shi if (!(shrinker->flags & SHRINKER_REGISTERED)) 676bb422a73STetsuo Handa return; 67741ca668aSYang Shi 6781da177e4SLinus Torvalds down_write(&shrinker_rwsem); 6791da177e4SLinus Torvalds list_del(&shrinker->list); 68041ca668aSYang Shi shrinker->flags &= ~SHRINKER_REGISTERED; 68141ca668aSYang Shi if (shrinker->flags & SHRINKER_MEMCG_AWARE) 68241ca668aSYang Shi unregister_memcg_shrinker(shrinker); 6831da177e4SLinus Torvalds up_write(&shrinker_rwsem); 68441ca668aSYang Shi 685ae393321SAndrew Vagin kfree(shrinker->nr_deferred); 686bb422a73STetsuo Handa shrinker->nr_deferred = NULL; 6871da177e4SLinus Torvalds } 6888e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 6891da177e4SLinus Torvalds 690880121beSChristian König /** 691880121beSChristian König * synchronize_shrinkers - Wait for all running shrinkers to complete. 692880121beSChristian König * 693880121beSChristian König * This is equivalent to calling unregister_shrink() and register_shrinker(), 694880121beSChristian König * but atomically and with less overhead. This is useful to guarantee that all 695880121beSChristian König * shrinker invocations have seen an update, before freeing memory, similar to 696880121beSChristian König * rcu. 697880121beSChristian König */ 698880121beSChristian König void synchronize_shrinkers(void) 699880121beSChristian König { 700880121beSChristian König down_write(&shrinker_rwsem); 701880121beSChristian König up_write(&shrinker_rwsem); 702880121beSChristian König } 703880121beSChristian König EXPORT_SYMBOL(synchronize_shrinkers); 704880121beSChristian König 7051da177e4SLinus Torvalds #define SHRINK_BATCH 128 7061d3d4437SGlauber Costa 707cb731d6cSVladimir Davydov static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, 7089092c71bSJosef Bacik struct shrinker *shrinker, int priority) 7091da177e4SLinus Torvalds { 71024f7c6b9SDave Chinner unsigned long freed = 0; 7111da177e4SLinus Torvalds unsigned long long delta; 712635697c6SKonstantin Khlebnikov long total_scan; 713d5bc5fd3SVladimir Davydov long freeable; 714acf92b48SDave Chinner long nr; 715acf92b48SDave Chinner long new_nr; 716e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 717e9299f50SDave Chinner : SHRINK_BATCH; 7185f33a080SShaohua Li long scanned = 0, next_deferred; 7191da177e4SLinus Torvalds 720d5bc5fd3SVladimir Davydov freeable = shrinker->count_objects(shrinker, shrinkctl); 7219b996468SKirill Tkhai if (freeable == 0 || freeable == SHRINK_EMPTY) 7229b996468SKirill Tkhai return freeable; 723635697c6SKonstantin Khlebnikov 724acf92b48SDave Chinner /* 725acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 726acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 727acf92b48SDave Chinner * don't also do this scanning work. 728acf92b48SDave Chinner */ 72986750830SYang Shi nr = xchg_nr_deferred(shrinker, shrinkctl); 730acf92b48SDave Chinner 7314b85afbdSJohannes Weiner if (shrinker->seeks) { 7329092c71bSJosef Bacik delta = freeable >> priority; 7339092c71bSJosef Bacik delta *= 4; 7349092c71bSJosef Bacik do_div(delta, shrinker->seeks); 7354b85afbdSJohannes Weiner } else { 7364b85afbdSJohannes Weiner /* 7374b85afbdSJohannes Weiner * These objects don't require any IO to create. Trim 7384b85afbdSJohannes Weiner * them aggressively under memory pressure to keep 7394b85afbdSJohannes Weiner * them from causing refetches in the IO caches. 7404b85afbdSJohannes Weiner */ 7414b85afbdSJohannes Weiner delta = freeable / 2; 7424b85afbdSJohannes Weiner } 743172b06c3SRoman Gushchin 74418bb473eSYang Shi total_scan = nr >> priority; 745acf92b48SDave Chinner total_scan += delta; 74618bb473eSYang Shi total_scan = min(total_scan, (2 * freeable)); 7471da177e4SLinus Torvalds 74824f7c6b9SDave Chinner trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, 7499092c71bSJosef Bacik freeable, delta, total_scan, priority); 75009576073SDave Chinner 7510b1fb40aSVladimir Davydov /* 7520b1fb40aSVladimir Davydov * Normally, we should not scan less than batch_size objects in one 7530b1fb40aSVladimir Davydov * pass to avoid too frequent shrinker calls, but if the slab has less 7540b1fb40aSVladimir Davydov * than batch_size objects in total and we are really tight on memory, 7550b1fb40aSVladimir Davydov * we will try to reclaim all available objects, otherwise we can end 7560b1fb40aSVladimir Davydov * up failing allocations although there are plenty of reclaimable 7570b1fb40aSVladimir Davydov * objects spread over several slabs with usage less than the 7580b1fb40aSVladimir Davydov * batch_size. 7590b1fb40aSVladimir Davydov * 7600b1fb40aSVladimir Davydov * We detect the "tight on memory" situations by looking at the total 7610b1fb40aSVladimir Davydov * number of objects we want to scan (total_scan). If it is greater 762d5bc5fd3SVladimir Davydov * than the total number of objects on slab (freeable), we must be 7630b1fb40aSVladimir Davydov * scanning at high prio and therefore should try to reclaim as much as 7640b1fb40aSVladimir Davydov * possible. 7650b1fb40aSVladimir Davydov */ 7660b1fb40aSVladimir Davydov while (total_scan >= batch_size || 767d5bc5fd3SVladimir Davydov total_scan >= freeable) { 76824f7c6b9SDave Chinner unsigned long ret; 7690b1fb40aSVladimir Davydov unsigned long nr_to_scan = min(batch_size, total_scan); 7701da177e4SLinus Torvalds 7710b1fb40aSVladimir Davydov shrinkctl->nr_to_scan = nr_to_scan; 772d460acb5SChris Wilson shrinkctl->nr_scanned = nr_to_scan; 77324f7c6b9SDave Chinner ret = shrinker->scan_objects(shrinker, shrinkctl); 77424f7c6b9SDave Chinner if (ret == SHRINK_STOP) 7751da177e4SLinus Torvalds break; 77624f7c6b9SDave Chinner freed += ret; 77724f7c6b9SDave Chinner 778d460acb5SChris Wilson count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned); 779d460acb5SChris Wilson total_scan -= shrinkctl->nr_scanned; 780d460acb5SChris Wilson scanned += shrinkctl->nr_scanned; 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds cond_resched(); 7831da177e4SLinus Torvalds } 7841da177e4SLinus Torvalds 78518bb473eSYang Shi /* 78618bb473eSYang Shi * The deferred work is increased by any new work (delta) that wasn't 78718bb473eSYang Shi * done, decreased by old deferred work that was done now. 78818bb473eSYang Shi * 78918bb473eSYang Shi * And it is capped to two times of the freeable items. 79018bb473eSYang Shi */ 79118bb473eSYang Shi next_deferred = max_t(long, (nr + delta - scanned), 0); 79218bb473eSYang Shi next_deferred = min(next_deferred, (2 * freeable)); 79318bb473eSYang Shi 794acf92b48SDave Chinner /* 795acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 79686750830SYang Shi * manner that handles concurrent updates. 797acf92b48SDave Chinner */ 79886750830SYang Shi new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl); 799acf92b48SDave Chinner 8008efb4b59SYang Shi trace_mm_shrink_slab_end(shrinker, shrinkctl->nid, freed, nr, new_nr, total_scan); 8011d3d4437SGlauber Costa return freed; 8021d3d4437SGlauber Costa } 8031d3d4437SGlauber Costa 8040a432dcbSYang Shi #ifdef CONFIG_MEMCG 805b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 806b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 807b0dedc49SKirill Tkhai { 808e4262c4fSYang Shi struct shrinker_info *info; 809b8e57efaSKirill Tkhai unsigned long ret, freed = 0; 810b8e57efaSKirill Tkhai int i; 811b0dedc49SKirill Tkhai 8120a432dcbSYang Shi if (!mem_cgroup_online(memcg)) 813b0dedc49SKirill Tkhai return 0; 814b0dedc49SKirill Tkhai 815b0dedc49SKirill Tkhai if (!down_read_trylock(&shrinker_rwsem)) 816b0dedc49SKirill Tkhai return 0; 817b0dedc49SKirill Tkhai 818468ab843SYang Shi info = shrinker_info_protected(memcg, nid); 819e4262c4fSYang Shi if (unlikely(!info)) 820b0dedc49SKirill Tkhai goto unlock; 821b0dedc49SKirill Tkhai 822e4262c4fSYang Shi for_each_set_bit(i, info->map, shrinker_nr_max) { 823b0dedc49SKirill Tkhai struct shrink_control sc = { 824b0dedc49SKirill Tkhai .gfp_mask = gfp_mask, 825b0dedc49SKirill Tkhai .nid = nid, 826b0dedc49SKirill Tkhai .memcg = memcg, 827b0dedc49SKirill Tkhai }; 828b0dedc49SKirill Tkhai struct shrinker *shrinker; 829b0dedc49SKirill Tkhai 830b0dedc49SKirill Tkhai shrinker = idr_find(&shrinker_idr, i); 83141ca668aSYang Shi if (unlikely(!shrinker || !(shrinker->flags & SHRINKER_REGISTERED))) { 8327e010df5SKirill Tkhai if (!shrinker) 833e4262c4fSYang Shi clear_bit(i, info->map); 834b0dedc49SKirill Tkhai continue; 835b0dedc49SKirill Tkhai } 836b0dedc49SKirill Tkhai 8370a432dcbSYang Shi /* Call non-slab shrinkers even though kmem is disabled */ 8380a432dcbSYang Shi if (!memcg_kmem_enabled() && 8390a432dcbSYang Shi !(shrinker->flags & SHRINKER_NONSLAB)) 8400a432dcbSYang Shi continue; 8410a432dcbSYang Shi 842b0dedc49SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 843f90280d6SKirill Tkhai if (ret == SHRINK_EMPTY) { 844e4262c4fSYang Shi clear_bit(i, info->map); 845f90280d6SKirill Tkhai /* 846f90280d6SKirill Tkhai * After the shrinker reported that it had no objects to 847f90280d6SKirill Tkhai * free, but before we cleared the corresponding bit in 848f90280d6SKirill Tkhai * the memcg shrinker map, a new object might have been 849f90280d6SKirill Tkhai * added. To make sure, we have the bit set in this 850f90280d6SKirill Tkhai * case, we invoke the shrinker one more time and reset 851f90280d6SKirill Tkhai * the bit if it reports that it is not empty anymore. 852f90280d6SKirill Tkhai * The memory barrier here pairs with the barrier in 8532bfd3637SYang Shi * set_shrinker_bit(): 854f90280d6SKirill Tkhai * 855f90280d6SKirill Tkhai * list_lru_add() shrink_slab_memcg() 856f90280d6SKirill Tkhai * list_add_tail() clear_bit() 857f90280d6SKirill Tkhai * <MB> <MB> 858f90280d6SKirill Tkhai * set_bit() do_shrink_slab() 859f90280d6SKirill Tkhai */ 860f90280d6SKirill Tkhai smp_mb__after_atomic(); 861f90280d6SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 8629b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 8639b996468SKirill Tkhai ret = 0; 864f90280d6SKirill Tkhai else 8652bfd3637SYang Shi set_shrinker_bit(memcg, nid, i); 866f90280d6SKirill Tkhai } 867b0dedc49SKirill Tkhai freed += ret; 868b0dedc49SKirill Tkhai 869b0dedc49SKirill Tkhai if (rwsem_is_contended(&shrinker_rwsem)) { 870b0dedc49SKirill Tkhai freed = freed ? : 1; 871b0dedc49SKirill Tkhai break; 872b0dedc49SKirill Tkhai } 873b0dedc49SKirill Tkhai } 874b0dedc49SKirill Tkhai unlock: 875b0dedc49SKirill Tkhai up_read(&shrinker_rwsem); 876b0dedc49SKirill Tkhai return freed; 877b0dedc49SKirill Tkhai } 8780a432dcbSYang Shi #else /* CONFIG_MEMCG */ 879b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 880b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 881b0dedc49SKirill Tkhai { 882b0dedc49SKirill Tkhai return 0; 883b0dedc49SKirill Tkhai } 8840a432dcbSYang Shi #endif /* CONFIG_MEMCG */ 885b0dedc49SKirill Tkhai 8866b4f7799SJohannes Weiner /** 887cb731d6cSVladimir Davydov * shrink_slab - shrink slab caches 8886b4f7799SJohannes Weiner * @gfp_mask: allocation context 8896b4f7799SJohannes Weiner * @nid: node whose slab caches to target 890cb731d6cSVladimir Davydov * @memcg: memory cgroup whose slab caches to target 8919092c71bSJosef Bacik * @priority: the reclaim priority 8921d3d4437SGlauber Costa * 8936b4f7799SJohannes Weiner * Call the shrink functions to age shrinkable caches. 8941d3d4437SGlauber Costa * 8956b4f7799SJohannes Weiner * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, 8966b4f7799SJohannes Weiner * unaware shrinkers will receive a node id of 0 instead. 8971d3d4437SGlauber Costa * 898aeed1d32SVladimir Davydov * @memcg specifies the memory cgroup to target. Unaware shrinkers 899aeed1d32SVladimir Davydov * are called only if it is the root cgroup. 900cb731d6cSVladimir Davydov * 9019092c71bSJosef Bacik * @priority is sc->priority, we take the number of objects and >> by priority 9029092c71bSJosef Bacik * in order to get the scan target. 9031d3d4437SGlauber Costa * 9046b4f7799SJohannes Weiner * Returns the number of reclaimed slab objects. 9051d3d4437SGlauber Costa */ 906cb731d6cSVladimir Davydov static unsigned long shrink_slab(gfp_t gfp_mask, int nid, 907cb731d6cSVladimir Davydov struct mem_cgroup *memcg, 9089092c71bSJosef Bacik int priority) 9091d3d4437SGlauber Costa { 910b8e57efaSKirill Tkhai unsigned long ret, freed = 0; 9111d3d4437SGlauber Costa struct shrinker *shrinker; 9121d3d4437SGlauber Costa 913fa1e512fSYang Shi /* 914fa1e512fSYang Shi * The root memcg might be allocated even though memcg is disabled 915fa1e512fSYang Shi * via "cgroup_disable=memory" boot parameter. This could make 916fa1e512fSYang Shi * mem_cgroup_is_root() return false, then just run memcg slab 917fa1e512fSYang Shi * shrink, but skip global shrink. This may result in premature 918fa1e512fSYang Shi * oom. 919fa1e512fSYang Shi */ 920fa1e512fSYang Shi if (!mem_cgroup_disabled() && !mem_cgroup_is_root(memcg)) 921b0dedc49SKirill Tkhai return shrink_slab_memcg(gfp_mask, nid, memcg, priority); 922cb731d6cSVladimir Davydov 923e830c63aSTetsuo Handa if (!down_read_trylock(&shrinker_rwsem)) 9241d3d4437SGlauber Costa goto out; 9251d3d4437SGlauber Costa 9261d3d4437SGlauber Costa list_for_each_entry(shrinker, &shrinker_list, list) { 9276b4f7799SJohannes Weiner struct shrink_control sc = { 9286b4f7799SJohannes Weiner .gfp_mask = gfp_mask, 9296b4f7799SJohannes Weiner .nid = nid, 930cb731d6cSVladimir Davydov .memcg = memcg, 9316b4f7799SJohannes Weiner }; 9326b4f7799SJohannes Weiner 9339b996468SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 9349b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 9359b996468SKirill Tkhai ret = 0; 9369b996468SKirill Tkhai freed += ret; 937e496612cSMinchan Kim /* 938e496612cSMinchan Kim * Bail out if someone want to register a new shrinker to 93955b65a57SEthon Paul * prevent the registration from being stalled for long periods 940e496612cSMinchan Kim * by parallel ongoing shrinking. 941e496612cSMinchan Kim */ 942e496612cSMinchan Kim if (rwsem_is_contended(&shrinker_rwsem)) { 943e496612cSMinchan Kim freed = freed ? : 1; 944e496612cSMinchan Kim break; 945e496612cSMinchan Kim } 946ec97097bSVladimir Davydov } 9471d3d4437SGlauber Costa 9481da177e4SLinus Torvalds up_read(&shrinker_rwsem); 949f06590bdSMinchan Kim out: 950f06590bdSMinchan Kim cond_resched(); 95124f7c6b9SDave Chinner return freed; 9521da177e4SLinus Torvalds } 9531da177e4SLinus Torvalds 954e4b424b7SGang Li static void drop_slab_node(int nid) 955cb731d6cSVladimir Davydov { 956cb731d6cSVladimir Davydov unsigned long freed; 9571399af7eSVlastimil Babka int shift = 0; 958cb731d6cSVladimir Davydov 959cb731d6cSVladimir Davydov do { 960cb731d6cSVladimir Davydov struct mem_cgroup *memcg = NULL; 961cb731d6cSVladimir Davydov 962069c411dSChunxin Zang if (fatal_signal_pending(current)) 963069c411dSChunxin Zang return; 964069c411dSChunxin Zang 965cb731d6cSVladimir Davydov freed = 0; 966aeed1d32SVladimir Davydov memcg = mem_cgroup_iter(NULL, NULL, NULL); 967cb731d6cSVladimir Davydov do { 9689092c71bSJosef Bacik freed += shrink_slab(GFP_KERNEL, nid, memcg, 0); 969cb731d6cSVladimir Davydov } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 9701399af7eSVlastimil Babka } while ((freed >> shift++) > 1); 971cb731d6cSVladimir Davydov } 972cb731d6cSVladimir Davydov 973cb731d6cSVladimir Davydov void drop_slab(void) 974cb731d6cSVladimir Davydov { 975cb731d6cSVladimir Davydov int nid; 976cb731d6cSVladimir Davydov 977cb731d6cSVladimir Davydov for_each_online_node(nid) 978cb731d6cSVladimir Davydov drop_slab_node(nid); 979cb731d6cSVladimir Davydov } 980cb731d6cSVladimir Davydov 9811da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 9821da177e4SLinus Torvalds { 983ceddc3a5SJohannes Weiner /* 984ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 98567891fffSMatthew Wilcox * that isolated the page, the page cache and optional buffer 98667891fffSMatthew Wilcox * heads at page->private. 987ceddc3a5SJohannes Weiner */ 9883efe62e4SMatthew Wilcox (Oracle) int page_cache_pins = thp_nr_pages(page); 98967891fffSMatthew Wilcox return page_count(page) - page_has_private(page) == 1 + page_cache_pins; 9901da177e4SLinus Torvalds } 9911da177e4SLinus Torvalds 992cb16556dSYang Shi static int may_write_to_inode(struct inode *inode) 9931da177e4SLinus Torvalds { 994930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 9951da177e4SLinus Torvalds return 1; 996703c2708STejun Heo if (!inode_write_congested(inode)) 9971da177e4SLinus Torvalds return 1; 998703c2708STejun Heo if (inode_to_bdi(inode) == current->backing_dev_info) 9991da177e4SLinus Torvalds return 1; 10001da177e4SLinus Torvalds return 0; 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds /* 10041da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 10051da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 10061da177e4SLinus Torvalds * fsync(), msync() or close(). 10071da177e4SLinus Torvalds * 10081da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 10091da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 10101da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 10111da177e4SLinus Torvalds * 10121da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 10131da177e4SLinus Torvalds * __GFP_FS. 10141da177e4SLinus Torvalds */ 10151da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 10161da177e4SLinus Torvalds struct page *page, int error) 10171da177e4SLinus Torvalds { 10187eaceaccSJens Axboe lock_page(page); 10193e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 10203e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 10211da177e4SLinus Torvalds unlock_page(page); 10221da177e4SLinus Torvalds } 10231da177e4SLinus Torvalds 10241b4e3f26SMel Gorman static bool skip_throttle_noprogress(pg_data_t *pgdat) 10251b4e3f26SMel Gorman { 10261b4e3f26SMel Gorman int reclaimable = 0, write_pending = 0; 10271b4e3f26SMel Gorman int i; 10281b4e3f26SMel Gorman 10291b4e3f26SMel Gorman /* 10301b4e3f26SMel Gorman * If kswapd is disabled, reschedule if necessary but do not 10311b4e3f26SMel Gorman * throttle as the system is likely near OOM. 10321b4e3f26SMel Gorman */ 10331b4e3f26SMel Gorman if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 10341b4e3f26SMel Gorman return true; 10351b4e3f26SMel Gorman 10361b4e3f26SMel Gorman /* 10371b4e3f26SMel Gorman * If there are a lot of dirty/writeback pages then do not 10381b4e3f26SMel Gorman * throttle as throttling will occur when the pages cycle 10391b4e3f26SMel Gorman * towards the end of the LRU if still under writeback. 10401b4e3f26SMel Gorman */ 10411b4e3f26SMel Gorman for (i = 0; i < MAX_NR_ZONES; i++) { 10421b4e3f26SMel Gorman struct zone *zone = pgdat->node_zones + i; 10431b4e3f26SMel Gorman 10441b4e3f26SMel Gorman if (!populated_zone(zone)) 10451b4e3f26SMel Gorman continue; 10461b4e3f26SMel Gorman 10471b4e3f26SMel Gorman reclaimable += zone_reclaimable_pages(zone); 10481b4e3f26SMel Gorman write_pending += zone_page_state_snapshot(zone, 10491b4e3f26SMel Gorman NR_ZONE_WRITE_PENDING); 10501b4e3f26SMel Gorman } 10511b4e3f26SMel Gorman if (2 * write_pending <= reclaimable) 10521b4e3f26SMel Gorman return true; 10531b4e3f26SMel Gorman 10541b4e3f26SMel Gorman return false; 10551b4e3f26SMel Gorman } 10561b4e3f26SMel Gorman 1057c3f4a9a2SMel Gorman void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason) 10588cd7c588SMel Gorman { 10598cd7c588SMel Gorman wait_queue_head_t *wqh = &pgdat->reclaim_wait[reason]; 1060c3f4a9a2SMel Gorman long timeout, ret; 10618cd7c588SMel Gorman DEFINE_WAIT(wait); 10628cd7c588SMel Gorman 10638cd7c588SMel Gorman /* 10648cd7c588SMel Gorman * Do not throttle IO workers, kthreads other than kswapd or 10658cd7c588SMel Gorman * workqueues. They may be required for reclaim to make 10668cd7c588SMel Gorman * forward progress (e.g. journalling workqueues or kthreads). 10678cd7c588SMel Gorman */ 10688cd7c588SMel Gorman if (!current_is_kswapd() && 1069b485c6f1SMel Gorman current->flags & (PF_IO_WORKER|PF_KTHREAD)) { 1070b485c6f1SMel Gorman cond_resched(); 10718cd7c588SMel Gorman return; 1072b485c6f1SMel Gorman } 10738cd7c588SMel Gorman 1074c3f4a9a2SMel Gorman /* 1075c3f4a9a2SMel Gorman * These figures are pulled out of thin air. 1076c3f4a9a2SMel Gorman * VMSCAN_THROTTLE_ISOLATED is a transient condition based on too many 1077c3f4a9a2SMel Gorman * parallel reclaimers which is a short-lived event so the timeout is 1078c3f4a9a2SMel Gorman * short. Failing to make progress or waiting on writeback are 1079c3f4a9a2SMel Gorman * potentially long-lived events so use a longer timeout. This is shaky 1080c3f4a9a2SMel Gorman * logic as a failure to make progress could be due to anything from 1081c3f4a9a2SMel Gorman * writeback to a slow device to excessive references pages at the tail 1082c3f4a9a2SMel Gorman * of the inactive LRU. 1083c3f4a9a2SMel Gorman */ 1084c3f4a9a2SMel Gorman switch(reason) { 1085c3f4a9a2SMel Gorman case VMSCAN_THROTTLE_WRITEBACK: 1086c3f4a9a2SMel Gorman timeout = HZ/10; 1087c3f4a9a2SMel Gorman 1088c3f4a9a2SMel Gorman if (atomic_inc_return(&pgdat->nr_writeback_throttled) == 1) { 10898cd7c588SMel Gorman WRITE_ONCE(pgdat->nr_reclaim_start, 10908cd7c588SMel Gorman node_page_state(pgdat, NR_THROTTLED_WRITTEN)); 10918cd7c588SMel Gorman } 10928cd7c588SMel Gorman 1093c3f4a9a2SMel Gorman break; 10941b4e3f26SMel Gorman case VMSCAN_THROTTLE_CONGESTED: 10951b4e3f26SMel Gorman fallthrough; 1096c3f4a9a2SMel Gorman case VMSCAN_THROTTLE_NOPROGRESS: 10971b4e3f26SMel Gorman if (skip_throttle_noprogress(pgdat)) { 10981b4e3f26SMel Gorman cond_resched(); 10991b4e3f26SMel Gorman return; 11001b4e3f26SMel Gorman } 11011b4e3f26SMel Gorman 11021b4e3f26SMel Gorman timeout = 1; 11031b4e3f26SMel Gorman 1104c3f4a9a2SMel Gorman break; 1105c3f4a9a2SMel Gorman case VMSCAN_THROTTLE_ISOLATED: 1106c3f4a9a2SMel Gorman timeout = HZ/50; 1107c3f4a9a2SMel Gorman break; 1108c3f4a9a2SMel Gorman default: 1109c3f4a9a2SMel Gorman WARN_ON_ONCE(1); 1110c3f4a9a2SMel Gorman timeout = HZ; 1111c3f4a9a2SMel Gorman break; 1112c3f4a9a2SMel Gorman } 1113c3f4a9a2SMel Gorman 11148cd7c588SMel Gorman prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); 11158cd7c588SMel Gorman ret = schedule_timeout(timeout); 11168cd7c588SMel Gorman finish_wait(wqh, &wait); 1117d818fca1SMel Gorman 1118c3f4a9a2SMel Gorman if (reason == VMSCAN_THROTTLE_WRITEBACK) 11198cd7c588SMel Gorman atomic_dec(&pgdat->nr_writeback_throttled); 11208cd7c588SMel Gorman 11218cd7c588SMel Gorman trace_mm_vmscan_throttled(pgdat->node_id, jiffies_to_usecs(timeout), 11228cd7c588SMel Gorman jiffies_to_usecs(timeout - ret), 11238cd7c588SMel Gorman reason); 11248cd7c588SMel Gorman } 11258cd7c588SMel Gorman 11268cd7c588SMel Gorman /* 11278cd7c588SMel Gorman * Account for pages written if tasks are throttled waiting on dirty 11288cd7c588SMel Gorman * pages to clean. If enough pages have been cleaned since throttling 11298cd7c588SMel Gorman * started then wakeup the throttled tasks. 11308cd7c588SMel Gorman */ 1131512b7931SLinus Torvalds void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio, 11328cd7c588SMel Gorman int nr_throttled) 11338cd7c588SMel Gorman { 11348cd7c588SMel Gorman unsigned long nr_written; 11358cd7c588SMel Gorman 1136512b7931SLinus Torvalds node_stat_add_folio(folio, NR_THROTTLED_WRITTEN); 11378cd7c588SMel Gorman 11388cd7c588SMel Gorman /* 11398cd7c588SMel Gorman * This is an inaccurate read as the per-cpu deltas may not 11408cd7c588SMel Gorman * be synchronised. However, given that the system is 11418cd7c588SMel Gorman * writeback throttled, it is not worth taking the penalty 11428cd7c588SMel Gorman * of getting an accurate count. At worst, the throttle 11438cd7c588SMel Gorman * timeout guarantees forward progress. 11448cd7c588SMel Gorman */ 11458cd7c588SMel Gorman nr_written = node_page_state(pgdat, NR_THROTTLED_WRITTEN) - 11468cd7c588SMel Gorman READ_ONCE(pgdat->nr_reclaim_start); 11478cd7c588SMel Gorman 11488cd7c588SMel Gorman if (nr_written > SWAP_CLUSTER_MAX * nr_throttled) 11498cd7c588SMel Gorman wake_up(&pgdat->reclaim_wait[VMSCAN_THROTTLE_WRITEBACK]); 11508cd7c588SMel Gorman } 11518cd7c588SMel Gorman 115204e62a29SChristoph Lameter /* possible outcome of pageout() */ 115304e62a29SChristoph Lameter typedef enum { 115404e62a29SChristoph Lameter /* failed to write page out, page is locked */ 115504e62a29SChristoph Lameter PAGE_KEEP, 115604e62a29SChristoph Lameter /* move page to the active list, page is locked */ 115704e62a29SChristoph Lameter PAGE_ACTIVATE, 115804e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 115904e62a29SChristoph Lameter PAGE_SUCCESS, 116004e62a29SChristoph Lameter /* page is clean and locked */ 116104e62a29SChristoph Lameter PAGE_CLEAN, 116204e62a29SChristoph Lameter } pageout_t; 116304e62a29SChristoph Lameter 11641da177e4SLinus Torvalds /* 11651742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 11661742f19fSAndrew Morton * Calls ->writepage(). 11671da177e4SLinus Torvalds */ 1168cb16556dSYang Shi static pageout_t pageout(struct page *page, struct address_space *mapping) 11691da177e4SLinus Torvalds { 11701da177e4SLinus Torvalds /* 11711da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 11721da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 11731da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 11741da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 11751da177e4SLinus Torvalds * PagePrivate for that. 11761da177e4SLinus Torvalds * 11778174202bSAl Viro * If this process is currently in __generic_file_write_iter() against 11781da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 11791da177e4SLinus Torvalds * will block. 11801da177e4SLinus Torvalds * 11811da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 11821da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 11831da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 11841da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 11851da177e4SLinus Torvalds */ 11861da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 11871da177e4SLinus Torvalds return PAGE_KEEP; 11881da177e4SLinus Torvalds if (!mapping) { 11891da177e4SLinus Torvalds /* 11901da177e4SLinus Torvalds * Some data journaling orphaned pages can have 11911da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 11921da177e4SLinus Torvalds */ 1193266cf658SDavid Howells if (page_has_private(page)) { 11941da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 11951da177e4SLinus Torvalds ClearPageDirty(page); 1196b1de0d13SMitchel Humpherys pr_info("%s: orphaned page\n", __func__); 11971da177e4SLinus Torvalds return PAGE_CLEAN; 11981da177e4SLinus Torvalds } 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds return PAGE_KEEP; 12011da177e4SLinus Torvalds } 12021da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 12031da177e4SLinus Torvalds return PAGE_ACTIVATE; 1204cb16556dSYang Shi if (!may_write_to_inode(mapping->host)) 12051da177e4SLinus Torvalds return PAGE_KEEP; 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 12081da177e4SLinus Torvalds int res; 12091da177e4SLinus Torvalds struct writeback_control wbc = { 12101da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 12111da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 1212111ebb6eSOGAWA Hirofumi .range_start = 0, 1213111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 12141da177e4SLinus Torvalds .for_reclaim = 1, 12151da177e4SLinus Torvalds }; 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds SetPageReclaim(page); 12181da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 12191da177e4SLinus Torvalds if (res < 0) 12201da177e4SLinus Torvalds handle_write_error(mapping, page, res); 1221994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 12221da177e4SLinus Torvalds ClearPageReclaim(page); 12231da177e4SLinus Torvalds return PAGE_ACTIVATE; 12241da177e4SLinus Torvalds } 1225c661b078SAndy Whitcroft 12261da177e4SLinus Torvalds if (!PageWriteback(page)) { 12271da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 12281da177e4SLinus Torvalds ClearPageReclaim(page); 12291da177e4SLinus Torvalds } 12303aa23851Syalin wang trace_mm_vmscan_writepage(page); 1231c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_WRITE); 12321da177e4SLinus Torvalds return PAGE_SUCCESS; 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds return PAGE_CLEAN; 12361da177e4SLinus Torvalds } 12371da177e4SLinus Torvalds 1238a649fd92SAndrew Morton /* 1239e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 1240e286781dSNick Piggin * gets returned with a refcount of 0. 1241a649fd92SAndrew Morton */ 1242a528910eSJohannes Weiner static int __remove_mapping(struct address_space *mapping, struct page *page, 1243b910718aSJohannes Weiner bool reclaimed, struct mem_cgroup *target_memcg) 124449d2e9ccSChristoph Lameter { 12458927f647SMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 1246bd4c82c2SHuang Ying int refcount; 1247aae466b0SJoonsoo Kim void *shadow = NULL; 1248c4843a75SGreg Thelen 124928e4d965SNick Piggin BUG_ON(!PageLocked(page)); 125028e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 125149d2e9ccSChristoph Lameter 125251b8c1feSJohannes Weiner if (!PageSwapCache(page)) 125351b8c1feSJohannes Weiner spin_lock(&mapping->host->i_lock); 125430472509SJohannes Weiner xa_lock_irq(&mapping->i_pages); 125549d2e9ccSChristoph Lameter /* 12560fd0e6b0SNick Piggin * The non racy check for a busy page. 12570fd0e6b0SNick Piggin * 12580fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 12590fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 12600fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 12610fd0e6b0SNick Piggin * here, then the following race may occur: 12620fd0e6b0SNick Piggin * 12630fd0e6b0SNick Piggin * get_user_pages(&page); 12640fd0e6b0SNick Piggin * [user mapping goes away] 12650fd0e6b0SNick Piggin * write_to(page); 12660fd0e6b0SNick Piggin * !PageDirty(page) [good] 12670fd0e6b0SNick Piggin * SetPageDirty(page); 12680fd0e6b0SNick Piggin * put_page(page); 12690fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 12700fd0e6b0SNick Piggin * 12710fd0e6b0SNick Piggin * [oops, our write_to data is lost] 12720fd0e6b0SNick Piggin * 12730fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 12740fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 12750139aa7bSJoonsoo Kim * load is not satisfied before that of page->_refcount. 12760fd0e6b0SNick Piggin * 12770fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 1278b93b0163SMatthew Wilcox * and thus under the i_pages lock, then this ordering is not required. 127949d2e9ccSChristoph Lameter */ 1280906d278dSWilliam Kucharski refcount = 1 + compound_nr(page); 1281bd4c82c2SHuang Ying if (!page_ref_freeze(page, refcount)) 128249d2e9ccSChristoph Lameter goto cannot_free; 12831c4c3b99SJiang Biao /* note: atomic_cmpxchg in page_ref_freeze provides the smp_rmb */ 1284e286781dSNick Piggin if (unlikely(PageDirty(page))) { 1285bd4c82c2SHuang Ying page_ref_unfreeze(page, refcount); 128649d2e9ccSChristoph Lameter goto cannot_free; 1287e286781dSNick Piggin } 128849d2e9ccSChristoph Lameter 128949d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 129049d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 1291*3ecb0087SMatthew Wilcox (Oracle) mem_cgroup_swapout(folio, swap); 1292aae466b0SJoonsoo Kim if (reclaimed && !mapping_exiting(mapping)) 12938927f647SMatthew Wilcox (Oracle) shadow = workingset_eviction(folio, target_memcg); 1294aae466b0SJoonsoo Kim __delete_from_swap_cache(page, swap, shadow); 129530472509SJohannes Weiner xa_unlock_irq(&mapping->i_pages); 129675f6d6d2SMinchan Kim put_swap_page(page, swap); 1297e286781dSNick Piggin } else { 12986072d13cSLinus Torvalds void (*freepage)(struct page *); 12996072d13cSLinus Torvalds 13006072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 1301a528910eSJohannes Weiner /* 1302a528910eSJohannes Weiner * Remember a shadow entry for reclaimed file cache in 1303a528910eSJohannes Weiner * order to detect refaults, thus thrashing, later on. 1304a528910eSJohannes Weiner * 1305a528910eSJohannes Weiner * But don't store shadows in an address space that is 1306238c3046Sdylan-meiners * already exiting. This is not just an optimization, 1307a528910eSJohannes Weiner * inode reclaim needs to empty out the radix tree or 1308a528910eSJohannes Weiner * the nodes are lost. Don't plant shadows behind its 1309a528910eSJohannes Weiner * back. 1310f9fe48beSRoss Zwisler * 1311f9fe48beSRoss Zwisler * We also don't store shadows for DAX mappings because the 1312f9fe48beSRoss Zwisler * only page cache pages found in these are zero pages 1313f9fe48beSRoss Zwisler * covering holes, and because we don't want to mix DAX 1314f9fe48beSRoss Zwisler * exceptional entries and shadow exceptional entries in the 1315b93b0163SMatthew Wilcox * same address_space. 1316a528910eSJohannes Weiner */ 13179de4f22aSHuang Ying if (reclaimed && page_is_file_lru(page) && 1318f9fe48beSRoss Zwisler !mapping_exiting(mapping) && !dax_mapping(mapping)) 13198927f647SMatthew Wilcox (Oracle) shadow = workingset_eviction(folio, target_memcg); 13208927f647SMatthew Wilcox (Oracle) __filemap_remove_folio(folio, shadow); 132130472509SJohannes Weiner xa_unlock_irq(&mapping->i_pages); 132251b8c1feSJohannes Weiner if (mapping_shrinkable(mapping)) 132351b8c1feSJohannes Weiner inode_add_lru(mapping->host); 132451b8c1feSJohannes Weiner spin_unlock(&mapping->host->i_lock); 13256072d13cSLinus Torvalds 13266072d13cSLinus Torvalds if (freepage != NULL) 13276072d13cSLinus Torvalds freepage(page); 1328e286781dSNick Piggin } 1329e286781dSNick Piggin 133049d2e9ccSChristoph Lameter return 1; 133149d2e9ccSChristoph Lameter 133249d2e9ccSChristoph Lameter cannot_free: 133330472509SJohannes Weiner xa_unlock_irq(&mapping->i_pages); 133451b8c1feSJohannes Weiner if (!PageSwapCache(page)) 133551b8c1feSJohannes Weiner spin_unlock(&mapping->host->i_lock); 133649d2e9ccSChristoph Lameter return 0; 133749d2e9ccSChristoph Lameter } 133849d2e9ccSChristoph Lameter 13391da177e4SLinus Torvalds /* 1340e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 1341e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 1342e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 1343e286781dSNick Piggin * this page. 1344e286781dSNick Piggin */ 1345e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 1346e286781dSNick Piggin { 1347b910718aSJohannes Weiner if (__remove_mapping(mapping, page, false, NULL)) { 1348e286781dSNick Piggin /* 1349e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 1350e286781dSNick Piggin * drops the pagecache ref for us without requiring another 1351e286781dSNick Piggin * atomic operation. 1352e286781dSNick Piggin */ 1353fe896d18SJoonsoo Kim page_ref_unfreeze(page, 1); 1354e286781dSNick Piggin return 1; 1355e286781dSNick Piggin } 1356e286781dSNick Piggin return 0; 1357e286781dSNick Piggin } 1358e286781dSNick Piggin 1359894bc310SLee Schermerhorn /** 1360894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 1361894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 1362894bc310SLee Schermerhorn * 1363894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 1364894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 1365894bc310SLee Schermerhorn * 1366894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 1367894bc310SLee Schermerhorn */ 1368894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 1369894bc310SLee Schermerhorn { 1370c53954a0SMel Gorman lru_cache_add(page); 1371894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 1372894bc310SLee Schermerhorn } 1373894bc310SLee Schermerhorn 1374dfc8d636SJohannes Weiner enum page_references { 1375dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 1376dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 137764574746SJohannes Weiner PAGEREF_KEEP, 1378dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 1379dfc8d636SJohannes Weiner }; 1380dfc8d636SJohannes Weiner 1381dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 1382dfc8d636SJohannes Weiner struct scan_control *sc) 1383dfc8d636SJohannes Weiner { 138464574746SJohannes Weiner int referenced_ptes, referenced_page; 1385dfc8d636SJohannes Weiner unsigned long vm_flags; 1386dfc8d636SJohannes Weiner 1387c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 1388c3ac9a8aSJohannes Weiner &vm_flags); 138964574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 1390dfc8d636SJohannes Weiner 1391dfc8d636SJohannes Weiner /* 139247d4f3eeSHugh Dickins * The supposedly reclaimable page was found to be in a VM_LOCKED vma. 139347d4f3eeSHugh Dickins * Let the page, now marked Mlocked, be moved to the unevictable list. 1394dfc8d636SJohannes Weiner */ 1395dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 139647d4f3eeSHugh Dickins return PAGEREF_ACTIVATE; 1397dfc8d636SJohannes Weiner 139864574746SJohannes Weiner if (referenced_ptes) { 139964574746SJohannes Weiner /* 140064574746SJohannes Weiner * All mapped pages start out with page table 140164574746SJohannes Weiner * references from the instantiating fault, so we need 140264574746SJohannes Weiner * to look twice if a mapped file page is used more 140364574746SJohannes Weiner * than once. 140464574746SJohannes Weiner * 140564574746SJohannes Weiner * Mark it and spare it for another trip around the 140664574746SJohannes Weiner * inactive list. Another page table reference will 140764574746SJohannes Weiner * lead to its activation. 140864574746SJohannes Weiner * 140964574746SJohannes Weiner * Note: the mark is set for activated pages as well 141064574746SJohannes Weiner * so that recently deactivated but used pages are 141164574746SJohannes Weiner * quickly recovered. 141264574746SJohannes Weiner */ 141364574746SJohannes Weiner SetPageReferenced(page); 141464574746SJohannes Weiner 141534dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 1416dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 1417dfc8d636SJohannes Weiner 1418c909e993SKonstantin Khlebnikov /* 1419c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 1420c909e993SKonstantin Khlebnikov */ 1421b518154eSJoonsoo Kim if ((vm_flags & VM_EXEC) && !PageSwapBacked(page)) 1422c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 1423c909e993SKonstantin Khlebnikov 142464574746SJohannes Weiner return PAGEREF_KEEP; 142564574746SJohannes Weiner } 142664574746SJohannes Weiner 1427dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 14282e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 1429dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 143064574746SJohannes Weiner 143164574746SJohannes Weiner return PAGEREF_RECLAIM; 1432dfc8d636SJohannes Weiner } 1433dfc8d636SJohannes Weiner 1434e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 1435e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page, 1436e2be15f6SMel Gorman bool *dirty, bool *writeback) 1437e2be15f6SMel Gorman { 1438b4597226SMel Gorman struct address_space *mapping; 1439b4597226SMel Gorman 1440e2be15f6SMel Gorman /* 1441e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 1442e2be15f6SMel Gorman * from reclaim context. Do not stall reclaim based on them 1443e2be15f6SMel Gorman */ 14449de4f22aSHuang Ying if (!page_is_file_lru(page) || 1445802a3a92SShaohua Li (PageAnon(page) && !PageSwapBacked(page))) { 1446e2be15f6SMel Gorman *dirty = false; 1447e2be15f6SMel Gorman *writeback = false; 1448e2be15f6SMel Gorman return; 1449e2be15f6SMel Gorman } 1450e2be15f6SMel Gorman 1451e2be15f6SMel Gorman /* By default assume that the page flags are accurate */ 1452e2be15f6SMel Gorman *dirty = PageDirty(page); 1453e2be15f6SMel Gorman *writeback = PageWriteback(page); 1454b4597226SMel Gorman 1455b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 1456b4597226SMel Gorman if (!page_has_private(page)) 1457b4597226SMel Gorman return; 1458b4597226SMel Gorman 1459b4597226SMel Gorman mapping = page_mapping(page); 1460b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 1461b4597226SMel Gorman mapping->a_ops->is_dirty_writeback(page, dirty, writeback); 1462e2be15f6SMel Gorman } 1463e2be15f6SMel Gorman 146426aa2d19SDave Hansen static struct page *alloc_demote_page(struct page *page, unsigned long node) 146526aa2d19SDave Hansen { 146626aa2d19SDave Hansen struct migration_target_control mtc = { 146726aa2d19SDave Hansen /* 146826aa2d19SDave Hansen * Allocate from 'node', or fail quickly and quietly. 146926aa2d19SDave Hansen * When this happens, 'page' will likely just be discarded 147026aa2d19SDave Hansen * instead of migrated. 147126aa2d19SDave Hansen */ 147226aa2d19SDave Hansen .gfp_mask = (GFP_HIGHUSER_MOVABLE & ~__GFP_RECLAIM) | 147326aa2d19SDave Hansen __GFP_THISNODE | __GFP_NOWARN | 147426aa2d19SDave Hansen __GFP_NOMEMALLOC | GFP_NOWAIT, 147526aa2d19SDave Hansen .nid = node 147626aa2d19SDave Hansen }; 147726aa2d19SDave Hansen 147826aa2d19SDave Hansen return alloc_migration_target(page, (unsigned long)&mtc); 147926aa2d19SDave Hansen } 148026aa2d19SDave Hansen 148126aa2d19SDave Hansen /* 148226aa2d19SDave Hansen * Take pages on @demote_list and attempt to demote them to 148326aa2d19SDave Hansen * another node. Pages which are not demoted are left on 148426aa2d19SDave Hansen * @demote_pages. 148526aa2d19SDave Hansen */ 148626aa2d19SDave Hansen static unsigned int demote_page_list(struct list_head *demote_pages, 148726aa2d19SDave Hansen struct pglist_data *pgdat) 148826aa2d19SDave Hansen { 148926aa2d19SDave Hansen int target_nid = next_demotion_node(pgdat->node_id); 149026aa2d19SDave Hansen unsigned int nr_succeeded; 149126aa2d19SDave Hansen 149226aa2d19SDave Hansen if (list_empty(demote_pages)) 149326aa2d19SDave Hansen return 0; 149426aa2d19SDave Hansen 149526aa2d19SDave Hansen if (target_nid == NUMA_NO_NODE) 149626aa2d19SDave Hansen return 0; 149726aa2d19SDave Hansen 149826aa2d19SDave Hansen /* Demotion ignores all cpuset and mempolicy settings */ 1499cb75463cSKai Song migrate_pages(demote_pages, alloc_demote_page, NULL, 150026aa2d19SDave Hansen target_nid, MIGRATE_ASYNC, MR_DEMOTION, 150126aa2d19SDave Hansen &nr_succeeded); 150226aa2d19SDave Hansen 1503668e4147SYang Shi if (current_is_kswapd()) 1504668e4147SYang Shi __count_vm_events(PGDEMOTE_KSWAPD, nr_succeeded); 1505668e4147SYang Shi else 1506668e4147SYang Shi __count_vm_events(PGDEMOTE_DIRECT, nr_succeeded); 1507668e4147SYang Shi 150826aa2d19SDave Hansen return nr_succeeded; 150926aa2d19SDave Hansen } 151026aa2d19SDave Hansen 1511e286781dSNick Piggin /* 15121742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 15131da177e4SLinus Torvalds */ 1514730ec8c0SManinder Singh static unsigned int shrink_page_list(struct list_head *page_list, 1515599d0c95SMel Gorman struct pglist_data *pgdat, 1516f84f6e2bSMel Gorman struct scan_control *sc, 15173c710c1aSMichal Hocko struct reclaim_stat *stat, 15188940b34aSMinchan Kim bool ignore_references) 15191da177e4SLinus Torvalds { 15201da177e4SLinus Torvalds LIST_HEAD(ret_pages); 1521abe4c3b5SMel Gorman LIST_HEAD(free_pages); 152226aa2d19SDave Hansen LIST_HEAD(demote_pages); 1523730ec8c0SManinder Singh unsigned int nr_reclaimed = 0; 1524730ec8c0SManinder Singh unsigned int pgactivate = 0; 152526aa2d19SDave Hansen bool do_demote_pass; 15261da177e4SLinus Torvalds 1527060f005fSKirill Tkhai memset(stat, 0, sizeof(*stat)); 15281da177e4SLinus Torvalds cond_resched(); 152926aa2d19SDave Hansen do_demote_pass = can_demote(pgdat->node_id, sc); 15301da177e4SLinus Torvalds 153126aa2d19SDave Hansen retry: 15321da177e4SLinus Torvalds while (!list_empty(page_list)) { 15331da177e4SLinus Torvalds struct address_space *mapping; 15341da177e4SLinus Torvalds struct page *page; 15358940b34aSMinchan Kim enum page_references references = PAGEREF_RECLAIM; 15364b793062SKirill Tkhai bool dirty, writeback, may_enter_fs; 153798879b3bSYang Shi unsigned int nr_pages; 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds cond_resched(); 15401da177e4SLinus Torvalds 15411da177e4SLinus Torvalds page = lru_to_page(page_list); 15421da177e4SLinus Torvalds list_del(&page->lru); 15431da177e4SLinus Torvalds 1544529ae9aaSNick Piggin if (!trylock_page(page)) 15451da177e4SLinus Torvalds goto keep; 15461da177e4SLinus Torvalds 1547309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 15481da177e4SLinus Torvalds 1549d8c6546bSMatthew Wilcox (Oracle) nr_pages = compound_nr(page); 155098879b3bSYang Shi 155198879b3bSYang Shi /* Account the number of base pages even though THP */ 155298879b3bSYang Shi sc->nr_scanned += nr_pages; 155380e43426SChristoph Lameter 155439b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) 1555ad6b6704SMinchan Kim goto activate_locked; 1556894bc310SLee Schermerhorn 1557a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 155880e43426SChristoph Lameter goto keep_locked; 155980e43426SChristoph Lameter 1560c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 1561c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 1562c661b078SAndy Whitcroft 1563e62e384eSMichal Hocko /* 1564894befecSAndrey Ryabinin * The number of dirty pages determines if a node is marked 15658cd7c588SMel Gorman * reclaim_congested. kswapd will stall and start writing 15668cd7c588SMel Gorman * pages if the tail of the LRU is all dirty unqueued pages. 1567e2be15f6SMel Gorman */ 1568e2be15f6SMel Gorman page_check_dirty_writeback(page, &dirty, &writeback); 1569e2be15f6SMel Gorman if (dirty || writeback) 1570060f005fSKirill Tkhai stat->nr_dirty++; 1571e2be15f6SMel Gorman 1572e2be15f6SMel Gorman if (dirty && !writeback) 1573060f005fSKirill Tkhai stat->nr_unqueued_dirty++; 1574e2be15f6SMel Gorman 1575d04e8acdSMel Gorman /* 1576d04e8acdSMel Gorman * Treat this page as congested if the underlying BDI is or if 1577d04e8acdSMel Gorman * pages are cycling through the LRU so quickly that the 1578d04e8acdSMel Gorman * pages marked for immediate reclaim are making it to the 1579d04e8acdSMel Gorman * end of the LRU a second time. 1580d04e8acdSMel Gorman */ 1581e2be15f6SMel Gorman mapping = page_mapping(page); 15821da58ee2SJamie Liu if (((dirty || writeback) && mapping && 1583703c2708STejun Heo inode_write_congested(mapping->host)) || 1584d04e8acdSMel Gorman (writeback && PageReclaim(page))) 1585060f005fSKirill Tkhai stat->nr_congested++; 1586e2be15f6SMel Gorman 1587e2be15f6SMel Gorman /* 1588283aba9fSMel Gorman * If a page at the tail of the LRU is under writeback, there 1589283aba9fSMel Gorman * are three cases to consider. 1590e62e384eSMichal Hocko * 1591283aba9fSMel Gorman * 1) If reclaim is encountering an excessive number of pages 1592283aba9fSMel Gorman * under writeback and this page is both under writeback and 1593283aba9fSMel Gorman * PageReclaim then it indicates that pages are being queued 1594283aba9fSMel Gorman * for IO but are being recycled through the LRU before the 1595283aba9fSMel Gorman * IO can complete. Waiting on the page itself risks an 1596283aba9fSMel Gorman * indefinite stall if it is impossible to writeback the 1597283aba9fSMel Gorman * page due to IO error or disconnected storage so instead 1598b1a6f21eSMel Gorman * note that the LRU is being scanned too quickly and the 1599b1a6f21eSMel Gorman * caller can stall after page list has been processed. 1600c3b94f44SHugh Dickins * 160197c9341fSTejun Heo * 2) Global or new memcg reclaim encounters a page that is 1602ecf5fc6eSMichal Hocko * not marked for immediate reclaim, or the caller does not 1603ecf5fc6eSMichal Hocko * have __GFP_FS (or __GFP_IO if it's simply going to swap, 1604ecf5fc6eSMichal Hocko * not to fs). In this case mark the page for immediate 160597c9341fSTejun Heo * reclaim and continue scanning. 1606283aba9fSMel Gorman * 1607ecf5fc6eSMichal Hocko * Require may_enter_fs because we would wait on fs, which 1608ecf5fc6eSMichal Hocko * may not have submitted IO yet. And the loop driver might 1609283aba9fSMel Gorman * enter reclaim, and deadlock if it waits on a page for 1610283aba9fSMel Gorman * which it is needed to do the write (loop masks off 1611283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 1612283aba9fSMel Gorman * would probably show more reasons. 1613283aba9fSMel Gorman * 16147fadc820SHugh Dickins * 3) Legacy memcg encounters a page that is already marked 1615283aba9fSMel Gorman * PageReclaim. memcg does not have any dirty pages 1616283aba9fSMel Gorman * throttling so we could easily OOM just because too many 1617283aba9fSMel Gorman * pages are in writeback and there is nothing else to 1618283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 1619c55e8d03SJohannes Weiner * 1620c55e8d03SJohannes Weiner * In cases 1) and 2) we activate the pages to get them out of 1621c55e8d03SJohannes Weiner * the way while we continue scanning for clean pages on the 1622c55e8d03SJohannes Weiner * inactive list and refilling from the active list. The 1623c55e8d03SJohannes Weiner * observation here is that waiting for disk writes is more 1624c55e8d03SJohannes Weiner * expensive than potentially causing reloads down the line. 1625c55e8d03SJohannes Weiner * Since they're marked for immediate reclaim, they won't put 1626c55e8d03SJohannes Weiner * memory pressure on the cache working set any longer than it 1627c55e8d03SJohannes Weiner * takes to write them to disk. 1628e62e384eSMichal Hocko */ 1629283aba9fSMel Gorman if (PageWriteback(page)) { 1630283aba9fSMel Gorman /* Case 1 above */ 1631283aba9fSMel Gorman if (current_is_kswapd() && 1632283aba9fSMel Gorman PageReclaim(page) && 1633599d0c95SMel Gorman test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { 1634060f005fSKirill Tkhai stat->nr_immediate++; 1635c55e8d03SJohannes Weiner goto activate_locked; 1636283aba9fSMel Gorman 1637283aba9fSMel Gorman /* Case 2 above */ 1638b5ead35eSJohannes Weiner } else if (writeback_throttling_sane(sc) || 1639ecf5fc6eSMichal Hocko !PageReclaim(page) || !may_enter_fs) { 1640c3b94f44SHugh Dickins /* 1641c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 1642c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 1643c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 1644c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 1645c3b94f44SHugh Dickins * enough to care. What we do want is for this 1646c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 1647c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 1648c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 1649c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 1650c3b94f44SHugh Dickins */ 1651c3b94f44SHugh Dickins SetPageReclaim(page); 1652060f005fSKirill Tkhai stat->nr_writeback++; 1653c55e8d03SJohannes Weiner goto activate_locked; 1654283aba9fSMel Gorman 1655283aba9fSMel Gorman /* Case 3 above */ 1656283aba9fSMel Gorman } else { 16577fadc820SHugh Dickins unlock_page(page); 1658c3b94f44SHugh Dickins wait_on_page_writeback(page); 16597fadc820SHugh Dickins /* then go back and try same page again */ 16607fadc820SHugh Dickins list_add_tail(&page->lru, page_list); 16617fadc820SHugh Dickins continue; 1662e62e384eSMichal Hocko } 1663283aba9fSMel Gorman } 16641da177e4SLinus Torvalds 16658940b34aSMinchan Kim if (!ignore_references) 16666a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 166702c6de8dSMinchan Kim 1668dfc8d636SJohannes Weiner switch (references) { 1669dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 16701da177e4SLinus Torvalds goto activate_locked; 167164574746SJohannes Weiner case PAGEREF_KEEP: 167298879b3bSYang Shi stat->nr_ref_keep += nr_pages; 167364574746SJohannes Weiner goto keep_locked; 1674dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 1675dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 1676dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 1677dfc8d636SJohannes Weiner } 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds /* 168026aa2d19SDave Hansen * Before reclaiming the page, try to relocate 168126aa2d19SDave Hansen * its contents to another node. 168226aa2d19SDave Hansen */ 168326aa2d19SDave Hansen if (do_demote_pass && 168426aa2d19SDave Hansen (thp_migration_supported() || !PageTransHuge(page))) { 168526aa2d19SDave Hansen list_add(&page->lru, &demote_pages); 168626aa2d19SDave Hansen unlock_page(page); 168726aa2d19SDave Hansen continue; 168826aa2d19SDave Hansen } 168926aa2d19SDave Hansen 169026aa2d19SDave Hansen /* 16911da177e4SLinus Torvalds * Anonymous process memory has backing store? 16921da177e4SLinus Torvalds * Try to allocate it some swap space here. 1693802a3a92SShaohua Li * Lazyfree page could be freed directly 16941da177e4SLinus Torvalds */ 1695bd4c82c2SHuang Ying if (PageAnon(page) && PageSwapBacked(page)) { 1696bd4c82c2SHuang Ying if (!PageSwapCache(page)) { 169763eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 169863eb6b93SHugh Dickins goto keep_locked; 1699feb889fbSLinus Torvalds if (page_maybe_dma_pinned(page)) 1700feb889fbSLinus Torvalds goto keep_locked; 1701747552b1SHuang Ying if (PageTransHuge(page)) { 1702b8f593cdSHuang Ying /* cannot split THP, skip it */ 1703747552b1SHuang Ying if (!can_split_huge_page(page, NULL)) 1704b8f593cdSHuang Ying goto activate_locked; 1705747552b1SHuang Ying /* 1706747552b1SHuang Ying * Split pages without a PMD map right 1707747552b1SHuang Ying * away. Chances are some or all of the 1708747552b1SHuang Ying * tail pages can be freed without IO. 1709747552b1SHuang Ying */ 1710747552b1SHuang Ying if (!compound_mapcount(page) && 1711bd4c82c2SHuang Ying split_huge_page_to_list(page, 1712bd4c82c2SHuang Ying page_list)) 1713747552b1SHuang Ying goto activate_locked; 1714747552b1SHuang Ying } 17150f074658SMinchan Kim if (!add_to_swap(page)) { 17160f074658SMinchan Kim if (!PageTransHuge(page)) 171798879b3bSYang Shi goto activate_locked_split; 1718bd4c82c2SHuang Ying /* Fallback to swap normal pages */ 1719bd4c82c2SHuang Ying if (split_huge_page_to_list(page, 1720bd4c82c2SHuang Ying page_list)) 17210f074658SMinchan Kim goto activate_locked; 1722fe490cc0SHuang Ying #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1723fe490cc0SHuang Ying count_vm_event(THP_SWPOUT_FALLBACK); 1724fe490cc0SHuang Ying #endif 17250f074658SMinchan Kim if (!add_to_swap(page)) 172698879b3bSYang Shi goto activate_locked_split; 17270f074658SMinchan Kim } 17280f074658SMinchan Kim 17294b793062SKirill Tkhai may_enter_fs = true; 17301da177e4SLinus Torvalds 1731e2be15f6SMel Gorman /* Adding to swap updated mapping */ 17321da177e4SLinus Torvalds mapping = page_mapping(page); 1733bd4c82c2SHuang Ying } 17347751b2daSKirill A. Shutemov } else if (unlikely(PageTransHuge(page))) { 17357751b2daSKirill A. Shutemov /* Split file THP */ 17367751b2daSKirill A. Shutemov if (split_huge_page_to_list(page, page_list)) 17377751b2daSKirill A. Shutemov goto keep_locked; 1738e2be15f6SMel Gorman } 17391da177e4SLinus Torvalds 17401da177e4SLinus Torvalds /* 174198879b3bSYang Shi * THP may get split above, need minus tail pages and update 174298879b3bSYang Shi * nr_pages to avoid accounting tail pages twice. 174398879b3bSYang Shi * 174498879b3bSYang Shi * The tail pages that are added into swap cache successfully 174598879b3bSYang Shi * reach here. 174698879b3bSYang Shi */ 174798879b3bSYang Shi if ((nr_pages > 1) && !PageTransHuge(page)) { 174898879b3bSYang Shi sc->nr_scanned -= (nr_pages - 1); 174998879b3bSYang Shi nr_pages = 1; 175098879b3bSYang Shi } 175198879b3bSYang Shi 175298879b3bSYang Shi /* 17531da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 17541da177e4SLinus Torvalds * processes. Try to unmap it here. 17551da177e4SLinus Torvalds */ 1756802a3a92SShaohua Li if (page_mapped(page)) { 1757013339dfSShakeel Butt enum ttu_flags flags = TTU_BATCH_FLUSH; 17581f318a9bSJaewon Kim bool was_swapbacked = PageSwapBacked(page); 1759bd4c82c2SHuang Ying 1760bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page))) 1761bd4c82c2SHuang Ying flags |= TTU_SPLIT_HUGE_PMD; 17621f318a9bSJaewon Kim 17631fb08ac6SYang Shi try_to_unmap(page, flags); 17641fb08ac6SYang Shi if (page_mapped(page)) { 176598879b3bSYang Shi stat->nr_unmap_fail += nr_pages; 17661f318a9bSJaewon Kim if (!was_swapbacked && PageSwapBacked(page)) 17671f318a9bSJaewon Kim stat->nr_lazyfree_fail += nr_pages; 17681da177e4SLinus Torvalds goto activate_locked; 17691da177e4SLinus Torvalds } 17701da177e4SLinus Torvalds } 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds if (PageDirty(page)) { 1773ee72886dSMel Gorman /* 17744eda4823SJohannes Weiner * Only kswapd can writeback filesystem pages 17754eda4823SJohannes Weiner * to avoid risk of stack overflow. But avoid 17764eda4823SJohannes Weiner * injecting inefficient single-page IO into 17774eda4823SJohannes Weiner * flusher writeback as much as possible: only 17784eda4823SJohannes Weiner * write pages when we've encountered many 17794eda4823SJohannes Weiner * dirty pages, and when we've already scanned 17804eda4823SJohannes Weiner * the rest of the LRU for clean pages and see 17814eda4823SJohannes Weiner * the same dirty pages again (PageReclaim). 1782ee72886dSMel Gorman */ 17839de4f22aSHuang Ying if (page_is_file_lru(page) && 17844eda4823SJohannes Weiner (!current_is_kswapd() || !PageReclaim(page) || 1785599d0c95SMel Gorman !test_bit(PGDAT_DIRTY, &pgdat->flags))) { 178649ea7eb6SMel Gorman /* 178749ea7eb6SMel Gorman * Immediately reclaim when written back. 178849ea7eb6SMel Gorman * Similar in principal to deactivate_page() 178949ea7eb6SMel Gorman * except we already have the page isolated 179049ea7eb6SMel Gorman * and know it's dirty 179149ea7eb6SMel Gorman */ 1792c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_IMMEDIATE); 179349ea7eb6SMel Gorman SetPageReclaim(page); 179449ea7eb6SMel Gorman 1795c55e8d03SJohannes Weiner goto activate_locked; 1796ee72886dSMel Gorman } 1797ee72886dSMel Gorman 1798dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 17991da177e4SLinus Torvalds goto keep_locked; 18004dd4b920SAndrew Morton if (!may_enter_fs) 18011da177e4SLinus Torvalds goto keep_locked; 180252a8363eSChristoph Lameter if (!sc->may_writepage) 18031da177e4SLinus Torvalds goto keep_locked; 18041da177e4SLinus Torvalds 1805d950c947SMel Gorman /* 1806d950c947SMel Gorman * Page is dirty. Flush the TLB if a writable entry 1807d950c947SMel Gorman * potentially exists to avoid CPU writes after IO 1808d950c947SMel Gorman * starts and then write it out here. 1809d950c947SMel Gorman */ 1810d950c947SMel Gorman try_to_unmap_flush_dirty(); 1811cb16556dSYang Shi switch (pageout(page, mapping)) { 18121da177e4SLinus Torvalds case PAGE_KEEP: 18131da177e4SLinus Torvalds goto keep_locked; 18141da177e4SLinus Torvalds case PAGE_ACTIVATE: 18151da177e4SLinus Torvalds goto activate_locked; 18161da177e4SLinus Torvalds case PAGE_SUCCESS: 18176c357848SMatthew Wilcox (Oracle) stat->nr_pageout += thp_nr_pages(page); 181896f8bf4fSJohannes Weiner 18197d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 182041ac1999SMel Gorman goto keep; 18217d3579e8SKOSAKI Motohiro if (PageDirty(page)) 18221da177e4SLinus Torvalds goto keep; 18237d3579e8SKOSAKI Motohiro 18241da177e4SLinus Torvalds /* 18251da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 18261da177e4SLinus Torvalds * ahead and try to reclaim the page. 18271da177e4SLinus Torvalds */ 1828529ae9aaSNick Piggin if (!trylock_page(page)) 18291da177e4SLinus Torvalds goto keep; 18301da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 18311da177e4SLinus Torvalds goto keep_locked; 18321da177e4SLinus Torvalds mapping = page_mapping(page); 183301359eb2SGustavo A. R. Silva fallthrough; 18341da177e4SLinus Torvalds case PAGE_CLEAN: 18351da177e4SLinus Torvalds ; /* try to free the page below */ 18361da177e4SLinus Torvalds } 18371da177e4SLinus Torvalds } 18381da177e4SLinus Torvalds 18391da177e4SLinus Torvalds /* 18401da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 18411da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 18421da177e4SLinus Torvalds * the page as well. 18431da177e4SLinus Torvalds * 18441da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 18451da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 18461da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 18471da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 18481da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 18491da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 18501da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 18511da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 18521da177e4SLinus Torvalds * 18531da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 18541da177e4SLinus Torvalds * the pages which were not successfully invalidated in 1855d12b8951SYang Shi * truncate_cleanup_page(). We try to drop those buffers here 18561da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 18571da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 18581da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 18591da177e4SLinus Torvalds */ 1860266cf658SDavid Howells if (page_has_private(page)) { 18611da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 18621da177e4SLinus Torvalds goto activate_locked; 1863e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 1864e286781dSNick Piggin unlock_page(page); 1865e286781dSNick Piggin if (put_page_testzero(page)) 18661da177e4SLinus Torvalds goto free_it; 1867e286781dSNick Piggin else { 1868e286781dSNick Piggin /* 1869e286781dSNick Piggin * rare race with speculative reference. 1870e286781dSNick Piggin * the speculative reference will free 1871e286781dSNick Piggin * this page shortly, so we may 1872e286781dSNick Piggin * increment nr_reclaimed here (and 1873e286781dSNick Piggin * leave it off the LRU). 1874e286781dSNick Piggin */ 1875e286781dSNick Piggin nr_reclaimed++; 1876e286781dSNick Piggin continue; 1877e286781dSNick Piggin } 1878e286781dSNick Piggin } 18791da177e4SLinus Torvalds } 18801da177e4SLinus Torvalds 1881802a3a92SShaohua Li if (PageAnon(page) && !PageSwapBacked(page)) { 1882802a3a92SShaohua Li /* follow __remove_mapping for reference */ 1883802a3a92SShaohua Li if (!page_ref_freeze(page, 1)) 188449d2e9ccSChristoph Lameter goto keep_locked; 1885d17be2d9SMiaohe Lin /* 1886d17be2d9SMiaohe Lin * The page has only one reference left, which is 1887d17be2d9SMiaohe Lin * from the isolation. After the caller puts the 1888d17be2d9SMiaohe Lin * page back on lru and drops the reference, the 1889d17be2d9SMiaohe Lin * page will be freed anyway. It doesn't matter 1890d17be2d9SMiaohe Lin * which lru it goes. So we don't bother checking 1891d17be2d9SMiaohe Lin * PageDirty here. 1892d17be2d9SMiaohe Lin */ 1893802a3a92SShaohua Li count_vm_event(PGLAZYFREED); 18942262185cSRoman Gushchin count_memcg_page_event(page, PGLAZYFREED); 1895b910718aSJohannes Weiner } else if (!mapping || !__remove_mapping(mapping, page, true, 1896b910718aSJohannes Weiner sc->target_mem_cgroup)) 1897802a3a92SShaohua Li goto keep_locked; 18989a1ea439SHugh Dickins 18999a1ea439SHugh Dickins unlock_page(page); 1900e286781dSNick Piggin free_it: 190198879b3bSYang Shi /* 190298879b3bSYang Shi * THP may get swapped out in a whole, need account 190398879b3bSYang Shi * all base pages. 190498879b3bSYang Shi */ 190598879b3bSYang Shi nr_reclaimed += nr_pages; 1906abe4c3b5SMel Gorman 1907abe4c3b5SMel Gorman /* 1908abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1909abe4c3b5SMel Gorman * appear not as the counts should be low 1910abe4c3b5SMel Gorman */ 19117ae88534SYang Shi if (unlikely(PageTransHuge(page))) 1912ff45fc3cSMatthew Wilcox (Oracle) destroy_compound_page(page); 19137ae88534SYang Shi else 1914abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 19151da177e4SLinus Torvalds continue; 19161da177e4SLinus Torvalds 191798879b3bSYang Shi activate_locked_split: 191898879b3bSYang Shi /* 191998879b3bSYang Shi * The tail pages that are failed to add into swap cache 192098879b3bSYang Shi * reach here. Fixup nr_scanned and nr_pages. 192198879b3bSYang Shi */ 192298879b3bSYang Shi if (nr_pages > 1) { 192398879b3bSYang Shi sc->nr_scanned -= (nr_pages - 1); 192498879b3bSYang Shi nr_pages = 1; 192598879b3bSYang Shi } 19261da177e4SLinus Torvalds activate_locked: 192768a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 1928ad6b6704SMinchan Kim if (PageSwapCache(page) && (mem_cgroup_swap_full(page) || 1929ad6b6704SMinchan Kim PageMlocked(page))) 1930a2c43eedSHugh Dickins try_to_free_swap(page); 1931309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 1932ad6b6704SMinchan Kim if (!PageMlocked(page)) { 19339de4f22aSHuang Ying int type = page_is_file_lru(page); 19341da177e4SLinus Torvalds SetPageActive(page); 193598879b3bSYang Shi stat->nr_activate[type] += nr_pages; 19362262185cSRoman Gushchin count_memcg_page_event(page, PGACTIVATE); 1937ad6b6704SMinchan Kim } 19381da177e4SLinus Torvalds keep_locked: 19391da177e4SLinus Torvalds unlock_page(page); 19401da177e4SLinus Torvalds keep: 19411da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 1942309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); 19431da177e4SLinus Torvalds } 194426aa2d19SDave Hansen /* 'page_list' is always empty here */ 194526aa2d19SDave Hansen 194626aa2d19SDave Hansen /* Migrate pages selected for demotion */ 194726aa2d19SDave Hansen nr_reclaimed += demote_page_list(&demote_pages, pgdat); 194826aa2d19SDave Hansen /* Pages that could not be demoted are still in @demote_pages */ 194926aa2d19SDave Hansen if (!list_empty(&demote_pages)) { 195026aa2d19SDave Hansen /* Pages which failed to demoted go back on @page_list for retry: */ 195126aa2d19SDave Hansen list_splice_init(&demote_pages, page_list); 195226aa2d19SDave Hansen do_demote_pass = false; 195326aa2d19SDave Hansen goto retry; 195426aa2d19SDave Hansen } 1955abe4c3b5SMel Gorman 195698879b3bSYang Shi pgactivate = stat->nr_activate[0] + stat->nr_activate[1]; 195798879b3bSYang Shi 1958747db954SJohannes Weiner mem_cgroup_uncharge_list(&free_pages); 195972b252aeSMel Gorman try_to_unmap_flush(); 19602d4894b5SMel Gorman free_unref_page_list(&free_pages); 1961abe4c3b5SMel Gorman 19621da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1963886cf190SKirill Tkhai count_vm_events(PGACTIVATE, pgactivate); 19640a31bc97SJohannes Weiner 196505ff5137SAndrew Morton return nr_reclaimed; 19661da177e4SLinus Torvalds } 19671da177e4SLinus Torvalds 1968730ec8c0SManinder Singh unsigned int reclaim_clean_pages_from_list(struct zone *zone, 196902c6de8dSMinchan Kim struct list_head *page_list) 197002c6de8dSMinchan Kim { 197102c6de8dSMinchan Kim struct scan_control sc = { 197202c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 197302c6de8dSMinchan Kim .may_unmap = 1, 197402c6de8dSMinchan Kim }; 19751f318a9bSJaewon Kim struct reclaim_stat stat; 1976730ec8c0SManinder Singh unsigned int nr_reclaimed; 197702c6de8dSMinchan Kim struct page *page, *next; 197802c6de8dSMinchan Kim LIST_HEAD(clean_pages); 19792d2b8d2bSYu Zhao unsigned int noreclaim_flag; 198002c6de8dSMinchan Kim 198102c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 1982ae37c7ffSOscar Salvador if (!PageHuge(page) && page_is_file_lru(page) && 1983ae37c7ffSOscar Salvador !PageDirty(page) && !__PageMovable(page) && 1984ae37c7ffSOscar Salvador !PageUnevictable(page)) { 198502c6de8dSMinchan Kim ClearPageActive(page); 198602c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 198702c6de8dSMinchan Kim } 198802c6de8dSMinchan Kim } 198902c6de8dSMinchan Kim 19902d2b8d2bSYu Zhao /* 19912d2b8d2bSYu Zhao * We should be safe here since we are only dealing with file pages and 19922d2b8d2bSYu Zhao * we are not kswapd and therefore cannot write dirty file pages. But 19932d2b8d2bSYu Zhao * call memalloc_noreclaim_save() anyway, just in case these conditions 19942d2b8d2bSYu Zhao * change in the future. 19952d2b8d2bSYu Zhao */ 19962d2b8d2bSYu Zhao noreclaim_flag = memalloc_noreclaim_save(); 19971f318a9bSJaewon Kim nr_reclaimed = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc, 1998013339dfSShakeel Butt &stat, true); 19992d2b8d2bSYu Zhao memalloc_noreclaim_restore(noreclaim_flag); 20002d2b8d2bSYu Zhao 200102c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 20022da9f630SNicholas Piggin mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, 20032da9f630SNicholas Piggin -(long)nr_reclaimed); 20041f318a9bSJaewon Kim /* 20051f318a9bSJaewon Kim * Since lazyfree pages are isolated from file LRU from the beginning, 20061f318a9bSJaewon Kim * they will rotate back to anonymous LRU in the end if it failed to 20071f318a9bSJaewon Kim * discard so isolated count will be mismatched. 20081f318a9bSJaewon Kim * Compensate the isolated count for both LRU lists. 20091f318a9bSJaewon Kim */ 20101f318a9bSJaewon Kim mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON, 20111f318a9bSJaewon Kim stat.nr_lazyfree_fail); 20121f318a9bSJaewon Kim mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, 20132da9f630SNicholas Piggin -(long)stat.nr_lazyfree_fail); 20141f318a9bSJaewon Kim return nr_reclaimed; 201502c6de8dSMinchan Kim } 201602c6de8dSMinchan Kim 20175ad333ebSAndy Whitcroft /* 20185ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 20195ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 20205ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 20215ad333ebSAndy Whitcroft * 20225ad333ebSAndy Whitcroft * page: page to consider 20235ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 20245ad333ebSAndy Whitcroft * 2025c2135f7cSAlex Shi * returns true on success, false on failure. 20265ad333ebSAndy Whitcroft */ 2027c2135f7cSAlex Shi bool __isolate_lru_page_prepare(struct page *page, isolate_mode_t mode) 20285ad333ebSAndy Whitcroft { 20295ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 20305ad333ebSAndy Whitcroft if (!PageLRU(page)) 2031c2135f7cSAlex Shi return false; 20325ad333ebSAndy Whitcroft 2033e46a2879SMinchan Kim /* Compaction should not handle unevictable pages but CMA can do so */ 2034e46a2879SMinchan Kim if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) 2035c2135f7cSAlex Shi return false; 2036894bc310SLee Schermerhorn 2037c8244935SMel Gorman /* 2038c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 2039c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 2040c8244935SMel Gorman * blocking - clean pages for the most part. 2041c8244935SMel Gorman * 2042c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 2043c8244935SMel Gorman * that it is possible to migrate without blocking 2044c8244935SMel Gorman */ 20451276ad68SJohannes Weiner if (mode & ISOLATE_ASYNC_MIGRATE) { 2046c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 2047c8244935SMel Gorman if (PageWriteback(page)) 2048c2135f7cSAlex Shi return false; 204939deaf85SMinchan Kim 2050c8244935SMel Gorman if (PageDirty(page)) { 2051c8244935SMel Gorman struct address_space *mapping; 205269d763fcSMel Gorman bool migrate_dirty; 2053c8244935SMel Gorman 2054c8244935SMel Gorman /* 2055c8244935SMel Gorman * Only pages without mappings or that have a 2056c8244935SMel Gorman * ->migratepage callback are possible to migrate 205769d763fcSMel Gorman * without blocking. However, we can be racing with 205869d763fcSMel Gorman * truncation so it's necessary to lock the page 205969d763fcSMel Gorman * to stabilise the mapping as truncation holds 206069d763fcSMel Gorman * the page lock until after the page is removed 206169d763fcSMel Gorman * from the page cache. 2062c8244935SMel Gorman */ 206369d763fcSMel Gorman if (!trylock_page(page)) 2064c2135f7cSAlex Shi return false; 206569d763fcSMel Gorman 2066c8244935SMel Gorman mapping = page_mapping(page); 2067145e1a71SHugh Dickins migrate_dirty = !mapping || mapping->a_ops->migratepage; 206869d763fcSMel Gorman unlock_page(page); 206969d763fcSMel Gorman if (!migrate_dirty) 2070c2135f7cSAlex Shi return false; 2071c8244935SMel Gorman } 2072c8244935SMel Gorman } 2073c8244935SMel Gorman 2074f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 2075c2135f7cSAlex Shi return false; 2076f80c0673SMinchan Kim 2077c2135f7cSAlex Shi return true; 20785ad333ebSAndy Whitcroft } 20795ad333ebSAndy Whitcroft 20807ee36a14SMel Gorman /* 20817ee36a14SMel Gorman * Update LRU sizes after isolating pages. The LRU size updates must 208255b65a57SEthon Paul * be complete before mem_cgroup_update_lru_size due to a sanity check. 20837ee36a14SMel Gorman */ 20847ee36a14SMel Gorman static __always_inline void update_lru_sizes(struct lruvec *lruvec, 2085b4536f0cSMichal Hocko enum lru_list lru, unsigned long *nr_zone_taken) 20867ee36a14SMel Gorman { 20877ee36a14SMel Gorman int zid; 20887ee36a14SMel Gorman 20897ee36a14SMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 20907ee36a14SMel Gorman if (!nr_zone_taken[zid]) 20917ee36a14SMel Gorman continue; 20927ee36a14SMel Gorman 2093a892cb6bSWei Yang update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 20947ee36a14SMel Gorman } 20957ee36a14SMel Gorman 20967ee36a14SMel Gorman } 20977ee36a14SMel Gorman 2098f611fab7SMel Gorman /* 209915b44736SHugh Dickins * Isolating page from the lruvec to fill in @dst list by nr_to_scan times. 210015b44736SHugh Dickins * 210115b44736SHugh Dickins * lruvec->lru_lock is heavily contended. Some of the functions that 21021da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 21031da177e4SLinus Torvalds * and working on them outside the LRU lock. 21041da177e4SLinus Torvalds * 21051da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 21061da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 21071da177e4SLinus Torvalds * 210815b44736SHugh Dickins * Lru_lock must be held before calling this function. 21091da177e4SLinus Torvalds * 2110791b48b6SMinchan Kim * @nr_to_scan: The number of eligible pages to look through on the list. 21115dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 21121da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 2113f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 2114fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 21153cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 21161da177e4SLinus Torvalds * 21171da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 21181da177e4SLinus Torvalds */ 211969e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 21205dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 2121fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 2122a9e7c39fSKirill Tkhai enum lru_list lru) 21231da177e4SLinus Torvalds { 212475b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 212569e05944SAndrew Morton unsigned long nr_taken = 0; 2126599d0c95SMel Gorman unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; 21277cc30fcfSMel Gorman unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; 21283db65812SJohannes Weiner unsigned long skipped = 0; 2129791b48b6SMinchan Kim unsigned long scan, total_scan, nr_pages; 2130b2e18757SMel Gorman LIST_HEAD(pages_skipped); 2131a9e7c39fSKirill Tkhai isolate_mode_t mode = (sc->may_unmap ? 0 : ISOLATE_UNMAPPED); 21321da177e4SLinus Torvalds 213398879b3bSYang Shi total_scan = 0; 2134791b48b6SMinchan Kim scan = 0; 213598879b3bSYang Shi while (scan < nr_to_scan && !list_empty(src)) { 21365ad333ebSAndy Whitcroft struct page *page; 21375ad333ebSAndy Whitcroft 21381da177e4SLinus Torvalds page = lru_to_page(src); 21391da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 21401da177e4SLinus Torvalds 2141d8c6546bSMatthew Wilcox (Oracle) nr_pages = compound_nr(page); 214298879b3bSYang Shi total_scan += nr_pages; 214398879b3bSYang Shi 2144b2e18757SMel Gorman if (page_zonenum(page) > sc->reclaim_idx) { 2145b2e18757SMel Gorman list_move(&page->lru, &pages_skipped); 214698879b3bSYang Shi nr_skipped[page_zonenum(page)] += nr_pages; 2147b2e18757SMel Gorman continue; 2148b2e18757SMel Gorman } 2149b2e18757SMel Gorman 2150791b48b6SMinchan Kim /* 2151791b48b6SMinchan Kim * Do not count skipped pages because that makes the function 2152791b48b6SMinchan Kim * return with no isolated pages if the LRU mostly contains 2153791b48b6SMinchan Kim * ineligible pages. This causes the VM to not reclaim any 2154791b48b6SMinchan Kim * pages, triggering a premature OOM. 215598879b3bSYang Shi * 215698879b3bSYang Shi * Account all tail pages of THP. This would not cause 215798879b3bSYang Shi * premature OOM since __isolate_lru_page() returns -EBUSY 215898879b3bSYang Shi * only when the page is being freed somewhere else. 2159791b48b6SMinchan Kim */ 216098879b3bSYang Shi scan += nr_pages; 2161c2135f7cSAlex Shi if (!__isolate_lru_page_prepare(page, mode)) { 2162c2135f7cSAlex Shi /* It is being freed elsewhere */ 2163c2135f7cSAlex Shi list_move(&page->lru, src); 2164c2135f7cSAlex Shi continue; 2165c2135f7cSAlex Shi } 21669df41314SAlex Shi /* 21679df41314SAlex Shi * Be careful not to clear PageLRU until after we're 21689df41314SAlex Shi * sure the page is not being freed elsewhere -- the 21699df41314SAlex Shi * page release code relies on it. 21709df41314SAlex Shi */ 2171c2135f7cSAlex Shi if (unlikely(!get_page_unless_zero(page))) { 2172c2135f7cSAlex Shi list_move(&page->lru, src); 2173c2135f7cSAlex Shi continue; 2174c2135f7cSAlex Shi } 21759df41314SAlex Shi 21769df41314SAlex Shi if (!TestClearPageLRU(page)) { 2177c2135f7cSAlex Shi /* Another thread is already isolating this page */ 21789df41314SAlex Shi put_page(page); 2179c2135f7cSAlex Shi list_move(&page->lru, src); 2180c2135f7cSAlex Shi continue; 21819df41314SAlex Shi } 21829df41314SAlex Shi 2183599d0c95SMel Gorman nr_taken += nr_pages; 2184599d0c95SMel Gorman nr_zone_taken[page_zonenum(page)] += nr_pages; 21855ad333ebSAndy Whitcroft list_move(&page->lru, dst); 21865ad333ebSAndy Whitcroft } 21871da177e4SLinus Torvalds 2188b2e18757SMel Gorman /* 2189b2e18757SMel Gorman * Splice any skipped pages to the start of the LRU list. Note that 2190b2e18757SMel Gorman * this disrupts the LRU order when reclaiming for lower zones but 2191b2e18757SMel Gorman * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX 2192b2e18757SMel Gorman * scanning would soon rescan the same pages to skip and put the 2193b2e18757SMel Gorman * system at risk of premature OOM. 2194b2e18757SMel Gorman */ 21957cc30fcfSMel Gorman if (!list_empty(&pages_skipped)) { 21967cc30fcfSMel Gorman int zid; 21977cc30fcfSMel Gorman 21983db65812SJohannes Weiner list_splice(&pages_skipped, src); 21997cc30fcfSMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 22007cc30fcfSMel Gorman if (!nr_skipped[zid]) 22017cc30fcfSMel Gorman continue; 22027cc30fcfSMel Gorman 22037cc30fcfSMel Gorman __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); 22041265e3a6SMichal Hocko skipped += nr_skipped[zid]; 22057cc30fcfSMel Gorman } 22067cc30fcfSMel Gorman } 2207791b48b6SMinchan Kim *nr_scanned = total_scan; 22081265e3a6SMichal Hocko trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, 2209791b48b6SMinchan Kim total_scan, skipped, nr_taken, mode, lru); 2210b4536f0cSMichal Hocko update_lru_sizes(lruvec, lru, nr_zone_taken); 22111da177e4SLinus Torvalds return nr_taken; 22121da177e4SLinus Torvalds } 22131da177e4SLinus Torvalds 221462695a84SNick Piggin /** 2215d1d8a3b4SMatthew Wilcox (Oracle) * folio_isolate_lru() - Try to isolate a folio from its LRU list. 2216d1d8a3b4SMatthew Wilcox (Oracle) * @folio: Folio to isolate from its LRU list. 221762695a84SNick Piggin * 2218d1d8a3b4SMatthew Wilcox (Oracle) * Isolate a @folio from an LRU list and adjust the vmstat statistic 2219d1d8a3b4SMatthew Wilcox (Oracle) * corresponding to whatever LRU list the folio was on. 222062695a84SNick Piggin * 2221d1d8a3b4SMatthew Wilcox (Oracle) * The folio will have its LRU flag cleared. If it was found on the 2222d1d8a3b4SMatthew Wilcox (Oracle) * active list, it will have the Active flag set. If it was found on the 2223d1d8a3b4SMatthew Wilcox (Oracle) * unevictable list, it will have the Unevictable flag set. These flags 2224894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 222562695a84SNick Piggin * 2226d1d8a3b4SMatthew Wilcox (Oracle) * Context: 2227a5d09bedSMike Rapoport * 222862695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 2229d1d8a3b4SMatthew Wilcox (Oracle) * fundamental difference from isolate_lru_pages() (which is called 223062695a84SNick Piggin * without a stable reference). 2231d1d8a3b4SMatthew Wilcox (Oracle) * (2) The lru_lock must not be held. 2232d1d8a3b4SMatthew Wilcox (Oracle) * (3) Interrupts must be enabled. 2233d1d8a3b4SMatthew Wilcox (Oracle) * 2234d1d8a3b4SMatthew Wilcox (Oracle) * Return: 0 if the folio was removed from an LRU list. 2235d1d8a3b4SMatthew Wilcox (Oracle) * -EBUSY if the folio was not on an LRU list. 223662695a84SNick Piggin */ 2237d1d8a3b4SMatthew Wilcox (Oracle) int folio_isolate_lru(struct folio *folio) 223862695a84SNick Piggin { 223962695a84SNick Piggin int ret = -EBUSY; 224062695a84SNick Piggin 2241d1d8a3b4SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_ref_count(folio), folio); 22420c917313SKonstantin Khlebnikov 2243d1d8a3b4SMatthew Wilcox (Oracle) if (folio_test_clear_lru(folio)) { 2244fa9add64SHugh Dickins struct lruvec *lruvec; 224562695a84SNick Piggin 2246d1d8a3b4SMatthew Wilcox (Oracle) folio_get(folio); 2247e809c3feSMatthew Wilcox (Oracle) lruvec = folio_lruvec_lock_irq(folio); 2248d1d8a3b4SMatthew Wilcox (Oracle) lruvec_del_folio(lruvec, folio); 22496168d0daSAlex Shi unlock_page_lruvec_irq(lruvec); 2250fa9add64SHugh Dickins ret = 0; 225162695a84SNick Piggin } 2252d25b5bd8SAlex Shi 225362695a84SNick Piggin return ret; 225462695a84SNick Piggin } 225562695a84SNick Piggin 22565ad333ebSAndy Whitcroft /* 2257d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 2258178821b8SXianting Tian * then get rescheduled. When there are massive number of tasks doing page 2259d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 2260d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 2261d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 226235cd7815SRik van Riel */ 2263599d0c95SMel Gorman static int too_many_isolated(struct pglist_data *pgdat, int file, 226435cd7815SRik van Riel struct scan_control *sc) 226535cd7815SRik van Riel { 226635cd7815SRik van Riel unsigned long inactive, isolated; 2267d818fca1SMel Gorman bool too_many; 226835cd7815SRik van Riel 226935cd7815SRik van Riel if (current_is_kswapd()) 227035cd7815SRik van Riel return 0; 227135cd7815SRik van Riel 2272b5ead35eSJohannes Weiner if (!writeback_throttling_sane(sc)) 227335cd7815SRik van Riel return 0; 227435cd7815SRik van Riel 227535cd7815SRik van Riel if (file) { 2276599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_FILE); 2277599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_FILE); 227835cd7815SRik van Riel } else { 2279599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_ANON); 2280599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_ANON); 228135cd7815SRik van Riel } 228235cd7815SRik van Riel 22833cf23841SFengguang Wu /* 22843cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 22853cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 22863cf23841SFengguang Wu * deadlock. 22873cf23841SFengguang Wu */ 2288d0164adcSMel Gorman if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) 22893cf23841SFengguang Wu inactive >>= 3; 22903cf23841SFengguang Wu 2291d818fca1SMel Gorman too_many = isolated > inactive; 2292d818fca1SMel Gorman 2293d818fca1SMel Gorman /* Wake up tasks throttled due to too_many_isolated. */ 2294d818fca1SMel Gorman if (!too_many) 2295d818fca1SMel Gorman wake_throttle_isolated(pgdat); 2296d818fca1SMel Gorman 2297d818fca1SMel Gorman return too_many; 229835cd7815SRik van Riel } 229935cd7815SRik van Riel 2300a222f341SKirill Tkhai /* 230115b44736SHugh Dickins * move_pages_to_lru() moves pages from private @list to appropriate LRU list. 230215b44736SHugh Dickins * On return, @list is reused as a list of pages to be freed by the caller. 2303a222f341SKirill Tkhai * 2304a222f341SKirill Tkhai * Returns the number of pages moved to the given lruvec. 2305a222f341SKirill Tkhai */ 23069ef56b78SMuchun Song static unsigned int move_pages_to_lru(struct lruvec *lruvec, 2307a222f341SKirill Tkhai struct list_head *list) 230866635629SMel Gorman { 2309a222f341SKirill Tkhai int nr_pages, nr_moved = 0; 23103f79768fSHugh Dickins LIST_HEAD(pages_to_free); 2311a222f341SKirill Tkhai struct page *page; 231266635629SMel Gorman 2313a222f341SKirill Tkhai while (!list_empty(list)) { 2314a222f341SKirill Tkhai page = lru_to_page(list); 2315309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 2316a222f341SKirill Tkhai list_del(&page->lru); 23173d06afabSAlex Shi if (unlikely(!page_evictable(page))) { 23186168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 231966635629SMel Gorman putback_lru_page(page); 23206168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 232166635629SMel Gorman continue; 232266635629SMel Gorman } 2323fa9add64SHugh Dickins 23243d06afabSAlex Shi /* 23253d06afabSAlex Shi * The SetPageLRU needs to be kept here for list integrity. 23263d06afabSAlex Shi * Otherwise: 23273d06afabSAlex Shi * #0 move_pages_to_lru #1 release_pages 23283d06afabSAlex Shi * if !put_page_testzero 23293d06afabSAlex Shi * if (put_page_testzero()) 23303d06afabSAlex Shi * !PageLRU //skip lru_lock 23313d06afabSAlex Shi * SetPageLRU() 23323d06afabSAlex Shi * list_add(&page->lru,) 23333d06afabSAlex Shi * list_add(&page->lru,) 23343d06afabSAlex Shi */ 23357a608572SLinus Torvalds SetPageLRU(page); 2336a222f341SKirill Tkhai 23373d06afabSAlex Shi if (unlikely(put_page_testzero(page))) { 233887560179SYu Zhao __clear_page_lru_flags(page); 23392bcf8879SHugh Dickins 23402bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 23416168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 2342ff45fc3cSMatthew Wilcox (Oracle) destroy_compound_page(page); 23436168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 23442bcf8879SHugh Dickins } else 23452bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 23463d06afabSAlex Shi 23473d06afabSAlex Shi continue; 23483d06afabSAlex Shi } 23493d06afabSAlex Shi 2350afca9157SAlex Shi /* 2351afca9157SAlex Shi * All pages were isolated from the same lruvec (and isolation 2352afca9157SAlex Shi * inhibits memcg migration). 2353afca9157SAlex Shi */ 23540de340cbSMatthew Wilcox (Oracle) VM_BUG_ON_PAGE(!folio_matches_lruvec(page_folio(page), lruvec), page); 23553a9c9788SYu Zhao add_page_to_lru_list(page, lruvec); 23563d06afabSAlex Shi nr_pages = thp_nr_pages(page); 2357a222f341SKirill Tkhai nr_moved += nr_pages; 235831d8fcacSJohannes Weiner if (PageActive(page)) 235931d8fcacSJohannes Weiner workingset_age_nonresident(lruvec, nr_pages); 236066635629SMel Gorman } 236166635629SMel Gorman 23623f79768fSHugh Dickins /* 23633f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 23643f79768fSHugh Dickins */ 2365a222f341SKirill Tkhai list_splice(&pages_to_free, list); 2366a222f341SKirill Tkhai 2367a222f341SKirill Tkhai return nr_moved; 236866635629SMel Gorman } 236966635629SMel Gorman 237066635629SMel Gorman /* 2371399ba0b9SNeilBrown * If a kernel thread (such as nfsd for loop-back mounts) services 2372a37b0715SNeilBrown * a backing device by writing to the page cache it sets PF_LOCAL_THROTTLE. 2373399ba0b9SNeilBrown * In that case we should only throttle if the backing device it is 2374399ba0b9SNeilBrown * writing to is congested. In other cases it is safe to throttle. 2375399ba0b9SNeilBrown */ 2376399ba0b9SNeilBrown static int current_may_throttle(void) 2377399ba0b9SNeilBrown { 2378a37b0715SNeilBrown return !(current->flags & PF_LOCAL_THROTTLE) || 2379399ba0b9SNeilBrown current->backing_dev_info == NULL || 2380399ba0b9SNeilBrown bdi_write_congested(current->backing_dev_info); 2381399ba0b9SNeilBrown } 2382399ba0b9SNeilBrown 2383399ba0b9SNeilBrown /* 2384b2e18757SMel Gorman * shrink_inactive_list() is a helper for shrink_node(). It returns the number 23851742f19fSAndrew Morton * of reclaimed pages 23861da177e4SLinus Torvalds */ 23879ef56b78SMuchun Song static unsigned long 23881a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 23899e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 23901da177e4SLinus Torvalds { 23911da177e4SLinus Torvalds LIST_HEAD(page_list); 2392e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 2393730ec8c0SManinder Singh unsigned int nr_reclaimed = 0; 2394e247dbceSKOSAKI Motohiro unsigned long nr_taken; 2395060f005fSKirill Tkhai struct reclaim_stat stat; 2396497a6c1bSJohannes Weiner bool file = is_file_lru(lru); 2397f46b7912SKirill Tkhai enum vm_event_item item; 2398599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 2399db73ee0dSMichal Hocko bool stalled = false; 240078dc583dSKOSAKI Motohiro 2401599d0c95SMel Gorman while (unlikely(too_many_isolated(pgdat, file, sc))) { 2402db73ee0dSMichal Hocko if (stalled) 2403db73ee0dSMichal Hocko return 0; 2404db73ee0dSMichal Hocko 2405db73ee0dSMichal Hocko /* wait a bit for the reclaimer. */ 2406db73ee0dSMichal Hocko stalled = true; 2407c3f4a9a2SMel Gorman reclaim_throttle(pgdat, VMSCAN_THROTTLE_ISOLATED); 240835cd7815SRik van Riel 240935cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 241035cd7815SRik van Riel if (fatal_signal_pending(current)) 241135cd7815SRik van Riel return SWAP_CLUSTER_MAX; 241235cd7815SRik van Riel } 241335cd7815SRik van Riel 24141da177e4SLinus Torvalds lru_add_drain(); 2415f80c0673SMinchan Kim 24166168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 24171da177e4SLinus Torvalds 24185dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 2419a9e7c39fSKirill Tkhai &nr_scanned, sc, lru); 242095d918fcSKonstantin Khlebnikov 2421599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 2422f46b7912SKirill Tkhai item = current_is_kswapd() ? PGSCAN_KSWAPD : PGSCAN_DIRECT; 2423b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 2424f46b7912SKirill Tkhai __count_vm_events(item, nr_scanned); 2425f46b7912SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned); 2426497a6c1bSJohannes Weiner __count_vm_events(PGSCAN_ANON + file, nr_scanned); 2427497a6c1bSJohannes Weiner 24286168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 2429d563c050SHillf Danton 2430d563c050SHillf Danton if (nr_taken == 0) 243166635629SMel Gorman return 0; 2432b35ea17bSKOSAKI Motohiro 2433013339dfSShakeel Butt nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, &stat, false); 2434c661b078SAndy Whitcroft 24356168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 2436497a6c1bSJohannes Weiner move_pages_to_lru(lruvec, &page_list); 2437497a6c1bSJohannes Weiner 2438497a6c1bSJohannes Weiner __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 2439f46b7912SKirill Tkhai item = current_is_kswapd() ? PGSTEAL_KSWAPD : PGSTEAL_DIRECT; 2440b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 2441f46b7912SKirill Tkhai __count_vm_events(item, nr_reclaimed); 2442f46b7912SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed); 2443497a6c1bSJohannes Weiner __count_vm_events(PGSTEAL_ANON + file, nr_reclaimed); 24446168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 24453f79768fSHugh Dickins 244675cc3c91SAlex Shi lru_note_cost(lruvec, file, stat.nr_pageout); 2447747db954SJohannes Weiner mem_cgroup_uncharge_list(&page_list); 24482d4894b5SMel Gorman free_unref_page_list(&page_list); 2449e11da5b4SMel Gorman 245092df3a72SMel Gorman /* 24511c610d5fSAndrey Ryabinin * If dirty pages are scanned that are not queued for IO, it 24521c610d5fSAndrey Ryabinin * implies that flushers are not doing their job. This can 24531c610d5fSAndrey Ryabinin * happen when memory pressure pushes dirty pages to the end of 24541c610d5fSAndrey Ryabinin * the LRU before the dirty limits are breached and the dirty 24551c610d5fSAndrey Ryabinin * data has expired. It can also happen when the proportion of 24561c610d5fSAndrey Ryabinin * dirty pages grows not through writes but through memory 24571c610d5fSAndrey Ryabinin * pressure reclaiming all the clean cache. And in some cases, 24581c610d5fSAndrey Ryabinin * the flushers simply cannot keep up with the allocation 24591c610d5fSAndrey Ryabinin * rate. Nudge the flusher threads in case they are asleep. 24601c610d5fSAndrey Ryabinin */ 24611c610d5fSAndrey Ryabinin if (stat.nr_unqueued_dirty == nr_taken) 24621c610d5fSAndrey Ryabinin wakeup_flusher_threads(WB_REASON_VMSCAN); 24631c610d5fSAndrey Ryabinin 2464d108c772SAndrey Ryabinin sc->nr.dirty += stat.nr_dirty; 2465d108c772SAndrey Ryabinin sc->nr.congested += stat.nr_congested; 2466d108c772SAndrey Ryabinin sc->nr.unqueued_dirty += stat.nr_unqueued_dirty; 2467d108c772SAndrey Ryabinin sc->nr.writeback += stat.nr_writeback; 2468d108c772SAndrey Ryabinin sc->nr.immediate += stat.nr_immediate; 2469d108c772SAndrey Ryabinin sc->nr.taken += nr_taken; 2470d108c772SAndrey Ryabinin if (file) 2471d108c772SAndrey Ryabinin sc->nr.file_taken += nr_taken; 24728e950282SMel Gorman 2473599d0c95SMel Gorman trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, 2474d51d1e64SSteven Rostedt nr_scanned, nr_reclaimed, &stat, sc->priority, file); 247505ff5137SAndrew Morton return nr_reclaimed; 24761da177e4SLinus Torvalds } 24771da177e4SLinus Torvalds 247815b44736SHugh Dickins /* 247915b44736SHugh Dickins * shrink_active_list() moves pages from the active LRU to the inactive LRU. 248015b44736SHugh Dickins * 248115b44736SHugh Dickins * We move them the other way if the page is referenced by one or more 248215b44736SHugh Dickins * processes. 248315b44736SHugh Dickins * 248415b44736SHugh Dickins * If the pages are mostly unmapped, the processing is fast and it is 248515b44736SHugh Dickins * appropriate to hold lru_lock across the whole operation. But if 248615b44736SHugh Dickins * the pages are mapped, the processing is slow (page_referenced()), so 248715b44736SHugh Dickins * we should drop lru_lock around each page. It's impossible to balance 248815b44736SHugh Dickins * this, so instead we remove the pages from the LRU while processing them. 248915b44736SHugh Dickins * It is safe to rely on PG_active against the non-LRU pages in here because 249015b44736SHugh Dickins * nobody will play with that bit on a non-LRU page. 249115b44736SHugh Dickins * 249215b44736SHugh Dickins * The downside is that we have to touch page->_refcount against each page. 249315b44736SHugh Dickins * But we had to alter page->flags anyway. 249415b44736SHugh Dickins */ 2495f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 24961a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 2497f16015fbSJohannes Weiner struct scan_control *sc, 24989e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 24991cfb419bSKAMEZAWA Hiroyuki { 250044c241f1SKOSAKI Motohiro unsigned long nr_taken; 2501f626012dSHugh Dickins unsigned long nr_scanned; 25026fe6b7e3SWu Fengguang unsigned long vm_flags; 25031cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 25048cab4754SWu Fengguang LIST_HEAD(l_active); 2505b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 25061cfb419bSKAMEZAWA Hiroyuki struct page *page; 25079d998b4fSMichal Hocko unsigned nr_deactivate, nr_activate; 25089d998b4fSMichal Hocko unsigned nr_rotated = 0; 25093cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 2510599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 25111cfb419bSKAMEZAWA Hiroyuki 25121da177e4SLinus Torvalds lru_add_drain(); 2513f80c0673SMinchan Kim 25146168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 2515925b7673SJohannes Weiner 25165dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 2517a9e7c39fSKirill Tkhai &nr_scanned, sc, lru); 251889b5fae5SJohannes Weiner 2519599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 25201cfb419bSKAMEZAWA Hiroyuki 2521912c0572SShakeel Butt if (!cgroup_reclaim(sc)) 2522599d0c95SMel Gorman __count_vm_events(PGREFILL, nr_scanned); 25232fa2690cSYafang Shao __count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned); 25249d5e6a9fSHugh Dickins 25256168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 25261da177e4SLinus Torvalds 25271da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 25281da177e4SLinus Torvalds cond_resched(); 25291da177e4SLinus Torvalds page = lru_to_page(&l_hold); 25301da177e4SLinus Torvalds list_del(&page->lru); 25317e9cd484SRik van Riel 253239b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 2533894bc310SLee Schermerhorn putback_lru_page(page); 2534894bc310SLee Schermerhorn continue; 2535894bc310SLee Schermerhorn } 2536894bc310SLee Schermerhorn 2537cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 2538cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 2539cc715d99SMel Gorman if (page_has_private(page)) 2540cc715d99SMel Gorman try_to_release_page(page, 0); 2541cc715d99SMel Gorman unlock_page(page); 2542cc715d99SMel Gorman } 2543cc715d99SMel Gorman } 2544cc715d99SMel Gorman 2545c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 2546c3ac9a8aSJohannes Weiner &vm_flags)) { 25478cab4754SWu Fengguang /* 25488cab4754SWu Fengguang * Identify referenced, file-backed active pages and 25498cab4754SWu Fengguang * give them one more trip around the active list. So 25508cab4754SWu Fengguang * that executable code get better chances to stay in 25518cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 25528cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 25538cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 25548cab4754SWu Fengguang * so we ignore them here. 25558cab4754SWu Fengguang */ 25569de4f22aSHuang Ying if ((vm_flags & VM_EXEC) && page_is_file_lru(page)) { 25576c357848SMatthew Wilcox (Oracle) nr_rotated += thp_nr_pages(page); 25588cab4754SWu Fengguang list_add(&page->lru, &l_active); 25598cab4754SWu Fengguang continue; 25608cab4754SWu Fengguang } 25618cab4754SWu Fengguang } 25627e9cd484SRik van Riel 25635205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 25641899ad18SJohannes Weiner SetPageWorkingset(page); 25651da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 25661da177e4SLinus Torvalds } 25671da177e4SLinus Torvalds 2568b555749aSAndrew Morton /* 25698cab4754SWu Fengguang * Move pages back to the lru list. 2570b555749aSAndrew Morton */ 25716168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 2572556adecbSRik van Riel 2573a222f341SKirill Tkhai nr_activate = move_pages_to_lru(lruvec, &l_active); 2574a222f341SKirill Tkhai nr_deactivate = move_pages_to_lru(lruvec, &l_inactive); 2575f372d89eSKirill Tkhai /* Keep all free pages in l_active list */ 2576f372d89eSKirill Tkhai list_splice(&l_inactive, &l_active); 25779851ac13SKirill Tkhai 25789851ac13SKirill Tkhai __count_vm_events(PGDEACTIVATE, nr_deactivate); 25799851ac13SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate); 25809851ac13SKirill Tkhai 2581599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 25826168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 25832bcf8879SHugh Dickins 2584f372d89eSKirill Tkhai mem_cgroup_uncharge_list(&l_active); 2585f372d89eSKirill Tkhai free_unref_page_list(&l_active); 25869d998b4fSMichal Hocko trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate, 25879d998b4fSMichal Hocko nr_deactivate, nr_rotated, sc->priority, file); 25881da177e4SLinus Torvalds } 25891da177e4SLinus Torvalds 25901a4e58ccSMinchan Kim unsigned long reclaim_pages(struct list_head *page_list) 25911a4e58ccSMinchan Kim { 2592f661d007SYang Shi int nid = NUMA_NO_NODE; 2593730ec8c0SManinder Singh unsigned int nr_reclaimed = 0; 25941a4e58ccSMinchan Kim LIST_HEAD(node_page_list); 25951a4e58ccSMinchan Kim struct reclaim_stat dummy_stat; 25961a4e58ccSMinchan Kim struct page *page; 25972d2b8d2bSYu Zhao unsigned int noreclaim_flag; 25981a4e58ccSMinchan Kim struct scan_control sc = { 25991a4e58ccSMinchan Kim .gfp_mask = GFP_KERNEL, 26001a4e58ccSMinchan Kim .may_writepage = 1, 26011a4e58ccSMinchan Kim .may_unmap = 1, 26021a4e58ccSMinchan Kim .may_swap = 1, 260326aa2d19SDave Hansen .no_demotion = 1, 26041a4e58ccSMinchan Kim }; 26051a4e58ccSMinchan Kim 26062d2b8d2bSYu Zhao noreclaim_flag = memalloc_noreclaim_save(); 26072d2b8d2bSYu Zhao 26081a4e58ccSMinchan Kim while (!list_empty(page_list)) { 26091a4e58ccSMinchan Kim page = lru_to_page(page_list); 2610f661d007SYang Shi if (nid == NUMA_NO_NODE) { 26111a4e58ccSMinchan Kim nid = page_to_nid(page); 26121a4e58ccSMinchan Kim INIT_LIST_HEAD(&node_page_list); 26131a4e58ccSMinchan Kim } 26141a4e58ccSMinchan Kim 26151a4e58ccSMinchan Kim if (nid == page_to_nid(page)) { 26161a4e58ccSMinchan Kim ClearPageActive(page); 26171a4e58ccSMinchan Kim list_move(&page->lru, &node_page_list); 26181a4e58ccSMinchan Kim continue; 26191a4e58ccSMinchan Kim } 26201a4e58ccSMinchan Kim 26211a4e58ccSMinchan Kim nr_reclaimed += shrink_page_list(&node_page_list, 26221a4e58ccSMinchan Kim NODE_DATA(nid), 2623013339dfSShakeel Butt &sc, &dummy_stat, false); 26241a4e58ccSMinchan Kim while (!list_empty(&node_page_list)) { 26251a4e58ccSMinchan Kim page = lru_to_page(&node_page_list); 26261a4e58ccSMinchan Kim list_del(&page->lru); 26271a4e58ccSMinchan Kim putback_lru_page(page); 26281a4e58ccSMinchan Kim } 26291a4e58ccSMinchan Kim 2630f661d007SYang Shi nid = NUMA_NO_NODE; 26311a4e58ccSMinchan Kim } 26321a4e58ccSMinchan Kim 26331a4e58ccSMinchan Kim if (!list_empty(&node_page_list)) { 26341a4e58ccSMinchan Kim nr_reclaimed += shrink_page_list(&node_page_list, 26351a4e58ccSMinchan Kim NODE_DATA(nid), 2636013339dfSShakeel Butt &sc, &dummy_stat, false); 26371a4e58ccSMinchan Kim while (!list_empty(&node_page_list)) { 26381a4e58ccSMinchan Kim page = lru_to_page(&node_page_list); 26391a4e58ccSMinchan Kim list_del(&page->lru); 26401a4e58ccSMinchan Kim putback_lru_page(page); 26411a4e58ccSMinchan Kim } 26421a4e58ccSMinchan Kim } 26431a4e58ccSMinchan Kim 26442d2b8d2bSYu Zhao memalloc_noreclaim_restore(noreclaim_flag); 26452d2b8d2bSYu Zhao 26461a4e58ccSMinchan Kim return nr_reclaimed; 26471a4e58ccSMinchan Kim } 26481a4e58ccSMinchan Kim 2649b91ac374SJohannes Weiner static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 2650b91ac374SJohannes Weiner struct lruvec *lruvec, struct scan_control *sc) 2651b91ac374SJohannes Weiner { 2652b91ac374SJohannes Weiner if (is_active_lru(lru)) { 2653b91ac374SJohannes Weiner if (sc->may_deactivate & (1 << is_file_lru(lru))) 2654b91ac374SJohannes Weiner shrink_active_list(nr_to_scan, lruvec, sc, lru); 2655b91ac374SJohannes Weiner else 2656b91ac374SJohannes Weiner sc->skipped_deactivate = 1; 2657b91ac374SJohannes Weiner return 0; 2658b91ac374SJohannes Weiner } 2659b91ac374SJohannes Weiner 2660b91ac374SJohannes Weiner return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 2661b91ac374SJohannes Weiner } 2662b91ac374SJohannes Weiner 266359dc76b0SRik van Riel /* 266459dc76b0SRik van Riel * The inactive anon list should be small enough that the VM never has 266559dc76b0SRik van Riel * to do too much work. 266614797e23SKOSAKI Motohiro * 266759dc76b0SRik van Riel * The inactive file list should be small enough to leave most memory 266859dc76b0SRik van Riel * to the established workingset on the scan-resistant active list, 266959dc76b0SRik van Riel * but large enough to avoid thrashing the aggregate readahead window. 267059dc76b0SRik van Riel * 267159dc76b0SRik van Riel * Both inactive lists should also be large enough that each inactive 267259dc76b0SRik van Riel * page has a chance to be referenced again before it is reclaimed. 267359dc76b0SRik van Riel * 26742a2e4885SJohannes Weiner * If that fails and refaulting is observed, the inactive list grows. 26752a2e4885SJohannes Weiner * 267659dc76b0SRik van Riel * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages 26773a50d14dSAndrey Ryabinin * on this LRU, maintained by the pageout code. An inactive_ratio 267859dc76b0SRik van Riel * of 3 means 3:1 or 25% of the pages are kept on the inactive list. 267959dc76b0SRik van Riel * 268059dc76b0SRik van Riel * total target max 268159dc76b0SRik van Riel * memory ratio inactive 268259dc76b0SRik van Riel * ------------------------------------- 268359dc76b0SRik van Riel * 10MB 1 5MB 268459dc76b0SRik van Riel * 100MB 1 50MB 268559dc76b0SRik van Riel * 1GB 3 250MB 268659dc76b0SRik van Riel * 10GB 10 0.9GB 268759dc76b0SRik van Riel * 100GB 31 3GB 268859dc76b0SRik van Riel * 1TB 101 10GB 268959dc76b0SRik van Riel * 10TB 320 32GB 269014797e23SKOSAKI Motohiro */ 2691b91ac374SJohannes Weiner static bool inactive_is_low(struct lruvec *lruvec, enum lru_list inactive_lru) 269214797e23SKOSAKI Motohiro { 2693b91ac374SJohannes Weiner enum lru_list active_lru = inactive_lru + LRU_ACTIVE; 26942a2e4885SJohannes Weiner unsigned long inactive, active; 26952a2e4885SJohannes Weiner unsigned long inactive_ratio; 269659dc76b0SRik van Riel unsigned long gb; 269759dc76b0SRik van Riel 2698b91ac374SJohannes Weiner inactive = lruvec_page_state(lruvec, NR_LRU_BASE + inactive_lru); 2699b91ac374SJohannes Weiner active = lruvec_page_state(lruvec, NR_LRU_BASE + active_lru); 2700f8d1a311SMel Gorman 270159dc76b0SRik van Riel gb = (inactive + active) >> (30 - PAGE_SHIFT); 27024002570cSJoonsoo Kim if (gb) 270359dc76b0SRik van Riel inactive_ratio = int_sqrt(10 * gb); 2704b39415b2SRik van Riel else 270559dc76b0SRik van Riel inactive_ratio = 1; 2706fd538803SMichal Hocko 270759dc76b0SRik van Riel return inactive * inactive_ratio < active; 2708b39415b2SRik van Riel } 2709b39415b2SRik van Riel 27109a265114SJohannes Weiner enum scan_balance { 27119a265114SJohannes Weiner SCAN_EQUAL, 27129a265114SJohannes Weiner SCAN_FRACT, 27139a265114SJohannes Weiner SCAN_ANON, 27149a265114SJohannes Weiner SCAN_FILE, 27159a265114SJohannes Weiner }; 27169a265114SJohannes Weiner 27171da177e4SLinus Torvalds /* 27184f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 27194f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 27204f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 27214f98a2feSRik van Riel * onto the active list instead of evict. 27224f98a2feSRik van Riel * 2723be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 2724be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 27254f98a2feSRik van Riel */ 2726afaf07a6SJohannes Weiner static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, 2727afaf07a6SJohannes Weiner unsigned long *nr) 27284f98a2feSRik van Riel { 2729a2a36488SKeith Busch struct pglist_data *pgdat = lruvec_pgdat(lruvec); 2730afaf07a6SJohannes Weiner struct mem_cgroup *memcg = lruvec_memcg(lruvec); 2731d483a5ddSJohannes Weiner unsigned long anon_cost, file_cost, total_cost; 273233377678SVladimir Davydov int swappiness = mem_cgroup_swappiness(memcg); 2733ed017373SYu Zhao u64 fraction[ANON_AND_FILE]; 27349a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 27359a265114SJohannes Weiner enum scan_balance scan_balance; 27369a265114SJohannes Weiner unsigned long ap, fp; 27379a265114SJohannes Weiner enum lru_list lru; 273876a33fc3SShaohua Li 273976a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 2740a2a36488SKeith Busch if (!sc->may_swap || !can_reclaim_anon_pages(memcg, pgdat->node_id, sc)) { 27419a265114SJohannes Weiner scan_balance = SCAN_FILE; 274276a33fc3SShaohua Li goto out; 274376a33fc3SShaohua Li } 27444f98a2feSRik van Riel 274510316b31SJohannes Weiner /* 274610316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 274710316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 274810316b31SJohannes Weiner * disable swapping for individual groups completely when 274910316b31SJohannes Weiner * using the memory controller's swap limit feature would be 275010316b31SJohannes Weiner * too expensive. 275110316b31SJohannes Weiner */ 2752b5ead35eSJohannes Weiner if (cgroup_reclaim(sc) && !swappiness) { 27539a265114SJohannes Weiner scan_balance = SCAN_FILE; 275410316b31SJohannes Weiner goto out; 275510316b31SJohannes Weiner } 275610316b31SJohannes Weiner 275710316b31SJohannes Weiner /* 275810316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 275910316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 276010316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 276110316b31SJohannes Weiner */ 276202695175SJohannes Weiner if (!sc->priority && swappiness) { 27639a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 276410316b31SJohannes Weiner goto out; 276510316b31SJohannes Weiner } 276610316b31SJohannes Weiner 276711d16c25SJohannes Weiner /* 276853138ceaSJohannes Weiner * If the system is almost out of file pages, force-scan anon. 276962376251SJohannes Weiner */ 2770b91ac374SJohannes Weiner if (sc->file_is_tiny) { 277162376251SJohannes Weiner scan_balance = SCAN_ANON; 277262376251SJohannes Weiner goto out; 277362376251SJohannes Weiner } 277462376251SJohannes Weiner 277562376251SJohannes Weiner /* 2776b91ac374SJohannes Weiner * If there is enough inactive page cache, we do not reclaim 2777b91ac374SJohannes Weiner * anything from the anonymous working right now. 2778e9868505SRik van Riel */ 2779b91ac374SJohannes Weiner if (sc->cache_trim_mode) { 27809a265114SJohannes Weiner scan_balance = SCAN_FILE; 2781e9868505SRik van Riel goto out; 27824f98a2feSRik van Riel } 27834f98a2feSRik van Riel 27849a265114SJohannes Weiner scan_balance = SCAN_FRACT; 27854f98a2feSRik van Riel /* 2786314b57fbSJohannes Weiner * Calculate the pressure balance between anon and file pages. 2787314b57fbSJohannes Weiner * 2788314b57fbSJohannes Weiner * The amount of pressure we put on each LRU is inversely 2789314b57fbSJohannes Weiner * proportional to the cost of reclaiming each list, as 2790314b57fbSJohannes Weiner * determined by the share of pages that are refaulting, times 2791314b57fbSJohannes Weiner * the relative IO cost of bringing back a swapped out 2792314b57fbSJohannes Weiner * anonymous page vs reloading a filesystem page (swappiness). 2793314b57fbSJohannes Weiner * 2794d483a5ddSJohannes Weiner * Although we limit that influence to ensure no list gets 2795d483a5ddSJohannes Weiner * left behind completely: at least a third of the pressure is 2796d483a5ddSJohannes Weiner * applied, before swappiness. 2797d483a5ddSJohannes Weiner * 2798314b57fbSJohannes Weiner * With swappiness at 100, anon and file have equal IO cost. 279958c37f6eSKOSAKI Motohiro */ 2800d483a5ddSJohannes Weiner total_cost = sc->anon_cost + sc->file_cost; 2801d483a5ddSJohannes Weiner anon_cost = total_cost + sc->anon_cost; 2802d483a5ddSJohannes Weiner file_cost = total_cost + sc->file_cost; 2803d483a5ddSJohannes Weiner total_cost = anon_cost + file_cost; 280458c37f6eSKOSAKI Motohiro 2805d483a5ddSJohannes Weiner ap = swappiness * (total_cost + 1); 2806d483a5ddSJohannes Weiner ap /= anon_cost + 1; 28074f98a2feSRik van Riel 2808d483a5ddSJohannes Weiner fp = (200 - swappiness) * (total_cost + 1); 2809d483a5ddSJohannes Weiner fp /= file_cost + 1; 28104f98a2feSRik van Riel 281176a33fc3SShaohua Li fraction[0] = ap; 281276a33fc3SShaohua Li fraction[1] = fp; 2813a4fe1631SJohannes Weiner denominator = ap + fp; 281476a33fc3SShaohua Li out: 28154111304dSHugh Dickins for_each_evictable_lru(lru) { 28164111304dSHugh Dickins int file = is_file_lru(lru); 28179783aa99SChris Down unsigned long lruvec_size; 2818f56ce412SJohannes Weiner unsigned long low, min; 281976a33fc3SShaohua Li unsigned long scan; 282076a33fc3SShaohua Li 28219783aa99SChris Down lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); 2822f56ce412SJohannes Weiner mem_cgroup_protection(sc->target_mem_cgroup, memcg, 2823f56ce412SJohannes Weiner &min, &low); 28249783aa99SChris Down 2825f56ce412SJohannes Weiner if (min || low) { 28269783aa99SChris Down /* 28279783aa99SChris Down * Scale a cgroup's reclaim pressure by proportioning 28289783aa99SChris Down * its current usage to its memory.low or memory.min 28299783aa99SChris Down * setting. 28309783aa99SChris Down * 28319783aa99SChris Down * This is important, as otherwise scanning aggression 28329783aa99SChris Down * becomes extremely binary -- from nothing as we 28339783aa99SChris Down * approach the memory protection threshold, to totally 28349783aa99SChris Down * nominal as we exceed it. This results in requiring 28359783aa99SChris Down * setting extremely liberal protection thresholds. It 28369783aa99SChris Down * also means we simply get no protection at all if we 28379783aa99SChris Down * set it too low, which is not ideal. 28381bc63fb1SChris Down * 28391bc63fb1SChris Down * If there is any protection in place, we reduce scan 28401bc63fb1SChris Down * pressure by how much of the total memory used is 28411bc63fb1SChris Down * within protection thresholds. 28429783aa99SChris Down * 28439de7ca46SChris Down * There is one special case: in the first reclaim pass, 28449de7ca46SChris Down * we skip over all groups that are within their low 28459de7ca46SChris Down * protection. If that fails to reclaim enough pages to 28469de7ca46SChris Down * satisfy the reclaim goal, we come back and override 28479de7ca46SChris Down * the best-effort low protection. However, we still 28489de7ca46SChris Down * ideally want to honor how well-behaved groups are in 28499de7ca46SChris Down * that case instead of simply punishing them all 28509de7ca46SChris Down * equally. As such, we reclaim them based on how much 28511bc63fb1SChris Down * memory they are using, reducing the scan pressure 28521bc63fb1SChris Down * again by how much of the total memory used is under 28531bc63fb1SChris Down * hard protection. 28549783aa99SChris Down */ 28551bc63fb1SChris Down unsigned long cgroup_size = mem_cgroup_size(memcg); 2856f56ce412SJohannes Weiner unsigned long protection; 2857f56ce412SJohannes Weiner 2858f56ce412SJohannes Weiner /* memory.low scaling, make sure we retry before OOM */ 2859f56ce412SJohannes Weiner if (!sc->memcg_low_reclaim && low > min) { 2860f56ce412SJohannes Weiner protection = low; 2861f56ce412SJohannes Weiner sc->memcg_low_skipped = 1; 2862f56ce412SJohannes Weiner } else { 2863f56ce412SJohannes Weiner protection = min; 2864f56ce412SJohannes Weiner } 28651bc63fb1SChris Down 28661bc63fb1SChris Down /* Avoid TOCTOU with earlier protection check */ 28671bc63fb1SChris Down cgroup_size = max(cgroup_size, protection); 28681bc63fb1SChris Down 28691bc63fb1SChris Down scan = lruvec_size - lruvec_size * protection / 287032d4f4b7SRik van Riel (cgroup_size + 1); 28719783aa99SChris Down 28729783aa99SChris Down /* 28731bc63fb1SChris Down * Minimally target SWAP_CLUSTER_MAX pages to keep 287455b65a57SEthon Paul * reclaim moving forwards, avoiding decrementing 28759de7ca46SChris Down * sc->priority further than desirable. 28769783aa99SChris Down */ 28771bc63fb1SChris Down scan = max(scan, SWAP_CLUSTER_MAX); 28789783aa99SChris Down } else { 28799783aa99SChris Down scan = lruvec_size; 28809783aa99SChris Down } 28819783aa99SChris Down 28829783aa99SChris Down scan >>= sc->priority; 28839783aa99SChris Down 2884688035f7SJohannes Weiner /* 2885688035f7SJohannes Weiner * If the cgroup's already been deleted, make sure to 2886688035f7SJohannes Weiner * scrape out the remaining cache. 2887688035f7SJohannes Weiner */ 2888688035f7SJohannes Weiner if (!scan && !mem_cgroup_online(memcg)) 28899783aa99SChris Down scan = min(lruvec_size, SWAP_CLUSTER_MAX); 28909a265114SJohannes Weiner 28919a265114SJohannes Weiner switch (scan_balance) { 28929a265114SJohannes Weiner case SCAN_EQUAL: 28939a265114SJohannes Weiner /* Scan lists relative to size */ 28949a265114SJohannes Weiner break; 28959a265114SJohannes Weiner case SCAN_FRACT: 28969a265114SJohannes Weiner /* 28979a265114SJohannes Weiner * Scan types proportional to swappiness and 28989a265114SJohannes Weiner * their relative recent reclaim efficiency. 289976073c64SGavin Shan * Make sure we don't miss the last page on 290076073c64SGavin Shan * the offlined memory cgroups because of a 290176073c64SGavin Shan * round-off error. 29029a265114SJohannes Weiner */ 290376073c64SGavin Shan scan = mem_cgroup_online(memcg) ? 290476073c64SGavin Shan div64_u64(scan * fraction[file], denominator) : 290576073c64SGavin Shan DIV64_U64_ROUND_UP(scan * fraction[file], 29066f04f48dSSuleiman Souhlal denominator); 29079a265114SJohannes Weiner break; 29089a265114SJohannes Weiner case SCAN_FILE: 29099a265114SJohannes Weiner case SCAN_ANON: 29109a265114SJohannes Weiner /* Scan one type exclusively */ 2911e072bff6SMateusz Nosek if ((scan_balance == SCAN_FILE) != file) 29129a265114SJohannes Weiner scan = 0; 29139a265114SJohannes Weiner break; 29149a265114SJohannes Weiner default: 29159a265114SJohannes Weiner /* Look ma, no brain */ 29169a265114SJohannes Weiner BUG(); 29179a265114SJohannes Weiner } 29186b4f7799SJohannes Weiner 29194111304dSHugh Dickins nr[lru] = scan; 292076a33fc3SShaohua Li } 29216e08a369SWu Fengguang } 29224f98a2feSRik van Riel 29232f368a9fSDave Hansen /* 29242f368a9fSDave Hansen * Anonymous LRU management is a waste if there is 29252f368a9fSDave Hansen * ultimately no way to reclaim the memory. 29262f368a9fSDave Hansen */ 29272f368a9fSDave Hansen static bool can_age_anon_pages(struct pglist_data *pgdat, 29282f368a9fSDave Hansen struct scan_control *sc) 29292f368a9fSDave Hansen { 29302f368a9fSDave Hansen /* Aging the anon LRU is valuable if swap is present: */ 29312f368a9fSDave Hansen if (total_swap_pages > 0) 29322f368a9fSDave Hansen return true; 29332f368a9fSDave Hansen 29342f368a9fSDave Hansen /* Also valuable if anon pages can be demoted: */ 29352f368a9fSDave Hansen return can_demote(pgdat->node_id, sc); 29362f368a9fSDave Hansen } 29372f368a9fSDave Hansen 2938afaf07a6SJohannes Weiner static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) 29399b4f98cdSJohannes Weiner { 29409b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 2941e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 29429b4f98cdSJohannes Weiner unsigned long nr_to_scan; 29439b4f98cdSJohannes Weiner enum lru_list lru; 29449b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 29459b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 29469b4f98cdSJohannes Weiner struct blk_plug plug; 29471a501907SMel Gorman bool scan_adjusted; 29489b4f98cdSJohannes Weiner 2949afaf07a6SJohannes Weiner get_scan_count(lruvec, sc, nr); 29509b4f98cdSJohannes Weiner 2951e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 2952e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 2953e82e0561SMel Gorman 29541a501907SMel Gorman /* 29551a501907SMel Gorman * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal 29561a501907SMel Gorman * event that can occur when there is little memory pressure e.g. 29571a501907SMel Gorman * multiple streaming readers/writers. Hence, we do not abort scanning 29581a501907SMel Gorman * when the requested number of pages are reclaimed when scanning at 29591a501907SMel Gorman * DEF_PRIORITY on the assumption that the fact we are direct 29601a501907SMel Gorman * reclaiming implies that kswapd is not keeping up and it is best to 29611a501907SMel Gorman * do a batch of work at once. For memcg reclaim one check is made to 29621a501907SMel Gorman * abort proportional reclaim if either the file or anon lru has already 29631a501907SMel Gorman * dropped to zero at the first pass. 29641a501907SMel Gorman */ 2965b5ead35eSJohannes Weiner scan_adjusted = (!cgroup_reclaim(sc) && !current_is_kswapd() && 29661a501907SMel Gorman sc->priority == DEF_PRIORITY); 29671a501907SMel Gorman 29689b4f98cdSJohannes Weiner blk_start_plug(&plug); 29699b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 29709b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 2971e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 2972e82e0561SMel Gorman unsigned long nr_scanned; 2973e82e0561SMel Gorman 29749b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 29759b4f98cdSJohannes Weiner if (nr[lru]) { 29769b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 29779b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 29789b4f98cdSJohannes Weiner 29799b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 29803b991208SJohannes Weiner lruvec, sc); 29819b4f98cdSJohannes Weiner } 29829b4f98cdSJohannes Weiner } 2983e82e0561SMel Gorman 2984bd041733SMichal Hocko cond_resched(); 2985bd041733SMichal Hocko 2986e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 2987e82e0561SMel Gorman continue; 2988e82e0561SMel Gorman 29899b4f98cdSJohannes Weiner /* 2990e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 29911a501907SMel Gorman * requested. Ensure that the anon and file LRUs are scanned 2992e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 2993e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 2994e82e0561SMel Gorman * proportional to the original scan target. 2995e82e0561SMel Gorman */ 2996e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 2997e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 2998e82e0561SMel Gorman 29991a501907SMel Gorman /* 30001a501907SMel Gorman * It's just vindictive to attack the larger once the smaller 30011a501907SMel Gorman * has gone to zero. And given the way we stop scanning the 30021a501907SMel Gorman * smaller below, this makes sure that we only make one nudge 30031a501907SMel Gorman * towards proportionality once we've got nr_to_reclaim. 30041a501907SMel Gorman */ 30051a501907SMel Gorman if (!nr_file || !nr_anon) 30061a501907SMel Gorman break; 30071a501907SMel Gorman 3008e82e0561SMel Gorman if (nr_file > nr_anon) { 3009e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 3010e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 3011e82e0561SMel Gorman lru = LRU_BASE; 3012e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 3013e82e0561SMel Gorman } else { 3014e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 3015e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 3016e82e0561SMel Gorman lru = LRU_FILE; 3017e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 3018e82e0561SMel Gorman } 3019e82e0561SMel Gorman 3020e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 3021e82e0561SMel Gorman nr[lru] = 0; 3022e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 3023e82e0561SMel Gorman 3024e82e0561SMel Gorman /* 3025e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 3026e82e0561SMel Gorman * scan target and the percentage scanning already complete 3027e82e0561SMel Gorman */ 3028e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 3029e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 3030e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 3031e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 3032e82e0561SMel Gorman 3033e82e0561SMel Gorman lru += LRU_ACTIVE; 3034e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 3035e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 3036e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 3037e82e0561SMel Gorman 3038e82e0561SMel Gorman scan_adjusted = true; 30399b4f98cdSJohannes Weiner } 30409b4f98cdSJohannes Weiner blk_finish_plug(&plug); 30419b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 30429b4f98cdSJohannes Weiner 30439b4f98cdSJohannes Weiner /* 30449b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 30459b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 30469b4f98cdSJohannes Weiner */ 30472f368a9fSDave Hansen if (can_age_anon_pages(lruvec_pgdat(lruvec), sc) && 30482f368a9fSDave Hansen inactive_is_low(lruvec, LRU_INACTIVE_ANON)) 30499b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 30509b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 30519b4f98cdSJohannes Weiner } 30529b4f98cdSJohannes Weiner 305323b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 30549e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 305523b9da55SMel Gorman { 3056d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 305723b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 30589e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 305923b9da55SMel Gorman return true; 306023b9da55SMel Gorman 306123b9da55SMel Gorman return false; 306223b9da55SMel Gorman } 306323b9da55SMel Gorman 30644f98a2feSRik van Riel /* 306523b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 306623b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 306723b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 3068df3a45f9SQiwu Chen * calls try_to_compact_pages() that it will have enough free pages to succeed. 306923b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 30703e7d3449SMel Gorman */ 3071a9dd0a83SMel Gorman static inline bool should_continue_reclaim(struct pglist_data *pgdat, 30723e7d3449SMel Gorman unsigned long nr_reclaimed, 30733e7d3449SMel Gorman struct scan_control *sc) 30743e7d3449SMel Gorman { 30753e7d3449SMel Gorman unsigned long pages_for_compaction; 30763e7d3449SMel Gorman unsigned long inactive_lru_pages; 3077a9dd0a83SMel Gorman int z; 30783e7d3449SMel Gorman 30793e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 30809e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 30813e7d3449SMel Gorman return false; 30823e7d3449SMel Gorman 30833e7d3449SMel Gorman /* 30845ee04716SVlastimil Babka * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX 30855ee04716SVlastimil Babka * number of pages that were scanned. This will return to the caller 30865ee04716SVlastimil Babka * with the risk reclaim/compaction and the resulting allocation attempt 30875ee04716SVlastimil Babka * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL 30885ee04716SVlastimil Babka * allocations through requiring that the full LRU list has been scanned 30895ee04716SVlastimil Babka * first, by assuming that zero delta of sc->nr_scanned means full LRU 30905ee04716SVlastimil Babka * scan, but that approximation was wrong, and there were corner cases 30915ee04716SVlastimil Babka * where always a non-zero amount of pages were scanned. 30922876592fSMel Gorman */ 30932876592fSMel Gorman if (!nr_reclaimed) 30942876592fSMel Gorman return false; 30953e7d3449SMel Gorman 30963e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 3097a9dd0a83SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 3098a9dd0a83SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 30996aa303deSMel Gorman if (!managed_zone(zone)) 3100a9dd0a83SMel Gorman continue; 3101a9dd0a83SMel Gorman 3102a9dd0a83SMel Gorman switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) { 3103cf378319SVlastimil Babka case COMPACT_SUCCESS: 31043e7d3449SMel Gorman case COMPACT_CONTINUE: 31053e7d3449SMel Gorman return false; 31063e7d3449SMel Gorman default: 3107a9dd0a83SMel Gorman /* check next zone */ 3108a9dd0a83SMel Gorman ; 31093e7d3449SMel Gorman } 31103e7d3449SMel Gorman } 31111c6c1597SHillf Danton 31121c6c1597SHillf Danton /* 31131c6c1597SHillf Danton * If we have not reclaimed enough pages for compaction and the 31141c6c1597SHillf Danton * inactive lists are large enough, continue reclaiming 31151c6c1597SHillf Danton */ 31161c6c1597SHillf Danton pages_for_compaction = compact_gap(sc->order); 31171c6c1597SHillf Danton inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); 3118a2a36488SKeith Busch if (can_reclaim_anon_pages(NULL, pgdat->node_id, sc)) 31191c6c1597SHillf Danton inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); 31201c6c1597SHillf Danton 31215ee04716SVlastimil Babka return inactive_lru_pages > pages_for_compaction; 3122a9dd0a83SMel Gorman } 31233e7d3449SMel Gorman 31240f6a5cffSJohannes Weiner static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc) 3125f16015fbSJohannes Weiner { 31260f6a5cffSJohannes Weiner struct mem_cgroup *target_memcg = sc->target_mem_cgroup; 3127694fbc0fSAndrew Morton struct mem_cgroup *memcg; 3128d108c772SAndrey Ryabinin 31290f6a5cffSJohannes Weiner memcg = mem_cgroup_iter(target_memcg, NULL, NULL); 3130694fbc0fSAndrew Morton do { 3131afaf07a6SJohannes Weiner struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); 31328e8ae645SJohannes Weiner unsigned long reclaimed; 3133cb731d6cSVladimir Davydov unsigned long scanned; 31349b4f98cdSJohannes Weiner 3135e3336cabSXunlei Pang /* 3136e3336cabSXunlei Pang * This loop can become CPU-bound when target memcgs 3137e3336cabSXunlei Pang * aren't eligible for reclaim - either because they 3138e3336cabSXunlei Pang * don't have any reclaimable pages, or because their 3139e3336cabSXunlei Pang * memory is explicitly protected. Avoid soft lockups. 3140e3336cabSXunlei Pang */ 3141e3336cabSXunlei Pang cond_resched(); 3142e3336cabSXunlei Pang 314345c7f7e1SChris Down mem_cgroup_calculate_protection(target_memcg, memcg); 314445c7f7e1SChris Down 314545c7f7e1SChris Down if (mem_cgroup_below_min(memcg)) { 3146bf8d5d52SRoman Gushchin /* 3147bf8d5d52SRoman Gushchin * Hard protection. 3148bf8d5d52SRoman Gushchin * If there is no reclaimable memory, OOM. 3149bf8d5d52SRoman Gushchin */ 3150bf8d5d52SRoman Gushchin continue; 315145c7f7e1SChris Down } else if (mem_cgroup_below_low(memcg)) { 3152bf8d5d52SRoman Gushchin /* 3153bf8d5d52SRoman Gushchin * Soft protection. 3154bf8d5d52SRoman Gushchin * Respect the protection only as long as 3155bf8d5d52SRoman Gushchin * there is an unprotected supply 3156bf8d5d52SRoman Gushchin * of reclaimable memory from other cgroups. 3157bf8d5d52SRoman Gushchin */ 3158d6622f63SYisheng Xie if (!sc->memcg_low_reclaim) { 3159d6622f63SYisheng Xie sc->memcg_low_skipped = 1; 3160241994edSJohannes Weiner continue; 3161d6622f63SYisheng Xie } 3162e27be240SJohannes Weiner memcg_memory_event(memcg, MEMCG_LOW); 3163241994edSJohannes Weiner } 3164241994edSJohannes Weiner 31658e8ae645SJohannes Weiner reclaimed = sc->nr_reclaimed; 3166cb731d6cSVladimir Davydov scanned = sc->nr_scanned; 3167afaf07a6SJohannes Weiner 3168afaf07a6SJohannes Weiner shrink_lruvec(lruvec, sc); 3169f9be23d6SKonstantin Khlebnikov 317028360f39SMel Gorman shrink_slab(sc->gfp_mask, pgdat->node_id, memcg, 317128360f39SMel Gorman sc->priority); 3172cb731d6cSVladimir Davydov 31738e8ae645SJohannes Weiner /* Record the group's reclaim efficiency */ 31748e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, memcg, false, 31758e8ae645SJohannes Weiner sc->nr_scanned - scanned, 31768e8ae645SJohannes Weiner sc->nr_reclaimed - reclaimed); 31778e8ae645SJohannes Weiner 31780f6a5cffSJohannes Weiner } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL))); 31790f6a5cffSJohannes Weiner } 31800f6a5cffSJohannes Weiner 31816c9e0907SLiu Song static void shrink_node(pg_data_t *pgdat, struct scan_control *sc) 31820f6a5cffSJohannes Weiner { 31830f6a5cffSJohannes Weiner struct reclaim_state *reclaim_state = current->reclaim_state; 31840f6a5cffSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 31851b05117dSJohannes Weiner struct lruvec *target_lruvec; 31860f6a5cffSJohannes Weiner bool reclaimable = false; 3187b91ac374SJohannes Weiner unsigned long file; 31880f6a5cffSJohannes Weiner 31891b05117dSJohannes Weiner target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat); 31901b05117dSJohannes Weiner 31910f6a5cffSJohannes Weiner again: 3192aa48e47eSShakeel Butt /* 3193aa48e47eSShakeel Butt * Flush the memory cgroup stats, so that we read accurate per-memcg 3194aa48e47eSShakeel Butt * lruvec stats for heuristics. 3195aa48e47eSShakeel Butt */ 3196aa48e47eSShakeel Butt mem_cgroup_flush_stats(); 3197aa48e47eSShakeel Butt 31980f6a5cffSJohannes Weiner memset(&sc->nr, 0, sizeof(sc->nr)); 31990f6a5cffSJohannes Weiner 32000f6a5cffSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 32010f6a5cffSJohannes Weiner nr_scanned = sc->nr_scanned; 32020f6a5cffSJohannes Weiner 320353138ceaSJohannes Weiner /* 32047cf111bcSJohannes Weiner * Determine the scan balance between anon and file LRUs. 32057cf111bcSJohannes Weiner */ 32066168d0daSAlex Shi spin_lock_irq(&target_lruvec->lru_lock); 32077cf111bcSJohannes Weiner sc->anon_cost = target_lruvec->anon_cost; 32087cf111bcSJohannes Weiner sc->file_cost = target_lruvec->file_cost; 32096168d0daSAlex Shi spin_unlock_irq(&target_lruvec->lru_lock); 32107cf111bcSJohannes Weiner 32117cf111bcSJohannes Weiner /* 3212b91ac374SJohannes Weiner * Target desirable inactive:active list ratios for the anon 3213b91ac374SJohannes Weiner * and file LRU lists. 3214b91ac374SJohannes Weiner */ 3215b91ac374SJohannes Weiner if (!sc->force_deactivate) { 3216b91ac374SJohannes Weiner unsigned long refaults; 3217b91ac374SJohannes Weiner 3218170b04b7SJoonsoo Kim refaults = lruvec_page_state(target_lruvec, 3219170b04b7SJoonsoo Kim WORKINGSET_ACTIVATE_ANON); 3220170b04b7SJoonsoo Kim if (refaults != target_lruvec->refaults[0] || 3221170b04b7SJoonsoo Kim inactive_is_low(target_lruvec, LRU_INACTIVE_ANON)) 3222b91ac374SJohannes Weiner sc->may_deactivate |= DEACTIVATE_ANON; 3223b91ac374SJohannes Weiner else 3224b91ac374SJohannes Weiner sc->may_deactivate &= ~DEACTIVATE_ANON; 3225b91ac374SJohannes Weiner 3226b91ac374SJohannes Weiner /* 3227b91ac374SJohannes Weiner * When refaults are being observed, it means a new 3228b91ac374SJohannes Weiner * workingset is being established. Deactivate to get 3229b91ac374SJohannes Weiner * rid of any stale active pages quickly. 3230b91ac374SJohannes Weiner */ 3231b91ac374SJohannes Weiner refaults = lruvec_page_state(target_lruvec, 3232170b04b7SJoonsoo Kim WORKINGSET_ACTIVATE_FILE); 3233170b04b7SJoonsoo Kim if (refaults != target_lruvec->refaults[1] || 3234b91ac374SJohannes Weiner inactive_is_low(target_lruvec, LRU_INACTIVE_FILE)) 3235b91ac374SJohannes Weiner sc->may_deactivate |= DEACTIVATE_FILE; 3236b91ac374SJohannes Weiner else 3237b91ac374SJohannes Weiner sc->may_deactivate &= ~DEACTIVATE_FILE; 3238b91ac374SJohannes Weiner } else 3239b91ac374SJohannes Weiner sc->may_deactivate = DEACTIVATE_ANON | DEACTIVATE_FILE; 3240b91ac374SJohannes Weiner 3241b91ac374SJohannes Weiner /* 3242b91ac374SJohannes Weiner * If we have plenty of inactive file pages that aren't 3243b91ac374SJohannes Weiner * thrashing, try to reclaim those first before touching 3244b91ac374SJohannes Weiner * anonymous pages. 3245b91ac374SJohannes Weiner */ 3246b91ac374SJohannes Weiner file = lruvec_page_state(target_lruvec, NR_INACTIVE_FILE); 3247b91ac374SJohannes Weiner if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE)) 3248b91ac374SJohannes Weiner sc->cache_trim_mode = 1; 3249b91ac374SJohannes Weiner else 3250b91ac374SJohannes Weiner sc->cache_trim_mode = 0; 3251b91ac374SJohannes Weiner 3252b91ac374SJohannes Weiner /* 325353138ceaSJohannes Weiner * Prevent the reclaimer from falling into the cache trap: as 325453138ceaSJohannes Weiner * cache pages start out inactive, every cache fault will tip 325553138ceaSJohannes Weiner * the scan balance towards the file LRU. And as the file LRU 325653138ceaSJohannes Weiner * shrinks, so does the window for rotation from references. 325753138ceaSJohannes Weiner * This means we have a runaway feedback loop where a tiny 325853138ceaSJohannes Weiner * thrashing file LRU becomes infinitely more attractive than 325953138ceaSJohannes Weiner * anon pages. Try to detect this based on file LRU size. 326053138ceaSJohannes Weiner */ 326153138ceaSJohannes Weiner if (!cgroup_reclaim(sc)) { 326253138ceaSJohannes Weiner unsigned long total_high_wmark = 0; 3263b91ac374SJohannes Weiner unsigned long free, anon; 3264b91ac374SJohannes Weiner int z; 326553138ceaSJohannes Weiner 326653138ceaSJohannes Weiner free = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); 326753138ceaSJohannes Weiner file = node_page_state(pgdat, NR_ACTIVE_FILE) + 326853138ceaSJohannes Weiner node_page_state(pgdat, NR_INACTIVE_FILE); 326953138ceaSJohannes Weiner 327053138ceaSJohannes Weiner for (z = 0; z < MAX_NR_ZONES; z++) { 327153138ceaSJohannes Weiner struct zone *zone = &pgdat->node_zones[z]; 327253138ceaSJohannes Weiner if (!managed_zone(zone)) 327353138ceaSJohannes Weiner continue; 327453138ceaSJohannes Weiner 327553138ceaSJohannes Weiner total_high_wmark += high_wmark_pages(zone); 327653138ceaSJohannes Weiner } 327753138ceaSJohannes Weiner 3278b91ac374SJohannes Weiner /* 3279b91ac374SJohannes Weiner * Consider anon: if that's low too, this isn't a 3280b91ac374SJohannes Weiner * runaway file reclaim problem, but rather just 3281b91ac374SJohannes Weiner * extreme pressure. Reclaim as per usual then. 3282b91ac374SJohannes Weiner */ 3283b91ac374SJohannes Weiner anon = node_page_state(pgdat, NR_INACTIVE_ANON); 3284b91ac374SJohannes Weiner 3285b91ac374SJohannes Weiner sc->file_is_tiny = 3286b91ac374SJohannes Weiner file + free <= total_high_wmark && 3287b91ac374SJohannes Weiner !(sc->may_deactivate & DEACTIVATE_ANON) && 3288b91ac374SJohannes Weiner anon >> sc->priority; 328953138ceaSJohannes Weiner } 329053138ceaSJohannes Weiner 32910f6a5cffSJohannes Weiner shrink_node_memcgs(pgdat, sc); 329270ddf637SAnton Vorontsov 32936b4f7799SJohannes Weiner if (reclaim_state) { 3294cb731d6cSVladimir Davydov sc->nr_reclaimed += reclaim_state->reclaimed_slab; 32956b4f7799SJohannes Weiner reclaim_state->reclaimed_slab = 0; 32966b4f7799SJohannes Weiner } 32976b4f7799SJohannes Weiner 32988e8ae645SJohannes Weiner /* Record the subtree's reclaim efficiency */ 32991b05117dSJohannes Weiner vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, 330070ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 330170ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 330270ddf637SAnton Vorontsov 33032344d7e4SJohannes Weiner if (sc->nr_reclaimed - nr_reclaimed) 33042344d7e4SJohannes Weiner reclaimable = true; 33052344d7e4SJohannes Weiner 3306e3c1ac58SAndrey Ryabinin if (current_is_kswapd()) { 3307d108c772SAndrey Ryabinin /* 3308e3c1ac58SAndrey Ryabinin * If reclaim is isolating dirty pages under writeback, 3309e3c1ac58SAndrey Ryabinin * it implies that the long-lived page allocation rate 3310e3c1ac58SAndrey Ryabinin * is exceeding the page laundering rate. Either the 3311e3c1ac58SAndrey Ryabinin * global limits are not being effective at throttling 3312e3c1ac58SAndrey Ryabinin * processes due to the page distribution throughout 3313e3c1ac58SAndrey Ryabinin * zones or there is heavy usage of a slow backing 3314e3c1ac58SAndrey Ryabinin * device. The only option is to throttle from reclaim 3315e3c1ac58SAndrey Ryabinin * context which is not ideal as there is no guarantee 3316d108c772SAndrey Ryabinin * the dirtying process is throttled in the same way 3317d108c772SAndrey Ryabinin * balance_dirty_pages() manages. 3318d108c772SAndrey Ryabinin * 3319e3c1ac58SAndrey Ryabinin * Once a node is flagged PGDAT_WRITEBACK, kswapd will 3320e3c1ac58SAndrey Ryabinin * count the number of pages under pages flagged for 3321e3c1ac58SAndrey Ryabinin * immediate reclaim and stall if any are encountered 3322e3c1ac58SAndrey Ryabinin * in the nr_immediate check below. 3323d108c772SAndrey Ryabinin */ 3324d108c772SAndrey Ryabinin if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken) 3325d108c772SAndrey Ryabinin set_bit(PGDAT_WRITEBACK, &pgdat->flags); 3326d108c772SAndrey Ryabinin 3327d108c772SAndrey Ryabinin /* Allow kswapd to start writing pages during reclaim.*/ 3328d108c772SAndrey Ryabinin if (sc->nr.unqueued_dirty == sc->nr.file_taken) 3329d108c772SAndrey Ryabinin set_bit(PGDAT_DIRTY, &pgdat->flags); 3330d108c772SAndrey Ryabinin 3331d108c772SAndrey Ryabinin /* 33321eba09c1SRandy Dunlap * If kswapd scans pages marked for immediate 3333d108c772SAndrey Ryabinin * reclaim and under writeback (nr_immediate), it 3334d108c772SAndrey Ryabinin * implies that pages are cycling through the LRU 33358cd7c588SMel Gorman * faster than they are written so forcibly stall 33368cd7c588SMel Gorman * until some pages complete writeback. 3337d108c772SAndrey Ryabinin */ 3338d108c772SAndrey Ryabinin if (sc->nr.immediate) 3339c3f4a9a2SMel Gorman reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK); 3340d108c772SAndrey Ryabinin } 3341d108c772SAndrey Ryabinin 3342d108c772SAndrey Ryabinin /* 33438cd7c588SMel Gorman * Tag a node/memcg as congested if all the dirty pages were marked 33448cd7c588SMel Gorman * for writeback and immediate reclaim (counted in nr.congested). 33451b05117dSJohannes Weiner * 3346e3c1ac58SAndrey Ryabinin * Legacy memcg will stall in page writeback so avoid forcibly 33478cd7c588SMel Gorman * stalling in reclaim_throttle(). 3348e3c1ac58SAndrey Ryabinin */ 33491b05117dSJohannes Weiner if ((current_is_kswapd() || 33501b05117dSJohannes Weiner (cgroup_reclaim(sc) && writeback_throttling_sane(sc))) && 3351e3c1ac58SAndrey Ryabinin sc->nr.dirty && sc->nr.dirty == sc->nr.congested) 33521b05117dSJohannes Weiner set_bit(LRUVEC_CONGESTED, &target_lruvec->flags); 3353e3c1ac58SAndrey Ryabinin 3354e3c1ac58SAndrey Ryabinin /* 33558cd7c588SMel Gorman * Stall direct reclaim for IO completions if the lruvec is 33568cd7c588SMel Gorman * node is congested. Allow kswapd to continue until it 3357d108c772SAndrey Ryabinin * starts encountering unqueued dirty pages or cycling through 3358d108c772SAndrey Ryabinin * the LRU too quickly. 3359d108c772SAndrey Ryabinin */ 33601b05117dSJohannes Weiner if (!current_is_kswapd() && current_may_throttle() && 33611b05117dSJohannes Weiner !sc->hibernation_mode && 33621b05117dSJohannes Weiner test_bit(LRUVEC_CONGESTED, &target_lruvec->flags)) 33631b4e3f26SMel Gorman reclaim_throttle(pgdat, VMSCAN_THROTTLE_CONGESTED); 3364d108c772SAndrey Ryabinin 3365d2af3397SJohannes Weiner if (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, 3366d2af3397SJohannes Weiner sc)) 3367d2af3397SJohannes Weiner goto again; 33682344d7e4SJohannes Weiner 3369c73322d0SJohannes Weiner /* 3370c73322d0SJohannes Weiner * Kswapd gives up on balancing particular nodes after too 3371c73322d0SJohannes Weiner * many failures to reclaim anything from them and goes to 3372c73322d0SJohannes Weiner * sleep. On reclaim progress, reset the failure counter. A 3373c73322d0SJohannes Weiner * successful direct reclaim run will revive a dormant kswapd. 3374c73322d0SJohannes Weiner */ 3375c73322d0SJohannes Weiner if (reclaimable) 3376c73322d0SJohannes Weiner pgdat->kswapd_failures = 0; 3377f16015fbSJohannes Weiner } 3378f16015fbSJohannes Weiner 337953853e2dSVlastimil Babka /* 3380fdd4c614SVlastimil Babka * Returns true if compaction should go ahead for a costly-order request, or 3381fdd4c614SVlastimil Babka * the allocation would already succeed without compaction. Return false if we 3382fdd4c614SVlastimil Babka * should reclaim first. 338353853e2dSVlastimil Babka */ 33844f588331SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 3385fe4b1b24SMel Gorman { 338631483b6aSMel Gorman unsigned long watermark; 3387fdd4c614SVlastimil Babka enum compact_result suitable; 3388fe4b1b24SMel Gorman 3389fdd4c614SVlastimil Babka suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx); 3390fdd4c614SVlastimil Babka if (suitable == COMPACT_SUCCESS) 3391fdd4c614SVlastimil Babka /* Allocation should succeed already. Don't reclaim. */ 3392fdd4c614SVlastimil Babka return true; 3393fdd4c614SVlastimil Babka if (suitable == COMPACT_SKIPPED) 3394fdd4c614SVlastimil Babka /* Compaction cannot yet proceed. Do reclaim. */ 3395fe4b1b24SMel Gorman return false; 3396fe4b1b24SMel Gorman 3397fdd4c614SVlastimil Babka /* 3398fdd4c614SVlastimil Babka * Compaction is already possible, but it takes time to run and there 3399fdd4c614SVlastimil Babka * are potentially other callers using the pages just freed. So proceed 3400fdd4c614SVlastimil Babka * with reclaim to make a buffer of free pages available to give 3401fdd4c614SVlastimil Babka * compaction a reasonable chance of completing and allocating the page. 3402fdd4c614SVlastimil Babka * Note that we won't actually reclaim the whole buffer in one attempt 3403fdd4c614SVlastimil Babka * as the target watermark in should_continue_reclaim() is lower. But if 3404fdd4c614SVlastimil Babka * we are already above the high+gap watermark, don't reclaim at all. 3405fdd4c614SVlastimil Babka */ 3406fdd4c614SVlastimil Babka watermark = high_wmark_pages(zone) + compact_gap(sc->order); 3407fdd4c614SVlastimil Babka 3408fdd4c614SVlastimil Babka return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); 3409fe4b1b24SMel Gorman } 3410fe4b1b24SMel Gorman 341169392a40SMel Gorman static void consider_reclaim_throttle(pg_data_t *pgdat, struct scan_control *sc) 341269392a40SMel Gorman { 341366ce520bSMel Gorman /* 341466ce520bSMel Gorman * If reclaim is making progress greater than 12% efficiency then 341566ce520bSMel Gorman * wake all the NOPROGRESS throttled tasks. 341666ce520bSMel Gorman */ 341766ce520bSMel Gorman if (sc->nr_reclaimed > (sc->nr_scanned >> 3)) { 341869392a40SMel Gorman wait_queue_head_t *wqh; 341969392a40SMel Gorman 342069392a40SMel Gorman wqh = &pgdat->reclaim_wait[VMSCAN_THROTTLE_NOPROGRESS]; 342169392a40SMel Gorman if (waitqueue_active(wqh)) 342269392a40SMel Gorman wake_up(wqh); 342369392a40SMel Gorman 342469392a40SMel Gorman return; 342569392a40SMel Gorman } 342669392a40SMel Gorman 342769392a40SMel Gorman /* 34281b4e3f26SMel Gorman * Do not throttle kswapd or cgroup reclaim on NOPROGRESS as it will 34291b4e3f26SMel Gorman * throttle on VMSCAN_THROTTLE_WRITEBACK if there are too many pages 34301b4e3f26SMel Gorman * under writeback and marked for immediate reclaim at the tail of the 34311b4e3f26SMel Gorman * LRU. 343269392a40SMel Gorman */ 34331b4e3f26SMel Gorman if (current_is_kswapd() || cgroup_reclaim(sc)) 343469392a40SMel Gorman return; 343569392a40SMel Gorman 343669392a40SMel Gorman /* Throttle if making no progress at high prioities. */ 34371b4e3f26SMel Gorman if (sc->priority == 1 && !sc->nr_reclaimed) 3438c3f4a9a2SMel Gorman reclaim_throttle(pgdat, VMSCAN_THROTTLE_NOPROGRESS); 343969392a40SMel Gorman } 344069392a40SMel Gorman 34411da177e4SLinus Torvalds /* 34421da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 34431da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 34441da177e4SLinus Torvalds * request. 34451da177e4SLinus Torvalds * 34461da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 34471da177e4SLinus Torvalds * scan then give up on it. 34481da177e4SLinus Torvalds */ 34490a0337e0SMichal Hocko static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 34501da177e4SLinus Torvalds { 3451dd1a239fSMel Gorman struct zoneref *z; 345254a6eb5cSMel Gorman struct zone *zone; 34530608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 34540608f43dSAndrew Morton unsigned long nr_soft_scanned; 3455619d0d76SWeijie Yang gfp_t orig_mask; 345679dafcdcSMel Gorman pg_data_t *last_pgdat = NULL; 34571b4e3f26SMel Gorman pg_data_t *first_pgdat = NULL; 34581cfb419bSKAMEZAWA Hiroyuki 3459cc715d99SMel Gorman /* 3460cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 3461cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 3462cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 3463cc715d99SMel Gorman */ 3464619d0d76SWeijie Yang orig_mask = sc->gfp_mask; 3465b2e18757SMel Gorman if (buffer_heads_over_limit) { 3466cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 34674f588331SMel Gorman sc->reclaim_idx = gfp_zone(sc->gfp_mask); 3468b2e18757SMel Gorman } 3469cc715d99SMel Gorman 3470d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 3471b2e18757SMel Gorman sc->reclaim_idx, sc->nodemask) { 3472b2e18757SMel Gorman /* 34731cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 34741cfb419bSKAMEZAWA Hiroyuki * to global LRU. 34751cfb419bSKAMEZAWA Hiroyuki */ 3476b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) { 3477344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, 3478344736f2SVladimir Davydov GFP_KERNEL | __GFP_HARDWALL)) 34791da177e4SLinus Torvalds continue; 348065ec02cbSVladimir Davydov 3481e0887c19SRik van Riel /* 3482e0c23279SMel Gorman * If we already have plenty of memory free for 3483e0c23279SMel Gorman * compaction in this zone, don't free any more. 3484e0c23279SMel Gorman * Even though compaction is invoked for any 3485e0c23279SMel Gorman * non-zero order, only frequent costly order 3486e0c23279SMel Gorman * reclamation is disruptive enough to become a 3487c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 3488c7cfa37bSCopot Alexandru * page allocations. 3489e0887c19SRik van Riel */ 34900b06496aSJohannes Weiner if (IS_ENABLED(CONFIG_COMPACTION) && 34910b06496aSJohannes Weiner sc->order > PAGE_ALLOC_COSTLY_ORDER && 34924f588331SMel Gorman compaction_ready(zone, sc)) { 34930b06496aSJohannes Weiner sc->compaction_ready = true; 3494e0887c19SRik van Riel continue; 3495e0887c19SRik van Riel } 34960b06496aSJohannes Weiner 34970608f43dSAndrew Morton /* 349879dafcdcSMel Gorman * Shrink each node in the zonelist once. If the 349979dafcdcSMel Gorman * zonelist is ordered by zone (not the default) then a 350079dafcdcSMel Gorman * node may be shrunk multiple times but in that case 350179dafcdcSMel Gorman * the user prefers lower zones being preserved. 350279dafcdcSMel Gorman */ 350379dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 350479dafcdcSMel Gorman continue; 350579dafcdcSMel Gorman 350679dafcdcSMel Gorman /* 35070608f43dSAndrew Morton * This steals pages from memory cgroups over softlimit 35080608f43dSAndrew Morton * and returns the number of reclaimed pages and 35090608f43dSAndrew Morton * scanned pages. This works for global memory pressure 35100608f43dSAndrew Morton * and balancing, not for a memcg's limit. 35110608f43dSAndrew Morton */ 35120608f43dSAndrew Morton nr_soft_scanned = 0; 3513ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, 35140608f43dSAndrew Morton sc->order, sc->gfp_mask, 35150608f43dSAndrew Morton &nr_soft_scanned); 35160608f43dSAndrew Morton sc->nr_reclaimed += nr_soft_reclaimed; 35170608f43dSAndrew Morton sc->nr_scanned += nr_soft_scanned; 3518ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 3519ac34a1a3SKAMEZAWA Hiroyuki } 3520d149e3b2SYing Han 35211b4e3f26SMel Gorman if (!first_pgdat) 35221b4e3f26SMel Gorman first_pgdat = zone->zone_pgdat; 35231b4e3f26SMel Gorman 352479dafcdcSMel Gorman /* See comment about same check for global reclaim above */ 352579dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 352679dafcdcSMel Gorman continue; 352779dafcdcSMel Gorman last_pgdat = zone->zone_pgdat; 3528970a39a3SMel Gorman shrink_node(zone->zone_pgdat, sc); 35291da177e4SLinus Torvalds } 3530e0c23279SMel Gorman 353180082938SMel Gorman if (first_pgdat) 35321b4e3f26SMel Gorman consider_reclaim_throttle(first_pgdat, sc); 35331b4e3f26SMel Gorman 353465ec02cbSVladimir Davydov /* 3535619d0d76SWeijie Yang * Restore to original mask to avoid the impact on the caller if we 3536619d0d76SWeijie Yang * promoted it to __GFP_HIGHMEM. 3537619d0d76SWeijie Yang */ 3538619d0d76SWeijie Yang sc->gfp_mask = orig_mask; 35391da177e4SLinus Torvalds } 35401da177e4SLinus Torvalds 3541b910718aSJohannes Weiner static void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat) 35422a2e4885SJohannes Weiner { 3543b910718aSJohannes Weiner struct lruvec *target_lruvec; 35442a2e4885SJohannes Weiner unsigned long refaults; 35452a2e4885SJohannes Weiner 3546b910718aSJohannes Weiner target_lruvec = mem_cgroup_lruvec(target_memcg, pgdat); 3547170b04b7SJoonsoo Kim refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_ANON); 3548170b04b7SJoonsoo Kim target_lruvec->refaults[0] = refaults; 3549170b04b7SJoonsoo Kim refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_FILE); 3550170b04b7SJoonsoo Kim target_lruvec->refaults[1] = refaults; 35512a2e4885SJohannes Weiner } 35522a2e4885SJohannes Weiner 35531da177e4SLinus Torvalds /* 35541da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 35551da177e4SLinus Torvalds * 35561da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 35571da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 35581da177e4SLinus Torvalds * 35591da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 35601da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 35615b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 35625b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 35635b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 35645b0830cbSJens Axboe * work, and the allocation attempt will fail. 3565a41f24eaSNishanth Aravamudan * 3566a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 3567a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 35681da177e4SLinus Torvalds */ 3569dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 35703115cd91SVladimir Davydov struct scan_control *sc) 35711da177e4SLinus Torvalds { 3572241994edSJohannes Weiner int initial_priority = sc->priority; 35732a2e4885SJohannes Weiner pg_data_t *last_pgdat; 35742a2e4885SJohannes Weiner struct zoneref *z; 35752a2e4885SJohannes Weiner struct zone *zone; 3576241994edSJohannes Weiner retry: 3577873b4771SKeika Kobayashi delayacct_freepages_start(); 3578873b4771SKeika Kobayashi 3579b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 35807cc30fcfSMel Gorman __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); 35811da177e4SLinus Torvalds 35829e3b2f8cSKonstantin Khlebnikov do { 358370ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 358470ddf637SAnton Vorontsov sc->priority); 358566e1707bSBalbir Singh sc->nr_scanned = 0; 35860a0337e0SMichal Hocko shrink_zones(zonelist, sc); 3587e0c23279SMel Gorman 3588bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 35890b06496aSJohannes Weiner break; 35900b06496aSJohannes Weiner 35910b06496aSJohannes Weiner if (sc->compaction_ready) 35920b06496aSJohannes Weiner break; 35931da177e4SLinus Torvalds 35941da177e4SLinus Torvalds /* 35950e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 35960e50ce3bSMinchan Kim * writepage even in laptop mode. 35970e50ce3bSMinchan Kim */ 35980e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 35990e50ce3bSMinchan Kim sc->may_writepage = 1; 36000b06496aSJohannes Weiner } while (--sc->priority >= 0); 3601bb21c7ceSKOSAKI Motohiro 36022a2e4885SJohannes Weiner last_pgdat = NULL; 36032a2e4885SJohannes Weiner for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx, 36042a2e4885SJohannes Weiner sc->nodemask) { 36052a2e4885SJohannes Weiner if (zone->zone_pgdat == last_pgdat) 36062a2e4885SJohannes Weiner continue; 36072a2e4885SJohannes Weiner last_pgdat = zone->zone_pgdat; 36081b05117dSJohannes Weiner 36092a2e4885SJohannes Weiner snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat); 36101b05117dSJohannes Weiner 36111b05117dSJohannes Weiner if (cgroup_reclaim(sc)) { 36121b05117dSJohannes Weiner struct lruvec *lruvec; 36131b05117dSJohannes Weiner 36141b05117dSJohannes Weiner lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, 36151b05117dSJohannes Weiner zone->zone_pgdat); 36161b05117dSJohannes Weiner clear_bit(LRUVEC_CONGESTED, &lruvec->flags); 36171b05117dSJohannes Weiner } 36182a2e4885SJohannes Weiner } 36192a2e4885SJohannes Weiner 3620873b4771SKeika Kobayashi delayacct_freepages_end(); 3621873b4771SKeika Kobayashi 3622bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 3623bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 3624bb21c7ceSKOSAKI Motohiro 36250cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 36260b06496aSJohannes Weiner if (sc->compaction_ready) 36277335084dSMel Gorman return 1; 36287335084dSMel Gorman 3629b91ac374SJohannes Weiner /* 3630b91ac374SJohannes Weiner * We make inactive:active ratio decisions based on the node's 3631b91ac374SJohannes Weiner * composition of memory, but a restrictive reclaim_idx or a 3632b91ac374SJohannes Weiner * memory.low cgroup setting can exempt large amounts of 3633b91ac374SJohannes Weiner * memory from reclaim. Neither of which are very common, so 3634b91ac374SJohannes Weiner * instead of doing costly eligibility calculations of the 3635b91ac374SJohannes Weiner * entire cgroup subtree up front, we assume the estimates are 3636b91ac374SJohannes Weiner * good, and retry with forcible deactivation if that fails. 3637b91ac374SJohannes Weiner */ 3638b91ac374SJohannes Weiner if (sc->skipped_deactivate) { 3639b91ac374SJohannes Weiner sc->priority = initial_priority; 3640b91ac374SJohannes Weiner sc->force_deactivate = 1; 3641b91ac374SJohannes Weiner sc->skipped_deactivate = 0; 3642b91ac374SJohannes Weiner goto retry; 3643b91ac374SJohannes Weiner } 3644b91ac374SJohannes Weiner 3645241994edSJohannes Weiner /* Untapped cgroup reserves? Don't OOM, retry. */ 3646d6622f63SYisheng Xie if (sc->memcg_low_skipped) { 3647241994edSJohannes Weiner sc->priority = initial_priority; 3648b91ac374SJohannes Weiner sc->force_deactivate = 0; 3649d6622f63SYisheng Xie sc->memcg_low_reclaim = 1; 3650d6622f63SYisheng Xie sc->memcg_low_skipped = 0; 3651241994edSJohannes Weiner goto retry; 3652241994edSJohannes Weiner } 3653241994edSJohannes Weiner 3654bb21c7ceSKOSAKI Motohiro return 0; 36551da177e4SLinus Torvalds } 36561da177e4SLinus Torvalds 3657c73322d0SJohannes Weiner static bool allow_direct_reclaim(pg_data_t *pgdat) 36585515061dSMel Gorman { 36595515061dSMel Gorman struct zone *zone; 36605515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 36615515061dSMel Gorman unsigned long free_pages = 0; 36625515061dSMel Gorman int i; 36635515061dSMel Gorman bool wmark_ok; 36645515061dSMel Gorman 3665c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3666c73322d0SJohannes Weiner return true; 3667c73322d0SJohannes Weiner 36685515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 36695515061dSMel Gorman zone = &pgdat->node_zones[i]; 3670d450abd8SJohannes Weiner if (!managed_zone(zone)) 3671d450abd8SJohannes Weiner continue; 3672d450abd8SJohannes Weiner 3673d450abd8SJohannes Weiner if (!zone_reclaimable_pages(zone)) 3674675becceSMel Gorman continue; 3675675becceSMel Gorman 36765515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 36775515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 36785515061dSMel Gorman } 36795515061dSMel Gorman 3680675becceSMel Gorman /* If there are no reserves (unexpected config) then do not throttle */ 3681675becceSMel Gorman if (!pfmemalloc_reserve) 3682675becceSMel Gorman return true; 3683675becceSMel Gorman 36845515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 36855515061dSMel Gorman 36865515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 36875515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 368897a225e6SJoonsoo Kim if (READ_ONCE(pgdat->kswapd_highest_zoneidx) > ZONE_NORMAL) 368997a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, ZONE_NORMAL); 36905644e1fbSQian Cai 36915515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 36925515061dSMel Gorman } 36935515061dSMel Gorman 36945515061dSMel Gorman return wmark_ok; 36955515061dSMel Gorman } 36965515061dSMel Gorman 36975515061dSMel Gorman /* 36985515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 36995515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 37005515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 370150694c28SMel Gorman * when the low watermark is reached. 370250694c28SMel Gorman * 370350694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 370450694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 37055515061dSMel Gorman */ 370650694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 37075515061dSMel Gorman nodemask_t *nodemask) 37085515061dSMel Gorman { 3709675becceSMel Gorman struct zoneref *z; 37105515061dSMel Gorman struct zone *zone; 3711675becceSMel Gorman pg_data_t *pgdat = NULL; 37125515061dSMel Gorman 37135515061dSMel Gorman /* 37145515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 37155515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 37165515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 37175515061dSMel Gorman * committing a transaction where throttling it could forcing other 37185515061dSMel Gorman * processes to block on log_wait_commit(). 37195515061dSMel Gorman */ 37205515061dSMel Gorman if (current->flags & PF_KTHREAD) 372150694c28SMel Gorman goto out; 372250694c28SMel Gorman 372350694c28SMel Gorman /* 372450694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 372550694c28SMel Gorman * It should return quickly so it can exit and free its memory 372650694c28SMel Gorman */ 372750694c28SMel Gorman if (fatal_signal_pending(current)) 372850694c28SMel Gorman goto out; 37295515061dSMel Gorman 3730675becceSMel Gorman /* 3731675becceSMel Gorman * Check if the pfmemalloc reserves are ok by finding the first node 3732675becceSMel Gorman * with a usable ZONE_NORMAL or lower zone. The expectation is that 3733675becceSMel Gorman * GFP_KERNEL will be required for allocating network buffers when 3734675becceSMel Gorman * swapping over the network so ZONE_HIGHMEM is unusable. 3735675becceSMel Gorman * 3736675becceSMel Gorman * Throttling is based on the first usable node and throttled processes 3737675becceSMel Gorman * wait on a queue until kswapd makes progress and wakes them. There 3738675becceSMel Gorman * is an affinity then between processes waking up and where reclaim 3739675becceSMel Gorman * progress has been made assuming the process wakes on the same node. 3740675becceSMel Gorman * More importantly, processes running on remote nodes will not compete 3741675becceSMel Gorman * for remote pfmemalloc reserves and processes on different nodes 3742675becceSMel Gorman * should make reasonable progress. 3743675becceSMel Gorman */ 3744675becceSMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 374517636faaSMichael S. Tsirkin gfp_zone(gfp_mask), nodemask) { 3746675becceSMel Gorman if (zone_idx(zone) > ZONE_NORMAL) 3747675becceSMel Gorman continue; 3748675becceSMel Gorman 3749675becceSMel Gorman /* Throttle based on the first usable node */ 37505515061dSMel Gorman pgdat = zone->zone_pgdat; 3751c73322d0SJohannes Weiner if (allow_direct_reclaim(pgdat)) 375250694c28SMel Gorman goto out; 3753675becceSMel Gorman break; 3754675becceSMel Gorman } 3755675becceSMel Gorman 3756675becceSMel Gorman /* If no zone was usable by the allocation flags then do not throttle */ 3757675becceSMel Gorman if (!pgdat) 3758675becceSMel Gorman goto out; 37595515061dSMel Gorman 376068243e76SMel Gorman /* Account for the throttling */ 376168243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 376268243e76SMel Gorman 37635515061dSMel Gorman /* 37645515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 37655515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 37665515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 37675515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 37685515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 37695515061dSMel Gorman * second before continuing. 37705515061dSMel Gorman */ 37712e786d9eSMiaohe Lin if (!(gfp_mask & __GFP_FS)) 37725515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 3773c73322d0SJohannes Weiner allow_direct_reclaim(pgdat), HZ); 37742e786d9eSMiaohe Lin else 37755515061dSMel Gorman /* Throttle until kswapd wakes the process */ 37765515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 3777c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)); 377850694c28SMel Gorman 377950694c28SMel Gorman if (fatal_signal_pending(current)) 378050694c28SMel Gorman return true; 378150694c28SMel Gorman 378250694c28SMel Gorman out: 378350694c28SMel Gorman return false; 37845515061dSMel Gorman } 37855515061dSMel Gorman 3786dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 3787327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 378866e1707bSBalbir Singh { 378933906bc5SMel Gorman unsigned long nr_reclaimed; 379066e1707bSBalbir Singh struct scan_control sc = { 379122fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3792f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 3793b2e18757SMel Gorman .reclaim_idx = gfp_zone(gfp_mask), 3794ee814fe2SJohannes Weiner .order = order, 3795ee814fe2SJohannes Weiner .nodemask = nodemask, 3796ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 3797ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3798a6dc60f8SJohannes Weiner .may_unmap = 1, 37992e2e4259SKOSAKI Motohiro .may_swap = 1, 380066e1707bSBalbir Singh }; 380166e1707bSBalbir Singh 38025515061dSMel Gorman /* 3803bb451fdfSGreg Thelen * scan_control uses s8 fields for order, priority, and reclaim_idx. 3804bb451fdfSGreg Thelen * Confirm they are large enough for max values. 3805bb451fdfSGreg Thelen */ 3806bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_ORDER > S8_MAX); 3807bb451fdfSGreg Thelen BUILD_BUG_ON(DEF_PRIORITY > S8_MAX); 3808bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX); 3809bb451fdfSGreg Thelen 3810bb451fdfSGreg Thelen /* 381150694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 381250694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 381350694c28SMel Gorman * point. 38145515061dSMel Gorman */ 3815f2f43e56SNick Desaulniers if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask)) 38165515061dSMel Gorman return 1; 38175515061dSMel Gorman 38181732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 38193481c37fSYafang Shao trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask); 382033906bc5SMel Gorman 38213115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 382233906bc5SMel Gorman 382333906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 38241732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 382533906bc5SMel Gorman 382633906bc5SMel Gorman return nr_reclaimed; 382766e1707bSBalbir Singh } 382866e1707bSBalbir Singh 3829c255a458SAndrew Morton #ifdef CONFIG_MEMCG 383066e1707bSBalbir Singh 3831d2e5fb92SMichal Hocko /* Only used by soft limit reclaim. Do not reuse for anything else. */ 3832a9dd0a83SMel Gorman unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, 38334e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 3834ef8f2327SMel Gorman pg_data_t *pgdat, 38350ae5e89cSYing Han unsigned long *nr_scanned) 38364e416953SBalbir Singh { 3837afaf07a6SJohannes Weiner struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); 38384e416953SBalbir Singh struct scan_control sc = { 3839b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3840ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 38414e416953SBalbir Singh .may_writepage = !laptop_mode, 38424e416953SBalbir Singh .may_unmap = 1, 3843b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 38444e416953SBalbir Singh .may_swap = !noswap, 38454e416953SBalbir Singh }; 38460ae5e89cSYing Han 3847d2e5fb92SMichal Hocko WARN_ON_ONCE(!current->reclaim_state); 3848d2e5fb92SMichal Hocko 38494e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 38504e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 3851bdce6d9eSKOSAKI Motohiro 38529e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 38533481c37fSYafang Shao sc.gfp_mask); 3854bdce6d9eSKOSAKI Motohiro 38554e416953SBalbir Singh /* 38564e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 38574e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 3858a9dd0a83SMel Gorman * if we don't reclaim here, the shrink_node from balance_pgdat 38594e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 38604e416953SBalbir Singh * the priority and make it zero. 38614e416953SBalbir Singh */ 3862afaf07a6SJohannes Weiner shrink_lruvec(lruvec, &sc); 3863bdce6d9eSKOSAKI Motohiro 3864bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 3865bdce6d9eSKOSAKI Motohiro 38660ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 38670308f7cfSYafang Shao 38684e416953SBalbir Singh return sc.nr_reclaimed; 38694e416953SBalbir Singh } 38704e416953SBalbir Singh 387172835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 3872b70a2a21SJohannes Weiner unsigned long nr_pages, 38738c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 3874b70a2a21SJohannes Weiner bool may_swap) 387566e1707bSBalbir Singh { 3876bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 3877499118e9SVlastimil Babka unsigned int noreclaim_flag; 387866e1707bSBalbir Singh struct scan_control sc = { 3879b70a2a21SJohannes Weiner .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 38807dea19f9SMichal Hocko .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | 3881ee814fe2SJohannes Weiner (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 3882b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 3883ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 3884ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 388566e1707bSBalbir Singh .may_writepage = !laptop_mode, 3886a6dc60f8SJohannes Weiner .may_unmap = 1, 3887b70a2a21SJohannes Weiner .may_swap = may_swap, 3888a09ed5e0SYing Han }; 3889fa40d1eeSShakeel Butt /* 3890fa40d1eeSShakeel Butt * Traverse the ZONELIST_FALLBACK zonelist of the current node to put 3891fa40d1eeSShakeel Butt * equal pressure on all the nodes. This is based on the assumption that 3892fa40d1eeSShakeel Butt * the reclaim does not bail out early. 3893fa40d1eeSShakeel Butt */ 3894fa40d1eeSShakeel Butt struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 389566e1707bSBalbir Singh 38961732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 38973481c37fSYafang Shao trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask); 3898499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 3899eb414681SJohannes Weiner 39003115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3901eb414681SJohannes Weiner 3902499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 3903bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 39041732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 3905bdce6d9eSKOSAKI Motohiro 3906bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 390766e1707bSBalbir Singh } 390866e1707bSBalbir Singh #endif 390966e1707bSBalbir Singh 39101d82de61SMel Gorman static void age_active_anon(struct pglist_data *pgdat, 3911ef8f2327SMel Gorman struct scan_control *sc) 3912f16015fbSJohannes Weiner { 3913b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 3914b91ac374SJohannes Weiner struct lruvec *lruvec; 3915b95a2f2dSJohannes Weiner 39162f368a9fSDave Hansen if (!can_age_anon_pages(pgdat, sc)) 3917b95a2f2dSJohannes Weiner return; 3918b95a2f2dSJohannes Weiner 3919b91ac374SJohannes Weiner lruvec = mem_cgroup_lruvec(NULL, pgdat); 3920b91ac374SJohannes Weiner if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON)) 3921b91ac374SJohannes Weiner return; 3922b91ac374SJohannes Weiner 3923b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 3924b95a2f2dSJohannes Weiner do { 3925b91ac374SJohannes Weiner lruvec = mem_cgroup_lruvec(memcg, pgdat); 39261a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 39279e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 3928b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 3929b95a2f2dSJohannes Weiner } while (memcg); 3930f16015fbSJohannes Weiner } 3931f16015fbSJohannes Weiner 393297a225e6SJoonsoo Kim static bool pgdat_watermark_boosted(pg_data_t *pgdat, int highest_zoneidx) 39331c30844dSMel Gorman { 39341c30844dSMel Gorman int i; 39351c30844dSMel Gorman struct zone *zone; 39361c30844dSMel Gorman 39371c30844dSMel Gorman /* 39381c30844dSMel Gorman * Check for watermark boosts top-down as the higher zones 39391c30844dSMel Gorman * are more likely to be boosted. Both watermarks and boosts 39401eba09c1SRandy Dunlap * should not be checked at the same time as reclaim would 39411c30844dSMel Gorman * start prematurely when there is no boosting and a lower 39421c30844dSMel Gorman * zone is balanced. 39431c30844dSMel Gorman */ 394497a225e6SJoonsoo Kim for (i = highest_zoneidx; i >= 0; i--) { 39451c30844dSMel Gorman zone = pgdat->node_zones + i; 39461c30844dSMel Gorman if (!managed_zone(zone)) 39471c30844dSMel Gorman continue; 39481c30844dSMel Gorman 39491c30844dSMel Gorman if (zone->watermark_boost) 39501c30844dSMel Gorman return true; 39511c30844dSMel Gorman } 39521c30844dSMel Gorman 39531c30844dSMel Gorman return false; 39541c30844dSMel Gorman } 39551c30844dSMel Gorman 3956e716f2ebSMel Gorman /* 3957e716f2ebSMel Gorman * Returns true if there is an eligible zone balanced for the request order 395897a225e6SJoonsoo Kim * and highest_zoneidx 3959e716f2ebSMel Gorman */ 396097a225e6SJoonsoo Kim static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx) 396160cefed4SJohannes Weiner { 3962e716f2ebSMel Gorman int i; 3963e716f2ebSMel Gorman unsigned long mark = -1; 3964e716f2ebSMel Gorman struct zone *zone; 396560cefed4SJohannes Weiner 39661c30844dSMel Gorman /* 39671c30844dSMel Gorman * Check watermarks bottom-up as lower zones are more likely to 39681c30844dSMel Gorman * meet watermarks. 39691c30844dSMel Gorman */ 397097a225e6SJoonsoo Kim for (i = 0; i <= highest_zoneidx; i++) { 3971e716f2ebSMel Gorman zone = pgdat->node_zones + i; 39726256c6b4SMel Gorman 3973e716f2ebSMel Gorman if (!managed_zone(zone)) 3974e716f2ebSMel Gorman continue; 3975e716f2ebSMel Gorman 3976e716f2ebSMel Gorman mark = high_wmark_pages(zone); 397797a225e6SJoonsoo Kim if (zone_watermark_ok_safe(zone, order, mark, highest_zoneidx)) 39786256c6b4SMel Gorman return true; 397960cefed4SJohannes Weiner } 398060cefed4SJohannes Weiner 3981e716f2ebSMel Gorman /* 398297a225e6SJoonsoo Kim * If a node has no populated zone within highest_zoneidx, it does not 3983e716f2ebSMel Gorman * need balancing by definition. This can happen if a zone-restricted 3984e716f2ebSMel Gorman * allocation tries to wake a remote kswapd. 3985e716f2ebSMel Gorman */ 3986e716f2ebSMel Gorman if (mark == -1) 3987e716f2ebSMel Gorman return true; 3988e716f2ebSMel Gorman 3989e716f2ebSMel Gorman return false; 3990e716f2ebSMel Gorman } 3991e716f2ebSMel Gorman 3992631b6e08SMel Gorman /* Clear pgdat state for congested, dirty or under writeback. */ 3993631b6e08SMel Gorman static void clear_pgdat_congested(pg_data_t *pgdat) 3994631b6e08SMel Gorman { 39951b05117dSJohannes Weiner struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat); 39961b05117dSJohannes Weiner 39971b05117dSJohannes Weiner clear_bit(LRUVEC_CONGESTED, &lruvec->flags); 3998631b6e08SMel Gorman clear_bit(PGDAT_DIRTY, &pgdat->flags); 3999631b6e08SMel Gorman clear_bit(PGDAT_WRITEBACK, &pgdat->flags); 4000631b6e08SMel Gorman } 4001631b6e08SMel Gorman 40021741c877SMel Gorman /* 40035515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 40045515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 40055515061dSMel Gorman * 40065515061dSMel Gorman * Returns true if kswapd is ready to sleep 40075515061dSMel Gorman */ 400897a225e6SJoonsoo Kim static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, 400997a225e6SJoonsoo Kim int highest_zoneidx) 4010f50de2d3SMel Gorman { 40115515061dSMel Gorman /* 40129e5e3661SVlastimil Babka * The throttled processes are normally woken up in balance_pgdat() as 4013c73322d0SJohannes Weiner * soon as allow_direct_reclaim() is true. But there is a potential 40149e5e3661SVlastimil Babka * race between when kswapd checks the watermarks and a process gets 40159e5e3661SVlastimil Babka * throttled. There is also a potential race if processes get 40169e5e3661SVlastimil Babka * throttled, kswapd wakes, a large process exits thereby balancing the 40179e5e3661SVlastimil Babka * zones, which causes kswapd to exit balance_pgdat() before reaching 40189e5e3661SVlastimil Babka * the wake up checks. If kswapd is going to sleep, no process should 40199e5e3661SVlastimil Babka * be sleeping on pfmemalloc_wait, so wake them now if necessary. If 40209e5e3661SVlastimil Babka * the wake up is premature, processes will wake kswapd and get 40219e5e3661SVlastimil Babka * throttled again. The difference from wake ups in balance_pgdat() is 40229e5e3661SVlastimil Babka * that here we are under prepare_to_wait(). 40235515061dSMel Gorman */ 40249e5e3661SVlastimil Babka if (waitqueue_active(&pgdat->pfmemalloc_wait)) 40259e5e3661SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 4026f50de2d3SMel Gorman 4027c73322d0SJohannes Weiner /* Hopeless node, leave it to direct reclaim */ 4028c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 4029c73322d0SJohannes Weiner return true; 4030c73322d0SJohannes Weiner 403197a225e6SJoonsoo Kim if (pgdat_balanced(pgdat, order, highest_zoneidx)) { 4032631b6e08SMel Gorman clear_pgdat_congested(pgdat); 4033333b0a45SShantanu Goel return true; 40341d82de61SMel Gorman } 40351d82de61SMel Gorman 4036333b0a45SShantanu Goel return false; 4037f50de2d3SMel Gorman } 4038f50de2d3SMel Gorman 40391da177e4SLinus Torvalds /* 40401d82de61SMel Gorman * kswapd shrinks a node of pages that are at or below the highest usable 40411d82de61SMel Gorman * zone that is currently unbalanced. 4042b8e83b94SMel Gorman * 4043b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 4044283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 4045283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 404675485363SMel Gorman */ 40471d82de61SMel Gorman static bool kswapd_shrink_node(pg_data_t *pgdat, 4048accf6242SVlastimil Babka struct scan_control *sc) 404975485363SMel Gorman { 40501d82de61SMel Gorman struct zone *zone; 40511d82de61SMel Gorman int z; 405275485363SMel Gorman 40531d82de61SMel Gorman /* Reclaim a number of pages proportional to the number of zones */ 40541d82de61SMel Gorman sc->nr_to_reclaim = 0; 4055970a39a3SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 40561d82de61SMel Gorman zone = pgdat->node_zones + z; 40576aa303deSMel Gorman if (!managed_zone(zone)) 40581d82de61SMel Gorman continue; 40597c954f6dSMel Gorman 40601d82de61SMel Gorman sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); 40617c954f6dSMel Gorman } 40627c954f6dSMel Gorman 40631d82de61SMel Gorman /* 40641d82de61SMel Gorman * Historically care was taken to put equal pressure on all zones but 40651d82de61SMel Gorman * now pressure is applied based on node LRU order. 40661d82de61SMel Gorman */ 4067970a39a3SMel Gorman shrink_node(pgdat, sc); 40681d82de61SMel Gorman 40691d82de61SMel Gorman /* 40701d82de61SMel Gorman * Fragmentation may mean that the system cannot be rebalanced for 40711d82de61SMel Gorman * high-order allocations. If twice the allocation size has been 40721d82de61SMel Gorman * reclaimed then recheck watermarks only at order-0 to prevent 40731d82de61SMel Gorman * excessive reclaim. Assume that a process requested a high-order 40741d82de61SMel Gorman * can direct reclaim/compact. 40751d82de61SMel Gorman */ 40769861a62cSVlastimil Babka if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order)) 40771d82de61SMel Gorman sc->order = 0; 40781d82de61SMel Gorman 4079b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 408075485363SMel Gorman } 408175485363SMel Gorman 4082c49c2c47SMel Gorman /* Page allocator PCP high watermark is lowered if reclaim is active. */ 4083c49c2c47SMel Gorman static inline void 4084c49c2c47SMel Gorman update_reclaim_active(pg_data_t *pgdat, int highest_zoneidx, bool active) 4085c49c2c47SMel Gorman { 4086c49c2c47SMel Gorman int i; 4087c49c2c47SMel Gorman struct zone *zone; 4088c49c2c47SMel Gorman 4089c49c2c47SMel Gorman for (i = 0; i <= highest_zoneidx; i++) { 4090c49c2c47SMel Gorman zone = pgdat->node_zones + i; 4091c49c2c47SMel Gorman 4092c49c2c47SMel Gorman if (!managed_zone(zone)) 4093c49c2c47SMel Gorman continue; 4094c49c2c47SMel Gorman 4095c49c2c47SMel Gorman if (active) 4096c49c2c47SMel Gorman set_bit(ZONE_RECLAIM_ACTIVE, &zone->flags); 4097c49c2c47SMel Gorman else 4098c49c2c47SMel Gorman clear_bit(ZONE_RECLAIM_ACTIVE, &zone->flags); 4099c49c2c47SMel Gorman } 4100c49c2c47SMel Gorman } 4101c49c2c47SMel Gorman 4102c49c2c47SMel Gorman static inline void 4103c49c2c47SMel Gorman set_reclaim_active(pg_data_t *pgdat, int highest_zoneidx) 4104c49c2c47SMel Gorman { 4105c49c2c47SMel Gorman update_reclaim_active(pgdat, highest_zoneidx, true); 4106c49c2c47SMel Gorman } 4107c49c2c47SMel Gorman 4108c49c2c47SMel Gorman static inline void 4109c49c2c47SMel Gorman clear_reclaim_active(pg_data_t *pgdat, int highest_zoneidx) 4110c49c2c47SMel Gorman { 4111c49c2c47SMel Gorman update_reclaim_active(pgdat, highest_zoneidx, false); 4112c49c2c47SMel Gorman } 4113c49c2c47SMel Gorman 411475485363SMel Gorman /* 41151d82de61SMel Gorman * For kswapd, balance_pgdat() will reclaim pages across a node from zones 41161d82de61SMel Gorman * that are eligible for use by the caller until at least one zone is 41171d82de61SMel Gorman * balanced. 41181da177e4SLinus Torvalds * 41191d82de61SMel Gorman * Returns the order kswapd finished reclaiming at. 41201da177e4SLinus Torvalds * 41211da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 412241858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 41238bb4e7a2SWei Yang * found to have free_pages <= high_wmark_pages(zone), any page in that zone 41241d82de61SMel Gorman * or lower is eligible for reclaim until at least one usable zone is 41251d82de61SMel Gorman * balanced. 41261da177e4SLinus Torvalds */ 412797a225e6SJoonsoo Kim static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx) 41281da177e4SLinus Torvalds { 41291da177e4SLinus Torvalds int i; 41300608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 41310608f43dSAndrew Morton unsigned long nr_soft_scanned; 4132eb414681SJohannes Weiner unsigned long pflags; 41331c30844dSMel Gorman unsigned long nr_boost_reclaim; 41341c30844dSMel Gorman unsigned long zone_boosts[MAX_NR_ZONES] = { 0, }; 41351c30844dSMel Gorman bool boosted; 41361d82de61SMel Gorman struct zone *zone; 4137179e9639SAndrew Morton struct scan_control sc = { 4138179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 4139ee814fe2SJohannes Weiner .order = order, 4140a6dc60f8SJohannes Weiner .may_unmap = 1, 4141179e9639SAndrew Morton }; 414293781325SOmar Sandoval 41431732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 4144eb414681SJohannes Weiner psi_memstall_enter(&pflags); 41454f3eaf45SMatthew Wilcox (Oracle) __fs_reclaim_acquire(_THIS_IP_); 414693781325SOmar Sandoval 4147f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 41481da177e4SLinus Torvalds 41491c30844dSMel Gorman /* 41501c30844dSMel Gorman * Account for the reclaim boost. Note that the zone boost is left in 41511c30844dSMel Gorman * place so that parallel allocations that are near the watermark will 41521c30844dSMel Gorman * stall or direct reclaim until kswapd is finished. 41531c30844dSMel Gorman */ 41541c30844dSMel Gorman nr_boost_reclaim = 0; 415597a225e6SJoonsoo Kim for (i = 0; i <= highest_zoneidx; i++) { 41561c30844dSMel Gorman zone = pgdat->node_zones + i; 41571c30844dSMel Gorman if (!managed_zone(zone)) 41581c30844dSMel Gorman continue; 41591c30844dSMel Gorman 41601c30844dSMel Gorman nr_boost_reclaim += zone->watermark_boost; 41611c30844dSMel Gorman zone_boosts[i] = zone->watermark_boost; 41621c30844dSMel Gorman } 41631c30844dSMel Gorman boosted = nr_boost_reclaim; 41641c30844dSMel Gorman 41651c30844dSMel Gorman restart: 4166c49c2c47SMel Gorman set_reclaim_active(pgdat, highest_zoneidx); 41671c30844dSMel Gorman sc.priority = DEF_PRIORITY; 41689e3b2f8cSKonstantin Khlebnikov do { 4169c73322d0SJohannes Weiner unsigned long nr_reclaimed = sc.nr_reclaimed; 4170b8e83b94SMel Gorman bool raise_priority = true; 41711c30844dSMel Gorman bool balanced; 417293781325SOmar Sandoval bool ret; 4173b8e83b94SMel Gorman 417497a225e6SJoonsoo Kim sc.reclaim_idx = highest_zoneidx; 41751da177e4SLinus Torvalds 417686c79f6bSMel Gorman /* 417784c7a777SMel Gorman * If the number of buffer_heads exceeds the maximum allowed 417884c7a777SMel Gorman * then consider reclaiming from all zones. This has a dual 417984c7a777SMel Gorman * purpose -- on 64-bit systems it is expected that 418084c7a777SMel Gorman * buffer_heads are stripped during active rotation. On 32-bit 418184c7a777SMel Gorman * systems, highmem pages can pin lowmem memory and shrinking 418284c7a777SMel Gorman * buffers can relieve lowmem pressure. Reclaim may still not 418384c7a777SMel Gorman * go ahead if all eligible zones for the original allocation 418484c7a777SMel Gorman * request are balanced to avoid excessive reclaim from kswapd. 418586c79f6bSMel Gorman */ 418686c79f6bSMel Gorman if (buffer_heads_over_limit) { 418786c79f6bSMel Gorman for (i = MAX_NR_ZONES - 1; i >= 0; i--) { 418886c79f6bSMel Gorman zone = pgdat->node_zones + i; 41896aa303deSMel Gorman if (!managed_zone(zone)) 419086c79f6bSMel Gorman continue; 419186c79f6bSMel Gorman 4192970a39a3SMel Gorman sc.reclaim_idx = i; 419386c79f6bSMel Gorman break; 419486c79f6bSMel Gorman } 419586c79f6bSMel Gorman } 419686c79f6bSMel Gorman 419786c79f6bSMel Gorman /* 41981c30844dSMel Gorman * If the pgdat is imbalanced then ignore boosting and preserve 41991c30844dSMel Gorman * the watermarks for a later time and restart. Note that the 42001c30844dSMel Gorman * zone watermarks will be still reset at the end of balancing 42011c30844dSMel Gorman * on the grounds that the normal reclaim should be enough to 42021c30844dSMel Gorman * re-evaluate if boosting is required when kswapd next wakes. 420386c79f6bSMel Gorman */ 420497a225e6SJoonsoo Kim balanced = pgdat_balanced(pgdat, sc.order, highest_zoneidx); 42051c30844dSMel Gorman if (!balanced && nr_boost_reclaim) { 42061c30844dSMel Gorman nr_boost_reclaim = 0; 42071c30844dSMel Gorman goto restart; 42081c30844dSMel Gorman } 42091c30844dSMel Gorman 42101c30844dSMel Gorman /* 42111c30844dSMel Gorman * If boosting is not active then only reclaim if there are no 42121c30844dSMel Gorman * eligible zones. Note that sc.reclaim_idx is not used as 42131c30844dSMel Gorman * buffer_heads_over_limit may have adjusted it. 42141c30844dSMel Gorman */ 42151c30844dSMel Gorman if (!nr_boost_reclaim && balanced) 42161da177e4SLinus Torvalds goto out; 4217e1dbeda6SAndrew Morton 42181c30844dSMel Gorman /* Limit the priority of boosting to avoid reclaim writeback */ 42191c30844dSMel Gorman if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2) 42201c30844dSMel Gorman raise_priority = false; 42211c30844dSMel Gorman 42221c30844dSMel Gorman /* 42231c30844dSMel Gorman * Do not writeback or swap pages for boosted reclaim. The 42241c30844dSMel Gorman * intent is to relieve pressure not issue sub-optimal IO 42251c30844dSMel Gorman * from reclaim context. If no pages are reclaimed, the 42261c30844dSMel Gorman * reclaim will be aborted. 42271c30844dSMel Gorman */ 42281c30844dSMel Gorman sc.may_writepage = !laptop_mode && !nr_boost_reclaim; 42291c30844dSMel Gorman sc.may_swap = !nr_boost_reclaim; 42301c30844dSMel Gorman 42311da177e4SLinus Torvalds /* 42321d82de61SMel Gorman * Do some background aging of the anon list, to give 42331d82de61SMel Gorman * pages a chance to be referenced before reclaiming. All 42341d82de61SMel Gorman * pages are rotated regardless of classzone as this is 42351d82de61SMel Gorman * about consistent aging. 42361d82de61SMel Gorman */ 4237ef8f2327SMel Gorman age_active_anon(pgdat, &sc); 42381d82de61SMel Gorman 42391d82de61SMel Gorman /* 4240b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 4241b7ea3c41SMel Gorman * even in laptop mode. 4242b7ea3c41SMel Gorman */ 4243047d72c3SJohannes Weiner if (sc.priority < DEF_PRIORITY - 2) 4244b7ea3c41SMel Gorman sc.may_writepage = 1; 4245b7ea3c41SMel Gorman 42461d82de61SMel Gorman /* Call soft limit reclaim before calling shrink_node. */ 42471da177e4SLinus Torvalds sc.nr_scanned = 0; 42480608f43dSAndrew Morton nr_soft_scanned = 0; 4249ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, 42501d82de61SMel Gorman sc.gfp_mask, &nr_soft_scanned); 42510608f43dSAndrew Morton sc.nr_reclaimed += nr_soft_reclaimed; 42520608f43dSAndrew Morton 425332a4330dSRik van Riel /* 42541d82de61SMel Gorman * There should be no need to raise the scanning priority if 42551d82de61SMel Gorman * enough pages are already being scanned that that high 42561d82de61SMel Gorman * watermark would be met at 100% efficiency. 425732a4330dSRik van Riel */ 4258970a39a3SMel Gorman if (kswapd_shrink_node(pgdat, &sc)) 4259b8e83b94SMel Gorman raise_priority = false; 4260d7868daeSMel Gorman 42615515061dSMel Gorman /* 42625515061dSMel Gorman * If the low watermark is met there is no need for processes 42635515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 42645515061dSMel Gorman * able to safely make forward progress. Wake them 42655515061dSMel Gorman */ 42665515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 4267c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)) 4268cfc51155SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 42695515061dSMel Gorman 4270b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 42714f3eaf45SMatthew Wilcox (Oracle) __fs_reclaim_release(_THIS_IP_); 427293781325SOmar Sandoval ret = try_to_freeze(); 42734f3eaf45SMatthew Wilcox (Oracle) __fs_reclaim_acquire(_THIS_IP_); 427493781325SOmar Sandoval if (ret || kthread_should_stop()) 4275b8e83b94SMel Gorman break; 4276b8e83b94SMel Gorman 4277b8e83b94SMel Gorman /* 4278b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 4279b8e83b94SMel Gorman * progress in reclaiming pages 4280b8e83b94SMel Gorman */ 4281c73322d0SJohannes Weiner nr_reclaimed = sc.nr_reclaimed - nr_reclaimed; 42821c30844dSMel Gorman nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed); 42831c30844dSMel Gorman 42841c30844dSMel Gorman /* 42851c30844dSMel Gorman * If reclaim made no progress for a boost, stop reclaim as 42861c30844dSMel Gorman * IO cannot be queued and it could be an infinite loop in 42871c30844dSMel Gorman * extreme circumstances. 42881c30844dSMel Gorman */ 42891c30844dSMel Gorman if (nr_boost_reclaim && !nr_reclaimed) 42901c30844dSMel Gorman break; 42911c30844dSMel Gorman 4292c73322d0SJohannes Weiner if (raise_priority || !nr_reclaimed) 4293b8e83b94SMel Gorman sc.priority--; 42941d82de61SMel Gorman } while (sc.priority >= 1); 42951da177e4SLinus Torvalds 4296c73322d0SJohannes Weiner if (!sc.nr_reclaimed) 4297c73322d0SJohannes Weiner pgdat->kswapd_failures++; 4298c73322d0SJohannes Weiner 4299b8e83b94SMel Gorman out: 4300c49c2c47SMel Gorman clear_reclaim_active(pgdat, highest_zoneidx); 4301c49c2c47SMel Gorman 43021c30844dSMel Gorman /* If reclaim was boosted, account for the reclaim done in this pass */ 43031c30844dSMel Gorman if (boosted) { 43041c30844dSMel Gorman unsigned long flags; 43051c30844dSMel Gorman 430697a225e6SJoonsoo Kim for (i = 0; i <= highest_zoneidx; i++) { 43071c30844dSMel Gorman if (!zone_boosts[i]) 43081c30844dSMel Gorman continue; 43091c30844dSMel Gorman 43101c30844dSMel Gorman /* Increments are under the zone lock */ 43111c30844dSMel Gorman zone = pgdat->node_zones + i; 43121c30844dSMel Gorman spin_lock_irqsave(&zone->lock, flags); 43131c30844dSMel Gorman zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]); 43141c30844dSMel Gorman spin_unlock_irqrestore(&zone->lock, flags); 43151c30844dSMel Gorman } 43161c30844dSMel Gorman 43171c30844dSMel Gorman /* 43181c30844dSMel Gorman * As there is now likely space, wakeup kcompact to defragment 43191c30844dSMel Gorman * pageblocks. 43201c30844dSMel Gorman */ 432197a225e6SJoonsoo Kim wakeup_kcompactd(pgdat, pageblock_order, highest_zoneidx); 43221c30844dSMel Gorman } 43231c30844dSMel Gorman 43242a2e4885SJohannes Weiner snapshot_refaults(NULL, pgdat); 43254f3eaf45SMatthew Wilcox (Oracle) __fs_reclaim_release(_THIS_IP_); 4326eb414681SJohannes Weiner psi_memstall_leave(&pflags); 43271732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 4328e5ca8071SYafang Shao 43290abdee2bSMel Gorman /* 43301d82de61SMel Gorman * Return the order kswapd stopped reclaiming at as 43311d82de61SMel Gorman * prepare_kswapd_sleep() takes it into account. If another caller 43321d82de61SMel Gorman * entered the allocator slow path while kswapd was awake, order will 43331d82de61SMel Gorman * remain at the higher level. 43340abdee2bSMel Gorman */ 43351d82de61SMel Gorman return sc.order; 43361da177e4SLinus Torvalds } 43371da177e4SLinus Torvalds 4338e716f2ebSMel Gorman /* 433997a225e6SJoonsoo Kim * The pgdat->kswapd_highest_zoneidx is used to pass the highest zone index to 434097a225e6SJoonsoo Kim * be reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is 434197a225e6SJoonsoo Kim * not a valid index then either kswapd runs for first time or kswapd couldn't 434297a225e6SJoonsoo Kim * sleep after previous reclaim attempt (node is still unbalanced). In that 434397a225e6SJoonsoo Kim * case return the zone index of the previous kswapd reclaim cycle. 4344e716f2ebSMel Gorman */ 434597a225e6SJoonsoo Kim static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat, 434697a225e6SJoonsoo Kim enum zone_type prev_highest_zoneidx) 4347e716f2ebSMel Gorman { 434897a225e6SJoonsoo Kim enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); 43495644e1fbSQian Cai 435097a225e6SJoonsoo Kim return curr_idx == MAX_NR_ZONES ? prev_highest_zoneidx : curr_idx; 4351e716f2ebSMel Gorman } 4352e716f2ebSMel Gorman 435338087d9bSMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, 435497a225e6SJoonsoo Kim unsigned int highest_zoneidx) 4355f0bc0a60SKOSAKI Motohiro { 4356f0bc0a60SKOSAKI Motohiro long remaining = 0; 4357f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 4358f0bc0a60SKOSAKI Motohiro 4359f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 4360f0bc0a60SKOSAKI Motohiro return; 4361f0bc0a60SKOSAKI Motohiro 4362f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 4363f0bc0a60SKOSAKI Motohiro 4364333b0a45SShantanu Goel /* 4365333b0a45SShantanu Goel * Try to sleep for a short interval. Note that kcompactd will only be 4366333b0a45SShantanu Goel * woken if it is possible to sleep for a short interval. This is 4367333b0a45SShantanu Goel * deliberate on the assumption that if reclaim cannot keep an 4368333b0a45SShantanu Goel * eligible zone balanced that it's also unlikely that compaction will 4369333b0a45SShantanu Goel * succeed. 4370333b0a45SShantanu Goel */ 437197a225e6SJoonsoo Kim if (prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { 4372fd901c95SVlastimil Babka /* 4373fd901c95SVlastimil Babka * Compaction records what page blocks it recently failed to 4374fd901c95SVlastimil Babka * isolate pages from and skips them in the future scanning. 4375fd901c95SVlastimil Babka * When kswapd is going to sleep, it is reasonable to assume 4376fd901c95SVlastimil Babka * that pages and compaction may succeed so reset the cache. 4377fd901c95SVlastimil Babka */ 4378fd901c95SVlastimil Babka reset_isolation_suitable(pgdat); 4379fd901c95SVlastimil Babka 4380fd901c95SVlastimil Babka /* 4381fd901c95SVlastimil Babka * We have freed the memory, now we should compact it to make 4382fd901c95SVlastimil Babka * allocation of the requested order possible. 4383fd901c95SVlastimil Babka */ 438497a225e6SJoonsoo Kim wakeup_kcompactd(pgdat, alloc_order, highest_zoneidx); 4385fd901c95SVlastimil Babka 4386f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 438738087d9bSMel Gorman 438838087d9bSMel Gorman /* 438997a225e6SJoonsoo Kim * If woken prematurely then reset kswapd_highest_zoneidx and 439038087d9bSMel Gorman * order. The values will either be from a wakeup request or 439138087d9bSMel Gorman * the previous request that slept prematurely. 439238087d9bSMel Gorman */ 439338087d9bSMel Gorman if (remaining) { 439497a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, 439597a225e6SJoonsoo Kim kswapd_highest_zoneidx(pgdat, 439697a225e6SJoonsoo Kim highest_zoneidx)); 43975644e1fbSQian Cai 43985644e1fbSQian Cai if (READ_ONCE(pgdat->kswapd_order) < reclaim_order) 43995644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, reclaim_order); 440038087d9bSMel Gorman } 440138087d9bSMel Gorman 4402f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 4403f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 4404f0bc0a60SKOSAKI Motohiro } 4405f0bc0a60SKOSAKI Motohiro 4406f0bc0a60SKOSAKI Motohiro /* 4407f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 4408f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 4409f0bc0a60SKOSAKI Motohiro */ 4410d9f21d42SMel Gorman if (!remaining && 441197a225e6SJoonsoo Kim prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { 4412f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 4413f0bc0a60SKOSAKI Motohiro 4414f0bc0a60SKOSAKI Motohiro /* 4415f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 4416f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 4417f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 4418f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 4419f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 4420f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 4421f0bc0a60SKOSAKI Motohiro */ 4422f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 44231c7e7f6cSAaditya Kumar 44241c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 4425f0bc0a60SKOSAKI Motohiro schedule(); 44261c7e7f6cSAaditya Kumar 4427f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 4428f0bc0a60SKOSAKI Motohiro } else { 4429f0bc0a60SKOSAKI Motohiro if (remaining) 4430f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 4431f0bc0a60SKOSAKI Motohiro else 4432f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 4433f0bc0a60SKOSAKI Motohiro } 4434f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 4435f0bc0a60SKOSAKI Motohiro } 4436f0bc0a60SKOSAKI Motohiro 44371da177e4SLinus Torvalds /* 44381da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 44391da177e4SLinus Torvalds * from the init process. 44401da177e4SLinus Torvalds * 44411da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 44421da177e4SLinus Torvalds * free memory available even if there is no other activity 44431da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 44441da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 44451da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 44461da177e4SLinus Torvalds * 44471da177e4SLinus Torvalds * If there are applications that are active memory-allocators 44481da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 44491da177e4SLinus Torvalds */ 44501da177e4SLinus Torvalds static int kswapd(void *p) 44511da177e4SLinus Torvalds { 4452e716f2ebSMel Gorman unsigned int alloc_order, reclaim_order; 445397a225e6SJoonsoo Kim unsigned int highest_zoneidx = MAX_NR_ZONES - 1; 44541da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t *)p; 44551da177e4SLinus Torvalds struct task_struct *tsk = current; 4456a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 44571da177e4SLinus Torvalds 4458174596a0SRusty Russell if (!cpumask_empty(cpumask)) 4459c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 44601da177e4SLinus Torvalds 44611da177e4SLinus Torvalds /* 44621da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 44631da177e4SLinus Torvalds * and that if we need more memory we should get access to it 44641da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 44651da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 44661da177e4SLinus Torvalds * 44671da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 44681da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 44691da177e4SLinus Torvalds * page out something else, and this flag essentially protects 44701da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 44711da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 44721da177e4SLinus Torvalds */ 4473930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 447483144186SRafael J. Wysocki set_freezable(); 44751da177e4SLinus Torvalds 44765644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, 0); 447797a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); 44788cd7c588SMel Gorman atomic_set(&pgdat->nr_writeback_throttled, 0); 44791da177e4SLinus Torvalds for ( ; ; ) { 44806f6313d4SJeff Liu bool ret; 44813e1d1d28SChristoph Lameter 44825644e1fbSQian Cai alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order); 448397a225e6SJoonsoo Kim highest_zoneidx = kswapd_highest_zoneidx(pgdat, 448497a225e6SJoonsoo Kim highest_zoneidx); 4485e716f2ebSMel Gorman 448638087d9bSMel Gorman kswapd_try_sleep: 448738087d9bSMel Gorman kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, 448897a225e6SJoonsoo Kim highest_zoneidx); 4489215ddd66SMel Gorman 449097a225e6SJoonsoo Kim /* Read the new order and highest_zoneidx */ 44912b47a24cSLukas Bulwahn alloc_order = READ_ONCE(pgdat->kswapd_order); 449297a225e6SJoonsoo Kim highest_zoneidx = kswapd_highest_zoneidx(pgdat, 449397a225e6SJoonsoo Kim highest_zoneidx); 44945644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, 0); 449597a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); 44961da177e4SLinus Torvalds 44978fe23e05SDavid Rientjes ret = try_to_freeze(); 44988fe23e05SDavid Rientjes if (kthread_should_stop()) 44998fe23e05SDavid Rientjes break; 45008fe23e05SDavid Rientjes 45018fe23e05SDavid Rientjes /* 45028fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 45038fe23e05SDavid Rientjes * after returning from the refrigerator 4504b1296cc4SRafael J. Wysocki */ 450538087d9bSMel Gorman if (ret) 450638087d9bSMel Gorman continue; 45071d82de61SMel Gorman 450838087d9bSMel Gorman /* 450938087d9bSMel Gorman * Reclaim begins at the requested order but if a high-order 451038087d9bSMel Gorman * reclaim fails then kswapd falls back to reclaiming for 451138087d9bSMel Gorman * order-0. If that happens, kswapd will consider sleeping 451238087d9bSMel Gorman * for the order it finished reclaiming at (reclaim_order) 451338087d9bSMel Gorman * but kcompactd is woken to compact for the original 451438087d9bSMel Gorman * request (alloc_order). 451538087d9bSMel Gorman */ 451697a225e6SJoonsoo Kim trace_mm_vmscan_kswapd_wake(pgdat->node_id, highest_zoneidx, 4517e5146b12SMel Gorman alloc_order); 451897a225e6SJoonsoo Kim reclaim_order = balance_pgdat(pgdat, alloc_order, 451997a225e6SJoonsoo Kim highest_zoneidx); 452038087d9bSMel Gorman if (reclaim_order < alloc_order) 452138087d9bSMel Gorman goto kswapd_try_sleep; 452233906bc5SMel Gorman } 4523b0a8cc58STakamori Yamaguchi 452471abdc15SJohannes Weiner tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); 452571abdc15SJohannes Weiner 45261da177e4SLinus Torvalds return 0; 45271da177e4SLinus Torvalds } 45281da177e4SLinus Torvalds 45291da177e4SLinus Torvalds /* 45305ecd9d40SDavid Rientjes * A zone is low on free memory or too fragmented for high-order memory. If 45315ecd9d40SDavid Rientjes * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's 45325ecd9d40SDavid Rientjes * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim 45335ecd9d40SDavid Rientjes * has failed or is not needed, still wake up kcompactd if only compaction is 45345ecd9d40SDavid Rientjes * needed. 45351da177e4SLinus Torvalds */ 45365ecd9d40SDavid Rientjes void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, 453797a225e6SJoonsoo Kim enum zone_type highest_zoneidx) 45381da177e4SLinus Torvalds { 45391da177e4SLinus Torvalds pg_data_t *pgdat; 45405644e1fbSQian Cai enum zone_type curr_idx; 45411da177e4SLinus Torvalds 45426aa303deSMel Gorman if (!managed_zone(zone)) 45431da177e4SLinus Torvalds return; 45441da177e4SLinus Torvalds 45455ecd9d40SDavid Rientjes if (!cpuset_zone_allowed(zone, gfp_flags)) 45461da177e4SLinus Torvalds return; 4547dffcac2cSShakeel Butt 45485644e1fbSQian Cai pgdat = zone->zone_pgdat; 454997a225e6SJoonsoo Kim curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); 45505644e1fbSQian Cai 455197a225e6SJoonsoo Kim if (curr_idx == MAX_NR_ZONES || curr_idx < highest_zoneidx) 455297a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, highest_zoneidx); 45535644e1fbSQian Cai 45545644e1fbSQian Cai if (READ_ONCE(pgdat->kswapd_order) < order) 45555644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, order); 45565644e1fbSQian Cai 45578d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 45581da177e4SLinus Torvalds return; 4559e1a55637SMel Gorman 45605ecd9d40SDavid Rientjes /* Hopeless node, leave it to direct reclaim if possible */ 45615ecd9d40SDavid Rientjes if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES || 456297a225e6SJoonsoo Kim (pgdat_balanced(pgdat, order, highest_zoneidx) && 456397a225e6SJoonsoo Kim !pgdat_watermark_boosted(pgdat, highest_zoneidx))) { 45645ecd9d40SDavid Rientjes /* 45655ecd9d40SDavid Rientjes * There may be plenty of free memory available, but it's too 45665ecd9d40SDavid Rientjes * fragmented for high-order allocations. Wake up kcompactd 45675ecd9d40SDavid Rientjes * and rely on compaction_suitable() to determine if it's 45685ecd9d40SDavid Rientjes * needed. If it fails, it will defer subsequent attempts to 45695ecd9d40SDavid Rientjes * ratelimit its work. 45705ecd9d40SDavid Rientjes */ 45715ecd9d40SDavid Rientjes if (!(gfp_flags & __GFP_DIRECT_RECLAIM)) 457297a225e6SJoonsoo Kim wakeup_kcompactd(pgdat, order, highest_zoneidx); 4573c73322d0SJohannes Weiner return; 45745ecd9d40SDavid Rientjes } 4575c73322d0SJohannes Weiner 457697a225e6SJoonsoo Kim trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, highest_zoneidx, order, 45775ecd9d40SDavid Rientjes gfp_flags); 45788d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 45791da177e4SLinus Torvalds } 45801da177e4SLinus Torvalds 4581c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 45821da177e4SLinus Torvalds /* 45837b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 4584d6277db4SRafael J. Wysocki * freed pages. 4585d6277db4SRafael J. Wysocki * 4586d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 4587d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 4588d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 45891da177e4SLinus Torvalds */ 45907b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 45911da177e4SLinus Torvalds { 4592d6277db4SRafael J. Wysocki struct scan_control sc = { 45937b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 4594ee814fe2SJohannes Weiner .gfp_mask = GFP_HIGHUSER_MOVABLE, 4595b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 45969e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 4597ee814fe2SJohannes Weiner .may_writepage = 1, 4598ee814fe2SJohannes Weiner .may_unmap = 1, 4599ee814fe2SJohannes Weiner .may_swap = 1, 4600ee814fe2SJohannes Weiner .hibernation_mode = 1, 46011da177e4SLinus Torvalds }; 46027b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 46037b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 4604499118e9SVlastimil Babka unsigned int noreclaim_flag; 46051da177e4SLinus Torvalds 4606d92a8cfcSPeter Zijlstra fs_reclaim_acquire(sc.gfp_mask); 460793781325SOmar Sandoval noreclaim_flag = memalloc_noreclaim_save(); 46081732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 4609d6277db4SRafael J. Wysocki 46103115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 4611d6277db4SRafael J. Wysocki 46121732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 4613499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 461493781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 4615d6277db4SRafael J. Wysocki 46167b51755cSKOSAKI Motohiro return nr_reclaimed; 46171da177e4SLinus Torvalds } 4618c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 46191da177e4SLinus Torvalds 46203218ae14SYasunori Goto /* 46213218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 46223218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 46233218ae14SYasunori Goto */ 4624b87c517aSMiaohe Lin void kswapd_run(int nid) 46253218ae14SYasunori Goto { 46263218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 46273218ae14SYasunori Goto 46283218ae14SYasunori Goto if (pgdat->kswapd) 4629b87c517aSMiaohe Lin return; 46303218ae14SYasunori Goto 46313218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 46323218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 46333218ae14SYasunori Goto /* failure at boot is fatal */ 4634c6202adfSThomas Gleixner BUG_ON(system_state < SYSTEM_RUNNING); 4635d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 4636d72515b8SXishi Qiu pgdat->kswapd = NULL; 46373218ae14SYasunori Goto } 46383218ae14SYasunori Goto } 46393218ae14SYasunori Goto 46408fe23e05SDavid Rientjes /* 4641d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 4642bfc8c901SVladimir Davydov * hold mem_hotplug_begin/end(). 46438fe23e05SDavid Rientjes */ 46448fe23e05SDavid Rientjes void kswapd_stop(int nid) 46458fe23e05SDavid Rientjes { 46468fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 46478fe23e05SDavid Rientjes 4648d8adde17SJiang Liu if (kswapd) { 46498fe23e05SDavid Rientjes kthread_stop(kswapd); 4650d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 4651d8adde17SJiang Liu } 46528fe23e05SDavid Rientjes } 46538fe23e05SDavid Rientjes 46541da177e4SLinus Torvalds static int __init kswapd_init(void) 46551da177e4SLinus Torvalds { 46566b700b5bSWei Yang int nid; 465769e05944SAndrew Morton 46581da177e4SLinus Torvalds swap_setup(); 465948fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 46603218ae14SYasunori Goto kswapd_run(nid); 46611da177e4SLinus Torvalds return 0; 46621da177e4SLinus Torvalds } 46631da177e4SLinus Torvalds 46641da177e4SLinus Torvalds module_init(kswapd_init) 46659eeff239SChristoph Lameter 46669eeff239SChristoph Lameter #ifdef CONFIG_NUMA 46679eeff239SChristoph Lameter /* 4668a5f5f91dSMel Gorman * Node reclaim mode 46699eeff239SChristoph Lameter * 4670a5f5f91dSMel Gorman * If non-zero call node_reclaim when the number of free pages falls below 46719eeff239SChristoph Lameter * the watermarks. 46729eeff239SChristoph Lameter */ 4673a5f5f91dSMel Gorman int node_reclaim_mode __read_mostly; 46749eeff239SChristoph Lameter 467551998364SDave Hansen /* 4676a5f5f91dSMel Gorman * Priority for NODE_RECLAIM. This determines the fraction of pages 4677a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 4678a92f7126SChristoph Lameter * a zone. 4679a92f7126SChristoph Lameter */ 4680a5f5f91dSMel Gorman #define NODE_RECLAIM_PRIORITY 4 4681a92f7126SChristoph Lameter 46829eeff239SChristoph Lameter /* 4683a5f5f91dSMel Gorman * Percentage of pages in a zone that must be unmapped for node_reclaim to 46849614634fSChristoph Lameter * occur. 46859614634fSChristoph Lameter */ 46869614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 46879614634fSChristoph Lameter 46889614634fSChristoph Lameter /* 46890ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 46900ff38490SChristoph Lameter * slab reclaim needs to occur. 46910ff38490SChristoph Lameter */ 46920ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 46930ff38490SChristoph Lameter 469411fb9989SMel Gorman static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) 469590afa5deSMel Gorman { 469611fb9989SMel Gorman unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); 469711fb9989SMel Gorman unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + 469811fb9989SMel Gorman node_page_state(pgdat, NR_ACTIVE_FILE); 469990afa5deSMel Gorman 470090afa5deSMel Gorman /* 470190afa5deSMel Gorman * It's possible for there to be more file mapped pages than 470290afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 470390afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 470490afa5deSMel Gorman */ 470590afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 470690afa5deSMel Gorman } 470790afa5deSMel Gorman 470890afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 4709a5f5f91dSMel Gorman static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) 471090afa5deSMel Gorman { 4711d031a157SAlexandru Moise unsigned long nr_pagecache_reclaimable; 4712d031a157SAlexandru Moise unsigned long delta = 0; 471390afa5deSMel Gorman 471490afa5deSMel Gorman /* 471595bbc0c7SZhihui Zhang * If RECLAIM_UNMAP is set, then all file pages are considered 471690afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 471711fb9989SMel Gorman * pages like swapcache and node_unmapped_file_pages() provides 471890afa5deSMel Gorman * a better estimate 471990afa5deSMel Gorman */ 4720a5f5f91dSMel Gorman if (node_reclaim_mode & RECLAIM_UNMAP) 4721a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); 472290afa5deSMel Gorman else 4723a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); 472490afa5deSMel Gorman 472590afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 4726a5f5f91dSMel Gorman if (!(node_reclaim_mode & RECLAIM_WRITE)) 4727a5f5f91dSMel Gorman delta += node_page_state(pgdat, NR_FILE_DIRTY); 472890afa5deSMel Gorman 472990afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 473090afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 473190afa5deSMel Gorman delta = nr_pagecache_reclaimable; 473290afa5deSMel Gorman 473390afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 473490afa5deSMel Gorman } 473590afa5deSMel Gorman 47360ff38490SChristoph Lameter /* 4737a5f5f91dSMel Gorman * Try to free up some pages from this node through reclaim. 47389eeff239SChristoph Lameter */ 4739a5f5f91dSMel Gorman static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 47409eeff239SChristoph Lameter { 47417fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 474269e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 47439eeff239SChristoph Lameter struct task_struct *p = current; 4744499118e9SVlastimil Babka unsigned int noreclaim_flag; 4745179e9639SAndrew Morton struct scan_control sc = { 474662b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 4747f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 4748bd2f6199SJohannes Weiner .order = order, 4749a5f5f91dSMel Gorman .priority = NODE_RECLAIM_PRIORITY, 4750a5f5f91dSMel Gorman .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), 4751a5f5f91dSMel Gorman .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), 4752ee814fe2SJohannes Weiner .may_swap = 1, 4753f2f43e56SNick Desaulniers .reclaim_idx = gfp_zone(gfp_mask), 4754179e9639SAndrew Morton }; 475557f29762SJohannes Weiner unsigned long pflags; 47569eeff239SChristoph Lameter 4757132bb8cfSYafang Shao trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order, 4758132bb8cfSYafang Shao sc.gfp_mask); 4759132bb8cfSYafang Shao 47609eeff239SChristoph Lameter cond_resched(); 476157f29762SJohannes Weiner psi_memstall_enter(&pflags); 476293781325SOmar Sandoval fs_reclaim_acquire(sc.gfp_mask); 4763d4f7796eSChristoph Lameter /* 476495bbc0c7SZhihui Zhang * We need to be able to allocate from the reserves for RECLAIM_UNMAP 4765d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 476695bbc0c7SZhihui Zhang * and RECLAIM_UNMAP. 4767d4f7796eSChristoph Lameter */ 4768499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 4769499118e9SVlastimil Babka p->flags |= PF_SWAPWRITE; 47701732d2b0SAndrew Morton set_task_reclaim_state(p, &sc.reclaim_state); 4771c84db23cSChristoph Lameter 4772a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) { 4773a92f7126SChristoph Lameter /* 4774894befecSAndrey Ryabinin * Free memory by calling shrink node with increasing 47750ff38490SChristoph Lameter * priorities until we have enough memory freed. 4776a92f7126SChristoph Lameter */ 4777a92f7126SChristoph Lameter do { 4778970a39a3SMel Gorman shrink_node(pgdat, &sc); 47799e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 47800ff38490SChristoph Lameter } 4781a92f7126SChristoph Lameter 47821732d2b0SAndrew Morton set_task_reclaim_state(p, NULL); 4783499118e9SVlastimil Babka current->flags &= ~PF_SWAPWRITE; 4784499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 478593781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 478657f29762SJohannes Weiner psi_memstall_leave(&pflags); 4787132bb8cfSYafang Shao 4788132bb8cfSYafang Shao trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed); 4789132bb8cfSYafang Shao 4790a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 47919eeff239SChristoph Lameter } 4792179e9639SAndrew Morton 4793a5f5f91dSMel Gorman int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 4794179e9639SAndrew Morton { 4795d773ed6bSDavid Rientjes int ret; 4796179e9639SAndrew Morton 4797179e9639SAndrew Morton /* 4798a5f5f91dSMel Gorman * Node reclaim reclaims unmapped file backed pages and 47990ff38490SChristoph Lameter * slab pages if we are over the defined limits. 480034aa1330SChristoph Lameter * 48019614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 48029614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 4803a5f5f91dSMel Gorman * thrown out if the node is overallocated. So we do not reclaim 4804a5f5f91dSMel Gorman * if less than a specified percentage of the node is used by 48059614634fSChristoph Lameter * unmapped file backed pages. 4806179e9639SAndrew Morton */ 4807a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && 4808d42f3245SRoman Gushchin node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) <= 4809d42f3245SRoman Gushchin pgdat->min_slab_pages) 4810a5f5f91dSMel Gorman return NODE_RECLAIM_FULL; 4811179e9639SAndrew Morton 4812179e9639SAndrew Morton /* 4813d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 4814179e9639SAndrew Morton */ 4815d0164adcSMel Gorman if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) 4816a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4817179e9639SAndrew Morton 4818179e9639SAndrew Morton /* 4819a5f5f91dSMel Gorman * Only run node reclaim on the local node or on nodes that do not 4820179e9639SAndrew Morton * have associated processors. This will favor the local processor 4821179e9639SAndrew Morton * over remote processors and spread off node memory allocations 4822179e9639SAndrew Morton * as wide as possible. 4823179e9639SAndrew Morton */ 4824a5f5f91dSMel Gorman if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) 4825a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4826d773ed6bSDavid Rientjes 4827a5f5f91dSMel Gorman if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) 4828a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4829fa5e084eSMel Gorman 4830a5f5f91dSMel Gorman ret = __node_reclaim(pgdat, gfp_mask, order); 4831a5f5f91dSMel Gorman clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); 4832d773ed6bSDavid Rientjes 483324cf7251SMel Gorman if (!ret) 483424cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 483524cf7251SMel Gorman 4836d773ed6bSDavid Rientjes return ret; 4837179e9639SAndrew Morton } 48389eeff239SChristoph Lameter #endif 4839894bc310SLee Schermerhorn 484089e004eaSLee Schermerhorn /** 484164e3d12fSKuo-Hsin Yang * check_move_unevictable_pages - check pages for evictability and move to 484264e3d12fSKuo-Hsin Yang * appropriate zone lru list 484364e3d12fSKuo-Hsin Yang * @pvec: pagevec with lru pages to check 484489e004eaSLee Schermerhorn * 484564e3d12fSKuo-Hsin Yang * Checks pages for evictability, if an evictable page is in the unevictable 484664e3d12fSKuo-Hsin Yang * lru list, moves it to the appropriate evictable lru list. This function 484764e3d12fSKuo-Hsin Yang * should be only used for lru pages. 484889e004eaSLee Schermerhorn */ 484964e3d12fSKuo-Hsin Yang void check_move_unevictable_pages(struct pagevec *pvec) 485089e004eaSLee Schermerhorn { 48516168d0daSAlex Shi struct lruvec *lruvec = NULL; 485224513264SHugh Dickins int pgscanned = 0; 485324513264SHugh Dickins int pgrescued = 0; 485489e004eaSLee Schermerhorn int i; 485589e004eaSLee Schermerhorn 485664e3d12fSKuo-Hsin Yang for (i = 0; i < pvec->nr; i++) { 485764e3d12fSKuo-Hsin Yang struct page *page = pvec->pages[i]; 48580de340cbSMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 48598d8869caSHugh Dickins int nr_pages; 486089e004eaSLee Schermerhorn 48618d8869caSHugh Dickins if (PageTransTail(page)) 48628d8869caSHugh Dickins continue; 48638d8869caSHugh Dickins 48648d8869caSHugh Dickins nr_pages = thp_nr_pages(page); 48658d8869caSHugh Dickins pgscanned += nr_pages; 48668d8869caSHugh Dickins 4867d25b5bd8SAlex Shi /* block memcg migration during page moving between lru */ 4868d25b5bd8SAlex Shi if (!TestClearPageLRU(page)) 4869d25b5bd8SAlex Shi continue; 4870d25b5bd8SAlex Shi 48710de340cbSMatthew Wilcox (Oracle) lruvec = folio_lruvec_relock_irq(folio, lruvec); 4872d25b5bd8SAlex Shi if (page_evictable(page) && PageUnevictable(page)) { 487346ae6b2cSYu Zhao del_page_from_lru_list(page, lruvec); 487424513264SHugh Dickins ClearPageUnevictable(page); 48753a9c9788SYu Zhao add_page_to_lru_list(page, lruvec); 48768d8869caSHugh Dickins pgrescued += nr_pages; 487789e004eaSLee Schermerhorn } 4878d25b5bd8SAlex Shi SetPageLRU(page); 487989e004eaSLee Schermerhorn } 488024513264SHugh Dickins 48816168d0daSAlex Shi if (lruvec) { 488224513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 488324513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 48846168d0daSAlex Shi unlock_page_lruvec_irq(lruvec); 4885d25b5bd8SAlex Shi } else if (pgscanned) { 4886d25b5bd8SAlex Shi count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 488724513264SHugh Dickins } 488885046579SHugh Dickins } 488964e3d12fSKuo-Hsin Yang EXPORT_SYMBOL_GPL(check_move_unevictable_pages); 4890