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> 59c574bbe9SHuang Ying #include <linux/sched/sysctl.h> 601da177e4SLinus Torvalds 610f8053a5SNick Piggin #include "internal.h" 620f8053a5SNick Piggin 6333906bc5SMel Gorman #define CREATE_TRACE_POINTS 6433906bc5SMel Gorman #include <trace/events/vmscan.h> 6533906bc5SMel Gorman 661da177e4SLinus Torvalds struct scan_control { 6722fba335SKOSAKI Motohiro /* How many pages shrink_list() should reclaim */ 6822fba335SKOSAKI Motohiro unsigned long nr_to_reclaim; 6922fba335SKOSAKI Motohiro 70ee814fe2SJohannes Weiner /* 71ee814fe2SJohannes Weiner * Nodemask of nodes allowed by the caller. If NULL, all nodes 72ee814fe2SJohannes Weiner * are scanned. 73ee814fe2SJohannes Weiner */ 74ee814fe2SJohannes Weiner nodemask_t *nodemask; 759e3b2f8cSKonstantin Khlebnikov 765f53e762SKOSAKI Motohiro /* 77f16015fbSJohannes Weiner * The memory cgroup that hit its limit and as a result is the 78f16015fbSJohannes Weiner * primary target of this reclaim invocation. 79f16015fbSJohannes Weiner */ 80f16015fbSJohannes Weiner struct mem_cgroup *target_mem_cgroup; 8166e1707bSBalbir Singh 827cf111bcSJohannes Weiner /* 837cf111bcSJohannes Weiner * Scan pressure balancing between anon and file LRUs 847cf111bcSJohannes Weiner */ 857cf111bcSJohannes Weiner unsigned long anon_cost; 867cf111bcSJohannes Weiner unsigned long file_cost; 877cf111bcSJohannes Weiner 88b91ac374SJohannes Weiner /* Can active pages be deactivated as part of reclaim? */ 89b91ac374SJohannes Weiner #define DEACTIVATE_ANON 1 90b91ac374SJohannes Weiner #define DEACTIVATE_FILE 2 91b91ac374SJohannes Weiner unsigned int may_deactivate:2; 92b91ac374SJohannes Weiner unsigned int force_deactivate:1; 93b91ac374SJohannes Weiner unsigned int skipped_deactivate:1; 94b91ac374SJohannes Weiner 951276ad68SJohannes Weiner /* Writepage batching in laptop mode; RECLAIM_WRITE */ 96ee814fe2SJohannes Weiner unsigned int may_writepage:1; 97ee814fe2SJohannes Weiner 98ee814fe2SJohannes Weiner /* Can mapped pages be reclaimed? */ 99ee814fe2SJohannes Weiner unsigned int may_unmap:1; 100ee814fe2SJohannes Weiner 101ee814fe2SJohannes Weiner /* Can pages be swapped as part of reclaim? */ 102ee814fe2SJohannes Weiner unsigned int may_swap:1; 103ee814fe2SJohannes Weiner 104d6622f63SYisheng Xie /* 105f56ce412SJohannes Weiner * Cgroup memory below memory.low is protected as long as we 106f56ce412SJohannes Weiner * don't threaten to OOM. If any cgroup is reclaimed at 107f56ce412SJohannes Weiner * reduced force or passed over entirely due to its memory.low 108f56ce412SJohannes Weiner * setting (memcg_low_skipped), and nothing is reclaimed as a 109f56ce412SJohannes Weiner * result, then go back for one more cycle that reclaims the protected 110f56ce412SJohannes Weiner * memory (memcg_low_reclaim) to avert OOM. 111d6622f63SYisheng Xie */ 112d6622f63SYisheng Xie unsigned int memcg_low_reclaim:1; 113d6622f63SYisheng Xie unsigned int memcg_low_skipped:1; 114241994edSJohannes Weiner 115ee814fe2SJohannes Weiner unsigned int hibernation_mode:1; 116ee814fe2SJohannes Weiner 117ee814fe2SJohannes Weiner /* One of the zones is ready for compaction */ 118ee814fe2SJohannes Weiner unsigned int compaction_ready:1; 119ee814fe2SJohannes Weiner 120b91ac374SJohannes Weiner /* There is easily reclaimable cold cache in the current node */ 121b91ac374SJohannes Weiner unsigned int cache_trim_mode:1; 122b91ac374SJohannes Weiner 12353138ceaSJohannes Weiner /* The file pages on the current node are dangerously low */ 12453138ceaSJohannes Weiner unsigned int file_is_tiny:1; 12553138ceaSJohannes Weiner 12626aa2d19SDave Hansen /* Always discard instead of demoting to lower tier memory */ 12726aa2d19SDave Hansen unsigned int no_demotion:1; 12826aa2d19SDave Hansen 129bb451fdfSGreg Thelen /* Allocation order */ 130bb451fdfSGreg Thelen s8 order; 131bb451fdfSGreg Thelen 132bb451fdfSGreg Thelen /* Scan (total_size >> priority) pages at once */ 133bb451fdfSGreg Thelen s8 priority; 134bb451fdfSGreg Thelen 135bb451fdfSGreg Thelen /* The highest zone to isolate pages for reclaim from */ 136bb451fdfSGreg Thelen s8 reclaim_idx; 137bb451fdfSGreg Thelen 138bb451fdfSGreg Thelen /* This context's GFP mask */ 139bb451fdfSGreg Thelen gfp_t gfp_mask; 140bb451fdfSGreg Thelen 141ee814fe2SJohannes Weiner /* Incremented by the number of inactive pages that were scanned */ 142ee814fe2SJohannes Weiner unsigned long nr_scanned; 143ee814fe2SJohannes Weiner 144ee814fe2SJohannes Weiner /* Number of pages freed so far during a call to shrink_zones() */ 145ee814fe2SJohannes Weiner unsigned long nr_reclaimed; 146d108c772SAndrey Ryabinin 147d108c772SAndrey Ryabinin struct { 148d108c772SAndrey Ryabinin unsigned int dirty; 149d108c772SAndrey Ryabinin unsigned int unqueued_dirty; 150d108c772SAndrey Ryabinin unsigned int congested; 151d108c772SAndrey Ryabinin unsigned int writeback; 152d108c772SAndrey Ryabinin unsigned int immediate; 153d108c772SAndrey Ryabinin unsigned int file_taken; 154d108c772SAndrey Ryabinin unsigned int taken; 155d108c772SAndrey Ryabinin } nr; 156e5ca8071SYafang Shao 157e5ca8071SYafang Shao /* for recording the reclaimed slab by now */ 158e5ca8071SYafang Shao struct reclaim_state reclaim_state; 1591da177e4SLinus Torvalds }; 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1621da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1631da177e4SLinus Torvalds do { \ 1641da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1651da177e4SLinus Torvalds struct page *prev; \ 1661da177e4SLinus Torvalds \ 1671da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1681da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1691da177e4SLinus Torvalds } \ 1701da177e4SLinus Torvalds } while (0) 1711da177e4SLinus Torvalds #else 1721da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1731da177e4SLinus Torvalds #endif 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds /* 176c843966cSJohannes Weiner * From 0 .. 200. Higher means more swappy. 1771da177e4SLinus Torvalds */ 1781da177e4SLinus Torvalds int vm_swappiness = 60; 1791da177e4SLinus Torvalds 1800a432dcbSYang Shi static void set_task_reclaim_state(struct task_struct *task, 1810a432dcbSYang Shi struct reclaim_state *rs) 1820a432dcbSYang Shi { 1830a432dcbSYang Shi /* Check for an overwrite */ 1840a432dcbSYang Shi WARN_ON_ONCE(rs && task->reclaim_state); 1850a432dcbSYang Shi 1860a432dcbSYang Shi /* Check for the nulling of an already-nulled member */ 1870a432dcbSYang Shi WARN_ON_ONCE(!rs && !task->reclaim_state); 1880a432dcbSYang Shi 1890a432dcbSYang Shi task->reclaim_state = rs; 1900a432dcbSYang Shi } 1910a432dcbSYang Shi 1921da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1931da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1941da177e4SLinus Torvalds 1950a432dcbSYang Shi #ifdef CONFIG_MEMCG 196a2fb1261SYang Shi static int shrinker_nr_max; 1972bfd3637SYang Shi 1983c6f17e6SYang Shi /* The shrinker_info is expanded in a batch of BITS_PER_LONG */ 199a2fb1261SYang Shi static inline int shrinker_map_size(int nr_items) 200a2fb1261SYang Shi { 201a2fb1261SYang Shi return (DIV_ROUND_UP(nr_items, BITS_PER_LONG) * sizeof(unsigned long)); 202a2fb1261SYang Shi } 2032bfd3637SYang Shi 2043c6f17e6SYang Shi static inline int shrinker_defer_size(int nr_items) 2053c6f17e6SYang Shi { 2063c6f17e6SYang Shi return (round_up(nr_items, BITS_PER_LONG) * sizeof(atomic_long_t)); 2073c6f17e6SYang Shi } 2083c6f17e6SYang Shi 209468ab843SYang Shi static struct shrinker_info *shrinker_info_protected(struct mem_cgroup *memcg, 210468ab843SYang Shi int nid) 211468ab843SYang Shi { 212468ab843SYang Shi return rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_info, 213468ab843SYang Shi lockdep_is_held(&shrinker_rwsem)); 214468ab843SYang Shi } 215468ab843SYang Shi 216e4262c4fSYang Shi static int expand_one_shrinker_info(struct mem_cgroup *memcg, 2173c6f17e6SYang Shi int map_size, int defer_size, 2183c6f17e6SYang Shi int old_map_size, int old_defer_size) 2192bfd3637SYang Shi { 220e4262c4fSYang Shi struct shrinker_info *new, *old; 2212bfd3637SYang Shi struct mem_cgroup_per_node *pn; 2222bfd3637SYang Shi int nid; 2233c6f17e6SYang Shi int size = map_size + defer_size; 2242bfd3637SYang Shi 2252bfd3637SYang Shi for_each_node(nid) { 2262bfd3637SYang Shi pn = memcg->nodeinfo[nid]; 227468ab843SYang Shi old = shrinker_info_protected(memcg, nid); 2282bfd3637SYang Shi /* Not yet online memcg */ 2292bfd3637SYang Shi if (!old) 2302bfd3637SYang Shi return 0; 2312bfd3637SYang Shi 2322bfd3637SYang Shi new = kvmalloc_node(sizeof(*new) + size, GFP_KERNEL, nid); 2332bfd3637SYang Shi if (!new) 2342bfd3637SYang Shi return -ENOMEM; 2352bfd3637SYang Shi 2363c6f17e6SYang Shi new->nr_deferred = (atomic_long_t *)(new + 1); 2373c6f17e6SYang Shi new->map = (void *)new->nr_deferred + defer_size; 2383c6f17e6SYang Shi 2393c6f17e6SYang Shi /* map: set all old bits, clear all new bits */ 2403c6f17e6SYang Shi memset(new->map, (int)0xff, old_map_size); 2413c6f17e6SYang Shi memset((void *)new->map + old_map_size, 0, map_size - old_map_size); 2423c6f17e6SYang Shi /* nr_deferred: copy old values, clear all new values */ 2433c6f17e6SYang Shi memcpy(new->nr_deferred, old->nr_deferred, old_defer_size); 2443c6f17e6SYang Shi memset((void *)new->nr_deferred + old_defer_size, 0, 2453c6f17e6SYang Shi defer_size - old_defer_size); 2462bfd3637SYang Shi 247e4262c4fSYang Shi rcu_assign_pointer(pn->shrinker_info, new); 24872673e86SYang Shi kvfree_rcu(old, rcu); 2492bfd3637SYang Shi } 2502bfd3637SYang Shi 2512bfd3637SYang Shi return 0; 2522bfd3637SYang Shi } 2532bfd3637SYang Shi 254e4262c4fSYang Shi void free_shrinker_info(struct mem_cgroup *memcg) 2552bfd3637SYang Shi { 2562bfd3637SYang Shi struct mem_cgroup_per_node *pn; 257e4262c4fSYang Shi struct shrinker_info *info; 2582bfd3637SYang Shi int nid; 2592bfd3637SYang Shi 2602bfd3637SYang Shi for_each_node(nid) { 2612bfd3637SYang Shi pn = memcg->nodeinfo[nid]; 262e4262c4fSYang Shi info = rcu_dereference_protected(pn->shrinker_info, true); 263e4262c4fSYang Shi kvfree(info); 264e4262c4fSYang Shi rcu_assign_pointer(pn->shrinker_info, NULL); 2652bfd3637SYang Shi } 2662bfd3637SYang Shi } 2672bfd3637SYang Shi 268e4262c4fSYang Shi int alloc_shrinker_info(struct mem_cgroup *memcg) 2692bfd3637SYang Shi { 270e4262c4fSYang Shi struct shrinker_info *info; 2712bfd3637SYang Shi int nid, size, ret = 0; 2723c6f17e6SYang Shi int map_size, defer_size = 0; 2732bfd3637SYang Shi 274d27cf2aaSYang Shi down_write(&shrinker_rwsem); 2753c6f17e6SYang Shi map_size = shrinker_map_size(shrinker_nr_max); 2763c6f17e6SYang Shi defer_size = shrinker_defer_size(shrinker_nr_max); 2773c6f17e6SYang Shi size = map_size + defer_size; 2782bfd3637SYang Shi for_each_node(nid) { 279e4262c4fSYang Shi info = kvzalloc_node(sizeof(*info) + size, GFP_KERNEL, nid); 280e4262c4fSYang Shi if (!info) { 281e4262c4fSYang Shi free_shrinker_info(memcg); 2822bfd3637SYang Shi ret = -ENOMEM; 2832bfd3637SYang Shi break; 2842bfd3637SYang Shi } 2853c6f17e6SYang Shi info->nr_deferred = (atomic_long_t *)(info + 1); 2863c6f17e6SYang Shi info->map = (void *)info->nr_deferred + defer_size; 287e4262c4fSYang Shi rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, info); 2882bfd3637SYang Shi } 289d27cf2aaSYang Shi up_write(&shrinker_rwsem); 2902bfd3637SYang Shi 2912bfd3637SYang Shi return ret; 2922bfd3637SYang Shi } 2932bfd3637SYang Shi 2943c6f17e6SYang Shi static inline bool need_expand(int nr_max) 2953c6f17e6SYang Shi { 2963c6f17e6SYang Shi return round_up(nr_max, BITS_PER_LONG) > 2973c6f17e6SYang Shi round_up(shrinker_nr_max, BITS_PER_LONG); 2983c6f17e6SYang Shi } 2993c6f17e6SYang Shi 300e4262c4fSYang Shi static int expand_shrinker_info(int new_id) 3012bfd3637SYang Shi { 3023c6f17e6SYang Shi int ret = 0; 303a2fb1261SYang Shi int new_nr_max = new_id + 1; 3043c6f17e6SYang Shi int map_size, defer_size = 0; 3053c6f17e6SYang Shi int old_map_size, old_defer_size = 0; 3062bfd3637SYang Shi struct mem_cgroup *memcg; 3072bfd3637SYang Shi 3083c6f17e6SYang Shi if (!need_expand(new_nr_max)) 309a2fb1261SYang Shi goto out; 3102bfd3637SYang Shi 3112bfd3637SYang Shi if (!root_mem_cgroup) 312d27cf2aaSYang Shi goto out; 313d27cf2aaSYang Shi 314d27cf2aaSYang Shi lockdep_assert_held(&shrinker_rwsem); 3152bfd3637SYang Shi 3163c6f17e6SYang Shi map_size = shrinker_map_size(new_nr_max); 3173c6f17e6SYang Shi defer_size = shrinker_defer_size(new_nr_max); 3183c6f17e6SYang Shi old_map_size = shrinker_map_size(shrinker_nr_max); 3193c6f17e6SYang Shi old_defer_size = shrinker_defer_size(shrinker_nr_max); 3203c6f17e6SYang Shi 3212bfd3637SYang Shi memcg = mem_cgroup_iter(NULL, NULL, NULL); 3222bfd3637SYang Shi do { 3233c6f17e6SYang Shi ret = expand_one_shrinker_info(memcg, map_size, defer_size, 3243c6f17e6SYang Shi old_map_size, old_defer_size); 3252bfd3637SYang Shi if (ret) { 3262bfd3637SYang Shi mem_cgroup_iter_break(NULL, memcg); 327d27cf2aaSYang Shi goto out; 3282bfd3637SYang Shi } 3292bfd3637SYang Shi } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 330d27cf2aaSYang Shi out: 3312bfd3637SYang Shi if (!ret) 332a2fb1261SYang Shi shrinker_nr_max = new_nr_max; 333d27cf2aaSYang Shi 3342bfd3637SYang Shi return ret; 3352bfd3637SYang Shi } 3362bfd3637SYang Shi 3372bfd3637SYang Shi void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id) 3382bfd3637SYang Shi { 3392bfd3637SYang Shi if (shrinker_id >= 0 && memcg && !mem_cgroup_is_root(memcg)) { 340e4262c4fSYang Shi struct shrinker_info *info; 3412bfd3637SYang Shi 3422bfd3637SYang Shi rcu_read_lock(); 343e4262c4fSYang Shi info = rcu_dereference(memcg->nodeinfo[nid]->shrinker_info); 3442bfd3637SYang Shi /* Pairs with smp mb in shrink_slab() */ 3452bfd3637SYang Shi smp_mb__before_atomic(); 346e4262c4fSYang Shi set_bit(shrinker_id, info->map); 3472bfd3637SYang Shi rcu_read_unlock(); 3482bfd3637SYang Shi } 3492bfd3637SYang Shi } 3502bfd3637SYang Shi 351b4c2b231SKirill Tkhai static DEFINE_IDR(shrinker_idr); 352b4c2b231SKirill Tkhai 353b4c2b231SKirill Tkhai static int prealloc_memcg_shrinker(struct shrinker *shrinker) 354b4c2b231SKirill Tkhai { 355b4c2b231SKirill Tkhai int id, ret = -ENOMEM; 356b4c2b231SKirill Tkhai 357476b30a0SYang Shi if (mem_cgroup_disabled()) 358476b30a0SYang Shi return -ENOSYS; 359476b30a0SYang Shi 360b4c2b231SKirill Tkhai down_write(&shrinker_rwsem); 361b4c2b231SKirill Tkhai /* This may call shrinker, so it must use down_read_trylock() */ 36241ca668aSYang Shi id = idr_alloc(&shrinker_idr, shrinker, 0, 0, GFP_KERNEL); 363b4c2b231SKirill Tkhai if (id < 0) 364b4c2b231SKirill Tkhai goto unlock; 365b4c2b231SKirill Tkhai 3660a4465d3SKirill Tkhai if (id >= shrinker_nr_max) { 367e4262c4fSYang Shi if (expand_shrinker_info(id)) { 3680a4465d3SKirill Tkhai idr_remove(&shrinker_idr, id); 3690a4465d3SKirill Tkhai goto unlock; 3700a4465d3SKirill Tkhai } 3710a4465d3SKirill Tkhai } 372b4c2b231SKirill Tkhai shrinker->id = id; 373b4c2b231SKirill Tkhai ret = 0; 374b4c2b231SKirill Tkhai unlock: 375b4c2b231SKirill Tkhai up_write(&shrinker_rwsem); 376b4c2b231SKirill Tkhai return ret; 377b4c2b231SKirill Tkhai } 378b4c2b231SKirill Tkhai 379b4c2b231SKirill Tkhai static void unregister_memcg_shrinker(struct shrinker *shrinker) 380b4c2b231SKirill Tkhai { 381b4c2b231SKirill Tkhai int id = shrinker->id; 382b4c2b231SKirill Tkhai 383b4c2b231SKirill Tkhai BUG_ON(id < 0); 384b4c2b231SKirill Tkhai 38541ca668aSYang Shi lockdep_assert_held(&shrinker_rwsem); 38641ca668aSYang Shi 387b4c2b231SKirill Tkhai idr_remove(&shrinker_idr, id); 388b4c2b231SKirill Tkhai } 389b4c2b231SKirill Tkhai 39086750830SYang Shi static long xchg_nr_deferred_memcg(int nid, struct shrinker *shrinker, 39186750830SYang Shi struct mem_cgroup *memcg) 39286750830SYang Shi { 39386750830SYang Shi struct shrinker_info *info; 39486750830SYang Shi 39586750830SYang Shi info = shrinker_info_protected(memcg, nid); 39686750830SYang Shi return atomic_long_xchg(&info->nr_deferred[shrinker->id], 0); 39786750830SYang Shi } 39886750830SYang Shi 39986750830SYang Shi static long add_nr_deferred_memcg(long nr, int nid, struct shrinker *shrinker, 40086750830SYang Shi struct mem_cgroup *memcg) 40186750830SYang Shi { 40286750830SYang Shi struct shrinker_info *info; 40386750830SYang Shi 40486750830SYang Shi info = shrinker_info_protected(memcg, nid); 40586750830SYang Shi return atomic_long_add_return(nr, &info->nr_deferred[shrinker->id]); 40686750830SYang Shi } 40786750830SYang Shi 408a178015cSYang Shi void reparent_shrinker_deferred(struct mem_cgroup *memcg) 409a178015cSYang Shi { 410a178015cSYang Shi int i, nid; 411a178015cSYang Shi long nr; 412a178015cSYang Shi struct mem_cgroup *parent; 413a178015cSYang Shi struct shrinker_info *child_info, *parent_info; 414a178015cSYang Shi 415a178015cSYang Shi parent = parent_mem_cgroup(memcg); 416a178015cSYang Shi if (!parent) 417a178015cSYang Shi parent = root_mem_cgroup; 418a178015cSYang Shi 419a178015cSYang Shi /* Prevent from concurrent shrinker_info expand */ 420a178015cSYang Shi down_read(&shrinker_rwsem); 421a178015cSYang Shi for_each_node(nid) { 422a178015cSYang Shi child_info = shrinker_info_protected(memcg, nid); 423a178015cSYang Shi parent_info = shrinker_info_protected(parent, nid); 424a178015cSYang Shi for (i = 0; i < shrinker_nr_max; i++) { 425a178015cSYang Shi nr = atomic_long_read(&child_info->nr_deferred[i]); 426a178015cSYang Shi atomic_long_add(nr, &parent_info->nr_deferred[i]); 427a178015cSYang Shi } 428a178015cSYang Shi } 429a178015cSYang Shi up_read(&shrinker_rwsem); 430a178015cSYang Shi } 431a178015cSYang Shi 432b5ead35eSJohannes Weiner static bool cgroup_reclaim(struct scan_control *sc) 43389b5fae5SJohannes Weiner { 434b5ead35eSJohannes Weiner return sc->target_mem_cgroup; 43589b5fae5SJohannes Weiner } 43697c9341fSTejun Heo 43797c9341fSTejun Heo /** 438b5ead35eSJohannes Weiner * writeback_throttling_sane - is the usual dirty throttling mechanism available? 43997c9341fSTejun Heo * @sc: scan_control in question 44097c9341fSTejun Heo * 44197c9341fSTejun Heo * The normal page dirty throttling mechanism in balance_dirty_pages() is 44297c9341fSTejun Heo * completely broken with the legacy memcg and direct stalling in 44397c9341fSTejun Heo * shrink_page_list() is used for throttling instead, which lacks all the 44497c9341fSTejun Heo * niceties such as fairness, adaptive pausing, bandwidth proportional 44597c9341fSTejun Heo * allocation and configurability. 44697c9341fSTejun Heo * 44797c9341fSTejun Heo * This function tests whether the vmscan currently in progress can assume 44897c9341fSTejun Heo * that the normal dirty throttling mechanism is operational. 44997c9341fSTejun Heo */ 450b5ead35eSJohannes Weiner static bool writeback_throttling_sane(struct scan_control *sc) 45197c9341fSTejun Heo { 452b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 45397c9341fSTejun Heo return true; 45497c9341fSTejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 45569234aceSLinus Torvalds if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) 45697c9341fSTejun Heo return true; 45797c9341fSTejun Heo #endif 45897c9341fSTejun Heo return false; 45997c9341fSTejun Heo } 46091a45470SKAMEZAWA Hiroyuki #else 4610a432dcbSYang Shi static int prealloc_memcg_shrinker(struct shrinker *shrinker) 4620a432dcbSYang Shi { 463476b30a0SYang Shi return -ENOSYS; 4640a432dcbSYang Shi } 4650a432dcbSYang Shi 4660a432dcbSYang Shi static void unregister_memcg_shrinker(struct shrinker *shrinker) 4670a432dcbSYang Shi { 4680a432dcbSYang Shi } 4690a432dcbSYang Shi 47086750830SYang Shi static long xchg_nr_deferred_memcg(int nid, struct shrinker *shrinker, 47186750830SYang Shi struct mem_cgroup *memcg) 47286750830SYang Shi { 47386750830SYang Shi return 0; 47486750830SYang Shi } 47586750830SYang Shi 47686750830SYang Shi static long add_nr_deferred_memcg(long nr, int nid, struct shrinker *shrinker, 47786750830SYang Shi struct mem_cgroup *memcg) 47886750830SYang Shi { 47986750830SYang Shi return 0; 48086750830SYang Shi } 48186750830SYang Shi 482b5ead35eSJohannes Weiner static bool cgroup_reclaim(struct scan_control *sc) 48389b5fae5SJohannes Weiner { 484b5ead35eSJohannes Weiner return false; 48589b5fae5SJohannes Weiner } 48697c9341fSTejun Heo 487b5ead35eSJohannes Weiner static bool writeback_throttling_sane(struct scan_control *sc) 48897c9341fSTejun Heo { 48997c9341fSTejun Heo return true; 49097c9341fSTejun Heo } 49191a45470SKAMEZAWA Hiroyuki #endif 49291a45470SKAMEZAWA Hiroyuki 49386750830SYang Shi static long xchg_nr_deferred(struct shrinker *shrinker, 49486750830SYang Shi struct shrink_control *sc) 49586750830SYang Shi { 49686750830SYang Shi int nid = sc->nid; 49786750830SYang Shi 49886750830SYang Shi if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) 49986750830SYang Shi nid = 0; 50086750830SYang Shi 50186750830SYang Shi if (sc->memcg && 50286750830SYang Shi (shrinker->flags & SHRINKER_MEMCG_AWARE)) 50386750830SYang Shi return xchg_nr_deferred_memcg(nid, shrinker, 50486750830SYang Shi sc->memcg); 50586750830SYang Shi 50686750830SYang Shi return atomic_long_xchg(&shrinker->nr_deferred[nid], 0); 50786750830SYang Shi } 50886750830SYang Shi 50986750830SYang Shi 51086750830SYang Shi static long add_nr_deferred(long nr, struct shrinker *shrinker, 51186750830SYang Shi struct shrink_control *sc) 51286750830SYang Shi { 51386750830SYang Shi int nid = sc->nid; 51486750830SYang Shi 51586750830SYang Shi if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) 51686750830SYang Shi nid = 0; 51786750830SYang Shi 51886750830SYang Shi if (sc->memcg && 51986750830SYang Shi (shrinker->flags & SHRINKER_MEMCG_AWARE)) 52086750830SYang Shi return add_nr_deferred_memcg(nr, nid, shrinker, 52186750830SYang Shi sc->memcg); 52286750830SYang Shi 52386750830SYang Shi return atomic_long_add_return(nr, &shrinker->nr_deferred[nid]); 52486750830SYang Shi } 52586750830SYang Shi 52626aa2d19SDave Hansen static bool can_demote(int nid, struct scan_control *sc) 52726aa2d19SDave Hansen { 52820b51af1SHuang Ying if (!numa_demotion_enabled) 52920b51af1SHuang Ying return false; 5303a235693SDave Hansen if (sc) { 5313a235693SDave Hansen if (sc->no_demotion) 53226aa2d19SDave Hansen return false; 5333a235693SDave Hansen /* It is pointless to do demotion in memcg reclaim */ 5343a235693SDave Hansen if (cgroup_reclaim(sc)) 5353a235693SDave Hansen return false; 5363a235693SDave Hansen } 53726aa2d19SDave Hansen if (next_demotion_node(nid) == NUMA_NO_NODE) 53826aa2d19SDave Hansen return false; 53926aa2d19SDave Hansen 54020b51af1SHuang Ying return true; 54126aa2d19SDave Hansen } 54226aa2d19SDave Hansen 543a2a36488SKeith Busch static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg, 544a2a36488SKeith Busch int nid, 545a2a36488SKeith Busch struct scan_control *sc) 546a2a36488SKeith Busch { 547a2a36488SKeith Busch if (memcg == NULL) { 548a2a36488SKeith Busch /* 549a2a36488SKeith Busch * For non-memcg reclaim, is there 550a2a36488SKeith Busch * space in any swap device? 551a2a36488SKeith Busch */ 552a2a36488SKeith Busch if (get_nr_swap_pages() > 0) 553a2a36488SKeith Busch return true; 554a2a36488SKeith Busch } else { 555a2a36488SKeith Busch /* Is the memcg below its swap limit? */ 556a2a36488SKeith Busch if (mem_cgroup_get_nr_swap_pages(memcg) > 0) 557a2a36488SKeith Busch return true; 558a2a36488SKeith Busch } 559a2a36488SKeith Busch 560a2a36488SKeith Busch /* 561a2a36488SKeith Busch * The page can not be swapped. 562a2a36488SKeith Busch * 563a2a36488SKeith Busch * Can it be reclaimed from this node via demotion? 564a2a36488SKeith Busch */ 565a2a36488SKeith Busch return can_demote(nid, sc); 566a2a36488SKeith Busch } 567a2a36488SKeith Busch 5685a1c84b4SMel Gorman /* 5695a1c84b4SMel Gorman * This misses isolated pages which are not accounted for to save counters. 5705a1c84b4SMel Gorman * As the data only determines if reclaim or compaction continues, it is 5715a1c84b4SMel Gorman * not expected that isolated pages will be a dominating factor. 5725a1c84b4SMel Gorman */ 5735a1c84b4SMel Gorman unsigned long zone_reclaimable_pages(struct zone *zone) 5745a1c84b4SMel Gorman { 5755a1c84b4SMel Gorman unsigned long nr; 5765a1c84b4SMel Gorman 5775a1c84b4SMel Gorman nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) + 5785a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE); 579a2a36488SKeith Busch if (can_reclaim_anon_pages(NULL, zone_to_nid(zone), NULL)) 5805a1c84b4SMel Gorman nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) + 5815a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON); 5825a1c84b4SMel Gorman 5835a1c84b4SMel Gorman return nr; 5845a1c84b4SMel Gorman } 5855a1c84b4SMel Gorman 586fd538803SMichal Hocko /** 587fd538803SMichal Hocko * lruvec_lru_size - Returns the number of pages on the given LRU list. 588fd538803SMichal Hocko * @lruvec: lru vector 589fd538803SMichal Hocko * @lru: lru to use 590fd538803SMichal Hocko * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list) 591fd538803SMichal Hocko */ 5922091339dSYu Zhao static unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, 5932091339dSYu Zhao int zone_idx) 594c9f299d9SKOSAKI Motohiro { 595de3b0150SJohannes Weiner unsigned long size = 0; 596fd538803SMichal Hocko int zid; 597a3d8e054SKOSAKI Motohiro 598de3b0150SJohannes Weiner for (zid = 0; zid <= zone_idx && zid < MAX_NR_ZONES; zid++) { 599fd538803SMichal Hocko struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; 600fd538803SMichal Hocko 601fd538803SMichal Hocko if (!managed_zone(zone)) 602fd538803SMichal Hocko continue; 603fd538803SMichal Hocko 604fd538803SMichal Hocko if (!mem_cgroup_disabled()) 605de3b0150SJohannes Weiner size += mem_cgroup_get_zone_lru_size(lruvec, lru, zid); 606fd538803SMichal Hocko else 607de3b0150SJohannes Weiner size += zone_page_state(zone, NR_ZONE_LRU_BASE + lru); 608c9f299d9SKOSAKI Motohiro } 609de3b0150SJohannes Weiner return size; 610b4536f0cSMichal Hocko } 611b4536f0cSMichal Hocko 6121da177e4SLinus Torvalds /* 6131d3d4437SGlauber Costa * Add a shrinker callback to be called from the vm. 6141da177e4SLinus Torvalds */ 6158e04944fSTetsuo Handa int prealloc_shrinker(struct shrinker *shrinker) 6161da177e4SLinus Torvalds { 617476b30a0SYang Shi unsigned int size; 618476b30a0SYang Shi int err; 6191d3d4437SGlauber Costa 620476b30a0SYang Shi if (shrinker->flags & SHRINKER_MEMCG_AWARE) { 621476b30a0SYang Shi err = prealloc_memcg_shrinker(shrinker); 622476b30a0SYang Shi if (err != -ENOSYS) 623476b30a0SYang Shi return err; 624476b30a0SYang Shi 625476b30a0SYang Shi shrinker->flags &= ~SHRINKER_MEMCG_AWARE; 626476b30a0SYang Shi } 627476b30a0SYang Shi 628476b30a0SYang Shi size = sizeof(*shrinker->nr_deferred); 6291d3d4437SGlauber Costa if (shrinker->flags & SHRINKER_NUMA_AWARE) 6301d3d4437SGlauber Costa size *= nr_node_ids; 6311d3d4437SGlauber Costa 6321d3d4437SGlauber Costa shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); 6331d3d4437SGlauber Costa if (!shrinker->nr_deferred) 6341d3d4437SGlauber Costa return -ENOMEM; 635b4c2b231SKirill Tkhai 6368e04944fSTetsuo Handa return 0; 6378e04944fSTetsuo Handa } 6381d3d4437SGlauber Costa 6398e04944fSTetsuo Handa void free_prealloced_shrinker(struct shrinker *shrinker) 6408e04944fSTetsuo Handa { 64141ca668aSYang Shi if (shrinker->flags & SHRINKER_MEMCG_AWARE) { 64241ca668aSYang Shi down_write(&shrinker_rwsem); 643b4c2b231SKirill Tkhai unregister_memcg_shrinker(shrinker); 64441ca668aSYang Shi up_write(&shrinker_rwsem); 645476b30a0SYang Shi return; 64641ca668aSYang Shi } 647b4c2b231SKirill Tkhai 6488e04944fSTetsuo Handa kfree(shrinker->nr_deferred); 6498e04944fSTetsuo Handa shrinker->nr_deferred = NULL; 6508e04944fSTetsuo Handa } 6518e04944fSTetsuo Handa 6528e04944fSTetsuo Handa void register_shrinker_prepared(struct shrinker *shrinker) 6538e04944fSTetsuo Handa { 6541da177e4SLinus Torvalds down_write(&shrinker_rwsem); 6551da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 65641ca668aSYang Shi shrinker->flags |= SHRINKER_REGISTERED; 6571da177e4SLinus Torvalds up_write(&shrinker_rwsem); 6588e04944fSTetsuo Handa } 6598e04944fSTetsuo Handa 6608e04944fSTetsuo Handa int register_shrinker(struct shrinker *shrinker) 6618e04944fSTetsuo Handa { 6628e04944fSTetsuo Handa int err = prealloc_shrinker(shrinker); 6638e04944fSTetsuo Handa 6648e04944fSTetsuo Handa if (err) 6658e04944fSTetsuo Handa return err; 6668e04944fSTetsuo Handa register_shrinker_prepared(shrinker); 6671d3d4437SGlauber Costa return 0; 6681da177e4SLinus Torvalds } 6698e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds /* 6721da177e4SLinus Torvalds * Remove one 6731da177e4SLinus Torvalds */ 6748e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 6751da177e4SLinus Torvalds { 67641ca668aSYang Shi if (!(shrinker->flags & SHRINKER_REGISTERED)) 677bb422a73STetsuo Handa return; 67841ca668aSYang Shi 6791da177e4SLinus Torvalds down_write(&shrinker_rwsem); 6801da177e4SLinus Torvalds list_del(&shrinker->list); 68141ca668aSYang Shi shrinker->flags &= ~SHRINKER_REGISTERED; 68241ca668aSYang Shi if (shrinker->flags & SHRINKER_MEMCG_AWARE) 68341ca668aSYang Shi unregister_memcg_shrinker(shrinker); 6841da177e4SLinus Torvalds up_write(&shrinker_rwsem); 68541ca668aSYang Shi 686ae393321SAndrew Vagin kfree(shrinker->nr_deferred); 687bb422a73STetsuo Handa shrinker->nr_deferred = NULL; 6881da177e4SLinus Torvalds } 6898e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 6901da177e4SLinus Torvalds 691880121beSChristian König /** 692880121beSChristian König * synchronize_shrinkers - Wait for all running shrinkers to complete. 693880121beSChristian König * 694880121beSChristian König * This is equivalent to calling unregister_shrink() and register_shrinker(), 695880121beSChristian König * but atomically and with less overhead. This is useful to guarantee that all 696880121beSChristian König * shrinker invocations have seen an update, before freeing memory, similar to 697880121beSChristian König * rcu. 698880121beSChristian König */ 699880121beSChristian König void synchronize_shrinkers(void) 700880121beSChristian König { 701880121beSChristian König down_write(&shrinker_rwsem); 702880121beSChristian König up_write(&shrinker_rwsem); 703880121beSChristian König } 704880121beSChristian König EXPORT_SYMBOL(synchronize_shrinkers); 705880121beSChristian König 7061da177e4SLinus Torvalds #define SHRINK_BATCH 128 7071d3d4437SGlauber Costa 708cb731d6cSVladimir Davydov static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, 7099092c71bSJosef Bacik struct shrinker *shrinker, int priority) 7101da177e4SLinus Torvalds { 71124f7c6b9SDave Chinner unsigned long freed = 0; 7121da177e4SLinus Torvalds unsigned long long delta; 713635697c6SKonstantin Khlebnikov long total_scan; 714d5bc5fd3SVladimir Davydov long freeable; 715acf92b48SDave Chinner long nr; 716acf92b48SDave Chinner long new_nr; 717e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 718e9299f50SDave Chinner : SHRINK_BATCH; 7195f33a080SShaohua Li long scanned = 0, next_deferred; 7201da177e4SLinus Torvalds 721d5bc5fd3SVladimir Davydov freeable = shrinker->count_objects(shrinker, shrinkctl); 7229b996468SKirill Tkhai if (freeable == 0 || freeable == SHRINK_EMPTY) 7239b996468SKirill Tkhai return freeable; 724635697c6SKonstantin Khlebnikov 725acf92b48SDave Chinner /* 726acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 727acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 728acf92b48SDave Chinner * don't also do this scanning work. 729acf92b48SDave Chinner */ 73086750830SYang Shi nr = xchg_nr_deferred(shrinker, shrinkctl); 731acf92b48SDave Chinner 7324b85afbdSJohannes Weiner if (shrinker->seeks) { 7339092c71bSJosef Bacik delta = freeable >> priority; 7349092c71bSJosef Bacik delta *= 4; 7359092c71bSJosef Bacik do_div(delta, shrinker->seeks); 7364b85afbdSJohannes Weiner } else { 7374b85afbdSJohannes Weiner /* 7384b85afbdSJohannes Weiner * These objects don't require any IO to create. Trim 7394b85afbdSJohannes Weiner * them aggressively under memory pressure to keep 7404b85afbdSJohannes Weiner * them from causing refetches in the IO caches. 7414b85afbdSJohannes Weiner */ 7424b85afbdSJohannes Weiner delta = freeable / 2; 7434b85afbdSJohannes Weiner } 744172b06c3SRoman Gushchin 74518bb473eSYang Shi total_scan = nr >> priority; 746acf92b48SDave Chinner total_scan += delta; 74718bb473eSYang Shi total_scan = min(total_scan, (2 * freeable)); 7481da177e4SLinus Torvalds 74924f7c6b9SDave Chinner trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, 7509092c71bSJosef Bacik freeable, delta, total_scan, priority); 75109576073SDave Chinner 7520b1fb40aSVladimir Davydov /* 7530b1fb40aSVladimir Davydov * Normally, we should not scan less than batch_size objects in one 7540b1fb40aSVladimir Davydov * pass to avoid too frequent shrinker calls, but if the slab has less 7550b1fb40aSVladimir Davydov * than batch_size objects in total and we are really tight on memory, 7560b1fb40aSVladimir Davydov * we will try to reclaim all available objects, otherwise we can end 7570b1fb40aSVladimir Davydov * up failing allocations although there are plenty of reclaimable 7580b1fb40aSVladimir Davydov * objects spread over several slabs with usage less than the 7590b1fb40aSVladimir Davydov * batch_size. 7600b1fb40aSVladimir Davydov * 7610b1fb40aSVladimir Davydov * We detect the "tight on memory" situations by looking at the total 7620b1fb40aSVladimir Davydov * number of objects we want to scan (total_scan). If it is greater 763d5bc5fd3SVladimir Davydov * than the total number of objects on slab (freeable), we must be 7640b1fb40aSVladimir Davydov * scanning at high prio and therefore should try to reclaim as much as 7650b1fb40aSVladimir Davydov * possible. 7660b1fb40aSVladimir Davydov */ 7670b1fb40aSVladimir Davydov while (total_scan >= batch_size || 768d5bc5fd3SVladimir Davydov total_scan >= freeable) { 76924f7c6b9SDave Chinner unsigned long ret; 7700b1fb40aSVladimir Davydov unsigned long nr_to_scan = min(batch_size, total_scan); 7711da177e4SLinus Torvalds 7720b1fb40aSVladimir Davydov shrinkctl->nr_to_scan = nr_to_scan; 773d460acb5SChris Wilson shrinkctl->nr_scanned = nr_to_scan; 77424f7c6b9SDave Chinner ret = shrinker->scan_objects(shrinker, shrinkctl); 77524f7c6b9SDave Chinner if (ret == SHRINK_STOP) 7761da177e4SLinus Torvalds break; 77724f7c6b9SDave Chinner freed += ret; 77824f7c6b9SDave Chinner 779d460acb5SChris Wilson count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned); 780d460acb5SChris Wilson total_scan -= shrinkctl->nr_scanned; 781d460acb5SChris Wilson scanned += shrinkctl->nr_scanned; 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds cond_resched(); 7841da177e4SLinus Torvalds } 7851da177e4SLinus Torvalds 78618bb473eSYang Shi /* 78718bb473eSYang Shi * The deferred work is increased by any new work (delta) that wasn't 78818bb473eSYang Shi * done, decreased by old deferred work that was done now. 78918bb473eSYang Shi * 79018bb473eSYang Shi * And it is capped to two times of the freeable items. 79118bb473eSYang Shi */ 79218bb473eSYang Shi next_deferred = max_t(long, (nr + delta - scanned), 0); 79318bb473eSYang Shi next_deferred = min(next_deferred, (2 * freeable)); 79418bb473eSYang Shi 795acf92b48SDave Chinner /* 796acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 79786750830SYang Shi * manner that handles concurrent updates. 798acf92b48SDave Chinner */ 79986750830SYang Shi new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl); 800acf92b48SDave Chinner 8018efb4b59SYang Shi trace_mm_shrink_slab_end(shrinker, shrinkctl->nid, freed, nr, new_nr, total_scan); 8021d3d4437SGlauber Costa return freed; 8031d3d4437SGlauber Costa } 8041d3d4437SGlauber Costa 8050a432dcbSYang Shi #ifdef CONFIG_MEMCG 806b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 807b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 808b0dedc49SKirill Tkhai { 809e4262c4fSYang Shi struct shrinker_info *info; 810b8e57efaSKirill Tkhai unsigned long ret, freed = 0; 811b8e57efaSKirill Tkhai int i; 812b0dedc49SKirill Tkhai 8130a432dcbSYang Shi if (!mem_cgroup_online(memcg)) 814b0dedc49SKirill Tkhai return 0; 815b0dedc49SKirill Tkhai 816b0dedc49SKirill Tkhai if (!down_read_trylock(&shrinker_rwsem)) 817b0dedc49SKirill Tkhai return 0; 818b0dedc49SKirill Tkhai 819468ab843SYang Shi info = shrinker_info_protected(memcg, nid); 820e4262c4fSYang Shi if (unlikely(!info)) 821b0dedc49SKirill Tkhai goto unlock; 822b0dedc49SKirill Tkhai 823e4262c4fSYang Shi for_each_set_bit(i, info->map, shrinker_nr_max) { 824b0dedc49SKirill Tkhai struct shrink_control sc = { 825b0dedc49SKirill Tkhai .gfp_mask = gfp_mask, 826b0dedc49SKirill Tkhai .nid = nid, 827b0dedc49SKirill Tkhai .memcg = memcg, 828b0dedc49SKirill Tkhai }; 829b0dedc49SKirill Tkhai struct shrinker *shrinker; 830b0dedc49SKirill Tkhai 831b0dedc49SKirill Tkhai shrinker = idr_find(&shrinker_idr, i); 83241ca668aSYang Shi if (unlikely(!shrinker || !(shrinker->flags & SHRINKER_REGISTERED))) { 8337e010df5SKirill Tkhai if (!shrinker) 834e4262c4fSYang Shi clear_bit(i, info->map); 835b0dedc49SKirill Tkhai continue; 836b0dedc49SKirill Tkhai } 837b0dedc49SKirill Tkhai 8380a432dcbSYang Shi /* Call non-slab shrinkers even though kmem is disabled */ 8390a432dcbSYang Shi if (!memcg_kmem_enabled() && 8400a432dcbSYang Shi !(shrinker->flags & SHRINKER_NONSLAB)) 8410a432dcbSYang Shi continue; 8420a432dcbSYang Shi 843b0dedc49SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 844f90280d6SKirill Tkhai if (ret == SHRINK_EMPTY) { 845e4262c4fSYang Shi clear_bit(i, info->map); 846f90280d6SKirill Tkhai /* 847f90280d6SKirill Tkhai * After the shrinker reported that it had no objects to 848f90280d6SKirill Tkhai * free, but before we cleared the corresponding bit in 849f90280d6SKirill Tkhai * the memcg shrinker map, a new object might have been 850f90280d6SKirill Tkhai * added. To make sure, we have the bit set in this 851f90280d6SKirill Tkhai * case, we invoke the shrinker one more time and reset 852f90280d6SKirill Tkhai * the bit if it reports that it is not empty anymore. 853f90280d6SKirill Tkhai * The memory barrier here pairs with the barrier in 8542bfd3637SYang Shi * set_shrinker_bit(): 855f90280d6SKirill Tkhai * 856f90280d6SKirill Tkhai * list_lru_add() shrink_slab_memcg() 857f90280d6SKirill Tkhai * list_add_tail() clear_bit() 858f90280d6SKirill Tkhai * <MB> <MB> 859f90280d6SKirill Tkhai * set_bit() do_shrink_slab() 860f90280d6SKirill Tkhai */ 861f90280d6SKirill Tkhai smp_mb__after_atomic(); 862f90280d6SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 8639b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 8649b996468SKirill Tkhai ret = 0; 865f90280d6SKirill Tkhai else 8662bfd3637SYang Shi set_shrinker_bit(memcg, nid, i); 867f90280d6SKirill Tkhai } 868b0dedc49SKirill Tkhai freed += ret; 869b0dedc49SKirill Tkhai 870b0dedc49SKirill Tkhai if (rwsem_is_contended(&shrinker_rwsem)) { 871b0dedc49SKirill Tkhai freed = freed ? : 1; 872b0dedc49SKirill Tkhai break; 873b0dedc49SKirill Tkhai } 874b0dedc49SKirill Tkhai } 875b0dedc49SKirill Tkhai unlock: 876b0dedc49SKirill Tkhai up_read(&shrinker_rwsem); 877b0dedc49SKirill Tkhai return freed; 878b0dedc49SKirill Tkhai } 8790a432dcbSYang Shi #else /* CONFIG_MEMCG */ 880b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 881b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 882b0dedc49SKirill Tkhai { 883b0dedc49SKirill Tkhai return 0; 884b0dedc49SKirill Tkhai } 8850a432dcbSYang Shi #endif /* CONFIG_MEMCG */ 886b0dedc49SKirill Tkhai 8876b4f7799SJohannes Weiner /** 888cb731d6cSVladimir Davydov * shrink_slab - shrink slab caches 8896b4f7799SJohannes Weiner * @gfp_mask: allocation context 8906b4f7799SJohannes Weiner * @nid: node whose slab caches to target 891cb731d6cSVladimir Davydov * @memcg: memory cgroup whose slab caches to target 8929092c71bSJosef Bacik * @priority: the reclaim priority 8931d3d4437SGlauber Costa * 8946b4f7799SJohannes Weiner * Call the shrink functions to age shrinkable caches. 8951d3d4437SGlauber Costa * 8966b4f7799SJohannes Weiner * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, 8976b4f7799SJohannes Weiner * unaware shrinkers will receive a node id of 0 instead. 8981d3d4437SGlauber Costa * 899aeed1d32SVladimir Davydov * @memcg specifies the memory cgroup to target. Unaware shrinkers 900aeed1d32SVladimir Davydov * are called only if it is the root cgroup. 901cb731d6cSVladimir Davydov * 9029092c71bSJosef Bacik * @priority is sc->priority, we take the number of objects and >> by priority 9039092c71bSJosef Bacik * in order to get the scan target. 9041d3d4437SGlauber Costa * 9056b4f7799SJohannes Weiner * Returns the number of reclaimed slab objects. 9061d3d4437SGlauber Costa */ 907cb731d6cSVladimir Davydov static unsigned long shrink_slab(gfp_t gfp_mask, int nid, 908cb731d6cSVladimir Davydov struct mem_cgroup *memcg, 9099092c71bSJosef Bacik int priority) 9101d3d4437SGlauber Costa { 911b8e57efaSKirill Tkhai unsigned long ret, freed = 0; 9121d3d4437SGlauber Costa struct shrinker *shrinker; 9131d3d4437SGlauber Costa 914fa1e512fSYang Shi /* 915fa1e512fSYang Shi * The root memcg might be allocated even though memcg is disabled 916fa1e512fSYang Shi * via "cgroup_disable=memory" boot parameter. This could make 917fa1e512fSYang Shi * mem_cgroup_is_root() return false, then just run memcg slab 918fa1e512fSYang Shi * shrink, but skip global shrink. This may result in premature 919fa1e512fSYang Shi * oom. 920fa1e512fSYang Shi */ 921fa1e512fSYang Shi if (!mem_cgroup_disabled() && !mem_cgroup_is_root(memcg)) 922b0dedc49SKirill Tkhai return shrink_slab_memcg(gfp_mask, nid, memcg, priority); 923cb731d6cSVladimir Davydov 924e830c63aSTetsuo Handa if (!down_read_trylock(&shrinker_rwsem)) 9251d3d4437SGlauber Costa goto out; 9261d3d4437SGlauber Costa 9271d3d4437SGlauber Costa list_for_each_entry(shrinker, &shrinker_list, list) { 9286b4f7799SJohannes Weiner struct shrink_control sc = { 9296b4f7799SJohannes Weiner .gfp_mask = gfp_mask, 9306b4f7799SJohannes Weiner .nid = nid, 931cb731d6cSVladimir Davydov .memcg = memcg, 9326b4f7799SJohannes Weiner }; 9336b4f7799SJohannes Weiner 9349b996468SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 9359b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 9369b996468SKirill Tkhai ret = 0; 9379b996468SKirill Tkhai freed += ret; 938e496612cSMinchan Kim /* 939e496612cSMinchan Kim * Bail out if someone want to register a new shrinker to 94055b65a57SEthon Paul * prevent the registration from being stalled for long periods 941e496612cSMinchan Kim * by parallel ongoing shrinking. 942e496612cSMinchan Kim */ 943e496612cSMinchan Kim if (rwsem_is_contended(&shrinker_rwsem)) { 944e496612cSMinchan Kim freed = freed ? : 1; 945e496612cSMinchan Kim break; 946e496612cSMinchan Kim } 947ec97097bSVladimir Davydov } 9481d3d4437SGlauber Costa 9491da177e4SLinus Torvalds up_read(&shrinker_rwsem); 950f06590bdSMinchan Kim out: 951f06590bdSMinchan Kim cond_resched(); 95224f7c6b9SDave Chinner return freed; 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds 955e4b424b7SGang Li static void drop_slab_node(int nid) 956cb731d6cSVladimir Davydov { 957cb731d6cSVladimir Davydov unsigned long freed; 9581399af7eSVlastimil Babka int shift = 0; 959cb731d6cSVladimir Davydov 960cb731d6cSVladimir Davydov do { 961cb731d6cSVladimir Davydov struct mem_cgroup *memcg = NULL; 962cb731d6cSVladimir Davydov 963069c411dSChunxin Zang if (fatal_signal_pending(current)) 964069c411dSChunxin Zang return; 965069c411dSChunxin Zang 966cb731d6cSVladimir Davydov freed = 0; 967aeed1d32SVladimir Davydov memcg = mem_cgroup_iter(NULL, NULL, NULL); 968cb731d6cSVladimir Davydov do { 9699092c71bSJosef Bacik freed += shrink_slab(GFP_KERNEL, nid, memcg, 0); 970cb731d6cSVladimir Davydov } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 9711399af7eSVlastimil Babka } while ((freed >> shift++) > 1); 972cb731d6cSVladimir Davydov } 973cb731d6cSVladimir Davydov 974cb731d6cSVladimir Davydov void drop_slab(void) 975cb731d6cSVladimir Davydov { 976cb731d6cSVladimir Davydov int nid; 977cb731d6cSVladimir Davydov 978cb731d6cSVladimir Davydov for_each_online_node(nid) 979cb731d6cSVladimir Davydov drop_slab_node(nid); 980cb731d6cSVladimir Davydov } 981cb731d6cSVladimir Davydov 982e0cd5e7fSMatthew Wilcox (Oracle) static inline int is_page_cache_freeable(struct folio *folio) 9831da177e4SLinus Torvalds { 984ceddc3a5SJohannes Weiner /* 985ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 98667891fffSMatthew Wilcox * that isolated the page, the page cache and optional buffer 98767891fffSMatthew Wilcox * heads at page->private. 988ceddc3a5SJohannes Weiner */ 989e0cd5e7fSMatthew Wilcox (Oracle) return folio_ref_count(folio) - folio_test_private(folio) == 990e0cd5e7fSMatthew Wilcox (Oracle) 1 + folio_nr_pages(folio); 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds /* 994e0cd5e7fSMatthew Wilcox (Oracle) * We detected a synchronous write error writing a folio out. Probably 9951da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 9961da177e4SLinus Torvalds * fsync(), msync() or close(). 9971da177e4SLinus Torvalds * 9981da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 999e0cd5e7fSMatthew Wilcox (Oracle) * prevents it from being freed up. But we have a ref on the folio and once 1000e0cd5e7fSMatthew Wilcox (Oracle) * that folio is locked, the mapping is pinned. 10011da177e4SLinus Torvalds * 1002e0cd5e7fSMatthew Wilcox (Oracle) * We're allowed to run sleeping folio_lock() here because we know the caller has 10031da177e4SLinus Torvalds * __GFP_FS. 10041da177e4SLinus Torvalds */ 10051da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 1006e0cd5e7fSMatthew Wilcox (Oracle) struct folio *folio, int error) 10071da177e4SLinus Torvalds { 1008e0cd5e7fSMatthew Wilcox (Oracle) folio_lock(folio); 1009e0cd5e7fSMatthew Wilcox (Oracle) if (folio_mapping(folio) == mapping) 10103e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 1011e0cd5e7fSMatthew Wilcox (Oracle) folio_unlock(folio); 10121da177e4SLinus Torvalds } 10131da177e4SLinus Torvalds 10141b4e3f26SMel Gorman static bool skip_throttle_noprogress(pg_data_t *pgdat) 10151b4e3f26SMel Gorman { 10161b4e3f26SMel Gorman int reclaimable = 0, write_pending = 0; 10171b4e3f26SMel Gorman int i; 10181b4e3f26SMel Gorman 10191b4e3f26SMel Gorman /* 10201b4e3f26SMel Gorman * If kswapd is disabled, reschedule if necessary but do not 10211b4e3f26SMel Gorman * throttle as the system is likely near OOM. 10221b4e3f26SMel Gorman */ 10231b4e3f26SMel Gorman if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 10241b4e3f26SMel Gorman return true; 10251b4e3f26SMel Gorman 10261b4e3f26SMel Gorman /* 10271b4e3f26SMel Gorman * If there are a lot of dirty/writeback pages then do not 10281b4e3f26SMel Gorman * throttle as throttling will occur when the pages cycle 10291b4e3f26SMel Gorman * towards the end of the LRU if still under writeback. 10301b4e3f26SMel Gorman */ 10311b4e3f26SMel Gorman for (i = 0; i < MAX_NR_ZONES; i++) { 10321b4e3f26SMel Gorman struct zone *zone = pgdat->node_zones + i; 10331b4e3f26SMel Gorman 10341b4e3f26SMel Gorman if (!populated_zone(zone)) 10351b4e3f26SMel Gorman continue; 10361b4e3f26SMel Gorman 10371b4e3f26SMel Gorman reclaimable += zone_reclaimable_pages(zone); 10381b4e3f26SMel Gorman write_pending += zone_page_state_snapshot(zone, 10391b4e3f26SMel Gorman NR_ZONE_WRITE_PENDING); 10401b4e3f26SMel Gorman } 10411b4e3f26SMel Gorman if (2 * write_pending <= reclaimable) 10421b4e3f26SMel Gorman return true; 10431b4e3f26SMel Gorman 10441b4e3f26SMel Gorman return false; 10451b4e3f26SMel Gorman } 10461b4e3f26SMel Gorman 1047c3f4a9a2SMel Gorman void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason) 10488cd7c588SMel Gorman { 10498cd7c588SMel Gorman wait_queue_head_t *wqh = &pgdat->reclaim_wait[reason]; 1050c3f4a9a2SMel Gorman long timeout, ret; 10518cd7c588SMel Gorman DEFINE_WAIT(wait); 10528cd7c588SMel Gorman 10538cd7c588SMel Gorman /* 10548cd7c588SMel Gorman * Do not throttle IO workers, kthreads other than kswapd or 10558cd7c588SMel Gorman * workqueues. They may be required for reclaim to make 10568cd7c588SMel Gorman * forward progress (e.g. journalling workqueues or kthreads). 10578cd7c588SMel Gorman */ 10588cd7c588SMel Gorman if (!current_is_kswapd() && 1059b485c6f1SMel Gorman current->flags & (PF_IO_WORKER|PF_KTHREAD)) { 1060b485c6f1SMel Gorman cond_resched(); 10618cd7c588SMel Gorman return; 1062b485c6f1SMel Gorman } 10638cd7c588SMel Gorman 1064c3f4a9a2SMel Gorman /* 1065c3f4a9a2SMel Gorman * These figures are pulled out of thin air. 1066c3f4a9a2SMel Gorman * VMSCAN_THROTTLE_ISOLATED is a transient condition based on too many 1067c3f4a9a2SMel Gorman * parallel reclaimers which is a short-lived event so the timeout is 1068c3f4a9a2SMel Gorman * short. Failing to make progress or waiting on writeback are 1069c3f4a9a2SMel Gorman * potentially long-lived events so use a longer timeout. This is shaky 1070c3f4a9a2SMel Gorman * logic as a failure to make progress could be due to anything from 1071c3f4a9a2SMel Gorman * writeback to a slow device to excessive references pages at the tail 1072c3f4a9a2SMel Gorman * of the inactive LRU. 1073c3f4a9a2SMel Gorman */ 1074c3f4a9a2SMel Gorman switch(reason) { 1075c3f4a9a2SMel Gorman case VMSCAN_THROTTLE_WRITEBACK: 1076c3f4a9a2SMel Gorman timeout = HZ/10; 1077c3f4a9a2SMel Gorman 1078c3f4a9a2SMel Gorman if (atomic_inc_return(&pgdat->nr_writeback_throttled) == 1) { 10798cd7c588SMel Gorman WRITE_ONCE(pgdat->nr_reclaim_start, 10808cd7c588SMel Gorman node_page_state(pgdat, NR_THROTTLED_WRITTEN)); 10818cd7c588SMel Gorman } 10828cd7c588SMel Gorman 1083c3f4a9a2SMel Gorman break; 10841b4e3f26SMel Gorman case VMSCAN_THROTTLE_CONGESTED: 10851b4e3f26SMel Gorman fallthrough; 1086c3f4a9a2SMel Gorman case VMSCAN_THROTTLE_NOPROGRESS: 10871b4e3f26SMel Gorman if (skip_throttle_noprogress(pgdat)) { 10881b4e3f26SMel Gorman cond_resched(); 10891b4e3f26SMel Gorman return; 10901b4e3f26SMel Gorman } 10911b4e3f26SMel Gorman 10921b4e3f26SMel Gorman timeout = 1; 10931b4e3f26SMel Gorman 1094c3f4a9a2SMel Gorman break; 1095c3f4a9a2SMel Gorman case VMSCAN_THROTTLE_ISOLATED: 1096c3f4a9a2SMel Gorman timeout = HZ/50; 1097c3f4a9a2SMel Gorman break; 1098c3f4a9a2SMel Gorman default: 1099c3f4a9a2SMel Gorman WARN_ON_ONCE(1); 1100c3f4a9a2SMel Gorman timeout = HZ; 1101c3f4a9a2SMel Gorman break; 1102c3f4a9a2SMel Gorman } 1103c3f4a9a2SMel Gorman 11048cd7c588SMel Gorman prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); 11058cd7c588SMel Gorman ret = schedule_timeout(timeout); 11068cd7c588SMel Gorman finish_wait(wqh, &wait); 1107d818fca1SMel Gorman 1108c3f4a9a2SMel Gorman if (reason == VMSCAN_THROTTLE_WRITEBACK) 11098cd7c588SMel Gorman atomic_dec(&pgdat->nr_writeback_throttled); 11108cd7c588SMel Gorman 11118cd7c588SMel Gorman trace_mm_vmscan_throttled(pgdat->node_id, jiffies_to_usecs(timeout), 11128cd7c588SMel Gorman jiffies_to_usecs(timeout - ret), 11138cd7c588SMel Gorman reason); 11148cd7c588SMel Gorman } 11158cd7c588SMel Gorman 11168cd7c588SMel Gorman /* 11178cd7c588SMel Gorman * Account for pages written if tasks are throttled waiting on dirty 11188cd7c588SMel Gorman * pages to clean. If enough pages have been cleaned since throttling 11198cd7c588SMel Gorman * started then wakeup the throttled tasks. 11208cd7c588SMel Gorman */ 1121512b7931SLinus Torvalds void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio, 11228cd7c588SMel Gorman int nr_throttled) 11238cd7c588SMel Gorman { 11248cd7c588SMel Gorman unsigned long nr_written; 11258cd7c588SMel Gorman 1126512b7931SLinus Torvalds node_stat_add_folio(folio, NR_THROTTLED_WRITTEN); 11278cd7c588SMel Gorman 11288cd7c588SMel Gorman /* 11298cd7c588SMel Gorman * This is an inaccurate read as the per-cpu deltas may not 11308cd7c588SMel Gorman * be synchronised. However, given that the system is 11318cd7c588SMel Gorman * writeback throttled, it is not worth taking the penalty 11328cd7c588SMel Gorman * of getting an accurate count. At worst, the throttle 11338cd7c588SMel Gorman * timeout guarantees forward progress. 11348cd7c588SMel Gorman */ 11358cd7c588SMel Gorman nr_written = node_page_state(pgdat, NR_THROTTLED_WRITTEN) - 11368cd7c588SMel Gorman READ_ONCE(pgdat->nr_reclaim_start); 11378cd7c588SMel Gorman 11388cd7c588SMel Gorman if (nr_written > SWAP_CLUSTER_MAX * nr_throttled) 11398cd7c588SMel Gorman wake_up(&pgdat->reclaim_wait[VMSCAN_THROTTLE_WRITEBACK]); 11408cd7c588SMel Gorman } 11418cd7c588SMel Gorman 114204e62a29SChristoph Lameter /* possible outcome of pageout() */ 114304e62a29SChristoph Lameter typedef enum { 114404e62a29SChristoph Lameter /* failed to write page out, page is locked */ 114504e62a29SChristoph Lameter PAGE_KEEP, 114604e62a29SChristoph Lameter /* move page to the active list, page is locked */ 114704e62a29SChristoph Lameter PAGE_ACTIVATE, 114804e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 114904e62a29SChristoph Lameter PAGE_SUCCESS, 115004e62a29SChristoph Lameter /* page is clean and locked */ 115104e62a29SChristoph Lameter PAGE_CLEAN, 115204e62a29SChristoph Lameter } pageout_t; 115304e62a29SChristoph Lameter 11541da177e4SLinus Torvalds /* 11551742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 11561742f19fSAndrew Morton * Calls ->writepage(). 11571da177e4SLinus Torvalds */ 1158e0cd5e7fSMatthew Wilcox (Oracle) static pageout_t pageout(struct folio *folio, struct address_space *mapping) 11591da177e4SLinus Torvalds { 11601da177e4SLinus Torvalds /* 1161e0cd5e7fSMatthew Wilcox (Oracle) * If the folio is dirty, only perform writeback if that write 11621da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 11631da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 11641da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 11651da177e4SLinus Torvalds * PagePrivate for that. 11661da177e4SLinus Torvalds * 11678174202bSAl Viro * If this process is currently in __generic_file_write_iter() against 1168e0cd5e7fSMatthew Wilcox (Oracle) * this folio's queue, we can perform writeback even if that 11691da177e4SLinus Torvalds * will block. 11701da177e4SLinus Torvalds * 1171e0cd5e7fSMatthew Wilcox (Oracle) * If the folio is swapcache, write it back even if that would 11721da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 11731da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 11741da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 11751da177e4SLinus Torvalds */ 1176e0cd5e7fSMatthew Wilcox (Oracle) if (!is_page_cache_freeable(folio)) 11771da177e4SLinus Torvalds return PAGE_KEEP; 11781da177e4SLinus Torvalds if (!mapping) { 11791da177e4SLinus Torvalds /* 1180e0cd5e7fSMatthew Wilcox (Oracle) * Some data journaling orphaned folios can have 1181e0cd5e7fSMatthew Wilcox (Oracle) * folio->mapping == NULL while being dirty with clean buffers. 11821da177e4SLinus Torvalds */ 1183e0cd5e7fSMatthew Wilcox (Oracle) if (folio_test_private(folio)) { 1184e0cd5e7fSMatthew Wilcox (Oracle) if (try_to_free_buffers(&folio->page)) { 1185e0cd5e7fSMatthew Wilcox (Oracle) folio_clear_dirty(folio); 1186e0cd5e7fSMatthew Wilcox (Oracle) pr_info("%s: orphaned folio\n", __func__); 11871da177e4SLinus Torvalds return PAGE_CLEAN; 11881da177e4SLinus Torvalds } 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds return PAGE_KEEP; 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 11931da177e4SLinus Torvalds return PAGE_ACTIVATE; 11941da177e4SLinus Torvalds 1195e0cd5e7fSMatthew Wilcox (Oracle) if (folio_clear_dirty_for_io(folio)) { 11961da177e4SLinus Torvalds int res; 11971da177e4SLinus Torvalds struct writeback_control wbc = { 11981da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 11991da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 1200111ebb6eSOGAWA Hirofumi .range_start = 0, 1201111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 12021da177e4SLinus Torvalds .for_reclaim = 1, 12031da177e4SLinus Torvalds }; 12041da177e4SLinus Torvalds 1205e0cd5e7fSMatthew Wilcox (Oracle) folio_set_reclaim(folio); 1206e0cd5e7fSMatthew Wilcox (Oracle) res = mapping->a_ops->writepage(&folio->page, &wbc); 12071da177e4SLinus Torvalds if (res < 0) 1208e0cd5e7fSMatthew Wilcox (Oracle) handle_write_error(mapping, folio, res); 1209994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 1210e0cd5e7fSMatthew Wilcox (Oracle) folio_clear_reclaim(folio); 12111da177e4SLinus Torvalds return PAGE_ACTIVATE; 12121da177e4SLinus Torvalds } 1213c661b078SAndy Whitcroft 1214e0cd5e7fSMatthew Wilcox (Oracle) if (!folio_test_writeback(folio)) { 12151da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 1216e0cd5e7fSMatthew Wilcox (Oracle) folio_clear_reclaim(folio); 12171da177e4SLinus Torvalds } 1218e0cd5e7fSMatthew Wilcox (Oracle) trace_mm_vmscan_write_folio(folio); 1219e0cd5e7fSMatthew Wilcox (Oracle) node_stat_add_folio(folio, NR_VMSCAN_WRITE); 12201da177e4SLinus Torvalds return PAGE_SUCCESS; 12211da177e4SLinus Torvalds } 12221da177e4SLinus Torvalds 12231da177e4SLinus Torvalds return PAGE_CLEAN; 12241da177e4SLinus Torvalds } 12251da177e4SLinus Torvalds 1226a649fd92SAndrew Morton /* 1227e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 1228e286781dSNick Piggin * gets returned with a refcount of 0. 1229a649fd92SAndrew Morton */ 1230be7c07d6SMatthew Wilcox (Oracle) static int __remove_mapping(struct address_space *mapping, struct folio *folio, 1231b910718aSJohannes Weiner bool reclaimed, struct mem_cgroup *target_memcg) 123249d2e9ccSChristoph Lameter { 1233bd4c82c2SHuang Ying int refcount; 1234aae466b0SJoonsoo Kim void *shadow = NULL; 1235c4843a75SGreg Thelen 1236be7c07d6SMatthew Wilcox (Oracle) BUG_ON(!folio_test_locked(folio)); 1237be7c07d6SMatthew Wilcox (Oracle) BUG_ON(mapping != folio_mapping(folio)); 123849d2e9ccSChristoph Lameter 1239be7c07d6SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio)) 124051b8c1feSJohannes Weiner spin_lock(&mapping->host->i_lock); 124130472509SJohannes Weiner xa_lock_irq(&mapping->i_pages); 124249d2e9ccSChristoph Lameter /* 12430fd0e6b0SNick Piggin * The non racy check for a busy page. 12440fd0e6b0SNick Piggin * 12450fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 12460fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 12470fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 12480fd0e6b0SNick Piggin * here, then the following race may occur: 12490fd0e6b0SNick Piggin * 12500fd0e6b0SNick Piggin * get_user_pages(&page); 12510fd0e6b0SNick Piggin * [user mapping goes away] 12520fd0e6b0SNick Piggin * write_to(page); 12530fd0e6b0SNick Piggin * !PageDirty(page) [good] 12540fd0e6b0SNick Piggin * SetPageDirty(page); 12550fd0e6b0SNick Piggin * put_page(page); 12560fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 12570fd0e6b0SNick Piggin * 12580fd0e6b0SNick Piggin * [oops, our write_to data is lost] 12590fd0e6b0SNick Piggin * 12600fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 12610fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 12620139aa7bSJoonsoo Kim * load is not satisfied before that of page->_refcount. 12630fd0e6b0SNick Piggin * 12640fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 1265b93b0163SMatthew Wilcox * and thus under the i_pages lock, then this ordering is not required. 126649d2e9ccSChristoph Lameter */ 1267be7c07d6SMatthew Wilcox (Oracle) refcount = 1 + folio_nr_pages(folio); 1268be7c07d6SMatthew Wilcox (Oracle) if (!folio_ref_freeze(folio, refcount)) 126949d2e9ccSChristoph Lameter goto cannot_free; 12701c4c3b99SJiang Biao /* note: atomic_cmpxchg in page_ref_freeze provides the smp_rmb */ 1271be7c07d6SMatthew Wilcox (Oracle) if (unlikely(folio_test_dirty(folio))) { 1272be7c07d6SMatthew Wilcox (Oracle) folio_ref_unfreeze(folio, refcount); 127349d2e9ccSChristoph Lameter goto cannot_free; 1274e286781dSNick Piggin } 127549d2e9ccSChristoph Lameter 1276be7c07d6SMatthew Wilcox (Oracle) if (folio_test_swapcache(folio)) { 1277be7c07d6SMatthew Wilcox (Oracle) swp_entry_t swap = folio_swap_entry(folio); 12783ecb0087SMatthew Wilcox (Oracle) mem_cgroup_swapout(folio, swap); 1279aae466b0SJoonsoo Kim if (reclaimed && !mapping_exiting(mapping)) 12808927f647SMatthew Wilcox (Oracle) shadow = workingset_eviction(folio, target_memcg); 1281be7c07d6SMatthew Wilcox (Oracle) __delete_from_swap_cache(&folio->page, swap, shadow); 128230472509SJohannes Weiner xa_unlock_irq(&mapping->i_pages); 1283be7c07d6SMatthew Wilcox (Oracle) put_swap_page(&folio->page, swap); 1284e286781dSNick Piggin } else { 12856072d13cSLinus Torvalds void (*freepage)(struct page *); 12866072d13cSLinus Torvalds 12876072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 1288a528910eSJohannes Weiner /* 1289a528910eSJohannes Weiner * Remember a shadow entry for reclaimed file cache in 1290a528910eSJohannes Weiner * order to detect refaults, thus thrashing, later on. 1291a528910eSJohannes Weiner * 1292a528910eSJohannes Weiner * But don't store shadows in an address space that is 1293238c3046Sdylan-meiners * already exiting. This is not just an optimization, 1294a528910eSJohannes Weiner * inode reclaim needs to empty out the radix tree or 1295a528910eSJohannes Weiner * the nodes are lost. Don't plant shadows behind its 1296a528910eSJohannes Weiner * back. 1297f9fe48beSRoss Zwisler * 1298f9fe48beSRoss Zwisler * We also don't store shadows for DAX mappings because the 1299f9fe48beSRoss Zwisler * only page cache pages found in these are zero pages 1300f9fe48beSRoss Zwisler * covering holes, and because we don't want to mix DAX 1301f9fe48beSRoss Zwisler * exceptional entries and shadow exceptional entries in the 1302b93b0163SMatthew Wilcox * same address_space. 1303a528910eSJohannes Weiner */ 1304be7c07d6SMatthew Wilcox (Oracle) if (reclaimed && folio_is_file_lru(folio) && 1305f9fe48beSRoss Zwisler !mapping_exiting(mapping) && !dax_mapping(mapping)) 13068927f647SMatthew Wilcox (Oracle) shadow = workingset_eviction(folio, target_memcg); 13078927f647SMatthew Wilcox (Oracle) __filemap_remove_folio(folio, shadow); 130830472509SJohannes Weiner xa_unlock_irq(&mapping->i_pages); 130951b8c1feSJohannes Weiner if (mapping_shrinkable(mapping)) 131051b8c1feSJohannes Weiner inode_add_lru(mapping->host); 131151b8c1feSJohannes Weiner spin_unlock(&mapping->host->i_lock); 13126072d13cSLinus Torvalds 13136072d13cSLinus Torvalds if (freepage != NULL) 1314be7c07d6SMatthew Wilcox (Oracle) freepage(&folio->page); 1315e286781dSNick Piggin } 1316e286781dSNick Piggin 131749d2e9ccSChristoph Lameter return 1; 131849d2e9ccSChristoph Lameter 131949d2e9ccSChristoph Lameter cannot_free: 132030472509SJohannes Weiner xa_unlock_irq(&mapping->i_pages); 1321be7c07d6SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio)) 132251b8c1feSJohannes Weiner spin_unlock(&mapping->host->i_lock); 132349d2e9ccSChristoph Lameter return 0; 132449d2e9ccSChristoph Lameter } 132549d2e9ccSChristoph Lameter 13265100da38SMatthew Wilcox (Oracle) /** 13275100da38SMatthew Wilcox (Oracle) * remove_mapping() - Attempt to remove a folio from its mapping. 13285100da38SMatthew Wilcox (Oracle) * @mapping: The address space. 13295100da38SMatthew Wilcox (Oracle) * @folio: The folio to remove. 13305100da38SMatthew Wilcox (Oracle) * 13315100da38SMatthew Wilcox (Oracle) * If the folio is dirty, under writeback or if someone else has a ref 13325100da38SMatthew Wilcox (Oracle) * on it, removal will fail. 13335100da38SMatthew Wilcox (Oracle) * Return: The number of pages removed from the mapping. 0 if the folio 13345100da38SMatthew Wilcox (Oracle) * could not be removed. 13355100da38SMatthew Wilcox (Oracle) * Context: The caller should have a single refcount on the folio and 13365100da38SMatthew Wilcox (Oracle) * hold its lock. 1337e286781dSNick Piggin */ 13385100da38SMatthew Wilcox (Oracle) long remove_mapping(struct address_space *mapping, struct folio *folio) 1339e286781dSNick Piggin { 1340be7c07d6SMatthew Wilcox (Oracle) if (__remove_mapping(mapping, folio, false, NULL)) { 1341e286781dSNick Piggin /* 13425100da38SMatthew Wilcox (Oracle) * Unfreezing the refcount with 1 effectively 1343e286781dSNick Piggin * drops the pagecache ref for us without requiring another 1344e286781dSNick Piggin * atomic operation. 1345e286781dSNick Piggin */ 1346be7c07d6SMatthew Wilcox (Oracle) folio_ref_unfreeze(folio, 1); 13475100da38SMatthew Wilcox (Oracle) return folio_nr_pages(folio); 1348e286781dSNick Piggin } 1349e286781dSNick Piggin return 0; 1350e286781dSNick Piggin } 1351e286781dSNick Piggin 1352894bc310SLee Schermerhorn /** 1353ca6d60f3SMatthew Wilcox (Oracle) * folio_putback_lru - Put previously isolated folio onto appropriate LRU list. 1354ca6d60f3SMatthew Wilcox (Oracle) * @folio: Folio to be returned to an LRU list. 1355894bc310SLee Schermerhorn * 1356ca6d60f3SMatthew Wilcox (Oracle) * Add previously isolated @folio to appropriate LRU list. 1357ca6d60f3SMatthew Wilcox (Oracle) * The folio may still be unevictable for other reasons. 1358894bc310SLee Schermerhorn * 1359ca6d60f3SMatthew Wilcox (Oracle) * Context: lru_lock must not be held, interrupts must be enabled. 1360894bc310SLee Schermerhorn */ 1361ca6d60f3SMatthew Wilcox (Oracle) void folio_putback_lru(struct folio *folio) 1362894bc310SLee Schermerhorn { 1363ca6d60f3SMatthew Wilcox (Oracle) folio_add_lru(folio); 1364ca6d60f3SMatthew Wilcox (Oracle) folio_put(folio); /* drop ref from isolate */ 1365894bc310SLee Schermerhorn } 1366894bc310SLee Schermerhorn 1367dfc8d636SJohannes Weiner enum page_references { 1368dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 1369dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 137064574746SJohannes Weiner PAGEREF_KEEP, 1371dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 1372dfc8d636SJohannes Weiner }; 1373dfc8d636SJohannes Weiner 1374d92013d1SMatthew Wilcox (Oracle) static enum page_references folio_check_references(struct folio *folio, 1375dfc8d636SJohannes Weiner struct scan_control *sc) 1376dfc8d636SJohannes Weiner { 1377d92013d1SMatthew Wilcox (Oracle) int referenced_ptes, referenced_folio; 1378dfc8d636SJohannes Weiner unsigned long vm_flags; 1379dfc8d636SJohannes Weiner 1380b3ac0413SMatthew Wilcox (Oracle) referenced_ptes = folio_referenced(folio, 1, sc->target_mem_cgroup, 1381c3ac9a8aSJohannes Weiner &vm_flags); 1382d92013d1SMatthew Wilcox (Oracle) referenced_folio = folio_test_clear_referenced(folio); 1383dfc8d636SJohannes Weiner 1384dfc8d636SJohannes Weiner /* 1385d92013d1SMatthew Wilcox (Oracle) * The supposedly reclaimable folio was found to be in a VM_LOCKED vma. 1386d92013d1SMatthew Wilcox (Oracle) * Let the folio, now marked Mlocked, be moved to the unevictable list. 1387dfc8d636SJohannes Weiner */ 1388dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 138947d4f3eeSHugh Dickins return PAGEREF_ACTIVATE; 1390dfc8d636SJohannes Weiner 139164574746SJohannes Weiner if (referenced_ptes) { 139264574746SJohannes Weiner /* 1393d92013d1SMatthew Wilcox (Oracle) * All mapped folios start out with page table 139464574746SJohannes Weiner * references from the instantiating fault, so we need 1395*9030fb0bSLinus Torvalds * to look twice if a mapped file/anon folio is used more 139664574746SJohannes Weiner * than once. 139764574746SJohannes Weiner * 139864574746SJohannes Weiner * Mark it and spare it for another trip around the 139964574746SJohannes Weiner * inactive list. Another page table reference will 140064574746SJohannes Weiner * lead to its activation. 140164574746SJohannes Weiner * 1402d92013d1SMatthew Wilcox (Oracle) * Note: the mark is set for activated folios as well 1403d92013d1SMatthew Wilcox (Oracle) * so that recently deactivated but used folios are 140464574746SJohannes Weiner * quickly recovered. 140564574746SJohannes Weiner */ 1406d92013d1SMatthew Wilcox (Oracle) folio_set_referenced(folio); 140764574746SJohannes Weiner 1408d92013d1SMatthew Wilcox (Oracle) if (referenced_folio || referenced_ptes > 1) 1409dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 1410dfc8d636SJohannes Weiner 1411c909e993SKonstantin Khlebnikov /* 1412d92013d1SMatthew Wilcox (Oracle) * Activate file-backed executable folios after first usage. 1413c909e993SKonstantin Khlebnikov */ 1414d92013d1SMatthew Wilcox (Oracle) if ((vm_flags & VM_EXEC) && !folio_test_swapbacked(folio)) 1415c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 1416c909e993SKonstantin Khlebnikov 141764574746SJohannes Weiner return PAGEREF_KEEP; 141864574746SJohannes Weiner } 141964574746SJohannes Weiner 1420d92013d1SMatthew Wilcox (Oracle) /* Reclaim if clean, defer dirty folios to writeback */ 1421d92013d1SMatthew Wilcox (Oracle) if (referenced_folio && !folio_test_swapbacked(folio)) 1422dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 142364574746SJohannes Weiner 142464574746SJohannes Weiner return PAGEREF_RECLAIM; 1425dfc8d636SJohannes Weiner } 1426dfc8d636SJohannes Weiner 1427e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 1428e20c41b1SMatthew Wilcox (Oracle) static void folio_check_dirty_writeback(struct folio *folio, 1429e2be15f6SMel Gorman bool *dirty, bool *writeback) 1430e2be15f6SMel Gorman { 1431b4597226SMel Gorman struct address_space *mapping; 1432b4597226SMel Gorman 1433e2be15f6SMel Gorman /* 1434e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 1435e2be15f6SMel Gorman * from reclaim context. Do not stall reclaim based on them 1436e2be15f6SMel Gorman */ 1437e20c41b1SMatthew Wilcox (Oracle) if (!folio_is_file_lru(folio) || 1438e20c41b1SMatthew Wilcox (Oracle) (folio_test_anon(folio) && !folio_test_swapbacked(folio))) { 1439e2be15f6SMel Gorman *dirty = false; 1440e2be15f6SMel Gorman *writeback = false; 1441e2be15f6SMel Gorman return; 1442e2be15f6SMel Gorman } 1443e2be15f6SMel Gorman 1444e20c41b1SMatthew Wilcox (Oracle) /* By default assume that the folio flags are accurate */ 1445e20c41b1SMatthew Wilcox (Oracle) *dirty = folio_test_dirty(folio); 1446e20c41b1SMatthew Wilcox (Oracle) *writeback = folio_test_writeback(folio); 1447b4597226SMel Gorman 1448b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 1449e20c41b1SMatthew Wilcox (Oracle) if (!folio_test_private(folio)) 1450b4597226SMel Gorman return; 1451b4597226SMel Gorman 1452e20c41b1SMatthew Wilcox (Oracle) mapping = folio_mapping(folio); 1453b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 1454e20c41b1SMatthew Wilcox (Oracle) mapping->a_ops->is_dirty_writeback(&folio->page, dirty, writeback); 1455e2be15f6SMel Gorman } 1456e2be15f6SMel Gorman 145726aa2d19SDave Hansen static struct page *alloc_demote_page(struct page *page, unsigned long node) 145826aa2d19SDave Hansen { 145926aa2d19SDave Hansen struct migration_target_control mtc = { 146026aa2d19SDave Hansen /* 146126aa2d19SDave Hansen * Allocate from 'node', or fail quickly and quietly. 146226aa2d19SDave Hansen * When this happens, 'page' will likely just be discarded 146326aa2d19SDave Hansen * instead of migrated. 146426aa2d19SDave Hansen */ 146526aa2d19SDave Hansen .gfp_mask = (GFP_HIGHUSER_MOVABLE & ~__GFP_RECLAIM) | 146626aa2d19SDave Hansen __GFP_THISNODE | __GFP_NOWARN | 146726aa2d19SDave Hansen __GFP_NOMEMALLOC | GFP_NOWAIT, 146826aa2d19SDave Hansen .nid = node 146926aa2d19SDave Hansen }; 147026aa2d19SDave Hansen 147126aa2d19SDave Hansen return alloc_migration_target(page, (unsigned long)&mtc); 147226aa2d19SDave Hansen } 147326aa2d19SDave Hansen 147426aa2d19SDave Hansen /* 147526aa2d19SDave Hansen * Take pages on @demote_list and attempt to demote them to 147626aa2d19SDave Hansen * another node. Pages which are not demoted are left on 147726aa2d19SDave Hansen * @demote_pages. 147826aa2d19SDave Hansen */ 147926aa2d19SDave Hansen static unsigned int demote_page_list(struct list_head *demote_pages, 148026aa2d19SDave Hansen struct pglist_data *pgdat) 148126aa2d19SDave Hansen { 148226aa2d19SDave Hansen int target_nid = next_demotion_node(pgdat->node_id); 148326aa2d19SDave Hansen unsigned int nr_succeeded; 148426aa2d19SDave Hansen 148526aa2d19SDave Hansen if (list_empty(demote_pages)) 148626aa2d19SDave Hansen return 0; 148726aa2d19SDave Hansen 148826aa2d19SDave Hansen if (target_nid == NUMA_NO_NODE) 148926aa2d19SDave Hansen return 0; 149026aa2d19SDave Hansen 149126aa2d19SDave Hansen /* Demotion ignores all cpuset and mempolicy settings */ 1492cb75463cSKai Song migrate_pages(demote_pages, alloc_demote_page, NULL, 149326aa2d19SDave Hansen target_nid, MIGRATE_ASYNC, MR_DEMOTION, 149426aa2d19SDave Hansen &nr_succeeded); 149526aa2d19SDave Hansen 1496668e4147SYang Shi if (current_is_kswapd()) 1497668e4147SYang Shi __count_vm_events(PGDEMOTE_KSWAPD, nr_succeeded); 1498668e4147SYang Shi else 1499668e4147SYang Shi __count_vm_events(PGDEMOTE_DIRECT, nr_succeeded); 1500668e4147SYang Shi 150126aa2d19SDave Hansen return nr_succeeded; 150226aa2d19SDave Hansen } 150326aa2d19SDave Hansen 1504e286781dSNick Piggin /* 15051742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 15061da177e4SLinus Torvalds */ 1507730ec8c0SManinder Singh static unsigned int shrink_page_list(struct list_head *page_list, 1508599d0c95SMel Gorman struct pglist_data *pgdat, 1509f84f6e2bSMel Gorman struct scan_control *sc, 15103c710c1aSMichal Hocko struct reclaim_stat *stat, 15118940b34aSMinchan Kim bool ignore_references) 15121da177e4SLinus Torvalds { 15131da177e4SLinus Torvalds LIST_HEAD(ret_pages); 1514abe4c3b5SMel Gorman LIST_HEAD(free_pages); 151526aa2d19SDave Hansen LIST_HEAD(demote_pages); 1516730ec8c0SManinder Singh unsigned int nr_reclaimed = 0; 1517730ec8c0SManinder Singh unsigned int pgactivate = 0; 151826aa2d19SDave Hansen bool do_demote_pass; 15191da177e4SLinus Torvalds 1520060f005fSKirill Tkhai memset(stat, 0, sizeof(*stat)); 15211da177e4SLinus Torvalds cond_resched(); 152226aa2d19SDave Hansen do_demote_pass = can_demote(pgdat->node_id, sc); 15231da177e4SLinus Torvalds 152426aa2d19SDave Hansen retry: 15251da177e4SLinus Torvalds while (!list_empty(page_list)) { 15261da177e4SLinus Torvalds struct address_space *mapping; 15271da177e4SLinus Torvalds struct page *page; 1528be7c07d6SMatthew Wilcox (Oracle) struct folio *folio; 15298940b34aSMinchan Kim enum page_references references = PAGEREF_RECLAIM; 15304b793062SKirill Tkhai bool dirty, writeback, may_enter_fs; 153198879b3bSYang Shi unsigned int nr_pages; 15321da177e4SLinus Torvalds 15331da177e4SLinus Torvalds cond_resched(); 15341da177e4SLinus Torvalds 1535be7c07d6SMatthew Wilcox (Oracle) folio = lru_to_folio(page_list); 1536be7c07d6SMatthew Wilcox (Oracle) list_del(&folio->lru); 1537be7c07d6SMatthew Wilcox (Oracle) page = &folio->page; 15381da177e4SLinus Torvalds 1539529ae9aaSNick Piggin if (!trylock_page(page)) 15401da177e4SLinus Torvalds goto keep; 15411da177e4SLinus Torvalds 1542309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 15431da177e4SLinus Torvalds 1544d8c6546bSMatthew Wilcox (Oracle) nr_pages = compound_nr(page); 154598879b3bSYang Shi 154698879b3bSYang Shi /* Account the number of base pages even though THP */ 154798879b3bSYang Shi sc->nr_scanned += nr_pages; 154880e43426SChristoph Lameter 154939b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) 1550ad6b6704SMinchan Kim goto activate_locked; 1551894bc310SLee Schermerhorn 1552a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 155380e43426SChristoph Lameter goto keep_locked; 155480e43426SChristoph Lameter 1555c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 1556c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 1557c661b078SAndy Whitcroft 1558e62e384eSMichal Hocko /* 1559894befecSAndrey Ryabinin * The number of dirty pages determines if a node is marked 15608cd7c588SMel Gorman * reclaim_congested. kswapd will stall and start writing 15618cd7c588SMel Gorman * pages if the tail of the LRU is all dirty unqueued pages. 1562e2be15f6SMel Gorman */ 1563e20c41b1SMatthew Wilcox (Oracle) folio_check_dirty_writeback(folio, &dirty, &writeback); 1564e2be15f6SMel Gorman if (dirty || writeback) 1565c79b7b96SMatthew Wilcox (Oracle) stat->nr_dirty += nr_pages; 1566e2be15f6SMel Gorman 1567e2be15f6SMel Gorman if (dirty && !writeback) 1568c79b7b96SMatthew Wilcox (Oracle) stat->nr_unqueued_dirty += nr_pages; 1569e2be15f6SMel Gorman 1570d04e8acdSMel Gorman /* 1571d04e8acdSMel Gorman * Treat this page as congested if the underlying BDI is or if 1572d04e8acdSMel Gorman * pages are cycling through the LRU so quickly that the 1573d04e8acdSMel Gorman * pages marked for immediate reclaim are making it to the 1574d04e8acdSMel Gorman * end of the LRU a second time. 1575d04e8acdSMel Gorman */ 1576e2be15f6SMel Gorman mapping = page_mapping(page); 1577fe55d563SNeilBrown if (writeback && PageReclaim(page)) 1578c79b7b96SMatthew Wilcox (Oracle) stat->nr_congested += nr_pages; 1579e2be15f6SMel Gorman 1580e2be15f6SMel Gorman /* 1581283aba9fSMel Gorman * If a page at the tail of the LRU is under writeback, there 1582283aba9fSMel Gorman * are three cases to consider. 1583e62e384eSMichal Hocko * 1584283aba9fSMel Gorman * 1) If reclaim is encountering an excessive number of pages 1585283aba9fSMel Gorman * under writeback and this page is both under writeback and 1586283aba9fSMel Gorman * PageReclaim then it indicates that pages are being queued 1587283aba9fSMel Gorman * for IO but are being recycled through the LRU before the 1588283aba9fSMel Gorman * IO can complete. Waiting on the page itself risks an 1589283aba9fSMel Gorman * indefinite stall if it is impossible to writeback the 1590283aba9fSMel Gorman * page due to IO error or disconnected storage so instead 1591b1a6f21eSMel Gorman * note that the LRU is being scanned too quickly and the 1592b1a6f21eSMel Gorman * caller can stall after page list has been processed. 1593c3b94f44SHugh Dickins * 159497c9341fSTejun Heo * 2) Global or new memcg reclaim encounters a page that is 1595ecf5fc6eSMichal Hocko * not marked for immediate reclaim, or the caller does not 1596ecf5fc6eSMichal Hocko * have __GFP_FS (or __GFP_IO if it's simply going to swap, 1597ecf5fc6eSMichal Hocko * not to fs). In this case mark the page for immediate 159897c9341fSTejun Heo * reclaim and continue scanning. 1599283aba9fSMel Gorman * 1600ecf5fc6eSMichal Hocko * Require may_enter_fs because we would wait on fs, which 1601ecf5fc6eSMichal Hocko * may not have submitted IO yet. And the loop driver might 1602283aba9fSMel Gorman * enter reclaim, and deadlock if it waits on a page for 1603283aba9fSMel Gorman * which it is needed to do the write (loop masks off 1604283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 1605283aba9fSMel Gorman * would probably show more reasons. 1606283aba9fSMel Gorman * 16077fadc820SHugh Dickins * 3) Legacy memcg encounters a page that is already marked 1608283aba9fSMel Gorman * PageReclaim. memcg does not have any dirty pages 1609283aba9fSMel Gorman * throttling so we could easily OOM just because too many 1610283aba9fSMel Gorman * pages are in writeback and there is nothing else to 1611283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 1612c55e8d03SJohannes Weiner * 1613c55e8d03SJohannes Weiner * In cases 1) and 2) we activate the pages to get them out of 1614c55e8d03SJohannes Weiner * the way while we continue scanning for clean pages on the 1615c55e8d03SJohannes Weiner * inactive list and refilling from the active list. The 1616c55e8d03SJohannes Weiner * observation here is that waiting for disk writes is more 1617c55e8d03SJohannes Weiner * expensive than potentially causing reloads down the line. 1618c55e8d03SJohannes Weiner * Since they're marked for immediate reclaim, they won't put 1619c55e8d03SJohannes Weiner * memory pressure on the cache working set any longer than it 1620c55e8d03SJohannes Weiner * takes to write them to disk. 1621e62e384eSMichal Hocko */ 1622283aba9fSMel Gorman if (PageWriteback(page)) { 1623283aba9fSMel Gorman /* Case 1 above */ 1624283aba9fSMel Gorman if (current_is_kswapd() && 1625283aba9fSMel Gorman PageReclaim(page) && 1626599d0c95SMel Gorman test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { 1627c79b7b96SMatthew Wilcox (Oracle) stat->nr_immediate += nr_pages; 1628c55e8d03SJohannes Weiner goto activate_locked; 1629283aba9fSMel Gorman 1630283aba9fSMel Gorman /* Case 2 above */ 1631b5ead35eSJohannes Weiner } else if (writeback_throttling_sane(sc) || 1632ecf5fc6eSMichal Hocko !PageReclaim(page) || !may_enter_fs) { 1633c3b94f44SHugh Dickins /* 1634c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 1635c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 1636c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 1637c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 1638c3b94f44SHugh Dickins * enough to care. What we do want is for this 1639c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 1640c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 1641c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 1642c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 1643c3b94f44SHugh Dickins */ 1644c3b94f44SHugh Dickins SetPageReclaim(page); 1645c79b7b96SMatthew Wilcox (Oracle) stat->nr_writeback += nr_pages; 1646c55e8d03SJohannes Weiner goto activate_locked; 1647283aba9fSMel Gorman 1648283aba9fSMel Gorman /* Case 3 above */ 1649283aba9fSMel Gorman } else { 16507fadc820SHugh Dickins unlock_page(page); 1651c3b94f44SHugh Dickins wait_on_page_writeback(page); 16527fadc820SHugh Dickins /* then go back and try same page again */ 16537fadc820SHugh Dickins list_add_tail(&page->lru, page_list); 16547fadc820SHugh Dickins continue; 1655e62e384eSMichal Hocko } 1656283aba9fSMel Gorman } 16571da177e4SLinus Torvalds 16588940b34aSMinchan Kim if (!ignore_references) 1659d92013d1SMatthew Wilcox (Oracle) references = folio_check_references(folio, sc); 166002c6de8dSMinchan Kim 1661dfc8d636SJohannes Weiner switch (references) { 1662dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 16631da177e4SLinus Torvalds goto activate_locked; 166464574746SJohannes Weiner case PAGEREF_KEEP: 166598879b3bSYang Shi stat->nr_ref_keep += nr_pages; 166664574746SJohannes Weiner goto keep_locked; 1667dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 1668dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 1669dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 1670dfc8d636SJohannes Weiner } 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds /* 167326aa2d19SDave Hansen * Before reclaiming the page, try to relocate 167426aa2d19SDave Hansen * its contents to another node. 167526aa2d19SDave Hansen */ 167626aa2d19SDave Hansen if (do_demote_pass && 167726aa2d19SDave Hansen (thp_migration_supported() || !PageTransHuge(page))) { 167826aa2d19SDave Hansen list_add(&page->lru, &demote_pages); 167926aa2d19SDave Hansen unlock_page(page); 168026aa2d19SDave Hansen continue; 168126aa2d19SDave Hansen } 168226aa2d19SDave Hansen 168326aa2d19SDave Hansen /* 16841da177e4SLinus Torvalds * Anonymous process memory has backing store? 16851da177e4SLinus Torvalds * Try to allocate it some swap space here. 1686802a3a92SShaohua Li * Lazyfree page could be freed directly 16871da177e4SLinus Torvalds */ 1688bd4c82c2SHuang Ying if (PageAnon(page) && PageSwapBacked(page)) { 1689bd4c82c2SHuang Ying if (!PageSwapCache(page)) { 169063eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 169163eb6b93SHugh Dickins goto keep_locked; 1692d4b4084aSMatthew Wilcox (Oracle) if (folio_maybe_dma_pinned(folio)) 1693feb889fbSLinus Torvalds goto keep_locked; 1694747552b1SHuang Ying if (PageTransHuge(page)) { 1695b8f593cdSHuang Ying /* cannot split THP, skip it */ 1696d4b4084aSMatthew Wilcox (Oracle) if (!can_split_folio(folio, NULL)) 1697b8f593cdSHuang Ying goto activate_locked; 1698747552b1SHuang Ying /* 1699747552b1SHuang Ying * Split pages without a PMD map right 1700747552b1SHuang Ying * away. Chances are some or all of the 1701747552b1SHuang Ying * tail pages can be freed without IO. 1702747552b1SHuang Ying */ 1703d4b4084aSMatthew Wilcox (Oracle) if (!folio_entire_mapcount(folio) && 1704346cf613SMatthew Wilcox (Oracle) split_folio_to_list(folio, 1705bd4c82c2SHuang Ying page_list)) 1706747552b1SHuang Ying goto activate_locked; 1707747552b1SHuang Ying } 17080f074658SMinchan Kim if (!add_to_swap(page)) { 17090f074658SMinchan Kim if (!PageTransHuge(page)) 171098879b3bSYang Shi goto activate_locked_split; 1711bd4c82c2SHuang Ying /* Fallback to swap normal pages */ 1712346cf613SMatthew Wilcox (Oracle) if (split_folio_to_list(folio, 1713bd4c82c2SHuang Ying page_list)) 17140f074658SMinchan Kim goto activate_locked; 1715fe490cc0SHuang Ying #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1716fe490cc0SHuang Ying count_vm_event(THP_SWPOUT_FALLBACK); 1717fe490cc0SHuang Ying #endif 17180f074658SMinchan Kim if (!add_to_swap(page)) 171998879b3bSYang Shi goto activate_locked_split; 17200f074658SMinchan Kim } 17210f074658SMinchan Kim 17224b793062SKirill Tkhai may_enter_fs = true; 17231da177e4SLinus Torvalds 1724e2be15f6SMel Gorman /* Adding to swap updated mapping */ 17251da177e4SLinus Torvalds mapping = page_mapping(page); 1726bd4c82c2SHuang Ying } 1727820c4e2eSMatthew Wilcox (Oracle) } else if (PageSwapBacked(page) && PageTransHuge(page)) { 1728820c4e2eSMatthew Wilcox (Oracle) /* Split shmem THP */ 1729346cf613SMatthew Wilcox (Oracle) if (split_folio_to_list(folio, page_list)) 17307751b2daSKirill A. Shutemov goto keep_locked; 1731e2be15f6SMel Gorman } 17321da177e4SLinus Torvalds 17331da177e4SLinus Torvalds /* 173498879b3bSYang Shi * THP may get split above, need minus tail pages and update 173598879b3bSYang Shi * nr_pages to avoid accounting tail pages twice. 173698879b3bSYang Shi * 173798879b3bSYang Shi * The tail pages that are added into swap cache successfully 173898879b3bSYang Shi * reach here. 173998879b3bSYang Shi */ 174098879b3bSYang Shi if ((nr_pages > 1) && !PageTransHuge(page)) { 174198879b3bSYang Shi sc->nr_scanned -= (nr_pages - 1); 174298879b3bSYang Shi nr_pages = 1; 174398879b3bSYang Shi } 174498879b3bSYang Shi 174598879b3bSYang Shi /* 17461da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 17471da177e4SLinus Torvalds * processes. Try to unmap it here. 17481da177e4SLinus Torvalds */ 1749802a3a92SShaohua Li if (page_mapped(page)) { 1750013339dfSShakeel Butt enum ttu_flags flags = TTU_BATCH_FLUSH; 17511f318a9bSJaewon Kim bool was_swapbacked = PageSwapBacked(page); 1752bd4c82c2SHuang Ying 1753343b2888SMatthew Wilcox (Oracle) if (PageTransHuge(page) && 1754343b2888SMatthew Wilcox (Oracle) thp_order(page) >= HPAGE_PMD_ORDER) 1755bd4c82c2SHuang Ying flags |= TTU_SPLIT_HUGE_PMD; 17561f318a9bSJaewon Kim 1757869f7ee6SMatthew Wilcox (Oracle) try_to_unmap(folio, flags); 17581fb08ac6SYang Shi if (page_mapped(page)) { 175998879b3bSYang Shi stat->nr_unmap_fail += nr_pages; 17601f318a9bSJaewon Kim if (!was_swapbacked && PageSwapBacked(page)) 17611f318a9bSJaewon Kim stat->nr_lazyfree_fail += nr_pages; 17621da177e4SLinus Torvalds goto activate_locked; 17631da177e4SLinus Torvalds } 17641da177e4SLinus Torvalds } 17651da177e4SLinus Torvalds 17661da177e4SLinus Torvalds if (PageDirty(page)) { 1767ee72886dSMel Gorman /* 17684eda4823SJohannes Weiner * Only kswapd can writeback filesystem pages 17694eda4823SJohannes Weiner * to avoid risk of stack overflow. But avoid 17704eda4823SJohannes Weiner * injecting inefficient single-page IO into 17714eda4823SJohannes Weiner * flusher writeback as much as possible: only 17724eda4823SJohannes Weiner * write pages when we've encountered many 17734eda4823SJohannes Weiner * dirty pages, and when we've already scanned 17744eda4823SJohannes Weiner * the rest of the LRU for clean pages and see 17754eda4823SJohannes Weiner * the same dirty pages again (PageReclaim). 1776ee72886dSMel Gorman */ 17779de4f22aSHuang Ying if (page_is_file_lru(page) && 17784eda4823SJohannes Weiner (!current_is_kswapd() || !PageReclaim(page) || 1779599d0c95SMel Gorman !test_bit(PGDAT_DIRTY, &pgdat->flags))) { 178049ea7eb6SMel Gorman /* 178149ea7eb6SMel Gorman * Immediately reclaim when written back. 178249ea7eb6SMel Gorman * Similar in principal to deactivate_page() 178349ea7eb6SMel Gorman * except we already have the page isolated 178449ea7eb6SMel Gorman * and know it's dirty 178549ea7eb6SMel Gorman */ 1786c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_IMMEDIATE); 178749ea7eb6SMel Gorman SetPageReclaim(page); 178849ea7eb6SMel Gorman 1789c55e8d03SJohannes Weiner goto activate_locked; 1790ee72886dSMel Gorman } 1791ee72886dSMel Gorman 1792dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 17931da177e4SLinus Torvalds goto keep_locked; 17944dd4b920SAndrew Morton if (!may_enter_fs) 17951da177e4SLinus Torvalds goto keep_locked; 179652a8363eSChristoph Lameter if (!sc->may_writepage) 17971da177e4SLinus Torvalds goto keep_locked; 17981da177e4SLinus Torvalds 1799d950c947SMel Gorman /* 1800d950c947SMel Gorman * Page is dirty. Flush the TLB if a writable entry 1801d950c947SMel Gorman * potentially exists to avoid CPU writes after IO 1802d950c947SMel Gorman * starts and then write it out here. 1803d950c947SMel Gorman */ 1804d950c947SMel Gorman try_to_unmap_flush_dirty(); 1805e0cd5e7fSMatthew Wilcox (Oracle) switch (pageout(folio, mapping)) { 18061da177e4SLinus Torvalds case PAGE_KEEP: 18071da177e4SLinus Torvalds goto keep_locked; 18081da177e4SLinus Torvalds case PAGE_ACTIVATE: 18091da177e4SLinus Torvalds goto activate_locked; 18101da177e4SLinus Torvalds case PAGE_SUCCESS: 1811c79b7b96SMatthew Wilcox (Oracle) stat->nr_pageout += nr_pages; 181296f8bf4fSJohannes Weiner 18137d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 181441ac1999SMel Gorman goto keep; 18157d3579e8SKOSAKI Motohiro if (PageDirty(page)) 18161da177e4SLinus Torvalds goto keep; 18177d3579e8SKOSAKI Motohiro 18181da177e4SLinus Torvalds /* 18191da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 18201da177e4SLinus Torvalds * ahead and try to reclaim the page. 18211da177e4SLinus Torvalds */ 1822529ae9aaSNick Piggin if (!trylock_page(page)) 18231da177e4SLinus Torvalds goto keep; 18241da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 18251da177e4SLinus Torvalds goto keep_locked; 18261da177e4SLinus Torvalds mapping = page_mapping(page); 182701359eb2SGustavo A. R. Silva fallthrough; 18281da177e4SLinus Torvalds case PAGE_CLEAN: 18291da177e4SLinus Torvalds ; /* try to free the page below */ 18301da177e4SLinus Torvalds } 18311da177e4SLinus Torvalds } 18321da177e4SLinus Torvalds 18331da177e4SLinus Torvalds /* 18341da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 18351da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 18361da177e4SLinus Torvalds * the page as well. 18371da177e4SLinus Torvalds * 18381da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 18391da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 18401da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 18411da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 18421da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 18431da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 18441da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 18451da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 18461da177e4SLinus Torvalds * 18471da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 18481da177e4SLinus Torvalds * the pages which were not successfully invalidated in 1849d12b8951SYang Shi * truncate_cleanup_page(). We try to drop those buffers here 18501da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 18511da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 18521da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 18531da177e4SLinus Torvalds */ 1854266cf658SDavid Howells if (page_has_private(page)) { 18551da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 18561da177e4SLinus Torvalds goto activate_locked; 1857e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 1858e286781dSNick Piggin unlock_page(page); 1859e286781dSNick Piggin if (put_page_testzero(page)) 18601da177e4SLinus Torvalds goto free_it; 1861e286781dSNick Piggin else { 1862e286781dSNick Piggin /* 1863e286781dSNick Piggin * rare race with speculative reference. 1864e286781dSNick Piggin * the speculative reference will free 1865e286781dSNick Piggin * this page shortly, so we may 1866e286781dSNick Piggin * increment nr_reclaimed here (and 1867e286781dSNick Piggin * leave it off the LRU). 1868e286781dSNick Piggin */ 1869e286781dSNick Piggin nr_reclaimed++; 1870e286781dSNick Piggin continue; 1871e286781dSNick Piggin } 1872e286781dSNick Piggin } 18731da177e4SLinus Torvalds } 18741da177e4SLinus Torvalds 1875802a3a92SShaohua Li if (PageAnon(page) && !PageSwapBacked(page)) { 1876802a3a92SShaohua Li /* follow __remove_mapping for reference */ 1877802a3a92SShaohua Li if (!page_ref_freeze(page, 1)) 187849d2e9ccSChristoph Lameter goto keep_locked; 1879d17be2d9SMiaohe Lin /* 1880d17be2d9SMiaohe Lin * The page has only one reference left, which is 1881d17be2d9SMiaohe Lin * from the isolation. After the caller puts the 1882d17be2d9SMiaohe Lin * page back on lru and drops the reference, the 1883d17be2d9SMiaohe Lin * page will be freed anyway. It doesn't matter 1884d17be2d9SMiaohe Lin * which lru it goes. So we don't bother checking 1885d17be2d9SMiaohe Lin * PageDirty here. 1886d17be2d9SMiaohe Lin */ 1887802a3a92SShaohua Li count_vm_event(PGLAZYFREED); 18882262185cSRoman Gushchin count_memcg_page_event(page, PGLAZYFREED); 1889be7c07d6SMatthew Wilcox (Oracle) } else if (!mapping || !__remove_mapping(mapping, folio, true, 1890b910718aSJohannes Weiner sc->target_mem_cgroup)) 1891802a3a92SShaohua Li goto keep_locked; 18929a1ea439SHugh Dickins 18939a1ea439SHugh Dickins unlock_page(page); 1894e286781dSNick Piggin free_it: 189598879b3bSYang Shi /* 189698879b3bSYang Shi * THP may get swapped out in a whole, need account 189798879b3bSYang Shi * all base pages. 189898879b3bSYang Shi */ 189998879b3bSYang Shi nr_reclaimed += nr_pages; 1900abe4c3b5SMel Gorman 1901abe4c3b5SMel Gorman /* 1902abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1903abe4c3b5SMel Gorman * appear not as the counts should be low 1904abe4c3b5SMel Gorman */ 19057ae88534SYang Shi if (unlikely(PageTransHuge(page))) 1906ff45fc3cSMatthew Wilcox (Oracle) destroy_compound_page(page); 19077ae88534SYang Shi else 1908abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 19091da177e4SLinus Torvalds continue; 19101da177e4SLinus Torvalds 191198879b3bSYang Shi activate_locked_split: 191298879b3bSYang Shi /* 191398879b3bSYang Shi * The tail pages that are failed to add into swap cache 191498879b3bSYang Shi * reach here. Fixup nr_scanned and nr_pages. 191598879b3bSYang Shi */ 191698879b3bSYang Shi if (nr_pages > 1) { 191798879b3bSYang Shi sc->nr_scanned -= (nr_pages - 1); 191898879b3bSYang Shi nr_pages = 1; 191998879b3bSYang Shi } 19201da177e4SLinus Torvalds activate_locked: 192168a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 1922ad6b6704SMinchan Kim if (PageSwapCache(page) && (mem_cgroup_swap_full(page) || 1923ad6b6704SMinchan Kim PageMlocked(page))) 1924a2c43eedSHugh Dickins try_to_free_swap(page); 1925309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 1926ad6b6704SMinchan Kim if (!PageMlocked(page)) { 19279de4f22aSHuang Ying int type = page_is_file_lru(page); 19281da177e4SLinus Torvalds SetPageActive(page); 192998879b3bSYang Shi stat->nr_activate[type] += nr_pages; 19302262185cSRoman Gushchin count_memcg_page_event(page, PGACTIVATE); 1931ad6b6704SMinchan Kim } 19321da177e4SLinus Torvalds keep_locked: 19331da177e4SLinus Torvalds unlock_page(page); 19341da177e4SLinus Torvalds keep: 19351da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 1936309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); 19371da177e4SLinus Torvalds } 193826aa2d19SDave Hansen /* 'page_list' is always empty here */ 193926aa2d19SDave Hansen 194026aa2d19SDave Hansen /* Migrate pages selected for demotion */ 194126aa2d19SDave Hansen nr_reclaimed += demote_page_list(&demote_pages, pgdat); 194226aa2d19SDave Hansen /* Pages that could not be demoted are still in @demote_pages */ 194326aa2d19SDave Hansen if (!list_empty(&demote_pages)) { 194426aa2d19SDave Hansen /* Pages which failed to demoted go back on @page_list for retry: */ 194526aa2d19SDave Hansen list_splice_init(&demote_pages, page_list); 194626aa2d19SDave Hansen do_demote_pass = false; 194726aa2d19SDave Hansen goto retry; 194826aa2d19SDave Hansen } 1949abe4c3b5SMel Gorman 195098879b3bSYang Shi pgactivate = stat->nr_activate[0] + stat->nr_activate[1]; 195198879b3bSYang Shi 1952747db954SJohannes Weiner mem_cgroup_uncharge_list(&free_pages); 195372b252aeSMel Gorman try_to_unmap_flush(); 19542d4894b5SMel Gorman free_unref_page_list(&free_pages); 1955abe4c3b5SMel Gorman 19561da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1957886cf190SKirill Tkhai count_vm_events(PGACTIVATE, pgactivate); 19580a31bc97SJohannes Weiner 195905ff5137SAndrew Morton return nr_reclaimed; 19601da177e4SLinus Torvalds } 19611da177e4SLinus Torvalds 1962730ec8c0SManinder Singh unsigned int reclaim_clean_pages_from_list(struct zone *zone, 196302c6de8dSMinchan Kim struct list_head *page_list) 196402c6de8dSMinchan Kim { 196502c6de8dSMinchan Kim struct scan_control sc = { 196602c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 196702c6de8dSMinchan Kim .may_unmap = 1, 196802c6de8dSMinchan Kim }; 19691f318a9bSJaewon Kim struct reclaim_stat stat; 1970730ec8c0SManinder Singh unsigned int nr_reclaimed; 197102c6de8dSMinchan Kim struct page *page, *next; 197202c6de8dSMinchan Kim LIST_HEAD(clean_pages); 19732d2b8d2bSYu Zhao unsigned int noreclaim_flag; 197402c6de8dSMinchan Kim 197502c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 1976ae37c7ffSOscar Salvador if (!PageHuge(page) && page_is_file_lru(page) && 1977ae37c7ffSOscar Salvador !PageDirty(page) && !__PageMovable(page) && 1978ae37c7ffSOscar Salvador !PageUnevictable(page)) { 197902c6de8dSMinchan Kim ClearPageActive(page); 198002c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 198102c6de8dSMinchan Kim } 198202c6de8dSMinchan Kim } 198302c6de8dSMinchan Kim 19842d2b8d2bSYu Zhao /* 19852d2b8d2bSYu Zhao * We should be safe here since we are only dealing with file pages and 19862d2b8d2bSYu Zhao * we are not kswapd and therefore cannot write dirty file pages. But 19872d2b8d2bSYu Zhao * call memalloc_noreclaim_save() anyway, just in case these conditions 19882d2b8d2bSYu Zhao * change in the future. 19892d2b8d2bSYu Zhao */ 19902d2b8d2bSYu Zhao noreclaim_flag = memalloc_noreclaim_save(); 19911f318a9bSJaewon Kim nr_reclaimed = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc, 1992013339dfSShakeel Butt &stat, true); 19932d2b8d2bSYu Zhao memalloc_noreclaim_restore(noreclaim_flag); 19942d2b8d2bSYu Zhao 199502c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 19962da9f630SNicholas Piggin mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, 19972da9f630SNicholas Piggin -(long)nr_reclaimed); 19981f318a9bSJaewon Kim /* 19991f318a9bSJaewon Kim * Since lazyfree pages are isolated from file LRU from the beginning, 20001f318a9bSJaewon Kim * they will rotate back to anonymous LRU in the end if it failed to 20011f318a9bSJaewon Kim * discard so isolated count will be mismatched. 20021f318a9bSJaewon Kim * Compensate the isolated count for both LRU lists. 20031f318a9bSJaewon Kim */ 20041f318a9bSJaewon Kim mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON, 20051f318a9bSJaewon Kim stat.nr_lazyfree_fail); 20061f318a9bSJaewon Kim mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, 20072da9f630SNicholas Piggin -(long)stat.nr_lazyfree_fail); 20081f318a9bSJaewon Kim return nr_reclaimed; 200902c6de8dSMinchan Kim } 201002c6de8dSMinchan Kim 20115ad333ebSAndy Whitcroft /* 20127ee36a14SMel Gorman * Update LRU sizes after isolating pages. The LRU size updates must 201355b65a57SEthon Paul * be complete before mem_cgroup_update_lru_size due to a sanity check. 20147ee36a14SMel Gorman */ 20157ee36a14SMel Gorman static __always_inline void update_lru_sizes(struct lruvec *lruvec, 2016b4536f0cSMichal Hocko enum lru_list lru, unsigned long *nr_zone_taken) 20177ee36a14SMel Gorman { 20187ee36a14SMel Gorman int zid; 20197ee36a14SMel Gorman 20207ee36a14SMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 20217ee36a14SMel Gorman if (!nr_zone_taken[zid]) 20227ee36a14SMel Gorman continue; 20237ee36a14SMel Gorman 2024a892cb6bSWei Yang update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 20257ee36a14SMel Gorman } 20267ee36a14SMel Gorman 20277ee36a14SMel Gorman } 20287ee36a14SMel Gorman 2029f611fab7SMel Gorman /* 203015b44736SHugh Dickins * Isolating page from the lruvec to fill in @dst list by nr_to_scan times. 203115b44736SHugh Dickins * 203215b44736SHugh Dickins * lruvec->lru_lock is heavily contended. Some of the functions that 20331da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 20341da177e4SLinus Torvalds * and working on them outside the LRU lock. 20351da177e4SLinus Torvalds * 20361da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 20371da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 20381da177e4SLinus Torvalds * 203915b44736SHugh Dickins * Lru_lock must be held before calling this function. 20401da177e4SLinus Torvalds * 2041791b48b6SMinchan Kim * @nr_to_scan: The number of eligible pages to look through on the list. 20425dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 20431da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 2044f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 2045fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 20463cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 20471da177e4SLinus Torvalds * 20481da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 20491da177e4SLinus Torvalds */ 205069e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 20515dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 2052fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 2053a9e7c39fSKirill Tkhai enum lru_list lru) 20541da177e4SLinus Torvalds { 205575b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 205669e05944SAndrew Morton unsigned long nr_taken = 0; 2057599d0c95SMel Gorman unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; 20587cc30fcfSMel Gorman unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; 20593db65812SJohannes Weiner unsigned long skipped = 0; 2060791b48b6SMinchan Kim unsigned long scan, total_scan, nr_pages; 2061b2e18757SMel Gorman LIST_HEAD(pages_skipped); 20621da177e4SLinus Torvalds 206398879b3bSYang Shi total_scan = 0; 2064791b48b6SMinchan Kim scan = 0; 206598879b3bSYang Shi while (scan < nr_to_scan && !list_empty(src)) { 206689f6c88aSHugh Dickins struct list_head *move_to = src; 20675ad333ebSAndy Whitcroft struct page *page; 20685ad333ebSAndy Whitcroft 20691da177e4SLinus Torvalds page = lru_to_page(src); 20701da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 20711da177e4SLinus Torvalds 2072d8c6546bSMatthew Wilcox (Oracle) nr_pages = compound_nr(page); 207398879b3bSYang Shi total_scan += nr_pages; 207498879b3bSYang Shi 2075b2e18757SMel Gorman if (page_zonenum(page) > sc->reclaim_idx) { 207698879b3bSYang Shi nr_skipped[page_zonenum(page)] += nr_pages; 207789f6c88aSHugh Dickins move_to = &pages_skipped; 207889f6c88aSHugh Dickins goto move; 2079b2e18757SMel Gorman } 2080b2e18757SMel Gorman 2081791b48b6SMinchan Kim /* 2082791b48b6SMinchan Kim * Do not count skipped pages because that makes the function 2083791b48b6SMinchan Kim * return with no isolated pages if the LRU mostly contains 2084791b48b6SMinchan Kim * ineligible pages. This causes the VM to not reclaim any 2085791b48b6SMinchan Kim * pages, triggering a premature OOM. 208689f6c88aSHugh Dickins * Account all tail pages of THP. 2087791b48b6SMinchan Kim */ 208898879b3bSYang Shi scan += nr_pages; 208989f6c88aSHugh Dickins 209089f6c88aSHugh Dickins if (!PageLRU(page)) 209189f6c88aSHugh Dickins goto move; 209289f6c88aSHugh Dickins if (!sc->may_unmap && page_mapped(page)) 209389f6c88aSHugh Dickins goto move; 209489f6c88aSHugh Dickins 20959df41314SAlex Shi /* 20969df41314SAlex Shi * Be careful not to clear PageLRU until after we're 20979df41314SAlex Shi * sure the page is not being freed elsewhere -- the 20989df41314SAlex Shi * page release code relies on it. 20999df41314SAlex Shi */ 210089f6c88aSHugh Dickins if (unlikely(!get_page_unless_zero(page))) 210189f6c88aSHugh Dickins goto move; 21029df41314SAlex Shi 21039df41314SAlex Shi if (!TestClearPageLRU(page)) { 2104c2135f7cSAlex Shi /* Another thread is already isolating this page */ 21059df41314SAlex Shi put_page(page); 210689f6c88aSHugh Dickins goto move; 21079df41314SAlex Shi } 21089df41314SAlex Shi 2109599d0c95SMel Gorman nr_taken += nr_pages; 2110599d0c95SMel Gorman nr_zone_taken[page_zonenum(page)] += nr_pages; 211189f6c88aSHugh Dickins move_to = dst; 211289f6c88aSHugh Dickins move: 211389f6c88aSHugh Dickins list_move(&page->lru, move_to); 21145ad333ebSAndy Whitcroft } 21151da177e4SLinus Torvalds 2116b2e18757SMel Gorman /* 2117b2e18757SMel Gorman * Splice any skipped pages to the start of the LRU list. Note that 2118b2e18757SMel Gorman * this disrupts the LRU order when reclaiming for lower zones but 2119b2e18757SMel Gorman * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX 2120b2e18757SMel Gorman * scanning would soon rescan the same pages to skip and put the 2121b2e18757SMel Gorman * system at risk of premature OOM. 2122b2e18757SMel Gorman */ 21237cc30fcfSMel Gorman if (!list_empty(&pages_skipped)) { 21247cc30fcfSMel Gorman int zid; 21257cc30fcfSMel Gorman 21263db65812SJohannes Weiner list_splice(&pages_skipped, src); 21277cc30fcfSMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 21287cc30fcfSMel Gorman if (!nr_skipped[zid]) 21297cc30fcfSMel Gorman continue; 21307cc30fcfSMel Gorman 21317cc30fcfSMel Gorman __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); 21321265e3a6SMichal Hocko skipped += nr_skipped[zid]; 21337cc30fcfSMel Gorman } 21347cc30fcfSMel Gorman } 2135791b48b6SMinchan Kim *nr_scanned = total_scan; 21361265e3a6SMichal Hocko trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, 213789f6c88aSHugh Dickins total_scan, skipped, nr_taken, 213889f6c88aSHugh Dickins sc->may_unmap ? 0 : ISOLATE_UNMAPPED, lru); 2139b4536f0cSMichal Hocko update_lru_sizes(lruvec, lru, nr_zone_taken); 21401da177e4SLinus Torvalds return nr_taken; 21411da177e4SLinus Torvalds } 21421da177e4SLinus Torvalds 214362695a84SNick Piggin /** 2144d1d8a3b4SMatthew Wilcox (Oracle) * folio_isolate_lru() - Try to isolate a folio from its LRU list. 2145d1d8a3b4SMatthew Wilcox (Oracle) * @folio: Folio to isolate from its LRU list. 214662695a84SNick Piggin * 2147d1d8a3b4SMatthew Wilcox (Oracle) * Isolate a @folio from an LRU list and adjust the vmstat statistic 2148d1d8a3b4SMatthew Wilcox (Oracle) * corresponding to whatever LRU list the folio was on. 214962695a84SNick Piggin * 2150d1d8a3b4SMatthew Wilcox (Oracle) * The folio will have its LRU flag cleared. If it was found on the 2151d1d8a3b4SMatthew Wilcox (Oracle) * active list, it will have the Active flag set. If it was found on the 2152d1d8a3b4SMatthew Wilcox (Oracle) * unevictable list, it will have the Unevictable flag set. These flags 2153894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 215462695a84SNick Piggin * 2155d1d8a3b4SMatthew Wilcox (Oracle) * Context: 2156a5d09bedSMike Rapoport * 215762695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 2158d1d8a3b4SMatthew Wilcox (Oracle) * fundamental difference from isolate_lru_pages() (which is called 215962695a84SNick Piggin * without a stable reference). 2160d1d8a3b4SMatthew Wilcox (Oracle) * (2) The lru_lock must not be held. 2161d1d8a3b4SMatthew Wilcox (Oracle) * (3) Interrupts must be enabled. 2162d1d8a3b4SMatthew Wilcox (Oracle) * 2163d1d8a3b4SMatthew Wilcox (Oracle) * Return: 0 if the folio was removed from an LRU list. 2164d1d8a3b4SMatthew Wilcox (Oracle) * -EBUSY if the folio was not on an LRU list. 216562695a84SNick Piggin */ 2166d1d8a3b4SMatthew Wilcox (Oracle) int folio_isolate_lru(struct folio *folio) 216762695a84SNick Piggin { 216862695a84SNick Piggin int ret = -EBUSY; 216962695a84SNick Piggin 2170d1d8a3b4SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_ref_count(folio), folio); 21710c917313SKonstantin Khlebnikov 2172d1d8a3b4SMatthew Wilcox (Oracle) if (folio_test_clear_lru(folio)) { 2173fa9add64SHugh Dickins struct lruvec *lruvec; 217462695a84SNick Piggin 2175d1d8a3b4SMatthew Wilcox (Oracle) folio_get(folio); 2176e809c3feSMatthew Wilcox (Oracle) lruvec = folio_lruvec_lock_irq(folio); 2177d1d8a3b4SMatthew Wilcox (Oracle) lruvec_del_folio(lruvec, folio); 21786168d0daSAlex Shi unlock_page_lruvec_irq(lruvec); 2179fa9add64SHugh Dickins ret = 0; 218062695a84SNick Piggin } 2181d25b5bd8SAlex Shi 218262695a84SNick Piggin return ret; 218362695a84SNick Piggin } 218462695a84SNick Piggin 21855ad333ebSAndy Whitcroft /* 2186d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 2187178821b8SXianting Tian * then get rescheduled. When there are massive number of tasks doing page 2188d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 2189d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 2190d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 219135cd7815SRik van Riel */ 2192599d0c95SMel Gorman static int too_many_isolated(struct pglist_data *pgdat, int file, 219335cd7815SRik van Riel struct scan_control *sc) 219435cd7815SRik van Riel { 219535cd7815SRik van Riel unsigned long inactive, isolated; 2196d818fca1SMel Gorman bool too_many; 219735cd7815SRik van Riel 219835cd7815SRik van Riel if (current_is_kswapd()) 219935cd7815SRik van Riel return 0; 220035cd7815SRik van Riel 2201b5ead35eSJohannes Weiner if (!writeback_throttling_sane(sc)) 220235cd7815SRik van Riel return 0; 220335cd7815SRik van Riel 220435cd7815SRik van Riel if (file) { 2205599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_FILE); 2206599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_FILE); 220735cd7815SRik van Riel } else { 2208599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_ANON); 2209599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_ANON); 221035cd7815SRik van Riel } 221135cd7815SRik van Riel 22123cf23841SFengguang Wu /* 22133cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 22143cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 22153cf23841SFengguang Wu * deadlock. 22163cf23841SFengguang Wu */ 2217d0164adcSMel Gorman if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) 22183cf23841SFengguang Wu inactive >>= 3; 22193cf23841SFengguang Wu 2220d818fca1SMel Gorman too_many = isolated > inactive; 2221d818fca1SMel Gorman 2222d818fca1SMel Gorman /* Wake up tasks throttled due to too_many_isolated. */ 2223d818fca1SMel Gorman if (!too_many) 2224d818fca1SMel Gorman wake_throttle_isolated(pgdat); 2225d818fca1SMel Gorman 2226d818fca1SMel Gorman return too_many; 222735cd7815SRik van Riel } 222835cd7815SRik van Riel 2229a222f341SKirill Tkhai /* 223015b44736SHugh Dickins * move_pages_to_lru() moves pages from private @list to appropriate LRU list. 223115b44736SHugh Dickins * On return, @list is reused as a list of pages to be freed by the caller. 2232a222f341SKirill Tkhai * 2233a222f341SKirill Tkhai * Returns the number of pages moved to the given lruvec. 2234a222f341SKirill Tkhai */ 22359ef56b78SMuchun Song static unsigned int move_pages_to_lru(struct lruvec *lruvec, 2236a222f341SKirill Tkhai struct list_head *list) 223766635629SMel Gorman { 2238a222f341SKirill Tkhai int nr_pages, nr_moved = 0; 22393f79768fSHugh Dickins LIST_HEAD(pages_to_free); 2240a222f341SKirill Tkhai struct page *page; 224166635629SMel Gorman 2242a222f341SKirill Tkhai while (!list_empty(list)) { 2243a222f341SKirill Tkhai page = lru_to_page(list); 2244309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 2245a222f341SKirill Tkhai list_del(&page->lru); 22463d06afabSAlex Shi if (unlikely(!page_evictable(page))) { 22476168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 224866635629SMel Gorman putback_lru_page(page); 22496168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 225066635629SMel Gorman continue; 225166635629SMel Gorman } 2252fa9add64SHugh Dickins 22533d06afabSAlex Shi /* 22543d06afabSAlex Shi * The SetPageLRU needs to be kept here for list integrity. 22553d06afabSAlex Shi * Otherwise: 22563d06afabSAlex Shi * #0 move_pages_to_lru #1 release_pages 22573d06afabSAlex Shi * if !put_page_testzero 22583d06afabSAlex Shi * if (put_page_testzero()) 22593d06afabSAlex Shi * !PageLRU //skip lru_lock 22603d06afabSAlex Shi * SetPageLRU() 22613d06afabSAlex Shi * list_add(&page->lru,) 22623d06afabSAlex Shi * list_add(&page->lru,) 22633d06afabSAlex Shi */ 22647a608572SLinus Torvalds SetPageLRU(page); 2265a222f341SKirill Tkhai 22663d06afabSAlex Shi if (unlikely(put_page_testzero(page))) { 226787560179SYu Zhao __clear_page_lru_flags(page); 22682bcf8879SHugh Dickins 22692bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 22706168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 2271ff45fc3cSMatthew Wilcox (Oracle) destroy_compound_page(page); 22726168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 22732bcf8879SHugh Dickins } else 22742bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 22753d06afabSAlex Shi 22763d06afabSAlex Shi continue; 22773d06afabSAlex Shi } 22783d06afabSAlex Shi 2279afca9157SAlex Shi /* 2280afca9157SAlex Shi * All pages were isolated from the same lruvec (and isolation 2281afca9157SAlex Shi * inhibits memcg migration). 2282afca9157SAlex Shi */ 22830de340cbSMatthew Wilcox (Oracle) VM_BUG_ON_PAGE(!folio_matches_lruvec(page_folio(page), lruvec), page); 22843a9c9788SYu Zhao add_page_to_lru_list(page, lruvec); 22853d06afabSAlex Shi nr_pages = thp_nr_pages(page); 2286a222f341SKirill Tkhai nr_moved += nr_pages; 228731d8fcacSJohannes Weiner if (PageActive(page)) 228831d8fcacSJohannes Weiner workingset_age_nonresident(lruvec, nr_pages); 228966635629SMel Gorman } 229066635629SMel Gorman 22913f79768fSHugh Dickins /* 22923f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 22933f79768fSHugh Dickins */ 2294a222f341SKirill Tkhai list_splice(&pages_to_free, list); 2295a222f341SKirill Tkhai 2296a222f341SKirill Tkhai return nr_moved; 229766635629SMel Gorman } 229866635629SMel Gorman 229966635629SMel Gorman /* 2300399ba0b9SNeilBrown * If a kernel thread (such as nfsd for loop-back mounts) services 2301a37b0715SNeilBrown * a backing device by writing to the page cache it sets PF_LOCAL_THROTTLE. 2302399ba0b9SNeilBrown * In that case we should only throttle if the backing device it is 2303399ba0b9SNeilBrown * writing to is congested. In other cases it is safe to throttle. 2304399ba0b9SNeilBrown */ 2305399ba0b9SNeilBrown static int current_may_throttle(void) 2306399ba0b9SNeilBrown { 2307b9b1335eSNeilBrown return !(current->flags & PF_LOCAL_THROTTLE); 2308399ba0b9SNeilBrown } 2309399ba0b9SNeilBrown 2310399ba0b9SNeilBrown /* 2311b2e18757SMel Gorman * shrink_inactive_list() is a helper for shrink_node(). It returns the number 23121742f19fSAndrew Morton * of reclaimed pages 23131da177e4SLinus Torvalds */ 23149ef56b78SMuchun Song static unsigned long 23151a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 23169e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 23171da177e4SLinus Torvalds { 23181da177e4SLinus Torvalds LIST_HEAD(page_list); 2319e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 2320730ec8c0SManinder Singh unsigned int nr_reclaimed = 0; 2321e247dbceSKOSAKI Motohiro unsigned long nr_taken; 2322060f005fSKirill Tkhai struct reclaim_stat stat; 2323497a6c1bSJohannes Weiner bool file = is_file_lru(lru); 2324f46b7912SKirill Tkhai enum vm_event_item item; 2325599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 2326db73ee0dSMichal Hocko bool stalled = false; 232778dc583dSKOSAKI Motohiro 2328599d0c95SMel Gorman while (unlikely(too_many_isolated(pgdat, file, sc))) { 2329db73ee0dSMichal Hocko if (stalled) 2330db73ee0dSMichal Hocko return 0; 2331db73ee0dSMichal Hocko 2332db73ee0dSMichal Hocko /* wait a bit for the reclaimer. */ 2333db73ee0dSMichal Hocko stalled = true; 2334c3f4a9a2SMel Gorman reclaim_throttle(pgdat, VMSCAN_THROTTLE_ISOLATED); 233535cd7815SRik van Riel 233635cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 233735cd7815SRik van Riel if (fatal_signal_pending(current)) 233835cd7815SRik van Riel return SWAP_CLUSTER_MAX; 233935cd7815SRik van Riel } 234035cd7815SRik van Riel 23411da177e4SLinus Torvalds lru_add_drain(); 2342f80c0673SMinchan Kim 23436168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 23441da177e4SLinus Torvalds 23455dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 2346a9e7c39fSKirill Tkhai &nr_scanned, sc, lru); 234795d918fcSKonstantin Khlebnikov 2348599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 2349f46b7912SKirill Tkhai item = current_is_kswapd() ? PGSCAN_KSWAPD : PGSCAN_DIRECT; 2350b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 2351f46b7912SKirill Tkhai __count_vm_events(item, nr_scanned); 2352f46b7912SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned); 2353497a6c1bSJohannes Weiner __count_vm_events(PGSCAN_ANON + file, nr_scanned); 2354497a6c1bSJohannes Weiner 23556168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 2356d563c050SHillf Danton 2357d563c050SHillf Danton if (nr_taken == 0) 235866635629SMel Gorman return 0; 2359b35ea17bSKOSAKI Motohiro 2360013339dfSShakeel Butt nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, &stat, false); 2361c661b078SAndy Whitcroft 23626168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 2363497a6c1bSJohannes Weiner move_pages_to_lru(lruvec, &page_list); 2364497a6c1bSJohannes Weiner 2365497a6c1bSJohannes Weiner __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 2366f46b7912SKirill Tkhai item = current_is_kswapd() ? PGSTEAL_KSWAPD : PGSTEAL_DIRECT; 2367b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 2368f46b7912SKirill Tkhai __count_vm_events(item, nr_reclaimed); 2369f46b7912SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed); 2370497a6c1bSJohannes Weiner __count_vm_events(PGSTEAL_ANON + file, nr_reclaimed); 23716168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 23723f79768fSHugh Dickins 237375cc3c91SAlex Shi lru_note_cost(lruvec, file, stat.nr_pageout); 2374747db954SJohannes Weiner mem_cgroup_uncharge_list(&page_list); 23752d4894b5SMel Gorman free_unref_page_list(&page_list); 2376e11da5b4SMel Gorman 237792df3a72SMel Gorman /* 23781c610d5fSAndrey Ryabinin * If dirty pages are scanned that are not queued for IO, it 23791c610d5fSAndrey Ryabinin * implies that flushers are not doing their job. This can 23801c610d5fSAndrey Ryabinin * happen when memory pressure pushes dirty pages to the end of 23811c610d5fSAndrey Ryabinin * the LRU before the dirty limits are breached and the dirty 23821c610d5fSAndrey Ryabinin * data has expired. It can also happen when the proportion of 23831c610d5fSAndrey Ryabinin * dirty pages grows not through writes but through memory 23841c610d5fSAndrey Ryabinin * pressure reclaiming all the clean cache. And in some cases, 23851c610d5fSAndrey Ryabinin * the flushers simply cannot keep up with the allocation 23861c610d5fSAndrey Ryabinin * rate. Nudge the flusher threads in case they are asleep. 23871c610d5fSAndrey Ryabinin */ 23881c610d5fSAndrey Ryabinin if (stat.nr_unqueued_dirty == nr_taken) 23891c610d5fSAndrey Ryabinin wakeup_flusher_threads(WB_REASON_VMSCAN); 23901c610d5fSAndrey Ryabinin 2391d108c772SAndrey Ryabinin sc->nr.dirty += stat.nr_dirty; 2392d108c772SAndrey Ryabinin sc->nr.congested += stat.nr_congested; 2393d108c772SAndrey Ryabinin sc->nr.unqueued_dirty += stat.nr_unqueued_dirty; 2394d108c772SAndrey Ryabinin sc->nr.writeback += stat.nr_writeback; 2395d108c772SAndrey Ryabinin sc->nr.immediate += stat.nr_immediate; 2396d108c772SAndrey Ryabinin sc->nr.taken += nr_taken; 2397d108c772SAndrey Ryabinin if (file) 2398d108c772SAndrey Ryabinin sc->nr.file_taken += nr_taken; 23998e950282SMel Gorman 2400599d0c95SMel Gorman trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, 2401d51d1e64SSteven Rostedt nr_scanned, nr_reclaimed, &stat, sc->priority, file); 240205ff5137SAndrew Morton return nr_reclaimed; 24031da177e4SLinus Torvalds } 24041da177e4SLinus Torvalds 240515b44736SHugh Dickins /* 240615b44736SHugh Dickins * shrink_active_list() moves pages from the active LRU to the inactive LRU. 240715b44736SHugh Dickins * 240815b44736SHugh Dickins * We move them the other way if the page is referenced by one or more 240915b44736SHugh Dickins * processes. 241015b44736SHugh Dickins * 241115b44736SHugh Dickins * If the pages are mostly unmapped, the processing is fast and it is 241215b44736SHugh Dickins * appropriate to hold lru_lock across the whole operation. But if 2413b3ac0413SMatthew Wilcox (Oracle) * the pages are mapped, the processing is slow (folio_referenced()), so 241415b44736SHugh Dickins * we should drop lru_lock around each page. It's impossible to balance 241515b44736SHugh Dickins * this, so instead we remove the pages from the LRU while processing them. 241615b44736SHugh Dickins * It is safe to rely on PG_active against the non-LRU pages in here because 241715b44736SHugh Dickins * nobody will play with that bit on a non-LRU page. 241815b44736SHugh Dickins * 241915b44736SHugh Dickins * The downside is that we have to touch page->_refcount against each page. 242015b44736SHugh Dickins * But we had to alter page->flags anyway. 242115b44736SHugh Dickins */ 2422f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 24231a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 2424f16015fbSJohannes Weiner struct scan_control *sc, 24259e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 24261cfb419bSKAMEZAWA Hiroyuki { 242744c241f1SKOSAKI Motohiro unsigned long nr_taken; 2428f626012dSHugh Dickins unsigned long nr_scanned; 24296fe6b7e3SWu Fengguang unsigned long vm_flags; 24301cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 24318cab4754SWu Fengguang LIST_HEAD(l_active); 2432b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 24339d998b4fSMichal Hocko unsigned nr_deactivate, nr_activate; 24349d998b4fSMichal Hocko unsigned nr_rotated = 0; 24353cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 2436599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 24371cfb419bSKAMEZAWA Hiroyuki 24381da177e4SLinus Torvalds lru_add_drain(); 2439f80c0673SMinchan Kim 24406168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 2441925b7673SJohannes Weiner 24425dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 2443a9e7c39fSKirill Tkhai &nr_scanned, sc, lru); 244489b5fae5SJohannes Weiner 2445599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 24461cfb419bSKAMEZAWA Hiroyuki 2447912c0572SShakeel Butt if (!cgroup_reclaim(sc)) 2448599d0c95SMel Gorman __count_vm_events(PGREFILL, nr_scanned); 24492fa2690cSYafang Shao __count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned); 24509d5e6a9fSHugh Dickins 24516168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 24521da177e4SLinus Torvalds 24531da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 2454b3ac0413SMatthew Wilcox (Oracle) struct folio *folio; 2455b3ac0413SMatthew Wilcox (Oracle) struct page *page; 2456b3ac0413SMatthew Wilcox (Oracle) 24571da177e4SLinus Torvalds cond_resched(); 2458b3ac0413SMatthew Wilcox (Oracle) folio = lru_to_folio(&l_hold); 2459b3ac0413SMatthew Wilcox (Oracle) list_del(&folio->lru); 2460b3ac0413SMatthew Wilcox (Oracle) page = &folio->page; 24617e9cd484SRik van Riel 246239b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 2463894bc310SLee Schermerhorn putback_lru_page(page); 2464894bc310SLee Schermerhorn continue; 2465894bc310SLee Schermerhorn } 2466894bc310SLee Schermerhorn 2467cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 2468cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 2469cc715d99SMel Gorman if (page_has_private(page)) 2470cc715d99SMel Gorman try_to_release_page(page, 0); 2471cc715d99SMel Gorman unlock_page(page); 2472cc715d99SMel Gorman } 2473cc715d99SMel Gorman } 2474cc715d99SMel Gorman 2475b3ac0413SMatthew Wilcox (Oracle) if (folio_referenced(folio, 0, sc->target_mem_cgroup, 2476c3ac9a8aSJohannes Weiner &vm_flags)) { 24778cab4754SWu Fengguang /* 24788cab4754SWu Fengguang * Identify referenced, file-backed active pages and 24798cab4754SWu Fengguang * give them one more trip around the active list. So 24808cab4754SWu Fengguang * that executable code get better chances to stay in 24818cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 24828cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 24838cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 24848cab4754SWu Fengguang * so we ignore them here. 24858cab4754SWu Fengguang */ 24869de4f22aSHuang Ying if ((vm_flags & VM_EXEC) && page_is_file_lru(page)) { 24876c357848SMatthew Wilcox (Oracle) nr_rotated += thp_nr_pages(page); 24888cab4754SWu Fengguang list_add(&page->lru, &l_active); 24898cab4754SWu Fengguang continue; 24908cab4754SWu Fengguang } 24918cab4754SWu Fengguang } 24927e9cd484SRik van Riel 24935205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 24941899ad18SJohannes Weiner SetPageWorkingset(page); 24951da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 24961da177e4SLinus Torvalds } 24971da177e4SLinus Torvalds 2498b555749aSAndrew Morton /* 24998cab4754SWu Fengguang * Move pages back to the lru list. 2500b555749aSAndrew Morton */ 25016168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 2502556adecbSRik van Riel 2503a222f341SKirill Tkhai nr_activate = move_pages_to_lru(lruvec, &l_active); 2504a222f341SKirill Tkhai nr_deactivate = move_pages_to_lru(lruvec, &l_inactive); 2505f372d89eSKirill Tkhai /* Keep all free pages in l_active list */ 2506f372d89eSKirill Tkhai list_splice(&l_inactive, &l_active); 25079851ac13SKirill Tkhai 25089851ac13SKirill Tkhai __count_vm_events(PGDEACTIVATE, nr_deactivate); 25099851ac13SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate); 25109851ac13SKirill Tkhai 2511599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 25126168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 25132bcf8879SHugh Dickins 2514f372d89eSKirill Tkhai mem_cgroup_uncharge_list(&l_active); 2515f372d89eSKirill Tkhai free_unref_page_list(&l_active); 25169d998b4fSMichal Hocko trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate, 25179d998b4fSMichal Hocko nr_deactivate, nr_rotated, sc->priority, file); 25181da177e4SLinus Torvalds } 25191da177e4SLinus Torvalds 25201a4e58ccSMinchan Kim unsigned long reclaim_pages(struct list_head *page_list) 25211a4e58ccSMinchan Kim { 2522f661d007SYang Shi int nid = NUMA_NO_NODE; 2523730ec8c0SManinder Singh unsigned int nr_reclaimed = 0; 25241a4e58ccSMinchan Kim LIST_HEAD(node_page_list); 25251a4e58ccSMinchan Kim struct reclaim_stat dummy_stat; 25261a4e58ccSMinchan Kim struct page *page; 25272d2b8d2bSYu Zhao unsigned int noreclaim_flag; 25281a4e58ccSMinchan Kim struct scan_control sc = { 25291a4e58ccSMinchan Kim .gfp_mask = GFP_KERNEL, 25301a4e58ccSMinchan Kim .may_writepage = 1, 25311a4e58ccSMinchan Kim .may_unmap = 1, 25321a4e58ccSMinchan Kim .may_swap = 1, 253326aa2d19SDave Hansen .no_demotion = 1, 25341a4e58ccSMinchan Kim }; 25351a4e58ccSMinchan Kim 25362d2b8d2bSYu Zhao noreclaim_flag = memalloc_noreclaim_save(); 25372d2b8d2bSYu Zhao 25381a4e58ccSMinchan Kim while (!list_empty(page_list)) { 25391a4e58ccSMinchan Kim page = lru_to_page(page_list); 2540f661d007SYang Shi if (nid == NUMA_NO_NODE) { 25411a4e58ccSMinchan Kim nid = page_to_nid(page); 25421a4e58ccSMinchan Kim INIT_LIST_HEAD(&node_page_list); 25431a4e58ccSMinchan Kim } 25441a4e58ccSMinchan Kim 25451a4e58ccSMinchan Kim if (nid == page_to_nid(page)) { 25461a4e58ccSMinchan Kim ClearPageActive(page); 25471a4e58ccSMinchan Kim list_move(&page->lru, &node_page_list); 25481a4e58ccSMinchan Kim continue; 25491a4e58ccSMinchan Kim } 25501a4e58ccSMinchan Kim 25511a4e58ccSMinchan Kim nr_reclaimed += shrink_page_list(&node_page_list, 25521a4e58ccSMinchan Kim NODE_DATA(nid), 2553013339dfSShakeel Butt &sc, &dummy_stat, false); 25541a4e58ccSMinchan Kim while (!list_empty(&node_page_list)) { 25551a4e58ccSMinchan Kim page = lru_to_page(&node_page_list); 25561a4e58ccSMinchan Kim list_del(&page->lru); 25571a4e58ccSMinchan Kim putback_lru_page(page); 25581a4e58ccSMinchan Kim } 25591a4e58ccSMinchan Kim 2560f661d007SYang Shi nid = NUMA_NO_NODE; 25611a4e58ccSMinchan Kim } 25621a4e58ccSMinchan Kim 25631a4e58ccSMinchan Kim if (!list_empty(&node_page_list)) { 25641a4e58ccSMinchan Kim nr_reclaimed += shrink_page_list(&node_page_list, 25651a4e58ccSMinchan Kim NODE_DATA(nid), 2566013339dfSShakeel Butt &sc, &dummy_stat, false); 25671a4e58ccSMinchan Kim while (!list_empty(&node_page_list)) { 25681a4e58ccSMinchan Kim page = lru_to_page(&node_page_list); 25691a4e58ccSMinchan Kim list_del(&page->lru); 25701a4e58ccSMinchan Kim putback_lru_page(page); 25711a4e58ccSMinchan Kim } 25721a4e58ccSMinchan Kim } 25731a4e58ccSMinchan Kim 25742d2b8d2bSYu Zhao memalloc_noreclaim_restore(noreclaim_flag); 25752d2b8d2bSYu Zhao 25761a4e58ccSMinchan Kim return nr_reclaimed; 25771a4e58ccSMinchan Kim } 25781a4e58ccSMinchan Kim 2579b91ac374SJohannes Weiner static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 2580b91ac374SJohannes Weiner struct lruvec *lruvec, struct scan_control *sc) 2581b91ac374SJohannes Weiner { 2582b91ac374SJohannes Weiner if (is_active_lru(lru)) { 2583b91ac374SJohannes Weiner if (sc->may_deactivate & (1 << is_file_lru(lru))) 2584b91ac374SJohannes Weiner shrink_active_list(nr_to_scan, lruvec, sc, lru); 2585b91ac374SJohannes Weiner else 2586b91ac374SJohannes Weiner sc->skipped_deactivate = 1; 2587b91ac374SJohannes Weiner return 0; 2588b91ac374SJohannes Weiner } 2589b91ac374SJohannes Weiner 2590b91ac374SJohannes Weiner return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 2591b91ac374SJohannes Weiner } 2592b91ac374SJohannes Weiner 259359dc76b0SRik van Riel /* 259459dc76b0SRik van Riel * The inactive anon list should be small enough that the VM never has 259559dc76b0SRik van Riel * to do too much work. 259614797e23SKOSAKI Motohiro * 259759dc76b0SRik van Riel * The inactive file list should be small enough to leave most memory 259859dc76b0SRik van Riel * to the established workingset on the scan-resistant active list, 259959dc76b0SRik van Riel * but large enough to avoid thrashing the aggregate readahead window. 260059dc76b0SRik van Riel * 260159dc76b0SRik van Riel * Both inactive lists should also be large enough that each inactive 260259dc76b0SRik van Riel * page has a chance to be referenced again before it is reclaimed. 260359dc76b0SRik van Riel * 26042a2e4885SJohannes Weiner * If that fails and refaulting is observed, the inactive list grows. 26052a2e4885SJohannes Weiner * 260659dc76b0SRik van Riel * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages 26073a50d14dSAndrey Ryabinin * on this LRU, maintained by the pageout code. An inactive_ratio 260859dc76b0SRik van Riel * of 3 means 3:1 or 25% of the pages are kept on the inactive list. 260959dc76b0SRik van Riel * 261059dc76b0SRik van Riel * total target max 261159dc76b0SRik van Riel * memory ratio inactive 261259dc76b0SRik van Riel * ------------------------------------- 261359dc76b0SRik van Riel * 10MB 1 5MB 261459dc76b0SRik van Riel * 100MB 1 50MB 261559dc76b0SRik van Riel * 1GB 3 250MB 261659dc76b0SRik van Riel * 10GB 10 0.9GB 261759dc76b0SRik van Riel * 100GB 31 3GB 261859dc76b0SRik van Riel * 1TB 101 10GB 261959dc76b0SRik van Riel * 10TB 320 32GB 262014797e23SKOSAKI Motohiro */ 2621b91ac374SJohannes Weiner static bool inactive_is_low(struct lruvec *lruvec, enum lru_list inactive_lru) 262214797e23SKOSAKI Motohiro { 2623b91ac374SJohannes Weiner enum lru_list active_lru = inactive_lru + LRU_ACTIVE; 26242a2e4885SJohannes Weiner unsigned long inactive, active; 26252a2e4885SJohannes Weiner unsigned long inactive_ratio; 262659dc76b0SRik van Riel unsigned long gb; 262759dc76b0SRik van Riel 2628b91ac374SJohannes Weiner inactive = lruvec_page_state(lruvec, NR_LRU_BASE + inactive_lru); 2629b91ac374SJohannes Weiner active = lruvec_page_state(lruvec, NR_LRU_BASE + active_lru); 2630f8d1a311SMel Gorman 263159dc76b0SRik van Riel gb = (inactive + active) >> (30 - PAGE_SHIFT); 26324002570cSJoonsoo Kim if (gb) 263359dc76b0SRik van Riel inactive_ratio = int_sqrt(10 * gb); 2634b39415b2SRik van Riel else 263559dc76b0SRik van Riel inactive_ratio = 1; 2636fd538803SMichal Hocko 263759dc76b0SRik van Riel return inactive * inactive_ratio < active; 2638b39415b2SRik van Riel } 2639b39415b2SRik van Riel 26409a265114SJohannes Weiner enum scan_balance { 26419a265114SJohannes Weiner SCAN_EQUAL, 26429a265114SJohannes Weiner SCAN_FRACT, 26439a265114SJohannes Weiner SCAN_ANON, 26449a265114SJohannes Weiner SCAN_FILE, 26459a265114SJohannes Weiner }; 26469a265114SJohannes Weiner 26471da177e4SLinus Torvalds /* 26484f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 26494f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 26504f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 26514f98a2feSRik van Riel * onto the active list instead of evict. 26524f98a2feSRik van Riel * 2653be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 2654be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 26554f98a2feSRik van Riel */ 2656afaf07a6SJohannes Weiner static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, 2657afaf07a6SJohannes Weiner unsigned long *nr) 26584f98a2feSRik van Riel { 2659a2a36488SKeith Busch struct pglist_data *pgdat = lruvec_pgdat(lruvec); 2660afaf07a6SJohannes Weiner struct mem_cgroup *memcg = lruvec_memcg(lruvec); 2661d483a5ddSJohannes Weiner unsigned long anon_cost, file_cost, total_cost; 266233377678SVladimir Davydov int swappiness = mem_cgroup_swappiness(memcg); 2663ed017373SYu Zhao u64 fraction[ANON_AND_FILE]; 26649a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 26659a265114SJohannes Weiner enum scan_balance scan_balance; 26669a265114SJohannes Weiner unsigned long ap, fp; 26679a265114SJohannes Weiner enum lru_list lru; 266876a33fc3SShaohua Li 266976a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 2670a2a36488SKeith Busch if (!sc->may_swap || !can_reclaim_anon_pages(memcg, pgdat->node_id, sc)) { 26719a265114SJohannes Weiner scan_balance = SCAN_FILE; 267276a33fc3SShaohua Li goto out; 267376a33fc3SShaohua Li } 26744f98a2feSRik van Riel 267510316b31SJohannes Weiner /* 267610316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 267710316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 267810316b31SJohannes Weiner * disable swapping for individual groups completely when 267910316b31SJohannes Weiner * using the memory controller's swap limit feature would be 268010316b31SJohannes Weiner * too expensive. 268110316b31SJohannes Weiner */ 2682b5ead35eSJohannes Weiner if (cgroup_reclaim(sc) && !swappiness) { 26839a265114SJohannes Weiner scan_balance = SCAN_FILE; 268410316b31SJohannes Weiner goto out; 268510316b31SJohannes Weiner } 268610316b31SJohannes Weiner 268710316b31SJohannes Weiner /* 268810316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 268910316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 269010316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 269110316b31SJohannes Weiner */ 269202695175SJohannes Weiner if (!sc->priority && swappiness) { 26939a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 269410316b31SJohannes Weiner goto out; 269510316b31SJohannes Weiner } 269610316b31SJohannes Weiner 269711d16c25SJohannes Weiner /* 269853138ceaSJohannes Weiner * If the system is almost out of file pages, force-scan anon. 269962376251SJohannes Weiner */ 2700b91ac374SJohannes Weiner if (sc->file_is_tiny) { 270162376251SJohannes Weiner scan_balance = SCAN_ANON; 270262376251SJohannes Weiner goto out; 270362376251SJohannes Weiner } 270462376251SJohannes Weiner 270562376251SJohannes Weiner /* 2706b91ac374SJohannes Weiner * If there is enough inactive page cache, we do not reclaim 2707b91ac374SJohannes Weiner * anything from the anonymous working right now. 2708e9868505SRik van Riel */ 2709b91ac374SJohannes Weiner if (sc->cache_trim_mode) { 27109a265114SJohannes Weiner scan_balance = SCAN_FILE; 2711e9868505SRik van Riel goto out; 27124f98a2feSRik van Riel } 27134f98a2feSRik van Riel 27149a265114SJohannes Weiner scan_balance = SCAN_FRACT; 27154f98a2feSRik van Riel /* 2716314b57fbSJohannes Weiner * Calculate the pressure balance between anon and file pages. 2717314b57fbSJohannes Weiner * 2718314b57fbSJohannes Weiner * The amount of pressure we put on each LRU is inversely 2719314b57fbSJohannes Weiner * proportional to the cost of reclaiming each list, as 2720314b57fbSJohannes Weiner * determined by the share of pages that are refaulting, times 2721314b57fbSJohannes Weiner * the relative IO cost of bringing back a swapped out 2722314b57fbSJohannes Weiner * anonymous page vs reloading a filesystem page (swappiness). 2723314b57fbSJohannes Weiner * 2724d483a5ddSJohannes Weiner * Although we limit that influence to ensure no list gets 2725d483a5ddSJohannes Weiner * left behind completely: at least a third of the pressure is 2726d483a5ddSJohannes Weiner * applied, before swappiness. 2727d483a5ddSJohannes Weiner * 2728314b57fbSJohannes Weiner * With swappiness at 100, anon and file have equal IO cost. 272958c37f6eSKOSAKI Motohiro */ 2730d483a5ddSJohannes Weiner total_cost = sc->anon_cost + sc->file_cost; 2731d483a5ddSJohannes Weiner anon_cost = total_cost + sc->anon_cost; 2732d483a5ddSJohannes Weiner file_cost = total_cost + sc->file_cost; 2733d483a5ddSJohannes Weiner total_cost = anon_cost + file_cost; 273458c37f6eSKOSAKI Motohiro 2735d483a5ddSJohannes Weiner ap = swappiness * (total_cost + 1); 2736d483a5ddSJohannes Weiner ap /= anon_cost + 1; 27374f98a2feSRik van Riel 2738d483a5ddSJohannes Weiner fp = (200 - swappiness) * (total_cost + 1); 2739d483a5ddSJohannes Weiner fp /= file_cost + 1; 27404f98a2feSRik van Riel 274176a33fc3SShaohua Li fraction[0] = ap; 274276a33fc3SShaohua Li fraction[1] = fp; 2743a4fe1631SJohannes Weiner denominator = ap + fp; 274476a33fc3SShaohua Li out: 27454111304dSHugh Dickins for_each_evictable_lru(lru) { 27464111304dSHugh Dickins int file = is_file_lru(lru); 27479783aa99SChris Down unsigned long lruvec_size; 2748f56ce412SJohannes Weiner unsigned long low, min; 274976a33fc3SShaohua Li unsigned long scan; 275076a33fc3SShaohua Li 27519783aa99SChris Down lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); 2752f56ce412SJohannes Weiner mem_cgroup_protection(sc->target_mem_cgroup, memcg, 2753f56ce412SJohannes Weiner &min, &low); 27549783aa99SChris Down 2755f56ce412SJohannes Weiner if (min || low) { 27569783aa99SChris Down /* 27579783aa99SChris Down * Scale a cgroup's reclaim pressure by proportioning 27589783aa99SChris Down * its current usage to its memory.low or memory.min 27599783aa99SChris Down * setting. 27609783aa99SChris Down * 27619783aa99SChris Down * This is important, as otherwise scanning aggression 27629783aa99SChris Down * becomes extremely binary -- from nothing as we 27639783aa99SChris Down * approach the memory protection threshold, to totally 27649783aa99SChris Down * nominal as we exceed it. This results in requiring 27659783aa99SChris Down * setting extremely liberal protection thresholds. It 27669783aa99SChris Down * also means we simply get no protection at all if we 27679783aa99SChris Down * set it too low, which is not ideal. 27681bc63fb1SChris Down * 27691bc63fb1SChris Down * If there is any protection in place, we reduce scan 27701bc63fb1SChris Down * pressure by how much of the total memory used is 27711bc63fb1SChris Down * within protection thresholds. 27729783aa99SChris Down * 27739de7ca46SChris Down * There is one special case: in the first reclaim pass, 27749de7ca46SChris Down * we skip over all groups that are within their low 27759de7ca46SChris Down * protection. If that fails to reclaim enough pages to 27769de7ca46SChris Down * satisfy the reclaim goal, we come back and override 27779de7ca46SChris Down * the best-effort low protection. However, we still 27789de7ca46SChris Down * ideally want to honor how well-behaved groups are in 27799de7ca46SChris Down * that case instead of simply punishing them all 27809de7ca46SChris Down * equally. As such, we reclaim them based on how much 27811bc63fb1SChris Down * memory they are using, reducing the scan pressure 27821bc63fb1SChris Down * again by how much of the total memory used is under 27831bc63fb1SChris Down * hard protection. 27849783aa99SChris Down */ 27851bc63fb1SChris Down unsigned long cgroup_size = mem_cgroup_size(memcg); 2786f56ce412SJohannes Weiner unsigned long protection; 2787f56ce412SJohannes Weiner 2788f56ce412SJohannes Weiner /* memory.low scaling, make sure we retry before OOM */ 2789f56ce412SJohannes Weiner if (!sc->memcg_low_reclaim && low > min) { 2790f56ce412SJohannes Weiner protection = low; 2791f56ce412SJohannes Weiner sc->memcg_low_skipped = 1; 2792f56ce412SJohannes Weiner } else { 2793f56ce412SJohannes Weiner protection = min; 2794f56ce412SJohannes Weiner } 27951bc63fb1SChris Down 27961bc63fb1SChris Down /* Avoid TOCTOU with earlier protection check */ 27971bc63fb1SChris Down cgroup_size = max(cgroup_size, protection); 27981bc63fb1SChris Down 27991bc63fb1SChris Down scan = lruvec_size - lruvec_size * protection / 280032d4f4b7SRik van Riel (cgroup_size + 1); 28019783aa99SChris Down 28029783aa99SChris Down /* 28031bc63fb1SChris Down * Minimally target SWAP_CLUSTER_MAX pages to keep 280455b65a57SEthon Paul * reclaim moving forwards, avoiding decrementing 28059de7ca46SChris Down * sc->priority further than desirable. 28069783aa99SChris Down */ 28071bc63fb1SChris Down scan = max(scan, SWAP_CLUSTER_MAX); 28089783aa99SChris Down } else { 28099783aa99SChris Down scan = lruvec_size; 28109783aa99SChris Down } 28119783aa99SChris Down 28129783aa99SChris Down scan >>= sc->priority; 28139783aa99SChris Down 2814688035f7SJohannes Weiner /* 2815688035f7SJohannes Weiner * If the cgroup's already been deleted, make sure to 2816688035f7SJohannes Weiner * scrape out the remaining cache. 2817688035f7SJohannes Weiner */ 2818688035f7SJohannes Weiner if (!scan && !mem_cgroup_online(memcg)) 28199783aa99SChris Down scan = min(lruvec_size, SWAP_CLUSTER_MAX); 28209a265114SJohannes Weiner 28219a265114SJohannes Weiner switch (scan_balance) { 28229a265114SJohannes Weiner case SCAN_EQUAL: 28239a265114SJohannes Weiner /* Scan lists relative to size */ 28249a265114SJohannes Weiner break; 28259a265114SJohannes Weiner case SCAN_FRACT: 28269a265114SJohannes Weiner /* 28279a265114SJohannes Weiner * Scan types proportional to swappiness and 28289a265114SJohannes Weiner * their relative recent reclaim efficiency. 282976073c64SGavin Shan * Make sure we don't miss the last page on 283076073c64SGavin Shan * the offlined memory cgroups because of a 283176073c64SGavin Shan * round-off error. 28329a265114SJohannes Weiner */ 283376073c64SGavin Shan scan = mem_cgroup_online(memcg) ? 283476073c64SGavin Shan div64_u64(scan * fraction[file], denominator) : 283576073c64SGavin Shan DIV64_U64_ROUND_UP(scan * fraction[file], 28366f04f48dSSuleiman Souhlal denominator); 28379a265114SJohannes Weiner break; 28389a265114SJohannes Weiner case SCAN_FILE: 28399a265114SJohannes Weiner case SCAN_ANON: 28409a265114SJohannes Weiner /* Scan one type exclusively */ 2841e072bff6SMateusz Nosek if ((scan_balance == SCAN_FILE) != file) 28429a265114SJohannes Weiner scan = 0; 28439a265114SJohannes Weiner break; 28449a265114SJohannes Weiner default: 28459a265114SJohannes Weiner /* Look ma, no brain */ 28469a265114SJohannes Weiner BUG(); 28479a265114SJohannes Weiner } 28486b4f7799SJohannes Weiner 28494111304dSHugh Dickins nr[lru] = scan; 285076a33fc3SShaohua Li } 28516e08a369SWu Fengguang } 28524f98a2feSRik van Riel 28532f368a9fSDave Hansen /* 28542f368a9fSDave Hansen * Anonymous LRU management is a waste if there is 28552f368a9fSDave Hansen * ultimately no way to reclaim the memory. 28562f368a9fSDave Hansen */ 28572f368a9fSDave Hansen static bool can_age_anon_pages(struct pglist_data *pgdat, 28582f368a9fSDave Hansen struct scan_control *sc) 28592f368a9fSDave Hansen { 28602f368a9fSDave Hansen /* Aging the anon LRU is valuable if swap is present: */ 28612f368a9fSDave Hansen if (total_swap_pages > 0) 28622f368a9fSDave Hansen return true; 28632f368a9fSDave Hansen 28642f368a9fSDave Hansen /* Also valuable if anon pages can be demoted: */ 28652f368a9fSDave Hansen return can_demote(pgdat->node_id, sc); 28662f368a9fSDave Hansen } 28672f368a9fSDave Hansen 2868afaf07a6SJohannes Weiner static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) 28699b4f98cdSJohannes Weiner { 28709b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 2871e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 28729b4f98cdSJohannes Weiner unsigned long nr_to_scan; 28739b4f98cdSJohannes Weiner enum lru_list lru; 28749b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 28759b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 28769b4f98cdSJohannes Weiner struct blk_plug plug; 28771a501907SMel Gorman bool scan_adjusted; 28789b4f98cdSJohannes Weiner 2879afaf07a6SJohannes Weiner get_scan_count(lruvec, sc, nr); 28809b4f98cdSJohannes Weiner 2881e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 2882e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 2883e82e0561SMel Gorman 28841a501907SMel Gorman /* 28851a501907SMel Gorman * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal 28861a501907SMel Gorman * event that can occur when there is little memory pressure e.g. 28871a501907SMel Gorman * multiple streaming readers/writers. Hence, we do not abort scanning 28881a501907SMel Gorman * when the requested number of pages are reclaimed when scanning at 28891a501907SMel Gorman * DEF_PRIORITY on the assumption that the fact we are direct 28901a501907SMel Gorman * reclaiming implies that kswapd is not keeping up and it is best to 28911a501907SMel Gorman * do a batch of work at once. For memcg reclaim one check is made to 28921a501907SMel Gorman * abort proportional reclaim if either the file or anon lru has already 28931a501907SMel Gorman * dropped to zero at the first pass. 28941a501907SMel Gorman */ 2895b5ead35eSJohannes Weiner scan_adjusted = (!cgroup_reclaim(sc) && !current_is_kswapd() && 28961a501907SMel Gorman sc->priority == DEF_PRIORITY); 28971a501907SMel Gorman 28989b4f98cdSJohannes Weiner blk_start_plug(&plug); 28999b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 29009b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 2901e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 2902e82e0561SMel Gorman unsigned long nr_scanned; 2903e82e0561SMel Gorman 29049b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 29059b4f98cdSJohannes Weiner if (nr[lru]) { 29069b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 29079b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 29089b4f98cdSJohannes Weiner 29099b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 29103b991208SJohannes Weiner lruvec, sc); 29119b4f98cdSJohannes Weiner } 29129b4f98cdSJohannes Weiner } 2913e82e0561SMel Gorman 2914bd041733SMichal Hocko cond_resched(); 2915bd041733SMichal Hocko 2916e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 2917e82e0561SMel Gorman continue; 2918e82e0561SMel Gorman 29199b4f98cdSJohannes Weiner /* 2920e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 29211a501907SMel Gorman * requested. Ensure that the anon and file LRUs are scanned 2922e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 2923e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 2924e82e0561SMel Gorman * proportional to the original scan target. 2925e82e0561SMel Gorman */ 2926e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 2927e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 2928e82e0561SMel Gorman 29291a501907SMel Gorman /* 29301a501907SMel Gorman * It's just vindictive to attack the larger once the smaller 29311a501907SMel Gorman * has gone to zero. And given the way we stop scanning the 29321a501907SMel Gorman * smaller below, this makes sure that we only make one nudge 29331a501907SMel Gorman * towards proportionality once we've got nr_to_reclaim. 29341a501907SMel Gorman */ 29351a501907SMel Gorman if (!nr_file || !nr_anon) 29361a501907SMel Gorman break; 29371a501907SMel Gorman 2938e82e0561SMel Gorman if (nr_file > nr_anon) { 2939e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 2940e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 2941e82e0561SMel Gorman lru = LRU_BASE; 2942e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 2943e82e0561SMel Gorman } else { 2944e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 2945e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 2946e82e0561SMel Gorman lru = LRU_FILE; 2947e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 2948e82e0561SMel Gorman } 2949e82e0561SMel Gorman 2950e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 2951e82e0561SMel Gorman nr[lru] = 0; 2952e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 2953e82e0561SMel Gorman 2954e82e0561SMel Gorman /* 2955e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 2956e82e0561SMel Gorman * scan target and the percentage scanning already complete 2957e82e0561SMel Gorman */ 2958e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 2959e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2960e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2961e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2962e82e0561SMel Gorman 2963e82e0561SMel Gorman lru += LRU_ACTIVE; 2964e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2965e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2966e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2967e82e0561SMel Gorman 2968e82e0561SMel Gorman scan_adjusted = true; 29699b4f98cdSJohannes Weiner } 29709b4f98cdSJohannes Weiner blk_finish_plug(&plug); 29719b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 29729b4f98cdSJohannes Weiner 29739b4f98cdSJohannes Weiner /* 29749b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 29759b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 29769b4f98cdSJohannes Weiner */ 29772f368a9fSDave Hansen if (can_age_anon_pages(lruvec_pgdat(lruvec), sc) && 29782f368a9fSDave Hansen inactive_is_low(lruvec, LRU_INACTIVE_ANON)) 29799b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 29809b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 29819b4f98cdSJohannes Weiner } 29829b4f98cdSJohannes Weiner 298323b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 29849e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 298523b9da55SMel Gorman { 2986d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 298723b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 29889e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 298923b9da55SMel Gorman return true; 299023b9da55SMel Gorman 299123b9da55SMel Gorman return false; 299223b9da55SMel Gorman } 299323b9da55SMel Gorman 29944f98a2feSRik van Riel /* 299523b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 299623b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 299723b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 2998df3a45f9SQiwu Chen * calls try_to_compact_pages() that it will have enough free pages to succeed. 299923b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 30003e7d3449SMel Gorman */ 3001a9dd0a83SMel Gorman static inline bool should_continue_reclaim(struct pglist_data *pgdat, 30023e7d3449SMel Gorman unsigned long nr_reclaimed, 30033e7d3449SMel Gorman struct scan_control *sc) 30043e7d3449SMel Gorman { 30053e7d3449SMel Gorman unsigned long pages_for_compaction; 30063e7d3449SMel Gorman unsigned long inactive_lru_pages; 3007a9dd0a83SMel Gorman int z; 30083e7d3449SMel Gorman 30093e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 30109e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 30113e7d3449SMel Gorman return false; 30123e7d3449SMel Gorman 30133e7d3449SMel Gorman /* 30145ee04716SVlastimil Babka * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX 30155ee04716SVlastimil Babka * number of pages that were scanned. This will return to the caller 30165ee04716SVlastimil Babka * with the risk reclaim/compaction and the resulting allocation attempt 30175ee04716SVlastimil Babka * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL 30185ee04716SVlastimil Babka * allocations through requiring that the full LRU list has been scanned 30195ee04716SVlastimil Babka * first, by assuming that zero delta of sc->nr_scanned means full LRU 30205ee04716SVlastimil Babka * scan, but that approximation was wrong, and there were corner cases 30215ee04716SVlastimil Babka * where always a non-zero amount of pages were scanned. 30222876592fSMel Gorman */ 30232876592fSMel Gorman if (!nr_reclaimed) 30242876592fSMel Gorman return false; 30253e7d3449SMel Gorman 30263e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 3027a9dd0a83SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 3028a9dd0a83SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 30296aa303deSMel Gorman if (!managed_zone(zone)) 3030a9dd0a83SMel Gorman continue; 3031a9dd0a83SMel Gorman 3032a9dd0a83SMel Gorman switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) { 3033cf378319SVlastimil Babka case COMPACT_SUCCESS: 30343e7d3449SMel Gorman case COMPACT_CONTINUE: 30353e7d3449SMel Gorman return false; 30363e7d3449SMel Gorman default: 3037a9dd0a83SMel Gorman /* check next zone */ 3038a9dd0a83SMel Gorman ; 30393e7d3449SMel Gorman } 30403e7d3449SMel Gorman } 30411c6c1597SHillf Danton 30421c6c1597SHillf Danton /* 30431c6c1597SHillf Danton * If we have not reclaimed enough pages for compaction and the 30441c6c1597SHillf Danton * inactive lists are large enough, continue reclaiming 30451c6c1597SHillf Danton */ 30461c6c1597SHillf Danton pages_for_compaction = compact_gap(sc->order); 30471c6c1597SHillf Danton inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); 3048a2a36488SKeith Busch if (can_reclaim_anon_pages(NULL, pgdat->node_id, sc)) 30491c6c1597SHillf Danton inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); 30501c6c1597SHillf Danton 30515ee04716SVlastimil Babka return inactive_lru_pages > pages_for_compaction; 3052a9dd0a83SMel Gorman } 30533e7d3449SMel Gorman 30540f6a5cffSJohannes Weiner static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc) 3055f16015fbSJohannes Weiner { 30560f6a5cffSJohannes Weiner struct mem_cgroup *target_memcg = sc->target_mem_cgroup; 3057694fbc0fSAndrew Morton struct mem_cgroup *memcg; 3058d108c772SAndrey Ryabinin 30590f6a5cffSJohannes Weiner memcg = mem_cgroup_iter(target_memcg, NULL, NULL); 3060694fbc0fSAndrew Morton do { 3061afaf07a6SJohannes Weiner struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); 30628e8ae645SJohannes Weiner unsigned long reclaimed; 3063cb731d6cSVladimir Davydov unsigned long scanned; 30649b4f98cdSJohannes Weiner 3065e3336cabSXunlei Pang /* 3066e3336cabSXunlei Pang * This loop can become CPU-bound when target memcgs 3067e3336cabSXunlei Pang * aren't eligible for reclaim - either because they 3068e3336cabSXunlei Pang * don't have any reclaimable pages, or because their 3069e3336cabSXunlei Pang * memory is explicitly protected. Avoid soft lockups. 3070e3336cabSXunlei Pang */ 3071e3336cabSXunlei Pang cond_resched(); 3072e3336cabSXunlei Pang 307345c7f7e1SChris Down mem_cgroup_calculate_protection(target_memcg, memcg); 307445c7f7e1SChris Down 307545c7f7e1SChris Down if (mem_cgroup_below_min(memcg)) { 3076bf8d5d52SRoman Gushchin /* 3077bf8d5d52SRoman Gushchin * Hard protection. 3078bf8d5d52SRoman Gushchin * If there is no reclaimable memory, OOM. 3079bf8d5d52SRoman Gushchin */ 3080bf8d5d52SRoman Gushchin continue; 308145c7f7e1SChris Down } else if (mem_cgroup_below_low(memcg)) { 3082bf8d5d52SRoman Gushchin /* 3083bf8d5d52SRoman Gushchin * Soft protection. 3084bf8d5d52SRoman Gushchin * Respect the protection only as long as 3085bf8d5d52SRoman Gushchin * there is an unprotected supply 3086bf8d5d52SRoman Gushchin * of reclaimable memory from other cgroups. 3087bf8d5d52SRoman Gushchin */ 3088d6622f63SYisheng Xie if (!sc->memcg_low_reclaim) { 3089d6622f63SYisheng Xie sc->memcg_low_skipped = 1; 3090241994edSJohannes Weiner continue; 3091d6622f63SYisheng Xie } 3092e27be240SJohannes Weiner memcg_memory_event(memcg, MEMCG_LOW); 3093241994edSJohannes Weiner } 3094241994edSJohannes Weiner 30958e8ae645SJohannes Weiner reclaimed = sc->nr_reclaimed; 3096cb731d6cSVladimir Davydov scanned = sc->nr_scanned; 3097afaf07a6SJohannes Weiner 3098afaf07a6SJohannes Weiner shrink_lruvec(lruvec, sc); 3099f9be23d6SKonstantin Khlebnikov 310028360f39SMel Gorman shrink_slab(sc->gfp_mask, pgdat->node_id, memcg, 310128360f39SMel Gorman sc->priority); 3102cb731d6cSVladimir Davydov 31038e8ae645SJohannes Weiner /* Record the group's reclaim efficiency */ 31048e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, memcg, false, 31058e8ae645SJohannes Weiner sc->nr_scanned - scanned, 31068e8ae645SJohannes Weiner sc->nr_reclaimed - reclaimed); 31078e8ae645SJohannes Weiner 31080f6a5cffSJohannes Weiner } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL))); 31090f6a5cffSJohannes Weiner } 31100f6a5cffSJohannes Weiner 31116c9e0907SLiu Song static void shrink_node(pg_data_t *pgdat, struct scan_control *sc) 31120f6a5cffSJohannes Weiner { 31130f6a5cffSJohannes Weiner struct reclaim_state *reclaim_state = current->reclaim_state; 31140f6a5cffSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 31151b05117dSJohannes Weiner struct lruvec *target_lruvec; 31160f6a5cffSJohannes Weiner bool reclaimable = false; 3117b91ac374SJohannes Weiner unsigned long file; 31180f6a5cffSJohannes Weiner 31191b05117dSJohannes Weiner target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat); 31201b05117dSJohannes Weiner 31210f6a5cffSJohannes Weiner again: 3122aa48e47eSShakeel Butt /* 3123aa48e47eSShakeel Butt * Flush the memory cgroup stats, so that we read accurate per-memcg 3124aa48e47eSShakeel Butt * lruvec stats for heuristics. 3125aa48e47eSShakeel Butt */ 3126aa48e47eSShakeel Butt mem_cgroup_flush_stats(); 3127aa48e47eSShakeel Butt 31280f6a5cffSJohannes Weiner memset(&sc->nr, 0, sizeof(sc->nr)); 31290f6a5cffSJohannes Weiner 31300f6a5cffSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 31310f6a5cffSJohannes Weiner nr_scanned = sc->nr_scanned; 31320f6a5cffSJohannes Weiner 313353138ceaSJohannes Weiner /* 31347cf111bcSJohannes Weiner * Determine the scan balance between anon and file LRUs. 31357cf111bcSJohannes Weiner */ 31366168d0daSAlex Shi spin_lock_irq(&target_lruvec->lru_lock); 31377cf111bcSJohannes Weiner sc->anon_cost = target_lruvec->anon_cost; 31387cf111bcSJohannes Weiner sc->file_cost = target_lruvec->file_cost; 31396168d0daSAlex Shi spin_unlock_irq(&target_lruvec->lru_lock); 31407cf111bcSJohannes Weiner 31417cf111bcSJohannes Weiner /* 3142b91ac374SJohannes Weiner * Target desirable inactive:active list ratios for the anon 3143b91ac374SJohannes Weiner * and file LRU lists. 3144b91ac374SJohannes Weiner */ 3145b91ac374SJohannes Weiner if (!sc->force_deactivate) { 3146b91ac374SJohannes Weiner unsigned long refaults; 3147b91ac374SJohannes Weiner 3148170b04b7SJoonsoo Kim refaults = lruvec_page_state(target_lruvec, 3149170b04b7SJoonsoo Kim WORKINGSET_ACTIVATE_ANON); 3150170b04b7SJoonsoo Kim if (refaults != target_lruvec->refaults[0] || 3151170b04b7SJoonsoo Kim inactive_is_low(target_lruvec, LRU_INACTIVE_ANON)) 3152b91ac374SJohannes Weiner sc->may_deactivate |= DEACTIVATE_ANON; 3153b91ac374SJohannes Weiner else 3154b91ac374SJohannes Weiner sc->may_deactivate &= ~DEACTIVATE_ANON; 3155b91ac374SJohannes Weiner 3156b91ac374SJohannes Weiner /* 3157b91ac374SJohannes Weiner * When refaults are being observed, it means a new 3158b91ac374SJohannes Weiner * workingset is being established. Deactivate to get 3159b91ac374SJohannes Weiner * rid of any stale active pages quickly. 3160b91ac374SJohannes Weiner */ 3161b91ac374SJohannes Weiner refaults = lruvec_page_state(target_lruvec, 3162170b04b7SJoonsoo Kim WORKINGSET_ACTIVATE_FILE); 3163170b04b7SJoonsoo Kim if (refaults != target_lruvec->refaults[1] || 3164b91ac374SJohannes Weiner inactive_is_low(target_lruvec, LRU_INACTIVE_FILE)) 3165b91ac374SJohannes Weiner sc->may_deactivate |= DEACTIVATE_FILE; 3166b91ac374SJohannes Weiner else 3167b91ac374SJohannes Weiner sc->may_deactivate &= ~DEACTIVATE_FILE; 3168b91ac374SJohannes Weiner } else 3169b91ac374SJohannes Weiner sc->may_deactivate = DEACTIVATE_ANON | DEACTIVATE_FILE; 3170b91ac374SJohannes Weiner 3171b91ac374SJohannes Weiner /* 3172b91ac374SJohannes Weiner * If we have plenty of inactive file pages that aren't 3173b91ac374SJohannes Weiner * thrashing, try to reclaim those first before touching 3174b91ac374SJohannes Weiner * anonymous pages. 3175b91ac374SJohannes Weiner */ 3176b91ac374SJohannes Weiner file = lruvec_page_state(target_lruvec, NR_INACTIVE_FILE); 3177b91ac374SJohannes Weiner if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE)) 3178b91ac374SJohannes Weiner sc->cache_trim_mode = 1; 3179b91ac374SJohannes Weiner else 3180b91ac374SJohannes Weiner sc->cache_trim_mode = 0; 3181b91ac374SJohannes Weiner 3182b91ac374SJohannes Weiner /* 318353138ceaSJohannes Weiner * Prevent the reclaimer from falling into the cache trap: as 318453138ceaSJohannes Weiner * cache pages start out inactive, every cache fault will tip 318553138ceaSJohannes Weiner * the scan balance towards the file LRU. And as the file LRU 318653138ceaSJohannes Weiner * shrinks, so does the window for rotation from references. 318753138ceaSJohannes Weiner * This means we have a runaway feedback loop where a tiny 318853138ceaSJohannes Weiner * thrashing file LRU becomes infinitely more attractive than 318953138ceaSJohannes Weiner * anon pages. Try to detect this based on file LRU size. 319053138ceaSJohannes Weiner */ 319153138ceaSJohannes Weiner if (!cgroup_reclaim(sc)) { 319253138ceaSJohannes Weiner unsigned long total_high_wmark = 0; 3193b91ac374SJohannes Weiner unsigned long free, anon; 3194b91ac374SJohannes Weiner int z; 319553138ceaSJohannes Weiner 319653138ceaSJohannes Weiner free = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); 319753138ceaSJohannes Weiner file = node_page_state(pgdat, NR_ACTIVE_FILE) + 319853138ceaSJohannes Weiner node_page_state(pgdat, NR_INACTIVE_FILE); 319953138ceaSJohannes Weiner 320053138ceaSJohannes Weiner for (z = 0; z < MAX_NR_ZONES; z++) { 320153138ceaSJohannes Weiner struct zone *zone = &pgdat->node_zones[z]; 320253138ceaSJohannes Weiner if (!managed_zone(zone)) 320353138ceaSJohannes Weiner continue; 320453138ceaSJohannes Weiner 320553138ceaSJohannes Weiner total_high_wmark += high_wmark_pages(zone); 320653138ceaSJohannes Weiner } 320753138ceaSJohannes Weiner 3208b91ac374SJohannes Weiner /* 3209b91ac374SJohannes Weiner * Consider anon: if that's low too, this isn't a 3210b91ac374SJohannes Weiner * runaway file reclaim problem, but rather just 3211b91ac374SJohannes Weiner * extreme pressure. Reclaim as per usual then. 3212b91ac374SJohannes Weiner */ 3213b91ac374SJohannes Weiner anon = node_page_state(pgdat, NR_INACTIVE_ANON); 3214b91ac374SJohannes Weiner 3215b91ac374SJohannes Weiner sc->file_is_tiny = 3216b91ac374SJohannes Weiner file + free <= total_high_wmark && 3217b91ac374SJohannes Weiner !(sc->may_deactivate & DEACTIVATE_ANON) && 3218b91ac374SJohannes Weiner anon >> sc->priority; 321953138ceaSJohannes Weiner } 322053138ceaSJohannes Weiner 32210f6a5cffSJohannes Weiner shrink_node_memcgs(pgdat, sc); 322270ddf637SAnton Vorontsov 32236b4f7799SJohannes Weiner if (reclaim_state) { 3224cb731d6cSVladimir Davydov sc->nr_reclaimed += reclaim_state->reclaimed_slab; 32256b4f7799SJohannes Weiner reclaim_state->reclaimed_slab = 0; 32266b4f7799SJohannes Weiner } 32276b4f7799SJohannes Weiner 32288e8ae645SJohannes Weiner /* Record the subtree's reclaim efficiency */ 32291b05117dSJohannes Weiner vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, 323070ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 323170ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 323270ddf637SAnton Vorontsov 32332344d7e4SJohannes Weiner if (sc->nr_reclaimed - nr_reclaimed) 32342344d7e4SJohannes Weiner reclaimable = true; 32352344d7e4SJohannes Weiner 3236e3c1ac58SAndrey Ryabinin if (current_is_kswapd()) { 3237d108c772SAndrey Ryabinin /* 3238e3c1ac58SAndrey Ryabinin * If reclaim is isolating dirty pages under writeback, 3239e3c1ac58SAndrey Ryabinin * it implies that the long-lived page allocation rate 3240e3c1ac58SAndrey Ryabinin * is exceeding the page laundering rate. Either the 3241e3c1ac58SAndrey Ryabinin * global limits are not being effective at throttling 3242e3c1ac58SAndrey Ryabinin * processes due to the page distribution throughout 3243e3c1ac58SAndrey Ryabinin * zones or there is heavy usage of a slow backing 3244e3c1ac58SAndrey Ryabinin * device. The only option is to throttle from reclaim 3245e3c1ac58SAndrey Ryabinin * context which is not ideal as there is no guarantee 3246d108c772SAndrey Ryabinin * the dirtying process is throttled in the same way 3247d108c772SAndrey Ryabinin * balance_dirty_pages() manages. 3248d108c772SAndrey Ryabinin * 3249e3c1ac58SAndrey Ryabinin * Once a node is flagged PGDAT_WRITEBACK, kswapd will 3250e3c1ac58SAndrey Ryabinin * count the number of pages under pages flagged for 3251e3c1ac58SAndrey Ryabinin * immediate reclaim and stall if any are encountered 3252e3c1ac58SAndrey Ryabinin * in the nr_immediate check below. 3253d108c772SAndrey Ryabinin */ 3254d108c772SAndrey Ryabinin if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken) 3255d108c772SAndrey Ryabinin set_bit(PGDAT_WRITEBACK, &pgdat->flags); 3256d108c772SAndrey Ryabinin 3257d108c772SAndrey Ryabinin /* Allow kswapd to start writing pages during reclaim.*/ 3258d108c772SAndrey Ryabinin if (sc->nr.unqueued_dirty == sc->nr.file_taken) 3259d108c772SAndrey Ryabinin set_bit(PGDAT_DIRTY, &pgdat->flags); 3260d108c772SAndrey Ryabinin 3261d108c772SAndrey Ryabinin /* 32621eba09c1SRandy Dunlap * If kswapd scans pages marked for immediate 3263d108c772SAndrey Ryabinin * reclaim and under writeback (nr_immediate), it 3264d108c772SAndrey Ryabinin * implies that pages are cycling through the LRU 32658cd7c588SMel Gorman * faster than they are written so forcibly stall 32668cd7c588SMel Gorman * until some pages complete writeback. 3267d108c772SAndrey Ryabinin */ 3268d108c772SAndrey Ryabinin if (sc->nr.immediate) 3269c3f4a9a2SMel Gorman reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK); 3270d108c772SAndrey Ryabinin } 3271d108c772SAndrey Ryabinin 3272d108c772SAndrey Ryabinin /* 32738cd7c588SMel Gorman * Tag a node/memcg as congested if all the dirty pages were marked 32748cd7c588SMel Gorman * for writeback and immediate reclaim (counted in nr.congested). 32751b05117dSJohannes Weiner * 3276e3c1ac58SAndrey Ryabinin * Legacy memcg will stall in page writeback so avoid forcibly 32778cd7c588SMel Gorman * stalling in reclaim_throttle(). 3278e3c1ac58SAndrey Ryabinin */ 32791b05117dSJohannes Weiner if ((current_is_kswapd() || 32801b05117dSJohannes Weiner (cgroup_reclaim(sc) && writeback_throttling_sane(sc))) && 3281e3c1ac58SAndrey Ryabinin sc->nr.dirty && sc->nr.dirty == sc->nr.congested) 32821b05117dSJohannes Weiner set_bit(LRUVEC_CONGESTED, &target_lruvec->flags); 3283e3c1ac58SAndrey Ryabinin 3284e3c1ac58SAndrey Ryabinin /* 32858cd7c588SMel Gorman * Stall direct reclaim for IO completions if the lruvec is 32868cd7c588SMel Gorman * node is congested. Allow kswapd to continue until it 3287d108c772SAndrey Ryabinin * starts encountering unqueued dirty pages or cycling through 3288d108c772SAndrey Ryabinin * the LRU too quickly. 3289d108c772SAndrey Ryabinin */ 32901b05117dSJohannes Weiner if (!current_is_kswapd() && current_may_throttle() && 32911b05117dSJohannes Weiner !sc->hibernation_mode && 32921b05117dSJohannes Weiner test_bit(LRUVEC_CONGESTED, &target_lruvec->flags)) 32931b4e3f26SMel Gorman reclaim_throttle(pgdat, VMSCAN_THROTTLE_CONGESTED); 3294d108c772SAndrey Ryabinin 3295d2af3397SJohannes Weiner if (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, 3296d2af3397SJohannes Weiner sc)) 3297d2af3397SJohannes Weiner goto again; 32982344d7e4SJohannes Weiner 3299c73322d0SJohannes Weiner /* 3300c73322d0SJohannes Weiner * Kswapd gives up on balancing particular nodes after too 3301c73322d0SJohannes Weiner * many failures to reclaim anything from them and goes to 3302c73322d0SJohannes Weiner * sleep. On reclaim progress, reset the failure counter. A 3303c73322d0SJohannes Weiner * successful direct reclaim run will revive a dormant kswapd. 3304c73322d0SJohannes Weiner */ 3305c73322d0SJohannes Weiner if (reclaimable) 3306c73322d0SJohannes Weiner pgdat->kswapd_failures = 0; 3307f16015fbSJohannes Weiner } 3308f16015fbSJohannes Weiner 330953853e2dSVlastimil Babka /* 3310fdd4c614SVlastimil Babka * Returns true if compaction should go ahead for a costly-order request, or 3311fdd4c614SVlastimil Babka * the allocation would already succeed without compaction. Return false if we 3312fdd4c614SVlastimil Babka * should reclaim first. 331353853e2dSVlastimil Babka */ 33144f588331SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 3315fe4b1b24SMel Gorman { 331631483b6aSMel Gorman unsigned long watermark; 3317fdd4c614SVlastimil Babka enum compact_result suitable; 3318fe4b1b24SMel Gorman 3319fdd4c614SVlastimil Babka suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx); 3320fdd4c614SVlastimil Babka if (suitable == COMPACT_SUCCESS) 3321fdd4c614SVlastimil Babka /* Allocation should succeed already. Don't reclaim. */ 3322fdd4c614SVlastimil Babka return true; 3323fdd4c614SVlastimil Babka if (suitable == COMPACT_SKIPPED) 3324fdd4c614SVlastimil Babka /* Compaction cannot yet proceed. Do reclaim. */ 3325fe4b1b24SMel Gorman return false; 3326fe4b1b24SMel Gorman 3327fdd4c614SVlastimil Babka /* 3328fdd4c614SVlastimil Babka * Compaction is already possible, but it takes time to run and there 3329fdd4c614SVlastimil Babka * are potentially other callers using the pages just freed. So proceed 3330fdd4c614SVlastimil Babka * with reclaim to make a buffer of free pages available to give 3331fdd4c614SVlastimil Babka * compaction a reasonable chance of completing and allocating the page. 3332fdd4c614SVlastimil Babka * Note that we won't actually reclaim the whole buffer in one attempt 3333fdd4c614SVlastimil Babka * as the target watermark in should_continue_reclaim() is lower. But if 3334fdd4c614SVlastimil Babka * we are already above the high+gap watermark, don't reclaim at all. 3335fdd4c614SVlastimil Babka */ 3336fdd4c614SVlastimil Babka watermark = high_wmark_pages(zone) + compact_gap(sc->order); 3337fdd4c614SVlastimil Babka 3338fdd4c614SVlastimil Babka return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); 3339fe4b1b24SMel Gorman } 3340fe4b1b24SMel Gorman 334169392a40SMel Gorman static void consider_reclaim_throttle(pg_data_t *pgdat, struct scan_control *sc) 334269392a40SMel Gorman { 334366ce520bSMel Gorman /* 334466ce520bSMel Gorman * If reclaim is making progress greater than 12% efficiency then 334566ce520bSMel Gorman * wake all the NOPROGRESS throttled tasks. 334666ce520bSMel Gorman */ 334766ce520bSMel Gorman if (sc->nr_reclaimed > (sc->nr_scanned >> 3)) { 334869392a40SMel Gorman wait_queue_head_t *wqh; 334969392a40SMel Gorman 335069392a40SMel Gorman wqh = &pgdat->reclaim_wait[VMSCAN_THROTTLE_NOPROGRESS]; 335169392a40SMel Gorman if (waitqueue_active(wqh)) 335269392a40SMel Gorman wake_up(wqh); 335369392a40SMel Gorman 335469392a40SMel Gorman return; 335569392a40SMel Gorman } 335669392a40SMel Gorman 335769392a40SMel Gorman /* 33581b4e3f26SMel Gorman * Do not throttle kswapd or cgroup reclaim on NOPROGRESS as it will 33591b4e3f26SMel Gorman * throttle on VMSCAN_THROTTLE_WRITEBACK if there are too many pages 33601b4e3f26SMel Gorman * under writeback and marked for immediate reclaim at the tail of the 33611b4e3f26SMel Gorman * LRU. 336269392a40SMel Gorman */ 33631b4e3f26SMel Gorman if (current_is_kswapd() || cgroup_reclaim(sc)) 336469392a40SMel Gorman return; 336569392a40SMel Gorman 336669392a40SMel Gorman /* Throttle if making no progress at high prioities. */ 33671b4e3f26SMel Gorman if (sc->priority == 1 && !sc->nr_reclaimed) 3368c3f4a9a2SMel Gorman reclaim_throttle(pgdat, VMSCAN_THROTTLE_NOPROGRESS); 336969392a40SMel Gorman } 337069392a40SMel Gorman 33711da177e4SLinus Torvalds /* 33721da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 33731da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 33741da177e4SLinus Torvalds * request. 33751da177e4SLinus Torvalds * 33761da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 33771da177e4SLinus Torvalds * scan then give up on it. 33781da177e4SLinus Torvalds */ 33790a0337e0SMichal Hocko static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 33801da177e4SLinus Torvalds { 3381dd1a239fSMel Gorman struct zoneref *z; 338254a6eb5cSMel Gorman struct zone *zone; 33830608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 33840608f43dSAndrew Morton unsigned long nr_soft_scanned; 3385619d0d76SWeijie Yang gfp_t orig_mask; 338679dafcdcSMel Gorman pg_data_t *last_pgdat = NULL; 33871b4e3f26SMel Gorman pg_data_t *first_pgdat = NULL; 33881cfb419bSKAMEZAWA Hiroyuki 3389cc715d99SMel Gorman /* 3390cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 3391cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 3392cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 3393cc715d99SMel Gorman */ 3394619d0d76SWeijie Yang orig_mask = sc->gfp_mask; 3395b2e18757SMel Gorman if (buffer_heads_over_limit) { 3396cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 33974f588331SMel Gorman sc->reclaim_idx = gfp_zone(sc->gfp_mask); 3398b2e18757SMel Gorman } 3399cc715d99SMel Gorman 3400d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 3401b2e18757SMel Gorman sc->reclaim_idx, sc->nodemask) { 3402b2e18757SMel Gorman /* 34031cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 34041cfb419bSKAMEZAWA Hiroyuki * to global LRU. 34051cfb419bSKAMEZAWA Hiroyuki */ 3406b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) { 3407344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, 3408344736f2SVladimir Davydov GFP_KERNEL | __GFP_HARDWALL)) 34091da177e4SLinus Torvalds continue; 341065ec02cbSVladimir Davydov 3411e0887c19SRik van Riel /* 3412e0c23279SMel Gorman * If we already have plenty of memory free for 3413e0c23279SMel Gorman * compaction in this zone, don't free any more. 3414e0c23279SMel Gorman * Even though compaction is invoked for any 3415e0c23279SMel Gorman * non-zero order, only frequent costly order 3416e0c23279SMel Gorman * reclamation is disruptive enough to become a 3417c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 3418c7cfa37bSCopot Alexandru * page allocations. 3419e0887c19SRik van Riel */ 34200b06496aSJohannes Weiner if (IS_ENABLED(CONFIG_COMPACTION) && 34210b06496aSJohannes Weiner sc->order > PAGE_ALLOC_COSTLY_ORDER && 34224f588331SMel Gorman compaction_ready(zone, sc)) { 34230b06496aSJohannes Weiner sc->compaction_ready = true; 3424e0887c19SRik van Riel continue; 3425e0887c19SRik van Riel } 34260b06496aSJohannes Weiner 34270608f43dSAndrew Morton /* 342879dafcdcSMel Gorman * Shrink each node in the zonelist once. If the 342979dafcdcSMel Gorman * zonelist is ordered by zone (not the default) then a 343079dafcdcSMel Gorman * node may be shrunk multiple times but in that case 343179dafcdcSMel Gorman * the user prefers lower zones being preserved. 343279dafcdcSMel Gorman */ 343379dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 343479dafcdcSMel Gorman continue; 343579dafcdcSMel Gorman 343679dafcdcSMel Gorman /* 34370608f43dSAndrew Morton * This steals pages from memory cgroups over softlimit 34380608f43dSAndrew Morton * and returns the number of reclaimed pages and 34390608f43dSAndrew Morton * scanned pages. This works for global memory pressure 34400608f43dSAndrew Morton * and balancing, not for a memcg's limit. 34410608f43dSAndrew Morton */ 34420608f43dSAndrew Morton nr_soft_scanned = 0; 3443ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, 34440608f43dSAndrew Morton sc->order, sc->gfp_mask, 34450608f43dSAndrew Morton &nr_soft_scanned); 34460608f43dSAndrew Morton sc->nr_reclaimed += nr_soft_reclaimed; 34470608f43dSAndrew Morton sc->nr_scanned += nr_soft_scanned; 3448ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 3449ac34a1a3SKAMEZAWA Hiroyuki } 3450d149e3b2SYing Han 34511b4e3f26SMel Gorman if (!first_pgdat) 34521b4e3f26SMel Gorman first_pgdat = zone->zone_pgdat; 34531b4e3f26SMel Gorman 345479dafcdcSMel Gorman /* See comment about same check for global reclaim above */ 345579dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 345679dafcdcSMel Gorman continue; 345779dafcdcSMel Gorman last_pgdat = zone->zone_pgdat; 3458970a39a3SMel Gorman shrink_node(zone->zone_pgdat, sc); 34591da177e4SLinus Torvalds } 3460e0c23279SMel Gorman 346180082938SMel Gorman if (first_pgdat) 34621b4e3f26SMel Gorman consider_reclaim_throttle(first_pgdat, sc); 34631b4e3f26SMel Gorman 346465ec02cbSVladimir Davydov /* 3465619d0d76SWeijie Yang * Restore to original mask to avoid the impact on the caller if we 3466619d0d76SWeijie Yang * promoted it to __GFP_HIGHMEM. 3467619d0d76SWeijie Yang */ 3468619d0d76SWeijie Yang sc->gfp_mask = orig_mask; 34691da177e4SLinus Torvalds } 34701da177e4SLinus Torvalds 3471b910718aSJohannes Weiner static void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat) 34722a2e4885SJohannes Weiner { 3473b910718aSJohannes Weiner struct lruvec *target_lruvec; 34742a2e4885SJohannes Weiner unsigned long refaults; 34752a2e4885SJohannes Weiner 3476b910718aSJohannes Weiner target_lruvec = mem_cgroup_lruvec(target_memcg, pgdat); 3477170b04b7SJoonsoo Kim refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_ANON); 3478170b04b7SJoonsoo Kim target_lruvec->refaults[0] = refaults; 3479170b04b7SJoonsoo Kim refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_FILE); 3480170b04b7SJoonsoo Kim target_lruvec->refaults[1] = refaults; 34812a2e4885SJohannes Weiner } 34822a2e4885SJohannes Weiner 34831da177e4SLinus Torvalds /* 34841da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 34851da177e4SLinus Torvalds * 34861da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 34871da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 34881da177e4SLinus Torvalds * 34891da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 34901da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 34915b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 34925b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 34935b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 34945b0830cbSJens Axboe * work, and the allocation attempt will fail. 3495a41f24eaSNishanth Aravamudan * 3496a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 3497a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 34981da177e4SLinus Torvalds */ 3499dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 35003115cd91SVladimir Davydov struct scan_control *sc) 35011da177e4SLinus Torvalds { 3502241994edSJohannes Weiner int initial_priority = sc->priority; 35032a2e4885SJohannes Weiner pg_data_t *last_pgdat; 35042a2e4885SJohannes Weiner struct zoneref *z; 35052a2e4885SJohannes Weiner struct zone *zone; 3506241994edSJohannes Weiner retry: 3507873b4771SKeika Kobayashi delayacct_freepages_start(); 3508873b4771SKeika Kobayashi 3509b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 35107cc30fcfSMel Gorman __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); 35111da177e4SLinus Torvalds 35129e3b2f8cSKonstantin Khlebnikov do { 351370ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 351470ddf637SAnton Vorontsov sc->priority); 351566e1707bSBalbir Singh sc->nr_scanned = 0; 35160a0337e0SMichal Hocko shrink_zones(zonelist, sc); 3517e0c23279SMel Gorman 3518bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 35190b06496aSJohannes Weiner break; 35200b06496aSJohannes Weiner 35210b06496aSJohannes Weiner if (sc->compaction_ready) 35220b06496aSJohannes Weiner break; 35231da177e4SLinus Torvalds 35241da177e4SLinus Torvalds /* 35250e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 35260e50ce3bSMinchan Kim * writepage even in laptop mode. 35270e50ce3bSMinchan Kim */ 35280e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 35290e50ce3bSMinchan Kim sc->may_writepage = 1; 35300b06496aSJohannes Weiner } while (--sc->priority >= 0); 3531bb21c7ceSKOSAKI Motohiro 35322a2e4885SJohannes Weiner last_pgdat = NULL; 35332a2e4885SJohannes Weiner for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx, 35342a2e4885SJohannes Weiner sc->nodemask) { 35352a2e4885SJohannes Weiner if (zone->zone_pgdat == last_pgdat) 35362a2e4885SJohannes Weiner continue; 35372a2e4885SJohannes Weiner last_pgdat = zone->zone_pgdat; 35381b05117dSJohannes Weiner 35392a2e4885SJohannes Weiner snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat); 35401b05117dSJohannes Weiner 35411b05117dSJohannes Weiner if (cgroup_reclaim(sc)) { 35421b05117dSJohannes Weiner struct lruvec *lruvec; 35431b05117dSJohannes Weiner 35441b05117dSJohannes Weiner lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, 35451b05117dSJohannes Weiner zone->zone_pgdat); 35461b05117dSJohannes Weiner clear_bit(LRUVEC_CONGESTED, &lruvec->flags); 35471b05117dSJohannes Weiner } 35482a2e4885SJohannes Weiner } 35492a2e4885SJohannes Weiner 3550873b4771SKeika Kobayashi delayacct_freepages_end(); 3551873b4771SKeika Kobayashi 3552bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 3553bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 3554bb21c7ceSKOSAKI Motohiro 35550cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 35560b06496aSJohannes Weiner if (sc->compaction_ready) 35577335084dSMel Gorman return 1; 35587335084dSMel Gorman 3559b91ac374SJohannes Weiner /* 3560b91ac374SJohannes Weiner * We make inactive:active ratio decisions based on the node's 3561b91ac374SJohannes Weiner * composition of memory, but a restrictive reclaim_idx or a 3562b91ac374SJohannes Weiner * memory.low cgroup setting can exempt large amounts of 3563b91ac374SJohannes Weiner * memory from reclaim. Neither of which are very common, so 3564b91ac374SJohannes Weiner * instead of doing costly eligibility calculations of the 3565b91ac374SJohannes Weiner * entire cgroup subtree up front, we assume the estimates are 3566b91ac374SJohannes Weiner * good, and retry with forcible deactivation if that fails. 3567b91ac374SJohannes Weiner */ 3568b91ac374SJohannes Weiner if (sc->skipped_deactivate) { 3569b91ac374SJohannes Weiner sc->priority = initial_priority; 3570b91ac374SJohannes Weiner sc->force_deactivate = 1; 3571b91ac374SJohannes Weiner sc->skipped_deactivate = 0; 3572b91ac374SJohannes Weiner goto retry; 3573b91ac374SJohannes Weiner } 3574b91ac374SJohannes Weiner 3575241994edSJohannes Weiner /* Untapped cgroup reserves? Don't OOM, retry. */ 3576d6622f63SYisheng Xie if (sc->memcg_low_skipped) { 3577241994edSJohannes Weiner sc->priority = initial_priority; 3578b91ac374SJohannes Weiner sc->force_deactivate = 0; 3579d6622f63SYisheng Xie sc->memcg_low_reclaim = 1; 3580d6622f63SYisheng Xie sc->memcg_low_skipped = 0; 3581241994edSJohannes Weiner goto retry; 3582241994edSJohannes Weiner } 3583241994edSJohannes Weiner 3584bb21c7ceSKOSAKI Motohiro return 0; 35851da177e4SLinus Torvalds } 35861da177e4SLinus Torvalds 3587c73322d0SJohannes Weiner static bool allow_direct_reclaim(pg_data_t *pgdat) 35885515061dSMel Gorman { 35895515061dSMel Gorman struct zone *zone; 35905515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 35915515061dSMel Gorman unsigned long free_pages = 0; 35925515061dSMel Gorman int i; 35935515061dSMel Gorman bool wmark_ok; 35945515061dSMel Gorman 3595c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3596c73322d0SJohannes Weiner return true; 3597c73322d0SJohannes Weiner 35985515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 35995515061dSMel Gorman zone = &pgdat->node_zones[i]; 3600d450abd8SJohannes Weiner if (!managed_zone(zone)) 3601d450abd8SJohannes Weiner continue; 3602d450abd8SJohannes Weiner 3603d450abd8SJohannes Weiner if (!zone_reclaimable_pages(zone)) 3604675becceSMel Gorman continue; 3605675becceSMel Gorman 36065515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 36075515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 36085515061dSMel Gorman } 36095515061dSMel Gorman 3610675becceSMel Gorman /* If there are no reserves (unexpected config) then do not throttle */ 3611675becceSMel Gorman if (!pfmemalloc_reserve) 3612675becceSMel Gorman return true; 3613675becceSMel Gorman 36145515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 36155515061dSMel Gorman 36165515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 36175515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 361897a225e6SJoonsoo Kim if (READ_ONCE(pgdat->kswapd_highest_zoneidx) > ZONE_NORMAL) 361997a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, ZONE_NORMAL); 36205644e1fbSQian Cai 36215515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 36225515061dSMel Gorman } 36235515061dSMel Gorman 36245515061dSMel Gorman return wmark_ok; 36255515061dSMel Gorman } 36265515061dSMel Gorman 36275515061dSMel Gorman /* 36285515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 36295515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 36305515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 363150694c28SMel Gorman * when the low watermark is reached. 363250694c28SMel Gorman * 363350694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 363450694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 36355515061dSMel Gorman */ 363650694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 36375515061dSMel Gorman nodemask_t *nodemask) 36385515061dSMel Gorman { 3639675becceSMel Gorman struct zoneref *z; 36405515061dSMel Gorman struct zone *zone; 3641675becceSMel Gorman pg_data_t *pgdat = NULL; 36425515061dSMel Gorman 36435515061dSMel Gorman /* 36445515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 36455515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 36465515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 36475515061dSMel Gorman * committing a transaction where throttling it could forcing other 36485515061dSMel Gorman * processes to block on log_wait_commit(). 36495515061dSMel Gorman */ 36505515061dSMel Gorman if (current->flags & PF_KTHREAD) 365150694c28SMel Gorman goto out; 365250694c28SMel Gorman 365350694c28SMel Gorman /* 365450694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 365550694c28SMel Gorman * It should return quickly so it can exit and free its memory 365650694c28SMel Gorman */ 365750694c28SMel Gorman if (fatal_signal_pending(current)) 365850694c28SMel Gorman goto out; 36595515061dSMel Gorman 3660675becceSMel Gorman /* 3661675becceSMel Gorman * Check if the pfmemalloc reserves are ok by finding the first node 3662675becceSMel Gorman * with a usable ZONE_NORMAL or lower zone. The expectation is that 3663675becceSMel Gorman * GFP_KERNEL will be required for allocating network buffers when 3664675becceSMel Gorman * swapping over the network so ZONE_HIGHMEM is unusable. 3665675becceSMel Gorman * 3666675becceSMel Gorman * Throttling is based on the first usable node and throttled processes 3667675becceSMel Gorman * wait on a queue until kswapd makes progress and wakes them. There 3668675becceSMel Gorman * is an affinity then between processes waking up and where reclaim 3669675becceSMel Gorman * progress has been made assuming the process wakes on the same node. 3670675becceSMel Gorman * More importantly, processes running on remote nodes will not compete 3671675becceSMel Gorman * for remote pfmemalloc reserves and processes on different nodes 3672675becceSMel Gorman * should make reasonable progress. 3673675becceSMel Gorman */ 3674675becceSMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 367517636faaSMichael S. Tsirkin gfp_zone(gfp_mask), nodemask) { 3676675becceSMel Gorman if (zone_idx(zone) > ZONE_NORMAL) 3677675becceSMel Gorman continue; 3678675becceSMel Gorman 3679675becceSMel Gorman /* Throttle based on the first usable node */ 36805515061dSMel Gorman pgdat = zone->zone_pgdat; 3681c73322d0SJohannes Weiner if (allow_direct_reclaim(pgdat)) 368250694c28SMel Gorman goto out; 3683675becceSMel Gorman break; 3684675becceSMel Gorman } 3685675becceSMel Gorman 3686675becceSMel Gorman /* If no zone was usable by the allocation flags then do not throttle */ 3687675becceSMel Gorman if (!pgdat) 3688675becceSMel Gorman goto out; 36895515061dSMel Gorman 369068243e76SMel Gorman /* Account for the throttling */ 369168243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 369268243e76SMel Gorman 36935515061dSMel Gorman /* 36945515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 36955515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 36965515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 36975515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 36985515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 36995515061dSMel Gorman * second before continuing. 37005515061dSMel Gorman */ 37012e786d9eSMiaohe Lin if (!(gfp_mask & __GFP_FS)) 37025515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 3703c73322d0SJohannes Weiner allow_direct_reclaim(pgdat), HZ); 37042e786d9eSMiaohe Lin else 37055515061dSMel Gorman /* Throttle until kswapd wakes the process */ 37065515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 3707c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)); 370850694c28SMel Gorman 370950694c28SMel Gorman if (fatal_signal_pending(current)) 371050694c28SMel Gorman return true; 371150694c28SMel Gorman 371250694c28SMel Gorman out: 371350694c28SMel Gorman return false; 37145515061dSMel Gorman } 37155515061dSMel Gorman 3716dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 3717327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 371866e1707bSBalbir Singh { 371933906bc5SMel Gorman unsigned long nr_reclaimed; 372066e1707bSBalbir Singh struct scan_control sc = { 372122fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3722f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 3723b2e18757SMel Gorman .reclaim_idx = gfp_zone(gfp_mask), 3724ee814fe2SJohannes Weiner .order = order, 3725ee814fe2SJohannes Weiner .nodemask = nodemask, 3726ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 3727ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3728a6dc60f8SJohannes Weiner .may_unmap = 1, 37292e2e4259SKOSAKI Motohiro .may_swap = 1, 373066e1707bSBalbir Singh }; 373166e1707bSBalbir Singh 37325515061dSMel Gorman /* 3733bb451fdfSGreg Thelen * scan_control uses s8 fields for order, priority, and reclaim_idx. 3734bb451fdfSGreg Thelen * Confirm they are large enough for max values. 3735bb451fdfSGreg Thelen */ 3736bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_ORDER > S8_MAX); 3737bb451fdfSGreg Thelen BUILD_BUG_ON(DEF_PRIORITY > S8_MAX); 3738bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX); 3739bb451fdfSGreg Thelen 3740bb451fdfSGreg Thelen /* 374150694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 374250694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 374350694c28SMel Gorman * point. 37445515061dSMel Gorman */ 3745f2f43e56SNick Desaulniers if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask)) 37465515061dSMel Gorman return 1; 37475515061dSMel Gorman 37481732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 37493481c37fSYafang Shao trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask); 375033906bc5SMel Gorman 37513115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 375233906bc5SMel Gorman 375333906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 37541732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 375533906bc5SMel Gorman 375633906bc5SMel Gorman return nr_reclaimed; 375766e1707bSBalbir Singh } 375866e1707bSBalbir Singh 3759c255a458SAndrew Morton #ifdef CONFIG_MEMCG 376066e1707bSBalbir Singh 3761d2e5fb92SMichal Hocko /* Only used by soft limit reclaim. Do not reuse for anything else. */ 3762a9dd0a83SMel Gorman unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, 37634e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 3764ef8f2327SMel Gorman pg_data_t *pgdat, 37650ae5e89cSYing Han unsigned long *nr_scanned) 37664e416953SBalbir Singh { 3767afaf07a6SJohannes Weiner struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); 37684e416953SBalbir Singh struct scan_control sc = { 3769b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3770ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 37714e416953SBalbir Singh .may_writepage = !laptop_mode, 37724e416953SBalbir Singh .may_unmap = 1, 3773b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 37744e416953SBalbir Singh .may_swap = !noswap, 37754e416953SBalbir Singh }; 37760ae5e89cSYing Han 3777d2e5fb92SMichal Hocko WARN_ON_ONCE(!current->reclaim_state); 3778d2e5fb92SMichal Hocko 37794e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 37804e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 3781bdce6d9eSKOSAKI Motohiro 37829e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 37833481c37fSYafang Shao sc.gfp_mask); 3784bdce6d9eSKOSAKI Motohiro 37854e416953SBalbir Singh /* 37864e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 37874e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 3788a9dd0a83SMel Gorman * if we don't reclaim here, the shrink_node from balance_pgdat 37894e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 37904e416953SBalbir Singh * the priority and make it zero. 37914e416953SBalbir Singh */ 3792afaf07a6SJohannes Weiner shrink_lruvec(lruvec, &sc); 3793bdce6d9eSKOSAKI Motohiro 3794bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 3795bdce6d9eSKOSAKI Motohiro 37960ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 37970308f7cfSYafang Shao 37984e416953SBalbir Singh return sc.nr_reclaimed; 37994e416953SBalbir Singh } 38004e416953SBalbir Singh 380172835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 3802b70a2a21SJohannes Weiner unsigned long nr_pages, 38038c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 3804b70a2a21SJohannes Weiner bool may_swap) 380566e1707bSBalbir Singh { 3806bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 3807499118e9SVlastimil Babka unsigned int noreclaim_flag; 380866e1707bSBalbir Singh struct scan_control sc = { 3809b70a2a21SJohannes Weiner .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 38107dea19f9SMichal Hocko .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | 3811ee814fe2SJohannes Weiner (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 3812b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 3813ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 3814ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 381566e1707bSBalbir Singh .may_writepage = !laptop_mode, 3816a6dc60f8SJohannes Weiner .may_unmap = 1, 3817b70a2a21SJohannes Weiner .may_swap = may_swap, 3818a09ed5e0SYing Han }; 3819fa40d1eeSShakeel Butt /* 3820fa40d1eeSShakeel Butt * Traverse the ZONELIST_FALLBACK zonelist of the current node to put 3821fa40d1eeSShakeel Butt * equal pressure on all the nodes. This is based on the assumption that 3822fa40d1eeSShakeel Butt * the reclaim does not bail out early. 3823fa40d1eeSShakeel Butt */ 3824fa40d1eeSShakeel Butt struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 382566e1707bSBalbir Singh 38261732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 38273481c37fSYafang Shao trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask); 3828499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 3829eb414681SJohannes Weiner 38303115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3831eb414681SJohannes Weiner 3832499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 3833bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 38341732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 3835bdce6d9eSKOSAKI Motohiro 3836bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 383766e1707bSBalbir Singh } 383866e1707bSBalbir Singh #endif 383966e1707bSBalbir Singh 38401d82de61SMel Gorman static void age_active_anon(struct pglist_data *pgdat, 3841ef8f2327SMel Gorman struct scan_control *sc) 3842f16015fbSJohannes Weiner { 3843b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 3844b91ac374SJohannes Weiner struct lruvec *lruvec; 3845b95a2f2dSJohannes Weiner 38462f368a9fSDave Hansen if (!can_age_anon_pages(pgdat, sc)) 3847b95a2f2dSJohannes Weiner return; 3848b95a2f2dSJohannes Weiner 3849b91ac374SJohannes Weiner lruvec = mem_cgroup_lruvec(NULL, pgdat); 3850b91ac374SJohannes Weiner if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON)) 3851b91ac374SJohannes Weiner return; 3852b91ac374SJohannes Weiner 3853b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 3854b95a2f2dSJohannes Weiner do { 3855b91ac374SJohannes Weiner lruvec = mem_cgroup_lruvec(memcg, pgdat); 38561a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 38579e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 3858b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 3859b95a2f2dSJohannes Weiner } while (memcg); 3860f16015fbSJohannes Weiner } 3861f16015fbSJohannes Weiner 386297a225e6SJoonsoo Kim static bool pgdat_watermark_boosted(pg_data_t *pgdat, int highest_zoneidx) 38631c30844dSMel Gorman { 38641c30844dSMel Gorman int i; 38651c30844dSMel Gorman struct zone *zone; 38661c30844dSMel Gorman 38671c30844dSMel Gorman /* 38681c30844dSMel Gorman * Check for watermark boosts top-down as the higher zones 38691c30844dSMel Gorman * are more likely to be boosted. Both watermarks and boosts 38701eba09c1SRandy Dunlap * should not be checked at the same time as reclaim would 38711c30844dSMel Gorman * start prematurely when there is no boosting and a lower 38721c30844dSMel Gorman * zone is balanced. 38731c30844dSMel Gorman */ 387497a225e6SJoonsoo Kim for (i = highest_zoneidx; i >= 0; i--) { 38751c30844dSMel Gorman zone = pgdat->node_zones + i; 38761c30844dSMel Gorman if (!managed_zone(zone)) 38771c30844dSMel Gorman continue; 38781c30844dSMel Gorman 38791c30844dSMel Gorman if (zone->watermark_boost) 38801c30844dSMel Gorman return true; 38811c30844dSMel Gorman } 38821c30844dSMel Gorman 38831c30844dSMel Gorman return false; 38841c30844dSMel Gorman } 38851c30844dSMel Gorman 3886e716f2ebSMel Gorman /* 3887e716f2ebSMel Gorman * Returns true if there is an eligible zone balanced for the request order 388897a225e6SJoonsoo Kim * and highest_zoneidx 3889e716f2ebSMel Gorman */ 389097a225e6SJoonsoo Kim static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx) 389160cefed4SJohannes Weiner { 3892e716f2ebSMel Gorman int i; 3893e716f2ebSMel Gorman unsigned long mark = -1; 3894e716f2ebSMel Gorman struct zone *zone; 389560cefed4SJohannes Weiner 38961c30844dSMel Gorman /* 38971c30844dSMel Gorman * Check watermarks bottom-up as lower zones are more likely to 38981c30844dSMel Gorman * meet watermarks. 38991c30844dSMel Gorman */ 390097a225e6SJoonsoo Kim for (i = 0; i <= highest_zoneidx; i++) { 3901e716f2ebSMel Gorman zone = pgdat->node_zones + i; 39026256c6b4SMel Gorman 3903e716f2ebSMel Gorman if (!managed_zone(zone)) 3904e716f2ebSMel Gorman continue; 3905e716f2ebSMel Gorman 3906c574bbe9SHuang Ying if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) 3907c574bbe9SHuang Ying mark = wmark_pages(zone, WMARK_PROMO); 3908c574bbe9SHuang Ying else 3909e716f2ebSMel Gorman mark = high_wmark_pages(zone); 391097a225e6SJoonsoo Kim if (zone_watermark_ok_safe(zone, order, mark, highest_zoneidx)) 39116256c6b4SMel Gorman return true; 391260cefed4SJohannes Weiner } 391360cefed4SJohannes Weiner 3914e716f2ebSMel Gorman /* 391597a225e6SJoonsoo Kim * If a node has no populated zone within highest_zoneidx, it does not 3916e716f2ebSMel Gorman * need balancing by definition. This can happen if a zone-restricted 3917e716f2ebSMel Gorman * allocation tries to wake a remote kswapd. 3918e716f2ebSMel Gorman */ 3919e716f2ebSMel Gorman if (mark == -1) 3920e716f2ebSMel Gorman return true; 3921e716f2ebSMel Gorman 3922e716f2ebSMel Gorman return false; 3923e716f2ebSMel Gorman } 3924e716f2ebSMel Gorman 3925631b6e08SMel Gorman /* Clear pgdat state for congested, dirty or under writeback. */ 3926631b6e08SMel Gorman static void clear_pgdat_congested(pg_data_t *pgdat) 3927631b6e08SMel Gorman { 39281b05117dSJohannes Weiner struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat); 39291b05117dSJohannes Weiner 39301b05117dSJohannes Weiner clear_bit(LRUVEC_CONGESTED, &lruvec->flags); 3931631b6e08SMel Gorman clear_bit(PGDAT_DIRTY, &pgdat->flags); 3932631b6e08SMel Gorman clear_bit(PGDAT_WRITEBACK, &pgdat->flags); 3933631b6e08SMel Gorman } 3934631b6e08SMel Gorman 39351741c877SMel Gorman /* 39365515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 39375515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 39385515061dSMel Gorman * 39395515061dSMel Gorman * Returns true if kswapd is ready to sleep 39405515061dSMel Gorman */ 394197a225e6SJoonsoo Kim static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, 394297a225e6SJoonsoo Kim int highest_zoneidx) 3943f50de2d3SMel Gorman { 39445515061dSMel Gorman /* 39459e5e3661SVlastimil Babka * The throttled processes are normally woken up in balance_pgdat() as 3946c73322d0SJohannes Weiner * soon as allow_direct_reclaim() is true. But there is a potential 39479e5e3661SVlastimil Babka * race between when kswapd checks the watermarks and a process gets 39489e5e3661SVlastimil Babka * throttled. There is also a potential race if processes get 39499e5e3661SVlastimil Babka * throttled, kswapd wakes, a large process exits thereby balancing the 39509e5e3661SVlastimil Babka * zones, which causes kswapd to exit balance_pgdat() before reaching 39519e5e3661SVlastimil Babka * the wake up checks. If kswapd is going to sleep, no process should 39529e5e3661SVlastimil Babka * be sleeping on pfmemalloc_wait, so wake them now if necessary. If 39539e5e3661SVlastimil Babka * the wake up is premature, processes will wake kswapd and get 39549e5e3661SVlastimil Babka * throttled again. The difference from wake ups in balance_pgdat() is 39559e5e3661SVlastimil Babka * that here we are under prepare_to_wait(). 39565515061dSMel Gorman */ 39579e5e3661SVlastimil Babka if (waitqueue_active(&pgdat->pfmemalloc_wait)) 39589e5e3661SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 3959f50de2d3SMel Gorman 3960c73322d0SJohannes Weiner /* Hopeless node, leave it to direct reclaim */ 3961c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3962c73322d0SJohannes Weiner return true; 3963c73322d0SJohannes Weiner 396497a225e6SJoonsoo Kim if (pgdat_balanced(pgdat, order, highest_zoneidx)) { 3965631b6e08SMel Gorman clear_pgdat_congested(pgdat); 3966333b0a45SShantanu Goel return true; 39671d82de61SMel Gorman } 39681d82de61SMel Gorman 3969333b0a45SShantanu Goel return false; 3970f50de2d3SMel Gorman } 3971f50de2d3SMel Gorman 39721da177e4SLinus Torvalds /* 39731d82de61SMel Gorman * kswapd shrinks a node of pages that are at or below the highest usable 39741d82de61SMel Gorman * zone that is currently unbalanced. 3975b8e83b94SMel Gorman * 3976b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 3977283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 3978283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 397975485363SMel Gorman */ 39801d82de61SMel Gorman static bool kswapd_shrink_node(pg_data_t *pgdat, 3981accf6242SVlastimil Babka struct scan_control *sc) 398275485363SMel Gorman { 39831d82de61SMel Gorman struct zone *zone; 39841d82de61SMel Gorman int z; 398575485363SMel Gorman 39861d82de61SMel Gorman /* Reclaim a number of pages proportional to the number of zones */ 39871d82de61SMel Gorman sc->nr_to_reclaim = 0; 3988970a39a3SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 39891d82de61SMel Gorman zone = pgdat->node_zones + z; 39906aa303deSMel Gorman if (!managed_zone(zone)) 39911d82de61SMel Gorman continue; 39927c954f6dSMel Gorman 39931d82de61SMel Gorman sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); 39947c954f6dSMel Gorman } 39957c954f6dSMel Gorman 39961d82de61SMel Gorman /* 39971d82de61SMel Gorman * Historically care was taken to put equal pressure on all zones but 39981d82de61SMel Gorman * now pressure is applied based on node LRU order. 39991d82de61SMel Gorman */ 4000970a39a3SMel Gorman shrink_node(pgdat, sc); 40011d82de61SMel Gorman 40021d82de61SMel Gorman /* 40031d82de61SMel Gorman * Fragmentation may mean that the system cannot be rebalanced for 40041d82de61SMel Gorman * high-order allocations. If twice the allocation size has been 40051d82de61SMel Gorman * reclaimed then recheck watermarks only at order-0 to prevent 40061d82de61SMel Gorman * excessive reclaim. Assume that a process requested a high-order 40071d82de61SMel Gorman * can direct reclaim/compact. 40081d82de61SMel Gorman */ 40099861a62cSVlastimil Babka if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order)) 40101d82de61SMel Gorman sc->order = 0; 40111d82de61SMel Gorman 4012b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 401375485363SMel Gorman } 401475485363SMel Gorman 4015c49c2c47SMel Gorman /* Page allocator PCP high watermark is lowered if reclaim is active. */ 4016c49c2c47SMel Gorman static inline void 4017c49c2c47SMel Gorman update_reclaim_active(pg_data_t *pgdat, int highest_zoneidx, bool active) 4018c49c2c47SMel Gorman { 4019c49c2c47SMel Gorman int i; 4020c49c2c47SMel Gorman struct zone *zone; 4021c49c2c47SMel Gorman 4022c49c2c47SMel Gorman for (i = 0; i <= highest_zoneidx; i++) { 4023c49c2c47SMel Gorman zone = pgdat->node_zones + i; 4024c49c2c47SMel Gorman 4025c49c2c47SMel Gorman if (!managed_zone(zone)) 4026c49c2c47SMel Gorman continue; 4027c49c2c47SMel Gorman 4028c49c2c47SMel Gorman if (active) 4029c49c2c47SMel Gorman set_bit(ZONE_RECLAIM_ACTIVE, &zone->flags); 4030c49c2c47SMel Gorman else 4031c49c2c47SMel Gorman clear_bit(ZONE_RECLAIM_ACTIVE, &zone->flags); 4032c49c2c47SMel Gorman } 4033c49c2c47SMel Gorman } 4034c49c2c47SMel Gorman 4035c49c2c47SMel Gorman static inline void 4036c49c2c47SMel Gorman set_reclaim_active(pg_data_t *pgdat, int highest_zoneidx) 4037c49c2c47SMel Gorman { 4038c49c2c47SMel Gorman update_reclaim_active(pgdat, highest_zoneidx, true); 4039c49c2c47SMel Gorman } 4040c49c2c47SMel Gorman 4041c49c2c47SMel Gorman static inline void 4042c49c2c47SMel Gorman clear_reclaim_active(pg_data_t *pgdat, int highest_zoneidx) 4043c49c2c47SMel Gorman { 4044c49c2c47SMel Gorman update_reclaim_active(pgdat, highest_zoneidx, false); 4045c49c2c47SMel Gorman } 4046c49c2c47SMel Gorman 404775485363SMel Gorman /* 40481d82de61SMel Gorman * For kswapd, balance_pgdat() will reclaim pages across a node from zones 40491d82de61SMel Gorman * that are eligible for use by the caller until at least one zone is 40501d82de61SMel Gorman * balanced. 40511da177e4SLinus Torvalds * 40521d82de61SMel Gorman * Returns the order kswapd finished reclaiming at. 40531da177e4SLinus Torvalds * 40541da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 405541858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 40568bb4e7a2SWei Yang * found to have free_pages <= high_wmark_pages(zone), any page in that zone 40571d82de61SMel Gorman * or lower is eligible for reclaim until at least one usable zone is 40581d82de61SMel Gorman * balanced. 40591da177e4SLinus Torvalds */ 406097a225e6SJoonsoo Kim static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx) 40611da177e4SLinus Torvalds { 40621da177e4SLinus Torvalds int i; 40630608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 40640608f43dSAndrew Morton unsigned long nr_soft_scanned; 4065eb414681SJohannes Weiner unsigned long pflags; 40661c30844dSMel Gorman unsigned long nr_boost_reclaim; 40671c30844dSMel Gorman unsigned long zone_boosts[MAX_NR_ZONES] = { 0, }; 40681c30844dSMel Gorman bool boosted; 40691d82de61SMel Gorman struct zone *zone; 4070179e9639SAndrew Morton struct scan_control sc = { 4071179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 4072ee814fe2SJohannes Weiner .order = order, 4073a6dc60f8SJohannes Weiner .may_unmap = 1, 4074179e9639SAndrew Morton }; 407593781325SOmar Sandoval 40761732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 4077eb414681SJohannes Weiner psi_memstall_enter(&pflags); 40784f3eaf45SMatthew Wilcox (Oracle) __fs_reclaim_acquire(_THIS_IP_); 407993781325SOmar Sandoval 4080f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 40811da177e4SLinus Torvalds 40821c30844dSMel Gorman /* 40831c30844dSMel Gorman * Account for the reclaim boost. Note that the zone boost is left in 40841c30844dSMel Gorman * place so that parallel allocations that are near the watermark will 40851c30844dSMel Gorman * stall or direct reclaim until kswapd is finished. 40861c30844dSMel Gorman */ 40871c30844dSMel Gorman nr_boost_reclaim = 0; 408897a225e6SJoonsoo Kim for (i = 0; i <= highest_zoneidx; i++) { 40891c30844dSMel Gorman zone = pgdat->node_zones + i; 40901c30844dSMel Gorman if (!managed_zone(zone)) 40911c30844dSMel Gorman continue; 40921c30844dSMel Gorman 40931c30844dSMel Gorman nr_boost_reclaim += zone->watermark_boost; 40941c30844dSMel Gorman zone_boosts[i] = zone->watermark_boost; 40951c30844dSMel Gorman } 40961c30844dSMel Gorman boosted = nr_boost_reclaim; 40971c30844dSMel Gorman 40981c30844dSMel Gorman restart: 4099c49c2c47SMel Gorman set_reclaim_active(pgdat, highest_zoneidx); 41001c30844dSMel Gorman sc.priority = DEF_PRIORITY; 41019e3b2f8cSKonstantin Khlebnikov do { 4102c73322d0SJohannes Weiner unsigned long nr_reclaimed = sc.nr_reclaimed; 4103b8e83b94SMel Gorman bool raise_priority = true; 41041c30844dSMel Gorman bool balanced; 410593781325SOmar Sandoval bool ret; 4106b8e83b94SMel Gorman 410797a225e6SJoonsoo Kim sc.reclaim_idx = highest_zoneidx; 41081da177e4SLinus Torvalds 410986c79f6bSMel Gorman /* 411084c7a777SMel Gorman * If the number of buffer_heads exceeds the maximum allowed 411184c7a777SMel Gorman * then consider reclaiming from all zones. This has a dual 411284c7a777SMel Gorman * purpose -- on 64-bit systems it is expected that 411384c7a777SMel Gorman * buffer_heads are stripped during active rotation. On 32-bit 411484c7a777SMel Gorman * systems, highmem pages can pin lowmem memory and shrinking 411584c7a777SMel Gorman * buffers can relieve lowmem pressure. Reclaim may still not 411684c7a777SMel Gorman * go ahead if all eligible zones for the original allocation 411784c7a777SMel Gorman * request are balanced to avoid excessive reclaim from kswapd. 411886c79f6bSMel Gorman */ 411986c79f6bSMel Gorman if (buffer_heads_over_limit) { 412086c79f6bSMel Gorman for (i = MAX_NR_ZONES - 1; i >= 0; i--) { 412186c79f6bSMel Gorman zone = pgdat->node_zones + i; 41226aa303deSMel Gorman if (!managed_zone(zone)) 412386c79f6bSMel Gorman continue; 412486c79f6bSMel Gorman 4125970a39a3SMel Gorman sc.reclaim_idx = i; 412686c79f6bSMel Gorman break; 412786c79f6bSMel Gorman } 412886c79f6bSMel Gorman } 412986c79f6bSMel Gorman 413086c79f6bSMel Gorman /* 41311c30844dSMel Gorman * If the pgdat is imbalanced then ignore boosting and preserve 41321c30844dSMel Gorman * the watermarks for a later time and restart. Note that the 41331c30844dSMel Gorman * zone watermarks will be still reset at the end of balancing 41341c30844dSMel Gorman * on the grounds that the normal reclaim should be enough to 41351c30844dSMel Gorman * re-evaluate if boosting is required when kswapd next wakes. 413686c79f6bSMel Gorman */ 413797a225e6SJoonsoo Kim balanced = pgdat_balanced(pgdat, sc.order, highest_zoneidx); 41381c30844dSMel Gorman if (!balanced && nr_boost_reclaim) { 41391c30844dSMel Gorman nr_boost_reclaim = 0; 41401c30844dSMel Gorman goto restart; 41411c30844dSMel Gorman } 41421c30844dSMel Gorman 41431c30844dSMel Gorman /* 41441c30844dSMel Gorman * If boosting is not active then only reclaim if there are no 41451c30844dSMel Gorman * eligible zones. Note that sc.reclaim_idx is not used as 41461c30844dSMel Gorman * buffer_heads_over_limit may have adjusted it. 41471c30844dSMel Gorman */ 41481c30844dSMel Gorman if (!nr_boost_reclaim && balanced) 41491da177e4SLinus Torvalds goto out; 4150e1dbeda6SAndrew Morton 41511c30844dSMel Gorman /* Limit the priority of boosting to avoid reclaim writeback */ 41521c30844dSMel Gorman if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2) 41531c30844dSMel Gorman raise_priority = false; 41541c30844dSMel Gorman 41551c30844dSMel Gorman /* 41561c30844dSMel Gorman * Do not writeback or swap pages for boosted reclaim. The 41571c30844dSMel Gorman * intent is to relieve pressure not issue sub-optimal IO 41581c30844dSMel Gorman * from reclaim context. If no pages are reclaimed, the 41591c30844dSMel Gorman * reclaim will be aborted. 41601c30844dSMel Gorman */ 41611c30844dSMel Gorman sc.may_writepage = !laptop_mode && !nr_boost_reclaim; 41621c30844dSMel Gorman sc.may_swap = !nr_boost_reclaim; 41631c30844dSMel Gorman 41641da177e4SLinus Torvalds /* 41651d82de61SMel Gorman * Do some background aging of the anon list, to give 41661d82de61SMel Gorman * pages a chance to be referenced before reclaiming. All 41671d82de61SMel Gorman * pages are rotated regardless of classzone as this is 41681d82de61SMel Gorman * about consistent aging. 41691d82de61SMel Gorman */ 4170ef8f2327SMel Gorman age_active_anon(pgdat, &sc); 41711d82de61SMel Gorman 41721d82de61SMel Gorman /* 4173b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 4174b7ea3c41SMel Gorman * even in laptop mode. 4175b7ea3c41SMel Gorman */ 4176047d72c3SJohannes Weiner if (sc.priority < DEF_PRIORITY - 2) 4177b7ea3c41SMel Gorman sc.may_writepage = 1; 4178b7ea3c41SMel Gorman 41791d82de61SMel Gorman /* Call soft limit reclaim before calling shrink_node. */ 41801da177e4SLinus Torvalds sc.nr_scanned = 0; 41810608f43dSAndrew Morton nr_soft_scanned = 0; 4182ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, 41831d82de61SMel Gorman sc.gfp_mask, &nr_soft_scanned); 41840608f43dSAndrew Morton sc.nr_reclaimed += nr_soft_reclaimed; 41850608f43dSAndrew Morton 418632a4330dSRik van Riel /* 41871d82de61SMel Gorman * There should be no need to raise the scanning priority if 41881d82de61SMel Gorman * enough pages are already being scanned that that high 41891d82de61SMel Gorman * watermark would be met at 100% efficiency. 419032a4330dSRik van Riel */ 4191970a39a3SMel Gorman if (kswapd_shrink_node(pgdat, &sc)) 4192b8e83b94SMel Gorman raise_priority = false; 4193d7868daeSMel Gorman 41945515061dSMel Gorman /* 41955515061dSMel Gorman * If the low watermark is met there is no need for processes 41965515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 41975515061dSMel Gorman * able to safely make forward progress. Wake them 41985515061dSMel Gorman */ 41995515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 4200c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)) 4201cfc51155SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 42025515061dSMel Gorman 4203b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 42044f3eaf45SMatthew Wilcox (Oracle) __fs_reclaim_release(_THIS_IP_); 420593781325SOmar Sandoval ret = try_to_freeze(); 42064f3eaf45SMatthew Wilcox (Oracle) __fs_reclaim_acquire(_THIS_IP_); 420793781325SOmar Sandoval if (ret || kthread_should_stop()) 4208b8e83b94SMel Gorman break; 4209b8e83b94SMel Gorman 4210b8e83b94SMel Gorman /* 4211b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 4212b8e83b94SMel Gorman * progress in reclaiming pages 4213b8e83b94SMel Gorman */ 4214c73322d0SJohannes Weiner nr_reclaimed = sc.nr_reclaimed - nr_reclaimed; 42151c30844dSMel Gorman nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed); 42161c30844dSMel Gorman 42171c30844dSMel Gorman /* 42181c30844dSMel Gorman * If reclaim made no progress for a boost, stop reclaim as 42191c30844dSMel Gorman * IO cannot be queued and it could be an infinite loop in 42201c30844dSMel Gorman * extreme circumstances. 42211c30844dSMel Gorman */ 42221c30844dSMel Gorman if (nr_boost_reclaim && !nr_reclaimed) 42231c30844dSMel Gorman break; 42241c30844dSMel Gorman 4225c73322d0SJohannes Weiner if (raise_priority || !nr_reclaimed) 4226b8e83b94SMel Gorman sc.priority--; 42271d82de61SMel Gorman } while (sc.priority >= 1); 42281da177e4SLinus Torvalds 4229c73322d0SJohannes Weiner if (!sc.nr_reclaimed) 4230c73322d0SJohannes Weiner pgdat->kswapd_failures++; 4231c73322d0SJohannes Weiner 4232b8e83b94SMel Gorman out: 4233c49c2c47SMel Gorman clear_reclaim_active(pgdat, highest_zoneidx); 4234c49c2c47SMel Gorman 42351c30844dSMel Gorman /* If reclaim was boosted, account for the reclaim done in this pass */ 42361c30844dSMel Gorman if (boosted) { 42371c30844dSMel Gorman unsigned long flags; 42381c30844dSMel Gorman 423997a225e6SJoonsoo Kim for (i = 0; i <= highest_zoneidx; i++) { 42401c30844dSMel Gorman if (!zone_boosts[i]) 42411c30844dSMel Gorman continue; 42421c30844dSMel Gorman 42431c30844dSMel Gorman /* Increments are under the zone lock */ 42441c30844dSMel Gorman zone = pgdat->node_zones + i; 42451c30844dSMel Gorman spin_lock_irqsave(&zone->lock, flags); 42461c30844dSMel Gorman zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]); 42471c30844dSMel Gorman spin_unlock_irqrestore(&zone->lock, flags); 42481c30844dSMel Gorman } 42491c30844dSMel Gorman 42501c30844dSMel Gorman /* 42511c30844dSMel Gorman * As there is now likely space, wakeup kcompact to defragment 42521c30844dSMel Gorman * pageblocks. 42531c30844dSMel Gorman */ 425497a225e6SJoonsoo Kim wakeup_kcompactd(pgdat, pageblock_order, highest_zoneidx); 42551c30844dSMel Gorman } 42561c30844dSMel Gorman 42572a2e4885SJohannes Weiner snapshot_refaults(NULL, pgdat); 42584f3eaf45SMatthew Wilcox (Oracle) __fs_reclaim_release(_THIS_IP_); 4259eb414681SJohannes Weiner psi_memstall_leave(&pflags); 42601732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 4261e5ca8071SYafang Shao 42620abdee2bSMel Gorman /* 42631d82de61SMel Gorman * Return the order kswapd stopped reclaiming at as 42641d82de61SMel Gorman * prepare_kswapd_sleep() takes it into account. If another caller 42651d82de61SMel Gorman * entered the allocator slow path while kswapd was awake, order will 42661d82de61SMel Gorman * remain at the higher level. 42670abdee2bSMel Gorman */ 42681d82de61SMel Gorman return sc.order; 42691da177e4SLinus Torvalds } 42701da177e4SLinus Torvalds 4271e716f2ebSMel Gorman /* 427297a225e6SJoonsoo Kim * The pgdat->kswapd_highest_zoneidx is used to pass the highest zone index to 427397a225e6SJoonsoo Kim * be reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is 427497a225e6SJoonsoo Kim * not a valid index then either kswapd runs for first time or kswapd couldn't 427597a225e6SJoonsoo Kim * sleep after previous reclaim attempt (node is still unbalanced). In that 427697a225e6SJoonsoo Kim * case return the zone index of the previous kswapd reclaim cycle. 4277e716f2ebSMel Gorman */ 427897a225e6SJoonsoo Kim static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat, 427997a225e6SJoonsoo Kim enum zone_type prev_highest_zoneidx) 4280e716f2ebSMel Gorman { 428197a225e6SJoonsoo Kim enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); 42825644e1fbSQian Cai 428397a225e6SJoonsoo Kim return curr_idx == MAX_NR_ZONES ? prev_highest_zoneidx : curr_idx; 4284e716f2ebSMel Gorman } 4285e716f2ebSMel Gorman 428638087d9bSMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, 428797a225e6SJoonsoo Kim unsigned int highest_zoneidx) 4288f0bc0a60SKOSAKI Motohiro { 4289f0bc0a60SKOSAKI Motohiro long remaining = 0; 4290f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 4291f0bc0a60SKOSAKI Motohiro 4292f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 4293f0bc0a60SKOSAKI Motohiro return; 4294f0bc0a60SKOSAKI Motohiro 4295f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 4296f0bc0a60SKOSAKI Motohiro 4297333b0a45SShantanu Goel /* 4298333b0a45SShantanu Goel * Try to sleep for a short interval. Note that kcompactd will only be 4299333b0a45SShantanu Goel * woken if it is possible to sleep for a short interval. This is 4300333b0a45SShantanu Goel * deliberate on the assumption that if reclaim cannot keep an 4301333b0a45SShantanu Goel * eligible zone balanced that it's also unlikely that compaction will 4302333b0a45SShantanu Goel * succeed. 4303333b0a45SShantanu Goel */ 430497a225e6SJoonsoo Kim if (prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { 4305fd901c95SVlastimil Babka /* 4306fd901c95SVlastimil Babka * Compaction records what page blocks it recently failed to 4307fd901c95SVlastimil Babka * isolate pages from and skips them in the future scanning. 4308fd901c95SVlastimil Babka * When kswapd is going to sleep, it is reasonable to assume 4309fd901c95SVlastimil Babka * that pages and compaction may succeed so reset the cache. 4310fd901c95SVlastimil Babka */ 4311fd901c95SVlastimil Babka reset_isolation_suitable(pgdat); 4312fd901c95SVlastimil Babka 4313fd901c95SVlastimil Babka /* 4314fd901c95SVlastimil Babka * We have freed the memory, now we should compact it to make 4315fd901c95SVlastimil Babka * allocation of the requested order possible. 4316fd901c95SVlastimil Babka */ 431797a225e6SJoonsoo Kim wakeup_kcompactd(pgdat, alloc_order, highest_zoneidx); 4318fd901c95SVlastimil Babka 4319f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 432038087d9bSMel Gorman 432138087d9bSMel Gorman /* 432297a225e6SJoonsoo Kim * If woken prematurely then reset kswapd_highest_zoneidx and 432338087d9bSMel Gorman * order. The values will either be from a wakeup request or 432438087d9bSMel Gorman * the previous request that slept prematurely. 432538087d9bSMel Gorman */ 432638087d9bSMel Gorman if (remaining) { 432797a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, 432897a225e6SJoonsoo Kim kswapd_highest_zoneidx(pgdat, 432997a225e6SJoonsoo Kim highest_zoneidx)); 43305644e1fbSQian Cai 43315644e1fbSQian Cai if (READ_ONCE(pgdat->kswapd_order) < reclaim_order) 43325644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, reclaim_order); 433338087d9bSMel Gorman } 433438087d9bSMel Gorman 4335f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 4336f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 4337f0bc0a60SKOSAKI Motohiro } 4338f0bc0a60SKOSAKI Motohiro 4339f0bc0a60SKOSAKI Motohiro /* 4340f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 4341f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 4342f0bc0a60SKOSAKI Motohiro */ 4343d9f21d42SMel Gorman if (!remaining && 434497a225e6SJoonsoo Kim prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { 4345f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 4346f0bc0a60SKOSAKI Motohiro 4347f0bc0a60SKOSAKI Motohiro /* 4348f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 4349f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 4350f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 4351f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 4352f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 4353f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 4354f0bc0a60SKOSAKI Motohiro */ 4355f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 43561c7e7f6cSAaditya Kumar 43571c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 4358f0bc0a60SKOSAKI Motohiro schedule(); 43591c7e7f6cSAaditya Kumar 4360f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 4361f0bc0a60SKOSAKI Motohiro } else { 4362f0bc0a60SKOSAKI Motohiro if (remaining) 4363f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 4364f0bc0a60SKOSAKI Motohiro else 4365f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 4366f0bc0a60SKOSAKI Motohiro } 4367f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 4368f0bc0a60SKOSAKI Motohiro } 4369f0bc0a60SKOSAKI Motohiro 43701da177e4SLinus Torvalds /* 43711da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 43721da177e4SLinus Torvalds * from the init process. 43731da177e4SLinus Torvalds * 43741da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 43751da177e4SLinus Torvalds * free memory available even if there is no other activity 43761da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 43771da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 43781da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 43791da177e4SLinus Torvalds * 43801da177e4SLinus Torvalds * If there are applications that are active memory-allocators 43811da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 43821da177e4SLinus Torvalds */ 43831da177e4SLinus Torvalds static int kswapd(void *p) 43841da177e4SLinus Torvalds { 4385e716f2ebSMel Gorman unsigned int alloc_order, reclaim_order; 438697a225e6SJoonsoo Kim unsigned int highest_zoneidx = MAX_NR_ZONES - 1; 43871da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t *)p; 43881da177e4SLinus Torvalds struct task_struct *tsk = current; 4389a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 43901da177e4SLinus Torvalds 4391174596a0SRusty Russell if (!cpumask_empty(cpumask)) 4392c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 43931da177e4SLinus Torvalds 43941da177e4SLinus Torvalds /* 43951da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 43961da177e4SLinus Torvalds * and that if we need more memory we should get access to it 43971da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 43981da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 43991da177e4SLinus Torvalds * 44001da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 44011da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 44021da177e4SLinus Torvalds * page out something else, and this flag essentially protects 44031da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 44041da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 44051da177e4SLinus Torvalds */ 4406b698f0a1SHugh Dickins tsk->flags |= PF_MEMALLOC | PF_KSWAPD; 440783144186SRafael J. Wysocki set_freezable(); 44081da177e4SLinus Torvalds 44095644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, 0); 441097a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); 44118cd7c588SMel Gorman atomic_set(&pgdat->nr_writeback_throttled, 0); 44121da177e4SLinus Torvalds for ( ; ; ) { 44136f6313d4SJeff Liu bool ret; 44143e1d1d28SChristoph Lameter 44155644e1fbSQian Cai alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order); 441697a225e6SJoonsoo Kim highest_zoneidx = kswapd_highest_zoneidx(pgdat, 441797a225e6SJoonsoo Kim highest_zoneidx); 4418e716f2ebSMel Gorman 441938087d9bSMel Gorman kswapd_try_sleep: 442038087d9bSMel Gorman kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, 442197a225e6SJoonsoo Kim highest_zoneidx); 4422215ddd66SMel Gorman 442397a225e6SJoonsoo Kim /* Read the new order and highest_zoneidx */ 44242b47a24cSLukas Bulwahn alloc_order = READ_ONCE(pgdat->kswapd_order); 442597a225e6SJoonsoo Kim highest_zoneidx = kswapd_highest_zoneidx(pgdat, 442697a225e6SJoonsoo Kim highest_zoneidx); 44275644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, 0); 442897a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); 44291da177e4SLinus Torvalds 44308fe23e05SDavid Rientjes ret = try_to_freeze(); 44318fe23e05SDavid Rientjes if (kthread_should_stop()) 44328fe23e05SDavid Rientjes break; 44338fe23e05SDavid Rientjes 44348fe23e05SDavid Rientjes /* 44358fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 44368fe23e05SDavid Rientjes * after returning from the refrigerator 4437b1296cc4SRafael J. Wysocki */ 443838087d9bSMel Gorman if (ret) 443938087d9bSMel Gorman continue; 44401d82de61SMel Gorman 444138087d9bSMel Gorman /* 444238087d9bSMel Gorman * Reclaim begins at the requested order but if a high-order 444338087d9bSMel Gorman * reclaim fails then kswapd falls back to reclaiming for 444438087d9bSMel Gorman * order-0. If that happens, kswapd will consider sleeping 444538087d9bSMel Gorman * for the order it finished reclaiming at (reclaim_order) 444638087d9bSMel Gorman * but kcompactd is woken to compact for the original 444738087d9bSMel Gorman * request (alloc_order). 444838087d9bSMel Gorman */ 444997a225e6SJoonsoo Kim trace_mm_vmscan_kswapd_wake(pgdat->node_id, highest_zoneidx, 4450e5146b12SMel Gorman alloc_order); 445197a225e6SJoonsoo Kim reclaim_order = balance_pgdat(pgdat, alloc_order, 445297a225e6SJoonsoo Kim highest_zoneidx); 445338087d9bSMel Gorman if (reclaim_order < alloc_order) 445438087d9bSMel Gorman goto kswapd_try_sleep; 445533906bc5SMel Gorman } 4456b0a8cc58STakamori Yamaguchi 4457b698f0a1SHugh Dickins tsk->flags &= ~(PF_MEMALLOC | PF_KSWAPD); 445871abdc15SJohannes Weiner 44591da177e4SLinus Torvalds return 0; 44601da177e4SLinus Torvalds } 44611da177e4SLinus Torvalds 44621da177e4SLinus Torvalds /* 44635ecd9d40SDavid Rientjes * A zone is low on free memory or too fragmented for high-order memory. If 44645ecd9d40SDavid Rientjes * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's 44655ecd9d40SDavid Rientjes * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim 44665ecd9d40SDavid Rientjes * has failed or is not needed, still wake up kcompactd if only compaction is 44675ecd9d40SDavid Rientjes * needed. 44681da177e4SLinus Torvalds */ 44695ecd9d40SDavid Rientjes void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, 447097a225e6SJoonsoo Kim enum zone_type highest_zoneidx) 44711da177e4SLinus Torvalds { 44721da177e4SLinus Torvalds pg_data_t *pgdat; 44735644e1fbSQian Cai enum zone_type curr_idx; 44741da177e4SLinus Torvalds 44756aa303deSMel Gorman if (!managed_zone(zone)) 44761da177e4SLinus Torvalds return; 44771da177e4SLinus Torvalds 44785ecd9d40SDavid Rientjes if (!cpuset_zone_allowed(zone, gfp_flags)) 44791da177e4SLinus Torvalds return; 4480dffcac2cSShakeel Butt 44815644e1fbSQian Cai pgdat = zone->zone_pgdat; 448297a225e6SJoonsoo Kim curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); 44835644e1fbSQian Cai 448497a225e6SJoonsoo Kim if (curr_idx == MAX_NR_ZONES || curr_idx < highest_zoneidx) 448597a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, highest_zoneidx); 44865644e1fbSQian Cai 44875644e1fbSQian Cai if (READ_ONCE(pgdat->kswapd_order) < order) 44885644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, order); 44895644e1fbSQian Cai 44908d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 44911da177e4SLinus Torvalds return; 4492e1a55637SMel Gorman 44935ecd9d40SDavid Rientjes /* Hopeless node, leave it to direct reclaim if possible */ 44945ecd9d40SDavid Rientjes if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES || 449597a225e6SJoonsoo Kim (pgdat_balanced(pgdat, order, highest_zoneidx) && 449697a225e6SJoonsoo Kim !pgdat_watermark_boosted(pgdat, highest_zoneidx))) { 44975ecd9d40SDavid Rientjes /* 44985ecd9d40SDavid Rientjes * There may be plenty of free memory available, but it's too 44995ecd9d40SDavid Rientjes * fragmented for high-order allocations. Wake up kcompactd 45005ecd9d40SDavid Rientjes * and rely on compaction_suitable() to determine if it's 45015ecd9d40SDavid Rientjes * needed. If it fails, it will defer subsequent attempts to 45025ecd9d40SDavid Rientjes * ratelimit its work. 45035ecd9d40SDavid Rientjes */ 45045ecd9d40SDavid Rientjes if (!(gfp_flags & __GFP_DIRECT_RECLAIM)) 450597a225e6SJoonsoo Kim wakeup_kcompactd(pgdat, order, highest_zoneidx); 4506c73322d0SJohannes Weiner return; 45075ecd9d40SDavid Rientjes } 4508c73322d0SJohannes Weiner 450997a225e6SJoonsoo Kim trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, highest_zoneidx, order, 45105ecd9d40SDavid Rientjes gfp_flags); 45118d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 45121da177e4SLinus Torvalds } 45131da177e4SLinus Torvalds 4514c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 45151da177e4SLinus Torvalds /* 45167b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 4517d6277db4SRafael J. Wysocki * freed pages. 4518d6277db4SRafael J. Wysocki * 4519d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 4520d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 4521d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 45221da177e4SLinus Torvalds */ 45237b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 45241da177e4SLinus Torvalds { 4525d6277db4SRafael J. Wysocki struct scan_control sc = { 45267b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 4527ee814fe2SJohannes Weiner .gfp_mask = GFP_HIGHUSER_MOVABLE, 4528b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 45299e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 4530ee814fe2SJohannes Weiner .may_writepage = 1, 4531ee814fe2SJohannes Weiner .may_unmap = 1, 4532ee814fe2SJohannes Weiner .may_swap = 1, 4533ee814fe2SJohannes Weiner .hibernation_mode = 1, 45341da177e4SLinus Torvalds }; 45357b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 45367b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 4537499118e9SVlastimil Babka unsigned int noreclaim_flag; 45381da177e4SLinus Torvalds 4539d92a8cfcSPeter Zijlstra fs_reclaim_acquire(sc.gfp_mask); 454093781325SOmar Sandoval noreclaim_flag = memalloc_noreclaim_save(); 45411732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 4542d6277db4SRafael J. Wysocki 45433115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 4544d6277db4SRafael J. Wysocki 45451732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 4546499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 454793781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 4548d6277db4SRafael J. Wysocki 45497b51755cSKOSAKI Motohiro return nr_reclaimed; 45501da177e4SLinus Torvalds } 4551c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 45521da177e4SLinus Torvalds 45533218ae14SYasunori Goto /* 45543218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 45553218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 45563218ae14SYasunori Goto */ 4557b87c517aSMiaohe Lin void kswapd_run(int nid) 45583218ae14SYasunori Goto { 45593218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 45603218ae14SYasunori Goto 45613218ae14SYasunori Goto if (pgdat->kswapd) 4562b87c517aSMiaohe Lin return; 45633218ae14SYasunori Goto 45643218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 45653218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 45663218ae14SYasunori Goto /* failure at boot is fatal */ 4567c6202adfSThomas Gleixner BUG_ON(system_state < SYSTEM_RUNNING); 4568d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 4569d72515b8SXishi Qiu pgdat->kswapd = NULL; 45703218ae14SYasunori Goto } 45713218ae14SYasunori Goto } 45723218ae14SYasunori Goto 45738fe23e05SDavid Rientjes /* 4574d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 4575bfc8c901SVladimir Davydov * hold mem_hotplug_begin/end(). 45768fe23e05SDavid Rientjes */ 45778fe23e05SDavid Rientjes void kswapd_stop(int nid) 45788fe23e05SDavid Rientjes { 45798fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 45808fe23e05SDavid Rientjes 4581d8adde17SJiang Liu if (kswapd) { 45828fe23e05SDavid Rientjes kthread_stop(kswapd); 4583d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 4584d8adde17SJiang Liu } 45858fe23e05SDavid Rientjes } 45868fe23e05SDavid Rientjes 45871da177e4SLinus Torvalds static int __init kswapd_init(void) 45881da177e4SLinus Torvalds { 45896b700b5bSWei Yang int nid; 459069e05944SAndrew Morton 45911da177e4SLinus Torvalds swap_setup(); 459248fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 45933218ae14SYasunori Goto kswapd_run(nid); 45941da177e4SLinus Torvalds return 0; 45951da177e4SLinus Torvalds } 45961da177e4SLinus Torvalds 45971da177e4SLinus Torvalds module_init(kswapd_init) 45989eeff239SChristoph Lameter 45999eeff239SChristoph Lameter #ifdef CONFIG_NUMA 46009eeff239SChristoph Lameter /* 4601a5f5f91dSMel Gorman * Node reclaim mode 46029eeff239SChristoph Lameter * 4603a5f5f91dSMel Gorman * If non-zero call node_reclaim when the number of free pages falls below 46049eeff239SChristoph Lameter * the watermarks. 46059eeff239SChristoph Lameter */ 4606a5f5f91dSMel Gorman int node_reclaim_mode __read_mostly; 46079eeff239SChristoph Lameter 460851998364SDave Hansen /* 4609a5f5f91dSMel Gorman * Priority for NODE_RECLAIM. This determines the fraction of pages 4610a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 4611a92f7126SChristoph Lameter * a zone. 4612a92f7126SChristoph Lameter */ 4613a5f5f91dSMel Gorman #define NODE_RECLAIM_PRIORITY 4 4614a92f7126SChristoph Lameter 46159eeff239SChristoph Lameter /* 4616a5f5f91dSMel Gorman * Percentage of pages in a zone that must be unmapped for node_reclaim to 46179614634fSChristoph Lameter * occur. 46189614634fSChristoph Lameter */ 46199614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 46209614634fSChristoph Lameter 46219614634fSChristoph Lameter /* 46220ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 46230ff38490SChristoph Lameter * slab reclaim needs to occur. 46240ff38490SChristoph Lameter */ 46250ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 46260ff38490SChristoph Lameter 462711fb9989SMel Gorman static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) 462890afa5deSMel Gorman { 462911fb9989SMel Gorman unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); 463011fb9989SMel Gorman unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + 463111fb9989SMel Gorman node_page_state(pgdat, NR_ACTIVE_FILE); 463290afa5deSMel Gorman 463390afa5deSMel Gorman /* 463490afa5deSMel Gorman * It's possible for there to be more file mapped pages than 463590afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 463690afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 463790afa5deSMel Gorman */ 463890afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 463990afa5deSMel Gorman } 464090afa5deSMel Gorman 464190afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 4642a5f5f91dSMel Gorman static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) 464390afa5deSMel Gorman { 4644d031a157SAlexandru Moise unsigned long nr_pagecache_reclaimable; 4645d031a157SAlexandru Moise unsigned long delta = 0; 464690afa5deSMel Gorman 464790afa5deSMel Gorman /* 464895bbc0c7SZhihui Zhang * If RECLAIM_UNMAP is set, then all file pages are considered 464990afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 465011fb9989SMel Gorman * pages like swapcache and node_unmapped_file_pages() provides 465190afa5deSMel Gorman * a better estimate 465290afa5deSMel Gorman */ 4653a5f5f91dSMel Gorman if (node_reclaim_mode & RECLAIM_UNMAP) 4654a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); 465590afa5deSMel Gorman else 4656a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); 465790afa5deSMel Gorman 465890afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 4659a5f5f91dSMel Gorman if (!(node_reclaim_mode & RECLAIM_WRITE)) 4660a5f5f91dSMel Gorman delta += node_page_state(pgdat, NR_FILE_DIRTY); 466190afa5deSMel Gorman 466290afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 466390afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 466490afa5deSMel Gorman delta = nr_pagecache_reclaimable; 466590afa5deSMel Gorman 466690afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 466790afa5deSMel Gorman } 466890afa5deSMel Gorman 46690ff38490SChristoph Lameter /* 4670a5f5f91dSMel Gorman * Try to free up some pages from this node through reclaim. 46719eeff239SChristoph Lameter */ 4672a5f5f91dSMel Gorman static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 46739eeff239SChristoph Lameter { 46747fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 467569e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 46769eeff239SChristoph Lameter struct task_struct *p = current; 4677499118e9SVlastimil Babka unsigned int noreclaim_flag; 4678179e9639SAndrew Morton struct scan_control sc = { 467962b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 4680f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 4681bd2f6199SJohannes Weiner .order = order, 4682a5f5f91dSMel Gorman .priority = NODE_RECLAIM_PRIORITY, 4683a5f5f91dSMel Gorman .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), 4684a5f5f91dSMel Gorman .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), 4685ee814fe2SJohannes Weiner .may_swap = 1, 4686f2f43e56SNick Desaulniers .reclaim_idx = gfp_zone(gfp_mask), 4687179e9639SAndrew Morton }; 468857f29762SJohannes Weiner unsigned long pflags; 46899eeff239SChristoph Lameter 4690132bb8cfSYafang Shao trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order, 4691132bb8cfSYafang Shao sc.gfp_mask); 4692132bb8cfSYafang Shao 46939eeff239SChristoph Lameter cond_resched(); 469457f29762SJohannes Weiner psi_memstall_enter(&pflags); 469593781325SOmar Sandoval fs_reclaim_acquire(sc.gfp_mask); 4696d4f7796eSChristoph Lameter /* 469795bbc0c7SZhihui Zhang * We need to be able to allocate from the reserves for RECLAIM_UNMAP 4698d4f7796eSChristoph Lameter */ 4699499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 47001732d2b0SAndrew Morton set_task_reclaim_state(p, &sc.reclaim_state); 4701c84db23cSChristoph Lameter 4702a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) { 4703a92f7126SChristoph Lameter /* 4704894befecSAndrey Ryabinin * Free memory by calling shrink node with increasing 47050ff38490SChristoph Lameter * priorities until we have enough memory freed. 4706a92f7126SChristoph Lameter */ 4707a92f7126SChristoph Lameter do { 4708970a39a3SMel Gorman shrink_node(pgdat, &sc); 47099e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 47100ff38490SChristoph Lameter } 4711a92f7126SChristoph Lameter 47121732d2b0SAndrew Morton set_task_reclaim_state(p, NULL); 4713499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 471493781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 471557f29762SJohannes Weiner psi_memstall_leave(&pflags); 4716132bb8cfSYafang Shao 4717132bb8cfSYafang Shao trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed); 4718132bb8cfSYafang Shao 4719a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 47209eeff239SChristoph Lameter } 4721179e9639SAndrew Morton 4722a5f5f91dSMel Gorman int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 4723179e9639SAndrew Morton { 4724d773ed6bSDavid Rientjes int ret; 4725179e9639SAndrew Morton 4726179e9639SAndrew Morton /* 4727a5f5f91dSMel Gorman * Node reclaim reclaims unmapped file backed pages and 47280ff38490SChristoph Lameter * slab pages if we are over the defined limits. 472934aa1330SChristoph Lameter * 47309614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 47319614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 4732a5f5f91dSMel Gorman * thrown out if the node is overallocated. So we do not reclaim 4733a5f5f91dSMel Gorman * if less than a specified percentage of the node is used by 47349614634fSChristoph Lameter * unmapped file backed pages. 4735179e9639SAndrew Morton */ 4736a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && 4737d42f3245SRoman Gushchin node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) <= 4738d42f3245SRoman Gushchin pgdat->min_slab_pages) 4739a5f5f91dSMel Gorman return NODE_RECLAIM_FULL; 4740179e9639SAndrew Morton 4741179e9639SAndrew Morton /* 4742d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 4743179e9639SAndrew Morton */ 4744d0164adcSMel Gorman if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) 4745a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4746179e9639SAndrew Morton 4747179e9639SAndrew Morton /* 4748a5f5f91dSMel Gorman * Only run node reclaim on the local node or on nodes that do not 4749179e9639SAndrew Morton * have associated processors. This will favor the local processor 4750179e9639SAndrew Morton * over remote processors and spread off node memory allocations 4751179e9639SAndrew Morton * as wide as possible. 4752179e9639SAndrew Morton */ 4753a5f5f91dSMel Gorman if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) 4754a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4755d773ed6bSDavid Rientjes 4756a5f5f91dSMel Gorman if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) 4757a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4758fa5e084eSMel Gorman 4759a5f5f91dSMel Gorman ret = __node_reclaim(pgdat, gfp_mask, order); 4760a5f5f91dSMel Gorman clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); 4761d773ed6bSDavid Rientjes 476224cf7251SMel Gorman if (!ret) 476324cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 476424cf7251SMel Gorman 4765d773ed6bSDavid Rientjes return ret; 4766179e9639SAndrew Morton } 47679eeff239SChristoph Lameter #endif 4768894bc310SLee Schermerhorn 476989e004eaSLee Schermerhorn /** 477064e3d12fSKuo-Hsin Yang * check_move_unevictable_pages - check pages for evictability and move to 477164e3d12fSKuo-Hsin Yang * appropriate zone lru list 477264e3d12fSKuo-Hsin Yang * @pvec: pagevec with lru pages to check 477389e004eaSLee Schermerhorn * 477464e3d12fSKuo-Hsin Yang * Checks pages for evictability, if an evictable page is in the unevictable 477564e3d12fSKuo-Hsin Yang * lru list, moves it to the appropriate evictable lru list. This function 477664e3d12fSKuo-Hsin Yang * should be only used for lru pages. 477789e004eaSLee Schermerhorn */ 477864e3d12fSKuo-Hsin Yang void check_move_unevictable_pages(struct pagevec *pvec) 477989e004eaSLee Schermerhorn { 47806168d0daSAlex Shi struct lruvec *lruvec = NULL; 478124513264SHugh Dickins int pgscanned = 0; 478224513264SHugh Dickins int pgrescued = 0; 478389e004eaSLee Schermerhorn int i; 478489e004eaSLee Schermerhorn 478564e3d12fSKuo-Hsin Yang for (i = 0; i < pvec->nr; i++) { 478664e3d12fSKuo-Hsin Yang struct page *page = pvec->pages[i]; 47870de340cbSMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 47888d8869caSHugh Dickins int nr_pages; 478989e004eaSLee Schermerhorn 47908d8869caSHugh Dickins if (PageTransTail(page)) 47918d8869caSHugh Dickins continue; 47928d8869caSHugh Dickins 47938d8869caSHugh Dickins nr_pages = thp_nr_pages(page); 47948d8869caSHugh Dickins pgscanned += nr_pages; 47958d8869caSHugh Dickins 4796d25b5bd8SAlex Shi /* block memcg migration during page moving between lru */ 4797d25b5bd8SAlex Shi if (!TestClearPageLRU(page)) 4798d25b5bd8SAlex Shi continue; 4799d25b5bd8SAlex Shi 48000de340cbSMatthew Wilcox (Oracle) lruvec = folio_lruvec_relock_irq(folio, lruvec); 4801d25b5bd8SAlex Shi if (page_evictable(page) && PageUnevictable(page)) { 480246ae6b2cSYu Zhao del_page_from_lru_list(page, lruvec); 480324513264SHugh Dickins ClearPageUnevictable(page); 48043a9c9788SYu Zhao add_page_to_lru_list(page, lruvec); 48058d8869caSHugh Dickins pgrescued += nr_pages; 480689e004eaSLee Schermerhorn } 4807d25b5bd8SAlex Shi SetPageLRU(page); 480889e004eaSLee Schermerhorn } 480924513264SHugh Dickins 48106168d0daSAlex Shi if (lruvec) { 481124513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 481224513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 48136168d0daSAlex Shi unlock_page_lruvec_irq(lruvec); 4814d25b5bd8SAlex Shi } else if (pgscanned) { 4815d25b5bd8SAlex Shi count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 481624513264SHugh Dickins } 481785046579SHugh Dickins } 481864e3d12fSKuo-Hsin Yang EXPORT_SYMBOL_GPL(check_move_unevictable_pages); 4819