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" 62014bb1deSNeilBrown #include "swap.h" 630f8053a5SNick Piggin 6433906bc5SMel Gorman #define CREATE_TRACE_POINTS 6533906bc5SMel Gorman #include <trace/events/vmscan.h> 6633906bc5SMel Gorman 671da177e4SLinus Torvalds struct scan_control { 6822fba335SKOSAKI Motohiro /* How many pages shrink_list() should reclaim */ 6922fba335SKOSAKI Motohiro unsigned long nr_to_reclaim; 7022fba335SKOSAKI Motohiro 71ee814fe2SJohannes Weiner /* 72ee814fe2SJohannes Weiner * Nodemask of nodes allowed by the caller. If NULL, all nodes 73ee814fe2SJohannes Weiner * are scanned. 74ee814fe2SJohannes Weiner */ 75ee814fe2SJohannes Weiner nodemask_t *nodemask; 769e3b2f8cSKonstantin Khlebnikov 775f53e762SKOSAKI Motohiro /* 78f16015fbSJohannes Weiner * The memory cgroup that hit its limit and as a result is the 79f16015fbSJohannes Weiner * primary target of this reclaim invocation. 80f16015fbSJohannes Weiner */ 81f16015fbSJohannes Weiner struct mem_cgroup *target_mem_cgroup; 8266e1707bSBalbir Singh 837cf111bcSJohannes Weiner /* 847cf111bcSJohannes Weiner * Scan pressure balancing between anon and file LRUs 857cf111bcSJohannes Weiner */ 867cf111bcSJohannes Weiner unsigned long anon_cost; 877cf111bcSJohannes Weiner unsigned long file_cost; 887cf111bcSJohannes Weiner 89b91ac374SJohannes Weiner /* Can active pages be deactivated as part of reclaim? */ 90b91ac374SJohannes Weiner #define DEACTIVATE_ANON 1 91b91ac374SJohannes Weiner #define DEACTIVATE_FILE 2 92b91ac374SJohannes Weiner unsigned int may_deactivate:2; 93b91ac374SJohannes Weiner unsigned int force_deactivate:1; 94b91ac374SJohannes Weiner unsigned int skipped_deactivate:1; 95b91ac374SJohannes Weiner 961276ad68SJohannes Weiner /* Writepage batching in laptop mode; RECLAIM_WRITE */ 97ee814fe2SJohannes Weiner unsigned int may_writepage:1; 98ee814fe2SJohannes Weiner 99ee814fe2SJohannes Weiner /* Can mapped pages be reclaimed? */ 100ee814fe2SJohannes Weiner unsigned int may_unmap:1; 101ee814fe2SJohannes Weiner 102ee814fe2SJohannes Weiner /* Can pages be swapped as part of reclaim? */ 103ee814fe2SJohannes Weiner unsigned int may_swap:1; 104ee814fe2SJohannes Weiner 105d6622f63SYisheng Xie /* 106f56ce412SJohannes Weiner * Cgroup memory below memory.low is protected as long as we 107f56ce412SJohannes Weiner * don't threaten to OOM. If any cgroup is reclaimed at 108f56ce412SJohannes Weiner * reduced force or passed over entirely due to its memory.low 109f56ce412SJohannes Weiner * setting (memcg_low_skipped), and nothing is reclaimed as a 110f56ce412SJohannes Weiner * result, then go back for one more cycle that reclaims the protected 111f56ce412SJohannes Weiner * memory (memcg_low_reclaim) to avert OOM. 112d6622f63SYisheng Xie */ 113d6622f63SYisheng Xie unsigned int memcg_low_reclaim:1; 114d6622f63SYisheng Xie unsigned int memcg_low_skipped:1; 115241994edSJohannes Weiner 116ee814fe2SJohannes Weiner unsigned int hibernation_mode:1; 117ee814fe2SJohannes Weiner 118ee814fe2SJohannes Weiner /* One of the zones is ready for compaction */ 119ee814fe2SJohannes Weiner unsigned int compaction_ready:1; 120ee814fe2SJohannes Weiner 121b91ac374SJohannes Weiner /* There is easily reclaimable cold cache in the current node */ 122b91ac374SJohannes Weiner unsigned int cache_trim_mode:1; 123b91ac374SJohannes Weiner 12453138ceaSJohannes Weiner /* The file pages on the current node are dangerously low */ 12553138ceaSJohannes Weiner unsigned int file_is_tiny:1; 12653138ceaSJohannes Weiner 12726aa2d19SDave Hansen /* Always discard instead of demoting to lower tier memory */ 12826aa2d19SDave Hansen unsigned int no_demotion:1; 12926aa2d19SDave Hansen 130bb451fdfSGreg Thelen /* Allocation order */ 131bb451fdfSGreg Thelen s8 order; 132bb451fdfSGreg Thelen 133bb451fdfSGreg Thelen /* Scan (total_size >> priority) pages at once */ 134bb451fdfSGreg Thelen s8 priority; 135bb451fdfSGreg Thelen 136bb451fdfSGreg Thelen /* The highest zone to isolate pages for reclaim from */ 137bb451fdfSGreg Thelen s8 reclaim_idx; 138bb451fdfSGreg Thelen 139bb451fdfSGreg Thelen /* This context's GFP mask */ 140bb451fdfSGreg Thelen gfp_t gfp_mask; 141bb451fdfSGreg Thelen 142ee814fe2SJohannes Weiner /* Incremented by the number of inactive pages that were scanned */ 143ee814fe2SJohannes Weiner unsigned long nr_scanned; 144ee814fe2SJohannes Weiner 145ee814fe2SJohannes Weiner /* Number of pages freed so far during a call to shrink_zones() */ 146ee814fe2SJohannes Weiner unsigned long nr_reclaimed; 147d108c772SAndrey Ryabinin 148d108c772SAndrey Ryabinin struct { 149d108c772SAndrey Ryabinin unsigned int dirty; 150d108c772SAndrey Ryabinin unsigned int unqueued_dirty; 151d108c772SAndrey Ryabinin unsigned int congested; 152d108c772SAndrey Ryabinin unsigned int writeback; 153d108c772SAndrey Ryabinin unsigned int immediate; 154d108c772SAndrey Ryabinin unsigned int file_taken; 155d108c772SAndrey Ryabinin unsigned int taken; 156d108c772SAndrey Ryabinin } nr; 157e5ca8071SYafang Shao 158e5ca8071SYafang Shao /* for recording the reclaimed slab by now */ 159e5ca8071SYafang Shao struct reclaim_state reclaim_state; 1601da177e4SLinus Torvalds }; 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1631da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1641da177e4SLinus Torvalds do { \ 1651da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1661da177e4SLinus Torvalds struct page *prev; \ 1671da177e4SLinus Torvalds \ 1681da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1691da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1701da177e4SLinus Torvalds } \ 1711da177e4SLinus Torvalds } while (0) 1721da177e4SLinus Torvalds #else 1731da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1741da177e4SLinus Torvalds #endif 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds /* 177c843966cSJohannes Weiner * From 0 .. 200. Higher means more swappy. 1781da177e4SLinus Torvalds */ 1791da177e4SLinus Torvalds int vm_swappiness = 60; 1801da177e4SLinus Torvalds 1810a432dcbSYang Shi static void set_task_reclaim_state(struct task_struct *task, 1820a432dcbSYang Shi struct reclaim_state *rs) 1830a432dcbSYang Shi { 1840a432dcbSYang Shi /* Check for an overwrite */ 1850a432dcbSYang Shi WARN_ON_ONCE(rs && task->reclaim_state); 1860a432dcbSYang Shi 1870a432dcbSYang Shi /* Check for the nulling of an already-nulled member */ 1880a432dcbSYang Shi WARN_ON_ONCE(!rs && !task->reclaim_state); 1890a432dcbSYang Shi 1900a432dcbSYang Shi task->reclaim_state = rs; 1910a432dcbSYang Shi } 1920a432dcbSYang Shi 1931da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1941da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1951da177e4SLinus Torvalds 1960a432dcbSYang Shi #ifdef CONFIG_MEMCG 197a2fb1261SYang Shi static int shrinker_nr_max; 1982bfd3637SYang Shi 1993c6f17e6SYang Shi /* The shrinker_info is expanded in a batch of BITS_PER_LONG */ 200a2fb1261SYang Shi static inline int shrinker_map_size(int nr_items) 201a2fb1261SYang Shi { 202a2fb1261SYang Shi return (DIV_ROUND_UP(nr_items, BITS_PER_LONG) * sizeof(unsigned long)); 203a2fb1261SYang Shi } 2042bfd3637SYang Shi 2053c6f17e6SYang Shi static inline int shrinker_defer_size(int nr_items) 2063c6f17e6SYang Shi { 2073c6f17e6SYang Shi return (round_up(nr_items, BITS_PER_LONG) * sizeof(atomic_long_t)); 2083c6f17e6SYang Shi } 2093c6f17e6SYang Shi 210468ab843SYang Shi static struct shrinker_info *shrinker_info_protected(struct mem_cgroup *memcg, 211468ab843SYang Shi int nid) 212468ab843SYang Shi { 213468ab843SYang Shi return rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_info, 214468ab843SYang Shi lockdep_is_held(&shrinker_rwsem)); 215468ab843SYang Shi } 216468ab843SYang Shi 217e4262c4fSYang Shi static int expand_one_shrinker_info(struct mem_cgroup *memcg, 2183c6f17e6SYang Shi int map_size, int defer_size, 2193c6f17e6SYang Shi int old_map_size, int old_defer_size) 2202bfd3637SYang Shi { 221e4262c4fSYang Shi struct shrinker_info *new, *old; 2222bfd3637SYang Shi struct mem_cgroup_per_node *pn; 2232bfd3637SYang Shi int nid; 2243c6f17e6SYang Shi int size = map_size + defer_size; 2252bfd3637SYang Shi 2262bfd3637SYang Shi for_each_node(nid) { 2272bfd3637SYang Shi pn = memcg->nodeinfo[nid]; 228468ab843SYang Shi old = shrinker_info_protected(memcg, nid); 2292bfd3637SYang Shi /* Not yet online memcg */ 2302bfd3637SYang Shi if (!old) 2312bfd3637SYang Shi return 0; 2322bfd3637SYang Shi 2332bfd3637SYang Shi new = kvmalloc_node(sizeof(*new) + size, GFP_KERNEL, nid); 2342bfd3637SYang Shi if (!new) 2352bfd3637SYang Shi return -ENOMEM; 2362bfd3637SYang Shi 2373c6f17e6SYang Shi new->nr_deferred = (atomic_long_t *)(new + 1); 2383c6f17e6SYang Shi new->map = (void *)new->nr_deferred + defer_size; 2393c6f17e6SYang Shi 2403c6f17e6SYang Shi /* map: set all old bits, clear all new bits */ 2413c6f17e6SYang Shi memset(new->map, (int)0xff, old_map_size); 2423c6f17e6SYang Shi memset((void *)new->map + old_map_size, 0, map_size - old_map_size); 2433c6f17e6SYang Shi /* nr_deferred: copy old values, clear all new values */ 2443c6f17e6SYang Shi memcpy(new->nr_deferred, old->nr_deferred, old_defer_size); 2453c6f17e6SYang Shi memset((void *)new->nr_deferred + old_defer_size, 0, 2463c6f17e6SYang Shi defer_size - old_defer_size); 2472bfd3637SYang Shi 248e4262c4fSYang Shi rcu_assign_pointer(pn->shrinker_info, new); 24972673e86SYang Shi kvfree_rcu(old, rcu); 2502bfd3637SYang Shi } 2512bfd3637SYang Shi 2522bfd3637SYang Shi return 0; 2532bfd3637SYang Shi } 2542bfd3637SYang Shi 255e4262c4fSYang Shi void free_shrinker_info(struct mem_cgroup *memcg) 2562bfd3637SYang Shi { 2572bfd3637SYang Shi struct mem_cgroup_per_node *pn; 258e4262c4fSYang Shi struct shrinker_info *info; 2592bfd3637SYang Shi int nid; 2602bfd3637SYang Shi 2612bfd3637SYang Shi for_each_node(nid) { 2622bfd3637SYang Shi pn = memcg->nodeinfo[nid]; 263e4262c4fSYang Shi info = rcu_dereference_protected(pn->shrinker_info, true); 264e4262c4fSYang Shi kvfree(info); 265e4262c4fSYang Shi rcu_assign_pointer(pn->shrinker_info, NULL); 2662bfd3637SYang Shi } 2672bfd3637SYang Shi } 2682bfd3637SYang Shi 269e4262c4fSYang Shi int alloc_shrinker_info(struct mem_cgroup *memcg) 2702bfd3637SYang Shi { 271e4262c4fSYang Shi struct shrinker_info *info; 2722bfd3637SYang Shi int nid, size, ret = 0; 2733c6f17e6SYang Shi int map_size, defer_size = 0; 2742bfd3637SYang Shi 275d27cf2aaSYang Shi down_write(&shrinker_rwsem); 2763c6f17e6SYang Shi map_size = shrinker_map_size(shrinker_nr_max); 2773c6f17e6SYang Shi defer_size = shrinker_defer_size(shrinker_nr_max); 2783c6f17e6SYang Shi size = map_size + defer_size; 2792bfd3637SYang Shi for_each_node(nid) { 280e4262c4fSYang Shi info = kvzalloc_node(sizeof(*info) + size, GFP_KERNEL, nid); 281e4262c4fSYang Shi if (!info) { 282e4262c4fSYang Shi free_shrinker_info(memcg); 2832bfd3637SYang Shi ret = -ENOMEM; 2842bfd3637SYang Shi break; 2852bfd3637SYang Shi } 2863c6f17e6SYang Shi info->nr_deferred = (atomic_long_t *)(info + 1); 2873c6f17e6SYang Shi info->map = (void *)info->nr_deferred + defer_size; 288e4262c4fSYang Shi rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, info); 2892bfd3637SYang Shi } 290d27cf2aaSYang Shi up_write(&shrinker_rwsem); 2912bfd3637SYang Shi 2922bfd3637SYang Shi return ret; 2932bfd3637SYang Shi } 2942bfd3637SYang Shi 2953c6f17e6SYang Shi static inline bool need_expand(int nr_max) 2963c6f17e6SYang Shi { 2973c6f17e6SYang Shi return round_up(nr_max, BITS_PER_LONG) > 2983c6f17e6SYang Shi round_up(shrinker_nr_max, BITS_PER_LONG); 2993c6f17e6SYang Shi } 3003c6f17e6SYang Shi 301e4262c4fSYang Shi static int expand_shrinker_info(int new_id) 3022bfd3637SYang Shi { 3033c6f17e6SYang Shi int ret = 0; 304a2fb1261SYang Shi int new_nr_max = new_id + 1; 3053c6f17e6SYang Shi int map_size, defer_size = 0; 3063c6f17e6SYang Shi int old_map_size, old_defer_size = 0; 3072bfd3637SYang Shi struct mem_cgroup *memcg; 3082bfd3637SYang Shi 3093c6f17e6SYang Shi if (!need_expand(new_nr_max)) 310a2fb1261SYang Shi goto out; 3112bfd3637SYang Shi 3122bfd3637SYang Shi if (!root_mem_cgroup) 313d27cf2aaSYang Shi goto out; 314d27cf2aaSYang Shi 315d27cf2aaSYang Shi lockdep_assert_held(&shrinker_rwsem); 3162bfd3637SYang Shi 3173c6f17e6SYang Shi map_size = shrinker_map_size(new_nr_max); 3183c6f17e6SYang Shi defer_size = shrinker_defer_size(new_nr_max); 3193c6f17e6SYang Shi old_map_size = shrinker_map_size(shrinker_nr_max); 3203c6f17e6SYang Shi old_defer_size = shrinker_defer_size(shrinker_nr_max); 3213c6f17e6SYang Shi 3222bfd3637SYang Shi memcg = mem_cgroup_iter(NULL, NULL, NULL); 3232bfd3637SYang Shi do { 3243c6f17e6SYang Shi ret = expand_one_shrinker_info(memcg, map_size, defer_size, 3253c6f17e6SYang Shi old_map_size, old_defer_size); 3262bfd3637SYang Shi if (ret) { 3272bfd3637SYang Shi mem_cgroup_iter_break(NULL, memcg); 328d27cf2aaSYang Shi goto out; 3292bfd3637SYang Shi } 3302bfd3637SYang Shi } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 331d27cf2aaSYang Shi out: 3322bfd3637SYang Shi if (!ret) 333a2fb1261SYang Shi shrinker_nr_max = new_nr_max; 334d27cf2aaSYang Shi 3352bfd3637SYang Shi return ret; 3362bfd3637SYang Shi } 3372bfd3637SYang Shi 3382bfd3637SYang Shi void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id) 3392bfd3637SYang Shi { 3402bfd3637SYang Shi if (shrinker_id >= 0 && memcg && !mem_cgroup_is_root(memcg)) { 341e4262c4fSYang Shi struct shrinker_info *info; 3422bfd3637SYang Shi 3432bfd3637SYang Shi rcu_read_lock(); 344e4262c4fSYang Shi info = rcu_dereference(memcg->nodeinfo[nid]->shrinker_info); 3452bfd3637SYang Shi /* Pairs with smp mb in shrink_slab() */ 3462bfd3637SYang Shi smp_mb__before_atomic(); 347e4262c4fSYang Shi set_bit(shrinker_id, info->map); 3482bfd3637SYang Shi rcu_read_unlock(); 3492bfd3637SYang Shi } 3502bfd3637SYang Shi } 3512bfd3637SYang Shi 352b4c2b231SKirill Tkhai static DEFINE_IDR(shrinker_idr); 353b4c2b231SKirill Tkhai 354b4c2b231SKirill Tkhai static int prealloc_memcg_shrinker(struct shrinker *shrinker) 355b4c2b231SKirill Tkhai { 356b4c2b231SKirill Tkhai int id, ret = -ENOMEM; 357b4c2b231SKirill Tkhai 358476b30a0SYang Shi if (mem_cgroup_disabled()) 359476b30a0SYang Shi return -ENOSYS; 360476b30a0SYang Shi 361b4c2b231SKirill Tkhai down_write(&shrinker_rwsem); 362b4c2b231SKirill Tkhai /* This may call shrinker, so it must use down_read_trylock() */ 36341ca668aSYang Shi id = idr_alloc(&shrinker_idr, shrinker, 0, 0, GFP_KERNEL); 364b4c2b231SKirill Tkhai if (id < 0) 365b4c2b231SKirill Tkhai goto unlock; 366b4c2b231SKirill Tkhai 3670a4465d3SKirill Tkhai if (id >= shrinker_nr_max) { 368e4262c4fSYang Shi if (expand_shrinker_info(id)) { 3690a4465d3SKirill Tkhai idr_remove(&shrinker_idr, id); 3700a4465d3SKirill Tkhai goto unlock; 3710a4465d3SKirill Tkhai } 3720a4465d3SKirill Tkhai } 373b4c2b231SKirill Tkhai shrinker->id = id; 374b4c2b231SKirill Tkhai ret = 0; 375b4c2b231SKirill Tkhai unlock: 376b4c2b231SKirill Tkhai up_write(&shrinker_rwsem); 377b4c2b231SKirill Tkhai return ret; 378b4c2b231SKirill Tkhai } 379b4c2b231SKirill Tkhai 380b4c2b231SKirill Tkhai static void unregister_memcg_shrinker(struct shrinker *shrinker) 381b4c2b231SKirill Tkhai { 382b4c2b231SKirill Tkhai int id = shrinker->id; 383b4c2b231SKirill Tkhai 384b4c2b231SKirill Tkhai BUG_ON(id < 0); 385b4c2b231SKirill Tkhai 38641ca668aSYang Shi lockdep_assert_held(&shrinker_rwsem); 38741ca668aSYang Shi 388b4c2b231SKirill Tkhai idr_remove(&shrinker_idr, id); 389b4c2b231SKirill Tkhai } 390b4c2b231SKirill Tkhai 39186750830SYang Shi static long xchg_nr_deferred_memcg(int nid, struct shrinker *shrinker, 39286750830SYang Shi struct mem_cgroup *memcg) 39386750830SYang Shi { 39486750830SYang Shi struct shrinker_info *info; 39586750830SYang Shi 39686750830SYang Shi info = shrinker_info_protected(memcg, nid); 39786750830SYang Shi return atomic_long_xchg(&info->nr_deferred[shrinker->id], 0); 39886750830SYang Shi } 39986750830SYang Shi 40086750830SYang Shi static long add_nr_deferred_memcg(long nr, int nid, struct shrinker *shrinker, 40186750830SYang Shi struct mem_cgroup *memcg) 40286750830SYang Shi { 40386750830SYang Shi struct shrinker_info *info; 40486750830SYang Shi 40586750830SYang Shi info = shrinker_info_protected(memcg, nid); 40686750830SYang Shi return atomic_long_add_return(nr, &info->nr_deferred[shrinker->id]); 40786750830SYang Shi } 40886750830SYang Shi 409a178015cSYang Shi void reparent_shrinker_deferred(struct mem_cgroup *memcg) 410a178015cSYang Shi { 411a178015cSYang Shi int i, nid; 412a178015cSYang Shi long nr; 413a178015cSYang Shi struct mem_cgroup *parent; 414a178015cSYang Shi struct shrinker_info *child_info, *parent_info; 415a178015cSYang Shi 416a178015cSYang Shi parent = parent_mem_cgroup(memcg); 417a178015cSYang Shi if (!parent) 418a178015cSYang Shi parent = root_mem_cgroup; 419a178015cSYang Shi 420a178015cSYang Shi /* Prevent from concurrent shrinker_info expand */ 421a178015cSYang Shi down_read(&shrinker_rwsem); 422a178015cSYang Shi for_each_node(nid) { 423a178015cSYang Shi child_info = shrinker_info_protected(memcg, nid); 424a178015cSYang Shi parent_info = shrinker_info_protected(parent, nid); 425a178015cSYang Shi for (i = 0; i < shrinker_nr_max; i++) { 426a178015cSYang Shi nr = atomic_long_read(&child_info->nr_deferred[i]); 427a178015cSYang Shi atomic_long_add(nr, &parent_info->nr_deferred[i]); 428a178015cSYang Shi } 429a178015cSYang Shi } 430a178015cSYang Shi up_read(&shrinker_rwsem); 431a178015cSYang Shi } 432a178015cSYang Shi 433b5ead35eSJohannes Weiner static bool cgroup_reclaim(struct scan_control *sc) 43489b5fae5SJohannes Weiner { 435b5ead35eSJohannes Weiner return sc->target_mem_cgroup; 43689b5fae5SJohannes Weiner } 43797c9341fSTejun Heo 43897c9341fSTejun Heo /** 439b5ead35eSJohannes Weiner * writeback_throttling_sane - is the usual dirty throttling mechanism available? 44097c9341fSTejun Heo * @sc: scan_control in question 44197c9341fSTejun Heo * 44297c9341fSTejun Heo * The normal page dirty throttling mechanism in balance_dirty_pages() is 44397c9341fSTejun Heo * completely broken with the legacy memcg and direct stalling in 44497c9341fSTejun Heo * shrink_page_list() is used for throttling instead, which lacks all the 44597c9341fSTejun Heo * niceties such as fairness, adaptive pausing, bandwidth proportional 44697c9341fSTejun Heo * allocation and configurability. 44797c9341fSTejun Heo * 44897c9341fSTejun Heo * This function tests whether the vmscan currently in progress can assume 44997c9341fSTejun Heo * that the normal dirty throttling mechanism is operational. 45097c9341fSTejun Heo */ 451b5ead35eSJohannes Weiner static bool writeback_throttling_sane(struct scan_control *sc) 45297c9341fSTejun Heo { 453b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 45497c9341fSTejun Heo return true; 45597c9341fSTejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 45669234aceSLinus Torvalds if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) 45797c9341fSTejun Heo return true; 45897c9341fSTejun Heo #endif 45997c9341fSTejun Heo return false; 46097c9341fSTejun Heo } 46191a45470SKAMEZAWA Hiroyuki #else 4620a432dcbSYang Shi static int prealloc_memcg_shrinker(struct shrinker *shrinker) 4630a432dcbSYang Shi { 464476b30a0SYang Shi return -ENOSYS; 4650a432dcbSYang Shi } 4660a432dcbSYang Shi 4670a432dcbSYang Shi static void unregister_memcg_shrinker(struct shrinker *shrinker) 4680a432dcbSYang Shi { 4690a432dcbSYang Shi } 4700a432dcbSYang Shi 47186750830SYang Shi static long xchg_nr_deferred_memcg(int nid, struct shrinker *shrinker, 47286750830SYang Shi struct mem_cgroup *memcg) 47386750830SYang Shi { 47486750830SYang Shi return 0; 47586750830SYang Shi } 47686750830SYang Shi 47786750830SYang Shi static long add_nr_deferred_memcg(long nr, int nid, struct shrinker *shrinker, 47886750830SYang Shi struct mem_cgroup *memcg) 47986750830SYang Shi { 48086750830SYang Shi return 0; 48186750830SYang Shi } 48286750830SYang Shi 483b5ead35eSJohannes Weiner static bool cgroup_reclaim(struct scan_control *sc) 48489b5fae5SJohannes Weiner { 485b5ead35eSJohannes Weiner return false; 48689b5fae5SJohannes Weiner } 48797c9341fSTejun Heo 488b5ead35eSJohannes Weiner static bool writeback_throttling_sane(struct scan_control *sc) 48997c9341fSTejun Heo { 49097c9341fSTejun Heo return true; 49197c9341fSTejun Heo } 49291a45470SKAMEZAWA Hiroyuki #endif 49391a45470SKAMEZAWA Hiroyuki 49486750830SYang Shi static long xchg_nr_deferred(struct shrinker *shrinker, 49586750830SYang Shi struct shrink_control *sc) 49686750830SYang Shi { 49786750830SYang Shi int nid = sc->nid; 49886750830SYang Shi 49986750830SYang Shi if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) 50086750830SYang Shi nid = 0; 50186750830SYang Shi 50286750830SYang Shi if (sc->memcg && 50386750830SYang Shi (shrinker->flags & SHRINKER_MEMCG_AWARE)) 50486750830SYang Shi return xchg_nr_deferred_memcg(nid, shrinker, 50586750830SYang Shi sc->memcg); 50686750830SYang Shi 50786750830SYang Shi return atomic_long_xchg(&shrinker->nr_deferred[nid], 0); 50886750830SYang Shi } 50986750830SYang Shi 51086750830SYang Shi 51186750830SYang Shi static long add_nr_deferred(long nr, struct shrinker *shrinker, 51286750830SYang Shi struct shrink_control *sc) 51386750830SYang Shi { 51486750830SYang Shi int nid = sc->nid; 51586750830SYang Shi 51686750830SYang Shi if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) 51786750830SYang Shi nid = 0; 51886750830SYang Shi 51986750830SYang Shi if (sc->memcg && 52086750830SYang Shi (shrinker->flags & SHRINKER_MEMCG_AWARE)) 52186750830SYang Shi return add_nr_deferred_memcg(nr, nid, shrinker, 52286750830SYang Shi sc->memcg); 52386750830SYang Shi 52486750830SYang Shi return atomic_long_add_return(nr, &shrinker->nr_deferred[nid]); 52586750830SYang Shi } 52686750830SYang Shi 52726aa2d19SDave Hansen static bool can_demote(int nid, struct scan_control *sc) 52826aa2d19SDave Hansen { 52920b51af1SHuang Ying if (!numa_demotion_enabled) 53020b51af1SHuang Ying return false; 531*3f1509c5SJohannes Weiner if (sc && sc->no_demotion) 53226aa2d19SDave Hansen return false; 53326aa2d19SDave Hansen if (next_demotion_node(nid) == NUMA_NO_NODE) 53426aa2d19SDave Hansen return false; 53526aa2d19SDave Hansen 53620b51af1SHuang Ying return true; 53726aa2d19SDave Hansen } 53826aa2d19SDave Hansen 539a2a36488SKeith Busch static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg, 540a2a36488SKeith Busch int nid, 541a2a36488SKeith Busch struct scan_control *sc) 542a2a36488SKeith Busch { 543a2a36488SKeith Busch if (memcg == NULL) { 544a2a36488SKeith Busch /* 545a2a36488SKeith Busch * For non-memcg reclaim, is there 546a2a36488SKeith Busch * space in any swap device? 547a2a36488SKeith Busch */ 548a2a36488SKeith Busch if (get_nr_swap_pages() > 0) 549a2a36488SKeith Busch return true; 550a2a36488SKeith Busch } else { 551a2a36488SKeith Busch /* Is the memcg below its swap limit? */ 552a2a36488SKeith Busch if (mem_cgroup_get_nr_swap_pages(memcg) > 0) 553a2a36488SKeith Busch return true; 554a2a36488SKeith Busch } 555a2a36488SKeith Busch 556a2a36488SKeith Busch /* 557a2a36488SKeith Busch * The page can not be swapped. 558a2a36488SKeith Busch * 559a2a36488SKeith Busch * Can it be reclaimed from this node via demotion? 560a2a36488SKeith Busch */ 561a2a36488SKeith Busch return can_demote(nid, sc); 562a2a36488SKeith Busch } 563a2a36488SKeith Busch 5645a1c84b4SMel Gorman /* 5655a1c84b4SMel Gorman * This misses isolated pages which are not accounted for to save counters. 5665a1c84b4SMel Gorman * As the data only determines if reclaim or compaction continues, it is 5675a1c84b4SMel Gorman * not expected that isolated pages will be a dominating factor. 5685a1c84b4SMel Gorman */ 5695a1c84b4SMel Gorman unsigned long zone_reclaimable_pages(struct zone *zone) 5705a1c84b4SMel Gorman { 5715a1c84b4SMel Gorman unsigned long nr; 5725a1c84b4SMel Gorman 5735a1c84b4SMel Gorman nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) + 5745a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE); 575a2a36488SKeith Busch if (can_reclaim_anon_pages(NULL, zone_to_nid(zone), NULL)) 5765a1c84b4SMel Gorman nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) + 5775a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON); 5785a1c84b4SMel Gorman 5795a1c84b4SMel Gorman return nr; 5805a1c84b4SMel Gorman } 5815a1c84b4SMel Gorman 582fd538803SMichal Hocko /** 583fd538803SMichal Hocko * lruvec_lru_size - Returns the number of pages on the given LRU list. 584fd538803SMichal Hocko * @lruvec: lru vector 585fd538803SMichal Hocko * @lru: lru to use 5868b3a899aSWei Yang * @zone_idx: zones to consider (use MAX_NR_ZONES - 1 for the whole LRU list) 587fd538803SMichal Hocko */ 5882091339dSYu Zhao static unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, 5892091339dSYu Zhao int zone_idx) 590c9f299d9SKOSAKI Motohiro { 591de3b0150SJohannes Weiner unsigned long size = 0; 592fd538803SMichal Hocko int zid; 593a3d8e054SKOSAKI Motohiro 5948b3a899aSWei Yang for (zid = 0; zid <= zone_idx; zid++) { 595fd538803SMichal Hocko struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; 596fd538803SMichal Hocko 597fd538803SMichal Hocko if (!managed_zone(zone)) 598fd538803SMichal Hocko continue; 599fd538803SMichal Hocko 600fd538803SMichal Hocko if (!mem_cgroup_disabled()) 601de3b0150SJohannes Weiner size += mem_cgroup_get_zone_lru_size(lruvec, lru, zid); 602fd538803SMichal Hocko else 603de3b0150SJohannes Weiner size += zone_page_state(zone, NR_ZONE_LRU_BASE + lru); 604c9f299d9SKOSAKI Motohiro } 605de3b0150SJohannes Weiner return size; 606b4536f0cSMichal Hocko } 607b4536f0cSMichal Hocko 6081da177e4SLinus Torvalds /* 6091d3d4437SGlauber Costa * Add a shrinker callback to be called from the vm. 6101da177e4SLinus Torvalds */ 6118e04944fSTetsuo Handa int prealloc_shrinker(struct shrinker *shrinker) 6121da177e4SLinus Torvalds { 613476b30a0SYang Shi unsigned int size; 614476b30a0SYang Shi int err; 6151d3d4437SGlauber Costa 616476b30a0SYang Shi if (shrinker->flags & SHRINKER_MEMCG_AWARE) { 617476b30a0SYang Shi err = prealloc_memcg_shrinker(shrinker); 618476b30a0SYang Shi if (err != -ENOSYS) 619476b30a0SYang Shi return err; 620476b30a0SYang Shi 621476b30a0SYang Shi shrinker->flags &= ~SHRINKER_MEMCG_AWARE; 622476b30a0SYang Shi } 623476b30a0SYang Shi 624476b30a0SYang Shi size = sizeof(*shrinker->nr_deferred); 6251d3d4437SGlauber Costa if (shrinker->flags & SHRINKER_NUMA_AWARE) 6261d3d4437SGlauber Costa size *= nr_node_ids; 6271d3d4437SGlauber Costa 6281d3d4437SGlauber Costa shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); 6291d3d4437SGlauber Costa if (!shrinker->nr_deferred) 6301d3d4437SGlauber Costa return -ENOMEM; 631b4c2b231SKirill Tkhai 6328e04944fSTetsuo Handa return 0; 6338e04944fSTetsuo Handa } 6341d3d4437SGlauber Costa 6358e04944fSTetsuo Handa void free_prealloced_shrinker(struct shrinker *shrinker) 6368e04944fSTetsuo Handa { 63741ca668aSYang Shi if (shrinker->flags & SHRINKER_MEMCG_AWARE) { 63841ca668aSYang Shi down_write(&shrinker_rwsem); 639b4c2b231SKirill Tkhai unregister_memcg_shrinker(shrinker); 64041ca668aSYang Shi up_write(&shrinker_rwsem); 641476b30a0SYang Shi return; 64241ca668aSYang Shi } 643b4c2b231SKirill Tkhai 6448e04944fSTetsuo Handa kfree(shrinker->nr_deferred); 6458e04944fSTetsuo Handa shrinker->nr_deferred = NULL; 6468e04944fSTetsuo Handa } 6478e04944fSTetsuo Handa 6488e04944fSTetsuo Handa void register_shrinker_prepared(struct shrinker *shrinker) 6498e04944fSTetsuo Handa { 6501da177e4SLinus Torvalds down_write(&shrinker_rwsem); 6511da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 65241ca668aSYang Shi shrinker->flags |= SHRINKER_REGISTERED; 6531da177e4SLinus Torvalds up_write(&shrinker_rwsem); 6548e04944fSTetsuo Handa } 6558e04944fSTetsuo Handa 6568e04944fSTetsuo Handa int register_shrinker(struct shrinker *shrinker) 6578e04944fSTetsuo Handa { 6588e04944fSTetsuo Handa int err = prealloc_shrinker(shrinker); 6598e04944fSTetsuo Handa 6608e04944fSTetsuo Handa if (err) 6618e04944fSTetsuo Handa return err; 6628e04944fSTetsuo Handa register_shrinker_prepared(shrinker); 6631d3d4437SGlauber Costa return 0; 6641da177e4SLinus Torvalds } 6658e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds /* 6681da177e4SLinus Torvalds * Remove one 6691da177e4SLinus Torvalds */ 6708e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 6711da177e4SLinus Torvalds { 67241ca668aSYang Shi if (!(shrinker->flags & SHRINKER_REGISTERED)) 673bb422a73STetsuo Handa return; 67441ca668aSYang Shi 6751da177e4SLinus Torvalds down_write(&shrinker_rwsem); 6761da177e4SLinus Torvalds list_del(&shrinker->list); 67741ca668aSYang Shi shrinker->flags &= ~SHRINKER_REGISTERED; 67841ca668aSYang Shi if (shrinker->flags & SHRINKER_MEMCG_AWARE) 67941ca668aSYang Shi unregister_memcg_shrinker(shrinker); 6801da177e4SLinus Torvalds up_write(&shrinker_rwsem); 68141ca668aSYang Shi 682ae393321SAndrew Vagin kfree(shrinker->nr_deferred); 683bb422a73STetsuo Handa shrinker->nr_deferred = NULL; 6841da177e4SLinus Torvalds } 6858e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 6861da177e4SLinus Torvalds 687880121beSChristian König /** 688880121beSChristian König * synchronize_shrinkers - Wait for all running shrinkers to complete. 689880121beSChristian König * 690880121beSChristian König * This is equivalent to calling unregister_shrink() and register_shrinker(), 691880121beSChristian König * but atomically and with less overhead. This is useful to guarantee that all 692880121beSChristian König * shrinker invocations have seen an update, before freeing memory, similar to 693880121beSChristian König * rcu. 694880121beSChristian König */ 695880121beSChristian König void synchronize_shrinkers(void) 696880121beSChristian König { 697880121beSChristian König down_write(&shrinker_rwsem); 698880121beSChristian König up_write(&shrinker_rwsem); 699880121beSChristian König } 700880121beSChristian König EXPORT_SYMBOL(synchronize_shrinkers); 701880121beSChristian König 7021da177e4SLinus Torvalds #define SHRINK_BATCH 128 7031d3d4437SGlauber Costa 704cb731d6cSVladimir Davydov static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, 7059092c71bSJosef Bacik struct shrinker *shrinker, int priority) 7061da177e4SLinus Torvalds { 70724f7c6b9SDave Chinner unsigned long freed = 0; 7081da177e4SLinus Torvalds unsigned long long delta; 709635697c6SKonstantin Khlebnikov long total_scan; 710d5bc5fd3SVladimir Davydov long freeable; 711acf92b48SDave Chinner long nr; 712acf92b48SDave Chinner long new_nr; 713e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 714e9299f50SDave Chinner : SHRINK_BATCH; 7155f33a080SShaohua Li long scanned = 0, next_deferred; 7161da177e4SLinus Torvalds 717d5bc5fd3SVladimir Davydov freeable = shrinker->count_objects(shrinker, shrinkctl); 7189b996468SKirill Tkhai if (freeable == 0 || freeable == SHRINK_EMPTY) 7199b996468SKirill Tkhai return freeable; 720635697c6SKonstantin Khlebnikov 721acf92b48SDave Chinner /* 722acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 723acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 724acf92b48SDave Chinner * don't also do this scanning work. 725acf92b48SDave Chinner */ 72686750830SYang Shi nr = xchg_nr_deferred(shrinker, shrinkctl); 727acf92b48SDave Chinner 7284b85afbdSJohannes Weiner if (shrinker->seeks) { 7299092c71bSJosef Bacik delta = freeable >> priority; 7309092c71bSJosef Bacik delta *= 4; 7319092c71bSJosef Bacik do_div(delta, shrinker->seeks); 7324b85afbdSJohannes Weiner } else { 7334b85afbdSJohannes Weiner /* 7344b85afbdSJohannes Weiner * These objects don't require any IO to create. Trim 7354b85afbdSJohannes Weiner * them aggressively under memory pressure to keep 7364b85afbdSJohannes Weiner * them from causing refetches in the IO caches. 7374b85afbdSJohannes Weiner */ 7384b85afbdSJohannes Weiner delta = freeable / 2; 7394b85afbdSJohannes Weiner } 740172b06c3SRoman Gushchin 74118bb473eSYang Shi total_scan = nr >> priority; 742acf92b48SDave Chinner total_scan += delta; 74318bb473eSYang Shi total_scan = min(total_scan, (2 * freeable)); 7441da177e4SLinus Torvalds 74524f7c6b9SDave Chinner trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, 7469092c71bSJosef Bacik freeable, delta, total_scan, priority); 74709576073SDave Chinner 7480b1fb40aSVladimir Davydov /* 7490b1fb40aSVladimir Davydov * Normally, we should not scan less than batch_size objects in one 7500b1fb40aSVladimir Davydov * pass to avoid too frequent shrinker calls, but if the slab has less 7510b1fb40aSVladimir Davydov * than batch_size objects in total and we are really tight on memory, 7520b1fb40aSVladimir Davydov * we will try to reclaim all available objects, otherwise we can end 7530b1fb40aSVladimir Davydov * up failing allocations although there are plenty of reclaimable 7540b1fb40aSVladimir Davydov * objects spread over several slabs with usage less than the 7550b1fb40aSVladimir Davydov * batch_size. 7560b1fb40aSVladimir Davydov * 7570b1fb40aSVladimir Davydov * We detect the "tight on memory" situations by looking at the total 7580b1fb40aSVladimir Davydov * number of objects we want to scan (total_scan). If it is greater 759d5bc5fd3SVladimir Davydov * than the total number of objects on slab (freeable), we must be 7600b1fb40aSVladimir Davydov * scanning at high prio and therefore should try to reclaim as much as 7610b1fb40aSVladimir Davydov * possible. 7620b1fb40aSVladimir Davydov */ 7630b1fb40aSVladimir Davydov while (total_scan >= batch_size || 764d5bc5fd3SVladimir Davydov total_scan >= freeable) { 76524f7c6b9SDave Chinner unsigned long ret; 7660b1fb40aSVladimir Davydov unsigned long nr_to_scan = min(batch_size, total_scan); 7671da177e4SLinus Torvalds 7680b1fb40aSVladimir Davydov shrinkctl->nr_to_scan = nr_to_scan; 769d460acb5SChris Wilson shrinkctl->nr_scanned = nr_to_scan; 77024f7c6b9SDave Chinner ret = shrinker->scan_objects(shrinker, shrinkctl); 77124f7c6b9SDave Chinner if (ret == SHRINK_STOP) 7721da177e4SLinus Torvalds break; 77324f7c6b9SDave Chinner freed += ret; 77424f7c6b9SDave Chinner 775d460acb5SChris Wilson count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned); 776d460acb5SChris Wilson total_scan -= shrinkctl->nr_scanned; 777d460acb5SChris Wilson scanned += shrinkctl->nr_scanned; 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds cond_resched(); 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds 78218bb473eSYang Shi /* 78318bb473eSYang Shi * The deferred work is increased by any new work (delta) that wasn't 78418bb473eSYang Shi * done, decreased by old deferred work that was done now. 78518bb473eSYang Shi * 78618bb473eSYang Shi * And it is capped to two times of the freeable items. 78718bb473eSYang Shi */ 78818bb473eSYang Shi next_deferred = max_t(long, (nr + delta - scanned), 0); 78918bb473eSYang Shi next_deferred = min(next_deferred, (2 * freeable)); 79018bb473eSYang Shi 791acf92b48SDave Chinner /* 792acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 79386750830SYang Shi * manner that handles concurrent updates. 794acf92b48SDave Chinner */ 79586750830SYang Shi new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl); 796acf92b48SDave Chinner 7978efb4b59SYang Shi trace_mm_shrink_slab_end(shrinker, shrinkctl->nid, freed, nr, new_nr, total_scan); 7981d3d4437SGlauber Costa return freed; 7991d3d4437SGlauber Costa } 8001d3d4437SGlauber Costa 8010a432dcbSYang Shi #ifdef CONFIG_MEMCG 802b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 803b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 804b0dedc49SKirill Tkhai { 805e4262c4fSYang Shi struct shrinker_info *info; 806b8e57efaSKirill Tkhai unsigned long ret, freed = 0; 807b8e57efaSKirill Tkhai int i; 808b0dedc49SKirill Tkhai 8090a432dcbSYang Shi if (!mem_cgroup_online(memcg)) 810b0dedc49SKirill Tkhai return 0; 811b0dedc49SKirill Tkhai 812b0dedc49SKirill Tkhai if (!down_read_trylock(&shrinker_rwsem)) 813b0dedc49SKirill Tkhai return 0; 814b0dedc49SKirill Tkhai 815468ab843SYang Shi info = shrinker_info_protected(memcg, nid); 816e4262c4fSYang Shi if (unlikely(!info)) 817b0dedc49SKirill Tkhai goto unlock; 818b0dedc49SKirill Tkhai 819e4262c4fSYang Shi for_each_set_bit(i, info->map, shrinker_nr_max) { 820b0dedc49SKirill Tkhai struct shrink_control sc = { 821b0dedc49SKirill Tkhai .gfp_mask = gfp_mask, 822b0dedc49SKirill Tkhai .nid = nid, 823b0dedc49SKirill Tkhai .memcg = memcg, 824b0dedc49SKirill Tkhai }; 825b0dedc49SKirill Tkhai struct shrinker *shrinker; 826b0dedc49SKirill Tkhai 827b0dedc49SKirill Tkhai shrinker = idr_find(&shrinker_idr, i); 82841ca668aSYang Shi if (unlikely(!shrinker || !(shrinker->flags & SHRINKER_REGISTERED))) { 8297e010df5SKirill Tkhai if (!shrinker) 830e4262c4fSYang Shi clear_bit(i, info->map); 831b0dedc49SKirill Tkhai continue; 832b0dedc49SKirill Tkhai } 833b0dedc49SKirill Tkhai 8340a432dcbSYang Shi /* Call non-slab shrinkers even though kmem is disabled */ 8350a432dcbSYang Shi if (!memcg_kmem_enabled() && 8360a432dcbSYang Shi !(shrinker->flags & SHRINKER_NONSLAB)) 8370a432dcbSYang Shi continue; 8380a432dcbSYang Shi 839b0dedc49SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 840f90280d6SKirill Tkhai if (ret == SHRINK_EMPTY) { 841e4262c4fSYang Shi clear_bit(i, info->map); 842f90280d6SKirill Tkhai /* 843f90280d6SKirill Tkhai * After the shrinker reported that it had no objects to 844f90280d6SKirill Tkhai * free, but before we cleared the corresponding bit in 845f90280d6SKirill Tkhai * the memcg shrinker map, a new object might have been 846f90280d6SKirill Tkhai * added. To make sure, we have the bit set in this 847f90280d6SKirill Tkhai * case, we invoke the shrinker one more time and reset 848f90280d6SKirill Tkhai * the bit if it reports that it is not empty anymore. 849f90280d6SKirill Tkhai * The memory barrier here pairs with the barrier in 8502bfd3637SYang Shi * set_shrinker_bit(): 851f90280d6SKirill Tkhai * 852f90280d6SKirill Tkhai * list_lru_add() shrink_slab_memcg() 853f90280d6SKirill Tkhai * list_add_tail() clear_bit() 854f90280d6SKirill Tkhai * <MB> <MB> 855f90280d6SKirill Tkhai * set_bit() do_shrink_slab() 856f90280d6SKirill Tkhai */ 857f90280d6SKirill Tkhai smp_mb__after_atomic(); 858f90280d6SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 8599b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 8609b996468SKirill Tkhai ret = 0; 861f90280d6SKirill Tkhai else 8622bfd3637SYang Shi set_shrinker_bit(memcg, nid, i); 863f90280d6SKirill Tkhai } 864b0dedc49SKirill Tkhai freed += ret; 865b0dedc49SKirill Tkhai 866b0dedc49SKirill Tkhai if (rwsem_is_contended(&shrinker_rwsem)) { 867b0dedc49SKirill Tkhai freed = freed ? : 1; 868b0dedc49SKirill Tkhai break; 869b0dedc49SKirill Tkhai } 870b0dedc49SKirill Tkhai } 871b0dedc49SKirill Tkhai unlock: 872b0dedc49SKirill Tkhai up_read(&shrinker_rwsem); 873b0dedc49SKirill Tkhai return freed; 874b0dedc49SKirill Tkhai } 8750a432dcbSYang Shi #else /* CONFIG_MEMCG */ 876b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 877b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 878b0dedc49SKirill Tkhai { 879b0dedc49SKirill Tkhai return 0; 880b0dedc49SKirill Tkhai } 8810a432dcbSYang Shi #endif /* CONFIG_MEMCG */ 882b0dedc49SKirill Tkhai 8836b4f7799SJohannes Weiner /** 884cb731d6cSVladimir Davydov * shrink_slab - shrink slab caches 8856b4f7799SJohannes Weiner * @gfp_mask: allocation context 8866b4f7799SJohannes Weiner * @nid: node whose slab caches to target 887cb731d6cSVladimir Davydov * @memcg: memory cgroup whose slab caches to target 8889092c71bSJosef Bacik * @priority: the reclaim priority 8891d3d4437SGlauber Costa * 8906b4f7799SJohannes Weiner * Call the shrink functions to age shrinkable caches. 8911d3d4437SGlauber Costa * 8926b4f7799SJohannes Weiner * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, 8936b4f7799SJohannes Weiner * unaware shrinkers will receive a node id of 0 instead. 8941d3d4437SGlauber Costa * 895aeed1d32SVladimir Davydov * @memcg specifies the memory cgroup to target. Unaware shrinkers 896aeed1d32SVladimir Davydov * are called only if it is the root cgroup. 897cb731d6cSVladimir Davydov * 8989092c71bSJosef Bacik * @priority is sc->priority, we take the number of objects and >> by priority 8999092c71bSJosef Bacik * in order to get the scan target. 9001d3d4437SGlauber Costa * 9016b4f7799SJohannes Weiner * Returns the number of reclaimed slab objects. 9021d3d4437SGlauber Costa */ 903cb731d6cSVladimir Davydov static unsigned long shrink_slab(gfp_t gfp_mask, int nid, 904cb731d6cSVladimir Davydov struct mem_cgroup *memcg, 9059092c71bSJosef Bacik int priority) 9061d3d4437SGlauber Costa { 907b8e57efaSKirill Tkhai unsigned long ret, freed = 0; 9081d3d4437SGlauber Costa struct shrinker *shrinker; 9091d3d4437SGlauber Costa 910fa1e512fSYang Shi /* 911fa1e512fSYang Shi * The root memcg might be allocated even though memcg is disabled 912fa1e512fSYang Shi * via "cgroup_disable=memory" boot parameter. This could make 913fa1e512fSYang Shi * mem_cgroup_is_root() return false, then just run memcg slab 914fa1e512fSYang Shi * shrink, but skip global shrink. This may result in premature 915fa1e512fSYang Shi * oom. 916fa1e512fSYang Shi */ 917fa1e512fSYang Shi if (!mem_cgroup_disabled() && !mem_cgroup_is_root(memcg)) 918b0dedc49SKirill Tkhai return shrink_slab_memcg(gfp_mask, nid, memcg, priority); 919cb731d6cSVladimir Davydov 920e830c63aSTetsuo Handa if (!down_read_trylock(&shrinker_rwsem)) 9211d3d4437SGlauber Costa goto out; 9221d3d4437SGlauber Costa 9231d3d4437SGlauber Costa list_for_each_entry(shrinker, &shrinker_list, list) { 9246b4f7799SJohannes Weiner struct shrink_control sc = { 9256b4f7799SJohannes Weiner .gfp_mask = gfp_mask, 9266b4f7799SJohannes Weiner .nid = nid, 927cb731d6cSVladimir Davydov .memcg = memcg, 9286b4f7799SJohannes Weiner }; 9296b4f7799SJohannes Weiner 9309b996468SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 9319b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 9329b996468SKirill Tkhai ret = 0; 9339b996468SKirill Tkhai freed += ret; 934e496612cSMinchan Kim /* 935e496612cSMinchan Kim * Bail out if someone want to register a new shrinker to 93655b65a57SEthon Paul * prevent the registration from being stalled for long periods 937e496612cSMinchan Kim * by parallel ongoing shrinking. 938e496612cSMinchan Kim */ 939e496612cSMinchan Kim if (rwsem_is_contended(&shrinker_rwsem)) { 940e496612cSMinchan Kim freed = freed ? : 1; 941e496612cSMinchan Kim break; 942e496612cSMinchan Kim } 943ec97097bSVladimir Davydov } 9441d3d4437SGlauber Costa 9451da177e4SLinus Torvalds up_read(&shrinker_rwsem); 946f06590bdSMinchan Kim out: 947f06590bdSMinchan Kim cond_resched(); 94824f7c6b9SDave Chinner return freed; 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds 951e4b424b7SGang Li static void drop_slab_node(int nid) 952cb731d6cSVladimir Davydov { 953cb731d6cSVladimir Davydov unsigned long freed; 9541399af7eSVlastimil Babka int shift = 0; 955cb731d6cSVladimir Davydov 956cb731d6cSVladimir Davydov do { 957cb731d6cSVladimir Davydov struct mem_cgroup *memcg = NULL; 958cb731d6cSVladimir Davydov 959069c411dSChunxin Zang if (fatal_signal_pending(current)) 960069c411dSChunxin Zang return; 961069c411dSChunxin Zang 962cb731d6cSVladimir Davydov freed = 0; 963aeed1d32SVladimir Davydov memcg = mem_cgroup_iter(NULL, NULL, NULL); 964cb731d6cSVladimir Davydov do { 9659092c71bSJosef Bacik freed += shrink_slab(GFP_KERNEL, nid, memcg, 0); 966cb731d6cSVladimir Davydov } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 9671399af7eSVlastimil Babka } while ((freed >> shift++) > 1); 968cb731d6cSVladimir Davydov } 969cb731d6cSVladimir Davydov 970cb731d6cSVladimir Davydov void drop_slab(void) 971cb731d6cSVladimir Davydov { 972cb731d6cSVladimir Davydov int nid; 973cb731d6cSVladimir Davydov 974cb731d6cSVladimir Davydov for_each_online_node(nid) 975cb731d6cSVladimir Davydov drop_slab_node(nid); 976cb731d6cSVladimir Davydov } 977cb731d6cSVladimir Davydov 978e0cd5e7fSMatthew Wilcox (Oracle) static inline int is_page_cache_freeable(struct folio *folio) 9791da177e4SLinus Torvalds { 980ceddc3a5SJohannes Weiner /* 981ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 98267891fffSMatthew Wilcox * that isolated the page, the page cache and optional buffer 98367891fffSMatthew Wilcox * heads at page->private. 984ceddc3a5SJohannes Weiner */ 985e0cd5e7fSMatthew Wilcox (Oracle) return folio_ref_count(folio) - folio_test_private(folio) == 986e0cd5e7fSMatthew Wilcox (Oracle) 1 + folio_nr_pages(folio); 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds /* 990e0cd5e7fSMatthew Wilcox (Oracle) * We detected a synchronous write error writing a folio out. Probably 9911da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 9921da177e4SLinus Torvalds * fsync(), msync() or close(). 9931da177e4SLinus Torvalds * 9941da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 995e0cd5e7fSMatthew Wilcox (Oracle) * prevents it from being freed up. But we have a ref on the folio and once 996e0cd5e7fSMatthew Wilcox (Oracle) * that folio is locked, the mapping is pinned. 9971da177e4SLinus Torvalds * 998e0cd5e7fSMatthew Wilcox (Oracle) * We're allowed to run sleeping folio_lock() here because we know the caller has 9991da177e4SLinus Torvalds * __GFP_FS. 10001da177e4SLinus Torvalds */ 10011da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 1002e0cd5e7fSMatthew Wilcox (Oracle) struct folio *folio, int error) 10031da177e4SLinus Torvalds { 1004e0cd5e7fSMatthew Wilcox (Oracle) folio_lock(folio); 1005e0cd5e7fSMatthew Wilcox (Oracle) if (folio_mapping(folio) == mapping) 10063e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 1007e0cd5e7fSMatthew Wilcox (Oracle) folio_unlock(folio); 10081da177e4SLinus Torvalds } 10091da177e4SLinus Torvalds 10101b4e3f26SMel Gorman static bool skip_throttle_noprogress(pg_data_t *pgdat) 10111b4e3f26SMel Gorman { 10121b4e3f26SMel Gorman int reclaimable = 0, write_pending = 0; 10131b4e3f26SMel Gorman int i; 10141b4e3f26SMel Gorman 10151b4e3f26SMel Gorman /* 10161b4e3f26SMel Gorman * If kswapd is disabled, reschedule if necessary but do not 10171b4e3f26SMel Gorman * throttle as the system is likely near OOM. 10181b4e3f26SMel Gorman */ 10191b4e3f26SMel Gorman if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 10201b4e3f26SMel Gorman return true; 10211b4e3f26SMel Gorman 10221b4e3f26SMel Gorman /* 10231b4e3f26SMel Gorman * If there are a lot of dirty/writeback pages then do not 10241b4e3f26SMel Gorman * throttle as throttling will occur when the pages cycle 10251b4e3f26SMel Gorman * towards the end of the LRU if still under writeback. 10261b4e3f26SMel Gorman */ 10271b4e3f26SMel Gorman for (i = 0; i < MAX_NR_ZONES; i++) { 10281b4e3f26SMel Gorman struct zone *zone = pgdat->node_zones + i; 10291b4e3f26SMel Gorman 103036c26128SWei Yang if (!managed_zone(zone)) 10311b4e3f26SMel Gorman continue; 10321b4e3f26SMel Gorman 10331b4e3f26SMel Gorman reclaimable += zone_reclaimable_pages(zone); 10341b4e3f26SMel Gorman write_pending += zone_page_state_snapshot(zone, 10351b4e3f26SMel Gorman NR_ZONE_WRITE_PENDING); 10361b4e3f26SMel Gorman } 10371b4e3f26SMel Gorman if (2 * write_pending <= reclaimable) 10381b4e3f26SMel Gorman return true; 10391b4e3f26SMel Gorman 10401b4e3f26SMel Gorman return false; 10411b4e3f26SMel Gorman } 10421b4e3f26SMel Gorman 1043c3f4a9a2SMel Gorman void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason) 10448cd7c588SMel Gorman { 10458cd7c588SMel Gorman wait_queue_head_t *wqh = &pgdat->reclaim_wait[reason]; 1046c3f4a9a2SMel Gorman long timeout, ret; 10478cd7c588SMel Gorman DEFINE_WAIT(wait); 10488cd7c588SMel Gorman 10498cd7c588SMel Gorman /* 10508cd7c588SMel Gorman * Do not throttle IO workers, kthreads other than kswapd or 10518cd7c588SMel Gorman * workqueues. They may be required for reclaim to make 10528cd7c588SMel Gorman * forward progress (e.g. journalling workqueues or kthreads). 10538cd7c588SMel Gorman */ 10548cd7c588SMel Gorman if (!current_is_kswapd() && 1055b485c6f1SMel Gorman current->flags & (PF_IO_WORKER|PF_KTHREAD)) { 1056b485c6f1SMel Gorman cond_resched(); 10578cd7c588SMel Gorman return; 1058b485c6f1SMel Gorman } 10598cd7c588SMel Gorman 1060c3f4a9a2SMel Gorman /* 1061c3f4a9a2SMel Gorman * These figures are pulled out of thin air. 1062c3f4a9a2SMel Gorman * VMSCAN_THROTTLE_ISOLATED is a transient condition based on too many 1063c3f4a9a2SMel Gorman * parallel reclaimers which is a short-lived event so the timeout is 1064c3f4a9a2SMel Gorman * short. Failing to make progress or waiting on writeback are 1065c3f4a9a2SMel Gorman * potentially long-lived events so use a longer timeout. This is shaky 1066c3f4a9a2SMel Gorman * logic as a failure to make progress could be due to anything from 1067c3f4a9a2SMel Gorman * writeback to a slow device to excessive references pages at the tail 1068c3f4a9a2SMel Gorman * of the inactive LRU. 1069c3f4a9a2SMel Gorman */ 1070c3f4a9a2SMel Gorman switch(reason) { 1071c3f4a9a2SMel Gorman case VMSCAN_THROTTLE_WRITEBACK: 1072c3f4a9a2SMel Gorman timeout = HZ/10; 1073c3f4a9a2SMel Gorman 1074c3f4a9a2SMel Gorman if (atomic_inc_return(&pgdat->nr_writeback_throttled) == 1) { 10758cd7c588SMel Gorman WRITE_ONCE(pgdat->nr_reclaim_start, 10768cd7c588SMel Gorman node_page_state(pgdat, NR_THROTTLED_WRITTEN)); 10778cd7c588SMel Gorman } 10788cd7c588SMel Gorman 1079c3f4a9a2SMel Gorman break; 10801b4e3f26SMel Gorman case VMSCAN_THROTTLE_CONGESTED: 10811b4e3f26SMel Gorman fallthrough; 1082c3f4a9a2SMel Gorman case VMSCAN_THROTTLE_NOPROGRESS: 10831b4e3f26SMel Gorman if (skip_throttle_noprogress(pgdat)) { 10841b4e3f26SMel Gorman cond_resched(); 10851b4e3f26SMel Gorman return; 10861b4e3f26SMel Gorman } 10871b4e3f26SMel Gorman 10881b4e3f26SMel Gorman timeout = 1; 10891b4e3f26SMel Gorman 1090c3f4a9a2SMel Gorman break; 1091c3f4a9a2SMel Gorman case VMSCAN_THROTTLE_ISOLATED: 1092c3f4a9a2SMel Gorman timeout = HZ/50; 1093c3f4a9a2SMel Gorman break; 1094c3f4a9a2SMel Gorman default: 1095c3f4a9a2SMel Gorman WARN_ON_ONCE(1); 1096c3f4a9a2SMel Gorman timeout = HZ; 1097c3f4a9a2SMel Gorman break; 1098c3f4a9a2SMel Gorman } 1099c3f4a9a2SMel Gorman 11008cd7c588SMel Gorman prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); 11018cd7c588SMel Gorman ret = schedule_timeout(timeout); 11028cd7c588SMel Gorman finish_wait(wqh, &wait); 1103d818fca1SMel Gorman 1104c3f4a9a2SMel Gorman if (reason == VMSCAN_THROTTLE_WRITEBACK) 11058cd7c588SMel Gorman atomic_dec(&pgdat->nr_writeback_throttled); 11068cd7c588SMel Gorman 11078cd7c588SMel Gorman trace_mm_vmscan_throttled(pgdat->node_id, jiffies_to_usecs(timeout), 11088cd7c588SMel Gorman jiffies_to_usecs(timeout - ret), 11098cd7c588SMel Gorman reason); 11108cd7c588SMel Gorman } 11118cd7c588SMel Gorman 11128cd7c588SMel Gorman /* 11138cd7c588SMel Gorman * Account for pages written if tasks are throttled waiting on dirty 11148cd7c588SMel Gorman * pages to clean. If enough pages have been cleaned since throttling 11158cd7c588SMel Gorman * started then wakeup the throttled tasks. 11168cd7c588SMel Gorman */ 1117512b7931SLinus Torvalds void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio, 11188cd7c588SMel Gorman int nr_throttled) 11198cd7c588SMel Gorman { 11208cd7c588SMel Gorman unsigned long nr_written; 11218cd7c588SMel Gorman 1122512b7931SLinus Torvalds node_stat_add_folio(folio, NR_THROTTLED_WRITTEN); 11238cd7c588SMel Gorman 11248cd7c588SMel Gorman /* 11258cd7c588SMel Gorman * This is an inaccurate read as the per-cpu deltas may not 11268cd7c588SMel Gorman * be synchronised. However, given that the system is 11278cd7c588SMel Gorman * writeback throttled, it is not worth taking the penalty 11288cd7c588SMel Gorman * of getting an accurate count. At worst, the throttle 11298cd7c588SMel Gorman * timeout guarantees forward progress. 11308cd7c588SMel Gorman */ 11318cd7c588SMel Gorman nr_written = node_page_state(pgdat, NR_THROTTLED_WRITTEN) - 11328cd7c588SMel Gorman READ_ONCE(pgdat->nr_reclaim_start); 11338cd7c588SMel Gorman 11348cd7c588SMel Gorman if (nr_written > SWAP_CLUSTER_MAX * nr_throttled) 11358cd7c588SMel Gorman wake_up(&pgdat->reclaim_wait[VMSCAN_THROTTLE_WRITEBACK]); 11368cd7c588SMel Gorman } 11378cd7c588SMel Gorman 113804e62a29SChristoph Lameter /* possible outcome of pageout() */ 113904e62a29SChristoph Lameter typedef enum { 114004e62a29SChristoph Lameter /* failed to write page out, page is locked */ 114104e62a29SChristoph Lameter PAGE_KEEP, 114204e62a29SChristoph Lameter /* move page to the active list, page is locked */ 114304e62a29SChristoph Lameter PAGE_ACTIVATE, 114404e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 114504e62a29SChristoph Lameter PAGE_SUCCESS, 114604e62a29SChristoph Lameter /* page is clean and locked */ 114704e62a29SChristoph Lameter PAGE_CLEAN, 114804e62a29SChristoph Lameter } pageout_t; 114904e62a29SChristoph Lameter 11501da177e4SLinus Torvalds /* 11511742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 11521742f19fSAndrew Morton * Calls ->writepage(). 11531da177e4SLinus Torvalds */ 11542282679fSNeilBrown static pageout_t pageout(struct folio *folio, struct address_space *mapping, 11552282679fSNeilBrown struct swap_iocb **plug) 11561da177e4SLinus Torvalds { 11571da177e4SLinus Torvalds /* 1158e0cd5e7fSMatthew Wilcox (Oracle) * If the folio is dirty, only perform writeback if that write 11591da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 11601da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 11611da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 11621da177e4SLinus Torvalds * PagePrivate for that. 11631da177e4SLinus Torvalds * 11648174202bSAl Viro * If this process is currently in __generic_file_write_iter() against 1165e0cd5e7fSMatthew Wilcox (Oracle) * this folio's queue, we can perform writeback even if that 11661da177e4SLinus Torvalds * will block. 11671da177e4SLinus Torvalds * 1168e0cd5e7fSMatthew Wilcox (Oracle) * If the folio is swapcache, write it back even if that would 11691da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 11701da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 11711da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 11721da177e4SLinus Torvalds */ 1173e0cd5e7fSMatthew Wilcox (Oracle) if (!is_page_cache_freeable(folio)) 11741da177e4SLinus Torvalds return PAGE_KEEP; 11751da177e4SLinus Torvalds if (!mapping) { 11761da177e4SLinus Torvalds /* 1177e0cd5e7fSMatthew Wilcox (Oracle) * Some data journaling orphaned folios can have 1178e0cd5e7fSMatthew Wilcox (Oracle) * folio->mapping == NULL while being dirty with clean buffers. 11791da177e4SLinus Torvalds */ 1180e0cd5e7fSMatthew Wilcox (Oracle) if (folio_test_private(folio)) { 1181e0cd5e7fSMatthew Wilcox (Oracle) if (try_to_free_buffers(&folio->page)) { 1182e0cd5e7fSMatthew Wilcox (Oracle) folio_clear_dirty(folio); 1183e0cd5e7fSMatthew Wilcox (Oracle) pr_info("%s: orphaned folio\n", __func__); 11841da177e4SLinus Torvalds return PAGE_CLEAN; 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds return PAGE_KEEP; 11881da177e4SLinus Torvalds } 11891da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 11901da177e4SLinus Torvalds return PAGE_ACTIVATE; 11911da177e4SLinus Torvalds 1192e0cd5e7fSMatthew Wilcox (Oracle) if (folio_clear_dirty_for_io(folio)) { 11931da177e4SLinus Torvalds int res; 11941da177e4SLinus Torvalds struct writeback_control wbc = { 11951da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 11961da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 1197111ebb6eSOGAWA Hirofumi .range_start = 0, 1198111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 11991da177e4SLinus Torvalds .for_reclaim = 1, 12002282679fSNeilBrown .swap_plug = plug, 12011da177e4SLinus Torvalds }; 12021da177e4SLinus Torvalds 1203e0cd5e7fSMatthew Wilcox (Oracle) folio_set_reclaim(folio); 1204e0cd5e7fSMatthew Wilcox (Oracle) res = mapping->a_ops->writepage(&folio->page, &wbc); 12051da177e4SLinus Torvalds if (res < 0) 1206e0cd5e7fSMatthew Wilcox (Oracle) handle_write_error(mapping, folio, res); 1207994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 1208e0cd5e7fSMatthew Wilcox (Oracle) folio_clear_reclaim(folio); 12091da177e4SLinus Torvalds return PAGE_ACTIVATE; 12101da177e4SLinus Torvalds } 1211c661b078SAndy Whitcroft 1212e0cd5e7fSMatthew Wilcox (Oracle) if (!folio_test_writeback(folio)) { 12131da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 1214e0cd5e7fSMatthew Wilcox (Oracle) folio_clear_reclaim(folio); 12151da177e4SLinus Torvalds } 1216e0cd5e7fSMatthew Wilcox (Oracle) trace_mm_vmscan_write_folio(folio); 1217e0cd5e7fSMatthew Wilcox (Oracle) node_stat_add_folio(folio, NR_VMSCAN_WRITE); 12181da177e4SLinus Torvalds return PAGE_SUCCESS; 12191da177e4SLinus Torvalds } 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds return PAGE_CLEAN; 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds 1224a649fd92SAndrew Morton /* 1225e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 1226e286781dSNick Piggin * gets returned with a refcount of 0. 1227a649fd92SAndrew Morton */ 1228be7c07d6SMatthew Wilcox (Oracle) static int __remove_mapping(struct address_space *mapping, struct folio *folio, 1229b910718aSJohannes Weiner bool reclaimed, struct mem_cgroup *target_memcg) 123049d2e9ccSChristoph Lameter { 1231bd4c82c2SHuang Ying int refcount; 1232aae466b0SJoonsoo Kim void *shadow = NULL; 1233c4843a75SGreg Thelen 1234be7c07d6SMatthew Wilcox (Oracle) BUG_ON(!folio_test_locked(folio)); 1235be7c07d6SMatthew Wilcox (Oracle) BUG_ON(mapping != folio_mapping(folio)); 123649d2e9ccSChristoph Lameter 1237be7c07d6SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio)) 123851b8c1feSJohannes Weiner spin_lock(&mapping->host->i_lock); 123930472509SJohannes Weiner xa_lock_irq(&mapping->i_pages); 124049d2e9ccSChristoph Lameter /* 12410fd0e6b0SNick Piggin * The non racy check for a busy page. 12420fd0e6b0SNick Piggin * 12430fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 12440fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 12450fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 12460fd0e6b0SNick Piggin * here, then the following race may occur: 12470fd0e6b0SNick Piggin * 12480fd0e6b0SNick Piggin * get_user_pages(&page); 12490fd0e6b0SNick Piggin * [user mapping goes away] 12500fd0e6b0SNick Piggin * write_to(page); 12510fd0e6b0SNick Piggin * !PageDirty(page) [good] 12520fd0e6b0SNick Piggin * SetPageDirty(page); 12530fd0e6b0SNick Piggin * put_page(page); 12540fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 12550fd0e6b0SNick Piggin * 12560fd0e6b0SNick Piggin * [oops, our write_to data is lost] 12570fd0e6b0SNick Piggin * 12580fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 12590fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 12600139aa7bSJoonsoo Kim * load is not satisfied before that of page->_refcount. 12610fd0e6b0SNick Piggin * 12620fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 1263b93b0163SMatthew Wilcox * and thus under the i_pages lock, then this ordering is not required. 126449d2e9ccSChristoph Lameter */ 1265be7c07d6SMatthew Wilcox (Oracle) refcount = 1 + folio_nr_pages(folio); 1266be7c07d6SMatthew Wilcox (Oracle) if (!folio_ref_freeze(folio, refcount)) 126749d2e9ccSChristoph Lameter goto cannot_free; 12681c4c3b99SJiang Biao /* note: atomic_cmpxchg in page_ref_freeze provides the smp_rmb */ 1269be7c07d6SMatthew Wilcox (Oracle) if (unlikely(folio_test_dirty(folio))) { 1270be7c07d6SMatthew Wilcox (Oracle) folio_ref_unfreeze(folio, refcount); 127149d2e9ccSChristoph Lameter goto cannot_free; 1272e286781dSNick Piggin } 127349d2e9ccSChristoph Lameter 1274be7c07d6SMatthew Wilcox (Oracle) if (folio_test_swapcache(folio)) { 1275be7c07d6SMatthew Wilcox (Oracle) swp_entry_t swap = folio_swap_entry(folio); 12763ecb0087SMatthew Wilcox (Oracle) mem_cgroup_swapout(folio, swap); 1277aae466b0SJoonsoo Kim if (reclaimed && !mapping_exiting(mapping)) 12788927f647SMatthew Wilcox (Oracle) shadow = workingset_eviction(folio, target_memcg); 1279be7c07d6SMatthew Wilcox (Oracle) __delete_from_swap_cache(&folio->page, swap, shadow); 128030472509SJohannes Weiner xa_unlock_irq(&mapping->i_pages); 1281be7c07d6SMatthew Wilcox (Oracle) put_swap_page(&folio->page, swap); 1282e286781dSNick Piggin } else { 12836072d13cSLinus Torvalds void (*freepage)(struct page *); 12846072d13cSLinus Torvalds 12856072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 1286a528910eSJohannes Weiner /* 1287a528910eSJohannes Weiner * Remember a shadow entry for reclaimed file cache in 1288a528910eSJohannes Weiner * order to detect refaults, thus thrashing, later on. 1289a528910eSJohannes Weiner * 1290a528910eSJohannes Weiner * But don't store shadows in an address space that is 1291238c3046Sdylan-meiners * already exiting. This is not just an optimization, 1292a528910eSJohannes Weiner * inode reclaim needs to empty out the radix tree or 1293a528910eSJohannes Weiner * the nodes are lost. Don't plant shadows behind its 1294a528910eSJohannes Weiner * back. 1295f9fe48beSRoss Zwisler * 1296f9fe48beSRoss Zwisler * We also don't store shadows for DAX mappings because the 1297f9fe48beSRoss Zwisler * only page cache pages found in these are zero pages 1298f9fe48beSRoss Zwisler * covering holes, and because we don't want to mix DAX 1299f9fe48beSRoss Zwisler * exceptional entries and shadow exceptional entries in the 1300b93b0163SMatthew Wilcox * same address_space. 1301a528910eSJohannes Weiner */ 1302be7c07d6SMatthew Wilcox (Oracle) if (reclaimed && folio_is_file_lru(folio) && 1303f9fe48beSRoss Zwisler !mapping_exiting(mapping) && !dax_mapping(mapping)) 13048927f647SMatthew Wilcox (Oracle) shadow = workingset_eviction(folio, target_memcg); 13058927f647SMatthew Wilcox (Oracle) __filemap_remove_folio(folio, shadow); 130630472509SJohannes Weiner xa_unlock_irq(&mapping->i_pages); 130751b8c1feSJohannes Weiner if (mapping_shrinkable(mapping)) 130851b8c1feSJohannes Weiner inode_add_lru(mapping->host); 130951b8c1feSJohannes Weiner spin_unlock(&mapping->host->i_lock); 13106072d13cSLinus Torvalds 13116072d13cSLinus Torvalds if (freepage != NULL) 1312be7c07d6SMatthew Wilcox (Oracle) freepage(&folio->page); 1313e286781dSNick Piggin } 1314e286781dSNick Piggin 131549d2e9ccSChristoph Lameter return 1; 131649d2e9ccSChristoph Lameter 131749d2e9ccSChristoph Lameter cannot_free: 131830472509SJohannes Weiner xa_unlock_irq(&mapping->i_pages); 1319be7c07d6SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio)) 132051b8c1feSJohannes Weiner spin_unlock(&mapping->host->i_lock); 132149d2e9ccSChristoph Lameter return 0; 132249d2e9ccSChristoph Lameter } 132349d2e9ccSChristoph Lameter 13245100da38SMatthew Wilcox (Oracle) /** 13255100da38SMatthew Wilcox (Oracle) * remove_mapping() - Attempt to remove a folio from its mapping. 13265100da38SMatthew Wilcox (Oracle) * @mapping: The address space. 13275100da38SMatthew Wilcox (Oracle) * @folio: The folio to remove. 13285100da38SMatthew Wilcox (Oracle) * 13295100da38SMatthew Wilcox (Oracle) * If the folio is dirty, under writeback or if someone else has a ref 13305100da38SMatthew Wilcox (Oracle) * on it, removal will fail. 13315100da38SMatthew Wilcox (Oracle) * Return: The number of pages removed from the mapping. 0 if the folio 13325100da38SMatthew Wilcox (Oracle) * could not be removed. 13335100da38SMatthew Wilcox (Oracle) * Context: The caller should have a single refcount on the folio and 13345100da38SMatthew Wilcox (Oracle) * hold its lock. 1335e286781dSNick Piggin */ 13365100da38SMatthew Wilcox (Oracle) long remove_mapping(struct address_space *mapping, struct folio *folio) 1337e286781dSNick Piggin { 1338be7c07d6SMatthew Wilcox (Oracle) if (__remove_mapping(mapping, folio, false, NULL)) { 1339e286781dSNick Piggin /* 13405100da38SMatthew Wilcox (Oracle) * Unfreezing the refcount with 1 effectively 1341e286781dSNick Piggin * drops the pagecache ref for us without requiring another 1342e286781dSNick Piggin * atomic operation. 1343e286781dSNick Piggin */ 1344be7c07d6SMatthew Wilcox (Oracle) folio_ref_unfreeze(folio, 1); 13455100da38SMatthew Wilcox (Oracle) return folio_nr_pages(folio); 1346e286781dSNick Piggin } 1347e286781dSNick Piggin return 0; 1348e286781dSNick Piggin } 1349e286781dSNick Piggin 1350894bc310SLee Schermerhorn /** 1351ca6d60f3SMatthew Wilcox (Oracle) * folio_putback_lru - Put previously isolated folio onto appropriate LRU list. 1352ca6d60f3SMatthew Wilcox (Oracle) * @folio: Folio to be returned to an LRU list. 1353894bc310SLee Schermerhorn * 1354ca6d60f3SMatthew Wilcox (Oracle) * Add previously isolated @folio to appropriate LRU list. 1355ca6d60f3SMatthew Wilcox (Oracle) * The folio may still be unevictable for other reasons. 1356894bc310SLee Schermerhorn * 1357ca6d60f3SMatthew Wilcox (Oracle) * Context: lru_lock must not be held, interrupts must be enabled. 1358894bc310SLee Schermerhorn */ 1359ca6d60f3SMatthew Wilcox (Oracle) void folio_putback_lru(struct folio *folio) 1360894bc310SLee Schermerhorn { 1361ca6d60f3SMatthew Wilcox (Oracle) folio_add_lru(folio); 1362ca6d60f3SMatthew Wilcox (Oracle) folio_put(folio); /* drop ref from isolate */ 1363894bc310SLee Schermerhorn } 1364894bc310SLee Schermerhorn 1365dfc8d636SJohannes Weiner enum page_references { 1366dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 1367dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 136864574746SJohannes Weiner PAGEREF_KEEP, 1369dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 1370dfc8d636SJohannes Weiner }; 1371dfc8d636SJohannes Weiner 1372d92013d1SMatthew Wilcox (Oracle) static enum page_references folio_check_references(struct folio *folio, 1373dfc8d636SJohannes Weiner struct scan_control *sc) 1374dfc8d636SJohannes Weiner { 1375d92013d1SMatthew Wilcox (Oracle) int referenced_ptes, referenced_folio; 1376dfc8d636SJohannes Weiner unsigned long vm_flags; 1377dfc8d636SJohannes Weiner 1378b3ac0413SMatthew Wilcox (Oracle) referenced_ptes = folio_referenced(folio, 1, sc->target_mem_cgroup, 1379c3ac9a8aSJohannes Weiner &vm_flags); 1380d92013d1SMatthew Wilcox (Oracle) referenced_folio = folio_test_clear_referenced(folio); 1381dfc8d636SJohannes Weiner 1382dfc8d636SJohannes Weiner /* 1383d92013d1SMatthew Wilcox (Oracle) * The supposedly reclaimable folio was found to be in a VM_LOCKED vma. 1384d92013d1SMatthew Wilcox (Oracle) * Let the folio, now marked Mlocked, be moved to the unevictable list. 1385dfc8d636SJohannes Weiner */ 1386dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 138747d4f3eeSHugh Dickins return PAGEREF_ACTIVATE; 1388dfc8d636SJohannes Weiner 13896d4675e6SMinchan Kim /* rmap lock contention: rotate */ 13906d4675e6SMinchan Kim if (referenced_ptes == -1) 13916d4675e6SMinchan Kim return PAGEREF_KEEP; 13926d4675e6SMinchan Kim 139364574746SJohannes Weiner if (referenced_ptes) { 139464574746SJohannes Weiner /* 1395d92013d1SMatthew Wilcox (Oracle) * All mapped folios start out with page table 139664574746SJohannes Weiner * references from the instantiating fault, so we need 13979030fb0bSLinus Torvalds * to look twice if a mapped file/anon folio is used more 139864574746SJohannes Weiner * than once. 139964574746SJohannes Weiner * 140064574746SJohannes Weiner * Mark it and spare it for another trip around the 140164574746SJohannes Weiner * inactive list. Another page table reference will 140264574746SJohannes Weiner * lead to its activation. 140364574746SJohannes Weiner * 1404d92013d1SMatthew Wilcox (Oracle) * Note: the mark is set for activated folios as well 1405d92013d1SMatthew Wilcox (Oracle) * so that recently deactivated but used folios are 140664574746SJohannes Weiner * quickly recovered. 140764574746SJohannes Weiner */ 1408d92013d1SMatthew Wilcox (Oracle) folio_set_referenced(folio); 140964574746SJohannes Weiner 1410d92013d1SMatthew Wilcox (Oracle) if (referenced_folio || referenced_ptes > 1) 1411dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 1412dfc8d636SJohannes Weiner 1413c909e993SKonstantin Khlebnikov /* 1414d92013d1SMatthew Wilcox (Oracle) * Activate file-backed executable folios after first usage. 1415c909e993SKonstantin Khlebnikov */ 1416f19a27e3SMiaohe Lin if ((vm_flags & VM_EXEC) && folio_is_file_lru(folio)) 1417c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 1418c909e993SKonstantin Khlebnikov 141964574746SJohannes Weiner return PAGEREF_KEEP; 142064574746SJohannes Weiner } 142164574746SJohannes Weiner 1422d92013d1SMatthew Wilcox (Oracle) /* Reclaim if clean, defer dirty folios to writeback */ 1423f19a27e3SMiaohe Lin if (referenced_folio && folio_is_file_lru(folio)) 1424dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 142564574746SJohannes Weiner 142664574746SJohannes Weiner return PAGEREF_RECLAIM; 1427dfc8d636SJohannes Weiner } 1428dfc8d636SJohannes Weiner 1429e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 1430e20c41b1SMatthew Wilcox (Oracle) static void folio_check_dirty_writeback(struct folio *folio, 1431e2be15f6SMel Gorman bool *dirty, bool *writeback) 1432e2be15f6SMel Gorman { 1433b4597226SMel Gorman struct address_space *mapping; 1434b4597226SMel Gorman 1435e2be15f6SMel Gorman /* 1436e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 143732a331a7SMiaohe Lin * from reclaim context. Do not stall reclaim based on them. 143832a331a7SMiaohe Lin * MADV_FREE anonymous pages are put into inactive file list too. 143932a331a7SMiaohe Lin * They could be mistakenly treated as file lru. So further anon 144032a331a7SMiaohe Lin * test is needed. 1441e2be15f6SMel Gorman */ 1442e20c41b1SMatthew Wilcox (Oracle) if (!folio_is_file_lru(folio) || 1443e20c41b1SMatthew Wilcox (Oracle) (folio_test_anon(folio) && !folio_test_swapbacked(folio))) { 1444e2be15f6SMel Gorman *dirty = false; 1445e2be15f6SMel Gorman *writeback = false; 1446e2be15f6SMel Gorman return; 1447e2be15f6SMel Gorman } 1448e2be15f6SMel Gorman 1449e20c41b1SMatthew Wilcox (Oracle) /* By default assume that the folio flags are accurate */ 1450e20c41b1SMatthew Wilcox (Oracle) *dirty = folio_test_dirty(folio); 1451e20c41b1SMatthew Wilcox (Oracle) *writeback = folio_test_writeback(folio); 1452b4597226SMel Gorman 1453b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 1454e20c41b1SMatthew Wilcox (Oracle) if (!folio_test_private(folio)) 1455b4597226SMel Gorman return; 1456b4597226SMel Gorman 1457e20c41b1SMatthew Wilcox (Oracle) mapping = folio_mapping(folio); 1458b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 1459e20c41b1SMatthew Wilcox (Oracle) mapping->a_ops->is_dirty_writeback(&folio->page, dirty, writeback); 1460e2be15f6SMel Gorman } 1461e2be15f6SMel Gorman 146226aa2d19SDave Hansen static struct page *alloc_demote_page(struct page *page, unsigned long node) 146326aa2d19SDave Hansen { 146426aa2d19SDave Hansen struct migration_target_control mtc = { 146526aa2d19SDave Hansen /* 146626aa2d19SDave Hansen * Allocate from 'node', or fail quickly and quietly. 146726aa2d19SDave Hansen * When this happens, 'page' will likely just be discarded 146826aa2d19SDave Hansen * instead of migrated. 146926aa2d19SDave Hansen */ 147026aa2d19SDave Hansen .gfp_mask = (GFP_HIGHUSER_MOVABLE & ~__GFP_RECLAIM) | 147126aa2d19SDave Hansen __GFP_THISNODE | __GFP_NOWARN | 147226aa2d19SDave Hansen __GFP_NOMEMALLOC | GFP_NOWAIT, 147326aa2d19SDave Hansen .nid = node 147426aa2d19SDave Hansen }; 147526aa2d19SDave Hansen 147626aa2d19SDave Hansen return alloc_migration_target(page, (unsigned long)&mtc); 147726aa2d19SDave Hansen } 147826aa2d19SDave Hansen 147926aa2d19SDave Hansen /* 148026aa2d19SDave Hansen * Take pages on @demote_list and attempt to demote them to 148126aa2d19SDave Hansen * another node. Pages which are not demoted are left on 148226aa2d19SDave Hansen * @demote_pages. 148326aa2d19SDave Hansen */ 148426aa2d19SDave Hansen static unsigned int demote_page_list(struct list_head *demote_pages, 148526aa2d19SDave Hansen struct pglist_data *pgdat) 148626aa2d19SDave Hansen { 148726aa2d19SDave Hansen int target_nid = next_demotion_node(pgdat->node_id); 148826aa2d19SDave Hansen unsigned int nr_succeeded; 148926aa2d19SDave Hansen 149026aa2d19SDave Hansen if (list_empty(demote_pages)) 149126aa2d19SDave Hansen return 0; 149226aa2d19SDave Hansen 149326aa2d19SDave Hansen if (target_nid == NUMA_NO_NODE) 149426aa2d19SDave Hansen return 0; 149526aa2d19SDave Hansen 149626aa2d19SDave Hansen /* Demotion ignores all cpuset and mempolicy settings */ 1497cb75463cSKai Song migrate_pages(demote_pages, alloc_demote_page, NULL, 149826aa2d19SDave Hansen target_nid, MIGRATE_ASYNC, MR_DEMOTION, 149926aa2d19SDave Hansen &nr_succeeded); 150026aa2d19SDave Hansen 1501668e4147SYang Shi if (current_is_kswapd()) 1502668e4147SYang Shi __count_vm_events(PGDEMOTE_KSWAPD, nr_succeeded); 1503668e4147SYang Shi else 1504668e4147SYang Shi __count_vm_events(PGDEMOTE_DIRECT, nr_succeeded); 1505668e4147SYang Shi 150626aa2d19SDave Hansen return nr_succeeded; 150726aa2d19SDave Hansen } 150826aa2d19SDave Hansen 1509c28a0e96SMatthew Wilcox (Oracle) static bool may_enter_fs(struct folio *folio, gfp_t gfp_mask) 1510d791ea67SNeilBrown { 1511d791ea67SNeilBrown if (gfp_mask & __GFP_FS) 1512d791ea67SNeilBrown return true; 1513c28a0e96SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio) || !(gfp_mask & __GFP_IO)) 1514d791ea67SNeilBrown return false; 1515d791ea67SNeilBrown /* 1516d791ea67SNeilBrown * We can "enter_fs" for swap-cache with only __GFP_IO 1517d791ea67SNeilBrown * providing this isn't SWP_FS_OPS. 1518d791ea67SNeilBrown * ->flags can be updated non-atomicially (scan_swap_map_slots), 1519d791ea67SNeilBrown * but that will never affect SWP_FS_OPS, so the data_race 1520d791ea67SNeilBrown * is safe. 1521d791ea67SNeilBrown */ 1522c28a0e96SMatthew Wilcox (Oracle) return !data_race(page_swap_flags(&folio->page) & SWP_FS_OPS); 1523d791ea67SNeilBrown } 1524d791ea67SNeilBrown 1525e286781dSNick Piggin /* 15261742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 15271da177e4SLinus Torvalds */ 1528730ec8c0SManinder Singh static unsigned int shrink_page_list(struct list_head *page_list, 1529599d0c95SMel Gorman struct pglist_data *pgdat, 1530f84f6e2bSMel Gorman struct scan_control *sc, 15313c710c1aSMichal Hocko struct reclaim_stat *stat, 15328940b34aSMinchan Kim bool ignore_references) 15331da177e4SLinus Torvalds { 15341da177e4SLinus Torvalds LIST_HEAD(ret_pages); 1535abe4c3b5SMel Gorman LIST_HEAD(free_pages); 153626aa2d19SDave Hansen LIST_HEAD(demote_pages); 1537730ec8c0SManinder Singh unsigned int nr_reclaimed = 0; 1538730ec8c0SManinder Singh unsigned int pgactivate = 0; 153926aa2d19SDave Hansen bool do_demote_pass; 15402282679fSNeilBrown struct swap_iocb *plug = NULL; 15411da177e4SLinus Torvalds 1542060f005fSKirill Tkhai memset(stat, 0, sizeof(*stat)); 15431da177e4SLinus Torvalds cond_resched(); 154426aa2d19SDave Hansen do_demote_pass = can_demote(pgdat->node_id, sc); 15451da177e4SLinus Torvalds 154626aa2d19SDave Hansen retry: 15471da177e4SLinus Torvalds while (!list_empty(page_list)) { 15481da177e4SLinus Torvalds struct address_space *mapping; 1549be7c07d6SMatthew Wilcox (Oracle) struct folio *folio; 15508940b34aSMinchan Kim enum page_references references = PAGEREF_RECLAIM; 1551d791ea67SNeilBrown bool dirty, writeback; 155298879b3bSYang Shi unsigned int nr_pages; 15531da177e4SLinus Torvalds 15541da177e4SLinus Torvalds cond_resched(); 15551da177e4SLinus Torvalds 1556be7c07d6SMatthew Wilcox (Oracle) folio = lru_to_folio(page_list); 1557be7c07d6SMatthew Wilcox (Oracle) list_del(&folio->lru); 15581da177e4SLinus Torvalds 1559c28a0e96SMatthew Wilcox (Oracle) if (!folio_trylock(folio)) 15601da177e4SLinus Torvalds goto keep; 15611da177e4SLinus Torvalds 1562c28a0e96SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_active(folio), folio); 15631da177e4SLinus Torvalds 1564c28a0e96SMatthew Wilcox (Oracle) nr_pages = folio_nr_pages(folio); 156598879b3bSYang Shi 1566c28a0e96SMatthew Wilcox (Oracle) /* Account the number of base pages */ 156798879b3bSYang Shi sc->nr_scanned += nr_pages; 156880e43426SChristoph Lameter 1569c28a0e96SMatthew Wilcox (Oracle) if (unlikely(!folio_evictable(folio))) 1570ad6b6704SMinchan Kim goto activate_locked; 1571894bc310SLee Schermerhorn 15721bee2c16SMatthew Wilcox (Oracle) if (!sc->may_unmap && folio_mapped(folio)) 157380e43426SChristoph Lameter goto keep_locked; 157480e43426SChristoph Lameter 1575e62e384eSMichal Hocko /* 1576894befecSAndrey Ryabinin * The number of dirty pages determines if a node is marked 15778cd7c588SMel Gorman * reclaim_congested. kswapd will stall and start writing 1578c28a0e96SMatthew Wilcox (Oracle) * folios if the tail of the LRU is all dirty unqueued folios. 1579e2be15f6SMel Gorman */ 1580e20c41b1SMatthew Wilcox (Oracle) folio_check_dirty_writeback(folio, &dirty, &writeback); 1581e2be15f6SMel Gorman if (dirty || writeback) 1582c79b7b96SMatthew Wilcox (Oracle) stat->nr_dirty += nr_pages; 1583e2be15f6SMel Gorman 1584e2be15f6SMel Gorman if (dirty && !writeback) 1585c79b7b96SMatthew Wilcox (Oracle) stat->nr_unqueued_dirty += nr_pages; 1586e2be15f6SMel Gorman 1587d04e8acdSMel Gorman /* 1588c28a0e96SMatthew Wilcox (Oracle) * Treat this folio as congested if folios are cycling 1589c28a0e96SMatthew Wilcox (Oracle) * through the LRU so quickly that the folios marked 1590c28a0e96SMatthew Wilcox (Oracle) * for immediate reclaim are making it to the end of 1591c28a0e96SMatthew Wilcox (Oracle) * the LRU a second time. 1592d04e8acdSMel Gorman */ 1593c28a0e96SMatthew Wilcox (Oracle) if (writeback && folio_test_reclaim(folio)) 1594c79b7b96SMatthew Wilcox (Oracle) stat->nr_congested += nr_pages; 1595e2be15f6SMel Gorman 1596e2be15f6SMel Gorman /* 1597d33e4e14SMatthew Wilcox (Oracle) * If a folio at the tail of the LRU is under writeback, there 1598283aba9fSMel Gorman * are three cases to consider. 1599e62e384eSMichal Hocko * 1600c28a0e96SMatthew Wilcox (Oracle) * 1) If reclaim is encountering an excessive number 1601c28a0e96SMatthew Wilcox (Oracle) * of folios under writeback and this folio has both 1602c28a0e96SMatthew Wilcox (Oracle) * the writeback and reclaim flags set, then it 1603d33e4e14SMatthew Wilcox (Oracle) * indicates that folios are being queued for I/O but 1604d33e4e14SMatthew Wilcox (Oracle) * are being recycled through the LRU before the I/O 1605d33e4e14SMatthew Wilcox (Oracle) * can complete. Waiting on the folio itself risks an 1606d33e4e14SMatthew Wilcox (Oracle) * indefinite stall if it is impossible to writeback 1607d33e4e14SMatthew Wilcox (Oracle) * the folio due to I/O error or disconnected storage 1608d33e4e14SMatthew Wilcox (Oracle) * so instead note that the LRU is being scanned too 1609d33e4e14SMatthew Wilcox (Oracle) * quickly and the caller can stall after the folio 1610d33e4e14SMatthew Wilcox (Oracle) * list has been processed. 1611c3b94f44SHugh Dickins * 1612d33e4e14SMatthew Wilcox (Oracle) * 2) Global or new memcg reclaim encounters a folio that is 1613ecf5fc6eSMichal Hocko * not marked for immediate reclaim, or the caller does not 1614ecf5fc6eSMichal Hocko * have __GFP_FS (or __GFP_IO if it's simply going to swap, 1615d33e4e14SMatthew Wilcox (Oracle) * not to fs). In this case mark the folio for immediate 161697c9341fSTejun Heo * reclaim and continue scanning. 1617283aba9fSMel Gorman * 1618d791ea67SNeilBrown * Require may_enter_fs() because we would wait on fs, which 1619d33e4e14SMatthew Wilcox (Oracle) * may not have submitted I/O yet. And the loop driver might 1620d33e4e14SMatthew Wilcox (Oracle) * enter reclaim, and deadlock if it waits on a folio for 1621283aba9fSMel Gorman * which it is needed to do the write (loop masks off 1622283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 1623283aba9fSMel Gorman * would probably show more reasons. 1624283aba9fSMel Gorman * 1625d33e4e14SMatthew Wilcox (Oracle) * 3) Legacy memcg encounters a folio that already has the 1626d33e4e14SMatthew Wilcox (Oracle) * reclaim flag set. memcg does not have any dirty folio 1627283aba9fSMel Gorman * throttling so we could easily OOM just because too many 1628d33e4e14SMatthew Wilcox (Oracle) * folios are in writeback and there is nothing else to 1629283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 1630c55e8d03SJohannes Weiner * 1631d33e4e14SMatthew Wilcox (Oracle) * In cases 1) and 2) we activate the folios to get them out of 1632d33e4e14SMatthew Wilcox (Oracle) * the way while we continue scanning for clean folios on the 1633c55e8d03SJohannes Weiner * inactive list and refilling from the active list. The 1634c55e8d03SJohannes Weiner * observation here is that waiting for disk writes is more 1635c55e8d03SJohannes Weiner * expensive than potentially causing reloads down the line. 1636c55e8d03SJohannes Weiner * Since they're marked for immediate reclaim, they won't put 1637c55e8d03SJohannes Weiner * memory pressure on the cache working set any longer than it 1638c55e8d03SJohannes Weiner * takes to write them to disk. 1639e62e384eSMichal Hocko */ 1640d33e4e14SMatthew Wilcox (Oracle) if (folio_test_writeback(folio)) { 1641283aba9fSMel Gorman /* Case 1 above */ 1642283aba9fSMel Gorman if (current_is_kswapd() && 1643d33e4e14SMatthew Wilcox (Oracle) folio_test_reclaim(folio) && 1644599d0c95SMel Gorman test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { 1645c79b7b96SMatthew Wilcox (Oracle) stat->nr_immediate += nr_pages; 1646c55e8d03SJohannes Weiner goto activate_locked; 1647283aba9fSMel Gorman 1648283aba9fSMel Gorman /* Case 2 above */ 1649b5ead35eSJohannes Weiner } else if (writeback_throttling_sane(sc) || 1650d33e4e14SMatthew Wilcox (Oracle) !folio_test_reclaim(folio) || 1651c28a0e96SMatthew Wilcox (Oracle) !may_enter_fs(folio, sc->gfp_mask)) { 1652c3b94f44SHugh Dickins /* 1653d33e4e14SMatthew Wilcox (Oracle) * This is slightly racy - 1654c28a0e96SMatthew Wilcox (Oracle) * folio_end_writeback() might have 1655c28a0e96SMatthew Wilcox (Oracle) * just cleared the reclaim flag, then 1656c28a0e96SMatthew Wilcox (Oracle) * setting the reclaim flag here ends up 1657c28a0e96SMatthew Wilcox (Oracle) * interpreted as the readahead flag - but 1658c28a0e96SMatthew Wilcox (Oracle) * that does not matter enough to care. 1659c28a0e96SMatthew Wilcox (Oracle) * What we do want is for this folio to 1660c28a0e96SMatthew Wilcox (Oracle) * have the reclaim flag set next time 1661c28a0e96SMatthew Wilcox (Oracle) * memcg reclaim reaches the tests above, 1662c28a0e96SMatthew Wilcox (Oracle) * so it will then wait for writeback to 1663c28a0e96SMatthew Wilcox (Oracle) * avoid OOM; and it's also appropriate 1664d33e4e14SMatthew Wilcox (Oracle) * in global reclaim. 1665c3b94f44SHugh Dickins */ 1666d33e4e14SMatthew Wilcox (Oracle) folio_set_reclaim(folio); 1667c79b7b96SMatthew Wilcox (Oracle) stat->nr_writeback += nr_pages; 1668c55e8d03SJohannes Weiner goto activate_locked; 1669283aba9fSMel Gorman 1670283aba9fSMel Gorman /* Case 3 above */ 1671283aba9fSMel Gorman } else { 1672d33e4e14SMatthew Wilcox (Oracle) folio_unlock(folio); 1673d33e4e14SMatthew Wilcox (Oracle) folio_wait_writeback(folio); 1674d33e4e14SMatthew Wilcox (Oracle) /* then go back and try same folio again */ 1675d33e4e14SMatthew Wilcox (Oracle) list_add_tail(&folio->lru, page_list); 16767fadc820SHugh Dickins continue; 1677e62e384eSMichal Hocko } 1678283aba9fSMel Gorman } 16791da177e4SLinus Torvalds 16808940b34aSMinchan Kim if (!ignore_references) 1681d92013d1SMatthew Wilcox (Oracle) references = folio_check_references(folio, sc); 168202c6de8dSMinchan Kim 1683dfc8d636SJohannes Weiner switch (references) { 1684dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 16851da177e4SLinus Torvalds goto activate_locked; 168664574746SJohannes Weiner case PAGEREF_KEEP: 168798879b3bSYang Shi stat->nr_ref_keep += nr_pages; 168864574746SJohannes Weiner goto keep_locked; 1689dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 1690dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 1691c28a0e96SMatthew Wilcox (Oracle) ; /* try to reclaim the folio below */ 1692dfc8d636SJohannes Weiner } 16931da177e4SLinus Torvalds 16941da177e4SLinus Torvalds /* 1695c28a0e96SMatthew Wilcox (Oracle) * Before reclaiming the folio, try to relocate 169626aa2d19SDave Hansen * its contents to another node. 169726aa2d19SDave Hansen */ 169826aa2d19SDave Hansen if (do_demote_pass && 1699c28a0e96SMatthew Wilcox (Oracle) (thp_migration_supported() || !folio_test_large(folio))) { 1700c28a0e96SMatthew Wilcox (Oracle) list_add(&folio->lru, &demote_pages); 1701c28a0e96SMatthew Wilcox (Oracle) folio_unlock(folio); 170226aa2d19SDave Hansen continue; 170326aa2d19SDave Hansen } 170426aa2d19SDave Hansen 170526aa2d19SDave Hansen /* 17061da177e4SLinus Torvalds * Anonymous process memory has backing store? 17071da177e4SLinus Torvalds * Try to allocate it some swap space here. 1708c28a0e96SMatthew Wilcox (Oracle) * Lazyfree folio could be freed directly 17091da177e4SLinus Torvalds */ 1710c28a0e96SMatthew Wilcox (Oracle) if (folio_test_anon(folio) && folio_test_swapbacked(folio)) { 1711c28a0e96SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio)) { 171263eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 171363eb6b93SHugh Dickins goto keep_locked; 1714d4b4084aSMatthew Wilcox (Oracle) if (folio_maybe_dma_pinned(folio)) 1715feb889fbSLinus Torvalds goto keep_locked; 1716c28a0e96SMatthew Wilcox (Oracle) if (folio_test_large(folio)) { 1717c28a0e96SMatthew Wilcox (Oracle) /* cannot split folio, skip it */ 1718d4b4084aSMatthew Wilcox (Oracle) if (!can_split_folio(folio, NULL)) 1719b8f593cdSHuang Ying goto activate_locked; 1720747552b1SHuang Ying /* 1721c28a0e96SMatthew Wilcox (Oracle) * Split folios without a PMD map right 1722747552b1SHuang Ying * away. Chances are some or all of the 1723747552b1SHuang Ying * tail pages can be freed without IO. 1724747552b1SHuang Ying */ 1725d4b4084aSMatthew Wilcox (Oracle) if (!folio_entire_mapcount(folio) && 1726346cf613SMatthew Wilcox (Oracle) split_folio_to_list(folio, 1727bd4c82c2SHuang Ying page_list)) 1728747552b1SHuang Ying goto activate_locked; 1729747552b1SHuang Ying } 173009c02e56SMatthew Wilcox (Oracle) if (!add_to_swap(folio)) { 173109c02e56SMatthew Wilcox (Oracle) if (!folio_test_large(folio)) 173298879b3bSYang Shi goto activate_locked_split; 1733bd4c82c2SHuang Ying /* Fallback to swap normal pages */ 1734346cf613SMatthew Wilcox (Oracle) if (split_folio_to_list(folio, 1735bd4c82c2SHuang Ying page_list)) 17360f074658SMinchan Kim goto activate_locked; 1737fe490cc0SHuang Ying #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1738fe490cc0SHuang Ying count_vm_event(THP_SWPOUT_FALLBACK); 1739fe490cc0SHuang Ying #endif 174009c02e56SMatthew Wilcox (Oracle) if (!add_to_swap(folio)) 174198879b3bSYang Shi goto activate_locked_split; 17420f074658SMinchan Kim } 1743bd4c82c2SHuang Ying } 1744c28a0e96SMatthew Wilcox (Oracle) } else if (folio_test_swapbacked(folio) && 1745c28a0e96SMatthew Wilcox (Oracle) folio_test_large(folio)) { 1746c28a0e96SMatthew Wilcox (Oracle) /* Split shmem folio */ 1747346cf613SMatthew Wilcox (Oracle) if (split_folio_to_list(folio, page_list)) 17487751b2daSKirill A. Shutemov goto keep_locked; 1749e2be15f6SMel Gorman } 17501da177e4SLinus Torvalds 17511da177e4SLinus Torvalds /* 1752c28a0e96SMatthew Wilcox (Oracle) * If the folio was split above, the tail pages will make 1753c28a0e96SMatthew Wilcox (Oracle) * their own pass through this function and be accounted 1754c28a0e96SMatthew Wilcox (Oracle) * then. 175598879b3bSYang Shi */ 1756c28a0e96SMatthew Wilcox (Oracle) if ((nr_pages > 1) && !folio_test_large(folio)) { 175798879b3bSYang Shi sc->nr_scanned -= (nr_pages - 1); 175898879b3bSYang Shi nr_pages = 1; 175998879b3bSYang Shi } 176098879b3bSYang Shi 176198879b3bSYang Shi /* 17621bee2c16SMatthew Wilcox (Oracle) * The folio is mapped into the page tables of one or more 17631da177e4SLinus Torvalds * processes. Try to unmap it here. 17641da177e4SLinus Torvalds */ 17651bee2c16SMatthew Wilcox (Oracle) if (folio_mapped(folio)) { 1766013339dfSShakeel Butt enum ttu_flags flags = TTU_BATCH_FLUSH; 17671bee2c16SMatthew Wilcox (Oracle) bool was_swapbacked = folio_test_swapbacked(folio); 1768bd4c82c2SHuang Ying 17691bee2c16SMatthew Wilcox (Oracle) if (folio_test_pmd_mappable(folio)) 1770bd4c82c2SHuang Ying flags |= TTU_SPLIT_HUGE_PMD; 17711f318a9bSJaewon Kim 1772869f7ee6SMatthew Wilcox (Oracle) try_to_unmap(folio, flags); 17731bee2c16SMatthew Wilcox (Oracle) if (folio_mapped(folio)) { 177498879b3bSYang Shi stat->nr_unmap_fail += nr_pages; 17751bee2c16SMatthew Wilcox (Oracle) if (!was_swapbacked && 17761bee2c16SMatthew Wilcox (Oracle) folio_test_swapbacked(folio)) 17771f318a9bSJaewon Kim stat->nr_lazyfree_fail += nr_pages; 17781da177e4SLinus Torvalds goto activate_locked; 17791da177e4SLinus Torvalds } 17801da177e4SLinus Torvalds } 17811da177e4SLinus Torvalds 17825441d490SMatthew Wilcox (Oracle) mapping = folio_mapping(folio); 178349bd2bf9SMatthew Wilcox (Oracle) if (folio_test_dirty(folio)) { 1784ee72886dSMel Gorman /* 178549bd2bf9SMatthew Wilcox (Oracle) * Only kswapd can writeback filesystem folios 17864eda4823SJohannes Weiner * to avoid risk of stack overflow. But avoid 178749bd2bf9SMatthew Wilcox (Oracle) * injecting inefficient single-folio I/O into 17884eda4823SJohannes Weiner * flusher writeback as much as possible: only 178949bd2bf9SMatthew Wilcox (Oracle) * write folios when we've encountered many 179049bd2bf9SMatthew Wilcox (Oracle) * dirty folios, and when we've already scanned 179149bd2bf9SMatthew Wilcox (Oracle) * the rest of the LRU for clean folios and see 179249bd2bf9SMatthew Wilcox (Oracle) * the same dirty folios again (with the reclaim 179349bd2bf9SMatthew Wilcox (Oracle) * flag set). 1794ee72886dSMel Gorman */ 179549bd2bf9SMatthew Wilcox (Oracle) if (folio_is_file_lru(folio) && 179649bd2bf9SMatthew Wilcox (Oracle) (!current_is_kswapd() || 179749bd2bf9SMatthew Wilcox (Oracle) !folio_test_reclaim(folio) || 1798599d0c95SMel Gorman !test_bit(PGDAT_DIRTY, &pgdat->flags))) { 179949ea7eb6SMel Gorman /* 180049ea7eb6SMel Gorman * Immediately reclaim when written back. 180149bd2bf9SMatthew Wilcox (Oracle) * Similar in principle to deactivate_page() 180249bd2bf9SMatthew Wilcox (Oracle) * except we already have the folio isolated 180349ea7eb6SMel Gorman * and know it's dirty 180449ea7eb6SMel Gorman */ 180549bd2bf9SMatthew Wilcox (Oracle) node_stat_mod_folio(folio, NR_VMSCAN_IMMEDIATE, 180649bd2bf9SMatthew Wilcox (Oracle) nr_pages); 180749bd2bf9SMatthew Wilcox (Oracle) folio_set_reclaim(folio); 180849ea7eb6SMel Gorman 1809c55e8d03SJohannes Weiner goto activate_locked; 1810ee72886dSMel Gorman } 1811ee72886dSMel Gorman 1812dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 18131da177e4SLinus Torvalds goto keep_locked; 1814c28a0e96SMatthew Wilcox (Oracle) if (!may_enter_fs(folio, sc->gfp_mask)) 18151da177e4SLinus Torvalds goto keep_locked; 181652a8363eSChristoph Lameter if (!sc->may_writepage) 18171da177e4SLinus Torvalds goto keep_locked; 18181da177e4SLinus Torvalds 1819d950c947SMel Gorman /* 182049bd2bf9SMatthew Wilcox (Oracle) * Folio is dirty. Flush the TLB if a writable entry 182149bd2bf9SMatthew Wilcox (Oracle) * potentially exists to avoid CPU writes after I/O 1822d950c947SMel Gorman * starts and then write it out here. 1823d950c947SMel Gorman */ 1824d950c947SMel Gorman try_to_unmap_flush_dirty(); 18252282679fSNeilBrown switch (pageout(folio, mapping, &plug)) { 18261da177e4SLinus Torvalds case PAGE_KEEP: 18271da177e4SLinus Torvalds goto keep_locked; 18281da177e4SLinus Torvalds case PAGE_ACTIVATE: 18291da177e4SLinus Torvalds goto activate_locked; 18301da177e4SLinus Torvalds case PAGE_SUCCESS: 1831c79b7b96SMatthew Wilcox (Oracle) stat->nr_pageout += nr_pages; 183296f8bf4fSJohannes Weiner 183349bd2bf9SMatthew Wilcox (Oracle) if (folio_test_writeback(folio)) 183441ac1999SMel Gorman goto keep; 183549bd2bf9SMatthew Wilcox (Oracle) if (folio_test_dirty(folio)) 18361da177e4SLinus Torvalds goto keep; 18377d3579e8SKOSAKI Motohiro 18381da177e4SLinus Torvalds /* 18391da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 184049bd2bf9SMatthew Wilcox (Oracle) * ahead and try to reclaim the folio. 18411da177e4SLinus Torvalds */ 184249bd2bf9SMatthew Wilcox (Oracle) if (!folio_trylock(folio)) 18431da177e4SLinus Torvalds goto keep; 184449bd2bf9SMatthew Wilcox (Oracle) if (folio_test_dirty(folio) || 184549bd2bf9SMatthew Wilcox (Oracle) folio_test_writeback(folio)) 18461da177e4SLinus Torvalds goto keep_locked; 184749bd2bf9SMatthew Wilcox (Oracle) mapping = folio_mapping(folio); 184801359eb2SGustavo A. R. Silva fallthrough; 18491da177e4SLinus Torvalds case PAGE_CLEAN: 185049bd2bf9SMatthew Wilcox (Oracle) ; /* try to free the folio below */ 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds } 18531da177e4SLinus Torvalds 18541da177e4SLinus Torvalds /* 18550a36111cSMatthew Wilcox (Oracle) * If the folio has buffers, try to free the buffer 18560a36111cSMatthew Wilcox (Oracle) * mappings associated with this folio. If we succeed 18570a36111cSMatthew Wilcox (Oracle) * we try to free the folio as well. 18581da177e4SLinus Torvalds * 18590a36111cSMatthew Wilcox (Oracle) * We do this even if the folio is dirty. 18600a36111cSMatthew Wilcox (Oracle) * filemap_release_folio() does not perform I/O, but it 18610a36111cSMatthew Wilcox (Oracle) * is possible for a folio to have the dirty flag set, 18620a36111cSMatthew Wilcox (Oracle) * but it is actually clean (all its buffers are clean). 18630a36111cSMatthew Wilcox (Oracle) * This happens if the buffers were written out directly, 18640a36111cSMatthew Wilcox (Oracle) * with submit_bh(). ext3 will do this, as well as 18650a36111cSMatthew Wilcox (Oracle) * the blockdev mapping. filemap_release_folio() will 18660a36111cSMatthew Wilcox (Oracle) * discover that cleanness and will drop the buffers 18670a36111cSMatthew Wilcox (Oracle) * and mark the folio clean - it can be freed. 18681da177e4SLinus Torvalds * 18690a36111cSMatthew Wilcox (Oracle) * Rarely, folios can have buffers and no ->mapping. 18700a36111cSMatthew Wilcox (Oracle) * These are the folios which were not successfully 18710a36111cSMatthew Wilcox (Oracle) * invalidated in truncate_cleanup_folio(). We try to 18720a36111cSMatthew Wilcox (Oracle) * drop those buffers here and if that worked, and the 18730a36111cSMatthew Wilcox (Oracle) * folio is no longer mapped into process address space 18740a36111cSMatthew Wilcox (Oracle) * (refcount == 1) it can be freed. Otherwise, leave 18750a36111cSMatthew Wilcox (Oracle) * the folio on the LRU so it is swappable. 18761da177e4SLinus Torvalds */ 18770a36111cSMatthew Wilcox (Oracle) if (folio_has_private(folio)) { 18780a36111cSMatthew Wilcox (Oracle) if (!filemap_release_folio(folio, sc->gfp_mask)) 18791da177e4SLinus Torvalds goto activate_locked; 18800a36111cSMatthew Wilcox (Oracle) if (!mapping && folio_ref_count(folio) == 1) { 18810a36111cSMatthew Wilcox (Oracle) folio_unlock(folio); 18820a36111cSMatthew Wilcox (Oracle) if (folio_put_testzero(folio)) 18831da177e4SLinus Torvalds goto free_it; 1884e286781dSNick Piggin else { 1885e286781dSNick Piggin /* 1886e286781dSNick Piggin * rare race with speculative reference. 1887e286781dSNick Piggin * the speculative reference will free 18880a36111cSMatthew Wilcox (Oracle) * this folio shortly, so we may 1889e286781dSNick Piggin * increment nr_reclaimed here (and 1890e286781dSNick Piggin * leave it off the LRU). 1891e286781dSNick Piggin */ 18929aafcffcSMiaohe Lin nr_reclaimed += nr_pages; 1893e286781dSNick Piggin continue; 1894e286781dSNick Piggin } 1895e286781dSNick Piggin } 18961da177e4SLinus Torvalds } 18971da177e4SLinus Torvalds 189864daa5d8SMatthew Wilcox (Oracle) if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) { 1899802a3a92SShaohua Li /* follow __remove_mapping for reference */ 190064daa5d8SMatthew Wilcox (Oracle) if (!folio_ref_freeze(folio, 1)) 190149d2e9ccSChristoph Lameter goto keep_locked; 1902d17be2d9SMiaohe Lin /* 190364daa5d8SMatthew Wilcox (Oracle) * The folio has only one reference left, which is 1904d17be2d9SMiaohe Lin * from the isolation. After the caller puts the 190564daa5d8SMatthew Wilcox (Oracle) * folio back on the lru and drops the reference, the 190664daa5d8SMatthew Wilcox (Oracle) * folio will be freed anyway. It doesn't matter 190764daa5d8SMatthew Wilcox (Oracle) * which lru it goes on. So we don't bother checking 190864daa5d8SMatthew Wilcox (Oracle) * the dirty flag here. 1909d17be2d9SMiaohe Lin */ 191064daa5d8SMatthew Wilcox (Oracle) count_vm_events(PGLAZYFREED, nr_pages); 191164daa5d8SMatthew Wilcox (Oracle) count_memcg_folio_events(folio, PGLAZYFREED, nr_pages); 1912be7c07d6SMatthew Wilcox (Oracle) } else if (!mapping || !__remove_mapping(mapping, folio, true, 1913b910718aSJohannes Weiner sc->target_mem_cgroup)) 1914802a3a92SShaohua Li goto keep_locked; 19159a1ea439SHugh Dickins 1916c28a0e96SMatthew Wilcox (Oracle) folio_unlock(folio); 1917e286781dSNick Piggin free_it: 191898879b3bSYang Shi /* 1919c28a0e96SMatthew Wilcox (Oracle) * Folio may get swapped out as a whole, need to account 1920c28a0e96SMatthew Wilcox (Oracle) * all pages in it. 192198879b3bSYang Shi */ 192298879b3bSYang Shi nr_reclaimed += nr_pages; 1923abe4c3b5SMel Gorman 1924abe4c3b5SMel Gorman /* 1925abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1926abe4c3b5SMel Gorman * appear not as the counts should be low 1927abe4c3b5SMel Gorman */ 1928c28a0e96SMatthew Wilcox (Oracle) if (unlikely(folio_test_large(folio))) 1929c28a0e96SMatthew Wilcox (Oracle) destroy_compound_page(&folio->page); 19307ae88534SYang Shi else 1931c28a0e96SMatthew Wilcox (Oracle) list_add(&folio->lru, &free_pages); 19321da177e4SLinus Torvalds continue; 19331da177e4SLinus Torvalds 193498879b3bSYang Shi activate_locked_split: 193598879b3bSYang Shi /* 193698879b3bSYang Shi * The tail pages that are failed to add into swap cache 193798879b3bSYang Shi * reach here. Fixup nr_scanned and nr_pages. 193898879b3bSYang Shi */ 193998879b3bSYang Shi if (nr_pages > 1) { 194098879b3bSYang Shi sc->nr_scanned -= (nr_pages - 1); 194198879b3bSYang Shi nr_pages = 1; 194298879b3bSYang Shi } 19431da177e4SLinus Torvalds activate_locked: 194468a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 1945246b6480SMatthew Wilcox (Oracle) if (folio_test_swapcache(folio) && 1946246b6480SMatthew Wilcox (Oracle) (mem_cgroup_swap_full(&folio->page) || 1947246b6480SMatthew Wilcox (Oracle) folio_test_mlocked(folio))) 1948246b6480SMatthew Wilcox (Oracle) try_to_free_swap(&folio->page); 1949246b6480SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_active(folio), folio); 1950246b6480SMatthew Wilcox (Oracle) if (!folio_test_mlocked(folio)) { 1951246b6480SMatthew Wilcox (Oracle) int type = folio_is_file_lru(folio); 1952246b6480SMatthew Wilcox (Oracle) folio_set_active(folio); 195398879b3bSYang Shi stat->nr_activate[type] += nr_pages; 1954246b6480SMatthew Wilcox (Oracle) count_memcg_folio_events(folio, PGACTIVATE, nr_pages); 1955ad6b6704SMinchan Kim } 19561da177e4SLinus Torvalds keep_locked: 1957c28a0e96SMatthew Wilcox (Oracle) folio_unlock(folio); 19581da177e4SLinus Torvalds keep: 1959c28a0e96SMatthew Wilcox (Oracle) list_add(&folio->lru, &ret_pages); 1960c28a0e96SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_lru(folio) || 1961c28a0e96SMatthew Wilcox (Oracle) folio_test_unevictable(folio), folio); 19621da177e4SLinus Torvalds } 196326aa2d19SDave Hansen /* 'page_list' is always empty here */ 196426aa2d19SDave Hansen 1965c28a0e96SMatthew Wilcox (Oracle) /* Migrate folios selected for demotion */ 196626aa2d19SDave Hansen nr_reclaimed += demote_page_list(&demote_pages, pgdat); 1967c28a0e96SMatthew Wilcox (Oracle) /* Folios that could not be demoted are still in @demote_pages */ 196826aa2d19SDave Hansen if (!list_empty(&demote_pages)) { 1969c28a0e96SMatthew Wilcox (Oracle) /* Folios which weren't demoted go back on @page_list for retry: */ 197026aa2d19SDave Hansen list_splice_init(&demote_pages, page_list); 197126aa2d19SDave Hansen do_demote_pass = false; 197226aa2d19SDave Hansen goto retry; 197326aa2d19SDave Hansen } 1974abe4c3b5SMel Gorman 197598879b3bSYang Shi pgactivate = stat->nr_activate[0] + stat->nr_activate[1]; 197698879b3bSYang Shi 1977747db954SJohannes Weiner mem_cgroup_uncharge_list(&free_pages); 197872b252aeSMel Gorman try_to_unmap_flush(); 19792d4894b5SMel Gorman free_unref_page_list(&free_pages); 1980abe4c3b5SMel Gorman 19811da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1982886cf190SKirill Tkhai count_vm_events(PGACTIVATE, pgactivate); 19830a31bc97SJohannes Weiner 19842282679fSNeilBrown if (plug) 19852282679fSNeilBrown swap_write_unplug(plug); 198605ff5137SAndrew Morton return nr_reclaimed; 19871da177e4SLinus Torvalds } 19881da177e4SLinus Torvalds 1989730ec8c0SManinder Singh unsigned int reclaim_clean_pages_from_list(struct zone *zone, 199002c6de8dSMinchan Kim struct list_head *page_list) 199102c6de8dSMinchan Kim { 199202c6de8dSMinchan Kim struct scan_control sc = { 199302c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 199402c6de8dSMinchan Kim .may_unmap = 1, 199502c6de8dSMinchan Kim }; 19961f318a9bSJaewon Kim struct reclaim_stat stat; 1997730ec8c0SManinder Singh unsigned int nr_reclaimed; 199802c6de8dSMinchan Kim struct page *page, *next; 199902c6de8dSMinchan Kim LIST_HEAD(clean_pages); 20002d2b8d2bSYu Zhao unsigned int noreclaim_flag; 200102c6de8dSMinchan Kim 200202c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 2003ae37c7ffSOscar Salvador if (!PageHuge(page) && page_is_file_lru(page) && 2004ae37c7ffSOscar Salvador !PageDirty(page) && !__PageMovable(page) && 2005ae37c7ffSOscar Salvador !PageUnevictable(page)) { 200602c6de8dSMinchan Kim ClearPageActive(page); 200702c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 200802c6de8dSMinchan Kim } 200902c6de8dSMinchan Kim } 201002c6de8dSMinchan Kim 20112d2b8d2bSYu Zhao /* 20122d2b8d2bSYu Zhao * We should be safe here since we are only dealing with file pages and 20132d2b8d2bSYu Zhao * we are not kswapd and therefore cannot write dirty file pages. But 20142d2b8d2bSYu Zhao * call memalloc_noreclaim_save() anyway, just in case these conditions 20152d2b8d2bSYu Zhao * change in the future. 20162d2b8d2bSYu Zhao */ 20172d2b8d2bSYu Zhao noreclaim_flag = memalloc_noreclaim_save(); 20181f318a9bSJaewon Kim nr_reclaimed = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc, 2019013339dfSShakeel Butt &stat, true); 20202d2b8d2bSYu Zhao memalloc_noreclaim_restore(noreclaim_flag); 20212d2b8d2bSYu Zhao 202202c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 20232da9f630SNicholas Piggin mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, 20242da9f630SNicholas Piggin -(long)nr_reclaimed); 20251f318a9bSJaewon Kim /* 20261f318a9bSJaewon Kim * Since lazyfree pages are isolated from file LRU from the beginning, 20271f318a9bSJaewon Kim * they will rotate back to anonymous LRU in the end if it failed to 20281f318a9bSJaewon Kim * discard so isolated count will be mismatched. 20291f318a9bSJaewon Kim * Compensate the isolated count for both LRU lists. 20301f318a9bSJaewon Kim */ 20311f318a9bSJaewon Kim mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON, 20321f318a9bSJaewon Kim stat.nr_lazyfree_fail); 20331f318a9bSJaewon Kim mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, 20342da9f630SNicholas Piggin -(long)stat.nr_lazyfree_fail); 20351f318a9bSJaewon Kim return nr_reclaimed; 203602c6de8dSMinchan Kim } 203702c6de8dSMinchan Kim 20385ad333ebSAndy Whitcroft /* 20397ee36a14SMel Gorman * Update LRU sizes after isolating pages. The LRU size updates must 204055b65a57SEthon Paul * be complete before mem_cgroup_update_lru_size due to a sanity check. 20417ee36a14SMel Gorman */ 20427ee36a14SMel Gorman static __always_inline void update_lru_sizes(struct lruvec *lruvec, 2043b4536f0cSMichal Hocko enum lru_list lru, unsigned long *nr_zone_taken) 20447ee36a14SMel Gorman { 20457ee36a14SMel Gorman int zid; 20467ee36a14SMel Gorman 20477ee36a14SMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 20487ee36a14SMel Gorman if (!nr_zone_taken[zid]) 20497ee36a14SMel Gorman continue; 20507ee36a14SMel Gorman 2051a892cb6bSWei Yang update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 20527ee36a14SMel Gorman } 20537ee36a14SMel Gorman 20547ee36a14SMel Gorman } 20557ee36a14SMel Gorman 2056f611fab7SMel Gorman /* 205715b44736SHugh Dickins * Isolating page from the lruvec to fill in @dst list by nr_to_scan times. 205815b44736SHugh Dickins * 205915b44736SHugh Dickins * lruvec->lru_lock is heavily contended. Some of the functions that 20601da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 20611da177e4SLinus Torvalds * and working on them outside the LRU lock. 20621da177e4SLinus Torvalds * 20631da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 20641da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 20651da177e4SLinus Torvalds * 206615b44736SHugh Dickins * Lru_lock must be held before calling this function. 20671da177e4SLinus Torvalds * 2068791b48b6SMinchan Kim * @nr_to_scan: The number of eligible pages to look through on the list. 20695dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 20701da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 2071f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 2072fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 20733cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 20741da177e4SLinus Torvalds * 20751da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 20761da177e4SLinus Torvalds */ 207769e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 20785dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 2079fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 2080a9e7c39fSKirill Tkhai enum lru_list lru) 20811da177e4SLinus Torvalds { 208275b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 208369e05944SAndrew Morton unsigned long nr_taken = 0; 2084599d0c95SMel Gorman unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; 20857cc30fcfSMel Gorman unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; 20863db65812SJohannes Weiner unsigned long skipped = 0; 2087791b48b6SMinchan Kim unsigned long scan, total_scan, nr_pages; 2088b2e18757SMel Gorman LIST_HEAD(pages_skipped); 20891da177e4SLinus Torvalds 209098879b3bSYang Shi total_scan = 0; 2091791b48b6SMinchan Kim scan = 0; 209298879b3bSYang Shi while (scan < nr_to_scan && !list_empty(src)) { 209389f6c88aSHugh Dickins struct list_head *move_to = src; 20945ad333ebSAndy Whitcroft struct page *page; 20955ad333ebSAndy Whitcroft 20961da177e4SLinus Torvalds page = lru_to_page(src); 20971da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 20981da177e4SLinus Torvalds 2099d8c6546bSMatthew Wilcox (Oracle) nr_pages = compound_nr(page); 210098879b3bSYang Shi total_scan += nr_pages; 210198879b3bSYang Shi 2102b2e18757SMel Gorman if (page_zonenum(page) > sc->reclaim_idx) { 210398879b3bSYang Shi nr_skipped[page_zonenum(page)] += nr_pages; 210489f6c88aSHugh Dickins move_to = &pages_skipped; 210589f6c88aSHugh Dickins goto move; 2106b2e18757SMel Gorman } 2107b2e18757SMel Gorman 2108791b48b6SMinchan Kim /* 2109791b48b6SMinchan Kim * Do not count skipped pages because that makes the function 2110791b48b6SMinchan Kim * return with no isolated pages if the LRU mostly contains 2111791b48b6SMinchan Kim * ineligible pages. This causes the VM to not reclaim any 2112791b48b6SMinchan Kim * pages, triggering a premature OOM. 211389f6c88aSHugh Dickins * Account all tail pages of THP. 2114791b48b6SMinchan Kim */ 211598879b3bSYang Shi scan += nr_pages; 211689f6c88aSHugh Dickins 211789f6c88aSHugh Dickins if (!PageLRU(page)) 211889f6c88aSHugh Dickins goto move; 211989f6c88aSHugh Dickins if (!sc->may_unmap && page_mapped(page)) 212089f6c88aSHugh Dickins goto move; 212189f6c88aSHugh Dickins 21229df41314SAlex Shi /* 21239df41314SAlex Shi * Be careful not to clear PageLRU until after we're 21249df41314SAlex Shi * sure the page is not being freed elsewhere -- the 21259df41314SAlex Shi * page release code relies on it. 21269df41314SAlex Shi */ 212789f6c88aSHugh Dickins if (unlikely(!get_page_unless_zero(page))) 212889f6c88aSHugh Dickins goto move; 21299df41314SAlex Shi 21309df41314SAlex Shi if (!TestClearPageLRU(page)) { 2131c2135f7cSAlex Shi /* Another thread is already isolating this page */ 21329df41314SAlex Shi put_page(page); 213389f6c88aSHugh Dickins goto move; 21349df41314SAlex Shi } 21359df41314SAlex Shi 2136599d0c95SMel Gorman nr_taken += nr_pages; 2137599d0c95SMel Gorman nr_zone_taken[page_zonenum(page)] += nr_pages; 213889f6c88aSHugh Dickins move_to = dst; 213989f6c88aSHugh Dickins move: 214089f6c88aSHugh Dickins list_move(&page->lru, move_to); 21415ad333ebSAndy Whitcroft } 21421da177e4SLinus Torvalds 2143b2e18757SMel Gorman /* 2144b2e18757SMel Gorman * Splice any skipped pages to the start of the LRU list. Note that 2145b2e18757SMel Gorman * this disrupts the LRU order when reclaiming for lower zones but 2146b2e18757SMel Gorman * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX 2147b2cb6826SMiaohe Lin * scanning would soon rescan the same pages to skip and waste lots 2148b2cb6826SMiaohe Lin * of cpu cycles. 2149b2e18757SMel Gorman */ 21507cc30fcfSMel Gorman if (!list_empty(&pages_skipped)) { 21517cc30fcfSMel Gorman int zid; 21527cc30fcfSMel Gorman 21533db65812SJohannes Weiner list_splice(&pages_skipped, src); 21547cc30fcfSMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 21557cc30fcfSMel Gorman if (!nr_skipped[zid]) 21567cc30fcfSMel Gorman continue; 21577cc30fcfSMel Gorman 21587cc30fcfSMel Gorman __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); 21591265e3a6SMichal Hocko skipped += nr_skipped[zid]; 21607cc30fcfSMel Gorman } 21617cc30fcfSMel Gorman } 2162791b48b6SMinchan Kim *nr_scanned = total_scan; 21631265e3a6SMichal Hocko trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, 216489f6c88aSHugh Dickins total_scan, skipped, nr_taken, 216589f6c88aSHugh Dickins sc->may_unmap ? 0 : ISOLATE_UNMAPPED, lru); 2166b4536f0cSMichal Hocko update_lru_sizes(lruvec, lru, nr_zone_taken); 21671da177e4SLinus Torvalds return nr_taken; 21681da177e4SLinus Torvalds } 21691da177e4SLinus Torvalds 217062695a84SNick Piggin /** 2171d1d8a3b4SMatthew Wilcox (Oracle) * folio_isolate_lru() - Try to isolate a folio from its LRU list. 2172d1d8a3b4SMatthew Wilcox (Oracle) * @folio: Folio to isolate from its LRU list. 217362695a84SNick Piggin * 2174d1d8a3b4SMatthew Wilcox (Oracle) * Isolate a @folio from an LRU list and adjust the vmstat statistic 2175d1d8a3b4SMatthew Wilcox (Oracle) * corresponding to whatever LRU list the folio was on. 217662695a84SNick Piggin * 2177d1d8a3b4SMatthew Wilcox (Oracle) * The folio will have its LRU flag cleared. If it was found on the 2178d1d8a3b4SMatthew Wilcox (Oracle) * active list, it will have the Active flag set. If it was found on the 2179d1d8a3b4SMatthew Wilcox (Oracle) * unevictable list, it will have the Unevictable flag set. These flags 2180894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 218162695a84SNick Piggin * 2182d1d8a3b4SMatthew Wilcox (Oracle) * Context: 2183a5d09bedSMike Rapoport * 218462695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 2185d1d8a3b4SMatthew Wilcox (Oracle) * fundamental difference from isolate_lru_pages() (which is called 218662695a84SNick Piggin * without a stable reference). 2187d1d8a3b4SMatthew Wilcox (Oracle) * (2) The lru_lock must not be held. 2188d1d8a3b4SMatthew Wilcox (Oracle) * (3) Interrupts must be enabled. 2189d1d8a3b4SMatthew Wilcox (Oracle) * 2190d1d8a3b4SMatthew Wilcox (Oracle) * Return: 0 if the folio was removed from an LRU list. 2191d1d8a3b4SMatthew Wilcox (Oracle) * -EBUSY if the folio was not on an LRU list. 219262695a84SNick Piggin */ 2193d1d8a3b4SMatthew Wilcox (Oracle) int folio_isolate_lru(struct folio *folio) 219462695a84SNick Piggin { 219562695a84SNick Piggin int ret = -EBUSY; 219662695a84SNick Piggin 2197d1d8a3b4SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_ref_count(folio), folio); 21980c917313SKonstantin Khlebnikov 2199d1d8a3b4SMatthew Wilcox (Oracle) if (folio_test_clear_lru(folio)) { 2200fa9add64SHugh Dickins struct lruvec *lruvec; 220162695a84SNick Piggin 2202d1d8a3b4SMatthew Wilcox (Oracle) folio_get(folio); 2203e809c3feSMatthew Wilcox (Oracle) lruvec = folio_lruvec_lock_irq(folio); 2204d1d8a3b4SMatthew Wilcox (Oracle) lruvec_del_folio(lruvec, folio); 22056168d0daSAlex Shi unlock_page_lruvec_irq(lruvec); 2206fa9add64SHugh Dickins ret = 0; 220762695a84SNick Piggin } 2208d25b5bd8SAlex Shi 220962695a84SNick Piggin return ret; 221062695a84SNick Piggin } 221162695a84SNick Piggin 22125ad333ebSAndy Whitcroft /* 2213d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 2214178821b8SXianting Tian * then get rescheduled. When there are massive number of tasks doing page 2215d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 2216d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 2217d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 221835cd7815SRik van Riel */ 2219599d0c95SMel Gorman static int too_many_isolated(struct pglist_data *pgdat, int file, 222035cd7815SRik van Riel struct scan_control *sc) 222135cd7815SRik van Riel { 222235cd7815SRik van Riel unsigned long inactive, isolated; 2223d818fca1SMel Gorman bool too_many; 222435cd7815SRik van Riel 222535cd7815SRik van Riel if (current_is_kswapd()) 222635cd7815SRik van Riel return 0; 222735cd7815SRik van Riel 2228b5ead35eSJohannes Weiner if (!writeback_throttling_sane(sc)) 222935cd7815SRik van Riel return 0; 223035cd7815SRik van Riel 223135cd7815SRik van Riel if (file) { 2232599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_FILE); 2233599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_FILE); 223435cd7815SRik van Riel } else { 2235599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_ANON); 2236599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_ANON); 223735cd7815SRik van Riel } 223835cd7815SRik van Riel 22393cf23841SFengguang Wu /* 22403cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 22413cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 22423cf23841SFengguang Wu * deadlock. 22433cf23841SFengguang Wu */ 2244d0164adcSMel Gorman if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) 22453cf23841SFengguang Wu inactive >>= 3; 22463cf23841SFengguang Wu 2247d818fca1SMel Gorman too_many = isolated > inactive; 2248d818fca1SMel Gorman 2249d818fca1SMel Gorman /* Wake up tasks throttled due to too_many_isolated. */ 2250d818fca1SMel Gorman if (!too_many) 2251d818fca1SMel Gorman wake_throttle_isolated(pgdat); 2252d818fca1SMel Gorman 2253d818fca1SMel Gorman return too_many; 225435cd7815SRik van Riel } 225535cd7815SRik van Riel 2256a222f341SKirill Tkhai /* 225715b44736SHugh Dickins * move_pages_to_lru() moves pages from private @list to appropriate LRU list. 225815b44736SHugh Dickins * On return, @list is reused as a list of pages to be freed by the caller. 2259a222f341SKirill Tkhai * 2260a222f341SKirill Tkhai * Returns the number of pages moved to the given lruvec. 2261a222f341SKirill Tkhai */ 22629ef56b78SMuchun Song static unsigned int move_pages_to_lru(struct lruvec *lruvec, 2263a222f341SKirill Tkhai struct list_head *list) 226466635629SMel Gorman { 2265a222f341SKirill Tkhai int nr_pages, nr_moved = 0; 22663f79768fSHugh Dickins LIST_HEAD(pages_to_free); 2267a222f341SKirill Tkhai struct page *page; 226866635629SMel Gorman 2269a222f341SKirill Tkhai while (!list_empty(list)) { 2270a222f341SKirill Tkhai page = lru_to_page(list); 2271309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 2272a222f341SKirill Tkhai list_del(&page->lru); 22733d06afabSAlex Shi if (unlikely(!page_evictable(page))) { 22746168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 227566635629SMel Gorman putback_lru_page(page); 22766168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 227766635629SMel Gorman continue; 227866635629SMel Gorman } 2279fa9add64SHugh Dickins 22803d06afabSAlex Shi /* 22813d06afabSAlex Shi * The SetPageLRU needs to be kept here for list integrity. 22823d06afabSAlex Shi * Otherwise: 22833d06afabSAlex Shi * #0 move_pages_to_lru #1 release_pages 22843d06afabSAlex Shi * if !put_page_testzero 22853d06afabSAlex Shi * if (put_page_testzero()) 22863d06afabSAlex Shi * !PageLRU //skip lru_lock 22873d06afabSAlex Shi * SetPageLRU() 22883d06afabSAlex Shi * list_add(&page->lru,) 22893d06afabSAlex Shi * list_add(&page->lru,) 22903d06afabSAlex Shi */ 22917a608572SLinus Torvalds SetPageLRU(page); 2292a222f341SKirill Tkhai 22933d06afabSAlex Shi if (unlikely(put_page_testzero(page))) { 229487560179SYu Zhao __clear_page_lru_flags(page); 22952bcf8879SHugh Dickins 22962bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 22976168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 2298ff45fc3cSMatthew Wilcox (Oracle) destroy_compound_page(page); 22996168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 23002bcf8879SHugh Dickins } else 23012bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 23023d06afabSAlex Shi 23033d06afabSAlex Shi continue; 23043d06afabSAlex Shi } 23053d06afabSAlex Shi 2306afca9157SAlex Shi /* 2307afca9157SAlex Shi * All pages were isolated from the same lruvec (and isolation 2308afca9157SAlex Shi * inhibits memcg migration). 2309afca9157SAlex Shi */ 23100de340cbSMatthew Wilcox (Oracle) VM_BUG_ON_PAGE(!folio_matches_lruvec(page_folio(page), lruvec), page); 23113a9c9788SYu Zhao add_page_to_lru_list(page, lruvec); 23123d06afabSAlex Shi nr_pages = thp_nr_pages(page); 2313a222f341SKirill Tkhai nr_moved += nr_pages; 231431d8fcacSJohannes Weiner if (PageActive(page)) 231531d8fcacSJohannes Weiner workingset_age_nonresident(lruvec, nr_pages); 231666635629SMel Gorman } 231766635629SMel Gorman 23183f79768fSHugh Dickins /* 23193f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 23203f79768fSHugh Dickins */ 2321a222f341SKirill Tkhai list_splice(&pages_to_free, list); 2322a222f341SKirill Tkhai 2323a222f341SKirill Tkhai return nr_moved; 232466635629SMel Gorman } 232566635629SMel Gorman 232666635629SMel Gorman /* 23275829f7dbSMiaohe Lin * If a kernel thread (such as nfsd for loop-back mounts) services a backing 23285829f7dbSMiaohe Lin * device by writing to the page cache it sets PF_LOCAL_THROTTLE. In this case 23295829f7dbSMiaohe Lin * we should not throttle. Otherwise it is safe to do so. 2330399ba0b9SNeilBrown */ 2331399ba0b9SNeilBrown static int current_may_throttle(void) 2332399ba0b9SNeilBrown { 2333b9b1335eSNeilBrown return !(current->flags & PF_LOCAL_THROTTLE); 2334399ba0b9SNeilBrown } 2335399ba0b9SNeilBrown 2336399ba0b9SNeilBrown /* 2337b2e18757SMel Gorman * shrink_inactive_list() is a helper for shrink_node(). It returns the number 23381742f19fSAndrew Morton * of reclaimed pages 23391da177e4SLinus Torvalds */ 23409ef56b78SMuchun Song static unsigned long 23411a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 23429e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 23431da177e4SLinus Torvalds { 23441da177e4SLinus Torvalds LIST_HEAD(page_list); 2345e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 2346730ec8c0SManinder Singh unsigned int nr_reclaimed = 0; 2347e247dbceSKOSAKI Motohiro unsigned long nr_taken; 2348060f005fSKirill Tkhai struct reclaim_stat stat; 2349497a6c1bSJohannes Weiner bool file = is_file_lru(lru); 2350f46b7912SKirill Tkhai enum vm_event_item item; 2351599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 2352db73ee0dSMichal Hocko bool stalled = false; 235378dc583dSKOSAKI Motohiro 2354599d0c95SMel Gorman while (unlikely(too_many_isolated(pgdat, file, sc))) { 2355db73ee0dSMichal Hocko if (stalled) 2356db73ee0dSMichal Hocko return 0; 2357db73ee0dSMichal Hocko 2358db73ee0dSMichal Hocko /* wait a bit for the reclaimer. */ 2359db73ee0dSMichal Hocko stalled = true; 2360c3f4a9a2SMel Gorman reclaim_throttle(pgdat, VMSCAN_THROTTLE_ISOLATED); 236135cd7815SRik van Riel 236235cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 236335cd7815SRik van Riel if (fatal_signal_pending(current)) 236435cd7815SRik van Riel return SWAP_CLUSTER_MAX; 236535cd7815SRik van Riel } 236635cd7815SRik van Riel 23671da177e4SLinus Torvalds lru_add_drain(); 2368f80c0673SMinchan Kim 23696168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 23701da177e4SLinus Torvalds 23715dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 2372a9e7c39fSKirill Tkhai &nr_scanned, sc, lru); 237395d918fcSKonstantin Khlebnikov 2374599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 2375f46b7912SKirill Tkhai item = current_is_kswapd() ? PGSCAN_KSWAPD : PGSCAN_DIRECT; 2376b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 2377f46b7912SKirill Tkhai __count_vm_events(item, nr_scanned); 2378f46b7912SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned); 2379497a6c1bSJohannes Weiner __count_vm_events(PGSCAN_ANON + file, nr_scanned); 2380497a6c1bSJohannes Weiner 23816168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 2382d563c050SHillf Danton 2383d563c050SHillf Danton if (nr_taken == 0) 238466635629SMel Gorman return 0; 2385b35ea17bSKOSAKI Motohiro 2386013339dfSShakeel Butt nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, &stat, false); 2387c661b078SAndy Whitcroft 23886168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 2389497a6c1bSJohannes Weiner move_pages_to_lru(lruvec, &page_list); 2390497a6c1bSJohannes Weiner 2391497a6c1bSJohannes Weiner __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 2392f46b7912SKirill Tkhai item = current_is_kswapd() ? PGSTEAL_KSWAPD : PGSTEAL_DIRECT; 2393b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 2394f46b7912SKirill Tkhai __count_vm_events(item, nr_reclaimed); 2395f46b7912SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed); 2396497a6c1bSJohannes Weiner __count_vm_events(PGSTEAL_ANON + file, nr_reclaimed); 23976168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 23983f79768fSHugh Dickins 239975cc3c91SAlex Shi lru_note_cost(lruvec, file, stat.nr_pageout); 2400747db954SJohannes Weiner mem_cgroup_uncharge_list(&page_list); 24012d4894b5SMel Gorman free_unref_page_list(&page_list); 2402e11da5b4SMel Gorman 240392df3a72SMel Gorman /* 24041c610d5fSAndrey Ryabinin * If dirty pages are scanned that are not queued for IO, it 24051c610d5fSAndrey Ryabinin * implies that flushers are not doing their job. This can 24061c610d5fSAndrey Ryabinin * happen when memory pressure pushes dirty pages to the end of 24071c610d5fSAndrey Ryabinin * the LRU before the dirty limits are breached and the dirty 24081c610d5fSAndrey Ryabinin * data has expired. It can also happen when the proportion of 24091c610d5fSAndrey Ryabinin * dirty pages grows not through writes but through memory 24101c610d5fSAndrey Ryabinin * pressure reclaiming all the clean cache. And in some cases, 24111c610d5fSAndrey Ryabinin * the flushers simply cannot keep up with the allocation 24121c610d5fSAndrey Ryabinin * rate. Nudge the flusher threads in case they are asleep. 24131c610d5fSAndrey Ryabinin */ 24141c610d5fSAndrey Ryabinin if (stat.nr_unqueued_dirty == nr_taken) 24151c610d5fSAndrey Ryabinin wakeup_flusher_threads(WB_REASON_VMSCAN); 24161c610d5fSAndrey Ryabinin 2417d108c772SAndrey Ryabinin sc->nr.dirty += stat.nr_dirty; 2418d108c772SAndrey Ryabinin sc->nr.congested += stat.nr_congested; 2419d108c772SAndrey Ryabinin sc->nr.unqueued_dirty += stat.nr_unqueued_dirty; 2420d108c772SAndrey Ryabinin sc->nr.writeback += stat.nr_writeback; 2421d108c772SAndrey Ryabinin sc->nr.immediate += stat.nr_immediate; 2422d108c772SAndrey Ryabinin sc->nr.taken += nr_taken; 2423d108c772SAndrey Ryabinin if (file) 2424d108c772SAndrey Ryabinin sc->nr.file_taken += nr_taken; 24258e950282SMel Gorman 2426599d0c95SMel Gorman trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, 2427d51d1e64SSteven Rostedt nr_scanned, nr_reclaimed, &stat, sc->priority, file); 242805ff5137SAndrew Morton return nr_reclaimed; 24291da177e4SLinus Torvalds } 24301da177e4SLinus Torvalds 243115b44736SHugh Dickins /* 243215b44736SHugh Dickins * shrink_active_list() moves pages from the active LRU to the inactive LRU. 243315b44736SHugh Dickins * 243415b44736SHugh Dickins * We move them the other way if the page is referenced by one or more 243515b44736SHugh Dickins * processes. 243615b44736SHugh Dickins * 243715b44736SHugh Dickins * If the pages are mostly unmapped, the processing is fast and it is 243815b44736SHugh Dickins * appropriate to hold lru_lock across the whole operation. But if 2439b3ac0413SMatthew Wilcox (Oracle) * the pages are mapped, the processing is slow (folio_referenced()), so 244015b44736SHugh Dickins * we should drop lru_lock around each page. It's impossible to balance 244115b44736SHugh Dickins * this, so instead we remove the pages from the LRU while processing them. 244215b44736SHugh Dickins * It is safe to rely on PG_active against the non-LRU pages in here because 244315b44736SHugh Dickins * nobody will play with that bit on a non-LRU page. 244415b44736SHugh Dickins * 244515b44736SHugh Dickins * The downside is that we have to touch page->_refcount against each page. 244615b44736SHugh Dickins * But we had to alter page->flags anyway. 244715b44736SHugh Dickins */ 2448f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 24491a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 2450f16015fbSJohannes Weiner struct scan_control *sc, 24519e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 24521cfb419bSKAMEZAWA Hiroyuki { 245344c241f1SKOSAKI Motohiro unsigned long nr_taken; 2454f626012dSHugh Dickins unsigned long nr_scanned; 24556fe6b7e3SWu Fengguang unsigned long vm_flags; 24561cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 24578cab4754SWu Fengguang LIST_HEAD(l_active); 2458b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 24599d998b4fSMichal Hocko unsigned nr_deactivate, nr_activate; 24609d998b4fSMichal Hocko unsigned nr_rotated = 0; 24613cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 2462599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 24631cfb419bSKAMEZAWA Hiroyuki 24641da177e4SLinus Torvalds lru_add_drain(); 2465f80c0673SMinchan Kim 24666168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 2467925b7673SJohannes Weiner 24685dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 2469a9e7c39fSKirill Tkhai &nr_scanned, sc, lru); 247089b5fae5SJohannes Weiner 2471599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 24721cfb419bSKAMEZAWA Hiroyuki 2473912c0572SShakeel Butt if (!cgroup_reclaim(sc)) 2474599d0c95SMel Gorman __count_vm_events(PGREFILL, nr_scanned); 24752fa2690cSYafang Shao __count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned); 24769d5e6a9fSHugh Dickins 24776168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 24781da177e4SLinus Torvalds 24791da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 2480b3ac0413SMatthew Wilcox (Oracle) struct folio *folio; 2481b3ac0413SMatthew Wilcox (Oracle) struct page *page; 2482b3ac0413SMatthew Wilcox (Oracle) 24831da177e4SLinus Torvalds cond_resched(); 2484b3ac0413SMatthew Wilcox (Oracle) folio = lru_to_folio(&l_hold); 2485b3ac0413SMatthew Wilcox (Oracle) list_del(&folio->lru); 2486b3ac0413SMatthew Wilcox (Oracle) page = &folio->page; 24877e9cd484SRik van Riel 248839b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 2489894bc310SLee Schermerhorn putback_lru_page(page); 2490894bc310SLee Schermerhorn continue; 2491894bc310SLee Schermerhorn } 2492894bc310SLee Schermerhorn 2493cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 2494cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 2495cc715d99SMel Gorman if (page_has_private(page)) 2496cc715d99SMel Gorman try_to_release_page(page, 0); 2497cc715d99SMel Gorman unlock_page(page); 2498cc715d99SMel Gorman } 2499cc715d99SMel Gorman } 2500cc715d99SMel Gorman 25016d4675e6SMinchan Kim /* Referenced or rmap lock contention: rotate */ 2502b3ac0413SMatthew Wilcox (Oracle) if (folio_referenced(folio, 0, sc->target_mem_cgroup, 25036d4675e6SMinchan Kim &vm_flags) != 0) { 25048cab4754SWu Fengguang /* 25058cab4754SWu Fengguang * Identify referenced, file-backed active pages and 25068cab4754SWu Fengguang * give them one more trip around the active list. So 25078cab4754SWu Fengguang * that executable code get better chances to stay in 25088cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 25098cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 25108cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 25118cab4754SWu Fengguang * so we ignore them here. 25128cab4754SWu Fengguang */ 25139de4f22aSHuang Ying if ((vm_flags & VM_EXEC) && page_is_file_lru(page)) { 25146c357848SMatthew Wilcox (Oracle) nr_rotated += thp_nr_pages(page); 25158cab4754SWu Fengguang list_add(&page->lru, &l_active); 25168cab4754SWu Fengguang continue; 25178cab4754SWu Fengguang } 25188cab4754SWu Fengguang } 25197e9cd484SRik van Riel 25205205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 25211899ad18SJohannes Weiner SetPageWorkingset(page); 25221da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 25231da177e4SLinus Torvalds } 25241da177e4SLinus Torvalds 2525b555749aSAndrew Morton /* 25268cab4754SWu Fengguang * Move pages back to the lru list. 2527b555749aSAndrew Morton */ 25286168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 2529556adecbSRik van Riel 2530a222f341SKirill Tkhai nr_activate = move_pages_to_lru(lruvec, &l_active); 2531a222f341SKirill Tkhai nr_deactivate = move_pages_to_lru(lruvec, &l_inactive); 2532f372d89eSKirill Tkhai /* Keep all free pages in l_active list */ 2533f372d89eSKirill Tkhai list_splice(&l_inactive, &l_active); 25349851ac13SKirill Tkhai 25359851ac13SKirill Tkhai __count_vm_events(PGDEACTIVATE, nr_deactivate); 25369851ac13SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate); 25379851ac13SKirill Tkhai 2538599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 25396168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 25402bcf8879SHugh Dickins 2541f372d89eSKirill Tkhai mem_cgroup_uncharge_list(&l_active); 2542f372d89eSKirill Tkhai free_unref_page_list(&l_active); 25439d998b4fSMichal Hocko trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate, 25449d998b4fSMichal Hocko nr_deactivate, nr_rotated, sc->priority, file); 25451da177e4SLinus Torvalds } 25461da177e4SLinus Torvalds 25471fe47c0bSMiaohe Lin static unsigned int reclaim_page_list(struct list_head *page_list, 25481fe47c0bSMiaohe Lin struct pglist_data *pgdat) 25491a4e58ccSMinchan Kim { 25501a4e58ccSMinchan Kim struct reclaim_stat dummy_stat; 25511fe47c0bSMiaohe Lin unsigned int nr_reclaimed; 25521fe47c0bSMiaohe Lin struct folio *folio; 25531a4e58ccSMinchan Kim struct scan_control sc = { 25541a4e58ccSMinchan Kim .gfp_mask = GFP_KERNEL, 25551a4e58ccSMinchan Kim .may_writepage = 1, 25561a4e58ccSMinchan Kim .may_unmap = 1, 25571a4e58ccSMinchan Kim .may_swap = 1, 255826aa2d19SDave Hansen .no_demotion = 1, 25591a4e58ccSMinchan Kim }; 25601a4e58ccSMinchan Kim 25611fe47c0bSMiaohe Lin nr_reclaimed = shrink_page_list(page_list, pgdat, &sc, &dummy_stat, false); 25621fe47c0bSMiaohe Lin while (!list_empty(page_list)) { 25631fe47c0bSMiaohe Lin folio = lru_to_folio(page_list); 25641fe47c0bSMiaohe Lin list_del(&folio->lru); 25651fe47c0bSMiaohe Lin folio_putback_lru(folio); 25661fe47c0bSMiaohe Lin } 25671fe47c0bSMiaohe Lin 25681fe47c0bSMiaohe Lin return nr_reclaimed; 25691fe47c0bSMiaohe Lin } 25701fe47c0bSMiaohe Lin 25711fe47c0bSMiaohe Lin unsigned long reclaim_pages(struct list_head *page_list) 25721fe47c0bSMiaohe Lin { 2573ed657e55SWei Yang int nid; 25741fe47c0bSMiaohe Lin unsigned int nr_reclaimed = 0; 25751fe47c0bSMiaohe Lin LIST_HEAD(node_page_list); 25761fe47c0bSMiaohe Lin struct page *page; 25771fe47c0bSMiaohe Lin unsigned int noreclaim_flag; 25781fe47c0bSMiaohe Lin 25791ae65e27SWei Yang if (list_empty(page_list)) 25801ae65e27SWei Yang return nr_reclaimed; 25811ae65e27SWei Yang 25822d2b8d2bSYu Zhao noreclaim_flag = memalloc_noreclaim_save(); 25832d2b8d2bSYu Zhao 2584ed657e55SWei Yang nid = page_to_nid(lru_to_page(page_list)); 25851ae65e27SWei Yang do { 25861a4e58ccSMinchan Kim page = lru_to_page(page_list); 25871a4e58ccSMinchan Kim 25881a4e58ccSMinchan Kim if (nid == page_to_nid(page)) { 25891a4e58ccSMinchan Kim ClearPageActive(page); 25901a4e58ccSMinchan Kim list_move(&page->lru, &node_page_list); 25911a4e58ccSMinchan Kim continue; 25921a4e58ccSMinchan Kim } 25931a4e58ccSMinchan Kim 25941fe47c0bSMiaohe Lin nr_reclaimed += reclaim_page_list(&node_page_list, NODE_DATA(nid)); 2595ed657e55SWei Yang nid = page_to_nid(lru_to_page(page_list)); 25961ae65e27SWei Yang } while (!list_empty(page_list)); 25971a4e58ccSMinchan Kim 25981fe47c0bSMiaohe Lin nr_reclaimed += reclaim_page_list(&node_page_list, NODE_DATA(nid)); 25991a4e58ccSMinchan Kim 26002d2b8d2bSYu Zhao memalloc_noreclaim_restore(noreclaim_flag); 26012d2b8d2bSYu Zhao 26021a4e58ccSMinchan Kim return nr_reclaimed; 26031a4e58ccSMinchan Kim } 26041a4e58ccSMinchan Kim 2605b91ac374SJohannes Weiner static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 2606b91ac374SJohannes Weiner struct lruvec *lruvec, struct scan_control *sc) 2607b91ac374SJohannes Weiner { 2608b91ac374SJohannes Weiner if (is_active_lru(lru)) { 2609b91ac374SJohannes Weiner if (sc->may_deactivate & (1 << is_file_lru(lru))) 2610b91ac374SJohannes Weiner shrink_active_list(nr_to_scan, lruvec, sc, lru); 2611b91ac374SJohannes Weiner else 2612b91ac374SJohannes Weiner sc->skipped_deactivate = 1; 2613b91ac374SJohannes Weiner return 0; 2614b91ac374SJohannes Weiner } 2615b91ac374SJohannes Weiner 2616b91ac374SJohannes Weiner return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 2617b91ac374SJohannes Weiner } 2618b91ac374SJohannes Weiner 261959dc76b0SRik van Riel /* 262059dc76b0SRik van Riel * The inactive anon list should be small enough that the VM never has 262159dc76b0SRik van Riel * to do too much work. 262214797e23SKOSAKI Motohiro * 262359dc76b0SRik van Riel * The inactive file list should be small enough to leave most memory 262459dc76b0SRik van Riel * to the established workingset on the scan-resistant active list, 262559dc76b0SRik van Riel * but large enough to avoid thrashing the aggregate readahead window. 262659dc76b0SRik van Riel * 262759dc76b0SRik van Riel * Both inactive lists should also be large enough that each inactive 262859dc76b0SRik van Riel * page has a chance to be referenced again before it is reclaimed. 262959dc76b0SRik van Riel * 26302a2e4885SJohannes Weiner * If that fails and refaulting is observed, the inactive list grows. 26312a2e4885SJohannes Weiner * 263259dc76b0SRik van Riel * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages 26333a50d14dSAndrey Ryabinin * on this LRU, maintained by the pageout code. An inactive_ratio 263459dc76b0SRik van Riel * of 3 means 3:1 or 25% of the pages are kept on the inactive list. 263559dc76b0SRik van Riel * 263659dc76b0SRik van Riel * total target max 263759dc76b0SRik van Riel * memory ratio inactive 263859dc76b0SRik van Riel * ------------------------------------- 263959dc76b0SRik van Riel * 10MB 1 5MB 264059dc76b0SRik van Riel * 100MB 1 50MB 264159dc76b0SRik van Riel * 1GB 3 250MB 264259dc76b0SRik van Riel * 10GB 10 0.9GB 264359dc76b0SRik van Riel * 100GB 31 3GB 264459dc76b0SRik van Riel * 1TB 101 10GB 264559dc76b0SRik van Riel * 10TB 320 32GB 264614797e23SKOSAKI Motohiro */ 2647b91ac374SJohannes Weiner static bool inactive_is_low(struct lruvec *lruvec, enum lru_list inactive_lru) 264814797e23SKOSAKI Motohiro { 2649b91ac374SJohannes Weiner enum lru_list active_lru = inactive_lru + LRU_ACTIVE; 26502a2e4885SJohannes Weiner unsigned long inactive, active; 26512a2e4885SJohannes Weiner unsigned long inactive_ratio; 265259dc76b0SRik van Riel unsigned long gb; 265359dc76b0SRik van Riel 2654b91ac374SJohannes Weiner inactive = lruvec_page_state(lruvec, NR_LRU_BASE + inactive_lru); 2655b91ac374SJohannes Weiner active = lruvec_page_state(lruvec, NR_LRU_BASE + active_lru); 2656f8d1a311SMel Gorman 265759dc76b0SRik van Riel gb = (inactive + active) >> (30 - PAGE_SHIFT); 26584002570cSJoonsoo Kim if (gb) 265959dc76b0SRik van Riel inactive_ratio = int_sqrt(10 * gb); 2660b39415b2SRik van Riel else 266159dc76b0SRik van Riel inactive_ratio = 1; 2662fd538803SMichal Hocko 266359dc76b0SRik van Riel return inactive * inactive_ratio < active; 2664b39415b2SRik van Riel } 2665b39415b2SRik van Riel 26669a265114SJohannes Weiner enum scan_balance { 26679a265114SJohannes Weiner SCAN_EQUAL, 26689a265114SJohannes Weiner SCAN_FRACT, 26699a265114SJohannes Weiner SCAN_ANON, 26709a265114SJohannes Weiner SCAN_FILE, 26719a265114SJohannes Weiner }; 26729a265114SJohannes Weiner 26731da177e4SLinus Torvalds /* 26744f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 267502e458d8SMiaohe Lin * scanned. 26764f98a2feSRik van Riel * 2677be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 2678be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 26794f98a2feSRik van Riel */ 2680afaf07a6SJohannes Weiner static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, 2681afaf07a6SJohannes Weiner unsigned long *nr) 26824f98a2feSRik van Riel { 2683a2a36488SKeith Busch struct pglist_data *pgdat = lruvec_pgdat(lruvec); 2684afaf07a6SJohannes Weiner struct mem_cgroup *memcg = lruvec_memcg(lruvec); 2685d483a5ddSJohannes Weiner unsigned long anon_cost, file_cost, total_cost; 268633377678SVladimir Davydov int swappiness = mem_cgroup_swappiness(memcg); 2687ed017373SYu Zhao u64 fraction[ANON_AND_FILE]; 26889a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 26899a265114SJohannes Weiner enum scan_balance scan_balance; 26909a265114SJohannes Weiner unsigned long ap, fp; 26919a265114SJohannes Weiner enum lru_list lru; 269276a33fc3SShaohua Li 269376a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 2694a2a36488SKeith Busch if (!sc->may_swap || !can_reclaim_anon_pages(memcg, pgdat->node_id, sc)) { 26959a265114SJohannes Weiner scan_balance = SCAN_FILE; 269676a33fc3SShaohua Li goto out; 269776a33fc3SShaohua Li } 26984f98a2feSRik van Riel 269910316b31SJohannes Weiner /* 270010316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 270110316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 270210316b31SJohannes Weiner * disable swapping for individual groups completely when 270310316b31SJohannes Weiner * using the memory controller's swap limit feature would be 270410316b31SJohannes Weiner * too expensive. 270510316b31SJohannes Weiner */ 2706b5ead35eSJohannes Weiner if (cgroup_reclaim(sc) && !swappiness) { 27079a265114SJohannes Weiner scan_balance = SCAN_FILE; 270810316b31SJohannes Weiner goto out; 270910316b31SJohannes Weiner } 271010316b31SJohannes Weiner 271110316b31SJohannes Weiner /* 271210316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 271310316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 271410316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 271510316b31SJohannes Weiner */ 271602695175SJohannes Weiner if (!sc->priority && swappiness) { 27179a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 271810316b31SJohannes Weiner goto out; 271910316b31SJohannes Weiner } 272010316b31SJohannes Weiner 272111d16c25SJohannes Weiner /* 272253138ceaSJohannes Weiner * If the system is almost out of file pages, force-scan anon. 272362376251SJohannes Weiner */ 2724b91ac374SJohannes Weiner if (sc->file_is_tiny) { 272562376251SJohannes Weiner scan_balance = SCAN_ANON; 272662376251SJohannes Weiner goto out; 272762376251SJohannes Weiner } 272862376251SJohannes Weiner 272962376251SJohannes Weiner /* 2730b91ac374SJohannes Weiner * If there is enough inactive page cache, we do not reclaim 2731b91ac374SJohannes Weiner * anything from the anonymous working right now. 2732e9868505SRik van Riel */ 2733b91ac374SJohannes Weiner if (sc->cache_trim_mode) { 27349a265114SJohannes Weiner scan_balance = SCAN_FILE; 2735e9868505SRik van Riel goto out; 27364f98a2feSRik van Riel } 27374f98a2feSRik van Riel 27389a265114SJohannes Weiner scan_balance = SCAN_FRACT; 27394f98a2feSRik van Riel /* 2740314b57fbSJohannes Weiner * Calculate the pressure balance between anon and file pages. 2741314b57fbSJohannes Weiner * 2742314b57fbSJohannes Weiner * The amount of pressure we put on each LRU is inversely 2743314b57fbSJohannes Weiner * proportional to the cost of reclaiming each list, as 2744314b57fbSJohannes Weiner * determined by the share of pages that are refaulting, times 2745314b57fbSJohannes Weiner * the relative IO cost of bringing back a swapped out 2746314b57fbSJohannes Weiner * anonymous page vs reloading a filesystem page (swappiness). 2747314b57fbSJohannes Weiner * 2748d483a5ddSJohannes Weiner * Although we limit that influence to ensure no list gets 2749d483a5ddSJohannes Weiner * left behind completely: at least a third of the pressure is 2750d483a5ddSJohannes Weiner * applied, before swappiness. 2751d483a5ddSJohannes Weiner * 2752314b57fbSJohannes Weiner * With swappiness at 100, anon and file have equal IO cost. 275358c37f6eSKOSAKI Motohiro */ 2754d483a5ddSJohannes Weiner total_cost = sc->anon_cost + sc->file_cost; 2755d483a5ddSJohannes Weiner anon_cost = total_cost + sc->anon_cost; 2756d483a5ddSJohannes Weiner file_cost = total_cost + sc->file_cost; 2757d483a5ddSJohannes Weiner total_cost = anon_cost + file_cost; 275858c37f6eSKOSAKI Motohiro 2759d483a5ddSJohannes Weiner ap = swappiness * (total_cost + 1); 2760d483a5ddSJohannes Weiner ap /= anon_cost + 1; 27614f98a2feSRik van Riel 2762d483a5ddSJohannes Weiner fp = (200 - swappiness) * (total_cost + 1); 2763d483a5ddSJohannes Weiner fp /= file_cost + 1; 27644f98a2feSRik van Riel 276576a33fc3SShaohua Li fraction[0] = ap; 276676a33fc3SShaohua Li fraction[1] = fp; 2767a4fe1631SJohannes Weiner denominator = ap + fp; 276876a33fc3SShaohua Li out: 27694111304dSHugh Dickins for_each_evictable_lru(lru) { 27704111304dSHugh Dickins int file = is_file_lru(lru); 27719783aa99SChris Down unsigned long lruvec_size; 2772f56ce412SJohannes Weiner unsigned long low, min; 277376a33fc3SShaohua Li unsigned long scan; 277476a33fc3SShaohua Li 27759783aa99SChris Down lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); 2776f56ce412SJohannes Weiner mem_cgroup_protection(sc->target_mem_cgroup, memcg, 2777f56ce412SJohannes Weiner &min, &low); 27789783aa99SChris Down 2779f56ce412SJohannes Weiner if (min || low) { 27809783aa99SChris Down /* 27819783aa99SChris Down * Scale a cgroup's reclaim pressure by proportioning 27829783aa99SChris Down * its current usage to its memory.low or memory.min 27839783aa99SChris Down * setting. 27849783aa99SChris Down * 27859783aa99SChris Down * This is important, as otherwise scanning aggression 27869783aa99SChris Down * becomes extremely binary -- from nothing as we 27879783aa99SChris Down * approach the memory protection threshold, to totally 27889783aa99SChris Down * nominal as we exceed it. This results in requiring 27899783aa99SChris Down * setting extremely liberal protection thresholds. It 27909783aa99SChris Down * also means we simply get no protection at all if we 27919783aa99SChris Down * set it too low, which is not ideal. 27921bc63fb1SChris Down * 27931bc63fb1SChris Down * If there is any protection in place, we reduce scan 27941bc63fb1SChris Down * pressure by how much of the total memory used is 27951bc63fb1SChris Down * within protection thresholds. 27969783aa99SChris Down * 27979de7ca46SChris Down * There is one special case: in the first reclaim pass, 27989de7ca46SChris Down * we skip over all groups that are within their low 27999de7ca46SChris Down * protection. If that fails to reclaim enough pages to 28009de7ca46SChris Down * satisfy the reclaim goal, we come back and override 28019de7ca46SChris Down * the best-effort low protection. However, we still 28029de7ca46SChris Down * ideally want to honor how well-behaved groups are in 28039de7ca46SChris Down * that case instead of simply punishing them all 28049de7ca46SChris Down * equally. As such, we reclaim them based on how much 28051bc63fb1SChris Down * memory they are using, reducing the scan pressure 28061bc63fb1SChris Down * again by how much of the total memory used is under 28071bc63fb1SChris Down * hard protection. 28089783aa99SChris Down */ 28091bc63fb1SChris Down unsigned long cgroup_size = mem_cgroup_size(memcg); 2810f56ce412SJohannes Weiner unsigned long protection; 2811f56ce412SJohannes Weiner 2812f56ce412SJohannes Weiner /* memory.low scaling, make sure we retry before OOM */ 2813f56ce412SJohannes Weiner if (!sc->memcg_low_reclaim && low > min) { 2814f56ce412SJohannes Weiner protection = low; 2815f56ce412SJohannes Weiner sc->memcg_low_skipped = 1; 2816f56ce412SJohannes Weiner } else { 2817f56ce412SJohannes Weiner protection = min; 2818f56ce412SJohannes Weiner } 28191bc63fb1SChris Down 28201bc63fb1SChris Down /* Avoid TOCTOU with earlier protection check */ 28211bc63fb1SChris Down cgroup_size = max(cgroup_size, protection); 28221bc63fb1SChris Down 28231bc63fb1SChris Down scan = lruvec_size - lruvec_size * protection / 282432d4f4b7SRik van Riel (cgroup_size + 1); 28259783aa99SChris Down 28269783aa99SChris Down /* 28271bc63fb1SChris Down * Minimally target SWAP_CLUSTER_MAX pages to keep 282855b65a57SEthon Paul * reclaim moving forwards, avoiding decrementing 28299de7ca46SChris Down * sc->priority further than desirable. 28309783aa99SChris Down */ 28311bc63fb1SChris Down scan = max(scan, SWAP_CLUSTER_MAX); 28329783aa99SChris Down } else { 28339783aa99SChris Down scan = lruvec_size; 28349783aa99SChris Down } 28359783aa99SChris Down 28369783aa99SChris Down scan >>= sc->priority; 28379783aa99SChris Down 2838688035f7SJohannes Weiner /* 2839688035f7SJohannes Weiner * If the cgroup's already been deleted, make sure to 2840688035f7SJohannes Weiner * scrape out the remaining cache. 2841688035f7SJohannes Weiner */ 2842688035f7SJohannes Weiner if (!scan && !mem_cgroup_online(memcg)) 28439783aa99SChris Down scan = min(lruvec_size, SWAP_CLUSTER_MAX); 28449a265114SJohannes Weiner 28459a265114SJohannes Weiner switch (scan_balance) { 28469a265114SJohannes Weiner case SCAN_EQUAL: 28479a265114SJohannes Weiner /* Scan lists relative to size */ 28489a265114SJohannes Weiner break; 28499a265114SJohannes Weiner case SCAN_FRACT: 28509a265114SJohannes Weiner /* 28519a265114SJohannes Weiner * Scan types proportional to swappiness and 28529a265114SJohannes Weiner * their relative recent reclaim efficiency. 285376073c64SGavin Shan * Make sure we don't miss the last page on 285476073c64SGavin Shan * the offlined memory cgroups because of a 285576073c64SGavin Shan * round-off error. 28569a265114SJohannes Weiner */ 285776073c64SGavin Shan scan = mem_cgroup_online(memcg) ? 285876073c64SGavin Shan div64_u64(scan * fraction[file], denominator) : 285976073c64SGavin Shan DIV64_U64_ROUND_UP(scan * fraction[file], 28606f04f48dSSuleiman Souhlal denominator); 28619a265114SJohannes Weiner break; 28629a265114SJohannes Weiner case SCAN_FILE: 28639a265114SJohannes Weiner case SCAN_ANON: 28649a265114SJohannes Weiner /* Scan one type exclusively */ 2865e072bff6SMateusz Nosek if ((scan_balance == SCAN_FILE) != file) 28669a265114SJohannes Weiner scan = 0; 28679a265114SJohannes Weiner break; 28689a265114SJohannes Weiner default: 28699a265114SJohannes Weiner /* Look ma, no brain */ 28709a265114SJohannes Weiner BUG(); 28719a265114SJohannes Weiner } 28726b4f7799SJohannes Weiner 28734111304dSHugh Dickins nr[lru] = scan; 287476a33fc3SShaohua Li } 28756e08a369SWu Fengguang } 28764f98a2feSRik van Riel 28772f368a9fSDave Hansen /* 28782f368a9fSDave Hansen * Anonymous LRU management is a waste if there is 28792f368a9fSDave Hansen * ultimately no way to reclaim the memory. 28802f368a9fSDave Hansen */ 28812f368a9fSDave Hansen static bool can_age_anon_pages(struct pglist_data *pgdat, 28822f368a9fSDave Hansen struct scan_control *sc) 28832f368a9fSDave Hansen { 28842f368a9fSDave Hansen /* Aging the anon LRU is valuable if swap is present: */ 28852f368a9fSDave Hansen if (total_swap_pages > 0) 28862f368a9fSDave Hansen return true; 28872f368a9fSDave Hansen 28882f368a9fSDave Hansen /* Also valuable if anon pages can be demoted: */ 28892f368a9fSDave Hansen return can_demote(pgdat->node_id, sc); 28902f368a9fSDave Hansen } 28912f368a9fSDave Hansen 2892afaf07a6SJohannes Weiner static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) 28939b4f98cdSJohannes Weiner { 28949b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 2895e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 28969b4f98cdSJohannes Weiner unsigned long nr_to_scan; 28979b4f98cdSJohannes Weiner enum lru_list lru; 28989b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 28999b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 29009b4f98cdSJohannes Weiner struct blk_plug plug; 29011a501907SMel Gorman bool scan_adjusted; 29029b4f98cdSJohannes Weiner 2903afaf07a6SJohannes Weiner get_scan_count(lruvec, sc, nr); 29049b4f98cdSJohannes Weiner 2905e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 2906e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 2907e82e0561SMel Gorman 29081a501907SMel Gorman /* 29091a501907SMel Gorman * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal 29101a501907SMel Gorman * event that can occur when there is little memory pressure e.g. 29111a501907SMel Gorman * multiple streaming readers/writers. Hence, we do not abort scanning 29121a501907SMel Gorman * when the requested number of pages are reclaimed when scanning at 29131a501907SMel Gorman * DEF_PRIORITY on the assumption that the fact we are direct 29141a501907SMel Gorman * reclaiming implies that kswapd is not keeping up and it is best to 29151a501907SMel Gorman * do a batch of work at once. For memcg reclaim one check is made to 29161a501907SMel Gorman * abort proportional reclaim if either the file or anon lru has already 29171a501907SMel Gorman * dropped to zero at the first pass. 29181a501907SMel Gorman */ 2919b5ead35eSJohannes Weiner scan_adjusted = (!cgroup_reclaim(sc) && !current_is_kswapd() && 29201a501907SMel Gorman sc->priority == DEF_PRIORITY); 29211a501907SMel Gorman 29229b4f98cdSJohannes Weiner blk_start_plug(&plug); 29239b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 29249b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 2925e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 2926e82e0561SMel Gorman unsigned long nr_scanned; 2927e82e0561SMel Gorman 29289b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 29299b4f98cdSJohannes Weiner if (nr[lru]) { 29309b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 29319b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 29329b4f98cdSJohannes Weiner 29339b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 29343b991208SJohannes Weiner lruvec, sc); 29359b4f98cdSJohannes Weiner } 29369b4f98cdSJohannes Weiner } 2937e82e0561SMel Gorman 2938bd041733SMichal Hocko cond_resched(); 2939bd041733SMichal Hocko 2940e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 2941e82e0561SMel Gorman continue; 2942e82e0561SMel Gorman 29439b4f98cdSJohannes Weiner /* 2944e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 29451a501907SMel Gorman * requested. Ensure that the anon and file LRUs are scanned 2946e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 2947e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 2948e82e0561SMel Gorman * proportional to the original scan target. 2949e82e0561SMel Gorman */ 2950e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 2951e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 2952e82e0561SMel Gorman 29531a501907SMel Gorman /* 29541a501907SMel Gorman * It's just vindictive to attack the larger once the smaller 29551a501907SMel Gorman * has gone to zero. And given the way we stop scanning the 29561a501907SMel Gorman * smaller below, this makes sure that we only make one nudge 29571a501907SMel Gorman * towards proportionality once we've got nr_to_reclaim. 29581a501907SMel Gorman */ 29591a501907SMel Gorman if (!nr_file || !nr_anon) 29601a501907SMel Gorman break; 29611a501907SMel Gorman 2962e82e0561SMel Gorman if (nr_file > nr_anon) { 2963e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 2964e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 2965e82e0561SMel Gorman lru = LRU_BASE; 2966e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 2967e82e0561SMel Gorman } else { 2968e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 2969e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 2970e82e0561SMel Gorman lru = LRU_FILE; 2971e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 2972e82e0561SMel Gorman } 2973e82e0561SMel Gorman 2974e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 2975e82e0561SMel Gorman nr[lru] = 0; 2976e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 2977e82e0561SMel Gorman 2978e82e0561SMel Gorman /* 2979e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 2980e82e0561SMel Gorman * scan target and the percentage scanning already complete 2981e82e0561SMel Gorman */ 2982e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 2983e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2984e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2985e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2986e82e0561SMel Gorman 2987e82e0561SMel Gorman lru += LRU_ACTIVE; 2988e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2989e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2990e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2991e82e0561SMel Gorman 2992e82e0561SMel Gorman scan_adjusted = true; 29939b4f98cdSJohannes Weiner } 29949b4f98cdSJohannes Weiner blk_finish_plug(&plug); 29959b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 29969b4f98cdSJohannes Weiner 29979b4f98cdSJohannes Weiner /* 29989b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 29999b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 30009b4f98cdSJohannes Weiner */ 30012f368a9fSDave Hansen if (can_age_anon_pages(lruvec_pgdat(lruvec), sc) && 30022f368a9fSDave Hansen inactive_is_low(lruvec, LRU_INACTIVE_ANON)) 30039b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 30049b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 30059b4f98cdSJohannes Weiner } 30069b4f98cdSJohannes Weiner 300723b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 30089e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 300923b9da55SMel Gorman { 3010d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 301123b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 30129e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 301323b9da55SMel Gorman return true; 301423b9da55SMel Gorman 301523b9da55SMel Gorman return false; 301623b9da55SMel Gorman } 301723b9da55SMel Gorman 30184f98a2feSRik van Riel /* 301923b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 302023b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 302123b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 3022df3a45f9SQiwu Chen * calls try_to_compact_pages() that it will have enough free pages to succeed. 302323b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 30243e7d3449SMel Gorman */ 3025a9dd0a83SMel Gorman static inline bool should_continue_reclaim(struct pglist_data *pgdat, 30263e7d3449SMel Gorman unsigned long nr_reclaimed, 30273e7d3449SMel Gorman struct scan_control *sc) 30283e7d3449SMel Gorman { 30293e7d3449SMel Gorman unsigned long pages_for_compaction; 30303e7d3449SMel Gorman unsigned long inactive_lru_pages; 3031a9dd0a83SMel Gorman int z; 30323e7d3449SMel Gorman 30333e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 30349e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 30353e7d3449SMel Gorman return false; 30363e7d3449SMel Gorman 30373e7d3449SMel Gorman /* 30385ee04716SVlastimil Babka * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX 30395ee04716SVlastimil Babka * number of pages that were scanned. This will return to the caller 30405ee04716SVlastimil Babka * with the risk reclaim/compaction and the resulting allocation attempt 30415ee04716SVlastimil Babka * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL 30425ee04716SVlastimil Babka * allocations through requiring that the full LRU list has been scanned 30435ee04716SVlastimil Babka * first, by assuming that zero delta of sc->nr_scanned means full LRU 30445ee04716SVlastimil Babka * scan, but that approximation was wrong, and there were corner cases 30455ee04716SVlastimil Babka * where always a non-zero amount of pages were scanned. 30462876592fSMel Gorman */ 30472876592fSMel Gorman if (!nr_reclaimed) 30482876592fSMel Gorman return false; 30493e7d3449SMel Gorman 30503e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 3051a9dd0a83SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 3052a9dd0a83SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 30536aa303deSMel Gorman if (!managed_zone(zone)) 3054a9dd0a83SMel Gorman continue; 3055a9dd0a83SMel Gorman 3056a9dd0a83SMel Gorman switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) { 3057cf378319SVlastimil Babka case COMPACT_SUCCESS: 30583e7d3449SMel Gorman case COMPACT_CONTINUE: 30593e7d3449SMel Gorman return false; 30603e7d3449SMel Gorman default: 3061a9dd0a83SMel Gorman /* check next zone */ 3062a9dd0a83SMel Gorman ; 30633e7d3449SMel Gorman } 30643e7d3449SMel Gorman } 30651c6c1597SHillf Danton 30661c6c1597SHillf Danton /* 30671c6c1597SHillf Danton * If we have not reclaimed enough pages for compaction and the 30681c6c1597SHillf Danton * inactive lists are large enough, continue reclaiming 30691c6c1597SHillf Danton */ 30701c6c1597SHillf Danton pages_for_compaction = compact_gap(sc->order); 30711c6c1597SHillf Danton inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); 3072a2a36488SKeith Busch if (can_reclaim_anon_pages(NULL, pgdat->node_id, sc)) 30731c6c1597SHillf Danton inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); 30741c6c1597SHillf Danton 30755ee04716SVlastimil Babka return inactive_lru_pages > pages_for_compaction; 3076a9dd0a83SMel Gorman } 30773e7d3449SMel Gorman 30780f6a5cffSJohannes Weiner static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc) 3079f16015fbSJohannes Weiner { 30800f6a5cffSJohannes Weiner struct mem_cgroup *target_memcg = sc->target_mem_cgroup; 3081694fbc0fSAndrew Morton struct mem_cgroup *memcg; 3082d108c772SAndrey Ryabinin 30830f6a5cffSJohannes Weiner memcg = mem_cgroup_iter(target_memcg, NULL, NULL); 3084694fbc0fSAndrew Morton do { 3085afaf07a6SJohannes Weiner struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); 30868e8ae645SJohannes Weiner unsigned long reclaimed; 3087cb731d6cSVladimir Davydov unsigned long scanned; 30889b4f98cdSJohannes Weiner 3089e3336cabSXunlei Pang /* 3090e3336cabSXunlei Pang * This loop can become CPU-bound when target memcgs 3091e3336cabSXunlei Pang * aren't eligible for reclaim - either because they 3092e3336cabSXunlei Pang * don't have any reclaimable pages, or because their 3093e3336cabSXunlei Pang * memory is explicitly protected. Avoid soft lockups. 3094e3336cabSXunlei Pang */ 3095e3336cabSXunlei Pang cond_resched(); 3096e3336cabSXunlei Pang 309745c7f7e1SChris Down mem_cgroup_calculate_protection(target_memcg, memcg); 309845c7f7e1SChris Down 309945c7f7e1SChris Down if (mem_cgroup_below_min(memcg)) { 3100bf8d5d52SRoman Gushchin /* 3101bf8d5d52SRoman Gushchin * Hard protection. 3102bf8d5d52SRoman Gushchin * If there is no reclaimable memory, OOM. 3103bf8d5d52SRoman Gushchin */ 3104bf8d5d52SRoman Gushchin continue; 310545c7f7e1SChris Down } else if (mem_cgroup_below_low(memcg)) { 3106bf8d5d52SRoman Gushchin /* 3107bf8d5d52SRoman Gushchin * Soft protection. 3108bf8d5d52SRoman Gushchin * Respect the protection only as long as 3109bf8d5d52SRoman Gushchin * there is an unprotected supply 3110bf8d5d52SRoman Gushchin * of reclaimable memory from other cgroups. 3111bf8d5d52SRoman Gushchin */ 3112d6622f63SYisheng Xie if (!sc->memcg_low_reclaim) { 3113d6622f63SYisheng Xie sc->memcg_low_skipped = 1; 3114241994edSJohannes Weiner continue; 3115d6622f63SYisheng Xie } 3116e27be240SJohannes Weiner memcg_memory_event(memcg, MEMCG_LOW); 3117241994edSJohannes Weiner } 3118241994edSJohannes Weiner 31198e8ae645SJohannes Weiner reclaimed = sc->nr_reclaimed; 3120cb731d6cSVladimir Davydov scanned = sc->nr_scanned; 3121afaf07a6SJohannes Weiner 3122afaf07a6SJohannes Weiner shrink_lruvec(lruvec, sc); 3123f9be23d6SKonstantin Khlebnikov 312428360f39SMel Gorman shrink_slab(sc->gfp_mask, pgdat->node_id, memcg, 312528360f39SMel Gorman sc->priority); 3126cb731d6cSVladimir Davydov 31278e8ae645SJohannes Weiner /* Record the group's reclaim efficiency */ 31288e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, memcg, false, 31298e8ae645SJohannes Weiner sc->nr_scanned - scanned, 31308e8ae645SJohannes Weiner sc->nr_reclaimed - reclaimed); 31318e8ae645SJohannes Weiner 31320f6a5cffSJohannes Weiner } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL))); 31330f6a5cffSJohannes Weiner } 31340f6a5cffSJohannes Weiner 31356c9e0907SLiu Song static void shrink_node(pg_data_t *pgdat, struct scan_control *sc) 31360f6a5cffSJohannes Weiner { 31370f6a5cffSJohannes Weiner struct reclaim_state *reclaim_state = current->reclaim_state; 31380f6a5cffSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 31391b05117dSJohannes Weiner struct lruvec *target_lruvec; 31400f6a5cffSJohannes Weiner bool reclaimable = false; 3141b91ac374SJohannes Weiner unsigned long file; 31420f6a5cffSJohannes Weiner 31431b05117dSJohannes Weiner target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat); 31441b05117dSJohannes Weiner 31450f6a5cffSJohannes Weiner again: 3146aa48e47eSShakeel Butt /* 3147aa48e47eSShakeel Butt * Flush the memory cgroup stats, so that we read accurate per-memcg 3148aa48e47eSShakeel Butt * lruvec stats for heuristics. 3149aa48e47eSShakeel Butt */ 3150aa48e47eSShakeel Butt mem_cgroup_flush_stats(); 3151aa48e47eSShakeel Butt 31520f6a5cffSJohannes Weiner memset(&sc->nr, 0, sizeof(sc->nr)); 31530f6a5cffSJohannes Weiner 31540f6a5cffSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 31550f6a5cffSJohannes Weiner nr_scanned = sc->nr_scanned; 31560f6a5cffSJohannes Weiner 315753138ceaSJohannes Weiner /* 31587cf111bcSJohannes Weiner * Determine the scan balance between anon and file LRUs. 31597cf111bcSJohannes Weiner */ 31606168d0daSAlex Shi spin_lock_irq(&target_lruvec->lru_lock); 31617cf111bcSJohannes Weiner sc->anon_cost = target_lruvec->anon_cost; 31627cf111bcSJohannes Weiner sc->file_cost = target_lruvec->file_cost; 31636168d0daSAlex Shi spin_unlock_irq(&target_lruvec->lru_lock); 31647cf111bcSJohannes Weiner 31657cf111bcSJohannes Weiner /* 3166b91ac374SJohannes Weiner * Target desirable inactive:active list ratios for the anon 3167b91ac374SJohannes Weiner * and file LRU lists. 3168b91ac374SJohannes Weiner */ 3169b91ac374SJohannes Weiner if (!sc->force_deactivate) { 3170b91ac374SJohannes Weiner unsigned long refaults; 3171b91ac374SJohannes Weiner 3172170b04b7SJoonsoo Kim refaults = lruvec_page_state(target_lruvec, 3173170b04b7SJoonsoo Kim WORKINGSET_ACTIVATE_ANON); 3174170b04b7SJoonsoo Kim if (refaults != target_lruvec->refaults[0] || 3175170b04b7SJoonsoo Kim inactive_is_low(target_lruvec, LRU_INACTIVE_ANON)) 3176b91ac374SJohannes Weiner sc->may_deactivate |= DEACTIVATE_ANON; 3177b91ac374SJohannes Weiner else 3178b91ac374SJohannes Weiner sc->may_deactivate &= ~DEACTIVATE_ANON; 3179b91ac374SJohannes Weiner 3180b91ac374SJohannes Weiner /* 3181b91ac374SJohannes Weiner * When refaults are being observed, it means a new 3182b91ac374SJohannes Weiner * workingset is being established. Deactivate to get 3183b91ac374SJohannes Weiner * rid of any stale active pages quickly. 3184b91ac374SJohannes Weiner */ 3185b91ac374SJohannes Weiner refaults = lruvec_page_state(target_lruvec, 3186170b04b7SJoonsoo Kim WORKINGSET_ACTIVATE_FILE); 3187170b04b7SJoonsoo Kim if (refaults != target_lruvec->refaults[1] || 3188b91ac374SJohannes Weiner inactive_is_low(target_lruvec, LRU_INACTIVE_FILE)) 3189b91ac374SJohannes Weiner sc->may_deactivate |= DEACTIVATE_FILE; 3190b91ac374SJohannes Weiner else 3191b91ac374SJohannes Weiner sc->may_deactivate &= ~DEACTIVATE_FILE; 3192b91ac374SJohannes Weiner } else 3193b91ac374SJohannes Weiner sc->may_deactivate = DEACTIVATE_ANON | DEACTIVATE_FILE; 3194b91ac374SJohannes Weiner 3195b91ac374SJohannes Weiner /* 3196b91ac374SJohannes Weiner * If we have plenty of inactive file pages that aren't 3197b91ac374SJohannes Weiner * thrashing, try to reclaim those first before touching 3198b91ac374SJohannes Weiner * anonymous pages. 3199b91ac374SJohannes Weiner */ 3200b91ac374SJohannes Weiner file = lruvec_page_state(target_lruvec, NR_INACTIVE_FILE); 3201b91ac374SJohannes Weiner if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE)) 3202b91ac374SJohannes Weiner sc->cache_trim_mode = 1; 3203b91ac374SJohannes Weiner else 3204b91ac374SJohannes Weiner sc->cache_trim_mode = 0; 3205b91ac374SJohannes Weiner 3206b91ac374SJohannes Weiner /* 320753138ceaSJohannes Weiner * Prevent the reclaimer from falling into the cache trap: as 320853138ceaSJohannes Weiner * cache pages start out inactive, every cache fault will tip 320953138ceaSJohannes Weiner * the scan balance towards the file LRU. And as the file LRU 321053138ceaSJohannes Weiner * shrinks, so does the window for rotation from references. 321153138ceaSJohannes Weiner * This means we have a runaway feedback loop where a tiny 321253138ceaSJohannes Weiner * thrashing file LRU becomes infinitely more attractive than 321353138ceaSJohannes Weiner * anon pages. Try to detect this based on file LRU size. 321453138ceaSJohannes Weiner */ 321553138ceaSJohannes Weiner if (!cgroup_reclaim(sc)) { 321653138ceaSJohannes Weiner unsigned long total_high_wmark = 0; 3217b91ac374SJohannes Weiner unsigned long free, anon; 3218b91ac374SJohannes Weiner int z; 321953138ceaSJohannes Weiner 322053138ceaSJohannes Weiner free = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); 322153138ceaSJohannes Weiner file = node_page_state(pgdat, NR_ACTIVE_FILE) + 322253138ceaSJohannes Weiner node_page_state(pgdat, NR_INACTIVE_FILE); 322353138ceaSJohannes Weiner 322453138ceaSJohannes Weiner for (z = 0; z < MAX_NR_ZONES; z++) { 322553138ceaSJohannes Weiner struct zone *zone = &pgdat->node_zones[z]; 322653138ceaSJohannes Weiner if (!managed_zone(zone)) 322753138ceaSJohannes Weiner continue; 322853138ceaSJohannes Weiner 322953138ceaSJohannes Weiner total_high_wmark += high_wmark_pages(zone); 323053138ceaSJohannes Weiner } 323153138ceaSJohannes Weiner 3232b91ac374SJohannes Weiner /* 3233b91ac374SJohannes Weiner * Consider anon: if that's low too, this isn't a 3234b91ac374SJohannes Weiner * runaway file reclaim problem, but rather just 3235b91ac374SJohannes Weiner * extreme pressure. Reclaim as per usual then. 3236b91ac374SJohannes Weiner */ 3237b91ac374SJohannes Weiner anon = node_page_state(pgdat, NR_INACTIVE_ANON); 3238b91ac374SJohannes Weiner 3239b91ac374SJohannes Weiner sc->file_is_tiny = 3240b91ac374SJohannes Weiner file + free <= total_high_wmark && 3241b91ac374SJohannes Weiner !(sc->may_deactivate & DEACTIVATE_ANON) && 3242b91ac374SJohannes Weiner anon >> sc->priority; 324353138ceaSJohannes Weiner } 324453138ceaSJohannes Weiner 32450f6a5cffSJohannes Weiner shrink_node_memcgs(pgdat, sc); 324670ddf637SAnton Vorontsov 32476b4f7799SJohannes Weiner if (reclaim_state) { 3248cb731d6cSVladimir Davydov sc->nr_reclaimed += reclaim_state->reclaimed_slab; 32496b4f7799SJohannes Weiner reclaim_state->reclaimed_slab = 0; 32506b4f7799SJohannes Weiner } 32516b4f7799SJohannes Weiner 32528e8ae645SJohannes Weiner /* Record the subtree's reclaim efficiency */ 32531b05117dSJohannes Weiner vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, 325470ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 325570ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 325670ddf637SAnton Vorontsov 32572344d7e4SJohannes Weiner if (sc->nr_reclaimed - nr_reclaimed) 32582344d7e4SJohannes Weiner reclaimable = true; 32592344d7e4SJohannes Weiner 3260e3c1ac58SAndrey Ryabinin if (current_is_kswapd()) { 3261d108c772SAndrey Ryabinin /* 3262e3c1ac58SAndrey Ryabinin * If reclaim is isolating dirty pages under writeback, 3263e3c1ac58SAndrey Ryabinin * it implies that the long-lived page allocation rate 3264e3c1ac58SAndrey Ryabinin * is exceeding the page laundering rate. Either the 3265e3c1ac58SAndrey Ryabinin * global limits are not being effective at throttling 3266e3c1ac58SAndrey Ryabinin * processes due to the page distribution throughout 3267e3c1ac58SAndrey Ryabinin * zones or there is heavy usage of a slow backing 3268e3c1ac58SAndrey Ryabinin * device. The only option is to throttle from reclaim 3269e3c1ac58SAndrey Ryabinin * context which is not ideal as there is no guarantee 3270d108c772SAndrey Ryabinin * the dirtying process is throttled in the same way 3271d108c772SAndrey Ryabinin * balance_dirty_pages() manages. 3272d108c772SAndrey Ryabinin * 3273e3c1ac58SAndrey Ryabinin * Once a node is flagged PGDAT_WRITEBACK, kswapd will 3274e3c1ac58SAndrey Ryabinin * count the number of pages under pages flagged for 3275e3c1ac58SAndrey Ryabinin * immediate reclaim and stall if any are encountered 3276e3c1ac58SAndrey Ryabinin * in the nr_immediate check below. 3277d108c772SAndrey Ryabinin */ 3278d108c772SAndrey Ryabinin if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken) 3279d108c772SAndrey Ryabinin set_bit(PGDAT_WRITEBACK, &pgdat->flags); 3280d108c772SAndrey Ryabinin 3281d108c772SAndrey Ryabinin /* Allow kswapd to start writing pages during reclaim.*/ 3282d108c772SAndrey Ryabinin if (sc->nr.unqueued_dirty == sc->nr.file_taken) 3283d108c772SAndrey Ryabinin set_bit(PGDAT_DIRTY, &pgdat->flags); 3284d108c772SAndrey Ryabinin 3285d108c772SAndrey Ryabinin /* 32861eba09c1SRandy Dunlap * If kswapd scans pages marked for immediate 3287d108c772SAndrey Ryabinin * reclaim and under writeback (nr_immediate), it 3288d108c772SAndrey Ryabinin * implies that pages are cycling through the LRU 32898cd7c588SMel Gorman * faster than they are written so forcibly stall 32908cd7c588SMel Gorman * until some pages complete writeback. 3291d108c772SAndrey Ryabinin */ 3292d108c772SAndrey Ryabinin if (sc->nr.immediate) 3293c3f4a9a2SMel Gorman reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK); 3294d108c772SAndrey Ryabinin } 3295d108c772SAndrey Ryabinin 3296d108c772SAndrey Ryabinin /* 32978cd7c588SMel Gorman * Tag a node/memcg as congested if all the dirty pages were marked 32988cd7c588SMel Gorman * for writeback and immediate reclaim (counted in nr.congested). 32991b05117dSJohannes Weiner * 3300e3c1ac58SAndrey Ryabinin * Legacy memcg will stall in page writeback so avoid forcibly 33018cd7c588SMel Gorman * stalling in reclaim_throttle(). 3302e3c1ac58SAndrey Ryabinin */ 33031b05117dSJohannes Weiner if ((current_is_kswapd() || 33041b05117dSJohannes Weiner (cgroup_reclaim(sc) && writeback_throttling_sane(sc))) && 3305e3c1ac58SAndrey Ryabinin sc->nr.dirty && sc->nr.dirty == sc->nr.congested) 33061b05117dSJohannes Weiner set_bit(LRUVEC_CONGESTED, &target_lruvec->flags); 3307e3c1ac58SAndrey Ryabinin 3308e3c1ac58SAndrey Ryabinin /* 33098cd7c588SMel Gorman * Stall direct reclaim for IO completions if the lruvec is 33108cd7c588SMel Gorman * node is congested. Allow kswapd to continue until it 3311d108c772SAndrey Ryabinin * starts encountering unqueued dirty pages or cycling through 3312d108c772SAndrey Ryabinin * the LRU too quickly. 3313d108c772SAndrey Ryabinin */ 33141b05117dSJohannes Weiner if (!current_is_kswapd() && current_may_throttle() && 33151b05117dSJohannes Weiner !sc->hibernation_mode && 33161b05117dSJohannes Weiner test_bit(LRUVEC_CONGESTED, &target_lruvec->flags)) 33171b4e3f26SMel Gorman reclaim_throttle(pgdat, VMSCAN_THROTTLE_CONGESTED); 3318d108c772SAndrey Ryabinin 3319d2af3397SJohannes Weiner if (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, 3320d2af3397SJohannes Weiner sc)) 3321d2af3397SJohannes Weiner goto again; 33222344d7e4SJohannes Weiner 3323c73322d0SJohannes Weiner /* 3324c73322d0SJohannes Weiner * Kswapd gives up on balancing particular nodes after too 3325c73322d0SJohannes Weiner * many failures to reclaim anything from them and goes to 3326c73322d0SJohannes Weiner * sleep. On reclaim progress, reset the failure counter. A 3327c73322d0SJohannes Weiner * successful direct reclaim run will revive a dormant kswapd. 3328c73322d0SJohannes Weiner */ 3329c73322d0SJohannes Weiner if (reclaimable) 3330c73322d0SJohannes Weiner pgdat->kswapd_failures = 0; 3331f16015fbSJohannes Weiner } 3332f16015fbSJohannes Weiner 333353853e2dSVlastimil Babka /* 3334fdd4c614SVlastimil Babka * Returns true if compaction should go ahead for a costly-order request, or 3335fdd4c614SVlastimil Babka * the allocation would already succeed without compaction. Return false if we 3336fdd4c614SVlastimil Babka * should reclaim first. 333753853e2dSVlastimil Babka */ 33384f588331SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 3339fe4b1b24SMel Gorman { 334031483b6aSMel Gorman unsigned long watermark; 3341fdd4c614SVlastimil Babka enum compact_result suitable; 3342fe4b1b24SMel Gorman 3343fdd4c614SVlastimil Babka suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx); 3344fdd4c614SVlastimil Babka if (suitable == COMPACT_SUCCESS) 3345fdd4c614SVlastimil Babka /* Allocation should succeed already. Don't reclaim. */ 3346fdd4c614SVlastimil Babka return true; 3347fdd4c614SVlastimil Babka if (suitable == COMPACT_SKIPPED) 3348fdd4c614SVlastimil Babka /* Compaction cannot yet proceed. Do reclaim. */ 3349fe4b1b24SMel Gorman return false; 3350fe4b1b24SMel Gorman 3351fdd4c614SVlastimil Babka /* 3352fdd4c614SVlastimil Babka * Compaction is already possible, but it takes time to run and there 3353fdd4c614SVlastimil Babka * are potentially other callers using the pages just freed. So proceed 3354fdd4c614SVlastimil Babka * with reclaim to make a buffer of free pages available to give 3355fdd4c614SVlastimil Babka * compaction a reasonable chance of completing and allocating the page. 3356fdd4c614SVlastimil Babka * Note that we won't actually reclaim the whole buffer in one attempt 3357fdd4c614SVlastimil Babka * as the target watermark in should_continue_reclaim() is lower. But if 3358fdd4c614SVlastimil Babka * we are already above the high+gap watermark, don't reclaim at all. 3359fdd4c614SVlastimil Babka */ 3360fdd4c614SVlastimil Babka watermark = high_wmark_pages(zone) + compact_gap(sc->order); 3361fdd4c614SVlastimil Babka 3362fdd4c614SVlastimil Babka return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); 3363fe4b1b24SMel Gorman } 3364fe4b1b24SMel Gorman 336569392a40SMel Gorman static void consider_reclaim_throttle(pg_data_t *pgdat, struct scan_control *sc) 336669392a40SMel Gorman { 336766ce520bSMel Gorman /* 336866ce520bSMel Gorman * If reclaim is making progress greater than 12% efficiency then 336966ce520bSMel Gorman * wake all the NOPROGRESS throttled tasks. 337066ce520bSMel Gorman */ 337166ce520bSMel Gorman if (sc->nr_reclaimed > (sc->nr_scanned >> 3)) { 337269392a40SMel Gorman wait_queue_head_t *wqh; 337369392a40SMel Gorman 337469392a40SMel Gorman wqh = &pgdat->reclaim_wait[VMSCAN_THROTTLE_NOPROGRESS]; 337569392a40SMel Gorman if (waitqueue_active(wqh)) 337669392a40SMel Gorman wake_up(wqh); 337769392a40SMel Gorman 337869392a40SMel Gorman return; 337969392a40SMel Gorman } 338069392a40SMel Gorman 338169392a40SMel Gorman /* 33821b4e3f26SMel Gorman * Do not throttle kswapd or cgroup reclaim on NOPROGRESS as it will 33831b4e3f26SMel Gorman * throttle on VMSCAN_THROTTLE_WRITEBACK if there are too many pages 33841b4e3f26SMel Gorman * under writeback and marked for immediate reclaim at the tail of the 33851b4e3f26SMel Gorman * LRU. 338669392a40SMel Gorman */ 33871b4e3f26SMel Gorman if (current_is_kswapd() || cgroup_reclaim(sc)) 338869392a40SMel Gorman return; 338969392a40SMel Gorman 339069392a40SMel Gorman /* Throttle if making no progress at high prioities. */ 33911b4e3f26SMel Gorman if (sc->priority == 1 && !sc->nr_reclaimed) 3392c3f4a9a2SMel Gorman reclaim_throttle(pgdat, VMSCAN_THROTTLE_NOPROGRESS); 339369392a40SMel Gorman } 339469392a40SMel Gorman 33951da177e4SLinus Torvalds /* 33961da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 33971da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 33981da177e4SLinus Torvalds * request. 33991da177e4SLinus Torvalds * 34001da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 34011da177e4SLinus Torvalds * scan then give up on it. 34021da177e4SLinus Torvalds */ 34030a0337e0SMichal Hocko static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 34041da177e4SLinus Torvalds { 3405dd1a239fSMel Gorman struct zoneref *z; 340654a6eb5cSMel Gorman struct zone *zone; 34070608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 34080608f43dSAndrew Morton unsigned long nr_soft_scanned; 3409619d0d76SWeijie Yang gfp_t orig_mask; 341079dafcdcSMel Gorman pg_data_t *last_pgdat = NULL; 34111b4e3f26SMel Gorman pg_data_t *first_pgdat = NULL; 34121cfb419bSKAMEZAWA Hiroyuki 3413cc715d99SMel Gorman /* 3414cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 3415cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 3416cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 3417cc715d99SMel Gorman */ 3418619d0d76SWeijie Yang orig_mask = sc->gfp_mask; 3419b2e18757SMel Gorman if (buffer_heads_over_limit) { 3420cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 34214f588331SMel Gorman sc->reclaim_idx = gfp_zone(sc->gfp_mask); 3422b2e18757SMel Gorman } 3423cc715d99SMel Gorman 3424d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 3425b2e18757SMel Gorman sc->reclaim_idx, sc->nodemask) { 3426b2e18757SMel Gorman /* 34271cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 34281cfb419bSKAMEZAWA Hiroyuki * to global LRU. 34291cfb419bSKAMEZAWA Hiroyuki */ 3430b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) { 3431344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, 3432344736f2SVladimir Davydov GFP_KERNEL | __GFP_HARDWALL)) 34331da177e4SLinus Torvalds continue; 343465ec02cbSVladimir Davydov 3435e0887c19SRik van Riel /* 3436e0c23279SMel Gorman * If we already have plenty of memory free for 3437e0c23279SMel Gorman * compaction in this zone, don't free any more. 3438e0c23279SMel Gorman * Even though compaction is invoked for any 3439e0c23279SMel Gorman * non-zero order, only frequent costly order 3440e0c23279SMel Gorman * reclamation is disruptive enough to become a 3441c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 3442c7cfa37bSCopot Alexandru * page allocations. 3443e0887c19SRik van Riel */ 34440b06496aSJohannes Weiner if (IS_ENABLED(CONFIG_COMPACTION) && 34450b06496aSJohannes Weiner sc->order > PAGE_ALLOC_COSTLY_ORDER && 34464f588331SMel Gorman compaction_ready(zone, sc)) { 34470b06496aSJohannes Weiner sc->compaction_ready = true; 3448e0887c19SRik van Riel continue; 3449e0887c19SRik van Riel } 34500b06496aSJohannes Weiner 34510608f43dSAndrew Morton /* 345279dafcdcSMel Gorman * Shrink each node in the zonelist once. If the 345379dafcdcSMel Gorman * zonelist is ordered by zone (not the default) then a 345479dafcdcSMel Gorman * node may be shrunk multiple times but in that case 345579dafcdcSMel Gorman * the user prefers lower zones being preserved. 345679dafcdcSMel Gorman */ 345779dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 345879dafcdcSMel Gorman continue; 345979dafcdcSMel Gorman 346079dafcdcSMel Gorman /* 34610608f43dSAndrew Morton * This steals pages from memory cgroups over softlimit 34620608f43dSAndrew Morton * and returns the number of reclaimed pages and 34630608f43dSAndrew Morton * scanned pages. This works for global memory pressure 34640608f43dSAndrew Morton * and balancing, not for a memcg's limit. 34650608f43dSAndrew Morton */ 34660608f43dSAndrew Morton nr_soft_scanned = 0; 3467ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, 34680608f43dSAndrew Morton sc->order, sc->gfp_mask, 34690608f43dSAndrew Morton &nr_soft_scanned); 34700608f43dSAndrew Morton sc->nr_reclaimed += nr_soft_reclaimed; 34710608f43dSAndrew Morton sc->nr_scanned += nr_soft_scanned; 3472ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 3473ac34a1a3SKAMEZAWA Hiroyuki } 3474d149e3b2SYing Han 34751b4e3f26SMel Gorman if (!first_pgdat) 34761b4e3f26SMel Gorman first_pgdat = zone->zone_pgdat; 34771b4e3f26SMel Gorman 347879dafcdcSMel Gorman /* See comment about same check for global reclaim above */ 347979dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 348079dafcdcSMel Gorman continue; 348179dafcdcSMel Gorman last_pgdat = zone->zone_pgdat; 3482970a39a3SMel Gorman shrink_node(zone->zone_pgdat, sc); 34831da177e4SLinus Torvalds } 3484e0c23279SMel Gorman 348580082938SMel Gorman if (first_pgdat) 34861b4e3f26SMel Gorman consider_reclaim_throttle(first_pgdat, sc); 34871b4e3f26SMel Gorman 348865ec02cbSVladimir Davydov /* 3489619d0d76SWeijie Yang * Restore to original mask to avoid the impact on the caller if we 3490619d0d76SWeijie Yang * promoted it to __GFP_HIGHMEM. 3491619d0d76SWeijie Yang */ 3492619d0d76SWeijie Yang sc->gfp_mask = orig_mask; 34931da177e4SLinus Torvalds } 34941da177e4SLinus Torvalds 3495b910718aSJohannes Weiner static void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat) 34962a2e4885SJohannes Weiner { 3497b910718aSJohannes Weiner struct lruvec *target_lruvec; 34982a2e4885SJohannes Weiner unsigned long refaults; 34992a2e4885SJohannes Weiner 3500b910718aSJohannes Weiner target_lruvec = mem_cgroup_lruvec(target_memcg, pgdat); 3501170b04b7SJoonsoo Kim refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_ANON); 3502170b04b7SJoonsoo Kim target_lruvec->refaults[0] = refaults; 3503170b04b7SJoonsoo Kim refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_FILE); 3504170b04b7SJoonsoo Kim target_lruvec->refaults[1] = refaults; 35052a2e4885SJohannes Weiner } 35062a2e4885SJohannes Weiner 35071da177e4SLinus Torvalds /* 35081da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 35091da177e4SLinus Torvalds * 35101da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 35111da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 35121da177e4SLinus Torvalds * 35131da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 35141da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 35155b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 35165b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 35175b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 35185b0830cbSJens Axboe * work, and the allocation attempt will fail. 3519a41f24eaSNishanth Aravamudan * 3520a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 3521a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 35221da177e4SLinus Torvalds */ 3523dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 35243115cd91SVladimir Davydov struct scan_control *sc) 35251da177e4SLinus Torvalds { 3526241994edSJohannes Weiner int initial_priority = sc->priority; 35272a2e4885SJohannes Weiner pg_data_t *last_pgdat; 35282a2e4885SJohannes Weiner struct zoneref *z; 35292a2e4885SJohannes Weiner struct zone *zone; 3530241994edSJohannes Weiner retry: 3531873b4771SKeika Kobayashi delayacct_freepages_start(); 3532873b4771SKeika Kobayashi 3533b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 35347cc30fcfSMel Gorman __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); 35351da177e4SLinus Torvalds 35369e3b2f8cSKonstantin Khlebnikov do { 353770ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 353870ddf637SAnton Vorontsov sc->priority); 353966e1707bSBalbir Singh sc->nr_scanned = 0; 35400a0337e0SMichal Hocko shrink_zones(zonelist, sc); 3541e0c23279SMel Gorman 3542bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 35430b06496aSJohannes Weiner break; 35440b06496aSJohannes Weiner 35450b06496aSJohannes Weiner if (sc->compaction_ready) 35460b06496aSJohannes Weiner break; 35471da177e4SLinus Torvalds 35481da177e4SLinus Torvalds /* 35490e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 35500e50ce3bSMinchan Kim * writepage even in laptop mode. 35510e50ce3bSMinchan Kim */ 35520e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 35530e50ce3bSMinchan Kim sc->may_writepage = 1; 35540b06496aSJohannes Weiner } while (--sc->priority >= 0); 3555bb21c7ceSKOSAKI Motohiro 35562a2e4885SJohannes Weiner last_pgdat = NULL; 35572a2e4885SJohannes Weiner for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx, 35582a2e4885SJohannes Weiner sc->nodemask) { 35592a2e4885SJohannes Weiner if (zone->zone_pgdat == last_pgdat) 35602a2e4885SJohannes Weiner continue; 35612a2e4885SJohannes Weiner last_pgdat = zone->zone_pgdat; 35621b05117dSJohannes Weiner 35632a2e4885SJohannes Weiner snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat); 35641b05117dSJohannes Weiner 35651b05117dSJohannes Weiner if (cgroup_reclaim(sc)) { 35661b05117dSJohannes Weiner struct lruvec *lruvec; 35671b05117dSJohannes Weiner 35681b05117dSJohannes Weiner lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, 35691b05117dSJohannes Weiner zone->zone_pgdat); 35701b05117dSJohannes Weiner clear_bit(LRUVEC_CONGESTED, &lruvec->flags); 35711b05117dSJohannes Weiner } 35722a2e4885SJohannes Weiner } 35732a2e4885SJohannes Weiner 3574873b4771SKeika Kobayashi delayacct_freepages_end(); 3575873b4771SKeika Kobayashi 3576bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 3577bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 3578bb21c7ceSKOSAKI Motohiro 35790cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 35800b06496aSJohannes Weiner if (sc->compaction_ready) 35817335084dSMel Gorman return 1; 35827335084dSMel Gorman 3583b91ac374SJohannes Weiner /* 3584b91ac374SJohannes Weiner * We make inactive:active ratio decisions based on the node's 3585b91ac374SJohannes Weiner * composition of memory, but a restrictive reclaim_idx or a 3586b91ac374SJohannes Weiner * memory.low cgroup setting can exempt large amounts of 3587b91ac374SJohannes Weiner * memory from reclaim. Neither of which are very common, so 3588b91ac374SJohannes Weiner * instead of doing costly eligibility calculations of the 3589b91ac374SJohannes Weiner * entire cgroup subtree up front, we assume the estimates are 3590b91ac374SJohannes Weiner * good, and retry with forcible deactivation if that fails. 3591b91ac374SJohannes Weiner */ 3592b91ac374SJohannes Weiner if (sc->skipped_deactivate) { 3593b91ac374SJohannes Weiner sc->priority = initial_priority; 3594b91ac374SJohannes Weiner sc->force_deactivate = 1; 3595b91ac374SJohannes Weiner sc->skipped_deactivate = 0; 3596b91ac374SJohannes Weiner goto retry; 3597b91ac374SJohannes Weiner } 3598b91ac374SJohannes Weiner 3599241994edSJohannes Weiner /* Untapped cgroup reserves? Don't OOM, retry. */ 3600d6622f63SYisheng Xie if (sc->memcg_low_skipped) { 3601241994edSJohannes Weiner sc->priority = initial_priority; 3602b91ac374SJohannes Weiner sc->force_deactivate = 0; 3603d6622f63SYisheng Xie sc->memcg_low_reclaim = 1; 3604d6622f63SYisheng Xie sc->memcg_low_skipped = 0; 3605241994edSJohannes Weiner goto retry; 3606241994edSJohannes Weiner } 3607241994edSJohannes Weiner 3608bb21c7ceSKOSAKI Motohiro return 0; 36091da177e4SLinus Torvalds } 36101da177e4SLinus Torvalds 3611c73322d0SJohannes Weiner static bool allow_direct_reclaim(pg_data_t *pgdat) 36125515061dSMel Gorman { 36135515061dSMel Gorman struct zone *zone; 36145515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 36155515061dSMel Gorman unsigned long free_pages = 0; 36165515061dSMel Gorman int i; 36175515061dSMel Gorman bool wmark_ok; 36185515061dSMel Gorman 3619c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3620c73322d0SJohannes Weiner return true; 3621c73322d0SJohannes Weiner 36225515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 36235515061dSMel Gorman zone = &pgdat->node_zones[i]; 3624d450abd8SJohannes Weiner if (!managed_zone(zone)) 3625d450abd8SJohannes Weiner continue; 3626d450abd8SJohannes Weiner 3627d450abd8SJohannes Weiner if (!zone_reclaimable_pages(zone)) 3628675becceSMel Gorman continue; 3629675becceSMel Gorman 36305515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 36315515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 36325515061dSMel Gorman } 36335515061dSMel Gorman 3634675becceSMel Gorman /* If there are no reserves (unexpected config) then do not throttle */ 3635675becceSMel Gorman if (!pfmemalloc_reserve) 3636675becceSMel Gorman return true; 3637675becceSMel Gorman 36385515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 36395515061dSMel Gorman 36405515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 36415515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 364297a225e6SJoonsoo Kim if (READ_ONCE(pgdat->kswapd_highest_zoneidx) > ZONE_NORMAL) 364397a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, ZONE_NORMAL); 36445644e1fbSQian Cai 36455515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 36465515061dSMel Gorman } 36475515061dSMel Gorman 36485515061dSMel Gorman return wmark_ok; 36495515061dSMel Gorman } 36505515061dSMel Gorman 36515515061dSMel Gorman /* 36525515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 36535515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 36545515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 365550694c28SMel Gorman * when the low watermark is reached. 365650694c28SMel Gorman * 365750694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 365850694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 36595515061dSMel Gorman */ 366050694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 36615515061dSMel Gorman nodemask_t *nodemask) 36625515061dSMel Gorman { 3663675becceSMel Gorman struct zoneref *z; 36645515061dSMel Gorman struct zone *zone; 3665675becceSMel Gorman pg_data_t *pgdat = NULL; 36665515061dSMel Gorman 36675515061dSMel Gorman /* 36685515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 36695515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 36705515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 36715515061dSMel Gorman * committing a transaction where throttling it could forcing other 36725515061dSMel Gorman * processes to block on log_wait_commit(). 36735515061dSMel Gorman */ 36745515061dSMel Gorman if (current->flags & PF_KTHREAD) 367550694c28SMel Gorman goto out; 367650694c28SMel Gorman 367750694c28SMel Gorman /* 367850694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 367950694c28SMel Gorman * It should return quickly so it can exit and free its memory 368050694c28SMel Gorman */ 368150694c28SMel Gorman if (fatal_signal_pending(current)) 368250694c28SMel Gorman goto out; 36835515061dSMel Gorman 3684675becceSMel Gorman /* 3685675becceSMel Gorman * Check if the pfmemalloc reserves are ok by finding the first node 3686675becceSMel Gorman * with a usable ZONE_NORMAL or lower zone. The expectation is that 3687675becceSMel Gorman * GFP_KERNEL will be required for allocating network buffers when 3688675becceSMel Gorman * swapping over the network so ZONE_HIGHMEM is unusable. 3689675becceSMel Gorman * 3690675becceSMel Gorman * Throttling is based on the first usable node and throttled processes 3691675becceSMel Gorman * wait on a queue until kswapd makes progress and wakes them. There 3692675becceSMel Gorman * is an affinity then between processes waking up and where reclaim 3693675becceSMel Gorman * progress has been made assuming the process wakes on the same node. 3694675becceSMel Gorman * More importantly, processes running on remote nodes will not compete 3695675becceSMel Gorman * for remote pfmemalloc reserves and processes on different nodes 3696675becceSMel Gorman * should make reasonable progress. 3697675becceSMel Gorman */ 3698675becceSMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 369917636faaSMichael S. Tsirkin gfp_zone(gfp_mask), nodemask) { 3700675becceSMel Gorman if (zone_idx(zone) > ZONE_NORMAL) 3701675becceSMel Gorman continue; 3702675becceSMel Gorman 3703675becceSMel Gorman /* Throttle based on the first usable node */ 37045515061dSMel Gorman pgdat = zone->zone_pgdat; 3705c73322d0SJohannes Weiner if (allow_direct_reclaim(pgdat)) 370650694c28SMel Gorman goto out; 3707675becceSMel Gorman break; 3708675becceSMel Gorman } 3709675becceSMel Gorman 3710675becceSMel Gorman /* If no zone was usable by the allocation flags then do not throttle */ 3711675becceSMel Gorman if (!pgdat) 3712675becceSMel Gorman goto out; 37135515061dSMel Gorman 371468243e76SMel Gorman /* Account for the throttling */ 371568243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 371668243e76SMel Gorman 37175515061dSMel Gorman /* 37185515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 37195515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 37205515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 37215515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 37225515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 37235515061dSMel Gorman * second before continuing. 37245515061dSMel Gorman */ 37252e786d9eSMiaohe Lin if (!(gfp_mask & __GFP_FS)) 37265515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 3727c73322d0SJohannes Weiner allow_direct_reclaim(pgdat), HZ); 37282e786d9eSMiaohe Lin else 37295515061dSMel Gorman /* Throttle until kswapd wakes the process */ 37305515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 3731c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)); 373250694c28SMel Gorman 373350694c28SMel Gorman if (fatal_signal_pending(current)) 373450694c28SMel Gorman return true; 373550694c28SMel Gorman 373650694c28SMel Gorman out: 373750694c28SMel Gorman return false; 37385515061dSMel Gorman } 37395515061dSMel Gorman 3740dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 3741327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 374266e1707bSBalbir Singh { 374333906bc5SMel Gorman unsigned long nr_reclaimed; 374466e1707bSBalbir Singh struct scan_control sc = { 374522fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3746f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 3747b2e18757SMel Gorman .reclaim_idx = gfp_zone(gfp_mask), 3748ee814fe2SJohannes Weiner .order = order, 3749ee814fe2SJohannes Weiner .nodemask = nodemask, 3750ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 3751ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3752a6dc60f8SJohannes Weiner .may_unmap = 1, 37532e2e4259SKOSAKI Motohiro .may_swap = 1, 375466e1707bSBalbir Singh }; 375566e1707bSBalbir Singh 37565515061dSMel Gorman /* 3757bb451fdfSGreg Thelen * scan_control uses s8 fields for order, priority, and reclaim_idx. 3758bb451fdfSGreg Thelen * Confirm they are large enough for max values. 3759bb451fdfSGreg Thelen */ 3760bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_ORDER > S8_MAX); 3761bb451fdfSGreg Thelen BUILD_BUG_ON(DEF_PRIORITY > S8_MAX); 3762bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX); 3763bb451fdfSGreg Thelen 3764bb451fdfSGreg Thelen /* 376550694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 376650694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 376750694c28SMel Gorman * point. 37685515061dSMel Gorman */ 3769f2f43e56SNick Desaulniers if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask)) 37705515061dSMel Gorman return 1; 37715515061dSMel Gorman 37721732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 37733481c37fSYafang Shao trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask); 377433906bc5SMel Gorman 37753115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 377633906bc5SMel Gorman 377733906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 37781732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 377933906bc5SMel Gorman 378033906bc5SMel Gorman return nr_reclaimed; 378166e1707bSBalbir Singh } 378266e1707bSBalbir Singh 3783c255a458SAndrew Morton #ifdef CONFIG_MEMCG 378466e1707bSBalbir Singh 3785d2e5fb92SMichal Hocko /* Only used by soft limit reclaim. Do not reuse for anything else. */ 3786a9dd0a83SMel Gorman unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, 37874e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 3788ef8f2327SMel Gorman pg_data_t *pgdat, 37890ae5e89cSYing Han unsigned long *nr_scanned) 37904e416953SBalbir Singh { 3791afaf07a6SJohannes Weiner struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); 37924e416953SBalbir Singh struct scan_control sc = { 3793b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3794ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 37954e416953SBalbir Singh .may_writepage = !laptop_mode, 37964e416953SBalbir Singh .may_unmap = 1, 3797b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 37984e416953SBalbir Singh .may_swap = !noswap, 37994e416953SBalbir Singh }; 38000ae5e89cSYing Han 3801d2e5fb92SMichal Hocko WARN_ON_ONCE(!current->reclaim_state); 3802d2e5fb92SMichal Hocko 38034e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 38044e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 3805bdce6d9eSKOSAKI Motohiro 38069e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 38073481c37fSYafang Shao sc.gfp_mask); 3808bdce6d9eSKOSAKI Motohiro 38094e416953SBalbir Singh /* 38104e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 38114e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 3812a9dd0a83SMel Gorman * if we don't reclaim here, the shrink_node from balance_pgdat 38134e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 38144e416953SBalbir Singh * the priority and make it zero. 38154e416953SBalbir Singh */ 3816afaf07a6SJohannes Weiner shrink_lruvec(lruvec, &sc); 3817bdce6d9eSKOSAKI Motohiro 3818bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 3819bdce6d9eSKOSAKI Motohiro 38200ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 38210308f7cfSYafang Shao 38224e416953SBalbir Singh return sc.nr_reclaimed; 38234e416953SBalbir Singh } 38244e416953SBalbir Singh 382572835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 3826b70a2a21SJohannes Weiner unsigned long nr_pages, 38278c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 3828b70a2a21SJohannes Weiner bool may_swap) 382966e1707bSBalbir Singh { 3830bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 3831499118e9SVlastimil Babka unsigned int noreclaim_flag; 383266e1707bSBalbir Singh struct scan_control sc = { 3833b70a2a21SJohannes Weiner .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 38347dea19f9SMichal Hocko .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | 3835ee814fe2SJohannes Weiner (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 3836b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 3837ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 3838ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 383966e1707bSBalbir Singh .may_writepage = !laptop_mode, 3840a6dc60f8SJohannes Weiner .may_unmap = 1, 3841b70a2a21SJohannes Weiner .may_swap = may_swap, 3842a09ed5e0SYing Han }; 3843fa40d1eeSShakeel Butt /* 3844fa40d1eeSShakeel Butt * Traverse the ZONELIST_FALLBACK zonelist of the current node to put 3845fa40d1eeSShakeel Butt * equal pressure on all the nodes. This is based on the assumption that 3846fa40d1eeSShakeel Butt * the reclaim does not bail out early. 3847fa40d1eeSShakeel Butt */ 3848fa40d1eeSShakeel Butt struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 384966e1707bSBalbir Singh 38501732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 38513481c37fSYafang Shao trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask); 3852499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 3853eb414681SJohannes Weiner 38543115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3855eb414681SJohannes Weiner 3856499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 3857bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 38581732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 3859bdce6d9eSKOSAKI Motohiro 3860bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 386166e1707bSBalbir Singh } 386266e1707bSBalbir Singh #endif 386366e1707bSBalbir Singh 38641d82de61SMel Gorman static void age_active_anon(struct pglist_data *pgdat, 3865ef8f2327SMel Gorman struct scan_control *sc) 3866f16015fbSJohannes Weiner { 3867b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 3868b91ac374SJohannes Weiner struct lruvec *lruvec; 3869b95a2f2dSJohannes Weiner 38702f368a9fSDave Hansen if (!can_age_anon_pages(pgdat, sc)) 3871b95a2f2dSJohannes Weiner return; 3872b95a2f2dSJohannes Weiner 3873b91ac374SJohannes Weiner lruvec = mem_cgroup_lruvec(NULL, pgdat); 3874b91ac374SJohannes Weiner if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON)) 3875b91ac374SJohannes Weiner return; 3876b91ac374SJohannes Weiner 3877b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 3878b95a2f2dSJohannes Weiner do { 3879b91ac374SJohannes Weiner lruvec = mem_cgroup_lruvec(memcg, pgdat); 38801a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 38819e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 3882b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 3883b95a2f2dSJohannes Weiner } while (memcg); 3884f16015fbSJohannes Weiner } 3885f16015fbSJohannes Weiner 388697a225e6SJoonsoo Kim static bool pgdat_watermark_boosted(pg_data_t *pgdat, int highest_zoneidx) 38871c30844dSMel Gorman { 38881c30844dSMel Gorman int i; 38891c30844dSMel Gorman struct zone *zone; 38901c30844dSMel Gorman 38911c30844dSMel Gorman /* 38921c30844dSMel Gorman * Check for watermark boosts top-down as the higher zones 38931c30844dSMel Gorman * are more likely to be boosted. Both watermarks and boosts 38941eba09c1SRandy Dunlap * should not be checked at the same time as reclaim would 38951c30844dSMel Gorman * start prematurely when there is no boosting and a lower 38961c30844dSMel Gorman * zone is balanced. 38971c30844dSMel Gorman */ 389897a225e6SJoonsoo Kim for (i = highest_zoneidx; i >= 0; i--) { 38991c30844dSMel Gorman zone = pgdat->node_zones + i; 39001c30844dSMel Gorman if (!managed_zone(zone)) 39011c30844dSMel Gorman continue; 39021c30844dSMel Gorman 39031c30844dSMel Gorman if (zone->watermark_boost) 39041c30844dSMel Gorman return true; 39051c30844dSMel Gorman } 39061c30844dSMel Gorman 39071c30844dSMel Gorman return false; 39081c30844dSMel Gorman } 39091c30844dSMel Gorman 3910e716f2ebSMel Gorman /* 3911e716f2ebSMel Gorman * Returns true if there is an eligible zone balanced for the request order 391297a225e6SJoonsoo Kim * and highest_zoneidx 3913e716f2ebSMel Gorman */ 391497a225e6SJoonsoo Kim static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx) 391560cefed4SJohannes Weiner { 3916e716f2ebSMel Gorman int i; 3917e716f2ebSMel Gorman unsigned long mark = -1; 3918e716f2ebSMel Gorman struct zone *zone; 391960cefed4SJohannes Weiner 39201c30844dSMel Gorman /* 39211c30844dSMel Gorman * Check watermarks bottom-up as lower zones are more likely to 39221c30844dSMel Gorman * meet watermarks. 39231c30844dSMel Gorman */ 392497a225e6SJoonsoo Kim for (i = 0; i <= highest_zoneidx; i++) { 3925e716f2ebSMel Gorman zone = pgdat->node_zones + i; 39266256c6b4SMel Gorman 3927e716f2ebSMel Gorman if (!managed_zone(zone)) 3928e716f2ebSMel Gorman continue; 3929e716f2ebSMel Gorman 3930c574bbe9SHuang Ying if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) 3931c574bbe9SHuang Ying mark = wmark_pages(zone, WMARK_PROMO); 3932c574bbe9SHuang Ying else 3933e716f2ebSMel Gorman mark = high_wmark_pages(zone); 393497a225e6SJoonsoo Kim if (zone_watermark_ok_safe(zone, order, mark, highest_zoneidx)) 39356256c6b4SMel Gorman return true; 393660cefed4SJohannes Weiner } 393760cefed4SJohannes Weiner 3938e716f2ebSMel Gorman /* 393936c26128SWei Yang * If a node has no managed zone within highest_zoneidx, it does not 3940e716f2ebSMel Gorman * need balancing by definition. This can happen if a zone-restricted 3941e716f2ebSMel Gorman * allocation tries to wake a remote kswapd. 3942e716f2ebSMel Gorman */ 3943e716f2ebSMel Gorman if (mark == -1) 3944e716f2ebSMel Gorman return true; 3945e716f2ebSMel Gorman 3946e716f2ebSMel Gorman return false; 3947e716f2ebSMel Gorman } 3948e716f2ebSMel Gorman 3949631b6e08SMel Gorman /* Clear pgdat state for congested, dirty or under writeback. */ 3950631b6e08SMel Gorman static void clear_pgdat_congested(pg_data_t *pgdat) 3951631b6e08SMel Gorman { 39521b05117dSJohannes Weiner struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat); 39531b05117dSJohannes Weiner 39541b05117dSJohannes Weiner clear_bit(LRUVEC_CONGESTED, &lruvec->flags); 3955631b6e08SMel Gorman clear_bit(PGDAT_DIRTY, &pgdat->flags); 3956631b6e08SMel Gorman clear_bit(PGDAT_WRITEBACK, &pgdat->flags); 3957631b6e08SMel Gorman } 3958631b6e08SMel Gorman 39591741c877SMel Gorman /* 39605515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 39615515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 39625515061dSMel Gorman * 39635515061dSMel Gorman * Returns true if kswapd is ready to sleep 39645515061dSMel Gorman */ 396597a225e6SJoonsoo Kim static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, 396697a225e6SJoonsoo Kim int highest_zoneidx) 3967f50de2d3SMel Gorman { 39685515061dSMel Gorman /* 39699e5e3661SVlastimil Babka * The throttled processes are normally woken up in balance_pgdat() as 3970c73322d0SJohannes Weiner * soon as allow_direct_reclaim() is true. But there is a potential 39719e5e3661SVlastimil Babka * race between when kswapd checks the watermarks and a process gets 39729e5e3661SVlastimil Babka * throttled. There is also a potential race if processes get 39739e5e3661SVlastimil Babka * throttled, kswapd wakes, a large process exits thereby balancing the 39749e5e3661SVlastimil Babka * zones, which causes kswapd to exit balance_pgdat() before reaching 39759e5e3661SVlastimil Babka * the wake up checks. If kswapd is going to sleep, no process should 39769e5e3661SVlastimil Babka * be sleeping on pfmemalloc_wait, so wake them now if necessary. If 39779e5e3661SVlastimil Babka * the wake up is premature, processes will wake kswapd and get 39789e5e3661SVlastimil Babka * throttled again. The difference from wake ups in balance_pgdat() is 39799e5e3661SVlastimil Babka * that here we are under prepare_to_wait(). 39805515061dSMel Gorman */ 39819e5e3661SVlastimil Babka if (waitqueue_active(&pgdat->pfmemalloc_wait)) 39829e5e3661SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 3983f50de2d3SMel Gorman 3984c73322d0SJohannes Weiner /* Hopeless node, leave it to direct reclaim */ 3985c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3986c73322d0SJohannes Weiner return true; 3987c73322d0SJohannes Weiner 398897a225e6SJoonsoo Kim if (pgdat_balanced(pgdat, order, highest_zoneidx)) { 3989631b6e08SMel Gorman clear_pgdat_congested(pgdat); 3990333b0a45SShantanu Goel return true; 39911d82de61SMel Gorman } 39921d82de61SMel Gorman 3993333b0a45SShantanu Goel return false; 3994f50de2d3SMel Gorman } 3995f50de2d3SMel Gorman 39961da177e4SLinus Torvalds /* 39971d82de61SMel Gorman * kswapd shrinks a node of pages that are at or below the highest usable 39981d82de61SMel Gorman * zone that is currently unbalanced. 3999b8e83b94SMel Gorman * 4000b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 4001283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 4002283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 400375485363SMel Gorman */ 40041d82de61SMel Gorman static bool kswapd_shrink_node(pg_data_t *pgdat, 4005accf6242SVlastimil Babka struct scan_control *sc) 400675485363SMel Gorman { 40071d82de61SMel Gorman struct zone *zone; 40081d82de61SMel Gorman int z; 400975485363SMel Gorman 40101d82de61SMel Gorman /* Reclaim a number of pages proportional to the number of zones */ 40111d82de61SMel Gorman sc->nr_to_reclaim = 0; 4012970a39a3SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 40131d82de61SMel Gorman zone = pgdat->node_zones + z; 40146aa303deSMel Gorman if (!managed_zone(zone)) 40151d82de61SMel Gorman continue; 40167c954f6dSMel Gorman 40171d82de61SMel Gorman sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); 40187c954f6dSMel Gorman } 40197c954f6dSMel Gorman 40201d82de61SMel Gorman /* 40211d82de61SMel Gorman * Historically care was taken to put equal pressure on all zones but 40221d82de61SMel Gorman * now pressure is applied based on node LRU order. 40231d82de61SMel Gorman */ 4024970a39a3SMel Gorman shrink_node(pgdat, sc); 40251d82de61SMel Gorman 40261d82de61SMel Gorman /* 40271d82de61SMel Gorman * Fragmentation may mean that the system cannot be rebalanced for 40281d82de61SMel Gorman * high-order allocations. If twice the allocation size has been 40291d82de61SMel Gorman * reclaimed then recheck watermarks only at order-0 to prevent 40301d82de61SMel Gorman * excessive reclaim. Assume that a process requested a high-order 40311d82de61SMel Gorman * can direct reclaim/compact. 40321d82de61SMel Gorman */ 40339861a62cSVlastimil Babka if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order)) 40341d82de61SMel Gorman sc->order = 0; 40351d82de61SMel Gorman 4036b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 403775485363SMel Gorman } 403875485363SMel Gorman 4039c49c2c47SMel Gorman /* Page allocator PCP high watermark is lowered if reclaim is active. */ 4040c49c2c47SMel Gorman static inline void 4041c49c2c47SMel Gorman update_reclaim_active(pg_data_t *pgdat, int highest_zoneidx, bool active) 4042c49c2c47SMel Gorman { 4043c49c2c47SMel Gorman int i; 4044c49c2c47SMel Gorman struct zone *zone; 4045c49c2c47SMel Gorman 4046c49c2c47SMel Gorman for (i = 0; i <= highest_zoneidx; i++) { 4047c49c2c47SMel Gorman zone = pgdat->node_zones + i; 4048c49c2c47SMel Gorman 4049c49c2c47SMel Gorman if (!managed_zone(zone)) 4050c49c2c47SMel Gorman continue; 4051c49c2c47SMel Gorman 4052c49c2c47SMel Gorman if (active) 4053c49c2c47SMel Gorman set_bit(ZONE_RECLAIM_ACTIVE, &zone->flags); 4054c49c2c47SMel Gorman else 4055c49c2c47SMel Gorman clear_bit(ZONE_RECLAIM_ACTIVE, &zone->flags); 4056c49c2c47SMel Gorman } 4057c49c2c47SMel Gorman } 4058c49c2c47SMel Gorman 4059c49c2c47SMel Gorman static inline void 4060c49c2c47SMel Gorman set_reclaim_active(pg_data_t *pgdat, int highest_zoneidx) 4061c49c2c47SMel Gorman { 4062c49c2c47SMel Gorman update_reclaim_active(pgdat, highest_zoneidx, true); 4063c49c2c47SMel Gorman } 4064c49c2c47SMel Gorman 4065c49c2c47SMel Gorman static inline void 4066c49c2c47SMel Gorman clear_reclaim_active(pg_data_t *pgdat, int highest_zoneidx) 4067c49c2c47SMel Gorman { 4068c49c2c47SMel Gorman update_reclaim_active(pgdat, highest_zoneidx, false); 4069c49c2c47SMel Gorman } 4070c49c2c47SMel Gorman 407175485363SMel Gorman /* 40721d82de61SMel Gorman * For kswapd, balance_pgdat() will reclaim pages across a node from zones 40731d82de61SMel Gorman * that are eligible for use by the caller until at least one zone is 40741d82de61SMel Gorman * balanced. 40751da177e4SLinus Torvalds * 40761d82de61SMel Gorman * Returns the order kswapd finished reclaiming at. 40771da177e4SLinus Torvalds * 40781da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 407941858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 40808bb4e7a2SWei Yang * found to have free_pages <= high_wmark_pages(zone), any page in that zone 40811d82de61SMel Gorman * or lower is eligible for reclaim until at least one usable zone is 40821d82de61SMel Gorman * balanced. 40831da177e4SLinus Torvalds */ 408497a225e6SJoonsoo Kim static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx) 40851da177e4SLinus Torvalds { 40861da177e4SLinus Torvalds int i; 40870608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 40880608f43dSAndrew Morton unsigned long nr_soft_scanned; 4089eb414681SJohannes Weiner unsigned long pflags; 40901c30844dSMel Gorman unsigned long nr_boost_reclaim; 40911c30844dSMel Gorman unsigned long zone_boosts[MAX_NR_ZONES] = { 0, }; 40921c30844dSMel Gorman bool boosted; 40931d82de61SMel Gorman struct zone *zone; 4094179e9639SAndrew Morton struct scan_control sc = { 4095179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 4096ee814fe2SJohannes Weiner .order = order, 4097a6dc60f8SJohannes Weiner .may_unmap = 1, 4098179e9639SAndrew Morton }; 409993781325SOmar Sandoval 41001732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 4101eb414681SJohannes Weiner psi_memstall_enter(&pflags); 41024f3eaf45SMatthew Wilcox (Oracle) __fs_reclaim_acquire(_THIS_IP_); 410393781325SOmar Sandoval 4104f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 41051da177e4SLinus Torvalds 41061c30844dSMel Gorman /* 41071c30844dSMel Gorman * Account for the reclaim boost. Note that the zone boost is left in 41081c30844dSMel Gorman * place so that parallel allocations that are near the watermark will 41091c30844dSMel Gorman * stall or direct reclaim until kswapd is finished. 41101c30844dSMel Gorman */ 41111c30844dSMel Gorman nr_boost_reclaim = 0; 411297a225e6SJoonsoo Kim for (i = 0; i <= highest_zoneidx; i++) { 41131c30844dSMel Gorman zone = pgdat->node_zones + i; 41141c30844dSMel Gorman if (!managed_zone(zone)) 41151c30844dSMel Gorman continue; 41161c30844dSMel Gorman 41171c30844dSMel Gorman nr_boost_reclaim += zone->watermark_boost; 41181c30844dSMel Gorman zone_boosts[i] = zone->watermark_boost; 41191c30844dSMel Gorman } 41201c30844dSMel Gorman boosted = nr_boost_reclaim; 41211c30844dSMel Gorman 41221c30844dSMel Gorman restart: 4123c49c2c47SMel Gorman set_reclaim_active(pgdat, highest_zoneidx); 41241c30844dSMel Gorman sc.priority = DEF_PRIORITY; 41259e3b2f8cSKonstantin Khlebnikov do { 4126c73322d0SJohannes Weiner unsigned long nr_reclaimed = sc.nr_reclaimed; 4127b8e83b94SMel Gorman bool raise_priority = true; 41281c30844dSMel Gorman bool balanced; 412993781325SOmar Sandoval bool ret; 4130b8e83b94SMel Gorman 413197a225e6SJoonsoo Kim sc.reclaim_idx = highest_zoneidx; 41321da177e4SLinus Torvalds 413386c79f6bSMel Gorman /* 413484c7a777SMel Gorman * If the number of buffer_heads exceeds the maximum allowed 413584c7a777SMel Gorman * then consider reclaiming from all zones. This has a dual 413684c7a777SMel Gorman * purpose -- on 64-bit systems it is expected that 413784c7a777SMel Gorman * buffer_heads are stripped during active rotation. On 32-bit 413884c7a777SMel Gorman * systems, highmem pages can pin lowmem memory and shrinking 413984c7a777SMel Gorman * buffers can relieve lowmem pressure. Reclaim may still not 414084c7a777SMel Gorman * go ahead if all eligible zones for the original allocation 414184c7a777SMel Gorman * request are balanced to avoid excessive reclaim from kswapd. 414286c79f6bSMel Gorman */ 414386c79f6bSMel Gorman if (buffer_heads_over_limit) { 414486c79f6bSMel Gorman for (i = MAX_NR_ZONES - 1; i >= 0; i--) { 414586c79f6bSMel Gorman zone = pgdat->node_zones + i; 41466aa303deSMel Gorman if (!managed_zone(zone)) 414786c79f6bSMel Gorman continue; 414886c79f6bSMel Gorman 4149970a39a3SMel Gorman sc.reclaim_idx = i; 415086c79f6bSMel Gorman break; 415186c79f6bSMel Gorman } 415286c79f6bSMel Gorman } 415386c79f6bSMel Gorman 415486c79f6bSMel Gorman /* 41551c30844dSMel Gorman * If the pgdat is imbalanced then ignore boosting and preserve 41561c30844dSMel Gorman * the watermarks for a later time and restart. Note that the 41571c30844dSMel Gorman * zone watermarks will be still reset at the end of balancing 41581c30844dSMel Gorman * on the grounds that the normal reclaim should be enough to 41591c30844dSMel Gorman * re-evaluate if boosting is required when kswapd next wakes. 416086c79f6bSMel Gorman */ 416197a225e6SJoonsoo Kim balanced = pgdat_balanced(pgdat, sc.order, highest_zoneidx); 41621c30844dSMel Gorman if (!balanced && nr_boost_reclaim) { 41631c30844dSMel Gorman nr_boost_reclaim = 0; 41641c30844dSMel Gorman goto restart; 41651c30844dSMel Gorman } 41661c30844dSMel Gorman 41671c30844dSMel Gorman /* 41681c30844dSMel Gorman * If boosting is not active then only reclaim if there are no 41691c30844dSMel Gorman * eligible zones. Note that sc.reclaim_idx is not used as 41701c30844dSMel Gorman * buffer_heads_over_limit may have adjusted it. 41711c30844dSMel Gorman */ 41721c30844dSMel Gorman if (!nr_boost_reclaim && balanced) 41731da177e4SLinus Torvalds goto out; 4174e1dbeda6SAndrew Morton 41751c30844dSMel Gorman /* Limit the priority of boosting to avoid reclaim writeback */ 41761c30844dSMel Gorman if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2) 41771c30844dSMel Gorman raise_priority = false; 41781c30844dSMel Gorman 41791c30844dSMel Gorman /* 41801c30844dSMel Gorman * Do not writeback or swap pages for boosted reclaim. The 41811c30844dSMel Gorman * intent is to relieve pressure not issue sub-optimal IO 41821c30844dSMel Gorman * from reclaim context. If no pages are reclaimed, the 41831c30844dSMel Gorman * reclaim will be aborted. 41841c30844dSMel Gorman */ 41851c30844dSMel Gorman sc.may_writepage = !laptop_mode && !nr_boost_reclaim; 41861c30844dSMel Gorman sc.may_swap = !nr_boost_reclaim; 41871c30844dSMel Gorman 41881da177e4SLinus Torvalds /* 41891d82de61SMel Gorman * Do some background aging of the anon list, to give 41901d82de61SMel Gorman * pages a chance to be referenced before reclaiming. All 41911d82de61SMel Gorman * pages are rotated regardless of classzone as this is 41921d82de61SMel Gorman * about consistent aging. 41931d82de61SMel Gorman */ 4194ef8f2327SMel Gorman age_active_anon(pgdat, &sc); 41951d82de61SMel Gorman 41961d82de61SMel Gorman /* 4197b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 4198b7ea3c41SMel Gorman * even in laptop mode. 4199b7ea3c41SMel Gorman */ 4200047d72c3SJohannes Weiner if (sc.priority < DEF_PRIORITY - 2) 4201b7ea3c41SMel Gorman sc.may_writepage = 1; 4202b7ea3c41SMel Gorman 42031d82de61SMel Gorman /* Call soft limit reclaim before calling shrink_node. */ 42041da177e4SLinus Torvalds sc.nr_scanned = 0; 42050608f43dSAndrew Morton nr_soft_scanned = 0; 4206ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, 42071d82de61SMel Gorman sc.gfp_mask, &nr_soft_scanned); 42080608f43dSAndrew Morton sc.nr_reclaimed += nr_soft_reclaimed; 42090608f43dSAndrew Morton 421032a4330dSRik van Riel /* 42111d82de61SMel Gorman * There should be no need to raise the scanning priority if 42121d82de61SMel Gorman * enough pages are already being scanned that that high 42131d82de61SMel Gorman * watermark would be met at 100% efficiency. 421432a4330dSRik van Riel */ 4215970a39a3SMel Gorman if (kswapd_shrink_node(pgdat, &sc)) 4216b8e83b94SMel Gorman raise_priority = false; 4217d7868daeSMel Gorman 42185515061dSMel Gorman /* 42195515061dSMel Gorman * If the low watermark is met there is no need for processes 42205515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 42215515061dSMel Gorman * able to safely make forward progress. Wake them 42225515061dSMel Gorman */ 42235515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 4224c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)) 4225cfc51155SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 42265515061dSMel Gorman 4227b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 42284f3eaf45SMatthew Wilcox (Oracle) __fs_reclaim_release(_THIS_IP_); 422993781325SOmar Sandoval ret = try_to_freeze(); 42304f3eaf45SMatthew Wilcox (Oracle) __fs_reclaim_acquire(_THIS_IP_); 423193781325SOmar Sandoval if (ret || kthread_should_stop()) 4232b8e83b94SMel Gorman break; 4233b8e83b94SMel Gorman 4234b8e83b94SMel Gorman /* 4235b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 4236b8e83b94SMel Gorman * progress in reclaiming pages 4237b8e83b94SMel Gorman */ 4238c73322d0SJohannes Weiner nr_reclaimed = sc.nr_reclaimed - nr_reclaimed; 42391c30844dSMel Gorman nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed); 42401c30844dSMel Gorman 42411c30844dSMel Gorman /* 42421c30844dSMel Gorman * If reclaim made no progress for a boost, stop reclaim as 42431c30844dSMel Gorman * IO cannot be queued and it could be an infinite loop in 42441c30844dSMel Gorman * extreme circumstances. 42451c30844dSMel Gorman */ 42461c30844dSMel Gorman if (nr_boost_reclaim && !nr_reclaimed) 42471c30844dSMel Gorman break; 42481c30844dSMel Gorman 4249c73322d0SJohannes Weiner if (raise_priority || !nr_reclaimed) 4250b8e83b94SMel Gorman sc.priority--; 42511d82de61SMel Gorman } while (sc.priority >= 1); 42521da177e4SLinus Torvalds 4253c73322d0SJohannes Weiner if (!sc.nr_reclaimed) 4254c73322d0SJohannes Weiner pgdat->kswapd_failures++; 4255c73322d0SJohannes Weiner 4256b8e83b94SMel Gorman out: 4257c49c2c47SMel Gorman clear_reclaim_active(pgdat, highest_zoneidx); 4258c49c2c47SMel Gorman 42591c30844dSMel Gorman /* If reclaim was boosted, account for the reclaim done in this pass */ 42601c30844dSMel Gorman if (boosted) { 42611c30844dSMel Gorman unsigned long flags; 42621c30844dSMel Gorman 426397a225e6SJoonsoo Kim for (i = 0; i <= highest_zoneidx; i++) { 42641c30844dSMel Gorman if (!zone_boosts[i]) 42651c30844dSMel Gorman continue; 42661c30844dSMel Gorman 42671c30844dSMel Gorman /* Increments are under the zone lock */ 42681c30844dSMel Gorman zone = pgdat->node_zones + i; 42691c30844dSMel Gorman spin_lock_irqsave(&zone->lock, flags); 42701c30844dSMel Gorman zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]); 42711c30844dSMel Gorman spin_unlock_irqrestore(&zone->lock, flags); 42721c30844dSMel Gorman } 42731c30844dSMel Gorman 42741c30844dSMel Gorman /* 42751c30844dSMel Gorman * As there is now likely space, wakeup kcompact to defragment 42761c30844dSMel Gorman * pageblocks. 42771c30844dSMel Gorman */ 427897a225e6SJoonsoo Kim wakeup_kcompactd(pgdat, pageblock_order, highest_zoneidx); 42791c30844dSMel Gorman } 42801c30844dSMel Gorman 42812a2e4885SJohannes Weiner snapshot_refaults(NULL, pgdat); 42824f3eaf45SMatthew Wilcox (Oracle) __fs_reclaim_release(_THIS_IP_); 4283eb414681SJohannes Weiner psi_memstall_leave(&pflags); 42841732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 4285e5ca8071SYafang Shao 42860abdee2bSMel Gorman /* 42871d82de61SMel Gorman * Return the order kswapd stopped reclaiming at as 42881d82de61SMel Gorman * prepare_kswapd_sleep() takes it into account. If another caller 42891d82de61SMel Gorman * entered the allocator slow path while kswapd was awake, order will 42901d82de61SMel Gorman * remain at the higher level. 42910abdee2bSMel Gorman */ 42921d82de61SMel Gorman return sc.order; 42931da177e4SLinus Torvalds } 42941da177e4SLinus Torvalds 4295e716f2ebSMel Gorman /* 429697a225e6SJoonsoo Kim * The pgdat->kswapd_highest_zoneidx is used to pass the highest zone index to 429797a225e6SJoonsoo Kim * be reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is 429897a225e6SJoonsoo Kim * not a valid index then either kswapd runs for first time or kswapd couldn't 429997a225e6SJoonsoo Kim * sleep after previous reclaim attempt (node is still unbalanced). In that 430097a225e6SJoonsoo Kim * case return the zone index of the previous kswapd reclaim cycle. 4301e716f2ebSMel Gorman */ 430297a225e6SJoonsoo Kim static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat, 430397a225e6SJoonsoo Kim enum zone_type prev_highest_zoneidx) 4304e716f2ebSMel Gorman { 430597a225e6SJoonsoo Kim enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); 43065644e1fbSQian Cai 430797a225e6SJoonsoo Kim return curr_idx == MAX_NR_ZONES ? prev_highest_zoneidx : curr_idx; 4308e716f2ebSMel Gorman } 4309e716f2ebSMel Gorman 431038087d9bSMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, 431197a225e6SJoonsoo Kim unsigned int highest_zoneidx) 4312f0bc0a60SKOSAKI Motohiro { 4313f0bc0a60SKOSAKI Motohiro long remaining = 0; 4314f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 4315f0bc0a60SKOSAKI Motohiro 4316f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 4317f0bc0a60SKOSAKI Motohiro return; 4318f0bc0a60SKOSAKI Motohiro 4319f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 4320f0bc0a60SKOSAKI Motohiro 4321333b0a45SShantanu Goel /* 4322333b0a45SShantanu Goel * Try to sleep for a short interval. Note that kcompactd will only be 4323333b0a45SShantanu Goel * woken if it is possible to sleep for a short interval. This is 4324333b0a45SShantanu Goel * deliberate on the assumption that if reclaim cannot keep an 4325333b0a45SShantanu Goel * eligible zone balanced that it's also unlikely that compaction will 4326333b0a45SShantanu Goel * succeed. 4327333b0a45SShantanu Goel */ 432897a225e6SJoonsoo Kim if (prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { 4329fd901c95SVlastimil Babka /* 4330fd901c95SVlastimil Babka * Compaction records what page blocks it recently failed to 4331fd901c95SVlastimil Babka * isolate pages from and skips them in the future scanning. 4332fd901c95SVlastimil Babka * When kswapd is going to sleep, it is reasonable to assume 4333fd901c95SVlastimil Babka * that pages and compaction may succeed so reset the cache. 4334fd901c95SVlastimil Babka */ 4335fd901c95SVlastimil Babka reset_isolation_suitable(pgdat); 4336fd901c95SVlastimil Babka 4337fd901c95SVlastimil Babka /* 4338fd901c95SVlastimil Babka * We have freed the memory, now we should compact it to make 4339fd901c95SVlastimil Babka * allocation of the requested order possible. 4340fd901c95SVlastimil Babka */ 434197a225e6SJoonsoo Kim wakeup_kcompactd(pgdat, alloc_order, highest_zoneidx); 4342fd901c95SVlastimil Babka 4343f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 434438087d9bSMel Gorman 434538087d9bSMel Gorman /* 434697a225e6SJoonsoo Kim * If woken prematurely then reset kswapd_highest_zoneidx and 434738087d9bSMel Gorman * order. The values will either be from a wakeup request or 434838087d9bSMel Gorman * the previous request that slept prematurely. 434938087d9bSMel Gorman */ 435038087d9bSMel Gorman if (remaining) { 435197a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, 435297a225e6SJoonsoo Kim kswapd_highest_zoneidx(pgdat, 435397a225e6SJoonsoo Kim highest_zoneidx)); 43545644e1fbSQian Cai 43555644e1fbSQian Cai if (READ_ONCE(pgdat->kswapd_order) < reclaim_order) 43565644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, reclaim_order); 435738087d9bSMel Gorman } 435838087d9bSMel Gorman 4359f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 4360f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 4361f0bc0a60SKOSAKI Motohiro } 4362f0bc0a60SKOSAKI Motohiro 4363f0bc0a60SKOSAKI Motohiro /* 4364f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 4365f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 4366f0bc0a60SKOSAKI Motohiro */ 4367d9f21d42SMel Gorman if (!remaining && 436897a225e6SJoonsoo Kim prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { 4369f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 4370f0bc0a60SKOSAKI Motohiro 4371f0bc0a60SKOSAKI Motohiro /* 4372f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 4373f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 4374f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 4375f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 4376f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 4377f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 4378f0bc0a60SKOSAKI Motohiro */ 4379f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 43801c7e7f6cSAaditya Kumar 43811c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 4382f0bc0a60SKOSAKI Motohiro schedule(); 43831c7e7f6cSAaditya Kumar 4384f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 4385f0bc0a60SKOSAKI Motohiro } else { 4386f0bc0a60SKOSAKI Motohiro if (remaining) 4387f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 4388f0bc0a60SKOSAKI Motohiro else 4389f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 4390f0bc0a60SKOSAKI Motohiro } 4391f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 4392f0bc0a60SKOSAKI Motohiro } 4393f0bc0a60SKOSAKI Motohiro 43941da177e4SLinus Torvalds /* 43951da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 43961da177e4SLinus Torvalds * from the init process. 43971da177e4SLinus Torvalds * 43981da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 43991da177e4SLinus Torvalds * free memory available even if there is no other activity 44001da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 44011da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 44021da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 44031da177e4SLinus Torvalds * 44041da177e4SLinus Torvalds * If there are applications that are active memory-allocators 44051da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 44061da177e4SLinus Torvalds */ 44071da177e4SLinus Torvalds static int kswapd(void *p) 44081da177e4SLinus Torvalds { 4409e716f2ebSMel Gorman unsigned int alloc_order, reclaim_order; 441097a225e6SJoonsoo Kim unsigned int highest_zoneidx = MAX_NR_ZONES - 1; 44111da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t *)p; 44121da177e4SLinus Torvalds struct task_struct *tsk = current; 4413a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 44141da177e4SLinus Torvalds 4415174596a0SRusty Russell if (!cpumask_empty(cpumask)) 4416c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 44171da177e4SLinus Torvalds 44181da177e4SLinus Torvalds /* 44191da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 44201da177e4SLinus Torvalds * and that if we need more memory we should get access to it 44211da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 44221da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 44231da177e4SLinus Torvalds * 44241da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 44251da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 44261da177e4SLinus Torvalds * page out something else, and this flag essentially protects 44271da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 44281da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 44291da177e4SLinus Torvalds */ 4430b698f0a1SHugh Dickins tsk->flags |= PF_MEMALLOC | PF_KSWAPD; 443183144186SRafael J. Wysocki set_freezable(); 44321da177e4SLinus Torvalds 44335644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, 0); 443497a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); 44358cd7c588SMel Gorman atomic_set(&pgdat->nr_writeback_throttled, 0); 44361da177e4SLinus Torvalds for ( ; ; ) { 44376f6313d4SJeff Liu bool ret; 44383e1d1d28SChristoph Lameter 44395644e1fbSQian Cai alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order); 444097a225e6SJoonsoo Kim highest_zoneidx = kswapd_highest_zoneidx(pgdat, 444197a225e6SJoonsoo Kim highest_zoneidx); 4442e716f2ebSMel Gorman 444338087d9bSMel Gorman kswapd_try_sleep: 444438087d9bSMel Gorman kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, 444597a225e6SJoonsoo Kim highest_zoneidx); 4446215ddd66SMel Gorman 444797a225e6SJoonsoo Kim /* Read the new order and highest_zoneidx */ 44482b47a24cSLukas Bulwahn alloc_order = READ_ONCE(pgdat->kswapd_order); 444997a225e6SJoonsoo Kim highest_zoneidx = kswapd_highest_zoneidx(pgdat, 445097a225e6SJoonsoo Kim highest_zoneidx); 44515644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, 0); 445297a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); 44531da177e4SLinus Torvalds 44548fe23e05SDavid Rientjes ret = try_to_freeze(); 44558fe23e05SDavid Rientjes if (kthread_should_stop()) 44568fe23e05SDavid Rientjes break; 44578fe23e05SDavid Rientjes 44588fe23e05SDavid Rientjes /* 44598fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 44608fe23e05SDavid Rientjes * after returning from the refrigerator 4461b1296cc4SRafael J. Wysocki */ 446238087d9bSMel Gorman if (ret) 446338087d9bSMel Gorman continue; 44641d82de61SMel Gorman 446538087d9bSMel Gorman /* 446638087d9bSMel Gorman * Reclaim begins at the requested order but if a high-order 446738087d9bSMel Gorman * reclaim fails then kswapd falls back to reclaiming for 446838087d9bSMel Gorman * order-0. If that happens, kswapd will consider sleeping 446938087d9bSMel Gorman * for the order it finished reclaiming at (reclaim_order) 447038087d9bSMel Gorman * but kcompactd is woken to compact for the original 447138087d9bSMel Gorman * request (alloc_order). 447238087d9bSMel Gorman */ 447397a225e6SJoonsoo Kim trace_mm_vmscan_kswapd_wake(pgdat->node_id, highest_zoneidx, 4474e5146b12SMel Gorman alloc_order); 447597a225e6SJoonsoo Kim reclaim_order = balance_pgdat(pgdat, alloc_order, 447697a225e6SJoonsoo Kim highest_zoneidx); 447738087d9bSMel Gorman if (reclaim_order < alloc_order) 447838087d9bSMel Gorman goto kswapd_try_sleep; 447933906bc5SMel Gorman } 4480b0a8cc58STakamori Yamaguchi 4481b698f0a1SHugh Dickins tsk->flags &= ~(PF_MEMALLOC | PF_KSWAPD); 448271abdc15SJohannes Weiner 44831da177e4SLinus Torvalds return 0; 44841da177e4SLinus Torvalds } 44851da177e4SLinus Torvalds 44861da177e4SLinus Torvalds /* 44875ecd9d40SDavid Rientjes * A zone is low on free memory or too fragmented for high-order memory. If 44885ecd9d40SDavid Rientjes * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's 44895ecd9d40SDavid Rientjes * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim 44905ecd9d40SDavid Rientjes * has failed or is not needed, still wake up kcompactd if only compaction is 44915ecd9d40SDavid Rientjes * needed. 44921da177e4SLinus Torvalds */ 44935ecd9d40SDavid Rientjes void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, 449497a225e6SJoonsoo Kim enum zone_type highest_zoneidx) 44951da177e4SLinus Torvalds { 44961da177e4SLinus Torvalds pg_data_t *pgdat; 44975644e1fbSQian Cai enum zone_type curr_idx; 44981da177e4SLinus Torvalds 44996aa303deSMel Gorman if (!managed_zone(zone)) 45001da177e4SLinus Torvalds return; 45011da177e4SLinus Torvalds 45025ecd9d40SDavid Rientjes if (!cpuset_zone_allowed(zone, gfp_flags)) 45031da177e4SLinus Torvalds return; 4504dffcac2cSShakeel Butt 45055644e1fbSQian Cai pgdat = zone->zone_pgdat; 450697a225e6SJoonsoo Kim curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); 45075644e1fbSQian Cai 450897a225e6SJoonsoo Kim if (curr_idx == MAX_NR_ZONES || curr_idx < highest_zoneidx) 450997a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, highest_zoneidx); 45105644e1fbSQian Cai 45115644e1fbSQian Cai if (READ_ONCE(pgdat->kswapd_order) < order) 45125644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, order); 45135644e1fbSQian Cai 45148d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 45151da177e4SLinus Torvalds return; 4516e1a55637SMel Gorman 45175ecd9d40SDavid Rientjes /* Hopeless node, leave it to direct reclaim if possible */ 45185ecd9d40SDavid Rientjes if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES || 451997a225e6SJoonsoo Kim (pgdat_balanced(pgdat, order, highest_zoneidx) && 452097a225e6SJoonsoo Kim !pgdat_watermark_boosted(pgdat, highest_zoneidx))) { 45215ecd9d40SDavid Rientjes /* 45225ecd9d40SDavid Rientjes * There may be plenty of free memory available, but it's too 45235ecd9d40SDavid Rientjes * fragmented for high-order allocations. Wake up kcompactd 45245ecd9d40SDavid Rientjes * and rely on compaction_suitable() to determine if it's 45255ecd9d40SDavid Rientjes * needed. If it fails, it will defer subsequent attempts to 45265ecd9d40SDavid Rientjes * ratelimit its work. 45275ecd9d40SDavid Rientjes */ 45285ecd9d40SDavid Rientjes if (!(gfp_flags & __GFP_DIRECT_RECLAIM)) 452997a225e6SJoonsoo Kim wakeup_kcompactd(pgdat, order, highest_zoneidx); 4530c73322d0SJohannes Weiner return; 45315ecd9d40SDavid Rientjes } 4532c73322d0SJohannes Weiner 453397a225e6SJoonsoo Kim trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, highest_zoneidx, order, 45345ecd9d40SDavid Rientjes gfp_flags); 45358d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 45361da177e4SLinus Torvalds } 45371da177e4SLinus Torvalds 4538c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 45391da177e4SLinus Torvalds /* 45407b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 4541d6277db4SRafael J. Wysocki * freed pages. 4542d6277db4SRafael J. Wysocki * 4543d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 4544d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 4545d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 45461da177e4SLinus Torvalds */ 45477b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 45481da177e4SLinus Torvalds { 4549d6277db4SRafael J. Wysocki struct scan_control sc = { 45507b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 4551ee814fe2SJohannes Weiner .gfp_mask = GFP_HIGHUSER_MOVABLE, 4552b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 45539e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 4554ee814fe2SJohannes Weiner .may_writepage = 1, 4555ee814fe2SJohannes Weiner .may_unmap = 1, 4556ee814fe2SJohannes Weiner .may_swap = 1, 4557ee814fe2SJohannes Weiner .hibernation_mode = 1, 45581da177e4SLinus Torvalds }; 45597b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 45607b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 4561499118e9SVlastimil Babka unsigned int noreclaim_flag; 45621da177e4SLinus Torvalds 4563d92a8cfcSPeter Zijlstra fs_reclaim_acquire(sc.gfp_mask); 456493781325SOmar Sandoval noreclaim_flag = memalloc_noreclaim_save(); 45651732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 4566d6277db4SRafael J. Wysocki 45673115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 4568d6277db4SRafael J. Wysocki 45691732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 4570499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 457193781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 4572d6277db4SRafael J. Wysocki 45737b51755cSKOSAKI Motohiro return nr_reclaimed; 45741da177e4SLinus Torvalds } 4575c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 45761da177e4SLinus Torvalds 45773218ae14SYasunori Goto /* 45783218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 45793218ae14SYasunori Goto */ 4580b87c517aSMiaohe Lin void kswapd_run(int nid) 45813218ae14SYasunori Goto { 45823218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 45833218ae14SYasunori Goto 45843218ae14SYasunori Goto if (pgdat->kswapd) 4585b87c517aSMiaohe Lin return; 45863218ae14SYasunori Goto 45873218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 45883218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 45893218ae14SYasunori Goto /* failure at boot is fatal */ 4590c6202adfSThomas Gleixner BUG_ON(system_state < SYSTEM_RUNNING); 4591d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 4592d72515b8SXishi Qiu pgdat->kswapd = NULL; 45933218ae14SYasunori Goto } 45943218ae14SYasunori Goto } 45953218ae14SYasunori Goto 45968fe23e05SDavid Rientjes /* 4597d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 4598bfc8c901SVladimir Davydov * hold mem_hotplug_begin/end(). 45998fe23e05SDavid Rientjes */ 46008fe23e05SDavid Rientjes void kswapd_stop(int nid) 46018fe23e05SDavid Rientjes { 46028fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 46038fe23e05SDavid Rientjes 4604d8adde17SJiang Liu if (kswapd) { 46058fe23e05SDavid Rientjes kthread_stop(kswapd); 4606d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 4607d8adde17SJiang Liu } 46088fe23e05SDavid Rientjes } 46098fe23e05SDavid Rientjes 46101da177e4SLinus Torvalds static int __init kswapd_init(void) 46111da177e4SLinus Torvalds { 46126b700b5bSWei Yang int nid; 461369e05944SAndrew Morton 46141da177e4SLinus Torvalds swap_setup(); 461548fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 46163218ae14SYasunori Goto kswapd_run(nid); 46171da177e4SLinus Torvalds return 0; 46181da177e4SLinus Torvalds } 46191da177e4SLinus Torvalds 46201da177e4SLinus Torvalds module_init(kswapd_init) 46219eeff239SChristoph Lameter 46229eeff239SChristoph Lameter #ifdef CONFIG_NUMA 46239eeff239SChristoph Lameter /* 4624a5f5f91dSMel Gorman * Node reclaim mode 46259eeff239SChristoph Lameter * 4626a5f5f91dSMel Gorman * If non-zero call node_reclaim when the number of free pages falls below 46279eeff239SChristoph Lameter * the watermarks. 46289eeff239SChristoph Lameter */ 4629a5f5f91dSMel Gorman int node_reclaim_mode __read_mostly; 46309eeff239SChristoph Lameter 463151998364SDave Hansen /* 4632a5f5f91dSMel Gorman * Priority for NODE_RECLAIM. This determines the fraction of pages 4633a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 4634a92f7126SChristoph Lameter * a zone. 4635a92f7126SChristoph Lameter */ 4636a5f5f91dSMel Gorman #define NODE_RECLAIM_PRIORITY 4 4637a92f7126SChristoph Lameter 46389eeff239SChristoph Lameter /* 4639a5f5f91dSMel Gorman * Percentage of pages in a zone that must be unmapped for node_reclaim to 46409614634fSChristoph Lameter * occur. 46419614634fSChristoph Lameter */ 46429614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 46439614634fSChristoph Lameter 46449614634fSChristoph Lameter /* 46450ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 46460ff38490SChristoph Lameter * slab reclaim needs to occur. 46470ff38490SChristoph Lameter */ 46480ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 46490ff38490SChristoph Lameter 465011fb9989SMel Gorman static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) 465190afa5deSMel Gorman { 465211fb9989SMel Gorman unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); 465311fb9989SMel Gorman unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + 465411fb9989SMel Gorman node_page_state(pgdat, NR_ACTIVE_FILE); 465590afa5deSMel Gorman 465690afa5deSMel Gorman /* 465790afa5deSMel Gorman * It's possible for there to be more file mapped pages than 465890afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 465990afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 466090afa5deSMel Gorman */ 466190afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 466290afa5deSMel Gorman } 466390afa5deSMel Gorman 466490afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 4665a5f5f91dSMel Gorman static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) 466690afa5deSMel Gorman { 4667d031a157SAlexandru Moise unsigned long nr_pagecache_reclaimable; 4668d031a157SAlexandru Moise unsigned long delta = 0; 466990afa5deSMel Gorman 467090afa5deSMel Gorman /* 467195bbc0c7SZhihui Zhang * If RECLAIM_UNMAP is set, then all file pages are considered 467290afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 467311fb9989SMel Gorman * pages like swapcache and node_unmapped_file_pages() provides 467490afa5deSMel Gorman * a better estimate 467590afa5deSMel Gorman */ 4676a5f5f91dSMel Gorman if (node_reclaim_mode & RECLAIM_UNMAP) 4677a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); 467890afa5deSMel Gorman else 4679a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); 468090afa5deSMel Gorman 468190afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 4682a5f5f91dSMel Gorman if (!(node_reclaim_mode & RECLAIM_WRITE)) 4683a5f5f91dSMel Gorman delta += node_page_state(pgdat, NR_FILE_DIRTY); 468490afa5deSMel Gorman 468590afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 468690afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 468790afa5deSMel Gorman delta = nr_pagecache_reclaimable; 468890afa5deSMel Gorman 468990afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 469090afa5deSMel Gorman } 469190afa5deSMel Gorman 46920ff38490SChristoph Lameter /* 4693a5f5f91dSMel Gorman * Try to free up some pages from this node through reclaim. 46949eeff239SChristoph Lameter */ 4695a5f5f91dSMel Gorman static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 46969eeff239SChristoph Lameter { 46977fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 469869e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 46999eeff239SChristoph Lameter struct task_struct *p = current; 4700499118e9SVlastimil Babka unsigned int noreclaim_flag; 4701179e9639SAndrew Morton struct scan_control sc = { 470262b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 4703f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 4704bd2f6199SJohannes Weiner .order = order, 4705a5f5f91dSMel Gorman .priority = NODE_RECLAIM_PRIORITY, 4706a5f5f91dSMel Gorman .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), 4707a5f5f91dSMel Gorman .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), 4708ee814fe2SJohannes Weiner .may_swap = 1, 4709f2f43e56SNick Desaulniers .reclaim_idx = gfp_zone(gfp_mask), 4710179e9639SAndrew Morton }; 471157f29762SJohannes Weiner unsigned long pflags; 47129eeff239SChristoph Lameter 4713132bb8cfSYafang Shao trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order, 4714132bb8cfSYafang Shao sc.gfp_mask); 4715132bb8cfSYafang Shao 47169eeff239SChristoph Lameter cond_resched(); 471757f29762SJohannes Weiner psi_memstall_enter(&pflags); 471893781325SOmar Sandoval fs_reclaim_acquire(sc.gfp_mask); 4719d4f7796eSChristoph Lameter /* 472095bbc0c7SZhihui Zhang * We need to be able to allocate from the reserves for RECLAIM_UNMAP 4721d4f7796eSChristoph Lameter */ 4722499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 47231732d2b0SAndrew Morton set_task_reclaim_state(p, &sc.reclaim_state); 4724c84db23cSChristoph Lameter 4725d8ff6fdeSMiaohe Lin if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages || 4726d8ff6fdeSMiaohe Lin node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) > pgdat->min_slab_pages) { 4727a92f7126SChristoph Lameter /* 4728894befecSAndrey Ryabinin * Free memory by calling shrink node with increasing 47290ff38490SChristoph Lameter * priorities until we have enough memory freed. 4730a92f7126SChristoph Lameter */ 4731a92f7126SChristoph Lameter do { 4732970a39a3SMel Gorman shrink_node(pgdat, &sc); 47339e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 47340ff38490SChristoph Lameter } 4735a92f7126SChristoph Lameter 47361732d2b0SAndrew Morton set_task_reclaim_state(p, NULL); 4737499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 473893781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 473957f29762SJohannes Weiner psi_memstall_leave(&pflags); 4740132bb8cfSYafang Shao 4741132bb8cfSYafang Shao trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed); 4742132bb8cfSYafang Shao 4743a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 47449eeff239SChristoph Lameter } 4745179e9639SAndrew Morton 4746a5f5f91dSMel Gorman int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 4747179e9639SAndrew Morton { 4748d773ed6bSDavid Rientjes int ret; 4749179e9639SAndrew Morton 4750179e9639SAndrew Morton /* 4751a5f5f91dSMel Gorman * Node reclaim reclaims unmapped file backed pages and 47520ff38490SChristoph Lameter * slab pages if we are over the defined limits. 475334aa1330SChristoph Lameter * 47549614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 47559614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 4756a5f5f91dSMel Gorman * thrown out if the node is overallocated. So we do not reclaim 4757a5f5f91dSMel Gorman * if less than a specified percentage of the node is used by 47589614634fSChristoph Lameter * unmapped file backed pages. 4759179e9639SAndrew Morton */ 4760a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && 4761d42f3245SRoman Gushchin node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) <= 4762d42f3245SRoman Gushchin pgdat->min_slab_pages) 4763a5f5f91dSMel Gorman return NODE_RECLAIM_FULL; 4764179e9639SAndrew Morton 4765179e9639SAndrew Morton /* 4766d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 4767179e9639SAndrew Morton */ 4768d0164adcSMel Gorman if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) 4769a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4770179e9639SAndrew Morton 4771179e9639SAndrew Morton /* 4772a5f5f91dSMel Gorman * Only run node reclaim on the local node or on nodes that do not 4773179e9639SAndrew Morton * have associated processors. This will favor the local processor 4774179e9639SAndrew Morton * over remote processors and spread off node memory allocations 4775179e9639SAndrew Morton * as wide as possible. 4776179e9639SAndrew Morton */ 4777a5f5f91dSMel Gorman if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) 4778a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4779d773ed6bSDavid Rientjes 4780a5f5f91dSMel Gorman if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) 4781a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4782fa5e084eSMel Gorman 4783a5f5f91dSMel Gorman ret = __node_reclaim(pgdat, gfp_mask, order); 4784a5f5f91dSMel Gorman clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); 4785d773ed6bSDavid Rientjes 478624cf7251SMel Gorman if (!ret) 478724cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 478824cf7251SMel Gorman 4789d773ed6bSDavid Rientjes return ret; 4790179e9639SAndrew Morton } 47919eeff239SChristoph Lameter #endif 4792894bc310SLee Schermerhorn 479389e004eaSLee Schermerhorn /** 479464e3d12fSKuo-Hsin Yang * check_move_unevictable_pages - check pages for evictability and move to 479564e3d12fSKuo-Hsin Yang * appropriate zone lru list 479664e3d12fSKuo-Hsin Yang * @pvec: pagevec with lru pages to check 479789e004eaSLee Schermerhorn * 479864e3d12fSKuo-Hsin Yang * Checks pages for evictability, if an evictable page is in the unevictable 479964e3d12fSKuo-Hsin Yang * lru list, moves it to the appropriate evictable lru list. This function 480064e3d12fSKuo-Hsin Yang * should be only used for lru pages. 480189e004eaSLee Schermerhorn */ 480264e3d12fSKuo-Hsin Yang void check_move_unevictable_pages(struct pagevec *pvec) 480389e004eaSLee Schermerhorn { 48046168d0daSAlex Shi struct lruvec *lruvec = NULL; 480524513264SHugh Dickins int pgscanned = 0; 480624513264SHugh Dickins int pgrescued = 0; 480789e004eaSLee Schermerhorn int i; 480889e004eaSLee Schermerhorn 480964e3d12fSKuo-Hsin Yang for (i = 0; i < pvec->nr; i++) { 481064e3d12fSKuo-Hsin Yang struct page *page = pvec->pages[i]; 48110de340cbSMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 48128d8869caSHugh Dickins int nr_pages; 481389e004eaSLee Schermerhorn 48148d8869caSHugh Dickins if (PageTransTail(page)) 48158d8869caSHugh Dickins continue; 48168d8869caSHugh Dickins 48178d8869caSHugh Dickins nr_pages = thp_nr_pages(page); 48188d8869caSHugh Dickins pgscanned += nr_pages; 48198d8869caSHugh Dickins 4820d25b5bd8SAlex Shi /* block memcg migration during page moving between lru */ 4821d25b5bd8SAlex Shi if (!TestClearPageLRU(page)) 4822d25b5bd8SAlex Shi continue; 4823d25b5bd8SAlex Shi 48240de340cbSMatthew Wilcox (Oracle) lruvec = folio_lruvec_relock_irq(folio, lruvec); 4825d25b5bd8SAlex Shi if (page_evictable(page) && PageUnevictable(page)) { 482646ae6b2cSYu Zhao del_page_from_lru_list(page, lruvec); 482724513264SHugh Dickins ClearPageUnevictable(page); 48283a9c9788SYu Zhao add_page_to_lru_list(page, lruvec); 48298d8869caSHugh Dickins pgrescued += nr_pages; 483089e004eaSLee Schermerhorn } 4831d25b5bd8SAlex Shi SetPageLRU(page); 483289e004eaSLee Schermerhorn } 483324513264SHugh Dickins 48346168d0daSAlex Shi if (lruvec) { 483524513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 483624513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 48376168d0daSAlex Shi unlock_page_lruvec_irq(lruvec); 4838d25b5bd8SAlex Shi } else if (pgscanned) { 4839d25b5bd8SAlex Shi count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 484024513264SHugh Dickins } 484185046579SHugh Dickins } 484264e3d12fSKuo-Hsin Yang EXPORT_SYMBOL_GPL(check_move_unevictable_pages); 4843