18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * linux/mm/vmscan.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Swap reorganised 29.12.95, Stephen Tweedie. 88c2ecf20Sopenharmony_ci * kswapd added: 7.1.96 sct 98c2ecf20Sopenharmony_ci * Removed kswapd_ctl limits, and swap out as many pages as needed 108c2ecf20Sopenharmony_ci * to bring the system back to freepages.high: 2.4.97, Rik van Riel. 118c2ecf20Sopenharmony_ci * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com). 128c2ecf20Sopenharmony_ci * Multiqueue VM started 5.8.00, Rik van Riel. 138c2ecf20Sopenharmony_ci */ 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include <linux/mm.h> 188c2ecf20Sopenharmony_ci#include <linux/sched/mm.h> 198c2ecf20Sopenharmony_ci#include <linux/module.h> 208c2ecf20Sopenharmony_ci#include <linux/gfp.h> 218c2ecf20Sopenharmony_ci#include <linux/kernel_stat.h> 228c2ecf20Sopenharmony_ci#include <linux/swap.h> 238c2ecf20Sopenharmony_ci#include <linux/pagemap.h> 248c2ecf20Sopenharmony_ci#include <linux/init.h> 258c2ecf20Sopenharmony_ci#include <linux/highmem.h> 268c2ecf20Sopenharmony_ci#include <linux/vmpressure.h> 278c2ecf20Sopenharmony_ci#include <linux/vmstat.h> 288c2ecf20Sopenharmony_ci#include <linux/file.h> 298c2ecf20Sopenharmony_ci#include <linux/writeback.h> 308c2ecf20Sopenharmony_ci#include <linux/blkdev.h> 318c2ecf20Sopenharmony_ci#include <linux/buffer_head.h> /* for try_to_release_page(), 328c2ecf20Sopenharmony_ci buffer_heads_over_limit */ 338c2ecf20Sopenharmony_ci#include <linux/mm_inline.h> 348c2ecf20Sopenharmony_ci#include <linux/backing-dev.h> 358c2ecf20Sopenharmony_ci#include <linux/rmap.h> 368c2ecf20Sopenharmony_ci#include <linux/topology.h> 378c2ecf20Sopenharmony_ci#include <linux/cpu.h> 388c2ecf20Sopenharmony_ci#include <linux/cpuset.h> 398c2ecf20Sopenharmony_ci#include <linux/compaction.h> 408c2ecf20Sopenharmony_ci#include <linux/notifier.h> 418c2ecf20Sopenharmony_ci#include <linux/rwsem.h> 428c2ecf20Sopenharmony_ci#include <linux/delay.h> 438c2ecf20Sopenharmony_ci#include <linux/kthread.h> 448c2ecf20Sopenharmony_ci#include <linux/freezer.h> 458c2ecf20Sopenharmony_ci#include <linux/memcontrol.h> 468c2ecf20Sopenharmony_ci#include <linux/delayacct.h> 478c2ecf20Sopenharmony_ci#include <linux/sysctl.h> 488c2ecf20Sopenharmony_ci#include <linux/oom.h> 498c2ecf20Sopenharmony_ci#include <linux/pagevec.h> 508c2ecf20Sopenharmony_ci#include <linux/prefetch.h> 518c2ecf20Sopenharmony_ci#include <linux/printk.h> 528c2ecf20Sopenharmony_ci#include <linux/dax.h> 538c2ecf20Sopenharmony_ci#include <linux/psi.h> 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci#include <asm/tlbflush.h> 568c2ecf20Sopenharmony_ci#include <asm/div64.h> 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci#include <linux/swapops.h> 598c2ecf20Sopenharmony_ci#include <linux/balloon_compaction.h> 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci#include "internal.h" 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci#define CREATE_TRACE_POINTS 648c2ecf20Sopenharmony_ci#include <trace/events/vmscan.h> 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci#ifdef CONFIG_HYPERHOLD_FILE_LRU 678c2ecf20Sopenharmony_ci#include <linux/memcg_policy.h> 688c2ecf20Sopenharmony_ci#endif 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci#ifdef CONFIG_RECLAIM_ACCT 718c2ecf20Sopenharmony_ci#include <linux/reclaim_acct.h> 728c2ecf20Sopenharmony_ci#endif 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci#ifdef ARCH_HAS_PREFETCHW 758c2ecf20Sopenharmony_ci#define prefetchw_prev_lru_page(_page, _base, _field) \ 768c2ecf20Sopenharmony_ci do { \ 778c2ecf20Sopenharmony_ci if ((_page)->lru.prev != _base) { \ 788c2ecf20Sopenharmony_ci struct page *prev; \ 798c2ecf20Sopenharmony_ci \ 808c2ecf20Sopenharmony_ci prev = lru_to_page(&(_page->lru)); \ 818c2ecf20Sopenharmony_ci prefetchw(&prev->_field); \ 828c2ecf20Sopenharmony_ci } \ 838c2ecf20Sopenharmony_ci } while (0) 848c2ecf20Sopenharmony_ci#else 858c2ecf20Sopenharmony_ci#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 868c2ecf20Sopenharmony_ci#endif 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci#ifdef CONFIG_HYPERHOLD_FILE_LRU 898c2ecf20Sopenharmony_ciunsigned int enough_inactive_file = 1; 908c2ecf20Sopenharmony_ci#endif 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci/* 938c2ecf20Sopenharmony_ci * From 0 .. 200. Higher means more swappy. 948c2ecf20Sopenharmony_ci */ 958c2ecf20Sopenharmony_ciint vm_swappiness = 60; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_cistatic void set_task_reclaim_state(struct task_struct *task, 988c2ecf20Sopenharmony_ci struct reclaim_state *rs) 998c2ecf20Sopenharmony_ci{ 1008c2ecf20Sopenharmony_ci /* Check for an overwrite */ 1018c2ecf20Sopenharmony_ci WARN_ON_ONCE(rs && task->reclaim_state); 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci /* Check for the nulling of an already-nulled member */ 1048c2ecf20Sopenharmony_ci WARN_ON_ONCE(!rs && !task->reclaim_state); 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci task->reclaim_state = rs; 1078c2ecf20Sopenharmony_ci} 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cistatic LIST_HEAD(shrinker_list); 1108c2ecf20Sopenharmony_cistatic DECLARE_RWSEM(shrinker_rwsem); 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci#ifdef CONFIG_MEMCG 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_cistatic DEFINE_IDR(shrinker_idr); 1158c2ecf20Sopenharmony_cistatic int shrinker_nr_max; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic int prealloc_memcg_shrinker(struct shrinker *shrinker) 1188c2ecf20Sopenharmony_ci{ 1198c2ecf20Sopenharmony_ci int id, ret = -ENOMEM; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci down_write(&shrinker_rwsem); 1228c2ecf20Sopenharmony_ci /* This may call shrinker, so it must use down_read_trylock() */ 1238c2ecf20Sopenharmony_ci id = idr_alloc(&shrinker_idr, shrinker, 0, 0, GFP_KERNEL); 1248c2ecf20Sopenharmony_ci if (id < 0) 1258c2ecf20Sopenharmony_ci goto unlock; 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci if (id >= shrinker_nr_max) { 1288c2ecf20Sopenharmony_ci if (memcg_expand_shrinker_maps(id)) { 1298c2ecf20Sopenharmony_ci idr_remove(&shrinker_idr, id); 1308c2ecf20Sopenharmony_ci goto unlock; 1318c2ecf20Sopenharmony_ci } 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci shrinker_nr_max = id + 1; 1348c2ecf20Sopenharmony_ci } 1358c2ecf20Sopenharmony_ci shrinker->id = id; 1368c2ecf20Sopenharmony_ci ret = 0; 1378c2ecf20Sopenharmony_ciunlock: 1388c2ecf20Sopenharmony_ci up_write(&shrinker_rwsem); 1398c2ecf20Sopenharmony_ci return ret; 1408c2ecf20Sopenharmony_ci} 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_cistatic void unregister_memcg_shrinker(struct shrinker *shrinker) 1438c2ecf20Sopenharmony_ci{ 1448c2ecf20Sopenharmony_ci int id = shrinker->id; 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci BUG_ON(id < 0); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci lockdep_assert_held(&shrinker_rwsem); 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci idr_remove(&shrinker_idr, id); 1518c2ecf20Sopenharmony_ci} 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_cibool cgroup_reclaim(struct scan_control *sc) 1548c2ecf20Sopenharmony_ci{ 1558c2ecf20Sopenharmony_ci return sc->target_mem_cgroup; 1568c2ecf20Sopenharmony_ci} 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci/** 1598c2ecf20Sopenharmony_ci * writeback_throttling_sane - is the usual dirty throttling mechanism available? 1608c2ecf20Sopenharmony_ci * @sc: scan_control in question 1618c2ecf20Sopenharmony_ci * 1628c2ecf20Sopenharmony_ci * The normal page dirty throttling mechanism in balance_dirty_pages() is 1638c2ecf20Sopenharmony_ci * completely broken with the legacy memcg and direct stalling in 1648c2ecf20Sopenharmony_ci * shrink_page_list() is used for throttling instead, which lacks all the 1658c2ecf20Sopenharmony_ci * niceties such as fairness, adaptive pausing, bandwidth proportional 1668c2ecf20Sopenharmony_ci * allocation and configurability. 1678c2ecf20Sopenharmony_ci * 1688c2ecf20Sopenharmony_ci * This function tests whether the vmscan currently in progress can assume 1698c2ecf20Sopenharmony_ci * that the normal dirty throttling mechanism is operational. 1708c2ecf20Sopenharmony_ci */ 1718c2ecf20Sopenharmony_cibool writeback_throttling_sane(struct scan_control *sc) 1728c2ecf20Sopenharmony_ci{ 1738c2ecf20Sopenharmony_ci if (!cgroup_reclaim(sc)) 1748c2ecf20Sopenharmony_ci return true; 1758c2ecf20Sopenharmony_ci#ifdef CONFIG_CGROUP_WRITEBACK 1768c2ecf20Sopenharmony_ci if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) 1778c2ecf20Sopenharmony_ci return true; 1788c2ecf20Sopenharmony_ci#endif 1798c2ecf20Sopenharmony_ci return false; 1808c2ecf20Sopenharmony_ci} 1818c2ecf20Sopenharmony_ci#else 1828c2ecf20Sopenharmony_cistatic int prealloc_memcg_shrinker(struct shrinker *shrinker) 1838c2ecf20Sopenharmony_ci{ 1848c2ecf20Sopenharmony_ci return 0; 1858c2ecf20Sopenharmony_ci} 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_cistatic void unregister_memcg_shrinker(struct shrinker *shrinker) 1888c2ecf20Sopenharmony_ci{ 1898c2ecf20Sopenharmony_ci} 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_cibool cgroup_reclaim(struct scan_control *sc) 1928c2ecf20Sopenharmony_ci{ 1938c2ecf20Sopenharmony_ci return false; 1948c2ecf20Sopenharmony_ci} 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_cibool writeback_throttling_sane(struct scan_control *sc) 1978c2ecf20Sopenharmony_ci{ 1988c2ecf20Sopenharmony_ci return true; 1998c2ecf20Sopenharmony_ci} 2008c2ecf20Sopenharmony_ci#endif 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci/* 2038c2ecf20Sopenharmony_ci * This misses isolated pages which are not accounted for to save counters. 2048c2ecf20Sopenharmony_ci * As the data only determines if reclaim or compaction continues, it is 2058c2ecf20Sopenharmony_ci * not expected that isolated pages will be a dominating factor. 2068c2ecf20Sopenharmony_ci */ 2078c2ecf20Sopenharmony_ciunsigned long zone_reclaimable_pages(struct zone *zone) 2088c2ecf20Sopenharmony_ci{ 2098c2ecf20Sopenharmony_ci unsigned long nr; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) + 2128c2ecf20Sopenharmony_ci zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE); 2138c2ecf20Sopenharmony_ci if (get_nr_swap_pages() > 0) 2148c2ecf20Sopenharmony_ci nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) + 2158c2ecf20Sopenharmony_ci zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON); 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci return nr; 2188c2ecf20Sopenharmony_ci} 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci/** 2218c2ecf20Sopenharmony_ci * lruvec_lru_size - Returns the number of pages on the given LRU list. 2228c2ecf20Sopenharmony_ci * @lruvec: lru vector 2238c2ecf20Sopenharmony_ci * @lru: lru to use 2248c2ecf20Sopenharmony_ci * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list) 2258c2ecf20Sopenharmony_ci */ 2268c2ecf20Sopenharmony_ciunsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx) 2278c2ecf20Sopenharmony_ci{ 2288c2ecf20Sopenharmony_ci unsigned long size = 0; 2298c2ecf20Sopenharmony_ci int zid; 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci#ifdef CONFIG_HYPERHOLD_FILE_LRU 2328c2ecf20Sopenharmony_ci if (!mem_cgroup_disabled() && is_node_lruvec(lruvec)) { 2338c2ecf20Sopenharmony_ci for (zid = 0; zid <= zone_idx && zid < MAX_NR_ZONES; zid++) { 2348c2ecf20Sopenharmony_ci struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci if (!managed_zone(zone)) 2378c2ecf20Sopenharmony_ci continue; 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci size += zone_page_state(zone, NR_ZONE_LRU_BASE + lru); 2408c2ecf20Sopenharmony_ci } 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci return size; 2438c2ecf20Sopenharmony_ci } 2448c2ecf20Sopenharmony_ci#endif 2458c2ecf20Sopenharmony_ci for (zid = 0; zid <= zone_idx && zid < MAX_NR_ZONES; zid++) { 2468c2ecf20Sopenharmony_ci struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci if (!managed_zone(zone)) 2498c2ecf20Sopenharmony_ci continue; 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci if (!mem_cgroup_disabled()) 2528c2ecf20Sopenharmony_ci size += mem_cgroup_get_zone_lru_size(lruvec, lru, zid); 2538c2ecf20Sopenharmony_ci else 2548c2ecf20Sopenharmony_ci size += zone_page_state(zone, NR_ZONE_LRU_BASE + lru); 2558c2ecf20Sopenharmony_ci } 2568c2ecf20Sopenharmony_ci return size; 2578c2ecf20Sopenharmony_ci} 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci/* 2608c2ecf20Sopenharmony_ci * Add a shrinker callback to be called from the vm. 2618c2ecf20Sopenharmony_ci */ 2628c2ecf20Sopenharmony_ciint prealloc_shrinker(struct shrinker *shrinker) 2638c2ecf20Sopenharmony_ci{ 2648c2ecf20Sopenharmony_ci unsigned int size = sizeof(*shrinker->nr_deferred); 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci if (shrinker->flags & SHRINKER_NUMA_AWARE) 2678c2ecf20Sopenharmony_ci size *= nr_node_ids; 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); 2708c2ecf20Sopenharmony_ci if (!shrinker->nr_deferred) 2718c2ecf20Sopenharmony_ci return -ENOMEM; 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci if (shrinker->flags & SHRINKER_MEMCG_AWARE) { 2748c2ecf20Sopenharmony_ci if (prealloc_memcg_shrinker(shrinker)) 2758c2ecf20Sopenharmony_ci goto free_deferred; 2768c2ecf20Sopenharmony_ci } 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci return 0; 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_cifree_deferred: 2818c2ecf20Sopenharmony_ci kfree(shrinker->nr_deferred); 2828c2ecf20Sopenharmony_ci shrinker->nr_deferred = NULL; 2838c2ecf20Sopenharmony_ci return -ENOMEM; 2848c2ecf20Sopenharmony_ci} 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_civoid free_prealloced_shrinker(struct shrinker *shrinker) 2878c2ecf20Sopenharmony_ci{ 2888c2ecf20Sopenharmony_ci if (!shrinker->nr_deferred) 2898c2ecf20Sopenharmony_ci return; 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci if (shrinker->flags & SHRINKER_MEMCG_AWARE) { 2928c2ecf20Sopenharmony_ci down_write(&shrinker_rwsem); 2938c2ecf20Sopenharmony_ci unregister_memcg_shrinker(shrinker); 2948c2ecf20Sopenharmony_ci up_write(&shrinker_rwsem); 2958c2ecf20Sopenharmony_ci } 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci kfree(shrinker->nr_deferred); 2988c2ecf20Sopenharmony_ci shrinker->nr_deferred = NULL; 2998c2ecf20Sopenharmony_ci} 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_civoid register_shrinker_prepared(struct shrinker *shrinker) 3028c2ecf20Sopenharmony_ci{ 3038c2ecf20Sopenharmony_ci down_write(&shrinker_rwsem); 3048c2ecf20Sopenharmony_ci list_add_tail(&shrinker->list, &shrinker_list); 3058c2ecf20Sopenharmony_ci shrinker->flags |= SHRINKER_REGISTERED; 3068c2ecf20Sopenharmony_ci up_write(&shrinker_rwsem); 3078c2ecf20Sopenharmony_ci} 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ciint register_shrinker(struct shrinker *shrinker) 3108c2ecf20Sopenharmony_ci{ 3118c2ecf20Sopenharmony_ci int err = prealloc_shrinker(shrinker); 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci if (err) 3148c2ecf20Sopenharmony_ci return err; 3158c2ecf20Sopenharmony_ci register_shrinker_prepared(shrinker); 3168c2ecf20Sopenharmony_ci return 0; 3178c2ecf20Sopenharmony_ci} 3188c2ecf20Sopenharmony_ciEXPORT_SYMBOL(register_shrinker); 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci/* 3218c2ecf20Sopenharmony_ci * Remove one 3228c2ecf20Sopenharmony_ci */ 3238c2ecf20Sopenharmony_civoid unregister_shrinker(struct shrinker *shrinker) 3248c2ecf20Sopenharmony_ci{ 3258c2ecf20Sopenharmony_ci if (!(shrinker->flags & SHRINKER_REGISTERED)) 3268c2ecf20Sopenharmony_ci return; 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci down_write(&shrinker_rwsem); 3298c2ecf20Sopenharmony_ci list_del(&shrinker->list); 3308c2ecf20Sopenharmony_ci shrinker->flags &= ~SHRINKER_REGISTERED; 3318c2ecf20Sopenharmony_ci if (shrinker->flags & SHRINKER_MEMCG_AWARE) 3328c2ecf20Sopenharmony_ci unregister_memcg_shrinker(shrinker); 3338c2ecf20Sopenharmony_ci up_write(&shrinker_rwsem); 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ci kfree(shrinker->nr_deferred); 3368c2ecf20Sopenharmony_ci shrinker->nr_deferred = NULL; 3378c2ecf20Sopenharmony_ci} 3388c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unregister_shrinker); 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_ci#define SHRINK_BATCH 128 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_cistatic unsigned long do_shrink_slab(struct shrink_control *shrinkctl, 3438c2ecf20Sopenharmony_ci struct shrinker *shrinker, int priority) 3448c2ecf20Sopenharmony_ci{ 3458c2ecf20Sopenharmony_ci unsigned long freed = 0; 3468c2ecf20Sopenharmony_ci unsigned long long delta; 3478c2ecf20Sopenharmony_ci long total_scan; 3488c2ecf20Sopenharmony_ci long freeable; 3498c2ecf20Sopenharmony_ci long nr; 3508c2ecf20Sopenharmony_ci long new_nr; 3518c2ecf20Sopenharmony_ci int nid = shrinkctl->nid; 3528c2ecf20Sopenharmony_ci long batch_size = shrinker->batch ? shrinker->batch 3538c2ecf20Sopenharmony_ci : SHRINK_BATCH; 3548c2ecf20Sopenharmony_ci long scanned = 0, next_deferred; 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_ci if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) 3578c2ecf20Sopenharmony_ci nid = 0; 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci freeable = shrinker->count_objects(shrinker, shrinkctl); 3608c2ecf20Sopenharmony_ci if (freeable == 0 || freeable == SHRINK_EMPTY) 3618c2ecf20Sopenharmony_ci return freeable; 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci /* 3648c2ecf20Sopenharmony_ci * copy the current shrinker scan count into a local variable 3658c2ecf20Sopenharmony_ci * and zero it so that other concurrent shrinker invocations 3668c2ecf20Sopenharmony_ci * don't also do this scanning work. 3678c2ecf20Sopenharmony_ci */ 3688c2ecf20Sopenharmony_ci nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0); 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci total_scan = nr; 3718c2ecf20Sopenharmony_ci if (shrinker->seeks) { 3728c2ecf20Sopenharmony_ci delta = freeable >> priority; 3738c2ecf20Sopenharmony_ci delta *= 4; 3748c2ecf20Sopenharmony_ci do_div(delta, shrinker->seeks); 3758c2ecf20Sopenharmony_ci } else { 3768c2ecf20Sopenharmony_ci /* 3778c2ecf20Sopenharmony_ci * These objects don't require any IO to create. Trim 3788c2ecf20Sopenharmony_ci * them aggressively under memory pressure to keep 3798c2ecf20Sopenharmony_ci * them from causing refetches in the IO caches. 3808c2ecf20Sopenharmony_ci */ 3818c2ecf20Sopenharmony_ci delta = freeable / 2; 3828c2ecf20Sopenharmony_ci } 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci total_scan += delta; 3858c2ecf20Sopenharmony_ci if (total_scan < 0) { 3868c2ecf20Sopenharmony_ci pr_err("shrink_slab: %pS negative objects to delete nr=%ld\n", 3878c2ecf20Sopenharmony_ci shrinker->scan_objects, total_scan); 3888c2ecf20Sopenharmony_ci total_scan = freeable; 3898c2ecf20Sopenharmony_ci next_deferred = nr; 3908c2ecf20Sopenharmony_ci } else 3918c2ecf20Sopenharmony_ci next_deferred = total_scan; 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci /* 3948c2ecf20Sopenharmony_ci * We need to avoid excessive windup on filesystem shrinkers 3958c2ecf20Sopenharmony_ci * due to large numbers of GFP_NOFS allocations causing the 3968c2ecf20Sopenharmony_ci * shrinkers to return -1 all the time. This results in a large 3978c2ecf20Sopenharmony_ci * nr being built up so when a shrink that can do some work 3988c2ecf20Sopenharmony_ci * comes along it empties the entire cache due to nr >>> 3998c2ecf20Sopenharmony_ci * freeable. This is bad for sustaining a working set in 4008c2ecf20Sopenharmony_ci * memory. 4018c2ecf20Sopenharmony_ci * 4028c2ecf20Sopenharmony_ci * Hence only allow the shrinker to scan the entire cache when 4038c2ecf20Sopenharmony_ci * a large delta change is calculated directly. 4048c2ecf20Sopenharmony_ci */ 4058c2ecf20Sopenharmony_ci if (delta < freeable / 4) 4068c2ecf20Sopenharmony_ci total_scan = min(total_scan, freeable / 2); 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci /* 4098c2ecf20Sopenharmony_ci * Avoid risking looping forever due to too large nr value: 4108c2ecf20Sopenharmony_ci * never try to free more than twice the estimate number of 4118c2ecf20Sopenharmony_ci * freeable entries. 4128c2ecf20Sopenharmony_ci */ 4138c2ecf20Sopenharmony_ci if (total_scan > freeable * 2) 4148c2ecf20Sopenharmony_ci total_scan = freeable * 2; 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, 4178c2ecf20Sopenharmony_ci freeable, delta, total_scan, priority); 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ci /* 4208c2ecf20Sopenharmony_ci * Normally, we should not scan less than batch_size objects in one 4218c2ecf20Sopenharmony_ci * pass to avoid too frequent shrinker calls, but if the slab has less 4228c2ecf20Sopenharmony_ci * than batch_size objects in total and we are really tight on memory, 4238c2ecf20Sopenharmony_ci * we will try to reclaim all available objects, otherwise we can end 4248c2ecf20Sopenharmony_ci * up failing allocations although there are plenty of reclaimable 4258c2ecf20Sopenharmony_ci * objects spread over several slabs with usage less than the 4268c2ecf20Sopenharmony_ci * batch_size. 4278c2ecf20Sopenharmony_ci * 4288c2ecf20Sopenharmony_ci * We detect the "tight on memory" situations by looking at the total 4298c2ecf20Sopenharmony_ci * number of objects we want to scan (total_scan). If it is greater 4308c2ecf20Sopenharmony_ci * than the total number of objects on slab (freeable), we must be 4318c2ecf20Sopenharmony_ci * scanning at high prio and therefore should try to reclaim as much as 4328c2ecf20Sopenharmony_ci * possible. 4338c2ecf20Sopenharmony_ci */ 4348c2ecf20Sopenharmony_ci while (total_scan >= batch_size || 4358c2ecf20Sopenharmony_ci total_scan >= freeable) { 4368c2ecf20Sopenharmony_ci unsigned long ret; 4378c2ecf20Sopenharmony_ci unsigned long nr_to_scan = min(batch_size, total_scan); 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci shrinkctl->nr_to_scan = nr_to_scan; 4408c2ecf20Sopenharmony_ci shrinkctl->nr_scanned = nr_to_scan; 4418c2ecf20Sopenharmony_ci ret = shrinker->scan_objects(shrinker, shrinkctl); 4428c2ecf20Sopenharmony_ci if (ret == SHRINK_STOP) 4438c2ecf20Sopenharmony_ci break; 4448c2ecf20Sopenharmony_ci freed += ret; 4458c2ecf20Sopenharmony_ci 4468c2ecf20Sopenharmony_ci count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned); 4478c2ecf20Sopenharmony_ci total_scan -= shrinkctl->nr_scanned; 4488c2ecf20Sopenharmony_ci scanned += shrinkctl->nr_scanned; 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_ci cond_resched(); 4518c2ecf20Sopenharmony_ci } 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ci if (next_deferred >= scanned) 4548c2ecf20Sopenharmony_ci next_deferred -= scanned; 4558c2ecf20Sopenharmony_ci else 4568c2ecf20Sopenharmony_ci next_deferred = 0; 4578c2ecf20Sopenharmony_ci /* 4588c2ecf20Sopenharmony_ci * move the unused scan count back into the shrinker in a 4598c2ecf20Sopenharmony_ci * manner that handles concurrent updates. If we exhausted the 4608c2ecf20Sopenharmony_ci * scan, there is no need to do an update. 4618c2ecf20Sopenharmony_ci */ 4628c2ecf20Sopenharmony_ci if (next_deferred > 0) 4638c2ecf20Sopenharmony_ci new_nr = atomic_long_add_return(next_deferred, 4648c2ecf20Sopenharmony_ci &shrinker->nr_deferred[nid]); 4658c2ecf20Sopenharmony_ci else 4668c2ecf20Sopenharmony_ci new_nr = atomic_long_read(&shrinker->nr_deferred[nid]); 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci trace_mm_shrink_slab_end(shrinker, nid, freed, nr, new_nr, total_scan); 4698c2ecf20Sopenharmony_ci return freed; 4708c2ecf20Sopenharmony_ci} 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ci#ifdef CONFIG_MEMCG 4738c2ecf20Sopenharmony_cistatic unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 4748c2ecf20Sopenharmony_ci struct mem_cgroup *memcg, int priority) 4758c2ecf20Sopenharmony_ci{ 4768c2ecf20Sopenharmony_ci struct memcg_shrinker_map *map; 4778c2ecf20Sopenharmony_ci unsigned long ret, freed = 0; 4788c2ecf20Sopenharmony_ci int i; 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci if (!mem_cgroup_online(memcg)) 4818c2ecf20Sopenharmony_ci return 0; 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci if (!down_read_trylock(&shrinker_rwsem)) 4848c2ecf20Sopenharmony_ci return 0; 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci map = rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_map, 4878c2ecf20Sopenharmony_ci true); 4888c2ecf20Sopenharmony_ci if (unlikely(!map)) 4898c2ecf20Sopenharmony_ci goto unlock; 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ci for_each_set_bit(i, map->map, shrinker_nr_max) { 4928c2ecf20Sopenharmony_ci struct shrink_control sc = { 4938c2ecf20Sopenharmony_ci .gfp_mask = gfp_mask, 4948c2ecf20Sopenharmony_ci .nid = nid, 4958c2ecf20Sopenharmony_ci .memcg = memcg, 4968c2ecf20Sopenharmony_ci }; 4978c2ecf20Sopenharmony_ci struct shrinker *shrinker; 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_ci shrinker = idr_find(&shrinker_idr, i); 5008c2ecf20Sopenharmony_ci if (unlikely(!shrinker || !(shrinker->flags & SHRINKER_REGISTERED))) { 5018c2ecf20Sopenharmony_ci if (!shrinker) 5028c2ecf20Sopenharmony_ci clear_bit(i, map->map); 5038c2ecf20Sopenharmony_ci continue; 5048c2ecf20Sopenharmony_ci } 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci /* Call non-slab shrinkers even though kmem is disabled */ 5078c2ecf20Sopenharmony_ci if (!memcg_kmem_enabled() && 5088c2ecf20Sopenharmony_ci !(shrinker->flags & SHRINKER_NONSLAB)) 5098c2ecf20Sopenharmony_ci continue; 5108c2ecf20Sopenharmony_ci 5118c2ecf20Sopenharmony_ci ret = do_shrink_slab(&sc, shrinker, priority); 5128c2ecf20Sopenharmony_ci if (ret == SHRINK_EMPTY) { 5138c2ecf20Sopenharmony_ci clear_bit(i, map->map); 5148c2ecf20Sopenharmony_ci /* 5158c2ecf20Sopenharmony_ci * After the shrinker reported that it had no objects to 5168c2ecf20Sopenharmony_ci * free, but before we cleared the corresponding bit in 5178c2ecf20Sopenharmony_ci * the memcg shrinker map, a new object might have been 5188c2ecf20Sopenharmony_ci * added. To make sure, we have the bit set in this 5198c2ecf20Sopenharmony_ci * case, we invoke the shrinker one more time and reset 5208c2ecf20Sopenharmony_ci * the bit if it reports that it is not empty anymore. 5218c2ecf20Sopenharmony_ci * The memory barrier here pairs with the barrier in 5228c2ecf20Sopenharmony_ci * memcg_set_shrinker_bit(): 5238c2ecf20Sopenharmony_ci * 5248c2ecf20Sopenharmony_ci * list_lru_add() shrink_slab_memcg() 5258c2ecf20Sopenharmony_ci * list_add_tail() clear_bit() 5268c2ecf20Sopenharmony_ci * <MB> <MB> 5278c2ecf20Sopenharmony_ci * set_bit() do_shrink_slab() 5288c2ecf20Sopenharmony_ci */ 5298c2ecf20Sopenharmony_ci smp_mb__after_atomic(); 5308c2ecf20Sopenharmony_ci ret = do_shrink_slab(&sc, shrinker, priority); 5318c2ecf20Sopenharmony_ci if (ret == SHRINK_EMPTY) 5328c2ecf20Sopenharmony_ci ret = 0; 5338c2ecf20Sopenharmony_ci else 5348c2ecf20Sopenharmony_ci memcg_set_shrinker_bit(memcg, nid, i); 5358c2ecf20Sopenharmony_ci } 5368c2ecf20Sopenharmony_ci freed += ret; 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ci if (rwsem_is_contended(&shrinker_rwsem)) { 5398c2ecf20Sopenharmony_ci freed = freed ? : 1; 5408c2ecf20Sopenharmony_ci break; 5418c2ecf20Sopenharmony_ci } 5428c2ecf20Sopenharmony_ci } 5438c2ecf20Sopenharmony_ciunlock: 5448c2ecf20Sopenharmony_ci up_read(&shrinker_rwsem); 5458c2ecf20Sopenharmony_ci return freed; 5468c2ecf20Sopenharmony_ci} 5478c2ecf20Sopenharmony_ci#else /* CONFIG_MEMCG */ 5488c2ecf20Sopenharmony_cistatic unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 5498c2ecf20Sopenharmony_ci struct mem_cgroup *memcg, int priority) 5508c2ecf20Sopenharmony_ci{ 5518c2ecf20Sopenharmony_ci return 0; 5528c2ecf20Sopenharmony_ci} 5538c2ecf20Sopenharmony_ci#endif /* CONFIG_MEMCG */ 5548c2ecf20Sopenharmony_ci 5558c2ecf20Sopenharmony_ci/** 5568c2ecf20Sopenharmony_ci * shrink_slab - shrink slab caches 5578c2ecf20Sopenharmony_ci * @gfp_mask: allocation context 5588c2ecf20Sopenharmony_ci * @nid: node whose slab caches to target 5598c2ecf20Sopenharmony_ci * @memcg: memory cgroup whose slab caches to target 5608c2ecf20Sopenharmony_ci * @priority: the reclaim priority 5618c2ecf20Sopenharmony_ci * 5628c2ecf20Sopenharmony_ci * Call the shrink functions to age shrinkable caches. 5638c2ecf20Sopenharmony_ci * 5648c2ecf20Sopenharmony_ci * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, 5658c2ecf20Sopenharmony_ci * unaware shrinkers will receive a node id of 0 instead. 5668c2ecf20Sopenharmony_ci * 5678c2ecf20Sopenharmony_ci * @memcg specifies the memory cgroup to target. Unaware shrinkers 5688c2ecf20Sopenharmony_ci * are called only if it is the root cgroup. 5698c2ecf20Sopenharmony_ci * 5708c2ecf20Sopenharmony_ci * @priority is sc->priority, we take the number of objects and >> by priority 5718c2ecf20Sopenharmony_ci * in order to get the scan target. 5728c2ecf20Sopenharmony_ci * 5738c2ecf20Sopenharmony_ci * Returns the number of reclaimed slab objects. 5748c2ecf20Sopenharmony_ci */ 5758c2ecf20Sopenharmony_ciunsigned long shrink_slab(gfp_t gfp_mask, int nid, 5768c2ecf20Sopenharmony_ci struct mem_cgroup *memcg, 5778c2ecf20Sopenharmony_ci int priority) 5788c2ecf20Sopenharmony_ci{ 5798c2ecf20Sopenharmony_ci unsigned long ret, freed = 0; 5808c2ecf20Sopenharmony_ci struct shrinker *shrinker; 5818c2ecf20Sopenharmony_ci 5828c2ecf20Sopenharmony_ci /* 5838c2ecf20Sopenharmony_ci * The root memcg might be allocated even though memcg is disabled 5848c2ecf20Sopenharmony_ci * via "cgroup_disable=memory" boot parameter. This could make 5858c2ecf20Sopenharmony_ci * mem_cgroup_is_root() return false, then just run memcg slab 5868c2ecf20Sopenharmony_ci * shrink, but skip global shrink. This may result in premature 5878c2ecf20Sopenharmony_ci * oom. 5888c2ecf20Sopenharmony_ci */ 5898c2ecf20Sopenharmony_ci if (!mem_cgroup_disabled() && !mem_cgroup_is_root(memcg)) 5908c2ecf20Sopenharmony_ci return shrink_slab_memcg(gfp_mask, nid, memcg, priority); 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_ci if (!down_read_trylock(&shrinker_rwsem)) 5938c2ecf20Sopenharmony_ci goto out; 5948c2ecf20Sopenharmony_ci 5958c2ecf20Sopenharmony_ci list_for_each_entry(shrinker, &shrinker_list, list) { 5968c2ecf20Sopenharmony_ci struct shrink_control sc = { 5978c2ecf20Sopenharmony_ci .gfp_mask = gfp_mask, 5988c2ecf20Sopenharmony_ci .nid = nid, 5998c2ecf20Sopenharmony_ci .memcg = memcg, 6008c2ecf20Sopenharmony_ci }; 6018c2ecf20Sopenharmony_ci 6028c2ecf20Sopenharmony_ci#ifdef CONFIG_RECLAIM_ACCT 6038c2ecf20Sopenharmony_ci reclaimacct_substage_start(RA_SHRINKSLAB); 6048c2ecf20Sopenharmony_ci#endif 6058c2ecf20Sopenharmony_ci ret = do_shrink_slab(&sc, shrinker, priority); 6068c2ecf20Sopenharmony_ci if (ret == SHRINK_EMPTY) 6078c2ecf20Sopenharmony_ci ret = 0; 6088c2ecf20Sopenharmony_ci freed += ret; 6098c2ecf20Sopenharmony_ci#ifdef CONFIG_RECLAIM_ACCT 6108c2ecf20Sopenharmony_ci reclaimacct_substage_end(RA_SHRINKSLAB, ret, shrinker); 6118c2ecf20Sopenharmony_ci#endif 6128c2ecf20Sopenharmony_ci /* 6138c2ecf20Sopenharmony_ci * Bail out if someone want to register a new shrinker to 6148c2ecf20Sopenharmony_ci * prevent the registration from being stalled for long periods 6158c2ecf20Sopenharmony_ci * by parallel ongoing shrinking. 6168c2ecf20Sopenharmony_ci */ 6178c2ecf20Sopenharmony_ci if (rwsem_is_contended(&shrinker_rwsem)) { 6188c2ecf20Sopenharmony_ci freed = freed ? : 1; 6198c2ecf20Sopenharmony_ci break; 6208c2ecf20Sopenharmony_ci } 6218c2ecf20Sopenharmony_ci } 6228c2ecf20Sopenharmony_ci 6238c2ecf20Sopenharmony_ci up_read(&shrinker_rwsem); 6248c2ecf20Sopenharmony_ciout: 6258c2ecf20Sopenharmony_ci cond_resched(); 6268c2ecf20Sopenharmony_ci return freed; 6278c2ecf20Sopenharmony_ci} 6288c2ecf20Sopenharmony_ci 6298c2ecf20Sopenharmony_civoid drop_slab_node(int nid) 6308c2ecf20Sopenharmony_ci{ 6318c2ecf20Sopenharmony_ci unsigned long freed; 6328c2ecf20Sopenharmony_ci int shift = 0; 6338c2ecf20Sopenharmony_ci 6348c2ecf20Sopenharmony_ci do { 6358c2ecf20Sopenharmony_ci struct mem_cgroup *memcg = NULL; 6368c2ecf20Sopenharmony_ci 6378c2ecf20Sopenharmony_ci if (fatal_signal_pending(current)) 6388c2ecf20Sopenharmony_ci return; 6398c2ecf20Sopenharmony_ci 6408c2ecf20Sopenharmony_ci freed = 0; 6418c2ecf20Sopenharmony_ci memcg = mem_cgroup_iter(NULL, NULL, NULL); 6428c2ecf20Sopenharmony_ci do { 6438c2ecf20Sopenharmony_ci freed += shrink_slab(GFP_KERNEL, nid, memcg, 0); 6448c2ecf20Sopenharmony_ci } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 6458c2ecf20Sopenharmony_ci } while ((freed >> shift++) > 1); 6468c2ecf20Sopenharmony_ci} 6478c2ecf20Sopenharmony_ci 6488c2ecf20Sopenharmony_civoid drop_slab(void) 6498c2ecf20Sopenharmony_ci{ 6508c2ecf20Sopenharmony_ci int nid; 6518c2ecf20Sopenharmony_ci 6528c2ecf20Sopenharmony_ci for_each_online_node(nid) 6538c2ecf20Sopenharmony_ci drop_slab_node(nid); 6548c2ecf20Sopenharmony_ci} 6558c2ecf20Sopenharmony_ci 6568c2ecf20Sopenharmony_cistatic inline int is_page_cache_freeable(struct page *page) 6578c2ecf20Sopenharmony_ci{ 6588c2ecf20Sopenharmony_ci /* 6598c2ecf20Sopenharmony_ci * A freeable page cache page is referenced only by the caller 6608c2ecf20Sopenharmony_ci * that isolated the page, the page cache and optional buffer 6618c2ecf20Sopenharmony_ci * heads at page->private. 6628c2ecf20Sopenharmony_ci */ 6638c2ecf20Sopenharmony_ci int page_cache_pins = thp_nr_pages(page); 6648c2ecf20Sopenharmony_ci return page_count(page) - page_has_private(page) == 1 + page_cache_pins; 6658c2ecf20Sopenharmony_ci} 6668c2ecf20Sopenharmony_ci 6678c2ecf20Sopenharmony_cistatic int may_write_to_inode(struct inode *inode) 6688c2ecf20Sopenharmony_ci{ 6698c2ecf20Sopenharmony_ci if (current->flags & PF_SWAPWRITE) 6708c2ecf20Sopenharmony_ci return 1; 6718c2ecf20Sopenharmony_ci if (!inode_write_congested(inode)) 6728c2ecf20Sopenharmony_ci return 1; 6738c2ecf20Sopenharmony_ci if (inode_to_bdi(inode) == current->backing_dev_info) 6748c2ecf20Sopenharmony_ci return 1; 6758c2ecf20Sopenharmony_ci return 0; 6768c2ecf20Sopenharmony_ci} 6778c2ecf20Sopenharmony_ci 6788c2ecf20Sopenharmony_ci/* 6798c2ecf20Sopenharmony_ci * We detected a synchronous write error writing a page out. Probably 6808c2ecf20Sopenharmony_ci * -ENOSPC. We need to propagate that into the address_space for a subsequent 6818c2ecf20Sopenharmony_ci * fsync(), msync() or close(). 6828c2ecf20Sopenharmony_ci * 6838c2ecf20Sopenharmony_ci * The tricky part is that after writepage we cannot touch the mapping: nothing 6848c2ecf20Sopenharmony_ci * prevents it from being freed up. But we have a ref on the page and once 6858c2ecf20Sopenharmony_ci * that page is locked, the mapping is pinned. 6868c2ecf20Sopenharmony_ci * 6878c2ecf20Sopenharmony_ci * We're allowed to run sleeping lock_page() here because we know the caller has 6888c2ecf20Sopenharmony_ci * __GFP_FS. 6898c2ecf20Sopenharmony_ci */ 6908c2ecf20Sopenharmony_cistatic void handle_write_error(struct address_space *mapping, 6918c2ecf20Sopenharmony_ci struct page *page, int error) 6928c2ecf20Sopenharmony_ci{ 6938c2ecf20Sopenharmony_ci lock_page(page); 6948c2ecf20Sopenharmony_ci if (page_mapping(page) == mapping) 6958c2ecf20Sopenharmony_ci mapping_set_error(mapping, error); 6968c2ecf20Sopenharmony_ci unlock_page(page); 6978c2ecf20Sopenharmony_ci} 6988c2ecf20Sopenharmony_ci 6998c2ecf20Sopenharmony_ci/* possible outcome of pageout() */ 7008c2ecf20Sopenharmony_citypedef enum { 7018c2ecf20Sopenharmony_ci /* failed to write page out, page is locked */ 7028c2ecf20Sopenharmony_ci PAGE_KEEP, 7038c2ecf20Sopenharmony_ci /* move page to the active list, page is locked */ 7048c2ecf20Sopenharmony_ci PAGE_ACTIVATE, 7058c2ecf20Sopenharmony_ci /* page has been sent to the disk successfully, page is unlocked */ 7068c2ecf20Sopenharmony_ci PAGE_SUCCESS, 7078c2ecf20Sopenharmony_ci /* page is clean and locked */ 7088c2ecf20Sopenharmony_ci PAGE_CLEAN, 7098c2ecf20Sopenharmony_ci} pageout_t; 7108c2ecf20Sopenharmony_ci 7118c2ecf20Sopenharmony_ci/* 7128c2ecf20Sopenharmony_ci * pageout is called by shrink_page_list() for each dirty page. 7138c2ecf20Sopenharmony_ci * Calls ->writepage(). 7148c2ecf20Sopenharmony_ci */ 7158c2ecf20Sopenharmony_cistatic pageout_t pageout(struct page *page, struct address_space *mapping) 7168c2ecf20Sopenharmony_ci{ 7178c2ecf20Sopenharmony_ci /* 7188c2ecf20Sopenharmony_ci * If the page is dirty, only perform writeback if that write 7198c2ecf20Sopenharmony_ci * will be non-blocking. To prevent this allocation from being 7208c2ecf20Sopenharmony_ci * stalled by pagecache activity. But note that there may be 7218c2ecf20Sopenharmony_ci * stalls if we need to run get_block(). We could test 7228c2ecf20Sopenharmony_ci * PagePrivate for that. 7238c2ecf20Sopenharmony_ci * 7248c2ecf20Sopenharmony_ci * If this process is currently in __generic_file_write_iter() against 7258c2ecf20Sopenharmony_ci * this page's queue, we can perform writeback even if that 7268c2ecf20Sopenharmony_ci * will block. 7278c2ecf20Sopenharmony_ci * 7288c2ecf20Sopenharmony_ci * If the page is swapcache, write it back even if that would 7298c2ecf20Sopenharmony_ci * block, for some throttling. This happens by accident, because 7308c2ecf20Sopenharmony_ci * swap_backing_dev_info is bust: it doesn't reflect the 7318c2ecf20Sopenharmony_ci * congestion state of the swapdevs. Easy to fix, if needed. 7328c2ecf20Sopenharmony_ci */ 7338c2ecf20Sopenharmony_ci if (!is_page_cache_freeable(page)) 7348c2ecf20Sopenharmony_ci return PAGE_KEEP; 7358c2ecf20Sopenharmony_ci if (!mapping) { 7368c2ecf20Sopenharmony_ci /* 7378c2ecf20Sopenharmony_ci * Some data journaling orphaned pages can have 7388c2ecf20Sopenharmony_ci * page->mapping == NULL while being dirty with clean buffers. 7398c2ecf20Sopenharmony_ci */ 7408c2ecf20Sopenharmony_ci if (page_has_private(page)) { 7418c2ecf20Sopenharmony_ci if (try_to_free_buffers(page)) { 7428c2ecf20Sopenharmony_ci ClearPageDirty(page); 7438c2ecf20Sopenharmony_ci pr_info("%s: orphaned page\n", __func__); 7448c2ecf20Sopenharmony_ci return PAGE_CLEAN; 7458c2ecf20Sopenharmony_ci } 7468c2ecf20Sopenharmony_ci } 7478c2ecf20Sopenharmony_ci return PAGE_KEEP; 7488c2ecf20Sopenharmony_ci } 7498c2ecf20Sopenharmony_ci if (mapping->a_ops->writepage == NULL) 7508c2ecf20Sopenharmony_ci return PAGE_ACTIVATE; 7518c2ecf20Sopenharmony_ci if (!may_write_to_inode(mapping->host)) 7528c2ecf20Sopenharmony_ci return PAGE_KEEP; 7538c2ecf20Sopenharmony_ci 7548c2ecf20Sopenharmony_ci if (clear_page_dirty_for_io(page)) { 7558c2ecf20Sopenharmony_ci int res; 7568c2ecf20Sopenharmony_ci struct writeback_control wbc = { 7578c2ecf20Sopenharmony_ci .sync_mode = WB_SYNC_NONE, 7588c2ecf20Sopenharmony_ci .nr_to_write = SWAP_CLUSTER_MAX, 7598c2ecf20Sopenharmony_ci .range_start = 0, 7608c2ecf20Sopenharmony_ci .range_end = LLONG_MAX, 7618c2ecf20Sopenharmony_ci .for_reclaim = 1, 7628c2ecf20Sopenharmony_ci }; 7638c2ecf20Sopenharmony_ci 7648c2ecf20Sopenharmony_ci SetPageReclaim(page); 7658c2ecf20Sopenharmony_ci res = mapping->a_ops->writepage(page, &wbc); 7668c2ecf20Sopenharmony_ci if (res < 0) 7678c2ecf20Sopenharmony_ci handle_write_error(mapping, page, res); 7688c2ecf20Sopenharmony_ci if (res == AOP_WRITEPAGE_ACTIVATE) { 7698c2ecf20Sopenharmony_ci ClearPageReclaim(page); 7708c2ecf20Sopenharmony_ci return PAGE_ACTIVATE; 7718c2ecf20Sopenharmony_ci } 7728c2ecf20Sopenharmony_ci 7738c2ecf20Sopenharmony_ci if (!PageWriteback(page)) { 7748c2ecf20Sopenharmony_ci /* synchronous write or broken a_ops? */ 7758c2ecf20Sopenharmony_ci ClearPageReclaim(page); 7768c2ecf20Sopenharmony_ci } 7778c2ecf20Sopenharmony_ci trace_mm_vmscan_writepage(page); 7788c2ecf20Sopenharmony_ci inc_node_page_state(page, NR_VMSCAN_WRITE); 7798c2ecf20Sopenharmony_ci return PAGE_SUCCESS; 7808c2ecf20Sopenharmony_ci } 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci return PAGE_CLEAN; 7838c2ecf20Sopenharmony_ci} 7848c2ecf20Sopenharmony_ci 7858c2ecf20Sopenharmony_ci/* 7868c2ecf20Sopenharmony_ci * Same as remove_mapping, but if the page is removed from the mapping, it 7878c2ecf20Sopenharmony_ci * gets returned with a refcount of 0. 7888c2ecf20Sopenharmony_ci */ 7898c2ecf20Sopenharmony_cistatic int __remove_mapping(struct address_space *mapping, struct page *page, 7908c2ecf20Sopenharmony_ci bool reclaimed, struct mem_cgroup *target_memcg) 7918c2ecf20Sopenharmony_ci{ 7928c2ecf20Sopenharmony_ci unsigned long flags; 7938c2ecf20Sopenharmony_ci int refcount; 7948c2ecf20Sopenharmony_ci void *shadow = NULL; 7958c2ecf20Sopenharmony_ci 7968c2ecf20Sopenharmony_ci BUG_ON(!PageLocked(page)); 7978c2ecf20Sopenharmony_ci BUG_ON(mapping != page_mapping(page)); 7988c2ecf20Sopenharmony_ci 7998c2ecf20Sopenharmony_ci xa_lock_irqsave(&mapping->i_pages, flags); 8008c2ecf20Sopenharmony_ci /* 8018c2ecf20Sopenharmony_ci * The non racy check for a busy page. 8028c2ecf20Sopenharmony_ci * 8038c2ecf20Sopenharmony_ci * Must be careful with the order of the tests. When someone has 8048c2ecf20Sopenharmony_ci * a ref to the page, it may be possible that they dirty it then 8058c2ecf20Sopenharmony_ci * drop the reference. So if PageDirty is tested before page_count 8068c2ecf20Sopenharmony_ci * here, then the following race may occur: 8078c2ecf20Sopenharmony_ci * 8088c2ecf20Sopenharmony_ci * get_user_pages(&page); 8098c2ecf20Sopenharmony_ci * [user mapping goes away] 8108c2ecf20Sopenharmony_ci * write_to(page); 8118c2ecf20Sopenharmony_ci * !PageDirty(page) [good] 8128c2ecf20Sopenharmony_ci * SetPageDirty(page); 8138c2ecf20Sopenharmony_ci * put_page(page); 8148c2ecf20Sopenharmony_ci * !page_count(page) [good, discard it] 8158c2ecf20Sopenharmony_ci * 8168c2ecf20Sopenharmony_ci * [oops, our write_to data is lost] 8178c2ecf20Sopenharmony_ci * 8188c2ecf20Sopenharmony_ci * Reversing the order of the tests ensures such a situation cannot 8198c2ecf20Sopenharmony_ci * escape unnoticed. The smp_rmb is needed to ensure the page->flags 8208c2ecf20Sopenharmony_ci * load is not satisfied before that of page->_refcount. 8218c2ecf20Sopenharmony_ci * 8228c2ecf20Sopenharmony_ci * Note that if SetPageDirty is always performed via set_page_dirty, 8238c2ecf20Sopenharmony_ci * and thus under the i_pages lock, then this ordering is not required. 8248c2ecf20Sopenharmony_ci */ 8258c2ecf20Sopenharmony_ci refcount = 1 + compound_nr(page); 8268c2ecf20Sopenharmony_ci if (!page_ref_freeze(page, refcount)) 8278c2ecf20Sopenharmony_ci goto cannot_free; 8288c2ecf20Sopenharmony_ci /* note: atomic_cmpxchg in page_ref_freeze provides the smp_rmb */ 8298c2ecf20Sopenharmony_ci if (unlikely(PageDirty(page))) { 8308c2ecf20Sopenharmony_ci page_ref_unfreeze(page, refcount); 8318c2ecf20Sopenharmony_ci goto cannot_free; 8328c2ecf20Sopenharmony_ci } 8338c2ecf20Sopenharmony_ci 8348c2ecf20Sopenharmony_ci if (PageSwapCache(page)) { 8358c2ecf20Sopenharmony_ci swp_entry_t swap = { .val = page_private(page) }; 8368c2ecf20Sopenharmony_ci mem_cgroup_swapout(page, swap); 8378c2ecf20Sopenharmony_ci if (reclaimed && !mapping_exiting(mapping)) 8388c2ecf20Sopenharmony_ci shadow = workingset_eviction(page, target_memcg); 8398c2ecf20Sopenharmony_ci __delete_from_swap_cache(page, swap, shadow); 8408c2ecf20Sopenharmony_ci xa_unlock_irqrestore(&mapping->i_pages, flags); 8418c2ecf20Sopenharmony_ci put_swap_page(page, swap); 8428c2ecf20Sopenharmony_ci } else { 8438c2ecf20Sopenharmony_ci void (*freepage)(struct page *); 8448c2ecf20Sopenharmony_ci 8458c2ecf20Sopenharmony_ci freepage = mapping->a_ops->freepage; 8468c2ecf20Sopenharmony_ci /* 8478c2ecf20Sopenharmony_ci * Remember a shadow entry for reclaimed file cache in 8488c2ecf20Sopenharmony_ci * order to detect refaults, thus thrashing, later on. 8498c2ecf20Sopenharmony_ci * 8508c2ecf20Sopenharmony_ci * But don't store shadows in an address space that is 8518c2ecf20Sopenharmony_ci * already exiting. This is not just an optimization, 8528c2ecf20Sopenharmony_ci * inode reclaim needs to empty out the radix tree or 8538c2ecf20Sopenharmony_ci * the nodes are lost. Don't plant shadows behind its 8548c2ecf20Sopenharmony_ci * back. 8558c2ecf20Sopenharmony_ci * 8568c2ecf20Sopenharmony_ci * We also don't store shadows for DAX mappings because the 8578c2ecf20Sopenharmony_ci * only page cache pages found in these are zero pages 8588c2ecf20Sopenharmony_ci * covering holes, and because we don't want to mix DAX 8598c2ecf20Sopenharmony_ci * exceptional entries and shadow exceptional entries in the 8608c2ecf20Sopenharmony_ci * same address_space. 8618c2ecf20Sopenharmony_ci */ 8628c2ecf20Sopenharmony_ci if (reclaimed && page_is_file_lru(page) && 8638c2ecf20Sopenharmony_ci !mapping_exiting(mapping) && !dax_mapping(mapping)) 8648c2ecf20Sopenharmony_ci shadow = workingset_eviction(page, target_memcg); 8658c2ecf20Sopenharmony_ci __delete_from_page_cache(page, shadow); 8668c2ecf20Sopenharmony_ci xa_unlock_irqrestore(&mapping->i_pages, flags); 8678c2ecf20Sopenharmony_ci 8688c2ecf20Sopenharmony_ci if (freepage != NULL) 8698c2ecf20Sopenharmony_ci freepage(page); 8708c2ecf20Sopenharmony_ci } 8718c2ecf20Sopenharmony_ci 8728c2ecf20Sopenharmony_ci return 1; 8738c2ecf20Sopenharmony_ci 8748c2ecf20Sopenharmony_cicannot_free: 8758c2ecf20Sopenharmony_ci xa_unlock_irqrestore(&mapping->i_pages, flags); 8768c2ecf20Sopenharmony_ci return 0; 8778c2ecf20Sopenharmony_ci} 8788c2ecf20Sopenharmony_ci 8798c2ecf20Sopenharmony_ci/* 8808c2ecf20Sopenharmony_ci * Attempt to detach a locked page from its ->mapping. If it is dirty or if 8818c2ecf20Sopenharmony_ci * someone else has a ref on the page, abort and return 0. If it was 8828c2ecf20Sopenharmony_ci * successfully detached, return 1. Assumes the caller has a single ref on 8838c2ecf20Sopenharmony_ci * this page. 8848c2ecf20Sopenharmony_ci */ 8858c2ecf20Sopenharmony_ciint remove_mapping(struct address_space *mapping, struct page *page) 8868c2ecf20Sopenharmony_ci{ 8878c2ecf20Sopenharmony_ci if (__remove_mapping(mapping, page, false, NULL)) { 8888c2ecf20Sopenharmony_ci /* 8898c2ecf20Sopenharmony_ci * Unfreezing the refcount with 1 rather than 2 effectively 8908c2ecf20Sopenharmony_ci * drops the pagecache ref for us without requiring another 8918c2ecf20Sopenharmony_ci * atomic operation. 8928c2ecf20Sopenharmony_ci */ 8938c2ecf20Sopenharmony_ci page_ref_unfreeze(page, 1); 8948c2ecf20Sopenharmony_ci return 1; 8958c2ecf20Sopenharmony_ci } 8968c2ecf20Sopenharmony_ci return 0; 8978c2ecf20Sopenharmony_ci} 8988c2ecf20Sopenharmony_ci 8998c2ecf20Sopenharmony_ci/** 9008c2ecf20Sopenharmony_ci * putback_lru_page - put previously isolated page onto appropriate LRU list 9018c2ecf20Sopenharmony_ci * @page: page to be put back to appropriate lru list 9028c2ecf20Sopenharmony_ci * 9038c2ecf20Sopenharmony_ci * Add previously isolated @page to appropriate LRU list. 9048c2ecf20Sopenharmony_ci * Page may still be unevictable for other reasons. 9058c2ecf20Sopenharmony_ci * 9068c2ecf20Sopenharmony_ci * lru_lock must not be held, interrupts must be enabled. 9078c2ecf20Sopenharmony_ci */ 9088c2ecf20Sopenharmony_civoid putback_lru_page(struct page *page) 9098c2ecf20Sopenharmony_ci{ 9108c2ecf20Sopenharmony_ci lru_cache_add(page); 9118c2ecf20Sopenharmony_ci put_page(page); /* drop ref from isolate */ 9128c2ecf20Sopenharmony_ci} 9138c2ecf20Sopenharmony_ci 9148c2ecf20Sopenharmony_cienum page_references { 9158c2ecf20Sopenharmony_ci PAGEREF_RECLAIM, 9168c2ecf20Sopenharmony_ci PAGEREF_RECLAIM_CLEAN, 9178c2ecf20Sopenharmony_ci PAGEREF_RECLAIM_PURGEABLE, 9188c2ecf20Sopenharmony_ci PAGEREF_KEEP, 9198c2ecf20Sopenharmony_ci PAGEREF_ACTIVATE, 9208c2ecf20Sopenharmony_ci}; 9218c2ecf20Sopenharmony_ci 9228c2ecf20Sopenharmony_cistatic enum page_references page_check_references(struct page *page, 9238c2ecf20Sopenharmony_ci struct scan_control *sc) 9248c2ecf20Sopenharmony_ci{ 9258c2ecf20Sopenharmony_ci int referenced_ptes, referenced_page; 9268c2ecf20Sopenharmony_ci unsigned long vm_flags; 9278c2ecf20Sopenharmony_ci 9288c2ecf20Sopenharmony_ci referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 9298c2ecf20Sopenharmony_ci &vm_flags); 9308c2ecf20Sopenharmony_ci referenced_page = TestClearPageReferenced(page); 9318c2ecf20Sopenharmony_ci 9328c2ecf20Sopenharmony_ci /* 9338c2ecf20Sopenharmony_ci * Mlock lost the isolation race with us. Let try_to_unmap() 9348c2ecf20Sopenharmony_ci * move the page to the unevictable list. 9358c2ecf20Sopenharmony_ci */ 9368c2ecf20Sopenharmony_ci if (vm_flags & VM_LOCKED) 9378c2ecf20Sopenharmony_ci return PAGEREF_RECLAIM; 9388c2ecf20Sopenharmony_ci 9398c2ecf20Sopenharmony_ci#ifdef CONFIG_MEM_PURGEABLE 9408c2ecf20Sopenharmony_ci if (vm_flags & VM_PURGEABLE) 9418c2ecf20Sopenharmony_ci return PAGEREF_RECLAIM_PURGEABLE; 9428c2ecf20Sopenharmony_ci#endif 9438c2ecf20Sopenharmony_ci if (referenced_ptes) { 9448c2ecf20Sopenharmony_ci /* 9458c2ecf20Sopenharmony_ci * All mapped pages start out with page table 9468c2ecf20Sopenharmony_ci * references from the instantiating fault, so we need 9478c2ecf20Sopenharmony_ci * to look twice if a mapped file page is used more 9488c2ecf20Sopenharmony_ci * than once. 9498c2ecf20Sopenharmony_ci * 9508c2ecf20Sopenharmony_ci * Mark it and spare it for another trip around the 9518c2ecf20Sopenharmony_ci * inactive list. Another page table reference will 9528c2ecf20Sopenharmony_ci * lead to its activation. 9538c2ecf20Sopenharmony_ci * 9548c2ecf20Sopenharmony_ci * Note: the mark is set for activated pages as well 9558c2ecf20Sopenharmony_ci * so that recently deactivated but used pages are 9568c2ecf20Sopenharmony_ci * quickly recovered. 9578c2ecf20Sopenharmony_ci */ 9588c2ecf20Sopenharmony_ci SetPageReferenced(page); 9598c2ecf20Sopenharmony_ci 9608c2ecf20Sopenharmony_ci if (referenced_page || referenced_ptes > 1) 9618c2ecf20Sopenharmony_ci return PAGEREF_ACTIVATE; 9628c2ecf20Sopenharmony_ci 9638c2ecf20Sopenharmony_ci /* 9648c2ecf20Sopenharmony_ci * Activate file-backed executable pages after first usage. 9658c2ecf20Sopenharmony_ci */ 9668c2ecf20Sopenharmony_ci if ((vm_flags & VM_EXEC) && !PageSwapBacked(page)) 9678c2ecf20Sopenharmony_ci return PAGEREF_ACTIVATE; 9688c2ecf20Sopenharmony_ci 9698c2ecf20Sopenharmony_ci return PAGEREF_KEEP; 9708c2ecf20Sopenharmony_ci } 9718c2ecf20Sopenharmony_ci 9728c2ecf20Sopenharmony_ci /* Reclaim if clean, defer dirty pages to writeback */ 9738c2ecf20Sopenharmony_ci if (referenced_page && !PageSwapBacked(page)) 9748c2ecf20Sopenharmony_ci return PAGEREF_RECLAIM_CLEAN; 9758c2ecf20Sopenharmony_ci 9768c2ecf20Sopenharmony_ci return PAGEREF_RECLAIM; 9778c2ecf20Sopenharmony_ci} 9788c2ecf20Sopenharmony_ci 9798c2ecf20Sopenharmony_ci/* Check if a page is dirty or under writeback */ 9808c2ecf20Sopenharmony_cistatic void page_check_dirty_writeback(struct page *page, 9818c2ecf20Sopenharmony_ci bool *dirty, bool *writeback) 9828c2ecf20Sopenharmony_ci{ 9838c2ecf20Sopenharmony_ci struct address_space *mapping; 9848c2ecf20Sopenharmony_ci 9858c2ecf20Sopenharmony_ci /* 9868c2ecf20Sopenharmony_ci * Anonymous pages are not handled by flushers and must be written 9878c2ecf20Sopenharmony_ci * from reclaim context. Do not stall reclaim based on them 9888c2ecf20Sopenharmony_ci */ 9898c2ecf20Sopenharmony_ci if (!page_is_file_lru(page) || 9908c2ecf20Sopenharmony_ci (PageAnon(page) && !PageSwapBacked(page))) { 9918c2ecf20Sopenharmony_ci *dirty = false; 9928c2ecf20Sopenharmony_ci *writeback = false; 9938c2ecf20Sopenharmony_ci return; 9948c2ecf20Sopenharmony_ci } 9958c2ecf20Sopenharmony_ci 9968c2ecf20Sopenharmony_ci /* By default assume that the page flags are accurate */ 9978c2ecf20Sopenharmony_ci *dirty = PageDirty(page); 9988c2ecf20Sopenharmony_ci *writeback = PageWriteback(page); 9998c2ecf20Sopenharmony_ci 10008c2ecf20Sopenharmony_ci /* Verify dirty/writeback state if the filesystem supports it */ 10018c2ecf20Sopenharmony_ci if (!page_has_private(page)) 10028c2ecf20Sopenharmony_ci return; 10038c2ecf20Sopenharmony_ci 10048c2ecf20Sopenharmony_ci mapping = page_mapping(page); 10058c2ecf20Sopenharmony_ci if (mapping && mapping->a_ops->is_dirty_writeback) 10068c2ecf20Sopenharmony_ci mapping->a_ops->is_dirty_writeback(page, dirty, writeback); 10078c2ecf20Sopenharmony_ci} 10088c2ecf20Sopenharmony_ci 10098c2ecf20Sopenharmony_ci/* 10108c2ecf20Sopenharmony_ci * shrink_page_list() returns the number of reclaimed pages 10118c2ecf20Sopenharmony_ci */ 10128c2ecf20Sopenharmony_ciunsigned int shrink_page_list(struct list_head *page_list, 10138c2ecf20Sopenharmony_ci struct pglist_data *pgdat, 10148c2ecf20Sopenharmony_ci struct scan_control *sc, 10158c2ecf20Sopenharmony_ci struct reclaim_stat *stat, 10168c2ecf20Sopenharmony_ci bool ignore_references) 10178c2ecf20Sopenharmony_ci{ 10188c2ecf20Sopenharmony_ci LIST_HEAD(ret_pages); 10198c2ecf20Sopenharmony_ci LIST_HEAD(free_pages); 10208c2ecf20Sopenharmony_ci unsigned int nr_reclaimed = 0; 10218c2ecf20Sopenharmony_ci unsigned int pgactivate = 0; 10228c2ecf20Sopenharmony_ci 10238c2ecf20Sopenharmony_ci memset(stat, 0, sizeof(*stat)); 10248c2ecf20Sopenharmony_ci cond_resched(); 10258c2ecf20Sopenharmony_ci 10268c2ecf20Sopenharmony_ci while (!list_empty(page_list)) { 10278c2ecf20Sopenharmony_ci struct address_space *mapping; 10288c2ecf20Sopenharmony_ci struct page *page; 10298c2ecf20Sopenharmony_ci enum page_references references = PAGEREF_RECLAIM; 10308c2ecf20Sopenharmony_ci bool dirty, writeback, may_enter_fs; 10318c2ecf20Sopenharmony_ci unsigned int nr_pages; 10328c2ecf20Sopenharmony_ci 10338c2ecf20Sopenharmony_ci cond_resched(); 10348c2ecf20Sopenharmony_ci 10358c2ecf20Sopenharmony_ci page = lru_to_page(page_list); 10368c2ecf20Sopenharmony_ci list_del(&page->lru); 10378c2ecf20Sopenharmony_ci 10388c2ecf20Sopenharmony_ci if (!trylock_page(page)) 10398c2ecf20Sopenharmony_ci goto keep; 10408c2ecf20Sopenharmony_ci 10418c2ecf20Sopenharmony_ci VM_BUG_ON_PAGE(PageActive(page), page); 10428c2ecf20Sopenharmony_ci 10438c2ecf20Sopenharmony_ci nr_pages = compound_nr(page); 10448c2ecf20Sopenharmony_ci 10458c2ecf20Sopenharmony_ci /* Account the number of base pages even though THP */ 10468c2ecf20Sopenharmony_ci sc->nr_scanned += nr_pages; 10478c2ecf20Sopenharmony_ci 10488c2ecf20Sopenharmony_ci if (unlikely(!page_evictable(page))) 10498c2ecf20Sopenharmony_ci goto activate_locked; 10508c2ecf20Sopenharmony_ci 10518c2ecf20Sopenharmony_ci if (!sc->may_unmap && page_mapped(page)) 10528c2ecf20Sopenharmony_ci goto keep_locked; 10538c2ecf20Sopenharmony_ci 10548c2ecf20Sopenharmony_ci may_enter_fs = (sc->gfp_mask & __GFP_FS) || 10558c2ecf20Sopenharmony_ci (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 10568c2ecf20Sopenharmony_ci 10578c2ecf20Sopenharmony_ci /* 10588c2ecf20Sopenharmony_ci * The number of dirty pages determines if a node is marked 10598c2ecf20Sopenharmony_ci * reclaim_congested which affects wait_iff_congested. kswapd 10608c2ecf20Sopenharmony_ci * will stall and start writing pages if the tail of the LRU 10618c2ecf20Sopenharmony_ci * is all dirty unqueued pages. 10628c2ecf20Sopenharmony_ci */ 10638c2ecf20Sopenharmony_ci page_check_dirty_writeback(page, &dirty, &writeback); 10648c2ecf20Sopenharmony_ci if (dirty || writeback) 10658c2ecf20Sopenharmony_ci stat->nr_dirty++; 10668c2ecf20Sopenharmony_ci 10678c2ecf20Sopenharmony_ci if (dirty && !writeback) 10688c2ecf20Sopenharmony_ci stat->nr_unqueued_dirty++; 10698c2ecf20Sopenharmony_ci 10708c2ecf20Sopenharmony_ci /* 10718c2ecf20Sopenharmony_ci * Treat this page as congested if the underlying BDI is or if 10728c2ecf20Sopenharmony_ci * pages are cycling through the LRU so quickly that the 10738c2ecf20Sopenharmony_ci * pages marked for immediate reclaim are making it to the 10748c2ecf20Sopenharmony_ci * end of the LRU a second time. 10758c2ecf20Sopenharmony_ci */ 10768c2ecf20Sopenharmony_ci mapping = page_mapping(page); 10778c2ecf20Sopenharmony_ci if (((dirty || writeback) && mapping && 10788c2ecf20Sopenharmony_ci inode_write_congested(mapping->host)) || 10798c2ecf20Sopenharmony_ci (writeback && PageReclaim(page))) 10808c2ecf20Sopenharmony_ci stat->nr_congested++; 10818c2ecf20Sopenharmony_ci 10828c2ecf20Sopenharmony_ci /* 10838c2ecf20Sopenharmony_ci * If a page at the tail of the LRU is under writeback, there 10848c2ecf20Sopenharmony_ci * are three cases to consider. 10858c2ecf20Sopenharmony_ci * 10868c2ecf20Sopenharmony_ci * 1) If reclaim is encountering an excessive number of pages 10878c2ecf20Sopenharmony_ci * under writeback and this page is both under writeback and 10888c2ecf20Sopenharmony_ci * PageReclaim then it indicates that pages are being queued 10898c2ecf20Sopenharmony_ci * for IO but are being recycled through the LRU before the 10908c2ecf20Sopenharmony_ci * IO can complete. Waiting on the page itself risks an 10918c2ecf20Sopenharmony_ci * indefinite stall if it is impossible to writeback the 10928c2ecf20Sopenharmony_ci * page due to IO error or disconnected storage so instead 10938c2ecf20Sopenharmony_ci * note that the LRU is being scanned too quickly and the 10948c2ecf20Sopenharmony_ci * caller can stall after page list has been processed. 10958c2ecf20Sopenharmony_ci * 10968c2ecf20Sopenharmony_ci * 2) Global or new memcg reclaim encounters a page that is 10978c2ecf20Sopenharmony_ci * not marked for immediate reclaim, or the caller does not 10988c2ecf20Sopenharmony_ci * have __GFP_FS (or __GFP_IO if it's simply going to swap, 10998c2ecf20Sopenharmony_ci * not to fs). In this case mark the page for immediate 11008c2ecf20Sopenharmony_ci * reclaim and continue scanning. 11018c2ecf20Sopenharmony_ci * 11028c2ecf20Sopenharmony_ci * Require may_enter_fs because we would wait on fs, which 11038c2ecf20Sopenharmony_ci * may not have submitted IO yet. And the loop driver might 11048c2ecf20Sopenharmony_ci * enter reclaim, and deadlock if it waits on a page for 11058c2ecf20Sopenharmony_ci * which it is needed to do the write (loop masks off 11068c2ecf20Sopenharmony_ci * __GFP_IO|__GFP_FS for this reason); but more thought 11078c2ecf20Sopenharmony_ci * would probably show more reasons. 11088c2ecf20Sopenharmony_ci * 11098c2ecf20Sopenharmony_ci * 3) Legacy memcg encounters a page that is already marked 11108c2ecf20Sopenharmony_ci * PageReclaim. memcg does not have any dirty pages 11118c2ecf20Sopenharmony_ci * throttling so we could easily OOM just because too many 11128c2ecf20Sopenharmony_ci * pages are in writeback and there is nothing else to 11138c2ecf20Sopenharmony_ci * reclaim. Wait for the writeback to complete. 11148c2ecf20Sopenharmony_ci * 11158c2ecf20Sopenharmony_ci * In cases 1) and 2) we activate the pages to get them out of 11168c2ecf20Sopenharmony_ci * the way while we continue scanning for clean pages on the 11178c2ecf20Sopenharmony_ci * inactive list and refilling from the active list. The 11188c2ecf20Sopenharmony_ci * observation here is that waiting for disk writes is more 11198c2ecf20Sopenharmony_ci * expensive than potentially causing reloads down the line. 11208c2ecf20Sopenharmony_ci * Since they're marked for immediate reclaim, they won't put 11218c2ecf20Sopenharmony_ci * memory pressure on the cache working set any longer than it 11228c2ecf20Sopenharmony_ci * takes to write them to disk. 11238c2ecf20Sopenharmony_ci */ 11248c2ecf20Sopenharmony_ci if (PageWriteback(page)) { 11258c2ecf20Sopenharmony_ci /* Case 1 above */ 11268c2ecf20Sopenharmony_ci if (current_is_kswapd() && 11278c2ecf20Sopenharmony_ci PageReclaim(page) && 11288c2ecf20Sopenharmony_ci test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { 11298c2ecf20Sopenharmony_ci stat->nr_immediate++; 11308c2ecf20Sopenharmony_ci goto activate_locked; 11318c2ecf20Sopenharmony_ci 11328c2ecf20Sopenharmony_ci /* Case 2 above */ 11338c2ecf20Sopenharmony_ci } else if (writeback_throttling_sane(sc) || 11348c2ecf20Sopenharmony_ci !PageReclaim(page) || !may_enter_fs) { 11358c2ecf20Sopenharmony_ci /* 11368c2ecf20Sopenharmony_ci * This is slightly racy - end_page_writeback() 11378c2ecf20Sopenharmony_ci * might have just cleared PageReclaim, then 11388c2ecf20Sopenharmony_ci * setting PageReclaim here end up interpreted 11398c2ecf20Sopenharmony_ci * as PageReadahead - but that does not matter 11408c2ecf20Sopenharmony_ci * enough to care. What we do want is for this 11418c2ecf20Sopenharmony_ci * page to have PageReclaim set next time memcg 11428c2ecf20Sopenharmony_ci * reclaim reaches the tests above, so it will 11438c2ecf20Sopenharmony_ci * then wait_on_page_writeback() to avoid OOM; 11448c2ecf20Sopenharmony_ci * and it's also appropriate in global reclaim. 11458c2ecf20Sopenharmony_ci */ 11468c2ecf20Sopenharmony_ci SetPageReclaim(page); 11478c2ecf20Sopenharmony_ci stat->nr_writeback++; 11488c2ecf20Sopenharmony_ci goto activate_locked; 11498c2ecf20Sopenharmony_ci 11508c2ecf20Sopenharmony_ci /* Case 3 above */ 11518c2ecf20Sopenharmony_ci } else { 11528c2ecf20Sopenharmony_ci unlock_page(page); 11538c2ecf20Sopenharmony_ci wait_on_page_writeback(page); 11548c2ecf20Sopenharmony_ci /* then go back and try same page again */ 11558c2ecf20Sopenharmony_ci list_add_tail(&page->lru, page_list); 11568c2ecf20Sopenharmony_ci continue; 11578c2ecf20Sopenharmony_ci } 11588c2ecf20Sopenharmony_ci } 11598c2ecf20Sopenharmony_ci 11608c2ecf20Sopenharmony_ci if (!ignore_references) 11618c2ecf20Sopenharmony_ci references = page_check_references(page, sc); 11628c2ecf20Sopenharmony_ci 11638c2ecf20Sopenharmony_ci switch (references) { 11648c2ecf20Sopenharmony_ci case PAGEREF_ACTIVATE: 11658c2ecf20Sopenharmony_ci goto activate_locked; 11668c2ecf20Sopenharmony_ci case PAGEREF_KEEP: 11678c2ecf20Sopenharmony_ci stat->nr_ref_keep += nr_pages; 11688c2ecf20Sopenharmony_ci goto keep_locked; 11698c2ecf20Sopenharmony_ci case PAGEREF_RECLAIM: 11708c2ecf20Sopenharmony_ci case PAGEREF_RECLAIM_CLEAN: 11718c2ecf20Sopenharmony_ci case PAGEREF_RECLAIM_PURGEABLE: 11728c2ecf20Sopenharmony_ci ; /* try to reclaim the page below */ 11738c2ecf20Sopenharmony_ci } 11748c2ecf20Sopenharmony_ci 11758c2ecf20Sopenharmony_ci /* 11768c2ecf20Sopenharmony_ci * Anonymous process memory has backing store? 11778c2ecf20Sopenharmony_ci * Try to allocate it some swap space here. 11788c2ecf20Sopenharmony_ci * Lazyfree page could be freed directly 11798c2ecf20Sopenharmony_ci */ 11808c2ecf20Sopenharmony_ci if (PageAnon(page) && PageSwapBacked(page)) { 11818c2ecf20Sopenharmony_ci if (!PageSwapCache(page) && references != PAGEREF_RECLAIM_PURGEABLE) { 11828c2ecf20Sopenharmony_ci if (!(sc->gfp_mask & __GFP_IO)) 11838c2ecf20Sopenharmony_ci goto keep_locked; 11848c2ecf20Sopenharmony_ci if (page_maybe_dma_pinned(page)) 11858c2ecf20Sopenharmony_ci goto keep_locked; 11868c2ecf20Sopenharmony_ci if (PageTransHuge(page)) { 11878c2ecf20Sopenharmony_ci /* cannot split THP, skip it */ 11888c2ecf20Sopenharmony_ci if (!can_split_huge_page(page, NULL)) 11898c2ecf20Sopenharmony_ci goto activate_locked; 11908c2ecf20Sopenharmony_ci /* 11918c2ecf20Sopenharmony_ci * Split pages without a PMD map right 11928c2ecf20Sopenharmony_ci * away. Chances are some or all of the 11938c2ecf20Sopenharmony_ci * tail pages can be freed without IO. 11948c2ecf20Sopenharmony_ci */ 11958c2ecf20Sopenharmony_ci if (!compound_mapcount(page) && 11968c2ecf20Sopenharmony_ci split_huge_page_to_list(page, 11978c2ecf20Sopenharmony_ci page_list)) 11988c2ecf20Sopenharmony_ci goto activate_locked; 11998c2ecf20Sopenharmony_ci } 12008c2ecf20Sopenharmony_ci if (!add_to_swap(page)) { 12018c2ecf20Sopenharmony_ci if (!PageTransHuge(page)) 12028c2ecf20Sopenharmony_ci goto activate_locked_split; 12038c2ecf20Sopenharmony_ci /* Fallback to swap normal pages */ 12048c2ecf20Sopenharmony_ci if (split_huge_page_to_list(page, 12058c2ecf20Sopenharmony_ci page_list)) 12068c2ecf20Sopenharmony_ci goto activate_locked; 12078c2ecf20Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE 12088c2ecf20Sopenharmony_ci count_vm_event(THP_SWPOUT_FALLBACK); 12098c2ecf20Sopenharmony_ci#endif 12108c2ecf20Sopenharmony_ci if (!add_to_swap(page)) 12118c2ecf20Sopenharmony_ci goto activate_locked_split; 12128c2ecf20Sopenharmony_ci } 12138c2ecf20Sopenharmony_ci 12148c2ecf20Sopenharmony_ci may_enter_fs = true; 12158c2ecf20Sopenharmony_ci 12168c2ecf20Sopenharmony_ci /* Adding to swap updated mapping */ 12178c2ecf20Sopenharmony_ci mapping = page_mapping(page); 12188c2ecf20Sopenharmony_ci } 12198c2ecf20Sopenharmony_ci } else if (unlikely(PageTransHuge(page))) { 12208c2ecf20Sopenharmony_ci /* Split file THP */ 12218c2ecf20Sopenharmony_ci if (split_huge_page_to_list(page, page_list)) 12228c2ecf20Sopenharmony_ci goto keep_locked; 12238c2ecf20Sopenharmony_ci } 12248c2ecf20Sopenharmony_ci 12258c2ecf20Sopenharmony_ci /* 12268c2ecf20Sopenharmony_ci * THP may get split above, need minus tail pages and update 12278c2ecf20Sopenharmony_ci * nr_pages to avoid accounting tail pages twice. 12288c2ecf20Sopenharmony_ci * 12298c2ecf20Sopenharmony_ci * The tail pages that are added into swap cache successfully 12308c2ecf20Sopenharmony_ci * reach here. 12318c2ecf20Sopenharmony_ci */ 12328c2ecf20Sopenharmony_ci if ((nr_pages > 1) && !PageTransHuge(page)) { 12338c2ecf20Sopenharmony_ci sc->nr_scanned -= (nr_pages - 1); 12348c2ecf20Sopenharmony_ci nr_pages = 1; 12358c2ecf20Sopenharmony_ci } 12368c2ecf20Sopenharmony_ci 12378c2ecf20Sopenharmony_ci /* 12388c2ecf20Sopenharmony_ci * The page is mapped into the page tables of one or more 12398c2ecf20Sopenharmony_ci * processes. Try to unmap it here. 12408c2ecf20Sopenharmony_ci */ 12418c2ecf20Sopenharmony_ci if (page_mapped(page)) { 12428c2ecf20Sopenharmony_ci enum ttu_flags flags = TTU_BATCH_FLUSH; 12438c2ecf20Sopenharmony_ci bool was_swapbacked = PageSwapBacked(page); 12448c2ecf20Sopenharmony_ci 12458c2ecf20Sopenharmony_ci if (unlikely(PageTransHuge(page))) 12468c2ecf20Sopenharmony_ci flags |= TTU_SPLIT_HUGE_PMD; 12478c2ecf20Sopenharmony_ci 12488c2ecf20Sopenharmony_ci if (!try_to_unmap(page, flags)) { 12498c2ecf20Sopenharmony_ci stat->nr_unmap_fail += nr_pages; 12508c2ecf20Sopenharmony_ci if (!was_swapbacked && PageSwapBacked(page)) 12518c2ecf20Sopenharmony_ci stat->nr_lazyfree_fail += nr_pages; 12528c2ecf20Sopenharmony_ci goto activate_locked; 12538c2ecf20Sopenharmony_ci } 12548c2ecf20Sopenharmony_ci } 12558c2ecf20Sopenharmony_ci 12568c2ecf20Sopenharmony_ci if (PageDirty(page) && references != PAGEREF_RECLAIM_PURGEABLE) { 12578c2ecf20Sopenharmony_ci /* 12588c2ecf20Sopenharmony_ci * Only kswapd can writeback filesystem pages 12598c2ecf20Sopenharmony_ci * to avoid risk of stack overflow. But avoid 12608c2ecf20Sopenharmony_ci * injecting inefficient single-page IO into 12618c2ecf20Sopenharmony_ci * flusher writeback as much as possible: only 12628c2ecf20Sopenharmony_ci * write pages when we've encountered many 12638c2ecf20Sopenharmony_ci * dirty pages, and when we've already scanned 12648c2ecf20Sopenharmony_ci * the rest of the LRU for clean pages and see 12658c2ecf20Sopenharmony_ci * the same dirty pages again (PageReclaim). 12668c2ecf20Sopenharmony_ci */ 12678c2ecf20Sopenharmony_ci if (page_is_file_lru(page) && 12688c2ecf20Sopenharmony_ci (!current_is_kswapd() || !PageReclaim(page) || 12698c2ecf20Sopenharmony_ci !test_bit(PGDAT_DIRTY, &pgdat->flags))) { 12708c2ecf20Sopenharmony_ci /* 12718c2ecf20Sopenharmony_ci * Immediately reclaim when written back. 12728c2ecf20Sopenharmony_ci * Similar in principal to deactivate_page() 12738c2ecf20Sopenharmony_ci * except we already have the page isolated 12748c2ecf20Sopenharmony_ci * and know it's dirty 12758c2ecf20Sopenharmony_ci */ 12768c2ecf20Sopenharmony_ci inc_node_page_state(page, NR_VMSCAN_IMMEDIATE); 12778c2ecf20Sopenharmony_ci SetPageReclaim(page); 12788c2ecf20Sopenharmony_ci 12798c2ecf20Sopenharmony_ci goto activate_locked; 12808c2ecf20Sopenharmony_ci } 12818c2ecf20Sopenharmony_ci 12828c2ecf20Sopenharmony_ci if (references == PAGEREF_RECLAIM_CLEAN) 12838c2ecf20Sopenharmony_ci goto keep_locked; 12848c2ecf20Sopenharmony_ci if (!may_enter_fs) 12858c2ecf20Sopenharmony_ci goto keep_locked; 12868c2ecf20Sopenharmony_ci if (!sc->may_writepage) 12878c2ecf20Sopenharmony_ci goto keep_locked; 12888c2ecf20Sopenharmony_ci 12898c2ecf20Sopenharmony_ci /* 12908c2ecf20Sopenharmony_ci * Page is dirty. Flush the TLB if a writable entry 12918c2ecf20Sopenharmony_ci * potentially exists to avoid CPU writes after IO 12928c2ecf20Sopenharmony_ci * starts and then write it out here. 12938c2ecf20Sopenharmony_ci */ 12948c2ecf20Sopenharmony_ci try_to_unmap_flush_dirty(); 12958c2ecf20Sopenharmony_ci switch (pageout(page, mapping)) { 12968c2ecf20Sopenharmony_ci case PAGE_KEEP: 12978c2ecf20Sopenharmony_ci goto keep_locked; 12988c2ecf20Sopenharmony_ci case PAGE_ACTIVATE: 12998c2ecf20Sopenharmony_ci goto activate_locked; 13008c2ecf20Sopenharmony_ci case PAGE_SUCCESS: 13018c2ecf20Sopenharmony_ci stat->nr_pageout += thp_nr_pages(page); 13028c2ecf20Sopenharmony_ci 13038c2ecf20Sopenharmony_ci if (PageWriteback(page)) 13048c2ecf20Sopenharmony_ci goto keep; 13058c2ecf20Sopenharmony_ci if (PageDirty(page)) 13068c2ecf20Sopenharmony_ci goto keep; 13078c2ecf20Sopenharmony_ci 13088c2ecf20Sopenharmony_ci /* 13098c2ecf20Sopenharmony_ci * A synchronous write - probably a ramdisk. Go 13108c2ecf20Sopenharmony_ci * ahead and try to reclaim the page. 13118c2ecf20Sopenharmony_ci */ 13128c2ecf20Sopenharmony_ci if (!trylock_page(page)) 13138c2ecf20Sopenharmony_ci goto keep; 13148c2ecf20Sopenharmony_ci if (PageDirty(page) || PageWriteback(page)) 13158c2ecf20Sopenharmony_ci goto keep_locked; 13168c2ecf20Sopenharmony_ci mapping = page_mapping(page); 13178c2ecf20Sopenharmony_ci case PAGE_CLEAN: 13188c2ecf20Sopenharmony_ci ; /* try to free the page below */ 13198c2ecf20Sopenharmony_ci } 13208c2ecf20Sopenharmony_ci } 13218c2ecf20Sopenharmony_ci 13228c2ecf20Sopenharmony_ci /* 13238c2ecf20Sopenharmony_ci * If the page has buffers, try to free the buffer mappings 13248c2ecf20Sopenharmony_ci * associated with this page. If we succeed we try to free 13258c2ecf20Sopenharmony_ci * the page as well. 13268c2ecf20Sopenharmony_ci * 13278c2ecf20Sopenharmony_ci * We do this even if the page is PageDirty(). 13288c2ecf20Sopenharmony_ci * try_to_release_page() does not perform I/O, but it is 13298c2ecf20Sopenharmony_ci * possible for a page to have PageDirty set, but it is actually 13308c2ecf20Sopenharmony_ci * clean (all its buffers are clean). This happens if the 13318c2ecf20Sopenharmony_ci * buffers were written out directly, with submit_bh(). ext3 13328c2ecf20Sopenharmony_ci * will do this, as well as the blockdev mapping. 13338c2ecf20Sopenharmony_ci * try_to_release_page() will discover that cleanness and will 13348c2ecf20Sopenharmony_ci * drop the buffers and mark the page clean - it can be freed. 13358c2ecf20Sopenharmony_ci * 13368c2ecf20Sopenharmony_ci * Rarely, pages can have buffers and no ->mapping. These are 13378c2ecf20Sopenharmony_ci * the pages which were not successfully invalidated in 13388c2ecf20Sopenharmony_ci * truncate_complete_page(). We try to drop those buffers here 13398c2ecf20Sopenharmony_ci * and if that worked, and the page is no longer mapped into 13408c2ecf20Sopenharmony_ci * process address space (page_count == 1) it can be freed. 13418c2ecf20Sopenharmony_ci * Otherwise, leave the page on the LRU so it is swappable. 13428c2ecf20Sopenharmony_ci */ 13438c2ecf20Sopenharmony_ci if (page_has_private(page)) { 13448c2ecf20Sopenharmony_ci if (!try_to_release_page(page, sc->gfp_mask)) 13458c2ecf20Sopenharmony_ci goto activate_locked; 13468c2ecf20Sopenharmony_ci if (!mapping && page_count(page) == 1) { 13478c2ecf20Sopenharmony_ci unlock_page(page); 13488c2ecf20Sopenharmony_ci if (put_page_testzero(page)) 13498c2ecf20Sopenharmony_ci goto free_it; 13508c2ecf20Sopenharmony_ci else { 13518c2ecf20Sopenharmony_ci /* 13528c2ecf20Sopenharmony_ci * rare race with speculative reference. 13538c2ecf20Sopenharmony_ci * the speculative reference will free 13548c2ecf20Sopenharmony_ci * this page shortly, so we may 13558c2ecf20Sopenharmony_ci * increment nr_reclaimed here (and 13568c2ecf20Sopenharmony_ci * leave it off the LRU). 13578c2ecf20Sopenharmony_ci */ 13588c2ecf20Sopenharmony_ci nr_reclaimed++; 13598c2ecf20Sopenharmony_ci continue; 13608c2ecf20Sopenharmony_ci } 13618c2ecf20Sopenharmony_ci } 13628c2ecf20Sopenharmony_ci } 13638c2ecf20Sopenharmony_ci 13648c2ecf20Sopenharmony_ci if (PageAnon(page) && 13658c2ecf20Sopenharmony_ci (!PageSwapBacked(page) || 13668c2ecf20Sopenharmony_ci references == PAGEREF_RECLAIM_PURGEABLE)) { 13678c2ecf20Sopenharmony_ci /* follow __remove_mapping for reference */ 13688c2ecf20Sopenharmony_ci if (!page_ref_freeze(page, 1)) 13698c2ecf20Sopenharmony_ci goto keep_locked; 13708c2ecf20Sopenharmony_ci if (PageDirty(page) && references != PAGEREF_RECLAIM_PURGEABLE) { 13718c2ecf20Sopenharmony_ci page_ref_unfreeze(page, 1); 13728c2ecf20Sopenharmony_ci goto keep_locked; 13738c2ecf20Sopenharmony_ci } 13748c2ecf20Sopenharmony_ci 13758c2ecf20Sopenharmony_ci count_vm_event(PGLAZYFREED); 13768c2ecf20Sopenharmony_ci count_memcg_page_event(page, PGLAZYFREED); 13778c2ecf20Sopenharmony_ci } else if (!mapping || !__remove_mapping(mapping, page, true, 13788c2ecf20Sopenharmony_ci sc->target_mem_cgroup)) 13798c2ecf20Sopenharmony_ci goto keep_locked; 13808c2ecf20Sopenharmony_ci 13818c2ecf20Sopenharmony_ci unlock_page(page); 13828c2ecf20Sopenharmony_cifree_it: 13838c2ecf20Sopenharmony_ci /* 13848c2ecf20Sopenharmony_ci * THP may get swapped out in a whole, need account 13858c2ecf20Sopenharmony_ci * all base pages. 13868c2ecf20Sopenharmony_ci */ 13878c2ecf20Sopenharmony_ci nr_reclaimed += nr_pages; 13888c2ecf20Sopenharmony_ci 13898c2ecf20Sopenharmony_ci /* 13908c2ecf20Sopenharmony_ci * Is there need to periodically free_page_list? It would 13918c2ecf20Sopenharmony_ci * appear not as the counts should be low 13928c2ecf20Sopenharmony_ci */ 13938c2ecf20Sopenharmony_ci if (unlikely(PageTransHuge(page))) 13948c2ecf20Sopenharmony_ci destroy_compound_page(page); 13958c2ecf20Sopenharmony_ci else 13968c2ecf20Sopenharmony_ci list_add(&page->lru, &free_pages); 13978c2ecf20Sopenharmony_ci continue; 13988c2ecf20Sopenharmony_ci 13998c2ecf20Sopenharmony_ciactivate_locked_split: 14008c2ecf20Sopenharmony_ci /* 14018c2ecf20Sopenharmony_ci * The tail pages that are failed to add into swap cache 14028c2ecf20Sopenharmony_ci * reach here. Fixup nr_scanned and nr_pages. 14038c2ecf20Sopenharmony_ci */ 14048c2ecf20Sopenharmony_ci if (nr_pages > 1) { 14058c2ecf20Sopenharmony_ci sc->nr_scanned -= (nr_pages - 1); 14068c2ecf20Sopenharmony_ci nr_pages = 1; 14078c2ecf20Sopenharmony_ci } 14088c2ecf20Sopenharmony_ciactivate_locked: 14098c2ecf20Sopenharmony_ci /* Not a candidate for swapping, so reclaim swap space. */ 14108c2ecf20Sopenharmony_ci if (PageSwapCache(page) && (mem_cgroup_swap_full(page) || 14118c2ecf20Sopenharmony_ci PageMlocked(page))) 14128c2ecf20Sopenharmony_ci try_to_free_swap(page); 14138c2ecf20Sopenharmony_ci VM_BUG_ON_PAGE(PageActive(page), page); 14148c2ecf20Sopenharmony_ci if (!PageMlocked(page)) { 14158c2ecf20Sopenharmony_ci int type = page_is_file_lru(page); 14168c2ecf20Sopenharmony_ci SetPageActive(page); 14178c2ecf20Sopenharmony_ci stat->nr_activate[type] += nr_pages; 14188c2ecf20Sopenharmony_ci count_memcg_page_event(page, PGACTIVATE); 14198c2ecf20Sopenharmony_ci } 14208c2ecf20Sopenharmony_cikeep_locked: 14218c2ecf20Sopenharmony_ci unlock_page(page); 14228c2ecf20Sopenharmony_cikeep: 14238c2ecf20Sopenharmony_ci list_add(&page->lru, &ret_pages); 14248c2ecf20Sopenharmony_ci VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); 14258c2ecf20Sopenharmony_ci } 14268c2ecf20Sopenharmony_ci 14278c2ecf20Sopenharmony_ci pgactivate = stat->nr_activate[0] + stat->nr_activate[1]; 14288c2ecf20Sopenharmony_ci 14298c2ecf20Sopenharmony_ci mem_cgroup_uncharge_list(&free_pages); 14308c2ecf20Sopenharmony_ci try_to_unmap_flush(); 14318c2ecf20Sopenharmony_ci free_unref_page_list(&free_pages); 14328c2ecf20Sopenharmony_ci 14338c2ecf20Sopenharmony_ci list_splice(&ret_pages, page_list); 14348c2ecf20Sopenharmony_ci count_vm_events(PGACTIVATE, pgactivate); 14358c2ecf20Sopenharmony_ci 14368c2ecf20Sopenharmony_ci return nr_reclaimed; 14378c2ecf20Sopenharmony_ci} 14388c2ecf20Sopenharmony_ci 14398c2ecf20Sopenharmony_ciunsigned int reclaim_clean_pages_from_list(struct zone *zone, 14408c2ecf20Sopenharmony_ci struct list_head *page_list) 14418c2ecf20Sopenharmony_ci{ 14428c2ecf20Sopenharmony_ci struct scan_control sc = { 14438c2ecf20Sopenharmony_ci .gfp_mask = GFP_KERNEL, 14448c2ecf20Sopenharmony_ci .priority = DEF_PRIORITY, 14458c2ecf20Sopenharmony_ci .may_unmap = 1, 14468c2ecf20Sopenharmony_ci }; 14478c2ecf20Sopenharmony_ci struct reclaim_stat stat; 14488c2ecf20Sopenharmony_ci unsigned int nr_reclaimed; 14498c2ecf20Sopenharmony_ci struct page *page, *next; 14508c2ecf20Sopenharmony_ci LIST_HEAD(clean_pages); 14518c2ecf20Sopenharmony_ci 14528c2ecf20Sopenharmony_ci list_for_each_entry_safe(page, next, page_list, lru) { 14538c2ecf20Sopenharmony_ci if (page_is_file_lru(page) && !PageDirty(page) && 14548c2ecf20Sopenharmony_ci !__PageMovable(page) && !PageUnevictable(page)) { 14558c2ecf20Sopenharmony_ci ClearPageActive(page); 14568c2ecf20Sopenharmony_ci list_move(&page->lru, &clean_pages); 14578c2ecf20Sopenharmony_ci } 14588c2ecf20Sopenharmony_ci } 14598c2ecf20Sopenharmony_ci 14608c2ecf20Sopenharmony_ci nr_reclaimed = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc, 14618c2ecf20Sopenharmony_ci &stat, true); 14628c2ecf20Sopenharmony_ci list_splice(&clean_pages, page_list); 14638c2ecf20Sopenharmony_ci mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, 14648c2ecf20Sopenharmony_ci -(long)nr_reclaimed); 14658c2ecf20Sopenharmony_ci /* 14668c2ecf20Sopenharmony_ci * Since lazyfree pages are isolated from file LRU from the beginning, 14678c2ecf20Sopenharmony_ci * they will rotate back to anonymous LRU in the end if it failed to 14688c2ecf20Sopenharmony_ci * discard so isolated count will be mismatched. 14698c2ecf20Sopenharmony_ci * Compensate the isolated count for both LRU lists. 14708c2ecf20Sopenharmony_ci */ 14718c2ecf20Sopenharmony_ci mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON, 14728c2ecf20Sopenharmony_ci stat.nr_lazyfree_fail); 14738c2ecf20Sopenharmony_ci mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, 14748c2ecf20Sopenharmony_ci -(long)stat.nr_lazyfree_fail); 14758c2ecf20Sopenharmony_ci return nr_reclaimed; 14768c2ecf20Sopenharmony_ci} 14778c2ecf20Sopenharmony_ci 14788c2ecf20Sopenharmony_ci/* 14798c2ecf20Sopenharmony_ci * Attempt to remove the specified page from its LRU. Only take this page 14808c2ecf20Sopenharmony_ci * if it is of the appropriate PageActive status. Pages which are being 14818c2ecf20Sopenharmony_ci * freed elsewhere are also ignored. 14828c2ecf20Sopenharmony_ci * 14838c2ecf20Sopenharmony_ci * page: page to consider 14848c2ecf20Sopenharmony_ci * mode: one of the LRU isolation modes defined above 14858c2ecf20Sopenharmony_ci * 14868c2ecf20Sopenharmony_ci * returns 0 on success, -ve errno on failure. 14878c2ecf20Sopenharmony_ci */ 14888c2ecf20Sopenharmony_ciint __isolate_lru_page(struct page *page, isolate_mode_t mode) 14898c2ecf20Sopenharmony_ci{ 14908c2ecf20Sopenharmony_ci int ret = -EINVAL; 14918c2ecf20Sopenharmony_ci 14928c2ecf20Sopenharmony_ci /* Only take pages on the LRU. */ 14938c2ecf20Sopenharmony_ci if (!PageLRU(page)) 14948c2ecf20Sopenharmony_ci return ret; 14958c2ecf20Sopenharmony_ci 14968c2ecf20Sopenharmony_ci /* Compaction should not handle unevictable pages but CMA can do so */ 14978c2ecf20Sopenharmony_ci if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) 14988c2ecf20Sopenharmony_ci return ret; 14998c2ecf20Sopenharmony_ci 15008c2ecf20Sopenharmony_ci ret = -EBUSY; 15018c2ecf20Sopenharmony_ci 15028c2ecf20Sopenharmony_ci /* 15038c2ecf20Sopenharmony_ci * To minimise LRU disruption, the caller can indicate that it only 15048c2ecf20Sopenharmony_ci * wants to isolate pages it will be able to operate on without 15058c2ecf20Sopenharmony_ci * blocking - clean pages for the most part. 15068c2ecf20Sopenharmony_ci * 15078c2ecf20Sopenharmony_ci * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 15088c2ecf20Sopenharmony_ci * that it is possible to migrate without blocking 15098c2ecf20Sopenharmony_ci */ 15108c2ecf20Sopenharmony_ci if (mode & ISOLATE_ASYNC_MIGRATE) { 15118c2ecf20Sopenharmony_ci /* All the caller can do on PageWriteback is block */ 15128c2ecf20Sopenharmony_ci if (PageWriteback(page)) 15138c2ecf20Sopenharmony_ci return ret; 15148c2ecf20Sopenharmony_ci 15158c2ecf20Sopenharmony_ci if (PageDirty(page)) { 15168c2ecf20Sopenharmony_ci struct address_space *mapping; 15178c2ecf20Sopenharmony_ci bool migrate_dirty; 15188c2ecf20Sopenharmony_ci 15198c2ecf20Sopenharmony_ci /* 15208c2ecf20Sopenharmony_ci * Only pages without mappings or that have a 15218c2ecf20Sopenharmony_ci * ->migratepage callback are possible to migrate 15228c2ecf20Sopenharmony_ci * without blocking. However, we can be racing with 15238c2ecf20Sopenharmony_ci * truncation so it's necessary to lock the page 15248c2ecf20Sopenharmony_ci * to stabilise the mapping as truncation holds 15258c2ecf20Sopenharmony_ci * the page lock until after the page is removed 15268c2ecf20Sopenharmony_ci * from the page cache. 15278c2ecf20Sopenharmony_ci */ 15288c2ecf20Sopenharmony_ci if (!trylock_page(page)) 15298c2ecf20Sopenharmony_ci return ret; 15308c2ecf20Sopenharmony_ci 15318c2ecf20Sopenharmony_ci mapping = page_mapping(page); 15328c2ecf20Sopenharmony_ci migrate_dirty = !mapping || mapping->a_ops->migratepage; 15338c2ecf20Sopenharmony_ci unlock_page(page); 15348c2ecf20Sopenharmony_ci if (!migrate_dirty) 15358c2ecf20Sopenharmony_ci return ret; 15368c2ecf20Sopenharmony_ci } 15378c2ecf20Sopenharmony_ci } 15388c2ecf20Sopenharmony_ci 15398c2ecf20Sopenharmony_ci if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 15408c2ecf20Sopenharmony_ci return ret; 15418c2ecf20Sopenharmony_ci 15428c2ecf20Sopenharmony_ci if (likely(get_page_unless_zero(page))) { 15438c2ecf20Sopenharmony_ci /* 15448c2ecf20Sopenharmony_ci * Be careful not to clear PageLRU until after we're 15458c2ecf20Sopenharmony_ci * sure the page is not being freed elsewhere -- the 15468c2ecf20Sopenharmony_ci * page release code relies on it. 15478c2ecf20Sopenharmony_ci */ 15488c2ecf20Sopenharmony_ci ClearPageLRU(page); 15498c2ecf20Sopenharmony_ci ret = 0; 15508c2ecf20Sopenharmony_ci } 15518c2ecf20Sopenharmony_ci 15528c2ecf20Sopenharmony_ci return ret; 15538c2ecf20Sopenharmony_ci} 15548c2ecf20Sopenharmony_ci 15558c2ecf20Sopenharmony_ci 15568c2ecf20Sopenharmony_ci/* 15578c2ecf20Sopenharmony_ci * Update LRU sizes after isolating pages. The LRU size updates must 15588c2ecf20Sopenharmony_ci * be complete before mem_cgroup_update_lru_size due to a sanity check. 15598c2ecf20Sopenharmony_ci */ 15608c2ecf20Sopenharmony_cistatic __always_inline void update_lru_sizes(struct lruvec *lruvec, 15618c2ecf20Sopenharmony_ci enum lru_list lru, unsigned long *nr_zone_taken) 15628c2ecf20Sopenharmony_ci{ 15638c2ecf20Sopenharmony_ci int zid; 15648c2ecf20Sopenharmony_ci 15658c2ecf20Sopenharmony_ci for (zid = 0; zid < MAX_NR_ZONES; zid++) { 15668c2ecf20Sopenharmony_ci if (!nr_zone_taken[zid]) 15678c2ecf20Sopenharmony_ci continue; 15688c2ecf20Sopenharmony_ci 15698c2ecf20Sopenharmony_ci update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 15708c2ecf20Sopenharmony_ci } 15718c2ecf20Sopenharmony_ci 15728c2ecf20Sopenharmony_ci} 15738c2ecf20Sopenharmony_ci 15748c2ecf20Sopenharmony_ci/** 15758c2ecf20Sopenharmony_ci * pgdat->lru_lock is heavily contended. Some of the functions that 15768c2ecf20Sopenharmony_ci * shrink the lists perform better by taking out a batch of pages 15778c2ecf20Sopenharmony_ci * and working on them outside the LRU lock. 15788c2ecf20Sopenharmony_ci * 15798c2ecf20Sopenharmony_ci * For pagecache intensive workloads, this function is the hottest 15808c2ecf20Sopenharmony_ci * spot in the kernel (apart from copy_*_user functions). 15818c2ecf20Sopenharmony_ci * 15828c2ecf20Sopenharmony_ci * Appropriate locks must be held before calling this function. 15838c2ecf20Sopenharmony_ci * 15848c2ecf20Sopenharmony_ci * @nr_to_scan: The number of eligible pages to look through on the list. 15858c2ecf20Sopenharmony_ci * @lruvec: The LRU vector to pull pages from. 15868c2ecf20Sopenharmony_ci * @dst: The temp list to put pages on to. 15878c2ecf20Sopenharmony_ci * @nr_scanned: The number of pages that were scanned. 15888c2ecf20Sopenharmony_ci * @sc: The scan_control struct for this reclaim session 15898c2ecf20Sopenharmony_ci * @lru: LRU list id for isolating 15908c2ecf20Sopenharmony_ci * 15918c2ecf20Sopenharmony_ci * returns how many pages were moved onto *@dst. 15928c2ecf20Sopenharmony_ci */ 15938c2ecf20Sopenharmony_ciunsigned long isolate_lru_pages(unsigned long nr_to_scan, 15948c2ecf20Sopenharmony_ci struct lruvec *lruvec, struct list_head *dst, 15958c2ecf20Sopenharmony_ci unsigned long *nr_scanned, struct scan_control *sc, 15968c2ecf20Sopenharmony_ci enum lru_list lru) 15978c2ecf20Sopenharmony_ci{ 15988c2ecf20Sopenharmony_ci struct list_head *src = &lruvec->lists[lru]; 15998c2ecf20Sopenharmony_ci unsigned long nr_taken = 0; 16008c2ecf20Sopenharmony_ci unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; 16018c2ecf20Sopenharmony_ci unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; 16028c2ecf20Sopenharmony_ci unsigned long skipped = 0; 16038c2ecf20Sopenharmony_ci unsigned long scan, total_scan, nr_pages; 16048c2ecf20Sopenharmony_ci LIST_HEAD(pages_skipped); 16058c2ecf20Sopenharmony_ci isolate_mode_t mode = (sc->may_unmap ? 0 : ISOLATE_UNMAPPED); 16068c2ecf20Sopenharmony_ci 16078c2ecf20Sopenharmony_ci total_scan = 0; 16088c2ecf20Sopenharmony_ci scan = 0; 16098c2ecf20Sopenharmony_ci while (scan < nr_to_scan && !list_empty(src)) { 16108c2ecf20Sopenharmony_ci struct page *page; 16118c2ecf20Sopenharmony_ci 16128c2ecf20Sopenharmony_ci page = lru_to_page(src); 16138c2ecf20Sopenharmony_ci prefetchw_prev_lru_page(page, src, flags); 16148c2ecf20Sopenharmony_ci 16158c2ecf20Sopenharmony_ci VM_BUG_ON_PAGE(!PageLRU(page), page); 16168c2ecf20Sopenharmony_ci 16178c2ecf20Sopenharmony_ci nr_pages = compound_nr(page); 16188c2ecf20Sopenharmony_ci total_scan += nr_pages; 16198c2ecf20Sopenharmony_ci 16208c2ecf20Sopenharmony_ci if (page_zonenum(page) > sc->reclaim_idx) { 16218c2ecf20Sopenharmony_ci list_move(&page->lru, &pages_skipped); 16228c2ecf20Sopenharmony_ci nr_skipped[page_zonenum(page)] += nr_pages; 16238c2ecf20Sopenharmony_ci continue; 16248c2ecf20Sopenharmony_ci } 16258c2ecf20Sopenharmony_ci 16268c2ecf20Sopenharmony_ci /* 16278c2ecf20Sopenharmony_ci * Do not count skipped pages because that makes the function 16288c2ecf20Sopenharmony_ci * return with no isolated pages if the LRU mostly contains 16298c2ecf20Sopenharmony_ci * ineligible pages. This causes the VM to not reclaim any 16308c2ecf20Sopenharmony_ci * pages, triggering a premature OOM. 16318c2ecf20Sopenharmony_ci * 16328c2ecf20Sopenharmony_ci * Account all tail pages of THP. This would not cause 16338c2ecf20Sopenharmony_ci * premature OOM since __isolate_lru_page() returns -EBUSY 16348c2ecf20Sopenharmony_ci * only when the page is being freed somewhere else. 16358c2ecf20Sopenharmony_ci */ 16368c2ecf20Sopenharmony_ci scan += nr_pages; 16378c2ecf20Sopenharmony_ci switch (__isolate_lru_page(page, mode)) { 16388c2ecf20Sopenharmony_ci case 0: 16398c2ecf20Sopenharmony_ci nr_taken += nr_pages; 16408c2ecf20Sopenharmony_ci nr_zone_taken[page_zonenum(page)] += nr_pages; 16418c2ecf20Sopenharmony_ci list_move(&page->lru, dst); 16428c2ecf20Sopenharmony_ci break; 16438c2ecf20Sopenharmony_ci 16448c2ecf20Sopenharmony_ci case -EBUSY: 16458c2ecf20Sopenharmony_ci /* else it is being freed elsewhere */ 16468c2ecf20Sopenharmony_ci list_move(&page->lru, src); 16478c2ecf20Sopenharmony_ci continue; 16488c2ecf20Sopenharmony_ci 16498c2ecf20Sopenharmony_ci default: 16508c2ecf20Sopenharmony_ci BUG(); 16518c2ecf20Sopenharmony_ci } 16528c2ecf20Sopenharmony_ci } 16538c2ecf20Sopenharmony_ci 16548c2ecf20Sopenharmony_ci /* 16558c2ecf20Sopenharmony_ci * Splice any skipped pages to the start of the LRU list. Note that 16568c2ecf20Sopenharmony_ci * this disrupts the LRU order when reclaiming for lower zones but 16578c2ecf20Sopenharmony_ci * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX 16588c2ecf20Sopenharmony_ci * scanning would soon rescan the same pages to skip and put the 16598c2ecf20Sopenharmony_ci * system at risk of premature OOM. 16608c2ecf20Sopenharmony_ci */ 16618c2ecf20Sopenharmony_ci if (!list_empty(&pages_skipped)) { 16628c2ecf20Sopenharmony_ci int zid; 16638c2ecf20Sopenharmony_ci 16648c2ecf20Sopenharmony_ci list_splice(&pages_skipped, src); 16658c2ecf20Sopenharmony_ci for (zid = 0; zid < MAX_NR_ZONES; zid++) { 16668c2ecf20Sopenharmony_ci if (!nr_skipped[zid]) 16678c2ecf20Sopenharmony_ci continue; 16688c2ecf20Sopenharmony_ci 16698c2ecf20Sopenharmony_ci __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); 16708c2ecf20Sopenharmony_ci skipped += nr_skipped[zid]; 16718c2ecf20Sopenharmony_ci } 16728c2ecf20Sopenharmony_ci } 16738c2ecf20Sopenharmony_ci *nr_scanned = total_scan; 16748c2ecf20Sopenharmony_ci trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, 16758c2ecf20Sopenharmony_ci total_scan, skipped, nr_taken, mode, lru); 16768c2ecf20Sopenharmony_ci update_lru_sizes(lruvec, lru, nr_zone_taken); 16778c2ecf20Sopenharmony_ci return nr_taken; 16788c2ecf20Sopenharmony_ci} 16798c2ecf20Sopenharmony_ci 16808c2ecf20Sopenharmony_ci/** 16818c2ecf20Sopenharmony_ci * isolate_lru_page - tries to isolate a page from its LRU list 16828c2ecf20Sopenharmony_ci * @page: page to isolate from its LRU list 16838c2ecf20Sopenharmony_ci * 16848c2ecf20Sopenharmony_ci * Isolates a @page from an LRU list, clears PageLRU and adjusts the 16858c2ecf20Sopenharmony_ci * vmstat statistic corresponding to whatever LRU list the page was on. 16868c2ecf20Sopenharmony_ci * 16878c2ecf20Sopenharmony_ci * Returns 0 if the page was removed from an LRU list. 16888c2ecf20Sopenharmony_ci * Returns -EBUSY if the page was not on an LRU list. 16898c2ecf20Sopenharmony_ci * 16908c2ecf20Sopenharmony_ci * The returned page will have PageLRU() cleared. If it was found on 16918c2ecf20Sopenharmony_ci * the active list, it will have PageActive set. If it was found on 16928c2ecf20Sopenharmony_ci * the unevictable list, it will have the PageUnevictable bit set. That flag 16938c2ecf20Sopenharmony_ci * may need to be cleared by the caller before letting the page go. 16948c2ecf20Sopenharmony_ci * 16958c2ecf20Sopenharmony_ci * The vmstat statistic corresponding to the list on which the page was 16968c2ecf20Sopenharmony_ci * found will be decremented. 16978c2ecf20Sopenharmony_ci * 16988c2ecf20Sopenharmony_ci * Restrictions: 16998c2ecf20Sopenharmony_ci * 17008c2ecf20Sopenharmony_ci * (1) Must be called with an elevated refcount on the page. This is a 17018c2ecf20Sopenharmony_ci * fundamental difference from isolate_lru_pages (which is called 17028c2ecf20Sopenharmony_ci * without a stable reference). 17038c2ecf20Sopenharmony_ci * (2) the lru_lock must not be held. 17048c2ecf20Sopenharmony_ci * (3) interrupts must be enabled. 17058c2ecf20Sopenharmony_ci */ 17068c2ecf20Sopenharmony_ciint isolate_lru_page(struct page *page) 17078c2ecf20Sopenharmony_ci{ 17088c2ecf20Sopenharmony_ci int ret = -EBUSY; 17098c2ecf20Sopenharmony_ci 17108c2ecf20Sopenharmony_ci VM_BUG_ON_PAGE(!page_count(page), page); 17118c2ecf20Sopenharmony_ci WARN_RATELIMIT(PageTail(page), "trying to isolate tail page"); 17128c2ecf20Sopenharmony_ci 17138c2ecf20Sopenharmony_ci if (PageLRU(page)) { 17148c2ecf20Sopenharmony_ci pg_data_t *pgdat = page_pgdat(page); 17158c2ecf20Sopenharmony_ci struct lruvec *lruvec; 17168c2ecf20Sopenharmony_ci 17178c2ecf20Sopenharmony_ci spin_lock_irq(&pgdat->lru_lock); 17188c2ecf20Sopenharmony_ci lruvec = mem_cgroup_page_lruvec(page, pgdat); 17198c2ecf20Sopenharmony_ci if (PageLRU(page)) { 17208c2ecf20Sopenharmony_ci int lru = page_lru(page); 17218c2ecf20Sopenharmony_ci get_page(page); 17228c2ecf20Sopenharmony_ci ClearPageLRU(page); 17238c2ecf20Sopenharmony_ci del_page_from_lru_list(page, lruvec, lru); 17248c2ecf20Sopenharmony_ci ret = 0; 17258c2ecf20Sopenharmony_ci } 17268c2ecf20Sopenharmony_ci spin_unlock_irq(&pgdat->lru_lock); 17278c2ecf20Sopenharmony_ci } 17288c2ecf20Sopenharmony_ci return ret; 17298c2ecf20Sopenharmony_ci} 17308c2ecf20Sopenharmony_ci 17318c2ecf20Sopenharmony_ci/* 17328c2ecf20Sopenharmony_ci * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 17338c2ecf20Sopenharmony_ci * then get rescheduled. When there are massive number of tasks doing page 17348c2ecf20Sopenharmony_ci * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 17358c2ecf20Sopenharmony_ci * the LRU list will go small and be scanned faster than necessary, leading to 17368c2ecf20Sopenharmony_ci * unnecessary swapping, thrashing and OOM. 17378c2ecf20Sopenharmony_ci */ 17388c2ecf20Sopenharmony_cistatic int too_many_isolated(struct pglist_data *pgdat, int file, 17398c2ecf20Sopenharmony_ci struct scan_control *sc) 17408c2ecf20Sopenharmony_ci{ 17418c2ecf20Sopenharmony_ci unsigned long inactive, isolated; 17428c2ecf20Sopenharmony_ci 17438c2ecf20Sopenharmony_ci if (current_is_kswapd()) 17448c2ecf20Sopenharmony_ci return 0; 17458c2ecf20Sopenharmony_ci 17468c2ecf20Sopenharmony_ci if (!writeback_throttling_sane(sc)) 17478c2ecf20Sopenharmony_ci return 0; 17488c2ecf20Sopenharmony_ci 17498c2ecf20Sopenharmony_ci if (file) { 17508c2ecf20Sopenharmony_ci inactive = node_page_state(pgdat, NR_INACTIVE_FILE); 17518c2ecf20Sopenharmony_ci isolated = node_page_state(pgdat, NR_ISOLATED_FILE); 17528c2ecf20Sopenharmony_ci } else { 17538c2ecf20Sopenharmony_ci inactive = node_page_state(pgdat, NR_INACTIVE_ANON); 17548c2ecf20Sopenharmony_ci isolated = node_page_state(pgdat, NR_ISOLATED_ANON); 17558c2ecf20Sopenharmony_ci } 17568c2ecf20Sopenharmony_ci 17578c2ecf20Sopenharmony_ci /* 17588c2ecf20Sopenharmony_ci * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 17598c2ecf20Sopenharmony_ci * won't get blocked by normal direct-reclaimers, forming a circular 17608c2ecf20Sopenharmony_ci * deadlock. 17618c2ecf20Sopenharmony_ci */ 17628c2ecf20Sopenharmony_ci if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) 17638c2ecf20Sopenharmony_ci inactive >>= 3; 17648c2ecf20Sopenharmony_ci 17658c2ecf20Sopenharmony_ci return isolated > inactive; 17668c2ecf20Sopenharmony_ci} 17678c2ecf20Sopenharmony_ci 17688c2ecf20Sopenharmony_ci/* 17698c2ecf20Sopenharmony_ci * This moves pages from @list to corresponding LRU list. 17708c2ecf20Sopenharmony_ci * 17718c2ecf20Sopenharmony_ci * We move them the other way if the page is referenced by one or more 17728c2ecf20Sopenharmony_ci * processes, from rmap. 17738c2ecf20Sopenharmony_ci * 17748c2ecf20Sopenharmony_ci * If the pages are mostly unmapped, the processing is fast and it is 17758c2ecf20Sopenharmony_ci * appropriate to hold zone_lru_lock across the whole operation. But if 17768c2ecf20Sopenharmony_ci * the pages are mapped, the processing is slow (page_referenced()) so we 17778c2ecf20Sopenharmony_ci * should drop zone_lru_lock around each page. It's impossible to balance 17788c2ecf20Sopenharmony_ci * this, so instead we remove the pages from the LRU while processing them. 17798c2ecf20Sopenharmony_ci * It is safe to rely on PG_active against the non-LRU pages in here because 17808c2ecf20Sopenharmony_ci * nobody will play with that bit on a non-LRU page. 17818c2ecf20Sopenharmony_ci * 17828c2ecf20Sopenharmony_ci * The downside is that we have to touch page->_refcount against each page. 17838c2ecf20Sopenharmony_ci * But we had to alter page->flags anyway. 17848c2ecf20Sopenharmony_ci * 17858c2ecf20Sopenharmony_ci * Returns the number of pages moved to the given lruvec. 17868c2ecf20Sopenharmony_ci */ 17878c2ecf20Sopenharmony_ci 17888c2ecf20Sopenharmony_ciunsigned move_pages_to_lru(struct lruvec *lruvec, struct list_head *list) 17898c2ecf20Sopenharmony_ci{ 17908c2ecf20Sopenharmony_ci struct pglist_data *pgdat = lruvec_pgdat(lruvec); 17918c2ecf20Sopenharmony_ci int nr_pages, nr_moved = 0; 17928c2ecf20Sopenharmony_ci LIST_HEAD(pages_to_free); 17938c2ecf20Sopenharmony_ci struct page *page; 17948c2ecf20Sopenharmony_ci enum lru_list lru; 17958c2ecf20Sopenharmony_ci#ifdef CONFIG_HYPERHOLD_FILE_LRU 17968c2ecf20Sopenharmony_ci bool prot; 17978c2ecf20Sopenharmony_ci bool file; 17988c2ecf20Sopenharmony_ci#endif 17998c2ecf20Sopenharmony_ci 18008c2ecf20Sopenharmony_ci while (!list_empty(list)) { 18018c2ecf20Sopenharmony_ci page = lru_to_page(list); 18028c2ecf20Sopenharmony_ci VM_BUG_ON_PAGE(PageLRU(page), page); 18038c2ecf20Sopenharmony_ci if (unlikely(!page_evictable(page))) { 18048c2ecf20Sopenharmony_ci list_del(&page->lru); 18058c2ecf20Sopenharmony_ci spin_unlock_irq(&pgdat->lru_lock); 18068c2ecf20Sopenharmony_ci putback_lru_page(page); 18078c2ecf20Sopenharmony_ci spin_lock_irq(&pgdat->lru_lock); 18088c2ecf20Sopenharmony_ci continue; 18098c2ecf20Sopenharmony_ci } 18108c2ecf20Sopenharmony_ci lruvec = mem_cgroup_page_lruvec(page, pgdat); 18118c2ecf20Sopenharmony_ci 18128c2ecf20Sopenharmony_ci SetPageLRU(page); 18138c2ecf20Sopenharmony_ci lru = page_lru(page); 18148c2ecf20Sopenharmony_ci 18158c2ecf20Sopenharmony_ci nr_pages = thp_nr_pages(page); 18168c2ecf20Sopenharmony_ci update_lru_size(lruvec, lru, page_zonenum(page), nr_pages); 18178c2ecf20Sopenharmony_ci list_move(&page->lru, &lruvec->lists[lru]); 18188c2ecf20Sopenharmony_ci 18198c2ecf20Sopenharmony_ci if (put_page_testzero(page)) { 18208c2ecf20Sopenharmony_ci __ClearPageLRU(page); 18218c2ecf20Sopenharmony_ci __ClearPageActive(page); 18228c2ecf20Sopenharmony_ci del_page_from_lru_list(page, lruvec, lru); 18238c2ecf20Sopenharmony_ci 18248c2ecf20Sopenharmony_ci if (unlikely(PageCompound(page))) { 18258c2ecf20Sopenharmony_ci spin_unlock_irq(&pgdat->lru_lock); 18268c2ecf20Sopenharmony_ci destroy_compound_page(page); 18278c2ecf20Sopenharmony_ci spin_lock_irq(&pgdat->lru_lock); 18288c2ecf20Sopenharmony_ci } else 18298c2ecf20Sopenharmony_ci list_add(&page->lru, &pages_to_free); 18308c2ecf20Sopenharmony_ci } else { 18318c2ecf20Sopenharmony_ci nr_moved += nr_pages; 18328c2ecf20Sopenharmony_ci#ifdef CONFIG_HYPERHOLD_FILE_LRU 18338c2ecf20Sopenharmony_ci if (PageActive(page)) { 18348c2ecf20Sopenharmony_ci prot = is_prot_page(page); 18358c2ecf20Sopenharmony_ci file = page_is_file_lru(page); 18368c2ecf20Sopenharmony_ci if (!prot && file) { 18378c2ecf20Sopenharmony_ci lruvec = node_lruvec(pgdat); 18388c2ecf20Sopenharmony_ci workingset_age_nonresident(lruvec, 18398c2ecf20Sopenharmony_ci nr_pages); 18408c2ecf20Sopenharmony_ci } else { 18418c2ecf20Sopenharmony_ci workingset_age_nonresident(lruvec, 18428c2ecf20Sopenharmony_ci nr_pages); 18438c2ecf20Sopenharmony_ci } 18448c2ecf20Sopenharmony_ci } 18458c2ecf20Sopenharmony_ci#else 18468c2ecf20Sopenharmony_ci if (PageActive(page)) 18478c2ecf20Sopenharmony_ci workingset_age_nonresident(lruvec, nr_pages); 18488c2ecf20Sopenharmony_ci#endif 18498c2ecf20Sopenharmony_ci } 18508c2ecf20Sopenharmony_ci } 18518c2ecf20Sopenharmony_ci 18528c2ecf20Sopenharmony_ci /* 18538c2ecf20Sopenharmony_ci * To save our caller's stack, now use input list for pages to free. 18548c2ecf20Sopenharmony_ci */ 18558c2ecf20Sopenharmony_ci list_splice(&pages_to_free, list); 18568c2ecf20Sopenharmony_ci 18578c2ecf20Sopenharmony_ci return nr_moved; 18588c2ecf20Sopenharmony_ci} 18598c2ecf20Sopenharmony_ci 18608c2ecf20Sopenharmony_ci/* 18618c2ecf20Sopenharmony_ci * If a kernel thread (such as nfsd for loop-back mounts) services 18628c2ecf20Sopenharmony_ci * a backing device by writing to the page cache it sets PF_LOCAL_THROTTLE. 18638c2ecf20Sopenharmony_ci * In that case we should only throttle if the backing device it is 18648c2ecf20Sopenharmony_ci * writing to is congested. In other cases it is safe to throttle. 18658c2ecf20Sopenharmony_ci */ 18668c2ecf20Sopenharmony_ciint current_may_throttle(void) 18678c2ecf20Sopenharmony_ci{ 18688c2ecf20Sopenharmony_ci return !(current->flags & PF_LOCAL_THROTTLE) || 18698c2ecf20Sopenharmony_ci current->backing_dev_info == NULL || 18708c2ecf20Sopenharmony_ci bdi_write_congested(current->backing_dev_info); 18718c2ecf20Sopenharmony_ci} 18728c2ecf20Sopenharmony_ci 18738c2ecf20Sopenharmony_ci/* 18748c2ecf20Sopenharmony_ci * shrink_inactive_list() is a helper for shrink_node(). It returns the number 18758c2ecf20Sopenharmony_ci * of reclaimed pages 18768c2ecf20Sopenharmony_ci */ 18778c2ecf20Sopenharmony_ciunsigned long shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 18788c2ecf20Sopenharmony_ci struct scan_control *sc, enum lru_list lru) 18798c2ecf20Sopenharmony_ci{ 18808c2ecf20Sopenharmony_ci LIST_HEAD(page_list); 18818c2ecf20Sopenharmony_ci unsigned long nr_scanned; 18828c2ecf20Sopenharmony_ci unsigned int nr_reclaimed = 0; 18838c2ecf20Sopenharmony_ci unsigned long nr_taken; 18848c2ecf20Sopenharmony_ci struct reclaim_stat stat; 18858c2ecf20Sopenharmony_ci bool file = is_file_lru(lru); 18868c2ecf20Sopenharmony_ci enum vm_event_item item; 18878c2ecf20Sopenharmony_ci struct pglist_data *pgdat = lruvec_pgdat(lruvec); 18888c2ecf20Sopenharmony_ci bool stalled = false; 18898c2ecf20Sopenharmony_ci 18908c2ecf20Sopenharmony_ci while (unlikely(too_many_isolated(pgdat, file, sc))) { 18918c2ecf20Sopenharmony_ci if (stalled) 18928c2ecf20Sopenharmony_ci return 0; 18938c2ecf20Sopenharmony_ci 18948c2ecf20Sopenharmony_ci#ifdef CONFIG_HYPERHOLD_FILE_LRU 18958c2ecf20Sopenharmony_ci sc->isolate_count++; 18968c2ecf20Sopenharmony_ci#endif 18978c2ecf20Sopenharmony_ci /* wait a bit for the reclaimer. */ 18988c2ecf20Sopenharmony_ci msleep(100); 18998c2ecf20Sopenharmony_ci stalled = true; 19008c2ecf20Sopenharmony_ci 19018c2ecf20Sopenharmony_ci /* We are about to die and free our memory. Return now. */ 19028c2ecf20Sopenharmony_ci if (fatal_signal_pending(current)) 19038c2ecf20Sopenharmony_ci return SWAP_CLUSTER_MAX; 19048c2ecf20Sopenharmony_ci } 19058c2ecf20Sopenharmony_ci 19068c2ecf20Sopenharmony_ci lru_add_drain(); 19078c2ecf20Sopenharmony_ci 19088c2ecf20Sopenharmony_ci spin_lock_irq(&pgdat->lru_lock); 19098c2ecf20Sopenharmony_ci 19108c2ecf20Sopenharmony_ci nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 19118c2ecf20Sopenharmony_ci &nr_scanned, sc, lru); 19128c2ecf20Sopenharmony_ci 19138c2ecf20Sopenharmony_ci __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 19148c2ecf20Sopenharmony_ci item = current_is_kswapd() ? PGSCAN_KSWAPD : PGSCAN_DIRECT; 19158c2ecf20Sopenharmony_ci if (!cgroup_reclaim(sc)) 19168c2ecf20Sopenharmony_ci __count_vm_events(item, nr_scanned); 19178c2ecf20Sopenharmony_ci __count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned); 19188c2ecf20Sopenharmony_ci __count_vm_events(PGSCAN_ANON + file, nr_scanned); 19198c2ecf20Sopenharmony_ci 19208c2ecf20Sopenharmony_ci spin_unlock_irq(&pgdat->lru_lock); 19218c2ecf20Sopenharmony_ci 19228c2ecf20Sopenharmony_ci if (nr_taken == 0) 19238c2ecf20Sopenharmony_ci return 0; 19248c2ecf20Sopenharmony_ci 19258c2ecf20Sopenharmony_ci nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, &stat, false); 19268c2ecf20Sopenharmony_ci 19278c2ecf20Sopenharmony_ci spin_lock_irq(&pgdat->lru_lock); 19288c2ecf20Sopenharmony_ci 19298c2ecf20Sopenharmony_ci move_pages_to_lru(lruvec, &page_list); 19308c2ecf20Sopenharmony_ci 19318c2ecf20Sopenharmony_ci __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 19328c2ecf20Sopenharmony_ci#ifdef CONFIG_HYPERHOLD_FILE_LRU 19338c2ecf20Sopenharmony_ci if (file) 19348c2ecf20Sopenharmony_ci lru_note_cost(node_lruvec(pgdat), file, stat.nr_pageout); 19358c2ecf20Sopenharmony_ci else 19368c2ecf20Sopenharmony_ci lru_note_cost(lruvec, file, stat.nr_pageout); 19378c2ecf20Sopenharmony_ci#else 19388c2ecf20Sopenharmony_ci lru_note_cost(lruvec, file, stat.nr_pageout); 19398c2ecf20Sopenharmony_ci 19408c2ecf20Sopenharmony_ci#endif 19418c2ecf20Sopenharmony_ci 19428c2ecf20Sopenharmony_ci item = current_is_kswapd() ? PGSTEAL_KSWAPD : PGSTEAL_DIRECT; 19438c2ecf20Sopenharmony_ci if (!cgroup_reclaim(sc)) 19448c2ecf20Sopenharmony_ci __count_vm_events(item, nr_reclaimed); 19458c2ecf20Sopenharmony_ci __count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed); 19468c2ecf20Sopenharmony_ci __count_vm_events(PGSTEAL_ANON + file, nr_reclaimed); 19478c2ecf20Sopenharmony_ci 19488c2ecf20Sopenharmony_ci spin_unlock_irq(&pgdat->lru_lock); 19498c2ecf20Sopenharmony_ci 19508c2ecf20Sopenharmony_ci mem_cgroup_uncharge_list(&page_list); 19518c2ecf20Sopenharmony_ci free_unref_page_list(&page_list); 19528c2ecf20Sopenharmony_ci 19538c2ecf20Sopenharmony_ci /* 19548c2ecf20Sopenharmony_ci * If dirty pages are scanned that are not queued for IO, it 19558c2ecf20Sopenharmony_ci * implies that flushers are not doing their job. This can 19568c2ecf20Sopenharmony_ci * happen when memory pressure pushes dirty pages to the end of 19578c2ecf20Sopenharmony_ci * the LRU before the dirty limits are breached and the dirty 19588c2ecf20Sopenharmony_ci * data has expired. It can also happen when the proportion of 19598c2ecf20Sopenharmony_ci * dirty pages grows not through writes but through memory 19608c2ecf20Sopenharmony_ci * pressure reclaiming all the clean cache. And in some cases, 19618c2ecf20Sopenharmony_ci * the flushers simply cannot keep up with the allocation 19628c2ecf20Sopenharmony_ci * rate. Nudge the flusher threads in case they are asleep. 19638c2ecf20Sopenharmony_ci */ 19648c2ecf20Sopenharmony_ci if (stat.nr_unqueued_dirty == nr_taken) 19658c2ecf20Sopenharmony_ci wakeup_flusher_threads(WB_REASON_VMSCAN); 19668c2ecf20Sopenharmony_ci 19678c2ecf20Sopenharmony_ci sc->nr.dirty += stat.nr_dirty; 19688c2ecf20Sopenharmony_ci sc->nr.congested += stat.nr_congested; 19698c2ecf20Sopenharmony_ci sc->nr.unqueued_dirty += stat.nr_unqueued_dirty; 19708c2ecf20Sopenharmony_ci sc->nr.writeback += stat.nr_writeback; 19718c2ecf20Sopenharmony_ci sc->nr.immediate += stat.nr_immediate; 19728c2ecf20Sopenharmony_ci sc->nr.taken += nr_taken; 19738c2ecf20Sopenharmony_ci if (file) 19748c2ecf20Sopenharmony_ci sc->nr.file_taken += nr_taken; 19758c2ecf20Sopenharmony_ci 19768c2ecf20Sopenharmony_ci trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, 19778c2ecf20Sopenharmony_ci nr_scanned, nr_reclaimed, &stat, sc->priority, file); 19788c2ecf20Sopenharmony_ci return nr_reclaimed; 19798c2ecf20Sopenharmony_ci} 19808c2ecf20Sopenharmony_ci 19818c2ecf20Sopenharmony_civoid shrink_active_list(unsigned long nr_to_scan, 19828c2ecf20Sopenharmony_ci struct lruvec *lruvec, 19838c2ecf20Sopenharmony_ci struct scan_control *sc, 19848c2ecf20Sopenharmony_ci enum lru_list lru) 19858c2ecf20Sopenharmony_ci{ 19868c2ecf20Sopenharmony_ci unsigned long nr_taken; 19878c2ecf20Sopenharmony_ci unsigned long nr_scanned; 19888c2ecf20Sopenharmony_ci unsigned long vm_flags; 19898c2ecf20Sopenharmony_ci LIST_HEAD(l_hold); /* The pages which were snipped off */ 19908c2ecf20Sopenharmony_ci LIST_HEAD(l_active); 19918c2ecf20Sopenharmony_ci LIST_HEAD(l_inactive); 19928c2ecf20Sopenharmony_ci struct page *page; 19938c2ecf20Sopenharmony_ci unsigned nr_deactivate, nr_activate; 19948c2ecf20Sopenharmony_ci unsigned nr_rotated = 0; 19958c2ecf20Sopenharmony_ci int file = is_file_lru(lru); 19968c2ecf20Sopenharmony_ci struct pglist_data *pgdat = lruvec_pgdat(lruvec); 19978c2ecf20Sopenharmony_ci 19988c2ecf20Sopenharmony_ci lru_add_drain(); 19998c2ecf20Sopenharmony_ci 20008c2ecf20Sopenharmony_ci spin_lock_irq(&pgdat->lru_lock); 20018c2ecf20Sopenharmony_ci 20028c2ecf20Sopenharmony_ci nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 20038c2ecf20Sopenharmony_ci &nr_scanned, sc, lru); 20048c2ecf20Sopenharmony_ci 20058c2ecf20Sopenharmony_ci __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 20068c2ecf20Sopenharmony_ci 20078c2ecf20Sopenharmony_ci if (!cgroup_reclaim(sc)) 20088c2ecf20Sopenharmony_ci __count_vm_events(PGREFILL, nr_scanned); 20098c2ecf20Sopenharmony_ci __count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned); 20108c2ecf20Sopenharmony_ci 20118c2ecf20Sopenharmony_ci spin_unlock_irq(&pgdat->lru_lock); 20128c2ecf20Sopenharmony_ci 20138c2ecf20Sopenharmony_ci while (!list_empty(&l_hold)) { 20148c2ecf20Sopenharmony_ci cond_resched(); 20158c2ecf20Sopenharmony_ci page = lru_to_page(&l_hold); 20168c2ecf20Sopenharmony_ci list_del(&page->lru); 20178c2ecf20Sopenharmony_ci 20188c2ecf20Sopenharmony_ci if (unlikely(!page_evictable(page))) { 20198c2ecf20Sopenharmony_ci putback_lru_page(page); 20208c2ecf20Sopenharmony_ci continue; 20218c2ecf20Sopenharmony_ci } 20228c2ecf20Sopenharmony_ci 20238c2ecf20Sopenharmony_ci if (unlikely(buffer_heads_over_limit)) { 20248c2ecf20Sopenharmony_ci if (page_has_private(page) && trylock_page(page)) { 20258c2ecf20Sopenharmony_ci if (page_has_private(page)) 20268c2ecf20Sopenharmony_ci try_to_release_page(page, 0); 20278c2ecf20Sopenharmony_ci unlock_page(page); 20288c2ecf20Sopenharmony_ci } 20298c2ecf20Sopenharmony_ci } 20308c2ecf20Sopenharmony_ci 20318c2ecf20Sopenharmony_ci if (page_referenced(page, 0, sc->target_mem_cgroup, 20328c2ecf20Sopenharmony_ci &vm_flags)) { 20338c2ecf20Sopenharmony_ci /* 20348c2ecf20Sopenharmony_ci * Identify referenced, file-backed active pages and 20358c2ecf20Sopenharmony_ci * give them one more trip around the active list. So 20368c2ecf20Sopenharmony_ci * that executable code get better chances to stay in 20378c2ecf20Sopenharmony_ci * memory under moderate memory pressure. Anon pages 20388c2ecf20Sopenharmony_ci * are not likely to be evicted by use-once streaming 20398c2ecf20Sopenharmony_ci * IO, plus JVM can create lots of anon VM_EXEC pages, 20408c2ecf20Sopenharmony_ci * so we ignore them here. 20418c2ecf20Sopenharmony_ci */ 20428c2ecf20Sopenharmony_ci if ((vm_flags & VM_EXEC) && page_is_file_lru(page)) { 20438c2ecf20Sopenharmony_ci nr_rotated += thp_nr_pages(page); 20448c2ecf20Sopenharmony_ci list_add(&page->lru, &l_active); 20458c2ecf20Sopenharmony_ci continue; 20468c2ecf20Sopenharmony_ci } 20478c2ecf20Sopenharmony_ci } 20488c2ecf20Sopenharmony_ci 20498c2ecf20Sopenharmony_ci ClearPageActive(page); /* we are de-activating */ 20508c2ecf20Sopenharmony_ci SetPageWorkingset(page); 20518c2ecf20Sopenharmony_ci list_add(&page->lru, &l_inactive); 20528c2ecf20Sopenharmony_ci } 20538c2ecf20Sopenharmony_ci 20548c2ecf20Sopenharmony_ci /* 20558c2ecf20Sopenharmony_ci * Move pages back to the lru list. 20568c2ecf20Sopenharmony_ci */ 20578c2ecf20Sopenharmony_ci spin_lock_irq(&pgdat->lru_lock); 20588c2ecf20Sopenharmony_ci 20598c2ecf20Sopenharmony_ci nr_activate = move_pages_to_lru(lruvec, &l_active); 20608c2ecf20Sopenharmony_ci nr_deactivate = move_pages_to_lru(lruvec, &l_inactive); 20618c2ecf20Sopenharmony_ci /* Keep all free pages in l_active list */ 20628c2ecf20Sopenharmony_ci list_splice(&l_inactive, &l_active); 20638c2ecf20Sopenharmony_ci 20648c2ecf20Sopenharmony_ci __count_vm_events(PGDEACTIVATE, nr_deactivate); 20658c2ecf20Sopenharmony_ci __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate); 20668c2ecf20Sopenharmony_ci 20678c2ecf20Sopenharmony_ci __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 20688c2ecf20Sopenharmony_ci spin_unlock_irq(&pgdat->lru_lock); 20698c2ecf20Sopenharmony_ci 20708c2ecf20Sopenharmony_ci mem_cgroup_uncharge_list(&l_active); 20718c2ecf20Sopenharmony_ci free_unref_page_list(&l_active); 20728c2ecf20Sopenharmony_ci trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate, 20738c2ecf20Sopenharmony_ci nr_deactivate, nr_rotated, sc->priority, file); 20748c2ecf20Sopenharmony_ci} 20758c2ecf20Sopenharmony_ci 20768c2ecf20Sopenharmony_ciunsigned long reclaim_pages(struct list_head *page_list) 20778c2ecf20Sopenharmony_ci{ 20788c2ecf20Sopenharmony_ci int nid = NUMA_NO_NODE; 20798c2ecf20Sopenharmony_ci unsigned int nr_reclaimed = 0; 20808c2ecf20Sopenharmony_ci LIST_HEAD(node_page_list); 20818c2ecf20Sopenharmony_ci struct reclaim_stat dummy_stat; 20828c2ecf20Sopenharmony_ci struct page *page; 20838c2ecf20Sopenharmony_ci struct scan_control sc = { 20848c2ecf20Sopenharmony_ci .gfp_mask = GFP_KERNEL, 20858c2ecf20Sopenharmony_ci .priority = DEF_PRIORITY, 20868c2ecf20Sopenharmony_ci .may_writepage = 1, 20878c2ecf20Sopenharmony_ci .may_unmap = 1, 20888c2ecf20Sopenharmony_ci .may_swap = 1, 20898c2ecf20Sopenharmony_ci }; 20908c2ecf20Sopenharmony_ci 20918c2ecf20Sopenharmony_ci while (!list_empty(page_list)) { 20928c2ecf20Sopenharmony_ci page = lru_to_page(page_list); 20938c2ecf20Sopenharmony_ci if (nid == NUMA_NO_NODE) { 20948c2ecf20Sopenharmony_ci nid = page_to_nid(page); 20958c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&node_page_list); 20968c2ecf20Sopenharmony_ci } 20978c2ecf20Sopenharmony_ci 20988c2ecf20Sopenharmony_ci if (nid == page_to_nid(page)) { 20998c2ecf20Sopenharmony_ci ClearPageActive(page); 21008c2ecf20Sopenharmony_ci list_move(&page->lru, &node_page_list); 21018c2ecf20Sopenharmony_ci continue; 21028c2ecf20Sopenharmony_ci } 21038c2ecf20Sopenharmony_ci 21048c2ecf20Sopenharmony_ci nr_reclaimed += shrink_page_list(&node_page_list, 21058c2ecf20Sopenharmony_ci NODE_DATA(nid), 21068c2ecf20Sopenharmony_ci &sc, &dummy_stat, false); 21078c2ecf20Sopenharmony_ci while (!list_empty(&node_page_list)) { 21088c2ecf20Sopenharmony_ci page = lru_to_page(&node_page_list); 21098c2ecf20Sopenharmony_ci list_del(&page->lru); 21108c2ecf20Sopenharmony_ci putback_lru_page(page); 21118c2ecf20Sopenharmony_ci } 21128c2ecf20Sopenharmony_ci 21138c2ecf20Sopenharmony_ci nid = NUMA_NO_NODE; 21148c2ecf20Sopenharmony_ci } 21158c2ecf20Sopenharmony_ci 21168c2ecf20Sopenharmony_ci if (!list_empty(&node_page_list)) { 21178c2ecf20Sopenharmony_ci nr_reclaimed += shrink_page_list(&node_page_list, 21188c2ecf20Sopenharmony_ci NODE_DATA(nid), 21198c2ecf20Sopenharmony_ci &sc, &dummy_stat, false); 21208c2ecf20Sopenharmony_ci while (!list_empty(&node_page_list)) { 21218c2ecf20Sopenharmony_ci page = lru_to_page(&node_page_list); 21228c2ecf20Sopenharmony_ci list_del(&page->lru); 21238c2ecf20Sopenharmony_ci putback_lru_page(page); 21248c2ecf20Sopenharmony_ci } 21258c2ecf20Sopenharmony_ci } 21268c2ecf20Sopenharmony_ci 21278c2ecf20Sopenharmony_ci return nr_reclaimed; 21288c2ecf20Sopenharmony_ci} 21298c2ecf20Sopenharmony_ci 21308c2ecf20Sopenharmony_ciunsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 21318c2ecf20Sopenharmony_ci struct lruvec *lruvec, struct scan_control *sc) 21328c2ecf20Sopenharmony_ci{ 21338c2ecf20Sopenharmony_ci#ifdef CONFIG_RECLAIM_ACCT 21348c2ecf20Sopenharmony_ci unsigned long nr_reclaimed; 21358c2ecf20Sopenharmony_ci unsigned int stub; 21368c2ecf20Sopenharmony_ci 21378c2ecf20Sopenharmony_ci stub = is_file_lru(lru) ? RA_SHRINKFILE : RA_SHRINKANON; 21388c2ecf20Sopenharmony_ci reclaimacct_substage_start(stub); 21398c2ecf20Sopenharmony_ci#endif 21408c2ecf20Sopenharmony_ci if (is_active_lru(lru)) { 21418c2ecf20Sopenharmony_ci if (sc->may_deactivate & (1 << is_file_lru(lru))) 21428c2ecf20Sopenharmony_ci shrink_active_list(nr_to_scan, lruvec, sc, lru); 21438c2ecf20Sopenharmony_ci else 21448c2ecf20Sopenharmony_ci sc->skipped_deactivate = 1; 21458c2ecf20Sopenharmony_ci#ifdef CONFIG_RECLAIM_ACCT 21468c2ecf20Sopenharmony_ci reclaimacct_substage_end(stub, 0, NULL); 21478c2ecf20Sopenharmony_ci#endif 21488c2ecf20Sopenharmony_ci return 0; 21498c2ecf20Sopenharmony_ci } 21508c2ecf20Sopenharmony_ci 21518c2ecf20Sopenharmony_ci#ifdef CONFIG_RECLAIM_ACCT 21528c2ecf20Sopenharmony_ci nr_reclaimed = shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 21538c2ecf20Sopenharmony_ci reclaimacct_substage_end(stub, nr_reclaimed, NULL); 21548c2ecf20Sopenharmony_ci return nr_reclaimed; 21558c2ecf20Sopenharmony_ci#else 21568c2ecf20Sopenharmony_ci return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 21578c2ecf20Sopenharmony_ci#endif 21588c2ecf20Sopenharmony_ci} 21598c2ecf20Sopenharmony_ci 21608c2ecf20Sopenharmony_ci/* 21618c2ecf20Sopenharmony_ci * The inactive anon list should be small enough that the VM never has 21628c2ecf20Sopenharmony_ci * to do too much work. 21638c2ecf20Sopenharmony_ci * 21648c2ecf20Sopenharmony_ci * The inactive file list should be small enough to leave most memory 21658c2ecf20Sopenharmony_ci * to the established workingset on the scan-resistant active list, 21668c2ecf20Sopenharmony_ci * but large enough to avoid thrashing the aggregate readahead window. 21678c2ecf20Sopenharmony_ci * 21688c2ecf20Sopenharmony_ci * Both inactive lists should also be large enough that each inactive 21698c2ecf20Sopenharmony_ci * page has a chance to be referenced again before it is reclaimed. 21708c2ecf20Sopenharmony_ci * 21718c2ecf20Sopenharmony_ci * If that fails and refaulting is observed, the inactive list grows. 21728c2ecf20Sopenharmony_ci * 21738c2ecf20Sopenharmony_ci * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages 21748c2ecf20Sopenharmony_ci * on this LRU, maintained by the pageout code. An inactive_ratio 21758c2ecf20Sopenharmony_ci * of 3 means 3:1 or 25% of the pages are kept on the inactive list. 21768c2ecf20Sopenharmony_ci * 21778c2ecf20Sopenharmony_ci * total target max 21788c2ecf20Sopenharmony_ci * memory ratio inactive 21798c2ecf20Sopenharmony_ci * ------------------------------------- 21808c2ecf20Sopenharmony_ci * 10MB 1 5MB 21818c2ecf20Sopenharmony_ci * 100MB 1 50MB 21828c2ecf20Sopenharmony_ci * 1GB 3 250MB 21838c2ecf20Sopenharmony_ci * 10GB 10 0.9GB 21848c2ecf20Sopenharmony_ci * 100GB 31 3GB 21858c2ecf20Sopenharmony_ci * 1TB 101 10GB 21868c2ecf20Sopenharmony_ci * 10TB 320 32GB 21878c2ecf20Sopenharmony_ci */ 21888c2ecf20Sopenharmony_cibool inactive_is_low(struct lruvec *lruvec, enum lru_list inactive_lru) 21898c2ecf20Sopenharmony_ci{ 21908c2ecf20Sopenharmony_ci enum lru_list active_lru = inactive_lru + LRU_ACTIVE; 21918c2ecf20Sopenharmony_ci unsigned long inactive, active; 21928c2ecf20Sopenharmony_ci unsigned long inactive_ratio; 21938c2ecf20Sopenharmony_ci unsigned long gb; 21948c2ecf20Sopenharmony_ci 21958c2ecf20Sopenharmony_ci inactive = lruvec_page_state(lruvec, NR_LRU_BASE + inactive_lru); 21968c2ecf20Sopenharmony_ci active = lruvec_page_state(lruvec, NR_LRU_BASE + active_lru); 21978c2ecf20Sopenharmony_ci 21988c2ecf20Sopenharmony_ci gb = (inactive + active) >> (30 - PAGE_SHIFT); 21998c2ecf20Sopenharmony_ci if (gb) 22008c2ecf20Sopenharmony_ci inactive_ratio = int_sqrt(10 * gb); 22018c2ecf20Sopenharmony_ci else 22028c2ecf20Sopenharmony_ci inactive_ratio = 1; 22038c2ecf20Sopenharmony_ci 22048c2ecf20Sopenharmony_ci return inactive * inactive_ratio < active; 22058c2ecf20Sopenharmony_ci} 22068c2ecf20Sopenharmony_ci 22078c2ecf20Sopenharmony_ci/* 22088c2ecf20Sopenharmony_ci * Determine how aggressively the anon and file LRU lists should be 22098c2ecf20Sopenharmony_ci * scanned. The relative value of each set of LRU lists is determined 22108c2ecf20Sopenharmony_ci * by looking at the fraction of the pages scanned we did rotate back 22118c2ecf20Sopenharmony_ci * onto the active list instead of evict. 22128c2ecf20Sopenharmony_ci * 22138c2ecf20Sopenharmony_ci * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 22148c2ecf20Sopenharmony_ci * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 22158c2ecf20Sopenharmony_ci */ 22168c2ecf20Sopenharmony_ci#ifndef CONFIG_HYPERHOLD_FILE_LRU 22178c2ecf20Sopenharmony_cistatic void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, 22188c2ecf20Sopenharmony_ci unsigned long *nr) 22198c2ecf20Sopenharmony_ci{ 22208c2ecf20Sopenharmony_ci struct mem_cgroup *memcg = lruvec_memcg(lruvec); 22218c2ecf20Sopenharmony_ci unsigned long anon_cost, file_cost, total_cost; 22228c2ecf20Sopenharmony_ci int swappiness = mem_cgroup_swappiness(memcg); 22238c2ecf20Sopenharmony_ci u64 fraction[ANON_AND_FILE]; 22248c2ecf20Sopenharmony_ci u64 denominator = 0; /* gcc */ 22258c2ecf20Sopenharmony_ci enum scan_balance scan_balance; 22268c2ecf20Sopenharmony_ci unsigned long ap, fp; 22278c2ecf20Sopenharmony_ci enum lru_list lru; 22288c2ecf20Sopenharmony_ci 22298c2ecf20Sopenharmony_ci /* If we have no swap space, do not bother scanning anon pages. */ 22308c2ecf20Sopenharmony_ci if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) { 22318c2ecf20Sopenharmony_ci scan_balance = SCAN_FILE; 22328c2ecf20Sopenharmony_ci goto out; 22338c2ecf20Sopenharmony_ci } 22348c2ecf20Sopenharmony_ci 22358c2ecf20Sopenharmony_ci /* 22368c2ecf20Sopenharmony_ci * Global reclaim will swap to prevent OOM even with no 22378c2ecf20Sopenharmony_ci * swappiness, but memcg users want to use this knob to 22388c2ecf20Sopenharmony_ci * disable swapping for individual groups completely when 22398c2ecf20Sopenharmony_ci * using the memory controller's swap limit feature would be 22408c2ecf20Sopenharmony_ci * too expensive. 22418c2ecf20Sopenharmony_ci */ 22428c2ecf20Sopenharmony_ci if (cgroup_reclaim(sc) && !swappiness) { 22438c2ecf20Sopenharmony_ci scan_balance = SCAN_FILE; 22448c2ecf20Sopenharmony_ci goto out; 22458c2ecf20Sopenharmony_ci } 22468c2ecf20Sopenharmony_ci 22478c2ecf20Sopenharmony_ci /* 22488c2ecf20Sopenharmony_ci * Do not apply any pressure balancing cleverness when the 22498c2ecf20Sopenharmony_ci * system is close to OOM, scan both anon and file equally 22508c2ecf20Sopenharmony_ci * (unless the swappiness setting disagrees with swapping). 22518c2ecf20Sopenharmony_ci */ 22528c2ecf20Sopenharmony_ci if (!sc->priority && swappiness) { 22538c2ecf20Sopenharmony_ci scan_balance = SCAN_EQUAL; 22548c2ecf20Sopenharmony_ci goto out; 22558c2ecf20Sopenharmony_ci } 22568c2ecf20Sopenharmony_ci 22578c2ecf20Sopenharmony_ci /* 22588c2ecf20Sopenharmony_ci * If the system is almost out of file pages, force-scan anon. 22598c2ecf20Sopenharmony_ci */ 22608c2ecf20Sopenharmony_ci if (sc->file_is_tiny) { 22618c2ecf20Sopenharmony_ci scan_balance = SCAN_ANON; 22628c2ecf20Sopenharmony_ci goto out; 22638c2ecf20Sopenharmony_ci } 22648c2ecf20Sopenharmony_ci 22658c2ecf20Sopenharmony_ci /* 22668c2ecf20Sopenharmony_ci * If there is enough inactive page cache, we do not reclaim 22678c2ecf20Sopenharmony_ci * anything from the anonymous working right now. 22688c2ecf20Sopenharmony_ci */ 22698c2ecf20Sopenharmony_ci if (sc->cache_trim_mode) { 22708c2ecf20Sopenharmony_ci scan_balance = SCAN_FILE; 22718c2ecf20Sopenharmony_ci goto out; 22728c2ecf20Sopenharmony_ci } 22738c2ecf20Sopenharmony_ci 22748c2ecf20Sopenharmony_ci scan_balance = SCAN_FRACT; 22758c2ecf20Sopenharmony_ci /* 22768c2ecf20Sopenharmony_ci * Calculate the pressure balance between anon and file pages. 22778c2ecf20Sopenharmony_ci * 22788c2ecf20Sopenharmony_ci * The amount of pressure we put on each LRU is inversely 22798c2ecf20Sopenharmony_ci * proportional to the cost of reclaiming each list, as 22808c2ecf20Sopenharmony_ci * determined by the share of pages that are refaulting, times 22818c2ecf20Sopenharmony_ci * the relative IO cost of bringing back a swapped out 22828c2ecf20Sopenharmony_ci * anonymous page vs reloading a filesystem page (swappiness). 22838c2ecf20Sopenharmony_ci * 22848c2ecf20Sopenharmony_ci * Although we limit that influence to ensure no list gets 22858c2ecf20Sopenharmony_ci * left behind completely: at least a third of the pressure is 22868c2ecf20Sopenharmony_ci * applied, before swappiness. 22878c2ecf20Sopenharmony_ci * 22888c2ecf20Sopenharmony_ci * With swappiness at 100, anon and file have equal IO cost. 22898c2ecf20Sopenharmony_ci */ 22908c2ecf20Sopenharmony_ci total_cost = sc->anon_cost + sc->file_cost; 22918c2ecf20Sopenharmony_ci anon_cost = total_cost + sc->anon_cost; 22928c2ecf20Sopenharmony_ci file_cost = total_cost + sc->file_cost; 22938c2ecf20Sopenharmony_ci total_cost = anon_cost + file_cost; 22948c2ecf20Sopenharmony_ci 22958c2ecf20Sopenharmony_ci ap = swappiness * (total_cost + 1); 22968c2ecf20Sopenharmony_ci ap /= anon_cost + 1; 22978c2ecf20Sopenharmony_ci 22988c2ecf20Sopenharmony_ci fp = (200 - swappiness) * (total_cost + 1); 22998c2ecf20Sopenharmony_ci fp /= file_cost + 1; 23008c2ecf20Sopenharmony_ci 23018c2ecf20Sopenharmony_ci fraction[0] = ap; 23028c2ecf20Sopenharmony_ci fraction[1] = fp; 23038c2ecf20Sopenharmony_ci denominator = ap + fp; 23048c2ecf20Sopenharmony_ciout: 23058c2ecf20Sopenharmony_ci for_each_evictable_lru(lru) { 23068c2ecf20Sopenharmony_ci int file = is_file_lru(lru); 23078c2ecf20Sopenharmony_ci unsigned long lruvec_size; 23088c2ecf20Sopenharmony_ci unsigned long low, min; 23098c2ecf20Sopenharmony_ci unsigned long scan; 23108c2ecf20Sopenharmony_ci 23118c2ecf20Sopenharmony_ci lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); 23128c2ecf20Sopenharmony_ci mem_cgroup_protection(sc->target_mem_cgroup, memcg, 23138c2ecf20Sopenharmony_ci &min, &low); 23148c2ecf20Sopenharmony_ci 23158c2ecf20Sopenharmony_ci if (min || low) { 23168c2ecf20Sopenharmony_ci /* 23178c2ecf20Sopenharmony_ci * Scale a cgroup's reclaim pressure by proportioning 23188c2ecf20Sopenharmony_ci * its current usage to its memory.low or memory.min 23198c2ecf20Sopenharmony_ci * setting. 23208c2ecf20Sopenharmony_ci * 23218c2ecf20Sopenharmony_ci * This is important, as otherwise scanning aggression 23228c2ecf20Sopenharmony_ci * becomes extremely binary -- from nothing as we 23238c2ecf20Sopenharmony_ci * approach the memory protection threshold, to totally 23248c2ecf20Sopenharmony_ci * nominal as we exceed it. This results in requiring 23258c2ecf20Sopenharmony_ci * setting extremely liberal protection thresholds. It 23268c2ecf20Sopenharmony_ci * also means we simply get no protection at all if we 23278c2ecf20Sopenharmony_ci * set it too low, which is not ideal. 23288c2ecf20Sopenharmony_ci * 23298c2ecf20Sopenharmony_ci * If there is any protection in place, we reduce scan 23308c2ecf20Sopenharmony_ci * pressure by how much of the total memory used is 23318c2ecf20Sopenharmony_ci * within protection thresholds. 23328c2ecf20Sopenharmony_ci * 23338c2ecf20Sopenharmony_ci * There is one special case: in the first reclaim pass, 23348c2ecf20Sopenharmony_ci * we skip over all groups that are within their low 23358c2ecf20Sopenharmony_ci * protection. If that fails to reclaim enough pages to 23368c2ecf20Sopenharmony_ci * satisfy the reclaim goal, we come back and override 23378c2ecf20Sopenharmony_ci * the best-effort low protection. However, we still 23388c2ecf20Sopenharmony_ci * ideally want to honor how well-behaved groups are in 23398c2ecf20Sopenharmony_ci * that case instead of simply punishing them all 23408c2ecf20Sopenharmony_ci * equally. As such, we reclaim them based on how much 23418c2ecf20Sopenharmony_ci * memory they are using, reducing the scan pressure 23428c2ecf20Sopenharmony_ci * again by how much of the total memory used is under 23438c2ecf20Sopenharmony_ci * hard protection. 23448c2ecf20Sopenharmony_ci */ 23458c2ecf20Sopenharmony_ci unsigned long cgroup_size = mem_cgroup_size(memcg); 23468c2ecf20Sopenharmony_ci unsigned long protection; 23478c2ecf20Sopenharmony_ci 23488c2ecf20Sopenharmony_ci /* memory.low scaling, make sure we retry before OOM */ 23498c2ecf20Sopenharmony_ci if (!sc->memcg_low_reclaim && low > min) { 23508c2ecf20Sopenharmony_ci protection = low; 23518c2ecf20Sopenharmony_ci sc->memcg_low_skipped = 1; 23528c2ecf20Sopenharmony_ci } else { 23538c2ecf20Sopenharmony_ci protection = min; 23548c2ecf20Sopenharmony_ci } 23558c2ecf20Sopenharmony_ci 23568c2ecf20Sopenharmony_ci /* Avoid TOCTOU with earlier protection check */ 23578c2ecf20Sopenharmony_ci cgroup_size = max(cgroup_size, protection); 23588c2ecf20Sopenharmony_ci 23598c2ecf20Sopenharmony_ci scan = lruvec_size - lruvec_size * protection / 23608c2ecf20Sopenharmony_ci (cgroup_size + 1); 23618c2ecf20Sopenharmony_ci 23628c2ecf20Sopenharmony_ci /* 23638c2ecf20Sopenharmony_ci * Minimally target SWAP_CLUSTER_MAX pages to keep 23648c2ecf20Sopenharmony_ci * reclaim moving forwards, avoiding decrementing 23658c2ecf20Sopenharmony_ci * sc->priority further than desirable. 23668c2ecf20Sopenharmony_ci */ 23678c2ecf20Sopenharmony_ci scan = max(scan, SWAP_CLUSTER_MAX); 23688c2ecf20Sopenharmony_ci } else { 23698c2ecf20Sopenharmony_ci scan = lruvec_size; 23708c2ecf20Sopenharmony_ci } 23718c2ecf20Sopenharmony_ci 23728c2ecf20Sopenharmony_ci scan >>= sc->priority; 23738c2ecf20Sopenharmony_ci 23748c2ecf20Sopenharmony_ci /* 23758c2ecf20Sopenharmony_ci * If the cgroup's already been deleted, make sure to 23768c2ecf20Sopenharmony_ci * scrape out the remaining cache. 23778c2ecf20Sopenharmony_ci */ 23788c2ecf20Sopenharmony_ci if (!scan && !mem_cgroup_online(memcg)) 23798c2ecf20Sopenharmony_ci scan = min(lruvec_size, SWAP_CLUSTER_MAX); 23808c2ecf20Sopenharmony_ci 23818c2ecf20Sopenharmony_ci switch (scan_balance) { 23828c2ecf20Sopenharmony_ci case SCAN_EQUAL: 23838c2ecf20Sopenharmony_ci /* Scan lists relative to size */ 23848c2ecf20Sopenharmony_ci break; 23858c2ecf20Sopenharmony_ci case SCAN_FRACT: 23868c2ecf20Sopenharmony_ci /* 23878c2ecf20Sopenharmony_ci * Scan types proportional to swappiness and 23888c2ecf20Sopenharmony_ci * their relative recent reclaim efficiency. 23898c2ecf20Sopenharmony_ci * Make sure we don't miss the last page on 23908c2ecf20Sopenharmony_ci * the offlined memory cgroups because of a 23918c2ecf20Sopenharmony_ci * round-off error. 23928c2ecf20Sopenharmony_ci */ 23938c2ecf20Sopenharmony_ci scan = mem_cgroup_online(memcg) ? 23948c2ecf20Sopenharmony_ci div64_u64(scan * fraction[file], denominator) : 23958c2ecf20Sopenharmony_ci DIV64_U64_ROUND_UP(scan * fraction[file], 23968c2ecf20Sopenharmony_ci denominator); 23978c2ecf20Sopenharmony_ci break; 23988c2ecf20Sopenharmony_ci case SCAN_FILE: 23998c2ecf20Sopenharmony_ci case SCAN_ANON: 24008c2ecf20Sopenharmony_ci /* Scan one type exclusively */ 24018c2ecf20Sopenharmony_ci if ((scan_balance == SCAN_FILE) != file) 24028c2ecf20Sopenharmony_ci scan = 0; 24038c2ecf20Sopenharmony_ci break; 24048c2ecf20Sopenharmony_ci default: 24058c2ecf20Sopenharmony_ci /* Look ma, no brain */ 24068c2ecf20Sopenharmony_ci BUG(); 24078c2ecf20Sopenharmony_ci } 24088c2ecf20Sopenharmony_ci 24098c2ecf20Sopenharmony_ci nr[lru] = scan; 24108c2ecf20Sopenharmony_ci } 24118c2ecf20Sopenharmony_ci} 24128c2ecf20Sopenharmony_ci 24138c2ecf20Sopenharmony_civoid shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) 24148c2ecf20Sopenharmony_ci{ 24158c2ecf20Sopenharmony_ci unsigned long nr[NR_LRU_LISTS]; 24168c2ecf20Sopenharmony_ci unsigned long targets[NR_LRU_LISTS]; 24178c2ecf20Sopenharmony_ci unsigned long nr_to_scan; 24188c2ecf20Sopenharmony_ci enum lru_list lru; 24198c2ecf20Sopenharmony_ci unsigned long nr_reclaimed = 0; 24208c2ecf20Sopenharmony_ci unsigned long nr_to_reclaim = sc->nr_to_reclaim; 24218c2ecf20Sopenharmony_ci bool proportional_reclaim; 24228c2ecf20Sopenharmony_ci struct blk_plug plug; 24238c2ecf20Sopenharmony_ci 24248c2ecf20Sopenharmony_ci get_scan_count(lruvec, sc, nr); 24258c2ecf20Sopenharmony_ci 24268c2ecf20Sopenharmony_ci /* Record the original scan target for proportional adjustments later */ 24278c2ecf20Sopenharmony_ci memcpy(targets, nr, sizeof(nr)); 24288c2ecf20Sopenharmony_ci 24298c2ecf20Sopenharmony_ci /* 24308c2ecf20Sopenharmony_ci * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal 24318c2ecf20Sopenharmony_ci * event that can occur when there is little memory pressure e.g. 24328c2ecf20Sopenharmony_ci * multiple streaming readers/writers. Hence, we do not abort scanning 24338c2ecf20Sopenharmony_ci * when the requested number of pages are reclaimed when scanning at 24348c2ecf20Sopenharmony_ci * DEF_PRIORITY on the assumption that the fact we are direct 24358c2ecf20Sopenharmony_ci * reclaiming implies that kswapd is not keeping up and it is best to 24368c2ecf20Sopenharmony_ci * do a batch of work at once. For memcg reclaim one check is made to 24378c2ecf20Sopenharmony_ci * abort proportional reclaim if either the file or anon lru has already 24388c2ecf20Sopenharmony_ci * dropped to zero at the first pass. 24398c2ecf20Sopenharmony_ci */ 24408c2ecf20Sopenharmony_ci proportional_reclaim = (!cgroup_reclaim(sc) && !current_is_kswapd() && 24418c2ecf20Sopenharmony_ci sc->priority == DEF_PRIORITY); 24428c2ecf20Sopenharmony_ci 24438c2ecf20Sopenharmony_ci blk_start_plug(&plug); 24448c2ecf20Sopenharmony_ci while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 24458c2ecf20Sopenharmony_ci nr[LRU_INACTIVE_FILE]) { 24468c2ecf20Sopenharmony_ci unsigned long nr_anon, nr_file, percentage; 24478c2ecf20Sopenharmony_ci unsigned long nr_scanned; 24488c2ecf20Sopenharmony_ci 24498c2ecf20Sopenharmony_ci for_each_evictable_lru(lru) { 24508c2ecf20Sopenharmony_ci if (nr[lru]) { 24518c2ecf20Sopenharmony_ci nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 24528c2ecf20Sopenharmony_ci nr[lru] -= nr_to_scan; 24538c2ecf20Sopenharmony_ci 24548c2ecf20Sopenharmony_ci nr_reclaimed += shrink_list(lru, nr_to_scan, 24558c2ecf20Sopenharmony_ci lruvec, sc); 24568c2ecf20Sopenharmony_ci } 24578c2ecf20Sopenharmony_ci } 24588c2ecf20Sopenharmony_ci 24598c2ecf20Sopenharmony_ci cond_resched(); 24608c2ecf20Sopenharmony_ci 24618c2ecf20Sopenharmony_ci if (nr_reclaimed < nr_to_reclaim || proportional_reclaim) 24628c2ecf20Sopenharmony_ci continue; 24638c2ecf20Sopenharmony_ci 24648c2ecf20Sopenharmony_ci /* 24658c2ecf20Sopenharmony_ci * For kswapd and memcg, reclaim at least the number of pages 24668c2ecf20Sopenharmony_ci * requested. Ensure that the anon and file LRUs are scanned 24678c2ecf20Sopenharmony_ci * proportionally what was requested by get_scan_count(). We 24688c2ecf20Sopenharmony_ci * stop reclaiming one LRU and reduce the amount scanning 24698c2ecf20Sopenharmony_ci * proportional to the original scan target. 24708c2ecf20Sopenharmony_ci */ 24718c2ecf20Sopenharmony_ci nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 24728c2ecf20Sopenharmony_ci nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 24738c2ecf20Sopenharmony_ci 24748c2ecf20Sopenharmony_ci /* 24758c2ecf20Sopenharmony_ci * It's just vindictive to attack the larger once the smaller 24768c2ecf20Sopenharmony_ci * has gone to zero. And given the way we stop scanning the 24778c2ecf20Sopenharmony_ci * smaller below, this makes sure that we only make one nudge 24788c2ecf20Sopenharmony_ci * towards proportionality once we've got nr_to_reclaim. 24798c2ecf20Sopenharmony_ci */ 24808c2ecf20Sopenharmony_ci if (!nr_file || !nr_anon) 24818c2ecf20Sopenharmony_ci break; 24828c2ecf20Sopenharmony_ci 24838c2ecf20Sopenharmony_ci if (nr_file > nr_anon) { 24848c2ecf20Sopenharmony_ci unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 24858c2ecf20Sopenharmony_ci targets[LRU_ACTIVE_ANON] + 1; 24868c2ecf20Sopenharmony_ci lru = LRU_BASE; 24878c2ecf20Sopenharmony_ci percentage = nr_anon * 100 / scan_target; 24888c2ecf20Sopenharmony_ci } else { 24898c2ecf20Sopenharmony_ci unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 24908c2ecf20Sopenharmony_ci targets[LRU_ACTIVE_FILE] + 1; 24918c2ecf20Sopenharmony_ci lru = LRU_FILE; 24928c2ecf20Sopenharmony_ci percentage = nr_file * 100 / scan_target; 24938c2ecf20Sopenharmony_ci } 24948c2ecf20Sopenharmony_ci 24958c2ecf20Sopenharmony_ci /* Stop scanning the smaller of the LRU */ 24968c2ecf20Sopenharmony_ci nr[lru] = 0; 24978c2ecf20Sopenharmony_ci nr[lru + LRU_ACTIVE] = 0; 24988c2ecf20Sopenharmony_ci 24998c2ecf20Sopenharmony_ci /* 25008c2ecf20Sopenharmony_ci * Recalculate the other LRU scan count based on its original 25018c2ecf20Sopenharmony_ci * scan target and the percentage scanning already complete 25028c2ecf20Sopenharmony_ci */ 25038c2ecf20Sopenharmony_ci lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 25048c2ecf20Sopenharmony_ci nr_scanned = targets[lru] - nr[lru]; 25058c2ecf20Sopenharmony_ci nr[lru] = targets[lru] * (100 - percentage) / 100; 25068c2ecf20Sopenharmony_ci nr[lru] -= min(nr[lru], nr_scanned); 25078c2ecf20Sopenharmony_ci 25088c2ecf20Sopenharmony_ci lru += LRU_ACTIVE; 25098c2ecf20Sopenharmony_ci nr_scanned = targets[lru] - nr[lru]; 25108c2ecf20Sopenharmony_ci nr[lru] = targets[lru] * (100 - percentage) / 100; 25118c2ecf20Sopenharmony_ci nr[lru] -= min(nr[lru], nr_scanned); 25128c2ecf20Sopenharmony_ci } 25138c2ecf20Sopenharmony_ci blk_finish_plug(&plug); 25148c2ecf20Sopenharmony_ci sc->nr_reclaimed += nr_reclaimed; 25158c2ecf20Sopenharmony_ci 25168c2ecf20Sopenharmony_ci /* 25178c2ecf20Sopenharmony_ci * Even if we did not try to evict anon pages at all, we want to 25188c2ecf20Sopenharmony_ci * rebalance the anon lru active/inactive ratio. 25198c2ecf20Sopenharmony_ci */ 25208c2ecf20Sopenharmony_ci if (total_swap_pages && inactive_is_low(lruvec, LRU_INACTIVE_ANON)) 25218c2ecf20Sopenharmony_ci shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 25228c2ecf20Sopenharmony_ci sc, LRU_ACTIVE_ANON); 25238c2ecf20Sopenharmony_ci} 25248c2ecf20Sopenharmony_ci#endif 25258c2ecf20Sopenharmony_ci 25268c2ecf20Sopenharmony_ci/* Use reclaim/compaction for costly allocs or under memory pressure */ 25278c2ecf20Sopenharmony_cistatic bool in_reclaim_compaction(struct scan_control *sc) 25288c2ecf20Sopenharmony_ci{ 25298c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 25308c2ecf20Sopenharmony_ci (sc->order > PAGE_ALLOC_COSTLY_ORDER || 25318c2ecf20Sopenharmony_ci sc->priority < DEF_PRIORITY - 2)) 25328c2ecf20Sopenharmony_ci return true; 25338c2ecf20Sopenharmony_ci 25348c2ecf20Sopenharmony_ci return false; 25358c2ecf20Sopenharmony_ci} 25368c2ecf20Sopenharmony_ci 25378c2ecf20Sopenharmony_ci/* 25388c2ecf20Sopenharmony_ci * Reclaim/compaction is used for high-order allocation requests. It reclaims 25398c2ecf20Sopenharmony_ci * order-0 pages before compacting the zone. should_continue_reclaim() returns 25408c2ecf20Sopenharmony_ci * true if more pages should be reclaimed such that when the page allocator 25418c2ecf20Sopenharmony_ci * calls try_to_compact_pages() that it will have enough free pages to succeed. 25428c2ecf20Sopenharmony_ci * It will give up earlier than that if there is difficulty reclaiming pages. 25438c2ecf20Sopenharmony_ci */ 25448c2ecf20Sopenharmony_ciinline bool should_continue_reclaim(struct pglist_data *pgdat, 25458c2ecf20Sopenharmony_ci unsigned long nr_reclaimed, 25468c2ecf20Sopenharmony_ci struct scan_control *sc) 25478c2ecf20Sopenharmony_ci{ 25488c2ecf20Sopenharmony_ci unsigned long pages_for_compaction; 25498c2ecf20Sopenharmony_ci unsigned long inactive_lru_pages; 25508c2ecf20Sopenharmony_ci int z; 25518c2ecf20Sopenharmony_ci 25528c2ecf20Sopenharmony_ci /* If not in reclaim/compaction mode, stop */ 25538c2ecf20Sopenharmony_ci if (!in_reclaim_compaction(sc)) 25548c2ecf20Sopenharmony_ci return false; 25558c2ecf20Sopenharmony_ci 25568c2ecf20Sopenharmony_ci /* 25578c2ecf20Sopenharmony_ci * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX 25588c2ecf20Sopenharmony_ci * number of pages that were scanned. This will return to the caller 25598c2ecf20Sopenharmony_ci * with the risk reclaim/compaction and the resulting allocation attempt 25608c2ecf20Sopenharmony_ci * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL 25618c2ecf20Sopenharmony_ci * allocations through requiring that the full LRU list has been scanned 25628c2ecf20Sopenharmony_ci * first, by assuming that zero delta of sc->nr_scanned means full LRU 25638c2ecf20Sopenharmony_ci * scan, but that approximation was wrong, and there were corner cases 25648c2ecf20Sopenharmony_ci * where always a non-zero amount of pages were scanned. 25658c2ecf20Sopenharmony_ci */ 25668c2ecf20Sopenharmony_ci if (!nr_reclaimed) 25678c2ecf20Sopenharmony_ci return false; 25688c2ecf20Sopenharmony_ci 25698c2ecf20Sopenharmony_ci /* If compaction would go ahead or the allocation would succeed, stop */ 25708c2ecf20Sopenharmony_ci for (z = 0; z <= sc->reclaim_idx; z++) { 25718c2ecf20Sopenharmony_ci struct zone *zone = &pgdat->node_zones[z]; 25728c2ecf20Sopenharmony_ci if (!managed_zone(zone)) 25738c2ecf20Sopenharmony_ci continue; 25748c2ecf20Sopenharmony_ci 25758c2ecf20Sopenharmony_ci switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) { 25768c2ecf20Sopenharmony_ci case COMPACT_SUCCESS: 25778c2ecf20Sopenharmony_ci case COMPACT_CONTINUE: 25788c2ecf20Sopenharmony_ci return false; 25798c2ecf20Sopenharmony_ci default: 25808c2ecf20Sopenharmony_ci /* check next zone */ 25818c2ecf20Sopenharmony_ci ; 25828c2ecf20Sopenharmony_ci } 25838c2ecf20Sopenharmony_ci } 25848c2ecf20Sopenharmony_ci 25858c2ecf20Sopenharmony_ci /* 25868c2ecf20Sopenharmony_ci * If we have not reclaimed enough pages for compaction and the 25878c2ecf20Sopenharmony_ci * inactive lists are large enough, continue reclaiming 25888c2ecf20Sopenharmony_ci */ 25898c2ecf20Sopenharmony_ci pages_for_compaction = compact_gap(sc->order); 25908c2ecf20Sopenharmony_ci inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); 25918c2ecf20Sopenharmony_ci if (get_nr_swap_pages() > 0) 25928c2ecf20Sopenharmony_ci inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); 25938c2ecf20Sopenharmony_ci 25948c2ecf20Sopenharmony_ci return inactive_lru_pages > pages_for_compaction; 25958c2ecf20Sopenharmony_ci} 25968c2ecf20Sopenharmony_ci 25978c2ecf20Sopenharmony_ci#ifndef CONFIG_HYPERHOLD_FILE_LRU 25988c2ecf20Sopenharmony_cistatic void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc) 25998c2ecf20Sopenharmony_ci{ 26008c2ecf20Sopenharmony_ci struct mem_cgroup *target_memcg = sc->target_mem_cgroup; 26018c2ecf20Sopenharmony_ci struct mem_cgroup *memcg; 26028c2ecf20Sopenharmony_ci 26038c2ecf20Sopenharmony_ci memcg = mem_cgroup_iter(target_memcg, NULL, NULL); 26048c2ecf20Sopenharmony_ci do { 26058c2ecf20Sopenharmony_ci struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); 26068c2ecf20Sopenharmony_ci unsigned long reclaimed; 26078c2ecf20Sopenharmony_ci unsigned long scanned; 26088c2ecf20Sopenharmony_ci 26098c2ecf20Sopenharmony_ci /* 26108c2ecf20Sopenharmony_ci * This loop can become CPU-bound when target memcgs 26118c2ecf20Sopenharmony_ci * aren't eligible for reclaim - either because they 26128c2ecf20Sopenharmony_ci * don't have any reclaimable pages, or because their 26138c2ecf20Sopenharmony_ci * memory is explicitly protected. Avoid soft lockups. 26148c2ecf20Sopenharmony_ci */ 26158c2ecf20Sopenharmony_ci cond_resched(); 26168c2ecf20Sopenharmony_ci 26178c2ecf20Sopenharmony_ci mem_cgroup_calculate_protection(target_memcg, memcg); 26188c2ecf20Sopenharmony_ci 26198c2ecf20Sopenharmony_ci if (mem_cgroup_below_min(memcg)) { 26208c2ecf20Sopenharmony_ci /* 26218c2ecf20Sopenharmony_ci * Hard protection. 26228c2ecf20Sopenharmony_ci * If there is no reclaimable memory, OOM. 26238c2ecf20Sopenharmony_ci */ 26248c2ecf20Sopenharmony_ci continue; 26258c2ecf20Sopenharmony_ci } else if (mem_cgroup_below_low(memcg)) { 26268c2ecf20Sopenharmony_ci /* 26278c2ecf20Sopenharmony_ci * Soft protection. 26288c2ecf20Sopenharmony_ci * Respect the protection only as long as 26298c2ecf20Sopenharmony_ci * there is an unprotected supply 26308c2ecf20Sopenharmony_ci * of reclaimable memory from other cgroups. 26318c2ecf20Sopenharmony_ci */ 26328c2ecf20Sopenharmony_ci if (!sc->memcg_low_reclaim) { 26338c2ecf20Sopenharmony_ci sc->memcg_low_skipped = 1; 26348c2ecf20Sopenharmony_ci continue; 26358c2ecf20Sopenharmony_ci } 26368c2ecf20Sopenharmony_ci memcg_memory_event(memcg, MEMCG_LOW); 26378c2ecf20Sopenharmony_ci } 26388c2ecf20Sopenharmony_ci 26398c2ecf20Sopenharmony_ci reclaimed = sc->nr_reclaimed; 26408c2ecf20Sopenharmony_ci scanned = sc->nr_scanned; 26418c2ecf20Sopenharmony_ci 26428c2ecf20Sopenharmony_ci shrink_lruvec(lruvec, sc); 26438c2ecf20Sopenharmony_ci 26448c2ecf20Sopenharmony_ci shrink_slab(sc->gfp_mask, pgdat->node_id, memcg, 26458c2ecf20Sopenharmony_ci sc->priority); 26468c2ecf20Sopenharmony_ci 26478c2ecf20Sopenharmony_ci /* Record the group's reclaim efficiency */ 26488c2ecf20Sopenharmony_ci vmpressure(sc->gfp_mask, memcg, false, 26498c2ecf20Sopenharmony_ci sc->nr_scanned - scanned, 26508c2ecf20Sopenharmony_ci sc->nr_reclaimed - reclaimed); 26518c2ecf20Sopenharmony_ci 26528c2ecf20Sopenharmony_ci } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL))); 26538c2ecf20Sopenharmony_ci} 26548c2ecf20Sopenharmony_ci 26558c2ecf20Sopenharmony_cistatic void shrink_node(pg_data_t *pgdat, struct scan_control *sc) 26568c2ecf20Sopenharmony_ci{ 26578c2ecf20Sopenharmony_ci struct reclaim_state *reclaim_state = current->reclaim_state; 26588c2ecf20Sopenharmony_ci unsigned long nr_reclaimed, nr_scanned; 26598c2ecf20Sopenharmony_ci struct lruvec *target_lruvec; 26608c2ecf20Sopenharmony_ci bool reclaimable = false; 26618c2ecf20Sopenharmony_ci unsigned long file; 26628c2ecf20Sopenharmony_ci 26638c2ecf20Sopenharmony_ci target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat); 26648c2ecf20Sopenharmony_ci 26658c2ecf20Sopenharmony_ciagain: 26668c2ecf20Sopenharmony_ci memset(&sc->nr, 0, sizeof(sc->nr)); 26678c2ecf20Sopenharmony_ci 26688c2ecf20Sopenharmony_ci nr_reclaimed = sc->nr_reclaimed; 26698c2ecf20Sopenharmony_ci nr_scanned = sc->nr_scanned; 26708c2ecf20Sopenharmony_ci 26718c2ecf20Sopenharmony_ci /* 26728c2ecf20Sopenharmony_ci * Determine the scan balance between anon and file LRUs. 26738c2ecf20Sopenharmony_ci */ 26748c2ecf20Sopenharmony_ci spin_lock_irq(&pgdat->lru_lock); 26758c2ecf20Sopenharmony_ci sc->anon_cost = target_lruvec->anon_cost; 26768c2ecf20Sopenharmony_ci sc->file_cost = target_lruvec->file_cost; 26778c2ecf20Sopenharmony_ci spin_unlock_irq(&pgdat->lru_lock); 26788c2ecf20Sopenharmony_ci 26798c2ecf20Sopenharmony_ci /* 26808c2ecf20Sopenharmony_ci * Target desirable inactive:active list ratios for the anon 26818c2ecf20Sopenharmony_ci * and file LRU lists. 26828c2ecf20Sopenharmony_ci */ 26838c2ecf20Sopenharmony_ci if (!sc->force_deactivate) { 26848c2ecf20Sopenharmony_ci unsigned long refaults; 26858c2ecf20Sopenharmony_ci 26868c2ecf20Sopenharmony_ci refaults = lruvec_page_state(target_lruvec, 26878c2ecf20Sopenharmony_ci WORKINGSET_ACTIVATE_ANON); 26888c2ecf20Sopenharmony_ci if (refaults != target_lruvec->refaults[0] || 26898c2ecf20Sopenharmony_ci inactive_is_low(target_lruvec, LRU_INACTIVE_ANON)) 26908c2ecf20Sopenharmony_ci sc->may_deactivate |= DEACTIVATE_ANON; 26918c2ecf20Sopenharmony_ci else 26928c2ecf20Sopenharmony_ci sc->may_deactivate &= ~DEACTIVATE_ANON; 26938c2ecf20Sopenharmony_ci 26948c2ecf20Sopenharmony_ci /* 26958c2ecf20Sopenharmony_ci * When refaults are being observed, it means a new 26968c2ecf20Sopenharmony_ci * workingset is being established. Deactivate to get 26978c2ecf20Sopenharmony_ci * rid of any stale active pages quickly. 26988c2ecf20Sopenharmony_ci */ 26998c2ecf20Sopenharmony_ci refaults = lruvec_page_state(target_lruvec, 27008c2ecf20Sopenharmony_ci WORKINGSET_ACTIVATE_FILE); 27018c2ecf20Sopenharmony_ci if (refaults != target_lruvec->refaults[1] || 27028c2ecf20Sopenharmony_ci inactive_is_low(target_lruvec, LRU_INACTIVE_FILE)) 27038c2ecf20Sopenharmony_ci sc->may_deactivate |= DEACTIVATE_FILE; 27048c2ecf20Sopenharmony_ci else 27058c2ecf20Sopenharmony_ci sc->may_deactivate &= ~DEACTIVATE_FILE; 27068c2ecf20Sopenharmony_ci } else 27078c2ecf20Sopenharmony_ci sc->may_deactivate = DEACTIVATE_ANON | DEACTIVATE_FILE; 27088c2ecf20Sopenharmony_ci 27098c2ecf20Sopenharmony_ci /* 27108c2ecf20Sopenharmony_ci * If we have plenty of inactive file pages that aren't 27118c2ecf20Sopenharmony_ci * thrashing, try to reclaim those first before touching 27128c2ecf20Sopenharmony_ci * anonymous pages. 27138c2ecf20Sopenharmony_ci */ 27148c2ecf20Sopenharmony_ci file = lruvec_page_state(target_lruvec, NR_INACTIVE_FILE); 27158c2ecf20Sopenharmony_ci if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE)) 27168c2ecf20Sopenharmony_ci sc->cache_trim_mode = 1; 27178c2ecf20Sopenharmony_ci else 27188c2ecf20Sopenharmony_ci sc->cache_trim_mode = 0; 27198c2ecf20Sopenharmony_ci 27208c2ecf20Sopenharmony_ci /* 27218c2ecf20Sopenharmony_ci * Prevent the reclaimer from falling into the cache trap: as 27228c2ecf20Sopenharmony_ci * cache pages start out inactive, every cache fault will tip 27238c2ecf20Sopenharmony_ci * the scan balance towards the file LRU. And as the file LRU 27248c2ecf20Sopenharmony_ci * shrinks, so does the window for rotation from references. 27258c2ecf20Sopenharmony_ci * This means we have a runaway feedback loop where a tiny 27268c2ecf20Sopenharmony_ci * thrashing file LRU becomes infinitely more attractive than 27278c2ecf20Sopenharmony_ci * anon pages. Try to detect this based on file LRU size. 27288c2ecf20Sopenharmony_ci */ 27298c2ecf20Sopenharmony_ci if (!cgroup_reclaim(sc)) { 27308c2ecf20Sopenharmony_ci unsigned long total_high_wmark = 0; 27318c2ecf20Sopenharmony_ci unsigned long free, anon; 27328c2ecf20Sopenharmony_ci int z; 27338c2ecf20Sopenharmony_ci 27348c2ecf20Sopenharmony_ci free = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); 27358c2ecf20Sopenharmony_ci file = node_page_state(pgdat, NR_ACTIVE_FILE) + 27368c2ecf20Sopenharmony_ci node_page_state(pgdat, NR_INACTIVE_FILE); 27378c2ecf20Sopenharmony_ci 27388c2ecf20Sopenharmony_ci for (z = 0; z < MAX_NR_ZONES; z++) { 27398c2ecf20Sopenharmony_ci struct zone *zone = &pgdat->node_zones[z]; 27408c2ecf20Sopenharmony_ci if (!managed_zone(zone)) 27418c2ecf20Sopenharmony_ci continue; 27428c2ecf20Sopenharmony_ci 27438c2ecf20Sopenharmony_ci total_high_wmark += high_wmark_pages(zone); 27448c2ecf20Sopenharmony_ci } 27458c2ecf20Sopenharmony_ci 27468c2ecf20Sopenharmony_ci /* 27478c2ecf20Sopenharmony_ci * Consider anon: if that's low too, this isn't a 27488c2ecf20Sopenharmony_ci * runaway file reclaim problem, but rather just 27498c2ecf20Sopenharmony_ci * extreme pressure. Reclaim as per usual then. 27508c2ecf20Sopenharmony_ci */ 27518c2ecf20Sopenharmony_ci anon = node_page_state(pgdat, NR_INACTIVE_ANON); 27528c2ecf20Sopenharmony_ci 27538c2ecf20Sopenharmony_ci sc->file_is_tiny = 27548c2ecf20Sopenharmony_ci file + free <= total_high_wmark && 27558c2ecf20Sopenharmony_ci !(sc->may_deactivate & DEACTIVATE_ANON) && 27568c2ecf20Sopenharmony_ci anon >> sc->priority; 27578c2ecf20Sopenharmony_ci } 27588c2ecf20Sopenharmony_ci 27598c2ecf20Sopenharmony_ci shrink_node_memcgs(pgdat, sc); 27608c2ecf20Sopenharmony_ci 27618c2ecf20Sopenharmony_ci if (reclaim_state) { 27628c2ecf20Sopenharmony_ci sc->nr_reclaimed += reclaim_state->reclaimed_slab; 27638c2ecf20Sopenharmony_ci reclaim_state->reclaimed_slab = 0; 27648c2ecf20Sopenharmony_ci } 27658c2ecf20Sopenharmony_ci 27668c2ecf20Sopenharmony_ci /* Record the subtree's reclaim efficiency */ 27678c2ecf20Sopenharmony_ci vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, 27688c2ecf20Sopenharmony_ci sc->nr_scanned - nr_scanned, 27698c2ecf20Sopenharmony_ci sc->nr_reclaimed - nr_reclaimed); 27708c2ecf20Sopenharmony_ci 27718c2ecf20Sopenharmony_ci if (sc->nr_reclaimed - nr_reclaimed) 27728c2ecf20Sopenharmony_ci reclaimable = true; 27738c2ecf20Sopenharmony_ci 27748c2ecf20Sopenharmony_ci if (current_is_kswapd()) { 27758c2ecf20Sopenharmony_ci /* 27768c2ecf20Sopenharmony_ci * If reclaim is isolating dirty pages under writeback, 27778c2ecf20Sopenharmony_ci * it implies that the long-lived page allocation rate 27788c2ecf20Sopenharmony_ci * is exceeding the page laundering rate. Either the 27798c2ecf20Sopenharmony_ci * global limits are not being effective at throttling 27808c2ecf20Sopenharmony_ci * processes due to the page distribution throughout 27818c2ecf20Sopenharmony_ci * zones or there is heavy usage of a slow backing 27828c2ecf20Sopenharmony_ci * device. The only option is to throttle from reclaim 27838c2ecf20Sopenharmony_ci * context which is not ideal as there is no guarantee 27848c2ecf20Sopenharmony_ci * the dirtying process is throttled in the same way 27858c2ecf20Sopenharmony_ci * balance_dirty_pages() manages. 27868c2ecf20Sopenharmony_ci * 27878c2ecf20Sopenharmony_ci * Once a node is flagged PGDAT_WRITEBACK, kswapd will 27888c2ecf20Sopenharmony_ci * count the number of pages under pages flagged for 27898c2ecf20Sopenharmony_ci * immediate reclaim and stall if any are encountered 27908c2ecf20Sopenharmony_ci * in the nr_immediate check below. 27918c2ecf20Sopenharmony_ci */ 27928c2ecf20Sopenharmony_ci if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken) 27938c2ecf20Sopenharmony_ci set_bit(PGDAT_WRITEBACK, &pgdat->flags); 27948c2ecf20Sopenharmony_ci 27958c2ecf20Sopenharmony_ci /* Allow kswapd to start writing pages during reclaim.*/ 27968c2ecf20Sopenharmony_ci if (sc->nr.unqueued_dirty == sc->nr.file_taken) 27978c2ecf20Sopenharmony_ci set_bit(PGDAT_DIRTY, &pgdat->flags); 27988c2ecf20Sopenharmony_ci 27998c2ecf20Sopenharmony_ci /* 28008c2ecf20Sopenharmony_ci * If kswapd scans pages marked for immediate 28018c2ecf20Sopenharmony_ci * reclaim and under writeback (nr_immediate), it 28028c2ecf20Sopenharmony_ci * implies that pages are cycling through the LRU 28038c2ecf20Sopenharmony_ci * faster than they are written so also forcibly stall. 28048c2ecf20Sopenharmony_ci */ 28058c2ecf20Sopenharmony_ci if (sc->nr.immediate) 28068c2ecf20Sopenharmony_ci congestion_wait(BLK_RW_ASYNC, HZ/10); 28078c2ecf20Sopenharmony_ci } 28088c2ecf20Sopenharmony_ci 28098c2ecf20Sopenharmony_ci /* 28108c2ecf20Sopenharmony_ci * Tag a node/memcg as congested if all the dirty pages 28118c2ecf20Sopenharmony_ci * scanned were backed by a congested BDI and 28128c2ecf20Sopenharmony_ci * wait_iff_congested will stall. 28138c2ecf20Sopenharmony_ci * 28148c2ecf20Sopenharmony_ci * Legacy memcg will stall in page writeback so avoid forcibly 28158c2ecf20Sopenharmony_ci * stalling in wait_iff_congested(). 28168c2ecf20Sopenharmony_ci */ 28178c2ecf20Sopenharmony_ci if ((current_is_kswapd() || 28188c2ecf20Sopenharmony_ci (cgroup_reclaim(sc) && writeback_throttling_sane(sc))) && 28198c2ecf20Sopenharmony_ci sc->nr.dirty && sc->nr.dirty == sc->nr.congested) 28208c2ecf20Sopenharmony_ci set_bit(LRUVEC_CONGESTED, &target_lruvec->flags); 28218c2ecf20Sopenharmony_ci 28228c2ecf20Sopenharmony_ci /* 28238c2ecf20Sopenharmony_ci * Stall direct reclaim for IO completions if underlying BDIs 28248c2ecf20Sopenharmony_ci * and node is congested. Allow kswapd to continue until it 28258c2ecf20Sopenharmony_ci * starts encountering unqueued dirty pages or cycling through 28268c2ecf20Sopenharmony_ci * the LRU too quickly. 28278c2ecf20Sopenharmony_ci */ 28288c2ecf20Sopenharmony_ci if (!current_is_kswapd() && current_may_throttle() && 28298c2ecf20Sopenharmony_ci !sc->hibernation_mode && 28308c2ecf20Sopenharmony_ci test_bit(LRUVEC_CONGESTED, &target_lruvec->flags)) 28318c2ecf20Sopenharmony_ci wait_iff_congested(BLK_RW_ASYNC, HZ/10); 28328c2ecf20Sopenharmony_ci 28338c2ecf20Sopenharmony_ci if (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, 28348c2ecf20Sopenharmony_ci sc)) 28358c2ecf20Sopenharmony_ci goto again; 28368c2ecf20Sopenharmony_ci 28378c2ecf20Sopenharmony_ci /* 28388c2ecf20Sopenharmony_ci * Kswapd gives up on balancing particular nodes after too 28398c2ecf20Sopenharmony_ci * many failures to reclaim anything from them and goes to 28408c2ecf20Sopenharmony_ci * sleep. On reclaim progress, reset the failure counter. A 28418c2ecf20Sopenharmony_ci * successful direct reclaim run will revive a dormant kswapd. 28428c2ecf20Sopenharmony_ci */ 28438c2ecf20Sopenharmony_ci if (reclaimable) 28448c2ecf20Sopenharmony_ci pgdat->kswapd_failures = 0; 28458c2ecf20Sopenharmony_ci} 28468c2ecf20Sopenharmony_ci#endif 28478c2ecf20Sopenharmony_ci 28488c2ecf20Sopenharmony_ci/* 28498c2ecf20Sopenharmony_ci * Returns true if compaction should go ahead for a costly-order request, or 28508c2ecf20Sopenharmony_ci * the allocation would already succeed without compaction. Return false if we 28518c2ecf20Sopenharmony_ci * should reclaim first. 28528c2ecf20Sopenharmony_ci */ 28538c2ecf20Sopenharmony_cistatic inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 28548c2ecf20Sopenharmony_ci{ 28558c2ecf20Sopenharmony_ci unsigned long watermark; 28568c2ecf20Sopenharmony_ci enum compact_result suitable; 28578c2ecf20Sopenharmony_ci 28588c2ecf20Sopenharmony_ci suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx); 28598c2ecf20Sopenharmony_ci if (suitable == COMPACT_SUCCESS) 28608c2ecf20Sopenharmony_ci /* Allocation should succeed already. Don't reclaim. */ 28618c2ecf20Sopenharmony_ci return true; 28628c2ecf20Sopenharmony_ci if (suitable == COMPACT_SKIPPED) 28638c2ecf20Sopenharmony_ci /* Compaction cannot yet proceed. Do reclaim. */ 28648c2ecf20Sopenharmony_ci return false; 28658c2ecf20Sopenharmony_ci 28668c2ecf20Sopenharmony_ci /* 28678c2ecf20Sopenharmony_ci * Compaction is already possible, but it takes time to run and there 28688c2ecf20Sopenharmony_ci * are potentially other callers using the pages just freed. So proceed 28698c2ecf20Sopenharmony_ci * with reclaim to make a buffer of free pages available to give 28708c2ecf20Sopenharmony_ci * compaction a reasonable chance of completing and allocating the page. 28718c2ecf20Sopenharmony_ci * Note that we won't actually reclaim the whole buffer in one attempt 28728c2ecf20Sopenharmony_ci * as the target watermark in should_continue_reclaim() is lower. But if 28738c2ecf20Sopenharmony_ci * we are already above the high+gap watermark, don't reclaim at all. 28748c2ecf20Sopenharmony_ci */ 28758c2ecf20Sopenharmony_ci watermark = high_wmark_pages(zone) + compact_gap(sc->order); 28768c2ecf20Sopenharmony_ci 28778c2ecf20Sopenharmony_ci return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); 28788c2ecf20Sopenharmony_ci} 28798c2ecf20Sopenharmony_ci 28808c2ecf20Sopenharmony_ci/* 28818c2ecf20Sopenharmony_ci * This is the direct reclaim path, for page-allocating processes. We only 28828c2ecf20Sopenharmony_ci * try to reclaim pages from zones which will satisfy the caller's allocation 28838c2ecf20Sopenharmony_ci * request. 28848c2ecf20Sopenharmony_ci * 28858c2ecf20Sopenharmony_ci * If a zone is deemed to be full of pinned pages then just give it a light 28868c2ecf20Sopenharmony_ci * scan then give up on it. 28878c2ecf20Sopenharmony_ci */ 28888c2ecf20Sopenharmony_cistatic void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 28898c2ecf20Sopenharmony_ci{ 28908c2ecf20Sopenharmony_ci struct zoneref *z; 28918c2ecf20Sopenharmony_ci struct zone *zone; 28928c2ecf20Sopenharmony_ci unsigned long nr_soft_reclaimed; 28938c2ecf20Sopenharmony_ci unsigned long nr_soft_scanned; 28948c2ecf20Sopenharmony_ci gfp_t orig_mask; 28958c2ecf20Sopenharmony_ci pg_data_t *last_pgdat = NULL; 28968c2ecf20Sopenharmony_ci 28978c2ecf20Sopenharmony_ci /* 28988c2ecf20Sopenharmony_ci * If the number of buffer_heads in the machine exceeds the maximum 28998c2ecf20Sopenharmony_ci * allowed level, force direct reclaim to scan the highmem zone as 29008c2ecf20Sopenharmony_ci * highmem pages could be pinning lowmem pages storing buffer_heads 29018c2ecf20Sopenharmony_ci */ 29028c2ecf20Sopenharmony_ci orig_mask = sc->gfp_mask; 29038c2ecf20Sopenharmony_ci if (buffer_heads_over_limit) { 29048c2ecf20Sopenharmony_ci sc->gfp_mask |= __GFP_HIGHMEM; 29058c2ecf20Sopenharmony_ci sc->reclaim_idx = gfp_zone(sc->gfp_mask); 29068c2ecf20Sopenharmony_ci } 29078c2ecf20Sopenharmony_ci 29088c2ecf20Sopenharmony_ci for_each_zone_zonelist_nodemask(zone, z, zonelist, 29098c2ecf20Sopenharmony_ci sc->reclaim_idx, sc->nodemask) { 29108c2ecf20Sopenharmony_ci /* 29118c2ecf20Sopenharmony_ci * Take care memory controller reclaiming has small influence 29128c2ecf20Sopenharmony_ci * to global LRU. 29138c2ecf20Sopenharmony_ci */ 29148c2ecf20Sopenharmony_ci if (!cgroup_reclaim(sc)) { 29158c2ecf20Sopenharmony_ci if (!cpuset_zone_allowed(zone, 29168c2ecf20Sopenharmony_ci GFP_KERNEL | __GFP_HARDWALL)) 29178c2ecf20Sopenharmony_ci continue; 29188c2ecf20Sopenharmony_ci 29198c2ecf20Sopenharmony_ci /* 29208c2ecf20Sopenharmony_ci * If we already have plenty of memory free for 29218c2ecf20Sopenharmony_ci * compaction in this zone, don't free any more. 29228c2ecf20Sopenharmony_ci * Even though compaction is invoked for any 29238c2ecf20Sopenharmony_ci * non-zero order, only frequent costly order 29248c2ecf20Sopenharmony_ci * reclamation is disruptive enough to become a 29258c2ecf20Sopenharmony_ci * noticeable problem, like transparent huge 29268c2ecf20Sopenharmony_ci * page allocations. 29278c2ecf20Sopenharmony_ci */ 29288c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_COMPACTION) && 29298c2ecf20Sopenharmony_ci sc->order > PAGE_ALLOC_COSTLY_ORDER && 29308c2ecf20Sopenharmony_ci compaction_ready(zone, sc)) { 29318c2ecf20Sopenharmony_ci sc->compaction_ready = true; 29328c2ecf20Sopenharmony_ci continue; 29338c2ecf20Sopenharmony_ci } 29348c2ecf20Sopenharmony_ci 29358c2ecf20Sopenharmony_ci /* 29368c2ecf20Sopenharmony_ci * Shrink each node in the zonelist once. If the 29378c2ecf20Sopenharmony_ci * zonelist is ordered by zone (not the default) then a 29388c2ecf20Sopenharmony_ci * node may be shrunk multiple times but in that case 29398c2ecf20Sopenharmony_ci * the user prefers lower zones being preserved. 29408c2ecf20Sopenharmony_ci */ 29418c2ecf20Sopenharmony_ci if (zone->zone_pgdat == last_pgdat) 29428c2ecf20Sopenharmony_ci continue; 29438c2ecf20Sopenharmony_ci 29448c2ecf20Sopenharmony_ci /* 29458c2ecf20Sopenharmony_ci * This steals pages from memory cgroups over softlimit 29468c2ecf20Sopenharmony_ci * and returns the number of reclaimed pages and 29478c2ecf20Sopenharmony_ci * scanned pages. This works for global memory pressure 29488c2ecf20Sopenharmony_ci * and balancing, not for a memcg's limit. 29498c2ecf20Sopenharmony_ci */ 29508c2ecf20Sopenharmony_ci nr_soft_scanned = 0; 29518c2ecf20Sopenharmony_ci nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, 29528c2ecf20Sopenharmony_ci sc->order, sc->gfp_mask, 29538c2ecf20Sopenharmony_ci &nr_soft_scanned); 29548c2ecf20Sopenharmony_ci sc->nr_reclaimed += nr_soft_reclaimed; 29558c2ecf20Sopenharmony_ci sc->nr_scanned += nr_soft_scanned; 29568c2ecf20Sopenharmony_ci /* need some check for avoid more shrink_zone() */ 29578c2ecf20Sopenharmony_ci } 29588c2ecf20Sopenharmony_ci 29598c2ecf20Sopenharmony_ci /* See comment about same check for global reclaim above */ 29608c2ecf20Sopenharmony_ci if (zone->zone_pgdat == last_pgdat) 29618c2ecf20Sopenharmony_ci continue; 29628c2ecf20Sopenharmony_ci last_pgdat = zone->zone_pgdat; 29638c2ecf20Sopenharmony_ci#ifdef CONFIG_HYPERHOLD_FILE_LRU 29648c2ecf20Sopenharmony_ci shrink_node_hyperhold(zone->zone_pgdat, sc); 29658c2ecf20Sopenharmony_ci#else 29668c2ecf20Sopenharmony_ci shrink_node(zone->zone_pgdat, sc); 29678c2ecf20Sopenharmony_ci#endif 29688c2ecf20Sopenharmony_ci } 29698c2ecf20Sopenharmony_ci 29708c2ecf20Sopenharmony_ci /* 29718c2ecf20Sopenharmony_ci * Restore to original mask to avoid the impact on the caller if we 29728c2ecf20Sopenharmony_ci * promoted it to __GFP_HIGHMEM. 29738c2ecf20Sopenharmony_ci */ 29748c2ecf20Sopenharmony_ci sc->gfp_mask = orig_mask; 29758c2ecf20Sopenharmony_ci} 29768c2ecf20Sopenharmony_ci 29778c2ecf20Sopenharmony_cistatic void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat) 29788c2ecf20Sopenharmony_ci{ 29798c2ecf20Sopenharmony_ci struct lruvec *target_lruvec; 29808c2ecf20Sopenharmony_ci unsigned long refaults; 29818c2ecf20Sopenharmony_ci 29828c2ecf20Sopenharmony_ci#ifdef CONFIG_HYPERHOLD_FILE_LRU 29838c2ecf20Sopenharmony_ci struct lruvec *lruvec; 29848c2ecf20Sopenharmony_ci 29858c2ecf20Sopenharmony_ci lruvec = node_lruvec(pgdat); 29868c2ecf20Sopenharmony_ci lruvec->refaults[0] = lruvec_page_state(lruvec, WORKINGSET_ACTIVATE_ANON); /* modified */ 29878c2ecf20Sopenharmony_ci lruvec->refaults[1] = lruvec_page_state(lruvec, WORKINGSET_ACTIVATE_FILE); /* modified */ 29888c2ecf20Sopenharmony_ci#endif 29898c2ecf20Sopenharmony_ci 29908c2ecf20Sopenharmony_ci target_lruvec = mem_cgroup_lruvec(target_memcg, pgdat); 29918c2ecf20Sopenharmony_ci refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_ANON); 29928c2ecf20Sopenharmony_ci target_lruvec->refaults[0] = refaults; 29938c2ecf20Sopenharmony_ci refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_FILE); 29948c2ecf20Sopenharmony_ci target_lruvec->refaults[1] = refaults; 29958c2ecf20Sopenharmony_ci} 29968c2ecf20Sopenharmony_ci 29978c2ecf20Sopenharmony_ci/* 29988c2ecf20Sopenharmony_ci * This is the main entry point to direct page reclaim. 29998c2ecf20Sopenharmony_ci * 30008c2ecf20Sopenharmony_ci * If a full scan of the inactive list fails to free enough memory then we 30018c2ecf20Sopenharmony_ci * are "out of memory" and something needs to be killed. 30028c2ecf20Sopenharmony_ci * 30038c2ecf20Sopenharmony_ci * If the caller is !__GFP_FS then the probability of a failure is reasonably 30048c2ecf20Sopenharmony_ci * high - the zone may be full of dirty or under-writeback pages, which this 30058c2ecf20Sopenharmony_ci * caller can't do much about. We kick the writeback threads and take explicit 30068c2ecf20Sopenharmony_ci * naps in the hope that some of these pages can be written. But if the 30078c2ecf20Sopenharmony_ci * allocating task holds filesystem locks which prevent writeout this might not 30088c2ecf20Sopenharmony_ci * work, and the allocation attempt will fail. 30098c2ecf20Sopenharmony_ci * 30108c2ecf20Sopenharmony_ci * returns: 0, if no pages reclaimed 30118c2ecf20Sopenharmony_ci * else, the number of pages reclaimed 30128c2ecf20Sopenharmony_ci */ 30138c2ecf20Sopenharmony_cistatic unsigned long do_try_to_free_pages(struct zonelist *zonelist, 30148c2ecf20Sopenharmony_ci struct scan_control *sc) 30158c2ecf20Sopenharmony_ci{ 30168c2ecf20Sopenharmony_ci int initial_priority = sc->priority; 30178c2ecf20Sopenharmony_ci pg_data_t *last_pgdat; 30188c2ecf20Sopenharmony_ci struct zoneref *z; 30198c2ecf20Sopenharmony_ci struct zone *zone; 30208c2ecf20Sopenharmony_ciretry: 30218c2ecf20Sopenharmony_ci delayacct_freepages_start(); 30228c2ecf20Sopenharmony_ci 30238c2ecf20Sopenharmony_ci if (!cgroup_reclaim(sc)) 30248c2ecf20Sopenharmony_ci __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); 30258c2ecf20Sopenharmony_ci 30268c2ecf20Sopenharmony_ci do { 30278c2ecf20Sopenharmony_ci vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 30288c2ecf20Sopenharmony_ci sc->priority); 30298c2ecf20Sopenharmony_ci sc->nr_scanned = 0; 30308c2ecf20Sopenharmony_ci shrink_zones(zonelist, sc); 30318c2ecf20Sopenharmony_ci 30328c2ecf20Sopenharmony_ci if (sc->nr_reclaimed >= sc->nr_to_reclaim) 30338c2ecf20Sopenharmony_ci break; 30348c2ecf20Sopenharmony_ci 30358c2ecf20Sopenharmony_ci if (sc->compaction_ready) 30368c2ecf20Sopenharmony_ci break; 30378c2ecf20Sopenharmony_ci 30388c2ecf20Sopenharmony_ci /* 30398c2ecf20Sopenharmony_ci * If we're getting trouble reclaiming, start doing 30408c2ecf20Sopenharmony_ci * writepage even in laptop mode. 30418c2ecf20Sopenharmony_ci */ 30428c2ecf20Sopenharmony_ci if (sc->priority < DEF_PRIORITY - 2) 30438c2ecf20Sopenharmony_ci sc->may_writepage = 1; 30448c2ecf20Sopenharmony_ci } while (--sc->priority >= 0); 30458c2ecf20Sopenharmony_ci 30468c2ecf20Sopenharmony_ci last_pgdat = NULL; 30478c2ecf20Sopenharmony_ci for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx, 30488c2ecf20Sopenharmony_ci sc->nodemask) { 30498c2ecf20Sopenharmony_ci if (zone->zone_pgdat == last_pgdat) 30508c2ecf20Sopenharmony_ci continue; 30518c2ecf20Sopenharmony_ci last_pgdat = zone->zone_pgdat; 30528c2ecf20Sopenharmony_ci 30538c2ecf20Sopenharmony_ci snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat); 30548c2ecf20Sopenharmony_ci 30558c2ecf20Sopenharmony_ci if (cgroup_reclaim(sc)) { 30568c2ecf20Sopenharmony_ci struct lruvec *lruvec; 30578c2ecf20Sopenharmony_ci 30588c2ecf20Sopenharmony_ci lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, 30598c2ecf20Sopenharmony_ci zone->zone_pgdat); 30608c2ecf20Sopenharmony_ci clear_bit(LRUVEC_CONGESTED, &lruvec->flags); 30618c2ecf20Sopenharmony_ci } 30628c2ecf20Sopenharmony_ci } 30638c2ecf20Sopenharmony_ci 30648c2ecf20Sopenharmony_ci delayacct_freepages_end(); 30658c2ecf20Sopenharmony_ci 30668c2ecf20Sopenharmony_ci if (sc->nr_reclaimed) 30678c2ecf20Sopenharmony_ci return sc->nr_reclaimed; 30688c2ecf20Sopenharmony_ci 30698c2ecf20Sopenharmony_ci /* Aborted reclaim to try compaction? don't OOM, then */ 30708c2ecf20Sopenharmony_ci if (sc->compaction_ready) 30718c2ecf20Sopenharmony_ci return 1; 30728c2ecf20Sopenharmony_ci 30738c2ecf20Sopenharmony_ci /* 30748c2ecf20Sopenharmony_ci * We make inactive:active ratio decisions based on the node's 30758c2ecf20Sopenharmony_ci * composition of memory, but a restrictive reclaim_idx or a 30768c2ecf20Sopenharmony_ci * memory.low cgroup setting can exempt large amounts of 30778c2ecf20Sopenharmony_ci * memory from reclaim. Neither of which are very common, so 30788c2ecf20Sopenharmony_ci * instead of doing costly eligibility calculations of the 30798c2ecf20Sopenharmony_ci * entire cgroup subtree up front, we assume the estimates are 30808c2ecf20Sopenharmony_ci * good, and retry with forcible deactivation if that fails. 30818c2ecf20Sopenharmony_ci */ 30828c2ecf20Sopenharmony_ci if (sc->skipped_deactivate) { 30838c2ecf20Sopenharmony_ci sc->priority = initial_priority; 30848c2ecf20Sopenharmony_ci sc->force_deactivate = 1; 30858c2ecf20Sopenharmony_ci sc->skipped_deactivate = 0; 30868c2ecf20Sopenharmony_ci goto retry; 30878c2ecf20Sopenharmony_ci } 30888c2ecf20Sopenharmony_ci 30898c2ecf20Sopenharmony_ci /* Untapped cgroup reserves? Don't OOM, retry. */ 30908c2ecf20Sopenharmony_ci if (sc->memcg_low_skipped) { 30918c2ecf20Sopenharmony_ci sc->priority = initial_priority; 30928c2ecf20Sopenharmony_ci sc->force_deactivate = 0; 30938c2ecf20Sopenharmony_ci sc->memcg_low_reclaim = 1; 30948c2ecf20Sopenharmony_ci sc->memcg_low_skipped = 0; 30958c2ecf20Sopenharmony_ci goto retry; 30968c2ecf20Sopenharmony_ci } 30978c2ecf20Sopenharmony_ci 30988c2ecf20Sopenharmony_ci return 0; 30998c2ecf20Sopenharmony_ci} 31008c2ecf20Sopenharmony_ci 31018c2ecf20Sopenharmony_cistatic bool allow_direct_reclaim(pg_data_t *pgdat) 31028c2ecf20Sopenharmony_ci{ 31038c2ecf20Sopenharmony_ci struct zone *zone; 31048c2ecf20Sopenharmony_ci unsigned long pfmemalloc_reserve = 0; 31058c2ecf20Sopenharmony_ci unsigned long free_pages = 0; 31068c2ecf20Sopenharmony_ci int i; 31078c2ecf20Sopenharmony_ci bool wmark_ok; 31088c2ecf20Sopenharmony_ci 31098c2ecf20Sopenharmony_ci if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 31108c2ecf20Sopenharmony_ci return true; 31118c2ecf20Sopenharmony_ci 31128c2ecf20Sopenharmony_ci for (i = 0; i <= ZONE_NORMAL; i++) { 31138c2ecf20Sopenharmony_ci zone = &pgdat->node_zones[i]; 31148c2ecf20Sopenharmony_ci if (!managed_zone(zone)) 31158c2ecf20Sopenharmony_ci continue; 31168c2ecf20Sopenharmony_ci 31178c2ecf20Sopenharmony_ci if (!zone_reclaimable_pages(zone)) 31188c2ecf20Sopenharmony_ci continue; 31198c2ecf20Sopenharmony_ci 31208c2ecf20Sopenharmony_ci pfmemalloc_reserve += min_wmark_pages(zone); 31218c2ecf20Sopenharmony_ci free_pages += zone_page_state(zone, NR_FREE_PAGES); 31228c2ecf20Sopenharmony_ci } 31238c2ecf20Sopenharmony_ci 31248c2ecf20Sopenharmony_ci /* If there are no reserves (unexpected config) then do not throttle */ 31258c2ecf20Sopenharmony_ci if (!pfmemalloc_reserve) 31268c2ecf20Sopenharmony_ci return true; 31278c2ecf20Sopenharmony_ci 31288c2ecf20Sopenharmony_ci wmark_ok = free_pages > pfmemalloc_reserve / 2; 31298c2ecf20Sopenharmony_ci 31308c2ecf20Sopenharmony_ci /* kswapd must be awake if processes are being throttled */ 31318c2ecf20Sopenharmony_ci if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 31328c2ecf20Sopenharmony_ci if (READ_ONCE(pgdat->kswapd_highest_zoneidx) > ZONE_NORMAL) 31338c2ecf20Sopenharmony_ci WRITE_ONCE(pgdat->kswapd_highest_zoneidx, ZONE_NORMAL); 31348c2ecf20Sopenharmony_ci 31358c2ecf20Sopenharmony_ci wake_up_interruptible(&pgdat->kswapd_wait); 31368c2ecf20Sopenharmony_ci } 31378c2ecf20Sopenharmony_ci 31388c2ecf20Sopenharmony_ci return wmark_ok; 31398c2ecf20Sopenharmony_ci} 31408c2ecf20Sopenharmony_ci 31418c2ecf20Sopenharmony_ci/* 31428c2ecf20Sopenharmony_ci * Throttle direct reclaimers if backing storage is backed by the network 31438c2ecf20Sopenharmony_ci * and the PFMEMALLOC reserve for the preferred node is getting dangerously 31448c2ecf20Sopenharmony_ci * depleted. kswapd will continue to make progress and wake the processes 31458c2ecf20Sopenharmony_ci * when the low watermark is reached. 31468c2ecf20Sopenharmony_ci * 31478c2ecf20Sopenharmony_ci * Returns true if a fatal signal was delivered during throttling. If this 31488c2ecf20Sopenharmony_ci * happens, the page allocator should not consider triggering the OOM killer. 31498c2ecf20Sopenharmony_ci */ 31508c2ecf20Sopenharmony_cistatic bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 31518c2ecf20Sopenharmony_ci nodemask_t *nodemask) 31528c2ecf20Sopenharmony_ci{ 31538c2ecf20Sopenharmony_ci struct zoneref *z; 31548c2ecf20Sopenharmony_ci struct zone *zone; 31558c2ecf20Sopenharmony_ci pg_data_t *pgdat = NULL; 31568c2ecf20Sopenharmony_ci 31578c2ecf20Sopenharmony_ci /* 31588c2ecf20Sopenharmony_ci * Kernel threads should not be throttled as they may be indirectly 31598c2ecf20Sopenharmony_ci * responsible for cleaning pages necessary for reclaim to make forward 31608c2ecf20Sopenharmony_ci * progress. kjournald for example may enter direct reclaim while 31618c2ecf20Sopenharmony_ci * committing a transaction where throttling it could forcing other 31628c2ecf20Sopenharmony_ci * processes to block on log_wait_commit(). 31638c2ecf20Sopenharmony_ci */ 31648c2ecf20Sopenharmony_ci if (current->flags & PF_KTHREAD) 31658c2ecf20Sopenharmony_ci goto out; 31668c2ecf20Sopenharmony_ci 31678c2ecf20Sopenharmony_ci /* 31688c2ecf20Sopenharmony_ci * If a fatal signal is pending, this process should not throttle. 31698c2ecf20Sopenharmony_ci * It should return quickly so it can exit and free its memory 31708c2ecf20Sopenharmony_ci */ 31718c2ecf20Sopenharmony_ci if (fatal_signal_pending(current)) 31728c2ecf20Sopenharmony_ci goto out; 31738c2ecf20Sopenharmony_ci 31748c2ecf20Sopenharmony_ci /* 31758c2ecf20Sopenharmony_ci * Check if the pfmemalloc reserves are ok by finding the first node 31768c2ecf20Sopenharmony_ci * with a usable ZONE_NORMAL or lower zone. The expectation is that 31778c2ecf20Sopenharmony_ci * GFP_KERNEL will be required for allocating network buffers when 31788c2ecf20Sopenharmony_ci * swapping over the network so ZONE_HIGHMEM is unusable. 31798c2ecf20Sopenharmony_ci * 31808c2ecf20Sopenharmony_ci * Throttling is based on the first usable node and throttled processes 31818c2ecf20Sopenharmony_ci * wait on a queue until kswapd makes progress and wakes them. There 31828c2ecf20Sopenharmony_ci * is an affinity then between processes waking up and where reclaim 31838c2ecf20Sopenharmony_ci * progress has been made assuming the process wakes on the same node. 31848c2ecf20Sopenharmony_ci * More importantly, processes running on remote nodes will not compete 31858c2ecf20Sopenharmony_ci * for remote pfmemalloc reserves and processes on different nodes 31868c2ecf20Sopenharmony_ci * should make reasonable progress. 31878c2ecf20Sopenharmony_ci */ 31888c2ecf20Sopenharmony_ci for_each_zone_zonelist_nodemask(zone, z, zonelist, 31898c2ecf20Sopenharmony_ci gfp_zone(gfp_mask), nodemask) { 31908c2ecf20Sopenharmony_ci if (zone_idx(zone) > ZONE_NORMAL) 31918c2ecf20Sopenharmony_ci continue; 31928c2ecf20Sopenharmony_ci 31938c2ecf20Sopenharmony_ci /* Throttle based on the first usable node */ 31948c2ecf20Sopenharmony_ci pgdat = zone->zone_pgdat; 31958c2ecf20Sopenharmony_ci if (allow_direct_reclaim(pgdat)) 31968c2ecf20Sopenharmony_ci goto out; 31978c2ecf20Sopenharmony_ci break; 31988c2ecf20Sopenharmony_ci } 31998c2ecf20Sopenharmony_ci 32008c2ecf20Sopenharmony_ci /* If no zone was usable by the allocation flags then do not throttle */ 32018c2ecf20Sopenharmony_ci if (!pgdat) 32028c2ecf20Sopenharmony_ci goto out; 32038c2ecf20Sopenharmony_ci 32048c2ecf20Sopenharmony_ci /* Account for the throttling */ 32058c2ecf20Sopenharmony_ci count_vm_event(PGSCAN_DIRECT_THROTTLE); 32068c2ecf20Sopenharmony_ci 32078c2ecf20Sopenharmony_ci /* 32088c2ecf20Sopenharmony_ci * If the caller cannot enter the filesystem, it's possible that it 32098c2ecf20Sopenharmony_ci * is due to the caller holding an FS lock or performing a journal 32108c2ecf20Sopenharmony_ci * transaction in the case of a filesystem like ext[3|4]. In this case, 32118c2ecf20Sopenharmony_ci * it is not safe to block on pfmemalloc_wait as kswapd could be 32128c2ecf20Sopenharmony_ci * blocked waiting on the same lock. Instead, throttle for up to a 32138c2ecf20Sopenharmony_ci * second before continuing. 32148c2ecf20Sopenharmony_ci */ 32158c2ecf20Sopenharmony_ci if (!(gfp_mask & __GFP_FS)) { 32168c2ecf20Sopenharmony_ci wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 32178c2ecf20Sopenharmony_ci allow_direct_reclaim(pgdat), HZ); 32188c2ecf20Sopenharmony_ci 32198c2ecf20Sopenharmony_ci goto check_pending; 32208c2ecf20Sopenharmony_ci } 32218c2ecf20Sopenharmony_ci 32228c2ecf20Sopenharmony_ci /* Throttle until kswapd wakes the process */ 32238c2ecf20Sopenharmony_ci wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 32248c2ecf20Sopenharmony_ci allow_direct_reclaim(pgdat)); 32258c2ecf20Sopenharmony_ci 32268c2ecf20Sopenharmony_cicheck_pending: 32278c2ecf20Sopenharmony_ci if (fatal_signal_pending(current)) 32288c2ecf20Sopenharmony_ci return true; 32298c2ecf20Sopenharmony_ci 32308c2ecf20Sopenharmony_ciout: 32318c2ecf20Sopenharmony_ci return false; 32328c2ecf20Sopenharmony_ci} 32338c2ecf20Sopenharmony_ci 32348c2ecf20Sopenharmony_ciunsigned long try_to_free_pages(struct zonelist *zonelist, int order, 32358c2ecf20Sopenharmony_ci gfp_t gfp_mask, nodemask_t *nodemask) 32368c2ecf20Sopenharmony_ci{ 32378c2ecf20Sopenharmony_ci unsigned long nr_reclaimed; 32388c2ecf20Sopenharmony_ci struct scan_control sc = { 32398c2ecf20Sopenharmony_ci .nr_to_reclaim = SWAP_CLUSTER_MAX, 32408c2ecf20Sopenharmony_ci .gfp_mask = current_gfp_context(gfp_mask), 32418c2ecf20Sopenharmony_ci .reclaim_idx = gfp_zone(gfp_mask), 32428c2ecf20Sopenharmony_ci .order = order, 32438c2ecf20Sopenharmony_ci .nodemask = nodemask, 32448c2ecf20Sopenharmony_ci .priority = DEF_PRIORITY, 32458c2ecf20Sopenharmony_ci .may_writepage = !laptop_mode, 32468c2ecf20Sopenharmony_ci .may_unmap = 1, 32478c2ecf20Sopenharmony_ci .may_swap = 1, 32488c2ecf20Sopenharmony_ci }; 32498c2ecf20Sopenharmony_ci 32508c2ecf20Sopenharmony_ci /* 32518c2ecf20Sopenharmony_ci * scan_control uses s8 fields for order, priority, and reclaim_idx. 32528c2ecf20Sopenharmony_ci * Confirm they are large enough for max values. 32538c2ecf20Sopenharmony_ci */ 32548c2ecf20Sopenharmony_ci BUILD_BUG_ON(MAX_ORDER > S8_MAX); 32558c2ecf20Sopenharmony_ci BUILD_BUG_ON(DEF_PRIORITY > S8_MAX); 32568c2ecf20Sopenharmony_ci BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX); 32578c2ecf20Sopenharmony_ci 32588c2ecf20Sopenharmony_ci /* 32598c2ecf20Sopenharmony_ci * Do not enter reclaim if fatal signal was delivered while throttled. 32608c2ecf20Sopenharmony_ci * 1 is returned so that the page allocator does not OOM kill at this 32618c2ecf20Sopenharmony_ci * point. 32628c2ecf20Sopenharmony_ci */ 32638c2ecf20Sopenharmony_ci if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask)) 32648c2ecf20Sopenharmony_ci return 1; 32658c2ecf20Sopenharmony_ci 32668c2ecf20Sopenharmony_ci set_task_reclaim_state(current, &sc.reclaim_state); 32678c2ecf20Sopenharmony_ci trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask); 32688c2ecf20Sopenharmony_ci 32698c2ecf20Sopenharmony_ci nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 32708c2ecf20Sopenharmony_ci 32718c2ecf20Sopenharmony_ci trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 32728c2ecf20Sopenharmony_ci set_task_reclaim_state(current, NULL); 32738c2ecf20Sopenharmony_ci 32748c2ecf20Sopenharmony_ci return nr_reclaimed; 32758c2ecf20Sopenharmony_ci} 32768c2ecf20Sopenharmony_ci 32778c2ecf20Sopenharmony_ci#ifdef CONFIG_MEMCG 32788c2ecf20Sopenharmony_ci 32798c2ecf20Sopenharmony_ci/* Only used by soft limit reclaim. Do not reuse for anything else. */ 32808c2ecf20Sopenharmony_ciunsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, 32818c2ecf20Sopenharmony_ci gfp_t gfp_mask, bool noswap, 32828c2ecf20Sopenharmony_ci pg_data_t *pgdat, 32838c2ecf20Sopenharmony_ci unsigned long *nr_scanned) 32848c2ecf20Sopenharmony_ci{ 32858c2ecf20Sopenharmony_ci struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); 32868c2ecf20Sopenharmony_ci struct scan_control sc = { 32878c2ecf20Sopenharmony_ci .nr_to_reclaim = SWAP_CLUSTER_MAX, 32888c2ecf20Sopenharmony_ci .target_mem_cgroup = memcg, 32898c2ecf20Sopenharmony_ci .may_writepage = !laptop_mode, 32908c2ecf20Sopenharmony_ci .may_unmap = 1, 32918c2ecf20Sopenharmony_ci .reclaim_idx = MAX_NR_ZONES - 1, 32928c2ecf20Sopenharmony_ci .may_swap = !noswap, 32938c2ecf20Sopenharmony_ci }; 32948c2ecf20Sopenharmony_ci#ifdef CONFIG_HYPERHOLD_FILE_LRU 32958c2ecf20Sopenharmony_ci unsigned long nr[NR_LRU_LISTS]; 32968c2ecf20Sopenharmony_ci#endif 32978c2ecf20Sopenharmony_ci 32988c2ecf20Sopenharmony_ci WARN_ON_ONCE(!current->reclaim_state); 32998c2ecf20Sopenharmony_ci 33008c2ecf20Sopenharmony_ci sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 33018c2ecf20Sopenharmony_ci (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 33028c2ecf20Sopenharmony_ci 33038c2ecf20Sopenharmony_ci trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 33048c2ecf20Sopenharmony_ci sc.gfp_mask); 33058c2ecf20Sopenharmony_ci 33068c2ecf20Sopenharmony_ci /* 33078c2ecf20Sopenharmony_ci * NOTE: Although we can get the priority field, using it 33088c2ecf20Sopenharmony_ci * here is not a good idea, since it limits the pages we can scan. 33098c2ecf20Sopenharmony_ci * if we don't reclaim here, the shrink_node from balance_pgdat 33108c2ecf20Sopenharmony_ci * will pick up pages from other mem cgroup's as well. We hack 33118c2ecf20Sopenharmony_ci * the priority and make it zero. 33128c2ecf20Sopenharmony_ci */ 33138c2ecf20Sopenharmony_ci#ifdef CONFIG_HYPERHOLD_FILE_LRU 33148c2ecf20Sopenharmony_ci nr[LRU_ACTIVE_ANON] = lruvec_lru_size(lruvec, 33158c2ecf20Sopenharmony_ci LRU_ACTIVE_ANON, MAX_NR_ZONES); 33168c2ecf20Sopenharmony_ci nr[LRU_INACTIVE_ANON] = lruvec_lru_size(lruvec, 33178c2ecf20Sopenharmony_ci LRU_INACTIVE_ANON, MAX_NR_ZONES); 33188c2ecf20Sopenharmony_ci nr[LRU_ACTIVE_FILE] = 0; 33198c2ecf20Sopenharmony_ci nr[LRU_INACTIVE_FILE] = 0; 33208c2ecf20Sopenharmony_ci shrink_anon_memcg(pgdat, memcg, &sc, nr); 33218c2ecf20Sopenharmony_ci#else 33228c2ecf20Sopenharmony_ci shrink_lruvec(lruvec, &sc); 33238c2ecf20Sopenharmony_ci#endif 33248c2ecf20Sopenharmony_ci 33258c2ecf20Sopenharmony_ci trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 33268c2ecf20Sopenharmony_ci 33278c2ecf20Sopenharmony_ci *nr_scanned = sc.nr_scanned; 33288c2ecf20Sopenharmony_ci 33298c2ecf20Sopenharmony_ci return sc.nr_reclaimed; 33308c2ecf20Sopenharmony_ci} 33318c2ecf20Sopenharmony_ci 33328c2ecf20Sopenharmony_ciunsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 33338c2ecf20Sopenharmony_ci unsigned long nr_pages, 33348c2ecf20Sopenharmony_ci gfp_t gfp_mask, 33358c2ecf20Sopenharmony_ci bool may_swap) 33368c2ecf20Sopenharmony_ci{ 33378c2ecf20Sopenharmony_ci unsigned long nr_reclaimed; 33388c2ecf20Sopenharmony_ci unsigned int noreclaim_flag; 33398c2ecf20Sopenharmony_ci struct scan_control sc = { 33408c2ecf20Sopenharmony_ci .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 33418c2ecf20Sopenharmony_ci .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | 33428c2ecf20Sopenharmony_ci (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 33438c2ecf20Sopenharmony_ci .reclaim_idx = MAX_NR_ZONES - 1, 33448c2ecf20Sopenharmony_ci .target_mem_cgroup = memcg, 33458c2ecf20Sopenharmony_ci .priority = DEF_PRIORITY, 33468c2ecf20Sopenharmony_ci .may_writepage = !laptop_mode, 33478c2ecf20Sopenharmony_ci .may_unmap = 1, 33488c2ecf20Sopenharmony_ci .may_swap = may_swap, 33498c2ecf20Sopenharmony_ci }; 33508c2ecf20Sopenharmony_ci /* 33518c2ecf20Sopenharmony_ci * Traverse the ZONELIST_FALLBACK zonelist of the current node to put 33528c2ecf20Sopenharmony_ci * equal pressure on all the nodes. This is based on the assumption that 33538c2ecf20Sopenharmony_ci * the reclaim does not bail out early. 33548c2ecf20Sopenharmony_ci */ 33558c2ecf20Sopenharmony_ci struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 33568c2ecf20Sopenharmony_ci 33578c2ecf20Sopenharmony_ci set_task_reclaim_state(current, &sc.reclaim_state); 33588c2ecf20Sopenharmony_ci trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask); 33598c2ecf20Sopenharmony_ci noreclaim_flag = memalloc_noreclaim_save(); 33608c2ecf20Sopenharmony_ci 33618c2ecf20Sopenharmony_ci nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 33628c2ecf20Sopenharmony_ci 33638c2ecf20Sopenharmony_ci memalloc_noreclaim_restore(noreclaim_flag); 33648c2ecf20Sopenharmony_ci trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 33658c2ecf20Sopenharmony_ci set_task_reclaim_state(current, NULL); 33668c2ecf20Sopenharmony_ci 33678c2ecf20Sopenharmony_ci return nr_reclaimed; 33688c2ecf20Sopenharmony_ci} 33698c2ecf20Sopenharmony_ci#endif 33708c2ecf20Sopenharmony_ci 33718c2ecf20Sopenharmony_cistatic void age_active_anon(struct pglist_data *pgdat, 33728c2ecf20Sopenharmony_ci struct scan_control *sc) 33738c2ecf20Sopenharmony_ci{ 33748c2ecf20Sopenharmony_ci struct mem_cgroup *memcg; 33758c2ecf20Sopenharmony_ci struct lruvec *lruvec; 33768c2ecf20Sopenharmony_ci 33778c2ecf20Sopenharmony_ci if (!total_swap_pages) 33788c2ecf20Sopenharmony_ci return; 33798c2ecf20Sopenharmony_ci 33808c2ecf20Sopenharmony_ci lruvec = mem_cgroup_lruvec(NULL, pgdat); 33818c2ecf20Sopenharmony_ci if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON)) 33828c2ecf20Sopenharmony_ci return; 33838c2ecf20Sopenharmony_ci 33848c2ecf20Sopenharmony_ci memcg = mem_cgroup_iter(NULL, NULL, NULL); 33858c2ecf20Sopenharmony_ci do { 33868c2ecf20Sopenharmony_ci lruvec = mem_cgroup_lruvec(memcg, pgdat); 33878c2ecf20Sopenharmony_ci shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 33888c2ecf20Sopenharmony_ci sc, LRU_ACTIVE_ANON); 33898c2ecf20Sopenharmony_ci memcg = mem_cgroup_iter(NULL, memcg, NULL); 33908c2ecf20Sopenharmony_ci } while (memcg); 33918c2ecf20Sopenharmony_ci} 33928c2ecf20Sopenharmony_ci 33938c2ecf20Sopenharmony_cistatic bool pgdat_watermark_boosted(pg_data_t *pgdat, int highest_zoneidx) 33948c2ecf20Sopenharmony_ci{ 33958c2ecf20Sopenharmony_ci int i; 33968c2ecf20Sopenharmony_ci struct zone *zone; 33978c2ecf20Sopenharmony_ci 33988c2ecf20Sopenharmony_ci /* 33998c2ecf20Sopenharmony_ci * Check for watermark boosts top-down as the higher zones 34008c2ecf20Sopenharmony_ci * are more likely to be boosted. Both watermarks and boosts 34018c2ecf20Sopenharmony_ci * should not be checked at the same time as reclaim would 34028c2ecf20Sopenharmony_ci * start prematurely when there is no boosting and a lower 34038c2ecf20Sopenharmony_ci * zone is balanced. 34048c2ecf20Sopenharmony_ci */ 34058c2ecf20Sopenharmony_ci for (i = highest_zoneidx; i >= 0; i--) { 34068c2ecf20Sopenharmony_ci zone = pgdat->node_zones + i; 34078c2ecf20Sopenharmony_ci if (!managed_zone(zone)) 34088c2ecf20Sopenharmony_ci continue; 34098c2ecf20Sopenharmony_ci 34108c2ecf20Sopenharmony_ci if (zone->watermark_boost) 34118c2ecf20Sopenharmony_ci return true; 34128c2ecf20Sopenharmony_ci } 34138c2ecf20Sopenharmony_ci 34148c2ecf20Sopenharmony_ci return false; 34158c2ecf20Sopenharmony_ci} 34168c2ecf20Sopenharmony_ci 34178c2ecf20Sopenharmony_ci/* 34188c2ecf20Sopenharmony_ci * Returns true if there is an eligible zone balanced for the request order 34198c2ecf20Sopenharmony_ci * and highest_zoneidx 34208c2ecf20Sopenharmony_ci */ 34218c2ecf20Sopenharmony_cistatic bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx) 34228c2ecf20Sopenharmony_ci{ 34238c2ecf20Sopenharmony_ci int i; 34248c2ecf20Sopenharmony_ci unsigned long mark = -1; 34258c2ecf20Sopenharmony_ci struct zone *zone; 34268c2ecf20Sopenharmony_ci 34278c2ecf20Sopenharmony_ci /* 34288c2ecf20Sopenharmony_ci * Check watermarks bottom-up as lower zones are more likely to 34298c2ecf20Sopenharmony_ci * meet watermarks. 34308c2ecf20Sopenharmony_ci */ 34318c2ecf20Sopenharmony_ci for (i = 0; i <= highest_zoneidx; i++) { 34328c2ecf20Sopenharmony_ci zone = pgdat->node_zones + i; 34338c2ecf20Sopenharmony_ci 34348c2ecf20Sopenharmony_ci if (!managed_zone(zone)) 34358c2ecf20Sopenharmony_ci continue; 34368c2ecf20Sopenharmony_ci 34378c2ecf20Sopenharmony_ci mark = high_wmark_pages(zone); 34388c2ecf20Sopenharmony_ci if (zone_watermark_ok_safe(zone, order, mark, highest_zoneidx)) 34398c2ecf20Sopenharmony_ci return true; 34408c2ecf20Sopenharmony_ci } 34418c2ecf20Sopenharmony_ci 34428c2ecf20Sopenharmony_ci /* 34438c2ecf20Sopenharmony_ci * If a node has no populated zone within highest_zoneidx, it does not 34448c2ecf20Sopenharmony_ci * need balancing by definition. This can happen if a zone-restricted 34458c2ecf20Sopenharmony_ci * allocation tries to wake a remote kswapd. 34468c2ecf20Sopenharmony_ci */ 34478c2ecf20Sopenharmony_ci if (mark == -1) 34488c2ecf20Sopenharmony_ci return true; 34498c2ecf20Sopenharmony_ci 34508c2ecf20Sopenharmony_ci return false; 34518c2ecf20Sopenharmony_ci} 34528c2ecf20Sopenharmony_ci 34538c2ecf20Sopenharmony_ci/* Clear pgdat state for congested, dirty or under writeback. */ 34548c2ecf20Sopenharmony_cistatic void clear_pgdat_congested(pg_data_t *pgdat) 34558c2ecf20Sopenharmony_ci{ 34568c2ecf20Sopenharmony_ci struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat); 34578c2ecf20Sopenharmony_ci 34588c2ecf20Sopenharmony_ci clear_bit(LRUVEC_CONGESTED, &lruvec->flags); 34598c2ecf20Sopenharmony_ci clear_bit(PGDAT_DIRTY, &pgdat->flags); 34608c2ecf20Sopenharmony_ci clear_bit(PGDAT_WRITEBACK, &pgdat->flags); 34618c2ecf20Sopenharmony_ci} 34628c2ecf20Sopenharmony_ci 34638c2ecf20Sopenharmony_ci/* 34648c2ecf20Sopenharmony_ci * Prepare kswapd for sleeping. This verifies that there are no processes 34658c2ecf20Sopenharmony_ci * waiting in throttle_direct_reclaim() and that watermarks have been met. 34668c2ecf20Sopenharmony_ci * 34678c2ecf20Sopenharmony_ci * Returns true if kswapd is ready to sleep 34688c2ecf20Sopenharmony_ci */ 34698c2ecf20Sopenharmony_cistatic bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, 34708c2ecf20Sopenharmony_ci int highest_zoneidx) 34718c2ecf20Sopenharmony_ci{ 34728c2ecf20Sopenharmony_ci /* 34738c2ecf20Sopenharmony_ci * The throttled processes are normally woken up in balance_pgdat() as 34748c2ecf20Sopenharmony_ci * soon as allow_direct_reclaim() is true. But there is a potential 34758c2ecf20Sopenharmony_ci * race between when kswapd checks the watermarks and a process gets 34768c2ecf20Sopenharmony_ci * throttled. There is also a potential race if processes get 34778c2ecf20Sopenharmony_ci * throttled, kswapd wakes, a large process exits thereby balancing the 34788c2ecf20Sopenharmony_ci * zones, which causes kswapd to exit balance_pgdat() before reaching 34798c2ecf20Sopenharmony_ci * the wake up checks. If kswapd is going to sleep, no process should 34808c2ecf20Sopenharmony_ci * be sleeping on pfmemalloc_wait, so wake them now if necessary. If 34818c2ecf20Sopenharmony_ci * the wake up is premature, processes will wake kswapd and get 34828c2ecf20Sopenharmony_ci * throttled again. The difference from wake ups in balance_pgdat() is 34838c2ecf20Sopenharmony_ci * that here we are under prepare_to_wait(). 34848c2ecf20Sopenharmony_ci */ 34858c2ecf20Sopenharmony_ci if (waitqueue_active(&pgdat->pfmemalloc_wait)) 34868c2ecf20Sopenharmony_ci wake_up_all(&pgdat->pfmemalloc_wait); 34878c2ecf20Sopenharmony_ci 34888c2ecf20Sopenharmony_ci /* Hopeless node, leave it to direct reclaim */ 34898c2ecf20Sopenharmony_ci if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 34908c2ecf20Sopenharmony_ci return true; 34918c2ecf20Sopenharmony_ci 34928c2ecf20Sopenharmony_ci if (pgdat_balanced(pgdat, order, highest_zoneidx)) { 34938c2ecf20Sopenharmony_ci clear_pgdat_congested(pgdat); 34948c2ecf20Sopenharmony_ci return true; 34958c2ecf20Sopenharmony_ci } 34968c2ecf20Sopenharmony_ci 34978c2ecf20Sopenharmony_ci return false; 34988c2ecf20Sopenharmony_ci} 34998c2ecf20Sopenharmony_ci 35008c2ecf20Sopenharmony_ci/* 35018c2ecf20Sopenharmony_ci * kswapd shrinks a node of pages that are at or below the highest usable 35028c2ecf20Sopenharmony_ci * zone that is currently unbalanced. 35038c2ecf20Sopenharmony_ci * 35048c2ecf20Sopenharmony_ci * Returns true if kswapd scanned at least the requested number of pages to 35058c2ecf20Sopenharmony_ci * reclaim or if the lack of progress was due to pages under writeback. 35068c2ecf20Sopenharmony_ci * This is used to determine if the scanning priority needs to be raised. 35078c2ecf20Sopenharmony_ci */ 35088c2ecf20Sopenharmony_cistatic bool kswapd_shrink_node(pg_data_t *pgdat, 35098c2ecf20Sopenharmony_ci struct scan_control *sc) 35108c2ecf20Sopenharmony_ci{ 35118c2ecf20Sopenharmony_ci struct zone *zone; 35128c2ecf20Sopenharmony_ci int z; 35138c2ecf20Sopenharmony_ci 35148c2ecf20Sopenharmony_ci /* Reclaim a number of pages proportional to the number of zones */ 35158c2ecf20Sopenharmony_ci sc->nr_to_reclaim = 0; 35168c2ecf20Sopenharmony_ci for (z = 0; z <= sc->reclaim_idx; z++) { 35178c2ecf20Sopenharmony_ci zone = pgdat->node_zones + z; 35188c2ecf20Sopenharmony_ci if (!managed_zone(zone)) 35198c2ecf20Sopenharmony_ci continue; 35208c2ecf20Sopenharmony_ci 35218c2ecf20Sopenharmony_ci sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); 35228c2ecf20Sopenharmony_ci } 35238c2ecf20Sopenharmony_ci 35248c2ecf20Sopenharmony_ci /* 35258c2ecf20Sopenharmony_ci * Historically care was taken to put equal pressure on all zones but 35268c2ecf20Sopenharmony_ci * now pressure is applied based on node LRU order. 35278c2ecf20Sopenharmony_ci */ 35288c2ecf20Sopenharmony_ci#ifdef CONFIG_HYPERHOLD_FILE_LRU 35298c2ecf20Sopenharmony_ci shrink_node_hyperhold(pgdat, sc); 35308c2ecf20Sopenharmony_ci#else 35318c2ecf20Sopenharmony_ci shrink_node(pgdat, sc); 35328c2ecf20Sopenharmony_ci#endif 35338c2ecf20Sopenharmony_ci 35348c2ecf20Sopenharmony_ci /* 35358c2ecf20Sopenharmony_ci * Fragmentation may mean that the system cannot be rebalanced for 35368c2ecf20Sopenharmony_ci * high-order allocations. If twice the allocation size has been 35378c2ecf20Sopenharmony_ci * reclaimed then recheck watermarks only at order-0 to prevent 35388c2ecf20Sopenharmony_ci * excessive reclaim. Assume that a process requested a high-order 35398c2ecf20Sopenharmony_ci * can direct reclaim/compact. 35408c2ecf20Sopenharmony_ci */ 35418c2ecf20Sopenharmony_ci if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order)) 35428c2ecf20Sopenharmony_ci sc->order = 0; 35438c2ecf20Sopenharmony_ci 35448c2ecf20Sopenharmony_ci return sc->nr_scanned >= sc->nr_to_reclaim; 35458c2ecf20Sopenharmony_ci} 35468c2ecf20Sopenharmony_ci 35478c2ecf20Sopenharmony_ci/* 35488c2ecf20Sopenharmony_ci * For kswapd, balance_pgdat() will reclaim pages across a node from zones 35498c2ecf20Sopenharmony_ci * that are eligible for use by the caller until at least one zone is 35508c2ecf20Sopenharmony_ci * balanced. 35518c2ecf20Sopenharmony_ci * 35528c2ecf20Sopenharmony_ci * Returns the order kswapd finished reclaiming at. 35538c2ecf20Sopenharmony_ci * 35548c2ecf20Sopenharmony_ci * kswapd scans the zones in the highmem->normal->dma direction. It skips 35558c2ecf20Sopenharmony_ci * zones which have free_pages > high_wmark_pages(zone), but once a zone is 35568c2ecf20Sopenharmony_ci * found to have free_pages <= high_wmark_pages(zone), any page in that zone 35578c2ecf20Sopenharmony_ci * or lower is eligible for reclaim until at least one usable zone is 35588c2ecf20Sopenharmony_ci * balanced. 35598c2ecf20Sopenharmony_ci */ 35608c2ecf20Sopenharmony_cistatic int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx) 35618c2ecf20Sopenharmony_ci{ 35628c2ecf20Sopenharmony_ci int i; 35638c2ecf20Sopenharmony_ci unsigned long nr_soft_reclaimed; 35648c2ecf20Sopenharmony_ci unsigned long nr_soft_scanned; 35658c2ecf20Sopenharmony_ci unsigned long pflags; 35668c2ecf20Sopenharmony_ci unsigned long nr_boost_reclaim; 35678c2ecf20Sopenharmony_ci unsigned long zone_boosts[MAX_NR_ZONES] = { 0, }; 35688c2ecf20Sopenharmony_ci bool boosted; 35698c2ecf20Sopenharmony_ci struct zone *zone; 35708c2ecf20Sopenharmony_ci struct scan_control sc = { 35718c2ecf20Sopenharmony_ci .gfp_mask = GFP_KERNEL, 35728c2ecf20Sopenharmony_ci .order = order, 35738c2ecf20Sopenharmony_ci .may_unmap = 1, 35748c2ecf20Sopenharmony_ci }; 35758c2ecf20Sopenharmony_ci 35768c2ecf20Sopenharmony_ci set_task_reclaim_state(current, &sc.reclaim_state); 35778c2ecf20Sopenharmony_ci psi_memstall_enter(&pflags); 35788c2ecf20Sopenharmony_ci __fs_reclaim_acquire(); 35798c2ecf20Sopenharmony_ci 35808c2ecf20Sopenharmony_ci count_vm_event(PAGEOUTRUN); 35818c2ecf20Sopenharmony_ci 35828c2ecf20Sopenharmony_ci /* 35838c2ecf20Sopenharmony_ci * Account for the reclaim boost. Note that the zone boost is left in 35848c2ecf20Sopenharmony_ci * place so that parallel allocations that are near the watermark will 35858c2ecf20Sopenharmony_ci * stall or direct reclaim until kswapd is finished. 35868c2ecf20Sopenharmony_ci */ 35878c2ecf20Sopenharmony_ci nr_boost_reclaim = 0; 35888c2ecf20Sopenharmony_ci for (i = 0; i <= highest_zoneidx; i++) { 35898c2ecf20Sopenharmony_ci zone = pgdat->node_zones + i; 35908c2ecf20Sopenharmony_ci if (!managed_zone(zone)) 35918c2ecf20Sopenharmony_ci continue; 35928c2ecf20Sopenharmony_ci 35938c2ecf20Sopenharmony_ci nr_boost_reclaim += zone->watermark_boost; 35948c2ecf20Sopenharmony_ci zone_boosts[i] = zone->watermark_boost; 35958c2ecf20Sopenharmony_ci } 35968c2ecf20Sopenharmony_ci boosted = nr_boost_reclaim; 35978c2ecf20Sopenharmony_ci 35988c2ecf20Sopenharmony_cirestart: 35998c2ecf20Sopenharmony_ci sc.priority = DEF_PRIORITY; 36008c2ecf20Sopenharmony_ci do { 36018c2ecf20Sopenharmony_ci unsigned long nr_reclaimed = sc.nr_reclaimed; 36028c2ecf20Sopenharmony_ci bool raise_priority = true; 36038c2ecf20Sopenharmony_ci bool balanced; 36048c2ecf20Sopenharmony_ci bool ret; 36058c2ecf20Sopenharmony_ci 36068c2ecf20Sopenharmony_ci sc.reclaim_idx = highest_zoneidx; 36078c2ecf20Sopenharmony_ci 36088c2ecf20Sopenharmony_ci /* 36098c2ecf20Sopenharmony_ci * If the number of buffer_heads exceeds the maximum allowed 36108c2ecf20Sopenharmony_ci * then consider reclaiming from all zones. This has a dual 36118c2ecf20Sopenharmony_ci * purpose -- on 64-bit systems it is expected that 36128c2ecf20Sopenharmony_ci * buffer_heads are stripped during active rotation. On 32-bit 36138c2ecf20Sopenharmony_ci * systems, highmem pages can pin lowmem memory and shrinking 36148c2ecf20Sopenharmony_ci * buffers can relieve lowmem pressure. Reclaim may still not 36158c2ecf20Sopenharmony_ci * go ahead if all eligible zones for the original allocation 36168c2ecf20Sopenharmony_ci * request are balanced to avoid excessive reclaim from kswapd. 36178c2ecf20Sopenharmony_ci */ 36188c2ecf20Sopenharmony_ci if (buffer_heads_over_limit) { 36198c2ecf20Sopenharmony_ci for (i = MAX_NR_ZONES - 1; i >= 0; i--) { 36208c2ecf20Sopenharmony_ci zone = pgdat->node_zones + i; 36218c2ecf20Sopenharmony_ci if (!managed_zone(zone)) 36228c2ecf20Sopenharmony_ci continue; 36238c2ecf20Sopenharmony_ci 36248c2ecf20Sopenharmony_ci sc.reclaim_idx = i; 36258c2ecf20Sopenharmony_ci break; 36268c2ecf20Sopenharmony_ci } 36278c2ecf20Sopenharmony_ci } 36288c2ecf20Sopenharmony_ci 36298c2ecf20Sopenharmony_ci /* 36308c2ecf20Sopenharmony_ci * If the pgdat is imbalanced then ignore boosting and preserve 36318c2ecf20Sopenharmony_ci * the watermarks for a later time and restart. Note that the 36328c2ecf20Sopenharmony_ci * zone watermarks will be still reset at the end of balancing 36338c2ecf20Sopenharmony_ci * on the grounds that the normal reclaim should be enough to 36348c2ecf20Sopenharmony_ci * re-evaluate if boosting is required when kswapd next wakes. 36358c2ecf20Sopenharmony_ci */ 36368c2ecf20Sopenharmony_ci balanced = pgdat_balanced(pgdat, sc.order, highest_zoneidx); 36378c2ecf20Sopenharmony_ci if (!balanced && nr_boost_reclaim) { 36388c2ecf20Sopenharmony_ci nr_boost_reclaim = 0; 36398c2ecf20Sopenharmony_ci goto restart; 36408c2ecf20Sopenharmony_ci } 36418c2ecf20Sopenharmony_ci 36428c2ecf20Sopenharmony_ci /* 36438c2ecf20Sopenharmony_ci * If boosting is not active then only reclaim if there are no 36448c2ecf20Sopenharmony_ci * eligible zones. Note that sc.reclaim_idx is not used as 36458c2ecf20Sopenharmony_ci * buffer_heads_over_limit may have adjusted it. 36468c2ecf20Sopenharmony_ci */ 36478c2ecf20Sopenharmony_ci if (!nr_boost_reclaim && balanced) 36488c2ecf20Sopenharmony_ci goto out; 36498c2ecf20Sopenharmony_ci 36508c2ecf20Sopenharmony_ci /* Limit the priority of boosting to avoid reclaim writeback */ 36518c2ecf20Sopenharmony_ci if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2) 36528c2ecf20Sopenharmony_ci raise_priority = false; 36538c2ecf20Sopenharmony_ci 36548c2ecf20Sopenharmony_ci /* 36558c2ecf20Sopenharmony_ci * Do not writeback or swap pages for boosted reclaim. The 36568c2ecf20Sopenharmony_ci * intent is to relieve pressure not issue sub-optimal IO 36578c2ecf20Sopenharmony_ci * from reclaim context. If no pages are reclaimed, the 36588c2ecf20Sopenharmony_ci * reclaim will be aborted. 36598c2ecf20Sopenharmony_ci */ 36608c2ecf20Sopenharmony_ci sc.may_writepage = !laptop_mode && !nr_boost_reclaim; 36618c2ecf20Sopenharmony_ci sc.may_swap = !nr_boost_reclaim; 36628c2ecf20Sopenharmony_ci 36638c2ecf20Sopenharmony_ci /* 36648c2ecf20Sopenharmony_ci * Do some background aging of the anon list, to give 36658c2ecf20Sopenharmony_ci * pages a chance to be referenced before reclaiming. All 36668c2ecf20Sopenharmony_ci * pages are rotated regardless of classzone as this is 36678c2ecf20Sopenharmony_ci * about consistent aging. 36688c2ecf20Sopenharmony_ci */ 36698c2ecf20Sopenharmony_ci age_active_anon(pgdat, &sc); 36708c2ecf20Sopenharmony_ci 36718c2ecf20Sopenharmony_ci /* 36728c2ecf20Sopenharmony_ci * If we're getting trouble reclaiming, start doing writepage 36738c2ecf20Sopenharmony_ci * even in laptop mode. 36748c2ecf20Sopenharmony_ci */ 36758c2ecf20Sopenharmony_ci if (sc.priority < DEF_PRIORITY - 2) 36768c2ecf20Sopenharmony_ci sc.may_writepage = 1; 36778c2ecf20Sopenharmony_ci 36788c2ecf20Sopenharmony_ci /* Call soft limit reclaim before calling shrink_node. */ 36798c2ecf20Sopenharmony_ci sc.nr_scanned = 0; 36808c2ecf20Sopenharmony_ci nr_soft_scanned = 0; 36818c2ecf20Sopenharmony_ci nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, 36828c2ecf20Sopenharmony_ci sc.gfp_mask, &nr_soft_scanned); 36838c2ecf20Sopenharmony_ci sc.nr_reclaimed += nr_soft_reclaimed; 36848c2ecf20Sopenharmony_ci 36858c2ecf20Sopenharmony_ci /* 36868c2ecf20Sopenharmony_ci * There should be no need to raise the scanning priority if 36878c2ecf20Sopenharmony_ci * enough pages are already being scanned that that high 36888c2ecf20Sopenharmony_ci * watermark would be met at 100% efficiency. 36898c2ecf20Sopenharmony_ci */ 36908c2ecf20Sopenharmony_ci if (kswapd_shrink_node(pgdat, &sc)) 36918c2ecf20Sopenharmony_ci raise_priority = false; 36928c2ecf20Sopenharmony_ci 36938c2ecf20Sopenharmony_ci /* 36948c2ecf20Sopenharmony_ci * If the low watermark is met there is no need for processes 36958c2ecf20Sopenharmony_ci * to be throttled on pfmemalloc_wait as they should not be 36968c2ecf20Sopenharmony_ci * able to safely make forward progress. Wake them 36978c2ecf20Sopenharmony_ci */ 36988c2ecf20Sopenharmony_ci if (waitqueue_active(&pgdat->pfmemalloc_wait) && 36998c2ecf20Sopenharmony_ci allow_direct_reclaim(pgdat)) 37008c2ecf20Sopenharmony_ci wake_up_all(&pgdat->pfmemalloc_wait); 37018c2ecf20Sopenharmony_ci 37028c2ecf20Sopenharmony_ci /* Check if kswapd should be suspending */ 37038c2ecf20Sopenharmony_ci __fs_reclaim_release(); 37048c2ecf20Sopenharmony_ci ret = try_to_freeze(); 37058c2ecf20Sopenharmony_ci __fs_reclaim_acquire(); 37068c2ecf20Sopenharmony_ci if (ret || kthread_should_stop()) 37078c2ecf20Sopenharmony_ci break; 37088c2ecf20Sopenharmony_ci 37098c2ecf20Sopenharmony_ci /* 37108c2ecf20Sopenharmony_ci * Raise priority if scanning rate is too low or there was no 37118c2ecf20Sopenharmony_ci * progress in reclaiming pages 37128c2ecf20Sopenharmony_ci */ 37138c2ecf20Sopenharmony_ci nr_reclaimed = sc.nr_reclaimed - nr_reclaimed; 37148c2ecf20Sopenharmony_ci nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed); 37158c2ecf20Sopenharmony_ci 37168c2ecf20Sopenharmony_ci /* 37178c2ecf20Sopenharmony_ci * If reclaim made no progress for a boost, stop reclaim as 37188c2ecf20Sopenharmony_ci * IO cannot be queued and it could be an infinite loop in 37198c2ecf20Sopenharmony_ci * extreme circumstances. 37208c2ecf20Sopenharmony_ci */ 37218c2ecf20Sopenharmony_ci if (nr_boost_reclaim && !nr_reclaimed) 37228c2ecf20Sopenharmony_ci break; 37238c2ecf20Sopenharmony_ci 37248c2ecf20Sopenharmony_ci if (raise_priority || !nr_reclaimed) 37258c2ecf20Sopenharmony_ci sc.priority--; 37268c2ecf20Sopenharmony_ci } while (sc.priority >= 1); 37278c2ecf20Sopenharmony_ci 37288c2ecf20Sopenharmony_ci if (!sc.nr_reclaimed) 37298c2ecf20Sopenharmony_ci pgdat->kswapd_failures++; 37308c2ecf20Sopenharmony_ci 37318c2ecf20Sopenharmony_ciout: 37328c2ecf20Sopenharmony_ci /* If reclaim was boosted, account for the reclaim done in this pass */ 37338c2ecf20Sopenharmony_ci if (boosted) { 37348c2ecf20Sopenharmony_ci unsigned long flags; 37358c2ecf20Sopenharmony_ci 37368c2ecf20Sopenharmony_ci for (i = 0; i <= highest_zoneidx; i++) { 37378c2ecf20Sopenharmony_ci if (!zone_boosts[i]) 37388c2ecf20Sopenharmony_ci continue; 37398c2ecf20Sopenharmony_ci 37408c2ecf20Sopenharmony_ci /* Increments are under the zone lock */ 37418c2ecf20Sopenharmony_ci zone = pgdat->node_zones + i; 37428c2ecf20Sopenharmony_ci spin_lock_irqsave(&zone->lock, flags); 37438c2ecf20Sopenharmony_ci zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]); 37448c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&zone->lock, flags); 37458c2ecf20Sopenharmony_ci } 37468c2ecf20Sopenharmony_ci 37478c2ecf20Sopenharmony_ci /* 37488c2ecf20Sopenharmony_ci * As there is now likely space, wakeup kcompact to defragment 37498c2ecf20Sopenharmony_ci * pageblocks. 37508c2ecf20Sopenharmony_ci */ 37518c2ecf20Sopenharmony_ci wakeup_kcompactd(pgdat, pageblock_order, highest_zoneidx); 37528c2ecf20Sopenharmony_ci } 37538c2ecf20Sopenharmony_ci 37548c2ecf20Sopenharmony_ci snapshot_refaults(NULL, pgdat); 37558c2ecf20Sopenharmony_ci __fs_reclaim_release(); 37568c2ecf20Sopenharmony_ci psi_memstall_leave(&pflags); 37578c2ecf20Sopenharmony_ci set_task_reclaim_state(current, NULL); 37588c2ecf20Sopenharmony_ci 37598c2ecf20Sopenharmony_ci /* 37608c2ecf20Sopenharmony_ci * Return the order kswapd stopped reclaiming at as 37618c2ecf20Sopenharmony_ci * prepare_kswapd_sleep() takes it into account. If another caller 37628c2ecf20Sopenharmony_ci * entered the allocator slow path while kswapd was awake, order will 37638c2ecf20Sopenharmony_ci * remain at the higher level. 37648c2ecf20Sopenharmony_ci */ 37658c2ecf20Sopenharmony_ci return sc.order; 37668c2ecf20Sopenharmony_ci} 37678c2ecf20Sopenharmony_ci 37688c2ecf20Sopenharmony_ci/* 37698c2ecf20Sopenharmony_ci * The pgdat->kswapd_highest_zoneidx is used to pass the highest zone index to 37708c2ecf20Sopenharmony_ci * be reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is 37718c2ecf20Sopenharmony_ci * not a valid index then either kswapd runs for first time or kswapd couldn't 37728c2ecf20Sopenharmony_ci * sleep after previous reclaim attempt (node is still unbalanced). In that 37738c2ecf20Sopenharmony_ci * case return the zone index of the previous kswapd reclaim cycle. 37748c2ecf20Sopenharmony_ci */ 37758c2ecf20Sopenharmony_cistatic enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat, 37768c2ecf20Sopenharmony_ci enum zone_type prev_highest_zoneidx) 37778c2ecf20Sopenharmony_ci{ 37788c2ecf20Sopenharmony_ci enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); 37798c2ecf20Sopenharmony_ci 37808c2ecf20Sopenharmony_ci return curr_idx == MAX_NR_ZONES ? prev_highest_zoneidx : curr_idx; 37818c2ecf20Sopenharmony_ci} 37828c2ecf20Sopenharmony_ci 37838c2ecf20Sopenharmony_cistatic void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, 37848c2ecf20Sopenharmony_ci unsigned int highest_zoneidx) 37858c2ecf20Sopenharmony_ci{ 37868c2ecf20Sopenharmony_ci long remaining = 0; 37878c2ecf20Sopenharmony_ci DEFINE_WAIT(wait); 37888c2ecf20Sopenharmony_ci 37898c2ecf20Sopenharmony_ci if (freezing(current) || kthread_should_stop()) 37908c2ecf20Sopenharmony_ci return; 37918c2ecf20Sopenharmony_ci 37928c2ecf20Sopenharmony_ci prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 37938c2ecf20Sopenharmony_ci 37948c2ecf20Sopenharmony_ci /* 37958c2ecf20Sopenharmony_ci * Try to sleep for a short interval. Note that kcompactd will only be 37968c2ecf20Sopenharmony_ci * woken if it is possible to sleep for a short interval. This is 37978c2ecf20Sopenharmony_ci * deliberate on the assumption that if reclaim cannot keep an 37988c2ecf20Sopenharmony_ci * eligible zone balanced that it's also unlikely that compaction will 37998c2ecf20Sopenharmony_ci * succeed. 38008c2ecf20Sopenharmony_ci */ 38018c2ecf20Sopenharmony_ci if (prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { 38028c2ecf20Sopenharmony_ci /* 38038c2ecf20Sopenharmony_ci * Compaction records what page blocks it recently failed to 38048c2ecf20Sopenharmony_ci * isolate pages from and skips them in the future scanning. 38058c2ecf20Sopenharmony_ci * When kswapd is going to sleep, it is reasonable to assume 38068c2ecf20Sopenharmony_ci * that pages and compaction may succeed so reset the cache. 38078c2ecf20Sopenharmony_ci */ 38088c2ecf20Sopenharmony_ci reset_isolation_suitable(pgdat); 38098c2ecf20Sopenharmony_ci 38108c2ecf20Sopenharmony_ci /* 38118c2ecf20Sopenharmony_ci * We have freed the memory, now we should compact it to make 38128c2ecf20Sopenharmony_ci * allocation of the requested order possible. 38138c2ecf20Sopenharmony_ci */ 38148c2ecf20Sopenharmony_ci wakeup_kcompactd(pgdat, alloc_order, highest_zoneidx); 38158c2ecf20Sopenharmony_ci 38168c2ecf20Sopenharmony_ci remaining = schedule_timeout(HZ/10); 38178c2ecf20Sopenharmony_ci 38188c2ecf20Sopenharmony_ci /* 38198c2ecf20Sopenharmony_ci * If woken prematurely then reset kswapd_highest_zoneidx and 38208c2ecf20Sopenharmony_ci * order. The values will either be from a wakeup request or 38218c2ecf20Sopenharmony_ci * the previous request that slept prematurely. 38228c2ecf20Sopenharmony_ci */ 38238c2ecf20Sopenharmony_ci if (remaining) { 38248c2ecf20Sopenharmony_ci WRITE_ONCE(pgdat->kswapd_highest_zoneidx, 38258c2ecf20Sopenharmony_ci kswapd_highest_zoneidx(pgdat, 38268c2ecf20Sopenharmony_ci highest_zoneidx)); 38278c2ecf20Sopenharmony_ci 38288c2ecf20Sopenharmony_ci if (READ_ONCE(pgdat->kswapd_order) < reclaim_order) 38298c2ecf20Sopenharmony_ci WRITE_ONCE(pgdat->kswapd_order, reclaim_order); 38308c2ecf20Sopenharmony_ci } 38318c2ecf20Sopenharmony_ci 38328c2ecf20Sopenharmony_ci finish_wait(&pgdat->kswapd_wait, &wait); 38338c2ecf20Sopenharmony_ci prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 38348c2ecf20Sopenharmony_ci } 38358c2ecf20Sopenharmony_ci 38368c2ecf20Sopenharmony_ci /* 38378c2ecf20Sopenharmony_ci * After a short sleep, check if it was a premature sleep. If not, then 38388c2ecf20Sopenharmony_ci * go fully to sleep until explicitly woken up. 38398c2ecf20Sopenharmony_ci */ 38408c2ecf20Sopenharmony_ci if (!remaining && 38418c2ecf20Sopenharmony_ci prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { 38428c2ecf20Sopenharmony_ci trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 38438c2ecf20Sopenharmony_ci 38448c2ecf20Sopenharmony_ci /* 38458c2ecf20Sopenharmony_ci * vmstat counters are not perfectly accurate and the estimated 38468c2ecf20Sopenharmony_ci * value for counters such as NR_FREE_PAGES can deviate from the 38478c2ecf20Sopenharmony_ci * true value by nr_online_cpus * threshold. To avoid the zone 38488c2ecf20Sopenharmony_ci * watermarks being breached while under pressure, we reduce the 38498c2ecf20Sopenharmony_ci * per-cpu vmstat threshold while kswapd is awake and restore 38508c2ecf20Sopenharmony_ci * them before going back to sleep. 38518c2ecf20Sopenharmony_ci */ 38528c2ecf20Sopenharmony_ci set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 38538c2ecf20Sopenharmony_ci 38548c2ecf20Sopenharmony_ci if (!kthread_should_stop()) 38558c2ecf20Sopenharmony_ci schedule(); 38568c2ecf20Sopenharmony_ci 38578c2ecf20Sopenharmony_ci set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 38588c2ecf20Sopenharmony_ci } else { 38598c2ecf20Sopenharmony_ci if (remaining) 38608c2ecf20Sopenharmony_ci count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 38618c2ecf20Sopenharmony_ci else 38628c2ecf20Sopenharmony_ci count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 38638c2ecf20Sopenharmony_ci } 38648c2ecf20Sopenharmony_ci finish_wait(&pgdat->kswapd_wait, &wait); 38658c2ecf20Sopenharmony_ci} 38668c2ecf20Sopenharmony_ci 38678c2ecf20Sopenharmony_ci/* 38688c2ecf20Sopenharmony_ci * The background pageout daemon, started as a kernel thread 38698c2ecf20Sopenharmony_ci * from the init process. 38708c2ecf20Sopenharmony_ci * 38718c2ecf20Sopenharmony_ci * This basically trickles out pages so that we have _some_ 38728c2ecf20Sopenharmony_ci * free memory available even if there is no other activity 38738c2ecf20Sopenharmony_ci * that frees anything up. This is needed for things like routing 38748c2ecf20Sopenharmony_ci * etc, where we otherwise might have all activity going on in 38758c2ecf20Sopenharmony_ci * asynchronous contexts that cannot page things out. 38768c2ecf20Sopenharmony_ci * 38778c2ecf20Sopenharmony_ci * If there are applications that are active memory-allocators 38788c2ecf20Sopenharmony_ci * (most normal use), this basically shouldn't matter. 38798c2ecf20Sopenharmony_ci */ 38808c2ecf20Sopenharmony_cistatic int kswapd(void *p) 38818c2ecf20Sopenharmony_ci{ 38828c2ecf20Sopenharmony_ci unsigned int alloc_order, reclaim_order; 38838c2ecf20Sopenharmony_ci unsigned int highest_zoneidx = MAX_NR_ZONES - 1; 38848c2ecf20Sopenharmony_ci pg_data_t *pgdat = (pg_data_t*)p; 38858c2ecf20Sopenharmony_ci struct task_struct *tsk = current; 38868c2ecf20Sopenharmony_ci const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 38878c2ecf20Sopenharmony_ci#ifdef CONFIG_RECLAIM_ACCT 38888c2ecf20Sopenharmony_ci struct reclaim_acct ra = {0}; 38898c2ecf20Sopenharmony_ci#endif 38908c2ecf20Sopenharmony_ci 38918c2ecf20Sopenharmony_ci if (!cpumask_empty(cpumask)) 38928c2ecf20Sopenharmony_ci set_cpus_allowed_ptr(tsk, cpumask); 38938c2ecf20Sopenharmony_ci 38948c2ecf20Sopenharmony_ci /* 38958c2ecf20Sopenharmony_ci * Tell the memory management that we're a "memory allocator", 38968c2ecf20Sopenharmony_ci * and that if we need more memory we should get access to it 38978c2ecf20Sopenharmony_ci * regardless (see "__alloc_pages()"). "kswapd" should 38988c2ecf20Sopenharmony_ci * never get caught in the normal page freeing logic. 38998c2ecf20Sopenharmony_ci * 39008c2ecf20Sopenharmony_ci * (Kswapd normally doesn't need memory anyway, but sometimes 39018c2ecf20Sopenharmony_ci * you need a small amount of memory in order to be able to 39028c2ecf20Sopenharmony_ci * page out something else, and this flag essentially protects 39038c2ecf20Sopenharmony_ci * us from recursively trying to free more memory as we're 39048c2ecf20Sopenharmony_ci * trying to free the first piece of memory in the first place). 39058c2ecf20Sopenharmony_ci */ 39068c2ecf20Sopenharmony_ci tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 39078c2ecf20Sopenharmony_ci set_freezable(); 39088c2ecf20Sopenharmony_ci 39098c2ecf20Sopenharmony_ci WRITE_ONCE(pgdat->kswapd_order, 0); 39108c2ecf20Sopenharmony_ci WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); 39118c2ecf20Sopenharmony_ci for ( ; ; ) { 39128c2ecf20Sopenharmony_ci bool ret; 39138c2ecf20Sopenharmony_ci 39148c2ecf20Sopenharmony_ci alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order); 39158c2ecf20Sopenharmony_ci highest_zoneidx = kswapd_highest_zoneidx(pgdat, 39168c2ecf20Sopenharmony_ci highest_zoneidx); 39178c2ecf20Sopenharmony_ci 39188c2ecf20Sopenharmony_cikswapd_try_sleep: 39198c2ecf20Sopenharmony_ci kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, 39208c2ecf20Sopenharmony_ci highest_zoneidx); 39218c2ecf20Sopenharmony_ci 39228c2ecf20Sopenharmony_ci /* Read the new order and highest_zoneidx */ 39238c2ecf20Sopenharmony_ci alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order); 39248c2ecf20Sopenharmony_ci highest_zoneidx = kswapd_highest_zoneidx(pgdat, 39258c2ecf20Sopenharmony_ci highest_zoneidx); 39268c2ecf20Sopenharmony_ci WRITE_ONCE(pgdat->kswapd_order, 0); 39278c2ecf20Sopenharmony_ci WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); 39288c2ecf20Sopenharmony_ci 39298c2ecf20Sopenharmony_ci ret = try_to_freeze(); 39308c2ecf20Sopenharmony_ci if (kthread_should_stop()) 39318c2ecf20Sopenharmony_ci break; 39328c2ecf20Sopenharmony_ci 39338c2ecf20Sopenharmony_ci /* 39348c2ecf20Sopenharmony_ci * We can speed up thawing tasks if we don't call balance_pgdat 39358c2ecf20Sopenharmony_ci * after returning from the refrigerator 39368c2ecf20Sopenharmony_ci */ 39378c2ecf20Sopenharmony_ci if (ret) 39388c2ecf20Sopenharmony_ci continue; 39398c2ecf20Sopenharmony_ci 39408c2ecf20Sopenharmony_ci /* 39418c2ecf20Sopenharmony_ci * Reclaim begins at the requested order but if a high-order 39428c2ecf20Sopenharmony_ci * reclaim fails then kswapd falls back to reclaiming for 39438c2ecf20Sopenharmony_ci * order-0. If that happens, kswapd will consider sleeping 39448c2ecf20Sopenharmony_ci * for the order it finished reclaiming at (reclaim_order) 39458c2ecf20Sopenharmony_ci * but kcompactd is woken to compact for the original 39468c2ecf20Sopenharmony_ci * request (alloc_order). 39478c2ecf20Sopenharmony_ci */ 39488c2ecf20Sopenharmony_ci trace_mm_vmscan_kswapd_wake(pgdat->node_id, highest_zoneidx, 39498c2ecf20Sopenharmony_ci alloc_order); 39508c2ecf20Sopenharmony_ci#ifdef CONFIG_MEMORY_MONITOR 39518c2ecf20Sopenharmony_ci kswapd_monitor_wake_up_queue(); 39528c2ecf20Sopenharmony_ci#endif 39538c2ecf20Sopenharmony_ci#ifdef CONFIG_RECLAIM_ACCT 39548c2ecf20Sopenharmony_ci reclaimacct_start(KSWAPD_RECLAIM, &ra); 39558c2ecf20Sopenharmony_ci#endif 39568c2ecf20Sopenharmony_ci reclaim_order = balance_pgdat(pgdat, alloc_order, 39578c2ecf20Sopenharmony_ci highest_zoneidx); 39588c2ecf20Sopenharmony_ci#ifdef CONFIG_RECLAIM_ACCT 39598c2ecf20Sopenharmony_ci reclaimacct_end(KSWAPD_RECLAIM); 39608c2ecf20Sopenharmony_ci#endif 39618c2ecf20Sopenharmony_ci if (reclaim_order < alloc_order) 39628c2ecf20Sopenharmony_ci goto kswapd_try_sleep; 39638c2ecf20Sopenharmony_ci } 39648c2ecf20Sopenharmony_ci 39658c2ecf20Sopenharmony_ci tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); 39668c2ecf20Sopenharmony_ci 39678c2ecf20Sopenharmony_ci return 0; 39688c2ecf20Sopenharmony_ci} 39698c2ecf20Sopenharmony_ci 39708c2ecf20Sopenharmony_ci/* 39718c2ecf20Sopenharmony_ci * A zone is low on free memory or too fragmented for high-order memory. If 39728c2ecf20Sopenharmony_ci * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's 39738c2ecf20Sopenharmony_ci * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim 39748c2ecf20Sopenharmony_ci * has failed or is not needed, still wake up kcompactd if only compaction is 39758c2ecf20Sopenharmony_ci * needed. 39768c2ecf20Sopenharmony_ci */ 39778c2ecf20Sopenharmony_civoid wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, 39788c2ecf20Sopenharmony_ci enum zone_type highest_zoneidx) 39798c2ecf20Sopenharmony_ci{ 39808c2ecf20Sopenharmony_ci pg_data_t *pgdat; 39818c2ecf20Sopenharmony_ci enum zone_type curr_idx; 39828c2ecf20Sopenharmony_ci 39838c2ecf20Sopenharmony_ci if (!managed_zone(zone)) 39848c2ecf20Sopenharmony_ci return; 39858c2ecf20Sopenharmony_ci 39868c2ecf20Sopenharmony_ci if (!cpuset_zone_allowed(zone, gfp_flags)) 39878c2ecf20Sopenharmony_ci return; 39888c2ecf20Sopenharmony_ci 39898c2ecf20Sopenharmony_ci pgdat = zone->zone_pgdat; 39908c2ecf20Sopenharmony_ci curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); 39918c2ecf20Sopenharmony_ci 39928c2ecf20Sopenharmony_ci if (curr_idx == MAX_NR_ZONES || curr_idx < highest_zoneidx) 39938c2ecf20Sopenharmony_ci WRITE_ONCE(pgdat->kswapd_highest_zoneidx, highest_zoneidx); 39948c2ecf20Sopenharmony_ci 39958c2ecf20Sopenharmony_ci if (READ_ONCE(pgdat->kswapd_order) < order) 39968c2ecf20Sopenharmony_ci WRITE_ONCE(pgdat->kswapd_order, order); 39978c2ecf20Sopenharmony_ci 39988c2ecf20Sopenharmony_ci if (!waitqueue_active(&pgdat->kswapd_wait)) 39998c2ecf20Sopenharmony_ci return; 40008c2ecf20Sopenharmony_ci 40018c2ecf20Sopenharmony_ci /* Hopeless node, leave it to direct reclaim if possible */ 40028c2ecf20Sopenharmony_ci if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES || 40038c2ecf20Sopenharmony_ci (pgdat_balanced(pgdat, order, highest_zoneidx) && 40048c2ecf20Sopenharmony_ci !pgdat_watermark_boosted(pgdat, highest_zoneidx))) { 40058c2ecf20Sopenharmony_ci /* 40068c2ecf20Sopenharmony_ci * There may be plenty of free memory available, but it's too 40078c2ecf20Sopenharmony_ci * fragmented for high-order allocations. Wake up kcompactd 40088c2ecf20Sopenharmony_ci * and rely on compaction_suitable() to determine if it's 40098c2ecf20Sopenharmony_ci * needed. If it fails, it will defer subsequent attempts to 40108c2ecf20Sopenharmony_ci * ratelimit its work. 40118c2ecf20Sopenharmony_ci */ 40128c2ecf20Sopenharmony_ci if (!(gfp_flags & __GFP_DIRECT_RECLAIM)) 40138c2ecf20Sopenharmony_ci wakeup_kcompactd(pgdat, order, highest_zoneidx); 40148c2ecf20Sopenharmony_ci return; 40158c2ecf20Sopenharmony_ci } 40168c2ecf20Sopenharmony_ci 40178c2ecf20Sopenharmony_ci trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, highest_zoneidx, order, 40188c2ecf20Sopenharmony_ci gfp_flags); 40198c2ecf20Sopenharmony_ci wake_up_interruptible(&pgdat->kswapd_wait); 40208c2ecf20Sopenharmony_ci} 40218c2ecf20Sopenharmony_ci 40228c2ecf20Sopenharmony_ci#ifdef CONFIG_HIBERNATION 40238c2ecf20Sopenharmony_ci/* 40248c2ecf20Sopenharmony_ci * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 40258c2ecf20Sopenharmony_ci * freed pages. 40268c2ecf20Sopenharmony_ci * 40278c2ecf20Sopenharmony_ci * Rather than trying to age LRUs the aim is to preserve the overall 40288c2ecf20Sopenharmony_ci * LRU order by reclaiming preferentially 40298c2ecf20Sopenharmony_ci * inactive > active > active referenced > active mapped 40308c2ecf20Sopenharmony_ci */ 40318c2ecf20Sopenharmony_ciunsigned long shrink_all_memory(unsigned long nr_to_reclaim) 40328c2ecf20Sopenharmony_ci{ 40338c2ecf20Sopenharmony_ci struct scan_control sc = { 40348c2ecf20Sopenharmony_ci .nr_to_reclaim = nr_to_reclaim, 40358c2ecf20Sopenharmony_ci .gfp_mask = GFP_HIGHUSER_MOVABLE, 40368c2ecf20Sopenharmony_ci .reclaim_idx = MAX_NR_ZONES - 1, 40378c2ecf20Sopenharmony_ci .priority = DEF_PRIORITY, 40388c2ecf20Sopenharmony_ci .may_writepage = 1, 40398c2ecf20Sopenharmony_ci .may_unmap = 1, 40408c2ecf20Sopenharmony_ci .may_swap = 1, 40418c2ecf20Sopenharmony_ci .hibernation_mode = 1, 40428c2ecf20Sopenharmony_ci }; 40438c2ecf20Sopenharmony_ci struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 40448c2ecf20Sopenharmony_ci unsigned long nr_reclaimed; 40458c2ecf20Sopenharmony_ci unsigned int noreclaim_flag; 40468c2ecf20Sopenharmony_ci 40478c2ecf20Sopenharmony_ci fs_reclaim_acquire(sc.gfp_mask); 40488c2ecf20Sopenharmony_ci noreclaim_flag = memalloc_noreclaim_save(); 40498c2ecf20Sopenharmony_ci set_task_reclaim_state(current, &sc.reclaim_state); 40508c2ecf20Sopenharmony_ci 40518c2ecf20Sopenharmony_ci nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 40528c2ecf20Sopenharmony_ci 40538c2ecf20Sopenharmony_ci set_task_reclaim_state(current, NULL); 40548c2ecf20Sopenharmony_ci memalloc_noreclaim_restore(noreclaim_flag); 40558c2ecf20Sopenharmony_ci fs_reclaim_release(sc.gfp_mask); 40568c2ecf20Sopenharmony_ci 40578c2ecf20Sopenharmony_ci return nr_reclaimed; 40588c2ecf20Sopenharmony_ci} 40598c2ecf20Sopenharmony_ci#endif /* CONFIG_HIBERNATION */ 40608c2ecf20Sopenharmony_ci 40618c2ecf20Sopenharmony_ci/* 40628c2ecf20Sopenharmony_ci * This kswapd start function will be called by init and node-hot-add. 40638c2ecf20Sopenharmony_ci * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 40648c2ecf20Sopenharmony_ci */ 40658c2ecf20Sopenharmony_ciint kswapd_run(int nid) 40668c2ecf20Sopenharmony_ci{ 40678c2ecf20Sopenharmony_ci pg_data_t *pgdat = NODE_DATA(nid); 40688c2ecf20Sopenharmony_ci int ret = 0; 40698c2ecf20Sopenharmony_ci 40708c2ecf20Sopenharmony_ci if (pgdat->kswapd) 40718c2ecf20Sopenharmony_ci return 0; 40728c2ecf20Sopenharmony_ci 40738c2ecf20Sopenharmony_ci pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 40748c2ecf20Sopenharmony_ci if (IS_ERR(pgdat->kswapd)) { 40758c2ecf20Sopenharmony_ci /* failure at boot is fatal */ 40768c2ecf20Sopenharmony_ci BUG_ON(system_state < SYSTEM_RUNNING); 40778c2ecf20Sopenharmony_ci pr_err("Failed to start kswapd on node %d\n", nid); 40788c2ecf20Sopenharmony_ci ret = PTR_ERR(pgdat->kswapd); 40798c2ecf20Sopenharmony_ci pgdat->kswapd = NULL; 40808c2ecf20Sopenharmony_ci } 40818c2ecf20Sopenharmony_ci return ret; 40828c2ecf20Sopenharmony_ci} 40838c2ecf20Sopenharmony_ci 40848c2ecf20Sopenharmony_ci/* 40858c2ecf20Sopenharmony_ci * Called by memory hotplug when all memory in a node is offlined. Caller must 40868c2ecf20Sopenharmony_ci * hold mem_hotplug_begin/end(). 40878c2ecf20Sopenharmony_ci */ 40888c2ecf20Sopenharmony_civoid kswapd_stop(int nid) 40898c2ecf20Sopenharmony_ci{ 40908c2ecf20Sopenharmony_ci struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 40918c2ecf20Sopenharmony_ci 40928c2ecf20Sopenharmony_ci if (kswapd) { 40938c2ecf20Sopenharmony_ci kthread_stop(kswapd); 40948c2ecf20Sopenharmony_ci NODE_DATA(nid)->kswapd = NULL; 40958c2ecf20Sopenharmony_ci } 40968c2ecf20Sopenharmony_ci} 40978c2ecf20Sopenharmony_ci 40988c2ecf20Sopenharmony_ci#ifdef CONFIG_MEM_PURGEABLE_DEBUG 40998c2ecf20Sopenharmony_cistatic void __init purgeable_debugfs_init(void); 41008c2ecf20Sopenharmony_ci#endif 41018c2ecf20Sopenharmony_ci 41028c2ecf20Sopenharmony_cistatic int __init kswapd_init(void) 41038c2ecf20Sopenharmony_ci{ 41048c2ecf20Sopenharmony_ci int nid; 41058c2ecf20Sopenharmony_ci 41068c2ecf20Sopenharmony_ci swap_setup(); 41078c2ecf20Sopenharmony_ci for_each_node_state(nid, N_MEMORY) 41088c2ecf20Sopenharmony_ci kswapd_run(nid); 41098c2ecf20Sopenharmony_ci#ifdef CONFIG_MEM_PURGEABLE_DEBUG 41108c2ecf20Sopenharmony_ci purgeable_debugfs_init(); 41118c2ecf20Sopenharmony_ci#endif 41128c2ecf20Sopenharmony_ci return 0; 41138c2ecf20Sopenharmony_ci} 41148c2ecf20Sopenharmony_ci 41158c2ecf20Sopenharmony_cimodule_init(kswapd_init) 41168c2ecf20Sopenharmony_ci 41178c2ecf20Sopenharmony_ci#ifdef CONFIG_NUMA 41188c2ecf20Sopenharmony_ci/* 41198c2ecf20Sopenharmony_ci * Node reclaim mode 41208c2ecf20Sopenharmony_ci * 41218c2ecf20Sopenharmony_ci * If non-zero call node_reclaim when the number of free pages falls below 41228c2ecf20Sopenharmony_ci * the watermarks. 41238c2ecf20Sopenharmony_ci */ 41248c2ecf20Sopenharmony_ciint node_reclaim_mode __read_mostly; 41258c2ecf20Sopenharmony_ci 41268c2ecf20Sopenharmony_ci/* 41278c2ecf20Sopenharmony_ci * These bit locations are exposed in the vm.zone_reclaim_mode sysctl 41288c2ecf20Sopenharmony_ci * ABI. New bits are OK, but existing bits can never change. 41298c2ecf20Sopenharmony_ci */ 41308c2ecf20Sopenharmony_ci#define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ 41318c2ecf20Sopenharmony_ci#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 41328c2ecf20Sopenharmony_ci#define RECLAIM_UNMAP (1<<2) /* Unmap pages during reclaim */ 41338c2ecf20Sopenharmony_ci 41348c2ecf20Sopenharmony_ci/* 41358c2ecf20Sopenharmony_ci * Priority for NODE_RECLAIM. This determines the fraction of pages 41368c2ecf20Sopenharmony_ci * of a node considered for each zone_reclaim. 4 scans 1/16th of 41378c2ecf20Sopenharmony_ci * a zone. 41388c2ecf20Sopenharmony_ci */ 41398c2ecf20Sopenharmony_ci#define NODE_RECLAIM_PRIORITY 4 41408c2ecf20Sopenharmony_ci 41418c2ecf20Sopenharmony_ci/* 41428c2ecf20Sopenharmony_ci * Percentage of pages in a zone that must be unmapped for node_reclaim to 41438c2ecf20Sopenharmony_ci * occur. 41448c2ecf20Sopenharmony_ci */ 41458c2ecf20Sopenharmony_ciint sysctl_min_unmapped_ratio = 1; 41468c2ecf20Sopenharmony_ci 41478c2ecf20Sopenharmony_ci/* 41488c2ecf20Sopenharmony_ci * If the number of slab pages in a zone grows beyond this percentage then 41498c2ecf20Sopenharmony_ci * slab reclaim needs to occur. 41508c2ecf20Sopenharmony_ci */ 41518c2ecf20Sopenharmony_ciint sysctl_min_slab_ratio = 5; 41528c2ecf20Sopenharmony_ci 41538c2ecf20Sopenharmony_cistatic inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) 41548c2ecf20Sopenharmony_ci{ 41558c2ecf20Sopenharmony_ci unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); 41568c2ecf20Sopenharmony_ci unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + 41578c2ecf20Sopenharmony_ci node_page_state(pgdat, NR_ACTIVE_FILE); 41588c2ecf20Sopenharmony_ci 41598c2ecf20Sopenharmony_ci /* 41608c2ecf20Sopenharmony_ci * It's possible for there to be more file mapped pages than 41618c2ecf20Sopenharmony_ci * accounted for by the pages on the file LRU lists because 41628c2ecf20Sopenharmony_ci * tmpfs pages accounted for as ANON can also be FILE_MAPPED 41638c2ecf20Sopenharmony_ci */ 41648c2ecf20Sopenharmony_ci return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 41658c2ecf20Sopenharmony_ci} 41668c2ecf20Sopenharmony_ci 41678c2ecf20Sopenharmony_ci/* Work out how many page cache pages we can reclaim in this reclaim_mode */ 41688c2ecf20Sopenharmony_cistatic unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) 41698c2ecf20Sopenharmony_ci{ 41708c2ecf20Sopenharmony_ci unsigned long nr_pagecache_reclaimable; 41718c2ecf20Sopenharmony_ci unsigned long delta = 0; 41728c2ecf20Sopenharmony_ci 41738c2ecf20Sopenharmony_ci /* 41748c2ecf20Sopenharmony_ci * If RECLAIM_UNMAP is set, then all file pages are considered 41758c2ecf20Sopenharmony_ci * potentially reclaimable. Otherwise, we have to worry about 41768c2ecf20Sopenharmony_ci * pages like swapcache and node_unmapped_file_pages() provides 41778c2ecf20Sopenharmony_ci * a better estimate 41788c2ecf20Sopenharmony_ci */ 41798c2ecf20Sopenharmony_ci if (node_reclaim_mode & RECLAIM_UNMAP) 41808c2ecf20Sopenharmony_ci nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); 41818c2ecf20Sopenharmony_ci else 41828c2ecf20Sopenharmony_ci nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); 41838c2ecf20Sopenharmony_ci 41848c2ecf20Sopenharmony_ci /* If we can't clean pages, remove dirty pages from consideration */ 41858c2ecf20Sopenharmony_ci if (!(node_reclaim_mode & RECLAIM_WRITE)) 41868c2ecf20Sopenharmony_ci delta += node_page_state(pgdat, NR_FILE_DIRTY); 41878c2ecf20Sopenharmony_ci 41888c2ecf20Sopenharmony_ci /* Watch for any possible underflows due to delta */ 41898c2ecf20Sopenharmony_ci if (unlikely(delta > nr_pagecache_reclaimable)) 41908c2ecf20Sopenharmony_ci delta = nr_pagecache_reclaimable; 41918c2ecf20Sopenharmony_ci 41928c2ecf20Sopenharmony_ci return nr_pagecache_reclaimable - delta; 41938c2ecf20Sopenharmony_ci} 41948c2ecf20Sopenharmony_ci 41958c2ecf20Sopenharmony_ci/* 41968c2ecf20Sopenharmony_ci * Try to free up some pages from this node through reclaim. 41978c2ecf20Sopenharmony_ci */ 41988c2ecf20Sopenharmony_cistatic int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 41998c2ecf20Sopenharmony_ci{ 42008c2ecf20Sopenharmony_ci /* Minimum pages needed in order to stay on node */ 42018c2ecf20Sopenharmony_ci const unsigned long nr_pages = 1 << order; 42028c2ecf20Sopenharmony_ci struct task_struct *p = current; 42038c2ecf20Sopenharmony_ci unsigned int noreclaim_flag; 42048c2ecf20Sopenharmony_ci struct scan_control sc = { 42058c2ecf20Sopenharmony_ci .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 42068c2ecf20Sopenharmony_ci .gfp_mask = current_gfp_context(gfp_mask), 42078c2ecf20Sopenharmony_ci .order = order, 42088c2ecf20Sopenharmony_ci .priority = NODE_RECLAIM_PRIORITY, 42098c2ecf20Sopenharmony_ci .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), 42108c2ecf20Sopenharmony_ci .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), 42118c2ecf20Sopenharmony_ci .may_swap = 1, 42128c2ecf20Sopenharmony_ci .reclaim_idx = gfp_zone(gfp_mask), 42138c2ecf20Sopenharmony_ci }; 42148c2ecf20Sopenharmony_ci unsigned long pflags; 42158c2ecf20Sopenharmony_ci 42168c2ecf20Sopenharmony_ci trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order, 42178c2ecf20Sopenharmony_ci sc.gfp_mask); 42188c2ecf20Sopenharmony_ci 42198c2ecf20Sopenharmony_ci cond_resched(); 42208c2ecf20Sopenharmony_ci psi_memstall_enter(&pflags); 42218c2ecf20Sopenharmony_ci fs_reclaim_acquire(sc.gfp_mask); 42228c2ecf20Sopenharmony_ci /* 42238c2ecf20Sopenharmony_ci * We need to be able to allocate from the reserves for RECLAIM_UNMAP 42248c2ecf20Sopenharmony_ci * and we also need to be able to write out pages for RECLAIM_WRITE 42258c2ecf20Sopenharmony_ci * and RECLAIM_UNMAP. 42268c2ecf20Sopenharmony_ci */ 42278c2ecf20Sopenharmony_ci noreclaim_flag = memalloc_noreclaim_save(); 42288c2ecf20Sopenharmony_ci p->flags |= PF_SWAPWRITE; 42298c2ecf20Sopenharmony_ci set_task_reclaim_state(p, &sc.reclaim_state); 42308c2ecf20Sopenharmony_ci 42318c2ecf20Sopenharmony_ci if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) { 42328c2ecf20Sopenharmony_ci /* 42338c2ecf20Sopenharmony_ci * Free memory by calling shrink node with increasing 42348c2ecf20Sopenharmony_ci * priorities until we have enough memory freed. 42358c2ecf20Sopenharmony_ci */ 42368c2ecf20Sopenharmony_ci do { 42378c2ecf20Sopenharmony_ci#ifdef CONFIG_HYPERHOLD_FILE_LRU 42388c2ecf20Sopenharmony_ci shrink_node_hyperhold(pgdat, &sc); 42398c2ecf20Sopenharmony_ci#else 42408c2ecf20Sopenharmony_ci shrink_node(pgdat, &sc); 42418c2ecf20Sopenharmony_ci#endif 42428c2ecf20Sopenharmony_ci } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 42438c2ecf20Sopenharmony_ci } 42448c2ecf20Sopenharmony_ci 42458c2ecf20Sopenharmony_ci set_task_reclaim_state(p, NULL); 42468c2ecf20Sopenharmony_ci current->flags &= ~PF_SWAPWRITE; 42478c2ecf20Sopenharmony_ci memalloc_noreclaim_restore(noreclaim_flag); 42488c2ecf20Sopenharmony_ci fs_reclaim_release(sc.gfp_mask); 42498c2ecf20Sopenharmony_ci psi_memstall_leave(&pflags); 42508c2ecf20Sopenharmony_ci 42518c2ecf20Sopenharmony_ci trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed); 42528c2ecf20Sopenharmony_ci 42538c2ecf20Sopenharmony_ci return sc.nr_reclaimed >= nr_pages; 42548c2ecf20Sopenharmony_ci} 42558c2ecf20Sopenharmony_ci 42568c2ecf20Sopenharmony_ciint node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 42578c2ecf20Sopenharmony_ci{ 42588c2ecf20Sopenharmony_ci int ret; 42598c2ecf20Sopenharmony_ci 42608c2ecf20Sopenharmony_ci /* 42618c2ecf20Sopenharmony_ci * Node reclaim reclaims unmapped file backed pages and 42628c2ecf20Sopenharmony_ci * slab pages if we are over the defined limits. 42638c2ecf20Sopenharmony_ci * 42648c2ecf20Sopenharmony_ci * A small portion of unmapped file backed pages is needed for 42658c2ecf20Sopenharmony_ci * file I/O otherwise pages read by file I/O will be immediately 42668c2ecf20Sopenharmony_ci * thrown out if the node is overallocated. So we do not reclaim 42678c2ecf20Sopenharmony_ci * if less than a specified percentage of the node is used by 42688c2ecf20Sopenharmony_ci * unmapped file backed pages. 42698c2ecf20Sopenharmony_ci */ 42708c2ecf20Sopenharmony_ci if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && 42718c2ecf20Sopenharmony_ci node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) <= 42728c2ecf20Sopenharmony_ci pgdat->min_slab_pages) 42738c2ecf20Sopenharmony_ci return NODE_RECLAIM_FULL; 42748c2ecf20Sopenharmony_ci 42758c2ecf20Sopenharmony_ci /* 42768c2ecf20Sopenharmony_ci * Do not scan if the allocation should not be delayed. 42778c2ecf20Sopenharmony_ci */ 42788c2ecf20Sopenharmony_ci if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) 42798c2ecf20Sopenharmony_ci return NODE_RECLAIM_NOSCAN; 42808c2ecf20Sopenharmony_ci 42818c2ecf20Sopenharmony_ci /* 42828c2ecf20Sopenharmony_ci * Only run node reclaim on the local node or on nodes that do not 42838c2ecf20Sopenharmony_ci * have associated processors. This will favor the local processor 42848c2ecf20Sopenharmony_ci * over remote processors and spread off node memory allocations 42858c2ecf20Sopenharmony_ci * as wide as possible. 42868c2ecf20Sopenharmony_ci */ 42878c2ecf20Sopenharmony_ci if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) 42888c2ecf20Sopenharmony_ci return NODE_RECLAIM_NOSCAN; 42898c2ecf20Sopenharmony_ci 42908c2ecf20Sopenharmony_ci if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) 42918c2ecf20Sopenharmony_ci return NODE_RECLAIM_NOSCAN; 42928c2ecf20Sopenharmony_ci 42938c2ecf20Sopenharmony_ci ret = __node_reclaim(pgdat, gfp_mask, order); 42948c2ecf20Sopenharmony_ci clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); 42958c2ecf20Sopenharmony_ci 42968c2ecf20Sopenharmony_ci if (!ret) 42978c2ecf20Sopenharmony_ci count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 42988c2ecf20Sopenharmony_ci 42998c2ecf20Sopenharmony_ci return ret; 43008c2ecf20Sopenharmony_ci} 43018c2ecf20Sopenharmony_ci#endif 43028c2ecf20Sopenharmony_ci 43038c2ecf20Sopenharmony_ci/** 43048c2ecf20Sopenharmony_ci * check_move_unevictable_pages - check pages for evictability and move to 43058c2ecf20Sopenharmony_ci * appropriate zone lru list 43068c2ecf20Sopenharmony_ci * @pvec: pagevec with lru pages to check 43078c2ecf20Sopenharmony_ci * 43088c2ecf20Sopenharmony_ci * Checks pages for evictability, if an evictable page is in the unevictable 43098c2ecf20Sopenharmony_ci * lru list, moves it to the appropriate evictable lru list. This function 43108c2ecf20Sopenharmony_ci * should be only used for lru pages. 43118c2ecf20Sopenharmony_ci */ 43128c2ecf20Sopenharmony_civoid check_move_unevictable_pages(struct pagevec *pvec) 43138c2ecf20Sopenharmony_ci{ 43148c2ecf20Sopenharmony_ci struct lruvec *lruvec; 43158c2ecf20Sopenharmony_ci struct pglist_data *pgdat = NULL; 43168c2ecf20Sopenharmony_ci int pgscanned = 0; 43178c2ecf20Sopenharmony_ci int pgrescued = 0; 43188c2ecf20Sopenharmony_ci int i; 43198c2ecf20Sopenharmony_ci 43208c2ecf20Sopenharmony_ci for (i = 0; i < pvec->nr; i++) { 43218c2ecf20Sopenharmony_ci struct page *page = pvec->pages[i]; 43228c2ecf20Sopenharmony_ci struct pglist_data *pagepgdat = page_pgdat(page); 43238c2ecf20Sopenharmony_ci int nr_pages; 43248c2ecf20Sopenharmony_ci 43258c2ecf20Sopenharmony_ci if (PageTransTail(page)) 43268c2ecf20Sopenharmony_ci continue; 43278c2ecf20Sopenharmony_ci 43288c2ecf20Sopenharmony_ci nr_pages = thp_nr_pages(page); 43298c2ecf20Sopenharmony_ci pgscanned += nr_pages; 43308c2ecf20Sopenharmony_ci 43318c2ecf20Sopenharmony_ci if (pagepgdat != pgdat) { 43328c2ecf20Sopenharmony_ci if (pgdat) 43338c2ecf20Sopenharmony_ci spin_unlock_irq(&pgdat->lru_lock); 43348c2ecf20Sopenharmony_ci pgdat = pagepgdat; 43358c2ecf20Sopenharmony_ci spin_lock_irq(&pgdat->lru_lock); 43368c2ecf20Sopenharmony_ci } 43378c2ecf20Sopenharmony_ci lruvec = mem_cgroup_page_lruvec(page, pgdat); 43388c2ecf20Sopenharmony_ci 43398c2ecf20Sopenharmony_ci if (!PageLRU(page) || !PageUnevictable(page)) 43408c2ecf20Sopenharmony_ci continue; 43418c2ecf20Sopenharmony_ci 43428c2ecf20Sopenharmony_ci if (page_evictable(page)) { 43438c2ecf20Sopenharmony_ci enum lru_list lru = page_lru_base_type(page); 43448c2ecf20Sopenharmony_ci 43458c2ecf20Sopenharmony_ci VM_BUG_ON_PAGE(PageActive(page), page); 43468c2ecf20Sopenharmony_ci ClearPageUnevictable(page); 43478c2ecf20Sopenharmony_ci del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE); 43488c2ecf20Sopenharmony_ci add_page_to_lru_list(page, lruvec, lru); 43498c2ecf20Sopenharmony_ci pgrescued += nr_pages; 43508c2ecf20Sopenharmony_ci } 43518c2ecf20Sopenharmony_ci } 43528c2ecf20Sopenharmony_ci 43538c2ecf20Sopenharmony_ci if (pgdat) { 43548c2ecf20Sopenharmony_ci __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 43558c2ecf20Sopenharmony_ci __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 43568c2ecf20Sopenharmony_ci spin_unlock_irq(&pgdat->lru_lock); 43578c2ecf20Sopenharmony_ci } 43588c2ecf20Sopenharmony_ci} 43598c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(check_move_unevictable_pages); 43608c2ecf20Sopenharmony_ci 43618c2ecf20Sopenharmony_ci#ifdef CONFIG_MEM_PURGEABLE_DEBUG 43628c2ecf20Sopenharmony_cistatic unsigned long purgeable_node(pg_data_t *pgdata, struct scan_control *sc) 43638c2ecf20Sopenharmony_ci{ 43648c2ecf20Sopenharmony_ci struct mem_cgroup *memcg = NULL; 43658c2ecf20Sopenharmony_ci unsigned long nr = 0; 43668c2ecf20Sopenharmony_ci#ifdef CONFIG_MEMCG 43678c2ecf20Sopenharmony_ci while ((memcg = mem_cgroup_iter(NULL, memcg, NULL))) 43688c2ecf20Sopenharmony_ci#endif 43698c2ecf20Sopenharmony_ci { 43708c2ecf20Sopenharmony_ci struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdata); 43718c2ecf20Sopenharmony_ci 43728c2ecf20Sopenharmony_ci shrink_list(LRU_ACTIVE_PURGEABLE, -1, lruvec, sc); 43738c2ecf20Sopenharmony_ci nr += shrink_list(LRU_INACTIVE_PURGEABLE, -1, lruvec, sc); 43748c2ecf20Sopenharmony_ci } 43758c2ecf20Sopenharmony_ci 43768c2ecf20Sopenharmony_ci pr_info("reclaim %lu purgeable pages.\n", nr); 43778c2ecf20Sopenharmony_ci 43788c2ecf20Sopenharmony_ci return nr; 43798c2ecf20Sopenharmony_ci} 43808c2ecf20Sopenharmony_ci 43818c2ecf20Sopenharmony_cistatic int purgeable(struct ctl_table *table, int write, void *buffer, 43828c2ecf20Sopenharmony_ci size_t *lenp, loff_t *ppos) 43838c2ecf20Sopenharmony_ci{ 43848c2ecf20Sopenharmony_ci struct scan_control sc = { 43858c2ecf20Sopenharmony_ci .gfp_mask = GFP_KERNEL, 43868c2ecf20Sopenharmony_ci .order = 0, 43878c2ecf20Sopenharmony_ci .priority = DEF_PRIORITY, 43888c2ecf20Sopenharmony_ci .may_deactivate = DEACTIVATE_ANON, 43898c2ecf20Sopenharmony_ci .may_writepage = 1, 43908c2ecf20Sopenharmony_ci .may_unmap = 1, 43918c2ecf20Sopenharmony_ci .may_swap = 1, 43928c2ecf20Sopenharmony_ci .reclaim_idx = MAX_NR_ZONES - 1, 43938c2ecf20Sopenharmony_ci }; 43948c2ecf20Sopenharmony_ci int nid = 0; 43958c2ecf20Sopenharmony_ci const struct cred *cred = current_cred(); 43968c2ecf20Sopenharmony_ci if (!cred) 43978c2ecf20Sopenharmony_ci return 0; 43988c2ecf20Sopenharmony_ci 43998c2ecf20Sopenharmony_ci if (!uid_eq(cred->euid, GLOBAL_MEMMGR_UID) && 44008c2ecf20Sopenharmony_ci !uid_eq(cred->euid, GLOBAL_ROOT_UID)) { 44018c2ecf20Sopenharmony_ci pr_err("no permission to shrink purgeable heap!\n"); 44028c2ecf20Sopenharmony_ci return -EINVAL; 44038c2ecf20Sopenharmony_ci } 44048c2ecf20Sopenharmony_ci for_each_node_state(nid, N_MEMORY) 44058c2ecf20Sopenharmony_ci purgeable_node(NODE_DATA(nid), &sc); 44068c2ecf20Sopenharmony_ci return 0; 44078c2ecf20Sopenharmony_ci} 44088c2ecf20Sopenharmony_ci 44098c2ecf20Sopenharmony_cistatic struct ctl_table ker_tab[] = { 44108c2ecf20Sopenharmony_ci { 44118c2ecf20Sopenharmony_ci .procname = "purgeable", 44128c2ecf20Sopenharmony_ci .mode = 0666, 44138c2ecf20Sopenharmony_ci .proc_handler = purgeable, 44148c2ecf20Sopenharmony_ci }, 44158c2ecf20Sopenharmony_ci {}, 44168c2ecf20Sopenharmony_ci}; 44178c2ecf20Sopenharmony_ci 44188c2ecf20Sopenharmony_cistatic struct ctl_table sys_tab[] = { 44198c2ecf20Sopenharmony_ci { 44208c2ecf20Sopenharmony_ci .procname = "kernel", 44218c2ecf20Sopenharmony_ci .mode = 0555, 44228c2ecf20Sopenharmony_ci .child = ker_tab, 44238c2ecf20Sopenharmony_ci }, 44248c2ecf20Sopenharmony_ci {}, 44258c2ecf20Sopenharmony_ci}; 44268c2ecf20Sopenharmony_ci 44278c2ecf20Sopenharmony_cistatic struct ctl_table_header *purgeable_header; 44288c2ecf20Sopenharmony_ci 44298c2ecf20Sopenharmony_cistatic void __init purgeable_debugfs_init(void) 44308c2ecf20Sopenharmony_ci{ 44318c2ecf20Sopenharmony_ci purgeable_header = register_sysctl_table(sys_tab); 44328c2ecf20Sopenharmony_ci if (!purgeable_header) 44338c2ecf20Sopenharmony_ci pr_err("register purgeable sysctl table failed.\n"); 44348c2ecf20Sopenharmony_ci} 44358c2ecf20Sopenharmony_ci 44368c2ecf20Sopenharmony_cistatic void __exit purgeable_debugfs_exit(void) 44378c2ecf20Sopenharmony_ci{ 44388c2ecf20Sopenharmony_ci unregister_sysctl_table(purgeable_header); 44398c2ecf20Sopenharmony_ci} 44408c2ecf20Sopenharmony_ci#endif /* CONFIG_MEM_PURGEABLE_DEBUG */ 4441