11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/mm/vmscan.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Swap reorganised 29.12.95, Stephen Tweedie. 71da177e4SLinus Torvalds * kswapd added: 7.1.96 sct 81da177e4SLinus Torvalds * Removed kswapd_ctl limits, and swap out as many pages as needed 91da177e4SLinus Torvalds * to bring the system back to freepages.high: 2.4.97, Rik van Riel. 101da177e4SLinus Torvalds * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com). 111da177e4SLinus Torvalds * Multiqueue VM started 5.8.00, Rik van Riel. 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 14b1de0d13SMitchel Humpherys #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 15b1de0d13SMitchel Humpherys 161da177e4SLinus Torvalds #include <linux/mm.h> 171da177e4SLinus Torvalds #include <linux/module.h> 185a0e3ad6STejun Heo #include <linux/gfp.h> 191da177e4SLinus Torvalds #include <linux/kernel_stat.h> 201da177e4SLinus Torvalds #include <linux/swap.h> 211da177e4SLinus Torvalds #include <linux/pagemap.h> 221da177e4SLinus Torvalds #include <linux/init.h> 231da177e4SLinus Torvalds #include <linux/highmem.h> 2470ddf637SAnton Vorontsov #include <linux/vmpressure.h> 25e129b5c2SAndrew Morton #include <linux/vmstat.h> 261da177e4SLinus Torvalds #include <linux/file.h> 271da177e4SLinus Torvalds #include <linux/writeback.h> 281da177e4SLinus Torvalds #include <linux/blkdev.h> 291da177e4SLinus Torvalds #include <linux/buffer_head.h> /* for try_to_release_page(), 301da177e4SLinus Torvalds buffer_heads_over_limit */ 311da177e4SLinus Torvalds #include <linux/mm_inline.h> 321da177e4SLinus Torvalds #include <linux/backing-dev.h> 331da177e4SLinus Torvalds #include <linux/rmap.h> 341da177e4SLinus Torvalds #include <linux/topology.h> 351da177e4SLinus Torvalds #include <linux/cpu.h> 361da177e4SLinus Torvalds #include <linux/cpuset.h> 373e7d3449SMel Gorman #include <linux/compaction.h> 381da177e4SLinus Torvalds #include <linux/notifier.h> 391da177e4SLinus Torvalds #include <linux/rwsem.h> 40248a0301SRafael J. Wysocki #include <linux/delay.h> 413218ae14SYasunori Goto #include <linux/kthread.h> 427dfb7103SNigel Cunningham #include <linux/freezer.h> 4366e1707bSBalbir Singh #include <linux/memcontrol.h> 44873b4771SKeika Kobayashi #include <linux/delayacct.h> 45af936a16SLee Schermerhorn #include <linux/sysctl.h> 46929bea7cSKOSAKI Motohiro #include <linux/oom.h> 47268bb0ceSLinus Torvalds #include <linux/prefetch.h> 48b1de0d13SMitchel Humpherys #include <linux/printk.h> 49f9fe48beSRoss Zwisler #include <linux/dax.h> 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds #include <asm/tlbflush.h> 521da177e4SLinus Torvalds #include <asm/div64.h> 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds #include <linux/swapops.h> 55117aad1eSRafael Aquini #include <linux/balloon_compaction.h> 561da177e4SLinus Torvalds 570f8053a5SNick Piggin #include "internal.h" 580f8053a5SNick Piggin 5933906bc5SMel Gorman #define CREATE_TRACE_POINTS 6033906bc5SMel Gorman #include <trace/events/vmscan.h> 6133906bc5SMel Gorman 621da177e4SLinus Torvalds struct scan_control { 6322fba335SKOSAKI Motohiro /* How many pages shrink_list() should reclaim */ 6422fba335SKOSAKI Motohiro unsigned long nr_to_reclaim; 6522fba335SKOSAKI Motohiro 661da177e4SLinus Torvalds /* This context's GFP mask */ 676daa0e28SAl Viro gfp_t gfp_mask; 681da177e4SLinus Torvalds 69ee814fe2SJohannes Weiner /* Allocation order */ 705ad333ebSAndy Whitcroft int order; 7166e1707bSBalbir Singh 72ee814fe2SJohannes Weiner /* 73ee814fe2SJohannes Weiner * Nodemask of nodes allowed by the caller. If NULL, all nodes 74ee814fe2SJohannes Weiner * are scanned. 75ee814fe2SJohannes Weiner */ 76ee814fe2SJohannes Weiner nodemask_t *nodemask; 779e3b2f8cSKonstantin Khlebnikov 785f53e762SKOSAKI Motohiro /* 79f16015fbSJohannes Weiner * The memory cgroup that hit its limit and as a result is the 80f16015fbSJohannes Weiner * primary target of this reclaim invocation. 81f16015fbSJohannes Weiner */ 82f16015fbSJohannes Weiner struct mem_cgroup *target_mem_cgroup; 8366e1707bSBalbir Singh 84ee814fe2SJohannes Weiner /* Scan (total_size >> priority) pages at once */ 85ee814fe2SJohannes Weiner int priority; 86ee814fe2SJohannes Weiner 87b2e18757SMel Gorman /* The highest zone to isolate pages for reclaim from */ 88b2e18757SMel Gorman enum zone_type reclaim_idx; 89b2e18757SMel Gorman 90ee814fe2SJohannes Weiner unsigned int may_writepage:1; 91ee814fe2SJohannes Weiner 92ee814fe2SJohannes Weiner /* Can mapped pages be reclaimed? */ 93ee814fe2SJohannes Weiner unsigned int may_unmap:1; 94ee814fe2SJohannes Weiner 95ee814fe2SJohannes Weiner /* Can pages be swapped as part of reclaim? */ 96ee814fe2SJohannes Weiner unsigned int may_swap:1; 97ee814fe2SJohannes Weiner 98241994edSJohannes Weiner /* Can cgroups be reclaimed below their normal consumption range? */ 99241994edSJohannes Weiner unsigned int may_thrash:1; 100241994edSJohannes Weiner 101ee814fe2SJohannes Weiner unsigned int hibernation_mode:1; 102ee814fe2SJohannes Weiner 103ee814fe2SJohannes Weiner /* One of the zones is ready for compaction */ 104ee814fe2SJohannes Weiner unsigned int compaction_ready:1; 105ee814fe2SJohannes Weiner 106ee814fe2SJohannes Weiner /* Incremented by the number of inactive pages that were scanned */ 107ee814fe2SJohannes Weiner unsigned long nr_scanned; 108ee814fe2SJohannes Weiner 109ee814fe2SJohannes Weiner /* Number of pages freed so far during a call to shrink_zones() */ 110ee814fe2SJohannes Weiner unsigned long nr_reclaimed; 1111da177e4SLinus Torvalds }; 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH 1141da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) \ 1151da177e4SLinus Torvalds do { \ 1161da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1171da177e4SLinus Torvalds struct page *prev; \ 1181da177e4SLinus Torvalds \ 1191da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1201da177e4SLinus Torvalds prefetch(&prev->_field); \ 1211da177e4SLinus Torvalds } \ 1221da177e4SLinus Torvalds } while (0) 1231da177e4SLinus Torvalds #else 1241da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) 1251da177e4SLinus Torvalds #endif 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1281da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1291da177e4SLinus Torvalds do { \ 1301da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1311da177e4SLinus Torvalds struct page *prev; \ 1321da177e4SLinus Torvalds \ 1331da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1341da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1351da177e4SLinus Torvalds } \ 1361da177e4SLinus Torvalds } while (0) 1371da177e4SLinus Torvalds #else 1381da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1391da177e4SLinus Torvalds #endif 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* 1421da177e4SLinus Torvalds * From 0 .. 100. Higher means more swappy. 1431da177e4SLinus Torvalds */ 1441da177e4SLinus Torvalds int vm_swappiness = 60; 145d0480be4SWang Sheng-Hui /* 146d0480be4SWang Sheng-Hui * The total number of pages which are beyond the high watermark within all 147d0480be4SWang Sheng-Hui * zones. 148d0480be4SWang Sheng-Hui */ 149d0480be4SWang Sheng-Hui unsigned long vm_total_pages; 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1521da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1531da177e4SLinus Torvalds 154c255a458SAndrew Morton #ifdef CONFIG_MEMCG 15589b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 15689b5fae5SJohannes Weiner { 157f16015fbSJohannes Weiner return !sc->target_mem_cgroup; 15889b5fae5SJohannes Weiner } 15997c9341fSTejun Heo 16097c9341fSTejun Heo /** 16197c9341fSTejun Heo * sane_reclaim - is the usual dirty throttling mechanism operational? 16297c9341fSTejun Heo * @sc: scan_control in question 16397c9341fSTejun Heo * 16497c9341fSTejun Heo * The normal page dirty throttling mechanism in balance_dirty_pages() is 16597c9341fSTejun Heo * completely broken with the legacy memcg and direct stalling in 16697c9341fSTejun Heo * shrink_page_list() is used for throttling instead, which lacks all the 16797c9341fSTejun Heo * niceties such as fairness, adaptive pausing, bandwidth proportional 16897c9341fSTejun Heo * allocation and configurability. 16997c9341fSTejun Heo * 17097c9341fSTejun Heo * This function tests whether the vmscan currently in progress can assume 17197c9341fSTejun Heo * that the normal dirty throttling mechanism is operational. 17297c9341fSTejun Heo */ 17397c9341fSTejun Heo static bool sane_reclaim(struct scan_control *sc) 17497c9341fSTejun Heo { 17597c9341fSTejun Heo struct mem_cgroup *memcg = sc->target_mem_cgroup; 17697c9341fSTejun Heo 17797c9341fSTejun Heo if (!memcg) 17897c9341fSTejun Heo return true; 17997c9341fSTejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 18069234aceSLinus Torvalds if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) 18197c9341fSTejun Heo return true; 18297c9341fSTejun Heo #endif 18397c9341fSTejun Heo return false; 18497c9341fSTejun Heo } 18591a45470SKAMEZAWA Hiroyuki #else 18689b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 18789b5fae5SJohannes Weiner { 18889b5fae5SJohannes Weiner return true; 18989b5fae5SJohannes Weiner } 19097c9341fSTejun Heo 19197c9341fSTejun Heo static bool sane_reclaim(struct scan_control *sc) 19297c9341fSTejun Heo { 19397c9341fSTejun Heo return true; 19497c9341fSTejun Heo } 19591a45470SKAMEZAWA Hiroyuki #endif 19691a45470SKAMEZAWA Hiroyuki 1975a1c84b4SMel Gorman /* 1985a1c84b4SMel Gorman * This misses isolated pages which are not accounted for to save counters. 1995a1c84b4SMel Gorman * As the data only determines if reclaim or compaction continues, it is 2005a1c84b4SMel Gorman * not expected that isolated pages will be a dominating factor. 2015a1c84b4SMel Gorman */ 2025a1c84b4SMel Gorman unsigned long zone_reclaimable_pages(struct zone *zone) 2035a1c84b4SMel Gorman { 2045a1c84b4SMel Gorman unsigned long nr; 2055a1c84b4SMel Gorman 2065a1c84b4SMel Gorman nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) + 2075a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE); 2085a1c84b4SMel Gorman if (get_nr_swap_pages() > 0) 2095a1c84b4SMel Gorman nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) + 2105a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON); 2115a1c84b4SMel Gorman 2125a1c84b4SMel Gorman return nr; 2135a1c84b4SMel Gorman } 2145a1c84b4SMel Gorman 215599d0c95SMel Gorman unsigned long pgdat_reclaimable_pages(struct pglist_data *pgdat) 2166e543d57SLisa Du { 217599d0c95SMel Gorman unsigned long nr; 218599d0c95SMel Gorman 219599d0c95SMel Gorman nr = node_page_state_snapshot(pgdat, NR_ACTIVE_FILE) + 220599d0c95SMel Gorman node_page_state_snapshot(pgdat, NR_INACTIVE_FILE) + 221599d0c95SMel Gorman node_page_state_snapshot(pgdat, NR_ISOLATED_FILE); 222599d0c95SMel Gorman 223599d0c95SMel Gorman if (get_nr_swap_pages() > 0) 224599d0c95SMel Gorman nr += node_page_state_snapshot(pgdat, NR_ACTIVE_ANON) + 225599d0c95SMel Gorman node_page_state_snapshot(pgdat, NR_INACTIVE_ANON) + 226599d0c95SMel Gorman node_page_state_snapshot(pgdat, NR_ISOLATED_ANON); 227599d0c95SMel Gorman 228599d0c95SMel Gorman return nr; 229599d0c95SMel Gorman } 230599d0c95SMel Gorman 231599d0c95SMel Gorman bool pgdat_reclaimable(struct pglist_data *pgdat) 232599d0c95SMel Gorman { 233599d0c95SMel Gorman return node_page_state_snapshot(pgdat, NR_PAGES_SCANNED) < 234599d0c95SMel Gorman pgdat_reclaimable_pages(pgdat) * 6; 2356e543d57SLisa Du } 2366e543d57SLisa Du 237fd538803SMichal Hocko /** 238fd538803SMichal Hocko * lruvec_lru_size - Returns the number of pages on the given LRU list. 239fd538803SMichal Hocko * @lruvec: lru vector 240fd538803SMichal Hocko * @lru: lru to use 241fd538803SMichal Hocko * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list) 242fd538803SMichal Hocko */ 243fd538803SMichal Hocko unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx) 244c9f299d9SKOSAKI Motohiro { 245fd538803SMichal Hocko unsigned long lru_size; 246fd538803SMichal Hocko int zid; 247a3d8e054SKOSAKI Motohiro 248fd538803SMichal Hocko if (!mem_cgroup_disabled()) 249fd538803SMichal Hocko lru_size = mem_cgroup_get_lru_size(lruvec, lru); 250fd538803SMichal Hocko else 251fd538803SMichal Hocko lru_size = node_page_state(lruvec_pgdat(lruvec), NR_LRU_BASE + lru); 252fd538803SMichal Hocko 253fd538803SMichal Hocko for (zid = zone_idx + 1; zid < MAX_NR_ZONES; zid++) { 254fd538803SMichal Hocko struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; 255fd538803SMichal Hocko unsigned long size; 256fd538803SMichal Hocko 257fd538803SMichal Hocko if (!managed_zone(zone)) 258fd538803SMichal Hocko continue; 259fd538803SMichal Hocko 260fd538803SMichal Hocko if (!mem_cgroup_disabled()) 261fd538803SMichal Hocko size = mem_cgroup_get_zone_lru_size(lruvec, lru, zid); 262fd538803SMichal Hocko else 263fd538803SMichal Hocko size = zone_page_state(&lruvec_pgdat(lruvec)->node_zones[zid], 264fd538803SMichal Hocko NR_ZONE_LRU_BASE + lru); 265fd538803SMichal Hocko lru_size -= min(size, lru_size); 266c9f299d9SKOSAKI Motohiro } 267c9f299d9SKOSAKI Motohiro 268fd538803SMichal Hocko return lru_size; 269b4536f0cSMichal Hocko 270b4536f0cSMichal Hocko } 271b4536f0cSMichal Hocko 2721da177e4SLinus Torvalds /* 2731d3d4437SGlauber Costa * Add a shrinker callback to be called from the vm. 2741da177e4SLinus Torvalds */ 2751d3d4437SGlauber Costa int register_shrinker(struct shrinker *shrinker) 2761da177e4SLinus Torvalds { 2771d3d4437SGlauber Costa size_t size = sizeof(*shrinker->nr_deferred); 2781d3d4437SGlauber Costa 2791d3d4437SGlauber Costa if (shrinker->flags & SHRINKER_NUMA_AWARE) 2801d3d4437SGlauber Costa size *= nr_node_ids; 2811d3d4437SGlauber Costa 2821d3d4437SGlauber Costa shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); 2831d3d4437SGlauber Costa if (!shrinker->nr_deferred) 2841d3d4437SGlauber Costa return -ENOMEM; 2851d3d4437SGlauber Costa 2861da177e4SLinus Torvalds down_write(&shrinker_rwsem); 2871da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 2881da177e4SLinus Torvalds up_write(&shrinker_rwsem); 2891d3d4437SGlauber Costa return 0; 2901da177e4SLinus Torvalds } 2918e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds /* 2941da177e4SLinus Torvalds * Remove one 2951da177e4SLinus Torvalds */ 2968e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 2971da177e4SLinus Torvalds { 2981da177e4SLinus Torvalds down_write(&shrinker_rwsem); 2991da177e4SLinus Torvalds list_del(&shrinker->list); 3001da177e4SLinus Torvalds up_write(&shrinker_rwsem); 301ae393321SAndrew Vagin kfree(shrinker->nr_deferred); 3021da177e4SLinus Torvalds } 3038e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds #define SHRINK_BATCH 128 3061d3d4437SGlauber Costa 307cb731d6cSVladimir Davydov static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, 3086b4f7799SJohannes Weiner struct shrinker *shrinker, 3096b4f7799SJohannes Weiner unsigned long nr_scanned, 3106b4f7799SJohannes Weiner unsigned long nr_eligible) 3111da177e4SLinus Torvalds { 31224f7c6b9SDave Chinner unsigned long freed = 0; 3131da177e4SLinus Torvalds unsigned long long delta; 314635697c6SKonstantin Khlebnikov long total_scan; 315d5bc5fd3SVladimir Davydov long freeable; 316acf92b48SDave Chinner long nr; 317acf92b48SDave Chinner long new_nr; 3181d3d4437SGlauber Costa int nid = shrinkctl->nid; 319e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 320e9299f50SDave Chinner : SHRINK_BATCH; 3215f33a080SShaohua Li long scanned = 0, next_deferred; 3221da177e4SLinus Torvalds 323d5bc5fd3SVladimir Davydov freeable = shrinker->count_objects(shrinker, shrinkctl); 324d5bc5fd3SVladimir Davydov if (freeable == 0) 3251d3d4437SGlauber Costa return 0; 326635697c6SKonstantin Khlebnikov 327acf92b48SDave Chinner /* 328acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 329acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 330acf92b48SDave Chinner * don't also do this scanning work. 331acf92b48SDave Chinner */ 3321d3d4437SGlauber Costa nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0); 333acf92b48SDave Chinner 334acf92b48SDave Chinner total_scan = nr; 3356b4f7799SJohannes Weiner delta = (4 * nr_scanned) / shrinker->seeks; 336d5bc5fd3SVladimir Davydov delta *= freeable; 3376b4f7799SJohannes Weiner do_div(delta, nr_eligible + 1); 338acf92b48SDave Chinner total_scan += delta; 339acf92b48SDave Chinner if (total_scan < 0) { 3408612c663SPintu Kumar pr_err("shrink_slab: %pF negative objects to delete nr=%ld\n", 341a0b02131SDave Chinner shrinker->scan_objects, total_scan); 342d5bc5fd3SVladimir Davydov total_scan = freeable; 3435f33a080SShaohua Li next_deferred = nr; 3445f33a080SShaohua Li } else 3455f33a080SShaohua Li next_deferred = total_scan; 346ea164d73SAndrea Arcangeli 347ea164d73SAndrea Arcangeli /* 3483567b59aSDave Chinner * We need to avoid excessive windup on filesystem shrinkers 3493567b59aSDave Chinner * due to large numbers of GFP_NOFS allocations causing the 3503567b59aSDave Chinner * shrinkers to return -1 all the time. This results in a large 3513567b59aSDave Chinner * nr being built up so when a shrink that can do some work 3523567b59aSDave Chinner * comes along it empties the entire cache due to nr >>> 353d5bc5fd3SVladimir Davydov * freeable. This is bad for sustaining a working set in 3543567b59aSDave Chinner * memory. 3553567b59aSDave Chinner * 3563567b59aSDave Chinner * Hence only allow the shrinker to scan the entire cache when 3573567b59aSDave Chinner * a large delta change is calculated directly. 3583567b59aSDave Chinner */ 359d5bc5fd3SVladimir Davydov if (delta < freeable / 4) 360d5bc5fd3SVladimir Davydov total_scan = min(total_scan, freeable / 2); 3613567b59aSDave Chinner 3623567b59aSDave Chinner /* 363ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 364ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 365ea164d73SAndrea Arcangeli * freeable entries. 366ea164d73SAndrea Arcangeli */ 367d5bc5fd3SVladimir Davydov if (total_scan > freeable * 2) 368d5bc5fd3SVladimir Davydov total_scan = freeable * 2; 3691da177e4SLinus Torvalds 37024f7c6b9SDave Chinner trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, 3716b4f7799SJohannes Weiner nr_scanned, nr_eligible, 372d5bc5fd3SVladimir Davydov freeable, delta, total_scan); 37309576073SDave Chinner 3740b1fb40aSVladimir Davydov /* 3750b1fb40aSVladimir Davydov * Normally, we should not scan less than batch_size objects in one 3760b1fb40aSVladimir Davydov * pass to avoid too frequent shrinker calls, but if the slab has less 3770b1fb40aSVladimir Davydov * than batch_size objects in total and we are really tight on memory, 3780b1fb40aSVladimir Davydov * we will try to reclaim all available objects, otherwise we can end 3790b1fb40aSVladimir Davydov * up failing allocations although there are plenty of reclaimable 3800b1fb40aSVladimir Davydov * objects spread over several slabs with usage less than the 3810b1fb40aSVladimir Davydov * batch_size. 3820b1fb40aSVladimir Davydov * 3830b1fb40aSVladimir Davydov * We detect the "tight on memory" situations by looking at the total 3840b1fb40aSVladimir Davydov * number of objects we want to scan (total_scan). If it is greater 385d5bc5fd3SVladimir Davydov * than the total number of objects on slab (freeable), we must be 3860b1fb40aSVladimir Davydov * scanning at high prio and therefore should try to reclaim as much as 3870b1fb40aSVladimir Davydov * possible. 3880b1fb40aSVladimir Davydov */ 3890b1fb40aSVladimir Davydov while (total_scan >= batch_size || 390d5bc5fd3SVladimir Davydov total_scan >= freeable) { 39124f7c6b9SDave Chinner unsigned long ret; 3920b1fb40aSVladimir Davydov unsigned long nr_to_scan = min(batch_size, total_scan); 3931da177e4SLinus Torvalds 3940b1fb40aSVladimir Davydov shrinkctl->nr_to_scan = nr_to_scan; 39524f7c6b9SDave Chinner ret = shrinker->scan_objects(shrinker, shrinkctl); 39624f7c6b9SDave Chinner if (ret == SHRINK_STOP) 3971da177e4SLinus Torvalds break; 39824f7c6b9SDave Chinner freed += ret; 39924f7c6b9SDave Chinner 4000b1fb40aSVladimir Davydov count_vm_events(SLABS_SCANNED, nr_to_scan); 4010b1fb40aSVladimir Davydov total_scan -= nr_to_scan; 4025f33a080SShaohua Li scanned += nr_to_scan; 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds cond_resched(); 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds 4075f33a080SShaohua Li if (next_deferred >= scanned) 4085f33a080SShaohua Li next_deferred -= scanned; 4095f33a080SShaohua Li else 4105f33a080SShaohua Li next_deferred = 0; 411acf92b48SDave Chinner /* 412acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 413acf92b48SDave Chinner * manner that handles concurrent updates. If we exhausted the 414acf92b48SDave Chinner * scan, there is no need to do an update. 415acf92b48SDave Chinner */ 4165f33a080SShaohua Li if (next_deferred > 0) 4175f33a080SShaohua Li new_nr = atomic_long_add_return(next_deferred, 4181d3d4437SGlauber Costa &shrinker->nr_deferred[nid]); 41983aeeadaSKonstantin Khlebnikov else 4201d3d4437SGlauber Costa new_nr = atomic_long_read(&shrinker->nr_deferred[nid]); 421acf92b48SDave Chinner 422df9024a8SDave Hansen trace_mm_shrink_slab_end(shrinker, nid, freed, nr, new_nr, total_scan); 4231d3d4437SGlauber Costa return freed; 4241d3d4437SGlauber Costa } 4251d3d4437SGlauber Costa 4266b4f7799SJohannes Weiner /** 427cb731d6cSVladimir Davydov * shrink_slab - shrink slab caches 4286b4f7799SJohannes Weiner * @gfp_mask: allocation context 4296b4f7799SJohannes Weiner * @nid: node whose slab caches to target 430cb731d6cSVladimir Davydov * @memcg: memory cgroup whose slab caches to target 4316b4f7799SJohannes Weiner * @nr_scanned: pressure numerator 4326b4f7799SJohannes Weiner * @nr_eligible: pressure denominator 4331d3d4437SGlauber Costa * 4346b4f7799SJohannes Weiner * Call the shrink functions to age shrinkable caches. 4351d3d4437SGlauber Costa * 4366b4f7799SJohannes Weiner * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, 4376b4f7799SJohannes Weiner * unaware shrinkers will receive a node id of 0 instead. 4381d3d4437SGlauber Costa * 439cb731d6cSVladimir Davydov * @memcg specifies the memory cgroup to target. If it is not NULL, 440cb731d6cSVladimir Davydov * only shrinkers with SHRINKER_MEMCG_AWARE set will be called to scan 4410fc9f58aSVladimir Davydov * objects from the memory cgroup specified. Otherwise, only unaware 4420fc9f58aSVladimir Davydov * shrinkers are called. 443cb731d6cSVladimir Davydov * 4446b4f7799SJohannes Weiner * @nr_scanned and @nr_eligible form a ratio that indicate how much of 4456b4f7799SJohannes Weiner * the available objects should be scanned. Page reclaim for example 4466b4f7799SJohannes Weiner * passes the number of pages scanned and the number of pages on the 4476b4f7799SJohannes Weiner * LRU lists that it considered on @nid, plus a bias in @nr_scanned 4486b4f7799SJohannes Weiner * when it encountered mapped pages. The ratio is further biased by 4496b4f7799SJohannes Weiner * the ->seeks setting of the shrink function, which indicates the 4506b4f7799SJohannes Weiner * cost to recreate an object relative to that of an LRU page. 4511d3d4437SGlauber Costa * 4526b4f7799SJohannes Weiner * Returns the number of reclaimed slab objects. 4531d3d4437SGlauber Costa */ 454cb731d6cSVladimir Davydov static unsigned long shrink_slab(gfp_t gfp_mask, int nid, 455cb731d6cSVladimir Davydov struct mem_cgroup *memcg, 4566b4f7799SJohannes Weiner unsigned long nr_scanned, 4576b4f7799SJohannes Weiner unsigned long nr_eligible) 4581d3d4437SGlauber Costa { 4591d3d4437SGlauber Costa struct shrinker *shrinker; 4601d3d4437SGlauber Costa unsigned long freed = 0; 4611d3d4437SGlauber Costa 4620fc9f58aSVladimir Davydov if (memcg && (!memcg_kmem_enabled() || !mem_cgroup_online(memcg))) 463cb731d6cSVladimir Davydov return 0; 464cb731d6cSVladimir Davydov 4656b4f7799SJohannes Weiner if (nr_scanned == 0) 4666b4f7799SJohannes Weiner nr_scanned = SWAP_CLUSTER_MAX; 4671d3d4437SGlauber Costa 4681d3d4437SGlauber Costa if (!down_read_trylock(&shrinker_rwsem)) { 4691d3d4437SGlauber Costa /* 4701d3d4437SGlauber Costa * If we would return 0, our callers would understand that we 4711d3d4437SGlauber Costa * have nothing else to shrink and give up trying. By returning 4721d3d4437SGlauber Costa * 1 we keep it going and assume we'll be able to shrink next 4731d3d4437SGlauber Costa * time. 4741d3d4437SGlauber Costa */ 4751d3d4437SGlauber Costa freed = 1; 4761d3d4437SGlauber Costa goto out; 4771d3d4437SGlauber Costa } 4781d3d4437SGlauber Costa 4791d3d4437SGlauber Costa list_for_each_entry(shrinker, &shrinker_list, list) { 4806b4f7799SJohannes Weiner struct shrink_control sc = { 4816b4f7799SJohannes Weiner .gfp_mask = gfp_mask, 4826b4f7799SJohannes Weiner .nid = nid, 483cb731d6cSVladimir Davydov .memcg = memcg, 4846b4f7799SJohannes Weiner }; 4856b4f7799SJohannes Weiner 4860fc9f58aSVladimir Davydov /* 4870fc9f58aSVladimir Davydov * If kernel memory accounting is disabled, we ignore 4880fc9f58aSVladimir Davydov * SHRINKER_MEMCG_AWARE flag and call all shrinkers 4890fc9f58aSVladimir Davydov * passing NULL for memcg. 4900fc9f58aSVladimir Davydov */ 4910fc9f58aSVladimir Davydov if (memcg_kmem_enabled() && 4920fc9f58aSVladimir Davydov !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE)) 493cb731d6cSVladimir Davydov continue; 494cb731d6cSVladimir Davydov 4956b4f7799SJohannes Weiner if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) 4966b4f7799SJohannes Weiner sc.nid = 0; 4976b4f7799SJohannes Weiner 498cb731d6cSVladimir Davydov freed += do_shrink_slab(&sc, shrinker, nr_scanned, nr_eligible); 499ec97097bSVladimir Davydov } 5001d3d4437SGlauber Costa 5011da177e4SLinus Torvalds up_read(&shrinker_rwsem); 502f06590bdSMinchan Kim out: 503f06590bdSMinchan Kim cond_resched(); 50424f7c6b9SDave Chinner return freed; 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds 507cb731d6cSVladimir Davydov void drop_slab_node(int nid) 508cb731d6cSVladimir Davydov { 509cb731d6cSVladimir Davydov unsigned long freed; 510cb731d6cSVladimir Davydov 511cb731d6cSVladimir Davydov do { 512cb731d6cSVladimir Davydov struct mem_cgroup *memcg = NULL; 513cb731d6cSVladimir Davydov 514cb731d6cSVladimir Davydov freed = 0; 515cb731d6cSVladimir Davydov do { 516cb731d6cSVladimir Davydov freed += shrink_slab(GFP_KERNEL, nid, memcg, 517cb731d6cSVladimir Davydov 1000, 1000); 518cb731d6cSVladimir Davydov } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 519cb731d6cSVladimir Davydov } while (freed > 10); 520cb731d6cSVladimir Davydov } 521cb731d6cSVladimir Davydov 522cb731d6cSVladimir Davydov void drop_slab(void) 523cb731d6cSVladimir Davydov { 524cb731d6cSVladimir Davydov int nid; 525cb731d6cSVladimir Davydov 526cb731d6cSVladimir Davydov for_each_online_node(nid) 527cb731d6cSVladimir Davydov drop_slab_node(nid); 528cb731d6cSVladimir Davydov } 529cb731d6cSVladimir Davydov 5301da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 5311da177e4SLinus Torvalds { 532ceddc3a5SJohannes Weiner /* 533ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 534ceddc3a5SJohannes Weiner * that isolated the page, the page cache radix tree and 535ceddc3a5SJohannes Weiner * optional buffer heads at page->private. 536ceddc3a5SJohannes Weiner */ 537edcf4748SJohannes Weiner return page_count(page) - page_has_private(page) == 2; 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds 540703c2708STejun Heo static int may_write_to_inode(struct inode *inode, struct scan_control *sc) 5411da177e4SLinus Torvalds { 542930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 5431da177e4SLinus Torvalds return 1; 544703c2708STejun Heo if (!inode_write_congested(inode)) 5451da177e4SLinus Torvalds return 1; 546703c2708STejun Heo if (inode_to_bdi(inode) == current->backing_dev_info) 5471da177e4SLinus Torvalds return 1; 5481da177e4SLinus Torvalds return 0; 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds /* 5521da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 5531da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 5541da177e4SLinus Torvalds * fsync(), msync() or close(). 5551da177e4SLinus Torvalds * 5561da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 5571da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 5581da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 5591da177e4SLinus Torvalds * 5601da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 5611da177e4SLinus Torvalds * __GFP_FS. 5621da177e4SLinus Torvalds */ 5631da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 5641da177e4SLinus Torvalds struct page *page, int error) 5651da177e4SLinus Torvalds { 5667eaceaccSJens Axboe lock_page(page); 5673e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 5683e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 5691da177e4SLinus Torvalds unlock_page(page); 5701da177e4SLinus Torvalds } 5711da177e4SLinus Torvalds 57204e62a29SChristoph Lameter /* possible outcome of pageout() */ 57304e62a29SChristoph Lameter typedef enum { 57404e62a29SChristoph Lameter /* failed to write page out, page is locked */ 57504e62a29SChristoph Lameter PAGE_KEEP, 57604e62a29SChristoph Lameter /* move page to the active list, page is locked */ 57704e62a29SChristoph Lameter PAGE_ACTIVATE, 57804e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 57904e62a29SChristoph Lameter PAGE_SUCCESS, 58004e62a29SChristoph Lameter /* page is clean and locked */ 58104e62a29SChristoph Lameter PAGE_CLEAN, 58204e62a29SChristoph Lameter } pageout_t; 58304e62a29SChristoph Lameter 5841da177e4SLinus Torvalds /* 5851742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 5861742f19fSAndrew Morton * Calls ->writepage(). 5871da177e4SLinus Torvalds */ 588c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping, 5897d3579e8SKOSAKI Motohiro struct scan_control *sc) 5901da177e4SLinus Torvalds { 5911da177e4SLinus Torvalds /* 5921da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 5931da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 5941da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 5951da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 5961da177e4SLinus Torvalds * PagePrivate for that. 5971da177e4SLinus Torvalds * 5988174202bSAl Viro * If this process is currently in __generic_file_write_iter() against 5991da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 6001da177e4SLinus Torvalds * will block. 6011da177e4SLinus Torvalds * 6021da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 6031da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 6041da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 6051da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 6061da177e4SLinus Torvalds */ 6071da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 6081da177e4SLinus Torvalds return PAGE_KEEP; 6091da177e4SLinus Torvalds if (!mapping) { 6101da177e4SLinus Torvalds /* 6111da177e4SLinus Torvalds * Some data journaling orphaned pages can have 6121da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 6131da177e4SLinus Torvalds */ 614266cf658SDavid Howells if (page_has_private(page)) { 6151da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 6161da177e4SLinus Torvalds ClearPageDirty(page); 617b1de0d13SMitchel Humpherys pr_info("%s: orphaned page\n", __func__); 6181da177e4SLinus Torvalds return PAGE_CLEAN; 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds return PAGE_KEEP; 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 6241da177e4SLinus Torvalds return PAGE_ACTIVATE; 625703c2708STejun Heo if (!may_write_to_inode(mapping->host, sc)) 6261da177e4SLinus Torvalds return PAGE_KEEP; 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 6291da177e4SLinus Torvalds int res; 6301da177e4SLinus Torvalds struct writeback_control wbc = { 6311da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 6321da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 633111ebb6eSOGAWA Hirofumi .range_start = 0, 634111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 6351da177e4SLinus Torvalds .for_reclaim = 1, 6361da177e4SLinus Torvalds }; 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds SetPageReclaim(page); 6391da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 6401da177e4SLinus Torvalds if (res < 0) 6411da177e4SLinus Torvalds handle_write_error(mapping, page, res); 642994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 6431da177e4SLinus Torvalds ClearPageReclaim(page); 6441da177e4SLinus Torvalds return PAGE_ACTIVATE; 6451da177e4SLinus Torvalds } 646c661b078SAndy Whitcroft 6471da177e4SLinus Torvalds if (!PageWriteback(page)) { 6481da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 6491da177e4SLinus Torvalds ClearPageReclaim(page); 6501da177e4SLinus Torvalds } 6513aa23851Syalin wang trace_mm_vmscan_writepage(page); 652c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_WRITE); 6531da177e4SLinus Torvalds return PAGE_SUCCESS; 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds return PAGE_CLEAN; 6571da177e4SLinus Torvalds } 6581da177e4SLinus Torvalds 659a649fd92SAndrew Morton /* 660e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 661e286781dSNick Piggin * gets returned with a refcount of 0. 662a649fd92SAndrew Morton */ 663a528910eSJohannes Weiner static int __remove_mapping(struct address_space *mapping, struct page *page, 664a528910eSJohannes Weiner bool reclaimed) 66549d2e9ccSChristoph Lameter { 666c4843a75SGreg Thelen unsigned long flags; 667c4843a75SGreg Thelen 66828e4d965SNick Piggin BUG_ON(!PageLocked(page)); 66928e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 67049d2e9ccSChristoph Lameter 671c4843a75SGreg Thelen spin_lock_irqsave(&mapping->tree_lock, flags); 67249d2e9ccSChristoph Lameter /* 6730fd0e6b0SNick Piggin * The non racy check for a busy page. 6740fd0e6b0SNick Piggin * 6750fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 6760fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 6770fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 6780fd0e6b0SNick Piggin * here, then the following race may occur: 6790fd0e6b0SNick Piggin * 6800fd0e6b0SNick Piggin * get_user_pages(&page); 6810fd0e6b0SNick Piggin * [user mapping goes away] 6820fd0e6b0SNick Piggin * write_to(page); 6830fd0e6b0SNick Piggin * !PageDirty(page) [good] 6840fd0e6b0SNick Piggin * SetPageDirty(page); 6850fd0e6b0SNick Piggin * put_page(page); 6860fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 6870fd0e6b0SNick Piggin * 6880fd0e6b0SNick Piggin * [oops, our write_to data is lost] 6890fd0e6b0SNick Piggin * 6900fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 6910fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 6920139aa7bSJoonsoo Kim * load is not satisfied before that of page->_refcount. 6930fd0e6b0SNick Piggin * 6940fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 6950fd0e6b0SNick Piggin * and thus under tree_lock, then this ordering is not required. 69649d2e9ccSChristoph Lameter */ 697fe896d18SJoonsoo Kim if (!page_ref_freeze(page, 2)) 69849d2e9ccSChristoph Lameter goto cannot_free; 699e286781dSNick Piggin /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ 700e286781dSNick Piggin if (unlikely(PageDirty(page))) { 701fe896d18SJoonsoo Kim page_ref_unfreeze(page, 2); 70249d2e9ccSChristoph Lameter goto cannot_free; 703e286781dSNick Piggin } 70449d2e9ccSChristoph Lameter 70549d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 70649d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 7070a31bc97SJohannes Weiner mem_cgroup_swapout(page, swap); 70849d2e9ccSChristoph Lameter __delete_from_swap_cache(page); 709c4843a75SGreg Thelen spin_unlock_irqrestore(&mapping->tree_lock, flags); 7100a31bc97SJohannes Weiner swapcache_free(swap); 711e286781dSNick Piggin } else { 7126072d13cSLinus Torvalds void (*freepage)(struct page *); 713a528910eSJohannes Weiner void *shadow = NULL; 7146072d13cSLinus Torvalds 7156072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 716a528910eSJohannes Weiner /* 717a528910eSJohannes Weiner * Remember a shadow entry for reclaimed file cache in 718a528910eSJohannes Weiner * order to detect refaults, thus thrashing, later on. 719a528910eSJohannes Weiner * 720a528910eSJohannes Weiner * But don't store shadows in an address space that is 721a528910eSJohannes Weiner * already exiting. This is not just an optizimation, 722a528910eSJohannes Weiner * inode reclaim needs to empty out the radix tree or 723a528910eSJohannes Weiner * the nodes are lost. Don't plant shadows behind its 724a528910eSJohannes Weiner * back. 725f9fe48beSRoss Zwisler * 726f9fe48beSRoss Zwisler * We also don't store shadows for DAX mappings because the 727f9fe48beSRoss Zwisler * only page cache pages found in these are zero pages 728f9fe48beSRoss Zwisler * covering holes, and because we don't want to mix DAX 729f9fe48beSRoss Zwisler * exceptional entries and shadow exceptional entries in the 730f9fe48beSRoss Zwisler * same page_tree. 731a528910eSJohannes Weiner */ 732a528910eSJohannes Weiner if (reclaimed && page_is_file_cache(page) && 733f9fe48beSRoss Zwisler !mapping_exiting(mapping) && !dax_mapping(mapping)) 734a528910eSJohannes Weiner shadow = workingset_eviction(mapping, page); 73562cccb8cSJohannes Weiner __delete_from_page_cache(page, shadow); 736c4843a75SGreg Thelen spin_unlock_irqrestore(&mapping->tree_lock, flags); 7376072d13cSLinus Torvalds 7386072d13cSLinus Torvalds if (freepage != NULL) 7396072d13cSLinus Torvalds freepage(page); 740e286781dSNick Piggin } 741e286781dSNick Piggin 74249d2e9ccSChristoph Lameter return 1; 74349d2e9ccSChristoph Lameter 74449d2e9ccSChristoph Lameter cannot_free: 745c4843a75SGreg Thelen spin_unlock_irqrestore(&mapping->tree_lock, flags); 74649d2e9ccSChristoph Lameter return 0; 74749d2e9ccSChristoph Lameter } 74849d2e9ccSChristoph Lameter 7491da177e4SLinus Torvalds /* 750e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 751e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 752e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 753e286781dSNick Piggin * this page. 754e286781dSNick Piggin */ 755e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 756e286781dSNick Piggin { 757a528910eSJohannes Weiner if (__remove_mapping(mapping, page, false)) { 758e286781dSNick Piggin /* 759e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 760e286781dSNick Piggin * drops the pagecache ref for us without requiring another 761e286781dSNick Piggin * atomic operation. 762e286781dSNick Piggin */ 763fe896d18SJoonsoo Kim page_ref_unfreeze(page, 1); 764e286781dSNick Piggin return 1; 765e286781dSNick Piggin } 766e286781dSNick Piggin return 0; 767e286781dSNick Piggin } 768e286781dSNick Piggin 769894bc310SLee Schermerhorn /** 770894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 771894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 772894bc310SLee Schermerhorn * 773894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 774894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 775894bc310SLee Schermerhorn * 776894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 777894bc310SLee Schermerhorn */ 778894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 779894bc310SLee Schermerhorn { 7800ec3b74cSVlastimil Babka bool is_unevictable; 781bbfd28eeSLee Schermerhorn int was_unevictable = PageUnevictable(page); 782894bc310SLee Schermerhorn 783309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 784894bc310SLee Schermerhorn 785894bc310SLee Schermerhorn redo: 786894bc310SLee Schermerhorn ClearPageUnevictable(page); 787894bc310SLee Schermerhorn 78839b5f29aSHugh Dickins if (page_evictable(page)) { 789894bc310SLee Schermerhorn /* 790894bc310SLee Schermerhorn * For evictable pages, we can use the cache. 791894bc310SLee Schermerhorn * In event of a race, worst case is we end up with an 792894bc310SLee Schermerhorn * unevictable page on [in]active list. 793894bc310SLee Schermerhorn * We know how to handle that. 794894bc310SLee Schermerhorn */ 7950ec3b74cSVlastimil Babka is_unevictable = false; 796c53954a0SMel Gorman lru_cache_add(page); 797894bc310SLee Schermerhorn } else { 798894bc310SLee Schermerhorn /* 799894bc310SLee Schermerhorn * Put unevictable pages directly on zone's unevictable 800894bc310SLee Schermerhorn * list. 801894bc310SLee Schermerhorn */ 8020ec3b74cSVlastimil Babka is_unevictable = true; 803894bc310SLee Schermerhorn add_page_to_unevictable_list(page); 8046a7b9548SJohannes Weiner /* 80521ee9f39SMinchan Kim * When racing with an mlock or AS_UNEVICTABLE clearing 80621ee9f39SMinchan Kim * (page is unlocked) make sure that if the other thread 80721ee9f39SMinchan Kim * does not observe our setting of PG_lru and fails 80824513264SHugh Dickins * isolation/check_move_unevictable_pages, 80921ee9f39SMinchan Kim * we see PG_mlocked/AS_UNEVICTABLE cleared below and move 8106a7b9548SJohannes Weiner * the page back to the evictable list. 8116a7b9548SJohannes Weiner * 81221ee9f39SMinchan Kim * The other side is TestClearPageMlocked() or shmem_lock(). 8136a7b9548SJohannes Weiner */ 8146a7b9548SJohannes Weiner smp_mb(); 815894bc310SLee Schermerhorn } 816894bc310SLee Schermerhorn 817894bc310SLee Schermerhorn /* 818894bc310SLee Schermerhorn * page's status can change while we move it among lru. If an evictable 819894bc310SLee Schermerhorn * page is on unevictable list, it never be freed. To avoid that, 820894bc310SLee Schermerhorn * check after we added it to the list, again. 821894bc310SLee Schermerhorn */ 8220ec3b74cSVlastimil Babka if (is_unevictable && page_evictable(page)) { 823894bc310SLee Schermerhorn if (!isolate_lru_page(page)) { 824894bc310SLee Schermerhorn put_page(page); 825894bc310SLee Schermerhorn goto redo; 826894bc310SLee Schermerhorn } 827894bc310SLee Schermerhorn /* This means someone else dropped this page from LRU 828894bc310SLee Schermerhorn * So, it will be freed or putback to LRU again. There is 829894bc310SLee Schermerhorn * nothing to do here. 830894bc310SLee Schermerhorn */ 831894bc310SLee Schermerhorn } 832894bc310SLee Schermerhorn 8330ec3b74cSVlastimil Babka if (was_unevictable && !is_unevictable) 834bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGRESCUED); 8350ec3b74cSVlastimil Babka else if (!was_unevictable && is_unevictable) 836bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGCULLED); 837bbfd28eeSLee Schermerhorn 838894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 839894bc310SLee Schermerhorn } 840894bc310SLee Schermerhorn 841dfc8d636SJohannes Weiner enum page_references { 842dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 843dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 84464574746SJohannes Weiner PAGEREF_KEEP, 845dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 846dfc8d636SJohannes Weiner }; 847dfc8d636SJohannes Weiner 848dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 849dfc8d636SJohannes Weiner struct scan_control *sc) 850dfc8d636SJohannes Weiner { 85164574746SJohannes Weiner int referenced_ptes, referenced_page; 852dfc8d636SJohannes Weiner unsigned long vm_flags; 853dfc8d636SJohannes Weiner 854c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 855c3ac9a8aSJohannes Weiner &vm_flags); 85664574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 857dfc8d636SJohannes Weiner 858dfc8d636SJohannes Weiner /* 859dfc8d636SJohannes Weiner * Mlock lost the isolation race with us. Let try_to_unmap() 860dfc8d636SJohannes Weiner * move the page to the unevictable list. 861dfc8d636SJohannes Weiner */ 862dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 863dfc8d636SJohannes Weiner return PAGEREF_RECLAIM; 864dfc8d636SJohannes Weiner 86564574746SJohannes Weiner if (referenced_ptes) { 866e4898273SMichal Hocko if (PageSwapBacked(page)) 86764574746SJohannes Weiner return PAGEREF_ACTIVATE; 86864574746SJohannes Weiner /* 86964574746SJohannes Weiner * All mapped pages start out with page table 87064574746SJohannes Weiner * references from the instantiating fault, so we need 87164574746SJohannes Weiner * to look twice if a mapped file page is used more 87264574746SJohannes Weiner * than once. 87364574746SJohannes Weiner * 87464574746SJohannes Weiner * Mark it and spare it for another trip around the 87564574746SJohannes Weiner * inactive list. Another page table reference will 87664574746SJohannes Weiner * lead to its activation. 87764574746SJohannes Weiner * 87864574746SJohannes Weiner * Note: the mark is set for activated pages as well 87964574746SJohannes Weiner * so that recently deactivated but used pages are 88064574746SJohannes Weiner * quickly recovered. 88164574746SJohannes Weiner */ 88264574746SJohannes Weiner SetPageReferenced(page); 88364574746SJohannes Weiner 88434dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 885dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 886dfc8d636SJohannes Weiner 887c909e993SKonstantin Khlebnikov /* 888c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 889c909e993SKonstantin Khlebnikov */ 890c909e993SKonstantin Khlebnikov if (vm_flags & VM_EXEC) 891c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 892c909e993SKonstantin Khlebnikov 89364574746SJohannes Weiner return PAGEREF_KEEP; 89464574746SJohannes Weiner } 89564574746SJohannes Weiner 896dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 8972e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 898dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 89964574746SJohannes Weiner 90064574746SJohannes Weiner return PAGEREF_RECLAIM; 901dfc8d636SJohannes Weiner } 902dfc8d636SJohannes Weiner 903e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 904e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page, 905e2be15f6SMel Gorman bool *dirty, bool *writeback) 906e2be15f6SMel Gorman { 907b4597226SMel Gorman struct address_space *mapping; 908b4597226SMel Gorman 909e2be15f6SMel Gorman /* 910e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 911e2be15f6SMel Gorman * from reclaim context. Do not stall reclaim based on them 912e2be15f6SMel Gorman */ 913e2be15f6SMel Gorman if (!page_is_file_cache(page)) { 914e2be15f6SMel Gorman *dirty = false; 915e2be15f6SMel Gorman *writeback = false; 916e2be15f6SMel Gorman return; 917e2be15f6SMel Gorman } 918e2be15f6SMel Gorman 919e2be15f6SMel Gorman /* By default assume that the page flags are accurate */ 920e2be15f6SMel Gorman *dirty = PageDirty(page); 921e2be15f6SMel Gorman *writeback = PageWriteback(page); 922b4597226SMel Gorman 923b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 924b4597226SMel Gorman if (!page_has_private(page)) 925b4597226SMel Gorman return; 926b4597226SMel Gorman 927b4597226SMel Gorman mapping = page_mapping(page); 928b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 929b4597226SMel Gorman mapping->a_ops->is_dirty_writeback(page, dirty, writeback); 930e2be15f6SMel Gorman } 931e2be15f6SMel Gorman 9323c710c1aSMichal Hocko struct reclaim_stat { 9333c710c1aSMichal Hocko unsigned nr_dirty; 9343c710c1aSMichal Hocko unsigned nr_unqueued_dirty; 9353c710c1aSMichal Hocko unsigned nr_congested; 9363c710c1aSMichal Hocko unsigned nr_writeback; 9373c710c1aSMichal Hocko unsigned nr_immediate; 9385bccd166SMichal Hocko unsigned nr_activate; 9395bccd166SMichal Hocko unsigned nr_ref_keep; 9405bccd166SMichal Hocko unsigned nr_unmap_fail; 9413c710c1aSMichal Hocko }; 9423c710c1aSMichal Hocko 943e286781dSNick Piggin /* 9441742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 9451da177e4SLinus Torvalds */ 9461742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list, 947599d0c95SMel Gorman struct pglist_data *pgdat, 948f84f6e2bSMel Gorman struct scan_control *sc, 94902c6de8dSMinchan Kim enum ttu_flags ttu_flags, 9503c710c1aSMichal Hocko struct reclaim_stat *stat, 95102c6de8dSMinchan Kim bool force_reclaim) 9521da177e4SLinus Torvalds { 9531da177e4SLinus Torvalds LIST_HEAD(ret_pages); 954abe4c3b5SMel Gorman LIST_HEAD(free_pages); 9551da177e4SLinus Torvalds int pgactivate = 0; 9563c710c1aSMichal Hocko unsigned nr_unqueued_dirty = 0; 9573c710c1aSMichal Hocko unsigned nr_dirty = 0; 9583c710c1aSMichal Hocko unsigned nr_congested = 0; 9593c710c1aSMichal Hocko unsigned nr_reclaimed = 0; 9603c710c1aSMichal Hocko unsigned nr_writeback = 0; 9613c710c1aSMichal Hocko unsigned nr_immediate = 0; 9625bccd166SMichal Hocko unsigned nr_ref_keep = 0; 9635bccd166SMichal Hocko unsigned nr_unmap_fail = 0; 9641da177e4SLinus Torvalds 9651da177e4SLinus Torvalds cond_resched(); 9661da177e4SLinus Torvalds 9671da177e4SLinus Torvalds while (!list_empty(page_list)) { 9681da177e4SLinus Torvalds struct address_space *mapping; 9691da177e4SLinus Torvalds struct page *page; 9701da177e4SLinus Torvalds int may_enter_fs; 97102c6de8dSMinchan Kim enum page_references references = PAGEREF_RECLAIM_CLEAN; 972e2be15f6SMel Gorman bool dirty, writeback; 973854e9ed0SMinchan Kim bool lazyfree = false; 974854e9ed0SMinchan Kim int ret = SWAP_SUCCESS; 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds cond_resched(); 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds page = lru_to_page(page_list); 9791da177e4SLinus Torvalds list_del(&page->lru); 9801da177e4SLinus Torvalds 981529ae9aaSNick Piggin if (!trylock_page(page)) 9821da177e4SLinus Torvalds goto keep; 9831da177e4SLinus Torvalds 984309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds sc->nr_scanned++; 98780e43426SChristoph Lameter 98839b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) 989b291f000SNick Piggin goto cull_mlocked; 990894bc310SLee Schermerhorn 991a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 99280e43426SChristoph Lameter goto keep_locked; 99380e43426SChristoph Lameter 9941da177e4SLinus Torvalds /* Double the slab pressure for mapped and swapcache pages */ 9951da177e4SLinus Torvalds if (page_mapped(page) || PageSwapCache(page)) 9961da177e4SLinus Torvalds sc->nr_scanned++; 9971da177e4SLinus Torvalds 998c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 999c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 1000c661b078SAndy Whitcroft 1001e62e384eSMichal Hocko /* 1002e2be15f6SMel Gorman * The number of dirty pages determines if a zone is marked 1003e2be15f6SMel Gorman * reclaim_congested which affects wait_iff_congested. kswapd 1004e2be15f6SMel Gorman * will stall and start writing pages if the tail of the LRU 1005e2be15f6SMel Gorman * is all dirty unqueued pages. 1006e2be15f6SMel Gorman */ 1007e2be15f6SMel Gorman page_check_dirty_writeback(page, &dirty, &writeback); 1008e2be15f6SMel Gorman if (dirty || writeback) 1009e2be15f6SMel Gorman nr_dirty++; 1010e2be15f6SMel Gorman 1011e2be15f6SMel Gorman if (dirty && !writeback) 1012e2be15f6SMel Gorman nr_unqueued_dirty++; 1013e2be15f6SMel Gorman 1014d04e8acdSMel Gorman /* 1015d04e8acdSMel Gorman * Treat this page as congested if the underlying BDI is or if 1016d04e8acdSMel Gorman * pages are cycling through the LRU so quickly that the 1017d04e8acdSMel Gorman * pages marked for immediate reclaim are making it to the 1018d04e8acdSMel Gorman * end of the LRU a second time. 1019d04e8acdSMel Gorman */ 1020e2be15f6SMel Gorman mapping = page_mapping(page); 10211da58ee2SJamie Liu if (((dirty || writeback) && mapping && 1022703c2708STejun Heo inode_write_congested(mapping->host)) || 1023d04e8acdSMel Gorman (writeback && PageReclaim(page))) 1024e2be15f6SMel Gorman nr_congested++; 1025e2be15f6SMel Gorman 1026e2be15f6SMel Gorman /* 1027283aba9fSMel Gorman * If a page at the tail of the LRU is under writeback, there 1028283aba9fSMel Gorman * are three cases to consider. 1029e62e384eSMichal Hocko * 1030283aba9fSMel Gorman * 1) If reclaim is encountering an excessive number of pages 1031283aba9fSMel Gorman * under writeback and this page is both under writeback and 1032283aba9fSMel Gorman * PageReclaim then it indicates that pages are being queued 1033283aba9fSMel Gorman * for IO but are being recycled through the LRU before the 1034283aba9fSMel Gorman * IO can complete. Waiting on the page itself risks an 1035283aba9fSMel Gorman * indefinite stall if it is impossible to writeback the 1036283aba9fSMel Gorman * page due to IO error or disconnected storage so instead 1037b1a6f21eSMel Gorman * note that the LRU is being scanned too quickly and the 1038b1a6f21eSMel Gorman * caller can stall after page list has been processed. 1039c3b94f44SHugh Dickins * 104097c9341fSTejun Heo * 2) Global or new memcg reclaim encounters a page that is 1041ecf5fc6eSMichal Hocko * not marked for immediate reclaim, or the caller does not 1042ecf5fc6eSMichal Hocko * have __GFP_FS (or __GFP_IO if it's simply going to swap, 1043ecf5fc6eSMichal Hocko * not to fs). In this case mark the page for immediate 104497c9341fSTejun Heo * reclaim and continue scanning. 1045283aba9fSMel Gorman * 1046ecf5fc6eSMichal Hocko * Require may_enter_fs because we would wait on fs, which 1047ecf5fc6eSMichal Hocko * may not have submitted IO yet. And the loop driver might 1048283aba9fSMel Gorman * enter reclaim, and deadlock if it waits on a page for 1049283aba9fSMel Gorman * which it is needed to do the write (loop masks off 1050283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 1051283aba9fSMel Gorman * would probably show more reasons. 1052283aba9fSMel Gorman * 10537fadc820SHugh Dickins * 3) Legacy memcg encounters a page that is already marked 1054283aba9fSMel Gorman * PageReclaim. memcg does not have any dirty pages 1055283aba9fSMel Gorman * throttling so we could easily OOM just because too many 1056283aba9fSMel Gorman * pages are in writeback and there is nothing else to 1057283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 1058e62e384eSMichal Hocko */ 1059283aba9fSMel Gorman if (PageWriteback(page)) { 1060283aba9fSMel Gorman /* Case 1 above */ 1061283aba9fSMel Gorman if (current_is_kswapd() && 1062283aba9fSMel Gorman PageReclaim(page) && 1063599d0c95SMel Gorman test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { 1064b1a6f21eSMel Gorman nr_immediate++; 1065b1a6f21eSMel Gorman goto keep_locked; 1066283aba9fSMel Gorman 1067283aba9fSMel Gorman /* Case 2 above */ 106897c9341fSTejun Heo } else if (sane_reclaim(sc) || 1069ecf5fc6eSMichal Hocko !PageReclaim(page) || !may_enter_fs) { 1070c3b94f44SHugh Dickins /* 1071c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 1072c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 1073c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 1074c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 1075c3b94f44SHugh Dickins * enough to care. What we do want is for this 1076c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 1077c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 1078c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 1079c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 1080c3b94f44SHugh Dickins */ 1081c3b94f44SHugh Dickins SetPageReclaim(page); 108292df3a72SMel Gorman nr_writeback++; 1083c3b94f44SHugh Dickins goto keep_locked; 1084283aba9fSMel Gorman 1085283aba9fSMel Gorman /* Case 3 above */ 1086283aba9fSMel Gorman } else { 10877fadc820SHugh Dickins unlock_page(page); 1088c3b94f44SHugh Dickins wait_on_page_writeback(page); 10897fadc820SHugh Dickins /* then go back and try same page again */ 10907fadc820SHugh Dickins list_add_tail(&page->lru, page_list); 10917fadc820SHugh Dickins continue; 1092e62e384eSMichal Hocko } 1093283aba9fSMel Gorman } 10941da177e4SLinus Torvalds 109502c6de8dSMinchan Kim if (!force_reclaim) 10966a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 109702c6de8dSMinchan Kim 1098dfc8d636SJohannes Weiner switch (references) { 1099dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 11001da177e4SLinus Torvalds goto activate_locked; 110164574746SJohannes Weiner case PAGEREF_KEEP: 11025bccd166SMichal Hocko nr_ref_keep++; 110364574746SJohannes Weiner goto keep_locked; 1104dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 1105dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 1106dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 1107dfc8d636SJohannes Weiner } 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds /* 11101da177e4SLinus Torvalds * Anonymous process memory has backing store? 11111da177e4SLinus Torvalds * Try to allocate it some swap space here. 11121da177e4SLinus Torvalds */ 1113b291f000SNick Piggin if (PageAnon(page) && !PageSwapCache(page)) { 111463eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 111563eb6b93SHugh Dickins goto keep_locked; 11165bc7b8acSShaohua Li if (!add_to_swap(page, page_list)) 11171da177e4SLinus Torvalds goto activate_locked; 1118854e9ed0SMinchan Kim lazyfree = true; 111963eb6b93SHugh Dickins may_enter_fs = 1; 11201da177e4SLinus Torvalds 1121e2be15f6SMel Gorman /* Adding to swap updated mapping */ 11221da177e4SLinus Torvalds mapping = page_mapping(page); 11237751b2daSKirill A. Shutemov } else if (unlikely(PageTransHuge(page))) { 11247751b2daSKirill A. Shutemov /* Split file THP */ 11257751b2daSKirill A. Shutemov if (split_huge_page_to_list(page, page_list)) 11267751b2daSKirill A. Shutemov goto keep_locked; 1127e2be15f6SMel Gorman } 11281da177e4SLinus Torvalds 11297751b2daSKirill A. Shutemov VM_BUG_ON_PAGE(PageTransHuge(page), page); 11307751b2daSKirill A. Shutemov 11311da177e4SLinus Torvalds /* 11321da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 11331da177e4SLinus Torvalds * processes. Try to unmap it here. 11341da177e4SLinus Torvalds */ 11351da177e4SLinus Torvalds if (page_mapped(page) && mapping) { 1136854e9ed0SMinchan Kim switch (ret = try_to_unmap(page, lazyfree ? 1137854e9ed0SMinchan Kim (ttu_flags | TTU_BATCH_FLUSH | TTU_LZFREE) : 1138854e9ed0SMinchan Kim (ttu_flags | TTU_BATCH_FLUSH))) { 11391da177e4SLinus Torvalds case SWAP_FAIL: 11405bccd166SMichal Hocko nr_unmap_fail++; 11411da177e4SLinus Torvalds goto activate_locked; 11421da177e4SLinus Torvalds case SWAP_AGAIN: 11431da177e4SLinus Torvalds goto keep_locked; 1144b291f000SNick Piggin case SWAP_MLOCK: 1145b291f000SNick Piggin goto cull_mlocked; 1146854e9ed0SMinchan Kim case SWAP_LZFREE: 1147854e9ed0SMinchan Kim goto lazyfree; 11481da177e4SLinus Torvalds case SWAP_SUCCESS: 11491da177e4SLinus Torvalds ; /* try to free the page below */ 11501da177e4SLinus Torvalds } 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds if (PageDirty(page)) { 1154ee72886dSMel Gorman /* 1155ee72886dSMel Gorman * Only kswapd can writeback filesystem pages to 1156d43006d5SMel Gorman * avoid risk of stack overflow but only writeback 1157d43006d5SMel Gorman * if many dirty pages have been encountered. 1158ee72886dSMel Gorman */ 1159f84f6e2bSMel Gorman if (page_is_file_cache(page) && 11609e3b2f8cSKonstantin Khlebnikov (!current_is_kswapd() || 1161599d0c95SMel Gorman !test_bit(PGDAT_DIRTY, &pgdat->flags))) { 116249ea7eb6SMel Gorman /* 116349ea7eb6SMel Gorman * Immediately reclaim when written back. 116449ea7eb6SMel Gorman * Similar in principal to deactivate_page() 116549ea7eb6SMel Gorman * except we already have the page isolated 116649ea7eb6SMel Gorman * and know it's dirty 116749ea7eb6SMel Gorman */ 1168c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_IMMEDIATE); 116949ea7eb6SMel Gorman SetPageReclaim(page); 117049ea7eb6SMel Gorman 1171ee72886dSMel Gorman goto keep_locked; 1172ee72886dSMel Gorman } 1173ee72886dSMel Gorman 1174dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 11751da177e4SLinus Torvalds goto keep_locked; 11764dd4b920SAndrew Morton if (!may_enter_fs) 11771da177e4SLinus Torvalds goto keep_locked; 117852a8363eSChristoph Lameter if (!sc->may_writepage) 11791da177e4SLinus Torvalds goto keep_locked; 11801da177e4SLinus Torvalds 1181d950c947SMel Gorman /* 1182d950c947SMel Gorman * Page is dirty. Flush the TLB if a writable entry 1183d950c947SMel Gorman * potentially exists to avoid CPU writes after IO 1184d950c947SMel Gorman * starts and then write it out here. 1185d950c947SMel Gorman */ 1186d950c947SMel Gorman try_to_unmap_flush_dirty(); 11877d3579e8SKOSAKI Motohiro switch (pageout(page, mapping, sc)) { 11881da177e4SLinus Torvalds case PAGE_KEEP: 11891da177e4SLinus Torvalds goto keep_locked; 11901da177e4SLinus Torvalds case PAGE_ACTIVATE: 11911da177e4SLinus Torvalds goto activate_locked; 11921da177e4SLinus Torvalds case PAGE_SUCCESS: 11937d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 119441ac1999SMel Gorman goto keep; 11957d3579e8SKOSAKI Motohiro if (PageDirty(page)) 11961da177e4SLinus Torvalds goto keep; 11977d3579e8SKOSAKI Motohiro 11981da177e4SLinus Torvalds /* 11991da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 12001da177e4SLinus Torvalds * ahead and try to reclaim the page. 12011da177e4SLinus Torvalds */ 1202529ae9aaSNick Piggin if (!trylock_page(page)) 12031da177e4SLinus Torvalds goto keep; 12041da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 12051da177e4SLinus Torvalds goto keep_locked; 12061da177e4SLinus Torvalds mapping = page_mapping(page); 12071da177e4SLinus Torvalds case PAGE_CLEAN: 12081da177e4SLinus Torvalds ; /* try to free the page below */ 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds } 12111da177e4SLinus Torvalds 12121da177e4SLinus Torvalds /* 12131da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 12141da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 12151da177e4SLinus Torvalds * the page as well. 12161da177e4SLinus Torvalds * 12171da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 12181da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 12191da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 12201da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 12211da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 12221da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 12231da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 12241da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 12251da177e4SLinus Torvalds * 12261da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 12271da177e4SLinus Torvalds * the pages which were not successfully invalidated in 12281da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 12291da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 12301da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 12311da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 12321da177e4SLinus Torvalds */ 1233266cf658SDavid Howells if (page_has_private(page)) { 12341da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 12351da177e4SLinus Torvalds goto activate_locked; 1236e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 1237e286781dSNick Piggin unlock_page(page); 1238e286781dSNick Piggin if (put_page_testzero(page)) 12391da177e4SLinus Torvalds goto free_it; 1240e286781dSNick Piggin else { 1241e286781dSNick Piggin /* 1242e286781dSNick Piggin * rare race with speculative reference. 1243e286781dSNick Piggin * the speculative reference will free 1244e286781dSNick Piggin * this page shortly, so we may 1245e286781dSNick Piggin * increment nr_reclaimed here (and 1246e286781dSNick Piggin * leave it off the LRU). 1247e286781dSNick Piggin */ 1248e286781dSNick Piggin nr_reclaimed++; 1249e286781dSNick Piggin continue; 1250e286781dSNick Piggin } 1251e286781dSNick Piggin } 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds 1254854e9ed0SMinchan Kim lazyfree: 1255a528910eSJohannes Weiner if (!mapping || !__remove_mapping(mapping, page, true)) 125649d2e9ccSChristoph Lameter goto keep_locked; 12571da177e4SLinus Torvalds 1258a978d6f5SNick Piggin /* 1259a978d6f5SNick Piggin * At this point, we have no other references and there is 1260a978d6f5SNick Piggin * no way to pick any more up (removed from LRU, removed 1261a978d6f5SNick Piggin * from pagecache). Can use non-atomic bitops now (and 1262a978d6f5SNick Piggin * we obviously don't have to worry about waking up a process 1263a978d6f5SNick Piggin * waiting on the page lock, because there are no references. 1264a978d6f5SNick Piggin */ 126548c935adSKirill A. Shutemov __ClearPageLocked(page); 1266e286781dSNick Piggin free_it: 1267854e9ed0SMinchan Kim if (ret == SWAP_LZFREE) 1268854e9ed0SMinchan Kim count_vm_event(PGLAZYFREED); 1269854e9ed0SMinchan Kim 127005ff5137SAndrew Morton nr_reclaimed++; 1271abe4c3b5SMel Gorman 1272abe4c3b5SMel Gorman /* 1273abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1274abe4c3b5SMel Gorman * appear not as the counts should be low 1275abe4c3b5SMel Gorman */ 1276abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 12771da177e4SLinus Torvalds continue; 12781da177e4SLinus Torvalds 1279b291f000SNick Piggin cull_mlocked: 128063d6c5adSHugh Dickins if (PageSwapCache(page)) 128163d6c5adSHugh Dickins try_to_free_swap(page); 1282b291f000SNick Piggin unlock_page(page); 1283c54839a7SJaewon Kim list_add(&page->lru, &ret_pages); 1284b291f000SNick Piggin continue; 1285b291f000SNick Piggin 12861da177e4SLinus Torvalds activate_locked: 128768a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 12885ccc5abaSVladimir Davydov if (PageSwapCache(page) && mem_cgroup_swap_full(page)) 1289a2c43eedSHugh Dickins try_to_free_swap(page); 1290309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 12911da177e4SLinus Torvalds SetPageActive(page); 12921da177e4SLinus Torvalds pgactivate++; 12931da177e4SLinus Torvalds keep_locked: 12941da177e4SLinus Torvalds unlock_page(page); 12951da177e4SLinus Torvalds keep: 12961da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 1297309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); 12981da177e4SLinus Torvalds } 1299abe4c3b5SMel Gorman 1300747db954SJohannes Weiner mem_cgroup_uncharge_list(&free_pages); 130172b252aeSMel Gorman try_to_unmap_flush(); 1302b745bc85SMel Gorman free_hot_cold_page_list(&free_pages, true); 1303abe4c3b5SMel Gorman 13041da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1305f8891e5eSChristoph Lameter count_vm_events(PGACTIVATE, pgactivate); 13060a31bc97SJohannes Weiner 13073c710c1aSMichal Hocko if (stat) { 13083c710c1aSMichal Hocko stat->nr_dirty = nr_dirty; 13093c710c1aSMichal Hocko stat->nr_congested = nr_congested; 13103c710c1aSMichal Hocko stat->nr_unqueued_dirty = nr_unqueued_dirty; 13113c710c1aSMichal Hocko stat->nr_writeback = nr_writeback; 13123c710c1aSMichal Hocko stat->nr_immediate = nr_immediate; 13135bccd166SMichal Hocko stat->nr_activate = pgactivate; 13145bccd166SMichal Hocko stat->nr_ref_keep = nr_ref_keep; 13155bccd166SMichal Hocko stat->nr_unmap_fail = nr_unmap_fail; 13163c710c1aSMichal Hocko } 131705ff5137SAndrew Morton return nr_reclaimed; 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds 132002c6de8dSMinchan Kim unsigned long reclaim_clean_pages_from_list(struct zone *zone, 132102c6de8dSMinchan Kim struct list_head *page_list) 132202c6de8dSMinchan Kim { 132302c6de8dSMinchan Kim struct scan_control sc = { 132402c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 132502c6de8dSMinchan Kim .priority = DEF_PRIORITY, 132602c6de8dSMinchan Kim .may_unmap = 1, 132702c6de8dSMinchan Kim }; 13283c710c1aSMichal Hocko unsigned long ret; 132902c6de8dSMinchan Kim struct page *page, *next; 133002c6de8dSMinchan Kim LIST_HEAD(clean_pages); 133102c6de8dSMinchan Kim 133202c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 1333117aad1eSRafael Aquini if (page_is_file_cache(page) && !PageDirty(page) && 1334b1123ea6SMinchan Kim !__PageMovable(page)) { 133502c6de8dSMinchan Kim ClearPageActive(page); 133602c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 133702c6de8dSMinchan Kim } 133802c6de8dSMinchan Kim } 133902c6de8dSMinchan Kim 1340599d0c95SMel Gorman ret = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc, 13413c710c1aSMichal Hocko TTU_UNMAP|TTU_IGNORE_ACCESS, NULL, true); 134202c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 1343599d0c95SMel Gorman mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, -ret); 134402c6de8dSMinchan Kim return ret; 134502c6de8dSMinchan Kim } 134602c6de8dSMinchan Kim 13475ad333ebSAndy Whitcroft /* 13485ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 13495ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 13505ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 13515ad333ebSAndy Whitcroft * 13525ad333ebSAndy Whitcroft * page: page to consider 13535ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 13545ad333ebSAndy Whitcroft * 13555ad333ebSAndy Whitcroft * returns 0 on success, -ve errno on failure. 13565ad333ebSAndy Whitcroft */ 1357f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode) 13585ad333ebSAndy Whitcroft { 13595ad333ebSAndy Whitcroft int ret = -EINVAL; 13605ad333ebSAndy Whitcroft 13615ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 13625ad333ebSAndy Whitcroft if (!PageLRU(page)) 13635ad333ebSAndy Whitcroft return ret; 13645ad333ebSAndy Whitcroft 1365e46a2879SMinchan Kim /* Compaction should not handle unevictable pages but CMA can do so */ 1366e46a2879SMinchan Kim if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) 1367894bc310SLee Schermerhorn return ret; 1368894bc310SLee Schermerhorn 13695ad333ebSAndy Whitcroft ret = -EBUSY; 137008e552c6SKAMEZAWA Hiroyuki 1371c8244935SMel Gorman /* 1372c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 1373c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 1374c8244935SMel Gorman * blocking - clean pages for the most part. 1375c8244935SMel Gorman * 1376c8244935SMel Gorman * ISOLATE_CLEAN means that only clean pages should be isolated. This 1377c8244935SMel Gorman * is used by reclaim when it is cannot write to backing storage 1378c8244935SMel Gorman * 1379c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 1380c8244935SMel Gorman * that it is possible to migrate without blocking 1381c8244935SMel Gorman */ 1382c8244935SMel Gorman if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) { 1383c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 1384c8244935SMel Gorman if (PageWriteback(page)) 138539deaf85SMinchan Kim return ret; 138639deaf85SMinchan Kim 1387c8244935SMel Gorman if (PageDirty(page)) { 1388c8244935SMel Gorman struct address_space *mapping; 1389c8244935SMel Gorman 1390c8244935SMel Gorman /* ISOLATE_CLEAN means only clean pages */ 1391c8244935SMel Gorman if (mode & ISOLATE_CLEAN) 1392c8244935SMel Gorman return ret; 1393c8244935SMel Gorman 1394c8244935SMel Gorman /* 1395c8244935SMel Gorman * Only pages without mappings or that have a 1396c8244935SMel Gorman * ->migratepage callback are possible to migrate 1397c8244935SMel Gorman * without blocking 1398c8244935SMel Gorman */ 1399c8244935SMel Gorman mapping = page_mapping(page); 1400c8244935SMel Gorman if (mapping && !mapping->a_ops->migratepage) 1401c8244935SMel Gorman return ret; 1402c8244935SMel Gorman } 1403c8244935SMel Gorman } 1404c8244935SMel Gorman 1405f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 1406f80c0673SMinchan Kim return ret; 1407f80c0673SMinchan Kim 14085ad333ebSAndy Whitcroft if (likely(get_page_unless_zero(page))) { 14095ad333ebSAndy Whitcroft /* 14105ad333ebSAndy Whitcroft * Be careful not to clear PageLRU until after we're 14115ad333ebSAndy Whitcroft * sure the page is not being freed elsewhere -- the 14125ad333ebSAndy Whitcroft * page release code relies on it. 14135ad333ebSAndy Whitcroft */ 14145ad333ebSAndy Whitcroft ClearPageLRU(page); 14155ad333ebSAndy Whitcroft ret = 0; 14165ad333ebSAndy Whitcroft } 14175ad333ebSAndy Whitcroft 14185ad333ebSAndy Whitcroft return ret; 14195ad333ebSAndy Whitcroft } 14205ad333ebSAndy Whitcroft 14217ee36a14SMel Gorman 14227ee36a14SMel Gorman /* 14237ee36a14SMel Gorman * Update LRU sizes after isolating pages. The LRU size updates must 14247ee36a14SMel Gorman * be complete before mem_cgroup_update_lru_size due to a santity check. 14257ee36a14SMel Gorman */ 14267ee36a14SMel Gorman static __always_inline void update_lru_sizes(struct lruvec *lruvec, 1427b4536f0cSMichal Hocko enum lru_list lru, unsigned long *nr_zone_taken) 14287ee36a14SMel Gorman { 14297ee36a14SMel Gorman int zid; 14307ee36a14SMel Gorman 14317ee36a14SMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 14327ee36a14SMel Gorman if (!nr_zone_taken[zid]) 14337ee36a14SMel Gorman continue; 14347ee36a14SMel Gorman 14357ee36a14SMel Gorman __update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 1436b4536f0cSMichal Hocko #ifdef CONFIG_MEMCG 1437b4536f0cSMichal Hocko mem_cgroup_update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 1438b4536f0cSMichal Hocko #endif 14397ee36a14SMel Gorman } 14407ee36a14SMel Gorman 14417ee36a14SMel Gorman } 14427ee36a14SMel Gorman 144349d2e9ccSChristoph Lameter /* 1444a52633d8SMel Gorman * zone_lru_lock is heavily contended. Some of the functions that 14451da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 14461da177e4SLinus Torvalds * and working on them outside the LRU lock. 14471da177e4SLinus Torvalds * 14481da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 14491da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 14501da177e4SLinus Torvalds * 14511da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 14521da177e4SLinus Torvalds * 14531da177e4SLinus Torvalds * @nr_to_scan: The number of pages to look through on the list. 14545dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 14551da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1456f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1457fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 14585ad333ebSAndy Whitcroft * @mode: One of the LRU isolation modes 14593cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 14601da177e4SLinus Torvalds * 14611da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 14621da177e4SLinus Torvalds */ 146369e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 14645dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1465fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 14663cb99451SKonstantin Khlebnikov isolate_mode_t mode, enum lru_list lru) 14671da177e4SLinus Torvalds { 146875b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 146969e05944SAndrew Morton unsigned long nr_taken = 0; 1470599d0c95SMel Gorman unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; 14717cc30fcfSMel Gorman unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; 14721265e3a6SMichal Hocko unsigned long skipped = 0, total_skipped = 0; 1473599d0c95SMel Gorman unsigned long scan, nr_pages; 1474b2e18757SMel Gorman LIST_HEAD(pages_skipped); 14751da177e4SLinus Torvalds 14760b802f10SVladimir Davydov for (scan = 0; scan < nr_to_scan && nr_taken < nr_to_scan && 1477d7f05528SMel Gorman !list_empty(src);) { 14785ad333ebSAndy Whitcroft struct page *page; 14795ad333ebSAndy Whitcroft 14801da177e4SLinus Torvalds page = lru_to_page(src); 14811da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 14821da177e4SLinus Torvalds 1483309381feSSasha Levin VM_BUG_ON_PAGE(!PageLRU(page), page); 14848d438f96SNick Piggin 1485b2e18757SMel Gorman if (page_zonenum(page) > sc->reclaim_idx) { 1486b2e18757SMel Gorman list_move(&page->lru, &pages_skipped); 14877cc30fcfSMel Gorman nr_skipped[page_zonenum(page)]++; 1488b2e18757SMel Gorman continue; 1489b2e18757SMel Gorman } 1490b2e18757SMel Gorman 1491d7f05528SMel Gorman /* 1492d7f05528SMel Gorman * Account for scanned and skipped separetly to avoid the pgdat 1493d7f05528SMel Gorman * being prematurely marked unreclaimable by pgdat_reclaimable. 1494d7f05528SMel Gorman */ 1495d7f05528SMel Gorman scan++; 1496d7f05528SMel Gorman 1497f3fd4a61SKonstantin Khlebnikov switch (__isolate_lru_page(page, mode)) { 14985ad333ebSAndy Whitcroft case 0: 1499599d0c95SMel Gorman nr_pages = hpage_nr_pages(page); 1500599d0c95SMel Gorman nr_taken += nr_pages; 1501599d0c95SMel Gorman nr_zone_taken[page_zonenum(page)] += nr_pages; 15025ad333ebSAndy Whitcroft list_move(&page->lru, dst); 15035ad333ebSAndy Whitcroft break; 15047c8ee9a8SNick Piggin 15055ad333ebSAndy Whitcroft case -EBUSY: 15065ad333ebSAndy Whitcroft /* else it is being freed elsewhere */ 15075ad333ebSAndy Whitcroft list_move(&page->lru, src); 15085ad333ebSAndy Whitcroft continue; 15095ad333ebSAndy Whitcroft 15105ad333ebSAndy Whitcroft default: 15115ad333ebSAndy Whitcroft BUG(); 15125ad333ebSAndy Whitcroft } 15135ad333ebSAndy Whitcroft } 15141da177e4SLinus Torvalds 1515b2e18757SMel Gorman /* 1516b2e18757SMel Gorman * Splice any skipped pages to the start of the LRU list. Note that 1517b2e18757SMel Gorman * this disrupts the LRU order when reclaiming for lower zones but 1518b2e18757SMel Gorman * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX 1519b2e18757SMel Gorman * scanning would soon rescan the same pages to skip and put the 1520b2e18757SMel Gorman * system at risk of premature OOM. 1521b2e18757SMel Gorman */ 15227cc30fcfSMel Gorman if (!list_empty(&pages_skipped)) { 15237cc30fcfSMel Gorman int zid; 15247cc30fcfSMel Gorman 15257cc30fcfSMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 15267cc30fcfSMel Gorman if (!nr_skipped[zid]) 15277cc30fcfSMel Gorman continue; 15287cc30fcfSMel Gorman 15297cc30fcfSMel Gorman __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); 15301265e3a6SMichal Hocko skipped += nr_skipped[zid]; 15317cc30fcfSMel Gorman } 1532d7f05528SMel Gorman 1533d7f05528SMel Gorman /* 1534d7f05528SMel Gorman * Account skipped pages as a partial scan as the pgdat may be 1535d7f05528SMel Gorman * close to unreclaimable. If the LRU list is empty, account 1536d7f05528SMel Gorman * skipped pages as a full scan. 1537d7f05528SMel Gorman */ 15381265e3a6SMichal Hocko total_skipped = list_empty(src) ? skipped : skipped >> 2; 1539d7f05528SMel Gorman 1540d7f05528SMel Gorman list_splice(&pages_skipped, src); 15417cc30fcfSMel Gorman } 15421265e3a6SMichal Hocko *nr_scanned = scan + total_skipped; 15431265e3a6SMichal Hocko trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, 154432b3f297SMichal Hocko scan, skipped, nr_taken, mode, lru); 1545b4536f0cSMichal Hocko update_lru_sizes(lruvec, lru, nr_zone_taken); 15461da177e4SLinus Torvalds return nr_taken; 15471da177e4SLinus Torvalds } 15481da177e4SLinus Torvalds 154962695a84SNick Piggin /** 155062695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 155162695a84SNick Piggin * @page: page to isolate from its LRU list 155262695a84SNick Piggin * 155362695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 155462695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 155562695a84SNick Piggin * 155662695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 155762695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 155862695a84SNick Piggin * 155962695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1560894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1561894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1562894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 156362695a84SNick Piggin * 156462695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 156562695a84SNick Piggin * found will be decremented. 156662695a84SNick Piggin * 156762695a84SNick Piggin * Restrictions: 156862695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 156962695a84SNick Piggin * fundamentnal difference from isolate_lru_pages (which is called 157062695a84SNick Piggin * without a stable reference). 157162695a84SNick Piggin * (2) the lru_lock must not be held. 157262695a84SNick Piggin * (3) interrupts must be enabled. 157362695a84SNick Piggin */ 157462695a84SNick Piggin int isolate_lru_page(struct page *page) 157562695a84SNick Piggin { 157662695a84SNick Piggin int ret = -EBUSY; 157762695a84SNick Piggin 1578309381feSSasha Levin VM_BUG_ON_PAGE(!page_count(page), page); 1579cf2a82eeSKirill A. Shutemov WARN_RATELIMIT(PageTail(page), "trying to isolate tail page"); 15800c917313SKonstantin Khlebnikov 158162695a84SNick Piggin if (PageLRU(page)) { 158262695a84SNick Piggin struct zone *zone = page_zone(page); 1583fa9add64SHugh Dickins struct lruvec *lruvec; 158462695a84SNick Piggin 1585a52633d8SMel Gorman spin_lock_irq(zone_lru_lock(zone)); 1586599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat); 15870c917313SKonstantin Khlebnikov if (PageLRU(page)) { 1588894bc310SLee Schermerhorn int lru = page_lru(page); 15890c917313SKonstantin Khlebnikov get_page(page); 159062695a84SNick Piggin ClearPageLRU(page); 1591fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 1592fa9add64SHugh Dickins ret = 0; 159362695a84SNick Piggin } 1594a52633d8SMel Gorman spin_unlock_irq(zone_lru_lock(zone)); 159562695a84SNick Piggin } 159662695a84SNick Piggin return ret; 159762695a84SNick Piggin } 159862695a84SNick Piggin 15995ad333ebSAndy Whitcroft /* 1600d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 1601d37dd5dcSFengguang Wu * then get resheduled. When there are massive number of tasks doing page 1602d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 1603d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 1604d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 160535cd7815SRik van Riel */ 1606599d0c95SMel Gorman static int too_many_isolated(struct pglist_data *pgdat, int file, 160735cd7815SRik van Riel struct scan_control *sc) 160835cd7815SRik van Riel { 160935cd7815SRik van Riel unsigned long inactive, isolated; 161035cd7815SRik van Riel 161135cd7815SRik van Riel if (current_is_kswapd()) 161235cd7815SRik van Riel return 0; 161335cd7815SRik van Riel 161497c9341fSTejun Heo if (!sane_reclaim(sc)) 161535cd7815SRik van Riel return 0; 161635cd7815SRik van Riel 161735cd7815SRik van Riel if (file) { 1618599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_FILE); 1619599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_FILE); 162035cd7815SRik van Riel } else { 1621599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_ANON); 1622599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_ANON); 162335cd7815SRik van Riel } 162435cd7815SRik van Riel 16253cf23841SFengguang Wu /* 16263cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 16273cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 16283cf23841SFengguang Wu * deadlock. 16293cf23841SFengguang Wu */ 1630d0164adcSMel Gorman if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) 16313cf23841SFengguang Wu inactive >>= 3; 16323cf23841SFengguang Wu 163335cd7815SRik van Riel return isolated > inactive; 163435cd7815SRik van Riel } 163535cd7815SRik van Riel 163666635629SMel Gorman static noinline_for_stack void 163775b00af7SHugh Dickins putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list) 163866635629SMel Gorman { 163927ac81d8SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 1640599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 16413f79768fSHugh Dickins LIST_HEAD(pages_to_free); 164266635629SMel Gorman 164366635629SMel Gorman /* 164466635629SMel Gorman * Put back any unfreeable pages. 164566635629SMel Gorman */ 164666635629SMel Gorman while (!list_empty(page_list)) { 16473f79768fSHugh Dickins struct page *page = lru_to_page(page_list); 164866635629SMel Gorman int lru; 16493f79768fSHugh Dickins 1650309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 165166635629SMel Gorman list_del(&page->lru); 165239b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 1653599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 165466635629SMel Gorman putback_lru_page(page); 1655599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 165666635629SMel Gorman continue; 165766635629SMel Gorman } 1658fa9add64SHugh Dickins 1659599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 1660fa9add64SHugh Dickins 16617a608572SLinus Torvalds SetPageLRU(page); 166266635629SMel Gorman lru = page_lru(page); 1663fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 1664fa9add64SHugh Dickins 166566635629SMel Gorman if (is_active_lru(lru)) { 166666635629SMel Gorman int file = is_file_lru(lru); 16679992af10SRik van Riel int numpages = hpage_nr_pages(page); 16689992af10SRik van Riel reclaim_stat->recent_rotated[file] += numpages; 166966635629SMel Gorman } 16702bcf8879SHugh Dickins if (put_page_testzero(page)) { 16712bcf8879SHugh Dickins __ClearPageLRU(page); 16722bcf8879SHugh Dickins __ClearPageActive(page); 1673fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 16742bcf8879SHugh Dickins 16752bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 1676599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1677747db954SJohannes Weiner mem_cgroup_uncharge(page); 16782bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 1679599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 16802bcf8879SHugh Dickins } else 16812bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 168266635629SMel Gorman } 168366635629SMel Gorman } 168466635629SMel Gorman 16853f79768fSHugh Dickins /* 16863f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 16873f79768fSHugh Dickins */ 16883f79768fSHugh Dickins list_splice(&pages_to_free, page_list); 168966635629SMel Gorman } 169066635629SMel Gorman 169166635629SMel Gorman /* 1692399ba0b9SNeilBrown * If a kernel thread (such as nfsd for loop-back mounts) services 1693399ba0b9SNeilBrown * a backing device by writing to the page cache it sets PF_LESS_THROTTLE. 1694399ba0b9SNeilBrown * In that case we should only throttle if the backing device it is 1695399ba0b9SNeilBrown * writing to is congested. In other cases it is safe to throttle. 1696399ba0b9SNeilBrown */ 1697399ba0b9SNeilBrown static int current_may_throttle(void) 1698399ba0b9SNeilBrown { 1699399ba0b9SNeilBrown return !(current->flags & PF_LESS_THROTTLE) || 1700399ba0b9SNeilBrown current->backing_dev_info == NULL || 1701399ba0b9SNeilBrown bdi_write_congested(current->backing_dev_info); 1702399ba0b9SNeilBrown } 1703399ba0b9SNeilBrown 170491dcade4SMinchan Kim static bool inactive_reclaimable_pages(struct lruvec *lruvec, 170591dcade4SMinchan Kim struct scan_control *sc, enum lru_list lru) 170691dcade4SMinchan Kim { 170791dcade4SMinchan Kim int zid; 170891dcade4SMinchan Kim struct zone *zone; 170991dcade4SMinchan Kim int file = is_file_lru(lru); 171091dcade4SMinchan Kim struct pglist_data *pgdat = lruvec_pgdat(lruvec); 171191dcade4SMinchan Kim 171291dcade4SMinchan Kim if (!global_reclaim(sc)) 171391dcade4SMinchan Kim return true; 171491dcade4SMinchan Kim 171591dcade4SMinchan Kim for (zid = sc->reclaim_idx; zid >= 0; zid--) { 171691dcade4SMinchan Kim zone = &pgdat->node_zones[zid]; 17176aa303deSMel Gorman if (!managed_zone(zone)) 171891dcade4SMinchan Kim continue; 171991dcade4SMinchan Kim 172091dcade4SMinchan Kim if (zone_page_state_snapshot(zone, NR_ZONE_LRU_BASE + 172191dcade4SMinchan Kim LRU_FILE * file) >= SWAP_CLUSTER_MAX) 172291dcade4SMinchan Kim return true; 172391dcade4SMinchan Kim } 172491dcade4SMinchan Kim 172591dcade4SMinchan Kim return false; 172691dcade4SMinchan Kim } 172791dcade4SMinchan Kim 1728399ba0b9SNeilBrown /* 1729b2e18757SMel Gorman * shrink_inactive_list() is a helper for shrink_node(). It returns the number 17301742f19fSAndrew Morton * of reclaimed pages 17311da177e4SLinus Torvalds */ 173266635629SMel Gorman static noinline_for_stack unsigned long 17331a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 17349e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 17351da177e4SLinus Torvalds { 17361da177e4SLinus Torvalds LIST_HEAD(page_list); 1737e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 173805ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 1739e247dbceSKOSAKI Motohiro unsigned long nr_taken; 17403c710c1aSMichal Hocko struct reclaim_stat stat = {}; 1741f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 17423cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 1743599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 17441a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 174578dc583dSKOSAKI Motohiro 174691dcade4SMinchan Kim if (!inactive_reclaimable_pages(lruvec, sc, lru)) 174791dcade4SMinchan Kim return 0; 174891dcade4SMinchan Kim 1749599d0c95SMel Gorman while (unlikely(too_many_isolated(pgdat, file, sc))) { 175058355c78SKOSAKI Motohiro congestion_wait(BLK_RW_ASYNC, HZ/10); 175135cd7815SRik van Riel 175235cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 175335cd7815SRik van Riel if (fatal_signal_pending(current)) 175435cd7815SRik van Riel return SWAP_CLUSTER_MAX; 175535cd7815SRik van Riel } 175635cd7815SRik van Riel 17571da177e4SLinus Torvalds lru_add_drain(); 1758f80c0673SMinchan Kim 1759f80c0673SMinchan Kim if (!sc->may_unmap) 176061317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1761f80c0673SMinchan Kim if (!sc->may_writepage) 176261317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1763f80c0673SMinchan Kim 1764599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 17651da177e4SLinus Torvalds 17665dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 17675dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 176895d918fcSKonstantin Khlebnikov 1769599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 17709d5e6a9fSHugh Dickins reclaim_stat->recent_scanned[file] += nr_taken; 177195d918fcSKonstantin Khlebnikov 177289b5fae5SJohannes Weiner if (global_reclaim(sc)) { 1773599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_PAGES_SCANNED, nr_scanned); 1774b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 1775599d0c95SMel Gorman __count_vm_events(PGSCAN_KSWAPD, nr_scanned); 1776b35ea17bSKOSAKI Motohiro else 1777599d0c95SMel Gorman __count_vm_events(PGSCAN_DIRECT, nr_scanned); 1778b35ea17bSKOSAKI Motohiro } 1779599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1780d563c050SHillf Danton 1781d563c050SHillf Danton if (nr_taken == 0) 178266635629SMel Gorman return 0; 1783b35ea17bSKOSAKI Motohiro 1784599d0c95SMel Gorman nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, TTU_UNMAP, 17853c710c1aSMichal Hocko &stat, false); 1786c661b078SAndy Whitcroft 1787599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 17883f79768fSHugh Dickins 1789904249aaSYing Han if (global_reclaim(sc)) { 1790b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 1791599d0c95SMel Gorman __count_vm_events(PGSTEAL_KSWAPD, nr_reclaimed); 1792904249aaSYing Han else 1793599d0c95SMel Gorman __count_vm_events(PGSTEAL_DIRECT, nr_reclaimed); 1794904249aaSYing Han } 1795a74609faSNick Piggin 179627ac81d8SKonstantin Khlebnikov putback_inactive_pages(lruvec, &page_list); 17973f79768fSHugh Dickins 1798599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 17993f79768fSHugh Dickins 1800599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 18013f79768fSHugh Dickins 1802747db954SJohannes Weiner mem_cgroup_uncharge_list(&page_list); 1803b745bc85SMel Gorman free_hot_cold_page_list(&page_list, true); 1804e11da5b4SMel Gorman 180592df3a72SMel Gorman /* 180692df3a72SMel Gorman * If reclaim is isolating dirty pages under writeback, it implies 180792df3a72SMel Gorman * that the long-lived page allocation rate is exceeding the page 180892df3a72SMel Gorman * laundering rate. Either the global limits are not being effective 180992df3a72SMel Gorman * at throttling processes due to the page distribution throughout 181092df3a72SMel Gorman * zones or there is heavy usage of a slow backing device. The 181192df3a72SMel Gorman * only option is to throttle from reclaim context which is not ideal 181292df3a72SMel Gorman * as there is no guarantee the dirtying process is throttled in the 181392df3a72SMel Gorman * same way balance_dirty_pages() manages. 181492df3a72SMel Gorman * 18158e950282SMel Gorman * Once a zone is flagged ZONE_WRITEBACK, kswapd will count the number 18168e950282SMel Gorman * of pages under pages flagged for immediate reclaim and stall if any 18178e950282SMel Gorman * are encountered in the nr_immediate check below. 181892df3a72SMel Gorman */ 18193c710c1aSMichal Hocko if (stat.nr_writeback && stat.nr_writeback == nr_taken) 1820599d0c95SMel Gorman set_bit(PGDAT_WRITEBACK, &pgdat->flags); 182192df3a72SMel Gorman 1822d43006d5SMel Gorman /* 182397c9341fSTejun Heo * Legacy memcg will stall in page writeback so avoid forcibly 182497c9341fSTejun Heo * stalling here. 1825d43006d5SMel Gorman */ 182697c9341fSTejun Heo if (sane_reclaim(sc)) { 1827b1a6f21eSMel Gorman /* 18288e950282SMel Gorman * Tag a zone as congested if all the dirty pages scanned were 18298e950282SMel Gorman * backed by a congested BDI and wait_iff_congested will stall. 18308e950282SMel Gorman */ 18313c710c1aSMichal Hocko if (stat.nr_dirty && stat.nr_dirty == stat.nr_congested) 1832599d0c95SMel Gorman set_bit(PGDAT_CONGESTED, &pgdat->flags); 18338e950282SMel Gorman 18348e950282SMel Gorman /* 1835b1a6f21eSMel Gorman * If dirty pages are scanned that are not queued for IO, it 1836b1a6f21eSMel Gorman * implies that flushers are not keeping up. In this case, flag 1837599d0c95SMel Gorman * the pgdat PGDAT_DIRTY and kswapd will start writing pages from 183857054651SJohannes Weiner * reclaim context. 1839b1a6f21eSMel Gorman */ 18403c710c1aSMichal Hocko if (stat.nr_unqueued_dirty == nr_taken) 1841599d0c95SMel Gorman set_bit(PGDAT_DIRTY, &pgdat->flags); 1842b1a6f21eSMel Gorman 1843b1a6f21eSMel Gorman /* 1844b738d764SLinus Torvalds * If kswapd scans pages marked marked for immediate 1845b738d764SLinus Torvalds * reclaim and under writeback (nr_immediate), it implies 1846b738d764SLinus Torvalds * that pages are cycling through the LRU faster than 1847b1a6f21eSMel Gorman * they are written so also forcibly stall. 1848b1a6f21eSMel Gorman */ 18493c710c1aSMichal Hocko if (stat.nr_immediate && current_may_throttle()) 1850b1a6f21eSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 1851e2be15f6SMel Gorman } 1852d43006d5SMel Gorman 18538e950282SMel Gorman /* 18548e950282SMel Gorman * Stall direct reclaim for IO completions if underlying BDIs or zone 18558e950282SMel Gorman * is congested. Allow kswapd to continue until it starts encountering 18568e950282SMel Gorman * unqueued dirty pages or cycling through the LRU too quickly. 18578e950282SMel Gorman */ 1858399ba0b9SNeilBrown if (!sc->hibernation_mode && !current_is_kswapd() && 1859399ba0b9SNeilBrown current_may_throttle()) 1860599d0c95SMel Gorman wait_iff_congested(pgdat, BLK_RW_ASYNC, HZ/10); 18618e950282SMel Gorman 1862599d0c95SMel Gorman trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, 1863599d0c95SMel Gorman nr_scanned, nr_reclaimed, 18645bccd166SMichal Hocko stat.nr_dirty, stat.nr_writeback, 18655bccd166SMichal Hocko stat.nr_congested, stat.nr_immediate, 18665bccd166SMichal Hocko stat.nr_activate, stat.nr_ref_keep, 18675bccd166SMichal Hocko stat.nr_unmap_fail, 1868ba5e9579Syalin wang sc->priority, file); 186905ff5137SAndrew Morton return nr_reclaimed; 18701da177e4SLinus Torvalds } 18711da177e4SLinus Torvalds 18723bb1a852SMartin Bligh /* 18731cfb419bSKAMEZAWA Hiroyuki * This moves pages from the active list to the inactive list. 18741cfb419bSKAMEZAWA Hiroyuki * 18751cfb419bSKAMEZAWA Hiroyuki * We move them the other way if the page is referenced by one or more 18761cfb419bSKAMEZAWA Hiroyuki * processes, from rmap. 18771cfb419bSKAMEZAWA Hiroyuki * 18781cfb419bSKAMEZAWA Hiroyuki * If the pages are mostly unmapped, the processing is fast and it is 1879a52633d8SMel Gorman * appropriate to hold zone_lru_lock across the whole operation. But if 18801cfb419bSKAMEZAWA Hiroyuki * the pages are mapped, the processing is slow (page_referenced()) so we 1881a52633d8SMel Gorman * should drop zone_lru_lock around each page. It's impossible to balance 18821cfb419bSKAMEZAWA Hiroyuki * this, so instead we remove the pages from the LRU while processing them. 18831cfb419bSKAMEZAWA Hiroyuki * It is safe to rely on PG_active against the non-LRU pages in here because 18841cfb419bSKAMEZAWA Hiroyuki * nobody will play with that bit on a non-LRU page. 18851cfb419bSKAMEZAWA Hiroyuki * 18860139aa7bSJoonsoo Kim * The downside is that we have to touch page->_refcount against each page. 18871cfb419bSKAMEZAWA Hiroyuki * But we had to alter page->flags anyway. 18889d998b4fSMichal Hocko * 18899d998b4fSMichal Hocko * Returns the number of pages moved to the given lru. 18901cfb419bSKAMEZAWA Hiroyuki */ 18911cfb419bSKAMEZAWA Hiroyuki 18929d998b4fSMichal Hocko static unsigned move_active_pages_to_lru(struct lruvec *lruvec, 18933eb4140fSWu Fengguang struct list_head *list, 18942bcf8879SHugh Dickins struct list_head *pages_to_free, 18953eb4140fSWu Fengguang enum lru_list lru) 18963eb4140fSWu Fengguang { 1897599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 18983eb4140fSWu Fengguang struct page *page; 1899fa9add64SHugh Dickins int nr_pages; 19009d998b4fSMichal Hocko int nr_moved = 0; 19013eb4140fSWu Fengguang 19023eb4140fSWu Fengguang while (!list_empty(list)) { 19033eb4140fSWu Fengguang page = lru_to_page(list); 1904599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 19053eb4140fSWu Fengguang 1906309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 19073eb4140fSWu Fengguang SetPageLRU(page); 19083eb4140fSWu Fengguang 1909fa9add64SHugh Dickins nr_pages = hpage_nr_pages(page); 1910599d0c95SMel Gorman update_lru_size(lruvec, lru, page_zonenum(page), nr_pages); 1911925b7673SJohannes Weiner list_move(&page->lru, &lruvec->lists[lru]); 19123eb4140fSWu Fengguang 19132bcf8879SHugh Dickins if (put_page_testzero(page)) { 19142bcf8879SHugh Dickins __ClearPageLRU(page); 19152bcf8879SHugh Dickins __ClearPageActive(page); 1916fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 19172bcf8879SHugh Dickins 19182bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 1919599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1920747db954SJohannes Weiner mem_cgroup_uncharge(page); 19212bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 1922599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 19232bcf8879SHugh Dickins } else 19242bcf8879SHugh Dickins list_add(&page->lru, pages_to_free); 19259d998b4fSMichal Hocko } else { 19269d998b4fSMichal Hocko nr_moved += nr_pages; 19273eb4140fSWu Fengguang } 19283eb4140fSWu Fengguang } 19299d5e6a9fSHugh Dickins 19303eb4140fSWu Fengguang if (!is_active_lru(lru)) 1931f0958906SMichal Hocko __count_vm_events(PGDEACTIVATE, nr_moved); 19329d998b4fSMichal Hocko 19339d998b4fSMichal Hocko return nr_moved; 19343eb4140fSWu Fengguang } 19351cfb419bSKAMEZAWA Hiroyuki 1936f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 19371a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 1938f16015fbSJohannes Weiner struct scan_control *sc, 19399e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 19401cfb419bSKAMEZAWA Hiroyuki { 194144c241f1SKOSAKI Motohiro unsigned long nr_taken; 1942f626012dSHugh Dickins unsigned long nr_scanned; 19436fe6b7e3SWu Fengguang unsigned long vm_flags; 19441cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 19458cab4754SWu Fengguang LIST_HEAD(l_active); 1946b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 19471cfb419bSKAMEZAWA Hiroyuki struct page *page; 19481a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 19499d998b4fSMichal Hocko unsigned nr_deactivate, nr_activate; 19509d998b4fSMichal Hocko unsigned nr_rotated = 0; 1951f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 19523cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 1953599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 19541cfb419bSKAMEZAWA Hiroyuki 19551da177e4SLinus Torvalds lru_add_drain(); 1956f80c0673SMinchan Kim 1957f80c0673SMinchan Kim if (!sc->may_unmap) 195861317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1959f80c0673SMinchan Kim if (!sc->may_writepage) 196061317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1961f80c0673SMinchan Kim 1962599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 1963925b7673SJohannes Weiner 19645dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 19655dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 196689b5fae5SJohannes Weiner 1967599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 1968b7c46d15SJohannes Weiner reclaim_stat->recent_scanned[file] += nr_taken; 19691cfb419bSKAMEZAWA Hiroyuki 19709d5e6a9fSHugh Dickins if (global_reclaim(sc)) 1971599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_PAGES_SCANNED, nr_scanned); 1972599d0c95SMel Gorman __count_vm_events(PGREFILL, nr_scanned); 19739d5e6a9fSHugh Dickins 1974599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 19771da177e4SLinus Torvalds cond_resched(); 19781da177e4SLinus Torvalds page = lru_to_page(&l_hold); 19791da177e4SLinus Torvalds list_del(&page->lru); 19807e9cd484SRik van Riel 198139b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 1982894bc310SLee Schermerhorn putback_lru_page(page); 1983894bc310SLee Schermerhorn continue; 1984894bc310SLee Schermerhorn } 1985894bc310SLee Schermerhorn 1986cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 1987cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 1988cc715d99SMel Gorman if (page_has_private(page)) 1989cc715d99SMel Gorman try_to_release_page(page, 0); 1990cc715d99SMel Gorman unlock_page(page); 1991cc715d99SMel Gorman } 1992cc715d99SMel Gorman } 1993cc715d99SMel Gorman 1994c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 1995c3ac9a8aSJohannes Weiner &vm_flags)) { 19969992af10SRik van Riel nr_rotated += hpage_nr_pages(page); 19978cab4754SWu Fengguang /* 19988cab4754SWu Fengguang * Identify referenced, file-backed active pages and 19998cab4754SWu Fengguang * give them one more trip around the active list. So 20008cab4754SWu Fengguang * that executable code get better chances to stay in 20018cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 20028cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 20038cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 20048cab4754SWu Fengguang * so we ignore them here. 20058cab4754SWu Fengguang */ 200641e20983SWu Fengguang if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { 20078cab4754SWu Fengguang list_add(&page->lru, &l_active); 20088cab4754SWu Fengguang continue; 20098cab4754SWu Fengguang } 20108cab4754SWu Fengguang } 20117e9cd484SRik van Riel 20125205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 20131da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 20141da177e4SLinus Torvalds } 20151da177e4SLinus Torvalds 2016b555749aSAndrew Morton /* 20178cab4754SWu Fengguang * Move pages back to the lru list. 2018b555749aSAndrew Morton */ 2019599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 20204f98a2feSRik van Riel /* 20218cab4754SWu Fengguang * Count referenced pages from currently used mappings as rotated, 20228cab4754SWu Fengguang * even though only some of them are actually re-activated. This 20238cab4754SWu Fengguang * helps balance scan pressure between file and anonymous pages in 20247c0db9e9SJerome Marchand * get_scan_count. 2025556adecbSRik van Riel */ 2026b7c46d15SJohannes Weiner reclaim_stat->recent_rotated[file] += nr_rotated; 2027556adecbSRik van Riel 20289d998b4fSMichal Hocko nr_activate = move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru); 20299d998b4fSMichal Hocko nr_deactivate = move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE); 2030599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 2031599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 20322bcf8879SHugh Dickins 2033747db954SJohannes Weiner mem_cgroup_uncharge_list(&l_hold); 2034b745bc85SMel Gorman free_hot_cold_page_list(&l_hold, true); 20359d998b4fSMichal Hocko trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate, 20369d998b4fSMichal Hocko nr_deactivate, nr_rotated, sc->priority, file); 20371da177e4SLinus Torvalds } 20381da177e4SLinus Torvalds 203959dc76b0SRik van Riel /* 204059dc76b0SRik van Riel * The inactive anon list should be small enough that the VM never has 204159dc76b0SRik van Riel * to do too much work. 204214797e23SKOSAKI Motohiro * 204359dc76b0SRik van Riel * The inactive file list should be small enough to leave most memory 204459dc76b0SRik van Riel * to the established workingset on the scan-resistant active list, 204559dc76b0SRik van Riel * but large enough to avoid thrashing the aggregate readahead window. 204659dc76b0SRik van Riel * 204759dc76b0SRik van Riel * Both inactive lists should also be large enough that each inactive 204859dc76b0SRik van Riel * page has a chance to be referenced again before it is reclaimed. 204959dc76b0SRik van Riel * 205059dc76b0SRik van Riel * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages 205159dc76b0SRik van Riel * on this LRU, maintained by the pageout code. A zone->inactive_ratio 205259dc76b0SRik van Riel * of 3 means 3:1 or 25% of the pages are kept on the inactive list. 205359dc76b0SRik van Riel * 205459dc76b0SRik van Riel * total target max 205559dc76b0SRik van Riel * memory ratio inactive 205659dc76b0SRik van Riel * ------------------------------------- 205759dc76b0SRik van Riel * 10MB 1 5MB 205859dc76b0SRik van Riel * 100MB 1 50MB 205959dc76b0SRik van Riel * 1GB 3 250MB 206059dc76b0SRik van Riel * 10GB 10 0.9GB 206159dc76b0SRik van Riel * 100GB 31 3GB 206259dc76b0SRik van Riel * 1TB 101 10GB 206359dc76b0SRik van Riel * 10TB 320 32GB 206414797e23SKOSAKI Motohiro */ 2065f8d1a311SMel Gorman static bool inactive_list_is_low(struct lruvec *lruvec, bool file, 2066dcec0b60SMichal Hocko struct scan_control *sc, bool trace) 206714797e23SKOSAKI Motohiro { 206859dc76b0SRik van Riel unsigned long inactive_ratio; 2069fd538803SMichal Hocko unsigned long inactive, active; 2070fd538803SMichal Hocko enum lru_list inactive_lru = file * LRU_FILE; 2071fd538803SMichal Hocko enum lru_list active_lru = file * LRU_FILE + LRU_ACTIVE; 207259dc76b0SRik van Riel unsigned long gb; 207359dc76b0SRik van Riel 207474e3f3c3SMinchan Kim /* 207574e3f3c3SMinchan Kim * If we don't have swap space, anonymous page deactivation 207674e3f3c3SMinchan Kim * is pointless. 207774e3f3c3SMinchan Kim */ 207859dc76b0SRik van Riel if (!file && !total_swap_pages) 207942e2e457SYaowei Bai return false; 208074e3f3c3SMinchan Kim 2081fd538803SMichal Hocko inactive = lruvec_lru_size(lruvec, inactive_lru, sc->reclaim_idx); 2082fd538803SMichal Hocko active = lruvec_lru_size(lruvec, active_lru, sc->reclaim_idx); 2083f8d1a311SMel Gorman 208459dc76b0SRik van Riel gb = (inactive + active) >> (30 - PAGE_SHIFT); 208559dc76b0SRik van Riel if (gb) 208659dc76b0SRik van Riel inactive_ratio = int_sqrt(10 * gb); 2087b39415b2SRik van Riel else 208859dc76b0SRik van Riel inactive_ratio = 1; 208959dc76b0SRik van Riel 2090dcec0b60SMichal Hocko if (trace) 2091fd538803SMichal Hocko trace_mm_vmscan_inactive_list_is_low(lruvec_pgdat(lruvec)->node_id, 2092dcec0b60SMichal Hocko sc->reclaim_idx, 2093fd538803SMichal Hocko lruvec_lru_size(lruvec, inactive_lru, MAX_NR_ZONES), inactive, 2094fd538803SMichal Hocko lruvec_lru_size(lruvec, active_lru, MAX_NR_ZONES), active, 2095fd538803SMichal Hocko inactive_ratio, file); 2096fd538803SMichal Hocko 209759dc76b0SRik van Riel return inactive * inactive_ratio < active; 2098b39415b2SRik van Riel } 2099b39415b2SRik van Riel 21004f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 21011a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, struct scan_control *sc) 2102b69408e8SChristoph Lameter { 2103b39415b2SRik van Riel if (is_active_lru(lru)) { 2104dcec0b60SMichal Hocko if (inactive_list_is_low(lruvec, is_file_lru(lru), sc, true)) 21051a93be0eSKonstantin Khlebnikov shrink_active_list(nr_to_scan, lruvec, sc, lru); 2106556adecbSRik van Riel return 0; 2107556adecbSRik van Riel } 2108556adecbSRik van Riel 21091a93be0eSKonstantin Khlebnikov return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 2110b69408e8SChristoph Lameter } 2111b69408e8SChristoph Lameter 21129a265114SJohannes Weiner enum scan_balance { 21139a265114SJohannes Weiner SCAN_EQUAL, 21149a265114SJohannes Weiner SCAN_FRACT, 21159a265114SJohannes Weiner SCAN_ANON, 21169a265114SJohannes Weiner SCAN_FILE, 21179a265114SJohannes Weiner }; 21189a265114SJohannes Weiner 21191da177e4SLinus Torvalds /* 21204f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 21214f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 21224f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 21234f98a2feSRik van Riel * onto the active list instead of evict. 21244f98a2feSRik van Riel * 2125be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 2126be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 21274f98a2feSRik van Riel */ 212833377678SVladimir Davydov static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, 21296b4f7799SJohannes Weiner struct scan_control *sc, unsigned long *nr, 21306b4f7799SJohannes Weiner unsigned long *lru_pages) 21314f98a2feSRik van Riel { 213233377678SVladimir Davydov int swappiness = mem_cgroup_swappiness(memcg); 213390126375SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 21349a265114SJohannes Weiner u64 fraction[2]; 21359a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 2136599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 21379a265114SJohannes Weiner unsigned long anon_prio, file_prio; 21389a265114SJohannes Weiner enum scan_balance scan_balance; 21390bf1457fSJohannes Weiner unsigned long anon, file; 21409a265114SJohannes Weiner bool force_scan = false; 21419a265114SJohannes Weiner unsigned long ap, fp; 21429a265114SJohannes Weiner enum lru_list lru; 21436f04f48dSSuleiman Souhlal bool some_scanned; 21446f04f48dSSuleiman Souhlal int pass; 2145246e87a9SKAMEZAWA Hiroyuki 2146f11c0ca5SJohannes Weiner /* 2147f11c0ca5SJohannes Weiner * If the zone or memcg is small, nr[l] can be 0. This 2148f11c0ca5SJohannes Weiner * results in no scanning on this priority and a potential 2149f11c0ca5SJohannes Weiner * priority drop. Global direct reclaim can go to the next 2150f11c0ca5SJohannes Weiner * zone and tends to have no problems. Global kswapd is for 2151f11c0ca5SJohannes Weiner * zone balancing and it needs to scan a minimum amount. When 2152f11c0ca5SJohannes Weiner * reclaiming for a memcg, a priority drop can cause high 2153f11c0ca5SJohannes Weiner * latencies, so it's better to scan a minimum amount there as 2154f11c0ca5SJohannes Weiner * well. 2155f11c0ca5SJohannes Weiner */ 215690cbc250SVladimir Davydov if (current_is_kswapd()) { 2157599d0c95SMel Gorman if (!pgdat_reclaimable(pgdat)) 2158a4d3e9e7SJohannes Weiner force_scan = true; 2159eb01aaabSVladimir Davydov if (!mem_cgroup_online(memcg)) 216090cbc250SVladimir Davydov force_scan = true; 216190cbc250SVladimir Davydov } 216289b5fae5SJohannes Weiner if (!global_reclaim(sc)) 2163a4d3e9e7SJohannes Weiner force_scan = true; 216476a33fc3SShaohua Li 216576a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 2166d8b38438SVladimir Davydov if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) { 21679a265114SJohannes Weiner scan_balance = SCAN_FILE; 216876a33fc3SShaohua Li goto out; 216976a33fc3SShaohua Li } 21704f98a2feSRik van Riel 217110316b31SJohannes Weiner /* 217210316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 217310316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 217410316b31SJohannes Weiner * disable swapping for individual groups completely when 217510316b31SJohannes Weiner * using the memory controller's swap limit feature would be 217610316b31SJohannes Weiner * too expensive. 217710316b31SJohannes Weiner */ 217802695175SJohannes Weiner if (!global_reclaim(sc) && !swappiness) { 21799a265114SJohannes Weiner scan_balance = SCAN_FILE; 218010316b31SJohannes Weiner goto out; 218110316b31SJohannes Weiner } 218210316b31SJohannes Weiner 218310316b31SJohannes Weiner /* 218410316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 218510316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 218610316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 218710316b31SJohannes Weiner */ 218802695175SJohannes Weiner if (!sc->priority && swappiness) { 21899a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 219010316b31SJohannes Weiner goto out; 219110316b31SJohannes Weiner } 219210316b31SJohannes Weiner 219311d16c25SJohannes Weiner /* 219462376251SJohannes Weiner * Prevent the reclaimer from falling into the cache trap: as 219562376251SJohannes Weiner * cache pages start out inactive, every cache fault will tip 219662376251SJohannes Weiner * the scan balance towards the file LRU. And as the file LRU 219762376251SJohannes Weiner * shrinks, so does the window for rotation from references. 219862376251SJohannes Weiner * This means we have a runaway feedback loop where a tiny 219962376251SJohannes Weiner * thrashing file LRU becomes infinitely more attractive than 220062376251SJohannes Weiner * anon pages. Try to detect this based on file LRU size. 220162376251SJohannes Weiner */ 220262376251SJohannes Weiner if (global_reclaim(sc)) { 2203599d0c95SMel Gorman unsigned long pgdatfile; 2204599d0c95SMel Gorman unsigned long pgdatfree; 2205599d0c95SMel Gorman int z; 2206599d0c95SMel Gorman unsigned long total_high_wmark = 0; 220762376251SJohannes Weiner 2208599d0c95SMel Gorman pgdatfree = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); 2209599d0c95SMel Gorman pgdatfile = node_page_state(pgdat, NR_ACTIVE_FILE) + 2210599d0c95SMel Gorman node_page_state(pgdat, NR_INACTIVE_FILE); 22112ab051e1SJerome Marchand 2212599d0c95SMel Gorman for (z = 0; z < MAX_NR_ZONES; z++) { 2213599d0c95SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 22146aa303deSMel Gorman if (!managed_zone(zone)) 2215599d0c95SMel Gorman continue; 2216599d0c95SMel Gorman 2217599d0c95SMel Gorman total_high_wmark += high_wmark_pages(zone); 2218599d0c95SMel Gorman } 2219599d0c95SMel Gorman 2220599d0c95SMel Gorman if (unlikely(pgdatfile + pgdatfree <= total_high_wmark)) { 222162376251SJohannes Weiner scan_balance = SCAN_ANON; 222262376251SJohannes Weiner goto out; 222362376251SJohannes Weiner } 222462376251SJohannes Weiner } 222562376251SJohannes Weiner 222662376251SJohannes Weiner /* 2227316bda0eSVladimir Davydov * If there is enough inactive page cache, i.e. if the size of the 2228316bda0eSVladimir Davydov * inactive list is greater than that of the active list *and* the 2229316bda0eSVladimir Davydov * inactive list actually has some pages to scan on this priority, we 2230316bda0eSVladimir Davydov * do not reclaim anything from the anonymous working set right now. 2231316bda0eSVladimir Davydov * Without the second condition we could end up never scanning an 2232316bda0eSVladimir Davydov * lruvec even if it has plenty of old anonymous pages unless the 2233316bda0eSVladimir Davydov * system is under heavy pressure. 2234e9868505SRik van Riel */ 2235dcec0b60SMichal Hocko if (!inactive_list_is_low(lruvec, true, sc, false) && 2236*71ab6cfeSMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, sc->reclaim_idx) >> sc->priority) { 22379a265114SJohannes Weiner scan_balance = SCAN_FILE; 2238e9868505SRik van Riel goto out; 22394f98a2feSRik van Riel } 22404f98a2feSRik van Riel 22419a265114SJohannes Weiner scan_balance = SCAN_FRACT; 22429a265114SJohannes Weiner 22434f98a2feSRik van Riel /* 224458c37f6eSKOSAKI Motohiro * With swappiness at 100, anonymous and file have the same priority. 224558c37f6eSKOSAKI Motohiro * This scanning priority is essentially the inverse of IO cost. 224658c37f6eSKOSAKI Motohiro */ 224702695175SJohannes Weiner anon_prio = swappiness; 224875b00af7SHugh Dickins file_prio = 200 - anon_prio; 224958c37f6eSKOSAKI Motohiro 225058c37f6eSKOSAKI Motohiro /* 22514f98a2feSRik van Riel * OK, so we have swap space and a fair amount of page cache 22524f98a2feSRik van Riel * pages. We use the recently rotated / recently scanned 22534f98a2feSRik van Riel * ratios to determine how valuable each cache is. 22544f98a2feSRik van Riel * 22554f98a2feSRik van Riel * Because workloads change over time (and to avoid overflow) 22564f98a2feSRik van Riel * we keep these statistics as a floating average, which ends 22574f98a2feSRik van Riel * up weighing recent references more than old ones. 22584f98a2feSRik van Riel * 22594f98a2feSRik van Riel * anon in [0], file in [1] 22604f98a2feSRik van Riel */ 22612ab051e1SJerome Marchand 2262fd538803SMichal Hocko anon = lruvec_lru_size(lruvec, LRU_ACTIVE_ANON, MAX_NR_ZONES) + 2263fd538803SMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, MAX_NR_ZONES); 2264fd538803SMichal Hocko file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES) + 2265fd538803SMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, MAX_NR_ZONES); 22662ab051e1SJerome Marchand 2267599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 226858c37f6eSKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { 22696e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[0] /= 2; 22706e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[0] /= 2; 22714f98a2feSRik van Riel } 22724f98a2feSRik van Riel 22736e901571SKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { 22746e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[1] /= 2; 22756e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[1] /= 2; 22764f98a2feSRik van Riel } 22774f98a2feSRik van Riel 22784f98a2feSRik van Riel /* 227900d8089cSRik van Riel * The amount of pressure on anon vs file pages is inversely 228000d8089cSRik van Riel * proportional to the fraction of recently scanned pages on 228100d8089cSRik van Riel * each list that were recently referenced and in active use. 22824f98a2feSRik van Riel */ 2283fe35004fSSatoru Moriya ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1); 22846e901571SKOSAKI Motohiro ap /= reclaim_stat->recent_rotated[0] + 1; 22854f98a2feSRik van Riel 2286fe35004fSSatoru Moriya fp = file_prio * (reclaim_stat->recent_scanned[1] + 1); 22876e901571SKOSAKI Motohiro fp /= reclaim_stat->recent_rotated[1] + 1; 2288599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 22894f98a2feSRik van Riel 229076a33fc3SShaohua Li fraction[0] = ap; 229176a33fc3SShaohua Li fraction[1] = fp; 229276a33fc3SShaohua Li denominator = ap + fp + 1; 229376a33fc3SShaohua Li out: 22946f04f48dSSuleiman Souhlal some_scanned = false; 22956f04f48dSSuleiman Souhlal /* Only use force_scan on second pass. */ 22966f04f48dSSuleiman Souhlal for (pass = 0; !some_scanned && pass < 2; pass++) { 22976b4f7799SJohannes Weiner *lru_pages = 0; 22984111304dSHugh Dickins for_each_evictable_lru(lru) { 22994111304dSHugh Dickins int file = is_file_lru(lru); 2300d778df51SJohannes Weiner unsigned long size; 230176a33fc3SShaohua Li unsigned long scan; 230276a33fc3SShaohua Li 2303*71ab6cfeSMichal Hocko size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); 2304d778df51SJohannes Weiner scan = size >> sc->priority; 23059a265114SJohannes Weiner 23066f04f48dSSuleiman Souhlal if (!scan && pass && force_scan) 2307d778df51SJohannes Weiner scan = min(size, SWAP_CLUSTER_MAX); 23089a265114SJohannes Weiner 23099a265114SJohannes Weiner switch (scan_balance) { 23109a265114SJohannes Weiner case SCAN_EQUAL: 23119a265114SJohannes Weiner /* Scan lists relative to size */ 23129a265114SJohannes Weiner break; 23139a265114SJohannes Weiner case SCAN_FRACT: 23149a265114SJohannes Weiner /* 23159a265114SJohannes Weiner * Scan types proportional to swappiness and 23169a265114SJohannes Weiner * their relative recent reclaim efficiency. 23179a265114SJohannes Weiner */ 23186f04f48dSSuleiman Souhlal scan = div64_u64(scan * fraction[file], 23196f04f48dSSuleiman Souhlal denominator); 23209a265114SJohannes Weiner break; 23219a265114SJohannes Weiner case SCAN_FILE: 23229a265114SJohannes Weiner case SCAN_ANON: 23239a265114SJohannes Weiner /* Scan one type exclusively */ 23246b4f7799SJohannes Weiner if ((scan_balance == SCAN_FILE) != file) { 23256b4f7799SJohannes Weiner size = 0; 23269a265114SJohannes Weiner scan = 0; 23276b4f7799SJohannes Weiner } 23289a265114SJohannes Weiner break; 23299a265114SJohannes Weiner default: 23309a265114SJohannes Weiner /* Look ma, no brain */ 23319a265114SJohannes Weiner BUG(); 23329a265114SJohannes Weiner } 23336b4f7799SJohannes Weiner 23346b4f7799SJohannes Weiner *lru_pages += size; 23354111304dSHugh Dickins nr[lru] = scan; 23366b4f7799SJohannes Weiner 23376f04f48dSSuleiman Souhlal /* 23386f04f48dSSuleiman Souhlal * Skip the second pass and don't force_scan, 23396f04f48dSSuleiman Souhlal * if we found something to scan. 23406f04f48dSSuleiman Souhlal */ 23416f04f48dSSuleiman Souhlal some_scanned |= !!scan; 23426f04f48dSSuleiman Souhlal } 234376a33fc3SShaohua Li } 23446e08a369SWu Fengguang } 23454f98a2feSRik van Riel 23469b4f98cdSJohannes Weiner /* 2347a9dd0a83SMel Gorman * This is a basic per-node page freer. Used by both kswapd and direct reclaim. 23489b4f98cdSJohannes Weiner */ 2349a9dd0a83SMel Gorman static void shrink_node_memcg(struct pglist_data *pgdat, struct mem_cgroup *memcg, 23506b4f7799SJohannes Weiner struct scan_control *sc, unsigned long *lru_pages) 23519b4f98cdSJohannes Weiner { 2352ef8f2327SMel Gorman struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg); 23539b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 2354e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 23559b4f98cdSJohannes Weiner unsigned long nr_to_scan; 23569b4f98cdSJohannes Weiner enum lru_list lru; 23579b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 23589b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 23599b4f98cdSJohannes Weiner struct blk_plug plug; 23601a501907SMel Gorman bool scan_adjusted; 23619b4f98cdSJohannes Weiner 236233377678SVladimir Davydov get_scan_count(lruvec, memcg, sc, nr, lru_pages); 23639b4f98cdSJohannes Weiner 2364e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 2365e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 2366e82e0561SMel Gorman 23671a501907SMel Gorman /* 23681a501907SMel Gorman * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal 23691a501907SMel Gorman * event that can occur when there is little memory pressure e.g. 23701a501907SMel Gorman * multiple streaming readers/writers. Hence, we do not abort scanning 23711a501907SMel Gorman * when the requested number of pages are reclaimed when scanning at 23721a501907SMel Gorman * DEF_PRIORITY on the assumption that the fact we are direct 23731a501907SMel Gorman * reclaiming implies that kswapd is not keeping up and it is best to 23741a501907SMel Gorman * do a batch of work at once. For memcg reclaim one check is made to 23751a501907SMel Gorman * abort proportional reclaim if either the file or anon lru has already 23761a501907SMel Gorman * dropped to zero at the first pass. 23771a501907SMel Gorman */ 23781a501907SMel Gorman scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() && 23791a501907SMel Gorman sc->priority == DEF_PRIORITY); 23801a501907SMel Gorman 23819b4f98cdSJohannes Weiner blk_start_plug(&plug); 23829b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 23839b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 2384e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 2385e82e0561SMel Gorman unsigned long nr_scanned; 2386e82e0561SMel Gorman 23879b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 23889b4f98cdSJohannes Weiner if (nr[lru]) { 23899b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 23909b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 23919b4f98cdSJohannes Weiner 23929b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 23939b4f98cdSJohannes Weiner lruvec, sc); 23949b4f98cdSJohannes Weiner } 23959b4f98cdSJohannes Weiner } 2396e82e0561SMel Gorman 2397bd041733SMichal Hocko cond_resched(); 2398bd041733SMichal Hocko 2399e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 2400e82e0561SMel Gorman continue; 2401e82e0561SMel Gorman 24029b4f98cdSJohannes Weiner /* 2403e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 24041a501907SMel Gorman * requested. Ensure that the anon and file LRUs are scanned 2405e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 2406e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 2407e82e0561SMel Gorman * proportional to the original scan target. 2408e82e0561SMel Gorman */ 2409e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 2410e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 2411e82e0561SMel Gorman 24121a501907SMel Gorman /* 24131a501907SMel Gorman * It's just vindictive to attack the larger once the smaller 24141a501907SMel Gorman * has gone to zero. And given the way we stop scanning the 24151a501907SMel Gorman * smaller below, this makes sure that we only make one nudge 24161a501907SMel Gorman * towards proportionality once we've got nr_to_reclaim. 24171a501907SMel Gorman */ 24181a501907SMel Gorman if (!nr_file || !nr_anon) 24191a501907SMel Gorman break; 24201a501907SMel Gorman 2421e82e0561SMel Gorman if (nr_file > nr_anon) { 2422e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 2423e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 2424e82e0561SMel Gorman lru = LRU_BASE; 2425e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 2426e82e0561SMel Gorman } else { 2427e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 2428e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 2429e82e0561SMel Gorman lru = LRU_FILE; 2430e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 2431e82e0561SMel Gorman } 2432e82e0561SMel Gorman 2433e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 2434e82e0561SMel Gorman nr[lru] = 0; 2435e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 2436e82e0561SMel Gorman 2437e82e0561SMel Gorman /* 2438e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 2439e82e0561SMel Gorman * scan target and the percentage scanning already complete 2440e82e0561SMel Gorman */ 2441e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 2442e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2443e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2444e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2445e82e0561SMel Gorman 2446e82e0561SMel Gorman lru += LRU_ACTIVE; 2447e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2448e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2449e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2450e82e0561SMel Gorman 2451e82e0561SMel Gorman scan_adjusted = true; 24529b4f98cdSJohannes Weiner } 24539b4f98cdSJohannes Weiner blk_finish_plug(&plug); 24549b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 24559b4f98cdSJohannes Weiner 24569b4f98cdSJohannes Weiner /* 24579b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 24589b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 24599b4f98cdSJohannes Weiner */ 2460dcec0b60SMichal Hocko if (inactive_list_is_low(lruvec, false, sc, true)) 24619b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 24629b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 24639b4f98cdSJohannes Weiner } 24649b4f98cdSJohannes Weiner 246523b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 24669e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 246723b9da55SMel Gorman { 2468d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 246923b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 24709e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 247123b9da55SMel Gorman return true; 247223b9da55SMel Gorman 247323b9da55SMel Gorman return false; 247423b9da55SMel Gorman } 247523b9da55SMel Gorman 24764f98a2feSRik van Riel /* 247723b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 247823b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 247923b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 248023b9da55SMel Gorman * calls try_to_compact_zone() that it will have enough free pages to succeed. 248123b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 24823e7d3449SMel Gorman */ 2483a9dd0a83SMel Gorman static inline bool should_continue_reclaim(struct pglist_data *pgdat, 24843e7d3449SMel Gorman unsigned long nr_reclaimed, 24853e7d3449SMel Gorman unsigned long nr_scanned, 24863e7d3449SMel Gorman struct scan_control *sc) 24873e7d3449SMel Gorman { 24883e7d3449SMel Gorman unsigned long pages_for_compaction; 24893e7d3449SMel Gorman unsigned long inactive_lru_pages; 2490a9dd0a83SMel Gorman int z; 24913e7d3449SMel Gorman 24923e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 24939e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 24943e7d3449SMel Gorman return false; 24953e7d3449SMel Gorman 24962876592fSMel Gorman /* Consider stopping depending on scan and reclaim activity */ 24972876592fSMel Gorman if (sc->gfp_mask & __GFP_REPEAT) { 24983e7d3449SMel Gorman /* 24992876592fSMel Gorman * For __GFP_REPEAT allocations, stop reclaiming if the 25002876592fSMel Gorman * full LRU list has been scanned and we are still failing 25012876592fSMel Gorman * to reclaim pages. This full LRU scan is potentially 25022876592fSMel Gorman * expensive but a __GFP_REPEAT caller really wants to succeed 25033e7d3449SMel Gorman */ 25043e7d3449SMel Gorman if (!nr_reclaimed && !nr_scanned) 25053e7d3449SMel Gorman return false; 25062876592fSMel Gorman } else { 25072876592fSMel Gorman /* 25082876592fSMel Gorman * For non-__GFP_REPEAT allocations which can presumably 25092876592fSMel Gorman * fail without consequence, stop if we failed to reclaim 25102876592fSMel Gorman * any pages from the last SWAP_CLUSTER_MAX number of 25112876592fSMel Gorman * pages that were scanned. This will return to the 25122876592fSMel Gorman * caller faster at the risk reclaim/compaction and 25132876592fSMel Gorman * the resulting allocation attempt fails 25142876592fSMel Gorman */ 25152876592fSMel Gorman if (!nr_reclaimed) 25162876592fSMel Gorman return false; 25172876592fSMel Gorman } 25183e7d3449SMel Gorman 25193e7d3449SMel Gorman /* 25203e7d3449SMel Gorman * If we have not reclaimed enough pages for compaction and the 25213e7d3449SMel Gorman * inactive lists are large enough, continue reclaiming 25223e7d3449SMel Gorman */ 25239861a62cSVlastimil Babka pages_for_compaction = compact_gap(sc->order); 2524a9dd0a83SMel Gorman inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); 2525ec8acf20SShaohua Li if (get_nr_swap_pages() > 0) 2526a9dd0a83SMel Gorman inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); 25273e7d3449SMel Gorman if (sc->nr_reclaimed < pages_for_compaction && 25283e7d3449SMel Gorman inactive_lru_pages > pages_for_compaction) 25293e7d3449SMel Gorman return true; 25303e7d3449SMel Gorman 25313e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 2532a9dd0a83SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 2533a9dd0a83SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 25346aa303deSMel Gorman if (!managed_zone(zone)) 2535a9dd0a83SMel Gorman continue; 2536a9dd0a83SMel Gorman 2537a9dd0a83SMel Gorman switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) { 2538cf378319SVlastimil Babka case COMPACT_SUCCESS: 25393e7d3449SMel Gorman case COMPACT_CONTINUE: 25403e7d3449SMel Gorman return false; 25413e7d3449SMel Gorman default: 2542a9dd0a83SMel Gorman /* check next zone */ 2543a9dd0a83SMel Gorman ; 25443e7d3449SMel Gorman } 25453e7d3449SMel Gorman } 2546a9dd0a83SMel Gorman return true; 2547a9dd0a83SMel Gorman } 25483e7d3449SMel Gorman 2549970a39a3SMel Gorman static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc) 2550f16015fbSJohannes Weiner { 2551cb731d6cSVladimir Davydov struct reclaim_state *reclaim_state = current->reclaim_state; 25529b4f98cdSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 25532344d7e4SJohannes Weiner bool reclaimable = false; 25549b4f98cdSJohannes Weiner 25559b4f98cdSJohannes Weiner do { 25565660048cSJohannes Weiner struct mem_cgroup *root = sc->target_mem_cgroup; 25575660048cSJohannes Weiner struct mem_cgroup_reclaim_cookie reclaim = { 2558ef8f2327SMel Gorman .pgdat = pgdat, 25599e3b2f8cSKonstantin Khlebnikov .priority = sc->priority, 25605660048cSJohannes Weiner }; 2561a9dd0a83SMel Gorman unsigned long node_lru_pages = 0; 2562694fbc0fSAndrew Morton struct mem_cgroup *memcg; 25635660048cSJohannes Weiner 25649b4f98cdSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 25659b4f98cdSJohannes Weiner nr_scanned = sc->nr_scanned; 25669b4f98cdSJohannes Weiner 2567694fbc0fSAndrew Morton memcg = mem_cgroup_iter(root, NULL, &reclaim); 2568694fbc0fSAndrew Morton do { 25696b4f7799SJohannes Weiner unsigned long lru_pages; 25708e8ae645SJohannes Weiner unsigned long reclaimed; 2571cb731d6cSVladimir Davydov unsigned long scanned; 25729b4f98cdSJohannes Weiner 2573241994edSJohannes Weiner if (mem_cgroup_low(root, memcg)) { 2574241994edSJohannes Weiner if (!sc->may_thrash) 2575241994edSJohannes Weiner continue; 2576241994edSJohannes Weiner mem_cgroup_events(memcg, MEMCG_LOW, 1); 2577241994edSJohannes Weiner } 2578241994edSJohannes Weiner 25798e8ae645SJohannes Weiner reclaimed = sc->nr_reclaimed; 2580cb731d6cSVladimir Davydov scanned = sc->nr_scanned; 25815660048cSJohannes Weiner 2582a9dd0a83SMel Gorman shrink_node_memcg(pgdat, memcg, sc, &lru_pages); 2583a9dd0a83SMel Gorman node_lru_pages += lru_pages; 2584f9be23d6SKonstantin Khlebnikov 2585b5afba29SVladimir Davydov if (memcg) 2586a9dd0a83SMel Gorman shrink_slab(sc->gfp_mask, pgdat->node_id, 2587cb731d6cSVladimir Davydov memcg, sc->nr_scanned - scanned, 2588cb731d6cSVladimir Davydov lru_pages); 2589cb731d6cSVladimir Davydov 25908e8ae645SJohannes Weiner /* Record the group's reclaim efficiency */ 25918e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, memcg, false, 25928e8ae645SJohannes Weiner sc->nr_scanned - scanned, 25938e8ae645SJohannes Weiner sc->nr_reclaimed - reclaimed); 25948e8ae645SJohannes Weiner 25955660048cSJohannes Weiner /* 2596a394cb8eSMichal Hocko * Direct reclaim and kswapd have to scan all memory 2597a394cb8eSMichal Hocko * cgroups to fulfill the overall scan target for the 2598a9dd0a83SMel Gorman * node. 2599a394cb8eSMichal Hocko * 2600a394cb8eSMichal Hocko * Limit reclaim, on the other hand, only cares about 2601a394cb8eSMichal Hocko * nr_to_reclaim pages to be reclaimed and it will 2602a394cb8eSMichal Hocko * retry with decreasing priority if one round over the 2603a394cb8eSMichal Hocko * whole hierarchy is not sufficient. 26045660048cSJohannes Weiner */ 2605a394cb8eSMichal Hocko if (!global_reclaim(sc) && 2606a394cb8eSMichal Hocko sc->nr_reclaimed >= sc->nr_to_reclaim) { 26075660048cSJohannes Weiner mem_cgroup_iter_break(root, memcg); 26085660048cSJohannes Weiner break; 26095660048cSJohannes Weiner } 2610241994edSJohannes Weiner } while ((memcg = mem_cgroup_iter(root, memcg, &reclaim))); 261170ddf637SAnton Vorontsov 26126b4f7799SJohannes Weiner /* 26136b4f7799SJohannes Weiner * Shrink the slab caches in the same proportion that 26146b4f7799SJohannes Weiner * the eligible LRU pages were scanned. 26156b4f7799SJohannes Weiner */ 2616b2e18757SMel Gorman if (global_reclaim(sc)) 2617a9dd0a83SMel Gorman shrink_slab(sc->gfp_mask, pgdat->node_id, NULL, 26186b4f7799SJohannes Weiner sc->nr_scanned - nr_scanned, 2619a9dd0a83SMel Gorman node_lru_pages); 26206b4f7799SJohannes Weiner 26216b4f7799SJohannes Weiner if (reclaim_state) { 2622cb731d6cSVladimir Davydov sc->nr_reclaimed += reclaim_state->reclaimed_slab; 26236b4f7799SJohannes Weiner reclaim_state->reclaimed_slab = 0; 26246b4f7799SJohannes Weiner } 26256b4f7799SJohannes Weiner 26268e8ae645SJohannes Weiner /* Record the subtree's reclaim efficiency */ 26278e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, 262870ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 262970ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 263070ddf637SAnton Vorontsov 26312344d7e4SJohannes Weiner if (sc->nr_reclaimed - nr_reclaimed) 26322344d7e4SJohannes Weiner reclaimable = true; 26332344d7e4SJohannes Weiner 2634a9dd0a83SMel Gorman } while (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, 26359b4f98cdSJohannes Weiner sc->nr_scanned - nr_scanned, sc)); 26362344d7e4SJohannes Weiner 26372344d7e4SJohannes Weiner return reclaimable; 2638f16015fbSJohannes Weiner } 2639f16015fbSJohannes Weiner 264053853e2dSVlastimil Babka /* 2641fdd4c614SVlastimil Babka * Returns true if compaction should go ahead for a costly-order request, or 2642fdd4c614SVlastimil Babka * the allocation would already succeed without compaction. Return false if we 2643fdd4c614SVlastimil Babka * should reclaim first. 264453853e2dSVlastimil Babka */ 26454f588331SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 2646fe4b1b24SMel Gorman { 264731483b6aSMel Gorman unsigned long watermark; 2648fdd4c614SVlastimil Babka enum compact_result suitable; 2649fe4b1b24SMel Gorman 2650fdd4c614SVlastimil Babka suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx); 2651fdd4c614SVlastimil Babka if (suitable == COMPACT_SUCCESS) 2652fdd4c614SVlastimil Babka /* Allocation should succeed already. Don't reclaim. */ 2653fdd4c614SVlastimil Babka return true; 2654fdd4c614SVlastimil Babka if (suitable == COMPACT_SKIPPED) 2655fdd4c614SVlastimil Babka /* Compaction cannot yet proceed. Do reclaim. */ 2656fe4b1b24SMel Gorman return false; 2657fe4b1b24SMel Gorman 2658fdd4c614SVlastimil Babka /* 2659fdd4c614SVlastimil Babka * Compaction is already possible, but it takes time to run and there 2660fdd4c614SVlastimil Babka * are potentially other callers using the pages just freed. So proceed 2661fdd4c614SVlastimil Babka * with reclaim to make a buffer of free pages available to give 2662fdd4c614SVlastimil Babka * compaction a reasonable chance of completing and allocating the page. 2663fdd4c614SVlastimil Babka * Note that we won't actually reclaim the whole buffer in one attempt 2664fdd4c614SVlastimil Babka * as the target watermark in should_continue_reclaim() is lower. But if 2665fdd4c614SVlastimil Babka * we are already above the high+gap watermark, don't reclaim at all. 2666fdd4c614SVlastimil Babka */ 2667fdd4c614SVlastimil Babka watermark = high_wmark_pages(zone) + compact_gap(sc->order); 2668fdd4c614SVlastimil Babka 2669fdd4c614SVlastimil Babka return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); 2670fe4b1b24SMel Gorman } 2671fe4b1b24SMel Gorman 26721da177e4SLinus Torvalds /* 26731da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 26741da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 26751da177e4SLinus Torvalds * request. 26761da177e4SLinus Torvalds * 26771da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 26781da177e4SLinus Torvalds * scan then give up on it. 26791da177e4SLinus Torvalds */ 26800a0337e0SMichal Hocko static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 26811da177e4SLinus Torvalds { 2682dd1a239fSMel Gorman struct zoneref *z; 268354a6eb5cSMel Gorman struct zone *zone; 26840608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 26850608f43dSAndrew Morton unsigned long nr_soft_scanned; 2686619d0d76SWeijie Yang gfp_t orig_mask; 268779dafcdcSMel Gorman pg_data_t *last_pgdat = NULL; 26881cfb419bSKAMEZAWA Hiroyuki 2689cc715d99SMel Gorman /* 2690cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 2691cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 2692cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 2693cc715d99SMel Gorman */ 2694619d0d76SWeijie Yang orig_mask = sc->gfp_mask; 2695b2e18757SMel Gorman if (buffer_heads_over_limit) { 2696cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 26974f588331SMel Gorman sc->reclaim_idx = gfp_zone(sc->gfp_mask); 2698b2e18757SMel Gorman } 2699cc715d99SMel Gorman 2700d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 2701b2e18757SMel Gorman sc->reclaim_idx, sc->nodemask) { 2702b2e18757SMel Gorman /* 27031cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 27041cfb419bSKAMEZAWA Hiroyuki * to global LRU. 27051cfb419bSKAMEZAWA Hiroyuki */ 270689b5fae5SJohannes Weiner if (global_reclaim(sc)) { 2707344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, 2708344736f2SVladimir Davydov GFP_KERNEL | __GFP_HARDWALL)) 27091da177e4SLinus Torvalds continue; 271065ec02cbSVladimir Davydov 27116e543d57SLisa Du if (sc->priority != DEF_PRIORITY && 2712599d0c95SMel Gorman !pgdat_reclaimable(zone->zone_pgdat)) 27131da177e4SLinus Torvalds continue; /* Let kswapd poll it */ 27140b06496aSJohannes Weiner 2715e0887c19SRik van Riel /* 2716e0c23279SMel Gorman * If we already have plenty of memory free for 2717e0c23279SMel Gorman * compaction in this zone, don't free any more. 2718e0c23279SMel Gorman * Even though compaction is invoked for any 2719e0c23279SMel Gorman * non-zero order, only frequent costly order 2720e0c23279SMel Gorman * reclamation is disruptive enough to become a 2721c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 2722c7cfa37bSCopot Alexandru * page allocations. 2723e0887c19SRik van Riel */ 27240b06496aSJohannes Weiner if (IS_ENABLED(CONFIG_COMPACTION) && 27250b06496aSJohannes Weiner sc->order > PAGE_ALLOC_COSTLY_ORDER && 27264f588331SMel Gorman compaction_ready(zone, sc)) { 27270b06496aSJohannes Weiner sc->compaction_ready = true; 2728e0887c19SRik van Riel continue; 2729e0887c19SRik van Riel } 27300b06496aSJohannes Weiner 27310608f43dSAndrew Morton /* 273279dafcdcSMel Gorman * Shrink each node in the zonelist once. If the 273379dafcdcSMel Gorman * zonelist is ordered by zone (not the default) then a 273479dafcdcSMel Gorman * node may be shrunk multiple times but in that case 273579dafcdcSMel Gorman * the user prefers lower zones being preserved. 273679dafcdcSMel Gorman */ 273779dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 273879dafcdcSMel Gorman continue; 273979dafcdcSMel Gorman 274079dafcdcSMel Gorman /* 27410608f43dSAndrew Morton * This steals pages from memory cgroups over softlimit 27420608f43dSAndrew Morton * and returns the number of reclaimed pages and 27430608f43dSAndrew Morton * scanned pages. This works for global memory pressure 27440608f43dSAndrew Morton * and balancing, not for a memcg's limit. 27450608f43dSAndrew Morton */ 27460608f43dSAndrew Morton nr_soft_scanned = 0; 2747ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, 27480608f43dSAndrew Morton sc->order, sc->gfp_mask, 27490608f43dSAndrew Morton &nr_soft_scanned); 27500608f43dSAndrew Morton sc->nr_reclaimed += nr_soft_reclaimed; 27510608f43dSAndrew Morton sc->nr_scanned += nr_soft_scanned; 2752ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 2753ac34a1a3SKAMEZAWA Hiroyuki } 2754d149e3b2SYing Han 275579dafcdcSMel Gorman /* See comment about same check for global reclaim above */ 275679dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 275779dafcdcSMel Gorman continue; 275879dafcdcSMel Gorman last_pgdat = zone->zone_pgdat; 2759970a39a3SMel Gorman shrink_node(zone->zone_pgdat, sc); 27601da177e4SLinus Torvalds } 2761e0c23279SMel Gorman 276265ec02cbSVladimir Davydov /* 2763619d0d76SWeijie Yang * Restore to original mask to avoid the impact on the caller if we 2764619d0d76SWeijie Yang * promoted it to __GFP_HIGHMEM. 2765619d0d76SWeijie Yang */ 2766619d0d76SWeijie Yang sc->gfp_mask = orig_mask; 27671da177e4SLinus Torvalds } 27681da177e4SLinus Torvalds 27691da177e4SLinus Torvalds /* 27701da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 27711da177e4SLinus Torvalds * 27721da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 27731da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 27741da177e4SLinus Torvalds * 27751da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 27761da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 27775b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 27785b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 27795b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 27805b0830cbSJens Axboe * work, and the allocation attempt will fail. 2781a41f24eaSNishanth Aravamudan * 2782a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 2783a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 27841da177e4SLinus Torvalds */ 2785dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 27863115cd91SVladimir Davydov struct scan_control *sc) 27871da177e4SLinus Torvalds { 2788241994edSJohannes Weiner int initial_priority = sc->priority; 278969e05944SAndrew Morton unsigned long total_scanned = 0; 279022fba335SKOSAKI Motohiro unsigned long writeback_threshold; 2791241994edSJohannes Weiner retry: 2792873b4771SKeika Kobayashi delayacct_freepages_start(); 2793873b4771SKeika Kobayashi 279489b5fae5SJohannes Weiner if (global_reclaim(sc)) 27957cc30fcfSMel Gorman __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); 27961da177e4SLinus Torvalds 27979e3b2f8cSKonstantin Khlebnikov do { 279870ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 279970ddf637SAnton Vorontsov sc->priority); 280066e1707bSBalbir Singh sc->nr_scanned = 0; 28010a0337e0SMichal Hocko shrink_zones(zonelist, sc); 2802e0c23279SMel Gorman 280366e1707bSBalbir Singh total_scanned += sc->nr_scanned; 2804bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 28050b06496aSJohannes Weiner break; 28060b06496aSJohannes Weiner 28070b06496aSJohannes Weiner if (sc->compaction_ready) 28080b06496aSJohannes Weiner break; 28091da177e4SLinus Torvalds 28101da177e4SLinus Torvalds /* 28110e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 28120e50ce3bSMinchan Kim * writepage even in laptop mode. 28130e50ce3bSMinchan Kim */ 28140e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 28150e50ce3bSMinchan Kim sc->may_writepage = 1; 28160e50ce3bSMinchan Kim 28170e50ce3bSMinchan Kim /* 28181da177e4SLinus Torvalds * Try to write back as many pages as we just scanned. This 28191da177e4SLinus Torvalds * tends to cause slow streaming writers to write data to the 28201da177e4SLinus Torvalds * disk smoothly, at the dirtying rate, which is nice. But 28211da177e4SLinus Torvalds * that's undesirable in laptop mode, where we *want* lumpy 28221da177e4SLinus Torvalds * writeout. So in laptop mode, write out the whole world. 28231da177e4SLinus Torvalds */ 282422fba335SKOSAKI Motohiro writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2; 282522fba335SKOSAKI Motohiro if (total_scanned > writeback_threshold) { 28260e175a18SCurt Wohlgemuth wakeup_flusher_threads(laptop_mode ? 0 : total_scanned, 28270e175a18SCurt Wohlgemuth WB_REASON_TRY_TO_FREE_PAGES); 282866e1707bSBalbir Singh sc->may_writepage = 1; 28291da177e4SLinus Torvalds } 28300b06496aSJohannes Weiner } while (--sc->priority >= 0); 2831bb21c7ceSKOSAKI Motohiro 2832873b4771SKeika Kobayashi delayacct_freepages_end(); 2833873b4771SKeika Kobayashi 2834bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 2835bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 2836bb21c7ceSKOSAKI Motohiro 28370cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 28380b06496aSJohannes Weiner if (sc->compaction_ready) 28397335084dSMel Gorman return 1; 28407335084dSMel Gorman 2841241994edSJohannes Weiner /* Untapped cgroup reserves? Don't OOM, retry. */ 2842241994edSJohannes Weiner if (!sc->may_thrash) { 2843241994edSJohannes Weiner sc->priority = initial_priority; 2844241994edSJohannes Weiner sc->may_thrash = 1; 2845241994edSJohannes Weiner goto retry; 2846241994edSJohannes Weiner } 2847241994edSJohannes Weiner 2848bb21c7ceSKOSAKI Motohiro return 0; 28491da177e4SLinus Torvalds } 28501da177e4SLinus Torvalds 28515515061dSMel Gorman static bool pfmemalloc_watermark_ok(pg_data_t *pgdat) 28525515061dSMel Gorman { 28535515061dSMel Gorman struct zone *zone; 28545515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 28555515061dSMel Gorman unsigned long free_pages = 0; 28565515061dSMel Gorman int i; 28575515061dSMel Gorman bool wmark_ok; 28585515061dSMel Gorman 28595515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 28605515061dSMel Gorman zone = &pgdat->node_zones[i]; 28616aa303deSMel Gorman if (!managed_zone(zone) || 2862599d0c95SMel Gorman pgdat_reclaimable_pages(pgdat) == 0) 2863675becceSMel Gorman continue; 2864675becceSMel Gorman 28655515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 28665515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 28675515061dSMel Gorman } 28685515061dSMel Gorman 2869675becceSMel Gorman /* If there are no reserves (unexpected config) then do not throttle */ 2870675becceSMel Gorman if (!pfmemalloc_reserve) 2871675becceSMel Gorman return true; 2872675becceSMel Gorman 28735515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 28745515061dSMel Gorman 28755515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 28765515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 287738087d9bSMel Gorman pgdat->kswapd_classzone_idx = min(pgdat->kswapd_classzone_idx, 28785515061dSMel Gorman (enum zone_type)ZONE_NORMAL); 28795515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 28805515061dSMel Gorman } 28815515061dSMel Gorman 28825515061dSMel Gorman return wmark_ok; 28835515061dSMel Gorman } 28845515061dSMel Gorman 28855515061dSMel Gorman /* 28865515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 28875515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 28885515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 288950694c28SMel Gorman * when the low watermark is reached. 289050694c28SMel Gorman * 289150694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 289250694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 28935515061dSMel Gorman */ 289450694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 28955515061dSMel Gorman nodemask_t *nodemask) 28965515061dSMel Gorman { 2897675becceSMel Gorman struct zoneref *z; 28985515061dSMel Gorman struct zone *zone; 2899675becceSMel Gorman pg_data_t *pgdat = NULL; 29005515061dSMel Gorman 29015515061dSMel Gorman /* 29025515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 29035515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 29045515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 29055515061dSMel Gorman * committing a transaction where throttling it could forcing other 29065515061dSMel Gorman * processes to block on log_wait_commit(). 29075515061dSMel Gorman */ 29085515061dSMel Gorman if (current->flags & PF_KTHREAD) 290950694c28SMel Gorman goto out; 291050694c28SMel Gorman 291150694c28SMel Gorman /* 291250694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 291350694c28SMel Gorman * It should return quickly so it can exit and free its memory 291450694c28SMel Gorman */ 291550694c28SMel Gorman if (fatal_signal_pending(current)) 291650694c28SMel Gorman goto out; 29175515061dSMel Gorman 2918675becceSMel Gorman /* 2919675becceSMel Gorman * Check if the pfmemalloc reserves are ok by finding the first node 2920675becceSMel Gorman * with a usable ZONE_NORMAL or lower zone. The expectation is that 2921675becceSMel Gorman * GFP_KERNEL will be required for allocating network buffers when 2922675becceSMel Gorman * swapping over the network so ZONE_HIGHMEM is unusable. 2923675becceSMel Gorman * 2924675becceSMel Gorman * Throttling is based on the first usable node and throttled processes 2925675becceSMel Gorman * wait on a queue until kswapd makes progress and wakes them. There 2926675becceSMel Gorman * is an affinity then between processes waking up and where reclaim 2927675becceSMel Gorman * progress has been made assuming the process wakes on the same node. 2928675becceSMel Gorman * More importantly, processes running on remote nodes will not compete 2929675becceSMel Gorman * for remote pfmemalloc reserves and processes on different nodes 2930675becceSMel Gorman * should make reasonable progress. 2931675becceSMel Gorman */ 2932675becceSMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 293317636faaSMichael S. Tsirkin gfp_zone(gfp_mask), nodemask) { 2934675becceSMel Gorman if (zone_idx(zone) > ZONE_NORMAL) 2935675becceSMel Gorman continue; 2936675becceSMel Gorman 2937675becceSMel Gorman /* Throttle based on the first usable node */ 29385515061dSMel Gorman pgdat = zone->zone_pgdat; 29395515061dSMel Gorman if (pfmemalloc_watermark_ok(pgdat)) 294050694c28SMel Gorman goto out; 2941675becceSMel Gorman break; 2942675becceSMel Gorman } 2943675becceSMel Gorman 2944675becceSMel Gorman /* If no zone was usable by the allocation flags then do not throttle */ 2945675becceSMel Gorman if (!pgdat) 2946675becceSMel Gorman goto out; 29475515061dSMel Gorman 294868243e76SMel Gorman /* Account for the throttling */ 294968243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 295068243e76SMel Gorman 29515515061dSMel Gorman /* 29525515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 29535515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 29545515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 29555515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 29565515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 29575515061dSMel Gorman * second before continuing. 29585515061dSMel Gorman */ 29595515061dSMel Gorman if (!(gfp_mask & __GFP_FS)) { 29605515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 29615515061dSMel Gorman pfmemalloc_watermark_ok(pgdat), HZ); 296250694c28SMel Gorman 296350694c28SMel Gorman goto check_pending; 29645515061dSMel Gorman } 29655515061dSMel Gorman 29665515061dSMel Gorman /* Throttle until kswapd wakes the process */ 29675515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 29685515061dSMel Gorman pfmemalloc_watermark_ok(pgdat)); 296950694c28SMel Gorman 297050694c28SMel Gorman check_pending: 297150694c28SMel Gorman if (fatal_signal_pending(current)) 297250694c28SMel Gorman return true; 297350694c28SMel Gorman 297450694c28SMel Gorman out: 297550694c28SMel Gorman return false; 29765515061dSMel Gorman } 29775515061dSMel Gorman 2978dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 2979327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 298066e1707bSBalbir Singh { 298133906bc5SMel Gorman unsigned long nr_reclaimed; 298266e1707bSBalbir Singh struct scan_control sc = { 298322fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 2984ee814fe2SJohannes Weiner .gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)), 2985b2e18757SMel Gorman .reclaim_idx = gfp_zone(gfp_mask), 2986ee814fe2SJohannes Weiner .order = order, 2987ee814fe2SJohannes Weiner .nodemask = nodemask, 2988ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 2989ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 2990a6dc60f8SJohannes Weiner .may_unmap = 1, 29912e2e4259SKOSAKI Motohiro .may_swap = 1, 299266e1707bSBalbir Singh }; 299366e1707bSBalbir Singh 29945515061dSMel Gorman /* 299550694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 299650694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 299750694c28SMel Gorman * point. 29985515061dSMel Gorman */ 299950694c28SMel Gorman if (throttle_direct_reclaim(gfp_mask, zonelist, nodemask)) 30005515061dSMel Gorman return 1; 30015515061dSMel Gorman 300233906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_begin(order, 300333906bc5SMel Gorman sc.may_writepage, 3004e5146b12SMel Gorman gfp_mask, 3005e5146b12SMel Gorman sc.reclaim_idx); 300633906bc5SMel Gorman 30073115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 300833906bc5SMel Gorman 300933906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 301033906bc5SMel Gorman 301133906bc5SMel Gorman return nr_reclaimed; 301266e1707bSBalbir Singh } 301366e1707bSBalbir Singh 3014c255a458SAndrew Morton #ifdef CONFIG_MEMCG 301566e1707bSBalbir Singh 3016a9dd0a83SMel Gorman unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, 30174e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 3018ef8f2327SMel Gorman pg_data_t *pgdat, 30190ae5e89cSYing Han unsigned long *nr_scanned) 30204e416953SBalbir Singh { 30214e416953SBalbir Singh struct scan_control sc = { 3022b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3023ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 30244e416953SBalbir Singh .may_writepage = !laptop_mode, 30254e416953SBalbir Singh .may_unmap = 1, 3026b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 30274e416953SBalbir Singh .may_swap = !noswap, 30284e416953SBalbir Singh }; 30296b4f7799SJohannes Weiner unsigned long lru_pages; 30300ae5e89cSYing Han 30314e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 30324e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 3033bdce6d9eSKOSAKI Motohiro 30349e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 3035bdce6d9eSKOSAKI Motohiro sc.may_writepage, 3036e5146b12SMel Gorman sc.gfp_mask, 3037e5146b12SMel Gorman sc.reclaim_idx); 3038bdce6d9eSKOSAKI Motohiro 30394e416953SBalbir Singh /* 30404e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 30414e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 3042a9dd0a83SMel Gorman * if we don't reclaim here, the shrink_node from balance_pgdat 30434e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 30444e416953SBalbir Singh * the priority and make it zero. 30454e416953SBalbir Singh */ 3046ef8f2327SMel Gorman shrink_node_memcg(pgdat, memcg, &sc, &lru_pages); 3047bdce6d9eSKOSAKI Motohiro 3048bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 3049bdce6d9eSKOSAKI Motohiro 30500ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 30514e416953SBalbir Singh return sc.nr_reclaimed; 30524e416953SBalbir Singh } 30534e416953SBalbir Singh 305472835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 3055b70a2a21SJohannes Weiner unsigned long nr_pages, 30568c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 3057b70a2a21SJohannes Weiner bool may_swap) 305866e1707bSBalbir Singh { 30594e416953SBalbir Singh struct zonelist *zonelist; 3060bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 3061889976dbSYing Han int nid; 306266e1707bSBalbir Singh struct scan_control sc = { 3063b70a2a21SJohannes Weiner .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 3064ee814fe2SJohannes Weiner .gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 3065ee814fe2SJohannes Weiner (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 3066b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 3067ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 3068ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 306966e1707bSBalbir Singh .may_writepage = !laptop_mode, 3070a6dc60f8SJohannes Weiner .may_unmap = 1, 3071b70a2a21SJohannes Weiner .may_swap = may_swap, 3072a09ed5e0SYing Han }; 307366e1707bSBalbir Singh 3074889976dbSYing Han /* 3075889976dbSYing Han * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't 3076889976dbSYing Han * take care of from where we get pages. So the node where we start the 3077889976dbSYing Han * scan does not need to be the current node. 3078889976dbSYing Han */ 307972835c86SJohannes Weiner nid = mem_cgroup_select_victim_node(memcg); 3080889976dbSYing Han 3081c9634cf0SAneesh Kumar K.V zonelist = &NODE_DATA(nid)->node_zonelists[ZONELIST_FALLBACK]; 3082bdce6d9eSKOSAKI Motohiro 3083bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_begin(0, 3084bdce6d9eSKOSAKI Motohiro sc.may_writepage, 3085e5146b12SMel Gorman sc.gfp_mask, 3086e5146b12SMel Gorman sc.reclaim_idx); 3087bdce6d9eSKOSAKI Motohiro 308889a28483SJohannes Weiner current->flags |= PF_MEMALLOC; 30893115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 309089a28483SJohannes Weiner current->flags &= ~PF_MEMALLOC; 3091bdce6d9eSKOSAKI Motohiro 3092bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 3093bdce6d9eSKOSAKI Motohiro 3094bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 309566e1707bSBalbir Singh } 309666e1707bSBalbir Singh #endif 309766e1707bSBalbir Singh 30981d82de61SMel Gorman static void age_active_anon(struct pglist_data *pgdat, 3099ef8f2327SMel Gorman struct scan_control *sc) 3100f16015fbSJohannes Weiner { 3101b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 3102b95a2f2dSJohannes Weiner 3103b95a2f2dSJohannes Weiner if (!total_swap_pages) 3104b95a2f2dSJohannes Weiner return; 3105b95a2f2dSJohannes Weiner 3106b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 3107b95a2f2dSJohannes Weiner do { 3108ef8f2327SMel Gorman struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg); 3109f16015fbSJohannes Weiner 3110dcec0b60SMichal Hocko if (inactive_list_is_low(lruvec, false, sc, true)) 31111a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 31129e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 3113b95a2f2dSJohannes Weiner 3114b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 3115b95a2f2dSJohannes Weiner } while (memcg); 3116f16015fbSJohannes Weiner } 3117f16015fbSJohannes Weiner 311831483b6aSMel Gorman static bool zone_balanced(struct zone *zone, int order, int classzone_idx) 311960cefed4SJohannes Weiner { 312031483b6aSMel Gorman unsigned long mark = high_wmark_pages(zone); 312160cefed4SJohannes Weiner 31226256c6b4SMel Gorman if (!zone_watermark_ok_safe(zone, order, mark, classzone_idx)) 31236256c6b4SMel Gorman return false; 31246256c6b4SMel Gorman 31256256c6b4SMel Gorman /* 31266256c6b4SMel Gorman * If any eligible zone is balanced then the node is not considered 31276256c6b4SMel Gorman * to be congested or dirty 31286256c6b4SMel Gorman */ 31296256c6b4SMel Gorman clear_bit(PGDAT_CONGESTED, &zone->zone_pgdat->flags); 31306256c6b4SMel Gorman clear_bit(PGDAT_DIRTY, &zone->zone_pgdat->flags); 31316256c6b4SMel Gorman 31326256c6b4SMel Gorman return true; 313360cefed4SJohannes Weiner } 313460cefed4SJohannes Weiner 31351741c877SMel Gorman /* 31365515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 31375515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 31385515061dSMel Gorman * 31395515061dSMel Gorman * Returns true if kswapd is ready to sleep 31405515061dSMel Gorman */ 3141d9f21d42SMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, int classzone_idx) 3142f50de2d3SMel Gorman { 31431d82de61SMel Gorman int i; 31441d82de61SMel Gorman 31455515061dSMel Gorman /* 31469e5e3661SVlastimil Babka * The throttled processes are normally woken up in balance_pgdat() as 31479e5e3661SVlastimil Babka * soon as pfmemalloc_watermark_ok() is true. But there is a potential 31489e5e3661SVlastimil Babka * race between when kswapd checks the watermarks and a process gets 31499e5e3661SVlastimil Babka * throttled. There is also a potential race if processes get 31509e5e3661SVlastimil Babka * throttled, kswapd wakes, a large process exits thereby balancing the 31519e5e3661SVlastimil Babka * zones, which causes kswapd to exit balance_pgdat() before reaching 31529e5e3661SVlastimil Babka * the wake up checks. If kswapd is going to sleep, no process should 31539e5e3661SVlastimil Babka * be sleeping on pfmemalloc_wait, so wake them now if necessary. If 31549e5e3661SVlastimil Babka * the wake up is premature, processes will wake kswapd and get 31559e5e3661SVlastimil Babka * throttled again. The difference from wake ups in balance_pgdat() is 31569e5e3661SVlastimil Babka * that here we are under prepare_to_wait(). 31575515061dSMel Gorman */ 31589e5e3661SVlastimil Babka if (waitqueue_active(&pgdat->pfmemalloc_wait)) 31599e5e3661SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 3160f50de2d3SMel Gorman 31611d82de61SMel Gorman for (i = 0; i <= classzone_idx; i++) { 31621d82de61SMel Gorman struct zone *zone = pgdat->node_zones + i; 31631d82de61SMel Gorman 31646aa303deSMel Gorman if (!managed_zone(zone)) 31651d82de61SMel Gorman continue; 31661d82de61SMel Gorman 316738087d9bSMel Gorman if (!zone_balanced(zone, order, classzone_idx)) 316838087d9bSMel Gorman return false; 31691d82de61SMel Gorman } 31701d82de61SMel Gorman 317138087d9bSMel Gorman return true; 3172f50de2d3SMel Gorman } 3173f50de2d3SMel Gorman 31741da177e4SLinus Torvalds /* 31751d82de61SMel Gorman * kswapd shrinks a node of pages that are at or below the highest usable 31761d82de61SMel Gorman * zone that is currently unbalanced. 3177b8e83b94SMel Gorman * 3178b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 3179283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 3180283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 318175485363SMel Gorman */ 31821d82de61SMel Gorman static bool kswapd_shrink_node(pg_data_t *pgdat, 3183accf6242SVlastimil Babka struct scan_control *sc) 318475485363SMel Gorman { 31851d82de61SMel Gorman struct zone *zone; 31861d82de61SMel Gorman int z; 318775485363SMel Gorman 31881d82de61SMel Gorman /* Reclaim a number of pages proportional to the number of zones */ 31891d82de61SMel Gorman sc->nr_to_reclaim = 0; 3190970a39a3SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 31911d82de61SMel Gorman zone = pgdat->node_zones + z; 31926aa303deSMel Gorman if (!managed_zone(zone)) 31931d82de61SMel Gorman continue; 31947c954f6dSMel Gorman 31951d82de61SMel Gorman sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); 31967c954f6dSMel Gorman } 31977c954f6dSMel Gorman 31981d82de61SMel Gorman /* 31991d82de61SMel Gorman * Historically care was taken to put equal pressure on all zones but 32001d82de61SMel Gorman * now pressure is applied based on node LRU order. 32011d82de61SMel Gorman */ 3202970a39a3SMel Gorman shrink_node(pgdat, sc); 32031d82de61SMel Gorman 32041d82de61SMel Gorman /* 32051d82de61SMel Gorman * Fragmentation may mean that the system cannot be rebalanced for 32061d82de61SMel Gorman * high-order allocations. If twice the allocation size has been 32071d82de61SMel Gorman * reclaimed then recheck watermarks only at order-0 to prevent 32081d82de61SMel Gorman * excessive reclaim. Assume that a process requested a high-order 32091d82de61SMel Gorman * can direct reclaim/compact. 32101d82de61SMel Gorman */ 32119861a62cSVlastimil Babka if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order)) 32121d82de61SMel Gorman sc->order = 0; 32131d82de61SMel Gorman 3214b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 321575485363SMel Gorman } 321675485363SMel Gorman 321775485363SMel Gorman /* 32181d82de61SMel Gorman * For kswapd, balance_pgdat() will reclaim pages across a node from zones 32191d82de61SMel Gorman * that are eligible for use by the caller until at least one zone is 32201d82de61SMel Gorman * balanced. 32211da177e4SLinus Torvalds * 32221d82de61SMel Gorman * Returns the order kswapd finished reclaiming at. 32231da177e4SLinus Torvalds * 32241da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 322541858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 32261d82de61SMel Gorman * found to have free_pages <= high_wmark_pages(zone), any page is that zone 32271d82de61SMel Gorman * or lower is eligible for reclaim until at least one usable zone is 32281d82de61SMel Gorman * balanced. 32291da177e4SLinus Torvalds */ 3230accf6242SVlastimil Babka static int balance_pgdat(pg_data_t *pgdat, int order, int classzone_idx) 32311da177e4SLinus Torvalds { 32321da177e4SLinus Torvalds int i; 32330608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 32340608f43dSAndrew Morton unsigned long nr_soft_scanned; 32351d82de61SMel Gorman struct zone *zone; 3236179e9639SAndrew Morton struct scan_control sc = { 3237179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 3238ee814fe2SJohannes Weiner .order = order, 3239b8e83b94SMel Gorman .priority = DEF_PRIORITY, 3240ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3241a6dc60f8SJohannes Weiner .may_unmap = 1, 32422e2e4259SKOSAKI Motohiro .may_swap = 1, 3243179e9639SAndrew Morton }; 3244f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 32451da177e4SLinus Torvalds 32469e3b2f8cSKonstantin Khlebnikov do { 3247b8e83b94SMel Gorman bool raise_priority = true; 3248b8e83b94SMel Gorman 3249b8e83b94SMel Gorman sc.nr_reclaimed = 0; 325084c7a777SMel Gorman sc.reclaim_idx = classzone_idx; 32511da177e4SLinus Torvalds 325286c79f6bSMel Gorman /* 325384c7a777SMel Gorman * If the number of buffer_heads exceeds the maximum allowed 325484c7a777SMel Gorman * then consider reclaiming from all zones. This has a dual 325584c7a777SMel Gorman * purpose -- on 64-bit systems it is expected that 325684c7a777SMel Gorman * buffer_heads are stripped during active rotation. On 32-bit 325784c7a777SMel Gorman * systems, highmem pages can pin lowmem memory and shrinking 325884c7a777SMel Gorman * buffers can relieve lowmem pressure. Reclaim may still not 325984c7a777SMel Gorman * go ahead if all eligible zones for the original allocation 326084c7a777SMel Gorman * request are balanced to avoid excessive reclaim from kswapd. 326186c79f6bSMel Gorman */ 326286c79f6bSMel Gorman if (buffer_heads_over_limit) { 326386c79f6bSMel Gorman for (i = MAX_NR_ZONES - 1; i >= 0; i--) { 326486c79f6bSMel Gorman zone = pgdat->node_zones + i; 32656aa303deSMel Gorman if (!managed_zone(zone)) 326686c79f6bSMel Gorman continue; 326786c79f6bSMel Gorman 3268970a39a3SMel Gorman sc.reclaim_idx = i; 326986c79f6bSMel Gorman break; 327086c79f6bSMel Gorman } 327186c79f6bSMel Gorman } 327286c79f6bSMel Gorman 327386c79f6bSMel Gorman /* 327486c79f6bSMel Gorman * Only reclaim if there are no eligible zones. Check from 327586c79f6bSMel Gorman * high to low zone as allocations prefer higher zones. 327686c79f6bSMel Gorman * Scanning from low to high zone would allow congestion to be 327786c79f6bSMel Gorman * cleared during a very small window when a small low 327886c79f6bSMel Gorman * zone was balanced even under extreme pressure when the 327984c7a777SMel Gorman * overall node may be congested. Note that sc.reclaim_idx 328084c7a777SMel Gorman * is not used as buffer_heads_over_limit may have adjusted 328184c7a777SMel Gorman * it. 328286c79f6bSMel Gorman */ 328384c7a777SMel Gorman for (i = classzone_idx; i >= 0; i--) { 32841d82de61SMel Gorman zone = pgdat->node_zones + i; 32856aa303deSMel Gorman if (!managed_zone(zone)) 32861da177e4SLinus Torvalds continue; 32871da177e4SLinus Torvalds 328884c7a777SMel Gorman if (zone_balanced(zone, sc.order, classzone_idx)) 32891da177e4SLinus Torvalds goto out; 329086c79f6bSMel Gorman } 3291e1dbeda6SAndrew Morton 32921da177e4SLinus Torvalds /* 32931d82de61SMel Gorman * Do some background aging of the anon list, to give 32941d82de61SMel Gorman * pages a chance to be referenced before reclaiming. All 32951d82de61SMel Gorman * pages are rotated regardless of classzone as this is 32961d82de61SMel Gorman * about consistent aging. 32971d82de61SMel Gorman */ 3298ef8f2327SMel Gorman age_active_anon(pgdat, &sc); 32991d82de61SMel Gorman 33001d82de61SMel Gorman /* 3301b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 3302b7ea3c41SMel Gorman * even in laptop mode. 3303b7ea3c41SMel Gorman */ 33041d82de61SMel Gorman if (sc.priority < DEF_PRIORITY - 2 || !pgdat_reclaimable(pgdat)) 3305b7ea3c41SMel Gorman sc.may_writepage = 1; 3306b7ea3c41SMel Gorman 33071d82de61SMel Gorman /* Call soft limit reclaim before calling shrink_node. */ 33081da177e4SLinus Torvalds sc.nr_scanned = 0; 33090608f43dSAndrew Morton nr_soft_scanned = 0; 3310ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, 33111d82de61SMel Gorman sc.gfp_mask, &nr_soft_scanned); 33120608f43dSAndrew Morton sc.nr_reclaimed += nr_soft_reclaimed; 33130608f43dSAndrew Morton 331432a4330dSRik van Riel /* 33151d82de61SMel Gorman * There should be no need to raise the scanning priority if 33161d82de61SMel Gorman * enough pages are already being scanned that that high 33171d82de61SMel Gorman * watermark would be met at 100% efficiency. 331832a4330dSRik van Riel */ 3319970a39a3SMel Gorman if (kswapd_shrink_node(pgdat, &sc)) 3320b8e83b94SMel Gorman raise_priority = false; 3321d7868daeSMel Gorman 33225515061dSMel Gorman /* 33235515061dSMel Gorman * If the low watermark is met there is no need for processes 33245515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 33255515061dSMel Gorman * able to safely make forward progress. Wake them 33265515061dSMel Gorman */ 33275515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 33285515061dSMel Gorman pfmemalloc_watermark_ok(pgdat)) 3329cfc51155SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 33305515061dSMel Gorman 3331b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 3332b8e83b94SMel Gorman if (try_to_freeze() || kthread_should_stop()) 3333b8e83b94SMel Gorman break; 3334b8e83b94SMel Gorman 3335b8e83b94SMel Gorman /* 3336b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 3337b8e83b94SMel Gorman * progress in reclaiming pages 3338b8e83b94SMel Gorman */ 3339b8e83b94SMel Gorman if (raise_priority || !sc.nr_reclaimed) 3340b8e83b94SMel Gorman sc.priority--; 33411d82de61SMel Gorman } while (sc.priority >= 1); 33421da177e4SLinus Torvalds 3343b8e83b94SMel Gorman out: 33440abdee2bSMel Gorman /* 33451d82de61SMel Gorman * Return the order kswapd stopped reclaiming at as 33461d82de61SMel Gorman * prepare_kswapd_sleep() takes it into account. If another caller 33471d82de61SMel Gorman * entered the allocator slow path while kswapd was awake, order will 33481d82de61SMel Gorman * remain at the higher level. 33490abdee2bSMel Gorman */ 33501d82de61SMel Gorman return sc.order; 33511da177e4SLinus Torvalds } 33521da177e4SLinus Torvalds 335338087d9bSMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, 335438087d9bSMel Gorman unsigned int classzone_idx) 3355f0bc0a60SKOSAKI Motohiro { 3356f0bc0a60SKOSAKI Motohiro long remaining = 0; 3357f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 3358f0bc0a60SKOSAKI Motohiro 3359f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 3360f0bc0a60SKOSAKI Motohiro return; 3361f0bc0a60SKOSAKI Motohiro 3362f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3363f0bc0a60SKOSAKI Motohiro 3364f0bc0a60SKOSAKI Motohiro /* Try to sleep for a short interval */ 3365d9f21d42SMel Gorman if (prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) { 3366fd901c95SVlastimil Babka /* 3367fd901c95SVlastimil Babka * Compaction records what page blocks it recently failed to 3368fd901c95SVlastimil Babka * isolate pages from and skips them in the future scanning. 3369fd901c95SVlastimil Babka * When kswapd is going to sleep, it is reasonable to assume 3370fd901c95SVlastimil Babka * that pages and compaction may succeed so reset the cache. 3371fd901c95SVlastimil Babka */ 3372fd901c95SVlastimil Babka reset_isolation_suitable(pgdat); 3373fd901c95SVlastimil Babka 3374fd901c95SVlastimil Babka /* 3375fd901c95SVlastimil Babka * We have freed the memory, now we should compact it to make 3376fd901c95SVlastimil Babka * allocation of the requested order possible. 3377fd901c95SVlastimil Babka */ 337838087d9bSMel Gorman wakeup_kcompactd(pgdat, alloc_order, classzone_idx); 3379fd901c95SVlastimil Babka 3380f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 338138087d9bSMel Gorman 338238087d9bSMel Gorman /* 338338087d9bSMel Gorman * If woken prematurely then reset kswapd_classzone_idx and 338438087d9bSMel Gorman * order. The values will either be from a wakeup request or 338538087d9bSMel Gorman * the previous request that slept prematurely. 338638087d9bSMel Gorman */ 338738087d9bSMel Gorman if (remaining) { 338838087d9bSMel Gorman pgdat->kswapd_classzone_idx = max(pgdat->kswapd_classzone_idx, classzone_idx); 338938087d9bSMel Gorman pgdat->kswapd_order = max(pgdat->kswapd_order, reclaim_order); 339038087d9bSMel Gorman } 339138087d9bSMel Gorman 3392f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3393f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3394f0bc0a60SKOSAKI Motohiro } 3395f0bc0a60SKOSAKI Motohiro 3396f0bc0a60SKOSAKI Motohiro /* 3397f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 3398f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 3399f0bc0a60SKOSAKI Motohiro */ 3400d9f21d42SMel Gorman if (!remaining && 3401d9f21d42SMel Gorman prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) { 3402f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 3403f0bc0a60SKOSAKI Motohiro 3404f0bc0a60SKOSAKI Motohiro /* 3405f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 3406f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 3407f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 3408f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 3409f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 3410f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 3411f0bc0a60SKOSAKI Motohiro */ 3412f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 34131c7e7f6cSAaditya Kumar 34141c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 3415f0bc0a60SKOSAKI Motohiro schedule(); 34161c7e7f6cSAaditya Kumar 3417f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 3418f0bc0a60SKOSAKI Motohiro } else { 3419f0bc0a60SKOSAKI Motohiro if (remaining) 3420f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 3421f0bc0a60SKOSAKI Motohiro else 3422f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 3423f0bc0a60SKOSAKI Motohiro } 3424f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3425f0bc0a60SKOSAKI Motohiro } 3426f0bc0a60SKOSAKI Motohiro 34271da177e4SLinus Torvalds /* 34281da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 34291da177e4SLinus Torvalds * from the init process. 34301da177e4SLinus Torvalds * 34311da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 34321da177e4SLinus Torvalds * free memory available even if there is no other activity 34331da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 34341da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 34351da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 34361da177e4SLinus Torvalds * 34371da177e4SLinus Torvalds * If there are applications that are active memory-allocators 34381da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 34391da177e4SLinus Torvalds */ 34401da177e4SLinus Torvalds static int kswapd(void *p) 34411da177e4SLinus Torvalds { 344238087d9bSMel Gorman unsigned int alloc_order, reclaim_order, classzone_idx; 34431da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 34441da177e4SLinus Torvalds struct task_struct *tsk = current; 3445f0bc0a60SKOSAKI Motohiro 34461da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 34471da177e4SLinus Torvalds .reclaimed_slab = 0, 34481da177e4SLinus Torvalds }; 3449a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 34501da177e4SLinus Torvalds 3451cf40bd16SNick Piggin lockdep_set_current_reclaim_state(GFP_KERNEL); 3452cf40bd16SNick Piggin 3453174596a0SRusty Russell if (!cpumask_empty(cpumask)) 3454c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 34551da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 34561da177e4SLinus Torvalds 34571da177e4SLinus Torvalds /* 34581da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 34591da177e4SLinus Torvalds * and that if we need more memory we should get access to it 34601da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 34611da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 34621da177e4SLinus Torvalds * 34631da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 34641da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 34651da177e4SLinus Torvalds * page out something else, and this flag essentially protects 34661da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 34671da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 34681da177e4SLinus Torvalds */ 3469930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 347083144186SRafael J. Wysocki set_freezable(); 34711da177e4SLinus Torvalds 347238087d9bSMel Gorman pgdat->kswapd_order = alloc_order = reclaim_order = 0; 347338087d9bSMel Gorman pgdat->kswapd_classzone_idx = classzone_idx = 0; 34741da177e4SLinus Torvalds for ( ; ; ) { 34756f6313d4SJeff Liu bool ret; 34763e1d1d28SChristoph Lameter 347738087d9bSMel Gorman kswapd_try_sleep: 347838087d9bSMel Gorman kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, 347938087d9bSMel Gorman classzone_idx); 3480215ddd66SMel Gorman 348138087d9bSMel Gorman /* Read the new order and classzone_idx */ 348238087d9bSMel Gorman alloc_order = reclaim_order = pgdat->kswapd_order; 348338087d9bSMel Gorman classzone_idx = pgdat->kswapd_classzone_idx; 348438087d9bSMel Gorman pgdat->kswapd_order = 0; 348538087d9bSMel Gorman pgdat->kswapd_classzone_idx = 0; 34861da177e4SLinus Torvalds 34878fe23e05SDavid Rientjes ret = try_to_freeze(); 34888fe23e05SDavid Rientjes if (kthread_should_stop()) 34898fe23e05SDavid Rientjes break; 34908fe23e05SDavid Rientjes 34918fe23e05SDavid Rientjes /* 34928fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 34938fe23e05SDavid Rientjes * after returning from the refrigerator 3494b1296cc4SRafael J. Wysocki */ 349538087d9bSMel Gorman if (ret) 349638087d9bSMel Gorman continue; 34971d82de61SMel Gorman 349838087d9bSMel Gorman /* 349938087d9bSMel Gorman * Reclaim begins at the requested order but if a high-order 350038087d9bSMel Gorman * reclaim fails then kswapd falls back to reclaiming for 350138087d9bSMel Gorman * order-0. If that happens, kswapd will consider sleeping 350238087d9bSMel Gorman * for the order it finished reclaiming at (reclaim_order) 350338087d9bSMel Gorman * but kcompactd is woken to compact for the original 350438087d9bSMel Gorman * request (alloc_order). 350538087d9bSMel Gorman */ 3506e5146b12SMel Gorman trace_mm_vmscan_kswapd_wake(pgdat->node_id, classzone_idx, 3507e5146b12SMel Gorman alloc_order); 350838087d9bSMel Gorman reclaim_order = balance_pgdat(pgdat, alloc_order, classzone_idx); 350938087d9bSMel Gorman if (reclaim_order < alloc_order) 351038087d9bSMel Gorman goto kswapd_try_sleep; 351138087d9bSMel Gorman 351238087d9bSMel Gorman alloc_order = reclaim_order = pgdat->kswapd_order; 351338087d9bSMel Gorman classzone_idx = pgdat->kswapd_classzone_idx; 351433906bc5SMel Gorman } 3515b0a8cc58STakamori Yamaguchi 351671abdc15SJohannes Weiner tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); 3517b0a8cc58STakamori Yamaguchi current->reclaim_state = NULL; 351871abdc15SJohannes Weiner lockdep_clear_current_reclaim_state(); 351971abdc15SJohannes Weiner 35201da177e4SLinus Torvalds return 0; 35211da177e4SLinus Torvalds } 35221da177e4SLinus Torvalds 35231da177e4SLinus Torvalds /* 35241da177e4SLinus Torvalds * A zone is low on free memory, so wake its kswapd task to service it. 35251da177e4SLinus Torvalds */ 352699504748SMel Gorman void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx) 35271da177e4SLinus Torvalds { 35281da177e4SLinus Torvalds pg_data_t *pgdat; 3529e1a55637SMel Gorman int z; 35301da177e4SLinus Torvalds 35316aa303deSMel Gorman if (!managed_zone(zone)) 35321da177e4SLinus Torvalds return; 35331da177e4SLinus Torvalds 3534344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, GFP_KERNEL | __GFP_HARDWALL)) 35351da177e4SLinus Torvalds return; 353688f5acf8SMel Gorman pgdat = zone->zone_pgdat; 353738087d9bSMel Gorman pgdat->kswapd_classzone_idx = max(pgdat->kswapd_classzone_idx, classzone_idx); 353838087d9bSMel Gorman pgdat->kswapd_order = max(pgdat->kswapd_order, order); 35398d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 35401da177e4SLinus Torvalds return; 3541e1a55637SMel Gorman 3542e1a55637SMel Gorman /* Only wake kswapd if all zones are unbalanced */ 3543e1a55637SMel Gorman for (z = 0; z <= classzone_idx; z++) { 3544e1a55637SMel Gorman zone = pgdat->node_zones + z; 35456aa303deSMel Gorman if (!managed_zone(zone)) 3546e1a55637SMel Gorman continue; 3547e1a55637SMel Gorman 3548e1a55637SMel Gorman if (zone_balanced(zone, order, classzone_idx)) 354988f5acf8SMel Gorman return; 3550e1a55637SMel Gorman } 355188f5acf8SMel Gorman 355288f5acf8SMel Gorman trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order); 35538d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 35541da177e4SLinus Torvalds } 35551da177e4SLinus Torvalds 3556c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 35571da177e4SLinus Torvalds /* 35587b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 3559d6277db4SRafael J. Wysocki * freed pages. 3560d6277db4SRafael J. Wysocki * 3561d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 3562d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 3563d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 35641da177e4SLinus Torvalds */ 35657b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 35661da177e4SLinus Torvalds { 3567d6277db4SRafael J. Wysocki struct reclaim_state reclaim_state; 3568d6277db4SRafael J. Wysocki struct scan_control sc = { 35697b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 3570ee814fe2SJohannes Weiner .gfp_mask = GFP_HIGHUSER_MOVABLE, 3571b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 35729e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 3573ee814fe2SJohannes Weiner .may_writepage = 1, 3574ee814fe2SJohannes Weiner .may_unmap = 1, 3575ee814fe2SJohannes Weiner .may_swap = 1, 3576ee814fe2SJohannes Weiner .hibernation_mode = 1, 35771da177e4SLinus Torvalds }; 35787b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 35797b51755cSKOSAKI Motohiro struct task_struct *p = current; 35807b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 35811da177e4SLinus Torvalds 35827b51755cSKOSAKI Motohiro p->flags |= PF_MEMALLOC; 35837b51755cSKOSAKI Motohiro lockdep_set_current_reclaim_state(sc.gfp_mask); 3584d6277db4SRafael J. Wysocki reclaim_state.reclaimed_slab = 0; 35857b51755cSKOSAKI Motohiro p->reclaim_state = &reclaim_state; 3586d6277db4SRafael J. Wysocki 35873115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3588d6277db4SRafael J. Wysocki 35897b51755cSKOSAKI Motohiro p->reclaim_state = NULL; 35907b51755cSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 35917b51755cSKOSAKI Motohiro p->flags &= ~PF_MEMALLOC; 3592d6277db4SRafael J. Wysocki 35937b51755cSKOSAKI Motohiro return nr_reclaimed; 35941da177e4SLinus Torvalds } 3595c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 35961da177e4SLinus Torvalds 35971da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 35981da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 35991da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 36001da177e4SLinus Torvalds restore their cpu bindings. */ 3601517bbed9SSebastian Andrzej Siewior static int kswapd_cpu_online(unsigned int cpu) 36021da177e4SLinus Torvalds { 360358c0a4a7SYasunori Goto int nid; 36041da177e4SLinus Torvalds 360548fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) { 3606c5f59f08SMike Travis pg_data_t *pgdat = NODE_DATA(nid); 3607a70f7302SRusty Russell const struct cpumask *mask; 3608a70f7302SRusty Russell 3609a70f7302SRusty Russell mask = cpumask_of_node(pgdat->node_id); 3610c5f59f08SMike Travis 36113e597945SRusty Russell if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 36121da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 3613c5f59f08SMike Travis set_cpus_allowed_ptr(pgdat->kswapd, mask); 36141da177e4SLinus Torvalds } 3615517bbed9SSebastian Andrzej Siewior return 0; 36161da177e4SLinus Torvalds } 36171da177e4SLinus Torvalds 36183218ae14SYasunori Goto /* 36193218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 36203218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 36213218ae14SYasunori Goto */ 36223218ae14SYasunori Goto int kswapd_run(int nid) 36233218ae14SYasunori Goto { 36243218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 36253218ae14SYasunori Goto int ret = 0; 36263218ae14SYasunori Goto 36273218ae14SYasunori Goto if (pgdat->kswapd) 36283218ae14SYasunori Goto return 0; 36293218ae14SYasunori Goto 36303218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 36313218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 36323218ae14SYasunori Goto /* failure at boot is fatal */ 36333218ae14SYasunori Goto BUG_ON(system_state == SYSTEM_BOOTING); 3634d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 3635d5dc0ad9SGavin Shan ret = PTR_ERR(pgdat->kswapd); 3636d72515b8SXishi Qiu pgdat->kswapd = NULL; 36373218ae14SYasunori Goto } 36383218ae14SYasunori Goto return ret; 36393218ae14SYasunori Goto } 36403218ae14SYasunori Goto 36418fe23e05SDavid Rientjes /* 3642d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 3643bfc8c901SVladimir Davydov * hold mem_hotplug_begin/end(). 36448fe23e05SDavid Rientjes */ 36458fe23e05SDavid Rientjes void kswapd_stop(int nid) 36468fe23e05SDavid Rientjes { 36478fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 36488fe23e05SDavid Rientjes 3649d8adde17SJiang Liu if (kswapd) { 36508fe23e05SDavid Rientjes kthread_stop(kswapd); 3651d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 3652d8adde17SJiang Liu } 36538fe23e05SDavid Rientjes } 36548fe23e05SDavid Rientjes 36551da177e4SLinus Torvalds static int __init kswapd_init(void) 36561da177e4SLinus Torvalds { 3657517bbed9SSebastian Andrzej Siewior int nid, ret; 365869e05944SAndrew Morton 36591da177e4SLinus Torvalds swap_setup(); 366048fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 36613218ae14SYasunori Goto kswapd_run(nid); 3662517bbed9SSebastian Andrzej Siewior ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, 3663517bbed9SSebastian Andrzej Siewior "mm/vmscan:online", kswapd_cpu_online, 3664517bbed9SSebastian Andrzej Siewior NULL); 3665517bbed9SSebastian Andrzej Siewior WARN_ON(ret < 0); 36661da177e4SLinus Torvalds return 0; 36671da177e4SLinus Torvalds } 36681da177e4SLinus Torvalds 36691da177e4SLinus Torvalds module_init(kswapd_init) 36709eeff239SChristoph Lameter 36719eeff239SChristoph Lameter #ifdef CONFIG_NUMA 36729eeff239SChristoph Lameter /* 3673a5f5f91dSMel Gorman * Node reclaim mode 36749eeff239SChristoph Lameter * 3675a5f5f91dSMel Gorman * If non-zero call node_reclaim when the number of free pages falls below 36769eeff239SChristoph Lameter * the watermarks. 36779eeff239SChristoph Lameter */ 3678a5f5f91dSMel Gorman int node_reclaim_mode __read_mostly; 36799eeff239SChristoph Lameter 36801b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 36817d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ 36821b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 368395bbc0c7SZhihui Zhang #define RECLAIM_UNMAP (1<<2) /* Unmap pages during reclaim */ 36841b2ffb78SChristoph Lameter 36859eeff239SChristoph Lameter /* 3686a5f5f91dSMel Gorman * Priority for NODE_RECLAIM. This determines the fraction of pages 3687a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 3688a92f7126SChristoph Lameter * a zone. 3689a92f7126SChristoph Lameter */ 3690a5f5f91dSMel Gorman #define NODE_RECLAIM_PRIORITY 4 3691a92f7126SChristoph Lameter 36929eeff239SChristoph Lameter /* 3693a5f5f91dSMel Gorman * Percentage of pages in a zone that must be unmapped for node_reclaim to 36949614634fSChristoph Lameter * occur. 36959614634fSChristoph Lameter */ 36969614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 36979614634fSChristoph Lameter 36989614634fSChristoph Lameter /* 36990ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 37000ff38490SChristoph Lameter * slab reclaim needs to occur. 37010ff38490SChristoph Lameter */ 37020ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 37030ff38490SChristoph Lameter 370411fb9989SMel Gorman static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) 370590afa5deSMel Gorman { 370611fb9989SMel Gorman unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); 370711fb9989SMel Gorman unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + 370811fb9989SMel Gorman node_page_state(pgdat, NR_ACTIVE_FILE); 370990afa5deSMel Gorman 371090afa5deSMel Gorman /* 371190afa5deSMel Gorman * It's possible for there to be more file mapped pages than 371290afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 371390afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 371490afa5deSMel Gorman */ 371590afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 371690afa5deSMel Gorman } 371790afa5deSMel Gorman 371890afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 3719a5f5f91dSMel Gorman static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) 372090afa5deSMel Gorman { 3721d031a157SAlexandru Moise unsigned long nr_pagecache_reclaimable; 3722d031a157SAlexandru Moise unsigned long delta = 0; 372390afa5deSMel Gorman 372490afa5deSMel Gorman /* 372595bbc0c7SZhihui Zhang * If RECLAIM_UNMAP is set, then all file pages are considered 372690afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 372711fb9989SMel Gorman * pages like swapcache and node_unmapped_file_pages() provides 372890afa5deSMel Gorman * a better estimate 372990afa5deSMel Gorman */ 3730a5f5f91dSMel Gorman if (node_reclaim_mode & RECLAIM_UNMAP) 3731a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); 373290afa5deSMel Gorman else 3733a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); 373490afa5deSMel Gorman 373590afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 3736a5f5f91dSMel Gorman if (!(node_reclaim_mode & RECLAIM_WRITE)) 3737a5f5f91dSMel Gorman delta += node_page_state(pgdat, NR_FILE_DIRTY); 373890afa5deSMel Gorman 373990afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 374090afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 374190afa5deSMel Gorman delta = nr_pagecache_reclaimable; 374290afa5deSMel Gorman 374390afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 374490afa5deSMel Gorman } 374590afa5deSMel Gorman 37460ff38490SChristoph Lameter /* 3747a5f5f91dSMel Gorman * Try to free up some pages from this node through reclaim. 37489eeff239SChristoph Lameter */ 3749a5f5f91dSMel Gorman static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 37509eeff239SChristoph Lameter { 37517fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 375269e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 37539eeff239SChristoph Lameter struct task_struct *p = current; 37549eeff239SChristoph Lameter struct reclaim_state reclaim_state; 3755a5f5f91dSMel Gorman int classzone_idx = gfp_zone(gfp_mask); 3756179e9639SAndrew Morton struct scan_control sc = { 375762b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 375821caf2fcSMing Lei .gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)), 3759bd2f6199SJohannes Weiner .order = order, 3760a5f5f91dSMel Gorman .priority = NODE_RECLAIM_PRIORITY, 3761a5f5f91dSMel Gorman .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), 3762a5f5f91dSMel Gorman .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), 3763ee814fe2SJohannes Weiner .may_swap = 1, 3764a5f5f91dSMel Gorman .reclaim_idx = classzone_idx, 3765179e9639SAndrew Morton }; 37669eeff239SChristoph Lameter 37679eeff239SChristoph Lameter cond_resched(); 3768d4f7796eSChristoph Lameter /* 376995bbc0c7SZhihui Zhang * We need to be able to allocate from the reserves for RECLAIM_UNMAP 3770d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 377195bbc0c7SZhihui Zhang * and RECLAIM_UNMAP. 3772d4f7796eSChristoph Lameter */ 3773d4f7796eSChristoph Lameter p->flags |= PF_MEMALLOC | PF_SWAPWRITE; 377476ca542dSKOSAKI Motohiro lockdep_set_current_reclaim_state(gfp_mask); 37759eeff239SChristoph Lameter reclaim_state.reclaimed_slab = 0; 37769eeff239SChristoph Lameter p->reclaim_state = &reclaim_state; 3777c84db23cSChristoph Lameter 3778a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) { 3779a92f7126SChristoph Lameter /* 37800ff38490SChristoph Lameter * Free memory by calling shrink zone with increasing 37810ff38490SChristoph Lameter * priorities until we have enough memory freed. 3782a92f7126SChristoph Lameter */ 3783a92f7126SChristoph Lameter do { 3784970a39a3SMel Gorman shrink_node(pgdat, &sc); 37859e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 37860ff38490SChristoph Lameter } 3787a92f7126SChristoph Lameter 37889eeff239SChristoph Lameter p->reclaim_state = NULL; 3789d4f7796eSChristoph Lameter current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); 379076ca542dSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 3791a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 37929eeff239SChristoph Lameter } 3793179e9639SAndrew Morton 3794a5f5f91dSMel Gorman int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 3795179e9639SAndrew Morton { 3796d773ed6bSDavid Rientjes int ret; 3797179e9639SAndrew Morton 3798179e9639SAndrew Morton /* 3799a5f5f91dSMel Gorman * Node reclaim reclaims unmapped file backed pages and 38000ff38490SChristoph Lameter * slab pages if we are over the defined limits. 380134aa1330SChristoph Lameter * 38029614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 38039614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 3804a5f5f91dSMel Gorman * thrown out if the node is overallocated. So we do not reclaim 3805a5f5f91dSMel Gorman * if less than a specified percentage of the node is used by 38069614634fSChristoph Lameter * unmapped file backed pages. 3807179e9639SAndrew Morton */ 3808a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && 3809a5f5f91dSMel Gorman sum_zone_node_page_state(pgdat->node_id, NR_SLAB_RECLAIMABLE) <= pgdat->min_slab_pages) 3810a5f5f91dSMel Gorman return NODE_RECLAIM_FULL; 3811179e9639SAndrew Morton 3812a5f5f91dSMel Gorman if (!pgdat_reclaimable(pgdat)) 3813a5f5f91dSMel Gorman return NODE_RECLAIM_FULL; 3814d773ed6bSDavid Rientjes 3815179e9639SAndrew Morton /* 3816d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 3817179e9639SAndrew Morton */ 3818d0164adcSMel Gorman if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) 3819a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 3820179e9639SAndrew Morton 3821179e9639SAndrew Morton /* 3822a5f5f91dSMel Gorman * Only run node reclaim on the local node or on nodes that do not 3823179e9639SAndrew Morton * have associated processors. This will favor the local processor 3824179e9639SAndrew Morton * over remote processors and spread off node memory allocations 3825179e9639SAndrew Morton * as wide as possible. 3826179e9639SAndrew Morton */ 3827a5f5f91dSMel Gorman if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) 3828a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 3829d773ed6bSDavid Rientjes 3830a5f5f91dSMel Gorman if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) 3831a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 3832fa5e084eSMel Gorman 3833a5f5f91dSMel Gorman ret = __node_reclaim(pgdat, gfp_mask, order); 3834a5f5f91dSMel Gorman clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); 3835d773ed6bSDavid Rientjes 383624cf7251SMel Gorman if (!ret) 383724cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 383824cf7251SMel Gorman 3839d773ed6bSDavid Rientjes return ret; 3840179e9639SAndrew Morton } 38419eeff239SChristoph Lameter #endif 3842894bc310SLee Schermerhorn 3843894bc310SLee Schermerhorn /* 3844894bc310SLee Schermerhorn * page_evictable - test whether a page is evictable 3845894bc310SLee Schermerhorn * @page: the page to test 3846894bc310SLee Schermerhorn * 3847894bc310SLee Schermerhorn * Test whether page is evictable--i.e., should be placed on active/inactive 384839b5f29aSHugh Dickins * lists vs unevictable list. 3849894bc310SLee Schermerhorn * 3850894bc310SLee Schermerhorn * Reasons page might not be evictable: 3851ba9ddf49SLee Schermerhorn * (1) page's mapping marked unevictable 3852b291f000SNick Piggin * (2) page is part of an mlocked VMA 3853ba9ddf49SLee Schermerhorn * 3854894bc310SLee Schermerhorn */ 385539b5f29aSHugh Dickins int page_evictable(struct page *page) 3856894bc310SLee Schermerhorn { 385739b5f29aSHugh Dickins return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page); 3858894bc310SLee Schermerhorn } 385989e004eaSLee Schermerhorn 386085046579SHugh Dickins #ifdef CONFIG_SHMEM 386189e004eaSLee Schermerhorn /** 386224513264SHugh Dickins * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list 386324513264SHugh Dickins * @pages: array of pages to check 386424513264SHugh Dickins * @nr_pages: number of pages to check 386589e004eaSLee Schermerhorn * 386624513264SHugh Dickins * Checks pages for evictability and moves them to the appropriate lru list. 386785046579SHugh Dickins * 386885046579SHugh Dickins * This function is only used for SysV IPC SHM_UNLOCK. 386989e004eaSLee Schermerhorn */ 387024513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages) 387189e004eaSLee Schermerhorn { 3872925b7673SJohannes Weiner struct lruvec *lruvec; 3873785b99feSMel Gorman struct pglist_data *pgdat = NULL; 387424513264SHugh Dickins int pgscanned = 0; 387524513264SHugh Dickins int pgrescued = 0; 387689e004eaSLee Schermerhorn int i; 387789e004eaSLee Schermerhorn 387824513264SHugh Dickins for (i = 0; i < nr_pages; i++) { 387924513264SHugh Dickins struct page *page = pages[i]; 3880785b99feSMel Gorman struct pglist_data *pagepgdat = page_pgdat(page); 388189e004eaSLee Schermerhorn 388224513264SHugh Dickins pgscanned++; 3883785b99feSMel Gorman if (pagepgdat != pgdat) { 3884785b99feSMel Gorman if (pgdat) 3885785b99feSMel Gorman spin_unlock_irq(&pgdat->lru_lock); 3886785b99feSMel Gorman pgdat = pagepgdat; 3887785b99feSMel Gorman spin_lock_irq(&pgdat->lru_lock); 388889e004eaSLee Schermerhorn } 3889785b99feSMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 389089e004eaSLee Schermerhorn 389124513264SHugh Dickins if (!PageLRU(page) || !PageUnevictable(page)) 389224513264SHugh Dickins continue; 389389e004eaSLee Schermerhorn 389439b5f29aSHugh Dickins if (page_evictable(page)) { 389524513264SHugh Dickins enum lru_list lru = page_lru_base_type(page); 389624513264SHugh Dickins 3897309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 389824513264SHugh Dickins ClearPageUnevictable(page); 3899fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE); 3900fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 390124513264SHugh Dickins pgrescued++; 390289e004eaSLee Schermerhorn } 390389e004eaSLee Schermerhorn } 390424513264SHugh Dickins 3905785b99feSMel Gorman if (pgdat) { 390624513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 390724513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 3908785b99feSMel Gorman spin_unlock_irq(&pgdat->lru_lock); 390924513264SHugh Dickins } 391085046579SHugh Dickins } 391185046579SHugh Dickins #endif /* CONFIG_SHMEM */ 3912