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 68ee814fe2SJohannes Weiner /* 69ee814fe2SJohannes Weiner * Nodemask of nodes allowed by the caller. If NULL, all nodes 70ee814fe2SJohannes Weiner * are scanned. 71ee814fe2SJohannes Weiner */ 72ee814fe2SJohannes Weiner nodemask_t *nodemask; 739e3b2f8cSKonstantin Khlebnikov 745f53e762SKOSAKI Motohiro /* 75f16015fbSJohannes Weiner * The memory cgroup that hit its limit and as a result is the 76f16015fbSJohannes Weiner * primary target of this reclaim invocation. 77f16015fbSJohannes Weiner */ 78f16015fbSJohannes Weiner struct mem_cgroup *target_mem_cgroup; 7966e1707bSBalbir Singh 801276ad68SJohannes Weiner /* Writepage batching in laptop mode; RECLAIM_WRITE */ 81ee814fe2SJohannes Weiner unsigned int may_writepage:1; 82ee814fe2SJohannes Weiner 83ee814fe2SJohannes Weiner /* Can mapped pages be reclaimed? */ 84ee814fe2SJohannes Weiner unsigned int may_unmap:1; 85ee814fe2SJohannes Weiner 86ee814fe2SJohannes Weiner /* Can pages be swapped as part of reclaim? */ 87ee814fe2SJohannes Weiner unsigned int may_swap:1; 88ee814fe2SJohannes Weiner 89d6622f63SYisheng Xie /* 90d6622f63SYisheng Xie * Cgroups are not reclaimed below their configured memory.low, 91d6622f63SYisheng Xie * unless we threaten to OOM. If any cgroups are skipped due to 92d6622f63SYisheng Xie * memory.low and nothing was reclaimed, go back for memory.low. 93d6622f63SYisheng Xie */ 94d6622f63SYisheng Xie unsigned int memcg_low_reclaim:1; 95d6622f63SYisheng Xie unsigned int memcg_low_skipped:1; 96241994edSJohannes Weiner 97ee814fe2SJohannes Weiner unsigned int hibernation_mode:1; 98ee814fe2SJohannes Weiner 99ee814fe2SJohannes Weiner /* One of the zones is ready for compaction */ 100ee814fe2SJohannes Weiner unsigned int compaction_ready:1; 101ee814fe2SJohannes Weiner 102bb451fdfSGreg Thelen /* Allocation order */ 103bb451fdfSGreg Thelen s8 order; 104bb451fdfSGreg Thelen 105bb451fdfSGreg Thelen /* Scan (total_size >> priority) pages at once */ 106bb451fdfSGreg Thelen s8 priority; 107bb451fdfSGreg Thelen 108bb451fdfSGreg Thelen /* The highest zone to isolate pages for reclaim from */ 109bb451fdfSGreg Thelen s8 reclaim_idx; 110bb451fdfSGreg Thelen 111bb451fdfSGreg Thelen /* This context's GFP mask */ 112bb451fdfSGreg Thelen gfp_t gfp_mask; 113bb451fdfSGreg Thelen 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; 119d108c772SAndrey Ryabinin 120d108c772SAndrey Ryabinin struct { 121d108c772SAndrey Ryabinin unsigned int dirty; 122d108c772SAndrey Ryabinin unsigned int unqueued_dirty; 123d108c772SAndrey Ryabinin unsigned int congested; 124d108c772SAndrey Ryabinin unsigned int writeback; 125d108c772SAndrey Ryabinin unsigned int immediate; 126d108c772SAndrey Ryabinin unsigned int file_taken; 127d108c772SAndrey Ryabinin unsigned int taken; 128d108c772SAndrey 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 172b4c2b231SKirill Tkhai #ifdef CONFIG_MEMCG_KMEM 173b4c2b231SKirill Tkhai static DEFINE_IDR(shrinker_idr); 174b4c2b231SKirill Tkhai static int shrinker_nr_max; 175b4c2b231SKirill Tkhai 176b4c2b231SKirill Tkhai static int prealloc_memcg_shrinker(struct shrinker *shrinker) 177b4c2b231SKirill Tkhai { 178b4c2b231SKirill Tkhai int id, ret = -ENOMEM; 179b4c2b231SKirill Tkhai 180b4c2b231SKirill Tkhai down_write(&shrinker_rwsem); 181b4c2b231SKirill Tkhai /* This may call shrinker, so it must use down_read_trylock() */ 182b4c2b231SKirill Tkhai id = idr_alloc(&shrinker_idr, shrinker, 0, 0, GFP_KERNEL); 183b4c2b231SKirill Tkhai if (id < 0) 184b4c2b231SKirill Tkhai goto unlock; 185b4c2b231SKirill Tkhai 1860a4465d3SKirill Tkhai if (id >= shrinker_nr_max) { 1870a4465d3SKirill Tkhai if (memcg_expand_shrinker_maps(id)) { 1880a4465d3SKirill Tkhai idr_remove(&shrinker_idr, id); 1890a4465d3SKirill Tkhai goto unlock; 1900a4465d3SKirill Tkhai } 1910a4465d3SKirill Tkhai 192b4c2b231SKirill Tkhai shrinker_nr_max = id + 1; 1930a4465d3SKirill Tkhai } 194b4c2b231SKirill Tkhai shrinker->id = id; 195b4c2b231SKirill Tkhai ret = 0; 196b4c2b231SKirill Tkhai unlock: 197b4c2b231SKirill Tkhai up_write(&shrinker_rwsem); 198b4c2b231SKirill Tkhai return ret; 199b4c2b231SKirill Tkhai } 200b4c2b231SKirill Tkhai 201b4c2b231SKirill Tkhai static void unregister_memcg_shrinker(struct shrinker *shrinker) 202b4c2b231SKirill Tkhai { 203b4c2b231SKirill Tkhai int id = shrinker->id; 204b4c2b231SKirill Tkhai 205b4c2b231SKirill Tkhai BUG_ON(id < 0); 206b4c2b231SKirill Tkhai 207b4c2b231SKirill Tkhai down_write(&shrinker_rwsem); 208b4c2b231SKirill Tkhai idr_remove(&shrinker_idr, id); 209b4c2b231SKirill Tkhai up_write(&shrinker_rwsem); 210b4c2b231SKirill Tkhai } 211b4c2b231SKirill Tkhai #else /* CONFIG_MEMCG_KMEM */ 212b4c2b231SKirill Tkhai static int prealloc_memcg_shrinker(struct shrinker *shrinker) 213b4c2b231SKirill Tkhai { 214b4c2b231SKirill Tkhai return 0; 215b4c2b231SKirill Tkhai } 216b4c2b231SKirill Tkhai 217b4c2b231SKirill Tkhai static void unregister_memcg_shrinker(struct shrinker *shrinker) 218b4c2b231SKirill Tkhai { 219b4c2b231SKirill Tkhai } 220b4c2b231SKirill Tkhai #endif /* CONFIG_MEMCG_KMEM */ 221b4c2b231SKirill Tkhai 222c255a458SAndrew Morton #ifdef CONFIG_MEMCG 22389b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 22489b5fae5SJohannes Weiner { 225f16015fbSJohannes Weiner return !sc->target_mem_cgroup; 22689b5fae5SJohannes Weiner } 22797c9341fSTejun Heo 22897c9341fSTejun Heo /** 22997c9341fSTejun Heo * sane_reclaim - is the usual dirty throttling mechanism operational? 23097c9341fSTejun Heo * @sc: scan_control in question 23197c9341fSTejun Heo * 23297c9341fSTejun Heo * The normal page dirty throttling mechanism in balance_dirty_pages() is 23397c9341fSTejun Heo * completely broken with the legacy memcg and direct stalling in 23497c9341fSTejun Heo * shrink_page_list() is used for throttling instead, which lacks all the 23597c9341fSTejun Heo * niceties such as fairness, adaptive pausing, bandwidth proportional 23697c9341fSTejun Heo * allocation and configurability. 23797c9341fSTejun Heo * 23897c9341fSTejun Heo * This function tests whether the vmscan currently in progress can assume 23997c9341fSTejun Heo * that the normal dirty throttling mechanism is operational. 24097c9341fSTejun Heo */ 24197c9341fSTejun Heo static bool sane_reclaim(struct scan_control *sc) 24297c9341fSTejun Heo { 24397c9341fSTejun Heo struct mem_cgroup *memcg = sc->target_mem_cgroup; 24497c9341fSTejun Heo 24597c9341fSTejun Heo if (!memcg) 24697c9341fSTejun Heo return true; 24797c9341fSTejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 24869234aceSLinus Torvalds if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) 24997c9341fSTejun Heo return true; 25097c9341fSTejun Heo #endif 25197c9341fSTejun Heo return false; 25297c9341fSTejun Heo } 253e3c1ac58SAndrey Ryabinin 254e3c1ac58SAndrey Ryabinin static void set_memcg_congestion(pg_data_t *pgdat, 255e3c1ac58SAndrey Ryabinin struct mem_cgroup *memcg, 256e3c1ac58SAndrey Ryabinin bool congested) 257e3c1ac58SAndrey Ryabinin { 258e3c1ac58SAndrey Ryabinin struct mem_cgroup_per_node *mn; 259e3c1ac58SAndrey Ryabinin 260e3c1ac58SAndrey Ryabinin if (!memcg) 261e3c1ac58SAndrey Ryabinin return; 262e3c1ac58SAndrey Ryabinin 263e3c1ac58SAndrey Ryabinin mn = mem_cgroup_nodeinfo(memcg, pgdat->node_id); 264e3c1ac58SAndrey Ryabinin WRITE_ONCE(mn->congested, congested); 265e3c1ac58SAndrey Ryabinin } 266e3c1ac58SAndrey Ryabinin 267e3c1ac58SAndrey Ryabinin static bool memcg_congested(pg_data_t *pgdat, 268e3c1ac58SAndrey Ryabinin struct mem_cgroup *memcg) 269e3c1ac58SAndrey Ryabinin { 270e3c1ac58SAndrey Ryabinin struct mem_cgroup_per_node *mn; 271e3c1ac58SAndrey Ryabinin 272e3c1ac58SAndrey Ryabinin mn = mem_cgroup_nodeinfo(memcg, pgdat->node_id); 273e3c1ac58SAndrey Ryabinin return READ_ONCE(mn->congested); 274e3c1ac58SAndrey Ryabinin 275e3c1ac58SAndrey Ryabinin } 27691a45470SKAMEZAWA Hiroyuki #else 27789b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 27889b5fae5SJohannes Weiner { 27989b5fae5SJohannes Weiner return true; 28089b5fae5SJohannes Weiner } 28197c9341fSTejun Heo 28297c9341fSTejun Heo static bool sane_reclaim(struct scan_control *sc) 28397c9341fSTejun Heo { 28497c9341fSTejun Heo return true; 28597c9341fSTejun Heo } 286e3c1ac58SAndrey Ryabinin 287e3c1ac58SAndrey Ryabinin static inline void set_memcg_congestion(struct pglist_data *pgdat, 288e3c1ac58SAndrey Ryabinin struct mem_cgroup *memcg, bool congested) 289e3c1ac58SAndrey Ryabinin { 290e3c1ac58SAndrey Ryabinin } 291e3c1ac58SAndrey Ryabinin 292e3c1ac58SAndrey Ryabinin static inline bool memcg_congested(struct pglist_data *pgdat, 293e3c1ac58SAndrey Ryabinin struct mem_cgroup *memcg) 294e3c1ac58SAndrey Ryabinin { 295e3c1ac58SAndrey Ryabinin return false; 296e3c1ac58SAndrey Ryabinin 297e3c1ac58SAndrey Ryabinin } 29891a45470SKAMEZAWA Hiroyuki #endif 29991a45470SKAMEZAWA Hiroyuki 3005a1c84b4SMel Gorman /* 3015a1c84b4SMel Gorman * This misses isolated pages which are not accounted for to save counters. 3025a1c84b4SMel Gorman * As the data only determines if reclaim or compaction continues, it is 3035a1c84b4SMel Gorman * not expected that isolated pages will be a dominating factor. 3045a1c84b4SMel Gorman */ 3055a1c84b4SMel Gorman unsigned long zone_reclaimable_pages(struct zone *zone) 3065a1c84b4SMel Gorman { 3075a1c84b4SMel Gorman unsigned long nr; 3085a1c84b4SMel Gorman 3095a1c84b4SMel Gorman nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) + 3105a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE); 3115a1c84b4SMel Gorman if (get_nr_swap_pages() > 0) 3125a1c84b4SMel Gorman nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) + 3135a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON); 3145a1c84b4SMel Gorman 3155a1c84b4SMel Gorman return nr; 3165a1c84b4SMel Gorman } 3175a1c84b4SMel Gorman 318fd538803SMichal Hocko /** 319fd538803SMichal Hocko * lruvec_lru_size - Returns the number of pages on the given LRU list. 320fd538803SMichal Hocko * @lruvec: lru vector 321fd538803SMichal Hocko * @lru: lru to use 322fd538803SMichal Hocko * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list) 323fd538803SMichal Hocko */ 324fd538803SMichal Hocko unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx) 325c9f299d9SKOSAKI Motohiro { 326fd538803SMichal Hocko unsigned long lru_size; 327fd538803SMichal Hocko int zid; 328a3d8e054SKOSAKI Motohiro 329fd538803SMichal Hocko if (!mem_cgroup_disabled()) 330fd538803SMichal Hocko lru_size = mem_cgroup_get_lru_size(lruvec, lru); 331fd538803SMichal Hocko else 332fd538803SMichal Hocko lru_size = node_page_state(lruvec_pgdat(lruvec), NR_LRU_BASE + lru); 333fd538803SMichal Hocko 334fd538803SMichal Hocko for (zid = zone_idx + 1; zid < MAX_NR_ZONES; zid++) { 335fd538803SMichal Hocko struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; 336fd538803SMichal Hocko unsigned long size; 337fd538803SMichal Hocko 338fd538803SMichal Hocko if (!managed_zone(zone)) 339fd538803SMichal Hocko continue; 340fd538803SMichal Hocko 341fd538803SMichal Hocko if (!mem_cgroup_disabled()) 342fd538803SMichal Hocko size = mem_cgroup_get_zone_lru_size(lruvec, lru, zid); 343fd538803SMichal Hocko else 344fd538803SMichal Hocko size = zone_page_state(&lruvec_pgdat(lruvec)->node_zones[zid], 345fd538803SMichal Hocko NR_ZONE_LRU_BASE + lru); 346fd538803SMichal Hocko lru_size -= min(size, lru_size); 347c9f299d9SKOSAKI Motohiro } 348c9f299d9SKOSAKI Motohiro 349fd538803SMichal Hocko return lru_size; 350b4536f0cSMichal Hocko 351b4536f0cSMichal Hocko } 352b4536f0cSMichal Hocko 3531da177e4SLinus Torvalds /* 3541d3d4437SGlauber Costa * Add a shrinker callback to be called from the vm. 3551da177e4SLinus Torvalds */ 3568e04944fSTetsuo Handa int prealloc_shrinker(struct shrinker *shrinker) 3571da177e4SLinus Torvalds { 3581d3d4437SGlauber Costa size_t size = sizeof(*shrinker->nr_deferred); 3591d3d4437SGlauber Costa 3601d3d4437SGlauber Costa if (shrinker->flags & SHRINKER_NUMA_AWARE) 3611d3d4437SGlauber Costa size *= nr_node_ids; 3621d3d4437SGlauber Costa 3631d3d4437SGlauber Costa shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); 3641d3d4437SGlauber Costa if (!shrinker->nr_deferred) 3651d3d4437SGlauber Costa return -ENOMEM; 366b4c2b231SKirill Tkhai 367b0dedc49SKirill Tkhai /* 368b0dedc49SKirill Tkhai * There is a window between prealloc_shrinker() 369b0dedc49SKirill Tkhai * and register_shrinker_prepared(). We don't want 370b0dedc49SKirill Tkhai * to clear bit of a shrinker in such the state 371b0dedc49SKirill Tkhai * in shrink_slab_memcg(), since this will impose 372b0dedc49SKirill Tkhai * restrictions on a code registering a shrinker 373b0dedc49SKirill Tkhai * (they would have to guarantee, their LRU lists 374b0dedc49SKirill Tkhai * are empty till shrinker is completely registered). 375b0dedc49SKirill Tkhai * So, we differ the situation, when 1)a shrinker 376b0dedc49SKirill Tkhai * is semi-registered (id is assigned, but it has 377b0dedc49SKirill Tkhai * not yet linked to shrinker_list) and 2)shrinker 378b0dedc49SKirill Tkhai * is not registered (id is not assigned). 379b0dedc49SKirill Tkhai */ 380b0dedc49SKirill Tkhai INIT_LIST_HEAD(&shrinker->list); 381b0dedc49SKirill Tkhai 382b4c2b231SKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) { 383b4c2b231SKirill Tkhai if (prealloc_memcg_shrinker(shrinker)) 384b4c2b231SKirill Tkhai goto free_deferred; 385b4c2b231SKirill Tkhai } 386b4c2b231SKirill Tkhai 3878e04944fSTetsuo Handa return 0; 388b4c2b231SKirill Tkhai 389b4c2b231SKirill Tkhai free_deferred: 390b4c2b231SKirill Tkhai kfree(shrinker->nr_deferred); 391b4c2b231SKirill Tkhai shrinker->nr_deferred = NULL; 392b4c2b231SKirill Tkhai return -ENOMEM; 3938e04944fSTetsuo Handa } 3941d3d4437SGlauber Costa 3958e04944fSTetsuo Handa void free_prealloced_shrinker(struct shrinker *shrinker) 3968e04944fSTetsuo Handa { 397b4c2b231SKirill Tkhai if (!shrinker->nr_deferred) 398b4c2b231SKirill Tkhai return; 399b4c2b231SKirill Tkhai 400b4c2b231SKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) 401b4c2b231SKirill Tkhai unregister_memcg_shrinker(shrinker); 402b4c2b231SKirill Tkhai 4038e04944fSTetsuo Handa kfree(shrinker->nr_deferred); 4048e04944fSTetsuo Handa shrinker->nr_deferred = NULL; 4058e04944fSTetsuo Handa } 4068e04944fSTetsuo Handa 4078e04944fSTetsuo Handa void register_shrinker_prepared(struct shrinker *shrinker) 4088e04944fSTetsuo Handa { 4091da177e4SLinus Torvalds down_write(&shrinker_rwsem); 4101da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 4111da177e4SLinus Torvalds up_write(&shrinker_rwsem); 4128e04944fSTetsuo Handa } 4138e04944fSTetsuo Handa 4148e04944fSTetsuo Handa int register_shrinker(struct shrinker *shrinker) 4158e04944fSTetsuo Handa { 4168e04944fSTetsuo Handa int err = prealloc_shrinker(shrinker); 4178e04944fSTetsuo Handa 4188e04944fSTetsuo Handa if (err) 4198e04944fSTetsuo Handa return err; 4208e04944fSTetsuo Handa register_shrinker_prepared(shrinker); 4211d3d4437SGlauber Costa return 0; 4221da177e4SLinus Torvalds } 4238e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds /* 4261da177e4SLinus Torvalds * Remove one 4271da177e4SLinus Torvalds */ 4288e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 4291da177e4SLinus Torvalds { 430bb422a73STetsuo Handa if (!shrinker->nr_deferred) 431bb422a73STetsuo Handa return; 432b4c2b231SKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) 433b4c2b231SKirill Tkhai unregister_memcg_shrinker(shrinker); 4341da177e4SLinus Torvalds down_write(&shrinker_rwsem); 4351da177e4SLinus Torvalds list_del(&shrinker->list); 4361da177e4SLinus Torvalds up_write(&shrinker_rwsem); 437ae393321SAndrew Vagin kfree(shrinker->nr_deferred); 438bb422a73STetsuo Handa shrinker->nr_deferred = NULL; 4391da177e4SLinus Torvalds } 4408e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds #define SHRINK_BATCH 128 4431d3d4437SGlauber Costa 444cb731d6cSVladimir Davydov static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, 4459092c71bSJosef Bacik struct shrinker *shrinker, int priority) 4461da177e4SLinus Torvalds { 44724f7c6b9SDave Chinner unsigned long freed = 0; 4481da177e4SLinus Torvalds unsigned long long delta; 449635697c6SKonstantin Khlebnikov long total_scan; 450d5bc5fd3SVladimir Davydov long freeable; 451acf92b48SDave Chinner long nr; 452acf92b48SDave Chinner long new_nr; 4531d3d4437SGlauber Costa int nid = shrinkctl->nid; 454e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 455e9299f50SDave Chinner : SHRINK_BATCH; 4565f33a080SShaohua Li long scanned = 0, next_deferred; 4571da177e4SLinus Torvalds 458*ac7fb3adSKirill Tkhai if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) 459*ac7fb3adSKirill Tkhai nid = 0; 460*ac7fb3adSKirill Tkhai 461d5bc5fd3SVladimir Davydov freeable = shrinker->count_objects(shrinker, shrinkctl); 4629b996468SKirill Tkhai if (freeable == 0 || freeable == SHRINK_EMPTY) 4639b996468SKirill Tkhai return freeable; 464635697c6SKonstantin Khlebnikov 465acf92b48SDave Chinner /* 466acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 467acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 468acf92b48SDave Chinner * don't also do this scanning work. 469acf92b48SDave Chinner */ 4701d3d4437SGlauber Costa nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0); 471acf92b48SDave Chinner 472acf92b48SDave Chinner total_scan = nr; 4739092c71bSJosef Bacik delta = freeable >> priority; 4749092c71bSJosef Bacik delta *= 4; 4759092c71bSJosef Bacik do_div(delta, shrinker->seeks); 476acf92b48SDave Chinner total_scan += delta; 477acf92b48SDave Chinner if (total_scan < 0) { 4788612c663SPintu Kumar pr_err("shrink_slab: %pF negative objects to delete nr=%ld\n", 479a0b02131SDave Chinner shrinker->scan_objects, total_scan); 480d5bc5fd3SVladimir Davydov total_scan = freeable; 4815f33a080SShaohua Li next_deferred = nr; 4825f33a080SShaohua Li } else 4835f33a080SShaohua Li next_deferred = total_scan; 484ea164d73SAndrea Arcangeli 485ea164d73SAndrea Arcangeli /* 4863567b59aSDave Chinner * We need to avoid excessive windup on filesystem shrinkers 4873567b59aSDave Chinner * due to large numbers of GFP_NOFS allocations causing the 4883567b59aSDave Chinner * shrinkers to return -1 all the time. This results in a large 4893567b59aSDave Chinner * nr being built up so when a shrink that can do some work 4903567b59aSDave Chinner * comes along it empties the entire cache due to nr >>> 491d5bc5fd3SVladimir Davydov * freeable. This is bad for sustaining a working set in 4923567b59aSDave Chinner * memory. 4933567b59aSDave Chinner * 4943567b59aSDave Chinner * Hence only allow the shrinker to scan the entire cache when 4953567b59aSDave Chinner * a large delta change is calculated directly. 4963567b59aSDave Chinner */ 497d5bc5fd3SVladimir Davydov if (delta < freeable / 4) 498d5bc5fd3SVladimir Davydov total_scan = min(total_scan, freeable / 2); 4993567b59aSDave Chinner 5003567b59aSDave Chinner /* 501ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 502ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 503ea164d73SAndrea Arcangeli * freeable entries. 504ea164d73SAndrea Arcangeli */ 505d5bc5fd3SVladimir Davydov if (total_scan > freeable * 2) 506d5bc5fd3SVladimir Davydov total_scan = freeable * 2; 5071da177e4SLinus Torvalds 50824f7c6b9SDave Chinner trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, 5099092c71bSJosef Bacik freeable, delta, total_scan, priority); 51009576073SDave Chinner 5110b1fb40aSVladimir Davydov /* 5120b1fb40aSVladimir Davydov * Normally, we should not scan less than batch_size objects in one 5130b1fb40aSVladimir Davydov * pass to avoid too frequent shrinker calls, but if the slab has less 5140b1fb40aSVladimir Davydov * than batch_size objects in total and we are really tight on memory, 5150b1fb40aSVladimir Davydov * we will try to reclaim all available objects, otherwise we can end 5160b1fb40aSVladimir Davydov * up failing allocations although there are plenty of reclaimable 5170b1fb40aSVladimir Davydov * objects spread over several slabs with usage less than the 5180b1fb40aSVladimir Davydov * batch_size. 5190b1fb40aSVladimir Davydov * 5200b1fb40aSVladimir Davydov * We detect the "tight on memory" situations by looking at the total 5210b1fb40aSVladimir Davydov * number of objects we want to scan (total_scan). If it is greater 522d5bc5fd3SVladimir Davydov * than the total number of objects on slab (freeable), we must be 5230b1fb40aSVladimir Davydov * scanning at high prio and therefore should try to reclaim as much as 5240b1fb40aSVladimir Davydov * possible. 5250b1fb40aSVladimir Davydov */ 5260b1fb40aSVladimir Davydov while (total_scan >= batch_size || 527d5bc5fd3SVladimir Davydov total_scan >= freeable) { 52824f7c6b9SDave Chinner unsigned long ret; 5290b1fb40aSVladimir Davydov unsigned long nr_to_scan = min(batch_size, total_scan); 5301da177e4SLinus Torvalds 5310b1fb40aSVladimir Davydov shrinkctl->nr_to_scan = nr_to_scan; 532d460acb5SChris Wilson shrinkctl->nr_scanned = nr_to_scan; 53324f7c6b9SDave Chinner ret = shrinker->scan_objects(shrinker, shrinkctl); 53424f7c6b9SDave Chinner if (ret == SHRINK_STOP) 5351da177e4SLinus Torvalds break; 53624f7c6b9SDave Chinner freed += ret; 53724f7c6b9SDave Chinner 538d460acb5SChris Wilson count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned); 539d460acb5SChris Wilson total_scan -= shrinkctl->nr_scanned; 540d460acb5SChris Wilson scanned += shrinkctl->nr_scanned; 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds cond_resched(); 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds 5455f33a080SShaohua Li if (next_deferred >= scanned) 5465f33a080SShaohua Li next_deferred -= scanned; 5475f33a080SShaohua Li else 5485f33a080SShaohua Li next_deferred = 0; 549acf92b48SDave Chinner /* 550acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 551acf92b48SDave Chinner * manner that handles concurrent updates. If we exhausted the 552acf92b48SDave Chinner * scan, there is no need to do an update. 553acf92b48SDave Chinner */ 5545f33a080SShaohua Li if (next_deferred > 0) 5555f33a080SShaohua Li new_nr = atomic_long_add_return(next_deferred, 5561d3d4437SGlauber Costa &shrinker->nr_deferred[nid]); 55783aeeadaSKonstantin Khlebnikov else 5581d3d4437SGlauber Costa new_nr = atomic_long_read(&shrinker->nr_deferred[nid]); 559acf92b48SDave Chinner 560df9024a8SDave Hansen trace_mm_shrink_slab_end(shrinker, nid, freed, nr, new_nr, total_scan); 5611d3d4437SGlauber Costa return freed; 5621d3d4437SGlauber Costa } 5631d3d4437SGlauber Costa 564b0dedc49SKirill Tkhai #ifdef CONFIG_MEMCG_KMEM 565b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 566b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 567b0dedc49SKirill Tkhai { 568b0dedc49SKirill Tkhai struct memcg_shrinker_map *map; 569b0dedc49SKirill Tkhai unsigned long freed = 0; 570b0dedc49SKirill Tkhai int ret, i; 571b0dedc49SKirill Tkhai 572b0dedc49SKirill Tkhai if (!memcg_kmem_enabled() || !mem_cgroup_online(memcg)) 573b0dedc49SKirill Tkhai return 0; 574b0dedc49SKirill Tkhai 575b0dedc49SKirill Tkhai if (!down_read_trylock(&shrinker_rwsem)) 576b0dedc49SKirill Tkhai return 0; 577b0dedc49SKirill Tkhai 578b0dedc49SKirill Tkhai map = rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_map, 579b0dedc49SKirill Tkhai true); 580b0dedc49SKirill Tkhai if (unlikely(!map)) 581b0dedc49SKirill Tkhai goto unlock; 582b0dedc49SKirill Tkhai 583b0dedc49SKirill Tkhai for_each_set_bit(i, map->map, shrinker_nr_max) { 584b0dedc49SKirill Tkhai struct shrink_control sc = { 585b0dedc49SKirill Tkhai .gfp_mask = gfp_mask, 586b0dedc49SKirill Tkhai .nid = nid, 587b0dedc49SKirill Tkhai .memcg = memcg, 588b0dedc49SKirill Tkhai }; 589b0dedc49SKirill Tkhai struct shrinker *shrinker; 590b0dedc49SKirill Tkhai 591b0dedc49SKirill Tkhai shrinker = idr_find(&shrinker_idr, i); 592b0dedc49SKirill Tkhai if (unlikely(!shrinker)) { 593b0dedc49SKirill Tkhai clear_bit(i, map->map); 594b0dedc49SKirill Tkhai continue; 595b0dedc49SKirill Tkhai } 596b0dedc49SKirill Tkhai 597b0dedc49SKirill Tkhai /* See comment in prealloc_shrinker() */ 598b0dedc49SKirill Tkhai if (unlikely(list_empty(&shrinker->list))) 599b0dedc49SKirill Tkhai continue; 600b0dedc49SKirill Tkhai 601b0dedc49SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 602f90280d6SKirill Tkhai if (ret == SHRINK_EMPTY) { 603f90280d6SKirill Tkhai clear_bit(i, map->map); 604f90280d6SKirill Tkhai /* 605f90280d6SKirill Tkhai * After the shrinker reported that it had no objects to 606f90280d6SKirill Tkhai * free, but before we cleared the corresponding bit in 607f90280d6SKirill Tkhai * the memcg shrinker map, a new object might have been 608f90280d6SKirill Tkhai * added. To make sure, we have the bit set in this 609f90280d6SKirill Tkhai * case, we invoke the shrinker one more time and reset 610f90280d6SKirill Tkhai * the bit if it reports that it is not empty anymore. 611f90280d6SKirill Tkhai * The memory barrier here pairs with the barrier in 612f90280d6SKirill Tkhai * memcg_set_shrinker_bit(): 613f90280d6SKirill Tkhai * 614f90280d6SKirill Tkhai * list_lru_add() shrink_slab_memcg() 615f90280d6SKirill Tkhai * list_add_tail() clear_bit() 616f90280d6SKirill Tkhai * <MB> <MB> 617f90280d6SKirill Tkhai * set_bit() do_shrink_slab() 618f90280d6SKirill Tkhai */ 619f90280d6SKirill Tkhai smp_mb__after_atomic(); 620f90280d6SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 6219b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 6229b996468SKirill Tkhai ret = 0; 623f90280d6SKirill Tkhai else 624f90280d6SKirill Tkhai memcg_set_shrinker_bit(memcg, nid, i); 625f90280d6SKirill Tkhai } 626b0dedc49SKirill Tkhai freed += ret; 627b0dedc49SKirill Tkhai 628b0dedc49SKirill Tkhai if (rwsem_is_contended(&shrinker_rwsem)) { 629b0dedc49SKirill Tkhai freed = freed ? : 1; 630b0dedc49SKirill Tkhai break; 631b0dedc49SKirill Tkhai } 632b0dedc49SKirill Tkhai } 633b0dedc49SKirill Tkhai unlock: 634b0dedc49SKirill Tkhai up_read(&shrinker_rwsem); 635b0dedc49SKirill Tkhai return freed; 636b0dedc49SKirill Tkhai } 637b0dedc49SKirill Tkhai #else /* CONFIG_MEMCG_KMEM */ 638b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 639b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 640b0dedc49SKirill Tkhai { 641b0dedc49SKirill Tkhai return 0; 642b0dedc49SKirill Tkhai } 643b0dedc49SKirill Tkhai #endif /* CONFIG_MEMCG_KMEM */ 644b0dedc49SKirill Tkhai 6456b4f7799SJohannes Weiner /** 646cb731d6cSVladimir Davydov * shrink_slab - shrink slab caches 6476b4f7799SJohannes Weiner * @gfp_mask: allocation context 6486b4f7799SJohannes Weiner * @nid: node whose slab caches to target 649cb731d6cSVladimir Davydov * @memcg: memory cgroup whose slab caches to target 6509092c71bSJosef Bacik * @priority: the reclaim priority 6511d3d4437SGlauber Costa * 6526b4f7799SJohannes Weiner * Call the shrink functions to age shrinkable caches. 6531d3d4437SGlauber Costa * 6546b4f7799SJohannes Weiner * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, 6556b4f7799SJohannes Weiner * unaware shrinkers will receive a node id of 0 instead. 6561d3d4437SGlauber Costa * 657aeed1d32SVladimir Davydov * @memcg specifies the memory cgroup to target. Unaware shrinkers 658aeed1d32SVladimir Davydov * are called only if it is the root cgroup. 659cb731d6cSVladimir Davydov * 6609092c71bSJosef Bacik * @priority is sc->priority, we take the number of objects and >> by priority 6619092c71bSJosef Bacik * in order to get the scan target. 6621d3d4437SGlauber Costa * 6636b4f7799SJohannes Weiner * Returns the number of reclaimed slab objects. 6641d3d4437SGlauber Costa */ 665cb731d6cSVladimir Davydov static unsigned long shrink_slab(gfp_t gfp_mask, int nid, 666cb731d6cSVladimir Davydov struct mem_cgroup *memcg, 6679092c71bSJosef Bacik int priority) 6681d3d4437SGlauber Costa { 6691d3d4437SGlauber Costa struct shrinker *shrinker; 6701d3d4437SGlauber Costa unsigned long freed = 0; 6719b996468SKirill Tkhai int ret; 6721d3d4437SGlauber Costa 673aeed1d32SVladimir Davydov if (!mem_cgroup_is_root(memcg)) 674b0dedc49SKirill Tkhai return shrink_slab_memcg(gfp_mask, nid, memcg, priority); 675cb731d6cSVladimir Davydov 676e830c63aSTetsuo Handa if (!down_read_trylock(&shrinker_rwsem)) 6771d3d4437SGlauber Costa goto out; 6781d3d4437SGlauber Costa 6791d3d4437SGlauber Costa list_for_each_entry(shrinker, &shrinker_list, list) { 6806b4f7799SJohannes Weiner struct shrink_control sc = { 6816b4f7799SJohannes Weiner .gfp_mask = gfp_mask, 6826b4f7799SJohannes Weiner .nid = nid, 683cb731d6cSVladimir Davydov .memcg = memcg, 6846b4f7799SJohannes Weiner }; 6856b4f7799SJohannes Weiner 6869b996468SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 6879b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 6889b996468SKirill Tkhai ret = 0; 6899b996468SKirill Tkhai freed += ret; 690e496612cSMinchan Kim /* 691e496612cSMinchan Kim * Bail out if someone want to register a new shrinker to 692e496612cSMinchan Kim * prevent the regsitration from being stalled for long periods 693e496612cSMinchan Kim * by parallel ongoing shrinking. 694e496612cSMinchan Kim */ 695e496612cSMinchan Kim if (rwsem_is_contended(&shrinker_rwsem)) { 696e496612cSMinchan Kim freed = freed ? : 1; 697e496612cSMinchan Kim break; 698e496612cSMinchan Kim } 699ec97097bSVladimir Davydov } 7001d3d4437SGlauber Costa 7011da177e4SLinus Torvalds up_read(&shrinker_rwsem); 702f06590bdSMinchan Kim out: 703f06590bdSMinchan Kim cond_resched(); 70424f7c6b9SDave Chinner return freed; 7051da177e4SLinus Torvalds } 7061da177e4SLinus Torvalds 707cb731d6cSVladimir Davydov void drop_slab_node(int nid) 708cb731d6cSVladimir Davydov { 709cb731d6cSVladimir Davydov unsigned long freed; 710cb731d6cSVladimir Davydov 711cb731d6cSVladimir Davydov do { 712cb731d6cSVladimir Davydov struct mem_cgroup *memcg = NULL; 713cb731d6cSVladimir Davydov 714cb731d6cSVladimir Davydov freed = 0; 715aeed1d32SVladimir Davydov memcg = mem_cgroup_iter(NULL, NULL, NULL); 716cb731d6cSVladimir Davydov do { 7179092c71bSJosef Bacik freed += shrink_slab(GFP_KERNEL, nid, memcg, 0); 718cb731d6cSVladimir Davydov } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 719cb731d6cSVladimir Davydov } while (freed > 10); 720cb731d6cSVladimir Davydov } 721cb731d6cSVladimir Davydov 722cb731d6cSVladimir Davydov void drop_slab(void) 723cb731d6cSVladimir Davydov { 724cb731d6cSVladimir Davydov int nid; 725cb731d6cSVladimir Davydov 726cb731d6cSVladimir Davydov for_each_online_node(nid) 727cb731d6cSVladimir Davydov drop_slab_node(nid); 728cb731d6cSVladimir Davydov } 729cb731d6cSVladimir Davydov 7301da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 7311da177e4SLinus Torvalds { 732ceddc3a5SJohannes Weiner /* 733ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 734ceddc3a5SJohannes Weiner * that isolated the page, the page cache radix tree and 735ceddc3a5SJohannes Weiner * optional buffer heads at page->private. 736ceddc3a5SJohannes Weiner */ 737bd4c82c2SHuang Ying int radix_pins = PageTransHuge(page) && PageSwapCache(page) ? 738bd4c82c2SHuang Ying HPAGE_PMD_NR : 1; 739bd4c82c2SHuang Ying return page_count(page) - page_has_private(page) == 1 + radix_pins; 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds 742703c2708STejun Heo static int may_write_to_inode(struct inode *inode, struct scan_control *sc) 7431da177e4SLinus Torvalds { 744930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 7451da177e4SLinus Torvalds return 1; 746703c2708STejun Heo if (!inode_write_congested(inode)) 7471da177e4SLinus Torvalds return 1; 748703c2708STejun Heo if (inode_to_bdi(inode) == current->backing_dev_info) 7491da177e4SLinus Torvalds return 1; 7501da177e4SLinus Torvalds return 0; 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds /* 7541da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 7551da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 7561da177e4SLinus Torvalds * fsync(), msync() or close(). 7571da177e4SLinus Torvalds * 7581da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 7591da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 7601da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 7611da177e4SLinus Torvalds * 7621da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 7631da177e4SLinus Torvalds * __GFP_FS. 7641da177e4SLinus Torvalds */ 7651da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 7661da177e4SLinus Torvalds struct page *page, int error) 7671da177e4SLinus Torvalds { 7687eaceaccSJens Axboe lock_page(page); 7693e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 7703e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 7711da177e4SLinus Torvalds unlock_page(page); 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 77404e62a29SChristoph Lameter /* possible outcome of pageout() */ 77504e62a29SChristoph Lameter typedef enum { 77604e62a29SChristoph Lameter /* failed to write page out, page is locked */ 77704e62a29SChristoph Lameter PAGE_KEEP, 77804e62a29SChristoph Lameter /* move page to the active list, page is locked */ 77904e62a29SChristoph Lameter PAGE_ACTIVATE, 78004e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 78104e62a29SChristoph Lameter PAGE_SUCCESS, 78204e62a29SChristoph Lameter /* page is clean and locked */ 78304e62a29SChristoph Lameter PAGE_CLEAN, 78404e62a29SChristoph Lameter } pageout_t; 78504e62a29SChristoph Lameter 7861da177e4SLinus Torvalds /* 7871742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 7881742f19fSAndrew Morton * Calls ->writepage(). 7891da177e4SLinus Torvalds */ 790c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping, 7917d3579e8SKOSAKI Motohiro struct scan_control *sc) 7921da177e4SLinus Torvalds { 7931da177e4SLinus Torvalds /* 7941da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 7951da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 7961da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 7971da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 7981da177e4SLinus Torvalds * PagePrivate for that. 7991da177e4SLinus Torvalds * 8008174202bSAl Viro * If this process is currently in __generic_file_write_iter() against 8011da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 8021da177e4SLinus Torvalds * will block. 8031da177e4SLinus Torvalds * 8041da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 8051da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 8061da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 8071da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 8081da177e4SLinus Torvalds */ 8091da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 8101da177e4SLinus Torvalds return PAGE_KEEP; 8111da177e4SLinus Torvalds if (!mapping) { 8121da177e4SLinus Torvalds /* 8131da177e4SLinus Torvalds * Some data journaling orphaned pages can have 8141da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 8151da177e4SLinus Torvalds */ 816266cf658SDavid Howells if (page_has_private(page)) { 8171da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 8181da177e4SLinus Torvalds ClearPageDirty(page); 819b1de0d13SMitchel Humpherys pr_info("%s: orphaned page\n", __func__); 8201da177e4SLinus Torvalds return PAGE_CLEAN; 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds } 8231da177e4SLinus Torvalds return PAGE_KEEP; 8241da177e4SLinus Torvalds } 8251da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 8261da177e4SLinus Torvalds return PAGE_ACTIVATE; 827703c2708STejun Heo if (!may_write_to_inode(mapping->host, sc)) 8281da177e4SLinus Torvalds return PAGE_KEEP; 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 8311da177e4SLinus Torvalds int res; 8321da177e4SLinus Torvalds struct writeback_control wbc = { 8331da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 8341da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 835111ebb6eSOGAWA Hirofumi .range_start = 0, 836111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 8371da177e4SLinus Torvalds .for_reclaim = 1, 8381da177e4SLinus Torvalds }; 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds SetPageReclaim(page); 8411da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 8421da177e4SLinus Torvalds if (res < 0) 8431da177e4SLinus Torvalds handle_write_error(mapping, page, res); 844994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 8451da177e4SLinus Torvalds ClearPageReclaim(page); 8461da177e4SLinus Torvalds return PAGE_ACTIVATE; 8471da177e4SLinus Torvalds } 848c661b078SAndy Whitcroft 8491da177e4SLinus Torvalds if (!PageWriteback(page)) { 8501da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 8511da177e4SLinus Torvalds ClearPageReclaim(page); 8521da177e4SLinus Torvalds } 8533aa23851Syalin wang trace_mm_vmscan_writepage(page); 854c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_WRITE); 8551da177e4SLinus Torvalds return PAGE_SUCCESS; 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds return PAGE_CLEAN; 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds 861a649fd92SAndrew Morton /* 862e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 863e286781dSNick Piggin * gets returned with a refcount of 0. 864a649fd92SAndrew Morton */ 865a528910eSJohannes Weiner static int __remove_mapping(struct address_space *mapping, struct page *page, 866a528910eSJohannes Weiner bool reclaimed) 86749d2e9ccSChristoph Lameter { 868c4843a75SGreg Thelen unsigned long flags; 869bd4c82c2SHuang Ying int refcount; 870c4843a75SGreg Thelen 87128e4d965SNick Piggin BUG_ON(!PageLocked(page)); 87228e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 87349d2e9ccSChristoph Lameter 874b93b0163SMatthew Wilcox xa_lock_irqsave(&mapping->i_pages, flags); 87549d2e9ccSChristoph Lameter /* 8760fd0e6b0SNick Piggin * The non racy check for a busy page. 8770fd0e6b0SNick Piggin * 8780fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 8790fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 8800fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 8810fd0e6b0SNick Piggin * here, then the following race may occur: 8820fd0e6b0SNick Piggin * 8830fd0e6b0SNick Piggin * get_user_pages(&page); 8840fd0e6b0SNick Piggin * [user mapping goes away] 8850fd0e6b0SNick Piggin * write_to(page); 8860fd0e6b0SNick Piggin * !PageDirty(page) [good] 8870fd0e6b0SNick Piggin * SetPageDirty(page); 8880fd0e6b0SNick Piggin * put_page(page); 8890fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 8900fd0e6b0SNick Piggin * 8910fd0e6b0SNick Piggin * [oops, our write_to data is lost] 8920fd0e6b0SNick Piggin * 8930fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 8940fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 8950139aa7bSJoonsoo Kim * load is not satisfied before that of page->_refcount. 8960fd0e6b0SNick Piggin * 8970fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 898b93b0163SMatthew Wilcox * and thus under the i_pages lock, then this ordering is not required. 89949d2e9ccSChristoph Lameter */ 900bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page)) && PageSwapCache(page)) 901bd4c82c2SHuang Ying refcount = 1 + HPAGE_PMD_NR; 902bd4c82c2SHuang Ying else 903bd4c82c2SHuang Ying refcount = 2; 904bd4c82c2SHuang Ying if (!page_ref_freeze(page, refcount)) 90549d2e9ccSChristoph Lameter goto cannot_free; 906e286781dSNick Piggin /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ 907e286781dSNick Piggin if (unlikely(PageDirty(page))) { 908bd4c82c2SHuang Ying page_ref_unfreeze(page, refcount); 90949d2e9ccSChristoph Lameter goto cannot_free; 910e286781dSNick Piggin } 91149d2e9ccSChristoph Lameter 91249d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 91349d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 9140a31bc97SJohannes Weiner mem_cgroup_swapout(page, swap); 91549d2e9ccSChristoph Lameter __delete_from_swap_cache(page); 916b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 91775f6d6d2SMinchan Kim put_swap_page(page, swap); 918e286781dSNick Piggin } else { 9196072d13cSLinus Torvalds void (*freepage)(struct page *); 920a528910eSJohannes Weiner void *shadow = NULL; 9216072d13cSLinus Torvalds 9226072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 923a528910eSJohannes Weiner /* 924a528910eSJohannes Weiner * Remember a shadow entry for reclaimed file cache in 925a528910eSJohannes Weiner * order to detect refaults, thus thrashing, later on. 926a528910eSJohannes Weiner * 927a528910eSJohannes Weiner * But don't store shadows in an address space that is 928a528910eSJohannes Weiner * already exiting. This is not just an optizimation, 929a528910eSJohannes Weiner * inode reclaim needs to empty out the radix tree or 930a528910eSJohannes Weiner * the nodes are lost. Don't plant shadows behind its 931a528910eSJohannes Weiner * back. 932f9fe48beSRoss Zwisler * 933f9fe48beSRoss Zwisler * We also don't store shadows for DAX mappings because the 934f9fe48beSRoss Zwisler * only page cache pages found in these are zero pages 935f9fe48beSRoss Zwisler * covering holes, and because we don't want to mix DAX 936f9fe48beSRoss Zwisler * exceptional entries and shadow exceptional entries in the 937b93b0163SMatthew Wilcox * same address_space. 938a528910eSJohannes Weiner */ 939a528910eSJohannes Weiner if (reclaimed && page_is_file_cache(page) && 940f9fe48beSRoss Zwisler !mapping_exiting(mapping) && !dax_mapping(mapping)) 941a528910eSJohannes Weiner shadow = workingset_eviction(mapping, page); 94262cccb8cSJohannes Weiner __delete_from_page_cache(page, shadow); 943b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 9446072d13cSLinus Torvalds 9456072d13cSLinus Torvalds if (freepage != NULL) 9466072d13cSLinus Torvalds freepage(page); 947e286781dSNick Piggin } 948e286781dSNick Piggin 94949d2e9ccSChristoph Lameter return 1; 95049d2e9ccSChristoph Lameter 95149d2e9ccSChristoph Lameter cannot_free: 952b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 95349d2e9ccSChristoph Lameter return 0; 95449d2e9ccSChristoph Lameter } 95549d2e9ccSChristoph Lameter 9561da177e4SLinus Torvalds /* 957e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 958e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 959e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 960e286781dSNick Piggin * this page. 961e286781dSNick Piggin */ 962e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 963e286781dSNick Piggin { 964a528910eSJohannes Weiner if (__remove_mapping(mapping, page, false)) { 965e286781dSNick Piggin /* 966e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 967e286781dSNick Piggin * drops the pagecache ref for us without requiring another 968e286781dSNick Piggin * atomic operation. 969e286781dSNick Piggin */ 970fe896d18SJoonsoo Kim page_ref_unfreeze(page, 1); 971e286781dSNick Piggin return 1; 972e286781dSNick Piggin } 973e286781dSNick Piggin return 0; 974e286781dSNick Piggin } 975e286781dSNick Piggin 976894bc310SLee Schermerhorn /** 977894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 978894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 979894bc310SLee Schermerhorn * 980894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 981894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 982894bc310SLee Schermerhorn * 983894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 984894bc310SLee Schermerhorn */ 985894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 986894bc310SLee Schermerhorn { 987c53954a0SMel Gorman lru_cache_add(page); 988894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 989894bc310SLee Schermerhorn } 990894bc310SLee Schermerhorn 991dfc8d636SJohannes Weiner enum page_references { 992dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 993dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 99464574746SJohannes Weiner PAGEREF_KEEP, 995dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 996dfc8d636SJohannes Weiner }; 997dfc8d636SJohannes Weiner 998dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 999dfc8d636SJohannes Weiner struct scan_control *sc) 1000dfc8d636SJohannes Weiner { 100164574746SJohannes Weiner int referenced_ptes, referenced_page; 1002dfc8d636SJohannes Weiner unsigned long vm_flags; 1003dfc8d636SJohannes Weiner 1004c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 1005c3ac9a8aSJohannes Weiner &vm_flags); 100664574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 1007dfc8d636SJohannes Weiner 1008dfc8d636SJohannes Weiner /* 1009dfc8d636SJohannes Weiner * Mlock lost the isolation race with us. Let try_to_unmap() 1010dfc8d636SJohannes Weiner * move the page to the unevictable list. 1011dfc8d636SJohannes Weiner */ 1012dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 1013dfc8d636SJohannes Weiner return PAGEREF_RECLAIM; 1014dfc8d636SJohannes Weiner 101564574746SJohannes Weiner if (referenced_ptes) { 1016e4898273SMichal Hocko if (PageSwapBacked(page)) 101764574746SJohannes Weiner return PAGEREF_ACTIVATE; 101864574746SJohannes Weiner /* 101964574746SJohannes Weiner * All mapped pages start out with page table 102064574746SJohannes Weiner * references from the instantiating fault, so we need 102164574746SJohannes Weiner * to look twice if a mapped file page is used more 102264574746SJohannes Weiner * than once. 102364574746SJohannes Weiner * 102464574746SJohannes Weiner * Mark it and spare it for another trip around the 102564574746SJohannes Weiner * inactive list. Another page table reference will 102664574746SJohannes Weiner * lead to its activation. 102764574746SJohannes Weiner * 102864574746SJohannes Weiner * Note: the mark is set for activated pages as well 102964574746SJohannes Weiner * so that recently deactivated but used pages are 103064574746SJohannes Weiner * quickly recovered. 103164574746SJohannes Weiner */ 103264574746SJohannes Weiner SetPageReferenced(page); 103364574746SJohannes Weiner 103434dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 1035dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 1036dfc8d636SJohannes Weiner 1037c909e993SKonstantin Khlebnikov /* 1038c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 1039c909e993SKonstantin Khlebnikov */ 1040c909e993SKonstantin Khlebnikov if (vm_flags & VM_EXEC) 1041c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 1042c909e993SKonstantin Khlebnikov 104364574746SJohannes Weiner return PAGEREF_KEEP; 104464574746SJohannes Weiner } 104564574746SJohannes Weiner 1046dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 10472e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 1048dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 104964574746SJohannes Weiner 105064574746SJohannes Weiner return PAGEREF_RECLAIM; 1051dfc8d636SJohannes Weiner } 1052dfc8d636SJohannes Weiner 1053e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 1054e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page, 1055e2be15f6SMel Gorman bool *dirty, bool *writeback) 1056e2be15f6SMel Gorman { 1057b4597226SMel Gorman struct address_space *mapping; 1058b4597226SMel Gorman 1059e2be15f6SMel Gorman /* 1060e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 1061e2be15f6SMel Gorman * from reclaim context. Do not stall reclaim based on them 1062e2be15f6SMel Gorman */ 1063802a3a92SShaohua Li if (!page_is_file_cache(page) || 1064802a3a92SShaohua Li (PageAnon(page) && !PageSwapBacked(page))) { 1065e2be15f6SMel Gorman *dirty = false; 1066e2be15f6SMel Gorman *writeback = false; 1067e2be15f6SMel Gorman return; 1068e2be15f6SMel Gorman } 1069e2be15f6SMel Gorman 1070e2be15f6SMel Gorman /* By default assume that the page flags are accurate */ 1071e2be15f6SMel Gorman *dirty = PageDirty(page); 1072e2be15f6SMel Gorman *writeback = PageWriteback(page); 1073b4597226SMel Gorman 1074b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 1075b4597226SMel Gorman if (!page_has_private(page)) 1076b4597226SMel Gorman return; 1077b4597226SMel Gorman 1078b4597226SMel Gorman mapping = page_mapping(page); 1079b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 1080b4597226SMel Gorman mapping->a_ops->is_dirty_writeback(page, dirty, writeback); 1081e2be15f6SMel Gorman } 1082e2be15f6SMel Gorman 1083e286781dSNick Piggin /* 10841742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 10851da177e4SLinus Torvalds */ 10861742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list, 1087599d0c95SMel Gorman struct pglist_data *pgdat, 1088f84f6e2bSMel Gorman struct scan_control *sc, 108902c6de8dSMinchan Kim enum ttu_flags ttu_flags, 10903c710c1aSMichal Hocko struct reclaim_stat *stat, 109102c6de8dSMinchan Kim bool force_reclaim) 10921da177e4SLinus Torvalds { 10931da177e4SLinus Torvalds LIST_HEAD(ret_pages); 1094abe4c3b5SMel Gorman LIST_HEAD(free_pages); 10951da177e4SLinus Torvalds int pgactivate = 0; 10963c710c1aSMichal Hocko unsigned nr_unqueued_dirty = 0; 10973c710c1aSMichal Hocko unsigned nr_dirty = 0; 10983c710c1aSMichal Hocko unsigned nr_congested = 0; 10993c710c1aSMichal Hocko unsigned nr_reclaimed = 0; 11003c710c1aSMichal Hocko unsigned nr_writeback = 0; 11013c710c1aSMichal Hocko unsigned nr_immediate = 0; 11025bccd166SMichal Hocko unsigned nr_ref_keep = 0; 11035bccd166SMichal Hocko unsigned nr_unmap_fail = 0; 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds cond_resched(); 11061da177e4SLinus Torvalds 11071da177e4SLinus Torvalds while (!list_empty(page_list)) { 11081da177e4SLinus Torvalds struct address_space *mapping; 11091da177e4SLinus Torvalds struct page *page; 11101da177e4SLinus Torvalds int may_enter_fs; 111102c6de8dSMinchan Kim enum page_references references = PAGEREF_RECLAIM_CLEAN; 1112e2be15f6SMel Gorman bool dirty, writeback; 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds cond_resched(); 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds page = lru_to_page(page_list); 11171da177e4SLinus Torvalds list_del(&page->lru); 11181da177e4SLinus Torvalds 1119529ae9aaSNick Piggin if (!trylock_page(page)) 11201da177e4SLinus Torvalds goto keep; 11211da177e4SLinus Torvalds 1122309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds sc->nr_scanned++; 112580e43426SChristoph Lameter 112639b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) 1127ad6b6704SMinchan Kim goto activate_locked; 1128894bc310SLee Schermerhorn 1129a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 113080e43426SChristoph Lameter goto keep_locked; 113180e43426SChristoph Lameter 11321da177e4SLinus Torvalds /* Double the slab pressure for mapped and swapcache pages */ 1133802a3a92SShaohua Li if ((page_mapped(page) || PageSwapCache(page)) && 1134802a3a92SShaohua Li !(PageAnon(page) && !PageSwapBacked(page))) 11351da177e4SLinus Torvalds sc->nr_scanned++; 11361da177e4SLinus Torvalds 1137c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 1138c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 1139c661b078SAndy Whitcroft 1140e62e384eSMichal Hocko /* 1141894befecSAndrey Ryabinin * The number of dirty pages determines if a node is marked 1142e2be15f6SMel Gorman * reclaim_congested which affects wait_iff_congested. kswapd 1143e2be15f6SMel Gorman * will stall and start writing pages if the tail of the LRU 1144e2be15f6SMel Gorman * is all dirty unqueued pages. 1145e2be15f6SMel Gorman */ 1146e2be15f6SMel Gorman page_check_dirty_writeback(page, &dirty, &writeback); 1147e2be15f6SMel Gorman if (dirty || writeback) 1148e2be15f6SMel Gorman nr_dirty++; 1149e2be15f6SMel Gorman 1150e2be15f6SMel Gorman if (dirty && !writeback) 1151e2be15f6SMel Gorman nr_unqueued_dirty++; 1152e2be15f6SMel Gorman 1153d04e8acdSMel Gorman /* 1154d04e8acdSMel Gorman * Treat this page as congested if the underlying BDI is or if 1155d04e8acdSMel Gorman * pages are cycling through the LRU so quickly that the 1156d04e8acdSMel Gorman * pages marked for immediate reclaim are making it to the 1157d04e8acdSMel Gorman * end of the LRU a second time. 1158d04e8acdSMel Gorman */ 1159e2be15f6SMel Gorman mapping = page_mapping(page); 11601da58ee2SJamie Liu if (((dirty || writeback) && mapping && 1161703c2708STejun Heo inode_write_congested(mapping->host)) || 1162d04e8acdSMel Gorman (writeback && PageReclaim(page))) 1163e2be15f6SMel Gorman nr_congested++; 1164e2be15f6SMel Gorman 1165e2be15f6SMel Gorman /* 1166283aba9fSMel Gorman * If a page at the tail of the LRU is under writeback, there 1167283aba9fSMel Gorman * are three cases to consider. 1168e62e384eSMichal Hocko * 1169283aba9fSMel Gorman * 1) If reclaim is encountering an excessive number of pages 1170283aba9fSMel Gorman * under writeback and this page is both under writeback and 1171283aba9fSMel Gorman * PageReclaim then it indicates that pages are being queued 1172283aba9fSMel Gorman * for IO but are being recycled through the LRU before the 1173283aba9fSMel Gorman * IO can complete. Waiting on the page itself risks an 1174283aba9fSMel Gorman * indefinite stall if it is impossible to writeback the 1175283aba9fSMel Gorman * page due to IO error or disconnected storage so instead 1176b1a6f21eSMel Gorman * note that the LRU is being scanned too quickly and the 1177b1a6f21eSMel Gorman * caller can stall after page list has been processed. 1178c3b94f44SHugh Dickins * 117997c9341fSTejun Heo * 2) Global or new memcg reclaim encounters a page that is 1180ecf5fc6eSMichal Hocko * not marked for immediate reclaim, or the caller does not 1181ecf5fc6eSMichal Hocko * have __GFP_FS (or __GFP_IO if it's simply going to swap, 1182ecf5fc6eSMichal Hocko * not to fs). In this case mark the page for immediate 118397c9341fSTejun Heo * reclaim and continue scanning. 1184283aba9fSMel Gorman * 1185ecf5fc6eSMichal Hocko * Require may_enter_fs because we would wait on fs, which 1186ecf5fc6eSMichal Hocko * may not have submitted IO yet. And the loop driver might 1187283aba9fSMel Gorman * enter reclaim, and deadlock if it waits on a page for 1188283aba9fSMel Gorman * which it is needed to do the write (loop masks off 1189283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 1190283aba9fSMel Gorman * would probably show more reasons. 1191283aba9fSMel Gorman * 11927fadc820SHugh Dickins * 3) Legacy memcg encounters a page that is already marked 1193283aba9fSMel Gorman * PageReclaim. memcg does not have any dirty pages 1194283aba9fSMel Gorman * throttling so we could easily OOM just because too many 1195283aba9fSMel Gorman * pages are in writeback and there is nothing else to 1196283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 1197c55e8d03SJohannes Weiner * 1198c55e8d03SJohannes Weiner * In cases 1) and 2) we activate the pages to get them out of 1199c55e8d03SJohannes Weiner * the way while we continue scanning for clean pages on the 1200c55e8d03SJohannes Weiner * inactive list and refilling from the active list. The 1201c55e8d03SJohannes Weiner * observation here is that waiting for disk writes is more 1202c55e8d03SJohannes Weiner * expensive than potentially causing reloads down the line. 1203c55e8d03SJohannes Weiner * Since they're marked for immediate reclaim, they won't put 1204c55e8d03SJohannes Weiner * memory pressure on the cache working set any longer than it 1205c55e8d03SJohannes Weiner * takes to write them to disk. 1206e62e384eSMichal Hocko */ 1207283aba9fSMel Gorman if (PageWriteback(page)) { 1208283aba9fSMel Gorman /* Case 1 above */ 1209283aba9fSMel Gorman if (current_is_kswapd() && 1210283aba9fSMel Gorman PageReclaim(page) && 1211599d0c95SMel Gorman test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { 1212b1a6f21eSMel Gorman nr_immediate++; 1213c55e8d03SJohannes Weiner goto activate_locked; 1214283aba9fSMel Gorman 1215283aba9fSMel Gorman /* Case 2 above */ 121697c9341fSTejun Heo } else if (sane_reclaim(sc) || 1217ecf5fc6eSMichal Hocko !PageReclaim(page) || !may_enter_fs) { 1218c3b94f44SHugh Dickins /* 1219c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 1220c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 1221c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 1222c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 1223c3b94f44SHugh Dickins * enough to care. What we do want is for this 1224c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 1225c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 1226c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 1227c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 1228c3b94f44SHugh Dickins */ 1229c3b94f44SHugh Dickins SetPageReclaim(page); 123092df3a72SMel Gorman nr_writeback++; 1231c55e8d03SJohannes Weiner goto activate_locked; 1232283aba9fSMel Gorman 1233283aba9fSMel Gorman /* Case 3 above */ 1234283aba9fSMel Gorman } else { 12357fadc820SHugh Dickins unlock_page(page); 1236c3b94f44SHugh Dickins wait_on_page_writeback(page); 12377fadc820SHugh Dickins /* then go back and try same page again */ 12387fadc820SHugh Dickins list_add_tail(&page->lru, page_list); 12397fadc820SHugh Dickins continue; 1240e62e384eSMichal Hocko } 1241283aba9fSMel Gorman } 12421da177e4SLinus Torvalds 124302c6de8dSMinchan Kim if (!force_reclaim) 12446a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 124502c6de8dSMinchan Kim 1246dfc8d636SJohannes Weiner switch (references) { 1247dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 12481da177e4SLinus Torvalds goto activate_locked; 124964574746SJohannes Weiner case PAGEREF_KEEP: 12505bccd166SMichal Hocko nr_ref_keep++; 125164574746SJohannes Weiner goto keep_locked; 1252dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 1253dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 1254dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 1255dfc8d636SJohannes Weiner } 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds /* 12581da177e4SLinus Torvalds * Anonymous process memory has backing store? 12591da177e4SLinus Torvalds * Try to allocate it some swap space here. 1260802a3a92SShaohua Li * Lazyfree page could be freed directly 12611da177e4SLinus Torvalds */ 1262bd4c82c2SHuang Ying if (PageAnon(page) && PageSwapBacked(page)) { 1263bd4c82c2SHuang Ying if (!PageSwapCache(page)) { 126463eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 126563eb6b93SHugh Dickins goto keep_locked; 1266747552b1SHuang Ying if (PageTransHuge(page)) { 1267b8f593cdSHuang Ying /* cannot split THP, skip it */ 1268747552b1SHuang Ying if (!can_split_huge_page(page, NULL)) 1269b8f593cdSHuang Ying goto activate_locked; 1270747552b1SHuang Ying /* 1271747552b1SHuang Ying * Split pages without a PMD map right 1272747552b1SHuang Ying * away. Chances are some or all of the 1273747552b1SHuang Ying * tail pages can be freed without IO. 1274747552b1SHuang Ying */ 1275747552b1SHuang Ying if (!compound_mapcount(page) && 1276bd4c82c2SHuang Ying split_huge_page_to_list(page, 1277bd4c82c2SHuang Ying page_list)) 1278747552b1SHuang Ying goto activate_locked; 1279747552b1SHuang Ying } 12800f074658SMinchan Kim if (!add_to_swap(page)) { 12810f074658SMinchan Kim if (!PageTransHuge(page)) 12821da177e4SLinus Torvalds goto activate_locked; 1283bd4c82c2SHuang Ying /* Fallback to swap normal pages */ 1284bd4c82c2SHuang Ying if (split_huge_page_to_list(page, 1285bd4c82c2SHuang Ying page_list)) 12860f074658SMinchan Kim goto activate_locked; 1287fe490cc0SHuang Ying #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1288fe490cc0SHuang Ying count_vm_event(THP_SWPOUT_FALLBACK); 1289fe490cc0SHuang Ying #endif 12900f074658SMinchan Kim if (!add_to_swap(page)) 12910f074658SMinchan Kim goto activate_locked; 12920f074658SMinchan Kim } 12930f074658SMinchan Kim 129463eb6b93SHugh Dickins may_enter_fs = 1; 12951da177e4SLinus Torvalds 1296e2be15f6SMel Gorman /* Adding to swap updated mapping */ 12971da177e4SLinus Torvalds mapping = page_mapping(page); 1298bd4c82c2SHuang Ying } 12997751b2daSKirill A. Shutemov } else if (unlikely(PageTransHuge(page))) { 13007751b2daSKirill A. Shutemov /* Split file THP */ 13017751b2daSKirill A. Shutemov if (split_huge_page_to_list(page, page_list)) 13027751b2daSKirill A. Shutemov goto keep_locked; 1303e2be15f6SMel Gorman } 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds /* 13061da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 13071da177e4SLinus Torvalds * processes. Try to unmap it here. 13081da177e4SLinus Torvalds */ 1309802a3a92SShaohua Li if (page_mapped(page)) { 1310bd4c82c2SHuang Ying enum ttu_flags flags = ttu_flags | TTU_BATCH_FLUSH; 1311bd4c82c2SHuang Ying 1312bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page))) 1313bd4c82c2SHuang Ying flags |= TTU_SPLIT_HUGE_PMD; 1314bd4c82c2SHuang Ying if (!try_to_unmap(page, flags)) { 13155bccd166SMichal Hocko nr_unmap_fail++; 13161da177e4SLinus Torvalds goto activate_locked; 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds 13201da177e4SLinus Torvalds if (PageDirty(page)) { 1321ee72886dSMel Gorman /* 13224eda4823SJohannes Weiner * Only kswapd can writeback filesystem pages 13234eda4823SJohannes Weiner * to avoid risk of stack overflow. But avoid 13244eda4823SJohannes Weiner * injecting inefficient single-page IO into 13254eda4823SJohannes Weiner * flusher writeback as much as possible: only 13264eda4823SJohannes Weiner * write pages when we've encountered many 13274eda4823SJohannes Weiner * dirty pages, and when we've already scanned 13284eda4823SJohannes Weiner * the rest of the LRU for clean pages and see 13294eda4823SJohannes Weiner * the same dirty pages again (PageReclaim). 1330ee72886dSMel Gorman */ 1331f84f6e2bSMel Gorman if (page_is_file_cache(page) && 13324eda4823SJohannes Weiner (!current_is_kswapd() || !PageReclaim(page) || 1333599d0c95SMel Gorman !test_bit(PGDAT_DIRTY, &pgdat->flags))) { 133449ea7eb6SMel Gorman /* 133549ea7eb6SMel Gorman * Immediately reclaim when written back. 133649ea7eb6SMel Gorman * Similar in principal to deactivate_page() 133749ea7eb6SMel Gorman * except we already have the page isolated 133849ea7eb6SMel Gorman * and know it's dirty 133949ea7eb6SMel Gorman */ 1340c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_IMMEDIATE); 134149ea7eb6SMel Gorman SetPageReclaim(page); 134249ea7eb6SMel Gorman 1343c55e8d03SJohannes Weiner goto activate_locked; 1344ee72886dSMel Gorman } 1345ee72886dSMel Gorman 1346dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 13471da177e4SLinus Torvalds goto keep_locked; 13484dd4b920SAndrew Morton if (!may_enter_fs) 13491da177e4SLinus Torvalds goto keep_locked; 135052a8363eSChristoph Lameter if (!sc->may_writepage) 13511da177e4SLinus Torvalds goto keep_locked; 13521da177e4SLinus Torvalds 1353d950c947SMel Gorman /* 1354d950c947SMel Gorman * Page is dirty. Flush the TLB if a writable entry 1355d950c947SMel Gorman * potentially exists to avoid CPU writes after IO 1356d950c947SMel Gorman * starts and then write it out here. 1357d950c947SMel Gorman */ 1358d950c947SMel Gorman try_to_unmap_flush_dirty(); 13597d3579e8SKOSAKI Motohiro switch (pageout(page, mapping, sc)) { 13601da177e4SLinus Torvalds case PAGE_KEEP: 13611da177e4SLinus Torvalds goto keep_locked; 13621da177e4SLinus Torvalds case PAGE_ACTIVATE: 13631da177e4SLinus Torvalds goto activate_locked; 13641da177e4SLinus Torvalds case PAGE_SUCCESS: 13657d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 136641ac1999SMel Gorman goto keep; 13677d3579e8SKOSAKI Motohiro if (PageDirty(page)) 13681da177e4SLinus Torvalds goto keep; 13697d3579e8SKOSAKI Motohiro 13701da177e4SLinus Torvalds /* 13711da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 13721da177e4SLinus Torvalds * ahead and try to reclaim the page. 13731da177e4SLinus Torvalds */ 1374529ae9aaSNick Piggin if (!trylock_page(page)) 13751da177e4SLinus Torvalds goto keep; 13761da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 13771da177e4SLinus Torvalds goto keep_locked; 13781da177e4SLinus Torvalds mapping = page_mapping(page); 13791da177e4SLinus Torvalds case PAGE_CLEAN: 13801da177e4SLinus Torvalds ; /* try to free the page below */ 13811da177e4SLinus Torvalds } 13821da177e4SLinus Torvalds } 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds /* 13851da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 13861da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 13871da177e4SLinus Torvalds * the page as well. 13881da177e4SLinus Torvalds * 13891da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 13901da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 13911da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 13921da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 13931da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 13941da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 13951da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 13961da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 13971da177e4SLinus Torvalds * 13981da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 13991da177e4SLinus Torvalds * the pages which were not successfully invalidated in 14001da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 14011da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 14021da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 14031da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 14041da177e4SLinus Torvalds */ 1405266cf658SDavid Howells if (page_has_private(page)) { 14061da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 14071da177e4SLinus Torvalds goto activate_locked; 1408e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 1409e286781dSNick Piggin unlock_page(page); 1410e286781dSNick Piggin if (put_page_testzero(page)) 14111da177e4SLinus Torvalds goto free_it; 1412e286781dSNick Piggin else { 1413e286781dSNick Piggin /* 1414e286781dSNick Piggin * rare race with speculative reference. 1415e286781dSNick Piggin * the speculative reference will free 1416e286781dSNick Piggin * this page shortly, so we may 1417e286781dSNick Piggin * increment nr_reclaimed here (and 1418e286781dSNick Piggin * leave it off the LRU). 1419e286781dSNick Piggin */ 1420e286781dSNick Piggin nr_reclaimed++; 1421e286781dSNick Piggin continue; 1422e286781dSNick Piggin } 1423e286781dSNick Piggin } 14241da177e4SLinus Torvalds } 14251da177e4SLinus Torvalds 1426802a3a92SShaohua Li if (PageAnon(page) && !PageSwapBacked(page)) { 1427802a3a92SShaohua Li /* follow __remove_mapping for reference */ 1428802a3a92SShaohua Li if (!page_ref_freeze(page, 1)) 142949d2e9ccSChristoph Lameter goto keep_locked; 1430802a3a92SShaohua Li if (PageDirty(page)) { 1431802a3a92SShaohua Li page_ref_unfreeze(page, 1); 1432802a3a92SShaohua Li goto keep_locked; 1433802a3a92SShaohua Li } 14341da177e4SLinus Torvalds 1435802a3a92SShaohua Li count_vm_event(PGLAZYFREED); 14362262185cSRoman Gushchin count_memcg_page_event(page, PGLAZYFREED); 1437802a3a92SShaohua Li } else if (!mapping || !__remove_mapping(mapping, page, true)) 1438802a3a92SShaohua Li goto keep_locked; 1439a978d6f5SNick Piggin /* 1440a978d6f5SNick Piggin * At this point, we have no other references and there is 1441a978d6f5SNick Piggin * no way to pick any more up (removed from LRU, removed 1442a978d6f5SNick Piggin * from pagecache). Can use non-atomic bitops now (and 1443a978d6f5SNick Piggin * we obviously don't have to worry about waking up a process 1444a978d6f5SNick Piggin * waiting on the page lock, because there are no references. 1445a978d6f5SNick Piggin */ 144648c935adSKirill A. Shutemov __ClearPageLocked(page); 1447e286781dSNick Piggin free_it: 144805ff5137SAndrew Morton nr_reclaimed++; 1449abe4c3b5SMel Gorman 1450abe4c3b5SMel Gorman /* 1451abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1452abe4c3b5SMel Gorman * appear not as the counts should be low 1453abe4c3b5SMel Gorman */ 1454bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page))) { 1455bd4c82c2SHuang Ying mem_cgroup_uncharge(page); 1456bd4c82c2SHuang Ying (*get_compound_page_dtor(page))(page); 1457bd4c82c2SHuang Ying } else 1458abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 14591da177e4SLinus Torvalds continue; 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds activate_locked: 146268a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 1463ad6b6704SMinchan Kim if (PageSwapCache(page) && (mem_cgroup_swap_full(page) || 1464ad6b6704SMinchan Kim PageMlocked(page))) 1465a2c43eedSHugh Dickins try_to_free_swap(page); 1466309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 1467ad6b6704SMinchan Kim if (!PageMlocked(page)) { 14681da177e4SLinus Torvalds SetPageActive(page); 14691da177e4SLinus Torvalds pgactivate++; 14702262185cSRoman Gushchin count_memcg_page_event(page, PGACTIVATE); 1471ad6b6704SMinchan Kim } 14721da177e4SLinus Torvalds keep_locked: 14731da177e4SLinus Torvalds unlock_page(page); 14741da177e4SLinus Torvalds keep: 14751da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 1476309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); 14771da177e4SLinus Torvalds } 1478abe4c3b5SMel Gorman 1479747db954SJohannes Weiner mem_cgroup_uncharge_list(&free_pages); 148072b252aeSMel Gorman try_to_unmap_flush(); 14812d4894b5SMel Gorman free_unref_page_list(&free_pages); 1482abe4c3b5SMel Gorman 14831da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1484f8891e5eSChristoph Lameter count_vm_events(PGACTIVATE, pgactivate); 14850a31bc97SJohannes Weiner 14863c710c1aSMichal Hocko if (stat) { 14873c710c1aSMichal Hocko stat->nr_dirty = nr_dirty; 14883c710c1aSMichal Hocko stat->nr_congested = nr_congested; 14893c710c1aSMichal Hocko stat->nr_unqueued_dirty = nr_unqueued_dirty; 14903c710c1aSMichal Hocko stat->nr_writeback = nr_writeback; 14913c710c1aSMichal Hocko stat->nr_immediate = nr_immediate; 14925bccd166SMichal Hocko stat->nr_activate = pgactivate; 14935bccd166SMichal Hocko stat->nr_ref_keep = nr_ref_keep; 14945bccd166SMichal Hocko stat->nr_unmap_fail = nr_unmap_fail; 14953c710c1aSMichal Hocko } 149605ff5137SAndrew Morton return nr_reclaimed; 14971da177e4SLinus Torvalds } 14981da177e4SLinus Torvalds 149902c6de8dSMinchan Kim unsigned long reclaim_clean_pages_from_list(struct zone *zone, 150002c6de8dSMinchan Kim struct list_head *page_list) 150102c6de8dSMinchan Kim { 150202c6de8dSMinchan Kim struct scan_control sc = { 150302c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 150402c6de8dSMinchan Kim .priority = DEF_PRIORITY, 150502c6de8dSMinchan Kim .may_unmap = 1, 150602c6de8dSMinchan Kim }; 15073c710c1aSMichal Hocko unsigned long ret; 150802c6de8dSMinchan Kim struct page *page, *next; 150902c6de8dSMinchan Kim LIST_HEAD(clean_pages); 151002c6de8dSMinchan Kim 151102c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 1512117aad1eSRafael Aquini if (page_is_file_cache(page) && !PageDirty(page) && 1513b1123ea6SMinchan Kim !__PageMovable(page)) { 151402c6de8dSMinchan Kim ClearPageActive(page); 151502c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 151602c6de8dSMinchan Kim } 151702c6de8dSMinchan Kim } 151802c6de8dSMinchan Kim 1519599d0c95SMel Gorman ret = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc, 1520a128ca71SShaohua Li TTU_IGNORE_ACCESS, NULL, true); 152102c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 1522599d0c95SMel Gorman mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, -ret); 152302c6de8dSMinchan Kim return ret; 152402c6de8dSMinchan Kim } 152502c6de8dSMinchan Kim 15265ad333ebSAndy Whitcroft /* 15275ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 15285ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 15295ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 15305ad333ebSAndy Whitcroft * 15315ad333ebSAndy Whitcroft * page: page to consider 15325ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 15335ad333ebSAndy Whitcroft * 15345ad333ebSAndy Whitcroft * returns 0 on success, -ve errno on failure. 15355ad333ebSAndy Whitcroft */ 1536f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode) 15375ad333ebSAndy Whitcroft { 15385ad333ebSAndy Whitcroft int ret = -EINVAL; 15395ad333ebSAndy Whitcroft 15405ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 15415ad333ebSAndy Whitcroft if (!PageLRU(page)) 15425ad333ebSAndy Whitcroft return ret; 15435ad333ebSAndy Whitcroft 1544e46a2879SMinchan Kim /* Compaction should not handle unevictable pages but CMA can do so */ 1545e46a2879SMinchan Kim if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) 1546894bc310SLee Schermerhorn return ret; 1547894bc310SLee Schermerhorn 15485ad333ebSAndy Whitcroft ret = -EBUSY; 154908e552c6SKAMEZAWA Hiroyuki 1550c8244935SMel Gorman /* 1551c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 1552c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 1553c8244935SMel Gorman * blocking - clean pages for the most part. 1554c8244935SMel Gorman * 1555c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 1556c8244935SMel Gorman * that it is possible to migrate without blocking 1557c8244935SMel Gorman */ 15581276ad68SJohannes Weiner if (mode & ISOLATE_ASYNC_MIGRATE) { 1559c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 1560c8244935SMel Gorman if (PageWriteback(page)) 156139deaf85SMinchan Kim return ret; 156239deaf85SMinchan Kim 1563c8244935SMel Gorman if (PageDirty(page)) { 1564c8244935SMel Gorman struct address_space *mapping; 156569d763fcSMel Gorman bool migrate_dirty; 1566c8244935SMel Gorman 1567c8244935SMel Gorman /* 1568c8244935SMel Gorman * Only pages without mappings or that have a 1569c8244935SMel Gorman * ->migratepage callback are possible to migrate 157069d763fcSMel Gorman * without blocking. However, we can be racing with 157169d763fcSMel Gorman * truncation so it's necessary to lock the page 157269d763fcSMel Gorman * to stabilise the mapping as truncation holds 157369d763fcSMel Gorman * the page lock until after the page is removed 157469d763fcSMel Gorman * from the page cache. 1575c8244935SMel Gorman */ 157669d763fcSMel Gorman if (!trylock_page(page)) 157769d763fcSMel Gorman return ret; 157869d763fcSMel Gorman 1579c8244935SMel Gorman mapping = page_mapping(page); 1580145e1a71SHugh Dickins migrate_dirty = !mapping || mapping->a_ops->migratepage; 158169d763fcSMel Gorman unlock_page(page); 158269d763fcSMel Gorman if (!migrate_dirty) 1583c8244935SMel Gorman return ret; 1584c8244935SMel Gorman } 1585c8244935SMel Gorman } 1586c8244935SMel Gorman 1587f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 1588f80c0673SMinchan Kim return ret; 1589f80c0673SMinchan Kim 15905ad333ebSAndy Whitcroft if (likely(get_page_unless_zero(page))) { 15915ad333ebSAndy Whitcroft /* 15925ad333ebSAndy Whitcroft * Be careful not to clear PageLRU until after we're 15935ad333ebSAndy Whitcroft * sure the page is not being freed elsewhere -- the 15945ad333ebSAndy Whitcroft * page release code relies on it. 15955ad333ebSAndy Whitcroft */ 15965ad333ebSAndy Whitcroft ClearPageLRU(page); 15975ad333ebSAndy Whitcroft ret = 0; 15985ad333ebSAndy Whitcroft } 15995ad333ebSAndy Whitcroft 16005ad333ebSAndy Whitcroft return ret; 16015ad333ebSAndy Whitcroft } 16025ad333ebSAndy Whitcroft 16037ee36a14SMel Gorman 16047ee36a14SMel Gorman /* 16057ee36a14SMel Gorman * Update LRU sizes after isolating pages. The LRU size updates must 16067ee36a14SMel Gorman * be complete before mem_cgroup_update_lru_size due to a santity check. 16077ee36a14SMel Gorman */ 16087ee36a14SMel Gorman static __always_inline void update_lru_sizes(struct lruvec *lruvec, 1609b4536f0cSMichal Hocko enum lru_list lru, unsigned long *nr_zone_taken) 16107ee36a14SMel Gorman { 16117ee36a14SMel Gorman int zid; 16127ee36a14SMel Gorman 16137ee36a14SMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 16147ee36a14SMel Gorman if (!nr_zone_taken[zid]) 16157ee36a14SMel Gorman continue; 16167ee36a14SMel Gorman 16177ee36a14SMel Gorman __update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 1618b4536f0cSMichal Hocko #ifdef CONFIG_MEMCG 1619b4536f0cSMichal Hocko mem_cgroup_update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 1620b4536f0cSMichal Hocko #endif 16217ee36a14SMel Gorman } 16227ee36a14SMel Gorman 16237ee36a14SMel Gorman } 16247ee36a14SMel Gorman 162549d2e9ccSChristoph Lameter /* 1626a52633d8SMel Gorman * zone_lru_lock is heavily contended. Some of the functions that 16271da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 16281da177e4SLinus Torvalds * and working on them outside the LRU lock. 16291da177e4SLinus Torvalds * 16301da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 16311da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 16321da177e4SLinus Torvalds * 16331da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 16341da177e4SLinus Torvalds * 1635791b48b6SMinchan Kim * @nr_to_scan: The number of eligible pages to look through on the list. 16365dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 16371da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1638f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1639fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 16405ad333ebSAndy Whitcroft * @mode: One of the LRU isolation modes 16413cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 16421da177e4SLinus Torvalds * 16431da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 16441da177e4SLinus Torvalds */ 164569e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 16465dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1647fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 16483cb99451SKonstantin Khlebnikov isolate_mode_t mode, enum lru_list lru) 16491da177e4SLinus Torvalds { 165075b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 165169e05944SAndrew Morton unsigned long nr_taken = 0; 1652599d0c95SMel Gorman unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; 16537cc30fcfSMel Gorman unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; 16543db65812SJohannes Weiner unsigned long skipped = 0; 1655791b48b6SMinchan Kim unsigned long scan, total_scan, nr_pages; 1656b2e18757SMel Gorman LIST_HEAD(pages_skipped); 16571da177e4SLinus Torvalds 1658791b48b6SMinchan Kim scan = 0; 1659791b48b6SMinchan Kim for (total_scan = 0; 1660791b48b6SMinchan Kim scan < nr_to_scan && nr_taken < nr_to_scan && !list_empty(src); 1661791b48b6SMinchan Kim total_scan++) { 16625ad333ebSAndy Whitcroft struct page *page; 16635ad333ebSAndy Whitcroft 16641da177e4SLinus Torvalds page = lru_to_page(src); 16651da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 16661da177e4SLinus Torvalds 1667309381feSSasha Levin VM_BUG_ON_PAGE(!PageLRU(page), page); 16688d438f96SNick Piggin 1669b2e18757SMel Gorman if (page_zonenum(page) > sc->reclaim_idx) { 1670b2e18757SMel Gorman list_move(&page->lru, &pages_skipped); 16717cc30fcfSMel Gorman nr_skipped[page_zonenum(page)]++; 1672b2e18757SMel Gorman continue; 1673b2e18757SMel Gorman } 1674b2e18757SMel Gorman 1675791b48b6SMinchan Kim /* 1676791b48b6SMinchan Kim * Do not count skipped pages because that makes the function 1677791b48b6SMinchan Kim * return with no isolated pages if the LRU mostly contains 1678791b48b6SMinchan Kim * ineligible pages. This causes the VM to not reclaim any 1679791b48b6SMinchan Kim * pages, triggering a premature OOM. 1680791b48b6SMinchan Kim */ 1681791b48b6SMinchan Kim scan++; 1682f3fd4a61SKonstantin Khlebnikov switch (__isolate_lru_page(page, mode)) { 16835ad333ebSAndy Whitcroft case 0: 1684599d0c95SMel Gorman nr_pages = hpage_nr_pages(page); 1685599d0c95SMel Gorman nr_taken += nr_pages; 1686599d0c95SMel Gorman nr_zone_taken[page_zonenum(page)] += nr_pages; 16875ad333ebSAndy Whitcroft list_move(&page->lru, dst); 16885ad333ebSAndy Whitcroft break; 16897c8ee9a8SNick Piggin 16905ad333ebSAndy Whitcroft case -EBUSY: 16915ad333ebSAndy Whitcroft /* else it is being freed elsewhere */ 16925ad333ebSAndy Whitcroft list_move(&page->lru, src); 16935ad333ebSAndy Whitcroft continue; 16945ad333ebSAndy Whitcroft 16955ad333ebSAndy Whitcroft default: 16965ad333ebSAndy Whitcroft BUG(); 16975ad333ebSAndy Whitcroft } 16985ad333ebSAndy Whitcroft } 16991da177e4SLinus Torvalds 1700b2e18757SMel Gorman /* 1701b2e18757SMel Gorman * Splice any skipped pages to the start of the LRU list. Note that 1702b2e18757SMel Gorman * this disrupts the LRU order when reclaiming for lower zones but 1703b2e18757SMel Gorman * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX 1704b2e18757SMel Gorman * scanning would soon rescan the same pages to skip and put the 1705b2e18757SMel Gorman * system at risk of premature OOM. 1706b2e18757SMel Gorman */ 17077cc30fcfSMel Gorman if (!list_empty(&pages_skipped)) { 17087cc30fcfSMel Gorman int zid; 17097cc30fcfSMel Gorman 17103db65812SJohannes Weiner list_splice(&pages_skipped, src); 17117cc30fcfSMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 17127cc30fcfSMel Gorman if (!nr_skipped[zid]) 17137cc30fcfSMel Gorman continue; 17147cc30fcfSMel Gorman 17157cc30fcfSMel Gorman __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); 17161265e3a6SMichal Hocko skipped += nr_skipped[zid]; 17177cc30fcfSMel Gorman } 17187cc30fcfSMel Gorman } 1719791b48b6SMinchan Kim *nr_scanned = total_scan; 17201265e3a6SMichal Hocko trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, 1721791b48b6SMinchan Kim total_scan, skipped, nr_taken, mode, lru); 1722b4536f0cSMichal Hocko update_lru_sizes(lruvec, lru, nr_zone_taken); 17231da177e4SLinus Torvalds return nr_taken; 17241da177e4SLinus Torvalds } 17251da177e4SLinus Torvalds 172662695a84SNick Piggin /** 172762695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 172862695a84SNick Piggin * @page: page to isolate from its LRU list 172962695a84SNick Piggin * 173062695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 173162695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 173262695a84SNick Piggin * 173362695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 173462695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 173562695a84SNick Piggin * 173662695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1737894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1738894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1739894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 174062695a84SNick Piggin * 174162695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 174262695a84SNick Piggin * found will be decremented. 174362695a84SNick Piggin * 174462695a84SNick Piggin * Restrictions: 1745a5d09bedSMike Rapoport * 174662695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 174762695a84SNick Piggin * fundamentnal difference from isolate_lru_pages (which is called 174862695a84SNick Piggin * without a stable reference). 174962695a84SNick Piggin * (2) the lru_lock must not be held. 175062695a84SNick Piggin * (3) interrupts must be enabled. 175162695a84SNick Piggin */ 175262695a84SNick Piggin int isolate_lru_page(struct page *page) 175362695a84SNick Piggin { 175462695a84SNick Piggin int ret = -EBUSY; 175562695a84SNick Piggin 1756309381feSSasha Levin VM_BUG_ON_PAGE(!page_count(page), page); 1757cf2a82eeSKirill A. Shutemov WARN_RATELIMIT(PageTail(page), "trying to isolate tail page"); 17580c917313SKonstantin Khlebnikov 175962695a84SNick Piggin if (PageLRU(page)) { 176062695a84SNick Piggin struct zone *zone = page_zone(page); 1761fa9add64SHugh Dickins struct lruvec *lruvec; 176262695a84SNick Piggin 1763a52633d8SMel Gorman spin_lock_irq(zone_lru_lock(zone)); 1764599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat); 17650c917313SKonstantin Khlebnikov if (PageLRU(page)) { 1766894bc310SLee Schermerhorn int lru = page_lru(page); 17670c917313SKonstantin Khlebnikov get_page(page); 176862695a84SNick Piggin ClearPageLRU(page); 1769fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 1770fa9add64SHugh Dickins ret = 0; 177162695a84SNick Piggin } 1772a52633d8SMel Gorman spin_unlock_irq(zone_lru_lock(zone)); 177362695a84SNick Piggin } 177462695a84SNick Piggin return ret; 177562695a84SNick Piggin } 177662695a84SNick Piggin 17775ad333ebSAndy Whitcroft /* 1778d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 1779d37dd5dcSFengguang Wu * then get resheduled. When there are massive number of tasks doing page 1780d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 1781d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 1782d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 178335cd7815SRik van Riel */ 1784599d0c95SMel Gorman static int too_many_isolated(struct pglist_data *pgdat, int file, 178535cd7815SRik van Riel struct scan_control *sc) 178635cd7815SRik van Riel { 178735cd7815SRik van Riel unsigned long inactive, isolated; 178835cd7815SRik van Riel 178935cd7815SRik van Riel if (current_is_kswapd()) 179035cd7815SRik van Riel return 0; 179135cd7815SRik van Riel 179297c9341fSTejun Heo if (!sane_reclaim(sc)) 179335cd7815SRik van Riel return 0; 179435cd7815SRik van Riel 179535cd7815SRik van Riel if (file) { 1796599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_FILE); 1797599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_FILE); 179835cd7815SRik van Riel } else { 1799599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_ANON); 1800599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_ANON); 180135cd7815SRik van Riel } 180235cd7815SRik van Riel 18033cf23841SFengguang Wu /* 18043cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 18053cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 18063cf23841SFengguang Wu * deadlock. 18073cf23841SFengguang Wu */ 1808d0164adcSMel Gorman if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) 18093cf23841SFengguang Wu inactive >>= 3; 18103cf23841SFengguang Wu 181135cd7815SRik van Riel return isolated > inactive; 181235cd7815SRik van Riel } 181335cd7815SRik van Riel 181466635629SMel Gorman static noinline_for_stack void 181575b00af7SHugh Dickins putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list) 181666635629SMel Gorman { 181727ac81d8SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 1818599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 18193f79768fSHugh Dickins LIST_HEAD(pages_to_free); 182066635629SMel Gorman 182166635629SMel Gorman /* 182266635629SMel Gorman * Put back any unfreeable pages. 182366635629SMel Gorman */ 182466635629SMel Gorman while (!list_empty(page_list)) { 18253f79768fSHugh Dickins struct page *page = lru_to_page(page_list); 182666635629SMel Gorman int lru; 18273f79768fSHugh Dickins 1828309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 182966635629SMel Gorman list_del(&page->lru); 183039b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 1831599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 183266635629SMel Gorman putback_lru_page(page); 1833599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 183466635629SMel Gorman continue; 183566635629SMel Gorman } 1836fa9add64SHugh Dickins 1837599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 1838fa9add64SHugh Dickins 18397a608572SLinus Torvalds SetPageLRU(page); 184066635629SMel Gorman lru = page_lru(page); 1841fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 1842fa9add64SHugh Dickins 184366635629SMel Gorman if (is_active_lru(lru)) { 184466635629SMel Gorman int file = is_file_lru(lru); 18459992af10SRik van Riel int numpages = hpage_nr_pages(page); 18469992af10SRik van Riel reclaim_stat->recent_rotated[file] += numpages; 184766635629SMel Gorman } 18482bcf8879SHugh Dickins if (put_page_testzero(page)) { 18492bcf8879SHugh Dickins __ClearPageLRU(page); 18502bcf8879SHugh Dickins __ClearPageActive(page); 1851fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 18522bcf8879SHugh Dickins 18532bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 1854599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1855747db954SJohannes Weiner mem_cgroup_uncharge(page); 18562bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 1857599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 18582bcf8879SHugh Dickins } else 18592bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 186066635629SMel Gorman } 186166635629SMel Gorman } 186266635629SMel Gorman 18633f79768fSHugh Dickins /* 18643f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 18653f79768fSHugh Dickins */ 18663f79768fSHugh Dickins list_splice(&pages_to_free, page_list); 186766635629SMel Gorman } 186866635629SMel Gorman 186966635629SMel Gorman /* 1870399ba0b9SNeilBrown * If a kernel thread (such as nfsd for loop-back mounts) services 1871399ba0b9SNeilBrown * a backing device by writing to the page cache it sets PF_LESS_THROTTLE. 1872399ba0b9SNeilBrown * In that case we should only throttle if the backing device it is 1873399ba0b9SNeilBrown * writing to is congested. In other cases it is safe to throttle. 1874399ba0b9SNeilBrown */ 1875399ba0b9SNeilBrown static int current_may_throttle(void) 1876399ba0b9SNeilBrown { 1877399ba0b9SNeilBrown return !(current->flags & PF_LESS_THROTTLE) || 1878399ba0b9SNeilBrown current->backing_dev_info == NULL || 1879399ba0b9SNeilBrown bdi_write_congested(current->backing_dev_info); 1880399ba0b9SNeilBrown } 1881399ba0b9SNeilBrown 1882399ba0b9SNeilBrown /* 1883b2e18757SMel Gorman * shrink_inactive_list() is a helper for shrink_node(). It returns the number 18841742f19fSAndrew Morton * of reclaimed pages 18851da177e4SLinus Torvalds */ 188666635629SMel Gorman static noinline_for_stack unsigned long 18871a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 18889e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 18891da177e4SLinus Torvalds { 18901da177e4SLinus Torvalds LIST_HEAD(page_list); 1891e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 189205ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 1893e247dbceSKOSAKI Motohiro unsigned long nr_taken; 18943c710c1aSMichal Hocko struct reclaim_stat stat = {}; 1895f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 18963cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 1897599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 18981a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 1899db73ee0dSMichal Hocko bool stalled = false; 190078dc583dSKOSAKI Motohiro 1901599d0c95SMel Gorman while (unlikely(too_many_isolated(pgdat, file, sc))) { 1902db73ee0dSMichal Hocko if (stalled) 1903db73ee0dSMichal Hocko return 0; 1904db73ee0dSMichal Hocko 1905db73ee0dSMichal Hocko /* wait a bit for the reclaimer. */ 1906db73ee0dSMichal Hocko msleep(100); 1907db73ee0dSMichal Hocko stalled = true; 190835cd7815SRik van Riel 190935cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 191035cd7815SRik van Riel if (fatal_signal_pending(current)) 191135cd7815SRik van Riel return SWAP_CLUSTER_MAX; 191235cd7815SRik van Riel } 191335cd7815SRik van Riel 19141da177e4SLinus Torvalds lru_add_drain(); 1915f80c0673SMinchan Kim 1916f80c0673SMinchan Kim if (!sc->may_unmap) 191761317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1918f80c0673SMinchan Kim 1919599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 19201da177e4SLinus Torvalds 19215dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 19225dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 192395d918fcSKonstantin Khlebnikov 1924599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 19259d5e6a9fSHugh Dickins reclaim_stat->recent_scanned[file] += nr_taken; 192695d918fcSKonstantin Khlebnikov 19272262185cSRoman Gushchin if (current_is_kswapd()) { 19282262185cSRoman Gushchin if (global_reclaim(sc)) 1929599d0c95SMel Gorman __count_vm_events(PGSCAN_KSWAPD, nr_scanned); 19302262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGSCAN_KSWAPD, 19312262185cSRoman Gushchin nr_scanned); 19322262185cSRoman Gushchin } else { 19332262185cSRoman Gushchin if (global_reclaim(sc)) 1934599d0c95SMel Gorman __count_vm_events(PGSCAN_DIRECT, nr_scanned); 19352262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGSCAN_DIRECT, 19362262185cSRoman Gushchin nr_scanned); 1937b35ea17bSKOSAKI Motohiro } 1938599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1939d563c050SHillf Danton 1940d563c050SHillf Danton if (nr_taken == 0) 194166635629SMel Gorman return 0; 1942b35ea17bSKOSAKI Motohiro 1943a128ca71SShaohua Li nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, 0, 19443c710c1aSMichal Hocko &stat, false); 1945c661b078SAndy Whitcroft 1946599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 19473f79768fSHugh Dickins 19482262185cSRoman Gushchin if (current_is_kswapd()) { 19492262185cSRoman Gushchin if (global_reclaim(sc)) 1950599d0c95SMel Gorman __count_vm_events(PGSTEAL_KSWAPD, nr_reclaimed); 19512262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_KSWAPD, 19522262185cSRoman Gushchin nr_reclaimed); 19532262185cSRoman Gushchin } else { 19542262185cSRoman Gushchin if (global_reclaim(sc)) 1955599d0c95SMel Gorman __count_vm_events(PGSTEAL_DIRECT, nr_reclaimed); 19562262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_DIRECT, 19572262185cSRoman Gushchin nr_reclaimed); 1958904249aaSYing Han } 1959a74609faSNick Piggin 196027ac81d8SKonstantin Khlebnikov putback_inactive_pages(lruvec, &page_list); 19613f79768fSHugh Dickins 1962599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 19633f79768fSHugh Dickins 1964599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 19653f79768fSHugh Dickins 1966747db954SJohannes Weiner mem_cgroup_uncharge_list(&page_list); 19672d4894b5SMel Gorman free_unref_page_list(&page_list); 1968e11da5b4SMel Gorman 196992df3a72SMel Gorman /* 19701c610d5fSAndrey Ryabinin * If dirty pages are scanned that are not queued for IO, it 19711c610d5fSAndrey Ryabinin * implies that flushers are not doing their job. This can 19721c610d5fSAndrey Ryabinin * happen when memory pressure pushes dirty pages to the end of 19731c610d5fSAndrey Ryabinin * the LRU before the dirty limits are breached and the dirty 19741c610d5fSAndrey Ryabinin * data has expired. It can also happen when the proportion of 19751c610d5fSAndrey Ryabinin * dirty pages grows not through writes but through memory 19761c610d5fSAndrey Ryabinin * pressure reclaiming all the clean cache. And in some cases, 19771c610d5fSAndrey Ryabinin * the flushers simply cannot keep up with the allocation 19781c610d5fSAndrey Ryabinin * rate. Nudge the flusher threads in case they are asleep. 19791c610d5fSAndrey Ryabinin */ 19801c610d5fSAndrey Ryabinin if (stat.nr_unqueued_dirty == nr_taken) 19811c610d5fSAndrey Ryabinin wakeup_flusher_threads(WB_REASON_VMSCAN); 19821c610d5fSAndrey Ryabinin 1983d108c772SAndrey Ryabinin sc->nr.dirty += stat.nr_dirty; 1984d108c772SAndrey Ryabinin sc->nr.congested += stat.nr_congested; 1985d108c772SAndrey Ryabinin sc->nr.unqueued_dirty += stat.nr_unqueued_dirty; 1986d108c772SAndrey Ryabinin sc->nr.writeback += stat.nr_writeback; 1987d108c772SAndrey Ryabinin sc->nr.immediate += stat.nr_immediate; 1988d108c772SAndrey Ryabinin sc->nr.taken += nr_taken; 1989d108c772SAndrey Ryabinin if (file) 1990d108c772SAndrey Ryabinin sc->nr.file_taken += nr_taken; 19918e950282SMel Gorman 1992599d0c95SMel Gorman trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, 1993d51d1e64SSteven Rostedt nr_scanned, nr_reclaimed, &stat, sc->priority, file); 199405ff5137SAndrew Morton return nr_reclaimed; 19951da177e4SLinus Torvalds } 19961da177e4SLinus Torvalds 19973bb1a852SMartin Bligh /* 19981cfb419bSKAMEZAWA Hiroyuki * This moves pages from the active list to the inactive list. 19991cfb419bSKAMEZAWA Hiroyuki * 20001cfb419bSKAMEZAWA Hiroyuki * We move them the other way if the page is referenced by one or more 20011cfb419bSKAMEZAWA Hiroyuki * processes, from rmap. 20021cfb419bSKAMEZAWA Hiroyuki * 20031cfb419bSKAMEZAWA Hiroyuki * If the pages are mostly unmapped, the processing is fast and it is 2004a52633d8SMel Gorman * appropriate to hold zone_lru_lock across the whole operation. But if 20051cfb419bSKAMEZAWA Hiroyuki * the pages are mapped, the processing is slow (page_referenced()) so we 2006a52633d8SMel Gorman * should drop zone_lru_lock around each page. It's impossible to balance 20071cfb419bSKAMEZAWA Hiroyuki * this, so instead we remove the pages from the LRU while processing them. 20081cfb419bSKAMEZAWA Hiroyuki * It is safe to rely on PG_active against the non-LRU pages in here because 20091cfb419bSKAMEZAWA Hiroyuki * nobody will play with that bit on a non-LRU page. 20101cfb419bSKAMEZAWA Hiroyuki * 20110139aa7bSJoonsoo Kim * The downside is that we have to touch page->_refcount against each page. 20121cfb419bSKAMEZAWA Hiroyuki * But we had to alter page->flags anyway. 20139d998b4fSMichal Hocko * 20149d998b4fSMichal Hocko * Returns the number of pages moved to the given lru. 20151cfb419bSKAMEZAWA Hiroyuki */ 20161cfb419bSKAMEZAWA Hiroyuki 20179d998b4fSMichal Hocko static unsigned move_active_pages_to_lru(struct lruvec *lruvec, 20183eb4140fSWu Fengguang struct list_head *list, 20192bcf8879SHugh Dickins struct list_head *pages_to_free, 20203eb4140fSWu Fengguang enum lru_list lru) 20213eb4140fSWu Fengguang { 2022599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 20233eb4140fSWu Fengguang struct page *page; 2024fa9add64SHugh Dickins int nr_pages; 20259d998b4fSMichal Hocko int nr_moved = 0; 20263eb4140fSWu Fengguang 20273eb4140fSWu Fengguang while (!list_empty(list)) { 20283eb4140fSWu Fengguang page = lru_to_page(list); 2029599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 20303eb4140fSWu Fengguang 2031309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 20323eb4140fSWu Fengguang SetPageLRU(page); 20333eb4140fSWu Fengguang 2034fa9add64SHugh Dickins nr_pages = hpage_nr_pages(page); 2035599d0c95SMel Gorman update_lru_size(lruvec, lru, page_zonenum(page), nr_pages); 2036925b7673SJohannes Weiner list_move(&page->lru, &lruvec->lists[lru]); 20373eb4140fSWu Fengguang 20382bcf8879SHugh Dickins if (put_page_testzero(page)) { 20392bcf8879SHugh Dickins __ClearPageLRU(page); 20402bcf8879SHugh Dickins __ClearPageActive(page); 2041fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 20422bcf8879SHugh Dickins 20432bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 2044599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 2045747db954SJohannes Weiner mem_cgroup_uncharge(page); 20462bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 2047599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 20482bcf8879SHugh Dickins } else 20492bcf8879SHugh Dickins list_add(&page->lru, pages_to_free); 20509d998b4fSMichal Hocko } else { 20519d998b4fSMichal Hocko nr_moved += nr_pages; 20523eb4140fSWu Fengguang } 20533eb4140fSWu Fengguang } 20549d5e6a9fSHugh Dickins 20552262185cSRoman Gushchin if (!is_active_lru(lru)) { 2056f0958906SMichal Hocko __count_vm_events(PGDEACTIVATE, nr_moved); 20572262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, 20582262185cSRoman Gushchin nr_moved); 20592262185cSRoman Gushchin } 20609d998b4fSMichal Hocko 20619d998b4fSMichal Hocko return nr_moved; 20623eb4140fSWu Fengguang } 20631cfb419bSKAMEZAWA Hiroyuki 2064f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 20651a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 2066f16015fbSJohannes Weiner struct scan_control *sc, 20679e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 20681cfb419bSKAMEZAWA Hiroyuki { 206944c241f1SKOSAKI Motohiro unsigned long nr_taken; 2070f626012dSHugh Dickins unsigned long nr_scanned; 20716fe6b7e3SWu Fengguang unsigned long vm_flags; 20721cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 20738cab4754SWu Fengguang LIST_HEAD(l_active); 2074b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 20751cfb419bSKAMEZAWA Hiroyuki struct page *page; 20761a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 20779d998b4fSMichal Hocko unsigned nr_deactivate, nr_activate; 20789d998b4fSMichal Hocko unsigned nr_rotated = 0; 2079f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 20803cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 2081599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 20821cfb419bSKAMEZAWA Hiroyuki 20831da177e4SLinus Torvalds lru_add_drain(); 2084f80c0673SMinchan Kim 2085f80c0673SMinchan Kim if (!sc->may_unmap) 208661317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 2087f80c0673SMinchan Kim 2088599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 2089925b7673SJohannes Weiner 20905dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 20915dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 209289b5fae5SJohannes Weiner 2093599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 2094b7c46d15SJohannes Weiner reclaim_stat->recent_scanned[file] += nr_taken; 20951cfb419bSKAMEZAWA Hiroyuki 2096599d0c95SMel Gorman __count_vm_events(PGREFILL, nr_scanned); 20972262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned); 20989d5e6a9fSHugh Dickins 2099599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 21001da177e4SLinus Torvalds 21011da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 21021da177e4SLinus Torvalds cond_resched(); 21031da177e4SLinus Torvalds page = lru_to_page(&l_hold); 21041da177e4SLinus Torvalds list_del(&page->lru); 21057e9cd484SRik van Riel 210639b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 2107894bc310SLee Schermerhorn putback_lru_page(page); 2108894bc310SLee Schermerhorn continue; 2109894bc310SLee Schermerhorn } 2110894bc310SLee Schermerhorn 2111cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 2112cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 2113cc715d99SMel Gorman if (page_has_private(page)) 2114cc715d99SMel Gorman try_to_release_page(page, 0); 2115cc715d99SMel Gorman unlock_page(page); 2116cc715d99SMel Gorman } 2117cc715d99SMel Gorman } 2118cc715d99SMel Gorman 2119c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 2120c3ac9a8aSJohannes Weiner &vm_flags)) { 21219992af10SRik van Riel nr_rotated += hpage_nr_pages(page); 21228cab4754SWu Fengguang /* 21238cab4754SWu Fengguang * Identify referenced, file-backed active pages and 21248cab4754SWu Fengguang * give them one more trip around the active list. So 21258cab4754SWu Fengguang * that executable code get better chances to stay in 21268cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 21278cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 21288cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 21298cab4754SWu Fengguang * so we ignore them here. 21308cab4754SWu Fengguang */ 213141e20983SWu Fengguang if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { 21328cab4754SWu Fengguang list_add(&page->lru, &l_active); 21338cab4754SWu Fengguang continue; 21348cab4754SWu Fengguang } 21358cab4754SWu Fengguang } 21367e9cd484SRik van Riel 21375205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 21381da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 21391da177e4SLinus Torvalds } 21401da177e4SLinus Torvalds 2141b555749aSAndrew Morton /* 21428cab4754SWu Fengguang * Move pages back to the lru list. 2143b555749aSAndrew Morton */ 2144599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 21454f98a2feSRik van Riel /* 21468cab4754SWu Fengguang * Count referenced pages from currently used mappings as rotated, 21478cab4754SWu Fengguang * even though only some of them are actually re-activated. This 21488cab4754SWu Fengguang * helps balance scan pressure between file and anonymous pages in 21497c0db9e9SJerome Marchand * get_scan_count. 2150556adecbSRik van Riel */ 2151b7c46d15SJohannes Weiner reclaim_stat->recent_rotated[file] += nr_rotated; 2152556adecbSRik van Riel 21539d998b4fSMichal Hocko nr_activate = move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru); 21549d998b4fSMichal Hocko nr_deactivate = move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE); 2155599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 2156599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 21572bcf8879SHugh Dickins 2158747db954SJohannes Weiner mem_cgroup_uncharge_list(&l_hold); 21592d4894b5SMel Gorman free_unref_page_list(&l_hold); 21609d998b4fSMichal Hocko trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate, 21619d998b4fSMichal Hocko nr_deactivate, nr_rotated, sc->priority, file); 21621da177e4SLinus Torvalds } 21631da177e4SLinus Torvalds 216459dc76b0SRik van Riel /* 216559dc76b0SRik van Riel * The inactive anon list should be small enough that the VM never has 216659dc76b0SRik van Riel * to do too much work. 216714797e23SKOSAKI Motohiro * 216859dc76b0SRik van Riel * The inactive file list should be small enough to leave most memory 216959dc76b0SRik van Riel * to the established workingset on the scan-resistant active list, 217059dc76b0SRik van Riel * but large enough to avoid thrashing the aggregate readahead window. 217159dc76b0SRik van Riel * 217259dc76b0SRik van Riel * Both inactive lists should also be large enough that each inactive 217359dc76b0SRik van Riel * page has a chance to be referenced again before it is reclaimed. 217459dc76b0SRik van Riel * 21752a2e4885SJohannes Weiner * If that fails and refaulting is observed, the inactive list grows. 21762a2e4885SJohannes Weiner * 217759dc76b0SRik van Riel * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages 21783a50d14dSAndrey Ryabinin * on this LRU, maintained by the pageout code. An inactive_ratio 217959dc76b0SRik van Riel * of 3 means 3:1 or 25% of the pages are kept on the inactive list. 218059dc76b0SRik van Riel * 218159dc76b0SRik van Riel * total target max 218259dc76b0SRik van Riel * memory ratio inactive 218359dc76b0SRik van Riel * ------------------------------------- 218459dc76b0SRik van Riel * 10MB 1 5MB 218559dc76b0SRik van Riel * 100MB 1 50MB 218659dc76b0SRik van Riel * 1GB 3 250MB 218759dc76b0SRik van Riel * 10GB 10 0.9GB 218859dc76b0SRik van Riel * 100GB 31 3GB 218959dc76b0SRik van Riel * 1TB 101 10GB 219059dc76b0SRik van Riel * 10TB 320 32GB 219114797e23SKOSAKI Motohiro */ 2192f8d1a311SMel Gorman static bool inactive_list_is_low(struct lruvec *lruvec, bool file, 21932a2e4885SJohannes Weiner struct mem_cgroup *memcg, 21942a2e4885SJohannes Weiner struct scan_control *sc, bool actual_reclaim) 219514797e23SKOSAKI Motohiro { 2196fd538803SMichal Hocko enum lru_list active_lru = file * LRU_FILE + LRU_ACTIVE; 21972a2e4885SJohannes Weiner struct pglist_data *pgdat = lruvec_pgdat(lruvec); 21982a2e4885SJohannes Weiner enum lru_list inactive_lru = file * LRU_FILE; 21992a2e4885SJohannes Weiner unsigned long inactive, active; 22002a2e4885SJohannes Weiner unsigned long inactive_ratio; 22012a2e4885SJohannes Weiner unsigned long refaults; 220259dc76b0SRik van Riel unsigned long gb; 220359dc76b0SRik van Riel 220474e3f3c3SMinchan Kim /* 220574e3f3c3SMinchan Kim * If we don't have swap space, anonymous page deactivation 220674e3f3c3SMinchan Kim * is pointless. 220774e3f3c3SMinchan Kim */ 220859dc76b0SRik van Riel if (!file && !total_swap_pages) 220942e2e457SYaowei Bai return false; 221074e3f3c3SMinchan Kim 2211fd538803SMichal Hocko inactive = lruvec_lru_size(lruvec, inactive_lru, sc->reclaim_idx); 2212fd538803SMichal Hocko active = lruvec_lru_size(lruvec, active_lru, sc->reclaim_idx); 2213f8d1a311SMel Gorman 22142a2e4885SJohannes Weiner if (memcg) 2215ccda7f43SJohannes Weiner refaults = memcg_page_state(memcg, WORKINGSET_ACTIVATE); 22162a2e4885SJohannes Weiner else 22172a2e4885SJohannes Weiner refaults = node_page_state(pgdat, WORKINGSET_ACTIVATE); 22182a2e4885SJohannes Weiner 22192a2e4885SJohannes Weiner /* 22202a2e4885SJohannes Weiner * When refaults are being observed, it means a new workingset 22212a2e4885SJohannes Weiner * is being established. Disable active list protection to get 22222a2e4885SJohannes Weiner * rid of the stale workingset quickly. 22232a2e4885SJohannes Weiner */ 22242a2e4885SJohannes Weiner if (file && actual_reclaim && lruvec->refaults != refaults) { 22252a2e4885SJohannes Weiner inactive_ratio = 0; 22262a2e4885SJohannes Weiner } else { 222759dc76b0SRik van Riel gb = (inactive + active) >> (30 - PAGE_SHIFT); 222859dc76b0SRik van Riel if (gb) 222959dc76b0SRik van Riel inactive_ratio = int_sqrt(10 * gb); 2230b39415b2SRik van Riel else 223159dc76b0SRik van Riel inactive_ratio = 1; 22322a2e4885SJohannes Weiner } 223359dc76b0SRik van Riel 22342a2e4885SJohannes Weiner if (actual_reclaim) 22352a2e4885SJohannes Weiner trace_mm_vmscan_inactive_list_is_low(pgdat->node_id, sc->reclaim_idx, 2236fd538803SMichal Hocko lruvec_lru_size(lruvec, inactive_lru, MAX_NR_ZONES), inactive, 2237fd538803SMichal Hocko lruvec_lru_size(lruvec, active_lru, MAX_NR_ZONES), active, 2238fd538803SMichal Hocko inactive_ratio, file); 2239fd538803SMichal Hocko 224059dc76b0SRik van Riel return inactive * inactive_ratio < active; 2241b39415b2SRik van Riel } 2242b39415b2SRik van Riel 22434f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 22442a2e4885SJohannes Weiner struct lruvec *lruvec, struct mem_cgroup *memcg, 22452a2e4885SJohannes Weiner struct scan_control *sc) 2246b69408e8SChristoph Lameter { 2247b39415b2SRik van Riel if (is_active_lru(lru)) { 22482a2e4885SJohannes Weiner if (inactive_list_is_low(lruvec, is_file_lru(lru), 22492a2e4885SJohannes Weiner memcg, sc, true)) 22501a93be0eSKonstantin Khlebnikov shrink_active_list(nr_to_scan, lruvec, sc, lru); 2251556adecbSRik van Riel return 0; 2252556adecbSRik van Riel } 2253556adecbSRik van Riel 22541a93be0eSKonstantin Khlebnikov return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 2255b69408e8SChristoph Lameter } 2256b69408e8SChristoph Lameter 22579a265114SJohannes Weiner enum scan_balance { 22589a265114SJohannes Weiner SCAN_EQUAL, 22599a265114SJohannes Weiner SCAN_FRACT, 22609a265114SJohannes Weiner SCAN_ANON, 22619a265114SJohannes Weiner SCAN_FILE, 22629a265114SJohannes Weiner }; 22639a265114SJohannes Weiner 22641da177e4SLinus Torvalds /* 22654f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 22664f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 22674f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 22684f98a2feSRik van Riel * onto the active list instead of evict. 22694f98a2feSRik van Riel * 2270be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 2271be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 22724f98a2feSRik van Riel */ 227333377678SVladimir Davydov static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, 22746b4f7799SJohannes Weiner struct scan_control *sc, unsigned long *nr, 22756b4f7799SJohannes Weiner unsigned long *lru_pages) 22764f98a2feSRik van Riel { 227733377678SVladimir Davydov int swappiness = mem_cgroup_swappiness(memcg); 227890126375SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 22799a265114SJohannes Weiner u64 fraction[2]; 22809a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 2281599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 22829a265114SJohannes Weiner unsigned long anon_prio, file_prio; 22839a265114SJohannes Weiner enum scan_balance scan_balance; 22840bf1457fSJohannes Weiner unsigned long anon, file; 22859a265114SJohannes Weiner unsigned long ap, fp; 22869a265114SJohannes Weiner enum lru_list lru; 228776a33fc3SShaohua Li 228876a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 2289d8b38438SVladimir Davydov if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) { 22909a265114SJohannes Weiner scan_balance = SCAN_FILE; 229176a33fc3SShaohua Li goto out; 229276a33fc3SShaohua Li } 22934f98a2feSRik van Riel 229410316b31SJohannes Weiner /* 229510316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 229610316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 229710316b31SJohannes Weiner * disable swapping for individual groups completely when 229810316b31SJohannes Weiner * using the memory controller's swap limit feature would be 229910316b31SJohannes Weiner * too expensive. 230010316b31SJohannes Weiner */ 230102695175SJohannes Weiner if (!global_reclaim(sc) && !swappiness) { 23029a265114SJohannes Weiner scan_balance = SCAN_FILE; 230310316b31SJohannes Weiner goto out; 230410316b31SJohannes Weiner } 230510316b31SJohannes Weiner 230610316b31SJohannes Weiner /* 230710316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 230810316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 230910316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 231010316b31SJohannes Weiner */ 231102695175SJohannes Weiner if (!sc->priority && swappiness) { 23129a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 231310316b31SJohannes Weiner goto out; 231410316b31SJohannes Weiner } 231510316b31SJohannes Weiner 231611d16c25SJohannes Weiner /* 231762376251SJohannes Weiner * Prevent the reclaimer from falling into the cache trap: as 231862376251SJohannes Weiner * cache pages start out inactive, every cache fault will tip 231962376251SJohannes Weiner * the scan balance towards the file LRU. And as the file LRU 232062376251SJohannes Weiner * shrinks, so does the window for rotation from references. 232162376251SJohannes Weiner * This means we have a runaway feedback loop where a tiny 232262376251SJohannes Weiner * thrashing file LRU becomes infinitely more attractive than 232362376251SJohannes Weiner * anon pages. Try to detect this based on file LRU size. 232462376251SJohannes Weiner */ 232562376251SJohannes Weiner if (global_reclaim(sc)) { 2326599d0c95SMel Gorman unsigned long pgdatfile; 2327599d0c95SMel Gorman unsigned long pgdatfree; 2328599d0c95SMel Gorman int z; 2329599d0c95SMel Gorman unsigned long total_high_wmark = 0; 233062376251SJohannes Weiner 2331599d0c95SMel Gorman pgdatfree = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); 2332599d0c95SMel Gorman pgdatfile = node_page_state(pgdat, NR_ACTIVE_FILE) + 2333599d0c95SMel Gorman node_page_state(pgdat, NR_INACTIVE_FILE); 23342ab051e1SJerome Marchand 2335599d0c95SMel Gorman for (z = 0; z < MAX_NR_ZONES; z++) { 2336599d0c95SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 23376aa303deSMel Gorman if (!managed_zone(zone)) 2338599d0c95SMel Gorman continue; 2339599d0c95SMel Gorman 2340599d0c95SMel Gorman total_high_wmark += high_wmark_pages(zone); 2341599d0c95SMel Gorman } 2342599d0c95SMel Gorman 2343599d0c95SMel Gorman if (unlikely(pgdatfile + pgdatfree <= total_high_wmark)) { 234406226226SDavid Rientjes /* 234506226226SDavid Rientjes * Force SCAN_ANON if there are enough inactive 234606226226SDavid Rientjes * anonymous pages on the LRU in eligible zones. 234706226226SDavid Rientjes * Otherwise, the small LRU gets thrashed. 234806226226SDavid Rientjes */ 234906226226SDavid Rientjes if (!inactive_list_is_low(lruvec, false, memcg, sc, false) && 235006226226SDavid Rientjes lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, sc->reclaim_idx) 235106226226SDavid Rientjes >> sc->priority) { 235262376251SJohannes Weiner scan_balance = SCAN_ANON; 235362376251SJohannes Weiner goto out; 235462376251SJohannes Weiner } 235562376251SJohannes Weiner } 235606226226SDavid Rientjes } 235762376251SJohannes Weiner 235862376251SJohannes Weiner /* 2359316bda0eSVladimir Davydov * If there is enough inactive page cache, i.e. if the size of the 2360316bda0eSVladimir Davydov * inactive list is greater than that of the active list *and* the 2361316bda0eSVladimir Davydov * inactive list actually has some pages to scan on this priority, we 2362316bda0eSVladimir Davydov * do not reclaim anything from the anonymous working set right now. 2363316bda0eSVladimir Davydov * Without the second condition we could end up never scanning an 2364316bda0eSVladimir Davydov * lruvec even if it has plenty of old anonymous pages unless the 2365316bda0eSVladimir Davydov * system is under heavy pressure. 2366e9868505SRik van Riel */ 23672a2e4885SJohannes Weiner if (!inactive_list_is_low(lruvec, true, memcg, sc, false) && 236871ab6cfeSMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, sc->reclaim_idx) >> sc->priority) { 23699a265114SJohannes Weiner scan_balance = SCAN_FILE; 2370e9868505SRik van Riel goto out; 23714f98a2feSRik van Riel } 23724f98a2feSRik van Riel 23739a265114SJohannes Weiner scan_balance = SCAN_FRACT; 23749a265114SJohannes Weiner 23754f98a2feSRik van Riel /* 237658c37f6eSKOSAKI Motohiro * With swappiness at 100, anonymous and file have the same priority. 237758c37f6eSKOSAKI Motohiro * This scanning priority is essentially the inverse of IO cost. 237858c37f6eSKOSAKI Motohiro */ 237902695175SJohannes Weiner anon_prio = swappiness; 238075b00af7SHugh Dickins file_prio = 200 - anon_prio; 238158c37f6eSKOSAKI Motohiro 238258c37f6eSKOSAKI Motohiro /* 23834f98a2feSRik van Riel * OK, so we have swap space and a fair amount of page cache 23844f98a2feSRik van Riel * pages. We use the recently rotated / recently scanned 23854f98a2feSRik van Riel * ratios to determine how valuable each cache is. 23864f98a2feSRik van Riel * 23874f98a2feSRik van Riel * Because workloads change over time (and to avoid overflow) 23884f98a2feSRik van Riel * we keep these statistics as a floating average, which ends 23894f98a2feSRik van Riel * up weighing recent references more than old ones. 23904f98a2feSRik van Riel * 23914f98a2feSRik van Riel * anon in [0], file in [1] 23924f98a2feSRik van Riel */ 23932ab051e1SJerome Marchand 2394fd538803SMichal Hocko anon = lruvec_lru_size(lruvec, LRU_ACTIVE_ANON, MAX_NR_ZONES) + 2395fd538803SMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, MAX_NR_ZONES); 2396fd538803SMichal Hocko file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES) + 2397fd538803SMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, MAX_NR_ZONES); 23982ab051e1SJerome Marchand 2399599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 240058c37f6eSKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { 24016e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[0] /= 2; 24026e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[0] /= 2; 24034f98a2feSRik van Riel } 24044f98a2feSRik van Riel 24056e901571SKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { 24066e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[1] /= 2; 24076e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[1] /= 2; 24084f98a2feSRik van Riel } 24094f98a2feSRik van Riel 24104f98a2feSRik van Riel /* 241100d8089cSRik van Riel * The amount of pressure on anon vs file pages is inversely 241200d8089cSRik van Riel * proportional to the fraction of recently scanned pages on 241300d8089cSRik van Riel * each list that were recently referenced and in active use. 24144f98a2feSRik van Riel */ 2415fe35004fSSatoru Moriya ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1); 24166e901571SKOSAKI Motohiro ap /= reclaim_stat->recent_rotated[0] + 1; 24174f98a2feSRik van Riel 2418fe35004fSSatoru Moriya fp = file_prio * (reclaim_stat->recent_scanned[1] + 1); 24196e901571SKOSAKI Motohiro fp /= reclaim_stat->recent_rotated[1] + 1; 2420599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 24214f98a2feSRik van Riel 242276a33fc3SShaohua Li fraction[0] = ap; 242376a33fc3SShaohua Li fraction[1] = fp; 242476a33fc3SShaohua Li denominator = ap + fp + 1; 242576a33fc3SShaohua Li out: 24266b4f7799SJohannes Weiner *lru_pages = 0; 24274111304dSHugh Dickins for_each_evictable_lru(lru) { 24284111304dSHugh Dickins int file = is_file_lru(lru); 2429d778df51SJohannes Weiner unsigned long size; 243076a33fc3SShaohua Li unsigned long scan; 243176a33fc3SShaohua Li 243271ab6cfeSMichal Hocko size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); 2433d778df51SJohannes Weiner scan = size >> sc->priority; 2434688035f7SJohannes Weiner /* 2435688035f7SJohannes Weiner * If the cgroup's already been deleted, make sure to 2436688035f7SJohannes Weiner * scrape out the remaining cache. 2437688035f7SJohannes Weiner */ 2438688035f7SJohannes Weiner if (!scan && !mem_cgroup_online(memcg)) 2439d778df51SJohannes Weiner scan = min(size, SWAP_CLUSTER_MAX); 24409a265114SJohannes Weiner 24419a265114SJohannes Weiner switch (scan_balance) { 24429a265114SJohannes Weiner case SCAN_EQUAL: 24439a265114SJohannes Weiner /* Scan lists relative to size */ 24449a265114SJohannes Weiner break; 24459a265114SJohannes Weiner case SCAN_FRACT: 24469a265114SJohannes Weiner /* 24479a265114SJohannes Weiner * Scan types proportional to swappiness and 24489a265114SJohannes Weiner * their relative recent reclaim efficiency. 24499a265114SJohannes Weiner */ 24506f04f48dSSuleiman Souhlal scan = div64_u64(scan * fraction[file], 24516f04f48dSSuleiman Souhlal denominator); 24529a265114SJohannes Weiner break; 24539a265114SJohannes Weiner case SCAN_FILE: 24549a265114SJohannes Weiner case SCAN_ANON: 24559a265114SJohannes Weiner /* Scan one type exclusively */ 24566b4f7799SJohannes Weiner if ((scan_balance == SCAN_FILE) != file) { 24576b4f7799SJohannes Weiner size = 0; 24589a265114SJohannes Weiner scan = 0; 24596b4f7799SJohannes Weiner } 24609a265114SJohannes Weiner break; 24619a265114SJohannes Weiner default: 24629a265114SJohannes Weiner /* Look ma, no brain */ 24639a265114SJohannes Weiner BUG(); 24649a265114SJohannes Weiner } 24656b4f7799SJohannes Weiner 24666b4f7799SJohannes Weiner *lru_pages += size; 24674111304dSHugh Dickins nr[lru] = scan; 246876a33fc3SShaohua Li } 24696e08a369SWu Fengguang } 24704f98a2feSRik van Riel 24719b4f98cdSJohannes Weiner /* 2472a9dd0a83SMel Gorman * This is a basic per-node page freer. Used by both kswapd and direct reclaim. 24739b4f98cdSJohannes Weiner */ 2474a9dd0a83SMel Gorman static void shrink_node_memcg(struct pglist_data *pgdat, struct mem_cgroup *memcg, 24756b4f7799SJohannes Weiner struct scan_control *sc, unsigned long *lru_pages) 24769b4f98cdSJohannes Weiner { 2477ef8f2327SMel Gorman struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg); 24789b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 2479e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 24809b4f98cdSJohannes Weiner unsigned long nr_to_scan; 24819b4f98cdSJohannes Weiner enum lru_list lru; 24829b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 24839b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 24849b4f98cdSJohannes Weiner struct blk_plug plug; 24851a501907SMel Gorman bool scan_adjusted; 24869b4f98cdSJohannes Weiner 248733377678SVladimir Davydov get_scan_count(lruvec, memcg, sc, nr, lru_pages); 24889b4f98cdSJohannes Weiner 2489e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 2490e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 2491e82e0561SMel Gorman 24921a501907SMel Gorman /* 24931a501907SMel Gorman * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal 24941a501907SMel Gorman * event that can occur when there is little memory pressure e.g. 24951a501907SMel Gorman * multiple streaming readers/writers. Hence, we do not abort scanning 24961a501907SMel Gorman * when the requested number of pages are reclaimed when scanning at 24971a501907SMel Gorman * DEF_PRIORITY on the assumption that the fact we are direct 24981a501907SMel Gorman * reclaiming implies that kswapd is not keeping up and it is best to 24991a501907SMel Gorman * do a batch of work at once. For memcg reclaim one check is made to 25001a501907SMel Gorman * abort proportional reclaim if either the file or anon lru has already 25011a501907SMel Gorman * dropped to zero at the first pass. 25021a501907SMel Gorman */ 25031a501907SMel Gorman scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() && 25041a501907SMel Gorman sc->priority == DEF_PRIORITY); 25051a501907SMel Gorman 25069b4f98cdSJohannes Weiner blk_start_plug(&plug); 25079b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 25089b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 2509e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 2510e82e0561SMel Gorman unsigned long nr_scanned; 2511e82e0561SMel Gorman 25129b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 25139b4f98cdSJohannes Weiner if (nr[lru]) { 25149b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 25159b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 25169b4f98cdSJohannes Weiner 25179b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 25182a2e4885SJohannes Weiner lruvec, memcg, sc); 25199b4f98cdSJohannes Weiner } 25209b4f98cdSJohannes Weiner } 2521e82e0561SMel Gorman 2522bd041733SMichal Hocko cond_resched(); 2523bd041733SMichal Hocko 2524e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 2525e82e0561SMel Gorman continue; 2526e82e0561SMel Gorman 25279b4f98cdSJohannes Weiner /* 2528e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 25291a501907SMel Gorman * requested. Ensure that the anon and file LRUs are scanned 2530e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 2531e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 2532e82e0561SMel Gorman * proportional to the original scan target. 2533e82e0561SMel Gorman */ 2534e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 2535e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 2536e82e0561SMel Gorman 25371a501907SMel Gorman /* 25381a501907SMel Gorman * It's just vindictive to attack the larger once the smaller 25391a501907SMel Gorman * has gone to zero. And given the way we stop scanning the 25401a501907SMel Gorman * smaller below, this makes sure that we only make one nudge 25411a501907SMel Gorman * towards proportionality once we've got nr_to_reclaim. 25421a501907SMel Gorman */ 25431a501907SMel Gorman if (!nr_file || !nr_anon) 25441a501907SMel Gorman break; 25451a501907SMel Gorman 2546e82e0561SMel Gorman if (nr_file > nr_anon) { 2547e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 2548e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 2549e82e0561SMel Gorman lru = LRU_BASE; 2550e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 2551e82e0561SMel Gorman } else { 2552e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 2553e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 2554e82e0561SMel Gorman lru = LRU_FILE; 2555e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 2556e82e0561SMel Gorman } 2557e82e0561SMel Gorman 2558e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 2559e82e0561SMel Gorman nr[lru] = 0; 2560e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 2561e82e0561SMel Gorman 2562e82e0561SMel Gorman /* 2563e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 2564e82e0561SMel Gorman * scan target and the percentage scanning already complete 2565e82e0561SMel Gorman */ 2566e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 2567e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2568e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2569e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2570e82e0561SMel Gorman 2571e82e0561SMel Gorman lru += LRU_ACTIVE; 2572e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2573e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2574e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2575e82e0561SMel Gorman 2576e82e0561SMel Gorman scan_adjusted = true; 25779b4f98cdSJohannes Weiner } 25789b4f98cdSJohannes Weiner blk_finish_plug(&plug); 25799b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 25809b4f98cdSJohannes Weiner 25819b4f98cdSJohannes Weiner /* 25829b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 25839b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 25849b4f98cdSJohannes Weiner */ 25852a2e4885SJohannes Weiner if (inactive_list_is_low(lruvec, false, memcg, sc, true)) 25869b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 25879b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 25889b4f98cdSJohannes Weiner } 25899b4f98cdSJohannes Weiner 259023b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 25919e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 259223b9da55SMel Gorman { 2593d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 259423b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 25959e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 259623b9da55SMel Gorman return true; 259723b9da55SMel Gorman 259823b9da55SMel Gorman return false; 259923b9da55SMel Gorman } 260023b9da55SMel Gorman 26014f98a2feSRik van Riel /* 260223b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 260323b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 260423b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 260523b9da55SMel Gorman * calls try_to_compact_zone() that it will have enough free pages to succeed. 260623b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 26073e7d3449SMel Gorman */ 2608a9dd0a83SMel Gorman static inline bool should_continue_reclaim(struct pglist_data *pgdat, 26093e7d3449SMel Gorman unsigned long nr_reclaimed, 26103e7d3449SMel Gorman unsigned long nr_scanned, 26113e7d3449SMel Gorman struct scan_control *sc) 26123e7d3449SMel Gorman { 26133e7d3449SMel Gorman unsigned long pages_for_compaction; 26143e7d3449SMel Gorman unsigned long inactive_lru_pages; 2615a9dd0a83SMel Gorman int z; 26163e7d3449SMel Gorman 26173e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 26189e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 26193e7d3449SMel Gorman return false; 26203e7d3449SMel Gorman 26212876592fSMel Gorman /* Consider stopping depending on scan and reclaim activity */ 2622dcda9b04SMichal Hocko if (sc->gfp_mask & __GFP_RETRY_MAYFAIL) { 26233e7d3449SMel Gorman /* 2624dcda9b04SMichal Hocko * For __GFP_RETRY_MAYFAIL allocations, stop reclaiming if the 26252876592fSMel Gorman * full LRU list has been scanned and we are still failing 26262876592fSMel Gorman * to reclaim pages. This full LRU scan is potentially 2627dcda9b04SMichal Hocko * expensive but a __GFP_RETRY_MAYFAIL caller really wants to succeed 26283e7d3449SMel Gorman */ 26293e7d3449SMel Gorman if (!nr_reclaimed && !nr_scanned) 26303e7d3449SMel Gorman return false; 26312876592fSMel Gorman } else { 26322876592fSMel Gorman /* 2633dcda9b04SMichal Hocko * For non-__GFP_RETRY_MAYFAIL allocations which can presumably 26342876592fSMel Gorman * fail without consequence, stop if we failed to reclaim 26352876592fSMel Gorman * any pages from the last SWAP_CLUSTER_MAX number of 26362876592fSMel Gorman * pages that were scanned. This will return to the 26372876592fSMel Gorman * caller faster at the risk reclaim/compaction and 26382876592fSMel Gorman * the resulting allocation attempt fails 26392876592fSMel Gorman */ 26402876592fSMel Gorman if (!nr_reclaimed) 26412876592fSMel Gorman return false; 26422876592fSMel Gorman } 26433e7d3449SMel Gorman 26443e7d3449SMel Gorman /* 26453e7d3449SMel Gorman * If we have not reclaimed enough pages for compaction and the 26463e7d3449SMel Gorman * inactive lists are large enough, continue reclaiming 26473e7d3449SMel Gorman */ 26489861a62cSVlastimil Babka pages_for_compaction = compact_gap(sc->order); 2649a9dd0a83SMel Gorman inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); 2650ec8acf20SShaohua Li if (get_nr_swap_pages() > 0) 2651a9dd0a83SMel Gorman inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); 26523e7d3449SMel Gorman if (sc->nr_reclaimed < pages_for_compaction && 26533e7d3449SMel Gorman inactive_lru_pages > pages_for_compaction) 26543e7d3449SMel Gorman return true; 26553e7d3449SMel Gorman 26563e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 2657a9dd0a83SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 2658a9dd0a83SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 26596aa303deSMel Gorman if (!managed_zone(zone)) 2660a9dd0a83SMel Gorman continue; 2661a9dd0a83SMel Gorman 2662a9dd0a83SMel Gorman switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) { 2663cf378319SVlastimil Babka case COMPACT_SUCCESS: 26643e7d3449SMel Gorman case COMPACT_CONTINUE: 26653e7d3449SMel Gorman return false; 26663e7d3449SMel Gorman default: 2667a9dd0a83SMel Gorman /* check next zone */ 2668a9dd0a83SMel Gorman ; 26693e7d3449SMel Gorman } 26703e7d3449SMel Gorman } 2671a9dd0a83SMel Gorman return true; 2672a9dd0a83SMel Gorman } 26733e7d3449SMel Gorman 2674e3c1ac58SAndrey Ryabinin static bool pgdat_memcg_congested(pg_data_t *pgdat, struct mem_cgroup *memcg) 2675e3c1ac58SAndrey Ryabinin { 2676e3c1ac58SAndrey Ryabinin return test_bit(PGDAT_CONGESTED, &pgdat->flags) || 2677e3c1ac58SAndrey Ryabinin (memcg && memcg_congested(pgdat, memcg)); 2678e3c1ac58SAndrey Ryabinin } 2679e3c1ac58SAndrey Ryabinin 2680970a39a3SMel Gorman static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc) 2681f16015fbSJohannes Weiner { 2682cb731d6cSVladimir Davydov struct reclaim_state *reclaim_state = current->reclaim_state; 26839b4f98cdSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 26842344d7e4SJohannes Weiner bool reclaimable = false; 26859b4f98cdSJohannes Weiner 26869b4f98cdSJohannes Weiner do { 26875660048cSJohannes Weiner struct mem_cgroup *root = sc->target_mem_cgroup; 26885660048cSJohannes Weiner struct mem_cgroup_reclaim_cookie reclaim = { 2689ef8f2327SMel Gorman .pgdat = pgdat, 26909e3b2f8cSKonstantin Khlebnikov .priority = sc->priority, 26915660048cSJohannes Weiner }; 2692a9dd0a83SMel Gorman unsigned long node_lru_pages = 0; 2693694fbc0fSAndrew Morton struct mem_cgroup *memcg; 26945660048cSJohannes Weiner 2695d108c772SAndrey Ryabinin memset(&sc->nr, 0, sizeof(sc->nr)); 2696d108c772SAndrey Ryabinin 26979b4f98cdSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 26989b4f98cdSJohannes Weiner nr_scanned = sc->nr_scanned; 26999b4f98cdSJohannes Weiner 2700694fbc0fSAndrew Morton memcg = mem_cgroup_iter(root, NULL, &reclaim); 2701694fbc0fSAndrew Morton do { 27026b4f7799SJohannes Weiner unsigned long lru_pages; 27038e8ae645SJohannes Weiner unsigned long reclaimed; 2704cb731d6cSVladimir Davydov unsigned long scanned; 27059b4f98cdSJohannes Weiner 2706bf8d5d52SRoman Gushchin switch (mem_cgroup_protected(root, memcg)) { 2707bf8d5d52SRoman Gushchin case MEMCG_PROT_MIN: 2708bf8d5d52SRoman Gushchin /* 2709bf8d5d52SRoman Gushchin * Hard protection. 2710bf8d5d52SRoman Gushchin * If there is no reclaimable memory, OOM. 2711bf8d5d52SRoman Gushchin */ 2712bf8d5d52SRoman Gushchin continue; 2713bf8d5d52SRoman Gushchin case MEMCG_PROT_LOW: 2714bf8d5d52SRoman Gushchin /* 2715bf8d5d52SRoman Gushchin * Soft protection. 2716bf8d5d52SRoman Gushchin * Respect the protection only as long as 2717bf8d5d52SRoman Gushchin * there is an unprotected supply 2718bf8d5d52SRoman Gushchin * of reclaimable memory from other cgroups. 2719bf8d5d52SRoman Gushchin */ 2720d6622f63SYisheng Xie if (!sc->memcg_low_reclaim) { 2721d6622f63SYisheng Xie sc->memcg_low_skipped = 1; 2722241994edSJohannes Weiner continue; 2723d6622f63SYisheng Xie } 2724e27be240SJohannes Weiner memcg_memory_event(memcg, MEMCG_LOW); 2725bf8d5d52SRoman Gushchin break; 2726bf8d5d52SRoman Gushchin case MEMCG_PROT_NONE: 2727bf8d5d52SRoman Gushchin break; 2728241994edSJohannes Weiner } 2729241994edSJohannes Weiner 27308e8ae645SJohannes Weiner reclaimed = sc->nr_reclaimed; 2731cb731d6cSVladimir Davydov scanned = sc->nr_scanned; 2732a9dd0a83SMel Gorman shrink_node_memcg(pgdat, memcg, sc, &lru_pages); 2733a9dd0a83SMel Gorman node_lru_pages += lru_pages; 2734f9be23d6SKonstantin Khlebnikov 2735a9dd0a83SMel Gorman shrink_slab(sc->gfp_mask, pgdat->node_id, 27369092c71bSJosef Bacik memcg, sc->priority); 2737cb731d6cSVladimir Davydov 27388e8ae645SJohannes Weiner /* Record the group's reclaim efficiency */ 27398e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, memcg, false, 27408e8ae645SJohannes Weiner sc->nr_scanned - scanned, 27418e8ae645SJohannes Weiner sc->nr_reclaimed - reclaimed); 27428e8ae645SJohannes Weiner 27435660048cSJohannes Weiner /* 2744a394cb8eSMichal Hocko * Direct reclaim and kswapd have to scan all memory 2745a394cb8eSMichal Hocko * cgroups to fulfill the overall scan target for the 2746a9dd0a83SMel Gorman * node. 2747a394cb8eSMichal Hocko * 2748a394cb8eSMichal Hocko * Limit reclaim, on the other hand, only cares about 2749a394cb8eSMichal Hocko * nr_to_reclaim pages to be reclaimed and it will 2750a394cb8eSMichal Hocko * retry with decreasing priority if one round over the 2751a394cb8eSMichal Hocko * whole hierarchy is not sufficient. 27525660048cSJohannes Weiner */ 2753a394cb8eSMichal Hocko if (!global_reclaim(sc) && 2754a394cb8eSMichal Hocko sc->nr_reclaimed >= sc->nr_to_reclaim) { 27555660048cSJohannes Weiner mem_cgroup_iter_break(root, memcg); 27565660048cSJohannes Weiner break; 27575660048cSJohannes Weiner } 2758241994edSJohannes Weiner } while ((memcg = mem_cgroup_iter(root, memcg, &reclaim))); 275970ddf637SAnton Vorontsov 27606b4f7799SJohannes Weiner if (reclaim_state) { 2761cb731d6cSVladimir Davydov sc->nr_reclaimed += reclaim_state->reclaimed_slab; 27626b4f7799SJohannes Weiner reclaim_state->reclaimed_slab = 0; 27636b4f7799SJohannes Weiner } 27646b4f7799SJohannes Weiner 27658e8ae645SJohannes Weiner /* Record the subtree's reclaim efficiency */ 27668e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, 276770ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 276870ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 276970ddf637SAnton Vorontsov 27702344d7e4SJohannes Weiner if (sc->nr_reclaimed - nr_reclaimed) 27712344d7e4SJohannes Weiner reclaimable = true; 27722344d7e4SJohannes Weiner 2773e3c1ac58SAndrey Ryabinin if (current_is_kswapd()) { 2774d108c772SAndrey Ryabinin /* 2775e3c1ac58SAndrey Ryabinin * If reclaim is isolating dirty pages under writeback, 2776e3c1ac58SAndrey Ryabinin * it implies that the long-lived page allocation rate 2777e3c1ac58SAndrey Ryabinin * is exceeding the page laundering rate. Either the 2778e3c1ac58SAndrey Ryabinin * global limits are not being effective at throttling 2779e3c1ac58SAndrey Ryabinin * processes due to the page distribution throughout 2780e3c1ac58SAndrey Ryabinin * zones or there is heavy usage of a slow backing 2781e3c1ac58SAndrey Ryabinin * device. The only option is to throttle from reclaim 2782e3c1ac58SAndrey Ryabinin * context which is not ideal as there is no guarantee 2783d108c772SAndrey Ryabinin * the dirtying process is throttled in the same way 2784d108c772SAndrey Ryabinin * balance_dirty_pages() manages. 2785d108c772SAndrey Ryabinin * 2786e3c1ac58SAndrey Ryabinin * Once a node is flagged PGDAT_WRITEBACK, kswapd will 2787e3c1ac58SAndrey Ryabinin * count the number of pages under pages flagged for 2788e3c1ac58SAndrey Ryabinin * immediate reclaim and stall if any are encountered 2789e3c1ac58SAndrey Ryabinin * in the nr_immediate check below. 2790d108c772SAndrey Ryabinin */ 2791d108c772SAndrey Ryabinin if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken) 2792d108c772SAndrey Ryabinin set_bit(PGDAT_WRITEBACK, &pgdat->flags); 2793d108c772SAndrey Ryabinin 2794d108c772SAndrey Ryabinin /* 2795d108c772SAndrey Ryabinin * Tag a node as congested if all the dirty pages 2796d108c772SAndrey Ryabinin * scanned were backed by a congested BDI and 2797d108c772SAndrey Ryabinin * wait_iff_congested will stall. 2798d108c772SAndrey Ryabinin */ 2799d108c772SAndrey Ryabinin if (sc->nr.dirty && sc->nr.dirty == sc->nr.congested) 2800d108c772SAndrey Ryabinin set_bit(PGDAT_CONGESTED, &pgdat->flags); 2801d108c772SAndrey Ryabinin 2802d108c772SAndrey Ryabinin /* Allow kswapd to start writing pages during reclaim.*/ 2803d108c772SAndrey Ryabinin if (sc->nr.unqueued_dirty == sc->nr.file_taken) 2804d108c772SAndrey Ryabinin set_bit(PGDAT_DIRTY, &pgdat->flags); 2805d108c772SAndrey Ryabinin 2806d108c772SAndrey Ryabinin /* 2807d108c772SAndrey Ryabinin * If kswapd scans pages marked marked for immediate 2808d108c772SAndrey Ryabinin * reclaim and under writeback (nr_immediate), it 2809d108c772SAndrey Ryabinin * implies that pages are cycling through the LRU 2810d108c772SAndrey Ryabinin * faster than they are written so also forcibly stall. 2811d108c772SAndrey Ryabinin */ 2812d108c772SAndrey Ryabinin if (sc->nr.immediate) 2813d108c772SAndrey Ryabinin congestion_wait(BLK_RW_ASYNC, HZ/10); 2814d108c772SAndrey Ryabinin } 2815d108c772SAndrey Ryabinin 2816d108c772SAndrey Ryabinin /* 2817e3c1ac58SAndrey Ryabinin * Legacy memcg will stall in page writeback so avoid forcibly 2818e3c1ac58SAndrey Ryabinin * stalling in wait_iff_congested(). 2819e3c1ac58SAndrey Ryabinin */ 2820e3c1ac58SAndrey Ryabinin if (!global_reclaim(sc) && sane_reclaim(sc) && 2821e3c1ac58SAndrey Ryabinin sc->nr.dirty && sc->nr.dirty == sc->nr.congested) 2822e3c1ac58SAndrey Ryabinin set_memcg_congestion(pgdat, root, true); 2823e3c1ac58SAndrey Ryabinin 2824e3c1ac58SAndrey Ryabinin /* 2825d108c772SAndrey Ryabinin * Stall direct reclaim for IO completions if underlying BDIs 2826d108c772SAndrey Ryabinin * and node is congested. Allow kswapd to continue until it 2827d108c772SAndrey Ryabinin * starts encountering unqueued dirty pages or cycling through 2828d108c772SAndrey Ryabinin * the LRU too quickly. 2829d108c772SAndrey Ryabinin */ 2830d108c772SAndrey Ryabinin if (!sc->hibernation_mode && !current_is_kswapd() && 2831e3c1ac58SAndrey Ryabinin current_may_throttle() && pgdat_memcg_congested(pgdat, root)) 2832e3c1ac58SAndrey Ryabinin wait_iff_congested(BLK_RW_ASYNC, HZ/10); 2833d108c772SAndrey Ryabinin 2834a9dd0a83SMel Gorman } while (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, 28359b4f98cdSJohannes Weiner sc->nr_scanned - nr_scanned, sc)); 28362344d7e4SJohannes Weiner 2837c73322d0SJohannes Weiner /* 2838c73322d0SJohannes Weiner * Kswapd gives up on balancing particular nodes after too 2839c73322d0SJohannes Weiner * many failures to reclaim anything from them and goes to 2840c73322d0SJohannes Weiner * sleep. On reclaim progress, reset the failure counter. A 2841c73322d0SJohannes Weiner * successful direct reclaim run will revive a dormant kswapd. 2842c73322d0SJohannes Weiner */ 2843c73322d0SJohannes Weiner if (reclaimable) 2844c73322d0SJohannes Weiner pgdat->kswapd_failures = 0; 2845c73322d0SJohannes Weiner 28462344d7e4SJohannes Weiner return reclaimable; 2847f16015fbSJohannes Weiner } 2848f16015fbSJohannes Weiner 284953853e2dSVlastimil Babka /* 2850fdd4c614SVlastimil Babka * Returns true if compaction should go ahead for a costly-order request, or 2851fdd4c614SVlastimil Babka * the allocation would already succeed without compaction. Return false if we 2852fdd4c614SVlastimil Babka * should reclaim first. 285353853e2dSVlastimil Babka */ 28544f588331SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 2855fe4b1b24SMel Gorman { 285631483b6aSMel Gorman unsigned long watermark; 2857fdd4c614SVlastimil Babka enum compact_result suitable; 2858fe4b1b24SMel Gorman 2859fdd4c614SVlastimil Babka suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx); 2860fdd4c614SVlastimil Babka if (suitable == COMPACT_SUCCESS) 2861fdd4c614SVlastimil Babka /* Allocation should succeed already. Don't reclaim. */ 2862fdd4c614SVlastimil Babka return true; 2863fdd4c614SVlastimil Babka if (suitable == COMPACT_SKIPPED) 2864fdd4c614SVlastimil Babka /* Compaction cannot yet proceed. Do reclaim. */ 2865fe4b1b24SMel Gorman return false; 2866fe4b1b24SMel Gorman 2867fdd4c614SVlastimil Babka /* 2868fdd4c614SVlastimil Babka * Compaction is already possible, but it takes time to run and there 2869fdd4c614SVlastimil Babka * are potentially other callers using the pages just freed. So proceed 2870fdd4c614SVlastimil Babka * with reclaim to make a buffer of free pages available to give 2871fdd4c614SVlastimil Babka * compaction a reasonable chance of completing and allocating the page. 2872fdd4c614SVlastimil Babka * Note that we won't actually reclaim the whole buffer in one attempt 2873fdd4c614SVlastimil Babka * as the target watermark in should_continue_reclaim() is lower. But if 2874fdd4c614SVlastimil Babka * we are already above the high+gap watermark, don't reclaim at all. 2875fdd4c614SVlastimil Babka */ 2876fdd4c614SVlastimil Babka watermark = high_wmark_pages(zone) + compact_gap(sc->order); 2877fdd4c614SVlastimil Babka 2878fdd4c614SVlastimil Babka return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); 2879fe4b1b24SMel Gorman } 2880fe4b1b24SMel Gorman 28811da177e4SLinus Torvalds /* 28821da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 28831da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 28841da177e4SLinus Torvalds * request. 28851da177e4SLinus Torvalds * 28861da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 28871da177e4SLinus Torvalds * scan then give up on it. 28881da177e4SLinus Torvalds */ 28890a0337e0SMichal Hocko static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 28901da177e4SLinus Torvalds { 2891dd1a239fSMel Gorman struct zoneref *z; 289254a6eb5cSMel Gorman struct zone *zone; 28930608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 28940608f43dSAndrew Morton unsigned long nr_soft_scanned; 2895619d0d76SWeijie Yang gfp_t orig_mask; 289679dafcdcSMel Gorman pg_data_t *last_pgdat = NULL; 28971cfb419bSKAMEZAWA Hiroyuki 2898cc715d99SMel Gorman /* 2899cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 2900cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 2901cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 2902cc715d99SMel Gorman */ 2903619d0d76SWeijie Yang orig_mask = sc->gfp_mask; 2904b2e18757SMel Gorman if (buffer_heads_over_limit) { 2905cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 29064f588331SMel Gorman sc->reclaim_idx = gfp_zone(sc->gfp_mask); 2907b2e18757SMel Gorman } 2908cc715d99SMel Gorman 2909d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 2910b2e18757SMel Gorman sc->reclaim_idx, sc->nodemask) { 2911b2e18757SMel Gorman /* 29121cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 29131cfb419bSKAMEZAWA Hiroyuki * to global LRU. 29141cfb419bSKAMEZAWA Hiroyuki */ 291589b5fae5SJohannes Weiner if (global_reclaim(sc)) { 2916344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, 2917344736f2SVladimir Davydov GFP_KERNEL | __GFP_HARDWALL)) 29181da177e4SLinus Torvalds continue; 291965ec02cbSVladimir Davydov 2920e0887c19SRik van Riel /* 2921e0c23279SMel Gorman * If we already have plenty of memory free for 2922e0c23279SMel Gorman * compaction in this zone, don't free any more. 2923e0c23279SMel Gorman * Even though compaction is invoked for any 2924e0c23279SMel Gorman * non-zero order, only frequent costly order 2925e0c23279SMel Gorman * reclamation is disruptive enough to become a 2926c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 2927c7cfa37bSCopot Alexandru * page allocations. 2928e0887c19SRik van Riel */ 29290b06496aSJohannes Weiner if (IS_ENABLED(CONFIG_COMPACTION) && 29300b06496aSJohannes Weiner sc->order > PAGE_ALLOC_COSTLY_ORDER && 29314f588331SMel Gorman compaction_ready(zone, sc)) { 29320b06496aSJohannes Weiner sc->compaction_ready = true; 2933e0887c19SRik van Riel continue; 2934e0887c19SRik van Riel } 29350b06496aSJohannes Weiner 29360608f43dSAndrew Morton /* 293779dafcdcSMel Gorman * Shrink each node in the zonelist once. If the 293879dafcdcSMel Gorman * zonelist is ordered by zone (not the default) then a 293979dafcdcSMel Gorman * node may be shrunk multiple times but in that case 294079dafcdcSMel Gorman * the user prefers lower zones being preserved. 294179dafcdcSMel Gorman */ 294279dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 294379dafcdcSMel Gorman continue; 294479dafcdcSMel Gorman 294579dafcdcSMel Gorman /* 29460608f43dSAndrew Morton * This steals pages from memory cgroups over softlimit 29470608f43dSAndrew Morton * and returns the number of reclaimed pages and 29480608f43dSAndrew Morton * scanned pages. This works for global memory pressure 29490608f43dSAndrew Morton * and balancing, not for a memcg's limit. 29500608f43dSAndrew Morton */ 29510608f43dSAndrew Morton nr_soft_scanned = 0; 2952ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, 29530608f43dSAndrew Morton sc->order, sc->gfp_mask, 29540608f43dSAndrew Morton &nr_soft_scanned); 29550608f43dSAndrew Morton sc->nr_reclaimed += nr_soft_reclaimed; 29560608f43dSAndrew Morton sc->nr_scanned += nr_soft_scanned; 2957ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 2958ac34a1a3SKAMEZAWA Hiroyuki } 2959d149e3b2SYing Han 296079dafcdcSMel Gorman /* See comment about same check for global reclaim above */ 296179dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 296279dafcdcSMel Gorman continue; 296379dafcdcSMel Gorman last_pgdat = zone->zone_pgdat; 2964970a39a3SMel Gorman shrink_node(zone->zone_pgdat, sc); 29651da177e4SLinus Torvalds } 2966e0c23279SMel Gorman 296765ec02cbSVladimir Davydov /* 2968619d0d76SWeijie Yang * Restore to original mask to avoid the impact on the caller if we 2969619d0d76SWeijie Yang * promoted it to __GFP_HIGHMEM. 2970619d0d76SWeijie Yang */ 2971619d0d76SWeijie Yang sc->gfp_mask = orig_mask; 29721da177e4SLinus Torvalds } 29731da177e4SLinus Torvalds 29742a2e4885SJohannes Weiner static void snapshot_refaults(struct mem_cgroup *root_memcg, pg_data_t *pgdat) 29752a2e4885SJohannes Weiner { 29762a2e4885SJohannes Weiner struct mem_cgroup *memcg; 29772a2e4885SJohannes Weiner 29782a2e4885SJohannes Weiner memcg = mem_cgroup_iter(root_memcg, NULL, NULL); 29792a2e4885SJohannes Weiner do { 29802a2e4885SJohannes Weiner unsigned long refaults; 29812a2e4885SJohannes Weiner struct lruvec *lruvec; 29822a2e4885SJohannes Weiner 29832a2e4885SJohannes Weiner if (memcg) 2984ccda7f43SJohannes Weiner refaults = memcg_page_state(memcg, WORKINGSET_ACTIVATE); 29852a2e4885SJohannes Weiner else 29862a2e4885SJohannes Weiner refaults = node_page_state(pgdat, WORKINGSET_ACTIVATE); 29872a2e4885SJohannes Weiner 29882a2e4885SJohannes Weiner lruvec = mem_cgroup_lruvec(pgdat, memcg); 29892a2e4885SJohannes Weiner lruvec->refaults = refaults; 29902a2e4885SJohannes Weiner } while ((memcg = mem_cgroup_iter(root_memcg, memcg, NULL))); 29912a2e4885SJohannes Weiner } 29922a2e4885SJohannes Weiner 29931da177e4SLinus Torvalds /* 29941da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 29951da177e4SLinus Torvalds * 29961da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 29971da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 29981da177e4SLinus Torvalds * 29991da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 30001da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 30015b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 30025b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 30035b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 30045b0830cbSJens Axboe * work, and the allocation attempt will fail. 3005a41f24eaSNishanth Aravamudan * 3006a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 3007a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 30081da177e4SLinus Torvalds */ 3009dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 30103115cd91SVladimir Davydov struct scan_control *sc) 30111da177e4SLinus Torvalds { 3012241994edSJohannes Weiner int initial_priority = sc->priority; 30132a2e4885SJohannes Weiner pg_data_t *last_pgdat; 30142a2e4885SJohannes Weiner struct zoneref *z; 30152a2e4885SJohannes Weiner struct zone *zone; 3016241994edSJohannes Weiner retry: 3017873b4771SKeika Kobayashi delayacct_freepages_start(); 3018873b4771SKeika Kobayashi 301989b5fae5SJohannes Weiner if (global_reclaim(sc)) 30207cc30fcfSMel Gorman __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); 30211da177e4SLinus Torvalds 30229e3b2f8cSKonstantin Khlebnikov do { 302370ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 302470ddf637SAnton Vorontsov sc->priority); 302566e1707bSBalbir Singh sc->nr_scanned = 0; 30260a0337e0SMichal Hocko shrink_zones(zonelist, sc); 3027e0c23279SMel Gorman 3028bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 30290b06496aSJohannes Weiner break; 30300b06496aSJohannes Weiner 30310b06496aSJohannes Weiner if (sc->compaction_ready) 30320b06496aSJohannes Weiner break; 30331da177e4SLinus Torvalds 30341da177e4SLinus Torvalds /* 30350e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 30360e50ce3bSMinchan Kim * writepage even in laptop mode. 30370e50ce3bSMinchan Kim */ 30380e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 30390e50ce3bSMinchan Kim sc->may_writepage = 1; 30400b06496aSJohannes Weiner } while (--sc->priority >= 0); 3041bb21c7ceSKOSAKI Motohiro 30422a2e4885SJohannes Weiner last_pgdat = NULL; 30432a2e4885SJohannes Weiner for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx, 30442a2e4885SJohannes Weiner sc->nodemask) { 30452a2e4885SJohannes Weiner if (zone->zone_pgdat == last_pgdat) 30462a2e4885SJohannes Weiner continue; 30472a2e4885SJohannes Weiner last_pgdat = zone->zone_pgdat; 30482a2e4885SJohannes Weiner snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat); 3049e3c1ac58SAndrey Ryabinin set_memcg_congestion(last_pgdat, sc->target_mem_cgroup, false); 30502a2e4885SJohannes Weiner } 30512a2e4885SJohannes Weiner 3052873b4771SKeika Kobayashi delayacct_freepages_end(); 3053873b4771SKeika Kobayashi 3054bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 3055bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 3056bb21c7ceSKOSAKI Motohiro 30570cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 30580b06496aSJohannes Weiner if (sc->compaction_ready) 30597335084dSMel Gorman return 1; 30607335084dSMel Gorman 3061241994edSJohannes Weiner /* Untapped cgroup reserves? Don't OOM, retry. */ 3062d6622f63SYisheng Xie if (sc->memcg_low_skipped) { 3063241994edSJohannes Weiner sc->priority = initial_priority; 3064d6622f63SYisheng Xie sc->memcg_low_reclaim = 1; 3065d6622f63SYisheng Xie sc->memcg_low_skipped = 0; 3066241994edSJohannes Weiner goto retry; 3067241994edSJohannes Weiner } 3068241994edSJohannes Weiner 3069bb21c7ceSKOSAKI Motohiro return 0; 30701da177e4SLinus Torvalds } 30711da177e4SLinus Torvalds 3072c73322d0SJohannes Weiner static bool allow_direct_reclaim(pg_data_t *pgdat) 30735515061dSMel Gorman { 30745515061dSMel Gorman struct zone *zone; 30755515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 30765515061dSMel Gorman unsigned long free_pages = 0; 30775515061dSMel Gorman int i; 30785515061dSMel Gorman bool wmark_ok; 30795515061dSMel Gorman 3080c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3081c73322d0SJohannes Weiner return true; 3082c73322d0SJohannes Weiner 30835515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 30845515061dSMel Gorman zone = &pgdat->node_zones[i]; 3085d450abd8SJohannes Weiner if (!managed_zone(zone)) 3086d450abd8SJohannes Weiner continue; 3087d450abd8SJohannes Weiner 3088d450abd8SJohannes Weiner if (!zone_reclaimable_pages(zone)) 3089675becceSMel Gorman continue; 3090675becceSMel Gorman 30915515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 30925515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 30935515061dSMel Gorman } 30945515061dSMel Gorman 3095675becceSMel Gorman /* If there are no reserves (unexpected config) then do not throttle */ 3096675becceSMel Gorman if (!pfmemalloc_reserve) 3097675becceSMel Gorman return true; 3098675becceSMel Gorman 30995515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 31005515061dSMel Gorman 31015515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 31025515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 310338087d9bSMel Gorman pgdat->kswapd_classzone_idx = min(pgdat->kswapd_classzone_idx, 31045515061dSMel Gorman (enum zone_type)ZONE_NORMAL); 31055515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 31065515061dSMel Gorman } 31075515061dSMel Gorman 31085515061dSMel Gorman return wmark_ok; 31095515061dSMel Gorman } 31105515061dSMel Gorman 31115515061dSMel Gorman /* 31125515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 31135515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 31145515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 311550694c28SMel Gorman * when the low watermark is reached. 311650694c28SMel Gorman * 311750694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 311850694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 31195515061dSMel Gorman */ 312050694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 31215515061dSMel Gorman nodemask_t *nodemask) 31225515061dSMel Gorman { 3123675becceSMel Gorman struct zoneref *z; 31245515061dSMel Gorman struct zone *zone; 3125675becceSMel Gorman pg_data_t *pgdat = NULL; 31265515061dSMel Gorman 31275515061dSMel Gorman /* 31285515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 31295515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 31305515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 31315515061dSMel Gorman * committing a transaction where throttling it could forcing other 31325515061dSMel Gorman * processes to block on log_wait_commit(). 31335515061dSMel Gorman */ 31345515061dSMel Gorman if (current->flags & PF_KTHREAD) 313550694c28SMel Gorman goto out; 313650694c28SMel Gorman 313750694c28SMel Gorman /* 313850694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 313950694c28SMel Gorman * It should return quickly so it can exit and free its memory 314050694c28SMel Gorman */ 314150694c28SMel Gorman if (fatal_signal_pending(current)) 314250694c28SMel Gorman goto out; 31435515061dSMel Gorman 3144675becceSMel Gorman /* 3145675becceSMel Gorman * Check if the pfmemalloc reserves are ok by finding the first node 3146675becceSMel Gorman * with a usable ZONE_NORMAL or lower zone. The expectation is that 3147675becceSMel Gorman * GFP_KERNEL will be required for allocating network buffers when 3148675becceSMel Gorman * swapping over the network so ZONE_HIGHMEM is unusable. 3149675becceSMel Gorman * 3150675becceSMel Gorman * Throttling is based on the first usable node and throttled processes 3151675becceSMel Gorman * wait on a queue until kswapd makes progress and wakes them. There 3152675becceSMel Gorman * is an affinity then between processes waking up and where reclaim 3153675becceSMel Gorman * progress has been made assuming the process wakes on the same node. 3154675becceSMel Gorman * More importantly, processes running on remote nodes will not compete 3155675becceSMel Gorman * for remote pfmemalloc reserves and processes on different nodes 3156675becceSMel Gorman * should make reasonable progress. 3157675becceSMel Gorman */ 3158675becceSMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 315917636faaSMichael S. Tsirkin gfp_zone(gfp_mask), nodemask) { 3160675becceSMel Gorman if (zone_idx(zone) > ZONE_NORMAL) 3161675becceSMel Gorman continue; 3162675becceSMel Gorman 3163675becceSMel Gorman /* Throttle based on the first usable node */ 31645515061dSMel Gorman pgdat = zone->zone_pgdat; 3165c73322d0SJohannes Weiner if (allow_direct_reclaim(pgdat)) 316650694c28SMel Gorman goto out; 3167675becceSMel Gorman break; 3168675becceSMel Gorman } 3169675becceSMel Gorman 3170675becceSMel Gorman /* If no zone was usable by the allocation flags then do not throttle */ 3171675becceSMel Gorman if (!pgdat) 3172675becceSMel Gorman goto out; 31735515061dSMel Gorman 317468243e76SMel Gorman /* Account for the throttling */ 317568243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 317668243e76SMel Gorman 31775515061dSMel Gorman /* 31785515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 31795515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 31805515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 31815515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 31825515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 31835515061dSMel Gorman * second before continuing. 31845515061dSMel Gorman */ 31855515061dSMel Gorman if (!(gfp_mask & __GFP_FS)) { 31865515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 3187c73322d0SJohannes Weiner allow_direct_reclaim(pgdat), HZ); 318850694c28SMel Gorman 318950694c28SMel Gorman goto check_pending; 31905515061dSMel Gorman } 31915515061dSMel Gorman 31925515061dSMel Gorman /* Throttle until kswapd wakes the process */ 31935515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 3194c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)); 319550694c28SMel Gorman 319650694c28SMel Gorman check_pending: 319750694c28SMel Gorman if (fatal_signal_pending(current)) 319850694c28SMel Gorman return true; 319950694c28SMel Gorman 320050694c28SMel Gorman out: 320150694c28SMel Gorman return false; 32025515061dSMel Gorman } 32035515061dSMel Gorman 3204dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 3205327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 320666e1707bSBalbir Singh { 320733906bc5SMel Gorman unsigned long nr_reclaimed; 320866e1707bSBalbir Singh struct scan_control sc = { 320922fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3210f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 3211b2e18757SMel Gorman .reclaim_idx = gfp_zone(gfp_mask), 3212ee814fe2SJohannes Weiner .order = order, 3213ee814fe2SJohannes Weiner .nodemask = nodemask, 3214ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 3215ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3216a6dc60f8SJohannes Weiner .may_unmap = 1, 32172e2e4259SKOSAKI Motohiro .may_swap = 1, 321866e1707bSBalbir Singh }; 321966e1707bSBalbir Singh 32205515061dSMel Gorman /* 3221bb451fdfSGreg Thelen * scan_control uses s8 fields for order, priority, and reclaim_idx. 3222bb451fdfSGreg Thelen * Confirm they are large enough for max values. 3223bb451fdfSGreg Thelen */ 3224bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_ORDER > S8_MAX); 3225bb451fdfSGreg Thelen BUILD_BUG_ON(DEF_PRIORITY > S8_MAX); 3226bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX); 3227bb451fdfSGreg Thelen 3228bb451fdfSGreg Thelen /* 322950694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 323050694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 323150694c28SMel Gorman * point. 32325515061dSMel Gorman */ 3233f2f43e56SNick Desaulniers if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask)) 32345515061dSMel Gorman return 1; 32355515061dSMel Gorman 323633906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_begin(order, 323733906bc5SMel Gorman sc.may_writepage, 3238f2f43e56SNick Desaulniers sc.gfp_mask, 3239e5146b12SMel Gorman sc.reclaim_idx); 324033906bc5SMel Gorman 32413115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 324233906bc5SMel Gorman 324333906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 324433906bc5SMel Gorman 324533906bc5SMel Gorman return nr_reclaimed; 324666e1707bSBalbir Singh } 324766e1707bSBalbir Singh 3248c255a458SAndrew Morton #ifdef CONFIG_MEMCG 324966e1707bSBalbir Singh 3250a9dd0a83SMel Gorman unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, 32514e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 3252ef8f2327SMel Gorman pg_data_t *pgdat, 32530ae5e89cSYing Han unsigned long *nr_scanned) 32544e416953SBalbir Singh { 32554e416953SBalbir Singh struct scan_control sc = { 3256b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3257ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 32584e416953SBalbir Singh .may_writepage = !laptop_mode, 32594e416953SBalbir Singh .may_unmap = 1, 3260b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 32614e416953SBalbir Singh .may_swap = !noswap, 32624e416953SBalbir Singh }; 32636b4f7799SJohannes Weiner unsigned long lru_pages; 32640ae5e89cSYing Han 32654e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 32664e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 3267bdce6d9eSKOSAKI Motohiro 32689e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 3269bdce6d9eSKOSAKI Motohiro sc.may_writepage, 3270e5146b12SMel Gorman sc.gfp_mask, 3271e5146b12SMel Gorman sc.reclaim_idx); 3272bdce6d9eSKOSAKI Motohiro 32734e416953SBalbir Singh /* 32744e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 32754e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 3276a9dd0a83SMel Gorman * if we don't reclaim here, the shrink_node from balance_pgdat 32774e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 32784e416953SBalbir Singh * the priority and make it zero. 32794e416953SBalbir Singh */ 3280ef8f2327SMel Gorman shrink_node_memcg(pgdat, memcg, &sc, &lru_pages); 3281bdce6d9eSKOSAKI Motohiro 3282bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 3283bdce6d9eSKOSAKI Motohiro 32840ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 32854e416953SBalbir Singh return sc.nr_reclaimed; 32864e416953SBalbir Singh } 32874e416953SBalbir Singh 328872835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 3289b70a2a21SJohannes Weiner unsigned long nr_pages, 32908c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 3291b70a2a21SJohannes Weiner bool may_swap) 329266e1707bSBalbir Singh { 32934e416953SBalbir Singh struct zonelist *zonelist; 3294bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 3295889976dbSYing Han int nid; 3296499118e9SVlastimil Babka unsigned int noreclaim_flag; 329766e1707bSBalbir Singh struct scan_control sc = { 3298b70a2a21SJohannes Weiner .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 32997dea19f9SMichal Hocko .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | 3300ee814fe2SJohannes Weiner (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 3301b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 3302ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 3303ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 330466e1707bSBalbir Singh .may_writepage = !laptop_mode, 3305a6dc60f8SJohannes Weiner .may_unmap = 1, 3306b70a2a21SJohannes Weiner .may_swap = may_swap, 3307a09ed5e0SYing Han }; 330866e1707bSBalbir Singh 3309889976dbSYing Han /* 3310889976dbSYing Han * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't 3311889976dbSYing Han * take care of from where we get pages. So the node where we start the 3312889976dbSYing Han * scan does not need to be the current node. 3313889976dbSYing Han */ 331472835c86SJohannes Weiner nid = mem_cgroup_select_victim_node(memcg); 3315889976dbSYing Han 3316c9634cf0SAneesh Kumar K.V zonelist = &NODE_DATA(nid)->node_zonelists[ZONELIST_FALLBACK]; 3317bdce6d9eSKOSAKI Motohiro 3318bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_begin(0, 3319bdce6d9eSKOSAKI Motohiro sc.may_writepage, 3320e5146b12SMel Gorman sc.gfp_mask, 3321e5146b12SMel Gorman sc.reclaim_idx); 3322bdce6d9eSKOSAKI Motohiro 3323499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 33243115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3325499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 3326bdce6d9eSKOSAKI Motohiro 3327bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 3328bdce6d9eSKOSAKI Motohiro 3329bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 333066e1707bSBalbir Singh } 333166e1707bSBalbir Singh #endif 333266e1707bSBalbir Singh 33331d82de61SMel Gorman static void age_active_anon(struct pglist_data *pgdat, 3334ef8f2327SMel Gorman struct scan_control *sc) 3335f16015fbSJohannes Weiner { 3336b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 3337b95a2f2dSJohannes Weiner 3338b95a2f2dSJohannes Weiner if (!total_swap_pages) 3339b95a2f2dSJohannes Weiner return; 3340b95a2f2dSJohannes Weiner 3341b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 3342b95a2f2dSJohannes Weiner do { 3343ef8f2327SMel Gorman struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg); 3344f16015fbSJohannes Weiner 33452a2e4885SJohannes Weiner if (inactive_list_is_low(lruvec, false, memcg, sc, true)) 33461a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 33479e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 3348b95a2f2dSJohannes Weiner 3349b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 3350b95a2f2dSJohannes Weiner } while (memcg); 3351f16015fbSJohannes Weiner } 3352f16015fbSJohannes Weiner 3353e716f2ebSMel Gorman /* 3354e716f2ebSMel Gorman * Returns true if there is an eligible zone balanced for the request order 3355e716f2ebSMel Gorman * and classzone_idx 3356e716f2ebSMel Gorman */ 3357e716f2ebSMel Gorman static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx) 335860cefed4SJohannes Weiner { 3359e716f2ebSMel Gorman int i; 3360e716f2ebSMel Gorman unsigned long mark = -1; 3361e716f2ebSMel Gorman struct zone *zone; 336260cefed4SJohannes Weiner 3363e716f2ebSMel Gorman for (i = 0; i <= classzone_idx; i++) { 3364e716f2ebSMel Gorman zone = pgdat->node_zones + i; 33656256c6b4SMel Gorman 3366e716f2ebSMel Gorman if (!managed_zone(zone)) 3367e716f2ebSMel Gorman continue; 3368e716f2ebSMel Gorman 3369e716f2ebSMel Gorman mark = high_wmark_pages(zone); 3370e716f2ebSMel Gorman if (zone_watermark_ok_safe(zone, order, mark, classzone_idx)) 33716256c6b4SMel Gorman return true; 337260cefed4SJohannes Weiner } 337360cefed4SJohannes Weiner 3374e716f2ebSMel Gorman /* 3375e716f2ebSMel Gorman * If a node has no populated zone within classzone_idx, it does not 3376e716f2ebSMel Gorman * need balancing by definition. This can happen if a zone-restricted 3377e716f2ebSMel Gorman * allocation tries to wake a remote kswapd. 3378e716f2ebSMel Gorman */ 3379e716f2ebSMel Gorman if (mark == -1) 3380e716f2ebSMel Gorman return true; 3381e716f2ebSMel Gorman 3382e716f2ebSMel Gorman return false; 3383e716f2ebSMel Gorman } 3384e716f2ebSMel Gorman 3385631b6e08SMel Gorman /* Clear pgdat state for congested, dirty or under writeback. */ 3386631b6e08SMel Gorman static void clear_pgdat_congested(pg_data_t *pgdat) 3387631b6e08SMel Gorman { 3388631b6e08SMel Gorman clear_bit(PGDAT_CONGESTED, &pgdat->flags); 3389631b6e08SMel Gorman clear_bit(PGDAT_DIRTY, &pgdat->flags); 3390631b6e08SMel Gorman clear_bit(PGDAT_WRITEBACK, &pgdat->flags); 3391631b6e08SMel Gorman } 3392631b6e08SMel Gorman 33931741c877SMel Gorman /* 33945515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 33955515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 33965515061dSMel Gorman * 33975515061dSMel Gorman * Returns true if kswapd is ready to sleep 33985515061dSMel Gorman */ 3399d9f21d42SMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, int classzone_idx) 3400f50de2d3SMel Gorman { 34015515061dSMel Gorman /* 34029e5e3661SVlastimil Babka * The throttled processes are normally woken up in balance_pgdat() as 3403c73322d0SJohannes Weiner * soon as allow_direct_reclaim() is true. But there is a potential 34049e5e3661SVlastimil Babka * race between when kswapd checks the watermarks and a process gets 34059e5e3661SVlastimil Babka * throttled. There is also a potential race if processes get 34069e5e3661SVlastimil Babka * throttled, kswapd wakes, a large process exits thereby balancing the 34079e5e3661SVlastimil Babka * zones, which causes kswapd to exit balance_pgdat() before reaching 34089e5e3661SVlastimil Babka * the wake up checks. If kswapd is going to sleep, no process should 34099e5e3661SVlastimil Babka * be sleeping on pfmemalloc_wait, so wake them now if necessary. If 34109e5e3661SVlastimil Babka * the wake up is premature, processes will wake kswapd and get 34119e5e3661SVlastimil Babka * throttled again. The difference from wake ups in balance_pgdat() is 34129e5e3661SVlastimil Babka * that here we are under prepare_to_wait(). 34135515061dSMel Gorman */ 34149e5e3661SVlastimil Babka if (waitqueue_active(&pgdat->pfmemalloc_wait)) 34159e5e3661SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 3416f50de2d3SMel Gorman 3417c73322d0SJohannes Weiner /* Hopeless node, leave it to direct reclaim */ 3418c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3419c73322d0SJohannes Weiner return true; 3420c73322d0SJohannes Weiner 3421e716f2ebSMel Gorman if (pgdat_balanced(pgdat, order, classzone_idx)) { 3422631b6e08SMel Gorman clear_pgdat_congested(pgdat); 3423333b0a45SShantanu Goel return true; 34241d82de61SMel Gorman } 34251d82de61SMel Gorman 3426333b0a45SShantanu Goel return false; 3427f50de2d3SMel Gorman } 3428f50de2d3SMel Gorman 34291da177e4SLinus Torvalds /* 34301d82de61SMel Gorman * kswapd shrinks a node of pages that are at or below the highest usable 34311d82de61SMel Gorman * zone that is currently unbalanced. 3432b8e83b94SMel Gorman * 3433b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 3434283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 3435283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 343675485363SMel Gorman */ 34371d82de61SMel Gorman static bool kswapd_shrink_node(pg_data_t *pgdat, 3438accf6242SVlastimil Babka struct scan_control *sc) 343975485363SMel Gorman { 34401d82de61SMel Gorman struct zone *zone; 34411d82de61SMel Gorman int z; 344275485363SMel Gorman 34431d82de61SMel Gorman /* Reclaim a number of pages proportional to the number of zones */ 34441d82de61SMel Gorman sc->nr_to_reclaim = 0; 3445970a39a3SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 34461d82de61SMel Gorman zone = pgdat->node_zones + z; 34476aa303deSMel Gorman if (!managed_zone(zone)) 34481d82de61SMel Gorman continue; 34497c954f6dSMel Gorman 34501d82de61SMel Gorman sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); 34517c954f6dSMel Gorman } 34527c954f6dSMel Gorman 34531d82de61SMel Gorman /* 34541d82de61SMel Gorman * Historically care was taken to put equal pressure on all zones but 34551d82de61SMel Gorman * now pressure is applied based on node LRU order. 34561d82de61SMel Gorman */ 3457970a39a3SMel Gorman shrink_node(pgdat, sc); 34581d82de61SMel Gorman 34591d82de61SMel Gorman /* 34601d82de61SMel Gorman * Fragmentation may mean that the system cannot be rebalanced for 34611d82de61SMel Gorman * high-order allocations. If twice the allocation size has been 34621d82de61SMel Gorman * reclaimed then recheck watermarks only at order-0 to prevent 34631d82de61SMel Gorman * excessive reclaim. Assume that a process requested a high-order 34641d82de61SMel Gorman * can direct reclaim/compact. 34651d82de61SMel Gorman */ 34669861a62cSVlastimil Babka if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order)) 34671d82de61SMel Gorman sc->order = 0; 34681d82de61SMel Gorman 3469b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 347075485363SMel Gorman } 347175485363SMel Gorman 347275485363SMel Gorman /* 34731d82de61SMel Gorman * For kswapd, balance_pgdat() will reclaim pages across a node from zones 34741d82de61SMel Gorman * that are eligible for use by the caller until at least one zone is 34751d82de61SMel Gorman * balanced. 34761da177e4SLinus Torvalds * 34771d82de61SMel Gorman * Returns the order kswapd finished reclaiming at. 34781da177e4SLinus Torvalds * 34791da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 348041858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 34811d82de61SMel Gorman * found to have free_pages <= high_wmark_pages(zone), any page is that zone 34821d82de61SMel Gorman * or lower is eligible for reclaim until at least one usable zone is 34831d82de61SMel Gorman * balanced. 34841da177e4SLinus Torvalds */ 3485accf6242SVlastimil Babka static int balance_pgdat(pg_data_t *pgdat, int order, int classzone_idx) 34861da177e4SLinus Torvalds { 34871da177e4SLinus Torvalds int i; 34880608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 34890608f43dSAndrew Morton unsigned long nr_soft_scanned; 34901d82de61SMel Gorman struct zone *zone; 3491179e9639SAndrew Morton struct scan_control sc = { 3492179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 3493ee814fe2SJohannes Weiner .order = order, 3494b8e83b94SMel Gorman .priority = DEF_PRIORITY, 3495ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3496a6dc60f8SJohannes Weiner .may_unmap = 1, 34972e2e4259SKOSAKI Motohiro .may_swap = 1, 3498179e9639SAndrew Morton }; 349993781325SOmar Sandoval 350093781325SOmar Sandoval __fs_reclaim_acquire(); 350193781325SOmar Sandoval 3502f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 35031da177e4SLinus Torvalds 35049e3b2f8cSKonstantin Khlebnikov do { 3505c73322d0SJohannes Weiner unsigned long nr_reclaimed = sc.nr_reclaimed; 3506b8e83b94SMel Gorman bool raise_priority = true; 350793781325SOmar Sandoval bool ret; 3508b8e83b94SMel Gorman 350984c7a777SMel Gorman sc.reclaim_idx = classzone_idx; 35101da177e4SLinus Torvalds 351186c79f6bSMel Gorman /* 351284c7a777SMel Gorman * If the number of buffer_heads exceeds the maximum allowed 351384c7a777SMel Gorman * then consider reclaiming from all zones. This has a dual 351484c7a777SMel Gorman * purpose -- on 64-bit systems it is expected that 351584c7a777SMel Gorman * buffer_heads are stripped during active rotation. On 32-bit 351684c7a777SMel Gorman * systems, highmem pages can pin lowmem memory and shrinking 351784c7a777SMel Gorman * buffers can relieve lowmem pressure. Reclaim may still not 351884c7a777SMel Gorman * go ahead if all eligible zones for the original allocation 351984c7a777SMel Gorman * request are balanced to avoid excessive reclaim from kswapd. 352086c79f6bSMel Gorman */ 352186c79f6bSMel Gorman if (buffer_heads_over_limit) { 352286c79f6bSMel Gorman for (i = MAX_NR_ZONES - 1; i >= 0; i--) { 352386c79f6bSMel Gorman zone = pgdat->node_zones + i; 35246aa303deSMel Gorman if (!managed_zone(zone)) 352586c79f6bSMel Gorman continue; 352686c79f6bSMel Gorman 3527970a39a3SMel Gorman sc.reclaim_idx = i; 352886c79f6bSMel Gorman break; 352986c79f6bSMel Gorman } 353086c79f6bSMel Gorman } 353186c79f6bSMel Gorman 353286c79f6bSMel Gorman /* 3533e716f2ebSMel Gorman * Only reclaim if there are no eligible zones. Note that 3534e716f2ebSMel Gorman * sc.reclaim_idx is not used as buffer_heads_over_limit may 3535e716f2ebSMel Gorman * have adjusted it. 353686c79f6bSMel Gorman */ 3537e716f2ebSMel Gorman if (pgdat_balanced(pgdat, sc.order, classzone_idx)) 35381da177e4SLinus Torvalds goto out; 3539e1dbeda6SAndrew Morton 35401da177e4SLinus Torvalds /* 35411d82de61SMel Gorman * Do some background aging of the anon list, to give 35421d82de61SMel Gorman * pages a chance to be referenced before reclaiming. All 35431d82de61SMel Gorman * pages are rotated regardless of classzone as this is 35441d82de61SMel Gorman * about consistent aging. 35451d82de61SMel Gorman */ 3546ef8f2327SMel Gorman age_active_anon(pgdat, &sc); 35471d82de61SMel Gorman 35481d82de61SMel Gorman /* 3549b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 3550b7ea3c41SMel Gorman * even in laptop mode. 3551b7ea3c41SMel Gorman */ 3552047d72c3SJohannes Weiner if (sc.priority < DEF_PRIORITY - 2) 3553b7ea3c41SMel Gorman sc.may_writepage = 1; 3554b7ea3c41SMel Gorman 35551d82de61SMel Gorman /* Call soft limit reclaim before calling shrink_node. */ 35561da177e4SLinus Torvalds sc.nr_scanned = 0; 35570608f43dSAndrew Morton nr_soft_scanned = 0; 3558ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, 35591d82de61SMel Gorman sc.gfp_mask, &nr_soft_scanned); 35600608f43dSAndrew Morton sc.nr_reclaimed += nr_soft_reclaimed; 35610608f43dSAndrew Morton 356232a4330dSRik van Riel /* 35631d82de61SMel Gorman * There should be no need to raise the scanning priority if 35641d82de61SMel Gorman * enough pages are already being scanned that that high 35651d82de61SMel Gorman * watermark would be met at 100% efficiency. 356632a4330dSRik van Riel */ 3567970a39a3SMel Gorman if (kswapd_shrink_node(pgdat, &sc)) 3568b8e83b94SMel Gorman raise_priority = false; 3569d7868daeSMel Gorman 35705515061dSMel Gorman /* 35715515061dSMel Gorman * If the low watermark is met there is no need for processes 35725515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 35735515061dSMel Gorman * able to safely make forward progress. Wake them 35745515061dSMel Gorman */ 35755515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 3576c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)) 3577cfc51155SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 35785515061dSMel Gorman 3579b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 358093781325SOmar Sandoval __fs_reclaim_release(); 358193781325SOmar Sandoval ret = try_to_freeze(); 358293781325SOmar Sandoval __fs_reclaim_acquire(); 358393781325SOmar Sandoval if (ret || kthread_should_stop()) 3584b8e83b94SMel Gorman break; 3585b8e83b94SMel Gorman 3586b8e83b94SMel Gorman /* 3587b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 3588b8e83b94SMel Gorman * progress in reclaiming pages 3589b8e83b94SMel Gorman */ 3590c73322d0SJohannes Weiner nr_reclaimed = sc.nr_reclaimed - nr_reclaimed; 3591c73322d0SJohannes Weiner if (raise_priority || !nr_reclaimed) 3592b8e83b94SMel Gorman sc.priority--; 35931d82de61SMel Gorman } while (sc.priority >= 1); 35941da177e4SLinus Torvalds 3595c73322d0SJohannes Weiner if (!sc.nr_reclaimed) 3596c73322d0SJohannes Weiner pgdat->kswapd_failures++; 3597c73322d0SJohannes Weiner 3598b8e83b94SMel Gorman out: 35992a2e4885SJohannes Weiner snapshot_refaults(NULL, pgdat); 360093781325SOmar Sandoval __fs_reclaim_release(); 36010abdee2bSMel Gorman /* 36021d82de61SMel Gorman * Return the order kswapd stopped reclaiming at as 36031d82de61SMel Gorman * prepare_kswapd_sleep() takes it into account. If another caller 36041d82de61SMel Gorman * entered the allocator slow path while kswapd was awake, order will 36051d82de61SMel Gorman * remain at the higher level. 36060abdee2bSMel Gorman */ 36071d82de61SMel Gorman return sc.order; 36081da177e4SLinus Torvalds } 36091da177e4SLinus Torvalds 3610e716f2ebSMel Gorman /* 3611e716f2ebSMel Gorman * pgdat->kswapd_classzone_idx is the highest zone index that a recent 3612e716f2ebSMel Gorman * allocation request woke kswapd for. When kswapd has not woken recently, 3613e716f2ebSMel Gorman * the value is MAX_NR_ZONES which is not a valid index. This compares a 3614e716f2ebSMel Gorman * given classzone and returns it or the highest classzone index kswapd 3615e716f2ebSMel Gorman * was recently woke for. 3616e716f2ebSMel Gorman */ 3617e716f2ebSMel Gorman static enum zone_type kswapd_classzone_idx(pg_data_t *pgdat, 3618e716f2ebSMel Gorman enum zone_type classzone_idx) 3619e716f2ebSMel Gorman { 3620e716f2ebSMel Gorman if (pgdat->kswapd_classzone_idx == MAX_NR_ZONES) 3621e716f2ebSMel Gorman return classzone_idx; 3622e716f2ebSMel Gorman 3623e716f2ebSMel Gorman return max(pgdat->kswapd_classzone_idx, classzone_idx); 3624e716f2ebSMel Gorman } 3625e716f2ebSMel Gorman 362638087d9bSMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, 362738087d9bSMel Gorman unsigned int classzone_idx) 3628f0bc0a60SKOSAKI Motohiro { 3629f0bc0a60SKOSAKI Motohiro long remaining = 0; 3630f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 3631f0bc0a60SKOSAKI Motohiro 3632f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 3633f0bc0a60SKOSAKI Motohiro return; 3634f0bc0a60SKOSAKI Motohiro 3635f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3636f0bc0a60SKOSAKI Motohiro 3637333b0a45SShantanu Goel /* 3638333b0a45SShantanu Goel * Try to sleep for a short interval. Note that kcompactd will only be 3639333b0a45SShantanu Goel * woken if it is possible to sleep for a short interval. This is 3640333b0a45SShantanu Goel * deliberate on the assumption that if reclaim cannot keep an 3641333b0a45SShantanu Goel * eligible zone balanced that it's also unlikely that compaction will 3642333b0a45SShantanu Goel * succeed. 3643333b0a45SShantanu Goel */ 3644d9f21d42SMel Gorman if (prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) { 3645fd901c95SVlastimil Babka /* 3646fd901c95SVlastimil Babka * Compaction records what page blocks it recently failed to 3647fd901c95SVlastimil Babka * isolate pages from and skips them in the future scanning. 3648fd901c95SVlastimil Babka * When kswapd is going to sleep, it is reasonable to assume 3649fd901c95SVlastimil Babka * that pages and compaction may succeed so reset the cache. 3650fd901c95SVlastimil Babka */ 3651fd901c95SVlastimil Babka reset_isolation_suitable(pgdat); 3652fd901c95SVlastimil Babka 3653fd901c95SVlastimil Babka /* 3654fd901c95SVlastimil Babka * We have freed the memory, now we should compact it to make 3655fd901c95SVlastimil Babka * allocation of the requested order possible. 3656fd901c95SVlastimil Babka */ 365738087d9bSMel Gorman wakeup_kcompactd(pgdat, alloc_order, classzone_idx); 3658fd901c95SVlastimil Babka 3659f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 366038087d9bSMel Gorman 366138087d9bSMel Gorman /* 366238087d9bSMel Gorman * If woken prematurely then reset kswapd_classzone_idx and 366338087d9bSMel Gorman * order. The values will either be from a wakeup request or 366438087d9bSMel Gorman * the previous request that slept prematurely. 366538087d9bSMel Gorman */ 366638087d9bSMel Gorman if (remaining) { 3667e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx); 366838087d9bSMel Gorman pgdat->kswapd_order = max(pgdat->kswapd_order, reclaim_order); 366938087d9bSMel Gorman } 367038087d9bSMel Gorman 3671f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3672f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3673f0bc0a60SKOSAKI Motohiro } 3674f0bc0a60SKOSAKI Motohiro 3675f0bc0a60SKOSAKI Motohiro /* 3676f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 3677f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 3678f0bc0a60SKOSAKI Motohiro */ 3679d9f21d42SMel Gorman if (!remaining && 3680d9f21d42SMel Gorman prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) { 3681f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 3682f0bc0a60SKOSAKI Motohiro 3683f0bc0a60SKOSAKI Motohiro /* 3684f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 3685f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 3686f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 3687f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 3688f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 3689f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 3690f0bc0a60SKOSAKI Motohiro */ 3691f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 36921c7e7f6cSAaditya Kumar 36931c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 3694f0bc0a60SKOSAKI Motohiro schedule(); 36951c7e7f6cSAaditya Kumar 3696f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 3697f0bc0a60SKOSAKI Motohiro } else { 3698f0bc0a60SKOSAKI Motohiro if (remaining) 3699f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 3700f0bc0a60SKOSAKI Motohiro else 3701f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 3702f0bc0a60SKOSAKI Motohiro } 3703f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3704f0bc0a60SKOSAKI Motohiro } 3705f0bc0a60SKOSAKI Motohiro 37061da177e4SLinus Torvalds /* 37071da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 37081da177e4SLinus Torvalds * from the init process. 37091da177e4SLinus Torvalds * 37101da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 37111da177e4SLinus Torvalds * free memory available even if there is no other activity 37121da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 37131da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 37141da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 37151da177e4SLinus Torvalds * 37161da177e4SLinus Torvalds * If there are applications that are active memory-allocators 37171da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 37181da177e4SLinus Torvalds */ 37191da177e4SLinus Torvalds static int kswapd(void *p) 37201da177e4SLinus Torvalds { 3721e716f2ebSMel Gorman unsigned int alloc_order, reclaim_order; 3722e716f2ebSMel Gorman unsigned int classzone_idx = MAX_NR_ZONES - 1; 37231da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 37241da177e4SLinus Torvalds struct task_struct *tsk = current; 3725f0bc0a60SKOSAKI Motohiro 37261da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 37271da177e4SLinus Torvalds .reclaimed_slab = 0, 37281da177e4SLinus Torvalds }; 3729a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 37301da177e4SLinus Torvalds 3731174596a0SRusty Russell if (!cpumask_empty(cpumask)) 3732c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 37331da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 37341da177e4SLinus Torvalds 37351da177e4SLinus Torvalds /* 37361da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 37371da177e4SLinus Torvalds * and that if we need more memory we should get access to it 37381da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 37391da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 37401da177e4SLinus Torvalds * 37411da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 37421da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 37431da177e4SLinus Torvalds * page out something else, and this flag essentially protects 37441da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 37451da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 37461da177e4SLinus Torvalds */ 3747930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 374883144186SRafael J. Wysocki set_freezable(); 37491da177e4SLinus Torvalds 3750e716f2ebSMel Gorman pgdat->kswapd_order = 0; 3751e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = MAX_NR_ZONES; 37521da177e4SLinus Torvalds for ( ; ; ) { 37536f6313d4SJeff Liu bool ret; 37543e1d1d28SChristoph Lameter 3755e716f2ebSMel Gorman alloc_order = reclaim_order = pgdat->kswapd_order; 3756e716f2ebSMel Gorman classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx); 3757e716f2ebSMel Gorman 375838087d9bSMel Gorman kswapd_try_sleep: 375938087d9bSMel Gorman kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, 376038087d9bSMel Gorman classzone_idx); 3761215ddd66SMel Gorman 376238087d9bSMel Gorman /* Read the new order and classzone_idx */ 376338087d9bSMel Gorman alloc_order = reclaim_order = pgdat->kswapd_order; 3764e716f2ebSMel Gorman classzone_idx = kswapd_classzone_idx(pgdat, 0); 376538087d9bSMel Gorman pgdat->kswapd_order = 0; 3766e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = MAX_NR_ZONES; 37671da177e4SLinus Torvalds 37688fe23e05SDavid Rientjes ret = try_to_freeze(); 37698fe23e05SDavid Rientjes if (kthread_should_stop()) 37708fe23e05SDavid Rientjes break; 37718fe23e05SDavid Rientjes 37728fe23e05SDavid Rientjes /* 37738fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 37748fe23e05SDavid Rientjes * after returning from the refrigerator 3775b1296cc4SRafael J. Wysocki */ 377638087d9bSMel Gorman if (ret) 377738087d9bSMel Gorman continue; 37781d82de61SMel Gorman 377938087d9bSMel Gorman /* 378038087d9bSMel Gorman * Reclaim begins at the requested order but if a high-order 378138087d9bSMel Gorman * reclaim fails then kswapd falls back to reclaiming for 378238087d9bSMel Gorman * order-0. If that happens, kswapd will consider sleeping 378338087d9bSMel Gorman * for the order it finished reclaiming at (reclaim_order) 378438087d9bSMel Gorman * but kcompactd is woken to compact for the original 378538087d9bSMel Gorman * request (alloc_order). 378638087d9bSMel Gorman */ 3787e5146b12SMel Gorman trace_mm_vmscan_kswapd_wake(pgdat->node_id, classzone_idx, 3788e5146b12SMel Gorman alloc_order); 378938087d9bSMel Gorman reclaim_order = balance_pgdat(pgdat, alloc_order, classzone_idx); 379038087d9bSMel Gorman if (reclaim_order < alloc_order) 379138087d9bSMel Gorman goto kswapd_try_sleep; 379233906bc5SMel Gorman } 3793b0a8cc58STakamori Yamaguchi 379471abdc15SJohannes Weiner tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); 3795b0a8cc58STakamori Yamaguchi current->reclaim_state = NULL; 379671abdc15SJohannes Weiner 37971da177e4SLinus Torvalds return 0; 37981da177e4SLinus Torvalds } 37991da177e4SLinus Torvalds 38001da177e4SLinus Torvalds /* 38015ecd9d40SDavid Rientjes * A zone is low on free memory or too fragmented for high-order memory. If 38025ecd9d40SDavid Rientjes * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's 38035ecd9d40SDavid Rientjes * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim 38045ecd9d40SDavid Rientjes * has failed or is not needed, still wake up kcompactd if only compaction is 38055ecd9d40SDavid Rientjes * needed. 38061da177e4SLinus Torvalds */ 38075ecd9d40SDavid Rientjes void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, 38085ecd9d40SDavid Rientjes enum zone_type classzone_idx) 38091da177e4SLinus Torvalds { 38101da177e4SLinus Torvalds pg_data_t *pgdat; 38111da177e4SLinus Torvalds 38126aa303deSMel Gorman if (!managed_zone(zone)) 38131da177e4SLinus Torvalds return; 38141da177e4SLinus Torvalds 38155ecd9d40SDavid Rientjes if (!cpuset_zone_allowed(zone, gfp_flags)) 38161da177e4SLinus Torvalds return; 381788f5acf8SMel Gorman pgdat = zone->zone_pgdat; 3818e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat, 3819e716f2ebSMel Gorman classzone_idx); 382038087d9bSMel Gorman pgdat->kswapd_order = max(pgdat->kswapd_order, order); 38218d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 38221da177e4SLinus Torvalds return; 3823e1a55637SMel Gorman 38245ecd9d40SDavid Rientjes /* Hopeless node, leave it to direct reclaim if possible */ 38255ecd9d40SDavid Rientjes if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES || 38265ecd9d40SDavid Rientjes pgdat_balanced(pgdat, order, classzone_idx)) { 38275ecd9d40SDavid Rientjes /* 38285ecd9d40SDavid Rientjes * There may be plenty of free memory available, but it's too 38295ecd9d40SDavid Rientjes * fragmented for high-order allocations. Wake up kcompactd 38305ecd9d40SDavid Rientjes * and rely on compaction_suitable() to determine if it's 38315ecd9d40SDavid Rientjes * needed. If it fails, it will defer subsequent attempts to 38325ecd9d40SDavid Rientjes * ratelimit its work. 38335ecd9d40SDavid Rientjes */ 38345ecd9d40SDavid Rientjes if (!(gfp_flags & __GFP_DIRECT_RECLAIM)) 38355ecd9d40SDavid Rientjes wakeup_kcompactd(pgdat, order, classzone_idx); 3836c73322d0SJohannes Weiner return; 38375ecd9d40SDavid Rientjes } 3838c73322d0SJohannes Weiner 38395ecd9d40SDavid Rientjes trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, classzone_idx, order, 38405ecd9d40SDavid Rientjes gfp_flags); 38418d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 38421da177e4SLinus Torvalds } 38431da177e4SLinus Torvalds 3844c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 38451da177e4SLinus Torvalds /* 38467b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 3847d6277db4SRafael J. Wysocki * freed pages. 3848d6277db4SRafael J. Wysocki * 3849d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 3850d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 3851d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 38521da177e4SLinus Torvalds */ 38537b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 38541da177e4SLinus Torvalds { 3855d6277db4SRafael J. Wysocki struct reclaim_state reclaim_state; 3856d6277db4SRafael J. Wysocki struct scan_control sc = { 38577b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 3858ee814fe2SJohannes Weiner .gfp_mask = GFP_HIGHUSER_MOVABLE, 3859b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 38609e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 3861ee814fe2SJohannes Weiner .may_writepage = 1, 3862ee814fe2SJohannes Weiner .may_unmap = 1, 3863ee814fe2SJohannes Weiner .may_swap = 1, 3864ee814fe2SJohannes Weiner .hibernation_mode = 1, 38651da177e4SLinus Torvalds }; 38667b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 38677b51755cSKOSAKI Motohiro struct task_struct *p = current; 38687b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 3869499118e9SVlastimil Babka unsigned int noreclaim_flag; 38701da177e4SLinus Torvalds 3871d92a8cfcSPeter Zijlstra fs_reclaim_acquire(sc.gfp_mask); 387293781325SOmar Sandoval noreclaim_flag = memalloc_noreclaim_save(); 3873d6277db4SRafael J. Wysocki reclaim_state.reclaimed_slab = 0; 38747b51755cSKOSAKI Motohiro p->reclaim_state = &reclaim_state; 3875d6277db4SRafael J. Wysocki 38763115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3877d6277db4SRafael J. Wysocki 38787b51755cSKOSAKI Motohiro p->reclaim_state = NULL; 3879499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 388093781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 3881d6277db4SRafael J. Wysocki 38827b51755cSKOSAKI Motohiro return nr_reclaimed; 38831da177e4SLinus Torvalds } 3884c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 38851da177e4SLinus Torvalds 38861da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 38871da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 38881da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 38891da177e4SLinus Torvalds restore their cpu bindings. */ 3890517bbed9SSebastian Andrzej Siewior static int kswapd_cpu_online(unsigned int cpu) 38911da177e4SLinus Torvalds { 389258c0a4a7SYasunori Goto int nid; 38931da177e4SLinus Torvalds 389448fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) { 3895c5f59f08SMike Travis pg_data_t *pgdat = NODE_DATA(nid); 3896a70f7302SRusty Russell const struct cpumask *mask; 3897a70f7302SRusty Russell 3898a70f7302SRusty Russell mask = cpumask_of_node(pgdat->node_id); 3899c5f59f08SMike Travis 39003e597945SRusty Russell if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 39011da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 3902c5f59f08SMike Travis set_cpus_allowed_ptr(pgdat->kswapd, mask); 39031da177e4SLinus Torvalds } 3904517bbed9SSebastian Andrzej Siewior return 0; 39051da177e4SLinus Torvalds } 39061da177e4SLinus Torvalds 39073218ae14SYasunori Goto /* 39083218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 39093218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 39103218ae14SYasunori Goto */ 39113218ae14SYasunori Goto int kswapd_run(int nid) 39123218ae14SYasunori Goto { 39133218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 39143218ae14SYasunori Goto int ret = 0; 39153218ae14SYasunori Goto 39163218ae14SYasunori Goto if (pgdat->kswapd) 39173218ae14SYasunori Goto return 0; 39183218ae14SYasunori Goto 39193218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 39203218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 39213218ae14SYasunori Goto /* failure at boot is fatal */ 3922c6202adfSThomas Gleixner BUG_ON(system_state < SYSTEM_RUNNING); 3923d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 3924d5dc0ad9SGavin Shan ret = PTR_ERR(pgdat->kswapd); 3925d72515b8SXishi Qiu pgdat->kswapd = NULL; 39263218ae14SYasunori Goto } 39273218ae14SYasunori Goto return ret; 39283218ae14SYasunori Goto } 39293218ae14SYasunori Goto 39308fe23e05SDavid Rientjes /* 3931d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 3932bfc8c901SVladimir Davydov * hold mem_hotplug_begin/end(). 39338fe23e05SDavid Rientjes */ 39348fe23e05SDavid Rientjes void kswapd_stop(int nid) 39358fe23e05SDavid Rientjes { 39368fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 39378fe23e05SDavid Rientjes 3938d8adde17SJiang Liu if (kswapd) { 39398fe23e05SDavid Rientjes kthread_stop(kswapd); 3940d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 3941d8adde17SJiang Liu } 39428fe23e05SDavid Rientjes } 39438fe23e05SDavid Rientjes 39441da177e4SLinus Torvalds static int __init kswapd_init(void) 39451da177e4SLinus Torvalds { 3946517bbed9SSebastian Andrzej Siewior int nid, ret; 394769e05944SAndrew Morton 39481da177e4SLinus Torvalds swap_setup(); 394948fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 39503218ae14SYasunori Goto kswapd_run(nid); 3951517bbed9SSebastian Andrzej Siewior ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, 3952517bbed9SSebastian Andrzej Siewior "mm/vmscan:online", kswapd_cpu_online, 3953517bbed9SSebastian Andrzej Siewior NULL); 3954517bbed9SSebastian Andrzej Siewior WARN_ON(ret < 0); 39551da177e4SLinus Torvalds return 0; 39561da177e4SLinus Torvalds } 39571da177e4SLinus Torvalds 39581da177e4SLinus Torvalds module_init(kswapd_init) 39599eeff239SChristoph Lameter 39609eeff239SChristoph Lameter #ifdef CONFIG_NUMA 39619eeff239SChristoph Lameter /* 3962a5f5f91dSMel Gorman * Node reclaim mode 39639eeff239SChristoph Lameter * 3964a5f5f91dSMel Gorman * If non-zero call node_reclaim when the number of free pages falls below 39659eeff239SChristoph Lameter * the watermarks. 39669eeff239SChristoph Lameter */ 3967a5f5f91dSMel Gorman int node_reclaim_mode __read_mostly; 39689eeff239SChristoph Lameter 39691b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 39707d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ 39711b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 397295bbc0c7SZhihui Zhang #define RECLAIM_UNMAP (1<<2) /* Unmap pages during reclaim */ 39731b2ffb78SChristoph Lameter 39749eeff239SChristoph Lameter /* 3975a5f5f91dSMel Gorman * Priority for NODE_RECLAIM. This determines the fraction of pages 3976a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 3977a92f7126SChristoph Lameter * a zone. 3978a92f7126SChristoph Lameter */ 3979a5f5f91dSMel Gorman #define NODE_RECLAIM_PRIORITY 4 3980a92f7126SChristoph Lameter 39819eeff239SChristoph Lameter /* 3982a5f5f91dSMel Gorman * Percentage of pages in a zone that must be unmapped for node_reclaim to 39839614634fSChristoph Lameter * occur. 39849614634fSChristoph Lameter */ 39859614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 39869614634fSChristoph Lameter 39879614634fSChristoph Lameter /* 39880ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 39890ff38490SChristoph Lameter * slab reclaim needs to occur. 39900ff38490SChristoph Lameter */ 39910ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 39920ff38490SChristoph Lameter 399311fb9989SMel Gorman static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) 399490afa5deSMel Gorman { 399511fb9989SMel Gorman unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); 399611fb9989SMel Gorman unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + 399711fb9989SMel Gorman node_page_state(pgdat, NR_ACTIVE_FILE); 399890afa5deSMel Gorman 399990afa5deSMel Gorman /* 400090afa5deSMel Gorman * It's possible for there to be more file mapped pages than 400190afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 400290afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 400390afa5deSMel Gorman */ 400490afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 400590afa5deSMel Gorman } 400690afa5deSMel Gorman 400790afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 4008a5f5f91dSMel Gorman static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) 400990afa5deSMel Gorman { 4010d031a157SAlexandru Moise unsigned long nr_pagecache_reclaimable; 4011d031a157SAlexandru Moise unsigned long delta = 0; 401290afa5deSMel Gorman 401390afa5deSMel Gorman /* 401495bbc0c7SZhihui Zhang * If RECLAIM_UNMAP is set, then all file pages are considered 401590afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 401611fb9989SMel Gorman * pages like swapcache and node_unmapped_file_pages() provides 401790afa5deSMel Gorman * a better estimate 401890afa5deSMel Gorman */ 4019a5f5f91dSMel Gorman if (node_reclaim_mode & RECLAIM_UNMAP) 4020a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); 402190afa5deSMel Gorman else 4022a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); 402390afa5deSMel Gorman 402490afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 4025a5f5f91dSMel Gorman if (!(node_reclaim_mode & RECLAIM_WRITE)) 4026a5f5f91dSMel Gorman delta += node_page_state(pgdat, NR_FILE_DIRTY); 402790afa5deSMel Gorman 402890afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 402990afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 403090afa5deSMel Gorman delta = nr_pagecache_reclaimable; 403190afa5deSMel Gorman 403290afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 403390afa5deSMel Gorman } 403490afa5deSMel Gorman 40350ff38490SChristoph Lameter /* 4036a5f5f91dSMel Gorman * Try to free up some pages from this node through reclaim. 40379eeff239SChristoph Lameter */ 4038a5f5f91dSMel Gorman static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 40399eeff239SChristoph Lameter { 40407fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 404169e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 40429eeff239SChristoph Lameter struct task_struct *p = current; 40439eeff239SChristoph Lameter struct reclaim_state reclaim_state; 4044499118e9SVlastimil Babka unsigned int noreclaim_flag; 4045179e9639SAndrew Morton struct scan_control sc = { 404662b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 4047f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 4048bd2f6199SJohannes Weiner .order = order, 4049a5f5f91dSMel Gorman .priority = NODE_RECLAIM_PRIORITY, 4050a5f5f91dSMel Gorman .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), 4051a5f5f91dSMel Gorman .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), 4052ee814fe2SJohannes Weiner .may_swap = 1, 4053f2f43e56SNick Desaulniers .reclaim_idx = gfp_zone(gfp_mask), 4054179e9639SAndrew Morton }; 40559eeff239SChristoph Lameter 40569eeff239SChristoph Lameter cond_resched(); 405793781325SOmar Sandoval fs_reclaim_acquire(sc.gfp_mask); 4058d4f7796eSChristoph Lameter /* 405995bbc0c7SZhihui Zhang * We need to be able to allocate from the reserves for RECLAIM_UNMAP 4060d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 406195bbc0c7SZhihui Zhang * and RECLAIM_UNMAP. 4062d4f7796eSChristoph Lameter */ 4063499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 4064499118e9SVlastimil Babka p->flags |= PF_SWAPWRITE; 40659eeff239SChristoph Lameter reclaim_state.reclaimed_slab = 0; 40669eeff239SChristoph Lameter p->reclaim_state = &reclaim_state; 4067c84db23cSChristoph Lameter 4068a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) { 4069a92f7126SChristoph Lameter /* 4070894befecSAndrey Ryabinin * Free memory by calling shrink node with increasing 40710ff38490SChristoph Lameter * priorities until we have enough memory freed. 4072a92f7126SChristoph Lameter */ 4073a92f7126SChristoph Lameter do { 4074970a39a3SMel Gorman shrink_node(pgdat, &sc); 40759e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 40760ff38490SChristoph Lameter } 4077a92f7126SChristoph Lameter 40789eeff239SChristoph Lameter p->reclaim_state = NULL; 4079499118e9SVlastimil Babka current->flags &= ~PF_SWAPWRITE; 4080499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 408193781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 4082a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 40839eeff239SChristoph Lameter } 4084179e9639SAndrew Morton 4085a5f5f91dSMel Gorman int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 4086179e9639SAndrew Morton { 4087d773ed6bSDavid Rientjes int ret; 4088179e9639SAndrew Morton 4089179e9639SAndrew Morton /* 4090a5f5f91dSMel Gorman * Node reclaim reclaims unmapped file backed pages and 40910ff38490SChristoph Lameter * slab pages if we are over the defined limits. 409234aa1330SChristoph Lameter * 40939614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 40949614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 4095a5f5f91dSMel Gorman * thrown out if the node is overallocated. So we do not reclaim 4096a5f5f91dSMel Gorman * if less than a specified percentage of the node is used by 40979614634fSChristoph Lameter * unmapped file backed pages. 4098179e9639SAndrew Morton */ 4099a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && 4100385386cfSJohannes Weiner node_page_state(pgdat, NR_SLAB_RECLAIMABLE) <= pgdat->min_slab_pages) 4101a5f5f91dSMel Gorman return NODE_RECLAIM_FULL; 4102179e9639SAndrew Morton 4103179e9639SAndrew Morton /* 4104d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 4105179e9639SAndrew Morton */ 4106d0164adcSMel Gorman if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) 4107a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4108179e9639SAndrew Morton 4109179e9639SAndrew Morton /* 4110a5f5f91dSMel Gorman * Only run node reclaim on the local node or on nodes that do not 4111179e9639SAndrew Morton * have associated processors. This will favor the local processor 4112179e9639SAndrew Morton * over remote processors and spread off node memory allocations 4113179e9639SAndrew Morton * as wide as possible. 4114179e9639SAndrew Morton */ 4115a5f5f91dSMel Gorman if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) 4116a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4117d773ed6bSDavid Rientjes 4118a5f5f91dSMel Gorman if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) 4119a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4120fa5e084eSMel Gorman 4121a5f5f91dSMel Gorman ret = __node_reclaim(pgdat, gfp_mask, order); 4122a5f5f91dSMel Gorman clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); 4123d773ed6bSDavid Rientjes 412424cf7251SMel Gorman if (!ret) 412524cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 412624cf7251SMel Gorman 4127d773ed6bSDavid Rientjes return ret; 4128179e9639SAndrew Morton } 41299eeff239SChristoph Lameter #endif 4130894bc310SLee Schermerhorn 4131894bc310SLee Schermerhorn /* 4132894bc310SLee Schermerhorn * page_evictable - test whether a page is evictable 4133894bc310SLee Schermerhorn * @page: the page to test 4134894bc310SLee Schermerhorn * 4135894bc310SLee Schermerhorn * Test whether page is evictable--i.e., should be placed on active/inactive 413639b5f29aSHugh Dickins * lists vs unevictable list. 4137894bc310SLee Schermerhorn * 4138894bc310SLee Schermerhorn * Reasons page might not be evictable: 4139ba9ddf49SLee Schermerhorn * (1) page's mapping marked unevictable 4140b291f000SNick Piggin * (2) page is part of an mlocked VMA 4141ba9ddf49SLee Schermerhorn * 4142894bc310SLee Schermerhorn */ 414339b5f29aSHugh Dickins int page_evictable(struct page *page) 4144894bc310SLee Schermerhorn { 4145e92bb4ddSHuang Ying int ret; 4146e92bb4ddSHuang Ying 4147e92bb4ddSHuang Ying /* Prevent address_space of inode and swap cache from being freed */ 4148e92bb4ddSHuang Ying rcu_read_lock(); 4149e92bb4ddSHuang Ying ret = !mapping_unevictable(page_mapping(page)) && !PageMlocked(page); 4150e92bb4ddSHuang Ying rcu_read_unlock(); 4151e92bb4ddSHuang Ying return ret; 4152894bc310SLee Schermerhorn } 415389e004eaSLee Schermerhorn 415485046579SHugh Dickins #ifdef CONFIG_SHMEM 415589e004eaSLee Schermerhorn /** 415624513264SHugh Dickins * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list 415724513264SHugh Dickins * @pages: array of pages to check 415824513264SHugh Dickins * @nr_pages: number of pages to check 415989e004eaSLee Schermerhorn * 416024513264SHugh Dickins * Checks pages for evictability and moves them to the appropriate lru list. 416185046579SHugh Dickins * 416285046579SHugh Dickins * This function is only used for SysV IPC SHM_UNLOCK. 416389e004eaSLee Schermerhorn */ 416424513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages) 416589e004eaSLee Schermerhorn { 4166925b7673SJohannes Weiner struct lruvec *lruvec; 4167785b99feSMel Gorman struct pglist_data *pgdat = NULL; 416824513264SHugh Dickins int pgscanned = 0; 416924513264SHugh Dickins int pgrescued = 0; 417089e004eaSLee Schermerhorn int i; 417189e004eaSLee Schermerhorn 417224513264SHugh Dickins for (i = 0; i < nr_pages; i++) { 417324513264SHugh Dickins struct page *page = pages[i]; 4174785b99feSMel Gorman struct pglist_data *pagepgdat = page_pgdat(page); 417589e004eaSLee Schermerhorn 417624513264SHugh Dickins pgscanned++; 4177785b99feSMel Gorman if (pagepgdat != pgdat) { 4178785b99feSMel Gorman if (pgdat) 4179785b99feSMel Gorman spin_unlock_irq(&pgdat->lru_lock); 4180785b99feSMel Gorman pgdat = pagepgdat; 4181785b99feSMel Gorman spin_lock_irq(&pgdat->lru_lock); 418289e004eaSLee Schermerhorn } 4183785b99feSMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 418489e004eaSLee Schermerhorn 418524513264SHugh Dickins if (!PageLRU(page) || !PageUnevictable(page)) 418624513264SHugh Dickins continue; 418789e004eaSLee Schermerhorn 418839b5f29aSHugh Dickins if (page_evictable(page)) { 418924513264SHugh Dickins enum lru_list lru = page_lru_base_type(page); 419024513264SHugh Dickins 4191309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 419224513264SHugh Dickins ClearPageUnevictable(page); 4193fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE); 4194fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 419524513264SHugh Dickins pgrescued++; 419689e004eaSLee Schermerhorn } 419789e004eaSLee Schermerhorn } 419824513264SHugh Dickins 4199785b99feSMel Gorman if (pgdat) { 420024513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 420124513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 4202785b99feSMel Gorman spin_unlock_irq(&pgdat->lru_lock); 420324513264SHugh Dickins } 420485046579SHugh Dickins } 420585046579SHugh Dickins #endif /* CONFIG_SHMEM */ 4206