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> 4964e3d12fSKuo-Hsin Yang #include <linux/pagevec.h> 50268bb0ceSLinus Torvalds #include <linux/prefetch.h> 51b1de0d13SMitchel Humpherys #include <linux/printk.h> 52f9fe48beSRoss Zwisler #include <linux/dax.h> 53eb414681SJohannes Weiner #include <linux/psi.h> 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds #include <asm/tlbflush.h> 561da177e4SLinus Torvalds #include <asm/div64.h> 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds #include <linux/swapops.h> 59117aad1eSRafael Aquini #include <linux/balloon_compaction.h> 601da177e4SLinus Torvalds 610f8053a5SNick Piggin #include "internal.h" 620f8053a5SNick Piggin 6333906bc5SMel Gorman #define CREATE_TRACE_POINTS 6433906bc5SMel Gorman #include <trace/events/vmscan.h> 6533906bc5SMel Gorman 661da177e4SLinus Torvalds struct scan_control { 6722fba335SKOSAKI Motohiro /* How many pages shrink_list() should reclaim */ 6822fba335SKOSAKI Motohiro unsigned long nr_to_reclaim; 6922fba335SKOSAKI Motohiro 70ee814fe2SJohannes Weiner /* 71ee814fe2SJohannes Weiner * Nodemask of nodes allowed by the caller. If NULL, all nodes 72ee814fe2SJohannes Weiner * are scanned. 73ee814fe2SJohannes Weiner */ 74ee814fe2SJohannes Weiner nodemask_t *nodemask; 759e3b2f8cSKonstantin Khlebnikov 765f53e762SKOSAKI Motohiro /* 77f16015fbSJohannes Weiner * The memory cgroup that hit its limit and as a result is the 78f16015fbSJohannes Weiner * primary target of this reclaim invocation. 79f16015fbSJohannes Weiner */ 80f16015fbSJohannes Weiner struct mem_cgroup *target_mem_cgroup; 8166e1707bSBalbir Singh 821276ad68SJohannes Weiner /* Writepage batching in laptop mode; RECLAIM_WRITE */ 83ee814fe2SJohannes Weiner unsigned int may_writepage:1; 84ee814fe2SJohannes Weiner 85ee814fe2SJohannes Weiner /* Can mapped pages be reclaimed? */ 86ee814fe2SJohannes Weiner unsigned int may_unmap:1; 87ee814fe2SJohannes Weiner 88ee814fe2SJohannes Weiner /* Can pages be swapped as part of reclaim? */ 89ee814fe2SJohannes Weiner unsigned int may_swap:1; 90ee814fe2SJohannes Weiner 911c30844dSMel Gorman /* e.g. boosted watermark reclaim leaves slabs alone */ 921c30844dSMel Gorman unsigned int may_shrinkslab:1; 931c30844dSMel Gorman 94d6622f63SYisheng Xie /* 95d6622f63SYisheng Xie * Cgroups are not reclaimed below their configured memory.low, 96d6622f63SYisheng Xie * unless we threaten to OOM. If any cgroups are skipped due to 97d6622f63SYisheng Xie * memory.low and nothing was reclaimed, go back for memory.low. 98d6622f63SYisheng Xie */ 99d6622f63SYisheng Xie unsigned int memcg_low_reclaim:1; 100d6622f63SYisheng Xie unsigned int memcg_low_skipped:1; 101241994edSJohannes Weiner 102ee814fe2SJohannes Weiner unsigned int hibernation_mode:1; 103ee814fe2SJohannes Weiner 104ee814fe2SJohannes Weiner /* One of the zones is ready for compaction */ 105ee814fe2SJohannes Weiner unsigned int compaction_ready:1; 106ee814fe2SJohannes Weiner 107bb451fdfSGreg Thelen /* Allocation order */ 108bb451fdfSGreg Thelen s8 order; 109bb451fdfSGreg Thelen 110bb451fdfSGreg Thelen /* Scan (total_size >> priority) pages at once */ 111bb451fdfSGreg Thelen s8 priority; 112bb451fdfSGreg Thelen 113bb451fdfSGreg Thelen /* The highest zone to isolate pages for reclaim from */ 114bb451fdfSGreg Thelen s8 reclaim_idx; 115bb451fdfSGreg Thelen 116bb451fdfSGreg Thelen /* This context's GFP mask */ 117bb451fdfSGreg Thelen gfp_t gfp_mask; 118bb451fdfSGreg Thelen 119ee814fe2SJohannes Weiner /* Incremented by the number of inactive pages that were scanned */ 120ee814fe2SJohannes Weiner unsigned long nr_scanned; 121ee814fe2SJohannes Weiner 122ee814fe2SJohannes Weiner /* Number of pages freed so far during a call to shrink_zones() */ 123ee814fe2SJohannes Weiner unsigned long nr_reclaimed; 124d108c772SAndrey Ryabinin 125d108c772SAndrey Ryabinin struct { 126d108c772SAndrey Ryabinin unsigned int dirty; 127d108c772SAndrey Ryabinin unsigned int unqueued_dirty; 128d108c772SAndrey Ryabinin unsigned int congested; 129d108c772SAndrey Ryabinin unsigned int writeback; 130d108c772SAndrey Ryabinin unsigned int immediate; 131d108c772SAndrey Ryabinin unsigned int file_taken; 132d108c772SAndrey Ryabinin unsigned int taken; 133d108c772SAndrey Ryabinin } nr; 134*e5ca8071SYafang Shao 135*e5ca8071SYafang Shao /* for recording the reclaimed slab by now */ 136*e5ca8071SYafang Shao struct reclaim_state reclaim_state; 1371da177e4SLinus Torvalds }; 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH 1401da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) \ 1411da177e4SLinus Torvalds do { \ 1421da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1431da177e4SLinus Torvalds struct page *prev; \ 1441da177e4SLinus Torvalds \ 1451da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1461da177e4SLinus Torvalds prefetch(&prev->_field); \ 1471da177e4SLinus Torvalds } \ 1481da177e4SLinus Torvalds } while (0) 1491da177e4SLinus Torvalds #else 1501da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) 1511da177e4SLinus Torvalds #endif 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1541da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1551da177e4SLinus Torvalds do { \ 1561da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1571da177e4SLinus Torvalds struct page *prev; \ 1581da177e4SLinus Torvalds \ 1591da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1601da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1611da177e4SLinus Torvalds } \ 1621da177e4SLinus Torvalds } while (0) 1631da177e4SLinus Torvalds #else 1641da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1651da177e4SLinus Torvalds #endif 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds /* 1681da177e4SLinus Torvalds * From 0 .. 100. Higher means more swappy. 1691da177e4SLinus Torvalds */ 1701da177e4SLinus Torvalds int vm_swappiness = 60; 171d0480be4SWang Sheng-Hui /* 172d0480be4SWang Sheng-Hui * The total number of pages which are beyond the high watermark within all 173d0480be4SWang Sheng-Hui * zones. 174d0480be4SWang Sheng-Hui */ 175d0480be4SWang Sheng-Hui unsigned long vm_total_pages; 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1781da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1791da177e4SLinus Torvalds 180b4c2b231SKirill Tkhai #ifdef CONFIG_MEMCG_KMEM 1817e010df5SKirill Tkhai 1827e010df5SKirill Tkhai /* 1837e010df5SKirill Tkhai * We allow subsystems to populate their shrinker-related 1847e010df5SKirill Tkhai * LRU lists before register_shrinker_prepared() is called 1857e010df5SKirill Tkhai * for the shrinker, since we don't want to impose 1867e010df5SKirill Tkhai * restrictions on their internal registration order. 1877e010df5SKirill Tkhai * In this case shrink_slab_memcg() may find corresponding 1887e010df5SKirill Tkhai * bit is set in the shrinkers map. 1897e010df5SKirill Tkhai * 1907e010df5SKirill Tkhai * This value is used by the function to detect registering 1917e010df5SKirill Tkhai * shrinkers and to skip do_shrink_slab() calls for them. 1927e010df5SKirill Tkhai */ 1937e010df5SKirill Tkhai #define SHRINKER_REGISTERING ((struct shrinker *)~0UL) 1947e010df5SKirill Tkhai 195b4c2b231SKirill Tkhai static DEFINE_IDR(shrinker_idr); 196b4c2b231SKirill Tkhai static int shrinker_nr_max; 197b4c2b231SKirill Tkhai 198b4c2b231SKirill Tkhai static int prealloc_memcg_shrinker(struct shrinker *shrinker) 199b4c2b231SKirill Tkhai { 200b4c2b231SKirill Tkhai int id, ret = -ENOMEM; 201b4c2b231SKirill Tkhai 202b4c2b231SKirill Tkhai down_write(&shrinker_rwsem); 203b4c2b231SKirill Tkhai /* This may call shrinker, so it must use down_read_trylock() */ 2047e010df5SKirill Tkhai id = idr_alloc(&shrinker_idr, SHRINKER_REGISTERING, 0, 0, GFP_KERNEL); 205b4c2b231SKirill Tkhai if (id < 0) 206b4c2b231SKirill Tkhai goto unlock; 207b4c2b231SKirill Tkhai 2080a4465d3SKirill Tkhai if (id >= shrinker_nr_max) { 2090a4465d3SKirill Tkhai if (memcg_expand_shrinker_maps(id)) { 2100a4465d3SKirill Tkhai idr_remove(&shrinker_idr, id); 2110a4465d3SKirill Tkhai goto unlock; 2120a4465d3SKirill Tkhai } 2130a4465d3SKirill Tkhai 214b4c2b231SKirill Tkhai shrinker_nr_max = id + 1; 2150a4465d3SKirill Tkhai } 216b4c2b231SKirill Tkhai shrinker->id = id; 217b4c2b231SKirill Tkhai ret = 0; 218b4c2b231SKirill Tkhai unlock: 219b4c2b231SKirill Tkhai up_write(&shrinker_rwsem); 220b4c2b231SKirill Tkhai return ret; 221b4c2b231SKirill Tkhai } 222b4c2b231SKirill Tkhai 223b4c2b231SKirill Tkhai static void unregister_memcg_shrinker(struct shrinker *shrinker) 224b4c2b231SKirill Tkhai { 225b4c2b231SKirill Tkhai int id = shrinker->id; 226b4c2b231SKirill Tkhai 227b4c2b231SKirill Tkhai BUG_ON(id < 0); 228b4c2b231SKirill Tkhai 229b4c2b231SKirill Tkhai down_write(&shrinker_rwsem); 230b4c2b231SKirill Tkhai idr_remove(&shrinker_idr, id); 231b4c2b231SKirill Tkhai up_write(&shrinker_rwsem); 232b4c2b231SKirill Tkhai } 233b4c2b231SKirill Tkhai #else /* CONFIG_MEMCG_KMEM */ 234b4c2b231SKirill Tkhai static int prealloc_memcg_shrinker(struct shrinker *shrinker) 235b4c2b231SKirill Tkhai { 236b4c2b231SKirill Tkhai return 0; 237b4c2b231SKirill Tkhai } 238b4c2b231SKirill Tkhai 239b4c2b231SKirill Tkhai static void unregister_memcg_shrinker(struct shrinker *shrinker) 240b4c2b231SKirill Tkhai { 241b4c2b231SKirill Tkhai } 242b4c2b231SKirill Tkhai #endif /* CONFIG_MEMCG_KMEM */ 243b4c2b231SKirill Tkhai 244c255a458SAndrew Morton #ifdef CONFIG_MEMCG 24589b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 24689b5fae5SJohannes Weiner { 247f16015fbSJohannes Weiner return !sc->target_mem_cgroup; 24889b5fae5SJohannes Weiner } 24997c9341fSTejun Heo 25097c9341fSTejun Heo /** 25197c9341fSTejun Heo * sane_reclaim - is the usual dirty throttling mechanism operational? 25297c9341fSTejun Heo * @sc: scan_control in question 25397c9341fSTejun Heo * 25497c9341fSTejun Heo * The normal page dirty throttling mechanism in balance_dirty_pages() is 25597c9341fSTejun Heo * completely broken with the legacy memcg and direct stalling in 25697c9341fSTejun Heo * shrink_page_list() is used for throttling instead, which lacks all the 25797c9341fSTejun Heo * niceties such as fairness, adaptive pausing, bandwidth proportional 25897c9341fSTejun Heo * allocation and configurability. 25997c9341fSTejun Heo * 26097c9341fSTejun Heo * This function tests whether the vmscan currently in progress can assume 26197c9341fSTejun Heo * that the normal dirty throttling mechanism is operational. 26297c9341fSTejun Heo */ 26397c9341fSTejun Heo static bool sane_reclaim(struct scan_control *sc) 26497c9341fSTejun Heo { 26597c9341fSTejun Heo struct mem_cgroup *memcg = sc->target_mem_cgroup; 26697c9341fSTejun Heo 26797c9341fSTejun Heo if (!memcg) 26897c9341fSTejun Heo return true; 26997c9341fSTejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 27069234aceSLinus Torvalds if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) 27197c9341fSTejun Heo return true; 27297c9341fSTejun Heo #endif 27397c9341fSTejun Heo return false; 27497c9341fSTejun Heo } 275e3c1ac58SAndrey Ryabinin 276e3c1ac58SAndrey Ryabinin static void set_memcg_congestion(pg_data_t *pgdat, 277e3c1ac58SAndrey Ryabinin struct mem_cgroup *memcg, 278e3c1ac58SAndrey Ryabinin bool congested) 279e3c1ac58SAndrey Ryabinin { 280e3c1ac58SAndrey Ryabinin struct mem_cgroup_per_node *mn; 281e3c1ac58SAndrey Ryabinin 282e3c1ac58SAndrey Ryabinin if (!memcg) 283e3c1ac58SAndrey Ryabinin return; 284e3c1ac58SAndrey Ryabinin 285e3c1ac58SAndrey Ryabinin mn = mem_cgroup_nodeinfo(memcg, pgdat->node_id); 286e3c1ac58SAndrey Ryabinin WRITE_ONCE(mn->congested, congested); 287e3c1ac58SAndrey Ryabinin } 288e3c1ac58SAndrey Ryabinin 289e3c1ac58SAndrey Ryabinin static bool memcg_congested(pg_data_t *pgdat, 290e3c1ac58SAndrey Ryabinin struct mem_cgroup *memcg) 291e3c1ac58SAndrey Ryabinin { 292e3c1ac58SAndrey Ryabinin struct mem_cgroup_per_node *mn; 293e3c1ac58SAndrey Ryabinin 294e3c1ac58SAndrey Ryabinin mn = mem_cgroup_nodeinfo(memcg, pgdat->node_id); 295e3c1ac58SAndrey Ryabinin return READ_ONCE(mn->congested); 296e3c1ac58SAndrey Ryabinin 297e3c1ac58SAndrey Ryabinin } 29891a45470SKAMEZAWA Hiroyuki #else 29989b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 30089b5fae5SJohannes Weiner { 30189b5fae5SJohannes Weiner return true; 30289b5fae5SJohannes Weiner } 30397c9341fSTejun Heo 30497c9341fSTejun Heo static bool sane_reclaim(struct scan_control *sc) 30597c9341fSTejun Heo { 30697c9341fSTejun Heo return true; 30797c9341fSTejun Heo } 308e3c1ac58SAndrey Ryabinin 309e3c1ac58SAndrey Ryabinin static inline void set_memcg_congestion(struct pglist_data *pgdat, 310e3c1ac58SAndrey Ryabinin struct mem_cgroup *memcg, bool congested) 311e3c1ac58SAndrey Ryabinin { 312e3c1ac58SAndrey Ryabinin } 313e3c1ac58SAndrey Ryabinin 314e3c1ac58SAndrey Ryabinin static inline bool memcg_congested(struct pglist_data *pgdat, 315e3c1ac58SAndrey Ryabinin struct mem_cgroup *memcg) 316e3c1ac58SAndrey Ryabinin { 317e3c1ac58SAndrey Ryabinin return false; 318e3c1ac58SAndrey Ryabinin 319e3c1ac58SAndrey Ryabinin } 32091a45470SKAMEZAWA Hiroyuki #endif 32191a45470SKAMEZAWA Hiroyuki 3225a1c84b4SMel Gorman /* 3235a1c84b4SMel Gorman * This misses isolated pages which are not accounted for to save counters. 3245a1c84b4SMel Gorman * As the data only determines if reclaim or compaction continues, it is 3255a1c84b4SMel Gorman * not expected that isolated pages will be a dominating factor. 3265a1c84b4SMel Gorman */ 3275a1c84b4SMel Gorman unsigned long zone_reclaimable_pages(struct zone *zone) 3285a1c84b4SMel Gorman { 3295a1c84b4SMel Gorman unsigned long nr; 3305a1c84b4SMel Gorman 3315a1c84b4SMel Gorman nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) + 3325a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE); 3335a1c84b4SMel Gorman if (get_nr_swap_pages() > 0) 3345a1c84b4SMel Gorman nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) + 3355a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON); 3365a1c84b4SMel Gorman 3375a1c84b4SMel Gorman return nr; 3385a1c84b4SMel Gorman } 3395a1c84b4SMel Gorman 340fd538803SMichal Hocko /** 341fd538803SMichal Hocko * lruvec_lru_size - Returns the number of pages on the given LRU list. 342fd538803SMichal Hocko * @lruvec: lru vector 343fd538803SMichal Hocko * @lru: lru to use 344fd538803SMichal Hocko * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list) 345fd538803SMichal Hocko */ 346fd538803SMichal Hocko unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx) 347c9f299d9SKOSAKI Motohiro { 348fd538803SMichal Hocko unsigned long lru_size; 349fd538803SMichal Hocko int zid; 350a3d8e054SKOSAKI Motohiro 351fd538803SMichal Hocko if (!mem_cgroup_disabled()) 352205b20ccSJohannes Weiner lru_size = lruvec_page_state_local(lruvec, NR_LRU_BASE + lru); 353fd538803SMichal Hocko else 354fd538803SMichal Hocko lru_size = node_page_state(lruvec_pgdat(lruvec), NR_LRU_BASE + lru); 355fd538803SMichal Hocko 356fd538803SMichal Hocko for (zid = zone_idx + 1; zid < MAX_NR_ZONES; zid++) { 357fd538803SMichal Hocko struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; 358fd538803SMichal Hocko unsigned long size; 359fd538803SMichal Hocko 360fd538803SMichal Hocko if (!managed_zone(zone)) 361fd538803SMichal Hocko continue; 362fd538803SMichal Hocko 363fd538803SMichal Hocko if (!mem_cgroup_disabled()) 364fd538803SMichal Hocko size = mem_cgroup_get_zone_lru_size(lruvec, lru, zid); 365fd538803SMichal Hocko else 366fd538803SMichal Hocko size = zone_page_state(&lruvec_pgdat(lruvec)->node_zones[zid], 367fd538803SMichal Hocko NR_ZONE_LRU_BASE + lru); 368fd538803SMichal Hocko lru_size -= min(size, lru_size); 369c9f299d9SKOSAKI Motohiro } 370c9f299d9SKOSAKI Motohiro 371fd538803SMichal Hocko return lru_size; 372b4536f0cSMichal Hocko 373b4536f0cSMichal Hocko } 374b4536f0cSMichal Hocko 3751da177e4SLinus Torvalds /* 3761d3d4437SGlauber Costa * Add a shrinker callback to be called from the vm. 3771da177e4SLinus Torvalds */ 3788e04944fSTetsuo Handa int prealloc_shrinker(struct shrinker *shrinker) 3791da177e4SLinus Torvalds { 380b9726c26SAlexey Dobriyan unsigned int size = sizeof(*shrinker->nr_deferred); 3811d3d4437SGlauber Costa 3821d3d4437SGlauber Costa if (shrinker->flags & SHRINKER_NUMA_AWARE) 3831d3d4437SGlauber Costa size *= nr_node_ids; 3841d3d4437SGlauber Costa 3851d3d4437SGlauber Costa shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); 3861d3d4437SGlauber Costa if (!shrinker->nr_deferred) 3871d3d4437SGlauber Costa return -ENOMEM; 388b4c2b231SKirill Tkhai 389b4c2b231SKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) { 390b4c2b231SKirill Tkhai if (prealloc_memcg_shrinker(shrinker)) 391b4c2b231SKirill Tkhai goto free_deferred; 392b4c2b231SKirill Tkhai } 393b4c2b231SKirill Tkhai 3948e04944fSTetsuo Handa return 0; 395b4c2b231SKirill Tkhai 396b4c2b231SKirill Tkhai free_deferred: 397b4c2b231SKirill Tkhai kfree(shrinker->nr_deferred); 398b4c2b231SKirill Tkhai shrinker->nr_deferred = NULL; 399b4c2b231SKirill Tkhai return -ENOMEM; 4008e04944fSTetsuo Handa } 4011d3d4437SGlauber Costa 4028e04944fSTetsuo Handa void free_prealloced_shrinker(struct shrinker *shrinker) 4038e04944fSTetsuo Handa { 404b4c2b231SKirill Tkhai if (!shrinker->nr_deferred) 405b4c2b231SKirill Tkhai return; 406b4c2b231SKirill Tkhai 407b4c2b231SKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) 408b4c2b231SKirill Tkhai unregister_memcg_shrinker(shrinker); 409b4c2b231SKirill Tkhai 4108e04944fSTetsuo Handa kfree(shrinker->nr_deferred); 4118e04944fSTetsuo Handa shrinker->nr_deferred = NULL; 4128e04944fSTetsuo Handa } 4138e04944fSTetsuo Handa 4148e04944fSTetsuo Handa void register_shrinker_prepared(struct shrinker *shrinker) 4158e04944fSTetsuo Handa { 4161da177e4SLinus Torvalds down_write(&shrinker_rwsem); 4171da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 4187e010df5SKirill Tkhai #ifdef CONFIG_MEMCG_KMEM 4198df4a44cSKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) 4207e010df5SKirill Tkhai idr_replace(&shrinker_idr, shrinker, shrinker->id); 4217e010df5SKirill Tkhai #endif 4221da177e4SLinus Torvalds up_write(&shrinker_rwsem); 4238e04944fSTetsuo Handa } 4248e04944fSTetsuo Handa 4258e04944fSTetsuo Handa int register_shrinker(struct shrinker *shrinker) 4268e04944fSTetsuo Handa { 4278e04944fSTetsuo Handa int err = prealloc_shrinker(shrinker); 4288e04944fSTetsuo Handa 4298e04944fSTetsuo Handa if (err) 4308e04944fSTetsuo Handa return err; 4318e04944fSTetsuo Handa register_shrinker_prepared(shrinker); 4321d3d4437SGlauber Costa return 0; 4331da177e4SLinus Torvalds } 4348e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds /* 4371da177e4SLinus Torvalds * Remove one 4381da177e4SLinus Torvalds */ 4398e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 4401da177e4SLinus Torvalds { 441bb422a73STetsuo Handa if (!shrinker->nr_deferred) 442bb422a73STetsuo Handa return; 443b4c2b231SKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) 444b4c2b231SKirill Tkhai unregister_memcg_shrinker(shrinker); 4451da177e4SLinus Torvalds down_write(&shrinker_rwsem); 4461da177e4SLinus Torvalds list_del(&shrinker->list); 4471da177e4SLinus Torvalds up_write(&shrinker_rwsem); 448ae393321SAndrew Vagin kfree(shrinker->nr_deferred); 449bb422a73STetsuo Handa shrinker->nr_deferred = NULL; 4501da177e4SLinus Torvalds } 4518e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds #define SHRINK_BATCH 128 4541d3d4437SGlauber Costa 455cb731d6cSVladimir Davydov static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, 4569092c71bSJosef Bacik struct shrinker *shrinker, int priority) 4571da177e4SLinus Torvalds { 45824f7c6b9SDave Chinner unsigned long freed = 0; 4591da177e4SLinus Torvalds unsigned long long delta; 460635697c6SKonstantin Khlebnikov long total_scan; 461d5bc5fd3SVladimir Davydov long freeable; 462acf92b48SDave Chinner long nr; 463acf92b48SDave Chinner long new_nr; 4641d3d4437SGlauber Costa int nid = shrinkctl->nid; 465e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 466e9299f50SDave Chinner : SHRINK_BATCH; 4675f33a080SShaohua Li long scanned = 0, next_deferred; 4681da177e4SLinus Torvalds 469ac7fb3adSKirill Tkhai if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) 470ac7fb3adSKirill Tkhai nid = 0; 471ac7fb3adSKirill Tkhai 472d5bc5fd3SVladimir Davydov freeable = shrinker->count_objects(shrinker, shrinkctl); 4739b996468SKirill Tkhai if (freeable == 0 || freeable == SHRINK_EMPTY) 4749b996468SKirill Tkhai return freeable; 475635697c6SKonstantin Khlebnikov 476acf92b48SDave Chinner /* 477acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 478acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 479acf92b48SDave Chinner * don't also do this scanning work. 480acf92b48SDave Chinner */ 4811d3d4437SGlauber Costa nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0); 482acf92b48SDave Chinner 483acf92b48SDave Chinner total_scan = nr; 4844b85afbdSJohannes Weiner if (shrinker->seeks) { 4859092c71bSJosef Bacik delta = freeable >> priority; 4869092c71bSJosef Bacik delta *= 4; 4879092c71bSJosef Bacik do_div(delta, shrinker->seeks); 4884b85afbdSJohannes Weiner } else { 4894b85afbdSJohannes Weiner /* 4904b85afbdSJohannes Weiner * These objects don't require any IO to create. Trim 4914b85afbdSJohannes Weiner * them aggressively under memory pressure to keep 4924b85afbdSJohannes Weiner * them from causing refetches in the IO caches. 4934b85afbdSJohannes Weiner */ 4944b85afbdSJohannes Weiner delta = freeable / 2; 4954b85afbdSJohannes Weiner } 496172b06c3SRoman Gushchin 497acf92b48SDave Chinner total_scan += delta; 498acf92b48SDave Chinner if (total_scan < 0) { 499d75f773cSSakari Ailus pr_err("shrink_slab: %pS negative objects to delete nr=%ld\n", 500a0b02131SDave Chinner shrinker->scan_objects, total_scan); 501d5bc5fd3SVladimir Davydov total_scan = freeable; 5025f33a080SShaohua Li next_deferred = nr; 5035f33a080SShaohua Li } else 5045f33a080SShaohua Li next_deferred = total_scan; 505ea164d73SAndrea Arcangeli 506ea164d73SAndrea Arcangeli /* 5073567b59aSDave Chinner * We need to avoid excessive windup on filesystem shrinkers 5083567b59aSDave Chinner * due to large numbers of GFP_NOFS allocations causing the 5093567b59aSDave Chinner * shrinkers to return -1 all the time. This results in a large 5103567b59aSDave Chinner * nr being built up so when a shrink that can do some work 5113567b59aSDave Chinner * comes along it empties the entire cache due to nr >>> 512d5bc5fd3SVladimir Davydov * freeable. This is bad for sustaining a working set in 5133567b59aSDave Chinner * memory. 5143567b59aSDave Chinner * 5153567b59aSDave Chinner * Hence only allow the shrinker to scan the entire cache when 5163567b59aSDave Chinner * a large delta change is calculated directly. 5173567b59aSDave Chinner */ 518d5bc5fd3SVladimir Davydov if (delta < freeable / 4) 519d5bc5fd3SVladimir Davydov total_scan = min(total_scan, freeable / 2); 5203567b59aSDave Chinner 5213567b59aSDave Chinner /* 522ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 523ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 524ea164d73SAndrea Arcangeli * freeable entries. 525ea164d73SAndrea Arcangeli */ 526d5bc5fd3SVladimir Davydov if (total_scan > freeable * 2) 527d5bc5fd3SVladimir Davydov total_scan = freeable * 2; 5281da177e4SLinus Torvalds 52924f7c6b9SDave Chinner trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, 5309092c71bSJosef Bacik freeable, delta, total_scan, priority); 53109576073SDave Chinner 5320b1fb40aSVladimir Davydov /* 5330b1fb40aSVladimir Davydov * Normally, we should not scan less than batch_size objects in one 5340b1fb40aSVladimir Davydov * pass to avoid too frequent shrinker calls, but if the slab has less 5350b1fb40aSVladimir Davydov * than batch_size objects in total and we are really tight on memory, 5360b1fb40aSVladimir Davydov * we will try to reclaim all available objects, otherwise we can end 5370b1fb40aSVladimir Davydov * up failing allocations although there are plenty of reclaimable 5380b1fb40aSVladimir Davydov * objects spread over several slabs with usage less than the 5390b1fb40aSVladimir Davydov * batch_size. 5400b1fb40aSVladimir Davydov * 5410b1fb40aSVladimir Davydov * We detect the "tight on memory" situations by looking at the total 5420b1fb40aSVladimir Davydov * number of objects we want to scan (total_scan). If it is greater 543d5bc5fd3SVladimir Davydov * than the total number of objects on slab (freeable), we must be 5440b1fb40aSVladimir Davydov * scanning at high prio and therefore should try to reclaim as much as 5450b1fb40aSVladimir Davydov * possible. 5460b1fb40aSVladimir Davydov */ 5470b1fb40aSVladimir Davydov while (total_scan >= batch_size || 548d5bc5fd3SVladimir Davydov total_scan >= freeable) { 54924f7c6b9SDave Chinner unsigned long ret; 5500b1fb40aSVladimir Davydov unsigned long nr_to_scan = min(batch_size, total_scan); 5511da177e4SLinus Torvalds 5520b1fb40aSVladimir Davydov shrinkctl->nr_to_scan = nr_to_scan; 553d460acb5SChris Wilson shrinkctl->nr_scanned = nr_to_scan; 55424f7c6b9SDave Chinner ret = shrinker->scan_objects(shrinker, shrinkctl); 55524f7c6b9SDave Chinner if (ret == SHRINK_STOP) 5561da177e4SLinus Torvalds break; 55724f7c6b9SDave Chinner freed += ret; 55824f7c6b9SDave Chinner 559d460acb5SChris Wilson count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned); 560d460acb5SChris Wilson total_scan -= shrinkctl->nr_scanned; 561d460acb5SChris Wilson scanned += shrinkctl->nr_scanned; 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds cond_resched(); 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds 5665f33a080SShaohua Li if (next_deferred >= scanned) 5675f33a080SShaohua Li next_deferred -= scanned; 5685f33a080SShaohua Li else 5695f33a080SShaohua Li next_deferred = 0; 570acf92b48SDave Chinner /* 571acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 572acf92b48SDave Chinner * manner that handles concurrent updates. If we exhausted the 573acf92b48SDave Chinner * scan, there is no need to do an update. 574acf92b48SDave Chinner */ 5755f33a080SShaohua Li if (next_deferred > 0) 5765f33a080SShaohua Li new_nr = atomic_long_add_return(next_deferred, 5771d3d4437SGlauber Costa &shrinker->nr_deferred[nid]); 57883aeeadaSKonstantin Khlebnikov else 5791d3d4437SGlauber Costa new_nr = atomic_long_read(&shrinker->nr_deferred[nid]); 580acf92b48SDave Chinner 581df9024a8SDave Hansen trace_mm_shrink_slab_end(shrinker, nid, freed, nr, new_nr, total_scan); 5821d3d4437SGlauber Costa return freed; 5831d3d4437SGlauber Costa } 5841d3d4437SGlauber Costa 585b0dedc49SKirill Tkhai #ifdef CONFIG_MEMCG_KMEM 586b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 587b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 588b0dedc49SKirill Tkhai { 589b0dedc49SKirill Tkhai struct memcg_shrinker_map *map; 590b8e57efaSKirill Tkhai unsigned long ret, freed = 0; 591b8e57efaSKirill Tkhai int i; 592b0dedc49SKirill Tkhai 593b0dedc49SKirill Tkhai if (!memcg_kmem_enabled() || !mem_cgroup_online(memcg)) 594b0dedc49SKirill Tkhai return 0; 595b0dedc49SKirill Tkhai 596b0dedc49SKirill Tkhai if (!down_read_trylock(&shrinker_rwsem)) 597b0dedc49SKirill Tkhai return 0; 598b0dedc49SKirill Tkhai 599b0dedc49SKirill Tkhai map = rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_map, 600b0dedc49SKirill Tkhai true); 601b0dedc49SKirill Tkhai if (unlikely(!map)) 602b0dedc49SKirill Tkhai goto unlock; 603b0dedc49SKirill Tkhai 604b0dedc49SKirill Tkhai for_each_set_bit(i, map->map, shrinker_nr_max) { 605b0dedc49SKirill Tkhai struct shrink_control sc = { 606b0dedc49SKirill Tkhai .gfp_mask = gfp_mask, 607b0dedc49SKirill Tkhai .nid = nid, 608b0dedc49SKirill Tkhai .memcg = memcg, 609b0dedc49SKirill Tkhai }; 610b0dedc49SKirill Tkhai struct shrinker *shrinker; 611b0dedc49SKirill Tkhai 612b0dedc49SKirill Tkhai shrinker = idr_find(&shrinker_idr, i); 6137e010df5SKirill Tkhai if (unlikely(!shrinker || shrinker == SHRINKER_REGISTERING)) { 6147e010df5SKirill Tkhai if (!shrinker) 615b0dedc49SKirill Tkhai clear_bit(i, map->map); 616b0dedc49SKirill Tkhai continue; 617b0dedc49SKirill Tkhai } 618b0dedc49SKirill Tkhai 619b0dedc49SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 620f90280d6SKirill Tkhai if (ret == SHRINK_EMPTY) { 621f90280d6SKirill Tkhai clear_bit(i, map->map); 622f90280d6SKirill Tkhai /* 623f90280d6SKirill Tkhai * After the shrinker reported that it had no objects to 624f90280d6SKirill Tkhai * free, but before we cleared the corresponding bit in 625f90280d6SKirill Tkhai * the memcg shrinker map, a new object might have been 626f90280d6SKirill Tkhai * added. To make sure, we have the bit set in this 627f90280d6SKirill Tkhai * case, we invoke the shrinker one more time and reset 628f90280d6SKirill Tkhai * the bit if it reports that it is not empty anymore. 629f90280d6SKirill Tkhai * The memory barrier here pairs with the barrier in 630f90280d6SKirill Tkhai * memcg_set_shrinker_bit(): 631f90280d6SKirill Tkhai * 632f90280d6SKirill Tkhai * list_lru_add() shrink_slab_memcg() 633f90280d6SKirill Tkhai * list_add_tail() clear_bit() 634f90280d6SKirill Tkhai * <MB> <MB> 635f90280d6SKirill Tkhai * set_bit() do_shrink_slab() 636f90280d6SKirill Tkhai */ 637f90280d6SKirill Tkhai smp_mb__after_atomic(); 638f90280d6SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 6399b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 6409b996468SKirill Tkhai ret = 0; 641f90280d6SKirill Tkhai else 642f90280d6SKirill Tkhai memcg_set_shrinker_bit(memcg, nid, i); 643f90280d6SKirill Tkhai } 644b0dedc49SKirill Tkhai freed += ret; 645b0dedc49SKirill Tkhai 646b0dedc49SKirill Tkhai if (rwsem_is_contended(&shrinker_rwsem)) { 647b0dedc49SKirill Tkhai freed = freed ? : 1; 648b0dedc49SKirill Tkhai break; 649b0dedc49SKirill Tkhai } 650b0dedc49SKirill Tkhai } 651b0dedc49SKirill Tkhai unlock: 652b0dedc49SKirill Tkhai up_read(&shrinker_rwsem); 653b0dedc49SKirill Tkhai return freed; 654b0dedc49SKirill Tkhai } 655b0dedc49SKirill Tkhai #else /* CONFIG_MEMCG_KMEM */ 656b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 657b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 658b0dedc49SKirill Tkhai { 659b0dedc49SKirill Tkhai return 0; 660b0dedc49SKirill Tkhai } 661b0dedc49SKirill Tkhai #endif /* CONFIG_MEMCG_KMEM */ 662b0dedc49SKirill Tkhai 6636b4f7799SJohannes Weiner /** 664cb731d6cSVladimir Davydov * shrink_slab - shrink slab caches 6656b4f7799SJohannes Weiner * @gfp_mask: allocation context 6666b4f7799SJohannes Weiner * @nid: node whose slab caches to target 667cb731d6cSVladimir Davydov * @memcg: memory cgroup whose slab caches to target 6689092c71bSJosef Bacik * @priority: the reclaim priority 6691d3d4437SGlauber Costa * 6706b4f7799SJohannes Weiner * Call the shrink functions to age shrinkable caches. 6711d3d4437SGlauber Costa * 6726b4f7799SJohannes Weiner * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, 6736b4f7799SJohannes Weiner * unaware shrinkers will receive a node id of 0 instead. 6741d3d4437SGlauber Costa * 675aeed1d32SVladimir Davydov * @memcg specifies the memory cgroup to target. Unaware shrinkers 676aeed1d32SVladimir Davydov * are called only if it is the root cgroup. 677cb731d6cSVladimir Davydov * 6789092c71bSJosef Bacik * @priority is sc->priority, we take the number of objects and >> by priority 6799092c71bSJosef Bacik * in order to get the scan target. 6801d3d4437SGlauber Costa * 6816b4f7799SJohannes Weiner * Returns the number of reclaimed slab objects. 6821d3d4437SGlauber Costa */ 683cb731d6cSVladimir Davydov static unsigned long shrink_slab(gfp_t gfp_mask, int nid, 684cb731d6cSVladimir Davydov struct mem_cgroup *memcg, 6859092c71bSJosef Bacik int priority) 6861d3d4437SGlauber Costa { 687b8e57efaSKirill Tkhai unsigned long ret, freed = 0; 6881d3d4437SGlauber Costa struct shrinker *shrinker; 6891d3d4437SGlauber Costa 690aeed1d32SVladimir Davydov if (!mem_cgroup_is_root(memcg)) 691b0dedc49SKirill Tkhai return shrink_slab_memcg(gfp_mask, nid, memcg, priority); 692cb731d6cSVladimir Davydov 693e830c63aSTetsuo Handa if (!down_read_trylock(&shrinker_rwsem)) 6941d3d4437SGlauber Costa goto out; 6951d3d4437SGlauber Costa 6961d3d4437SGlauber Costa list_for_each_entry(shrinker, &shrinker_list, list) { 6976b4f7799SJohannes Weiner struct shrink_control sc = { 6986b4f7799SJohannes Weiner .gfp_mask = gfp_mask, 6996b4f7799SJohannes Weiner .nid = nid, 700cb731d6cSVladimir Davydov .memcg = memcg, 7016b4f7799SJohannes Weiner }; 7026b4f7799SJohannes Weiner 7039b996468SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 7049b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 7059b996468SKirill Tkhai ret = 0; 7069b996468SKirill Tkhai freed += ret; 707e496612cSMinchan Kim /* 708e496612cSMinchan Kim * Bail out if someone want to register a new shrinker to 709e496612cSMinchan Kim * prevent the regsitration from being stalled for long periods 710e496612cSMinchan Kim * by parallel ongoing shrinking. 711e496612cSMinchan Kim */ 712e496612cSMinchan Kim if (rwsem_is_contended(&shrinker_rwsem)) { 713e496612cSMinchan Kim freed = freed ? : 1; 714e496612cSMinchan Kim break; 715e496612cSMinchan Kim } 716ec97097bSVladimir Davydov } 7171d3d4437SGlauber Costa 7181da177e4SLinus Torvalds up_read(&shrinker_rwsem); 719f06590bdSMinchan Kim out: 720f06590bdSMinchan Kim cond_resched(); 72124f7c6b9SDave Chinner return freed; 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds 724cb731d6cSVladimir Davydov void drop_slab_node(int nid) 725cb731d6cSVladimir Davydov { 726cb731d6cSVladimir Davydov unsigned long freed; 727cb731d6cSVladimir Davydov 728cb731d6cSVladimir Davydov do { 729cb731d6cSVladimir Davydov struct mem_cgroup *memcg = NULL; 730cb731d6cSVladimir Davydov 731cb731d6cSVladimir Davydov freed = 0; 732aeed1d32SVladimir Davydov memcg = mem_cgroup_iter(NULL, NULL, NULL); 733cb731d6cSVladimir Davydov do { 7349092c71bSJosef Bacik freed += shrink_slab(GFP_KERNEL, nid, memcg, 0); 735cb731d6cSVladimir Davydov } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 736cb731d6cSVladimir Davydov } while (freed > 10); 737cb731d6cSVladimir Davydov } 738cb731d6cSVladimir Davydov 739cb731d6cSVladimir Davydov void drop_slab(void) 740cb731d6cSVladimir Davydov { 741cb731d6cSVladimir Davydov int nid; 742cb731d6cSVladimir Davydov 743cb731d6cSVladimir Davydov for_each_online_node(nid) 744cb731d6cSVladimir Davydov drop_slab_node(nid); 745cb731d6cSVladimir Davydov } 746cb731d6cSVladimir Davydov 7471da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 7481da177e4SLinus Torvalds { 749ceddc3a5SJohannes Weiner /* 750ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 75167891fffSMatthew Wilcox * that isolated the page, the page cache and optional buffer 75267891fffSMatthew Wilcox * heads at page->private. 753ceddc3a5SJohannes Weiner */ 75467891fffSMatthew Wilcox int page_cache_pins = PageTransHuge(page) && PageSwapCache(page) ? 755bd4c82c2SHuang Ying HPAGE_PMD_NR : 1; 75667891fffSMatthew Wilcox return page_count(page) - page_has_private(page) == 1 + page_cache_pins; 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds 759703c2708STejun Heo static int may_write_to_inode(struct inode *inode, struct scan_control *sc) 7601da177e4SLinus Torvalds { 761930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 7621da177e4SLinus Torvalds return 1; 763703c2708STejun Heo if (!inode_write_congested(inode)) 7641da177e4SLinus Torvalds return 1; 765703c2708STejun Heo if (inode_to_bdi(inode) == current->backing_dev_info) 7661da177e4SLinus Torvalds return 1; 7671da177e4SLinus Torvalds return 0; 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds /* 7711da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 7721da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 7731da177e4SLinus Torvalds * fsync(), msync() or close(). 7741da177e4SLinus Torvalds * 7751da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 7761da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 7771da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 7781da177e4SLinus Torvalds * 7791da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 7801da177e4SLinus Torvalds * __GFP_FS. 7811da177e4SLinus Torvalds */ 7821da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 7831da177e4SLinus Torvalds struct page *page, int error) 7841da177e4SLinus Torvalds { 7857eaceaccSJens Axboe lock_page(page); 7863e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 7873e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 7881da177e4SLinus Torvalds unlock_page(page); 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds 79104e62a29SChristoph Lameter /* possible outcome of pageout() */ 79204e62a29SChristoph Lameter typedef enum { 79304e62a29SChristoph Lameter /* failed to write page out, page is locked */ 79404e62a29SChristoph Lameter PAGE_KEEP, 79504e62a29SChristoph Lameter /* move page to the active list, page is locked */ 79604e62a29SChristoph Lameter PAGE_ACTIVATE, 79704e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 79804e62a29SChristoph Lameter PAGE_SUCCESS, 79904e62a29SChristoph Lameter /* page is clean and locked */ 80004e62a29SChristoph Lameter PAGE_CLEAN, 80104e62a29SChristoph Lameter } pageout_t; 80204e62a29SChristoph Lameter 8031da177e4SLinus Torvalds /* 8041742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 8051742f19fSAndrew Morton * Calls ->writepage(). 8061da177e4SLinus Torvalds */ 807c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping, 8087d3579e8SKOSAKI Motohiro struct scan_control *sc) 8091da177e4SLinus Torvalds { 8101da177e4SLinus Torvalds /* 8111da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 8121da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 8131da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 8141da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 8151da177e4SLinus Torvalds * PagePrivate for that. 8161da177e4SLinus Torvalds * 8178174202bSAl Viro * If this process is currently in __generic_file_write_iter() against 8181da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 8191da177e4SLinus Torvalds * will block. 8201da177e4SLinus Torvalds * 8211da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 8221da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 8231da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 8241da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 8251da177e4SLinus Torvalds */ 8261da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 8271da177e4SLinus Torvalds return PAGE_KEEP; 8281da177e4SLinus Torvalds if (!mapping) { 8291da177e4SLinus Torvalds /* 8301da177e4SLinus Torvalds * Some data journaling orphaned pages can have 8311da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 8321da177e4SLinus Torvalds */ 833266cf658SDavid Howells if (page_has_private(page)) { 8341da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 8351da177e4SLinus Torvalds ClearPageDirty(page); 836b1de0d13SMitchel Humpherys pr_info("%s: orphaned page\n", __func__); 8371da177e4SLinus Torvalds return PAGE_CLEAN; 8381da177e4SLinus Torvalds } 8391da177e4SLinus Torvalds } 8401da177e4SLinus Torvalds return PAGE_KEEP; 8411da177e4SLinus Torvalds } 8421da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 8431da177e4SLinus Torvalds return PAGE_ACTIVATE; 844703c2708STejun Heo if (!may_write_to_inode(mapping->host, sc)) 8451da177e4SLinus Torvalds return PAGE_KEEP; 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 8481da177e4SLinus Torvalds int res; 8491da177e4SLinus Torvalds struct writeback_control wbc = { 8501da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 8511da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 852111ebb6eSOGAWA Hirofumi .range_start = 0, 853111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 8541da177e4SLinus Torvalds .for_reclaim = 1, 8551da177e4SLinus Torvalds }; 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds SetPageReclaim(page); 8581da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 8591da177e4SLinus Torvalds if (res < 0) 8601da177e4SLinus Torvalds handle_write_error(mapping, page, res); 861994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 8621da177e4SLinus Torvalds ClearPageReclaim(page); 8631da177e4SLinus Torvalds return PAGE_ACTIVATE; 8641da177e4SLinus Torvalds } 865c661b078SAndy Whitcroft 8661da177e4SLinus Torvalds if (!PageWriteback(page)) { 8671da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 8681da177e4SLinus Torvalds ClearPageReclaim(page); 8691da177e4SLinus Torvalds } 8703aa23851Syalin wang trace_mm_vmscan_writepage(page); 871c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_WRITE); 8721da177e4SLinus Torvalds return PAGE_SUCCESS; 8731da177e4SLinus Torvalds } 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds return PAGE_CLEAN; 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds 878a649fd92SAndrew Morton /* 879e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 880e286781dSNick Piggin * gets returned with a refcount of 0. 881a649fd92SAndrew Morton */ 882a528910eSJohannes Weiner static int __remove_mapping(struct address_space *mapping, struct page *page, 883a528910eSJohannes Weiner bool reclaimed) 88449d2e9ccSChristoph Lameter { 885c4843a75SGreg Thelen unsigned long flags; 886bd4c82c2SHuang Ying int refcount; 887c4843a75SGreg Thelen 88828e4d965SNick Piggin BUG_ON(!PageLocked(page)); 88928e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 89049d2e9ccSChristoph Lameter 891b93b0163SMatthew Wilcox xa_lock_irqsave(&mapping->i_pages, flags); 89249d2e9ccSChristoph Lameter /* 8930fd0e6b0SNick Piggin * The non racy check for a busy page. 8940fd0e6b0SNick Piggin * 8950fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 8960fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 8970fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 8980fd0e6b0SNick Piggin * here, then the following race may occur: 8990fd0e6b0SNick Piggin * 9000fd0e6b0SNick Piggin * get_user_pages(&page); 9010fd0e6b0SNick Piggin * [user mapping goes away] 9020fd0e6b0SNick Piggin * write_to(page); 9030fd0e6b0SNick Piggin * !PageDirty(page) [good] 9040fd0e6b0SNick Piggin * SetPageDirty(page); 9050fd0e6b0SNick Piggin * put_page(page); 9060fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 9070fd0e6b0SNick Piggin * 9080fd0e6b0SNick Piggin * [oops, our write_to data is lost] 9090fd0e6b0SNick Piggin * 9100fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 9110fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 9120139aa7bSJoonsoo Kim * load is not satisfied before that of page->_refcount. 9130fd0e6b0SNick Piggin * 9140fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 915b93b0163SMatthew Wilcox * and thus under the i_pages lock, then this ordering is not required. 91649d2e9ccSChristoph Lameter */ 917bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page)) && PageSwapCache(page)) 918bd4c82c2SHuang Ying refcount = 1 + HPAGE_PMD_NR; 919bd4c82c2SHuang Ying else 920bd4c82c2SHuang Ying refcount = 2; 921bd4c82c2SHuang Ying if (!page_ref_freeze(page, refcount)) 92249d2e9ccSChristoph Lameter goto cannot_free; 9231c4c3b99SJiang Biao /* note: atomic_cmpxchg in page_ref_freeze provides the smp_rmb */ 924e286781dSNick Piggin if (unlikely(PageDirty(page))) { 925bd4c82c2SHuang Ying page_ref_unfreeze(page, refcount); 92649d2e9ccSChristoph Lameter goto cannot_free; 927e286781dSNick Piggin } 92849d2e9ccSChristoph Lameter 92949d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 93049d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 9310a31bc97SJohannes Weiner mem_cgroup_swapout(page, swap); 9324e17ec25SMatthew Wilcox __delete_from_swap_cache(page, swap); 933b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 93475f6d6d2SMinchan Kim put_swap_page(page, swap); 935e286781dSNick Piggin } else { 9366072d13cSLinus Torvalds void (*freepage)(struct page *); 937a528910eSJohannes Weiner void *shadow = NULL; 9386072d13cSLinus Torvalds 9396072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 940a528910eSJohannes Weiner /* 941a528910eSJohannes Weiner * Remember a shadow entry for reclaimed file cache in 942a528910eSJohannes Weiner * order to detect refaults, thus thrashing, later on. 943a528910eSJohannes Weiner * 944a528910eSJohannes Weiner * But don't store shadows in an address space that is 945a528910eSJohannes Weiner * already exiting. This is not just an optizimation, 946a528910eSJohannes Weiner * inode reclaim needs to empty out the radix tree or 947a528910eSJohannes Weiner * the nodes are lost. Don't plant shadows behind its 948a528910eSJohannes Weiner * back. 949f9fe48beSRoss Zwisler * 950f9fe48beSRoss Zwisler * We also don't store shadows for DAX mappings because the 951f9fe48beSRoss Zwisler * only page cache pages found in these are zero pages 952f9fe48beSRoss Zwisler * covering holes, and because we don't want to mix DAX 953f9fe48beSRoss Zwisler * exceptional entries and shadow exceptional entries in the 954b93b0163SMatthew Wilcox * same address_space. 955a528910eSJohannes Weiner */ 956a528910eSJohannes Weiner if (reclaimed && page_is_file_cache(page) && 957f9fe48beSRoss Zwisler !mapping_exiting(mapping) && !dax_mapping(mapping)) 958a7ca12f9SAndrey Ryabinin shadow = workingset_eviction(page); 95962cccb8cSJohannes Weiner __delete_from_page_cache(page, shadow); 960b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 9616072d13cSLinus Torvalds 9626072d13cSLinus Torvalds if (freepage != NULL) 9636072d13cSLinus Torvalds freepage(page); 964e286781dSNick Piggin } 965e286781dSNick Piggin 96649d2e9ccSChristoph Lameter return 1; 96749d2e9ccSChristoph Lameter 96849d2e9ccSChristoph Lameter cannot_free: 969b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 97049d2e9ccSChristoph Lameter return 0; 97149d2e9ccSChristoph Lameter } 97249d2e9ccSChristoph Lameter 9731da177e4SLinus Torvalds /* 974e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 975e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 976e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 977e286781dSNick Piggin * this page. 978e286781dSNick Piggin */ 979e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 980e286781dSNick Piggin { 981a528910eSJohannes Weiner if (__remove_mapping(mapping, page, false)) { 982e286781dSNick Piggin /* 983e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 984e286781dSNick Piggin * drops the pagecache ref for us without requiring another 985e286781dSNick Piggin * atomic operation. 986e286781dSNick Piggin */ 987fe896d18SJoonsoo Kim page_ref_unfreeze(page, 1); 988e286781dSNick Piggin return 1; 989e286781dSNick Piggin } 990e286781dSNick Piggin return 0; 991e286781dSNick Piggin } 992e286781dSNick Piggin 993894bc310SLee Schermerhorn /** 994894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 995894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 996894bc310SLee Schermerhorn * 997894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 998894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 999894bc310SLee Schermerhorn * 1000894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 1001894bc310SLee Schermerhorn */ 1002894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 1003894bc310SLee Schermerhorn { 1004c53954a0SMel Gorman lru_cache_add(page); 1005894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 1006894bc310SLee Schermerhorn } 1007894bc310SLee Schermerhorn 1008dfc8d636SJohannes Weiner enum page_references { 1009dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 1010dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 101164574746SJohannes Weiner PAGEREF_KEEP, 1012dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 1013dfc8d636SJohannes Weiner }; 1014dfc8d636SJohannes Weiner 1015dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 1016dfc8d636SJohannes Weiner struct scan_control *sc) 1017dfc8d636SJohannes Weiner { 101864574746SJohannes Weiner int referenced_ptes, referenced_page; 1019dfc8d636SJohannes Weiner unsigned long vm_flags; 1020dfc8d636SJohannes Weiner 1021c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 1022c3ac9a8aSJohannes Weiner &vm_flags); 102364574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 1024dfc8d636SJohannes Weiner 1025dfc8d636SJohannes Weiner /* 1026dfc8d636SJohannes Weiner * Mlock lost the isolation race with us. Let try_to_unmap() 1027dfc8d636SJohannes Weiner * move the page to the unevictable list. 1028dfc8d636SJohannes Weiner */ 1029dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 1030dfc8d636SJohannes Weiner return PAGEREF_RECLAIM; 1031dfc8d636SJohannes Weiner 103264574746SJohannes Weiner if (referenced_ptes) { 1033e4898273SMichal Hocko if (PageSwapBacked(page)) 103464574746SJohannes Weiner return PAGEREF_ACTIVATE; 103564574746SJohannes Weiner /* 103664574746SJohannes Weiner * All mapped pages start out with page table 103764574746SJohannes Weiner * references from the instantiating fault, so we need 103864574746SJohannes Weiner * to look twice if a mapped file page is used more 103964574746SJohannes Weiner * than once. 104064574746SJohannes Weiner * 104164574746SJohannes Weiner * Mark it and spare it for another trip around the 104264574746SJohannes Weiner * inactive list. Another page table reference will 104364574746SJohannes Weiner * lead to its activation. 104464574746SJohannes Weiner * 104564574746SJohannes Weiner * Note: the mark is set for activated pages as well 104664574746SJohannes Weiner * so that recently deactivated but used pages are 104764574746SJohannes Weiner * quickly recovered. 104864574746SJohannes Weiner */ 104964574746SJohannes Weiner SetPageReferenced(page); 105064574746SJohannes Weiner 105134dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 1052dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 1053dfc8d636SJohannes Weiner 1054c909e993SKonstantin Khlebnikov /* 1055c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 1056c909e993SKonstantin Khlebnikov */ 1057c909e993SKonstantin Khlebnikov if (vm_flags & VM_EXEC) 1058c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 1059c909e993SKonstantin Khlebnikov 106064574746SJohannes Weiner return PAGEREF_KEEP; 106164574746SJohannes Weiner } 106264574746SJohannes Weiner 1063dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 10642e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 1065dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 106664574746SJohannes Weiner 106764574746SJohannes Weiner return PAGEREF_RECLAIM; 1068dfc8d636SJohannes Weiner } 1069dfc8d636SJohannes Weiner 1070e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 1071e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page, 1072e2be15f6SMel Gorman bool *dirty, bool *writeback) 1073e2be15f6SMel Gorman { 1074b4597226SMel Gorman struct address_space *mapping; 1075b4597226SMel Gorman 1076e2be15f6SMel Gorman /* 1077e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 1078e2be15f6SMel Gorman * from reclaim context. Do not stall reclaim based on them 1079e2be15f6SMel Gorman */ 1080802a3a92SShaohua Li if (!page_is_file_cache(page) || 1081802a3a92SShaohua Li (PageAnon(page) && !PageSwapBacked(page))) { 1082e2be15f6SMel Gorman *dirty = false; 1083e2be15f6SMel Gorman *writeback = false; 1084e2be15f6SMel Gorman return; 1085e2be15f6SMel Gorman } 1086e2be15f6SMel Gorman 1087e2be15f6SMel Gorman /* By default assume that the page flags are accurate */ 1088e2be15f6SMel Gorman *dirty = PageDirty(page); 1089e2be15f6SMel Gorman *writeback = PageWriteback(page); 1090b4597226SMel Gorman 1091b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 1092b4597226SMel Gorman if (!page_has_private(page)) 1093b4597226SMel Gorman return; 1094b4597226SMel Gorman 1095b4597226SMel Gorman mapping = page_mapping(page); 1096b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 1097b4597226SMel Gorman mapping->a_ops->is_dirty_writeback(page, dirty, writeback); 1098e2be15f6SMel Gorman } 1099e2be15f6SMel Gorman 1100e286781dSNick Piggin /* 11011742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 11021da177e4SLinus Torvalds */ 11031742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list, 1104599d0c95SMel Gorman struct pglist_data *pgdat, 1105f84f6e2bSMel Gorman struct scan_control *sc, 110602c6de8dSMinchan Kim enum ttu_flags ttu_flags, 11073c710c1aSMichal Hocko struct reclaim_stat *stat, 110802c6de8dSMinchan Kim bool force_reclaim) 11091da177e4SLinus Torvalds { 11101da177e4SLinus Torvalds LIST_HEAD(ret_pages); 1111abe4c3b5SMel Gorman LIST_HEAD(free_pages); 11123c710c1aSMichal Hocko unsigned nr_reclaimed = 0; 1113886cf190SKirill Tkhai unsigned pgactivate = 0; 11141da177e4SLinus Torvalds 1115060f005fSKirill Tkhai memset(stat, 0, sizeof(*stat)); 11161da177e4SLinus Torvalds cond_resched(); 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds while (!list_empty(page_list)) { 11191da177e4SLinus Torvalds struct address_space *mapping; 11201da177e4SLinus Torvalds struct page *page; 11211da177e4SLinus Torvalds int may_enter_fs; 112202c6de8dSMinchan Kim enum page_references references = PAGEREF_RECLAIM_CLEAN; 1123e2be15f6SMel Gorman bool dirty, writeback; 112498879b3bSYang Shi unsigned int nr_pages; 11251da177e4SLinus Torvalds 11261da177e4SLinus Torvalds cond_resched(); 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds page = lru_to_page(page_list); 11291da177e4SLinus Torvalds list_del(&page->lru); 11301da177e4SLinus Torvalds 1131529ae9aaSNick Piggin if (!trylock_page(page)) 11321da177e4SLinus Torvalds goto keep; 11331da177e4SLinus Torvalds 1134309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 11351da177e4SLinus Torvalds 113698879b3bSYang Shi nr_pages = 1 << compound_order(page); 113798879b3bSYang Shi 113898879b3bSYang Shi /* Account the number of base pages even though THP */ 113998879b3bSYang Shi sc->nr_scanned += nr_pages; 114080e43426SChristoph Lameter 114139b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) 1142ad6b6704SMinchan Kim goto activate_locked; 1143894bc310SLee Schermerhorn 1144a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 114580e43426SChristoph Lameter goto keep_locked; 114680e43426SChristoph Lameter 1147c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 1148c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 1149c661b078SAndy Whitcroft 1150e62e384eSMichal Hocko /* 1151894befecSAndrey Ryabinin * The number of dirty pages determines if a node is marked 1152e2be15f6SMel Gorman * reclaim_congested which affects wait_iff_congested. kswapd 1153e2be15f6SMel Gorman * will stall and start writing pages if the tail of the LRU 1154e2be15f6SMel Gorman * is all dirty unqueued pages. 1155e2be15f6SMel Gorman */ 1156e2be15f6SMel Gorman page_check_dirty_writeback(page, &dirty, &writeback); 1157e2be15f6SMel Gorman if (dirty || writeback) 1158060f005fSKirill Tkhai stat->nr_dirty++; 1159e2be15f6SMel Gorman 1160e2be15f6SMel Gorman if (dirty && !writeback) 1161060f005fSKirill Tkhai stat->nr_unqueued_dirty++; 1162e2be15f6SMel Gorman 1163d04e8acdSMel Gorman /* 1164d04e8acdSMel Gorman * Treat this page as congested if the underlying BDI is or if 1165d04e8acdSMel Gorman * pages are cycling through the LRU so quickly that the 1166d04e8acdSMel Gorman * pages marked for immediate reclaim are making it to the 1167d04e8acdSMel Gorman * end of the LRU a second time. 1168d04e8acdSMel Gorman */ 1169e2be15f6SMel Gorman mapping = page_mapping(page); 11701da58ee2SJamie Liu if (((dirty || writeback) && mapping && 1171703c2708STejun Heo inode_write_congested(mapping->host)) || 1172d04e8acdSMel Gorman (writeback && PageReclaim(page))) 1173060f005fSKirill Tkhai stat->nr_congested++; 1174e2be15f6SMel Gorman 1175e2be15f6SMel Gorman /* 1176283aba9fSMel Gorman * If a page at the tail of the LRU is under writeback, there 1177283aba9fSMel Gorman * are three cases to consider. 1178e62e384eSMichal Hocko * 1179283aba9fSMel Gorman * 1) If reclaim is encountering an excessive number of pages 1180283aba9fSMel Gorman * under writeback and this page is both under writeback and 1181283aba9fSMel Gorman * PageReclaim then it indicates that pages are being queued 1182283aba9fSMel Gorman * for IO but are being recycled through the LRU before the 1183283aba9fSMel Gorman * IO can complete. Waiting on the page itself risks an 1184283aba9fSMel Gorman * indefinite stall if it is impossible to writeback the 1185283aba9fSMel Gorman * page due to IO error or disconnected storage so instead 1186b1a6f21eSMel Gorman * note that the LRU is being scanned too quickly and the 1187b1a6f21eSMel Gorman * caller can stall after page list has been processed. 1188c3b94f44SHugh Dickins * 118997c9341fSTejun Heo * 2) Global or new memcg reclaim encounters a page that is 1190ecf5fc6eSMichal Hocko * not marked for immediate reclaim, or the caller does not 1191ecf5fc6eSMichal Hocko * have __GFP_FS (or __GFP_IO if it's simply going to swap, 1192ecf5fc6eSMichal Hocko * not to fs). In this case mark the page for immediate 119397c9341fSTejun Heo * reclaim and continue scanning. 1194283aba9fSMel Gorman * 1195ecf5fc6eSMichal Hocko * Require may_enter_fs because we would wait on fs, which 1196ecf5fc6eSMichal Hocko * may not have submitted IO yet. And the loop driver might 1197283aba9fSMel Gorman * enter reclaim, and deadlock if it waits on a page for 1198283aba9fSMel Gorman * which it is needed to do the write (loop masks off 1199283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 1200283aba9fSMel Gorman * would probably show more reasons. 1201283aba9fSMel Gorman * 12027fadc820SHugh Dickins * 3) Legacy memcg encounters a page that is already marked 1203283aba9fSMel Gorman * PageReclaim. memcg does not have any dirty pages 1204283aba9fSMel Gorman * throttling so we could easily OOM just because too many 1205283aba9fSMel Gorman * pages are in writeback and there is nothing else to 1206283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 1207c55e8d03SJohannes Weiner * 1208c55e8d03SJohannes Weiner * In cases 1) and 2) we activate the pages to get them out of 1209c55e8d03SJohannes Weiner * the way while we continue scanning for clean pages on the 1210c55e8d03SJohannes Weiner * inactive list and refilling from the active list. The 1211c55e8d03SJohannes Weiner * observation here is that waiting for disk writes is more 1212c55e8d03SJohannes Weiner * expensive than potentially causing reloads down the line. 1213c55e8d03SJohannes Weiner * Since they're marked for immediate reclaim, they won't put 1214c55e8d03SJohannes Weiner * memory pressure on the cache working set any longer than it 1215c55e8d03SJohannes Weiner * takes to write them to disk. 1216e62e384eSMichal Hocko */ 1217283aba9fSMel Gorman if (PageWriteback(page)) { 1218283aba9fSMel Gorman /* Case 1 above */ 1219283aba9fSMel Gorman if (current_is_kswapd() && 1220283aba9fSMel Gorman PageReclaim(page) && 1221599d0c95SMel Gorman test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { 1222060f005fSKirill Tkhai stat->nr_immediate++; 1223c55e8d03SJohannes Weiner goto activate_locked; 1224283aba9fSMel Gorman 1225283aba9fSMel Gorman /* Case 2 above */ 122697c9341fSTejun Heo } else if (sane_reclaim(sc) || 1227ecf5fc6eSMichal Hocko !PageReclaim(page) || !may_enter_fs) { 1228c3b94f44SHugh Dickins /* 1229c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 1230c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 1231c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 1232c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 1233c3b94f44SHugh Dickins * enough to care. What we do want is for this 1234c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 1235c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 1236c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 1237c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 1238c3b94f44SHugh Dickins */ 1239c3b94f44SHugh Dickins SetPageReclaim(page); 1240060f005fSKirill Tkhai stat->nr_writeback++; 1241c55e8d03SJohannes Weiner goto activate_locked; 1242283aba9fSMel Gorman 1243283aba9fSMel Gorman /* Case 3 above */ 1244283aba9fSMel Gorman } else { 12457fadc820SHugh Dickins unlock_page(page); 1246c3b94f44SHugh Dickins wait_on_page_writeback(page); 12477fadc820SHugh Dickins /* then go back and try same page again */ 12487fadc820SHugh Dickins list_add_tail(&page->lru, page_list); 12497fadc820SHugh Dickins continue; 1250e62e384eSMichal Hocko } 1251283aba9fSMel Gorman } 12521da177e4SLinus Torvalds 125302c6de8dSMinchan Kim if (!force_reclaim) 12546a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 125502c6de8dSMinchan Kim 1256dfc8d636SJohannes Weiner switch (references) { 1257dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 12581da177e4SLinus Torvalds goto activate_locked; 125964574746SJohannes Weiner case PAGEREF_KEEP: 126098879b3bSYang Shi stat->nr_ref_keep += nr_pages; 126164574746SJohannes Weiner goto keep_locked; 1262dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 1263dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 1264dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 1265dfc8d636SJohannes Weiner } 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds /* 12681da177e4SLinus Torvalds * Anonymous process memory has backing store? 12691da177e4SLinus Torvalds * Try to allocate it some swap space here. 1270802a3a92SShaohua Li * Lazyfree page could be freed directly 12711da177e4SLinus Torvalds */ 1272bd4c82c2SHuang Ying if (PageAnon(page) && PageSwapBacked(page)) { 1273bd4c82c2SHuang Ying if (!PageSwapCache(page)) { 127463eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 127563eb6b93SHugh Dickins goto keep_locked; 1276747552b1SHuang Ying if (PageTransHuge(page)) { 1277b8f593cdSHuang Ying /* cannot split THP, skip it */ 1278747552b1SHuang Ying if (!can_split_huge_page(page, NULL)) 1279b8f593cdSHuang Ying goto activate_locked; 1280747552b1SHuang Ying /* 1281747552b1SHuang Ying * Split pages without a PMD map right 1282747552b1SHuang Ying * away. Chances are some or all of the 1283747552b1SHuang Ying * tail pages can be freed without IO. 1284747552b1SHuang Ying */ 1285747552b1SHuang Ying if (!compound_mapcount(page) && 1286bd4c82c2SHuang Ying split_huge_page_to_list(page, 1287bd4c82c2SHuang Ying page_list)) 1288747552b1SHuang Ying goto activate_locked; 1289747552b1SHuang Ying } 12900f074658SMinchan Kim if (!add_to_swap(page)) { 12910f074658SMinchan Kim if (!PageTransHuge(page)) 129298879b3bSYang Shi goto activate_locked_split; 1293bd4c82c2SHuang Ying /* Fallback to swap normal pages */ 1294bd4c82c2SHuang Ying if (split_huge_page_to_list(page, 1295bd4c82c2SHuang Ying page_list)) 12960f074658SMinchan Kim goto activate_locked; 1297fe490cc0SHuang Ying #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1298fe490cc0SHuang Ying count_vm_event(THP_SWPOUT_FALLBACK); 1299fe490cc0SHuang Ying #endif 13000f074658SMinchan Kim if (!add_to_swap(page)) 130198879b3bSYang Shi goto activate_locked_split; 13020f074658SMinchan Kim } 13030f074658SMinchan Kim 130463eb6b93SHugh Dickins may_enter_fs = 1; 13051da177e4SLinus Torvalds 1306e2be15f6SMel Gorman /* Adding to swap updated mapping */ 13071da177e4SLinus Torvalds mapping = page_mapping(page); 1308bd4c82c2SHuang Ying } 13097751b2daSKirill A. Shutemov } else if (unlikely(PageTransHuge(page))) { 13107751b2daSKirill A. Shutemov /* Split file THP */ 13117751b2daSKirill A. Shutemov if (split_huge_page_to_list(page, page_list)) 13127751b2daSKirill A. Shutemov goto keep_locked; 1313e2be15f6SMel Gorman } 13141da177e4SLinus Torvalds 13151da177e4SLinus Torvalds /* 131698879b3bSYang Shi * THP may get split above, need minus tail pages and update 131798879b3bSYang Shi * nr_pages to avoid accounting tail pages twice. 131898879b3bSYang Shi * 131998879b3bSYang Shi * The tail pages that are added into swap cache successfully 132098879b3bSYang Shi * reach here. 132198879b3bSYang Shi */ 132298879b3bSYang Shi if ((nr_pages > 1) && !PageTransHuge(page)) { 132398879b3bSYang Shi sc->nr_scanned -= (nr_pages - 1); 132498879b3bSYang Shi nr_pages = 1; 132598879b3bSYang Shi } 132698879b3bSYang Shi 132798879b3bSYang Shi /* 13281da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 13291da177e4SLinus Torvalds * processes. Try to unmap it here. 13301da177e4SLinus Torvalds */ 1331802a3a92SShaohua Li if (page_mapped(page)) { 1332bd4c82c2SHuang Ying enum ttu_flags flags = ttu_flags | TTU_BATCH_FLUSH; 1333bd4c82c2SHuang Ying 1334bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page))) 1335bd4c82c2SHuang Ying flags |= TTU_SPLIT_HUGE_PMD; 1336bd4c82c2SHuang Ying if (!try_to_unmap(page, flags)) { 133798879b3bSYang Shi stat->nr_unmap_fail += nr_pages; 13381da177e4SLinus Torvalds goto activate_locked; 13391da177e4SLinus Torvalds } 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds 13421da177e4SLinus Torvalds if (PageDirty(page)) { 1343ee72886dSMel Gorman /* 13444eda4823SJohannes Weiner * Only kswapd can writeback filesystem pages 13454eda4823SJohannes Weiner * to avoid risk of stack overflow. But avoid 13464eda4823SJohannes Weiner * injecting inefficient single-page IO into 13474eda4823SJohannes Weiner * flusher writeback as much as possible: only 13484eda4823SJohannes Weiner * write pages when we've encountered many 13494eda4823SJohannes Weiner * dirty pages, and when we've already scanned 13504eda4823SJohannes Weiner * the rest of the LRU for clean pages and see 13514eda4823SJohannes Weiner * the same dirty pages again (PageReclaim). 1352ee72886dSMel Gorman */ 1353f84f6e2bSMel Gorman if (page_is_file_cache(page) && 13544eda4823SJohannes Weiner (!current_is_kswapd() || !PageReclaim(page) || 1355599d0c95SMel Gorman !test_bit(PGDAT_DIRTY, &pgdat->flags))) { 135649ea7eb6SMel Gorman /* 135749ea7eb6SMel Gorman * Immediately reclaim when written back. 135849ea7eb6SMel Gorman * Similar in principal to deactivate_page() 135949ea7eb6SMel Gorman * except we already have the page isolated 136049ea7eb6SMel Gorman * and know it's dirty 136149ea7eb6SMel Gorman */ 1362c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_IMMEDIATE); 136349ea7eb6SMel Gorman SetPageReclaim(page); 136449ea7eb6SMel Gorman 1365c55e8d03SJohannes Weiner goto activate_locked; 1366ee72886dSMel Gorman } 1367ee72886dSMel Gorman 1368dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 13691da177e4SLinus Torvalds goto keep_locked; 13704dd4b920SAndrew Morton if (!may_enter_fs) 13711da177e4SLinus Torvalds goto keep_locked; 137252a8363eSChristoph Lameter if (!sc->may_writepage) 13731da177e4SLinus Torvalds goto keep_locked; 13741da177e4SLinus Torvalds 1375d950c947SMel Gorman /* 1376d950c947SMel Gorman * Page is dirty. Flush the TLB if a writable entry 1377d950c947SMel Gorman * potentially exists to avoid CPU writes after IO 1378d950c947SMel Gorman * starts and then write it out here. 1379d950c947SMel Gorman */ 1380d950c947SMel Gorman try_to_unmap_flush_dirty(); 13817d3579e8SKOSAKI Motohiro switch (pageout(page, mapping, sc)) { 13821da177e4SLinus Torvalds case PAGE_KEEP: 13831da177e4SLinus Torvalds goto keep_locked; 13841da177e4SLinus Torvalds case PAGE_ACTIVATE: 13851da177e4SLinus Torvalds goto activate_locked; 13861da177e4SLinus Torvalds case PAGE_SUCCESS: 13877d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 138841ac1999SMel Gorman goto keep; 13897d3579e8SKOSAKI Motohiro if (PageDirty(page)) 13901da177e4SLinus Torvalds goto keep; 13917d3579e8SKOSAKI Motohiro 13921da177e4SLinus Torvalds /* 13931da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 13941da177e4SLinus Torvalds * ahead and try to reclaim the page. 13951da177e4SLinus Torvalds */ 1396529ae9aaSNick Piggin if (!trylock_page(page)) 13971da177e4SLinus Torvalds goto keep; 13981da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 13991da177e4SLinus Torvalds goto keep_locked; 14001da177e4SLinus Torvalds mapping = page_mapping(page); 14011da177e4SLinus Torvalds case PAGE_CLEAN: 14021da177e4SLinus Torvalds ; /* try to free the page below */ 14031da177e4SLinus Torvalds } 14041da177e4SLinus Torvalds } 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds /* 14071da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 14081da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 14091da177e4SLinus Torvalds * the page as well. 14101da177e4SLinus Torvalds * 14111da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 14121da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 14131da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 14141da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 14151da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 14161da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 14171da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 14181da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 14191da177e4SLinus Torvalds * 14201da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 14211da177e4SLinus Torvalds * the pages which were not successfully invalidated in 14221da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 14231da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 14241da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 14251da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 14261da177e4SLinus Torvalds */ 1427266cf658SDavid Howells if (page_has_private(page)) { 14281da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 14291da177e4SLinus Torvalds goto activate_locked; 1430e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 1431e286781dSNick Piggin unlock_page(page); 1432e286781dSNick Piggin if (put_page_testzero(page)) 14331da177e4SLinus Torvalds goto free_it; 1434e286781dSNick Piggin else { 1435e286781dSNick Piggin /* 1436e286781dSNick Piggin * rare race with speculative reference. 1437e286781dSNick Piggin * the speculative reference will free 1438e286781dSNick Piggin * this page shortly, so we may 1439e286781dSNick Piggin * increment nr_reclaimed here (and 1440e286781dSNick Piggin * leave it off the LRU). 1441e286781dSNick Piggin */ 1442e286781dSNick Piggin nr_reclaimed++; 1443e286781dSNick Piggin continue; 1444e286781dSNick Piggin } 1445e286781dSNick Piggin } 14461da177e4SLinus Torvalds } 14471da177e4SLinus Torvalds 1448802a3a92SShaohua Li if (PageAnon(page) && !PageSwapBacked(page)) { 1449802a3a92SShaohua Li /* follow __remove_mapping for reference */ 1450802a3a92SShaohua Li if (!page_ref_freeze(page, 1)) 145149d2e9ccSChristoph Lameter goto keep_locked; 1452802a3a92SShaohua Li if (PageDirty(page)) { 1453802a3a92SShaohua Li page_ref_unfreeze(page, 1); 1454802a3a92SShaohua Li goto keep_locked; 1455802a3a92SShaohua Li } 14561da177e4SLinus Torvalds 1457802a3a92SShaohua Li count_vm_event(PGLAZYFREED); 14582262185cSRoman Gushchin count_memcg_page_event(page, PGLAZYFREED); 1459802a3a92SShaohua Li } else if (!mapping || !__remove_mapping(mapping, page, true)) 1460802a3a92SShaohua Li goto keep_locked; 14619a1ea439SHugh Dickins 14629a1ea439SHugh Dickins unlock_page(page); 1463e286781dSNick Piggin free_it: 146498879b3bSYang Shi /* 146598879b3bSYang Shi * THP may get swapped out in a whole, need account 146698879b3bSYang Shi * all base pages. 146798879b3bSYang Shi */ 146898879b3bSYang Shi nr_reclaimed += nr_pages; 1469abe4c3b5SMel Gorman 1470abe4c3b5SMel Gorman /* 1471abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1472abe4c3b5SMel Gorman * appear not as the counts should be low 1473abe4c3b5SMel Gorman */ 1474bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page))) { 1475bd4c82c2SHuang Ying mem_cgroup_uncharge(page); 1476bd4c82c2SHuang Ying (*get_compound_page_dtor(page))(page); 1477bd4c82c2SHuang Ying } else 1478abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 14791da177e4SLinus Torvalds continue; 14801da177e4SLinus Torvalds 148198879b3bSYang Shi activate_locked_split: 148298879b3bSYang Shi /* 148398879b3bSYang Shi * The tail pages that are failed to add into swap cache 148498879b3bSYang Shi * reach here. Fixup nr_scanned and nr_pages. 148598879b3bSYang Shi */ 148698879b3bSYang Shi if (nr_pages > 1) { 148798879b3bSYang Shi sc->nr_scanned -= (nr_pages - 1); 148898879b3bSYang Shi nr_pages = 1; 148998879b3bSYang Shi } 14901da177e4SLinus Torvalds activate_locked: 149168a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 1492ad6b6704SMinchan Kim if (PageSwapCache(page) && (mem_cgroup_swap_full(page) || 1493ad6b6704SMinchan Kim PageMlocked(page))) 1494a2c43eedSHugh Dickins try_to_free_swap(page); 1495309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 1496ad6b6704SMinchan Kim if (!PageMlocked(page)) { 1497886cf190SKirill Tkhai int type = page_is_file_cache(page); 14981da177e4SLinus Torvalds SetPageActive(page); 149998879b3bSYang Shi stat->nr_activate[type] += nr_pages; 15002262185cSRoman Gushchin count_memcg_page_event(page, PGACTIVATE); 1501ad6b6704SMinchan Kim } 15021da177e4SLinus Torvalds keep_locked: 15031da177e4SLinus Torvalds unlock_page(page); 15041da177e4SLinus Torvalds keep: 15051da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 1506309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); 15071da177e4SLinus Torvalds } 1508abe4c3b5SMel Gorman 150998879b3bSYang Shi pgactivate = stat->nr_activate[0] + stat->nr_activate[1]; 151098879b3bSYang Shi 1511747db954SJohannes Weiner mem_cgroup_uncharge_list(&free_pages); 151272b252aeSMel Gorman try_to_unmap_flush(); 15132d4894b5SMel Gorman free_unref_page_list(&free_pages); 1514abe4c3b5SMel Gorman 15151da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1516886cf190SKirill Tkhai count_vm_events(PGACTIVATE, pgactivate); 15170a31bc97SJohannes Weiner 151805ff5137SAndrew Morton return nr_reclaimed; 15191da177e4SLinus Torvalds } 15201da177e4SLinus Torvalds 152102c6de8dSMinchan Kim unsigned long reclaim_clean_pages_from_list(struct zone *zone, 152202c6de8dSMinchan Kim struct list_head *page_list) 152302c6de8dSMinchan Kim { 152402c6de8dSMinchan Kim struct scan_control sc = { 152502c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 152602c6de8dSMinchan Kim .priority = DEF_PRIORITY, 152702c6de8dSMinchan Kim .may_unmap = 1, 152802c6de8dSMinchan Kim }; 1529060f005fSKirill Tkhai struct reclaim_stat dummy_stat; 15303c710c1aSMichal Hocko unsigned long ret; 153102c6de8dSMinchan Kim struct page *page, *next; 153202c6de8dSMinchan Kim LIST_HEAD(clean_pages); 153302c6de8dSMinchan Kim 153402c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 1535117aad1eSRafael Aquini if (page_is_file_cache(page) && !PageDirty(page) && 1536a58f2cefSMinchan Kim !__PageMovable(page) && !PageUnevictable(page)) { 153702c6de8dSMinchan Kim ClearPageActive(page); 153802c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 153902c6de8dSMinchan Kim } 154002c6de8dSMinchan Kim } 154102c6de8dSMinchan Kim 1542599d0c95SMel Gorman ret = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc, 1543060f005fSKirill Tkhai TTU_IGNORE_ACCESS, &dummy_stat, true); 154402c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 1545599d0c95SMel Gorman mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, -ret); 154602c6de8dSMinchan Kim return ret; 154702c6de8dSMinchan Kim } 154802c6de8dSMinchan Kim 15495ad333ebSAndy Whitcroft /* 15505ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 15515ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 15525ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 15535ad333ebSAndy Whitcroft * 15545ad333ebSAndy Whitcroft * page: page to consider 15555ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 15565ad333ebSAndy Whitcroft * 15575ad333ebSAndy Whitcroft * returns 0 on success, -ve errno on failure. 15585ad333ebSAndy Whitcroft */ 1559f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode) 15605ad333ebSAndy Whitcroft { 15615ad333ebSAndy Whitcroft int ret = -EINVAL; 15625ad333ebSAndy Whitcroft 15635ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 15645ad333ebSAndy Whitcroft if (!PageLRU(page)) 15655ad333ebSAndy Whitcroft return ret; 15665ad333ebSAndy Whitcroft 1567e46a2879SMinchan Kim /* Compaction should not handle unevictable pages but CMA can do so */ 1568e46a2879SMinchan Kim if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) 1569894bc310SLee Schermerhorn return ret; 1570894bc310SLee Schermerhorn 15715ad333ebSAndy Whitcroft ret = -EBUSY; 157208e552c6SKAMEZAWA Hiroyuki 1573c8244935SMel Gorman /* 1574c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 1575c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 1576c8244935SMel Gorman * blocking - clean pages for the most part. 1577c8244935SMel Gorman * 1578c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 1579c8244935SMel Gorman * that it is possible to migrate without blocking 1580c8244935SMel Gorman */ 15811276ad68SJohannes Weiner if (mode & ISOLATE_ASYNC_MIGRATE) { 1582c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 1583c8244935SMel Gorman if (PageWriteback(page)) 158439deaf85SMinchan Kim return ret; 158539deaf85SMinchan Kim 1586c8244935SMel Gorman if (PageDirty(page)) { 1587c8244935SMel Gorman struct address_space *mapping; 158869d763fcSMel Gorman bool migrate_dirty; 1589c8244935SMel Gorman 1590c8244935SMel Gorman /* 1591c8244935SMel Gorman * Only pages without mappings or that have a 1592c8244935SMel Gorman * ->migratepage callback are possible to migrate 159369d763fcSMel Gorman * without blocking. However, we can be racing with 159469d763fcSMel Gorman * truncation so it's necessary to lock the page 159569d763fcSMel Gorman * to stabilise the mapping as truncation holds 159669d763fcSMel Gorman * the page lock until after the page is removed 159769d763fcSMel Gorman * from the page cache. 1598c8244935SMel Gorman */ 159969d763fcSMel Gorman if (!trylock_page(page)) 160069d763fcSMel Gorman return ret; 160169d763fcSMel Gorman 1602c8244935SMel Gorman mapping = page_mapping(page); 1603145e1a71SHugh Dickins migrate_dirty = !mapping || mapping->a_ops->migratepage; 160469d763fcSMel Gorman unlock_page(page); 160569d763fcSMel Gorman if (!migrate_dirty) 1606c8244935SMel Gorman return ret; 1607c8244935SMel Gorman } 1608c8244935SMel Gorman } 1609c8244935SMel Gorman 1610f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 1611f80c0673SMinchan Kim return ret; 1612f80c0673SMinchan Kim 16135ad333ebSAndy Whitcroft if (likely(get_page_unless_zero(page))) { 16145ad333ebSAndy Whitcroft /* 16155ad333ebSAndy Whitcroft * Be careful not to clear PageLRU until after we're 16165ad333ebSAndy Whitcroft * sure the page is not being freed elsewhere -- the 16175ad333ebSAndy Whitcroft * page release code relies on it. 16185ad333ebSAndy Whitcroft */ 16195ad333ebSAndy Whitcroft ClearPageLRU(page); 16205ad333ebSAndy Whitcroft ret = 0; 16215ad333ebSAndy Whitcroft } 16225ad333ebSAndy Whitcroft 16235ad333ebSAndy Whitcroft return ret; 16245ad333ebSAndy Whitcroft } 16255ad333ebSAndy Whitcroft 16267ee36a14SMel Gorman 16277ee36a14SMel Gorman /* 16287ee36a14SMel Gorman * Update LRU sizes after isolating pages. The LRU size updates must 16297ee36a14SMel Gorman * be complete before mem_cgroup_update_lru_size due to a santity check. 16307ee36a14SMel Gorman */ 16317ee36a14SMel Gorman static __always_inline void update_lru_sizes(struct lruvec *lruvec, 1632b4536f0cSMichal Hocko enum lru_list lru, unsigned long *nr_zone_taken) 16337ee36a14SMel Gorman { 16347ee36a14SMel Gorman int zid; 16357ee36a14SMel Gorman 16367ee36a14SMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 16377ee36a14SMel Gorman if (!nr_zone_taken[zid]) 16387ee36a14SMel Gorman continue; 16397ee36a14SMel Gorman 16407ee36a14SMel Gorman __update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 1641b4536f0cSMichal Hocko #ifdef CONFIG_MEMCG 1642b4536f0cSMichal Hocko mem_cgroup_update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 1643b4536f0cSMichal Hocko #endif 16447ee36a14SMel Gorman } 16457ee36a14SMel Gorman 16467ee36a14SMel Gorman } 16477ee36a14SMel Gorman 1648f4b7e272SAndrey Ryabinin /** 1649f4b7e272SAndrey Ryabinin * pgdat->lru_lock is heavily contended. Some of the functions that 16501da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 16511da177e4SLinus Torvalds * and working on them outside the LRU lock. 16521da177e4SLinus Torvalds * 16531da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 16541da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 16551da177e4SLinus Torvalds * 16561da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 16571da177e4SLinus Torvalds * 1658791b48b6SMinchan Kim * @nr_to_scan: The number of eligible pages to look through on the list. 16595dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 16601da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1661f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1662fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 16635ad333ebSAndy Whitcroft * @mode: One of the LRU isolation modes 16643cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 16651da177e4SLinus Torvalds * 16661da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 16671da177e4SLinus Torvalds */ 166869e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 16695dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1670fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 1671a9e7c39fSKirill Tkhai enum lru_list lru) 16721da177e4SLinus Torvalds { 167375b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 167469e05944SAndrew Morton unsigned long nr_taken = 0; 1675599d0c95SMel Gorman unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; 16767cc30fcfSMel Gorman unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; 16773db65812SJohannes Weiner unsigned long skipped = 0; 1678791b48b6SMinchan Kim unsigned long scan, total_scan, nr_pages; 1679b2e18757SMel Gorman LIST_HEAD(pages_skipped); 1680a9e7c39fSKirill Tkhai isolate_mode_t mode = (sc->may_unmap ? 0 : ISOLATE_UNMAPPED); 16811da177e4SLinus Torvalds 168298879b3bSYang Shi total_scan = 0; 1683791b48b6SMinchan Kim scan = 0; 168498879b3bSYang Shi while (scan < nr_to_scan && !list_empty(src)) { 16855ad333ebSAndy Whitcroft struct page *page; 16865ad333ebSAndy Whitcroft 16871da177e4SLinus Torvalds page = lru_to_page(src); 16881da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 16891da177e4SLinus Torvalds 1690309381feSSasha Levin VM_BUG_ON_PAGE(!PageLRU(page), page); 16918d438f96SNick Piggin 169298879b3bSYang Shi nr_pages = 1 << compound_order(page); 169398879b3bSYang Shi total_scan += nr_pages; 169498879b3bSYang Shi 1695b2e18757SMel Gorman if (page_zonenum(page) > sc->reclaim_idx) { 1696b2e18757SMel Gorman list_move(&page->lru, &pages_skipped); 169798879b3bSYang Shi nr_skipped[page_zonenum(page)] += nr_pages; 1698b2e18757SMel Gorman continue; 1699b2e18757SMel Gorman } 1700b2e18757SMel Gorman 1701791b48b6SMinchan Kim /* 1702791b48b6SMinchan Kim * Do not count skipped pages because that makes the function 1703791b48b6SMinchan Kim * return with no isolated pages if the LRU mostly contains 1704791b48b6SMinchan Kim * ineligible pages. This causes the VM to not reclaim any 1705791b48b6SMinchan Kim * pages, triggering a premature OOM. 170698879b3bSYang Shi * 170798879b3bSYang Shi * Account all tail pages of THP. This would not cause 170898879b3bSYang Shi * premature OOM since __isolate_lru_page() returns -EBUSY 170998879b3bSYang Shi * only when the page is being freed somewhere else. 1710791b48b6SMinchan Kim */ 171198879b3bSYang Shi scan += nr_pages; 1712f3fd4a61SKonstantin Khlebnikov switch (__isolate_lru_page(page, mode)) { 17135ad333ebSAndy Whitcroft case 0: 1714599d0c95SMel Gorman nr_taken += nr_pages; 1715599d0c95SMel Gorman nr_zone_taken[page_zonenum(page)] += nr_pages; 17165ad333ebSAndy Whitcroft list_move(&page->lru, dst); 17175ad333ebSAndy Whitcroft break; 17187c8ee9a8SNick Piggin 17195ad333ebSAndy Whitcroft case -EBUSY: 17205ad333ebSAndy Whitcroft /* else it is being freed elsewhere */ 17215ad333ebSAndy Whitcroft list_move(&page->lru, src); 17225ad333ebSAndy Whitcroft continue; 17235ad333ebSAndy Whitcroft 17245ad333ebSAndy Whitcroft default: 17255ad333ebSAndy Whitcroft BUG(); 17265ad333ebSAndy Whitcroft } 17275ad333ebSAndy Whitcroft } 17281da177e4SLinus Torvalds 1729b2e18757SMel Gorman /* 1730b2e18757SMel Gorman * Splice any skipped pages to the start of the LRU list. Note that 1731b2e18757SMel Gorman * this disrupts the LRU order when reclaiming for lower zones but 1732b2e18757SMel Gorman * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX 1733b2e18757SMel Gorman * scanning would soon rescan the same pages to skip and put the 1734b2e18757SMel Gorman * system at risk of premature OOM. 1735b2e18757SMel Gorman */ 17367cc30fcfSMel Gorman if (!list_empty(&pages_skipped)) { 17377cc30fcfSMel Gorman int zid; 17387cc30fcfSMel Gorman 17393db65812SJohannes Weiner list_splice(&pages_skipped, src); 17407cc30fcfSMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 17417cc30fcfSMel Gorman if (!nr_skipped[zid]) 17427cc30fcfSMel Gorman continue; 17437cc30fcfSMel Gorman 17447cc30fcfSMel Gorman __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); 17451265e3a6SMichal Hocko skipped += nr_skipped[zid]; 17467cc30fcfSMel Gorman } 17477cc30fcfSMel Gorman } 1748791b48b6SMinchan Kim *nr_scanned = total_scan; 17491265e3a6SMichal Hocko trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, 1750791b48b6SMinchan Kim total_scan, skipped, nr_taken, mode, lru); 1751b4536f0cSMichal Hocko update_lru_sizes(lruvec, lru, nr_zone_taken); 17521da177e4SLinus Torvalds return nr_taken; 17531da177e4SLinus Torvalds } 17541da177e4SLinus Torvalds 175562695a84SNick Piggin /** 175662695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 175762695a84SNick Piggin * @page: page to isolate from its LRU list 175862695a84SNick Piggin * 175962695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 176062695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 176162695a84SNick Piggin * 176262695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 176362695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 176462695a84SNick Piggin * 176562695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1766894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1767894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1768894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 176962695a84SNick Piggin * 177062695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 177162695a84SNick Piggin * found will be decremented. 177262695a84SNick Piggin * 177362695a84SNick Piggin * Restrictions: 1774a5d09bedSMike Rapoport * 177562695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 177662695a84SNick Piggin * fundamentnal difference from isolate_lru_pages (which is called 177762695a84SNick Piggin * without a stable reference). 177862695a84SNick Piggin * (2) the lru_lock must not be held. 177962695a84SNick Piggin * (3) interrupts must be enabled. 178062695a84SNick Piggin */ 178162695a84SNick Piggin int isolate_lru_page(struct page *page) 178262695a84SNick Piggin { 178362695a84SNick Piggin int ret = -EBUSY; 178462695a84SNick Piggin 1785309381feSSasha Levin VM_BUG_ON_PAGE(!page_count(page), page); 1786cf2a82eeSKirill A. Shutemov WARN_RATELIMIT(PageTail(page), "trying to isolate tail page"); 17870c917313SKonstantin Khlebnikov 178862695a84SNick Piggin if (PageLRU(page)) { 1789f4b7e272SAndrey Ryabinin pg_data_t *pgdat = page_pgdat(page); 1790fa9add64SHugh Dickins struct lruvec *lruvec; 179162695a84SNick Piggin 1792f4b7e272SAndrey Ryabinin spin_lock_irq(&pgdat->lru_lock); 1793f4b7e272SAndrey Ryabinin lruvec = mem_cgroup_page_lruvec(page, pgdat); 17940c917313SKonstantin Khlebnikov if (PageLRU(page)) { 1795894bc310SLee Schermerhorn int lru = page_lru(page); 17960c917313SKonstantin Khlebnikov get_page(page); 179762695a84SNick Piggin ClearPageLRU(page); 1798fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 1799fa9add64SHugh Dickins ret = 0; 180062695a84SNick Piggin } 1801f4b7e272SAndrey Ryabinin spin_unlock_irq(&pgdat->lru_lock); 180262695a84SNick Piggin } 180362695a84SNick Piggin return ret; 180462695a84SNick Piggin } 180562695a84SNick Piggin 18065ad333ebSAndy Whitcroft /* 1807d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 1808d37dd5dcSFengguang Wu * then get resheduled. When there are massive number of tasks doing page 1809d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 1810d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 1811d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 181235cd7815SRik van Riel */ 1813599d0c95SMel Gorman static int too_many_isolated(struct pglist_data *pgdat, int file, 181435cd7815SRik van Riel struct scan_control *sc) 181535cd7815SRik van Riel { 181635cd7815SRik van Riel unsigned long inactive, isolated; 181735cd7815SRik van Riel 181835cd7815SRik van Riel if (current_is_kswapd()) 181935cd7815SRik van Riel return 0; 182035cd7815SRik van Riel 182197c9341fSTejun Heo if (!sane_reclaim(sc)) 182235cd7815SRik van Riel return 0; 182335cd7815SRik van Riel 182435cd7815SRik van Riel if (file) { 1825599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_FILE); 1826599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_FILE); 182735cd7815SRik van Riel } else { 1828599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_ANON); 1829599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_ANON); 183035cd7815SRik van Riel } 183135cd7815SRik van Riel 18323cf23841SFengguang Wu /* 18333cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 18343cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 18353cf23841SFengguang Wu * deadlock. 18363cf23841SFengguang Wu */ 1837d0164adcSMel Gorman if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) 18383cf23841SFengguang Wu inactive >>= 3; 18393cf23841SFengguang Wu 184035cd7815SRik van Riel return isolated > inactive; 184135cd7815SRik van Riel } 184235cd7815SRik van Riel 1843a222f341SKirill Tkhai /* 1844a222f341SKirill Tkhai * This moves pages from @list to corresponding LRU list. 1845a222f341SKirill Tkhai * 1846a222f341SKirill Tkhai * We move them the other way if the page is referenced by one or more 1847a222f341SKirill Tkhai * processes, from rmap. 1848a222f341SKirill Tkhai * 1849a222f341SKirill Tkhai * If the pages are mostly unmapped, the processing is fast and it is 1850a222f341SKirill Tkhai * appropriate to hold zone_lru_lock across the whole operation. But if 1851a222f341SKirill Tkhai * the pages are mapped, the processing is slow (page_referenced()) so we 1852a222f341SKirill Tkhai * should drop zone_lru_lock around each page. It's impossible to balance 1853a222f341SKirill Tkhai * this, so instead we remove the pages from the LRU while processing them. 1854a222f341SKirill Tkhai * It is safe to rely on PG_active against the non-LRU pages in here because 1855a222f341SKirill Tkhai * nobody will play with that bit on a non-LRU page. 1856a222f341SKirill Tkhai * 1857a222f341SKirill Tkhai * The downside is that we have to touch page->_refcount against each page. 1858a222f341SKirill Tkhai * But we had to alter page->flags anyway. 1859a222f341SKirill Tkhai * 1860a222f341SKirill Tkhai * Returns the number of pages moved to the given lruvec. 1861a222f341SKirill Tkhai */ 1862a222f341SKirill Tkhai 1863a222f341SKirill Tkhai static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec, 1864a222f341SKirill Tkhai struct list_head *list) 186566635629SMel Gorman { 1866599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 1867a222f341SKirill Tkhai int nr_pages, nr_moved = 0; 18683f79768fSHugh Dickins LIST_HEAD(pages_to_free); 1869a222f341SKirill Tkhai struct page *page; 1870a222f341SKirill Tkhai enum lru_list lru; 187166635629SMel Gorman 1872a222f341SKirill Tkhai while (!list_empty(list)) { 1873a222f341SKirill Tkhai page = lru_to_page(list); 1874309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 187539b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 1876a222f341SKirill Tkhai list_del(&page->lru); 1877599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 187866635629SMel Gorman putback_lru_page(page); 1879599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 188066635629SMel Gorman continue; 188166635629SMel Gorman } 1882599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 1883fa9add64SHugh Dickins 18847a608572SLinus Torvalds SetPageLRU(page); 188566635629SMel Gorman lru = page_lru(page); 1886a222f341SKirill Tkhai 1887a222f341SKirill Tkhai nr_pages = hpage_nr_pages(page); 1888a222f341SKirill Tkhai update_lru_size(lruvec, lru, page_zonenum(page), nr_pages); 1889a222f341SKirill Tkhai list_move(&page->lru, &lruvec->lists[lru]); 1890fa9add64SHugh Dickins 18912bcf8879SHugh Dickins if (put_page_testzero(page)) { 18922bcf8879SHugh Dickins __ClearPageLRU(page); 18932bcf8879SHugh Dickins __ClearPageActive(page); 1894fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 18952bcf8879SHugh Dickins 18962bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 1897599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1898747db954SJohannes Weiner mem_cgroup_uncharge(page); 18992bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 1900599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 19012bcf8879SHugh Dickins } else 19022bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 1903a222f341SKirill Tkhai } else { 1904a222f341SKirill Tkhai nr_moved += nr_pages; 190566635629SMel Gorman } 190666635629SMel Gorman } 190766635629SMel Gorman 19083f79768fSHugh Dickins /* 19093f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 19103f79768fSHugh Dickins */ 1911a222f341SKirill Tkhai list_splice(&pages_to_free, list); 1912a222f341SKirill Tkhai 1913a222f341SKirill Tkhai return nr_moved; 191466635629SMel Gorman } 191566635629SMel Gorman 191666635629SMel Gorman /* 1917399ba0b9SNeilBrown * If a kernel thread (such as nfsd for loop-back mounts) services 1918399ba0b9SNeilBrown * a backing device by writing to the page cache it sets PF_LESS_THROTTLE. 1919399ba0b9SNeilBrown * In that case we should only throttle if the backing device it is 1920399ba0b9SNeilBrown * writing to is congested. In other cases it is safe to throttle. 1921399ba0b9SNeilBrown */ 1922399ba0b9SNeilBrown static int current_may_throttle(void) 1923399ba0b9SNeilBrown { 1924399ba0b9SNeilBrown return !(current->flags & PF_LESS_THROTTLE) || 1925399ba0b9SNeilBrown current->backing_dev_info == NULL || 1926399ba0b9SNeilBrown bdi_write_congested(current->backing_dev_info); 1927399ba0b9SNeilBrown } 1928399ba0b9SNeilBrown 1929399ba0b9SNeilBrown /* 1930b2e18757SMel Gorman * shrink_inactive_list() is a helper for shrink_node(). It returns the number 19311742f19fSAndrew Morton * of reclaimed pages 19321da177e4SLinus Torvalds */ 193366635629SMel Gorman static noinline_for_stack unsigned long 19341a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 19359e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 19361da177e4SLinus Torvalds { 19371da177e4SLinus Torvalds LIST_HEAD(page_list); 1938e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 193905ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 1940e247dbceSKOSAKI Motohiro unsigned long nr_taken; 1941060f005fSKirill Tkhai struct reclaim_stat stat; 19423cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 1943f46b7912SKirill Tkhai enum vm_event_item item; 1944599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 19451a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 1946db73ee0dSMichal Hocko bool stalled = false; 194778dc583dSKOSAKI Motohiro 1948599d0c95SMel Gorman while (unlikely(too_many_isolated(pgdat, file, sc))) { 1949db73ee0dSMichal Hocko if (stalled) 1950db73ee0dSMichal Hocko return 0; 1951db73ee0dSMichal Hocko 1952db73ee0dSMichal Hocko /* wait a bit for the reclaimer. */ 1953db73ee0dSMichal Hocko msleep(100); 1954db73ee0dSMichal Hocko stalled = true; 195535cd7815SRik van Riel 195635cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 195735cd7815SRik van Riel if (fatal_signal_pending(current)) 195835cd7815SRik van Riel return SWAP_CLUSTER_MAX; 195935cd7815SRik van Riel } 196035cd7815SRik van Riel 19611da177e4SLinus Torvalds lru_add_drain(); 1962f80c0673SMinchan Kim 1963599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 19641da177e4SLinus Torvalds 19655dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 1966a9e7c39fSKirill Tkhai &nr_scanned, sc, lru); 196795d918fcSKonstantin Khlebnikov 1968599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 19699d5e6a9fSHugh Dickins reclaim_stat->recent_scanned[file] += nr_taken; 197095d918fcSKonstantin Khlebnikov 1971f46b7912SKirill Tkhai item = current_is_kswapd() ? PGSCAN_KSWAPD : PGSCAN_DIRECT; 19722262185cSRoman Gushchin if (global_reclaim(sc)) 1973f46b7912SKirill Tkhai __count_vm_events(item, nr_scanned); 1974f46b7912SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned); 1975599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1976d563c050SHillf Danton 1977d563c050SHillf Danton if (nr_taken == 0) 197866635629SMel Gorman return 0; 1979b35ea17bSKOSAKI Motohiro 1980a128ca71SShaohua Li nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, 0, 19813c710c1aSMichal Hocko &stat, false); 1982c661b078SAndy Whitcroft 1983599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 19843f79768fSHugh Dickins 1985f46b7912SKirill Tkhai item = current_is_kswapd() ? PGSTEAL_KSWAPD : PGSTEAL_DIRECT; 19862262185cSRoman Gushchin if (global_reclaim(sc)) 1987f46b7912SKirill Tkhai __count_vm_events(item, nr_reclaimed); 1988f46b7912SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed); 1989b17f18afSKirill Tkhai reclaim_stat->recent_rotated[0] += stat.nr_activate[0]; 1990b17f18afSKirill Tkhai reclaim_stat->recent_rotated[1] += stat.nr_activate[1]; 1991a74609faSNick Piggin 1992a222f341SKirill Tkhai move_pages_to_lru(lruvec, &page_list); 19933f79768fSHugh Dickins 1994599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 19953f79768fSHugh Dickins 1996599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 19973f79768fSHugh Dickins 1998747db954SJohannes Weiner mem_cgroup_uncharge_list(&page_list); 19992d4894b5SMel Gorman free_unref_page_list(&page_list); 2000e11da5b4SMel Gorman 200192df3a72SMel Gorman /* 20021c610d5fSAndrey Ryabinin * If dirty pages are scanned that are not queued for IO, it 20031c610d5fSAndrey Ryabinin * implies that flushers are not doing their job. This can 20041c610d5fSAndrey Ryabinin * happen when memory pressure pushes dirty pages to the end of 20051c610d5fSAndrey Ryabinin * the LRU before the dirty limits are breached and the dirty 20061c610d5fSAndrey Ryabinin * data has expired. It can also happen when the proportion of 20071c610d5fSAndrey Ryabinin * dirty pages grows not through writes but through memory 20081c610d5fSAndrey Ryabinin * pressure reclaiming all the clean cache. And in some cases, 20091c610d5fSAndrey Ryabinin * the flushers simply cannot keep up with the allocation 20101c610d5fSAndrey Ryabinin * rate. Nudge the flusher threads in case they are asleep. 20111c610d5fSAndrey Ryabinin */ 20121c610d5fSAndrey Ryabinin if (stat.nr_unqueued_dirty == nr_taken) 20131c610d5fSAndrey Ryabinin wakeup_flusher_threads(WB_REASON_VMSCAN); 20141c610d5fSAndrey Ryabinin 2015d108c772SAndrey Ryabinin sc->nr.dirty += stat.nr_dirty; 2016d108c772SAndrey Ryabinin sc->nr.congested += stat.nr_congested; 2017d108c772SAndrey Ryabinin sc->nr.unqueued_dirty += stat.nr_unqueued_dirty; 2018d108c772SAndrey Ryabinin sc->nr.writeback += stat.nr_writeback; 2019d108c772SAndrey Ryabinin sc->nr.immediate += stat.nr_immediate; 2020d108c772SAndrey Ryabinin sc->nr.taken += nr_taken; 2021d108c772SAndrey Ryabinin if (file) 2022d108c772SAndrey Ryabinin sc->nr.file_taken += nr_taken; 20238e950282SMel Gorman 2024599d0c95SMel Gorman trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, 2025d51d1e64SSteven Rostedt nr_scanned, nr_reclaimed, &stat, sc->priority, file); 202605ff5137SAndrew Morton return nr_reclaimed; 20271da177e4SLinus Torvalds } 20281da177e4SLinus Torvalds 2029f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 20301a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 2031f16015fbSJohannes Weiner struct scan_control *sc, 20329e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 20331cfb419bSKAMEZAWA Hiroyuki { 203444c241f1SKOSAKI Motohiro unsigned long nr_taken; 2035f626012dSHugh Dickins unsigned long nr_scanned; 20366fe6b7e3SWu Fengguang unsigned long vm_flags; 20371cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 20388cab4754SWu Fengguang LIST_HEAD(l_active); 2039b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 20401cfb419bSKAMEZAWA Hiroyuki struct page *page; 20411a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 20429d998b4fSMichal Hocko unsigned nr_deactivate, nr_activate; 20439d998b4fSMichal Hocko unsigned nr_rotated = 0; 20443cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 2045599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 20461cfb419bSKAMEZAWA Hiroyuki 20471da177e4SLinus Torvalds lru_add_drain(); 2048f80c0673SMinchan Kim 2049599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 2050925b7673SJohannes Weiner 20515dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 2052a9e7c39fSKirill Tkhai &nr_scanned, sc, lru); 205389b5fae5SJohannes Weiner 2054599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 2055b7c46d15SJohannes Weiner reclaim_stat->recent_scanned[file] += nr_taken; 20561cfb419bSKAMEZAWA Hiroyuki 2057599d0c95SMel Gorman __count_vm_events(PGREFILL, nr_scanned); 20582fa2690cSYafang Shao __count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned); 20599d5e6a9fSHugh Dickins 2060599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 20611da177e4SLinus Torvalds 20621da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 20631da177e4SLinus Torvalds cond_resched(); 20641da177e4SLinus Torvalds page = lru_to_page(&l_hold); 20651da177e4SLinus Torvalds list_del(&page->lru); 20667e9cd484SRik van Riel 206739b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 2068894bc310SLee Schermerhorn putback_lru_page(page); 2069894bc310SLee Schermerhorn continue; 2070894bc310SLee Schermerhorn } 2071894bc310SLee Schermerhorn 2072cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 2073cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 2074cc715d99SMel Gorman if (page_has_private(page)) 2075cc715d99SMel Gorman try_to_release_page(page, 0); 2076cc715d99SMel Gorman unlock_page(page); 2077cc715d99SMel Gorman } 2078cc715d99SMel Gorman } 2079cc715d99SMel Gorman 2080c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 2081c3ac9a8aSJohannes Weiner &vm_flags)) { 20829992af10SRik van Riel nr_rotated += hpage_nr_pages(page); 20838cab4754SWu Fengguang /* 20848cab4754SWu Fengguang * Identify referenced, file-backed active pages and 20858cab4754SWu Fengguang * give them one more trip around the active list. So 20868cab4754SWu Fengguang * that executable code get better chances to stay in 20878cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 20888cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 20898cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 20908cab4754SWu Fengguang * so we ignore them here. 20918cab4754SWu Fengguang */ 209241e20983SWu Fengguang if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { 20938cab4754SWu Fengguang list_add(&page->lru, &l_active); 20948cab4754SWu Fengguang continue; 20958cab4754SWu Fengguang } 20968cab4754SWu Fengguang } 20977e9cd484SRik van Riel 20985205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 20991899ad18SJohannes Weiner SetPageWorkingset(page); 21001da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 21011da177e4SLinus Torvalds } 21021da177e4SLinus Torvalds 2103b555749aSAndrew Morton /* 21048cab4754SWu Fengguang * Move pages back to the lru list. 2105b555749aSAndrew Morton */ 2106599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 21074f98a2feSRik van Riel /* 21088cab4754SWu Fengguang * Count referenced pages from currently used mappings as rotated, 21098cab4754SWu Fengguang * even though only some of them are actually re-activated. This 21108cab4754SWu Fengguang * helps balance scan pressure between file and anonymous pages in 21117c0db9e9SJerome Marchand * get_scan_count. 2112556adecbSRik van Riel */ 2113b7c46d15SJohannes Weiner reclaim_stat->recent_rotated[file] += nr_rotated; 2114556adecbSRik van Riel 2115a222f341SKirill Tkhai nr_activate = move_pages_to_lru(lruvec, &l_active); 2116a222f341SKirill Tkhai nr_deactivate = move_pages_to_lru(lruvec, &l_inactive); 2117f372d89eSKirill Tkhai /* Keep all free pages in l_active list */ 2118f372d89eSKirill Tkhai list_splice(&l_inactive, &l_active); 21199851ac13SKirill Tkhai 21209851ac13SKirill Tkhai __count_vm_events(PGDEACTIVATE, nr_deactivate); 21219851ac13SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate); 21229851ac13SKirill Tkhai 2123599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 2124599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 21252bcf8879SHugh Dickins 2126f372d89eSKirill Tkhai mem_cgroup_uncharge_list(&l_active); 2127f372d89eSKirill Tkhai free_unref_page_list(&l_active); 21289d998b4fSMichal Hocko trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate, 21299d998b4fSMichal Hocko nr_deactivate, nr_rotated, sc->priority, file); 21301da177e4SLinus Torvalds } 21311da177e4SLinus Torvalds 213259dc76b0SRik van Riel /* 213359dc76b0SRik van Riel * The inactive anon list should be small enough that the VM never has 213459dc76b0SRik van Riel * to do too much work. 213514797e23SKOSAKI Motohiro * 213659dc76b0SRik van Riel * The inactive file list should be small enough to leave most memory 213759dc76b0SRik van Riel * to the established workingset on the scan-resistant active list, 213859dc76b0SRik van Riel * but large enough to avoid thrashing the aggregate readahead window. 213959dc76b0SRik van Riel * 214059dc76b0SRik van Riel * Both inactive lists should also be large enough that each inactive 214159dc76b0SRik van Riel * page has a chance to be referenced again before it is reclaimed. 214259dc76b0SRik van Riel * 21432a2e4885SJohannes Weiner * If that fails and refaulting is observed, the inactive list grows. 21442a2e4885SJohannes Weiner * 214559dc76b0SRik van Riel * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages 21463a50d14dSAndrey Ryabinin * on this LRU, maintained by the pageout code. An inactive_ratio 214759dc76b0SRik van Riel * of 3 means 3:1 or 25% of the pages are kept on the inactive list. 214859dc76b0SRik van Riel * 214959dc76b0SRik van Riel * total target max 215059dc76b0SRik van Riel * memory ratio inactive 215159dc76b0SRik van Riel * ------------------------------------- 215259dc76b0SRik van Riel * 10MB 1 5MB 215359dc76b0SRik van Riel * 100MB 1 50MB 215459dc76b0SRik van Riel * 1GB 3 250MB 215559dc76b0SRik van Riel * 10GB 10 0.9GB 215659dc76b0SRik van Riel * 100GB 31 3GB 215759dc76b0SRik van Riel * 1TB 101 10GB 215859dc76b0SRik van Riel * 10TB 320 32GB 215914797e23SKOSAKI Motohiro */ 2160f8d1a311SMel Gorman static bool inactive_list_is_low(struct lruvec *lruvec, bool file, 21612c012a4aSKuo-Hsin Yang struct scan_control *sc, bool trace) 216214797e23SKOSAKI Motohiro { 2163fd538803SMichal Hocko enum lru_list active_lru = file * LRU_FILE + LRU_ACTIVE; 21642a2e4885SJohannes Weiner struct pglist_data *pgdat = lruvec_pgdat(lruvec); 21652a2e4885SJohannes Weiner enum lru_list inactive_lru = file * LRU_FILE; 21662a2e4885SJohannes Weiner unsigned long inactive, active; 21672a2e4885SJohannes Weiner unsigned long inactive_ratio; 21682a2e4885SJohannes Weiner unsigned long refaults; 216959dc76b0SRik van Riel unsigned long gb; 217059dc76b0SRik van Riel 217174e3f3c3SMinchan Kim /* 217274e3f3c3SMinchan Kim * If we don't have swap space, anonymous page deactivation 217374e3f3c3SMinchan Kim * is pointless. 217474e3f3c3SMinchan Kim */ 217559dc76b0SRik van Riel if (!file && !total_swap_pages) 217642e2e457SYaowei Bai return false; 217774e3f3c3SMinchan Kim 2178fd538803SMichal Hocko inactive = lruvec_lru_size(lruvec, inactive_lru, sc->reclaim_idx); 2179fd538803SMichal Hocko active = lruvec_lru_size(lruvec, active_lru, sc->reclaim_idx); 2180f8d1a311SMel Gorman 21812a2e4885SJohannes Weiner /* 21822a2e4885SJohannes Weiner * When refaults are being observed, it means a new workingset 21832a2e4885SJohannes Weiner * is being established. Disable active list protection to get 21842a2e4885SJohannes Weiner * rid of the stale workingset quickly. 21852a2e4885SJohannes Weiner */ 2186205b20ccSJohannes Weiner refaults = lruvec_page_state_local(lruvec, WORKINGSET_ACTIVATE); 21872c012a4aSKuo-Hsin Yang if (file && lruvec->refaults != refaults) { 21882a2e4885SJohannes Weiner inactive_ratio = 0; 21892a2e4885SJohannes Weiner } else { 219059dc76b0SRik van Riel gb = (inactive + active) >> (30 - PAGE_SHIFT); 219159dc76b0SRik van Riel if (gb) 219259dc76b0SRik van Riel inactive_ratio = int_sqrt(10 * gb); 2193b39415b2SRik van Riel else 219459dc76b0SRik van Riel inactive_ratio = 1; 21952a2e4885SJohannes Weiner } 219659dc76b0SRik van Riel 21972c012a4aSKuo-Hsin Yang if (trace) 21982a2e4885SJohannes Weiner trace_mm_vmscan_inactive_list_is_low(pgdat->node_id, sc->reclaim_idx, 2199fd538803SMichal Hocko lruvec_lru_size(lruvec, inactive_lru, MAX_NR_ZONES), inactive, 2200fd538803SMichal Hocko lruvec_lru_size(lruvec, active_lru, MAX_NR_ZONES), active, 2201fd538803SMichal Hocko inactive_ratio, file); 2202fd538803SMichal Hocko 220359dc76b0SRik van Riel return inactive * inactive_ratio < active; 2204b39415b2SRik van Riel } 2205b39415b2SRik van Riel 22064f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 22073b991208SJohannes Weiner struct lruvec *lruvec, struct scan_control *sc) 2208b69408e8SChristoph Lameter { 2209b39415b2SRik van Riel if (is_active_lru(lru)) { 22103b991208SJohannes Weiner if (inactive_list_is_low(lruvec, is_file_lru(lru), sc, true)) 22111a93be0eSKonstantin Khlebnikov shrink_active_list(nr_to_scan, lruvec, sc, lru); 2212556adecbSRik van Riel return 0; 2213556adecbSRik van Riel } 2214556adecbSRik van Riel 22151a93be0eSKonstantin Khlebnikov return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 2216b69408e8SChristoph Lameter } 2217b69408e8SChristoph Lameter 22189a265114SJohannes Weiner enum scan_balance { 22199a265114SJohannes Weiner SCAN_EQUAL, 22209a265114SJohannes Weiner SCAN_FRACT, 22219a265114SJohannes Weiner SCAN_ANON, 22229a265114SJohannes Weiner SCAN_FILE, 22239a265114SJohannes Weiner }; 22249a265114SJohannes Weiner 22251da177e4SLinus Torvalds /* 22264f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 22274f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 22284f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 22294f98a2feSRik van Riel * onto the active list instead of evict. 22304f98a2feSRik van Riel * 2231be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 2232be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 22334f98a2feSRik van Riel */ 223433377678SVladimir Davydov static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, 22356b4f7799SJohannes Weiner struct scan_control *sc, unsigned long *nr, 22366b4f7799SJohannes Weiner unsigned long *lru_pages) 22374f98a2feSRik van Riel { 223833377678SVladimir Davydov int swappiness = mem_cgroup_swappiness(memcg); 223990126375SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 22409a265114SJohannes Weiner u64 fraction[2]; 22419a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 2242599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 22439a265114SJohannes Weiner unsigned long anon_prio, file_prio; 22449a265114SJohannes Weiner enum scan_balance scan_balance; 22450bf1457fSJohannes Weiner unsigned long anon, file; 22469a265114SJohannes Weiner unsigned long ap, fp; 22479a265114SJohannes Weiner enum lru_list lru; 224876a33fc3SShaohua Li 224976a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 2250d8b38438SVladimir Davydov if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) { 22519a265114SJohannes Weiner scan_balance = SCAN_FILE; 225276a33fc3SShaohua Li goto out; 225376a33fc3SShaohua Li } 22544f98a2feSRik van Riel 225510316b31SJohannes Weiner /* 225610316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 225710316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 225810316b31SJohannes Weiner * disable swapping for individual groups completely when 225910316b31SJohannes Weiner * using the memory controller's swap limit feature would be 226010316b31SJohannes Weiner * too expensive. 226110316b31SJohannes Weiner */ 226202695175SJohannes Weiner if (!global_reclaim(sc) && !swappiness) { 22639a265114SJohannes Weiner scan_balance = SCAN_FILE; 226410316b31SJohannes Weiner goto out; 226510316b31SJohannes Weiner } 226610316b31SJohannes Weiner 226710316b31SJohannes Weiner /* 226810316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 226910316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 227010316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 227110316b31SJohannes Weiner */ 227202695175SJohannes Weiner if (!sc->priority && swappiness) { 22739a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 227410316b31SJohannes Weiner goto out; 227510316b31SJohannes Weiner } 227610316b31SJohannes Weiner 227711d16c25SJohannes Weiner /* 227862376251SJohannes Weiner * Prevent the reclaimer from falling into the cache trap: as 227962376251SJohannes Weiner * cache pages start out inactive, every cache fault will tip 228062376251SJohannes Weiner * the scan balance towards the file LRU. And as the file LRU 228162376251SJohannes Weiner * shrinks, so does the window for rotation from references. 228262376251SJohannes Weiner * This means we have a runaway feedback loop where a tiny 228362376251SJohannes Weiner * thrashing file LRU becomes infinitely more attractive than 228462376251SJohannes Weiner * anon pages. Try to detect this based on file LRU size. 228562376251SJohannes Weiner */ 228662376251SJohannes Weiner if (global_reclaim(sc)) { 2287599d0c95SMel Gorman unsigned long pgdatfile; 2288599d0c95SMel Gorman unsigned long pgdatfree; 2289599d0c95SMel Gorman int z; 2290599d0c95SMel Gorman unsigned long total_high_wmark = 0; 229162376251SJohannes Weiner 2292599d0c95SMel Gorman pgdatfree = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); 2293599d0c95SMel Gorman pgdatfile = node_page_state(pgdat, NR_ACTIVE_FILE) + 2294599d0c95SMel Gorman node_page_state(pgdat, NR_INACTIVE_FILE); 22952ab051e1SJerome Marchand 2296599d0c95SMel Gorman for (z = 0; z < MAX_NR_ZONES; z++) { 2297599d0c95SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 22986aa303deSMel Gorman if (!managed_zone(zone)) 2299599d0c95SMel Gorman continue; 2300599d0c95SMel Gorman 2301599d0c95SMel Gorman total_high_wmark += high_wmark_pages(zone); 2302599d0c95SMel Gorman } 2303599d0c95SMel Gorman 2304599d0c95SMel Gorman if (unlikely(pgdatfile + pgdatfree <= total_high_wmark)) { 230506226226SDavid Rientjes /* 230606226226SDavid Rientjes * Force SCAN_ANON if there are enough inactive 230706226226SDavid Rientjes * anonymous pages on the LRU in eligible zones. 230806226226SDavid Rientjes * Otherwise, the small LRU gets thrashed. 230906226226SDavid Rientjes */ 23103b991208SJohannes Weiner if (!inactive_list_is_low(lruvec, false, sc, false) && 231106226226SDavid Rientjes lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, sc->reclaim_idx) 231206226226SDavid Rientjes >> sc->priority) { 231362376251SJohannes Weiner scan_balance = SCAN_ANON; 231462376251SJohannes Weiner goto out; 231562376251SJohannes Weiner } 231662376251SJohannes Weiner } 231706226226SDavid Rientjes } 231862376251SJohannes Weiner 231962376251SJohannes Weiner /* 2320316bda0eSVladimir Davydov * If there is enough inactive page cache, i.e. if the size of the 2321316bda0eSVladimir Davydov * inactive list is greater than that of the active list *and* the 2322316bda0eSVladimir Davydov * inactive list actually has some pages to scan on this priority, we 2323316bda0eSVladimir Davydov * do not reclaim anything from the anonymous working set right now. 2324316bda0eSVladimir Davydov * Without the second condition we could end up never scanning an 2325316bda0eSVladimir Davydov * lruvec even if it has plenty of old anonymous pages unless the 2326316bda0eSVladimir Davydov * system is under heavy pressure. 2327e9868505SRik van Riel */ 23283b991208SJohannes Weiner if (!inactive_list_is_low(lruvec, true, sc, false) && 232971ab6cfeSMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, sc->reclaim_idx) >> sc->priority) { 23309a265114SJohannes Weiner scan_balance = SCAN_FILE; 2331e9868505SRik van Riel goto out; 23324f98a2feSRik van Riel } 23334f98a2feSRik van Riel 23349a265114SJohannes Weiner scan_balance = SCAN_FRACT; 23359a265114SJohannes Weiner 23364f98a2feSRik van Riel /* 233758c37f6eSKOSAKI Motohiro * With swappiness at 100, anonymous and file have the same priority. 233858c37f6eSKOSAKI Motohiro * This scanning priority is essentially the inverse of IO cost. 233958c37f6eSKOSAKI Motohiro */ 234002695175SJohannes Weiner anon_prio = swappiness; 234175b00af7SHugh Dickins file_prio = 200 - anon_prio; 234258c37f6eSKOSAKI Motohiro 234358c37f6eSKOSAKI Motohiro /* 23444f98a2feSRik van Riel * OK, so we have swap space and a fair amount of page cache 23454f98a2feSRik van Riel * pages. We use the recently rotated / recently scanned 23464f98a2feSRik van Riel * ratios to determine how valuable each cache is. 23474f98a2feSRik van Riel * 23484f98a2feSRik van Riel * Because workloads change over time (and to avoid overflow) 23494f98a2feSRik van Riel * we keep these statistics as a floating average, which ends 23504f98a2feSRik van Riel * up weighing recent references more than old ones. 23514f98a2feSRik van Riel * 23524f98a2feSRik van Riel * anon in [0], file in [1] 23534f98a2feSRik van Riel */ 23542ab051e1SJerome Marchand 2355fd538803SMichal Hocko anon = lruvec_lru_size(lruvec, LRU_ACTIVE_ANON, MAX_NR_ZONES) + 2356fd538803SMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, MAX_NR_ZONES); 2357fd538803SMichal Hocko file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES) + 2358fd538803SMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, MAX_NR_ZONES); 23592ab051e1SJerome Marchand 2360599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 236158c37f6eSKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { 23626e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[0] /= 2; 23636e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[0] /= 2; 23644f98a2feSRik van Riel } 23654f98a2feSRik van Riel 23666e901571SKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { 23676e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[1] /= 2; 23686e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[1] /= 2; 23694f98a2feSRik van Riel } 23704f98a2feSRik van Riel 23714f98a2feSRik van Riel /* 237200d8089cSRik van Riel * The amount of pressure on anon vs file pages is inversely 237300d8089cSRik van Riel * proportional to the fraction of recently scanned pages on 237400d8089cSRik van Riel * each list that were recently referenced and in active use. 23754f98a2feSRik van Riel */ 2376fe35004fSSatoru Moriya ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1); 23776e901571SKOSAKI Motohiro ap /= reclaim_stat->recent_rotated[0] + 1; 23784f98a2feSRik van Riel 2379fe35004fSSatoru Moriya fp = file_prio * (reclaim_stat->recent_scanned[1] + 1); 23806e901571SKOSAKI Motohiro fp /= reclaim_stat->recent_rotated[1] + 1; 2381599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 23824f98a2feSRik van Riel 238376a33fc3SShaohua Li fraction[0] = ap; 238476a33fc3SShaohua Li fraction[1] = fp; 238576a33fc3SShaohua Li denominator = ap + fp + 1; 238676a33fc3SShaohua Li out: 23876b4f7799SJohannes Weiner *lru_pages = 0; 23884111304dSHugh Dickins for_each_evictable_lru(lru) { 23894111304dSHugh Dickins int file = is_file_lru(lru); 2390d778df51SJohannes Weiner unsigned long size; 239176a33fc3SShaohua Li unsigned long scan; 239276a33fc3SShaohua Li 239371ab6cfeSMichal Hocko size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); 2394d778df51SJohannes Weiner scan = size >> sc->priority; 2395688035f7SJohannes Weiner /* 2396688035f7SJohannes Weiner * If the cgroup's already been deleted, make sure to 2397688035f7SJohannes Weiner * scrape out the remaining cache. 2398688035f7SJohannes Weiner */ 2399688035f7SJohannes Weiner if (!scan && !mem_cgroup_online(memcg)) 2400d778df51SJohannes Weiner scan = min(size, SWAP_CLUSTER_MAX); 24019a265114SJohannes Weiner 24029a265114SJohannes Weiner switch (scan_balance) { 24039a265114SJohannes Weiner case SCAN_EQUAL: 24049a265114SJohannes Weiner /* Scan lists relative to size */ 24059a265114SJohannes Weiner break; 24069a265114SJohannes Weiner case SCAN_FRACT: 24079a265114SJohannes Weiner /* 24089a265114SJohannes Weiner * Scan types proportional to swappiness and 24099a265114SJohannes Weiner * their relative recent reclaim efficiency. 241068600f62SRoman Gushchin * Make sure we don't miss the last page 241168600f62SRoman Gushchin * because of a round-off error. 24129a265114SJohannes Weiner */ 241368600f62SRoman Gushchin scan = DIV64_U64_ROUND_UP(scan * fraction[file], 24146f04f48dSSuleiman Souhlal denominator); 24159a265114SJohannes Weiner break; 24169a265114SJohannes Weiner case SCAN_FILE: 24179a265114SJohannes Weiner case SCAN_ANON: 24189a265114SJohannes Weiner /* Scan one type exclusively */ 24196b4f7799SJohannes Weiner if ((scan_balance == SCAN_FILE) != file) { 24206b4f7799SJohannes Weiner size = 0; 24219a265114SJohannes Weiner scan = 0; 24226b4f7799SJohannes Weiner } 24239a265114SJohannes Weiner break; 24249a265114SJohannes Weiner default: 24259a265114SJohannes Weiner /* Look ma, no brain */ 24269a265114SJohannes Weiner BUG(); 24279a265114SJohannes Weiner } 24286b4f7799SJohannes Weiner 24296b4f7799SJohannes Weiner *lru_pages += size; 24304111304dSHugh Dickins nr[lru] = scan; 243176a33fc3SShaohua Li } 24326e08a369SWu Fengguang } 24334f98a2feSRik van Riel 24349b4f98cdSJohannes Weiner /* 2435a9dd0a83SMel Gorman * This is a basic per-node page freer. Used by both kswapd and direct reclaim. 24369b4f98cdSJohannes Weiner */ 2437a9dd0a83SMel Gorman static void shrink_node_memcg(struct pglist_data *pgdat, struct mem_cgroup *memcg, 24386b4f7799SJohannes Weiner struct scan_control *sc, unsigned long *lru_pages) 24399b4f98cdSJohannes Weiner { 2440ef8f2327SMel Gorman struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg); 24419b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 2442e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 24439b4f98cdSJohannes Weiner unsigned long nr_to_scan; 24449b4f98cdSJohannes Weiner enum lru_list lru; 24459b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 24469b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 24479b4f98cdSJohannes Weiner struct blk_plug plug; 24481a501907SMel Gorman bool scan_adjusted; 24499b4f98cdSJohannes Weiner 245033377678SVladimir Davydov get_scan_count(lruvec, memcg, sc, nr, lru_pages); 24519b4f98cdSJohannes Weiner 2452e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 2453e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 2454e82e0561SMel Gorman 24551a501907SMel Gorman /* 24561a501907SMel Gorman * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal 24571a501907SMel Gorman * event that can occur when there is little memory pressure e.g. 24581a501907SMel Gorman * multiple streaming readers/writers. Hence, we do not abort scanning 24591a501907SMel Gorman * when the requested number of pages are reclaimed when scanning at 24601a501907SMel Gorman * DEF_PRIORITY on the assumption that the fact we are direct 24611a501907SMel Gorman * reclaiming implies that kswapd is not keeping up and it is best to 24621a501907SMel Gorman * do a batch of work at once. For memcg reclaim one check is made to 24631a501907SMel Gorman * abort proportional reclaim if either the file or anon lru has already 24641a501907SMel Gorman * dropped to zero at the first pass. 24651a501907SMel Gorman */ 24661a501907SMel Gorman scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() && 24671a501907SMel Gorman sc->priority == DEF_PRIORITY); 24681a501907SMel Gorman 24699b4f98cdSJohannes Weiner blk_start_plug(&plug); 24709b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 24719b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 2472e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 2473e82e0561SMel Gorman unsigned long nr_scanned; 2474e82e0561SMel Gorman 24759b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 24769b4f98cdSJohannes Weiner if (nr[lru]) { 24779b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 24789b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 24799b4f98cdSJohannes Weiner 24809b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 24813b991208SJohannes Weiner lruvec, sc); 24829b4f98cdSJohannes Weiner } 24839b4f98cdSJohannes Weiner } 2484e82e0561SMel Gorman 2485bd041733SMichal Hocko cond_resched(); 2486bd041733SMichal Hocko 2487e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 2488e82e0561SMel Gorman continue; 2489e82e0561SMel Gorman 24909b4f98cdSJohannes Weiner /* 2491e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 24921a501907SMel Gorman * requested. Ensure that the anon and file LRUs are scanned 2493e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 2494e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 2495e82e0561SMel Gorman * proportional to the original scan target. 2496e82e0561SMel Gorman */ 2497e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 2498e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 2499e82e0561SMel Gorman 25001a501907SMel Gorman /* 25011a501907SMel Gorman * It's just vindictive to attack the larger once the smaller 25021a501907SMel Gorman * has gone to zero. And given the way we stop scanning the 25031a501907SMel Gorman * smaller below, this makes sure that we only make one nudge 25041a501907SMel Gorman * towards proportionality once we've got nr_to_reclaim. 25051a501907SMel Gorman */ 25061a501907SMel Gorman if (!nr_file || !nr_anon) 25071a501907SMel Gorman break; 25081a501907SMel Gorman 2509e82e0561SMel Gorman if (nr_file > nr_anon) { 2510e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 2511e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 2512e82e0561SMel Gorman lru = LRU_BASE; 2513e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 2514e82e0561SMel Gorman } else { 2515e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 2516e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 2517e82e0561SMel Gorman lru = LRU_FILE; 2518e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 2519e82e0561SMel Gorman } 2520e82e0561SMel Gorman 2521e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 2522e82e0561SMel Gorman nr[lru] = 0; 2523e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 2524e82e0561SMel Gorman 2525e82e0561SMel Gorman /* 2526e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 2527e82e0561SMel Gorman * scan target and the percentage scanning already complete 2528e82e0561SMel Gorman */ 2529e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 2530e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2531e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2532e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2533e82e0561SMel Gorman 2534e82e0561SMel Gorman lru += LRU_ACTIVE; 2535e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2536e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2537e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2538e82e0561SMel Gorman 2539e82e0561SMel Gorman scan_adjusted = true; 25409b4f98cdSJohannes Weiner } 25419b4f98cdSJohannes Weiner blk_finish_plug(&plug); 25429b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 25439b4f98cdSJohannes Weiner 25449b4f98cdSJohannes Weiner /* 25459b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 25469b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 25479b4f98cdSJohannes Weiner */ 25483b991208SJohannes Weiner if (inactive_list_is_low(lruvec, false, sc, true)) 25499b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 25509b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 25519b4f98cdSJohannes Weiner } 25529b4f98cdSJohannes Weiner 255323b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 25549e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 255523b9da55SMel Gorman { 2556d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 255723b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 25589e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 255923b9da55SMel Gorman return true; 256023b9da55SMel Gorman 256123b9da55SMel Gorman return false; 256223b9da55SMel Gorman } 256323b9da55SMel Gorman 25644f98a2feSRik van Riel /* 256523b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 256623b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 256723b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 256823b9da55SMel Gorman * calls try_to_compact_zone() that it will have enough free pages to succeed. 256923b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 25703e7d3449SMel Gorman */ 2571a9dd0a83SMel Gorman static inline bool should_continue_reclaim(struct pglist_data *pgdat, 25723e7d3449SMel Gorman unsigned long nr_reclaimed, 25733e7d3449SMel Gorman unsigned long nr_scanned, 25743e7d3449SMel Gorman struct scan_control *sc) 25753e7d3449SMel Gorman { 25763e7d3449SMel Gorman unsigned long pages_for_compaction; 25773e7d3449SMel Gorman unsigned long inactive_lru_pages; 2578a9dd0a83SMel Gorman int z; 25793e7d3449SMel Gorman 25803e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 25819e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 25823e7d3449SMel Gorman return false; 25833e7d3449SMel Gorman 25842876592fSMel Gorman /* Consider stopping depending on scan and reclaim activity */ 2585dcda9b04SMichal Hocko if (sc->gfp_mask & __GFP_RETRY_MAYFAIL) { 25863e7d3449SMel Gorman /* 2587dcda9b04SMichal Hocko * For __GFP_RETRY_MAYFAIL allocations, stop reclaiming if the 25882876592fSMel Gorman * full LRU list has been scanned and we are still failing 25892876592fSMel Gorman * to reclaim pages. This full LRU scan is potentially 2590dcda9b04SMichal Hocko * expensive but a __GFP_RETRY_MAYFAIL caller really wants to succeed 25913e7d3449SMel Gorman */ 25923e7d3449SMel Gorman if (!nr_reclaimed && !nr_scanned) 25933e7d3449SMel Gorman return false; 25942876592fSMel Gorman } else { 25952876592fSMel Gorman /* 2596dcda9b04SMichal Hocko * For non-__GFP_RETRY_MAYFAIL allocations which can presumably 25972876592fSMel Gorman * fail without consequence, stop if we failed to reclaim 25982876592fSMel Gorman * any pages from the last SWAP_CLUSTER_MAX number of 25992876592fSMel Gorman * pages that were scanned. This will return to the 26002876592fSMel Gorman * caller faster at the risk reclaim/compaction and 26012876592fSMel Gorman * the resulting allocation attempt fails 26022876592fSMel Gorman */ 26032876592fSMel Gorman if (!nr_reclaimed) 26042876592fSMel Gorman return false; 26052876592fSMel Gorman } 26063e7d3449SMel Gorman 26073e7d3449SMel Gorman /* 26083e7d3449SMel Gorman * If we have not reclaimed enough pages for compaction and the 26093e7d3449SMel Gorman * inactive lists are large enough, continue reclaiming 26103e7d3449SMel Gorman */ 26119861a62cSVlastimil Babka pages_for_compaction = compact_gap(sc->order); 2612a9dd0a83SMel Gorman inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); 2613ec8acf20SShaohua Li if (get_nr_swap_pages() > 0) 2614a9dd0a83SMel Gorman inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); 26153e7d3449SMel Gorman if (sc->nr_reclaimed < pages_for_compaction && 26163e7d3449SMel Gorman inactive_lru_pages > pages_for_compaction) 26173e7d3449SMel Gorman return true; 26183e7d3449SMel Gorman 26193e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 2620a9dd0a83SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 2621a9dd0a83SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 26226aa303deSMel Gorman if (!managed_zone(zone)) 2623a9dd0a83SMel Gorman continue; 2624a9dd0a83SMel Gorman 2625a9dd0a83SMel Gorman switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) { 2626cf378319SVlastimil Babka case COMPACT_SUCCESS: 26273e7d3449SMel Gorman case COMPACT_CONTINUE: 26283e7d3449SMel Gorman return false; 26293e7d3449SMel Gorman default: 2630a9dd0a83SMel Gorman /* check next zone */ 2631a9dd0a83SMel Gorman ; 26323e7d3449SMel Gorman } 26333e7d3449SMel Gorman } 2634a9dd0a83SMel Gorman return true; 2635a9dd0a83SMel Gorman } 26363e7d3449SMel Gorman 2637e3c1ac58SAndrey Ryabinin static bool pgdat_memcg_congested(pg_data_t *pgdat, struct mem_cgroup *memcg) 2638e3c1ac58SAndrey Ryabinin { 2639e3c1ac58SAndrey Ryabinin return test_bit(PGDAT_CONGESTED, &pgdat->flags) || 2640e3c1ac58SAndrey Ryabinin (memcg && memcg_congested(pgdat, memcg)); 2641e3c1ac58SAndrey Ryabinin } 2642e3c1ac58SAndrey Ryabinin 2643970a39a3SMel Gorman static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc) 2644f16015fbSJohannes Weiner { 2645cb731d6cSVladimir Davydov struct reclaim_state *reclaim_state = current->reclaim_state; 26469b4f98cdSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 26472344d7e4SJohannes Weiner bool reclaimable = false; 26489b4f98cdSJohannes Weiner 26499b4f98cdSJohannes Weiner do { 26505660048cSJohannes Weiner struct mem_cgroup *root = sc->target_mem_cgroup; 26515660048cSJohannes Weiner struct mem_cgroup_reclaim_cookie reclaim = { 2652ef8f2327SMel Gorman .pgdat = pgdat, 26539e3b2f8cSKonstantin Khlebnikov .priority = sc->priority, 26545660048cSJohannes Weiner }; 2655a9dd0a83SMel Gorman unsigned long node_lru_pages = 0; 2656694fbc0fSAndrew Morton struct mem_cgroup *memcg; 26575660048cSJohannes Weiner 2658d108c772SAndrey Ryabinin memset(&sc->nr, 0, sizeof(sc->nr)); 2659d108c772SAndrey Ryabinin 26609b4f98cdSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 26619b4f98cdSJohannes Weiner nr_scanned = sc->nr_scanned; 26629b4f98cdSJohannes Weiner 2663694fbc0fSAndrew Morton memcg = mem_cgroup_iter(root, NULL, &reclaim); 2664694fbc0fSAndrew Morton do { 26656b4f7799SJohannes Weiner unsigned long lru_pages; 26668e8ae645SJohannes Weiner unsigned long reclaimed; 2667cb731d6cSVladimir Davydov unsigned long scanned; 26689b4f98cdSJohannes Weiner 2669bf8d5d52SRoman Gushchin switch (mem_cgroup_protected(root, memcg)) { 2670bf8d5d52SRoman Gushchin case MEMCG_PROT_MIN: 2671bf8d5d52SRoman Gushchin /* 2672bf8d5d52SRoman Gushchin * Hard protection. 2673bf8d5d52SRoman Gushchin * If there is no reclaimable memory, OOM. 2674bf8d5d52SRoman Gushchin */ 2675bf8d5d52SRoman Gushchin continue; 2676bf8d5d52SRoman Gushchin case MEMCG_PROT_LOW: 2677bf8d5d52SRoman Gushchin /* 2678bf8d5d52SRoman Gushchin * Soft protection. 2679bf8d5d52SRoman Gushchin * Respect the protection only as long as 2680bf8d5d52SRoman Gushchin * there is an unprotected supply 2681bf8d5d52SRoman Gushchin * of reclaimable memory from other cgroups. 2682bf8d5d52SRoman Gushchin */ 2683d6622f63SYisheng Xie if (!sc->memcg_low_reclaim) { 2684d6622f63SYisheng Xie sc->memcg_low_skipped = 1; 2685241994edSJohannes Weiner continue; 2686d6622f63SYisheng Xie } 2687e27be240SJohannes Weiner memcg_memory_event(memcg, MEMCG_LOW); 2688bf8d5d52SRoman Gushchin break; 2689bf8d5d52SRoman Gushchin case MEMCG_PROT_NONE: 2690bf8d5d52SRoman Gushchin break; 2691241994edSJohannes Weiner } 2692241994edSJohannes Weiner 26938e8ae645SJohannes Weiner reclaimed = sc->nr_reclaimed; 2694cb731d6cSVladimir Davydov scanned = sc->nr_scanned; 2695a9dd0a83SMel Gorman shrink_node_memcg(pgdat, memcg, sc, &lru_pages); 2696a9dd0a83SMel Gorman node_lru_pages += lru_pages; 2697f9be23d6SKonstantin Khlebnikov 26981c30844dSMel Gorman if (sc->may_shrinkslab) { 2699a9dd0a83SMel Gorman shrink_slab(sc->gfp_mask, pgdat->node_id, 27009092c71bSJosef Bacik memcg, sc->priority); 27011c30844dSMel Gorman } 2702cb731d6cSVladimir Davydov 27038e8ae645SJohannes Weiner /* Record the group's reclaim efficiency */ 27048e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, memcg, false, 27058e8ae645SJohannes Weiner sc->nr_scanned - scanned, 27068e8ae645SJohannes Weiner sc->nr_reclaimed - reclaimed); 27078e8ae645SJohannes Weiner 27085660048cSJohannes Weiner /* 27092bb0f34fSYang Shi * Kswapd have to scan all memory cgroups to fulfill 27102bb0f34fSYang Shi * the overall scan target for the node. 2711a394cb8eSMichal Hocko * 2712a394cb8eSMichal Hocko * Limit reclaim, on the other hand, only cares about 2713a394cb8eSMichal Hocko * nr_to_reclaim pages to be reclaimed and it will 2714a394cb8eSMichal Hocko * retry with decreasing priority if one round over the 2715a394cb8eSMichal Hocko * whole hierarchy is not sufficient. 27165660048cSJohannes Weiner */ 27172bb0f34fSYang Shi if (!current_is_kswapd() && 2718a394cb8eSMichal Hocko sc->nr_reclaimed >= sc->nr_to_reclaim) { 27195660048cSJohannes Weiner mem_cgroup_iter_break(root, memcg); 27205660048cSJohannes Weiner break; 27215660048cSJohannes Weiner } 2722241994edSJohannes Weiner } while ((memcg = mem_cgroup_iter(root, memcg, &reclaim))); 272370ddf637SAnton Vorontsov 27246b4f7799SJohannes Weiner if (reclaim_state) { 2725cb731d6cSVladimir Davydov sc->nr_reclaimed += reclaim_state->reclaimed_slab; 27266b4f7799SJohannes Weiner reclaim_state->reclaimed_slab = 0; 27276b4f7799SJohannes Weiner } 27286b4f7799SJohannes Weiner 27298e8ae645SJohannes Weiner /* Record the subtree's reclaim efficiency */ 27308e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, 273170ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 273270ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 273370ddf637SAnton Vorontsov 27342344d7e4SJohannes Weiner if (sc->nr_reclaimed - nr_reclaimed) 27352344d7e4SJohannes Weiner reclaimable = true; 27362344d7e4SJohannes Weiner 2737e3c1ac58SAndrey Ryabinin if (current_is_kswapd()) { 2738d108c772SAndrey Ryabinin /* 2739e3c1ac58SAndrey Ryabinin * If reclaim is isolating dirty pages under writeback, 2740e3c1ac58SAndrey Ryabinin * it implies that the long-lived page allocation rate 2741e3c1ac58SAndrey Ryabinin * is exceeding the page laundering rate. Either the 2742e3c1ac58SAndrey Ryabinin * global limits are not being effective at throttling 2743e3c1ac58SAndrey Ryabinin * processes due to the page distribution throughout 2744e3c1ac58SAndrey Ryabinin * zones or there is heavy usage of a slow backing 2745e3c1ac58SAndrey Ryabinin * device. The only option is to throttle from reclaim 2746e3c1ac58SAndrey Ryabinin * context which is not ideal as there is no guarantee 2747d108c772SAndrey Ryabinin * the dirtying process is throttled in the same way 2748d108c772SAndrey Ryabinin * balance_dirty_pages() manages. 2749d108c772SAndrey Ryabinin * 2750e3c1ac58SAndrey Ryabinin * Once a node is flagged PGDAT_WRITEBACK, kswapd will 2751e3c1ac58SAndrey Ryabinin * count the number of pages under pages flagged for 2752e3c1ac58SAndrey Ryabinin * immediate reclaim and stall if any are encountered 2753e3c1ac58SAndrey Ryabinin * in the nr_immediate check below. 2754d108c772SAndrey Ryabinin */ 2755d108c772SAndrey Ryabinin if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken) 2756d108c772SAndrey Ryabinin set_bit(PGDAT_WRITEBACK, &pgdat->flags); 2757d108c772SAndrey Ryabinin 2758d108c772SAndrey Ryabinin /* 2759d108c772SAndrey Ryabinin * Tag a node as congested if all the dirty pages 2760d108c772SAndrey Ryabinin * scanned were backed by a congested BDI and 2761d108c772SAndrey Ryabinin * wait_iff_congested will stall. 2762d108c772SAndrey Ryabinin */ 2763d108c772SAndrey Ryabinin if (sc->nr.dirty && sc->nr.dirty == sc->nr.congested) 2764d108c772SAndrey Ryabinin set_bit(PGDAT_CONGESTED, &pgdat->flags); 2765d108c772SAndrey Ryabinin 2766d108c772SAndrey Ryabinin /* Allow kswapd to start writing pages during reclaim.*/ 2767d108c772SAndrey Ryabinin if (sc->nr.unqueued_dirty == sc->nr.file_taken) 2768d108c772SAndrey Ryabinin set_bit(PGDAT_DIRTY, &pgdat->flags); 2769d108c772SAndrey Ryabinin 2770d108c772SAndrey Ryabinin /* 2771d108c772SAndrey Ryabinin * If kswapd scans pages marked marked for immediate 2772d108c772SAndrey Ryabinin * reclaim and under writeback (nr_immediate), it 2773d108c772SAndrey Ryabinin * implies that pages are cycling through the LRU 2774d108c772SAndrey Ryabinin * faster than they are written so also forcibly stall. 2775d108c772SAndrey Ryabinin */ 2776d108c772SAndrey Ryabinin if (sc->nr.immediate) 2777d108c772SAndrey Ryabinin congestion_wait(BLK_RW_ASYNC, HZ/10); 2778d108c772SAndrey Ryabinin } 2779d108c772SAndrey Ryabinin 2780d108c772SAndrey Ryabinin /* 2781e3c1ac58SAndrey Ryabinin * Legacy memcg will stall in page writeback so avoid forcibly 2782e3c1ac58SAndrey Ryabinin * stalling in wait_iff_congested(). 2783e3c1ac58SAndrey Ryabinin */ 2784e3c1ac58SAndrey Ryabinin if (!global_reclaim(sc) && sane_reclaim(sc) && 2785e3c1ac58SAndrey Ryabinin sc->nr.dirty && sc->nr.dirty == sc->nr.congested) 2786e3c1ac58SAndrey Ryabinin set_memcg_congestion(pgdat, root, true); 2787e3c1ac58SAndrey Ryabinin 2788e3c1ac58SAndrey Ryabinin /* 2789d108c772SAndrey Ryabinin * Stall direct reclaim for IO completions if underlying BDIs 2790d108c772SAndrey Ryabinin * and node is congested. Allow kswapd to continue until it 2791d108c772SAndrey Ryabinin * starts encountering unqueued dirty pages or cycling through 2792d108c772SAndrey Ryabinin * the LRU too quickly. 2793d108c772SAndrey Ryabinin */ 2794d108c772SAndrey Ryabinin if (!sc->hibernation_mode && !current_is_kswapd() && 2795e3c1ac58SAndrey Ryabinin current_may_throttle() && pgdat_memcg_congested(pgdat, root)) 2796e3c1ac58SAndrey Ryabinin wait_iff_congested(BLK_RW_ASYNC, HZ/10); 2797d108c772SAndrey Ryabinin 2798a9dd0a83SMel Gorman } while (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, 27999b4f98cdSJohannes Weiner sc->nr_scanned - nr_scanned, sc)); 28002344d7e4SJohannes Weiner 2801c73322d0SJohannes Weiner /* 2802c73322d0SJohannes Weiner * Kswapd gives up on balancing particular nodes after too 2803c73322d0SJohannes Weiner * many failures to reclaim anything from them and goes to 2804c73322d0SJohannes Weiner * sleep. On reclaim progress, reset the failure counter. A 2805c73322d0SJohannes Weiner * successful direct reclaim run will revive a dormant kswapd. 2806c73322d0SJohannes Weiner */ 2807c73322d0SJohannes Weiner if (reclaimable) 2808c73322d0SJohannes Weiner pgdat->kswapd_failures = 0; 2809c73322d0SJohannes Weiner 28102344d7e4SJohannes Weiner return reclaimable; 2811f16015fbSJohannes Weiner } 2812f16015fbSJohannes Weiner 281353853e2dSVlastimil Babka /* 2814fdd4c614SVlastimil Babka * Returns true if compaction should go ahead for a costly-order request, or 2815fdd4c614SVlastimil Babka * the allocation would already succeed without compaction. Return false if we 2816fdd4c614SVlastimil Babka * should reclaim first. 281753853e2dSVlastimil Babka */ 28184f588331SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 2819fe4b1b24SMel Gorman { 282031483b6aSMel Gorman unsigned long watermark; 2821fdd4c614SVlastimil Babka enum compact_result suitable; 2822fe4b1b24SMel Gorman 2823fdd4c614SVlastimil Babka suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx); 2824fdd4c614SVlastimil Babka if (suitable == COMPACT_SUCCESS) 2825fdd4c614SVlastimil Babka /* Allocation should succeed already. Don't reclaim. */ 2826fdd4c614SVlastimil Babka return true; 2827fdd4c614SVlastimil Babka if (suitable == COMPACT_SKIPPED) 2828fdd4c614SVlastimil Babka /* Compaction cannot yet proceed. Do reclaim. */ 2829fe4b1b24SMel Gorman return false; 2830fe4b1b24SMel Gorman 2831fdd4c614SVlastimil Babka /* 2832fdd4c614SVlastimil Babka * Compaction is already possible, but it takes time to run and there 2833fdd4c614SVlastimil Babka * are potentially other callers using the pages just freed. So proceed 2834fdd4c614SVlastimil Babka * with reclaim to make a buffer of free pages available to give 2835fdd4c614SVlastimil Babka * compaction a reasonable chance of completing and allocating the page. 2836fdd4c614SVlastimil Babka * Note that we won't actually reclaim the whole buffer in one attempt 2837fdd4c614SVlastimil Babka * as the target watermark in should_continue_reclaim() is lower. But if 2838fdd4c614SVlastimil Babka * we are already above the high+gap watermark, don't reclaim at all. 2839fdd4c614SVlastimil Babka */ 2840fdd4c614SVlastimil Babka watermark = high_wmark_pages(zone) + compact_gap(sc->order); 2841fdd4c614SVlastimil Babka 2842fdd4c614SVlastimil Babka return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); 2843fe4b1b24SMel Gorman } 2844fe4b1b24SMel Gorman 28451da177e4SLinus Torvalds /* 28461da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 28471da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 28481da177e4SLinus Torvalds * request. 28491da177e4SLinus Torvalds * 28501da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 28511da177e4SLinus Torvalds * scan then give up on it. 28521da177e4SLinus Torvalds */ 28530a0337e0SMichal Hocko static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 28541da177e4SLinus Torvalds { 2855dd1a239fSMel Gorman struct zoneref *z; 285654a6eb5cSMel Gorman struct zone *zone; 28570608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 28580608f43dSAndrew Morton unsigned long nr_soft_scanned; 2859619d0d76SWeijie Yang gfp_t orig_mask; 286079dafcdcSMel Gorman pg_data_t *last_pgdat = NULL; 28611cfb419bSKAMEZAWA Hiroyuki 2862cc715d99SMel Gorman /* 2863cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 2864cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 2865cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 2866cc715d99SMel Gorman */ 2867619d0d76SWeijie Yang orig_mask = sc->gfp_mask; 2868b2e18757SMel Gorman if (buffer_heads_over_limit) { 2869cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 28704f588331SMel Gorman sc->reclaim_idx = gfp_zone(sc->gfp_mask); 2871b2e18757SMel Gorman } 2872cc715d99SMel Gorman 2873d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 2874b2e18757SMel Gorman sc->reclaim_idx, sc->nodemask) { 2875b2e18757SMel Gorman /* 28761cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 28771cfb419bSKAMEZAWA Hiroyuki * to global LRU. 28781cfb419bSKAMEZAWA Hiroyuki */ 287989b5fae5SJohannes Weiner if (global_reclaim(sc)) { 2880344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, 2881344736f2SVladimir Davydov GFP_KERNEL | __GFP_HARDWALL)) 28821da177e4SLinus Torvalds continue; 288365ec02cbSVladimir Davydov 2884e0887c19SRik van Riel /* 2885e0c23279SMel Gorman * If we already have plenty of memory free for 2886e0c23279SMel Gorman * compaction in this zone, don't free any more. 2887e0c23279SMel Gorman * Even though compaction is invoked for any 2888e0c23279SMel Gorman * non-zero order, only frequent costly order 2889e0c23279SMel Gorman * reclamation is disruptive enough to become a 2890c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 2891c7cfa37bSCopot Alexandru * page allocations. 2892e0887c19SRik van Riel */ 28930b06496aSJohannes Weiner if (IS_ENABLED(CONFIG_COMPACTION) && 28940b06496aSJohannes Weiner sc->order > PAGE_ALLOC_COSTLY_ORDER && 28954f588331SMel Gorman compaction_ready(zone, sc)) { 28960b06496aSJohannes Weiner sc->compaction_ready = true; 2897e0887c19SRik van Riel continue; 2898e0887c19SRik van Riel } 28990b06496aSJohannes Weiner 29000608f43dSAndrew Morton /* 290179dafcdcSMel Gorman * Shrink each node in the zonelist once. If the 290279dafcdcSMel Gorman * zonelist is ordered by zone (not the default) then a 290379dafcdcSMel Gorman * node may be shrunk multiple times but in that case 290479dafcdcSMel Gorman * the user prefers lower zones being preserved. 290579dafcdcSMel Gorman */ 290679dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 290779dafcdcSMel Gorman continue; 290879dafcdcSMel Gorman 290979dafcdcSMel Gorman /* 29100608f43dSAndrew Morton * This steals pages from memory cgroups over softlimit 29110608f43dSAndrew Morton * and returns the number of reclaimed pages and 29120608f43dSAndrew Morton * scanned pages. This works for global memory pressure 29130608f43dSAndrew Morton * and balancing, not for a memcg's limit. 29140608f43dSAndrew Morton */ 29150608f43dSAndrew Morton nr_soft_scanned = 0; 2916ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, 29170608f43dSAndrew Morton sc->order, sc->gfp_mask, 29180608f43dSAndrew Morton &nr_soft_scanned); 29190608f43dSAndrew Morton sc->nr_reclaimed += nr_soft_reclaimed; 29200608f43dSAndrew Morton sc->nr_scanned += nr_soft_scanned; 2921ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 2922ac34a1a3SKAMEZAWA Hiroyuki } 2923d149e3b2SYing Han 292479dafcdcSMel Gorman /* See comment about same check for global reclaim above */ 292579dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 292679dafcdcSMel Gorman continue; 292779dafcdcSMel Gorman last_pgdat = zone->zone_pgdat; 2928970a39a3SMel Gorman shrink_node(zone->zone_pgdat, sc); 29291da177e4SLinus Torvalds } 2930e0c23279SMel Gorman 293165ec02cbSVladimir Davydov /* 2932619d0d76SWeijie Yang * Restore to original mask to avoid the impact on the caller if we 2933619d0d76SWeijie Yang * promoted it to __GFP_HIGHMEM. 2934619d0d76SWeijie Yang */ 2935619d0d76SWeijie Yang sc->gfp_mask = orig_mask; 29361da177e4SLinus Torvalds } 29371da177e4SLinus Torvalds 29382a2e4885SJohannes Weiner static void snapshot_refaults(struct mem_cgroup *root_memcg, pg_data_t *pgdat) 29392a2e4885SJohannes Weiner { 29402a2e4885SJohannes Weiner struct mem_cgroup *memcg; 29412a2e4885SJohannes Weiner 29422a2e4885SJohannes Weiner memcg = mem_cgroup_iter(root_memcg, NULL, NULL); 29432a2e4885SJohannes Weiner do { 29442a2e4885SJohannes Weiner unsigned long refaults; 29452a2e4885SJohannes Weiner struct lruvec *lruvec; 29462a2e4885SJohannes Weiner 29472a2e4885SJohannes Weiner lruvec = mem_cgroup_lruvec(pgdat, memcg); 2948205b20ccSJohannes Weiner refaults = lruvec_page_state_local(lruvec, WORKINGSET_ACTIVATE); 29492a2e4885SJohannes Weiner lruvec->refaults = refaults; 29502a2e4885SJohannes Weiner } while ((memcg = mem_cgroup_iter(root_memcg, memcg, NULL))); 29512a2e4885SJohannes Weiner } 29522a2e4885SJohannes Weiner 29531da177e4SLinus Torvalds /* 29541da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 29551da177e4SLinus Torvalds * 29561da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 29571da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 29581da177e4SLinus Torvalds * 29591da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 29601da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 29615b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 29625b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 29635b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 29645b0830cbSJens Axboe * work, and the allocation attempt will fail. 2965a41f24eaSNishanth Aravamudan * 2966a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 2967a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 29681da177e4SLinus Torvalds */ 2969dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 29703115cd91SVladimir Davydov struct scan_control *sc) 29711da177e4SLinus Torvalds { 2972241994edSJohannes Weiner int initial_priority = sc->priority; 29732a2e4885SJohannes Weiner pg_data_t *last_pgdat; 29742a2e4885SJohannes Weiner struct zoneref *z; 29752a2e4885SJohannes Weiner struct zone *zone; 2976241994edSJohannes Weiner retry: 2977873b4771SKeika Kobayashi delayacct_freepages_start(); 2978873b4771SKeika Kobayashi 297989b5fae5SJohannes Weiner if (global_reclaim(sc)) 29807cc30fcfSMel Gorman __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); 29811da177e4SLinus Torvalds 29829e3b2f8cSKonstantin Khlebnikov do { 298370ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 298470ddf637SAnton Vorontsov sc->priority); 298566e1707bSBalbir Singh sc->nr_scanned = 0; 29860a0337e0SMichal Hocko shrink_zones(zonelist, sc); 2987e0c23279SMel Gorman 2988bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 29890b06496aSJohannes Weiner break; 29900b06496aSJohannes Weiner 29910b06496aSJohannes Weiner if (sc->compaction_ready) 29920b06496aSJohannes Weiner break; 29931da177e4SLinus Torvalds 29941da177e4SLinus Torvalds /* 29950e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 29960e50ce3bSMinchan Kim * writepage even in laptop mode. 29970e50ce3bSMinchan Kim */ 29980e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 29990e50ce3bSMinchan Kim sc->may_writepage = 1; 30000b06496aSJohannes Weiner } while (--sc->priority >= 0); 3001bb21c7ceSKOSAKI Motohiro 30022a2e4885SJohannes Weiner last_pgdat = NULL; 30032a2e4885SJohannes Weiner for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx, 30042a2e4885SJohannes Weiner sc->nodemask) { 30052a2e4885SJohannes Weiner if (zone->zone_pgdat == last_pgdat) 30062a2e4885SJohannes Weiner continue; 30072a2e4885SJohannes Weiner last_pgdat = zone->zone_pgdat; 30082a2e4885SJohannes Weiner snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat); 3009e3c1ac58SAndrey Ryabinin set_memcg_congestion(last_pgdat, sc->target_mem_cgroup, false); 30102a2e4885SJohannes Weiner } 30112a2e4885SJohannes Weiner 3012873b4771SKeika Kobayashi delayacct_freepages_end(); 3013873b4771SKeika Kobayashi 3014bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 3015bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 3016bb21c7ceSKOSAKI Motohiro 30170cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 30180b06496aSJohannes Weiner if (sc->compaction_ready) 30197335084dSMel Gorman return 1; 30207335084dSMel Gorman 3021241994edSJohannes Weiner /* Untapped cgroup reserves? Don't OOM, retry. */ 3022d6622f63SYisheng Xie if (sc->memcg_low_skipped) { 3023241994edSJohannes Weiner sc->priority = initial_priority; 3024d6622f63SYisheng Xie sc->memcg_low_reclaim = 1; 3025d6622f63SYisheng Xie sc->memcg_low_skipped = 0; 3026241994edSJohannes Weiner goto retry; 3027241994edSJohannes Weiner } 3028241994edSJohannes Weiner 3029bb21c7ceSKOSAKI Motohiro return 0; 30301da177e4SLinus Torvalds } 30311da177e4SLinus Torvalds 3032c73322d0SJohannes Weiner static bool allow_direct_reclaim(pg_data_t *pgdat) 30335515061dSMel Gorman { 30345515061dSMel Gorman struct zone *zone; 30355515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 30365515061dSMel Gorman unsigned long free_pages = 0; 30375515061dSMel Gorman int i; 30385515061dSMel Gorman bool wmark_ok; 30395515061dSMel Gorman 3040c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3041c73322d0SJohannes Weiner return true; 3042c73322d0SJohannes Weiner 30435515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 30445515061dSMel Gorman zone = &pgdat->node_zones[i]; 3045d450abd8SJohannes Weiner if (!managed_zone(zone)) 3046d450abd8SJohannes Weiner continue; 3047d450abd8SJohannes Weiner 3048d450abd8SJohannes Weiner if (!zone_reclaimable_pages(zone)) 3049675becceSMel Gorman continue; 3050675becceSMel Gorman 30515515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 30525515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 30535515061dSMel Gorman } 30545515061dSMel Gorman 3055675becceSMel Gorman /* If there are no reserves (unexpected config) then do not throttle */ 3056675becceSMel Gorman if (!pfmemalloc_reserve) 3057675becceSMel Gorman return true; 3058675becceSMel Gorman 30595515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 30605515061dSMel Gorman 30615515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 30625515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 306338087d9bSMel Gorman pgdat->kswapd_classzone_idx = min(pgdat->kswapd_classzone_idx, 30645515061dSMel Gorman (enum zone_type)ZONE_NORMAL); 30655515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 30665515061dSMel Gorman } 30675515061dSMel Gorman 30685515061dSMel Gorman return wmark_ok; 30695515061dSMel Gorman } 30705515061dSMel Gorman 30715515061dSMel Gorman /* 30725515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 30735515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 30745515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 307550694c28SMel Gorman * when the low watermark is reached. 307650694c28SMel Gorman * 307750694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 307850694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 30795515061dSMel Gorman */ 308050694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 30815515061dSMel Gorman nodemask_t *nodemask) 30825515061dSMel Gorman { 3083675becceSMel Gorman struct zoneref *z; 30845515061dSMel Gorman struct zone *zone; 3085675becceSMel Gorman pg_data_t *pgdat = NULL; 30865515061dSMel Gorman 30875515061dSMel Gorman /* 30885515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 30895515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 30905515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 30915515061dSMel Gorman * committing a transaction where throttling it could forcing other 30925515061dSMel Gorman * processes to block on log_wait_commit(). 30935515061dSMel Gorman */ 30945515061dSMel Gorman if (current->flags & PF_KTHREAD) 309550694c28SMel Gorman goto out; 309650694c28SMel Gorman 309750694c28SMel Gorman /* 309850694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 309950694c28SMel Gorman * It should return quickly so it can exit and free its memory 310050694c28SMel Gorman */ 310150694c28SMel Gorman if (fatal_signal_pending(current)) 310250694c28SMel Gorman goto out; 31035515061dSMel Gorman 3104675becceSMel Gorman /* 3105675becceSMel Gorman * Check if the pfmemalloc reserves are ok by finding the first node 3106675becceSMel Gorman * with a usable ZONE_NORMAL or lower zone. The expectation is that 3107675becceSMel Gorman * GFP_KERNEL will be required for allocating network buffers when 3108675becceSMel Gorman * swapping over the network so ZONE_HIGHMEM is unusable. 3109675becceSMel Gorman * 3110675becceSMel Gorman * Throttling is based on the first usable node and throttled processes 3111675becceSMel Gorman * wait on a queue until kswapd makes progress and wakes them. There 3112675becceSMel Gorman * is an affinity then between processes waking up and where reclaim 3113675becceSMel Gorman * progress has been made assuming the process wakes on the same node. 3114675becceSMel Gorman * More importantly, processes running on remote nodes will not compete 3115675becceSMel Gorman * for remote pfmemalloc reserves and processes on different nodes 3116675becceSMel Gorman * should make reasonable progress. 3117675becceSMel Gorman */ 3118675becceSMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 311917636faaSMichael S. Tsirkin gfp_zone(gfp_mask), nodemask) { 3120675becceSMel Gorman if (zone_idx(zone) > ZONE_NORMAL) 3121675becceSMel Gorman continue; 3122675becceSMel Gorman 3123675becceSMel Gorman /* Throttle based on the first usable node */ 31245515061dSMel Gorman pgdat = zone->zone_pgdat; 3125c73322d0SJohannes Weiner if (allow_direct_reclaim(pgdat)) 312650694c28SMel Gorman goto out; 3127675becceSMel Gorman break; 3128675becceSMel Gorman } 3129675becceSMel Gorman 3130675becceSMel Gorman /* If no zone was usable by the allocation flags then do not throttle */ 3131675becceSMel Gorman if (!pgdat) 3132675becceSMel Gorman goto out; 31335515061dSMel Gorman 313468243e76SMel Gorman /* Account for the throttling */ 313568243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 313668243e76SMel Gorman 31375515061dSMel Gorman /* 31385515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 31395515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 31405515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 31415515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 31425515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 31435515061dSMel Gorman * second before continuing. 31445515061dSMel Gorman */ 31455515061dSMel Gorman if (!(gfp_mask & __GFP_FS)) { 31465515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 3147c73322d0SJohannes Weiner allow_direct_reclaim(pgdat), HZ); 314850694c28SMel Gorman 314950694c28SMel Gorman goto check_pending; 31505515061dSMel Gorman } 31515515061dSMel Gorman 31525515061dSMel Gorman /* Throttle until kswapd wakes the process */ 31535515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 3154c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)); 315550694c28SMel Gorman 315650694c28SMel Gorman check_pending: 315750694c28SMel Gorman if (fatal_signal_pending(current)) 315850694c28SMel Gorman return true; 315950694c28SMel Gorman 316050694c28SMel Gorman out: 316150694c28SMel Gorman return false; 31625515061dSMel Gorman } 31635515061dSMel Gorman 3164dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 3165327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 316666e1707bSBalbir Singh { 316733906bc5SMel Gorman unsigned long nr_reclaimed; 316866e1707bSBalbir Singh struct scan_control sc = { 316922fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3170f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 3171b2e18757SMel Gorman .reclaim_idx = gfp_zone(gfp_mask), 3172ee814fe2SJohannes Weiner .order = order, 3173ee814fe2SJohannes Weiner .nodemask = nodemask, 3174ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 3175ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3176a6dc60f8SJohannes Weiner .may_unmap = 1, 31772e2e4259SKOSAKI Motohiro .may_swap = 1, 31781c30844dSMel Gorman .may_shrinkslab = 1, 317966e1707bSBalbir Singh }; 318066e1707bSBalbir Singh 31815515061dSMel Gorman /* 3182bb451fdfSGreg Thelen * scan_control uses s8 fields for order, priority, and reclaim_idx. 3183bb451fdfSGreg Thelen * Confirm they are large enough for max values. 3184bb451fdfSGreg Thelen */ 3185bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_ORDER > S8_MAX); 3186bb451fdfSGreg Thelen BUILD_BUG_ON(DEF_PRIORITY > S8_MAX); 3187bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX); 3188bb451fdfSGreg Thelen 3189bb451fdfSGreg Thelen /* 319050694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 319150694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 319250694c28SMel Gorman * point. 31935515061dSMel Gorman */ 3194f2f43e56SNick Desaulniers if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask)) 31955515061dSMel Gorman return 1; 31965515061dSMel Gorman 31973481c37fSYafang Shao trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask); 319833906bc5SMel Gorman 31993115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 320033906bc5SMel Gorman 320133906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 320233906bc5SMel Gorman 320333906bc5SMel Gorman return nr_reclaimed; 320466e1707bSBalbir Singh } 320566e1707bSBalbir Singh 3206c255a458SAndrew Morton #ifdef CONFIG_MEMCG 320766e1707bSBalbir Singh 3208a9dd0a83SMel Gorman unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, 32094e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 3210ef8f2327SMel Gorman pg_data_t *pgdat, 32110ae5e89cSYing Han unsigned long *nr_scanned) 32124e416953SBalbir Singh { 32134e416953SBalbir Singh struct scan_control sc = { 3214b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3215ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 32164e416953SBalbir Singh .may_writepage = !laptop_mode, 32174e416953SBalbir Singh .may_unmap = 1, 3218b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 32194e416953SBalbir Singh .may_swap = !noswap, 32201c30844dSMel Gorman .may_shrinkslab = 1, 32214e416953SBalbir Singh }; 32226b4f7799SJohannes Weiner unsigned long lru_pages; 32230ae5e89cSYing Han 32244e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 32254e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 3226bdce6d9eSKOSAKI Motohiro 32279e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 32283481c37fSYafang Shao sc.gfp_mask); 3229bdce6d9eSKOSAKI Motohiro 32304e416953SBalbir Singh /* 32314e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 32324e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 3233a9dd0a83SMel Gorman * if we don't reclaim here, the shrink_node from balance_pgdat 32344e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 32354e416953SBalbir Singh * the priority and make it zero. 32364e416953SBalbir Singh */ 3237ef8f2327SMel Gorman shrink_node_memcg(pgdat, memcg, &sc, &lru_pages); 3238bdce6d9eSKOSAKI Motohiro 3239bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 3240bdce6d9eSKOSAKI Motohiro 32410ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 32424e416953SBalbir Singh return sc.nr_reclaimed; 32434e416953SBalbir Singh } 32444e416953SBalbir Singh 324572835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 3246b70a2a21SJohannes Weiner unsigned long nr_pages, 32478c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 3248b70a2a21SJohannes Weiner bool may_swap) 324966e1707bSBalbir Singh { 32504e416953SBalbir Singh struct zonelist *zonelist; 3251bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 3252eb414681SJohannes Weiner unsigned long pflags; 3253889976dbSYing Han int nid; 3254499118e9SVlastimil Babka unsigned int noreclaim_flag; 325566e1707bSBalbir Singh struct scan_control sc = { 3256b70a2a21SJohannes Weiner .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 32577dea19f9SMichal Hocko .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | 3258ee814fe2SJohannes Weiner (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 3259b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 3260ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 3261ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 326266e1707bSBalbir Singh .may_writepage = !laptop_mode, 3263a6dc60f8SJohannes Weiner .may_unmap = 1, 3264b70a2a21SJohannes Weiner .may_swap = may_swap, 32651c30844dSMel Gorman .may_shrinkslab = 1, 3266a09ed5e0SYing Han }; 326766e1707bSBalbir Singh 3268889976dbSYing Han /* 3269889976dbSYing Han * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't 3270889976dbSYing Han * take care of from where we get pages. So the node where we start the 3271889976dbSYing Han * scan does not need to be the current node. 3272889976dbSYing Han */ 327372835c86SJohannes Weiner nid = mem_cgroup_select_victim_node(memcg); 3274889976dbSYing Han 3275c9634cf0SAneesh Kumar K.V zonelist = &NODE_DATA(nid)->node_zonelists[ZONELIST_FALLBACK]; 3276bdce6d9eSKOSAKI Motohiro 32773481c37fSYafang Shao trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask); 3278bdce6d9eSKOSAKI Motohiro 3279eb414681SJohannes Weiner psi_memstall_enter(&pflags); 3280499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 3281eb414681SJohannes Weiner 32823115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3283eb414681SJohannes Weiner 3284499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 3285eb414681SJohannes Weiner psi_memstall_leave(&pflags); 3286bdce6d9eSKOSAKI Motohiro 3287bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 3288bdce6d9eSKOSAKI Motohiro 3289bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 329066e1707bSBalbir Singh } 329166e1707bSBalbir Singh #endif 329266e1707bSBalbir Singh 32931d82de61SMel Gorman static void age_active_anon(struct pglist_data *pgdat, 3294ef8f2327SMel Gorman struct scan_control *sc) 3295f16015fbSJohannes Weiner { 3296b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 3297b95a2f2dSJohannes Weiner 3298b95a2f2dSJohannes Weiner if (!total_swap_pages) 3299b95a2f2dSJohannes Weiner return; 3300b95a2f2dSJohannes Weiner 3301b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 3302b95a2f2dSJohannes Weiner do { 3303ef8f2327SMel Gorman struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg); 3304f16015fbSJohannes Weiner 33053b991208SJohannes Weiner if (inactive_list_is_low(lruvec, false, sc, true)) 33061a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 33079e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 3308b95a2f2dSJohannes Weiner 3309b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 3310b95a2f2dSJohannes Weiner } while (memcg); 3311f16015fbSJohannes Weiner } 3312f16015fbSJohannes Weiner 33131c30844dSMel Gorman static bool pgdat_watermark_boosted(pg_data_t *pgdat, int classzone_idx) 33141c30844dSMel Gorman { 33151c30844dSMel Gorman int i; 33161c30844dSMel Gorman struct zone *zone; 33171c30844dSMel Gorman 33181c30844dSMel Gorman /* 33191c30844dSMel Gorman * Check for watermark boosts top-down as the higher zones 33201c30844dSMel Gorman * are more likely to be boosted. Both watermarks and boosts 33211c30844dSMel Gorman * should not be checked at the time time as reclaim would 33221c30844dSMel Gorman * start prematurely when there is no boosting and a lower 33231c30844dSMel Gorman * zone is balanced. 33241c30844dSMel Gorman */ 33251c30844dSMel Gorman for (i = classzone_idx; i >= 0; i--) { 33261c30844dSMel Gorman zone = pgdat->node_zones + i; 33271c30844dSMel Gorman if (!managed_zone(zone)) 33281c30844dSMel Gorman continue; 33291c30844dSMel Gorman 33301c30844dSMel Gorman if (zone->watermark_boost) 33311c30844dSMel Gorman return true; 33321c30844dSMel Gorman } 33331c30844dSMel Gorman 33341c30844dSMel Gorman return false; 33351c30844dSMel Gorman } 33361c30844dSMel Gorman 3337e716f2ebSMel Gorman /* 3338e716f2ebSMel Gorman * Returns true if there is an eligible zone balanced for the request order 3339e716f2ebSMel Gorman * and classzone_idx 3340e716f2ebSMel Gorman */ 3341e716f2ebSMel Gorman static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx) 334260cefed4SJohannes Weiner { 3343e716f2ebSMel Gorman int i; 3344e716f2ebSMel Gorman unsigned long mark = -1; 3345e716f2ebSMel Gorman struct zone *zone; 334660cefed4SJohannes Weiner 33471c30844dSMel Gorman /* 33481c30844dSMel Gorman * Check watermarks bottom-up as lower zones are more likely to 33491c30844dSMel Gorman * meet watermarks. 33501c30844dSMel Gorman */ 3351e716f2ebSMel Gorman for (i = 0; i <= classzone_idx; i++) { 3352e716f2ebSMel Gorman zone = pgdat->node_zones + i; 33536256c6b4SMel Gorman 3354e716f2ebSMel Gorman if (!managed_zone(zone)) 3355e716f2ebSMel Gorman continue; 3356e716f2ebSMel Gorman 3357e716f2ebSMel Gorman mark = high_wmark_pages(zone); 3358e716f2ebSMel Gorman if (zone_watermark_ok_safe(zone, order, mark, classzone_idx)) 33596256c6b4SMel Gorman return true; 336060cefed4SJohannes Weiner } 336160cefed4SJohannes Weiner 3362e716f2ebSMel Gorman /* 3363e716f2ebSMel Gorman * If a node has no populated zone within classzone_idx, it does not 3364e716f2ebSMel Gorman * need balancing by definition. This can happen if a zone-restricted 3365e716f2ebSMel Gorman * allocation tries to wake a remote kswapd. 3366e716f2ebSMel Gorman */ 3367e716f2ebSMel Gorman if (mark == -1) 3368e716f2ebSMel Gorman return true; 3369e716f2ebSMel Gorman 3370e716f2ebSMel Gorman return false; 3371e716f2ebSMel Gorman } 3372e716f2ebSMel Gorman 3373631b6e08SMel Gorman /* Clear pgdat state for congested, dirty or under writeback. */ 3374631b6e08SMel Gorman static void clear_pgdat_congested(pg_data_t *pgdat) 3375631b6e08SMel Gorman { 3376631b6e08SMel Gorman clear_bit(PGDAT_CONGESTED, &pgdat->flags); 3377631b6e08SMel Gorman clear_bit(PGDAT_DIRTY, &pgdat->flags); 3378631b6e08SMel Gorman clear_bit(PGDAT_WRITEBACK, &pgdat->flags); 3379631b6e08SMel Gorman } 3380631b6e08SMel Gorman 33811741c877SMel Gorman /* 33825515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 33835515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 33845515061dSMel Gorman * 33855515061dSMel Gorman * Returns true if kswapd is ready to sleep 33865515061dSMel Gorman */ 3387d9f21d42SMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, int classzone_idx) 3388f50de2d3SMel Gorman { 33895515061dSMel Gorman /* 33909e5e3661SVlastimil Babka * The throttled processes are normally woken up in balance_pgdat() as 3391c73322d0SJohannes Weiner * soon as allow_direct_reclaim() is true. But there is a potential 33929e5e3661SVlastimil Babka * race between when kswapd checks the watermarks and a process gets 33939e5e3661SVlastimil Babka * throttled. There is also a potential race if processes get 33949e5e3661SVlastimil Babka * throttled, kswapd wakes, a large process exits thereby balancing the 33959e5e3661SVlastimil Babka * zones, which causes kswapd to exit balance_pgdat() before reaching 33969e5e3661SVlastimil Babka * the wake up checks. If kswapd is going to sleep, no process should 33979e5e3661SVlastimil Babka * be sleeping on pfmemalloc_wait, so wake them now if necessary. If 33989e5e3661SVlastimil Babka * the wake up is premature, processes will wake kswapd and get 33999e5e3661SVlastimil Babka * throttled again. The difference from wake ups in balance_pgdat() is 34009e5e3661SVlastimil Babka * that here we are under prepare_to_wait(). 34015515061dSMel Gorman */ 34029e5e3661SVlastimil Babka if (waitqueue_active(&pgdat->pfmemalloc_wait)) 34039e5e3661SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 3404f50de2d3SMel Gorman 3405c73322d0SJohannes Weiner /* Hopeless node, leave it to direct reclaim */ 3406c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3407c73322d0SJohannes Weiner return true; 3408c73322d0SJohannes Weiner 3409e716f2ebSMel Gorman if (pgdat_balanced(pgdat, order, classzone_idx)) { 3410631b6e08SMel Gorman clear_pgdat_congested(pgdat); 3411333b0a45SShantanu Goel return true; 34121d82de61SMel Gorman } 34131d82de61SMel Gorman 3414333b0a45SShantanu Goel return false; 3415f50de2d3SMel Gorman } 3416f50de2d3SMel Gorman 34171da177e4SLinus Torvalds /* 34181d82de61SMel Gorman * kswapd shrinks a node of pages that are at or below the highest usable 34191d82de61SMel Gorman * zone that is currently unbalanced. 3420b8e83b94SMel Gorman * 3421b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 3422283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 3423283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 342475485363SMel Gorman */ 34251d82de61SMel Gorman static bool kswapd_shrink_node(pg_data_t *pgdat, 3426accf6242SVlastimil Babka struct scan_control *sc) 342775485363SMel Gorman { 34281d82de61SMel Gorman struct zone *zone; 34291d82de61SMel Gorman int z; 343075485363SMel Gorman 34311d82de61SMel Gorman /* Reclaim a number of pages proportional to the number of zones */ 34321d82de61SMel Gorman sc->nr_to_reclaim = 0; 3433970a39a3SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 34341d82de61SMel Gorman zone = pgdat->node_zones + z; 34356aa303deSMel Gorman if (!managed_zone(zone)) 34361d82de61SMel Gorman continue; 34377c954f6dSMel Gorman 34381d82de61SMel Gorman sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); 34397c954f6dSMel Gorman } 34407c954f6dSMel Gorman 34411d82de61SMel Gorman /* 34421d82de61SMel Gorman * Historically care was taken to put equal pressure on all zones but 34431d82de61SMel Gorman * now pressure is applied based on node LRU order. 34441d82de61SMel Gorman */ 3445970a39a3SMel Gorman shrink_node(pgdat, sc); 34461d82de61SMel Gorman 34471d82de61SMel Gorman /* 34481d82de61SMel Gorman * Fragmentation may mean that the system cannot be rebalanced for 34491d82de61SMel Gorman * high-order allocations. If twice the allocation size has been 34501d82de61SMel Gorman * reclaimed then recheck watermarks only at order-0 to prevent 34511d82de61SMel Gorman * excessive reclaim. Assume that a process requested a high-order 34521d82de61SMel Gorman * can direct reclaim/compact. 34531d82de61SMel Gorman */ 34549861a62cSVlastimil Babka if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order)) 34551d82de61SMel Gorman sc->order = 0; 34561d82de61SMel Gorman 3457b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 345875485363SMel Gorman } 345975485363SMel Gorman 346075485363SMel Gorman /* 34611d82de61SMel Gorman * For kswapd, balance_pgdat() will reclaim pages across a node from zones 34621d82de61SMel Gorman * that are eligible for use by the caller until at least one zone is 34631d82de61SMel Gorman * balanced. 34641da177e4SLinus Torvalds * 34651d82de61SMel Gorman * Returns the order kswapd finished reclaiming at. 34661da177e4SLinus Torvalds * 34671da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 346841858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 34698bb4e7a2SWei Yang * found to have free_pages <= high_wmark_pages(zone), any page in that zone 34701d82de61SMel Gorman * or lower is eligible for reclaim until at least one usable zone is 34711d82de61SMel Gorman * balanced. 34721da177e4SLinus Torvalds */ 3473accf6242SVlastimil Babka static int balance_pgdat(pg_data_t *pgdat, int order, int classzone_idx) 34741da177e4SLinus Torvalds { 34751da177e4SLinus Torvalds int i; 34760608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 34770608f43dSAndrew Morton unsigned long nr_soft_scanned; 3478eb414681SJohannes Weiner unsigned long pflags; 34791c30844dSMel Gorman unsigned long nr_boost_reclaim; 34801c30844dSMel Gorman unsigned long zone_boosts[MAX_NR_ZONES] = { 0, }; 34811c30844dSMel Gorman bool boosted; 34821d82de61SMel Gorman struct zone *zone; 3483179e9639SAndrew Morton struct scan_control sc = { 3484179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 3485ee814fe2SJohannes Weiner .order = order, 3486a6dc60f8SJohannes Weiner .may_unmap = 1, 3487179e9639SAndrew Morton }; 348893781325SOmar Sandoval 3489*e5ca8071SYafang Shao current->reclaim_state = &sc.reclaim_state; 3490eb414681SJohannes Weiner psi_memstall_enter(&pflags); 349193781325SOmar Sandoval __fs_reclaim_acquire(); 349293781325SOmar Sandoval 3493f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 34941da177e4SLinus Torvalds 34951c30844dSMel Gorman /* 34961c30844dSMel Gorman * Account for the reclaim boost. Note that the zone boost is left in 34971c30844dSMel Gorman * place so that parallel allocations that are near the watermark will 34981c30844dSMel Gorman * stall or direct reclaim until kswapd is finished. 34991c30844dSMel Gorman */ 35001c30844dSMel Gorman nr_boost_reclaim = 0; 35011c30844dSMel Gorman for (i = 0; i <= classzone_idx; i++) { 35021c30844dSMel Gorman zone = pgdat->node_zones + i; 35031c30844dSMel Gorman if (!managed_zone(zone)) 35041c30844dSMel Gorman continue; 35051c30844dSMel Gorman 35061c30844dSMel Gorman nr_boost_reclaim += zone->watermark_boost; 35071c30844dSMel Gorman zone_boosts[i] = zone->watermark_boost; 35081c30844dSMel Gorman } 35091c30844dSMel Gorman boosted = nr_boost_reclaim; 35101c30844dSMel Gorman 35111c30844dSMel Gorman restart: 35121c30844dSMel Gorman sc.priority = DEF_PRIORITY; 35139e3b2f8cSKonstantin Khlebnikov do { 3514c73322d0SJohannes Weiner unsigned long nr_reclaimed = sc.nr_reclaimed; 3515b8e83b94SMel Gorman bool raise_priority = true; 35161c30844dSMel Gorman bool balanced; 351793781325SOmar Sandoval bool ret; 3518b8e83b94SMel Gorman 351984c7a777SMel Gorman sc.reclaim_idx = classzone_idx; 35201da177e4SLinus Torvalds 352186c79f6bSMel Gorman /* 352284c7a777SMel Gorman * If the number of buffer_heads exceeds the maximum allowed 352384c7a777SMel Gorman * then consider reclaiming from all zones. This has a dual 352484c7a777SMel Gorman * purpose -- on 64-bit systems it is expected that 352584c7a777SMel Gorman * buffer_heads are stripped during active rotation. On 32-bit 352684c7a777SMel Gorman * systems, highmem pages can pin lowmem memory and shrinking 352784c7a777SMel Gorman * buffers can relieve lowmem pressure. Reclaim may still not 352884c7a777SMel Gorman * go ahead if all eligible zones for the original allocation 352984c7a777SMel Gorman * request are balanced to avoid excessive reclaim from kswapd. 353086c79f6bSMel Gorman */ 353186c79f6bSMel Gorman if (buffer_heads_over_limit) { 353286c79f6bSMel Gorman for (i = MAX_NR_ZONES - 1; i >= 0; i--) { 353386c79f6bSMel Gorman zone = pgdat->node_zones + i; 35346aa303deSMel Gorman if (!managed_zone(zone)) 353586c79f6bSMel Gorman continue; 353686c79f6bSMel Gorman 3537970a39a3SMel Gorman sc.reclaim_idx = i; 353886c79f6bSMel Gorman break; 353986c79f6bSMel Gorman } 354086c79f6bSMel Gorman } 354186c79f6bSMel Gorman 354286c79f6bSMel Gorman /* 35431c30844dSMel Gorman * If the pgdat is imbalanced then ignore boosting and preserve 35441c30844dSMel Gorman * the watermarks for a later time and restart. Note that the 35451c30844dSMel Gorman * zone watermarks will be still reset at the end of balancing 35461c30844dSMel Gorman * on the grounds that the normal reclaim should be enough to 35471c30844dSMel Gorman * re-evaluate if boosting is required when kswapd next wakes. 354886c79f6bSMel Gorman */ 35491c30844dSMel Gorman balanced = pgdat_balanced(pgdat, sc.order, classzone_idx); 35501c30844dSMel Gorman if (!balanced && nr_boost_reclaim) { 35511c30844dSMel Gorman nr_boost_reclaim = 0; 35521c30844dSMel Gorman goto restart; 35531c30844dSMel Gorman } 35541c30844dSMel Gorman 35551c30844dSMel Gorman /* 35561c30844dSMel Gorman * If boosting is not active then only reclaim if there are no 35571c30844dSMel Gorman * eligible zones. Note that sc.reclaim_idx is not used as 35581c30844dSMel Gorman * buffer_heads_over_limit may have adjusted it. 35591c30844dSMel Gorman */ 35601c30844dSMel Gorman if (!nr_boost_reclaim && balanced) 35611da177e4SLinus Torvalds goto out; 3562e1dbeda6SAndrew Morton 35631c30844dSMel Gorman /* Limit the priority of boosting to avoid reclaim writeback */ 35641c30844dSMel Gorman if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2) 35651c30844dSMel Gorman raise_priority = false; 35661c30844dSMel Gorman 35671c30844dSMel Gorman /* 35681c30844dSMel Gorman * Do not writeback or swap pages for boosted reclaim. The 35691c30844dSMel Gorman * intent is to relieve pressure not issue sub-optimal IO 35701c30844dSMel Gorman * from reclaim context. If no pages are reclaimed, the 35711c30844dSMel Gorman * reclaim will be aborted. 35721c30844dSMel Gorman */ 35731c30844dSMel Gorman sc.may_writepage = !laptop_mode && !nr_boost_reclaim; 35741c30844dSMel Gorman sc.may_swap = !nr_boost_reclaim; 35751c30844dSMel Gorman sc.may_shrinkslab = !nr_boost_reclaim; 35761c30844dSMel Gorman 35771da177e4SLinus Torvalds /* 35781d82de61SMel Gorman * Do some background aging of the anon list, to give 35791d82de61SMel Gorman * pages a chance to be referenced before reclaiming. All 35801d82de61SMel Gorman * pages are rotated regardless of classzone as this is 35811d82de61SMel Gorman * about consistent aging. 35821d82de61SMel Gorman */ 3583ef8f2327SMel Gorman age_active_anon(pgdat, &sc); 35841d82de61SMel Gorman 35851d82de61SMel Gorman /* 3586b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 3587b7ea3c41SMel Gorman * even in laptop mode. 3588b7ea3c41SMel Gorman */ 3589047d72c3SJohannes Weiner if (sc.priority < DEF_PRIORITY - 2) 3590b7ea3c41SMel Gorman sc.may_writepage = 1; 3591b7ea3c41SMel Gorman 35921d82de61SMel Gorman /* Call soft limit reclaim before calling shrink_node. */ 35931da177e4SLinus Torvalds sc.nr_scanned = 0; 35940608f43dSAndrew Morton nr_soft_scanned = 0; 3595ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, 35961d82de61SMel Gorman sc.gfp_mask, &nr_soft_scanned); 35970608f43dSAndrew Morton sc.nr_reclaimed += nr_soft_reclaimed; 35980608f43dSAndrew Morton 359932a4330dSRik van Riel /* 36001d82de61SMel Gorman * There should be no need to raise the scanning priority if 36011d82de61SMel Gorman * enough pages are already being scanned that that high 36021d82de61SMel Gorman * watermark would be met at 100% efficiency. 360332a4330dSRik van Riel */ 3604970a39a3SMel Gorman if (kswapd_shrink_node(pgdat, &sc)) 3605b8e83b94SMel Gorman raise_priority = false; 3606d7868daeSMel Gorman 36075515061dSMel Gorman /* 36085515061dSMel Gorman * If the low watermark is met there is no need for processes 36095515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 36105515061dSMel Gorman * able to safely make forward progress. Wake them 36115515061dSMel Gorman */ 36125515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 3613c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)) 3614cfc51155SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 36155515061dSMel Gorman 3616b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 361793781325SOmar Sandoval __fs_reclaim_release(); 361893781325SOmar Sandoval ret = try_to_freeze(); 361993781325SOmar Sandoval __fs_reclaim_acquire(); 362093781325SOmar Sandoval if (ret || kthread_should_stop()) 3621b8e83b94SMel Gorman break; 3622b8e83b94SMel Gorman 3623b8e83b94SMel Gorman /* 3624b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 3625b8e83b94SMel Gorman * progress in reclaiming pages 3626b8e83b94SMel Gorman */ 3627c73322d0SJohannes Weiner nr_reclaimed = sc.nr_reclaimed - nr_reclaimed; 36281c30844dSMel Gorman nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed); 36291c30844dSMel Gorman 36301c30844dSMel Gorman /* 36311c30844dSMel Gorman * If reclaim made no progress for a boost, stop reclaim as 36321c30844dSMel Gorman * IO cannot be queued and it could be an infinite loop in 36331c30844dSMel Gorman * extreme circumstances. 36341c30844dSMel Gorman */ 36351c30844dSMel Gorman if (nr_boost_reclaim && !nr_reclaimed) 36361c30844dSMel Gorman break; 36371c30844dSMel Gorman 3638c73322d0SJohannes Weiner if (raise_priority || !nr_reclaimed) 3639b8e83b94SMel Gorman sc.priority--; 36401d82de61SMel Gorman } while (sc.priority >= 1); 36411da177e4SLinus Torvalds 3642c73322d0SJohannes Weiner if (!sc.nr_reclaimed) 3643c73322d0SJohannes Weiner pgdat->kswapd_failures++; 3644c73322d0SJohannes Weiner 3645b8e83b94SMel Gorman out: 36461c30844dSMel Gorman /* If reclaim was boosted, account for the reclaim done in this pass */ 36471c30844dSMel Gorman if (boosted) { 36481c30844dSMel Gorman unsigned long flags; 36491c30844dSMel Gorman 36501c30844dSMel Gorman for (i = 0; i <= classzone_idx; i++) { 36511c30844dSMel Gorman if (!zone_boosts[i]) 36521c30844dSMel Gorman continue; 36531c30844dSMel Gorman 36541c30844dSMel Gorman /* Increments are under the zone lock */ 36551c30844dSMel Gorman zone = pgdat->node_zones + i; 36561c30844dSMel Gorman spin_lock_irqsave(&zone->lock, flags); 36571c30844dSMel Gorman zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]); 36581c30844dSMel Gorman spin_unlock_irqrestore(&zone->lock, flags); 36591c30844dSMel Gorman } 36601c30844dSMel Gorman 36611c30844dSMel Gorman /* 36621c30844dSMel Gorman * As there is now likely space, wakeup kcompact to defragment 36631c30844dSMel Gorman * pageblocks. 36641c30844dSMel Gorman */ 36651c30844dSMel Gorman wakeup_kcompactd(pgdat, pageblock_order, classzone_idx); 36661c30844dSMel Gorman } 36671c30844dSMel Gorman 36682a2e4885SJohannes Weiner snapshot_refaults(NULL, pgdat); 366993781325SOmar Sandoval __fs_reclaim_release(); 3670eb414681SJohannes Weiner psi_memstall_leave(&pflags); 3671*e5ca8071SYafang Shao current->reclaim_state = NULL; 3672*e5ca8071SYafang Shao 36730abdee2bSMel Gorman /* 36741d82de61SMel Gorman * Return the order kswapd stopped reclaiming at as 36751d82de61SMel Gorman * prepare_kswapd_sleep() takes it into account. If another caller 36761d82de61SMel Gorman * entered the allocator slow path while kswapd was awake, order will 36771d82de61SMel Gorman * remain at the higher level. 36780abdee2bSMel Gorman */ 36791d82de61SMel Gorman return sc.order; 36801da177e4SLinus Torvalds } 36811da177e4SLinus Torvalds 3682e716f2ebSMel Gorman /* 3683dffcac2cSShakeel Butt * The pgdat->kswapd_classzone_idx is used to pass the highest zone index to be 3684dffcac2cSShakeel Butt * reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is not 3685dffcac2cSShakeel Butt * a valid index then either kswapd runs for first time or kswapd couldn't sleep 3686dffcac2cSShakeel Butt * after previous reclaim attempt (node is still unbalanced). In that case 3687dffcac2cSShakeel Butt * return the zone index of the previous kswapd reclaim cycle. 3688e716f2ebSMel Gorman */ 3689e716f2ebSMel Gorman static enum zone_type kswapd_classzone_idx(pg_data_t *pgdat, 3690dffcac2cSShakeel Butt enum zone_type prev_classzone_idx) 3691e716f2ebSMel Gorman { 3692e716f2ebSMel Gorman if (pgdat->kswapd_classzone_idx == MAX_NR_ZONES) 3693dffcac2cSShakeel Butt return prev_classzone_idx; 3694dffcac2cSShakeel Butt return pgdat->kswapd_classzone_idx; 3695e716f2ebSMel Gorman } 3696e716f2ebSMel Gorman 369738087d9bSMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, 369838087d9bSMel Gorman unsigned int classzone_idx) 3699f0bc0a60SKOSAKI Motohiro { 3700f0bc0a60SKOSAKI Motohiro long remaining = 0; 3701f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 3702f0bc0a60SKOSAKI Motohiro 3703f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 3704f0bc0a60SKOSAKI Motohiro return; 3705f0bc0a60SKOSAKI Motohiro 3706f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3707f0bc0a60SKOSAKI Motohiro 3708333b0a45SShantanu Goel /* 3709333b0a45SShantanu Goel * Try to sleep for a short interval. Note that kcompactd will only be 3710333b0a45SShantanu Goel * woken if it is possible to sleep for a short interval. This is 3711333b0a45SShantanu Goel * deliberate on the assumption that if reclaim cannot keep an 3712333b0a45SShantanu Goel * eligible zone balanced that it's also unlikely that compaction will 3713333b0a45SShantanu Goel * succeed. 3714333b0a45SShantanu Goel */ 3715d9f21d42SMel Gorman if (prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) { 3716fd901c95SVlastimil Babka /* 3717fd901c95SVlastimil Babka * Compaction records what page blocks it recently failed to 3718fd901c95SVlastimil Babka * isolate pages from and skips them in the future scanning. 3719fd901c95SVlastimil Babka * When kswapd is going to sleep, it is reasonable to assume 3720fd901c95SVlastimil Babka * that pages and compaction may succeed so reset the cache. 3721fd901c95SVlastimil Babka */ 3722fd901c95SVlastimil Babka reset_isolation_suitable(pgdat); 3723fd901c95SVlastimil Babka 3724fd901c95SVlastimil Babka /* 3725fd901c95SVlastimil Babka * We have freed the memory, now we should compact it to make 3726fd901c95SVlastimil Babka * allocation of the requested order possible. 3727fd901c95SVlastimil Babka */ 372838087d9bSMel Gorman wakeup_kcompactd(pgdat, alloc_order, classzone_idx); 3729fd901c95SVlastimil Babka 3730f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 373138087d9bSMel Gorman 373238087d9bSMel Gorman /* 373338087d9bSMel Gorman * If woken prematurely then reset kswapd_classzone_idx and 373438087d9bSMel Gorman * order. The values will either be from a wakeup request or 373538087d9bSMel Gorman * the previous request that slept prematurely. 373638087d9bSMel Gorman */ 373738087d9bSMel Gorman if (remaining) { 3738e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx); 373938087d9bSMel Gorman pgdat->kswapd_order = max(pgdat->kswapd_order, reclaim_order); 374038087d9bSMel Gorman } 374138087d9bSMel Gorman 3742f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3743f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3744f0bc0a60SKOSAKI Motohiro } 3745f0bc0a60SKOSAKI Motohiro 3746f0bc0a60SKOSAKI Motohiro /* 3747f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 3748f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 3749f0bc0a60SKOSAKI Motohiro */ 3750d9f21d42SMel Gorman if (!remaining && 3751d9f21d42SMel Gorman prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) { 3752f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 3753f0bc0a60SKOSAKI Motohiro 3754f0bc0a60SKOSAKI Motohiro /* 3755f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 3756f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 3757f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 3758f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 3759f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 3760f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 3761f0bc0a60SKOSAKI Motohiro */ 3762f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 37631c7e7f6cSAaditya Kumar 37641c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 3765f0bc0a60SKOSAKI Motohiro schedule(); 37661c7e7f6cSAaditya Kumar 3767f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 3768f0bc0a60SKOSAKI Motohiro } else { 3769f0bc0a60SKOSAKI Motohiro if (remaining) 3770f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 3771f0bc0a60SKOSAKI Motohiro else 3772f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 3773f0bc0a60SKOSAKI Motohiro } 3774f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3775f0bc0a60SKOSAKI Motohiro } 3776f0bc0a60SKOSAKI Motohiro 37771da177e4SLinus Torvalds /* 37781da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 37791da177e4SLinus Torvalds * from the init process. 37801da177e4SLinus Torvalds * 37811da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 37821da177e4SLinus Torvalds * free memory available even if there is no other activity 37831da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 37841da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 37851da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 37861da177e4SLinus Torvalds * 37871da177e4SLinus Torvalds * If there are applications that are active memory-allocators 37881da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 37891da177e4SLinus Torvalds */ 37901da177e4SLinus Torvalds static int kswapd(void *p) 37911da177e4SLinus Torvalds { 3792e716f2ebSMel Gorman unsigned int alloc_order, reclaim_order; 3793e716f2ebSMel Gorman unsigned int classzone_idx = MAX_NR_ZONES - 1; 37941da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 37951da177e4SLinus Torvalds struct task_struct *tsk = current; 3796a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 37971da177e4SLinus Torvalds 3798174596a0SRusty Russell if (!cpumask_empty(cpumask)) 3799c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 38001da177e4SLinus Torvalds 38011da177e4SLinus Torvalds /* 38021da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 38031da177e4SLinus Torvalds * and that if we need more memory we should get access to it 38041da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 38051da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 38061da177e4SLinus Torvalds * 38071da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 38081da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 38091da177e4SLinus Torvalds * page out something else, and this flag essentially protects 38101da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 38111da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 38121da177e4SLinus Torvalds */ 3813930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 381483144186SRafael J. Wysocki set_freezable(); 38151da177e4SLinus Torvalds 3816e716f2ebSMel Gorman pgdat->kswapd_order = 0; 3817e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = MAX_NR_ZONES; 38181da177e4SLinus Torvalds for ( ; ; ) { 38196f6313d4SJeff Liu bool ret; 38203e1d1d28SChristoph Lameter 3821e716f2ebSMel Gorman alloc_order = reclaim_order = pgdat->kswapd_order; 3822e716f2ebSMel Gorman classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx); 3823e716f2ebSMel Gorman 382438087d9bSMel Gorman kswapd_try_sleep: 382538087d9bSMel Gorman kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, 382638087d9bSMel Gorman classzone_idx); 3827215ddd66SMel Gorman 382838087d9bSMel Gorman /* Read the new order and classzone_idx */ 382938087d9bSMel Gorman alloc_order = reclaim_order = pgdat->kswapd_order; 3830dffcac2cSShakeel Butt classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx); 383138087d9bSMel Gorman pgdat->kswapd_order = 0; 3832e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = MAX_NR_ZONES; 38331da177e4SLinus Torvalds 38348fe23e05SDavid Rientjes ret = try_to_freeze(); 38358fe23e05SDavid Rientjes if (kthread_should_stop()) 38368fe23e05SDavid Rientjes break; 38378fe23e05SDavid Rientjes 38388fe23e05SDavid Rientjes /* 38398fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 38408fe23e05SDavid Rientjes * after returning from the refrigerator 3841b1296cc4SRafael J. Wysocki */ 384238087d9bSMel Gorman if (ret) 384338087d9bSMel Gorman continue; 38441d82de61SMel Gorman 384538087d9bSMel Gorman /* 384638087d9bSMel Gorman * Reclaim begins at the requested order but if a high-order 384738087d9bSMel Gorman * reclaim fails then kswapd falls back to reclaiming for 384838087d9bSMel Gorman * order-0. If that happens, kswapd will consider sleeping 384938087d9bSMel Gorman * for the order it finished reclaiming at (reclaim_order) 385038087d9bSMel Gorman * but kcompactd is woken to compact for the original 385138087d9bSMel Gorman * request (alloc_order). 385238087d9bSMel Gorman */ 3853e5146b12SMel Gorman trace_mm_vmscan_kswapd_wake(pgdat->node_id, classzone_idx, 3854e5146b12SMel Gorman alloc_order); 385538087d9bSMel Gorman reclaim_order = balance_pgdat(pgdat, alloc_order, classzone_idx); 385638087d9bSMel Gorman if (reclaim_order < alloc_order) 385738087d9bSMel Gorman goto kswapd_try_sleep; 385833906bc5SMel Gorman } 3859b0a8cc58STakamori Yamaguchi 386071abdc15SJohannes Weiner tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); 386171abdc15SJohannes Weiner 38621da177e4SLinus Torvalds return 0; 38631da177e4SLinus Torvalds } 38641da177e4SLinus Torvalds 38651da177e4SLinus Torvalds /* 38665ecd9d40SDavid Rientjes * A zone is low on free memory or too fragmented for high-order memory. If 38675ecd9d40SDavid Rientjes * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's 38685ecd9d40SDavid Rientjes * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim 38695ecd9d40SDavid Rientjes * has failed or is not needed, still wake up kcompactd if only compaction is 38705ecd9d40SDavid Rientjes * needed. 38711da177e4SLinus Torvalds */ 38725ecd9d40SDavid Rientjes void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, 38735ecd9d40SDavid Rientjes enum zone_type classzone_idx) 38741da177e4SLinus Torvalds { 38751da177e4SLinus Torvalds pg_data_t *pgdat; 38761da177e4SLinus Torvalds 38776aa303deSMel Gorman if (!managed_zone(zone)) 38781da177e4SLinus Torvalds return; 38791da177e4SLinus Torvalds 38805ecd9d40SDavid Rientjes if (!cpuset_zone_allowed(zone, gfp_flags)) 38811da177e4SLinus Torvalds return; 388288f5acf8SMel Gorman pgdat = zone->zone_pgdat; 3883dffcac2cSShakeel Butt 3884dffcac2cSShakeel Butt if (pgdat->kswapd_classzone_idx == MAX_NR_ZONES) 3885dffcac2cSShakeel Butt pgdat->kswapd_classzone_idx = classzone_idx; 3886dffcac2cSShakeel Butt else 3887dffcac2cSShakeel Butt pgdat->kswapd_classzone_idx = max(pgdat->kswapd_classzone_idx, 3888e716f2ebSMel Gorman classzone_idx); 388938087d9bSMel Gorman pgdat->kswapd_order = max(pgdat->kswapd_order, order); 38908d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 38911da177e4SLinus Torvalds return; 3892e1a55637SMel Gorman 38935ecd9d40SDavid Rientjes /* Hopeless node, leave it to direct reclaim if possible */ 38945ecd9d40SDavid Rientjes if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES || 38951c30844dSMel Gorman (pgdat_balanced(pgdat, order, classzone_idx) && 38961c30844dSMel Gorman !pgdat_watermark_boosted(pgdat, classzone_idx))) { 38975ecd9d40SDavid Rientjes /* 38985ecd9d40SDavid Rientjes * There may be plenty of free memory available, but it's too 38995ecd9d40SDavid Rientjes * fragmented for high-order allocations. Wake up kcompactd 39005ecd9d40SDavid Rientjes * and rely on compaction_suitable() to determine if it's 39015ecd9d40SDavid Rientjes * needed. If it fails, it will defer subsequent attempts to 39025ecd9d40SDavid Rientjes * ratelimit its work. 39035ecd9d40SDavid Rientjes */ 39045ecd9d40SDavid Rientjes if (!(gfp_flags & __GFP_DIRECT_RECLAIM)) 39055ecd9d40SDavid Rientjes wakeup_kcompactd(pgdat, order, classzone_idx); 3906c73322d0SJohannes Weiner return; 39075ecd9d40SDavid Rientjes } 3908c73322d0SJohannes Weiner 39095ecd9d40SDavid Rientjes trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, classzone_idx, order, 39105ecd9d40SDavid Rientjes gfp_flags); 39118d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 39121da177e4SLinus Torvalds } 39131da177e4SLinus Torvalds 3914c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 39151da177e4SLinus Torvalds /* 39167b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 3917d6277db4SRafael J. Wysocki * freed pages. 3918d6277db4SRafael J. Wysocki * 3919d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 3920d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 3921d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 39221da177e4SLinus Torvalds */ 39237b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 39241da177e4SLinus Torvalds { 3925d6277db4SRafael J. Wysocki struct scan_control sc = { 39267b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 3927ee814fe2SJohannes Weiner .gfp_mask = GFP_HIGHUSER_MOVABLE, 3928b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 39299e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 3930ee814fe2SJohannes Weiner .may_writepage = 1, 3931ee814fe2SJohannes Weiner .may_unmap = 1, 3932ee814fe2SJohannes Weiner .may_swap = 1, 3933ee814fe2SJohannes Weiner .hibernation_mode = 1, 39341da177e4SLinus Torvalds }; 39357b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 39367b51755cSKOSAKI Motohiro struct task_struct *p = current; 39377b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 3938499118e9SVlastimil Babka unsigned int noreclaim_flag; 39391da177e4SLinus Torvalds 3940d92a8cfcSPeter Zijlstra fs_reclaim_acquire(sc.gfp_mask); 394193781325SOmar Sandoval noreclaim_flag = memalloc_noreclaim_save(); 3942*e5ca8071SYafang Shao p->reclaim_state = &sc.reclaim_state; 3943d6277db4SRafael J. Wysocki 39443115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3945d6277db4SRafael J. Wysocki 39467b51755cSKOSAKI Motohiro p->reclaim_state = NULL; 3947499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 394893781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 3949d6277db4SRafael J. Wysocki 39507b51755cSKOSAKI Motohiro return nr_reclaimed; 39511da177e4SLinus Torvalds } 3952c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 39531da177e4SLinus Torvalds 39541da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 39551da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 39561da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 39571da177e4SLinus Torvalds restore their cpu bindings. */ 3958517bbed9SSebastian Andrzej Siewior static int kswapd_cpu_online(unsigned int cpu) 39591da177e4SLinus Torvalds { 396058c0a4a7SYasunori Goto int nid; 39611da177e4SLinus Torvalds 396248fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) { 3963c5f59f08SMike Travis pg_data_t *pgdat = NODE_DATA(nid); 3964a70f7302SRusty Russell const struct cpumask *mask; 3965a70f7302SRusty Russell 3966a70f7302SRusty Russell mask = cpumask_of_node(pgdat->node_id); 3967c5f59f08SMike Travis 39683e597945SRusty Russell if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 39691da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 3970c5f59f08SMike Travis set_cpus_allowed_ptr(pgdat->kswapd, mask); 39711da177e4SLinus Torvalds } 3972517bbed9SSebastian Andrzej Siewior return 0; 39731da177e4SLinus Torvalds } 39741da177e4SLinus Torvalds 39753218ae14SYasunori Goto /* 39763218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 39773218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 39783218ae14SYasunori Goto */ 39793218ae14SYasunori Goto int kswapd_run(int nid) 39803218ae14SYasunori Goto { 39813218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 39823218ae14SYasunori Goto int ret = 0; 39833218ae14SYasunori Goto 39843218ae14SYasunori Goto if (pgdat->kswapd) 39853218ae14SYasunori Goto return 0; 39863218ae14SYasunori Goto 39873218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 39883218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 39893218ae14SYasunori Goto /* failure at boot is fatal */ 3990c6202adfSThomas Gleixner BUG_ON(system_state < SYSTEM_RUNNING); 3991d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 3992d5dc0ad9SGavin Shan ret = PTR_ERR(pgdat->kswapd); 3993d72515b8SXishi Qiu pgdat->kswapd = NULL; 39943218ae14SYasunori Goto } 39953218ae14SYasunori Goto return ret; 39963218ae14SYasunori Goto } 39973218ae14SYasunori Goto 39988fe23e05SDavid Rientjes /* 3999d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 4000bfc8c901SVladimir Davydov * hold mem_hotplug_begin/end(). 40018fe23e05SDavid Rientjes */ 40028fe23e05SDavid Rientjes void kswapd_stop(int nid) 40038fe23e05SDavid Rientjes { 40048fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 40058fe23e05SDavid Rientjes 4006d8adde17SJiang Liu if (kswapd) { 40078fe23e05SDavid Rientjes kthread_stop(kswapd); 4008d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 4009d8adde17SJiang Liu } 40108fe23e05SDavid Rientjes } 40118fe23e05SDavid Rientjes 40121da177e4SLinus Torvalds static int __init kswapd_init(void) 40131da177e4SLinus Torvalds { 4014517bbed9SSebastian Andrzej Siewior int nid, ret; 401569e05944SAndrew Morton 40161da177e4SLinus Torvalds swap_setup(); 401748fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 40183218ae14SYasunori Goto kswapd_run(nid); 4019517bbed9SSebastian Andrzej Siewior ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, 4020517bbed9SSebastian Andrzej Siewior "mm/vmscan:online", kswapd_cpu_online, 4021517bbed9SSebastian Andrzej Siewior NULL); 4022517bbed9SSebastian Andrzej Siewior WARN_ON(ret < 0); 40231da177e4SLinus Torvalds return 0; 40241da177e4SLinus Torvalds } 40251da177e4SLinus Torvalds 40261da177e4SLinus Torvalds module_init(kswapd_init) 40279eeff239SChristoph Lameter 40289eeff239SChristoph Lameter #ifdef CONFIG_NUMA 40299eeff239SChristoph Lameter /* 4030a5f5f91dSMel Gorman * Node reclaim mode 40319eeff239SChristoph Lameter * 4032a5f5f91dSMel Gorman * If non-zero call node_reclaim when the number of free pages falls below 40339eeff239SChristoph Lameter * the watermarks. 40349eeff239SChristoph Lameter */ 4035a5f5f91dSMel Gorman int node_reclaim_mode __read_mostly; 40369eeff239SChristoph Lameter 40371b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 40387d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ 40391b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 404095bbc0c7SZhihui Zhang #define RECLAIM_UNMAP (1<<2) /* Unmap pages during reclaim */ 40411b2ffb78SChristoph Lameter 40429eeff239SChristoph Lameter /* 4043a5f5f91dSMel Gorman * Priority for NODE_RECLAIM. This determines the fraction of pages 4044a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 4045a92f7126SChristoph Lameter * a zone. 4046a92f7126SChristoph Lameter */ 4047a5f5f91dSMel Gorman #define NODE_RECLAIM_PRIORITY 4 4048a92f7126SChristoph Lameter 40499eeff239SChristoph Lameter /* 4050a5f5f91dSMel Gorman * Percentage of pages in a zone that must be unmapped for node_reclaim to 40519614634fSChristoph Lameter * occur. 40529614634fSChristoph Lameter */ 40539614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 40549614634fSChristoph Lameter 40559614634fSChristoph Lameter /* 40560ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 40570ff38490SChristoph Lameter * slab reclaim needs to occur. 40580ff38490SChristoph Lameter */ 40590ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 40600ff38490SChristoph Lameter 406111fb9989SMel Gorman static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) 406290afa5deSMel Gorman { 406311fb9989SMel Gorman unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); 406411fb9989SMel Gorman unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + 406511fb9989SMel Gorman node_page_state(pgdat, NR_ACTIVE_FILE); 406690afa5deSMel Gorman 406790afa5deSMel Gorman /* 406890afa5deSMel Gorman * It's possible for there to be more file mapped pages than 406990afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 407090afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 407190afa5deSMel Gorman */ 407290afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 407390afa5deSMel Gorman } 407490afa5deSMel Gorman 407590afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 4076a5f5f91dSMel Gorman static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) 407790afa5deSMel Gorman { 4078d031a157SAlexandru Moise unsigned long nr_pagecache_reclaimable; 4079d031a157SAlexandru Moise unsigned long delta = 0; 408090afa5deSMel Gorman 408190afa5deSMel Gorman /* 408295bbc0c7SZhihui Zhang * If RECLAIM_UNMAP is set, then all file pages are considered 408390afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 408411fb9989SMel Gorman * pages like swapcache and node_unmapped_file_pages() provides 408590afa5deSMel Gorman * a better estimate 408690afa5deSMel Gorman */ 4087a5f5f91dSMel Gorman if (node_reclaim_mode & RECLAIM_UNMAP) 4088a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); 408990afa5deSMel Gorman else 4090a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); 409190afa5deSMel Gorman 409290afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 4093a5f5f91dSMel Gorman if (!(node_reclaim_mode & RECLAIM_WRITE)) 4094a5f5f91dSMel Gorman delta += node_page_state(pgdat, NR_FILE_DIRTY); 409590afa5deSMel Gorman 409690afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 409790afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 409890afa5deSMel Gorman delta = nr_pagecache_reclaimable; 409990afa5deSMel Gorman 410090afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 410190afa5deSMel Gorman } 410290afa5deSMel Gorman 41030ff38490SChristoph Lameter /* 4104a5f5f91dSMel Gorman * Try to free up some pages from this node through reclaim. 41059eeff239SChristoph Lameter */ 4106a5f5f91dSMel Gorman static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 41079eeff239SChristoph Lameter { 41087fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 410969e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 41109eeff239SChristoph Lameter struct task_struct *p = current; 4111499118e9SVlastimil Babka unsigned int noreclaim_flag; 4112179e9639SAndrew Morton struct scan_control sc = { 411362b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 4114f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 4115bd2f6199SJohannes Weiner .order = order, 4116a5f5f91dSMel Gorman .priority = NODE_RECLAIM_PRIORITY, 4117a5f5f91dSMel Gorman .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), 4118a5f5f91dSMel Gorman .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), 4119ee814fe2SJohannes Weiner .may_swap = 1, 4120f2f43e56SNick Desaulniers .reclaim_idx = gfp_zone(gfp_mask), 4121179e9639SAndrew Morton }; 41229eeff239SChristoph Lameter 4123132bb8cfSYafang Shao trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order, 4124132bb8cfSYafang Shao sc.gfp_mask); 4125132bb8cfSYafang Shao 41269eeff239SChristoph Lameter cond_resched(); 412793781325SOmar Sandoval fs_reclaim_acquire(sc.gfp_mask); 4128d4f7796eSChristoph Lameter /* 412995bbc0c7SZhihui Zhang * We need to be able to allocate from the reserves for RECLAIM_UNMAP 4130d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 413195bbc0c7SZhihui Zhang * and RECLAIM_UNMAP. 4132d4f7796eSChristoph Lameter */ 4133499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 4134499118e9SVlastimil Babka p->flags |= PF_SWAPWRITE; 4135*e5ca8071SYafang Shao p->reclaim_state = &sc.reclaim_state; 4136c84db23cSChristoph Lameter 4137a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) { 4138a92f7126SChristoph Lameter /* 4139894befecSAndrey Ryabinin * Free memory by calling shrink node with increasing 41400ff38490SChristoph Lameter * priorities until we have enough memory freed. 4141a92f7126SChristoph Lameter */ 4142a92f7126SChristoph Lameter do { 4143970a39a3SMel Gorman shrink_node(pgdat, &sc); 41449e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 41450ff38490SChristoph Lameter } 4146a92f7126SChristoph Lameter 41479eeff239SChristoph Lameter p->reclaim_state = NULL; 4148499118e9SVlastimil Babka current->flags &= ~PF_SWAPWRITE; 4149499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 415093781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 4151132bb8cfSYafang Shao 4152132bb8cfSYafang Shao trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed); 4153132bb8cfSYafang Shao 4154a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 41559eeff239SChristoph Lameter } 4156179e9639SAndrew Morton 4157a5f5f91dSMel Gorman int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 4158179e9639SAndrew Morton { 4159d773ed6bSDavid Rientjes int ret; 4160179e9639SAndrew Morton 4161179e9639SAndrew Morton /* 4162a5f5f91dSMel Gorman * Node reclaim reclaims unmapped file backed pages and 41630ff38490SChristoph Lameter * slab pages if we are over the defined limits. 416434aa1330SChristoph Lameter * 41659614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 41669614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 4167a5f5f91dSMel Gorman * thrown out if the node is overallocated. So we do not reclaim 4168a5f5f91dSMel Gorman * if less than a specified percentage of the node is used by 41699614634fSChristoph Lameter * unmapped file backed pages. 4170179e9639SAndrew Morton */ 4171a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && 4172385386cfSJohannes Weiner node_page_state(pgdat, NR_SLAB_RECLAIMABLE) <= pgdat->min_slab_pages) 4173a5f5f91dSMel Gorman return NODE_RECLAIM_FULL; 4174179e9639SAndrew Morton 4175179e9639SAndrew Morton /* 4176d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 4177179e9639SAndrew Morton */ 4178d0164adcSMel Gorman if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) 4179a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4180179e9639SAndrew Morton 4181179e9639SAndrew Morton /* 4182a5f5f91dSMel Gorman * Only run node reclaim on the local node or on nodes that do not 4183179e9639SAndrew Morton * have associated processors. This will favor the local processor 4184179e9639SAndrew Morton * over remote processors and spread off node memory allocations 4185179e9639SAndrew Morton * as wide as possible. 4186179e9639SAndrew Morton */ 4187a5f5f91dSMel Gorman if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) 4188a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4189d773ed6bSDavid Rientjes 4190a5f5f91dSMel Gorman if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) 4191a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4192fa5e084eSMel Gorman 4193a5f5f91dSMel Gorman ret = __node_reclaim(pgdat, gfp_mask, order); 4194a5f5f91dSMel Gorman clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); 4195d773ed6bSDavid Rientjes 419624cf7251SMel Gorman if (!ret) 419724cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 419824cf7251SMel Gorman 4199d773ed6bSDavid Rientjes return ret; 4200179e9639SAndrew Morton } 42019eeff239SChristoph Lameter #endif 4202894bc310SLee Schermerhorn 4203894bc310SLee Schermerhorn /* 4204894bc310SLee Schermerhorn * page_evictable - test whether a page is evictable 4205894bc310SLee Schermerhorn * @page: the page to test 4206894bc310SLee Schermerhorn * 4207894bc310SLee Schermerhorn * Test whether page is evictable--i.e., should be placed on active/inactive 420839b5f29aSHugh Dickins * lists vs unevictable list. 4209894bc310SLee Schermerhorn * 4210894bc310SLee Schermerhorn * Reasons page might not be evictable: 4211ba9ddf49SLee Schermerhorn * (1) page's mapping marked unevictable 4212b291f000SNick Piggin * (2) page is part of an mlocked VMA 4213ba9ddf49SLee Schermerhorn * 4214894bc310SLee Schermerhorn */ 421539b5f29aSHugh Dickins int page_evictable(struct page *page) 4216894bc310SLee Schermerhorn { 4217e92bb4ddSHuang Ying int ret; 4218e92bb4ddSHuang Ying 4219e92bb4ddSHuang Ying /* Prevent address_space of inode and swap cache from being freed */ 4220e92bb4ddSHuang Ying rcu_read_lock(); 4221e92bb4ddSHuang Ying ret = !mapping_unevictable(page_mapping(page)) && !PageMlocked(page); 4222e92bb4ddSHuang Ying rcu_read_unlock(); 4223e92bb4ddSHuang Ying return ret; 4224894bc310SLee Schermerhorn } 422589e004eaSLee Schermerhorn 422689e004eaSLee Schermerhorn /** 422764e3d12fSKuo-Hsin Yang * check_move_unevictable_pages - check pages for evictability and move to 422864e3d12fSKuo-Hsin Yang * appropriate zone lru list 422964e3d12fSKuo-Hsin Yang * @pvec: pagevec with lru pages to check 423089e004eaSLee Schermerhorn * 423164e3d12fSKuo-Hsin Yang * Checks pages for evictability, if an evictable page is in the unevictable 423264e3d12fSKuo-Hsin Yang * lru list, moves it to the appropriate evictable lru list. This function 423364e3d12fSKuo-Hsin Yang * should be only used for lru pages. 423489e004eaSLee Schermerhorn */ 423564e3d12fSKuo-Hsin Yang void check_move_unevictable_pages(struct pagevec *pvec) 423689e004eaSLee Schermerhorn { 4237925b7673SJohannes Weiner struct lruvec *lruvec; 4238785b99feSMel Gorman struct pglist_data *pgdat = NULL; 423924513264SHugh Dickins int pgscanned = 0; 424024513264SHugh Dickins int pgrescued = 0; 424189e004eaSLee Schermerhorn int i; 424289e004eaSLee Schermerhorn 424364e3d12fSKuo-Hsin Yang for (i = 0; i < pvec->nr; i++) { 424464e3d12fSKuo-Hsin Yang struct page *page = pvec->pages[i]; 4245785b99feSMel Gorman struct pglist_data *pagepgdat = page_pgdat(page); 424689e004eaSLee Schermerhorn 424724513264SHugh Dickins pgscanned++; 4248785b99feSMel Gorman if (pagepgdat != pgdat) { 4249785b99feSMel Gorman if (pgdat) 4250785b99feSMel Gorman spin_unlock_irq(&pgdat->lru_lock); 4251785b99feSMel Gorman pgdat = pagepgdat; 4252785b99feSMel Gorman spin_lock_irq(&pgdat->lru_lock); 425389e004eaSLee Schermerhorn } 4254785b99feSMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 425589e004eaSLee Schermerhorn 425624513264SHugh Dickins if (!PageLRU(page) || !PageUnevictable(page)) 425724513264SHugh Dickins continue; 425889e004eaSLee Schermerhorn 425939b5f29aSHugh Dickins if (page_evictable(page)) { 426024513264SHugh Dickins enum lru_list lru = page_lru_base_type(page); 426124513264SHugh Dickins 4262309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 426324513264SHugh Dickins ClearPageUnevictable(page); 4264fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE); 4265fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 426624513264SHugh Dickins pgrescued++; 426789e004eaSLee Schermerhorn } 426889e004eaSLee Schermerhorn } 426924513264SHugh Dickins 4270785b99feSMel Gorman if (pgdat) { 427124513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 427224513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 4273785b99feSMel Gorman spin_unlock_irq(&pgdat->lru_lock); 427424513264SHugh Dickins } 427585046579SHugh Dickins } 427664e3d12fSKuo-Hsin Yang EXPORT_SYMBOL_GPL(check_move_unevictable_pages); 4277