1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/mm/vmscan.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Swap reorganised 29.12.95, Stephen Tweedie. 81da177e4SLinus Torvalds * kswapd added: 7.1.96 sct 91da177e4SLinus Torvalds * Removed kswapd_ctl limits, and swap out as many pages as needed 101da177e4SLinus Torvalds * to bring the system back to freepages.high: 2.4.97, Rik van Riel. 111da177e4SLinus Torvalds * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com). 121da177e4SLinus Torvalds * Multiqueue VM started 5.8.00, Rik van Riel. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds 15b1de0d13SMitchel Humpherys #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 16b1de0d13SMitchel Humpherys 171da177e4SLinus Torvalds #include <linux/mm.h> 185b3cc15aSIngo Molnar #include <linux/sched/mm.h> 191da177e4SLinus Torvalds #include <linux/module.h> 205a0e3ad6STejun Heo #include <linux/gfp.h> 211da177e4SLinus Torvalds #include <linux/kernel_stat.h> 221da177e4SLinus Torvalds #include <linux/swap.h> 231da177e4SLinus Torvalds #include <linux/pagemap.h> 241da177e4SLinus Torvalds #include <linux/init.h> 251da177e4SLinus Torvalds #include <linux/highmem.h> 2670ddf637SAnton Vorontsov #include <linux/vmpressure.h> 27e129b5c2SAndrew Morton #include <linux/vmstat.h> 281da177e4SLinus Torvalds #include <linux/file.h> 291da177e4SLinus Torvalds #include <linux/writeback.h> 301da177e4SLinus Torvalds #include <linux/blkdev.h> 311da177e4SLinus Torvalds #include <linux/buffer_head.h> /* for try_to_release_page(), 321da177e4SLinus Torvalds buffer_heads_over_limit */ 331da177e4SLinus Torvalds #include <linux/mm_inline.h> 341da177e4SLinus Torvalds #include <linux/backing-dev.h> 351da177e4SLinus Torvalds #include <linux/rmap.h> 361da177e4SLinus Torvalds #include <linux/topology.h> 371da177e4SLinus Torvalds #include <linux/cpu.h> 381da177e4SLinus Torvalds #include <linux/cpuset.h> 393e7d3449SMel Gorman #include <linux/compaction.h> 401da177e4SLinus Torvalds #include <linux/notifier.h> 411da177e4SLinus Torvalds #include <linux/rwsem.h> 42248a0301SRafael J. Wysocki #include <linux/delay.h> 433218ae14SYasunori Goto #include <linux/kthread.h> 447dfb7103SNigel Cunningham #include <linux/freezer.h> 4566e1707bSBalbir Singh #include <linux/memcontrol.h> 46873b4771SKeika Kobayashi #include <linux/delayacct.h> 47af936a16SLee Schermerhorn #include <linux/sysctl.h> 48929bea7cSKOSAKI Motohiro #include <linux/oom.h> 49268bb0ceSLinus Torvalds #include <linux/prefetch.h> 50b1de0d13SMitchel Humpherys #include <linux/printk.h> 51f9fe48beSRoss Zwisler #include <linux/dax.h> 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds #include <asm/tlbflush.h> 541da177e4SLinus Torvalds #include <asm/div64.h> 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds #include <linux/swapops.h> 57117aad1eSRafael Aquini #include <linux/balloon_compaction.h> 581da177e4SLinus Torvalds 590f8053a5SNick Piggin #include "internal.h" 600f8053a5SNick Piggin 6133906bc5SMel Gorman #define CREATE_TRACE_POINTS 6233906bc5SMel Gorman #include <trace/events/vmscan.h> 6333906bc5SMel Gorman 641da177e4SLinus Torvalds struct scan_control { 6522fba335SKOSAKI Motohiro /* How many pages shrink_list() should reclaim */ 6622fba335SKOSAKI Motohiro unsigned long nr_to_reclaim; 6722fba335SKOSAKI Motohiro 681da177e4SLinus Torvalds /* This context's GFP mask */ 696daa0e28SAl Viro gfp_t gfp_mask; 701da177e4SLinus Torvalds 71ee814fe2SJohannes Weiner /* Allocation order */ 725ad333ebSAndy Whitcroft int order; 7366e1707bSBalbir Singh 74ee814fe2SJohannes Weiner /* 75ee814fe2SJohannes Weiner * Nodemask of nodes allowed by the caller. If NULL, all nodes 76ee814fe2SJohannes Weiner * are scanned. 77ee814fe2SJohannes Weiner */ 78ee814fe2SJohannes Weiner nodemask_t *nodemask; 799e3b2f8cSKonstantin Khlebnikov 805f53e762SKOSAKI Motohiro /* 81f16015fbSJohannes Weiner * The memory cgroup that hit its limit and as a result is the 82f16015fbSJohannes Weiner * primary target of this reclaim invocation. 83f16015fbSJohannes Weiner */ 84f16015fbSJohannes Weiner struct mem_cgroup *target_mem_cgroup; 8566e1707bSBalbir Singh 86ee814fe2SJohannes Weiner /* Scan (total_size >> priority) pages at once */ 87ee814fe2SJohannes Weiner int priority; 88ee814fe2SJohannes Weiner 89b2e18757SMel Gorman /* The highest zone to isolate pages for reclaim from */ 90b2e18757SMel Gorman enum zone_type reclaim_idx; 91b2e18757SMel Gorman 921276ad68SJohannes Weiner /* Writepage batching in laptop mode; RECLAIM_WRITE */ 93ee814fe2SJohannes Weiner unsigned int may_writepage:1; 94ee814fe2SJohannes Weiner 95ee814fe2SJohannes Weiner /* Can mapped pages be reclaimed? */ 96ee814fe2SJohannes Weiner unsigned int may_unmap:1; 97ee814fe2SJohannes Weiner 98ee814fe2SJohannes Weiner /* Can pages be swapped as part of reclaim? */ 99ee814fe2SJohannes Weiner unsigned int may_swap:1; 100ee814fe2SJohannes Weiner 101d6622f63SYisheng Xie /* 102d6622f63SYisheng Xie * Cgroups are not reclaimed below their configured memory.low, 103d6622f63SYisheng Xie * unless we threaten to OOM. If any cgroups are skipped due to 104d6622f63SYisheng Xie * memory.low and nothing was reclaimed, go back for memory.low. 105d6622f63SYisheng Xie */ 106d6622f63SYisheng Xie unsigned int memcg_low_reclaim:1; 107d6622f63SYisheng Xie unsigned int memcg_low_skipped:1; 108241994edSJohannes Weiner 109ee814fe2SJohannes Weiner unsigned int hibernation_mode:1; 110ee814fe2SJohannes Weiner 111ee814fe2SJohannes Weiner /* One of the zones is ready for compaction */ 112ee814fe2SJohannes Weiner unsigned int compaction_ready:1; 113ee814fe2SJohannes Weiner 114ee814fe2SJohannes Weiner /* Incremented by the number of inactive pages that were scanned */ 115ee814fe2SJohannes Weiner unsigned long nr_scanned; 116ee814fe2SJohannes Weiner 117ee814fe2SJohannes Weiner /* Number of pages freed so far during a call to shrink_zones() */ 118ee814fe2SJohannes Weiner unsigned long nr_reclaimed; 119*d108c772SAndrey Ryabinin 120*d108c772SAndrey Ryabinin struct { 121*d108c772SAndrey Ryabinin unsigned int dirty; 122*d108c772SAndrey Ryabinin unsigned int unqueued_dirty; 123*d108c772SAndrey Ryabinin unsigned int congested; 124*d108c772SAndrey Ryabinin unsigned int writeback; 125*d108c772SAndrey Ryabinin unsigned int immediate; 126*d108c772SAndrey Ryabinin unsigned int file_taken; 127*d108c772SAndrey Ryabinin unsigned int taken; 128*d108c772SAndrey Ryabinin } nr; 1291da177e4SLinus Torvalds }; 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH 1321da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) \ 1331da177e4SLinus Torvalds do { \ 1341da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1351da177e4SLinus Torvalds struct page *prev; \ 1361da177e4SLinus Torvalds \ 1371da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1381da177e4SLinus Torvalds prefetch(&prev->_field); \ 1391da177e4SLinus Torvalds } \ 1401da177e4SLinus Torvalds } while (0) 1411da177e4SLinus Torvalds #else 1421da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) 1431da177e4SLinus Torvalds #endif 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1461da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1471da177e4SLinus Torvalds do { \ 1481da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1491da177e4SLinus Torvalds struct page *prev; \ 1501da177e4SLinus Torvalds \ 1511da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1521da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1531da177e4SLinus Torvalds } \ 1541da177e4SLinus Torvalds } while (0) 1551da177e4SLinus Torvalds #else 1561da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1571da177e4SLinus Torvalds #endif 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds /* 1601da177e4SLinus Torvalds * From 0 .. 100. Higher means more swappy. 1611da177e4SLinus Torvalds */ 1621da177e4SLinus Torvalds int vm_swappiness = 60; 163d0480be4SWang Sheng-Hui /* 164d0480be4SWang Sheng-Hui * The total number of pages which are beyond the high watermark within all 165d0480be4SWang Sheng-Hui * zones. 166d0480be4SWang Sheng-Hui */ 167d0480be4SWang Sheng-Hui unsigned long vm_total_pages; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1701da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1711da177e4SLinus Torvalds 172c255a458SAndrew Morton #ifdef CONFIG_MEMCG 17389b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 17489b5fae5SJohannes Weiner { 175f16015fbSJohannes Weiner return !sc->target_mem_cgroup; 17689b5fae5SJohannes Weiner } 17797c9341fSTejun Heo 17897c9341fSTejun Heo /** 17997c9341fSTejun Heo * sane_reclaim - is the usual dirty throttling mechanism operational? 18097c9341fSTejun Heo * @sc: scan_control in question 18197c9341fSTejun Heo * 18297c9341fSTejun Heo * The normal page dirty throttling mechanism in balance_dirty_pages() is 18397c9341fSTejun Heo * completely broken with the legacy memcg and direct stalling in 18497c9341fSTejun Heo * shrink_page_list() is used for throttling instead, which lacks all the 18597c9341fSTejun Heo * niceties such as fairness, adaptive pausing, bandwidth proportional 18697c9341fSTejun Heo * allocation and configurability. 18797c9341fSTejun Heo * 18897c9341fSTejun Heo * This function tests whether the vmscan currently in progress can assume 18997c9341fSTejun Heo * that the normal dirty throttling mechanism is operational. 19097c9341fSTejun Heo */ 19197c9341fSTejun Heo static bool sane_reclaim(struct scan_control *sc) 19297c9341fSTejun Heo { 19397c9341fSTejun Heo struct mem_cgroup *memcg = sc->target_mem_cgroup; 19497c9341fSTejun Heo 19597c9341fSTejun Heo if (!memcg) 19697c9341fSTejun Heo return true; 19797c9341fSTejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 19869234aceSLinus Torvalds if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) 19997c9341fSTejun Heo return true; 20097c9341fSTejun Heo #endif 20197c9341fSTejun Heo return false; 20297c9341fSTejun Heo } 20391a45470SKAMEZAWA Hiroyuki #else 20489b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 20589b5fae5SJohannes Weiner { 20689b5fae5SJohannes Weiner return true; 20789b5fae5SJohannes Weiner } 20897c9341fSTejun Heo 20997c9341fSTejun Heo static bool sane_reclaim(struct scan_control *sc) 21097c9341fSTejun Heo { 21197c9341fSTejun Heo return true; 21297c9341fSTejun Heo } 21391a45470SKAMEZAWA Hiroyuki #endif 21491a45470SKAMEZAWA Hiroyuki 2155a1c84b4SMel Gorman /* 2165a1c84b4SMel Gorman * This misses isolated pages which are not accounted for to save counters. 2175a1c84b4SMel Gorman * As the data only determines if reclaim or compaction continues, it is 2185a1c84b4SMel Gorman * not expected that isolated pages will be a dominating factor. 2195a1c84b4SMel Gorman */ 2205a1c84b4SMel Gorman unsigned long zone_reclaimable_pages(struct zone *zone) 2215a1c84b4SMel Gorman { 2225a1c84b4SMel Gorman unsigned long nr; 2235a1c84b4SMel Gorman 2245a1c84b4SMel Gorman nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) + 2255a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE); 2265a1c84b4SMel Gorman if (get_nr_swap_pages() > 0) 2275a1c84b4SMel Gorman nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) + 2285a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON); 2295a1c84b4SMel Gorman 2305a1c84b4SMel Gorman return nr; 2315a1c84b4SMel Gorman } 2325a1c84b4SMel Gorman 233fd538803SMichal Hocko /** 234fd538803SMichal Hocko * lruvec_lru_size - Returns the number of pages on the given LRU list. 235fd538803SMichal Hocko * @lruvec: lru vector 236fd538803SMichal Hocko * @lru: lru to use 237fd538803SMichal Hocko * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list) 238fd538803SMichal Hocko */ 239fd538803SMichal Hocko unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx) 240c9f299d9SKOSAKI Motohiro { 241fd538803SMichal Hocko unsigned long lru_size; 242fd538803SMichal Hocko int zid; 243a3d8e054SKOSAKI Motohiro 244fd538803SMichal Hocko if (!mem_cgroup_disabled()) 245fd538803SMichal Hocko lru_size = mem_cgroup_get_lru_size(lruvec, lru); 246fd538803SMichal Hocko else 247fd538803SMichal Hocko lru_size = node_page_state(lruvec_pgdat(lruvec), NR_LRU_BASE + lru); 248fd538803SMichal Hocko 249fd538803SMichal Hocko for (zid = zone_idx + 1; zid < MAX_NR_ZONES; zid++) { 250fd538803SMichal Hocko struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; 251fd538803SMichal Hocko unsigned long size; 252fd538803SMichal Hocko 253fd538803SMichal Hocko if (!managed_zone(zone)) 254fd538803SMichal Hocko continue; 255fd538803SMichal Hocko 256fd538803SMichal Hocko if (!mem_cgroup_disabled()) 257fd538803SMichal Hocko size = mem_cgroup_get_zone_lru_size(lruvec, lru, zid); 258fd538803SMichal Hocko else 259fd538803SMichal Hocko size = zone_page_state(&lruvec_pgdat(lruvec)->node_zones[zid], 260fd538803SMichal Hocko NR_ZONE_LRU_BASE + lru); 261fd538803SMichal Hocko lru_size -= min(size, lru_size); 262c9f299d9SKOSAKI Motohiro } 263c9f299d9SKOSAKI Motohiro 264fd538803SMichal Hocko return lru_size; 265b4536f0cSMichal Hocko 266b4536f0cSMichal Hocko } 267b4536f0cSMichal Hocko 2681da177e4SLinus Torvalds /* 2691d3d4437SGlauber Costa * Add a shrinker callback to be called from the vm. 2701da177e4SLinus Torvalds */ 2711d3d4437SGlauber Costa int register_shrinker(struct shrinker *shrinker) 2721da177e4SLinus Torvalds { 2731d3d4437SGlauber Costa size_t size = sizeof(*shrinker->nr_deferred); 2741d3d4437SGlauber Costa 2751d3d4437SGlauber Costa if (shrinker->flags & SHRINKER_NUMA_AWARE) 2761d3d4437SGlauber Costa size *= nr_node_ids; 2771d3d4437SGlauber Costa 2781d3d4437SGlauber Costa shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); 2791d3d4437SGlauber Costa if (!shrinker->nr_deferred) 2801d3d4437SGlauber Costa return -ENOMEM; 2811d3d4437SGlauber Costa 2821da177e4SLinus Torvalds down_write(&shrinker_rwsem); 2831da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 2841da177e4SLinus Torvalds up_write(&shrinker_rwsem); 2851d3d4437SGlauber Costa return 0; 2861da177e4SLinus Torvalds } 2878e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds /* 2901da177e4SLinus Torvalds * Remove one 2911da177e4SLinus Torvalds */ 2928e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 2931da177e4SLinus Torvalds { 294bb422a73STetsuo Handa if (!shrinker->nr_deferred) 295bb422a73STetsuo Handa return; 2961da177e4SLinus Torvalds down_write(&shrinker_rwsem); 2971da177e4SLinus Torvalds list_del(&shrinker->list); 2981da177e4SLinus Torvalds up_write(&shrinker_rwsem); 299ae393321SAndrew Vagin kfree(shrinker->nr_deferred); 300bb422a73STetsuo Handa shrinker->nr_deferred = NULL; 3011da177e4SLinus Torvalds } 3028e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds #define SHRINK_BATCH 128 3051d3d4437SGlauber Costa 306cb731d6cSVladimir Davydov static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, 3079092c71bSJosef Bacik struct shrinker *shrinker, int priority) 3081da177e4SLinus Torvalds { 30924f7c6b9SDave Chinner unsigned long freed = 0; 3101da177e4SLinus Torvalds unsigned long long delta; 311635697c6SKonstantin Khlebnikov long total_scan; 312d5bc5fd3SVladimir Davydov long freeable; 313acf92b48SDave Chinner long nr; 314acf92b48SDave Chinner long new_nr; 3151d3d4437SGlauber Costa int nid = shrinkctl->nid; 316e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 317e9299f50SDave Chinner : SHRINK_BATCH; 3185f33a080SShaohua Li long scanned = 0, next_deferred; 3191da177e4SLinus Torvalds 320d5bc5fd3SVladimir Davydov freeable = shrinker->count_objects(shrinker, shrinkctl); 321d5bc5fd3SVladimir Davydov if (freeable == 0) 3221d3d4437SGlauber Costa return 0; 323635697c6SKonstantin Khlebnikov 324acf92b48SDave Chinner /* 325acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 326acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 327acf92b48SDave Chinner * don't also do this scanning work. 328acf92b48SDave Chinner */ 3291d3d4437SGlauber Costa nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0); 330acf92b48SDave Chinner 331acf92b48SDave Chinner total_scan = nr; 3329092c71bSJosef Bacik delta = freeable >> priority; 3339092c71bSJosef Bacik delta *= 4; 3349092c71bSJosef Bacik do_div(delta, shrinker->seeks); 335acf92b48SDave Chinner total_scan += delta; 336acf92b48SDave Chinner if (total_scan < 0) { 3378612c663SPintu Kumar pr_err("shrink_slab: %pF negative objects to delete nr=%ld\n", 338a0b02131SDave Chinner shrinker->scan_objects, total_scan); 339d5bc5fd3SVladimir Davydov total_scan = freeable; 3405f33a080SShaohua Li next_deferred = nr; 3415f33a080SShaohua Li } else 3425f33a080SShaohua Li next_deferred = total_scan; 343ea164d73SAndrea Arcangeli 344ea164d73SAndrea Arcangeli /* 3453567b59aSDave Chinner * We need to avoid excessive windup on filesystem shrinkers 3463567b59aSDave Chinner * due to large numbers of GFP_NOFS allocations causing the 3473567b59aSDave Chinner * shrinkers to return -1 all the time. This results in a large 3483567b59aSDave Chinner * nr being built up so when a shrink that can do some work 3493567b59aSDave Chinner * comes along it empties the entire cache due to nr >>> 350d5bc5fd3SVladimir Davydov * freeable. This is bad for sustaining a working set in 3513567b59aSDave Chinner * memory. 3523567b59aSDave Chinner * 3533567b59aSDave Chinner * Hence only allow the shrinker to scan the entire cache when 3543567b59aSDave Chinner * a large delta change is calculated directly. 3553567b59aSDave Chinner */ 356d5bc5fd3SVladimir Davydov if (delta < freeable / 4) 357d5bc5fd3SVladimir Davydov total_scan = min(total_scan, freeable / 2); 3583567b59aSDave Chinner 3593567b59aSDave Chinner /* 360ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 361ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 362ea164d73SAndrea Arcangeli * freeable entries. 363ea164d73SAndrea Arcangeli */ 364d5bc5fd3SVladimir Davydov if (total_scan > freeable * 2) 365d5bc5fd3SVladimir Davydov total_scan = freeable * 2; 3661da177e4SLinus Torvalds 36724f7c6b9SDave Chinner trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, 3689092c71bSJosef Bacik freeable, delta, total_scan, priority); 36909576073SDave Chinner 3700b1fb40aSVladimir Davydov /* 3710b1fb40aSVladimir Davydov * Normally, we should not scan less than batch_size objects in one 3720b1fb40aSVladimir Davydov * pass to avoid too frequent shrinker calls, but if the slab has less 3730b1fb40aSVladimir Davydov * than batch_size objects in total and we are really tight on memory, 3740b1fb40aSVladimir Davydov * we will try to reclaim all available objects, otherwise we can end 3750b1fb40aSVladimir Davydov * up failing allocations although there are plenty of reclaimable 3760b1fb40aSVladimir Davydov * objects spread over several slabs with usage less than the 3770b1fb40aSVladimir Davydov * batch_size. 3780b1fb40aSVladimir Davydov * 3790b1fb40aSVladimir Davydov * We detect the "tight on memory" situations by looking at the total 3800b1fb40aSVladimir Davydov * number of objects we want to scan (total_scan). If it is greater 381d5bc5fd3SVladimir Davydov * than the total number of objects on slab (freeable), we must be 3820b1fb40aSVladimir Davydov * scanning at high prio and therefore should try to reclaim as much as 3830b1fb40aSVladimir Davydov * possible. 3840b1fb40aSVladimir Davydov */ 3850b1fb40aSVladimir Davydov while (total_scan >= batch_size || 386d5bc5fd3SVladimir Davydov total_scan >= freeable) { 38724f7c6b9SDave Chinner unsigned long ret; 3880b1fb40aSVladimir Davydov unsigned long nr_to_scan = min(batch_size, total_scan); 3891da177e4SLinus Torvalds 3900b1fb40aSVladimir Davydov shrinkctl->nr_to_scan = nr_to_scan; 391d460acb5SChris Wilson shrinkctl->nr_scanned = nr_to_scan; 39224f7c6b9SDave Chinner ret = shrinker->scan_objects(shrinker, shrinkctl); 39324f7c6b9SDave Chinner if (ret == SHRINK_STOP) 3941da177e4SLinus Torvalds break; 39524f7c6b9SDave Chinner freed += ret; 39624f7c6b9SDave Chinner 397d460acb5SChris Wilson count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned); 398d460acb5SChris Wilson total_scan -= shrinkctl->nr_scanned; 399d460acb5SChris Wilson scanned += shrinkctl->nr_scanned; 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds cond_resched(); 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds 4045f33a080SShaohua Li if (next_deferred >= scanned) 4055f33a080SShaohua Li next_deferred -= scanned; 4065f33a080SShaohua Li else 4075f33a080SShaohua Li next_deferred = 0; 408acf92b48SDave Chinner /* 409acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 410acf92b48SDave Chinner * manner that handles concurrent updates. If we exhausted the 411acf92b48SDave Chinner * scan, there is no need to do an update. 412acf92b48SDave Chinner */ 4135f33a080SShaohua Li if (next_deferred > 0) 4145f33a080SShaohua Li new_nr = atomic_long_add_return(next_deferred, 4151d3d4437SGlauber Costa &shrinker->nr_deferred[nid]); 41683aeeadaSKonstantin Khlebnikov else 4171d3d4437SGlauber Costa new_nr = atomic_long_read(&shrinker->nr_deferred[nid]); 418acf92b48SDave Chinner 419df9024a8SDave Hansen trace_mm_shrink_slab_end(shrinker, nid, freed, nr, new_nr, total_scan); 4201d3d4437SGlauber Costa return freed; 4211d3d4437SGlauber Costa } 4221d3d4437SGlauber Costa 4236b4f7799SJohannes Weiner /** 424cb731d6cSVladimir Davydov * shrink_slab - shrink slab caches 4256b4f7799SJohannes Weiner * @gfp_mask: allocation context 4266b4f7799SJohannes Weiner * @nid: node whose slab caches to target 427cb731d6cSVladimir Davydov * @memcg: memory cgroup whose slab caches to target 4289092c71bSJosef Bacik * @priority: the reclaim priority 4291d3d4437SGlauber Costa * 4306b4f7799SJohannes Weiner * Call the shrink functions to age shrinkable caches. 4311d3d4437SGlauber Costa * 4326b4f7799SJohannes Weiner * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, 4336b4f7799SJohannes Weiner * unaware shrinkers will receive a node id of 0 instead. 4341d3d4437SGlauber Costa * 435cb731d6cSVladimir Davydov * @memcg specifies the memory cgroup to target. If it is not NULL, 436cb731d6cSVladimir Davydov * only shrinkers with SHRINKER_MEMCG_AWARE set will be called to scan 4370fc9f58aSVladimir Davydov * objects from the memory cgroup specified. Otherwise, only unaware 4380fc9f58aSVladimir Davydov * shrinkers are called. 439cb731d6cSVladimir Davydov * 4409092c71bSJosef Bacik * @priority is sc->priority, we take the number of objects and >> by priority 4419092c71bSJosef Bacik * in order to get the scan target. 4421d3d4437SGlauber Costa * 4436b4f7799SJohannes Weiner * Returns the number of reclaimed slab objects. 4441d3d4437SGlauber Costa */ 445cb731d6cSVladimir Davydov static unsigned long shrink_slab(gfp_t gfp_mask, int nid, 446cb731d6cSVladimir Davydov struct mem_cgroup *memcg, 4479092c71bSJosef Bacik int priority) 4481d3d4437SGlauber Costa { 4491d3d4437SGlauber Costa struct shrinker *shrinker; 4501d3d4437SGlauber Costa unsigned long freed = 0; 4511d3d4437SGlauber Costa 4520fc9f58aSVladimir Davydov if (memcg && (!memcg_kmem_enabled() || !mem_cgroup_online(memcg))) 453cb731d6cSVladimir Davydov return 0; 454cb731d6cSVladimir Davydov 455e830c63aSTetsuo Handa if (!down_read_trylock(&shrinker_rwsem)) 4561d3d4437SGlauber Costa goto out; 4571d3d4437SGlauber Costa 4581d3d4437SGlauber Costa list_for_each_entry(shrinker, &shrinker_list, list) { 4596b4f7799SJohannes Weiner struct shrink_control sc = { 4606b4f7799SJohannes Weiner .gfp_mask = gfp_mask, 4616b4f7799SJohannes Weiner .nid = nid, 462cb731d6cSVladimir Davydov .memcg = memcg, 4636b4f7799SJohannes Weiner }; 4646b4f7799SJohannes Weiner 4650fc9f58aSVladimir Davydov /* 4660fc9f58aSVladimir Davydov * If kernel memory accounting is disabled, we ignore 4670fc9f58aSVladimir Davydov * SHRINKER_MEMCG_AWARE flag and call all shrinkers 4680fc9f58aSVladimir Davydov * passing NULL for memcg. 4690fc9f58aSVladimir Davydov */ 4700fc9f58aSVladimir Davydov if (memcg_kmem_enabled() && 4710fc9f58aSVladimir Davydov !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE)) 472cb731d6cSVladimir Davydov continue; 473cb731d6cSVladimir Davydov 4746b4f7799SJohannes Weiner if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) 4756b4f7799SJohannes Weiner sc.nid = 0; 4766b4f7799SJohannes Weiner 4779092c71bSJosef Bacik freed += do_shrink_slab(&sc, shrinker, priority); 478e496612cSMinchan Kim /* 479e496612cSMinchan Kim * Bail out if someone want to register a new shrinker to 480e496612cSMinchan Kim * prevent the regsitration from being stalled for long periods 481e496612cSMinchan Kim * by parallel ongoing shrinking. 482e496612cSMinchan Kim */ 483e496612cSMinchan Kim if (rwsem_is_contended(&shrinker_rwsem)) { 484e496612cSMinchan Kim freed = freed ? : 1; 485e496612cSMinchan Kim break; 486e496612cSMinchan Kim } 487ec97097bSVladimir Davydov } 4881d3d4437SGlauber Costa 4891da177e4SLinus Torvalds up_read(&shrinker_rwsem); 490f06590bdSMinchan Kim out: 491f06590bdSMinchan Kim cond_resched(); 49224f7c6b9SDave Chinner return freed; 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds 495cb731d6cSVladimir Davydov void drop_slab_node(int nid) 496cb731d6cSVladimir Davydov { 497cb731d6cSVladimir Davydov unsigned long freed; 498cb731d6cSVladimir Davydov 499cb731d6cSVladimir Davydov do { 500cb731d6cSVladimir Davydov struct mem_cgroup *memcg = NULL; 501cb731d6cSVladimir Davydov 502cb731d6cSVladimir Davydov freed = 0; 503cb731d6cSVladimir Davydov do { 5049092c71bSJosef Bacik freed += shrink_slab(GFP_KERNEL, nid, memcg, 0); 505cb731d6cSVladimir Davydov } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 506cb731d6cSVladimir Davydov } while (freed > 10); 507cb731d6cSVladimir Davydov } 508cb731d6cSVladimir Davydov 509cb731d6cSVladimir Davydov void drop_slab(void) 510cb731d6cSVladimir Davydov { 511cb731d6cSVladimir Davydov int nid; 512cb731d6cSVladimir Davydov 513cb731d6cSVladimir Davydov for_each_online_node(nid) 514cb731d6cSVladimir Davydov drop_slab_node(nid); 515cb731d6cSVladimir Davydov } 516cb731d6cSVladimir Davydov 5171da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 5181da177e4SLinus Torvalds { 519ceddc3a5SJohannes Weiner /* 520ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 521ceddc3a5SJohannes Weiner * that isolated the page, the page cache radix tree and 522ceddc3a5SJohannes Weiner * optional buffer heads at page->private. 523ceddc3a5SJohannes Weiner */ 524bd4c82c2SHuang Ying int radix_pins = PageTransHuge(page) && PageSwapCache(page) ? 525bd4c82c2SHuang Ying HPAGE_PMD_NR : 1; 526bd4c82c2SHuang Ying return page_count(page) - page_has_private(page) == 1 + radix_pins; 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds 529703c2708STejun Heo static int may_write_to_inode(struct inode *inode, struct scan_control *sc) 5301da177e4SLinus Torvalds { 531930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 5321da177e4SLinus Torvalds return 1; 533703c2708STejun Heo if (!inode_write_congested(inode)) 5341da177e4SLinus Torvalds return 1; 535703c2708STejun Heo if (inode_to_bdi(inode) == current->backing_dev_info) 5361da177e4SLinus Torvalds return 1; 5371da177e4SLinus Torvalds return 0; 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds /* 5411da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 5421da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 5431da177e4SLinus Torvalds * fsync(), msync() or close(). 5441da177e4SLinus Torvalds * 5451da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 5461da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 5471da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 5481da177e4SLinus Torvalds * 5491da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 5501da177e4SLinus Torvalds * __GFP_FS. 5511da177e4SLinus Torvalds */ 5521da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 5531da177e4SLinus Torvalds struct page *page, int error) 5541da177e4SLinus Torvalds { 5557eaceaccSJens Axboe lock_page(page); 5563e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 5573e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 5581da177e4SLinus Torvalds unlock_page(page); 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 56104e62a29SChristoph Lameter /* possible outcome of pageout() */ 56204e62a29SChristoph Lameter typedef enum { 56304e62a29SChristoph Lameter /* failed to write page out, page is locked */ 56404e62a29SChristoph Lameter PAGE_KEEP, 56504e62a29SChristoph Lameter /* move page to the active list, page is locked */ 56604e62a29SChristoph Lameter PAGE_ACTIVATE, 56704e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 56804e62a29SChristoph Lameter PAGE_SUCCESS, 56904e62a29SChristoph Lameter /* page is clean and locked */ 57004e62a29SChristoph Lameter PAGE_CLEAN, 57104e62a29SChristoph Lameter } pageout_t; 57204e62a29SChristoph Lameter 5731da177e4SLinus Torvalds /* 5741742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 5751742f19fSAndrew Morton * Calls ->writepage(). 5761da177e4SLinus Torvalds */ 577c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping, 5787d3579e8SKOSAKI Motohiro struct scan_control *sc) 5791da177e4SLinus Torvalds { 5801da177e4SLinus Torvalds /* 5811da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 5821da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 5831da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 5841da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 5851da177e4SLinus Torvalds * PagePrivate for that. 5861da177e4SLinus Torvalds * 5878174202bSAl Viro * If this process is currently in __generic_file_write_iter() against 5881da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 5891da177e4SLinus Torvalds * will block. 5901da177e4SLinus Torvalds * 5911da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 5921da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 5931da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 5941da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 5951da177e4SLinus Torvalds */ 5961da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 5971da177e4SLinus Torvalds return PAGE_KEEP; 5981da177e4SLinus Torvalds if (!mapping) { 5991da177e4SLinus Torvalds /* 6001da177e4SLinus Torvalds * Some data journaling orphaned pages can have 6011da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 6021da177e4SLinus Torvalds */ 603266cf658SDavid Howells if (page_has_private(page)) { 6041da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 6051da177e4SLinus Torvalds ClearPageDirty(page); 606b1de0d13SMitchel Humpherys pr_info("%s: orphaned page\n", __func__); 6071da177e4SLinus Torvalds return PAGE_CLEAN; 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds return PAGE_KEEP; 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 6131da177e4SLinus Torvalds return PAGE_ACTIVATE; 614703c2708STejun Heo if (!may_write_to_inode(mapping->host, sc)) 6151da177e4SLinus Torvalds return PAGE_KEEP; 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 6181da177e4SLinus Torvalds int res; 6191da177e4SLinus Torvalds struct writeback_control wbc = { 6201da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 6211da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 622111ebb6eSOGAWA Hirofumi .range_start = 0, 623111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 6241da177e4SLinus Torvalds .for_reclaim = 1, 6251da177e4SLinus Torvalds }; 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds SetPageReclaim(page); 6281da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 6291da177e4SLinus Torvalds if (res < 0) 6301da177e4SLinus Torvalds handle_write_error(mapping, page, res); 631994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 6321da177e4SLinus Torvalds ClearPageReclaim(page); 6331da177e4SLinus Torvalds return PAGE_ACTIVATE; 6341da177e4SLinus Torvalds } 635c661b078SAndy Whitcroft 6361da177e4SLinus Torvalds if (!PageWriteback(page)) { 6371da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 6381da177e4SLinus Torvalds ClearPageReclaim(page); 6391da177e4SLinus Torvalds } 6403aa23851Syalin wang trace_mm_vmscan_writepage(page); 641c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_WRITE); 6421da177e4SLinus Torvalds return PAGE_SUCCESS; 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds return PAGE_CLEAN; 6461da177e4SLinus Torvalds } 6471da177e4SLinus Torvalds 648a649fd92SAndrew Morton /* 649e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 650e286781dSNick Piggin * gets returned with a refcount of 0. 651a649fd92SAndrew Morton */ 652a528910eSJohannes Weiner static int __remove_mapping(struct address_space *mapping, struct page *page, 653a528910eSJohannes Weiner bool reclaimed) 65449d2e9ccSChristoph Lameter { 655c4843a75SGreg Thelen unsigned long flags; 656bd4c82c2SHuang Ying int refcount; 657c4843a75SGreg Thelen 65828e4d965SNick Piggin BUG_ON(!PageLocked(page)); 65928e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 66049d2e9ccSChristoph Lameter 661c4843a75SGreg Thelen spin_lock_irqsave(&mapping->tree_lock, flags); 66249d2e9ccSChristoph Lameter /* 6630fd0e6b0SNick Piggin * The non racy check for a busy page. 6640fd0e6b0SNick Piggin * 6650fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 6660fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 6670fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 6680fd0e6b0SNick Piggin * here, then the following race may occur: 6690fd0e6b0SNick Piggin * 6700fd0e6b0SNick Piggin * get_user_pages(&page); 6710fd0e6b0SNick Piggin * [user mapping goes away] 6720fd0e6b0SNick Piggin * write_to(page); 6730fd0e6b0SNick Piggin * !PageDirty(page) [good] 6740fd0e6b0SNick Piggin * SetPageDirty(page); 6750fd0e6b0SNick Piggin * put_page(page); 6760fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 6770fd0e6b0SNick Piggin * 6780fd0e6b0SNick Piggin * [oops, our write_to data is lost] 6790fd0e6b0SNick Piggin * 6800fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 6810fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 6820139aa7bSJoonsoo Kim * load is not satisfied before that of page->_refcount. 6830fd0e6b0SNick Piggin * 6840fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 6850fd0e6b0SNick Piggin * and thus under tree_lock, then this ordering is not required. 68649d2e9ccSChristoph Lameter */ 687bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page)) && PageSwapCache(page)) 688bd4c82c2SHuang Ying refcount = 1 + HPAGE_PMD_NR; 689bd4c82c2SHuang Ying else 690bd4c82c2SHuang Ying refcount = 2; 691bd4c82c2SHuang Ying if (!page_ref_freeze(page, refcount)) 69249d2e9ccSChristoph Lameter goto cannot_free; 693e286781dSNick Piggin /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ 694e286781dSNick Piggin if (unlikely(PageDirty(page))) { 695bd4c82c2SHuang Ying page_ref_unfreeze(page, refcount); 69649d2e9ccSChristoph Lameter goto cannot_free; 697e286781dSNick Piggin } 69849d2e9ccSChristoph Lameter 69949d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 70049d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 7010a31bc97SJohannes Weiner mem_cgroup_swapout(page, swap); 70249d2e9ccSChristoph Lameter __delete_from_swap_cache(page); 703c4843a75SGreg Thelen spin_unlock_irqrestore(&mapping->tree_lock, flags); 70475f6d6d2SMinchan Kim put_swap_page(page, swap); 705e286781dSNick Piggin } else { 7066072d13cSLinus Torvalds void (*freepage)(struct page *); 707a528910eSJohannes Weiner void *shadow = NULL; 7086072d13cSLinus Torvalds 7096072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 710a528910eSJohannes Weiner /* 711a528910eSJohannes Weiner * Remember a shadow entry for reclaimed file cache in 712a528910eSJohannes Weiner * order to detect refaults, thus thrashing, later on. 713a528910eSJohannes Weiner * 714a528910eSJohannes Weiner * But don't store shadows in an address space that is 715a528910eSJohannes Weiner * already exiting. This is not just an optizimation, 716a528910eSJohannes Weiner * inode reclaim needs to empty out the radix tree or 717a528910eSJohannes Weiner * the nodes are lost. Don't plant shadows behind its 718a528910eSJohannes Weiner * back. 719f9fe48beSRoss Zwisler * 720f9fe48beSRoss Zwisler * We also don't store shadows for DAX mappings because the 721f9fe48beSRoss Zwisler * only page cache pages found in these are zero pages 722f9fe48beSRoss Zwisler * covering holes, and because we don't want to mix DAX 723f9fe48beSRoss Zwisler * exceptional entries and shadow exceptional entries in the 724f9fe48beSRoss Zwisler * same page_tree. 725a528910eSJohannes Weiner */ 726a528910eSJohannes Weiner if (reclaimed && page_is_file_cache(page) && 727f9fe48beSRoss Zwisler !mapping_exiting(mapping) && !dax_mapping(mapping)) 728a528910eSJohannes Weiner shadow = workingset_eviction(mapping, page); 72962cccb8cSJohannes Weiner __delete_from_page_cache(page, shadow); 730c4843a75SGreg Thelen spin_unlock_irqrestore(&mapping->tree_lock, flags); 7316072d13cSLinus Torvalds 7326072d13cSLinus Torvalds if (freepage != NULL) 7336072d13cSLinus Torvalds freepage(page); 734e286781dSNick Piggin } 735e286781dSNick Piggin 73649d2e9ccSChristoph Lameter return 1; 73749d2e9ccSChristoph Lameter 73849d2e9ccSChristoph Lameter cannot_free: 739c4843a75SGreg Thelen spin_unlock_irqrestore(&mapping->tree_lock, flags); 74049d2e9ccSChristoph Lameter return 0; 74149d2e9ccSChristoph Lameter } 74249d2e9ccSChristoph Lameter 7431da177e4SLinus Torvalds /* 744e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 745e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 746e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 747e286781dSNick Piggin * this page. 748e286781dSNick Piggin */ 749e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 750e286781dSNick Piggin { 751a528910eSJohannes Weiner if (__remove_mapping(mapping, page, false)) { 752e286781dSNick Piggin /* 753e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 754e286781dSNick Piggin * drops the pagecache ref for us without requiring another 755e286781dSNick Piggin * atomic operation. 756e286781dSNick Piggin */ 757fe896d18SJoonsoo Kim page_ref_unfreeze(page, 1); 758e286781dSNick Piggin return 1; 759e286781dSNick Piggin } 760e286781dSNick Piggin return 0; 761e286781dSNick Piggin } 762e286781dSNick Piggin 763894bc310SLee Schermerhorn /** 764894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 765894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 766894bc310SLee Schermerhorn * 767894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 768894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 769894bc310SLee Schermerhorn * 770894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 771894bc310SLee Schermerhorn */ 772894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 773894bc310SLee Schermerhorn { 774c53954a0SMel Gorman lru_cache_add(page); 775894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 776894bc310SLee Schermerhorn } 777894bc310SLee Schermerhorn 778dfc8d636SJohannes Weiner enum page_references { 779dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 780dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 78164574746SJohannes Weiner PAGEREF_KEEP, 782dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 783dfc8d636SJohannes Weiner }; 784dfc8d636SJohannes Weiner 785dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 786dfc8d636SJohannes Weiner struct scan_control *sc) 787dfc8d636SJohannes Weiner { 78864574746SJohannes Weiner int referenced_ptes, referenced_page; 789dfc8d636SJohannes Weiner unsigned long vm_flags; 790dfc8d636SJohannes Weiner 791c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 792c3ac9a8aSJohannes Weiner &vm_flags); 79364574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 794dfc8d636SJohannes Weiner 795dfc8d636SJohannes Weiner /* 796dfc8d636SJohannes Weiner * Mlock lost the isolation race with us. Let try_to_unmap() 797dfc8d636SJohannes Weiner * move the page to the unevictable list. 798dfc8d636SJohannes Weiner */ 799dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 800dfc8d636SJohannes Weiner return PAGEREF_RECLAIM; 801dfc8d636SJohannes Weiner 80264574746SJohannes Weiner if (referenced_ptes) { 803e4898273SMichal Hocko if (PageSwapBacked(page)) 80464574746SJohannes Weiner return PAGEREF_ACTIVATE; 80564574746SJohannes Weiner /* 80664574746SJohannes Weiner * All mapped pages start out with page table 80764574746SJohannes Weiner * references from the instantiating fault, so we need 80864574746SJohannes Weiner * to look twice if a mapped file page is used more 80964574746SJohannes Weiner * than once. 81064574746SJohannes Weiner * 81164574746SJohannes Weiner * Mark it and spare it for another trip around the 81264574746SJohannes Weiner * inactive list. Another page table reference will 81364574746SJohannes Weiner * lead to its activation. 81464574746SJohannes Weiner * 81564574746SJohannes Weiner * Note: the mark is set for activated pages as well 81664574746SJohannes Weiner * so that recently deactivated but used pages are 81764574746SJohannes Weiner * quickly recovered. 81864574746SJohannes Weiner */ 81964574746SJohannes Weiner SetPageReferenced(page); 82064574746SJohannes Weiner 82134dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 822dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 823dfc8d636SJohannes Weiner 824c909e993SKonstantin Khlebnikov /* 825c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 826c909e993SKonstantin Khlebnikov */ 827c909e993SKonstantin Khlebnikov if (vm_flags & VM_EXEC) 828c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 829c909e993SKonstantin Khlebnikov 83064574746SJohannes Weiner return PAGEREF_KEEP; 83164574746SJohannes Weiner } 83264574746SJohannes Weiner 833dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 8342e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 835dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 83664574746SJohannes Weiner 83764574746SJohannes Weiner return PAGEREF_RECLAIM; 838dfc8d636SJohannes Weiner } 839dfc8d636SJohannes Weiner 840e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 841e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page, 842e2be15f6SMel Gorman bool *dirty, bool *writeback) 843e2be15f6SMel Gorman { 844b4597226SMel Gorman struct address_space *mapping; 845b4597226SMel Gorman 846e2be15f6SMel Gorman /* 847e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 848e2be15f6SMel Gorman * from reclaim context. Do not stall reclaim based on them 849e2be15f6SMel Gorman */ 850802a3a92SShaohua Li if (!page_is_file_cache(page) || 851802a3a92SShaohua Li (PageAnon(page) && !PageSwapBacked(page))) { 852e2be15f6SMel Gorman *dirty = false; 853e2be15f6SMel Gorman *writeback = false; 854e2be15f6SMel Gorman return; 855e2be15f6SMel Gorman } 856e2be15f6SMel Gorman 857e2be15f6SMel Gorman /* By default assume that the page flags are accurate */ 858e2be15f6SMel Gorman *dirty = PageDirty(page); 859e2be15f6SMel Gorman *writeback = PageWriteback(page); 860b4597226SMel Gorman 861b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 862b4597226SMel Gorman if (!page_has_private(page)) 863b4597226SMel Gorman return; 864b4597226SMel Gorman 865b4597226SMel Gorman mapping = page_mapping(page); 866b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 867b4597226SMel Gorman mapping->a_ops->is_dirty_writeback(page, dirty, writeback); 868e2be15f6SMel Gorman } 869e2be15f6SMel Gorman 8703c710c1aSMichal Hocko struct reclaim_stat { 8713c710c1aSMichal Hocko unsigned nr_dirty; 8723c710c1aSMichal Hocko unsigned nr_unqueued_dirty; 8733c710c1aSMichal Hocko unsigned nr_congested; 8743c710c1aSMichal Hocko unsigned nr_writeback; 8753c710c1aSMichal Hocko unsigned nr_immediate; 8765bccd166SMichal Hocko unsigned nr_activate; 8775bccd166SMichal Hocko unsigned nr_ref_keep; 8785bccd166SMichal Hocko unsigned nr_unmap_fail; 8793c710c1aSMichal Hocko }; 8803c710c1aSMichal Hocko 881e286781dSNick Piggin /* 8821742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 8831da177e4SLinus Torvalds */ 8841742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list, 885599d0c95SMel Gorman struct pglist_data *pgdat, 886f84f6e2bSMel Gorman struct scan_control *sc, 88702c6de8dSMinchan Kim enum ttu_flags ttu_flags, 8883c710c1aSMichal Hocko struct reclaim_stat *stat, 88902c6de8dSMinchan Kim bool force_reclaim) 8901da177e4SLinus Torvalds { 8911da177e4SLinus Torvalds LIST_HEAD(ret_pages); 892abe4c3b5SMel Gorman LIST_HEAD(free_pages); 8931da177e4SLinus Torvalds int pgactivate = 0; 8943c710c1aSMichal Hocko unsigned nr_unqueued_dirty = 0; 8953c710c1aSMichal Hocko unsigned nr_dirty = 0; 8963c710c1aSMichal Hocko unsigned nr_congested = 0; 8973c710c1aSMichal Hocko unsigned nr_reclaimed = 0; 8983c710c1aSMichal Hocko unsigned nr_writeback = 0; 8993c710c1aSMichal Hocko unsigned nr_immediate = 0; 9005bccd166SMichal Hocko unsigned nr_ref_keep = 0; 9015bccd166SMichal Hocko unsigned nr_unmap_fail = 0; 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds cond_resched(); 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds while (!list_empty(page_list)) { 9061da177e4SLinus Torvalds struct address_space *mapping; 9071da177e4SLinus Torvalds struct page *page; 9081da177e4SLinus Torvalds int may_enter_fs; 90902c6de8dSMinchan Kim enum page_references references = PAGEREF_RECLAIM_CLEAN; 910e2be15f6SMel Gorman bool dirty, writeback; 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds cond_resched(); 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds page = lru_to_page(page_list); 9151da177e4SLinus Torvalds list_del(&page->lru); 9161da177e4SLinus Torvalds 917529ae9aaSNick Piggin if (!trylock_page(page)) 9181da177e4SLinus Torvalds goto keep; 9191da177e4SLinus Torvalds 920309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds sc->nr_scanned++; 92380e43426SChristoph Lameter 92439b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) 925ad6b6704SMinchan Kim goto activate_locked; 926894bc310SLee Schermerhorn 927a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 92880e43426SChristoph Lameter goto keep_locked; 92980e43426SChristoph Lameter 9301da177e4SLinus Torvalds /* Double the slab pressure for mapped and swapcache pages */ 931802a3a92SShaohua Li if ((page_mapped(page) || PageSwapCache(page)) && 932802a3a92SShaohua Li !(PageAnon(page) && !PageSwapBacked(page))) 9331da177e4SLinus Torvalds sc->nr_scanned++; 9341da177e4SLinus Torvalds 935c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 936c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 937c661b078SAndy Whitcroft 938e62e384eSMichal Hocko /* 939894befecSAndrey Ryabinin * The number of dirty pages determines if a node is marked 940e2be15f6SMel Gorman * reclaim_congested which affects wait_iff_congested. kswapd 941e2be15f6SMel Gorman * will stall and start writing pages if the tail of the LRU 942e2be15f6SMel Gorman * is all dirty unqueued pages. 943e2be15f6SMel Gorman */ 944e2be15f6SMel Gorman page_check_dirty_writeback(page, &dirty, &writeback); 945e2be15f6SMel Gorman if (dirty || writeback) 946e2be15f6SMel Gorman nr_dirty++; 947e2be15f6SMel Gorman 948e2be15f6SMel Gorman if (dirty && !writeback) 949e2be15f6SMel Gorman nr_unqueued_dirty++; 950e2be15f6SMel Gorman 951d04e8acdSMel Gorman /* 952d04e8acdSMel Gorman * Treat this page as congested if the underlying BDI is or if 953d04e8acdSMel Gorman * pages are cycling through the LRU so quickly that the 954d04e8acdSMel Gorman * pages marked for immediate reclaim are making it to the 955d04e8acdSMel Gorman * end of the LRU a second time. 956d04e8acdSMel Gorman */ 957e2be15f6SMel Gorman mapping = page_mapping(page); 9581da58ee2SJamie Liu if (((dirty || writeback) && mapping && 959703c2708STejun Heo inode_write_congested(mapping->host)) || 960d04e8acdSMel Gorman (writeback && PageReclaim(page))) 961e2be15f6SMel Gorman nr_congested++; 962e2be15f6SMel Gorman 963e2be15f6SMel Gorman /* 964283aba9fSMel Gorman * If a page at the tail of the LRU is under writeback, there 965283aba9fSMel Gorman * are three cases to consider. 966e62e384eSMichal Hocko * 967283aba9fSMel Gorman * 1) If reclaim is encountering an excessive number of pages 968283aba9fSMel Gorman * under writeback and this page is both under writeback and 969283aba9fSMel Gorman * PageReclaim then it indicates that pages are being queued 970283aba9fSMel Gorman * for IO but are being recycled through the LRU before the 971283aba9fSMel Gorman * IO can complete. Waiting on the page itself risks an 972283aba9fSMel Gorman * indefinite stall if it is impossible to writeback the 973283aba9fSMel Gorman * page due to IO error or disconnected storage so instead 974b1a6f21eSMel Gorman * note that the LRU is being scanned too quickly and the 975b1a6f21eSMel Gorman * caller can stall after page list has been processed. 976c3b94f44SHugh Dickins * 97797c9341fSTejun Heo * 2) Global or new memcg reclaim encounters a page that is 978ecf5fc6eSMichal Hocko * not marked for immediate reclaim, or the caller does not 979ecf5fc6eSMichal Hocko * have __GFP_FS (or __GFP_IO if it's simply going to swap, 980ecf5fc6eSMichal Hocko * not to fs). In this case mark the page for immediate 98197c9341fSTejun Heo * reclaim and continue scanning. 982283aba9fSMel Gorman * 983ecf5fc6eSMichal Hocko * Require may_enter_fs because we would wait on fs, which 984ecf5fc6eSMichal Hocko * may not have submitted IO yet. And the loop driver might 985283aba9fSMel Gorman * enter reclaim, and deadlock if it waits on a page for 986283aba9fSMel Gorman * which it is needed to do the write (loop masks off 987283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 988283aba9fSMel Gorman * would probably show more reasons. 989283aba9fSMel Gorman * 9907fadc820SHugh Dickins * 3) Legacy memcg encounters a page that is already marked 991283aba9fSMel Gorman * PageReclaim. memcg does not have any dirty pages 992283aba9fSMel Gorman * throttling so we could easily OOM just because too many 993283aba9fSMel Gorman * pages are in writeback and there is nothing else to 994283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 995c55e8d03SJohannes Weiner * 996c55e8d03SJohannes Weiner * In cases 1) and 2) we activate the pages to get them out of 997c55e8d03SJohannes Weiner * the way while we continue scanning for clean pages on the 998c55e8d03SJohannes Weiner * inactive list and refilling from the active list. The 999c55e8d03SJohannes Weiner * observation here is that waiting for disk writes is more 1000c55e8d03SJohannes Weiner * expensive than potentially causing reloads down the line. 1001c55e8d03SJohannes Weiner * Since they're marked for immediate reclaim, they won't put 1002c55e8d03SJohannes Weiner * memory pressure on the cache working set any longer than it 1003c55e8d03SJohannes Weiner * takes to write them to disk. 1004e62e384eSMichal Hocko */ 1005283aba9fSMel Gorman if (PageWriteback(page)) { 1006283aba9fSMel Gorman /* Case 1 above */ 1007283aba9fSMel Gorman if (current_is_kswapd() && 1008283aba9fSMel Gorman PageReclaim(page) && 1009599d0c95SMel Gorman test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { 1010b1a6f21eSMel Gorman nr_immediate++; 1011c55e8d03SJohannes Weiner goto activate_locked; 1012283aba9fSMel Gorman 1013283aba9fSMel Gorman /* Case 2 above */ 101497c9341fSTejun Heo } else if (sane_reclaim(sc) || 1015ecf5fc6eSMichal Hocko !PageReclaim(page) || !may_enter_fs) { 1016c3b94f44SHugh Dickins /* 1017c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 1018c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 1019c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 1020c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 1021c3b94f44SHugh Dickins * enough to care. What we do want is for this 1022c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 1023c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 1024c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 1025c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 1026c3b94f44SHugh Dickins */ 1027c3b94f44SHugh Dickins SetPageReclaim(page); 102892df3a72SMel Gorman nr_writeback++; 1029c55e8d03SJohannes Weiner goto activate_locked; 1030283aba9fSMel Gorman 1031283aba9fSMel Gorman /* Case 3 above */ 1032283aba9fSMel Gorman } else { 10337fadc820SHugh Dickins unlock_page(page); 1034c3b94f44SHugh Dickins wait_on_page_writeback(page); 10357fadc820SHugh Dickins /* then go back and try same page again */ 10367fadc820SHugh Dickins list_add_tail(&page->lru, page_list); 10377fadc820SHugh Dickins continue; 1038e62e384eSMichal Hocko } 1039283aba9fSMel Gorman } 10401da177e4SLinus Torvalds 104102c6de8dSMinchan Kim if (!force_reclaim) 10426a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 104302c6de8dSMinchan Kim 1044dfc8d636SJohannes Weiner switch (references) { 1045dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 10461da177e4SLinus Torvalds goto activate_locked; 104764574746SJohannes Weiner case PAGEREF_KEEP: 10485bccd166SMichal Hocko nr_ref_keep++; 104964574746SJohannes Weiner goto keep_locked; 1050dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 1051dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 1052dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 1053dfc8d636SJohannes Weiner } 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds /* 10561da177e4SLinus Torvalds * Anonymous process memory has backing store? 10571da177e4SLinus Torvalds * Try to allocate it some swap space here. 1058802a3a92SShaohua Li * Lazyfree page could be freed directly 10591da177e4SLinus Torvalds */ 1060bd4c82c2SHuang Ying if (PageAnon(page) && PageSwapBacked(page)) { 1061bd4c82c2SHuang Ying if (!PageSwapCache(page)) { 106263eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 106363eb6b93SHugh Dickins goto keep_locked; 1064747552b1SHuang Ying if (PageTransHuge(page)) { 1065b8f593cdSHuang Ying /* cannot split THP, skip it */ 1066747552b1SHuang Ying if (!can_split_huge_page(page, NULL)) 1067b8f593cdSHuang Ying goto activate_locked; 1068747552b1SHuang Ying /* 1069747552b1SHuang Ying * Split pages without a PMD map right 1070747552b1SHuang Ying * away. Chances are some or all of the 1071747552b1SHuang Ying * tail pages can be freed without IO. 1072747552b1SHuang Ying */ 1073747552b1SHuang Ying if (!compound_mapcount(page) && 1074bd4c82c2SHuang Ying split_huge_page_to_list(page, 1075bd4c82c2SHuang Ying page_list)) 1076747552b1SHuang Ying goto activate_locked; 1077747552b1SHuang Ying } 10780f074658SMinchan Kim if (!add_to_swap(page)) { 10790f074658SMinchan Kim if (!PageTransHuge(page)) 10801da177e4SLinus Torvalds goto activate_locked; 1081bd4c82c2SHuang Ying /* Fallback to swap normal pages */ 1082bd4c82c2SHuang Ying if (split_huge_page_to_list(page, 1083bd4c82c2SHuang Ying page_list)) 10840f074658SMinchan Kim goto activate_locked; 1085fe490cc0SHuang Ying #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1086fe490cc0SHuang Ying count_vm_event(THP_SWPOUT_FALLBACK); 1087fe490cc0SHuang Ying #endif 10880f074658SMinchan Kim if (!add_to_swap(page)) 10890f074658SMinchan Kim goto activate_locked; 10900f074658SMinchan Kim } 10910f074658SMinchan Kim 109263eb6b93SHugh Dickins may_enter_fs = 1; 10931da177e4SLinus Torvalds 1094e2be15f6SMel Gorman /* Adding to swap updated mapping */ 10951da177e4SLinus Torvalds mapping = page_mapping(page); 1096bd4c82c2SHuang Ying } 10977751b2daSKirill A. Shutemov } else if (unlikely(PageTransHuge(page))) { 10987751b2daSKirill A. Shutemov /* Split file THP */ 10997751b2daSKirill A. Shutemov if (split_huge_page_to_list(page, page_list)) 11007751b2daSKirill A. Shutemov goto keep_locked; 1101e2be15f6SMel Gorman } 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds /* 11041da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 11051da177e4SLinus Torvalds * processes. Try to unmap it here. 11061da177e4SLinus Torvalds */ 1107802a3a92SShaohua Li if (page_mapped(page)) { 1108bd4c82c2SHuang Ying enum ttu_flags flags = ttu_flags | TTU_BATCH_FLUSH; 1109bd4c82c2SHuang Ying 1110bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page))) 1111bd4c82c2SHuang Ying flags |= TTU_SPLIT_HUGE_PMD; 1112bd4c82c2SHuang Ying if (!try_to_unmap(page, flags)) { 11135bccd166SMichal Hocko nr_unmap_fail++; 11141da177e4SLinus Torvalds goto activate_locked; 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds if (PageDirty(page)) { 1119ee72886dSMel Gorman /* 11204eda4823SJohannes Weiner * Only kswapd can writeback filesystem pages 11214eda4823SJohannes Weiner * to avoid risk of stack overflow. But avoid 11224eda4823SJohannes Weiner * injecting inefficient single-page IO into 11234eda4823SJohannes Weiner * flusher writeback as much as possible: only 11244eda4823SJohannes Weiner * write pages when we've encountered many 11254eda4823SJohannes Weiner * dirty pages, and when we've already scanned 11264eda4823SJohannes Weiner * the rest of the LRU for clean pages and see 11274eda4823SJohannes Weiner * the same dirty pages again (PageReclaim). 1128ee72886dSMel Gorman */ 1129f84f6e2bSMel Gorman if (page_is_file_cache(page) && 11304eda4823SJohannes Weiner (!current_is_kswapd() || !PageReclaim(page) || 1131599d0c95SMel Gorman !test_bit(PGDAT_DIRTY, &pgdat->flags))) { 113249ea7eb6SMel Gorman /* 113349ea7eb6SMel Gorman * Immediately reclaim when written back. 113449ea7eb6SMel Gorman * Similar in principal to deactivate_page() 113549ea7eb6SMel Gorman * except we already have the page isolated 113649ea7eb6SMel Gorman * and know it's dirty 113749ea7eb6SMel Gorman */ 1138c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_IMMEDIATE); 113949ea7eb6SMel Gorman SetPageReclaim(page); 114049ea7eb6SMel Gorman 1141c55e8d03SJohannes Weiner goto activate_locked; 1142ee72886dSMel Gorman } 1143ee72886dSMel Gorman 1144dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 11451da177e4SLinus Torvalds goto keep_locked; 11464dd4b920SAndrew Morton if (!may_enter_fs) 11471da177e4SLinus Torvalds goto keep_locked; 114852a8363eSChristoph Lameter if (!sc->may_writepage) 11491da177e4SLinus Torvalds goto keep_locked; 11501da177e4SLinus Torvalds 1151d950c947SMel Gorman /* 1152d950c947SMel Gorman * Page is dirty. Flush the TLB if a writable entry 1153d950c947SMel Gorman * potentially exists to avoid CPU writes after IO 1154d950c947SMel Gorman * starts and then write it out here. 1155d950c947SMel Gorman */ 1156d950c947SMel Gorman try_to_unmap_flush_dirty(); 11577d3579e8SKOSAKI Motohiro switch (pageout(page, mapping, sc)) { 11581da177e4SLinus Torvalds case PAGE_KEEP: 11591da177e4SLinus Torvalds goto keep_locked; 11601da177e4SLinus Torvalds case PAGE_ACTIVATE: 11611da177e4SLinus Torvalds goto activate_locked; 11621da177e4SLinus Torvalds case PAGE_SUCCESS: 11637d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 116441ac1999SMel Gorman goto keep; 11657d3579e8SKOSAKI Motohiro if (PageDirty(page)) 11661da177e4SLinus Torvalds goto keep; 11677d3579e8SKOSAKI Motohiro 11681da177e4SLinus Torvalds /* 11691da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 11701da177e4SLinus Torvalds * ahead and try to reclaim the page. 11711da177e4SLinus Torvalds */ 1172529ae9aaSNick Piggin if (!trylock_page(page)) 11731da177e4SLinus Torvalds goto keep; 11741da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 11751da177e4SLinus Torvalds goto keep_locked; 11761da177e4SLinus Torvalds mapping = page_mapping(page); 11771da177e4SLinus Torvalds case PAGE_CLEAN: 11781da177e4SLinus Torvalds ; /* try to free the page below */ 11791da177e4SLinus Torvalds } 11801da177e4SLinus Torvalds } 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds /* 11831da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 11841da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 11851da177e4SLinus Torvalds * the page as well. 11861da177e4SLinus Torvalds * 11871da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 11881da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 11891da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 11901da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 11911da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 11921da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 11931da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 11941da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 11951da177e4SLinus Torvalds * 11961da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 11971da177e4SLinus Torvalds * the pages which were not successfully invalidated in 11981da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 11991da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 12001da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 12011da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 12021da177e4SLinus Torvalds */ 1203266cf658SDavid Howells if (page_has_private(page)) { 12041da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 12051da177e4SLinus Torvalds goto activate_locked; 1206e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 1207e286781dSNick Piggin unlock_page(page); 1208e286781dSNick Piggin if (put_page_testzero(page)) 12091da177e4SLinus Torvalds goto free_it; 1210e286781dSNick Piggin else { 1211e286781dSNick Piggin /* 1212e286781dSNick Piggin * rare race with speculative reference. 1213e286781dSNick Piggin * the speculative reference will free 1214e286781dSNick Piggin * this page shortly, so we may 1215e286781dSNick Piggin * increment nr_reclaimed here (and 1216e286781dSNick Piggin * leave it off the LRU). 1217e286781dSNick Piggin */ 1218e286781dSNick Piggin nr_reclaimed++; 1219e286781dSNick Piggin continue; 1220e286781dSNick Piggin } 1221e286781dSNick Piggin } 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds 1224802a3a92SShaohua Li if (PageAnon(page) && !PageSwapBacked(page)) { 1225802a3a92SShaohua Li /* follow __remove_mapping for reference */ 1226802a3a92SShaohua Li if (!page_ref_freeze(page, 1)) 122749d2e9ccSChristoph Lameter goto keep_locked; 1228802a3a92SShaohua Li if (PageDirty(page)) { 1229802a3a92SShaohua Li page_ref_unfreeze(page, 1); 1230802a3a92SShaohua Li goto keep_locked; 1231802a3a92SShaohua Li } 12321da177e4SLinus Torvalds 1233802a3a92SShaohua Li count_vm_event(PGLAZYFREED); 12342262185cSRoman Gushchin count_memcg_page_event(page, PGLAZYFREED); 1235802a3a92SShaohua Li } else if (!mapping || !__remove_mapping(mapping, page, true)) 1236802a3a92SShaohua Li goto keep_locked; 1237a978d6f5SNick Piggin /* 1238a978d6f5SNick Piggin * At this point, we have no other references and there is 1239a978d6f5SNick Piggin * no way to pick any more up (removed from LRU, removed 1240a978d6f5SNick Piggin * from pagecache). Can use non-atomic bitops now (and 1241a978d6f5SNick Piggin * we obviously don't have to worry about waking up a process 1242a978d6f5SNick Piggin * waiting on the page lock, because there are no references. 1243a978d6f5SNick Piggin */ 124448c935adSKirill A. Shutemov __ClearPageLocked(page); 1245e286781dSNick Piggin free_it: 124605ff5137SAndrew Morton nr_reclaimed++; 1247abe4c3b5SMel Gorman 1248abe4c3b5SMel Gorman /* 1249abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1250abe4c3b5SMel Gorman * appear not as the counts should be low 1251abe4c3b5SMel Gorman */ 1252bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page))) { 1253bd4c82c2SHuang Ying mem_cgroup_uncharge(page); 1254bd4c82c2SHuang Ying (*get_compound_page_dtor(page))(page); 1255bd4c82c2SHuang Ying } else 1256abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 12571da177e4SLinus Torvalds continue; 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds activate_locked: 126068a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 1261ad6b6704SMinchan Kim if (PageSwapCache(page) && (mem_cgroup_swap_full(page) || 1262ad6b6704SMinchan Kim PageMlocked(page))) 1263a2c43eedSHugh Dickins try_to_free_swap(page); 1264309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 1265ad6b6704SMinchan Kim if (!PageMlocked(page)) { 12661da177e4SLinus Torvalds SetPageActive(page); 12671da177e4SLinus Torvalds pgactivate++; 12682262185cSRoman Gushchin count_memcg_page_event(page, PGACTIVATE); 1269ad6b6704SMinchan Kim } 12701da177e4SLinus Torvalds keep_locked: 12711da177e4SLinus Torvalds unlock_page(page); 12721da177e4SLinus Torvalds keep: 12731da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 1274309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); 12751da177e4SLinus Torvalds } 1276abe4c3b5SMel Gorman 1277747db954SJohannes Weiner mem_cgroup_uncharge_list(&free_pages); 127872b252aeSMel Gorman try_to_unmap_flush(); 12792d4894b5SMel Gorman free_unref_page_list(&free_pages); 1280abe4c3b5SMel Gorman 12811da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1282f8891e5eSChristoph Lameter count_vm_events(PGACTIVATE, pgactivate); 12830a31bc97SJohannes Weiner 12843c710c1aSMichal Hocko if (stat) { 12853c710c1aSMichal Hocko stat->nr_dirty = nr_dirty; 12863c710c1aSMichal Hocko stat->nr_congested = nr_congested; 12873c710c1aSMichal Hocko stat->nr_unqueued_dirty = nr_unqueued_dirty; 12883c710c1aSMichal Hocko stat->nr_writeback = nr_writeback; 12893c710c1aSMichal Hocko stat->nr_immediate = nr_immediate; 12905bccd166SMichal Hocko stat->nr_activate = pgactivate; 12915bccd166SMichal Hocko stat->nr_ref_keep = nr_ref_keep; 12925bccd166SMichal Hocko stat->nr_unmap_fail = nr_unmap_fail; 12933c710c1aSMichal Hocko } 129405ff5137SAndrew Morton return nr_reclaimed; 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds 129702c6de8dSMinchan Kim unsigned long reclaim_clean_pages_from_list(struct zone *zone, 129802c6de8dSMinchan Kim struct list_head *page_list) 129902c6de8dSMinchan Kim { 130002c6de8dSMinchan Kim struct scan_control sc = { 130102c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 130202c6de8dSMinchan Kim .priority = DEF_PRIORITY, 130302c6de8dSMinchan Kim .may_unmap = 1, 130402c6de8dSMinchan Kim }; 13053c710c1aSMichal Hocko unsigned long ret; 130602c6de8dSMinchan Kim struct page *page, *next; 130702c6de8dSMinchan Kim LIST_HEAD(clean_pages); 130802c6de8dSMinchan Kim 130902c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 1310117aad1eSRafael Aquini if (page_is_file_cache(page) && !PageDirty(page) && 1311b1123ea6SMinchan Kim !__PageMovable(page)) { 131202c6de8dSMinchan Kim ClearPageActive(page); 131302c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 131402c6de8dSMinchan Kim } 131502c6de8dSMinchan Kim } 131602c6de8dSMinchan Kim 1317599d0c95SMel Gorman ret = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc, 1318a128ca71SShaohua Li TTU_IGNORE_ACCESS, NULL, true); 131902c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 1320599d0c95SMel Gorman mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, -ret); 132102c6de8dSMinchan Kim return ret; 132202c6de8dSMinchan Kim } 132302c6de8dSMinchan Kim 13245ad333ebSAndy Whitcroft /* 13255ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 13265ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 13275ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 13285ad333ebSAndy Whitcroft * 13295ad333ebSAndy Whitcroft * page: page to consider 13305ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 13315ad333ebSAndy Whitcroft * 13325ad333ebSAndy Whitcroft * returns 0 on success, -ve errno on failure. 13335ad333ebSAndy Whitcroft */ 1334f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode) 13355ad333ebSAndy Whitcroft { 13365ad333ebSAndy Whitcroft int ret = -EINVAL; 13375ad333ebSAndy Whitcroft 13385ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 13395ad333ebSAndy Whitcroft if (!PageLRU(page)) 13405ad333ebSAndy Whitcroft return ret; 13415ad333ebSAndy Whitcroft 1342e46a2879SMinchan Kim /* Compaction should not handle unevictable pages but CMA can do so */ 1343e46a2879SMinchan Kim if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) 1344894bc310SLee Schermerhorn return ret; 1345894bc310SLee Schermerhorn 13465ad333ebSAndy Whitcroft ret = -EBUSY; 134708e552c6SKAMEZAWA Hiroyuki 1348c8244935SMel Gorman /* 1349c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 1350c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 1351c8244935SMel Gorman * blocking - clean pages for the most part. 1352c8244935SMel Gorman * 1353c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 1354c8244935SMel Gorman * that it is possible to migrate without blocking 1355c8244935SMel Gorman */ 13561276ad68SJohannes Weiner if (mode & ISOLATE_ASYNC_MIGRATE) { 1357c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 1358c8244935SMel Gorman if (PageWriteback(page)) 135939deaf85SMinchan Kim return ret; 136039deaf85SMinchan Kim 1361c8244935SMel Gorman if (PageDirty(page)) { 1362c8244935SMel Gorman struct address_space *mapping; 136369d763fcSMel Gorman bool migrate_dirty; 1364c8244935SMel Gorman 1365c8244935SMel Gorman /* 1366c8244935SMel Gorman * Only pages without mappings or that have a 1367c8244935SMel Gorman * ->migratepage callback are possible to migrate 136869d763fcSMel Gorman * without blocking. However, we can be racing with 136969d763fcSMel Gorman * truncation so it's necessary to lock the page 137069d763fcSMel Gorman * to stabilise the mapping as truncation holds 137169d763fcSMel Gorman * the page lock until after the page is removed 137269d763fcSMel Gorman * from the page cache. 1373c8244935SMel Gorman */ 137469d763fcSMel Gorman if (!trylock_page(page)) 137569d763fcSMel Gorman return ret; 137669d763fcSMel Gorman 1377c8244935SMel Gorman mapping = page_mapping(page); 137869d763fcSMel Gorman migrate_dirty = mapping && mapping->a_ops->migratepage; 137969d763fcSMel Gorman unlock_page(page); 138069d763fcSMel Gorman if (!migrate_dirty) 1381c8244935SMel Gorman return ret; 1382c8244935SMel Gorman } 1383c8244935SMel Gorman } 1384c8244935SMel Gorman 1385f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 1386f80c0673SMinchan Kim return ret; 1387f80c0673SMinchan Kim 13885ad333ebSAndy Whitcroft if (likely(get_page_unless_zero(page))) { 13895ad333ebSAndy Whitcroft /* 13905ad333ebSAndy Whitcroft * Be careful not to clear PageLRU until after we're 13915ad333ebSAndy Whitcroft * sure the page is not being freed elsewhere -- the 13925ad333ebSAndy Whitcroft * page release code relies on it. 13935ad333ebSAndy Whitcroft */ 13945ad333ebSAndy Whitcroft ClearPageLRU(page); 13955ad333ebSAndy Whitcroft ret = 0; 13965ad333ebSAndy Whitcroft } 13975ad333ebSAndy Whitcroft 13985ad333ebSAndy Whitcroft return ret; 13995ad333ebSAndy Whitcroft } 14005ad333ebSAndy Whitcroft 14017ee36a14SMel Gorman 14027ee36a14SMel Gorman /* 14037ee36a14SMel Gorman * Update LRU sizes after isolating pages. The LRU size updates must 14047ee36a14SMel Gorman * be complete before mem_cgroup_update_lru_size due to a santity check. 14057ee36a14SMel Gorman */ 14067ee36a14SMel Gorman static __always_inline void update_lru_sizes(struct lruvec *lruvec, 1407b4536f0cSMichal Hocko enum lru_list lru, unsigned long *nr_zone_taken) 14087ee36a14SMel Gorman { 14097ee36a14SMel Gorman int zid; 14107ee36a14SMel Gorman 14117ee36a14SMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 14127ee36a14SMel Gorman if (!nr_zone_taken[zid]) 14137ee36a14SMel Gorman continue; 14147ee36a14SMel Gorman 14157ee36a14SMel Gorman __update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 1416b4536f0cSMichal Hocko #ifdef CONFIG_MEMCG 1417b4536f0cSMichal Hocko mem_cgroup_update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 1418b4536f0cSMichal Hocko #endif 14197ee36a14SMel Gorman } 14207ee36a14SMel Gorman 14217ee36a14SMel Gorman } 14227ee36a14SMel Gorman 142349d2e9ccSChristoph Lameter /* 1424a52633d8SMel Gorman * zone_lru_lock is heavily contended. Some of the functions that 14251da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 14261da177e4SLinus Torvalds * and working on them outside the LRU lock. 14271da177e4SLinus Torvalds * 14281da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 14291da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 14301da177e4SLinus Torvalds * 14311da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 14321da177e4SLinus Torvalds * 1433791b48b6SMinchan Kim * @nr_to_scan: The number of eligible pages to look through on the list. 14345dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 14351da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1436f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1437fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 14385ad333ebSAndy Whitcroft * @mode: One of the LRU isolation modes 14393cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 14401da177e4SLinus Torvalds * 14411da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 14421da177e4SLinus Torvalds */ 144369e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 14445dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1445fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 14463cb99451SKonstantin Khlebnikov isolate_mode_t mode, enum lru_list lru) 14471da177e4SLinus Torvalds { 144875b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 144969e05944SAndrew Morton unsigned long nr_taken = 0; 1450599d0c95SMel Gorman unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; 14517cc30fcfSMel Gorman unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; 14523db65812SJohannes Weiner unsigned long skipped = 0; 1453791b48b6SMinchan Kim unsigned long scan, total_scan, nr_pages; 1454b2e18757SMel Gorman LIST_HEAD(pages_skipped); 14551da177e4SLinus Torvalds 1456791b48b6SMinchan Kim scan = 0; 1457791b48b6SMinchan Kim for (total_scan = 0; 1458791b48b6SMinchan Kim scan < nr_to_scan && nr_taken < nr_to_scan && !list_empty(src); 1459791b48b6SMinchan Kim total_scan++) { 14605ad333ebSAndy Whitcroft struct page *page; 14615ad333ebSAndy Whitcroft 14621da177e4SLinus Torvalds page = lru_to_page(src); 14631da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 14641da177e4SLinus Torvalds 1465309381feSSasha Levin VM_BUG_ON_PAGE(!PageLRU(page), page); 14668d438f96SNick Piggin 1467b2e18757SMel Gorman if (page_zonenum(page) > sc->reclaim_idx) { 1468b2e18757SMel Gorman list_move(&page->lru, &pages_skipped); 14697cc30fcfSMel Gorman nr_skipped[page_zonenum(page)]++; 1470b2e18757SMel Gorman continue; 1471b2e18757SMel Gorman } 1472b2e18757SMel Gorman 1473791b48b6SMinchan Kim /* 1474791b48b6SMinchan Kim * Do not count skipped pages because that makes the function 1475791b48b6SMinchan Kim * return with no isolated pages if the LRU mostly contains 1476791b48b6SMinchan Kim * ineligible pages. This causes the VM to not reclaim any 1477791b48b6SMinchan Kim * pages, triggering a premature OOM. 1478791b48b6SMinchan Kim */ 1479791b48b6SMinchan Kim scan++; 1480f3fd4a61SKonstantin Khlebnikov switch (__isolate_lru_page(page, mode)) { 14815ad333ebSAndy Whitcroft case 0: 1482599d0c95SMel Gorman nr_pages = hpage_nr_pages(page); 1483599d0c95SMel Gorman nr_taken += nr_pages; 1484599d0c95SMel Gorman nr_zone_taken[page_zonenum(page)] += nr_pages; 14855ad333ebSAndy Whitcroft list_move(&page->lru, dst); 14865ad333ebSAndy Whitcroft break; 14877c8ee9a8SNick Piggin 14885ad333ebSAndy Whitcroft case -EBUSY: 14895ad333ebSAndy Whitcroft /* else it is being freed elsewhere */ 14905ad333ebSAndy Whitcroft list_move(&page->lru, src); 14915ad333ebSAndy Whitcroft continue; 14925ad333ebSAndy Whitcroft 14935ad333ebSAndy Whitcroft default: 14945ad333ebSAndy Whitcroft BUG(); 14955ad333ebSAndy Whitcroft } 14965ad333ebSAndy Whitcroft } 14971da177e4SLinus Torvalds 1498b2e18757SMel Gorman /* 1499b2e18757SMel Gorman * Splice any skipped pages to the start of the LRU list. Note that 1500b2e18757SMel Gorman * this disrupts the LRU order when reclaiming for lower zones but 1501b2e18757SMel Gorman * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX 1502b2e18757SMel Gorman * scanning would soon rescan the same pages to skip and put the 1503b2e18757SMel Gorman * system at risk of premature OOM. 1504b2e18757SMel Gorman */ 15057cc30fcfSMel Gorman if (!list_empty(&pages_skipped)) { 15067cc30fcfSMel Gorman int zid; 15077cc30fcfSMel Gorman 15083db65812SJohannes Weiner list_splice(&pages_skipped, src); 15097cc30fcfSMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 15107cc30fcfSMel Gorman if (!nr_skipped[zid]) 15117cc30fcfSMel Gorman continue; 15127cc30fcfSMel Gorman 15137cc30fcfSMel Gorman __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); 15141265e3a6SMichal Hocko skipped += nr_skipped[zid]; 15157cc30fcfSMel Gorman } 15167cc30fcfSMel Gorman } 1517791b48b6SMinchan Kim *nr_scanned = total_scan; 15181265e3a6SMichal Hocko trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, 1519791b48b6SMinchan Kim total_scan, skipped, nr_taken, mode, lru); 1520b4536f0cSMichal Hocko update_lru_sizes(lruvec, lru, nr_zone_taken); 15211da177e4SLinus Torvalds return nr_taken; 15221da177e4SLinus Torvalds } 15231da177e4SLinus Torvalds 152462695a84SNick Piggin /** 152562695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 152662695a84SNick Piggin * @page: page to isolate from its LRU list 152762695a84SNick Piggin * 152862695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 152962695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 153062695a84SNick Piggin * 153162695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 153262695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 153362695a84SNick Piggin * 153462695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1535894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1536894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1537894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 153862695a84SNick Piggin * 153962695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 154062695a84SNick Piggin * found will be decremented. 154162695a84SNick Piggin * 154262695a84SNick Piggin * Restrictions: 1543a5d09bedSMike Rapoport * 154462695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 154562695a84SNick Piggin * fundamentnal difference from isolate_lru_pages (which is called 154662695a84SNick Piggin * without a stable reference). 154762695a84SNick Piggin * (2) the lru_lock must not be held. 154862695a84SNick Piggin * (3) interrupts must be enabled. 154962695a84SNick Piggin */ 155062695a84SNick Piggin int isolate_lru_page(struct page *page) 155162695a84SNick Piggin { 155262695a84SNick Piggin int ret = -EBUSY; 155362695a84SNick Piggin 1554309381feSSasha Levin VM_BUG_ON_PAGE(!page_count(page), page); 1555cf2a82eeSKirill A. Shutemov WARN_RATELIMIT(PageTail(page), "trying to isolate tail page"); 15560c917313SKonstantin Khlebnikov 155762695a84SNick Piggin if (PageLRU(page)) { 155862695a84SNick Piggin struct zone *zone = page_zone(page); 1559fa9add64SHugh Dickins struct lruvec *lruvec; 156062695a84SNick Piggin 1561a52633d8SMel Gorman spin_lock_irq(zone_lru_lock(zone)); 1562599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat); 15630c917313SKonstantin Khlebnikov if (PageLRU(page)) { 1564894bc310SLee Schermerhorn int lru = page_lru(page); 15650c917313SKonstantin Khlebnikov get_page(page); 156662695a84SNick Piggin ClearPageLRU(page); 1567fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 1568fa9add64SHugh Dickins ret = 0; 156962695a84SNick Piggin } 1570a52633d8SMel Gorman spin_unlock_irq(zone_lru_lock(zone)); 157162695a84SNick Piggin } 157262695a84SNick Piggin return ret; 157362695a84SNick Piggin } 157462695a84SNick Piggin 15755ad333ebSAndy Whitcroft /* 1576d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 1577d37dd5dcSFengguang Wu * then get resheduled. When there are massive number of tasks doing page 1578d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 1579d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 1580d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 158135cd7815SRik van Riel */ 1582599d0c95SMel Gorman static int too_many_isolated(struct pglist_data *pgdat, int file, 158335cd7815SRik van Riel struct scan_control *sc) 158435cd7815SRik van Riel { 158535cd7815SRik van Riel unsigned long inactive, isolated; 158635cd7815SRik van Riel 158735cd7815SRik van Riel if (current_is_kswapd()) 158835cd7815SRik van Riel return 0; 158935cd7815SRik van Riel 159097c9341fSTejun Heo if (!sane_reclaim(sc)) 159135cd7815SRik van Riel return 0; 159235cd7815SRik van Riel 159335cd7815SRik van Riel if (file) { 1594599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_FILE); 1595599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_FILE); 159635cd7815SRik van Riel } else { 1597599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_ANON); 1598599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_ANON); 159935cd7815SRik van Riel } 160035cd7815SRik van Riel 16013cf23841SFengguang Wu /* 16023cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 16033cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 16043cf23841SFengguang Wu * deadlock. 16053cf23841SFengguang Wu */ 1606d0164adcSMel Gorman if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) 16073cf23841SFengguang Wu inactive >>= 3; 16083cf23841SFengguang Wu 160935cd7815SRik van Riel return isolated > inactive; 161035cd7815SRik van Riel } 161135cd7815SRik van Riel 161266635629SMel Gorman static noinline_for_stack void 161375b00af7SHugh Dickins putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list) 161466635629SMel Gorman { 161527ac81d8SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 1616599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 16173f79768fSHugh Dickins LIST_HEAD(pages_to_free); 161866635629SMel Gorman 161966635629SMel Gorman /* 162066635629SMel Gorman * Put back any unfreeable pages. 162166635629SMel Gorman */ 162266635629SMel Gorman while (!list_empty(page_list)) { 16233f79768fSHugh Dickins struct page *page = lru_to_page(page_list); 162466635629SMel Gorman int lru; 16253f79768fSHugh Dickins 1626309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 162766635629SMel Gorman list_del(&page->lru); 162839b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 1629599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 163066635629SMel Gorman putback_lru_page(page); 1631599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 163266635629SMel Gorman continue; 163366635629SMel Gorman } 1634fa9add64SHugh Dickins 1635599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 1636fa9add64SHugh Dickins 16377a608572SLinus Torvalds SetPageLRU(page); 163866635629SMel Gorman lru = page_lru(page); 1639fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 1640fa9add64SHugh Dickins 164166635629SMel Gorman if (is_active_lru(lru)) { 164266635629SMel Gorman int file = is_file_lru(lru); 16439992af10SRik van Riel int numpages = hpage_nr_pages(page); 16449992af10SRik van Riel reclaim_stat->recent_rotated[file] += numpages; 164566635629SMel Gorman } 16462bcf8879SHugh Dickins if (put_page_testzero(page)) { 16472bcf8879SHugh Dickins __ClearPageLRU(page); 16482bcf8879SHugh Dickins __ClearPageActive(page); 1649fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 16502bcf8879SHugh Dickins 16512bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 1652599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1653747db954SJohannes Weiner mem_cgroup_uncharge(page); 16542bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 1655599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 16562bcf8879SHugh Dickins } else 16572bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 165866635629SMel Gorman } 165966635629SMel Gorman } 166066635629SMel Gorman 16613f79768fSHugh Dickins /* 16623f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 16633f79768fSHugh Dickins */ 16643f79768fSHugh Dickins list_splice(&pages_to_free, page_list); 166566635629SMel Gorman } 166666635629SMel Gorman 166766635629SMel Gorman /* 1668399ba0b9SNeilBrown * If a kernel thread (such as nfsd for loop-back mounts) services 1669399ba0b9SNeilBrown * a backing device by writing to the page cache it sets PF_LESS_THROTTLE. 1670399ba0b9SNeilBrown * In that case we should only throttle if the backing device it is 1671399ba0b9SNeilBrown * writing to is congested. In other cases it is safe to throttle. 1672399ba0b9SNeilBrown */ 1673399ba0b9SNeilBrown static int current_may_throttle(void) 1674399ba0b9SNeilBrown { 1675399ba0b9SNeilBrown return !(current->flags & PF_LESS_THROTTLE) || 1676399ba0b9SNeilBrown current->backing_dev_info == NULL || 1677399ba0b9SNeilBrown bdi_write_congested(current->backing_dev_info); 1678399ba0b9SNeilBrown } 1679399ba0b9SNeilBrown 1680399ba0b9SNeilBrown /* 1681b2e18757SMel Gorman * shrink_inactive_list() is a helper for shrink_node(). It returns the number 16821742f19fSAndrew Morton * of reclaimed pages 16831da177e4SLinus Torvalds */ 168466635629SMel Gorman static noinline_for_stack unsigned long 16851a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 16869e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 16871da177e4SLinus Torvalds { 16881da177e4SLinus Torvalds LIST_HEAD(page_list); 1689e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 169005ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 1691e247dbceSKOSAKI Motohiro unsigned long nr_taken; 16923c710c1aSMichal Hocko struct reclaim_stat stat = {}; 1693f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 16943cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 1695599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 16961a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 1697db73ee0dSMichal Hocko bool stalled = false; 169878dc583dSKOSAKI Motohiro 1699599d0c95SMel Gorman while (unlikely(too_many_isolated(pgdat, file, sc))) { 1700db73ee0dSMichal Hocko if (stalled) 1701db73ee0dSMichal Hocko return 0; 1702db73ee0dSMichal Hocko 1703db73ee0dSMichal Hocko /* wait a bit for the reclaimer. */ 1704db73ee0dSMichal Hocko msleep(100); 1705db73ee0dSMichal Hocko stalled = true; 170635cd7815SRik van Riel 170735cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 170835cd7815SRik van Riel if (fatal_signal_pending(current)) 170935cd7815SRik van Riel return SWAP_CLUSTER_MAX; 171035cd7815SRik van Riel } 171135cd7815SRik van Riel 17121da177e4SLinus Torvalds lru_add_drain(); 1713f80c0673SMinchan Kim 1714f80c0673SMinchan Kim if (!sc->may_unmap) 171561317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1716f80c0673SMinchan Kim 1717599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 17181da177e4SLinus Torvalds 17195dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 17205dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 172195d918fcSKonstantin Khlebnikov 1722599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 17239d5e6a9fSHugh Dickins reclaim_stat->recent_scanned[file] += nr_taken; 172495d918fcSKonstantin Khlebnikov 17252262185cSRoman Gushchin if (current_is_kswapd()) { 17262262185cSRoman Gushchin if (global_reclaim(sc)) 1727599d0c95SMel Gorman __count_vm_events(PGSCAN_KSWAPD, nr_scanned); 17282262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGSCAN_KSWAPD, 17292262185cSRoman Gushchin nr_scanned); 17302262185cSRoman Gushchin } else { 17312262185cSRoman Gushchin if (global_reclaim(sc)) 1732599d0c95SMel Gorman __count_vm_events(PGSCAN_DIRECT, nr_scanned); 17332262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGSCAN_DIRECT, 17342262185cSRoman Gushchin nr_scanned); 1735b35ea17bSKOSAKI Motohiro } 1736599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1737d563c050SHillf Danton 1738d563c050SHillf Danton if (nr_taken == 0) 173966635629SMel Gorman return 0; 1740b35ea17bSKOSAKI Motohiro 1741a128ca71SShaohua Li nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, 0, 17423c710c1aSMichal Hocko &stat, false); 1743c661b078SAndy Whitcroft 1744599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 17453f79768fSHugh Dickins 17462262185cSRoman Gushchin if (current_is_kswapd()) { 17472262185cSRoman Gushchin if (global_reclaim(sc)) 1748599d0c95SMel Gorman __count_vm_events(PGSTEAL_KSWAPD, nr_reclaimed); 17492262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_KSWAPD, 17502262185cSRoman Gushchin nr_reclaimed); 17512262185cSRoman Gushchin } else { 17522262185cSRoman Gushchin if (global_reclaim(sc)) 1753599d0c95SMel Gorman __count_vm_events(PGSTEAL_DIRECT, nr_reclaimed); 17542262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_DIRECT, 17552262185cSRoman Gushchin nr_reclaimed); 1756904249aaSYing Han } 1757a74609faSNick Piggin 175827ac81d8SKonstantin Khlebnikov putback_inactive_pages(lruvec, &page_list); 17593f79768fSHugh Dickins 1760599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 17613f79768fSHugh Dickins 1762599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 17633f79768fSHugh Dickins 1764747db954SJohannes Weiner mem_cgroup_uncharge_list(&page_list); 17652d4894b5SMel Gorman free_unref_page_list(&page_list); 1766e11da5b4SMel Gorman 176792df3a72SMel Gorman /* 17681c610d5fSAndrey Ryabinin * If dirty pages are scanned that are not queued for IO, it 17691c610d5fSAndrey Ryabinin * implies that flushers are not doing their job. This can 17701c610d5fSAndrey Ryabinin * happen when memory pressure pushes dirty pages to the end of 17711c610d5fSAndrey Ryabinin * the LRU before the dirty limits are breached and the dirty 17721c610d5fSAndrey Ryabinin * data has expired. It can also happen when the proportion of 17731c610d5fSAndrey Ryabinin * dirty pages grows not through writes but through memory 17741c610d5fSAndrey Ryabinin * pressure reclaiming all the clean cache. And in some cases, 17751c610d5fSAndrey Ryabinin * the flushers simply cannot keep up with the allocation 17761c610d5fSAndrey Ryabinin * rate. Nudge the flusher threads in case they are asleep. 17771c610d5fSAndrey Ryabinin */ 17781c610d5fSAndrey Ryabinin if (stat.nr_unqueued_dirty == nr_taken) 17791c610d5fSAndrey Ryabinin wakeup_flusher_threads(WB_REASON_VMSCAN); 17801c610d5fSAndrey Ryabinin 1781*d108c772SAndrey Ryabinin sc->nr.dirty += stat.nr_dirty; 1782*d108c772SAndrey Ryabinin sc->nr.congested += stat.nr_congested; 1783*d108c772SAndrey Ryabinin sc->nr.unqueued_dirty += stat.nr_unqueued_dirty; 1784*d108c772SAndrey Ryabinin sc->nr.writeback += stat.nr_writeback; 1785*d108c772SAndrey Ryabinin sc->nr.immediate += stat.nr_immediate; 1786*d108c772SAndrey Ryabinin sc->nr.taken += nr_taken; 1787*d108c772SAndrey Ryabinin if (file) 1788*d108c772SAndrey Ryabinin sc->nr.file_taken += nr_taken; 17898e950282SMel Gorman 1790599d0c95SMel Gorman trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, 1791599d0c95SMel Gorman nr_scanned, nr_reclaimed, 17925bccd166SMichal Hocko stat.nr_dirty, stat.nr_writeback, 17935bccd166SMichal Hocko stat.nr_congested, stat.nr_immediate, 17945bccd166SMichal Hocko stat.nr_activate, stat.nr_ref_keep, 17955bccd166SMichal Hocko stat.nr_unmap_fail, 1796ba5e9579Syalin wang sc->priority, file); 179705ff5137SAndrew Morton return nr_reclaimed; 17981da177e4SLinus Torvalds } 17991da177e4SLinus Torvalds 18003bb1a852SMartin Bligh /* 18011cfb419bSKAMEZAWA Hiroyuki * This moves pages from the active list to the inactive list. 18021cfb419bSKAMEZAWA Hiroyuki * 18031cfb419bSKAMEZAWA Hiroyuki * We move them the other way if the page is referenced by one or more 18041cfb419bSKAMEZAWA Hiroyuki * processes, from rmap. 18051cfb419bSKAMEZAWA Hiroyuki * 18061cfb419bSKAMEZAWA Hiroyuki * If the pages are mostly unmapped, the processing is fast and it is 1807a52633d8SMel Gorman * appropriate to hold zone_lru_lock across the whole operation. But if 18081cfb419bSKAMEZAWA Hiroyuki * the pages are mapped, the processing is slow (page_referenced()) so we 1809a52633d8SMel Gorman * should drop zone_lru_lock around each page. It's impossible to balance 18101cfb419bSKAMEZAWA Hiroyuki * this, so instead we remove the pages from the LRU while processing them. 18111cfb419bSKAMEZAWA Hiroyuki * It is safe to rely on PG_active against the non-LRU pages in here because 18121cfb419bSKAMEZAWA Hiroyuki * nobody will play with that bit on a non-LRU page. 18131cfb419bSKAMEZAWA Hiroyuki * 18140139aa7bSJoonsoo Kim * The downside is that we have to touch page->_refcount against each page. 18151cfb419bSKAMEZAWA Hiroyuki * But we had to alter page->flags anyway. 18169d998b4fSMichal Hocko * 18179d998b4fSMichal Hocko * Returns the number of pages moved to the given lru. 18181cfb419bSKAMEZAWA Hiroyuki */ 18191cfb419bSKAMEZAWA Hiroyuki 18209d998b4fSMichal Hocko static unsigned move_active_pages_to_lru(struct lruvec *lruvec, 18213eb4140fSWu Fengguang struct list_head *list, 18222bcf8879SHugh Dickins struct list_head *pages_to_free, 18233eb4140fSWu Fengguang enum lru_list lru) 18243eb4140fSWu Fengguang { 1825599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 18263eb4140fSWu Fengguang struct page *page; 1827fa9add64SHugh Dickins int nr_pages; 18289d998b4fSMichal Hocko int nr_moved = 0; 18293eb4140fSWu Fengguang 18303eb4140fSWu Fengguang while (!list_empty(list)) { 18313eb4140fSWu Fengguang page = lru_to_page(list); 1832599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 18333eb4140fSWu Fengguang 1834309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 18353eb4140fSWu Fengguang SetPageLRU(page); 18363eb4140fSWu Fengguang 1837fa9add64SHugh Dickins nr_pages = hpage_nr_pages(page); 1838599d0c95SMel Gorman update_lru_size(lruvec, lru, page_zonenum(page), nr_pages); 1839925b7673SJohannes Weiner list_move(&page->lru, &lruvec->lists[lru]); 18403eb4140fSWu Fengguang 18412bcf8879SHugh Dickins if (put_page_testzero(page)) { 18422bcf8879SHugh Dickins __ClearPageLRU(page); 18432bcf8879SHugh Dickins __ClearPageActive(page); 1844fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 18452bcf8879SHugh Dickins 18462bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 1847599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1848747db954SJohannes Weiner mem_cgroup_uncharge(page); 18492bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 1850599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 18512bcf8879SHugh Dickins } else 18522bcf8879SHugh Dickins list_add(&page->lru, pages_to_free); 18539d998b4fSMichal Hocko } else { 18549d998b4fSMichal Hocko nr_moved += nr_pages; 18553eb4140fSWu Fengguang } 18563eb4140fSWu Fengguang } 18579d5e6a9fSHugh Dickins 18582262185cSRoman Gushchin if (!is_active_lru(lru)) { 1859f0958906SMichal Hocko __count_vm_events(PGDEACTIVATE, nr_moved); 18602262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, 18612262185cSRoman Gushchin nr_moved); 18622262185cSRoman Gushchin } 18639d998b4fSMichal Hocko 18649d998b4fSMichal Hocko return nr_moved; 18653eb4140fSWu Fengguang } 18661cfb419bSKAMEZAWA Hiroyuki 1867f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 18681a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 1869f16015fbSJohannes Weiner struct scan_control *sc, 18709e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 18711cfb419bSKAMEZAWA Hiroyuki { 187244c241f1SKOSAKI Motohiro unsigned long nr_taken; 1873f626012dSHugh Dickins unsigned long nr_scanned; 18746fe6b7e3SWu Fengguang unsigned long vm_flags; 18751cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 18768cab4754SWu Fengguang LIST_HEAD(l_active); 1877b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 18781cfb419bSKAMEZAWA Hiroyuki struct page *page; 18791a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 18809d998b4fSMichal Hocko unsigned nr_deactivate, nr_activate; 18819d998b4fSMichal Hocko unsigned nr_rotated = 0; 1882f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 18833cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 1884599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 18851cfb419bSKAMEZAWA Hiroyuki 18861da177e4SLinus Torvalds lru_add_drain(); 1887f80c0673SMinchan Kim 1888f80c0673SMinchan Kim if (!sc->may_unmap) 188961317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1890f80c0673SMinchan Kim 1891599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 1892925b7673SJohannes Weiner 18935dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 18945dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 189589b5fae5SJohannes Weiner 1896599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 1897b7c46d15SJohannes Weiner reclaim_stat->recent_scanned[file] += nr_taken; 18981cfb419bSKAMEZAWA Hiroyuki 1899599d0c95SMel Gorman __count_vm_events(PGREFILL, nr_scanned); 19002262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned); 19019d5e6a9fSHugh Dickins 1902599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 19031da177e4SLinus Torvalds 19041da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 19051da177e4SLinus Torvalds cond_resched(); 19061da177e4SLinus Torvalds page = lru_to_page(&l_hold); 19071da177e4SLinus Torvalds list_del(&page->lru); 19087e9cd484SRik van Riel 190939b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 1910894bc310SLee Schermerhorn putback_lru_page(page); 1911894bc310SLee Schermerhorn continue; 1912894bc310SLee Schermerhorn } 1913894bc310SLee Schermerhorn 1914cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 1915cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 1916cc715d99SMel Gorman if (page_has_private(page)) 1917cc715d99SMel Gorman try_to_release_page(page, 0); 1918cc715d99SMel Gorman unlock_page(page); 1919cc715d99SMel Gorman } 1920cc715d99SMel Gorman } 1921cc715d99SMel Gorman 1922c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 1923c3ac9a8aSJohannes Weiner &vm_flags)) { 19249992af10SRik van Riel nr_rotated += hpage_nr_pages(page); 19258cab4754SWu Fengguang /* 19268cab4754SWu Fengguang * Identify referenced, file-backed active pages and 19278cab4754SWu Fengguang * give them one more trip around the active list. So 19288cab4754SWu Fengguang * that executable code get better chances to stay in 19298cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 19308cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 19318cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 19328cab4754SWu Fengguang * so we ignore them here. 19338cab4754SWu Fengguang */ 193441e20983SWu Fengguang if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { 19358cab4754SWu Fengguang list_add(&page->lru, &l_active); 19368cab4754SWu Fengguang continue; 19378cab4754SWu Fengguang } 19388cab4754SWu Fengguang } 19397e9cd484SRik van Riel 19405205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 19411da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 19421da177e4SLinus Torvalds } 19431da177e4SLinus Torvalds 1944b555749aSAndrew Morton /* 19458cab4754SWu Fengguang * Move pages back to the lru list. 1946b555749aSAndrew Morton */ 1947599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 19484f98a2feSRik van Riel /* 19498cab4754SWu Fengguang * Count referenced pages from currently used mappings as rotated, 19508cab4754SWu Fengguang * even though only some of them are actually re-activated. This 19518cab4754SWu Fengguang * helps balance scan pressure between file and anonymous pages in 19527c0db9e9SJerome Marchand * get_scan_count. 1953556adecbSRik van Riel */ 1954b7c46d15SJohannes Weiner reclaim_stat->recent_rotated[file] += nr_rotated; 1955556adecbSRik van Riel 19569d998b4fSMichal Hocko nr_activate = move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru); 19579d998b4fSMichal Hocko nr_deactivate = move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE); 1958599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 1959599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 19602bcf8879SHugh Dickins 1961747db954SJohannes Weiner mem_cgroup_uncharge_list(&l_hold); 19622d4894b5SMel Gorman free_unref_page_list(&l_hold); 19639d998b4fSMichal Hocko trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate, 19649d998b4fSMichal Hocko nr_deactivate, nr_rotated, sc->priority, file); 19651da177e4SLinus Torvalds } 19661da177e4SLinus Torvalds 196759dc76b0SRik van Riel /* 196859dc76b0SRik van Riel * The inactive anon list should be small enough that the VM never has 196959dc76b0SRik van Riel * to do too much work. 197014797e23SKOSAKI Motohiro * 197159dc76b0SRik van Riel * The inactive file list should be small enough to leave most memory 197259dc76b0SRik van Riel * to the established workingset on the scan-resistant active list, 197359dc76b0SRik van Riel * but large enough to avoid thrashing the aggregate readahead window. 197459dc76b0SRik van Riel * 197559dc76b0SRik van Riel * Both inactive lists should also be large enough that each inactive 197659dc76b0SRik van Riel * page has a chance to be referenced again before it is reclaimed. 197759dc76b0SRik van Riel * 19782a2e4885SJohannes Weiner * If that fails and refaulting is observed, the inactive list grows. 19792a2e4885SJohannes Weiner * 198059dc76b0SRik van Riel * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages 19813a50d14dSAndrey Ryabinin * on this LRU, maintained by the pageout code. An inactive_ratio 198259dc76b0SRik van Riel * of 3 means 3:1 or 25% of the pages are kept on the inactive list. 198359dc76b0SRik van Riel * 198459dc76b0SRik van Riel * total target max 198559dc76b0SRik van Riel * memory ratio inactive 198659dc76b0SRik van Riel * ------------------------------------- 198759dc76b0SRik van Riel * 10MB 1 5MB 198859dc76b0SRik van Riel * 100MB 1 50MB 198959dc76b0SRik van Riel * 1GB 3 250MB 199059dc76b0SRik van Riel * 10GB 10 0.9GB 199159dc76b0SRik van Riel * 100GB 31 3GB 199259dc76b0SRik van Riel * 1TB 101 10GB 199359dc76b0SRik van Riel * 10TB 320 32GB 199414797e23SKOSAKI Motohiro */ 1995f8d1a311SMel Gorman static bool inactive_list_is_low(struct lruvec *lruvec, bool file, 19962a2e4885SJohannes Weiner struct mem_cgroup *memcg, 19972a2e4885SJohannes Weiner struct scan_control *sc, bool actual_reclaim) 199814797e23SKOSAKI Motohiro { 1999fd538803SMichal Hocko enum lru_list active_lru = file * LRU_FILE + LRU_ACTIVE; 20002a2e4885SJohannes Weiner struct pglist_data *pgdat = lruvec_pgdat(lruvec); 20012a2e4885SJohannes Weiner enum lru_list inactive_lru = file * LRU_FILE; 20022a2e4885SJohannes Weiner unsigned long inactive, active; 20032a2e4885SJohannes Weiner unsigned long inactive_ratio; 20042a2e4885SJohannes Weiner unsigned long refaults; 200559dc76b0SRik van Riel unsigned long gb; 200659dc76b0SRik van Riel 200774e3f3c3SMinchan Kim /* 200874e3f3c3SMinchan Kim * If we don't have swap space, anonymous page deactivation 200974e3f3c3SMinchan Kim * is pointless. 201074e3f3c3SMinchan Kim */ 201159dc76b0SRik van Riel if (!file && !total_swap_pages) 201242e2e457SYaowei Bai return false; 201374e3f3c3SMinchan Kim 2014fd538803SMichal Hocko inactive = lruvec_lru_size(lruvec, inactive_lru, sc->reclaim_idx); 2015fd538803SMichal Hocko active = lruvec_lru_size(lruvec, active_lru, sc->reclaim_idx); 2016f8d1a311SMel Gorman 20172a2e4885SJohannes Weiner if (memcg) 2018ccda7f43SJohannes Weiner refaults = memcg_page_state(memcg, WORKINGSET_ACTIVATE); 20192a2e4885SJohannes Weiner else 20202a2e4885SJohannes Weiner refaults = node_page_state(pgdat, WORKINGSET_ACTIVATE); 20212a2e4885SJohannes Weiner 20222a2e4885SJohannes Weiner /* 20232a2e4885SJohannes Weiner * When refaults are being observed, it means a new workingset 20242a2e4885SJohannes Weiner * is being established. Disable active list protection to get 20252a2e4885SJohannes Weiner * rid of the stale workingset quickly. 20262a2e4885SJohannes Weiner */ 20272a2e4885SJohannes Weiner if (file && actual_reclaim && lruvec->refaults != refaults) { 20282a2e4885SJohannes Weiner inactive_ratio = 0; 20292a2e4885SJohannes Weiner } else { 203059dc76b0SRik van Riel gb = (inactive + active) >> (30 - PAGE_SHIFT); 203159dc76b0SRik van Riel if (gb) 203259dc76b0SRik van Riel inactive_ratio = int_sqrt(10 * gb); 2033b39415b2SRik van Riel else 203459dc76b0SRik van Riel inactive_ratio = 1; 20352a2e4885SJohannes Weiner } 203659dc76b0SRik van Riel 20372a2e4885SJohannes Weiner if (actual_reclaim) 20382a2e4885SJohannes Weiner trace_mm_vmscan_inactive_list_is_low(pgdat->node_id, sc->reclaim_idx, 2039fd538803SMichal Hocko lruvec_lru_size(lruvec, inactive_lru, MAX_NR_ZONES), inactive, 2040fd538803SMichal Hocko lruvec_lru_size(lruvec, active_lru, MAX_NR_ZONES), active, 2041fd538803SMichal Hocko inactive_ratio, file); 2042fd538803SMichal Hocko 204359dc76b0SRik van Riel return inactive * inactive_ratio < active; 2044b39415b2SRik van Riel } 2045b39415b2SRik van Riel 20464f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 20472a2e4885SJohannes Weiner struct lruvec *lruvec, struct mem_cgroup *memcg, 20482a2e4885SJohannes Weiner struct scan_control *sc) 2049b69408e8SChristoph Lameter { 2050b39415b2SRik van Riel if (is_active_lru(lru)) { 20512a2e4885SJohannes Weiner if (inactive_list_is_low(lruvec, is_file_lru(lru), 20522a2e4885SJohannes Weiner memcg, sc, true)) 20531a93be0eSKonstantin Khlebnikov shrink_active_list(nr_to_scan, lruvec, sc, lru); 2054556adecbSRik van Riel return 0; 2055556adecbSRik van Riel } 2056556adecbSRik van Riel 20571a93be0eSKonstantin Khlebnikov return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 2058b69408e8SChristoph Lameter } 2059b69408e8SChristoph Lameter 20609a265114SJohannes Weiner enum scan_balance { 20619a265114SJohannes Weiner SCAN_EQUAL, 20629a265114SJohannes Weiner SCAN_FRACT, 20639a265114SJohannes Weiner SCAN_ANON, 20649a265114SJohannes Weiner SCAN_FILE, 20659a265114SJohannes Weiner }; 20669a265114SJohannes Weiner 20671da177e4SLinus Torvalds /* 20684f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 20694f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 20704f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 20714f98a2feSRik van Riel * onto the active list instead of evict. 20724f98a2feSRik van Riel * 2073be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 2074be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 20754f98a2feSRik van Riel */ 207633377678SVladimir Davydov static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, 20776b4f7799SJohannes Weiner struct scan_control *sc, unsigned long *nr, 20786b4f7799SJohannes Weiner unsigned long *lru_pages) 20794f98a2feSRik van Riel { 208033377678SVladimir Davydov int swappiness = mem_cgroup_swappiness(memcg); 208190126375SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 20829a265114SJohannes Weiner u64 fraction[2]; 20839a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 2084599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 20859a265114SJohannes Weiner unsigned long anon_prio, file_prio; 20869a265114SJohannes Weiner enum scan_balance scan_balance; 20870bf1457fSJohannes Weiner unsigned long anon, file; 20889a265114SJohannes Weiner unsigned long ap, fp; 20899a265114SJohannes Weiner enum lru_list lru; 209076a33fc3SShaohua Li 209176a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 2092d8b38438SVladimir Davydov if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) { 20939a265114SJohannes Weiner scan_balance = SCAN_FILE; 209476a33fc3SShaohua Li goto out; 209576a33fc3SShaohua Li } 20964f98a2feSRik van Riel 209710316b31SJohannes Weiner /* 209810316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 209910316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 210010316b31SJohannes Weiner * disable swapping for individual groups completely when 210110316b31SJohannes Weiner * using the memory controller's swap limit feature would be 210210316b31SJohannes Weiner * too expensive. 210310316b31SJohannes Weiner */ 210402695175SJohannes Weiner if (!global_reclaim(sc) && !swappiness) { 21059a265114SJohannes Weiner scan_balance = SCAN_FILE; 210610316b31SJohannes Weiner goto out; 210710316b31SJohannes Weiner } 210810316b31SJohannes Weiner 210910316b31SJohannes Weiner /* 211010316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 211110316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 211210316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 211310316b31SJohannes Weiner */ 211402695175SJohannes Weiner if (!sc->priority && swappiness) { 21159a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 211610316b31SJohannes Weiner goto out; 211710316b31SJohannes Weiner } 211810316b31SJohannes Weiner 211911d16c25SJohannes Weiner /* 212062376251SJohannes Weiner * Prevent the reclaimer from falling into the cache trap: as 212162376251SJohannes Weiner * cache pages start out inactive, every cache fault will tip 212262376251SJohannes Weiner * the scan balance towards the file LRU. And as the file LRU 212362376251SJohannes Weiner * shrinks, so does the window for rotation from references. 212462376251SJohannes Weiner * This means we have a runaway feedback loop where a tiny 212562376251SJohannes Weiner * thrashing file LRU becomes infinitely more attractive than 212662376251SJohannes Weiner * anon pages. Try to detect this based on file LRU size. 212762376251SJohannes Weiner */ 212862376251SJohannes Weiner if (global_reclaim(sc)) { 2129599d0c95SMel Gorman unsigned long pgdatfile; 2130599d0c95SMel Gorman unsigned long pgdatfree; 2131599d0c95SMel Gorman int z; 2132599d0c95SMel Gorman unsigned long total_high_wmark = 0; 213362376251SJohannes Weiner 2134599d0c95SMel Gorman pgdatfree = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); 2135599d0c95SMel Gorman pgdatfile = node_page_state(pgdat, NR_ACTIVE_FILE) + 2136599d0c95SMel Gorman node_page_state(pgdat, NR_INACTIVE_FILE); 21372ab051e1SJerome Marchand 2138599d0c95SMel Gorman for (z = 0; z < MAX_NR_ZONES; z++) { 2139599d0c95SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 21406aa303deSMel Gorman if (!managed_zone(zone)) 2141599d0c95SMel Gorman continue; 2142599d0c95SMel Gorman 2143599d0c95SMel Gorman total_high_wmark += high_wmark_pages(zone); 2144599d0c95SMel Gorman } 2145599d0c95SMel Gorman 2146599d0c95SMel Gorman if (unlikely(pgdatfile + pgdatfree <= total_high_wmark)) { 214706226226SDavid Rientjes /* 214806226226SDavid Rientjes * Force SCAN_ANON if there are enough inactive 214906226226SDavid Rientjes * anonymous pages on the LRU in eligible zones. 215006226226SDavid Rientjes * Otherwise, the small LRU gets thrashed. 215106226226SDavid Rientjes */ 215206226226SDavid Rientjes if (!inactive_list_is_low(lruvec, false, memcg, sc, false) && 215306226226SDavid Rientjes lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, sc->reclaim_idx) 215406226226SDavid Rientjes >> sc->priority) { 215562376251SJohannes Weiner scan_balance = SCAN_ANON; 215662376251SJohannes Weiner goto out; 215762376251SJohannes Weiner } 215862376251SJohannes Weiner } 215906226226SDavid Rientjes } 216062376251SJohannes Weiner 216162376251SJohannes Weiner /* 2162316bda0eSVladimir Davydov * If there is enough inactive page cache, i.e. if the size of the 2163316bda0eSVladimir Davydov * inactive list is greater than that of the active list *and* the 2164316bda0eSVladimir Davydov * inactive list actually has some pages to scan on this priority, we 2165316bda0eSVladimir Davydov * do not reclaim anything from the anonymous working set right now. 2166316bda0eSVladimir Davydov * Without the second condition we could end up never scanning an 2167316bda0eSVladimir Davydov * lruvec even if it has plenty of old anonymous pages unless the 2168316bda0eSVladimir Davydov * system is under heavy pressure. 2169e9868505SRik van Riel */ 21702a2e4885SJohannes Weiner if (!inactive_list_is_low(lruvec, true, memcg, sc, false) && 217171ab6cfeSMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, sc->reclaim_idx) >> sc->priority) { 21729a265114SJohannes Weiner scan_balance = SCAN_FILE; 2173e9868505SRik van Riel goto out; 21744f98a2feSRik van Riel } 21754f98a2feSRik van Riel 21769a265114SJohannes Weiner scan_balance = SCAN_FRACT; 21779a265114SJohannes Weiner 21784f98a2feSRik van Riel /* 217958c37f6eSKOSAKI Motohiro * With swappiness at 100, anonymous and file have the same priority. 218058c37f6eSKOSAKI Motohiro * This scanning priority is essentially the inverse of IO cost. 218158c37f6eSKOSAKI Motohiro */ 218202695175SJohannes Weiner anon_prio = swappiness; 218375b00af7SHugh Dickins file_prio = 200 - anon_prio; 218458c37f6eSKOSAKI Motohiro 218558c37f6eSKOSAKI Motohiro /* 21864f98a2feSRik van Riel * OK, so we have swap space and a fair amount of page cache 21874f98a2feSRik van Riel * pages. We use the recently rotated / recently scanned 21884f98a2feSRik van Riel * ratios to determine how valuable each cache is. 21894f98a2feSRik van Riel * 21904f98a2feSRik van Riel * Because workloads change over time (and to avoid overflow) 21914f98a2feSRik van Riel * we keep these statistics as a floating average, which ends 21924f98a2feSRik van Riel * up weighing recent references more than old ones. 21934f98a2feSRik van Riel * 21944f98a2feSRik van Riel * anon in [0], file in [1] 21954f98a2feSRik van Riel */ 21962ab051e1SJerome Marchand 2197fd538803SMichal Hocko anon = lruvec_lru_size(lruvec, LRU_ACTIVE_ANON, MAX_NR_ZONES) + 2198fd538803SMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, MAX_NR_ZONES); 2199fd538803SMichal Hocko file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES) + 2200fd538803SMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, MAX_NR_ZONES); 22012ab051e1SJerome Marchand 2202599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 220358c37f6eSKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { 22046e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[0] /= 2; 22056e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[0] /= 2; 22064f98a2feSRik van Riel } 22074f98a2feSRik van Riel 22086e901571SKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { 22096e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[1] /= 2; 22106e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[1] /= 2; 22114f98a2feSRik van Riel } 22124f98a2feSRik van Riel 22134f98a2feSRik van Riel /* 221400d8089cSRik van Riel * The amount of pressure on anon vs file pages is inversely 221500d8089cSRik van Riel * proportional to the fraction of recently scanned pages on 221600d8089cSRik van Riel * each list that were recently referenced and in active use. 22174f98a2feSRik van Riel */ 2218fe35004fSSatoru Moriya ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1); 22196e901571SKOSAKI Motohiro ap /= reclaim_stat->recent_rotated[0] + 1; 22204f98a2feSRik van Riel 2221fe35004fSSatoru Moriya fp = file_prio * (reclaim_stat->recent_scanned[1] + 1); 22226e901571SKOSAKI Motohiro fp /= reclaim_stat->recent_rotated[1] + 1; 2223599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 22244f98a2feSRik van Riel 222576a33fc3SShaohua Li fraction[0] = ap; 222676a33fc3SShaohua Li fraction[1] = fp; 222776a33fc3SShaohua Li denominator = ap + fp + 1; 222876a33fc3SShaohua Li out: 22296b4f7799SJohannes Weiner *lru_pages = 0; 22304111304dSHugh Dickins for_each_evictable_lru(lru) { 22314111304dSHugh Dickins int file = is_file_lru(lru); 2232d778df51SJohannes Weiner unsigned long size; 223376a33fc3SShaohua Li unsigned long scan; 223476a33fc3SShaohua Li 223571ab6cfeSMichal Hocko size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); 2236d778df51SJohannes Weiner scan = size >> sc->priority; 2237688035f7SJohannes Weiner /* 2238688035f7SJohannes Weiner * If the cgroup's already been deleted, make sure to 2239688035f7SJohannes Weiner * scrape out the remaining cache. 2240688035f7SJohannes Weiner */ 2241688035f7SJohannes Weiner if (!scan && !mem_cgroup_online(memcg)) 2242d778df51SJohannes Weiner scan = min(size, SWAP_CLUSTER_MAX); 22439a265114SJohannes Weiner 22449a265114SJohannes Weiner switch (scan_balance) { 22459a265114SJohannes Weiner case SCAN_EQUAL: 22469a265114SJohannes Weiner /* Scan lists relative to size */ 22479a265114SJohannes Weiner break; 22489a265114SJohannes Weiner case SCAN_FRACT: 22499a265114SJohannes Weiner /* 22509a265114SJohannes Weiner * Scan types proportional to swappiness and 22519a265114SJohannes Weiner * their relative recent reclaim efficiency. 22529a265114SJohannes Weiner */ 22536f04f48dSSuleiman Souhlal scan = div64_u64(scan * fraction[file], 22546f04f48dSSuleiman Souhlal denominator); 22559a265114SJohannes Weiner break; 22569a265114SJohannes Weiner case SCAN_FILE: 22579a265114SJohannes Weiner case SCAN_ANON: 22589a265114SJohannes Weiner /* Scan one type exclusively */ 22596b4f7799SJohannes Weiner if ((scan_balance == SCAN_FILE) != file) { 22606b4f7799SJohannes Weiner size = 0; 22619a265114SJohannes Weiner scan = 0; 22626b4f7799SJohannes Weiner } 22639a265114SJohannes Weiner break; 22649a265114SJohannes Weiner default: 22659a265114SJohannes Weiner /* Look ma, no brain */ 22669a265114SJohannes Weiner BUG(); 22679a265114SJohannes Weiner } 22686b4f7799SJohannes Weiner 22696b4f7799SJohannes Weiner *lru_pages += size; 22704111304dSHugh Dickins nr[lru] = scan; 227176a33fc3SShaohua Li } 22726e08a369SWu Fengguang } 22734f98a2feSRik van Riel 22749b4f98cdSJohannes Weiner /* 2275a9dd0a83SMel Gorman * This is a basic per-node page freer. Used by both kswapd and direct reclaim. 22769b4f98cdSJohannes Weiner */ 2277a9dd0a83SMel Gorman static void shrink_node_memcg(struct pglist_data *pgdat, struct mem_cgroup *memcg, 22786b4f7799SJohannes Weiner struct scan_control *sc, unsigned long *lru_pages) 22799b4f98cdSJohannes Weiner { 2280ef8f2327SMel Gorman struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg); 22819b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 2282e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 22839b4f98cdSJohannes Weiner unsigned long nr_to_scan; 22849b4f98cdSJohannes Weiner enum lru_list lru; 22859b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 22869b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 22879b4f98cdSJohannes Weiner struct blk_plug plug; 22881a501907SMel Gorman bool scan_adjusted; 22899b4f98cdSJohannes Weiner 229033377678SVladimir Davydov get_scan_count(lruvec, memcg, sc, nr, lru_pages); 22919b4f98cdSJohannes Weiner 2292e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 2293e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 2294e82e0561SMel Gorman 22951a501907SMel Gorman /* 22961a501907SMel Gorman * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal 22971a501907SMel Gorman * event that can occur when there is little memory pressure e.g. 22981a501907SMel Gorman * multiple streaming readers/writers. Hence, we do not abort scanning 22991a501907SMel Gorman * when the requested number of pages are reclaimed when scanning at 23001a501907SMel Gorman * DEF_PRIORITY on the assumption that the fact we are direct 23011a501907SMel Gorman * reclaiming implies that kswapd is not keeping up and it is best to 23021a501907SMel Gorman * do a batch of work at once. For memcg reclaim one check is made to 23031a501907SMel Gorman * abort proportional reclaim if either the file or anon lru has already 23041a501907SMel Gorman * dropped to zero at the first pass. 23051a501907SMel Gorman */ 23061a501907SMel Gorman scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() && 23071a501907SMel Gorman sc->priority == DEF_PRIORITY); 23081a501907SMel Gorman 23099b4f98cdSJohannes Weiner blk_start_plug(&plug); 23109b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 23119b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 2312e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 2313e82e0561SMel Gorman unsigned long nr_scanned; 2314e82e0561SMel Gorman 23159b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 23169b4f98cdSJohannes Weiner if (nr[lru]) { 23179b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 23189b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 23199b4f98cdSJohannes Weiner 23209b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 23212a2e4885SJohannes Weiner lruvec, memcg, sc); 23229b4f98cdSJohannes Weiner } 23239b4f98cdSJohannes Weiner } 2324e82e0561SMel Gorman 2325bd041733SMichal Hocko cond_resched(); 2326bd041733SMichal Hocko 2327e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 2328e82e0561SMel Gorman continue; 2329e82e0561SMel Gorman 23309b4f98cdSJohannes Weiner /* 2331e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 23321a501907SMel Gorman * requested. Ensure that the anon and file LRUs are scanned 2333e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 2334e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 2335e82e0561SMel Gorman * proportional to the original scan target. 2336e82e0561SMel Gorman */ 2337e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 2338e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 2339e82e0561SMel Gorman 23401a501907SMel Gorman /* 23411a501907SMel Gorman * It's just vindictive to attack the larger once the smaller 23421a501907SMel Gorman * has gone to zero. And given the way we stop scanning the 23431a501907SMel Gorman * smaller below, this makes sure that we only make one nudge 23441a501907SMel Gorman * towards proportionality once we've got nr_to_reclaim. 23451a501907SMel Gorman */ 23461a501907SMel Gorman if (!nr_file || !nr_anon) 23471a501907SMel Gorman break; 23481a501907SMel Gorman 2349e82e0561SMel Gorman if (nr_file > nr_anon) { 2350e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 2351e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 2352e82e0561SMel Gorman lru = LRU_BASE; 2353e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 2354e82e0561SMel Gorman } else { 2355e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 2356e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 2357e82e0561SMel Gorman lru = LRU_FILE; 2358e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 2359e82e0561SMel Gorman } 2360e82e0561SMel Gorman 2361e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 2362e82e0561SMel Gorman nr[lru] = 0; 2363e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 2364e82e0561SMel Gorman 2365e82e0561SMel Gorman /* 2366e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 2367e82e0561SMel Gorman * scan target and the percentage scanning already complete 2368e82e0561SMel Gorman */ 2369e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 2370e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2371e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2372e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2373e82e0561SMel Gorman 2374e82e0561SMel Gorman lru += LRU_ACTIVE; 2375e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2376e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2377e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2378e82e0561SMel Gorman 2379e82e0561SMel Gorman scan_adjusted = true; 23809b4f98cdSJohannes Weiner } 23819b4f98cdSJohannes Weiner blk_finish_plug(&plug); 23829b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 23839b4f98cdSJohannes Weiner 23849b4f98cdSJohannes Weiner /* 23859b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 23869b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 23879b4f98cdSJohannes Weiner */ 23882a2e4885SJohannes Weiner if (inactive_list_is_low(lruvec, false, memcg, sc, true)) 23899b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 23909b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 23919b4f98cdSJohannes Weiner } 23929b4f98cdSJohannes Weiner 239323b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 23949e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 239523b9da55SMel Gorman { 2396d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 239723b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 23989e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 239923b9da55SMel Gorman return true; 240023b9da55SMel Gorman 240123b9da55SMel Gorman return false; 240223b9da55SMel Gorman } 240323b9da55SMel Gorman 24044f98a2feSRik van Riel /* 240523b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 240623b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 240723b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 240823b9da55SMel Gorman * calls try_to_compact_zone() that it will have enough free pages to succeed. 240923b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 24103e7d3449SMel Gorman */ 2411a9dd0a83SMel Gorman static inline bool should_continue_reclaim(struct pglist_data *pgdat, 24123e7d3449SMel Gorman unsigned long nr_reclaimed, 24133e7d3449SMel Gorman unsigned long nr_scanned, 24143e7d3449SMel Gorman struct scan_control *sc) 24153e7d3449SMel Gorman { 24163e7d3449SMel Gorman unsigned long pages_for_compaction; 24173e7d3449SMel Gorman unsigned long inactive_lru_pages; 2418a9dd0a83SMel Gorman int z; 24193e7d3449SMel Gorman 24203e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 24219e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 24223e7d3449SMel Gorman return false; 24233e7d3449SMel Gorman 24242876592fSMel Gorman /* Consider stopping depending on scan and reclaim activity */ 2425dcda9b04SMichal Hocko if (sc->gfp_mask & __GFP_RETRY_MAYFAIL) { 24263e7d3449SMel Gorman /* 2427dcda9b04SMichal Hocko * For __GFP_RETRY_MAYFAIL allocations, stop reclaiming if the 24282876592fSMel Gorman * full LRU list has been scanned and we are still failing 24292876592fSMel Gorman * to reclaim pages. This full LRU scan is potentially 2430dcda9b04SMichal Hocko * expensive but a __GFP_RETRY_MAYFAIL caller really wants to succeed 24313e7d3449SMel Gorman */ 24323e7d3449SMel Gorman if (!nr_reclaimed && !nr_scanned) 24333e7d3449SMel Gorman return false; 24342876592fSMel Gorman } else { 24352876592fSMel Gorman /* 2436dcda9b04SMichal Hocko * For non-__GFP_RETRY_MAYFAIL allocations which can presumably 24372876592fSMel Gorman * fail without consequence, stop if we failed to reclaim 24382876592fSMel Gorman * any pages from the last SWAP_CLUSTER_MAX number of 24392876592fSMel Gorman * pages that were scanned. This will return to the 24402876592fSMel Gorman * caller faster at the risk reclaim/compaction and 24412876592fSMel Gorman * the resulting allocation attempt fails 24422876592fSMel Gorman */ 24432876592fSMel Gorman if (!nr_reclaimed) 24442876592fSMel Gorman return false; 24452876592fSMel Gorman } 24463e7d3449SMel Gorman 24473e7d3449SMel Gorman /* 24483e7d3449SMel Gorman * If we have not reclaimed enough pages for compaction and the 24493e7d3449SMel Gorman * inactive lists are large enough, continue reclaiming 24503e7d3449SMel Gorman */ 24519861a62cSVlastimil Babka pages_for_compaction = compact_gap(sc->order); 2452a9dd0a83SMel Gorman inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); 2453ec8acf20SShaohua Li if (get_nr_swap_pages() > 0) 2454a9dd0a83SMel Gorman inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); 24553e7d3449SMel Gorman if (sc->nr_reclaimed < pages_for_compaction && 24563e7d3449SMel Gorman inactive_lru_pages > pages_for_compaction) 24573e7d3449SMel Gorman return true; 24583e7d3449SMel Gorman 24593e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 2460a9dd0a83SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 2461a9dd0a83SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 24626aa303deSMel Gorman if (!managed_zone(zone)) 2463a9dd0a83SMel Gorman continue; 2464a9dd0a83SMel Gorman 2465a9dd0a83SMel Gorman switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) { 2466cf378319SVlastimil Babka case COMPACT_SUCCESS: 24673e7d3449SMel Gorman case COMPACT_CONTINUE: 24683e7d3449SMel Gorman return false; 24693e7d3449SMel Gorman default: 2470a9dd0a83SMel Gorman /* check next zone */ 2471a9dd0a83SMel Gorman ; 24723e7d3449SMel Gorman } 24733e7d3449SMel Gorman } 2474a9dd0a83SMel Gorman return true; 2475a9dd0a83SMel Gorman } 24763e7d3449SMel Gorman 2477970a39a3SMel Gorman static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc) 2478f16015fbSJohannes Weiner { 2479cb731d6cSVladimir Davydov struct reclaim_state *reclaim_state = current->reclaim_state; 24809b4f98cdSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 24812344d7e4SJohannes Weiner bool reclaimable = false; 24829b4f98cdSJohannes Weiner 24839b4f98cdSJohannes Weiner do { 24845660048cSJohannes Weiner struct mem_cgroup *root = sc->target_mem_cgroup; 24855660048cSJohannes Weiner struct mem_cgroup_reclaim_cookie reclaim = { 2486ef8f2327SMel Gorman .pgdat = pgdat, 24879e3b2f8cSKonstantin Khlebnikov .priority = sc->priority, 24885660048cSJohannes Weiner }; 2489a9dd0a83SMel Gorman unsigned long node_lru_pages = 0; 2490694fbc0fSAndrew Morton struct mem_cgroup *memcg; 24915660048cSJohannes Weiner 2492*d108c772SAndrey Ryabinin memset(&sc->nr, 0, sizeof(sc->nr)); 2493*d108c772SAndrey Ryabinin 24949b4f98cdSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 24959b4f98cdSJohannes Weiner nr_scanned = sc->nr_scanned; 24969b4f98cdSJohannes Weiner 2497694fbc0fSAndrew Morton memcg = mem_cgroup_iter(root, NULL, &reclaim); 2498694fbc0fSAndrew Morton do { 24996b4f7799SJohannes Weiner unsigned long lru_pages; 25008e8ae645SJohannes Weiner unsigned long reclaimed; 2501cb731d6cSVladimir Davydov unsigned long scanned; 25029b4f98cdSJohannes Weiner 2503241994edSJohannes Weiner if (mem_cgroup_low(root, memcg)) { 2504d6622f63SYisheng Xie if (!sc->memcg_low_reclaim) { 2505d6622f63SYisheng Xie sc->memcg_low_skipped = 1; 2506241994edSJohannes Weiner continue; 2507d6622f63SYisheng Xie } 250831176c78SJohannes Weiner mem_cgroup_event(memcg, MEMCG_LOW); 2509241994edSJohannes Weiner } 2510241994edSJohannes Weiner 25118e8ae645SJohannes Weiner reclaimed = sc->nr_reclaimed; 2512cb731d6cSVladimir Davydov scanned = sc->nr_scanned; 2513a9dd0a83SMel Gorman shrink_node_memcg(pgdat, memcg, sc, &lru_pages); 2514a9dd0a83SMel Gorman node_lru_pages += lru_pages; 2515f9be23d6SKonstantin Khlebnikov 2516b5afba29SVladimir Davydov if (memcg) 2517a9dd0a83SMel Gorman shrink_slab(sc->gfp_mask, pgdat->node_id, 25189092c71bSJosef Bacik memcg, sc->priority); 2519cb731d6cSVladimir Davydov 25208e8ae645SJohannes Weiner /* Record the group's reclaim efficiency */ 25218e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, memcg, false, 25228e8ae645SJohannes Weiner sc->nr_scanned - scanned, 25238e8ae645SJohannes Weiner sc->nr_reclaimed - reclaimed); 25248e8ae645SJohannes Weiner 25255660048cSJohannes Weiner /* 2526a394cb8eSMichal Hocko * Direct reclaim and kswapd have to scan all memory 2527a394cb8eSMichal Hocko * cgroups to fulfill the overall scan target for the 2528a9dd0a83SMel Gorman * node. 2529a394cb8eSMichal Hocko * 2530a394cb8eSMichal Hocko * Limit reclaim, on the other hand, only cares about 2531a394cb8eSMichal Hocko * nr_to_reclaim pages to be reclaimed and it will 2532a394cb8eSMichal Hocko * retry with decreasing priority if one round over the 2533a394cb8eSMichal Hocko * whole hierarchy is not sufficient. 25345660048cSJohannes Weiner */ 2535a394cb8eSMichal Hocko if (!global_reclaim(sc) && 2536a394cb8eSMichal Hocko sc->nr_reclaimed >= sc->nr_to_reclaim) { 25375660048cSJohannes Weiner mem_cgroup_iter_break(root, memcg); 25385660048cSJohannes Weiner break; 25395660048cSJohannes Weiner } 2540241994edSJohannes Weiner } while ((memcg = mem_cgroup_iter(root, memcg, &reclaim))); 254170ddf637SAnton Vorontsov 2542b2e18757SMel Gorman if (global_reclaim(sc)) 2543a9dd0a83SMel Gorman shrink_slab(sc->gfp_mask, pgdat->node_id, NULL, 25449092c71bSJosef Bacik sc->priority); 25456b4f7799SJohannes Weiner 25466b4f7799SJohannes Weiner if (reclaim_state) { 2547cb731d6cSVladimir Davydov sc->nr_reclaimed += reclaim_state->reclaimed_slab; 25486b4f7799SJohannes Weiner reclaim_state->reclaimed_slab = 0; 25496b4f7799SJohannes Weiner } 25506b4f7799SJohannes Weiner 25518e8ae645SJohannes Weiner /* Record the subtree's reclaim efficiency */ 25528e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, 255370ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 255470ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 255570ddf637SAnton Vorontsov 25562344d7e4SJohannes Weiner if (sc->nr_reclaimed - nr_reclaimed) 25572344d7e4SJohannes Weiner reclaimable = true; 25582344d7e4SJohannes Weiner 2559*d108c772SAndrey Ryabinin /* 2560*d108c772SAndrey Ryabinin * If reclaim is isolating dirty pages under writeback, it 2561*d108c772SAndrey Ryabinin * implies that the long-lived page allocation rate is exceeding 2562*d108c772SAndrey Ryabinin * the page laundering rate. Either the global limits are not 2563*d108c772SAndrey Ryabinin * being effective at throttling processes due to the page 2564*d108c772SAndrey Ryabinin * distribution throughout zones or there is heavy usage of a 2565*d108c772SAndrey Ryabinin * slow backing device. The only option is to throttle from 2566*d108c772SAndrey Ryabinin * reclaim context which is not ideal as there is no guarantee 2567*d108c772SAndrey Ryabinin * the dirtying process is throttled in the same way 2568*d108c772SAndrey Ryabinin * balance_dirty_pages() manages. 2569*d108c772SAndrey Ryabinin * 2570*d108c772SAndrey Ryabinin * Once a node is flagged PGDAT_WRITEBACK, kswapd will count the 2571*d108c772SAndrey Ryabinin * number of pages under pages flagged for immediate reclaim and 2572*d108c772SAndrey Ryabinin * stall if any are encountered in the nr_immediate check below. 2573*d108c772SAndrey Ryabinin */ 2574*d108c772SAndrey Ryabinin if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken) 2575*d108c772SAndrey Ryabinin set_bit(PGDAT_WRITEBACK, &pgdat->flags); 2576*d108c772SAndrey Ryabinin 2577*d108c772SAndrey Ryabinin /* 2578*d108c772SAndrey Ryabinin * Legacy memcg will stall in page writeback so avoid forcibly 2579*d108c772SAndrey Ryabinin * stalling here. 2580*d108c772SAndrey Ryabinin */ 2581*d108c772SAndrey Ryabinin if (sane_reclaim(sc)) { 2582*d108c772SAndrey Ryabinin /* 2583*d108c772SAndrey Ryabinin * Tag a node as congested if all the dirty pages 2584*d108c772SAndrey Ryabinin * scanned were backed by a congested BDI and 2585*d108c772SAndrey Ryabinin * wait_iff_congested will stall. 2586*d108c772SAndrey Ryabinin */ 2587*d108c772SAndrey Ryabinin if (sc->nr.dirty && sc->nr.dirty == sc->nr.congested) 2588*d108c772SAndrey Ryabinin set_bit(PGDAT_CONGESTED, &pgdat->flags); 2589*d108c772SAndrey Ryabinin 2590*d108c772SAndrey Ryabinin /* Allow kswapd to start writing pages during reclaim.*/ 2591*d108c772SAndrey Ryabinin if (sc->nr.unqueued_dirty == sc->nr.file_taken) 2592*d108c772SAndrey Ryabinin set_bit(PGDAT_DIRTY, &pgdat->flags); 2593*d108c772SAndrey Ryabinin 2594*d108c772SAndrey Ryabinin /* 2595*d108c772SAndrey Ryabinin * If kswapd scans pages marked marked for immediate 2596*d108c772SAndrey Ryabinin * reclaim and under writeback (nr_immediate), it 2597*d108c772SAndrey Ryabinin * implies that pages are cycling through the LRU 2598*d108c772SAndrey Ryabinin * faster than they are written so also forcibly stall. 2599*d108c772SAndrey Ryabinin */ 2600*d108c772SAndrey Ryabinin if (sc->nr.immediate) 2601*d108c772SAndrey Ryabinin congestion_wait(BLK_RW_ASYNC, HZ/10); 2602*d108c772SAndrey Ryabinin } 2603*d108c772SAndrey Ryabinin 2604*d108c772SAndrey Ryabinin /* 2605*d108c772SAndrey Ryabinin * Stall direct reclaim for IO completions if underlying BDIs 2606*d108c772SAndrey Ryabinin * and node is congested. Allow kswapd to continue until it 2607*d108c772SAndrey Ryabinin * starts encountering unqueued dirty pages or cycling through 2608*d108c772SAndrey Ryabinin * the LRU too quickly. 2609*d108c772SAndrey Ryabinin */ 2610*d108c772SAndrey Ryabinin if (!sc->hibernation_mode && !current_is_kswapd() && 2611*d108c772SAndrey Ryabinin current_may_throttle()) 2612*d108c772SAndrey Ryabinin wait_iff_congested(pgdat, BLK_RW_ASYNC, HZ/10); 2613*d108c772SAndrey Ryabinin 2614a9dd0a83SMel Gorman } while (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, 26159b4f98cdSJohannes Weiner sc->nr_scanned - nr_scanned, sc)); 26162344d7e4SJohannes Weiner 2617c73322d0SJohannes Weiner /* 2618c73322d0SJohannes Weiner * Kswapd gives up on balancing particular nodes after too 2619c73322d0SJohannes Weiner * many failures to reclaim anything from them and goes to 2620c73322d0SJohannes Weiner * sleep. On reclaim progress, reset the failure counter. A 2621c73322d0SJohannes Weiner * successful direct reclaim run will revive a dormant kswapd. 2622c73322d0SJohannes Weiner */ 2623c73322d0SJohannes Weiner if (reclaimable) 2624c73322d0SJohannes Weiner pgdat->kswapd_failures = 0; 2625c73322d0SJohannes Weiner 26262344d7e4SJohannes Weiner return reclaimable; 2627f16015fbSJohannes Weiner } 2628f16015fbSJohannes Weiner 262953853e2dSVlastimil Babka /* 2630fdd4c614SVlastimil Babka * Returns true if compaction should go ahead for a costly-order request, or 2631fdd4c614SVlastimil Babka * the allocation would already succeed without compaction. Return false if we 2632fdd4c614SVlastimil Babka * should reclaim first. 263353853e2dSVlastimil Babka */ 26344f588331SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 2635fe4b1b24SMel Gorman { 263631483b6aSMel Gorman unsigned long watermark; 2637fdd4c614SVlastimil Babka enum compact_result suitable; 2638fe4b1b24SMel Gorman 2639fdd4c614SVlastimil Babka suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx); 2640fdd4c614SVlastimil Babka if (suitable == COMPACT_SUCCESS) 2641fdd4c614SVlastimil Babka /* Allocation should succeed already. Don't reclaim. */ 2642fdd4c614SVlastimil Babka return true; 2643fdd4c614SVlastimil Babka if (suitable == COMPACT_SKIPPED) 2644fdd4c614SVlastimil Babka /* Compaction cannot yet proceed. Do reclaim. */ 2645fe4b1b24SMel Gorman return false; 2646fe4b1b24SMel Gorman 2647fdd4c614SVlastimil Babka /* 2648fdd4c614SVlastimil Babka * Compaction is already possible, but it takes time to run and there 2649fdd4c614SVlastimil Babka * are potentially other callers using the pages just freed. So proceed 2650fdd4c614SVlastimil Babka * with reclaim to make a buffer of free pages available to give 2651fdd4c614SVlastimil Babka * compaction a reasonable chance of completing and allocating the page. 2652fdd4c614SVlastimil Babka * Note that we won't actually reclaim the whole buffer in one attempt 2653fdd4c614SVlastimil Babka * as the target watermark in should_continue_reclaim() is lower. But if 2654fdd4c614SVlastimil Babka * we are already above the high+gap watermark, don't reclaim at all. 2655fdd4c614SVlastimil Babka */ 2656fdd4c614SVlastimil Babka watermark = high_wmark_pages(zone) + compact_gap(sc->order); 2657fdd4c614SVlastimil Babka 2658fdd4c614SVlastimil Babka return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); 2659fe4b1b24SMel Gorman } 2660fe4b1b24SMel Gorman 26611da177e4SLinus Torvalds /* 26621da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 26631da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 26641da177e4SLinus Torvalds * request. 26651da177e4SLinus Torvalds * 26661da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 26671da177e4SLinus Torvalds * scan then give up on it. 26681da177e4SLinus Torvalds */ 26690a0337e0SMichal Hocko static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 26701da177e4SLinus Torvalds { 2671dd1a239fSMel Gorman struct zoneref *z; 267254a6eb5cSMel Gorman struct zone *zone; 26730608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 26740608f43dSAndrew Morton unsigned long nr_soft_scanned; 2675619d0d76SWeijie Yang gfp_t orig_mask; 267679dafcdcSMel Gorman pg_data_t *last_pgdat = NULL; 26771cfb419bSKAMEZAWA Hiroyuki 2678cc715d99SMel Gorman /* 2679cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 2680cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 2681cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 2682cc715d99SMel Gorman */ 2683619d0d76SWeijie Yang orig_mask = sc->gfp_mask; 2684b2e18757SMel Gorman if (buffer_heads_over_limit) { 2685cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 26864f588331SMel Gorman sc->reclaim_idx = gfp_zone(sc->gfp_mask); 2687b2e18757SMel Gorman } 2688cc715d99SMel Gorman 2689d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 2690b2e18757SMel Gorman sc->reclaim_idx, sc->nodemask) { 2691b2e18757SMel Gorman /* 26921cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 26931cfb419bSKAMEZAWA Hiroyuki * to global LRU. 26941cfb419bSKAMEZAWA Hiroyuki */ 269589b5fae5SJohannes Weiner if (global_reclaim(sc)) { 2696344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, 2697344736f2SVladimir Davydov GFP_KERNEL | __GFP_HARDWALL)) 26981da177e4SLinus Torvalds continue; 269965ec02cbSVladimir Davydov 2700e0887c19SRik van Riel /* 2701e0c23279SMel Gorman * If we already have plenty of memory free for 2702e0c23279SMel Gorman * compaction in this zone, don't free any more. 2703e0c23279SMel Gorman * Even though compaction is invoked for any 2704e0c23279SMel Gorman * non-zero order, only frequent costly order 2705e0c23279SMel Gorman * reclamation is disruptive enough to become a 2706c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 2707c7cfa37bSCopot Alexandru * page allocations. 2708e0887c19SRik van Riel */ 27090b06496aSJohannes Weiner if (IS_ENABLED(CONFIG_COMPACTION) && 27100b06496aSJohannes Weiner sc->order > PAGE_ALLOC_COSTLY_ORDER && 27114f588331SMel Gorman compaction_ready(zone, sc)) { 27120b06496aSJohannes Weiner sc->compaction_ready = true; 2713e0887c19SRik van Riel continue; 2714e0887c19SRik van Riel } 27150b06496aSJohannes Weiner 27160608f43dSAndrew Morton /* 271779dafcdcSMel Gorman * Shrink each node in the zonelist once. If the 271879dafcdcSMel Gorman * zonelist is ordered by zone (not the default) then a 271979dafcdcSMel Gorman * node may be shrunk multiple times but in that case 272079dafcdcSMel Gorman * the user prefers lower zones being preserved. 272179dafcdcSMel Gorman */ 272279dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 272379dafcdcSMel Gorman continue; 272479dafcdcSMel Gorman 272579dafcdcSMel Gorman /* 27260608f43dSAndrew Morton * This steals pages from memory cgroups over softlimit 27270608f43dSAndrew Morton * and returns the number of reclaimed pages and 27280608f43dSAndrew Morton * scanned pages. This works for global memory pressure 27290608f43dSAndrew Morton * and balancing, not for a memcg's limit. 27300608f43dSAndrew Morton */ 27310608f43dSAndrew Morton nr_soft_scanned = 0; 2732ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, 27330608f43dSAndrew Morton sc->order, sc->gfp_mask, 27340608f43dSAndrew Morton &nr_soft_scanned); 27350608f43dSAndrew Morton sc->nr_reclaimed += nr_soft_reclaimed; 27360608f43dSAndrew Morton sc->nr_scanned += nr_soft_scanned; 2737ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 2738ac34a1a3SKAMEZAWA Hiroyuki } 2739d149e3b2SYing Han 274079dafcdcSMel Gorman /* See comment about same check for global reclaim above */ 274179dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 274279dafcdcSMel Gorman continue; 274379dafcdcSMel Gorman last_pgdat = zone->zone_pgdat; 2744970a39a3SMel Gorman shrink_node(zone->zone_pgdat, sc); 27451da177e4SLinus Torvalds } 2746e0c23279SMel Gorman 274765ec02cbSVladimir Davydov /* 2748619d0d76SWeijie Yang * Restore to original mask to avoid the impact on the caller if we 2749619d0d76SWeijie Yang * promoted it to __GFP_HIGHMEM. 2750619d0d76SWeijie Yang */ 2751619d0d76SWeijie Yang sc->gfp_mask = orig_mask; 27521da177e4SLinus Torvalds } 27531da177e4SLinus Torvalds 27542a2e4885SJohannes Weiner static void snapshot_refaults(struct mem_cgroup *root_memcg, pg_data_t *pgdat) 27552a2e4885SJohannes Weiner { 27562a2e4885SJohannes Weiner struct mem_cgroup *memcg; 27572a2e4885SJohannes Weiner 27582a2e4885SJohannes Weiner memcg = mem_cgroup_iter(root_memcg, NULL, NULL); 27592a2e4885SJohannes Weiner do { 27602a2e4885SJohannes Weiner unsigned long refaults; 27612a2e4885SJohannes Weiner struct lruvec *lruvec; 27622a2e4885SJohannes Weiner 27632a2e4885SJohannes Weiner if (memcg) 2764ccda7f43SJohannes Weiner refaults = memcg_page_state(memcg, WORKINGSET_ACTIVATE); 27652a2e4885SJohannes Weiner else 27662a2e4885SJohannes Weiner refaults = node_page_state(pgdat, WORKINGSET_ACTIVATE); 27672a2e4885SJohannes Weiner 27682a2e4885SJohannes Weiner lruvec = mem_cgroup_lruvec(pgdat, memcg); 27692a2e4885SJohannes Weiner lruvec->refaults = refaults; 27702a2e4885SJohannes Weiner } while ((memcg = mem_cgroup_iter(root_memcg, memcg, NULL))); 27712a2e4885SJohannes Weiner } 27722a2e4885SJohannes Weiner 27731da177e4SLinus Torvalds /* 27741da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 27751da177e4SLinus Torvalds * 27761da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 27771da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 27781da177e4SLinus Torvalds * 27791da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 27801da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 27815b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 27825b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 27835b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 27845b0830cbSJens Axboe * work, and the allocation attempt will fail. 2785a41f24eaSNishanth Aravamudan * 2786a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 2787a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 27881da177e4SLinus Torvalds */ 2789dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 27903115cd91SVladimir Davydov struct scan_control *sc) 27911da177e4SLinus Torvalds { 2792241994edSJohannes Weiner int initial_priority = sc->priority; 27932a2e4885SJohannes Weiner pg_data_t *last_pgdat; 27942a2e4885SJohannes Weiner struct zoneref *z; 27952a2e4885SJohannes Weiner struct zone *zone; 2796241994edSJohannes Weiner retry: 2797873b4771SKeika Kobayashi delayacct_freepages_start(); 2798873b4771SKeika Kobayashi 279989b5fae5SJohannes Weiner if (global_reclaim(sc)) 28007cc30fcfSMel Gorman __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); 28011da177e4SLinus Torvalds 28029e3b2f8cSKonstantin Khlebnikov do { 280370ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 280470ddf637SAnton Vorontsov sc->priority); 280566e1707bSBalbir Singh sc->nr_scanned = 0; 28060a0337e0SMichal Hocko shrink_zones(zonelist, sc); 2807e0c23279SMel Gorman 2808bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 28090b06496aSJohannes Weiner break; 28100b06496aSJohannes Weiner 28110b06496aSJohannes Weiner if (sc->compaction_ready) 28120b06496aSJohannes Weiner break; 28131da177e4SLinus Torvalds 28141da177e4SLinus Torvalds /* 28150e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 28160e50ce3bSMinchan Kim * writepage even in laptop mode. 28170e50ce3bSMinchan Kim */ 28180e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 28190e50ce3bSMinchan Kim sc->may_writepage = 1; 28200b06496aSJohannes Weiner } while (--sc->priority >= 0); 2821bb21c7ceSKOSAKI Motohiro 28222a2e4885SJohannes Weiner last_pgdat = NULL; 28232a2e4885SJohannes Weiner for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx, 28242a2e4885SJohannes Weiner sc->nodemask) { 28252a2e4885SJohannes Weiner if (zone->zone_pgdat == last_pgdat) 28262a2e4885SJohannes Weiner continue; 28272a2e4885SJohannes Weiner last_pgdat = zone->zone_pgdat; 28282a2e4885SJohannes Weiner snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat); 28292a2e4885SJohannes Weiner } 28302a2e4885SJohannes Weiner 2831873b4771SKeika Kobayashi delayacct_freepages_end(); 2832873b4771SKeika Kobayashi 2833bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 2834bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 2835bb21c7ceSKOSAKI Motohiro 28360cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 28370b06496aSJohannes Weiner if (sc->compaction_ready) 28387335084dSMel Gorman return 1; 28397335084dSMel Gorman 2840241994edSJohannes Weiner /* Untapped cgroup reserves? Don't OOM, retry. */ 2841d6622f63SYisheng Xie if (sc->memcg_low_skipped) { 2842241994edSJohannes Weiner sc->priority = initial_priority; 2843d6622f63SYisheng Xie sc->memcg_low_reclaim = 1; 2844d6622f63SYisheng Xie sc->memcg_low_skipped = 0; 2845241994edSJohannes Weiner goto retry; 2846241994edSJohannes Weiner } 2847241994edSJohannes Weiner 2848bb21c7ceSKOSAKI Motohiro return 0; 28491da177e4SLinus Torvalds } 28501da177e4SLinus Torvalds 2851c73322d0SJohannes Weiner static bool allow_direct_reclaim(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 2859c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 2860c73322d0SJohannes Weiner return true; 2861c73322d0SJohannes Weiner 28625515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 28635515061dSMel Gorman zone = &pgdat->node_zones[i]; 2864d450abd8SJohannes Weiner if (!managed_zone(zone)) 2865d450abd8SJohannes Weiner continue; 2866d450abd8SJohannes Weiner 2867d450abd8SJohannes Weiner if (!zone_reclaimable_pages(zone)) 2868675becceSMel Gorman continue; 2869675becceSMel Gorman 28705515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 28715515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 28725515061dSMel Gorman } 28735515061dSMel Gorman 2874675becceSMel Gorman /* If there are no reserves (unexpected config) then do not throttle */ 2875675becceSMel Gorman if (!pfmemalloc_reserve) 2876675becceSMel Gorman return true; 2877675becceSMel Gorman 28785515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 28795515061dSMel Gorman 28805515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 28815515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 288238087d9bSMel Gorman pgdat->kswapd_classzone_idx = min(pgdat->kswapd_classzone_idx, 28835515061dSMel Gorman (enum zone_type)ZONE_NORMAL); 28845515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 28855515061dSMel Gorman } 28865515061dSMel Gorman 28875515061dSMel Gorman return wmark_ok; 28885515061dSMel Gorman } 28895515061dSMel Gorman 28905515061dSMel Gorman /* 28915515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 28925515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 28935515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 289450694c28SMel Gorman * when the low watermark is reached. 289550694c28SMel Gorman * 289650694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 289750694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 28985515061dSMel Gorman */ 289950694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 29005515061dSMel Gorman nodemask_t *nodemask) 29015515061dSMel Gorman { 2902675becceSMel Gorman struct zoneref *z; 29035515061dSMel Gorman struct zone *zone; 2904675becceSMel Gorman pg_data_t *pgdat = NULL; 29055515061dSMel Gorman 29065515061dSMel Gorman /* 29075515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 29085515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 29095515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 29105515061dSMel Gorman * committing a transaction where throttling it could forcing other 29115515061dSMel Gorman * processes to block on log_wait_commit(). 29125515061dSMel Gorman */ 29135515061dSMel Gorman if (current->flags & PF_KTHREAD) 291450694c28SMel Gorman goto out; 291550694c28SMel Gorman 291650694c28SMel Gorman /* 291750694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 291850694c28SMel Gorman * It should return quickly so it can exit and free its memory 291950694c28SMel Gorman */ 292050694c28SMel Gorman if (fatal_signal_pending(current)) 292150694c28SMel Gorman goto out; 29225515061dSMel Gorman 2923675becceSMel Gorman /* 2924675becceSMel Gorman * Check if the pfmemalloc reserves are ok by finding the first node 2925675becceSMel Gorman * with a usable ZONE_NORMAL or lower zone. The expectation is that 2926675becceSMel Gorman * GFP_KERNEL will be required for allocating network buffers when 2927675becceSMel Gorman * swapping over the network so ZONE_HIGHMEM is unusable. 2928675becceSMel Gorman * 2929675becceSMel Gorman * Throttling is based on the first usable node and throttled processes 2930675becceSMel Gorman * wait on a queue until kswapd makes progress and wakes them. There 2931675becceSMel Gorman * is an affinity then between processes waking up and where reclaim 2932675becceSMel Gorman * progress has been made assuming the process wakes on the same node. 2933675becceSMel Gorman * More importantly, processes running on remote nodes will not compete 2934675becceSMel Gorman * for remote pfmemalloc reserves and processes on different nodes 2935675becceSMel Gorman * should make reasonable progress. 2936675becceSMel Gorman */ 2937675becceSMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 293817636faaSMichael S. Tsirkin gfp_zone(gfp_mask), nodemask) { 2939675becceSMel Gorman if (zone_idx(zone) > ZONE_NORMAL) 2940675becceSMel Gorman continue; 2941675becceSMel Gorman 2942675becceSMel Gorman /* Throttle based on the first usable node */ 29435515061dSMel Gorman pgdat = zone->zone_pgdat; 2944c73322d0SJohannes Weiner if (allow_direct_reclaim(pgdat)) 294550694c28SMel Gorman goto out; 2946675becceSMel Gorman break; 2947675becceSMel Gorman } 2948675becceSMel Gorman 2949675becceSMel Gorman /* If no zone was usable by the allocation flags then do not throttle */ 2950675becceSMel Gorman if (!pgdat) 2951675becceSMel Gorman goto out; 29525515061dSMel Gorman 295368243e76SMel Gorman /* Account for the throttling */ 295468243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 295568243e76SMel Gorman 29565515061dSMel Gorman /* 29575515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 29585515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 29595515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 29605515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 29615515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 29625515061dSMel Gorman * second before continuing. 29635515061dSMel Gorman */ 29645515061dSMel Gorman if (!(gfp_mask & __GFP_FS)) { 29655515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 2966c73322d0SJohannes Weiner allow_direct_reclaim(pgdat), HZ); 296750694c28SMel Gorman 296850694c28SMel Gorman goto check_pending; 29695515061dSMel Gorman } 29705515061dSMel Gorman 29715515061dSMel Gorman /* Throttle until kswapd wakes the process */ 29725515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 2973c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)); 297450694c28SMel Gorman 297550694c28SMel Gorman check_pending: 297650694c28SMel Gorman if (fatal_signal_pending(current)) 297750694c28SMel Gorman return true; 297850694c28SMel Gorman 297950694c28SMel Gorman out: 298050694c28SMel Gorman return false; 29815515061dSMel Gorman } 29825515061dSMel Gorman 2983dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 2984327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 298566e1707bSBalbir Singh { 298633906bc5SMel Gorman unsigned long nr_reclaimed; 298766e1707bSBalbir Singh struct scan_control sc = { 298822fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 2989f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 2990b2e18757SMel Gorman .reclaim_idx = gfp_zone(gfp_mask), 2991ee814fe2SJohannes Weiner .order = order, 2992ee814fe2SJohannes Weiner .nodemask = nodemask, 2993ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 2994ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 2995a6dc60f8SJohannes Weiner .may_unmap = 1, 29962e2e4259SKOSAKI Motohiro .may_swap = 1, 299766e1707bSBalbir Singh }; 299866e1707bSBalbir Singh 29995515061dSMel Gorman /* 300050694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 300150694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 300250694c28SMel Gorman * point. 30035515061dSMel Gorman */ 3004f2f43e56SNick Desaulniers if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask)) 30055515061dSMel Gorman return 1; 30065515061dSMel Gorman 300733906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_begin(order, 300833906bc5SMel Gorman sc.may_writepage, 3009f2f43e56SNick Desaulniers sc.gfp_mask, 3010e5146b12SMel Gorman sc.reclaim_idx); 301133906bc5SMel Gorman 30123115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 301333906bc5SMel Gorman 301433906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 301533906bc5SMel Gorman 301633906bc5SMel Gorman return nr_reclaimed; 301766e1707bSBalbir Singh } 301866e1707bSBalbir Singh 3019c255a458SAndrew Morton #ifdef CONFIG_MEMCG 302066e1707bSBalbir Singh 3021a9dd0a83SMel Gorman unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, 30224e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 3023ef8f2327SMel Gorman pg_data_t *pgdat, 30240ae5e89cSYing Han unsigned long *nr_scanned) 30254e416953SBalbir Singh { 30264e416953SBalbir Singh struct scan_control sc = { 3027b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3028ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 30294e416953SBalbir Singh .may_writepage = !laptop_mode, 30304e416953SBalbir Singh .may_unmap = 1, 3031b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 30324e416953SBalbir Singh .may_swap = !noswap, 30334e416953SBalbir Singh }; 30346b4f7799SJohannes Weiner unsigned long lru_pages; 30350ae5e89cSYing Han 30364e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 30374e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 3038bdce6d9eSKOSAKI Motohiro 30399e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 3040bdce6d9eSKOSAKI Motohiro sc.may_writepage, 3041e5146b12SMel Gorman sc.gfp_mask, 3042e5146b12SMel Gorman sc.reclaim_idx); 3043bdce6d9eSKOSAKI Motohiro 30444e416953SBalbir Singh /* 30454e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 30464e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 3047a9dd0a83SMel Gorman * if we don't reclaim here, the shrink_node from balance_pgdat 30484e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 30494e416953SBalbir Singh * the priority and make it zero. 30504e416953SBalbir Singh */ 3051ef8f2327SMel Gorman shrink_node_memcg(pgdat, memcg, &sc, &lru_pages); 3052bdce6d9eSKOSAKI Motohiro 3053bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 3054bdce6d9eSKOSAKI Motohiro 30550ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 30564e416953SBalbir Singh return sc.nr_reclaimed; 30574e416953SBalbir Singh } 30584e416953SBalbir Singh 305972835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 3060b70a2a21SJohannes Weiner unsigned long nr_pages, 30618c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 3062b70a2a21SJohannes Weiner bool may_swap) 306366e1707bSBalbir Singh { 30644e416953SBalbir Singh struct zonelist *zonelist; 3065bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 3066889976dbSYing Han int nid; 3067499118e9SVlastimil Babka unsigned int noreclaim_flag; 306866e1707bSBalbir Singh struct scan_control sc = { 3069b70a2a21SJohannes Weiner .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 30707dea19f9SMichal Hocko .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | 3071ee814fe2SJohannes Weiner (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 3072b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 3073ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 3074ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 307566e1707bSBalbir Singh .may_writepage = !laptop_mode, 3076a6dc60f8SJohannes Weiner .may_unmap = 1, 3077b70a2a21SJohannes Weiner .may_swap = may_swap, 3078a09ed5e0SYing Han }; 307966e1707bSBalbir Singh 3080889976dbSYing Han /* 3081889976dbSYing Han * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't 3082889976dbSYing Han * take care of from where we get pages. So the node where we start the 3083889976dbSYing Han * scan does not need to be the current node. 3084889976dbSYing Han */ 308572835c86SJohannes Weiner nid = mem_cgroup_select_victim_node(memcg); 3086889976dbSYing Han 3087c9634cf0SAneesh Kumar K.V zonelist = &NODE_DATA(nid)->node_zonelists[ZONELIST_FALLBACK]; 3088bdce6d9eSKOSAKI Motohiro 3089bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_begin(0, 3090bdce6d9eSKOSAKI Motohiro sc.may_writepage, 3091e5146b12SMel Gorman sc.gfp_mask, 3092e5146b12SMel Gorman sc.reclaim_idx); 3093bdce6d9eSKOSAKI Motohiro 3094499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 30953115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3096499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 3097bdce6d9eSKOSAKI Motohiro 3098bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 3099bdce6d9eSKOSAKI Motohiro 3100bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 310166e1707bSBalbir Singh } 310266e1707bSBalbir Singh #endif 310366e1707bSBalbir Singh 31041d82de61SMel Gorman static void age_active_anon(struct pglist_data *pgdat, 3105ef8f2327SMel Gorman struct scan_control *sc) 3106f16015fbSJohannes Weiner { 3107b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 3108b95a2f2dSJohannes Weiner 3109b95a2f2dSJohannes Weiner if (!total_swap_pages) 3110b95a2f2dSJohannes Weiner return; 3111b95a2f2dSJohannes Weiner 3112b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 3113b95a2f2dSJohannes Weiner do { 3114ef8f2327SMel Gorman struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg); 3115f16015fbSJohannes Weiner 31162a2e4885SJohannes Weiner if (inactive_list_is_low(lruvec, false, memcg, sc, true)) 31171a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 31189e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 3119b95a2f2dSJohannes Weiner 3120b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 3121b95a2f2dSJohannes Weiner } while (memcg); 3122f16015fbSJohannes Weiner } 3123f16015fbSJohannes Weiner 3124e716f2ebSMel Gorman /* 3125e716f2ebSMel Gorman * Returns true if there is an eligible zone balanced for the request order 3126e716f2ebSMel Gorman * and classzone_idx 3127e716f2ebSMel Gorman */ 3128e716f2ebSMel Gorman static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx) 312960cefed4SJohannes Weiner { 3130e716f2ebSMel Gorman int i; 3131e716f2ebSMel Gorman unsigned long mark = -1; 3132e716f2ebSMel Gorman struct zone *zone; 313360cefed4SJohannes Weiner 3134e716f2ebSMel Gorman for (i = 0; i <= classzone_idx; i++) { 3135e716f2ebSMel Gorman zone = pgdat->node_zones + i; 31366256c6b4SMel Gorman 3137e716f2ebSMel Gorman if (!managed_zone(zone)) 3138e716f2ebSMel Gorman continue; 3139e716f2ebSMel Gorman 3140e716f2ebSMel Gorman mark = high_wmark_pages(zone); 3141e716f2ebSMel Gorman if (zone_watermark_ok_safe(zone, order, mark, classzone_idx)) 31426256c6b4SMel Gorman return true; 314360cefed4SJohannes Weiner } 314460cefed4SJohannes Weiner 3145e716f2ebSMel Gorman /* 3146e716f2ebSMel Gorman * If a node has no populated zone within classzone_idx, it does not 3147e716f2ebSMel Gorman * need balancing by definition. This can happen if a zone-restricted 3148e716f2ebSMel Gorman * allocation tries to wake a remote kswapd. 3149e716f2ebSMel Gorman */ 3150e716f2ebSMel Gorman if (mark == -1) 3151e716f2ebSMel Gorman return true; 3152e716f2ebSMel Gorman 3153e716f2ebSMel Gorman return false; 3154e716f2ebSMel Gorman } 3155e716f2ebSMel Gorman 3156631b6e08SMel Gorman /* Clear pgdat state for congested, dirty or under writeback. */ 3157631b6e08SMel Gorman static void clear_pgdat_congested(pg_data_t *pgdat) 3158631b6e08SMel Gorman { 3159631b6e08SMel Gorman clear_bit(PGDAT_CONGESTED, &pgdat->flags); 3160631b6e08SMel Gorman clear_bit(PGDAT_DIRTY, &pgdat->flags); 3161631b6e08SMel Gorman clear_bit(PGDAT_WRITEBACK, &pgdat->flags); 3162631b6e08SMel Gorman } 3163631b6e08SMel Gorman 31641741c877SMel Gorman /* 31655515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 31665515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 31675515061dSMel Gorman * 31685515061dSMel Gorman * Returns true if kswapd is ready to sleep 31695515061dSMel Gorman */ 3170d9f21d42SMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, int classzone_idx) 3171f50de2d3SMel Gorman { 31725515061dSMel Gorman /* 31739e5e3661SVlastimil Babka * The throttled processes are normally woken up in balance_pgdat() as 3174c73322d0SJohannes Weiner * soon as allow_direct_reclaim() is true. But there is a potential 31759e5e3661SVlastimil Babka * race between when kswapd checks the watermarks and a process gets 31769e5e3661SVlastimil Babka * throttled. There is also a potential race if processes get 31779e5e3661SVlastimil Babka * throttled, kswapd wakes, a large process exits thereby balancing the 31789e5e3661SVlastimil Babka * zones, which causes kswapd to exit balance_pgdat() before reaching 31799e5e3661SVlastimil Babka * the wake up checks. If kswapd is going to sleep, no process should 31809e5e3661SVlastimil Babka * be sleeping on pfmemalloc_wait, so wake them now if necessary. If 31819e5e3661SVlastimil Babka * the wake up is premature, processes will wake kswapd and get 31829e5e3661SVlastimil Babka * throttled again. The difference from wake ups in balance_pgdat() is 31839e5e3661SVlastimil Babka * that here we are under prepare_to_wait(). 31845515061dSMel Gorman */ 31859e5e3661SVlastimil Babka if (waitqueue_active(&pgdat->pfmemalloc_wait)) 31869e5e3661SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 3187f50de2d3SMel Gorman 3188c73322d0SJohannes Weiner /* Hopeless node, leave it to direct reclaim */ 3189c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3190c73322d0SJohannes Weiner return true; 3191c73322d0SJohannes Weiner 3192e716f2ebSMel Gorman if (pgdat_balanced(pgdat, order, classzone_idx)) { 3193631b6e08SMel Gorman clear_pgdat_congested(pgdat); 3194333b0a45SShantanu Goel return true; 31951d82de61SMel Gorman } 31961d82de61SMel Gorman 3197333b0a45SShantanu Goel return false; 3198f50de2d3SMel Gorman } 3199f50de2d3SMel Gorman 32001da177e4SLinus Torvalds /* 32011d82de61SMel Gorman * kswapd shrinks a node of pages that are at or below the highest usable 32021d82de61SMel Gorman * zone that is currently unbalanced. 3203b8e83b94SMel Gorman * 3204b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 3205283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 3206283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 320775485363SMel Gorman */ 32081d82de61SMel Gorman static bool kswapd_shrink_node(pg_data_t *pgdat, 3209accf6242SVlastimil Babka struct scan_control *sc) 321075485363SMel Gorman { 32111d82de61SMel Gorman struct zone *zone; 32121d82de61SMel Gorman int z; 321375485363SMel Gorman 32141d82de61SMel Gorman /* Reclaim a number of pages proportional to the number of zones */ 32151d82de61SMel Gorman sc->nr_to_reclaim = 0; 3216970a39a3SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 32171d82de61SMel Gorman zone = pgdat->node_zones + z; 32186aa303deSMel Gorman if (!managed_zone(zone)) 32191d82de61SMel Gorman continue; 32207c954f6dSMel Gorman 32211d82de61SMel Gorman sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); 32227c954f6dSMel Gorman } 32237c954f6dSMel Gorman 32241d82de61SMel Gorman /* 32251d82de61SMel Gorman * Historically care was taken to put equal pressure on all zones but 32261d82de61SMel Gorman * now pressure is applied based on node LRU order. 32271d82de61SMel Gorman */ 3228970a39a3SMel Gorman shrink_node(pgdat, sc); 32291d82de61SMel Gorman 32301d82de61SMel Gorman /* 32311d82de61SMel Gorman * Fragmentation may mean that the system cannot be rebalanced for 32321d82de61SMel Gorman * high-order allocations. If twice the allocation size has been 32331d82de61SMel Gorman * reclaimed then recheck watermarks only at order-0 to prevent 32341d82de61SMel Gorman * excessive reclaim. Assume that a process requested a high-order 32351d82de61SMel Gorman * can direct reclaim/compact. 32361d82de61SMel Gorman */ 32379861a62cSVlastimil Babka if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order)) 32381d82de61SMel Gorman sc->order = 0; 32391d82de61SMel Gorman 3240b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 324175485363SMel Gorman } 324275485363SMel Gorman 324375485363SMel Gorman /* 32441d82de61SMel Gorman * For kswapd, balance_pgdat() will reclaim pages across a node from zones 32451d82de61SMel Gorman * that are eligible for use by the caller until at least one zone is 32461d82de61SMel Gorman * balanced. 32471da177e4SLinus Torvalds * 32481d82de61SMel Gorman * Returns the order kswapd finished reclaiming at. 32491da177e4SLinus Torvalds * 32501da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 325141858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 32521d82de61SMel Gorman * found to have free_pages <= high_wmark_pages(zone), any page is that zone 32531d82de61SMel Gorman * or lower is eligible for reclaim until at least one usable zone is 32541d82de61SMel Gorman * balanced. 32551da177e4SLinus Torvalds */ 3256accf6242SVlastimil Babka static int balance_pgdat(pg_data_t *pgdat, int order, int classzone_idx) 32571da177e4SLinus Torvalds { 32581da177e4SLinus Torvalds int i; 32590608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 32600608f43dSAndrew Morton unsigned long nr_soft_scanned; 32611d82de61SMel Gorman struct zone *zone; 3262179e9639SAndrew Morton struct scan_control sc = { 3263179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 3264ee814fe2SJohannes Weiner .order = order, 3265b8e83b94SMel Gorman .priority = DEF_PRIORITY, 3266ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3267a6dc60f8SJohannes Weiner .may_unmap = 1, 32682e2e4259SKOSAKI Motohiro .may_swap = 1, 3269179e9639SAndrew Morton }; 3270f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 32711da177e4SLinus Torvalds 32729e3b2f8cSKonstantin Khlebnikov do { 3273c73322d0SJohannes Weiner unsigned long nr_reclaimed = sc.nr_reclaimed; 3274b8e83b94SMel Gorman bool raise_priority = true; 3275b8e83b94SMel Gorman 327684c7a777SMel Gorman sc.reclaim_idx = classzone_idx; 32771da177e4SLinus Torvalds 327886c79f6bSMel Gorman /* 327984c7a777SMel Gorman * If the number of buffer_heads exceeds the maximum allowed 328084c7a777SMel Gorman * then consider reclaiming from all zones. This has a dual 328184c7a777SMel Gorman * purpose -- on 64-bit systems it is expected that 328284c7a777SMel Gorman * buffer_heads are stripped during active rotation. On 32-bit 328384c7a777SMel Gorman * systems, highmem pages can pin lowmem memory and shrinking 328484c7a777SMel Gorman * buffers can relieve lowmem pressure. Reclaim may still not 328584c7a777SMel Gorman * go ahead if all eligible zones for the original allocation 328684c7a777SMel Gorman * request are balanced to avoid excessive reclaim from kswapd. 328786c79f6bSMel Gorman */ 328886c79f6bSMel Gorman if (buffer_heads_over_limit) { 328986c79f6bSMel Gorman for (i = MAX_NR_ZONES - 1; i >= 0; i--) { 329086c79f6bSMel Gorman zone = pgdat->node_zones + i; 32916aa303deSMel Gorman if (!managed_zone(zone)) 329286c79f6bSMel Gorman continue; 329386c79f6bSMel Gorman 3294970a39a3SMel Gorman sc.reclaim_idx = i; 329586c79f6bSMel Gorman break; 329686c79f6bSMel Gorman } 329786c79f6bSMel Gorman } 329886c79f6bSMel Gorman 329986c79f6bSMel Gorman /* 3300e716f2ebSMel Gorman * Only reclaim if there are no eligible zones. Note that 3301e716f2ebSMel Gorman * sc.reclaim_idx is not used as buffer_heads_over_limit may 3302e716f2ebSMel Gorman * have adjusted it. 330386c79f6bSMel Gorman */ 3304e716f2ebSMel Gorman if (pgdat_balanced(pgdat, sc.order, classzone_idx)) 33051da177e4SLinus Torvalds goto out; 3306e1dbeda6SAndrew Morton 33071da177e4SLinus Torvalds /* 33081d82de61SMel Gorman * Do some background aging of the anon list, to give 33091d82de61SMel Gorman * pages a chance to be referenced before reclaiming. All 33101d82de61SMel Gorman * pages are rotated regardless of classzone as this is 33111d82de61SMel Gorman * about consistent aging. 33121d82de61SMel Gorman */ 3313ef8f2327SMel Gorman age_active_anon(pgdat, &sc); 33141d82de61SMel Gorman 33151d82de61SMel Gorman /* 3316b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 3317b7ea3c41SMel Gorman * even in laptop mode. 3318b7ea3c41SMel Gorman */ 3319047d72c3SJohannes Weiner if (sc.priority < DEF_PRIORITY - 2) 3320b7ea3c41SMel Gorman sc.may_writepage = 1; 3321b7ea3c41SMel Gorman 33221d82de61SMel Gorman /* Call soft limit reclaim before calling shrink_node. */ 33231da177e4SLinus Torvalds sc.nr_scanned = 0; 33240608f43dSAndrew Morton nr_soft_scanned = 0; 3325ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, 33261d82de61SMel Gorman sc.gfp_mask, &nr_soft_scanned); 33270608f43dSAndrew Morton sc.nr_reclaimed += nr_soft_reclaimed; 33280608f43dSAndrew Morton 332932a4330dSRik van Riel /* 33301d82de61SMel Gorman * There should be no need to raise the scanning priority if 33311d82de61SMel Gorman * enough pages are already being scanned that that high 33321d82de61SMel Gorman * watermark would be met at 100% efficiency. 333332a4330dSRik van Riel */ 3334970a39a3SMel Gorman if (kswapd_shrink_node(pgdat, &sc)) 3335b8e83b94SMel Gorman raise_priority = false; 3336d7868daeSMel Gorman 33375515061dSMel Gorman /* 33385515061dSMel Gorman * If the low watermark is met there is no need for processes 33395515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 33405515061dSMel Gorman * able to safely make forward progress. Wake them 33415515061dSMel Gorman */ 33425515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 3343c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)) 3344cfc51155SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 33455515061dSMel Gorman 3346b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 3347b8e83b94SMel Gorman if (try_to_freeze() || kthread_should_stop()) 3348b8e83b94SMel Gorman break; 3349b8e83b94SMel Gorman 3350b8e83b94SMel Gorman /* 3351b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 3352b8e83b94SMel Gorman * progress in reclaiming pages 3353b8e83b94SMel Gorman */ 3354c73322d0SJohannes Weiner nr_reclaimed = sc.nr_reclaimed - nr_reclaimed; 3355c73322d0SJohannes Weiner if (raise_priority || !nr_reclaimed) 3356b8e83b94SMel Gorman sc.priority--; 33571d82de61SMel Gorman } while (sc.priority >= 1); 33581da177e4SLinus Torvalds 3359c73322d0SJohannes Weiner if (!sc.nr_reclaimed) 3360c73322d0SJohannes Weiner pgdat->kswapd_failures++; 3361c73322d0SJohannes Weiner 3362b8e83b94SMel Gorman out: 33632a2e4885SJohannes Weiner snapshot_refaults(NULL, pgdat); 33640abdee2bSMel Gorman /* 33651d82de61SMel Gorman * Return the order kswapd stopped reclaiming at as 33661d82de61SMel Gorman * prepare_kswapd_sleep() takes it into account. If another caller 33671d82de61SMel Gorman * entered the allocator slow path while kswapd was awake, order will 33681d82de61SMel Gorman * remain at the higher level. 33690abdee2bSMel Gorman */ 33701d82de61SMel Gorman return sc.order; 33711da177e4SLinus Torvalds } 33721da177e4SLinus Torvalds 3373e716f2ebSMel Gorman /* 3374e716f2ebSMel Gorman * pgdat->kswapd_classzone_idx is the highest zone index that a recent 3375e716f2ebSMel Gorman * allocation request woke kswapd for. When kswapd has not woken recently, 3376e716f2ebSMel Gorman * the value is MAX_NR_ZONES which is not a valid index. This compares a 3377e716f2ebSMel Gorman * given classzone and returns it or the highest classzone index kswapd 3378e716f2ebSMel Gorman * was recently woke for. 3379e716f2ebSMel Gorman */ 3380e716f2ebSMel Gorman static enum zone_type kswapd_classzone_idx(pg_data_t *pgdat, 3381e716f2ebSMel Gorman enum zone_type classzone_idx) 3382e716f2ebSMel Gorman { 3383e716f2ebSMel Gorman if (pgdat->kswapd_classzone_idx == MAX_NR_ZONES) 3384e716f2ebSMel Gorman return classzone_idx; 3385e716f2ebSMel Gorman 3386e716f2ebSMel Gorman return max(pgdat->kswapd_classzone_idx, classzone_idx); 3387e716f2ebSMel Gorman } 3388e716f2ebSMel Gorman 338938087d9bSMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, 339038087d9bSMel Gorman unsigned int classzone_idx) 3391f0bc0a60SKOSAKI Motohiro { 3392f0bc0a60SKOSAKI Motohiro long remaining = 0; 3393f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 3394f0bc0a60SKOSAKI Motohiro 3395f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 3396f0bc0a60SKOSAKI Motohiro return; 3397f0bc0a60SKOSAKI Motohiro 3398f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3399f0bc0a60SKOSAKI Motohiro 3400333b0a45SShantanu Goel /* 3401333b0a45SShantanu Goel * Try to sleep for a short interval. Note that kcompactd will only be 3402333b0a45SShantanu Goel * woken if it is possible to sleep for a short interval. This is 3403333b0a45SShantanu Goel * deliberate on the assumption that if reclaim cannot keep an 3404333b0a45SShantanu Goel * eligible zone balanced that it's also unlikely that compaction will 3405333b0a45SShantanu Goel * succeed. 3406333b0a45SShantanu Goel */ 3407d9f21d42SMel Gorman if (prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) { 3408fd901c95SVlastimil Babka /* 3409fd901c95SVlastimil Babka * Compaction records what page blocks it recently failed to 3410fd901c95SVlastimil Babka * isolate pages from and skips them in the future scanning. 3411fd901c95SVlastimil Babka * When kswapd is going to sleep, it is reasonable to assume 3412fd901c95SVlastimil Babka * that pages and compaction may succeed so reset the cache. 3413fd901c95SVlastimil Babka */ 3414fd901c95SVlastimil Babka reset_isolation_suitable(pgdat); 3415fd901c95SVlastimil Babka 3416fd901c95SVlastimil Babka /* 3417fd901c95SVlastimil Babka * We have freed the memory, now we should compact it to make 3418fd901c95SVlastimil Babka * allocation of the requested order possible. 3419fd901c95SVlastimil Babka */ 342038087d9bSMel Gorman wakeup_kcompactd(pgdat, alloc_order, classzone_idx); 3421fd901c95SVlastimil Babka 3422f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 342338087d9bSMel Gorman 342438087d9bSMel Gorman /* 342538087d9bSMel Gorman * If woken prematurely then reset kswapd_classzone_idx and 342638087d9bSMel Gorman * order. The values will either be from a wakeup request or 342738087d9bSMel Gorman * the previous request that slept prematurely. 342838087d9bSMel Gorman */ 342938087d9bSMel Gorman if (remaining) { 3430e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx); 343138087d9bSMel Gorman pgdat->kswapd_order = max(pgdat->kswapd_order, reclaim_order); 343238087d9bSMel Gorman } 343338087d9bSMel Gorman 3434f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3435f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3436f0bc0a60SKOSAKI Motohiro } 3437f0bc0a60SKOSAKI Motohiro 3438f0bc0a60SKOSAKI Motohiro /* 3439f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 3440f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 3441f0bc0a60SKOSAKI Motohiro */ 3442d9f21d42SMel Gorman if (!remaining && 3443d9f21d42SMel Gorman prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) { 3444f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 3445f0bc0a60SKOSAKI Motohiro 3446f0bc0a60SKOSAKI Motohiro /* 3447f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 3448f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 3449f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 3450f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 3451f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 3452f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 3453f0bc0a60SKOSAKI Motohiro */ 3454f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 34551c7e7f6cSAaditya Kumar 34561c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 3457f0bc0a60SKOSAKI Motohiro schedule(); 34581c7e7f6cSAaditya Kumar 3459f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 3460f0bc0a60SKOSAKI Motohiro } else { 3461f0bc0a60SKOSAKI Motohiro if (remaining) 3462f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 3463f0bc0a60SKOSAKI Motohiro else 3464f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 3465f0bc0a60SKOSAKI Motohiro } 3466f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3467f0bc0a60SKOSAKI Motohiro } 3468f0bc0a60SKOSAKI Motohiro 34691da177e4SLinus Torvalds /* 34701da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 34711da177e4SLinus Torvalds * from the init process. 34721da177e4SLinus Torvalds * 34731da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 34741da177e4SLinus Torvalds * free memory available even if there is no other activity 34751da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 34761da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 34771da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 34781da177e4SLinus Torvalds * 34791da177e4SLinus Torvalds * If there are applications that are active memory-allocators 34801da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 34811da177e4SLinus Torvalds */ 34821da177e4SLinus Torvalds static int kswapd(void *p) 34831da177e4SLinus Torvalds { 3484e716f2ebSMel Gorman unsigned int alloc_order, reclaim_order; 3485e716f2ebSMel Gorman unsigned int classzone_idx = MAX_NR_ZONES - 1; 34861da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 34871da177e4SLinus Torvalds struct task_struct *tsk = current; 3488f0bc0a60SKOSAKI Motohiro 34891da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 34901da177e4SLinus Torvalds .reclaimed_slab = 0, 34911da177e4SLinus Torvalds }; 3492a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 34931da177e4SLinus Torvalds 3494174596a0SRusty Russell if (!cpumask_empty(cpumask)) 3495c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 34961da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 34971da177e4SLinus Torvalds 34981da177e4SLinus Torvalds /* 34991da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 35001da177e4SLinus Torvalds * and that if we need more memory we should get access to it 35011da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 35021da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 35031da177e4SLinus Torvalds * 35041da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 35051da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 35061da177e4SLinus Torvalds * page out something else, and this flag essentially protects 35071da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 35081da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 35091da177e4SLinus Torvalds */ 3510930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 351183144186SRafael J. Wysocki set_freezable(); 35121da177e4SLinus Torvalds 3513e716f2ebSMel Gorman pgdat->kswapd_order = 0; 3514e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = MAX_NR_ZONES; 35151da177e4SLinus Torvalds for ( ; ; ) { 35166f6313d4SJeff Liu bool ret; 35173e1d1d28SChristoph Lameter 3518e716f2ebSMel Gorman alloc_order = reclaim_order = pgdat->kswapd_order; 3519e716f2ebSMel Gorman classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx); 3520e716f2ebSMel Gorman 352138087d9bSMel Gorman kswapd_try_sleep: 352238087d9bSMel Gorman kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, 352338087d9bSMel Gorman classzone_idx); 3524215ddd66SMel Gorman 352538087d9bSMel Gorman /* Read the new order and classzone_idx */ 352638087d9bSMel Gorman alloc_order = reclaim_order = pgdat->kswapd_order; 3527e716f2ebSMel Gorman classzone_idx = kswapd_classzone_idx(pgdat, 0); 352838087d9bSMel Gorman pgdat->kswapd_order = 0; 3529e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = MAX_NR_ZONES; 35301da177e4SLinus Torvalds 35318fe23e05SDavid Rientjes ret = try_to_freeze(); 35328fe23e05SDavid Rientjes if (kthread_should_stop()) 35338fe23e05SDavid Rientjes break; 35348fe23e05SDavid Rientjes 35358fe23e05SDavid Rientjes /* 35368fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 35378fe23e05SDavid Rientjes * after returning from the refrigerator 3538b1296cc4SRafael J. Wysocki */ 353938087d9bSMel Gorman if (ret) 354038087d9bSMel Gorman continue; 35411d82de61SMel Gorman 354238087d9bSMel Gorman /* 354338087d9bSMel Gorman * Reclaim begins at the requested order but if a high-order 354438087d9bSMel Gorman * reclaim fails then kswapd falls back to reclaiming for 354538087d9bSMel Gorman * order-0. If that happens, kswapd will consider sleeping 354638087d9bSMel Gorman * for the order it finished reclaiming at (reclaim_order) 354738087d9bSMel Gorman * but kcompactd is woken to compact for the original 354838087d9bSMel Gorman * request (alloc_order). 354938087d9bSMel Gorman */ 3550e5146b12SMel Gorman trace_mm_vmscan_kswapd_wake(pgdat->node_id, classzone_idx, 3551e5146b12SMel Gorman alloc_order); 3552d92a8cfcSPeter Zijlstra fs_reclaim_acquire(GFP_KERNEL); 355338087d9bSMel Gorman reclaim_order = balance_pgdat(pgdat, alloc_order, classzone_idx); 3554d92a8cfcSPeter Zijlstra fs_reclaim_release(GFP_KERNEL); 355538087d9bSMel Gorman if (reclaim_order < alloc_order) 355638087d9bSMel Gorman goto kswapd_try_sleep; 355733906bc5SMel Gorman } 3558b0a8cc58STakamori Yamaguchi 355971abdc15SJohannes Weiner tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); 3560b0a8cc58STakamori Yamaguchi current->reclaim_state = NULL; 356171abdc15SJohannes Weiner 35621da177e4SLinus Torvalds return 0; 35631da177e4SLinus Torvalds } 35641da177e4SLinus Torvalds 35651da177e4SLinus Torvalds /* 35665ecd9d40SDavid Rientjes * A zone is low on free memory or too fragmented for high-order memory. If 35675ecd9d40SDavid Rientjes * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's 35685ecd9d40SDavid Rientjes * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim 35695ecd9d40SDavid Rientjes * has failed or is not needed, still wake up kcompactd if only compaction is 35705ecd9d40SDavid Rientjes * needed. 35711da177e4SLinus Torvalds */ 35725ecd9d40SDavid Rientjes void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, 35735ecd9d40SDavid Rientjes enum zone_type classzone_idx) 35741da177e4SLinus Torvalds { 35751da177e4SLinus Torvalds pg_data_t *pgdat; 35761da177e4SLinus Torvalds 35776aa303deSMel Gorman if (!managed_zone(zone)) 35781da177e4SLinus Torvalds return; 35791da177e4SLinus Torvalds 35805ecd9d40SDavid Rientjes if (!cpuset_zone_allowed(zone, gfp_flags)) 35811da177e4SLinus Torvalds return; 358288f5acf8SMel Gorman pgdat = zone->zone_pgdat; 3583e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat, 3584e716f2ebSMel Gorman classzone_idx); 358538087d9bSMel Gorman pgdat->kswapd_order = max(pgdat->kswapd_order, order); 35868d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 35871da177e4SLinus Torvalds return; 3588e1a55637SMel Gorman 35895ecd9d40SDavid Rientjes /* Hopeless node, leave it to direct reclaim if possible */ 35905ecd9d40SDavid Rientjes if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES || 35915ecd9d40SDavid Rientjes pgdat_balanced(pgdat, order, classzone_idx)) { 35925ecd9d40SDavid Rientjes /* 35935ecd9d40SDavid Rientjes * There may be plenty of free memory available, but it's too 35945ecd9d40SDavid Rientjes * fragmented for high-order allocations. Wake up kcompactd 35955ecd9d40SDavid Rientjes * and rely on compaction_suitable() to determine if it's 35965ecd9d40SDavid Rientjes * needed. If it fails, it will defer subsequent attempts to 35975ecd9d40SDavid Rientjes * ratelimit its work. 35985ecd9d40SDavid Rientjes */ 35995ecd9d40SDavid Rientjes if (!(gfp_flags & __GFP_DIRECT_RECLAIM)) 36005ecd9d40SDavid Rientjes wakeup_kcompactd(pgdat, order, classzone_idx); 3601c73322d0SJohannes Weiner return; 36025ecd9d40SDavid Rientjes } 3603c73322d0SJohannes Weiner 36045ecd9d40SDavid Rientjes trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, classzone_idx, order, 36055ecd9d40SDavid Rientjes gfp_flags); 36068d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 36071da177e4SLinus Torvalds } 36081da177e4SLinus Torvalds 3609c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 36101da177e4SLinus Torvalds /* 36117b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 3612d6277db4SRafael J. Wysocki * freed pages. 3613d6277db4SRafael J. Wysocki * 3614d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 3615d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 3616d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 36171da177e4SLinus Torvalds */ 36187b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 36191da177e4SLinus Torvalds { 3620d6277db4SRafael J. Wysocki struct reclaim_state reclaim_state; 3621d6277db4SRafael J. Wysocki struct scan_control sc = { 36227b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 3623ee814fe2SJohannes Weiner .gfp_mask = GFP_HIGHUSER_MOVABLE, 3624b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 36259e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 3626ee814fe2SJohannes Weiner .may_writepage = 1, 3627ee814fe2SJohannes Weiner .may_unmap = 1, 3628ee814fe2SJohannes Weiner .may_swap = 1, 3629ee814fe2SJohannes Weiner .hibernation_mode = 1, 36301da177e4SLinus Torvalds }; 36317b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 36327b51755cSKOSAKI Motohiro struct task_struct *p = current; 36337b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 3634499118e9SVlastimil Babka unsigned int noreclaim_flag; 36351da177e4SLinus Torvalds 3636499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 3637d92a8cfcSPeter Zijlstra fs_reclaim_acquire(sc.gfp_mask); 3638d6277db4SRafael J. Wysocki reclaim_state.reclaimed_slab = 0; 36397b51755cSKOSAKI Motohiro p->reclaim_state = &reclaim_state; 3640d6277db4SRafael J. Wysocki 36413115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3642d6277db4SRafael J. Wysocki 36437b51755cSKOSAKI Motohiro p->reclaim_state = NULL; 3644d92a8cfcSPeter Zijlstra fs_reclaim_release(sc.gfp_mask); 3645499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 3646d6277db4SRafael J. Wysocki 36477b51755cSKOSAKI Motohiro return nr_reclaimed; 36481da177e4SLinus Torvalds } 3649c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 36501da177e4SLinus Torvalds 36511da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 36521da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 36531da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 36541da177e4SLinus Torvalds restore their cpu bindings. */ 3655517bbed9SSebastian Andrzej Siewior static int kswapd_cpu_online(unsigned int cpu) 36561da177e4SLinus Torvalds { 365758c0a4a7SYasunori Goto int nid; 36581da177e4SLinus Torvalds 365948fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) { 3660c5f59f08SMike Travis pg_data_t *pgdat = NODE_DATA(nid); 3661a70f7302SRusty Russell const struct cpumask *mask; 3662a70f7302SRusty Russell 3663a70f7302SRusty Russell mask = cpumask_of_node(pgdat->node_id); 3664c5f59f08SMike Travis 36653e597945SRusty Russell if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 36661da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 3667c5f59f08SMike Travis set_cpus_allowed_ptr(pgdat->kswapd, mask); 36681da177e4SLinus Torvalds } 3669517bbed9SSebastian Andrzej Siewior return 0; 36701da177e4SLinus Torvalds } 36711da177e4SLinus Torvalds 36723218ae14SYasunori Goto /* 36733218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 36743218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 36753218ae14SYasunori Goto */ 36763218ae14SYasunori Goto int kswapd_run(int nid) 36773218ae14SYasunori Goto { 36783218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 36793218ae14SYasunori Goto int ret = 0; 36803218ae14SYasunori Goto 36813218ae14SYasunori Goto if (pgdat->kswapd) 36823218ae14SYasunori Goto return 0; 36833218ae14SYasunori Goto 36843218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 36853218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 36863218ae14SYasunori Goto /* failure at boot is fatal */ 3687c6202adfSThomas Gleixner BUG_ON(system_state < SYSTEM_RUNNING); 3688d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 3689d5dc0ad9SGavin Shan ret = PTR_ERR(pgdat->kswapd); 3690d72515b8SXishi Qiu pgdat->kswapd = NULL; 36913218ae14SYasunori Goto } 36923218ae14SYasunori Goto return ret; 36933218ae14SYasunori Goto } 36943218ae14SYasunori Goto 36958fe23e05SDavid Rientjes /* 3696d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 3697bfc8c901SVladimir Davydov * hold mem_hotplug_begin/end(). 36988fe23e05SDavid Rientjes */ 36998fe23e05SDavid Rientjes void kswapd_stop(int nid) 37008fe23e05SDavid Rientjes { 37018fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 37028fe23e05SDavid Rientjes 3703d8adde17SJiang Liu if (kswapd) { 37048fe23e05SDavid Rientjes kthread_stop(kswapd); 3705d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 3706d8adde17SJiang Liu } 37078fe23e05SDavid Rientjes } 37088fe23e05SDavid Rientjes 37091da177e4SLinus Torvalds static int __init kswapd_init(void) 37101da177e4SLinus Torvalds { 3711517bbed9SSebastian Andrzej Siewior int nid, ret; 371269e05944SAndrew Morton 37131da177e4SLinus Torvalds swap_setup(); 371448fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 37153218ae14SYasunori Goto kswapd_run(nid); 3716517bbed9SSebastian Andrzej Siewior ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, 3717517bbed9SSebastian Andrzej Siewior "mm/vmscan:online", kswapd_cpu_online, 3718517bbed9SSebastian Andrzej Siewior NULL); 3719517bbed9SSebastian Andrzej Siewior WARN_ON(ret < 0); 37201da177e4SLinus Torvalds return 0; 37211da177e4SLinus Torvalds } 37221da177e4SLinus Torvalds 37231da177e4SLinus Torvalds module_init(kswapd_init) 37249eeff239SChristoph Lameter 37259eeff239SChristoph Lameter #ifdef CONFIG_NUMA 37269eeff239SChristoph Lameter /* 3727a5f5f91dSMel Gorman * Node reclaim mode 37289eeff239SChristoph Lameter * 3729a5f5f91dSMel Gorman * If non-zero call node_reclaim when the number of free pages falls below 37309eeff239SChristoph Lameter * the watermarks. 37319eeff239SChristoph Lameter */ 3732a5f5f91dSMel Gorman int node_reclaim_mode __read_mostly; 37339eeff239SChristoph Lameter 37341b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 37357d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ 37361b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 373795bbc0c7SZhihui Zhang #define RECLAIM_UNMAP (1<<2) /* Unmap pages during reclaim */ 37381b2ffb78SChristoph Lameter 37399eeff239SChristoph Lameter /* 3740a5f5f91dSMel Gorman * Priority for NODE_RECLAIM. This determines the fraction of pages 3741a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 3742a92f7126SChristoph Lameter * a zone. 3743a92f7126SChristoph Lameter */ 3744a5f5f91dSMel Gorman #define NODE_RECLAIM_PRIORITY 4 3745a92f7126SChristoph Lameter 37469eeff239SChristoph Lameter /* 3747a5f5f91dSMel Gorman * Percentage of pages in a zone that must be unmapped for node_reclaim to 37489614634fSChristoph Lameter * occur. 37499614634fSChristoph Lameter */ 37509614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 37519614634fSChristoph Lameter 37529614634fSChristoph Lameter /* 37530ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 37540ff38490SChristoph Lameter * slab reclaim needs to occur. 37550ff38490SChristoph Lameter */ 37560ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 37570ff38490SChristoph Lameter 375811fb9989SMel Gorman static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) 375990afa5deSMel Gorman { 376011fb9989SMel Gorman unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); 376111fb9989SMel Gorman unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + 376211fb9989SMel Gorman node_page_state(pgdat, NR_ACTIVE_FILE); 376390afa5deSMel Gorman 376490afa5deSMel Gorman /* 376590afa5deSMel Gorman * It's possible for there to be more file mapped pages than 376690afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 376790afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 376890afa5deSMel Gorman */ 376990afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 377090afa5deSMel Gorman } 377190afa5deSMel Gorman 377290afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 3773a5f5f91dSMel Gorman static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) 377490afa5deSMel Gorman { 3775d031a157SAlexandru Moise unsigned long nr_pagecache_reclaimable; 3776d031a157SAlexandru Moise unsigned long delta = 0; 377790afa5deSMel Gorman 377890afa5deSMel Gorman /* 377995bbc0c7SZhihui Zhang * If RECLAIM_UNMAP is set, then all file pages are considered 378090afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 378111fb9989SMel Gorman * pages like swapcache and node_unmapped_file_pages() provides 378290afa5deSMel Gorman * a better estimate 378390afa5deSMel Gorman */ 3784a5f5f91dSMel Gorman if (node_reclaim_mode & RECLAIM_UNMAP) 3785a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); 378690afa5deSMel Gorman else 3787a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); 378890afa5deSMel Gorman 378990afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 3790a5f5f91dSMel Gorman if (!(node_reclaim_mode & RECLAIM_WRITE)) 3791a5f5f91dSMel Gorman delta += node_page_state(pgdat, NR_FILE_DIRTY); 379290afa5deSMel Gorman 379390afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 379490afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 379590afa5deSMel Gorman delta = nr_pagecache_reclaimable; 379690afa5deSMel Gorman 379790afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 379890afa5deSMel Gorman } 379990afa5deSMel Gorman 38000ff38490SChristoph Lameter /* 3801a5f5f91dSMel Gorman * Try to free up some pages from this node through reclaim. 38029eeff239SChristoph Lameter */ 3803a5f5f91dSMel Gorman static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 38049eeff239SChristoph Lameter { 38057fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 380669e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 38079eeff239SChristoph Lameter struct task_struct *p = current; 38089eeff239SChristoph Lameter struct reclaim_state reclaim_state; 3809499118e9SVlastimil Babka unsigned int noreclaim_flag; 3810179e9639SAndrew Morton struct scan_control sc = { 381162b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 3812f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 3813bd2f6199SJohannes Weiner .order = order, 3814a5f5f91dSMel Gorman .priority = NODE_RECLAIM_PRIORITY, 3815a5f5f91dSMel Gorman .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), 3816a5f5f91dSMel Gorman .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), 3817ee814fe2SJohannes Weiner .may_swap = 1, 3818f2f43e56SNick Desaulniers .reclaim_idx = gfp_zone(gfp_mask), 3819179e9639SAndrew Morton }; 38209eeff239SChristoph Lameter 38219eeff239SChristoph Lameter cond_resched(); 3822d4f7796eSChristoph Lameter /* 382395bbc0c7SZhihui Zhang * We need to be able to allocate from the reserves for RECLAIM_UNMAP 3824d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 382595bbc0c7SZhihui Zhang * and RECLAIM_UNMAP. 3826d4f7796eSChristoph Lameter */ 3827499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 3828499118e9SVlastimil Babka p->flags |= PF_SWAPWRITE; 3829d92a8cfcSPeter Zijlstra fs_reclaim_acquire(sc.gfp_mask); 38309eeff239SChristoph Lameter reclaim_state.reclaimed_slab = 0; 38319eeff239SChristoph Lameter p->reclaim_state = &reclaim_state; 3832c84db23cSChristoph Lameter 3833a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) { 3834a92f7126SChristoph Lameter /* 3835894befecSAndrey Ryabinin * Free memory by calling shrink node with increasing 38360ff38490SChristoph Lameter * priorities until we have enough memory freed. 3837a92f7126SChristoph Lameter */ 3838a92f7126SChristoph Lameter do { 3839970a39a3SMel Gorman shrink_node(pgdat, &sc); 38409e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 38410ff38490SChristoph Lameter } 3842a92f7126SChristoph Lameter 38439eeff239SChristoph Lameter p->reclaim_state = NULL; 3844d92a8cfcSPeter Zijlstra fs_reclaim_release(gfp_mask); 3845499118e9SVlastimil Babka current->flags &= ~PF_SWAPWRITE; 3846499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 3847a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 38489eeff239SChristoph Lameter } 3849179e9639SAndrew Morton 3850a5f5f91dSMel Gorman int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 3851179e9639SAndrew Morton { 3852d773ed6bSDavid Rientjes int ret; 3853179e9639SAndrew Morton 3854179e9639SAndrew Morton /* 3855a5f5f91dSMel Gorman * Node reclaim reclaims unmapped file backed pages and 38560ff38490SChristoph Lameter * slab pages if we are over the defined limits. 385734aa1330SChristoph Lameter * 38589614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 38599614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 3860a5f5f91dSMel Gorman * thrown out if the node is overallocated. So we do not reclaim 3861a5f5f91dSMel Gorman * if less than a specified percentage of the node is used by 38629614634fSChristoph Lameter * unmapped file backed pages. 3863179e9639SAndrew Morton */ 3864a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && 3865385386cfSJohannes Weiner node_page_state(pgdat, NR_SLAB_RECLAIMABLE) <= pgdat->min_slab_pages) 3866a5f5f91dSMel Gorman return NODE_RECLAIM_FULL; 3867179e9639SAndrew Morton 3868179e9639SAndrew Morton /* 3869d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 3870179e9639SAndrew Morton */ 3871d0164adcSMel Gorman if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) 3872a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 3873179e9639SAndrew Morton 3874179e9639SAndrew Morton /* 3875a5f5f91dSMel Gorman * Only run node reclaim on the local node or on nodes that do not 3876179e9639SAndrew Morton * have associated processors. This will favor the local processor 3877179e9639SAndrew Morton * over remote processors and spread off node memory allocations 3878179e9639SAndrew Morton * as wide as possible. 3879179e9639SAndrew Morton */ 3880a5f5f91dSMel Gorman if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) 3881a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 3882d773ed6bSDavid Rientjes 3883a5f5f91dSMel Gorman if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) 3884a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 3885fa5e084eSMel Gorman 3886a5f5f91dSMel Gorman ret = __node_reclaim(pgdat, gfp_mask, order); 3887a5f5f91dSMel Gorman clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); 3888d773ed6bSDavid Rientjes 388924cf7251SMel Gorman if (!ret) 389024cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 389124cf7251SMel Gorman 3892d773ed6bSDavid Rientjes return ret; 3893179e9639SAndrew Morton } 38949eeff239SChristoph Lameter #endif 3895894bc310SLee Schermerhorn 3896894bc310SLee Schermerhorn /* 3897894bc310SLee Schermerhorn * page_evictable - test whether a page is evictable 3898894bc310SLee Schermerhorn * @page: the page to test 3899894bc310SLee Schermerhorn * 3900894bc310SLee Schermerhorn * Test whether page is evictable--i.e., should be placed on active/inactive 390139b5f29aSHugh Dickins * lists vs unevictable list. 3902894bc310SLee Schermerhorn * 3903894bc310SLee Schermerhorn * Reasons page might not be evictable: 3904ba9ddf49SLee Schermerhorn * (1) page's mapping marked unevictable 3905b291f000SNick Piggin * (2) page is part of an mlocked VMA 3906ba9ddf49SLee Schermerhorn * 3907894bc310SLee Schermerhorn */ 390839b5f29aSHugh Dickins int page_evictable(struct page *page) 3909894bc310SLee Schermerhorn { 3910e92bb4ddSHuang Ying int ret; 3911e92bb4ddSHuang Ying 3912e92bb4ddSHuang Ying /* Prevent address_space of inode and swap cache from being freed */ 3913e92bb4ddSHuang Ying rcu_read_lock(); 3914e92bb4ddSHuang Ying ret = !mapping_unevictable(page_mapping(page)) && !PageMlocked(page); 3915e92bb4ddSHuang Ying rcu_read_unlock(); 3916e92bb4ddSHuang Ying return ret; 3917894bc310SLee Schermerhorn } 391889e004eaSLee Schermerhorn 391985046579SHugh Dickins #ifdef CONFIG_SHMEM 392089e004eaSLee Schermerhorn /** 392124513264SHugh Dickins * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list 392224513264SHugh Dickins * @pages: array of pages to check 392324513264SHugh Dickins * @nr_pages: number of pages to check 392489e004eaSLee Schermerhorn * 392524513264SHugh Dickins * Checks pages for evictability and moves them to the appropriate lru list. 392685046579SHugh Dickins * 392785046579SHugh Dickins * This function is only used for SysV IPC SHM_UNLOCK. 392889e004eaSLee Schermerhorn */ 392924513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages) 393089e004eaSLee Schermerhorn { 3931925b7673SJohannes Weiner struct lruvec *lruvec; 3932785b99feSMel Gorman struct pglist_data *pgdat = NULL; 393324513264SHugh Dickins int pgscanned = 0; 393424513264SHugh Dickins int pgrescued = 0; 393589e004eaSLee Schermerhorn int i; 393689e004eaSLee Schermerhorn 393724513264SHugh Dickins for (i = 0; i < nr_pages; i++) { 393824513264SHugh Dickins struct page *page = pages[i]; 3939785b99feSMel Gorman struct pglist_data *pagepgdat = page_pgdat(page); 394089e004eaSLee Schermerhorn 394124513264SHugh Dickins pgscanned++; 3942785b99feSMel Gorman if (pagepgdat != pgdat) { 3943785b99feSMel Gorman if (pgdat) 3944785b99feSMel Gorman spin_unlock_irq(&pgdat->lru_lock); 3945785b99feSMel Gorman pgdat = pagepgdat; 3946785b99feSMel Gorman spin_lock_irq(&pgdat->lru_lock); 394789e004eaSLee Schermerhorn } 3948785b99feSMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 394989e004eaSLee Schermerhorn 395024513264SHugh Dickins if (!PageLRU(page) || !PageUnevictable(page)) 395124513264SHugh Dickins continue; 395289e004eaSLee Schermerhorn 395339b5f29aSHugh Dickins if (page_evictable(page)) { 395424513264SHugh Dickins enum lru_list lru = page_lru_base_type(page); 395524513264SHugh Dickins 3956309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 395724513264SHugh Dickins ClearPageUnevictable(page); 3958fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE); 3959fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 396024513264SHugh Dickins pgrescued++; 396189e004eaSLee Schermerhorn } 396289e004eaSLee Schermerhorn } 396324513264SHugh Dickins 3964785b99feSMel Gorman if (pgdat) { 396524513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 396624513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 3967785b99feSMel Gorman spin_unlock_irq(&pgdat->lru_lock); 396824513264SHugh Dickins } 396985046579SHugh Dickins } 397085046579SHugh Dickins #endif /* CONFIG_SHMEM */ 3971