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 91d6622f63SYisheng Xie /* 92d6622f63SYisheng Xie * Cgroups are not reclaimed below their configured memory.low, 93d6622f63SYisheng Xie * unless we threaten to OOM. If any cgroups are skipped due to 94d6622f63SYisheng Xie * memory.low and nothing was reclaimed, go back for memory.low. 95d6622f63SYisheng Xie */ 96d6622f63SYisheng Xie unsigned int memcg_low_reclaim:1; 97d6622f63SYisheng Xie unsigned int memcg_low_skipped:1; 98241994edSJohannes Weiner 99ee814fe2SJohannes Weiner unsigned int hibernation_mode:1; 100ee814fe2SJohannes Weiner 101ee814fe2SJohannes Weiner /* One of the zones is ready for compaction */ 102ee814fe2SJohannes Weiner unsigned int compaction_ready:1; 103ee814fe2SJohannes Weiner 104bb451fdfSGreg Thelen /* Allocation order */ 105bb451fdfSGreg Thelen s8 order; 106bb451fdfSGreg Thelen 107bb451fdfSGreg Thelen /* Scan (total_size >> priority) pages at once */ 108bb451fdfSGreg Thelen s8 priority; 109bb451fdfSGreg Thelen 110bb451fdfSGreg Thelen /* The highest zone to isolate pages for reclaim from */ 111bb451fdfSGreg Thelen s8 reclaim_idx; 112bb451fdfSGreg Thelen 113bb451fdfSGreg Thelen /* This context's GFP mask */ 114bb451fdfSGreg Thelen gfp_t gfp_mask; 115bb451fdfSGreg Thelen 116ee814fe2SJohannes Weiner /* Incremented by the number of inactive pages that were scanned */ 117ee814fe2SJohannes Weiner unsigned long nr_scanned; 118ee814fe2SJohannes Weiner 119ee814fe2SJohannes Weiner /* Number of pages freed so far during a call to shrink_zones() */ 120ee814fe2SJohannes Weiner unsigned long nr_reclaimed; 121d108c772SAndrey Ryabinin 122d108c772SAndrey Ryabinin struct { 123d108c772SAndrey Ryabinin unsigned int dirty; 124d108c772SAndrey Ryabinin unsigned int unqueued_dirty; 125d108c772SAndrey Ryabinin unsigned int congested; 126d108c772SAndrey Ryabinin unsigned int writeback; 127d108c772SAndrey Ryabinin unsigned int immediate; 128d108c772SAndrey Ryabinin unsigned int file_taken; 129d108c772SAndrey Ryabinin unsigned int taken; 130d108c772SAndrey Ryabinin } nr; 131e5ca8071SYafang Shao 132e5ca8071SYafang Shao /* for recording the reclaimed slab by now */ 133e5ca8071SYafang Shao struct reclaim_state reclaim_state; 1341da177e4SLinus Torvalds }; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH 1371da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) \ 1381da177e4SLinus Torvalds do { \ 1391da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1401da177e4SLinus Torvalds struct page *prev; \ 1411da177e4SLinus Torvalds \ 1421da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1431da177e4SLinus Torvalds prefetch(&prev->_field); \ 1441da177e4SLinus Torvalds } \ 1451da177e4SLinus Torvalds } while (0) 1461da177e4SLinus Torvalds #else 1471da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) 1481da177e4SLinus Torvalds #endif 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1511da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1521da177e4SLinus Torvalds do { \ 1531da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1541da177e4SLinus Torvalds struct page *prev; \ 1551da177e4SLinus Torvalds \ 1561da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1571da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1581da177e4SLinus Torvalds } \ 1591da177e4SLinus Torvalds } while (0) 1601da177e4SLinus Torvalds #else 1611da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1621da177e4SLinus Torvalds #endif 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds /* 1651da177e4SLinus Torvalds * From 0 .. 100. Higher means more swappy. 1661da177e4SLinus Torvalds */ 1671da177e4SLinus Torvalds int vm_swappiness = 60; 168d0480be4SWang Sheng-Hui /* 169d0480be4SWang Sheng-Hui * The total number of pages which are beyond the high watermark within all 170d0480be4SWang Sheng-Hui * zones. 171d0480be4SWang Sheng-Hui */ 172d0480be4SWang Sheng-Hui unsigned long vm_total_pages; 1731da177e4SLinus Torvalds 1740a432dcbSYang Shi static void set_task_reclaim_state(struct task_struct *task, 1750a432dcbSYang Shi struct reclaim_state *rs) 1760a432dcbSYang Shi { 1770a432dcbSYang Shi /* Check for an overwrite */ 1780a432dcbSYang Shi WARN_ON_ONCE(rs && task->reclaim_state); 1790a432dcbSYang Shi 1800a432dcbSYang Shi /* Check for the nulling of an already-nulled member */ 1810a432dcbSYang Shi WARN_ON_ONCE(!rs && !task->reclaim_state); 1820a432dcbSYang Shi 1830a432dcbSYang Shi task->reclaim_state = rs; 1840a432dcbSYang Shi } 1850a432dcbSYang Shi 1861da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1871da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1881da177e4SLinus Torvalds 1890a432dcbSYang Shi #ifdef CONFIG_MEMCG 1907e010df5SKirill Tkhai /* 1917e010df5SKirill Tkhai * We allow subsystems to populate their shrinker-related 1927e010df5SKirill Tkhai * LRU lists before register_shrinker_prepared() is called 1937e010df5SKirill Tkhai * for the shrinker, since we don't want to impose 1947e010df5SKirill Tkhai * restrictions on their internal registration order. 1957e010df5SKirill Tkhai * In this case shrink_slab_memcg() may find corresponding 1967e010df5SKirill Tkhai * bit is set in the shrinkers map. 1977e010df5SKirill Tkhai * 1987e010df5SKirill Tkhai * This value is used by the function to detect registering 1997e010df5SKirill Tkhai * shrinkers and to skip do_shrink_slab() calls for them. 2007e010df5SKirill Tkhai */ 2017e010df5SKirill Tkhai #define SHRINKER_REGISTERING ((struct shrinker *)~0UL) 2027e010df5SKirill Tkhai 203b4c2b231SKirill Tkhai static DEFINE_IDR(shrinker_idr); 204b4c2b231SKirill Tkhai static int shrinker_nr_max; 205b4c2b231SKirill Tkhai 206b4c2b231SKirill Tkhai static int prealloc_memcg_shrinker(struct shrinker *shrinker) 207b4c2b231SKirill Tkhai { 208b4c2b231SKirill Tkhai int id, ret = -ENOMEM; 209b4c2b231SKirill Tkhai 210b4c2b231SKirill Tkhai down_write(&shrinker_rwsem); 211b4c2b231SKirill Tkhai /* This may call shrinker, so it must use down_read_trylock() */ 2127e010df5SKirill Tkhai id = idr_alloc(&shrinker_idr, SHRINKER_REGISTERING, 0, 0, GFP_KERNEL); 213b4c2b231SKirill Tkhai if (id < 0) 214b4c2b231SKirill Tkhai goto unlock; 215b4c2b231SKirill Tkhai 2160a4465d3SKirill Tkhai if (id >= shrinker_nr_max) { 2170a4465d3SKirill Tkhai if (memcg_expand_shrinker_maps(id)) { 2180a4465d3SKirill Tkhai idr_remove(&shrinker_idr, id); 2190a4465d3SKirill Tkhai goto unlock; 2200a4465d3SKirill Tkhai } 2210a4465d3SKirill Tkhai 222b4c2b231SKirill Tkhai shrinker_nr_max = id + 1; 2230a4465d3SKirill Tkhai } 224b4c2b231SKirill Tkhai shrinker->id = id; 225b4c2b231SKirill Tkhai ret = 0; 226b4c2b231SKirill Tkhai unlock: 227b4c2b231SKirill Tkhai up_write(&shrinker_rwsem); 228b4c2b231SKirill Tkhai return ret; 229b4c2b231SKirill Tkhai } 230b4c2b231SKirill Tkhai 231b4c2b231SKirill Tkhai static void unregister_memcg_shrinker(struct shrinker *shrinker) 232b4c2b231SKirill Tkhai { 233b4c2b231SKirill Tkhai int id = shrinker->id; 234b4c2b231SKirill Tkhai 235b4c2b231SKirill Tkhai BUG_ON(id < 0); 236b4c2b231SKirill Tkhai 237b4c2b231SKirill Tkhai down_write(&shrinker_rwsem); 238b4c2b231SKirill Tkhai idr_remove(&shrinker_idr, id); 239b4c2b231SKirill Tkhai up_write(&shrinker_rwsem); 240b4c2b231SKirill Tkhai } 241b4c2b231SKirill Tkhai 24289b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 24389b5fae5SJohannes Weiner { 244f16015fbSJohannes Weiner return !sc->target_mem_cgroup; 24589b5fae5SJohannes Weiner } 24697c9341fSTejun Heo 24797c9341fSTejun Heo /** 24897c9341fSTejun Heo * sane_reclaim - is the usual dirty throttling mechanism operational? 24997c9341fSTejun Heo * @sc: scan_control in question 25097c9341fSTejun Heo * 25197c9341fSTejun Heo * The normal page dirty throttling mechanism in balance_dirty_pages() is 25297c9341fSTejun Heo * completely broken with the legacy memcg and direct stalling in 25397c9341fSTejun Heo * shrink_page_list() is used for throttling instead, which lacks all the 25497c9341fSTejun Heo * niceties such as fairness, adaptive pausing, bandwidth proportional 25597c9341fSTejun Heo * allocation and configurability. 25697c9341fSTejun Heo * 25797c9341fSTejun Heo * This function tests whether the vmscan currently in progress can assume 25897c9341fSTejun Heo * that the normal dirty throttling mechanism is operational. 25997c9341fSTejun Heo */ 26097c9341fSTejun Heo static bool sane_reclaim(struct scan_control *sc) 26197c9341fSTejun Heo { 26297c9341fSTejun Heo struct mem_cgroup *memcg = sc->target_mem_cgroup; 26397c9341fSTejun Heo 26497c9341fSTejun Heo if (!memcg) 26597c9341fSTejun Heo return true; 26697c9341fSTejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 26769234aceSLinus Torvalds if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) 26897c9341fSTejun Heo return true; 26997c9341fSTejun Heo #endif 27097c9341fSTejun Heo return false; 27197c9341fSTejun Heo } 272e3c1ac58SAndrey Ryabinin 273e3c1ac58SAndrey Ryabinin static void set_memcg_congestion(pg_data_t *pgdat, 274e3c1ac58SAndrey Ryabinin struct mem_cgroup *memcg, 275e3c1ac58SAndrey Ryabinin bool congested) 276e3c1ac58SAndrey Ryabinin { 277e3c1ac58SAndrey Ryabinin struct mem_cgroup_per_node *mn; 278e3c1ac58SAndrey Ryabinin 279e3c1ac58SAndrey Ryabinin if (!memcg) 280e3c1ac58SAndrey Ryabinin return; 281e3c1ac58SAndrey Ryabinin 282e3c1ac58SAndrey Ryabinin mn = mem_cgroup_nodeinfo(memcg, pgdat->node_id); 283e3c1ac58SAndrey Ryabinin WRITE_ONCE(mn->congested, congested); 284e3c1ac58SAndrey Ryabinin } 285e3c1ac58SAndrey Ryabinin 286e3c1ac58SAndrey Ryabinin static bool memcg_congested(pg_data_t *pgdat, 287e3c1ac58SAndrey Ryabinin struct mem_cgroup *memcg) 288e3c1ac58SAndrey Ryabinin { 289e3c1ac58SAndrey Ryabinin struct mem_cgroup_per_node *mn; 290e3c1ac58SAndrey Ryabinin 291e3c1ac58SAndrey Ryabinin mn = mem_cgroup_nodeinfo(memcg, pgdat->node_id); 292e3c1ac58SAndrey Ryabinin return READ_ONCE(mn->congested); 293e3c1ac58SAndrey Ryabinin 294e3c1ac58SAndrey Ryabinin } 29591a45470SKAMEZAWA Hiroyuki #else 2960a432dcbSYang Shi static int prealloc_memcg_shrinker(struct shrinker *shrinker) 2970a432dcbSYang Shi { 2980a432dcbSYang Shi return 0; 2990a432dcbSYang Shi } 3000a432dcbSYang Shi 3010a432dcbSYang Shi static void unregister_memcg_shrinker(struct shrinker *shrinker) 3020a432dcbSYang Shi { 3030a432dcbSYang Shi } 3040a432dcbSYang Shi 30589b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 30689b5fae5SJohannes Weiner { 30789b5fae5SJohannes Weiner return true; 30889b5fae5SJohannes Weiner } 30997c9341fSTejun Heo 31097c9341fSTejun Heo static bool sane_reclaim(struct scan_control *sc) 31197c9341fSTejun Heo { 31297c9341fSTejun Heo return true; 31397c9341fSTejun Heo } 314e3c1ac58SAndrey Ryabinin 315e3c1ac58SAndrey Ryabinin static inline void set_memcg_congestion(struct pglist_data *pgdat, 316e3c1ac58SAndrey Ryabinin struct mem_cgroup *memcg, bool congested) 317e3c1ac58SAndrey Ryabinin { 318e3c1ac58SAndrey Ryabinin } 319e3c1ac58SAndrey Ryabinin 320e3c1ac58SAndrey Ryabinin static inline bool memcg_congested(struct pglist_data *pgdat, 321e3c1ac58SAndrey Ryabinin struct mem_cgroup *memcg) 322e3c1ac58SAndrey Ryabinin { 323e3c1ac58SAndrey Ryabinin return false; 324e3c1ac58SAndrey Ryabinin 325e3c1ac58SAndrey Ryabinin } 32691a45470SKAMEZAWA Hiroyuki #endif 32791a45470SKAMEZAWA Hiroyuki 3285a1c84b4SMel Gorman /* 3295a1c84b4SMel Gorman * This misses isolated pages which are not accounted for to save counters. 3305a1c84b4SMel Gorman * As the data only determines if reclaim or compaction continues, it is 3315a1c84b4SMel Gorman * not expected that isolated pages will be a dominating factor. 3325a1c84b4SMel Gorman */ 3335a1c84b4SMel Gorman unsigned long zone_reclaimable_pages(struct zone *zone) 3345a1c84b4SMel Gorman { 3355a1c84b4SMel Gorman unsigned long nr; 3365a1c84b4SMel Gorman 3375a1c84b4SMel Gorman nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) + 3385a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE); 3395a1c84b4SMel Gorman if (get_nr_swap_pages() > 0) 3405a1c84b4SMel Gorman nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) + 3415a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON); 3425a1c84b4SMel Gorman 3435a1c84b4SMel Gorman return nr; 3445a1c84b4SMel Gorman } 3455a1c84b4SMel Gorman 346fd538803SMichal Hocko /** 347fd538803SMichal Hocko * lruvec_lru_size - Returns the number of pages on the given LRU list. 348fd538803SMichal Hocko * @lruvec: lru vector 349fd538803SMichal Hocko * @lru: lru to use 350fd538803SMichal Hocko * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list) 351fd538803SMichal Hocko */ 352fd538803SMichal Hocko unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx) 353c9f299d9SKOSAKI Motohiro { 354fd538803SMichal Hocko unsigned long lru_size; 355fd538803SMichal Hocko int zid; 356a3d8e054SKOSAKI Motohiro 357fd538803SMichal Hocko if (!mem_cgroup_disabled()) 358205b20ccSJohannes Weiner lru_size = lruvec_page_state_local(lruvec, NR_LRU_BASE + lru); 359fd538803SMichal Hocko else 360fd538803SMichal Hocko lru_size = node_page_state(lruvec_pgdat(lruvec), NR_LRU_BASE + lru); 361fd538803SMichal Hocko 362fd538803SMichal Hocko for (zid = zone_idx + 1; zid < MAX_NR_ZONES; zid++) { 363fd538803SMichal Hocko struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; 364fd538803SMichal Hocko unsigned long size; 365fd538803SMichal Hocko 366fd538803SMichal Hocko if (!managed_zone(zone)) 367fd538803SMichal Hocko continue; 368fd538803SMichal Hocko 369fd538803SMichal Hocko if (!mem_cgroup_disabled()) 370fd538803SMichal Hocko size = mem_cgroup_get_zone_lru_size(lruvec, lru, zid); 371fd538803SMichal Hocko else 372fd538803SMichal Hocko size = zone_page_state(&lruvec_pgdat(lruvec)->node_zones[zid], 373fd538803SMichal Hocko NR_ZONE_LRU_BASE + lru); 374fd538803SMichal Hocko lru_size -= min(size, lru_size); 375c9f299d9SKOSAKI Motohiro } 376c9f299d9SKOSAKI Motohiro 377fd538803SMichal Hocko return lru_size; 378b4536f0cSMichal Hocko 379b4536f0cSMichal Hocko } 380b4536f0cSMichal Hocko 3811da177e4SLinus Torvalds /* 3821d3d4437SGlauber Costa * Add a shrinker callback to be called from the vm. 3831da177e4SLinus Torvalds */ 3848e04944fSTetsuo Handa int prealloc_shrinker(struct shrinker *shrinker) 3851da177e4SLinus Torvalds { 386b9726c26SAlexey Dobriyan unsigned int size = sizeof(*shrinker->nr_deferred); 3871d3d4437SGlauber Costa 3881d3d4437SGlauber Costa if (shrinker->flags & SHRINKER_NUMA_AWARE) 3891d3d4437SGlauber Costa size *= nr_node_ids; 3901d3d4437SGlauber Costa 3911d3d4437SGlauber Costa shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); 3921d3d4437SGlauber Costa if (!shrinker->nr_deferred) 3931d3d4437SGlauber Costa return -ENOMEM; 394b4c2b231SKirill Tkhai 395b4c2b231SKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) { 396b4c2b231SKirill Tkhai if (prealloc_memcg_shrinker(shrinker)) 397b4c2b231SKirill Tkhai goto free_deferred; 398b4c2b231SKirill Tkhai } 399b4c2b231SKirill Tkhai 4008e04944fSTetsuo Handa return 0; 401b4c2b231SKirill Tkhai 402b4c2b231SKirill Tkhai free_deferred: 403b4c2b231SKirill Tkhai kfree(shrinker->nr_deferred); 404b4c2b231SKirill Tkhai shrinker->nr_deferred = NULL; 405b4c2b231SKirill Tkhai return -ENOMEM; 4068e04944fSTetsuo Handa } 4071d3d4437SGlauber Costa 4088e04944fSTetsuo Handa void free_prealloced_shrinker(struct shrinker *shrinker) 4098e04944fSTetsuo Handa { 410b4c2b231SKirill Tkhai if (!shrinker->nr_deferred) 411b4c2b231SKirill Tkhai return; 412b4c2b231SKirill Tkhai 413b4c2b231SKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) 414b4c2b231SKirill Tkhai unregister_memcg_shrinker(shrinker); 415b4c2b231SKirill Tkhai 4168e04944fSTetsuo Handa kfree(shrinker->nr_deferred); 4178e04944fSTetsuo Handa shrinker->nr_deferred = NULL; 4188e04944fSTetsuo Handa } 4198e04944fSTetsuo Handa 4208e04944fSTetsuo Handa void register_shrinker_prepared(struct shrinker *shrinker) 4218e04944fSTetsuo Handa { 4221da177e4SLinus Torvalds down_write(&shrinker_rwsem); 4231da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 4247e010df5SKirill Tkhai #ifdef CONFIG_MEMCG_KMEM 4258df4a44cSKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) 4267e010df5SKirill Tkhai idr_replace(&shrinker_idr, shrinker, shrinker->id); 4277e010df5SKirill Tkhai #endif 4281da177e4SLinus Torvalds up_write(&shrinker_rwsem); 4298e04944fSTetsuo Handa } 4308e04944fSTetsuo Handa 4318e04944fSTetsuo Handa int register_shrinker(struct shrinker *shrinker) 4328e04944fSTetsuo Handa { 4338e04944fSTetsuo Handa int err = prealloc_shrinker(shrinker); 4348e04944fSTetsuo Handa 4358e04944fSTetsuo Handa if (err) 4368e04944fSTetsuo Handa return err; 4378e04944fSTetsuo Handa register_shrinker_prepared(shrinker); 4381d3d4437SGlauber Costa return 0; 4391da177e4SLinus Torvalds } 4408e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds /* 4431da177e4SLinus Torvalds * Remove one 4441da177e4SLinus Torvalds */ 4458e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 4461da177e4SLinus Torvalds { 447bb422a73STetsuo Handa if (!shrinker->nr_deferred) 448bb422a73STetsuo Handa return; 449b4c2b231SKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) 450b4c2b231SKirill Tkhai unregister_memcg_shrinker(shrinker); 4511da177e4SLinus Torvalds down_write(&shrinker_rwsem); 4521da177e4SLinus Torvalds list_del(&shrinker->list); 4531da177e4SLinus Torvalds up_write(&shrinker_rwsem); 454ae393321SAndrew Vagin kfree(shrinker->nr_deferred); 455bb422a73STetsuo Handa shrinker->nr_deferred = NULL; 4561da177e4SLinus Torvalds } 4578e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds #define SHRINK_BATCH 128 4601d3d4437SGlauber Costa 461cb731d6cSVladimir Davydov static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, 4629092c71bSJosef Bacik struct shrinker *shrinker, int priority) 4631da177e4SLinus Torvalds { 46424f7c6b9SDave Chinner unsigned long freed = 0; 4651da177e4SLinus Torvalds unsigned long long delta; 466635697c6SKonstantin Khlebnikov long total_scan; 467d5bc5fd3SVladimir Davydov long freeable; 468acf92b48SDave Chinner long nr; 469acf92b48SDave Chinner long new_nr; 4701d3d4437SGlauber Costa int nid = shrinkctl->nid; 471e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 472e9299f50SDave Chinner : SHRINK_BATCH; 4735f33a080SShaohua Li long scanned = 0, next_deferred; 4741da177e4SLinus Torvalds 475ac7fb3adSKirill Tkhai if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) 476ac7fb3adSKirill Tkhai nid = 0; 477ac7fb3adSKirill Tkhai 478d5bc5fd3SVladimir Davydov freeable = shrinker->count_objects(shrinker, shrinkctl); 4799b996468SKirill Tkhai if (freeable == 0 || freeable == SHRINK_EMPTY) 4809b996468SKirill Tkhai return freeable; 481635697c6SKonstantin Khlebnikov 482acf92b48SDave Chinner /* 483acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 484acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 485acf92b48SDave Chinner * don't also do this scanning work. 486acf92b48SDave Chinner */ 4871d3d4437SGlauber Costa nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0); 488acf92b48SDave Chinner 489acf92b48SDave Chinner total_scan = nr; 4904b85afbdSJohannes Weiner if (shrinker->seeks) { 4919092c71bSJosef Bacik delta = freeable >> priority; 4929092c71bSJosef Bacik delta *= 4; 4939092c71bSJosef Bacik do_div(delta, shrinker->seeks); 4944b85afbdSJohannes Weiner } else { 4954b85afbdSJohannes Weiner /* 4964b85afbdSJohannes Weiner * These objects don't require any IO to create. Trim 4974b85afbdSJohannes Weiner * them aggressively under memory pressure to keep 4984b85afbdSJohannes Weiner * them from causing refetches in the IO caches. 4994b85afbdSJohannes Weiner */ 5004b85afbdSJohannes Weiner delta = freeable / 2; 5014b85afbdSJohannes Weiner } 502172b06c3SRoman Gushchin 503acf92b48SDave Chinner total_scan += delta; 504acf92b48SDave Chinner if (total_scan < 0) { 505d75f773cSSakari Ailus pr_err("shrink_slab: %pS negative objects to delete nr=%ld\n", 506a0b02131SDave Chinner shrinker->scan_objects, total_scan); 507d5bc5fd3SVladimir Davydov total_scan = freeable; 5085f33a080SShaohua Li next_deferred = nr; 5095f33a080SShaohua Li } else 5105f33a080SShaohua Li next_deferred = total_scan; 511ea164d73SAndrea Arcangeli 512ea164d73SAndrea Arcangeli /* 5133567b59aSDave Chinner * We need to avoid excessive windup on filesystem shrinkers 5143567b59aSDave Chinner * due to large numbers of GFP_NOFS allocations causing the 5153567b59aSDave Chinner * shrinkers to return -1 all the time. This results in a large 5163567b59aSDave Chinner * nr being built up so when a shrink that can do some work 5173567b59aSDave Chinner * comes along it empties the entire cache due to nr >>> 518d5bc5fd3SVladimir Davydov * freeable. This is bad for sustaining a working set in 5193567b59aSDave Chinner * memory. 5203567b59aSDave Chinner * 5213567b59aSDave Chinner * Hence only allow the shrinker to scan the entire cache when 5223567b59aSDave Chinner * a large delta change is calculated directly. 5233567b59aSDave Chinner */ 524d5bc5fd3SVladimir Davydov if (delta < freeable / 4) 525d5bc5fd3SVladimir Davydov total_scan = min(total_scan, freeable / 2); 5263567b59aSDave Chinner 5273567b59aSDave Chinner /* 528ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 529ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 530ea164d73SAndrea Arcangeli * freeable entries. 531ea164d73SAndrea Arcangeli */ 532d5bc5fd3SVladimir Davydov if (total_scan > freeable * 2) 533d5bc5fd3SVladimir Davydov total_scan = freeable * 2; 5341da177e4SLinus Torvalds 53524f7c6b9SDave Chinner trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, 5369092c71bSJosef Bacik freeable, delta, total_scan, priority); 53709576073SDave Chinner 5380b1fb40aSVladimir Davydov /* 5390b1fb40aSVladimir Davydov * Normally, we should not scan less than batch_size objects in one 5400b1fb40aSVladimir Davydov * pass to avoid too frequent shrinker calls, but if the slab has less 5410b1fb40aSVladimir Davydov * than batch_size objects in total and we are really tight on memory, 5420b1fb40aSVladimir Davydov * we will try to reclaim all available objects, otherwise we can end 5430b1fb40aSVladimir Davydov * up failing allocations although there are plenty of reclaimable 5440b1fb40aSVladimir Davydov * objects spread over several slabs with usage less than the 5450b1fb40aSVladimir Davydov * batch_size. 5460b1fb40aSVladimir Davydov * 5470b1fb40aSVladimir Davydov * We detect the "tight on memory" situations by looking at the total 5480b1fb40aSVladimir Davydov * number of objects we want to scan (total_scan). If it is greater 549d5bc5fd3SVladimir Davydov * than the total number of objects on slab (freeable), we must be 5500b1fb40aSVladimir Davydov * scanning at high prio and therefore should try to reclaim as much as 5510b1fb40aSVladimir Davydov * possible. 5520b1fb40aSVladimir Davydov */ 5530b1fb40aSVladimir Davydov while (total_scan >= batch_size || 554d5bc5fd3SVladimir Davydov total_scan >= freeable) { 55524f7c6b9SDave Chinner unsigned long ret; 5560b1fb40aSVladimir Davydov unsigned long nr_to_scan = min(batch_size, total_scan); 5571da177e4SLinus Torvalds 5580b1fb40aSVladimir Davydov shrinkctl->nr_to_scan = nr_to_scan; 559d460acb5SChris Wilson shrinkctl->nr_scanned = nr_to_scan; 56024f7c6b9SDave Chinner ret = shrinker->scan_objects(shrinker, shrinkctl); 56124f7c6b9SDave Chinner if (ret == SHRINK_STOP) 5621da177e4SLinus Torvalds break; 56324f7c6b9SDave Chinner freed += ret; 56424f7c6b9SDave Chinner 565d460acb5SChris Wilson count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned); 566d460acb5SChris Wilson total_scan -= shrinkctl->nr_scanned; 567d460acb5SChris Wilson scanned += shrinkctl->nr_scanned; 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds cond_resched(); 5701da177e4SLinus Torvalds } 5711da177e4SLinus Torvalds 5725f33a080SShaohua Li if (next_deferred >= scanned) 5735f33a080SShaohua Li next_deferred -= scanned; 5745f33a080SShaohua Li else 5755f33a080SShaohua Li next_deferred = 0; 576acf92b48SDave Chinner /* 577acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 578acf92b48SDave Chinner * manner that handles concurrent updates. If we exhausted the 579acf92b48SDave Chinner * scan, there is no need to do an update. 580acf92b48SDave Chinner */ 5815f33a080SShaohua Li if (next_deferred > 0) 5825f33a080SShaohua Li new_nr = atomic_long_add_return(next_deferred, 5831d3d4437SGlauber Costa &shrinker->nr_deferred[nid]); 58483aeeadaSKonstantin Khlebnikov else 5851d3d4437SGlauber Costa new_nr = atomic_long_read(&shrinker->nr_deferred[nid]); 586acf92b48SDave Chinner 587df9024a8SDave Hansen trace_mm_shrink_slab_end(shrinker, nid, freed, nr, new_nr, total_scan); 5881d3d4437SGlauber Costa return freed; 5891d3d4437SGlauber Costa } 5901d3d4437SGlauber Costa 5910a432dcbSYang Shi #ifdef CONFIG_MEMCG 592b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 593b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 594b0dedc49SKirill Tkhai { 595b0dedc49SKirill Tkhai struct memcg_shrinker_map *map; 596b8e57efaSKirill Tkhai unsigned long ret, freed = 0; 597b8e57efaSKirill Tkhai int i; 598b0dedc49SKirill Tkhai 5990a432dcbSYang Shi if (!mem_cgroup_online(memcg)) 600b0dedc49SKirill Tkhai return 0; 601b0dedc49SKirill Tkhai 602b0dedc49SKirill Tkhai if (!down_read_trylock(&shrinker_rwsem)) 603b0dedc49SKirill Tkhai return 0; 604b0dedc49SKirill Tkhai 605b0dedc49SKirill Tkhai map = rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_map, 606b0dedc49SKirill Tkhai true); 607b0dedc49SKirill Tkhai if (unlikely(!map)) 608b0dedc49SKirill Tkhai goto unlock; 609b0dedc49SKirill Tkhai 610b0dedc49SKirill Tkhai for_each_set_bit(i, map->map, shrinker_nr_max) { 611b0dedc49SKirill Tkhai struct shrink_control sc = { 612b0dedc49SKirill Tkhai .gfp_mask = gfp_mask, 613b0dedc49SKirill Tkhai .nid = nid, 614b0dedc49SKirill Tkhai .memcg = memcg, 615b0dedc49SKirill Tkhai }; 616b0dedc49SKirill Tkhai struct shrinker *shrinker; 617b0dedc49SKirill Tkhai 618b0dedc49SKirill Tkhai shrinker = idr_find(&shrinker_idr, i); 6197e010df5SKirill Tkhai if (unlikely(!shrinker || shrinker == SHRINKER_REGISTERING)) { 6207e010df5SKirill Tkhai if (!shrinker) 621b0dedc49SKirill Tkhai clear_bit(i, map->map); 622b0dedc49SKirill Tkhai continue; 623b0dedc49SKirill Tkhai } 624b0dedc49SKirill Tkhai 6250a432dcbSYang Shi /* Call non-slab shrinkers even though kmem is disabled */ 6260a432dcbSYang Shi if (!memcg_kmem_enabled() && 6270a432dcbSYang Shi !(shrinker->flags & SHRINKER_NONSLAB)) 6280a432dcbSYang Shi continue; 6290a432dcbSYang Shi 630b0dedc49SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 631f90280d6SKirill Tkhai if (ret == SHRINK_EMPTY) { 632f90280d6SKirill Tkhai clear_bit(i, map->map); 633f90280d6SKirill Tkhai /* 634f90280d6SKirill Tkhai * After the shrinker reported that it had no objects to 635f90280d6SKirill Tkhai * free, but before we cleared the corresponding bit in 636f90280d6SKirill Tkhai * the memcg shrinker map, a new object might have been 637f90280d6SKirill Tkhai * added. To make sure, we have the bit set in this 638f90280d6SKirill Tkhai * case, we invoke the shrinker one more time and reset 639f90280d6SKirill Tkhai * the bit if it reports that it is not empty anymore. 640f90280d6SKirill Tkhai * The memory barrier here pairs with the barrier in 641f90280d6SKirill Tkhai * memcg_set_shrinker_bit(): 642f90280d6SKirill Tkhai * 643f90280d6SKirill Tkhai * list_lru_add() shrink_slab_memcg() 644f90280d6SKirill Tkhai * list_add_tail() clear_bit() 645f90280d6SKirill Tkhai * <MB> <MB> 646f90280d6SKirill Tkhai * set_bit() do_shrink_slab() 647f90280d6SKirill Tkhai */ 648f90280d6SKirill Tkhai smp_mb__after_atomic(); 649f90280d6SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 6509b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 6519b996468SKirill Tkhai ret = 0; 652f90280d6SKirill Tkhai else 653f90280d6SKirill Tkhai memcg_set_shrinker_bit(memcg, nid, i); 654f90280d6SKirill Tkhai } 655b0dedc49SKirill Tkhai freed += ret; 656b0dedc49SKirill Tkhai 657b0dedc49SKirill Tkhai if (rwsem_is_contended(&shrinker_rwsem)) { 658b0dedc49SKirill Tkhai freed = freed ? : 1; 659b0dedc49SKirill Tkhai break; 660b0dedc49SKirill Tkhai } 661b0dedc49SKirill Tkhai } 662b0dedc49SKirill Tkhai unlock: 663b0dedc49SKirill Tkhai up_read(&shrinker_rwsem); 664b0dedc49SKirill Tkhai return freed; 665b0dedc49SKirill Tkhai } 6660a432dcbSYang Shi #else /* CONFIG_MEMCG */ 667b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 668b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 669b0dedc49SKirill Tkhai { 670b0dedc49SKirill Tkhai return 0; 671b0dedc49SKirill Tkhai } 6720a432dcbSYang Shi #endif /* CONFIG_MEMCG */ 673b0dedc49SKirill Tkhai 6746b4f7799SJohannes Weiner /** 675cb731d6cSVladimir Davydov * shrink_slab - shrink slab caches 6766b4f7799SJohannes Weiner * @gfp_mask: allocation context 6776b4f7799SJohannes Weiner * @nid: node whose slab caches to target 678cb731d6cSVladimir Davydov * @memcg: memory cgroup whose slab caches to target 6799092c71bSJosef Bacik * @priority: the reclaim priority 6801d3d4437SGlauber Costa * 6816b4f7799SJohannes Weiner * Call the shrink functions to age shrinkable caches. 6821d3d4437SGlauber Costa * 6836b4f7799SJohannes Weiner * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, 6846b4f7799SJohannes Weiner * unaware shrinkers will receive a node id of 0 instead. 6851d3d4437SGlauber Costa * 686aeed1d32SVladimir Davydov * @memcg specifies the memory cgroup to target. Unaware shrinkers 687aeed1d32SVladimir Davydov * are called only if it is the root cgroup. 688cb731d6cSVladimir Davydov * 6899092c71bSJosef Bacik * @priority is sc->priority, we take the number of objects and >> by priority 6909092c71bSJosef Bacik * in order to get the scan target. 6911d3d4437SGlauber Costa * 6926b4f7799SJohannes Weiner * Returns the number of reclaimed slab objects. 6931d3d4437SGlauber Costa */ 694cb731d6cSVladimir Davydov static unsigned long shrink_slab(gfp_t gfp_mask, int nid, 695cb731d6cSVladimir Davydov struct mem_cgroup *memcg, 6969092c71bSJosef Bacik int priority) 6971d3d4437SGlauber Costa { 698b8e57efaSKirill Tkhai unsigned long ret, freed = 0; 6991d3d4437SGlauber Costa struct shrinker *shrinker; 7001d3d4437SGlauber Costa 701fa1e512fSYang Shi /* 702fa1e512fSYang Shi * The root memcg might be allocated even though memcg is disabled 703fa1e512fSYang Shi * via "cgroup_disable=memory" boot parameter. This could make 704fa1e512fSYang Shi * mem_cgroup_is_root() return false, then just run memcg slab 705fa1e512fSYang Shi * shrink, but skip global shrink. This may result in premature 706fa1e512fSYang Shi * oom. 707fa1e512fSYang Shi */ 708fa1e512fSYang Shi if (!mem_cgroup_disabled() && !mem_cgroup_is_root(memcg)) 709b0dedc49SKirill Tkhai return shrink_slab_memcg(gfp_mask, nid, memcg, priority); 710cb731d6cSVladimir Davydov 711e830c63aSTetsuo Handa if (!down_read_trylock(&shrinker_rwsem)) 7121d3d4437SGlauber Costa goto out; 7131d3d4437SGlauber Costa 7141d3d4437SGlauber Costa list_for_each_entry(shrinker, &shrinker_list, list) { 7156b4f7799SJohannes Weiner struct shrink_control sc = { 7166b4f7799SJohannes Weiner .gfp_mask = gfp_mask, 7176b4f7799SJohannes Weiner .nid = nid, 718cb731d6cSVladimir Davydov .memcg = memcg, 7196b4f7799SJohannes Weiner }; 7206b4f7799SJohannes Weiner 7219b996468SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 7229b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 7239b996468SKirill Tkhai ret = 0; 7249b996468SKirill Tkhai freed += ret; 725e496612cSMinchan Kim /* 726e496612cSMinchan Kim * Bail out if someone want to register a new shrinker to 727e496612cSMinchan Kim * prevent the regsitration from being stalled for long periods 728e496612cSMinchan Kim * by parallel ongoing shrinking. 729e496612cSMinchan Kim */ 730e496612cSMinchan Kim if (rwsem_is_contended(&shrinker_rwsem)) { 731e496612cSMinchan Kim freed = freed ? : 1; 732e496612cSMinchan Kim break; 733e496612cSMinchan Kim } 734ec97097bSVladimir Davydov } 7351d3d4437SGlauber Costa 7361da177e4SLinus Torvalds up_read(&shrinker_rwsem); 737f06590bdSMinchan Kim out: 738f06590bdSMinchan Kim cond_resched(); 73924f7c6b9SDave Chinner return freed; 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds 742cb731d6cSVladimir Davydov void drop_slab_node(int nid) 743cb731d6cSVladimir Davydov { 744cb731d6cSVladimir Davydov unsigned long freed; 745cb731d6cSVladimir Davydov 746cb731d6cSVladimir Davydov do { 747cb731d6cSVladimir Davydov struct mem_cgroup *memcg = NULL; 748cb731d6cSVladimir Davydov 749cb731d6cSVladimir Davydov freed = 0; 750aeed1d32SVladimir Davydov memcg = mem_cgroup_iter(NULL, NULL, NULL); 751cb731d6cSVladimir Davydov do { 7529092c71bSJosef Bacik freed += shrink_slab(GFP_KERNEL, nid, memcg, 0); 753cb731d6cSVladimir Davydov } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 754cb731d6cSVladimir Davydov } while (freed > 10); 755cb731d6cSVladimir Davydov } 756cb731d6cSVladimir Davydov 757cb731d6cSVladimir Davydov void drop_slab(void) 758cb731d6cSVladimir Davydov { 759cb731d6cSVladimir Davydov int nid; 760cb731d6cSVladimir Davydov 761cb731d6cSVladimir Davydov for_each_online_node(nid) 762cb731d6cSVladimir Davydov drop_slab_node(nid); 763cb731d6cSVladimir Davydov } 764cb731d6cSVladimir Davydov 7651da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 7661da177e4SLinus Torvalds { 767ceddc3a5SJohannes Weiner /* 768ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 76967891fffSMatthew Wilcox * that isolated the page, the page cache and optional buffer 77067891fffSMatthew Wilcox * heads at page->private. 771ceddc3a5SJohannes Weiner */ 77267891fffSMatthew Wilcox int page_cache_pins = PageTransHuge(page) && PageSwapCache(page) ? 773bd4c82c2SHuang Ying HPAGE_PMD_NR : 1; 77467891fffSMatthew Wilcox return page_count(page) - page_has_private(page) == 1 + page_cache_pins; 7751da177e4SLinus Torvalds } 7761da177e4SLinus Torvalds 777703c2708STejun Heo static int may_write_to_inode(struct inode *inode, struct scan_control *sc) 7781da177e4SLinus Torvalds { 779930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 7801da177e4SLinus Torvalds return 1; 781703c2708STejun Heo if (!inode_write_congested(inode)) 7821da177e4SLinus Torvalds return 1; 783703c2708STejun Heo if (inode_to_bdi(inode) == current->backing_dev_info) 7841da177e4SLinus Torvalds return 1; 7851da177e4SLinus Torvalds return 0; 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds /* 7891da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 7901da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 7911da177e4SLinus Torvalds * fsync(), msync() or close(). 7921da177e4SLinus Torvalds * 7931da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 7941da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 7951da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 7961da177e4SLinus Torvalds * 7971da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 7981da177e4SLinus Torvalds * __GFP_FS. 7991da177e4SLinus Torvalds */ 8001da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 8011da177e4SLinus Torvalds struct page *page, int error) 8021da177e4SLinus Torvalds { 8037eaceaccSJens Axboe lock_page(page); 8043e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 8053e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 8061da177e4SLinus Torvalds unlock_page(page); 8071da177e4SLinus Torvalds } 8081da177e4SLinus Torvalds 80904e62a29SChristoph Lameter /* possible outcome of pageout() */ 81004e62a29SChristoph Lameter typedef enum { 81104e62a29SChristoph Lameter /* failed to write page out, page is locked */ 81204e62a29SChristoph Lameter PAGE_KEEP, 81304e62a29SChristoph Lameter /* move page to the active list, page is locked */ 81404e62a29SChristoph Lameter PAGE_ACTIVATE, 81504e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 81604e62a29SChristoph Lameter PAGE_SUCCESS, 81704e62a29SChristoph Lameter /* page is clean and locked */ 81804e62a29SChristoph Lameter PAGE_CLEAN, 81904e62a29SChristoph Lameter } pageout_t; 82004e62a29SChristoph Lameter 8211da177e4SLinus Torvalds /* 8221742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 8231742f19fSAndrew Morton * Calls ->writepage(). 8241da177e4SLinus Torvalds */ 825c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping, 8267d3579e8SKOSAKI Motohiro struct scan_control *sc) 8271da177e4SLinus Torvalds { 8281da177e4SLinus Torvalds /* 8291da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 8301da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 8311da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 8321da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 8331da177e4SLinus Torvalds * PagePrivate for that. 8341da177e4SLinus Torvalds * 8358174202bSAl Viro * If this process is currently in __generic_file_write_iter() against 8361da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 8371da177e4SLinus Torvalds * will block. 8381da177e4SLinus Torvalds * 8391da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 8401da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 8411da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 8421da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 8431da177e4SLinus Torvalds */ 8441da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 8451da177e4SLinus Torvalds return PAGE_KEEP; 8461da177e4SLinus Torvalds if (!mapping) { 8471da177e4SLinus Torvalds /* 8481da177e4SLinus Torvalds * Some data journaling orphaned pages can have 8491da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 8501da177e4SLinus Torvalds */ 851266cf658SDavid Howells if (page_has_private(page)) { 8521da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 8531da177e4SLinus Torvalds ClearPageDirty(page); 854b1de0d13SMitchel Humpherys pr_info("%s: orphaned page\n", __func__); 8551da177e4SLinus Torvalds return PAGE_CLEAN; 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds return PAGE_KEEP; 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 8611da177e4SLinus Torvalds return PAGE_ACTIVATE; 862703c2708STejun Heo if (!may_write_to_inode(mapping->host, sc)) 8631da177e4SLinus Torvalds return PAGE_KEEP; 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 8661da177e4SLinus Torvalds int res; 8671da177e4SLinus Torvalds struct writeback_control wbc = { 8681da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 8691da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 870111ebb6eSOGAWA Hirofumi .range_start = 0, 871111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 8721da177e4SLinus Torvalds .for_reclaim = 1, 8731da177e4SLinus Torvalds }; 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds SetPageReclaim(page); 8761da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 8771da177e4SLinus Torvalds if (res < 0) 8781da177e4SLinus Torvalds handle_write_error(mapping, page, res); 879994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 8801da177e4SLinus Torvalds ClearPageReclaim(page); 8811da177e4SLinus Torvalds return PAGE_ACTIVATE; 8821da177e4SLinus Torvalds } 883c661b078SAndy Whitcroft 8841da177e4SLinus Torvalds if (!PageWriteback(page)) { 8851da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 8861da177e4SLinus Torvalds ClearPageReclaim(page); 8871da177e4SLinus Torvalds } 8883aa23851Syalin wang trace_mm_vmscan_writepage(page); 889c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_WRITE); 8901da177e4SLinus Torvalds return PAGE_SUCCESS; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds return PAGE_CLEAN; 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds 896a649fd92SAndrew Morton /* 897e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 898e286781dSNick Piggin * gets returned with a refcount of 0. 899a649fd92SAndrew Morton */ 900a528910eSJohannes Weiner static int __remove_mapping(struct address_space *mapping, struct page *page, 901a528910eSJohannes Weiner bool reclaimed) 90249d2e9ccSChristoph Lameter { 903c4843a75SGreg Thelen unsigned long flags; 904bd4c82c2SHuang Ying int refcount; 905c4843a75SGreg Thelen 90628e4d965SNick Piggin BUG_ON(!PageLocked(page)); 90728e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 90849d2e9ccSChristoph Lameter 909b93b0163SMatthew Wilcox xa_lock_irqsave(&mapping->i_pages, flags); 91049d2e9ccSChristoph Lameter /* 9110fd0e6b0SNick Piggin * The non racy check for a busy page. 9120fd0e6b0SNick Piggin * 9130fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 9140fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 9150fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 9160fd0e6b0SNick Piggin * here, then the following race may occur: 9170fd0e6b0SNick Piggin * 9180fd0e6b0SNick Piggin * get_user_pages(&page); 9190fd0e6b0SNick Piggin * [user mapping goes away] 9200fd0e6b0SNick Piggin * write_to(page); 9210fd0e6b0SNick Piggin * !PageDirty(page) [good] 9220fd0e6b0SNick Piggin * SetPageDirty(page); 9230fd0e6b0SNick Piggin * put_page(page); 9240fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 9250fd0e6b0SNick Piggin * 9260fd0e6b0SNick Piggin * [oops, our write_to data is lost] 9270fd0e6b0SNick Piggin * 9280fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 9290fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 9300139aa7bSJoonsoo Kim * load is not satisfied before that of page->_refcount. 9310fd0e6b0SNick Piggin * 9320fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 933b93b0163SMatthew Wilcox * and thus under the i_pages lock, then this ordering is not required. 93449d2e9ccSChristoph Lameter */ 935bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page)) && PageSwapCache(page)) 936bd4c82c2SHuang Ying refcount = 1 + HPAGE_PMD_NR; 937bd4c82c2SHuang Ying else 938bd4c82c2SHuang Ying refcount = 2; 939bd4c82c2SHuang Ying if (!page_ref_freeze(page, refcount)) 94049d2e9ccSChristoph Lameter goto cannot_free; 9411c4c3b99SJiang Biao /* note: atomic_cmpxchg in page_ref_freeze provides the smp_rmb */ 942e286781dSNick Piggin if (unlikely(PageDirty(page))) { 943bd4c82c2SHuang Ying page_ref_unfreeze(page, refcount); 94449d2e9ccSChristoph Lameter goto cannot_free; 945e286781dSNick Piggin } 94649d2e9ccSChristoph Lameter 94749d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 94849d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 9490a31bc97SJohannes Weiner mem_cgroup_swapout(page, swap); 9504e17ec25SMatthew Wilcox __delete_from_swap_cache(page, swap); 951b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 95275f6d6d2SMinchan Kim put_swap_page(page, swap); 953e286781dSNick Piggin } else { 9546072d13cSLinus Torvalds void (*freepage)(struct page *); 955a528910eSJohannes Weiner void *shadow = NULL; 9566072d13cSLinus Torvalds 9576072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 958a528910eSJohannes Weiner /* 959a528910eSJohannes Weiner * Remember a shadow entry for reclaimed file cache in 960a528910eSJohannes Weiner * order to detect refaults, thus thrashing, later on. 961a528910eSJohannes Weiner * 962a528910eSJohannes Weiner * But don't store shadows in an address space that is 963a528910eSJohannes Weiner * already exiting. This is not just an optizimation, 964a528910eSJohannes Weiner * inode reclaim needs to empty out the radix tree or 965a528910eSJohannes Weiner * the nodes are lost. Don't plant shadows behind its 966a528910eSJohannes Weiner * back. 967f9fe48beSRoss Zwisler * 968f9fe48beSRoss Zwisler * We also don't store shadows for DAX mappings because the 969f9fe48beSRoss Zwisler * only page cache pages found in these are zero pages 970f9fe48beSRoss Zwisler * covering holes, and because we don't want to mix DAX 971f9fe48beSRoss Zwisler * exceptional entries and shadow exceptional entries in the 972b93b0163SMatthew Wilcox * same address_space. 973a528910eSJohannes Weiner */ 974a528910eSJohannes Weiner if (reclaimed && page_is_file_cache(page) && 975f9fe48beSRoss Zwisler !mapping_exiting(mapping) && !dax_mapping(mapping)) 976a7ca12f9SAndrey Ryabinin shadow = workingset_eviction(page); 97762cccb8cSJohannes Weiner __delete_from_page_cache(page, shadow); 978b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 9796072d13cSLinus Torvalds 9806072d13cSLinus Torvalds if (freepage != NULL) 9816072d13cSLinus Torvalds freepage(page); 982e286781dSNick Piggin } 983e286781dSNick Piggin 98449d2e9ccSChristoph Lameter return 1; 98549d2e9ccSChristoph Lameter 98649d2e9ccSChristoph Lameter cannot_free: 987b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 98849d2e9ccSChristoph Lameter return 0; 98949d2e9ccSChristoph Lameter } 99049d2e9ccSChristoph Lameter 9911da177e4SLinus Torvalds /* 992e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 993e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 994e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 995e286781dSNick Piggin * this page. 996e286781dSNick Piggin */ 997e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 998e286781dSNick Piggin { 999a528910eSJohannes Weiner if (__remove_mapping(mapping, page, false)) { 1000e286781dSNick Piggin /* 1001e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 1002e286781dSNick Piggin * drops the pagecache ref for us without requiring another 1003e286781dSNick Piggin * atomic operation. 1004e286781dSNick Piggin */ 1005fe896d18SJoonsoo Kim page_ref_unfreeze(page, 1); 1006e286781dSNick Piggin return 1; 1007e286781dSNick Piggin } 1008e286781dSNick Piggin return 0; 1009e286781dSNick Piggin } 1010e286781dSNick Piggin 1011894bc310SLee Schermerhorn /** 1012894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 1013894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 1014894bc310SLee Schermerhorn * 1015894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 1016894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 1017894bc310SLee Schermerhorn * 1018894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 1019894bc310SLee Schermerhorn */ 1020894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 1021894bc310SLee Schermerhorn { 1022c53954a0SMel Gorman lru_cache_add(page); 1023894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 1024894bc310SLee Schermerhorn } 1025894bc310SLee Schermerhorn 1026dfc8d636SJohannes Weiner enum page_references { 1027dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 1028dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 102964574746SJohannes Weiner PAGEREF_KEEP, 1030dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 1031dfc8d636SJohannes Weiner }; 1032dfc8d636SJohannes Weiner 1033dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 1034dfc8d636SJohannes Weiner struct scan_control *sc) 1035dfc8d636SJohannes Weiner { 103664574746SJohannes Weiner int referenced_ptes, referenced_page; 1037dfc8d636SJohannes Weiner unsigned long vm_flags; 1038dfc8d636SJohannes Weiner 1039c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 1040c3ac9a8aSJohannes Weiner &vm_flags); 104164574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 1042dfc8d636SJohannes Weiner 1043dfc8d636SJohannes Weiner /* 1044dfc8d636SJohannes Weiner * Mlock lost the isolation race with us. Let try_to_unmap() 1045dfc8d636SJohannes Weiner * move the page to the unevictable list. 1046dfc8d636SJohannes Weiner */ 1047dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 1048dfc8d636SJohannes Weiner return PAGEREF_RECLAIM; 1049dfc8d636SJohannes Weiner 105064574746SJohannes Weiner if (referenced_ptes) { 1051e4898273SMichal Hocko if (PageSwapBacked(page)) 105264574746SJohannes Weiner return PAGEREF_ACTIVATE; 105364574746SJohannes Weiner /* 105464574746SJohannes Weiner * All mapped pages start out with page table 105564574746SJohannes Weiner * references from the instantiating fault, so we need 105664574746SJohannes Weiner * to look twice if a mapped file page is used more 105764574746SJohannes Weiner * than once. 105864574746SJohannes Weiner * 105964574746SJohannes Weiner * Mark it and spare it for another trip around the 106064574746SJohannes Weiner * inactive list. Another page table reference will 106164574746SJohannes Weiner * lead to its activation. 106264574746SJohannes Weiner * 106364574746SJohannes Weiner * Note: the mark is set for activated pages as well 106464574746SJohannes Weiner * so that recently deactivated but used pages are 106564574746SJohannes Weiner * quickly recovered. 106664574746SJohannes Weiner */ 106764574746SJohannes Weiner SetPageReferenced(page); 106864574746SJohannes Weiner 106934dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 1070dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 1071dfc8d636SJohannes Weiner 1072c909e993SKonstantin Khlebnikov /* 1073c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 1074c909e993SKonstantin Khlebnikov */ 1075c909e993SKonstantin Khlebnikov if (vm_flags & VM_EXEC) 1076c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 1077c909e993SKonstantin Khlebnikov 107864574746SJohannes Weiner return PAGEREF_KEEP; 107964574746SJohannes Weiner } 108064574746SJohannes Weiner 1081dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 10822e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 1083dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 108464574746SJohannes Weiner 108564574746SJohannes Weiner return PAGEREF_RECLAIM; 1086dfc8d636SJohannes Weiner } 1087dfc8d636SJohannes Weiner 1088e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 1089e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page, 1090e2be15f6SMel Gorman bool *dirty, bool *writeback) 1091e2be15f6SMel Gorman { 1092b4597226SMel Gorman struct address_space *mapping; 1093b4597226SMel Gorman 1094e2be15f6SMel Gorman /* 1095e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 1096e2be15f6SMel Gorman * from reclaim context. Do not stall reclaim based on them 1097e2be15f6SMel Gorman */ 1098802a3a92SShaohua Li if (!page_is_file_cache(page) || 1099802a3a92SShaohua Li (PageAnon(page) && !PageSwapBacked(page))) { 1100e2be15f6SMel Gorman *dirty = false; 1101e2be15f6SMel Gorman *writeback = false; 1102e2be15f6SMel Gorman return; 1103e2be15f6SMel Gorman } 1104e2be15f6SMel Gorman 1105e2be15f6SMel Gorman /* By default assume that the page flags are accurate */ 1106e2be15f6SMel Gorman *dirty = PageDirty(page); 1107e2be15f6SMel Gorman *writeback = PageWriteback(page); 1108b4597226SMel Gorman 1109b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 1110b4597226SMel Gorman if (!page_has_private(page)) 1111b4597226SMel Gorman return; 1112b4597226SMel Gorman 1113b4597226SMel Gorman mapping = page_mapping(page); 1114b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 1115b4597226SMel Gorman mapping->a_ops->is_dirty_writeback(page, dirty, writeback); 1116e2be15f6SMel Gorman } 1117e2be15f6SMel Gorman 1118e286781dSNick Piggin /* 11191742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 11201da177e4SLinus Torvalds */ 11211742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list, 1122599d0c95SMel Gorman struct pglist_data *pgdat, 1123f84f6e2bSMel Gorman struct scan_control *sc, 112402c6de8dSMinchan Kim enum ttu_flags ttu_flags, 11253c710c1aSMichal Hocko struct reclaim_stat *stat, 11268940b34aSMinchan Kim bool ignore_references) 11271da177e4SLinus Torvalds { 11281da177e4SLinus Torvalds LIST_HEAD(ret_pages); 1129abe4c3b5SMel Gorman LIST_HEAD(free_pages); 11303c710c1aSMichal Hocko unsigned nr_reclaimed = 0; 1131886cf190SKirill Tkhai unsigned pgactivate = 0; 11321da177e4SLinus Torvalds 1133060f005fSKirill Tkhai memset(stat, 0, sizeof(*stat)); 11341da177e4SLinus Torvalds cond_resched(); 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds while (!list_empty(page_list)) { 11371da177e4SLinus Torvalds struct address_space *mapping; 11381da177e4SLinus Torvalds struct page *page; 11391da177e4SLinus Torvalds int may_enter_fs; 11408940b34aSMinchan Kim enum page_references references = PAGEREF_RECLAIM; 1141e2be15f6SMel Gorman bool dirty, writeback; 114298879b3bSYang Shi unsigned int nr_pages; 11431da177e4SLinus Torvalds 11441da177e4SLinus Torvalds cond_resched(); 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds page = lru_to_page(page_list); 11471da177e4SLinus Torvalds list_del(&page->lru); 11481da177e4SLinus Torvalds 1149529ae9aaSNick Piggin if (!trylock_page(page)) 11501da177e4SLinus Torvalds goto keep; 11511da177e4SLinus Torvalds 1152309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 11531da177e4SLinus Torvalds 1154d8c6546bSMatthew Wilcox (Oracle) nr_pages = compound_nr(page); 115598879b3bSYang Shi 115698879b3bSYang Shi /* Account the number of base pages even though THP */ 115798879b3bSYang Shi sc->nr_scanned += nr_pages; 115880e43426SChristoph Lameter 115939b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) 1160ad6b6704SMinchan Kim goto activate_locked; 1161894bc310SLee Schermerhorn 1162a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 116380e43426SChristoph Lameter goto keep_locked; 116480e43426SChristoph Lameter 1165c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 1166c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 1167c661b078SAndy Whitcroft 1168e62e384eSMichal Hocko /* 1169894befecSAndrey Ryabinin * The number of dirty pages determines if a node is marked 1170e2be15f6SMel Gorman * reclaim_congested which affects wait_iff_congested. kswapd 1171e2be15f6SMel Gorman * will stall and start writing pages if the tail of the LRU 1172e2be15f6SMel Gorman * is all dirty unqueued pages. 1173e2be15f6SMel Gorman */ 1174e2be15f6SMel Gorman page_check_dirty_writeback(page, &dirty, &writeback); 1175e2be15f6SMel Gorman if (dirty || writeback) 1176060f005fSKirill Tkhai stat->nr_dirty++; 1177e2be15f6SMel Gorman 1178e2be15f6SMel Gorman if (dirty && !writeback) 1179060f005fSKirill Tkhai stat->nr_unqueued_dirty++; 1180e2be15f6SMel Gorman 1181d04e8acdSMel Gorman /* 1182d04e8acdSMel Gorman * Treat this page as congested if the underlying BDI is or if 1183d04e8acdSMel Gorman * pages are cycling through the LRU so quickly that the 1184d04e8acdSMel Gorman * pages marked for immediate reclaim are making it to the 1185d04e8acdSMel Gorman * end of the LRU a second time. 1186d04e8acdSMel Gorman */ 1187e2be15f6SMel Gorman mapping = page_mapping(page); 11881da58ee2SJamie Liu if (((dirty || writeback) && mapping && 1189703c2708STejun Heo inode_write_congested(mapping->host)) || 1190d04e8acdSMel Gorman (writeback && PageReclaim(page))) 1191060f005fSKirill Tkhai stat->nr_congested++; 1192e2be15f6SMel Gorman 1193e2be15f6SMel Gorman /* 1194283aba9fSMel Gorman * If a page at the tail of the LRU is under writeback, there 1195283aba9fSMel Gorman * are three cases to consider. 1196e62e384eSMichal Hocko * 1197283aba9fSMel Gorman * 1) If reclaim is encountering an excessive number of pages 1198283aba9fSMel Gorman * under writeback and this page is both under writeback and 1199283aba9fSMel Gorman * PageReclaim then it indicates that pages are being queued 1200283aba9fSMel Gorman * for IO but are being recycled through the LRU before the 1201283aba9fSMel Gorman * IO can complete. Waiting on the page itself risks an 1202283aba9fSMel Gorman * indefinite stall if it is impossible to writeback the 1203283aba9fSMel Gorman * page due to IO error or disconnected storage so instead 1204b1a6f21eSMel Gorman * note that the LRU is being scanned too quickly and the 1205b1a6f21eSMel Gorman * caller can stall after page list has been processed. 1206c3b94f44SHugh Dickins * 120797c9341fSTejun Heo * 2) Global or new memcg reclaim encounters a page that is 1208ecf5fc6eSMichal Hocko * not marked for immediate reclaim, or the caller does not 1209ecf5fc6eSMichal Hocko * have __GFP_FS (or __GFP_IO if it's simply going to swap, 1210ecf5fc6eSMichal Hocko * not to fs). In this case mark the page for immediate 121197c9341fSTejun Heo * reclaim and continue scanning. 1212283aba9fSMel Gorman * 1213ecf5fc6eSMichal Hocko * Require may_enter_fs because we would wait on fs, which 1214ecf5fc6eSMichal Hocko * may not have submitted IO yet. And the loop driver might 1215283aba9fSMel Gorman * enter reclaim, and deadlock if it waits on a page for 1216283aba9fSMel Gorman * which it is needed to do the write (loop masks off 1217283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 1218283aba9fSMel Gorman * would probably show more reasons. 1219283aba9fSMel Gorman * 12207fadc820SHugh Dickins * 3) Legacy memcg encounters a page that is already marked 1221283aba9fSMel Gorman * PageReclaim. memcg does not have any dirty pages 1222283aba9fSMel Gorman * throttling so we could easily OOM just because too many 1223283aba9fSMel Gorman * pages are in writeback and there is nothing else to 1224283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 1225c55e8d03SJohannes Weiner * 1226c55e8d03SJohannes Weiner * In cases 1) and 2) we activate the pages to get them out of 1227c55e8d03SJohannes Weiner * the way while we continue scanning for clean pages on the 1228c55e8d03SJohannes Weiner * inactive list and refilling from the active list. The 1229c55e8d03SJohannes Weiner * observation here is that waiting for disk writes is more 1230c55e8d03SJohannes Weiner * expensive than potentially causing reloads down the line. 1231c55e8d03SJohannes Weiner * Since they're marked for immediate reclaim, they won't put 1232c55e8d03SJohannes Weiner * memory pressure on the cache working set any longer than it 1233c55e8d03SJohannes Weiner * takes to write them to disk. 1234e62e384eSMichal Hocko */ 1235283aba9fSMel Gorman if (PageWriteback(page)) { 1236283aba9fSMel Gorman /* Case 1 above */ 1237283aba9fSMel Gorman if (current_is_kswapd() && 1238283aba9fSMel Gorman PageReclaim(page) && 1239599d0c95SMel Gorman test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { 1240060f005fSKirill Tkhai stat->nr_immediate++; 1241c55e8d03SJohannes Weiner goto activate_locked; 1242283aba9fSMel Gorman 1243283aba9fSMel Gorman /* Case 2 above */ 124497c9341fSTejun Heo } else if (sane_reclaim(sc) || 1245ecf5fc6eSMichal Hocko !PageReclaim(page) || !may_enter_fs) { 1246c3b94f44SHugh Dickins /* 1247c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 1248c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 1249c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 1250c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 1251c3b94f44SHugh Dickins * enough to care. What we do want is for this 1252c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 1253c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 1254c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 1255c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 1256c3b94f44SHugh Dickins */ 1257c3b94f44SHugh Dickins SetPageReclaim(page); 1258060f005fSKirill Tkhai stat->nr_writeback++; 1259c55e8d03SJohannes Weiner goto activate_locked; 1260283aba9fSMel Gorman 1261283aba9fSMel Gorman /* Case 3 above */ 1262283aba9fSMel Gorman } else { 12637fadc820SHugh Dickins unlock_page(page); 1264c3b94f44SHugh Dickins wait_on_page_writeback(page); 12657fadc820SHugh Dickins /* then go back and try same page again */ 12667fadc820SHugh Dickins list_add_tail(&page->lru, page_list); 12677fadc820SHugh Dickins continue; 1268e62e384eSMichal Hocko } 1269283aba9fSMel Gorman } 12701da177e4SLinus Torvalds 12718940b34aSMinchan Kim if (!ignore_references) 12726a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 127302c6de8dSMinchan Kim 1274dfc8d636SJohannes Weiner switch (references) { 1275dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 12761da177e4SLinus Torvalds goto activate_locked; 127764574746SJohannes Weiner case PAGEREF_KEEP: 127898879b3bSYang Shi stat->nr_ref_keep += nr_pages; 127964574746SJohannes Weiner goto keep_locked; 1280dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 1281dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 1282dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 1283dfc8d636SJohannes Weiner } 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds /* 12861da177e4SLinus Torvalds * Anonymous process memory has backing store? 12871da177e4SLinus Torvalds * Try to allocate it some swap space here. 1288802a3a92SShaohua Li * Lazyfree page could be freed directly 12891da177e4SLinus Torvalds */ 1290bd4c82c2SHuang Ying if (PageAnon(page) && PageSwapBacked(page)) { 1291bd4c82c2SHuang Ying if (!PageSwapCache(page)) { 129263eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 129363eb6b93SHugh Dickins goto keep_locked; 1294747552b1SHuang Ying if (PageTransHuge(page)) { 1295b8f593cdSHuang Ying /* cannot split THP, skip it */ 1296747552b1SHuang Ying if (!can_split_huge_page(page, NULL)) 1297b8f593cdSHuang Ying goto activate_locked; 1298747552b1SHuang Ying /* 1299747552b1SHuang Ying * Split pages without a PMD map right 1300747552b1SHuang Ying * away. Chances are some or all of the 1301747552b1SHuang Ying * tail pages can be freed without IO. 1302747552b1SHuang Ying */ 1303747552b1SHuang Ying if (!compound_mapcount(page) && 1304bd4c82c2SHuang Ying split_huge_page_to_list(page, 1305bd4c82c2SHuang Ying page_list)) 1306747552b1SHuang Ying goto activate_locked; 1307747552b1SHuang Ying } 13080f074658SMinchan Kim if (!add_to_swap(page)) { 13090f074658SMinchan Kim if (!PageTransHuge(page)) 131098879b3bSYang Shi goto activate_locked_split; 1311bd4c82c2SHuang Ying /* Fallback to swap normal pages */ 1312bd4c82c2SHuang Ying if (split_huge_page_to_list(page, 1313bd4c82c2SHuang Ying page_list)) 13140f074658SMinchan Kim goto activate_locked; 1315fe490cc0SHuang Ying #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1316fe490cc0SHuang Ying count_vm_event(THP_SWPOUT_FALLBACK); 1317fe490cc0SHuang Ying #endif 13180f074658SMinchan Kim if (!add_to_swap(page)) 131998879b3bSYang Shi goto activate_locked_split; 13200f074658SMinchan Kim } 13210f074658SMinchan Kim 132263eb6b93SHugh Dickins may_enter_fs = 1; 13231da177e4SLinus Torvalds 1324e2be15f6SMel Gorman /* Adding to swap updated mapping */ 13251da177e4SLinus Torvalds mapping = page_mapping(page); 1326bd4c82c2SHuang Ying } 13277751b2daSKirill A. Shutemov } else if (unlikely(PageTransHuge(page))) { 13287751b2daSKirill A. Shutemov /* Split file THP */ 13297751b2daSKirill A. Shutemov if (split_huge_page_to_list(page, page_list)) 13307751b2daSKirill A. Shutemov goto keep_locked; 1331e2be15f6SMel Gorman } 13321da177e4SLinus Torvalds 13331da177e4SLinus Torvalds /* 133498879b3bSYang Shi * THP may get split above, need minus tail pages and update 133598879b3bSYang Shi * nr_pages to avoid accounting tail pages twice. 133698879b3bSYang Shi * 133798879b3bSYang Shi * The tail pages that are added into swap cache successfully 133898879b3bSYang Shi * reach here. 133998879b3bSYang Shi */ 134098879b3bSYang Shi if ((nr_pages > 1) && !PageTransHuge(page)) { 134198879b3bSYang Shi sc->nr_scanned -= (nr_pages - 1); 134298879b3bSYang Shi nr_pages = 1; 134398879b3bSYang Shi } 134498879b3bSYang Shi 134598879b3bSYang Shi /* 13461da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 13471da177e4SLinus Torvalds * processes. Try to unmap it here. 13481da177e4SLinus Torvalds */ 1349802a3a92SShaohua Li if (page_mapped(page)) { 1350bd4c82c2SHuang Ying enum ttu_flags flags = ttu_flags | TTU_BATCH_FLUSH; 1351bd4c82c2SHuang Ying 1352bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page))) 1353bd4c82c2SHuang Ying flags |= TTU_SPLIT_HUGE_PMD; 1354bd4c82c2SHuang Ying if (!try_to_unmap(page, flags)) { 135598879b3bSYang Shi stat->nr_unmap_fail += nr_pages; 13561da177e4SLinus Torvalds goto activate_locked; 13571da177e4SLinus Torvalds } 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds if (PageDirty(page)) { 1361ee72886dSMel Gorman /* 13624eda4823SJohannes Weiner * Only kswapd can writeback filesystem pages 13634eda4823SJohannes Weiner * to avoid risk of stack overflow. But avoid 13644eda4823SJohannes Weiner * injecting inefficient single-page IO into 13654eda4823SJohannes Weiner * flusher writeback as much as possible: only 13664eda4823SJohannes Weiner * write pages when we've encountered many 13674eda4823SJohannes Weiner * dirty pages, and when we've already scanned 13684eda4823SJohannes Weiner * the rest of the LRU for clean pages and see 13694eda4823SJohannes Weiner * the same dirty pages again (PageReclaim). 1370ee72886dSMel Gorman */ 1371f84f6e2bSMel Gorman if (page_is_file_cache(page) && 13724eda4823SJohannes Weiner (!current_is_kswapd() || !PageReclaim(page) || 1373599d0c95SMel Gorman !test_bit(PGDAT_DIRTY, &pgdat->flags))) { 137449ea7eb6SMel Gorman /* 137549ea7eb6SMel Gorman * Immediately reclaim when written back. 137649ea7eb6SMel Gorman * Similar in principal to deactivate_page() 137749ea7eb6SMel Gorman * except we already have the page isolated 137849ea7eb6SMel Gorman * and know it's dirty 137949ea7eb6SMel Gorman */ 1380c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_IMMEDIATE); 138149ea7eb6SMel Gorman SetPageReclaim(page); 138249ea7eb6SMel Gorman 1383c55e8d03SJohannes Weiner goto activate_locked; 1384ee72886dSMel Gorman } 1385ee72886dSMel Gorman 1386dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 13871da177e4SLinus Torvalds goto keep_locked; 13884dd4b920SAndrew Morton if (!may_enter_fs) 13891da177e4SLinus Torvalds goto keep_locked; 139052a8363eSChristoph Lameter if (!sc->may_writepage) 13911da177e4SLinus Torvalds goto keep_locked; 13921da177e4SLinus Torvalds 1393d950c947SMel Gorman /* 1394d950c947SMel Gorman * Page is dirty. Flush the TLB if a writable entry 1395d950c947SMel Gorman * potentially exists to avoid CPU writes after IO 1396d950c947SMel Gorman * starts and then write it out here. 1397d950c947SMel Gorman */ 1398d950c947SMel Gorman try_to_unmap_flush_dirty(); 13997d3579e8SKOSAKI Motohiro switch (pageout(page, mapping, sc)) { 14001da177e4SLinus Torvalds case PAGE_KEEP: 14011da177e4SLinus Torvalds goto keep_locked; 14021da177e4SLinus Torvalds case PAGE_ACTIVATE: 14031da177e4SLinus Torvalds goto activate_locked; 14041da177e4SLinus Torvalds case PAGE_SUCCESS: 14057d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 140641ac1999SMel Gorman goto keep; 14077d3579e8SKOSAKI Motohiro if (PageDirty(page)) 14081da177e4SLinus Torvalds goto keep; 14097d3579e8SKOSAKI Motohiro 14101da177e4SLinus Torvalds /* 14111da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 14121da177e4SLinus Torvalds * ahead and try to reclaim the page. 14131da177e4SLinus Torvalds */ 1414529ae9aaSNick Piggin if (!trylock_page(page)) 14151da177e4SLinus Torvalds goto keep; 14161da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 14171da177e4SLinus Torvalds goto keep_locked; 14181da177e4SLinus Torvalds mapping = page_mapping(page); 14191da177e4SLinus Torvalds case PAGE_CLEAN: 14201da177e4SLinus Torvalds ; /* try to free the page below */ 14211da177e4SLinus Torvalds } 14221da177e4SLinus Torvalds } 14231da177e4SLinus Torvalds 14241da177e4SLinus Torvalds /* 14251da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 14261da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 14271da177e4SLinus Torvalds * the page as well. 14281da177e4SLinus Torvalds * 14291da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 14301da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 14311da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 14321da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 14331da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 14341da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 14351da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 14361da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 14371da177e4SLinus Torvalds * 14381da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 14391da177e4SLinus Torvalds * the pages which were not successfully invalidated in 14401da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 14411da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 14421da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 14431da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 14441da177e4SLinus Torvalds */ 1445266cf658SDavid Howells if (page_has_private(page)) { 14461da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 14471da177e4SLinus Torvalds goto activate_locked; 1448e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 1449e286781dSNick Piggin unlock_page(page); 1450e286781dSNick Piggin if (put_page_testzero(page)) 14511da177e4SLinus Torvalds goto free_it; 1452e286781dSNick Piggin else { 1453e286781dSNick Piggin /* 1454e286781dSNick Piggin * rare race with speculative reference. 1455e286781dSNick Piggin * the speculative reference will free 1456e286781dSNick Piggin * this page shortly, so we may 1457e286781dSNick Piggin * increment nr_reclaimed here (and 1458e286781dSNick Piggin * leave it off the LRU). 1459e286781dSNick Piggin */ 1460e286781dSNick Piggin nr_reclaimed++; 1461e286781dSNick Piggin continue; 1462e286781dSNick Piggin } 1463e286781dSNick Piggin } 14641da177e4SLinus Torvalds } 14651da177e4SLinus Torvalds 1466802a3a92SShaohua Li if (PageAnon(page) && !PageSwapBacked(page)) { 1467802a3a92SShaohua Li /* follow __remove_mapping for reference */ 1468802a3a92SShaohua Li if (!page_ref_freeze(page, 1)) 146949d2e9ccSChristoph Lameter goto keep_locked; 1470802a3a92SShaohua Li if (PageDirty(page)) { 1471802a3a92SShaohua Li page_ref_unfreeze(page, 1); 1472802a3a92SShaohua Li goto keep_locked; 1473802a3a92SShaohua Li } 14741da177e4SLinus Torvalds 1475802a3a92SShaohua Li count_vm_event(PGLAZYFREED); 14762262185cSRoman Gushchin count_memcg_page_event(page, PGLAZYFREED); 1477802a3a92SShaohua Li } else if (!mapping || !__remove_mapping(mapping, page, true)) 1478802a3a92SShaohua Li goto keep_locked; 14799a1ea439SHugh Dickins 14809a1ea439SHugh Dickins unlock_page(page); 1481e286781dSNick Piggin free_it: 148298879b3bSYang Shi /* 148398879b3bSYang Shi * THP may get swapped out in a whole, need account 148498879b3bSYang Shi * all base pages. 148598879b3bSYang Shi */ 148698879b3bSYang Shi nr_reclaimed += nr_pages; 1487abe4c3b5SMel Gorman 1488abe4c3b5SMel Gorman /* 1489abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1490abe4c3b5SMel Gorman * appear not as the counts should be low 1491abe4c3b5SMel Gorman */ 14927ae88534SYang Shi if (unlikely(PageTransHuge(page))) 1493bd4c82c2SHuang Ying (*get_compound_page_dtor(page))(page); 14947ae88534SYang Shi else 1495abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 14961da177e4SLinus Torvalds continue; 14971da177e4SLinus Torvalds 149898879b3bSYang Shi activate_locked_split: 149998879b3bSYang Shi /* 150098879b3bSYang Shi * The tail pages that are failed to add into swap cache 150198879b3bSYang Shi * reach here. Fixup nr_scanned and nr_pages. 150298879b3bSYang Shi */ 150398879b3bSYang Shi if (nr_pages > 1) { 150498879b3bSYang Shi sc->nr_scanned -= (nr_pages - 1); 150598879b3bSYang Shi nr_pages = 1; 150698879b3bSYang Shi } 15071da177e4SLinus Torvalds activate_locked: 150868a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 1509ad6b6704SMinchan Kim if (PageSwapCache(page) && (mem_cgroup_swap_full(page) || 1510ad6b6704SMinchan Kim PageMlocked(page))) 1511a2c43eedSHugh Dickins try_to_free_swap(page); 1512309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 1513ad6b6704SMinchan Kim if (!PageMlocked(page)) { 1514886cf190SKirill Tkhai int type = page_is_file_cache(page); 15151da177e4SLinus Torvalds SetPageActive(page); 151698879b3bSYang Shi stat->nr_activate[type] += nr_pages; 15172262185cSRoman Gushchin count_memcg_page_event(page, PGACTIVATE); 1518ad6b6704SMinchan Kim } 15191da177e4SLinus Torvalds keep_locked: 15201da177e4SLinus Torvalds unlock_page(page); 15211da177e4SLinus Torvalds keep: 15221da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 1523309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); 15241da177e4SLinus Torvalds } 1525abe4c3b5SMel Gorman 152698879b3bSYang Shi pgactivate = stat->nr_activate[0] + stat->nr_activate[1]; 152798879b3bSYang Shi 1528747db954SJohannes Weiner mem_cgroup_uncharge_list(&free_pages); 152972b252aeSMel Gorman try_to_unmap_flush(); 15302d4894b5SMel Gorman free_unref_page_list(&free_pages); 1531abe4c3b5SMel Gorman 15321da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1533886cf190SKirill Tkhai count_vm_events(PGACTIVATE, pgactivate); 15340a31bc97SJohannes Weiner 153505ff5137SAndrew Morton return nr_reclaimed; 15361da177e4SLinus Torvalds } 15371da177e4SLinus Torvalds 153802c6de8dSMinchan Kim unsigned long reclaim_clean_pages_from_list(struct zone *zone, 153902c6de8dSMinchan Kim struct list_head *page_list) 154002c6de8dSMinchan Kim { 154102c6de8dSMinchan Kim struct scan_control sc = { 154202c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 154302c6de8dSMinchan Kim .priority = DEF_PRIORITY, 154402c6de8dSMinchan Kim .may_unmap = 1, 154502c6de8dSMinchan Kim }; 1546060f005fSKirill Tkhai struct reclaim_stat dummy_stat; 15473c710c1aSMichal Hocko unsigned long ret; 154802c6de8dSMinchan Kim struct page *page, *next; 154902c6de8dSMinchan Kim LIST_HEAD(clean_pages); 155002c6de8dSMinchan Kim 155102c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 1552117aad1eSRafael Aquini if (page_is_file_cache(page) && !PageDirty(page) && 1553a58f2cefSMinchan Kim !__PageMovable(page) && !PageUnevictable(page)) { 155402c6de8dSMinchan Kim ClearPageActive(page); 155502c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 155602c6de8dSMinchan Kim } 155702c6de8dSMinchan Kim } 155802c6de8dSMinchan Kim 1559599d0c95SMel Gorman ret = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc, 1560060f005fSKirill Tkhai TTU_IGNORE_ACCESS, &dummy_stat, true); 156102c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 1562599d0c95SMel Gorman mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, -ret); 156302c6de8dSMinchan Kim return ret; 156402c6de8dSMinchan Kim } 156502c6de8dSMinchan Kim 15665ad333ebSAndy Whitcroft /* 15675ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 15685ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 15695ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 15705ad333ebSAndy Whitcroft * 15715ad333ebSAndy Whitcroft * page: page to consider 15725ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 15735ad333ebSAndy Whitcroft * 15745ad333ebSAndy Whitcroft * returns 0 on success, -ve errno on failure. 15755ad333ebSAndy Whitcroft */ 1576f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode) 15775ad333ebSAndy Whitcroft { 15785ad333ebSAndy Whitcroft int ret = -EINVAL; 15795ad333ebSAndy Whitcroft 15805ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 15815ad333ebSAndy Whitcroft if (!PageLRU(page)) 15825ad333ebSAndy Whitcroft return ret; 15835ad333ebSAndy Whitcroft 1584e46a2879SMinchan Kim /* Compaction should not handle unevictable pages but CMA can do so */ 1585e46a2879SMinchan Kim if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) 1586894bc310SLee Schermerhorn return ret; 1587894bc310SLee Schermerhorn 15885ad333ebSAndy Whitcroft ret = -EBUSY; 158908e552c6SKAMEZAWA Hiroyuki 1590c8244935SMel Gorman /* 1591c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 1592c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 1593c8244935SMel Gorman * blocking - clean pages for the most part. 1594c8244935SMel Gorman * 1595c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 1596c8244935SMel Gorman * that it is possible to migrate without blocking 1597c8244935SMel Gorman */ 15981276ad68SJohannes Weiner if (mode & ISOLATE_ASYNC_MIGRATE) { 1599c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 1600c8244935SMel Gorman if (PageWriteback(page)) 160139deaf85SMinchan Kim return ret; 160239deaf85SMinchan Kim 1603c8244935SMel Gorman if (PageDirty(page)) { 1604c8244935SMel Gorman struct address_space *mapping; 160569d763fcSMel Gorman bool migrate_dirty; 1606c8244935SMel Gorman 1607c8244935SMel Gorman /* 1608c8244935SMel Gorman * Only pages without mappings or that have a 1609c8244935SMel Gorman * ->migratepage callback are possible to migrate 161069d763fcSMel Gorman * without blocking. However, we can be racing with 161169d763fcSMel Gorman * truncation so it's necessary to lock the page 161269d763fcSMel Gorman * to stabilise the mapping as truncation holds 161369d763fcSMel Gorman * the page lock until after the page is removed 161469d763fcSMel Gorman * from the page cache. 1615c8244935SMel Gorman */ 161669d763fcSMel Gorman if (!trylock_page(page)) 161769d763fcSMel Gorman return ret; 161869d763fcSMel Gorman 1619c8244935SMel Gorman mapping = page_mapping(page); 1620145e1a71SHugh Dickins migrate_dirty = !mapping || mapping->a_ops->migratepage; 162169d763fcSMel Gorman unlock_page(page); 162269d763fcSMel Gorman if (!migrate_dirty) 1623c8244935SMel Gorman return ret; 1624c8244935SMel Gorman } 1625c8244935SMel Gorman } 1626c8244935SMel Gorman 1627f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 1628f80c0673SMinchan Kim return ret; 1629f80c0673SMinchan Kim 16305ad333ebSAndy Whitcroft if (likely(get_page_unless_zero(page))) { 16315ad333ebSAndy Whitcroft /* 16325ad333ebSAndy Whitcroft * Be careful not to clear PageLRU until after we're 16335ad333ebSAndy Whitcroft * sure the page is not being freed elsewhere -- the 16345ad333ebSAndy Whitcroft * page release code relies on it. 16355ad333ebSAndy Whitcroft */ 16365ad333ebSAndy Whitcroft ClearPageLRU(page); 16375ad333ebSAndy Whitcroft ret = 0; 16385ad333ebSAndy Whitcroft } 16395ad333ebSAndy Whitcroft 16405ad333ebSAndy Whitcroft return ret; 16415ad333ebSAndy Whitcroft } 16425ad333ebSAndy Whitcroft 16437ee36a14SMel Gorman 16447ee36a14SMel Gorman /* 16457ee36a14SMel Gorman * Update LRU sizes after isolating pages. The LRU size updates must 16467ee36a14SMel Gorman * be complete before mem_cgroup_update_lru_size due to a santity check. 16477ee36a14SMel Gorman */ 16487ee36a14SMel Gorman static __always_inline void update_lru_sizes(struct lruvec *lruvec, 1649b4536f0cSMichal Hocko enum lru_list lru, unsigned long *nr_zone_taken) 16507ee36a14SMel Gorman { 16517ee36a14SMel Gorman int zid; 16527ee36a14SMel Gorman 16537ee36a14SMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 16547ee36a14SMel Gorman if (!nr_zone_taken[zid]) 16557ee36a14SMel Gorman continue; 16567ee36a14SMel Gorman 16577ee36a14SMel Gorman __update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 1658b4536f0cSMichal Hocko #ifdef CONFIG_MEMCG 1659b4536f0cSMichal Hocko mem_cgroup_update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 1660b4536f0cSMichal Hocko #endif 16617ee36a14SMel Gorman } 16627ee36a14SMel Gorman 16637ee36a14SMel Gorman } 16647ee36a14SMel Gorman 1665f4b7e272SAndrey Ryabinin /** 1666f4b7e272SAndrey Ryabinin * pgdat->lru_lock is heavily contended. Some of the functions that 16671da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 16681da177e4SLinus Torvalds * and working on them outside the LRU lock. 16691da177e4SLinus Torvalds * 16701da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 16711da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 16721da177e4SLinus Torvalds * 16731da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 16741da177e4SLinus Torvalds * 1675791b48b6SMinchan Kim * @nr_to_scan: The number of eligible pages to look through on the list. 16765dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 16771da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1678f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1679fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 16805ad333ebSAndy Whitcroft * @mode: One of the LRU isolation modes 16813cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 16821da177e4SLinus Torvalds * 16831da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 16841da177e4SLinus Torvalds */ 168569e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 16865dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1687fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 1688a9e7c39fSKirill Tkhai enum lru_list lru) 16891da177e4SLinus Torvalds { 169075b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 169169e05944SAndrew Morton unsigned long nr_taken = 0; 1692599d0c95SMel Gorman unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; 16937cc30fcfSMel Gorman unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; 16943db65812SJohannes Weiner unsigned long skipped = 0; 1695791b48b6SMinchan Kim unsigned long scan, total_scan, nr_pages; 1696b2e18757SMel Gorman LIST_HEAD(pages_skipped); 1697a9e7c39fSKirill Tkhai isolate_mode_t mode = (sc->may_unmap ? 0 : ISOLATE_UNMAPPED); 16981da177e4SLinus Torvalds 169998879b3bSYang Shi total_scan = 0; 1700791b48b6SMinchan Kim scan = 0; 170198879b3bSYang Shi while (scan < nr_to_scan && !list_empty(src)) { 17025ad333ebSAndy Whitcroft struct page *page; 17035ad333ebSAndy Whitcroft 17041da177e4SLinus Torvalds page = lru_to_page(src); 17051da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 17061da177e4SLinus Torvalds 1707309381feSSasha Levin VM_BUG_ON_PAGE(!PageLRU(page), page); 17088d438f96SNick Piggin 1709d8c6546bSMatthew Wilcox (Oracle) nr_pages = compound_nr(page); 171098879b3bSYang Shi total_scan += nr_pages; 171198879b3bSYang Shi 1712b2e18757SMel Gorman if (page_zonenum(page) > sc->reclaim_idx) { 1713b2e18757SMel Gorman list_move(&page->lru, &pages_skipped); 171498879b3bSYang Shi nr_skipped[page_zonenum(page)] += nr_pages; 1715b2e18757SMel Gorman continue; 1716b2e18757SMel Gorman } 1717b2e18757SMel Gorman 1718791b48b6SMinchan Kim /* 1719791b48b6SMinchan Kim * Do not count skipped pages because that makes the function 1720791b48b6SMinchan Kim * return with no isolated pages if the LRU mostly contains 1721791b48b6SMinchan Kim * ineligible pages. This causes the VM to not reclaim any 1722791b48b6SMinchan Kim * pages, triggering a premature OOM. 172398879b3bSYang Shi * 172498879b3bSYang Shi * Account all tail pages of THP. This would not cause 172598879b3bSYang Shi * premature OOM since __isolate_lru_page() returns -EBUSY 172698879b3bSYang Shi * only when the page is being freed somewhere else. 1727791b48b6SMinchan Kim */ 172898879b3bSYang Shi scan += nr_pages; 1729f3fd4a61SKonstantin Khlebnikov switch (__isolate_lru_page(page, mode)) { 17305ad333ebSAndy Whitcroft case 0: 1731599d0c95SMel Gorman nr_taken += nr_pages; 1732599d0c95SMel Gorman nr_zone_taken[page_zonenum(page)] += nr_pages; 17335ad333ebSAndy Whitcroft list_move(&page->lru, dst); 17345ad333ebSAndy Whitcroft break; 17357c8ee9a8SNick Piggin 17365ad333ebSAndy Whitcroft case -EBUSY: 17375ad333ebSAndy Whitcroft /* else it is being freed elsewhere */ 17385ad333ebSAndy Whitcroft list_move(&page->lru, src); 17395ad333ebSAndy Whitcroft continue; 17405ad333ebSAndy Whitcroft 17415ad333ebSAndy Whitcroft default: 17425ad333ebSAndy Whitcroft BUG(); 17435ad333ebSAndy Whitcroft } 17445ad333ebSAndy Whitcroft } 17451da177e4SLinus Torvalds 1746b2e18757SMel Gorman /* 1747b2e18757SMel Gorman * Splice any skipped pages to the start of the LRU list. Note that 1748b2e18757SMel Gorman * this disrupts the LRU order when reclaiming for lower zones but 1749b2e18757SMel Gorman * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX 1750b2e18757SMel Gorman * scanning would soon rescan the same pages to skip and put the 1751b2e18757SMel Gorman * system at risk of premature OOM. 1752b2e18757SMel Gorman */ 17537cc30fcfSMel Gorman if (!list_empty(&pages_skipped)) { 17547cc30fcfSMel Gorman int zid; 17557cc30fcfSMel Gorman 17563db65812SJohannes Weiner list_splice(&pages_skipped, src); 17577cc30fcfSMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 17587cc30fcfSMel Gorman if (!nr_skipped[zid]) 17597cc30fcfSMel Gorman continue; 17607cc30fcfSMel Gorman 17617cc30fcfSMel Gorman __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); 17621265e3a6SMichal Hocko skipped += nr_skipped[zid]; 17637cc30fcfSMel Gorman } 17647cc30fcfSMel Gorman } 1765791b48b6SMinchan Kim *nr_scanned = total_scan; 17661265e3a6SMichal Hocko trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, 1767791b48b6SMinchan Kim total_scan, skipped, nr_taken, mode, lru); 1768b4536f0cSMichal Hocko update_lru_sizes(lruvec, lru, nr_zone_taken); 17691da177e4SLinus Torvalds return nr_taken; 17701da177e4SLinus Torvalds } 17711da177e4SLinus Torvalds 177262695a84SNick Piggin /** 177362695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 177462695a84SNick Piggin * @page: page to isolate from its LRU list 177562695a84SNick Piggin * 177662695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 177762695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 177862695a84SNick Piggin * 177962695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 178062695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 178162695a84SNick Piggin * 178262695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1783894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1784894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1785894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 178662695a84SNick Piggin * 178762695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 178862695a84SNick Piggin * found will be decremented. 178962695a84SNick Piggin * 179062695a84SNick Piggin * Restrictions: 1791a5d09bedSMike Rapoport * 179262695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 179362695a84SNick Piggin * fundamentnal difference from isolate_lru_pages (which is called 179462695a84SNick Piggin * without a stable reference). 179562695a84SNick Piggin * (2) the lru_lock must not be held. 179662695a84SNick Piggin * (3) interrupts must be enabled. 179762695a84SNick Piggin */ 179862695a84SNick Piggin int isolate_lru_page(struct page *page) 179962695a84SNick Piggin { 180062695a84SNick Piggin int ret = -EBUSY; 180162695a84SNick Piggin 1802309381feSSasha Levin VM_BUG_ON_PAGE(!page_count(page), page); 1803cf2a82eeSKirill A. Shutemov WARN_RATELIMIT(PageTail(page), "trying to isolate tail page"); 18040c917313SKonstantin Khlebnikov 180562695a84SNick Piggin if (PageLRU(page)) { 1806f4b7e272SAndrey Ryabinin pg_data_t *pgdat = page_pgdat(page); 1807fa9add64SHugh Dickins struct lruvec *lruvec; 180862695a84SNick Piggin 1809f4b7e272SAndrey Ryabinin spin_lock_irq(&pgdat->lru_lock); 1810f4b7e272SAndrey Ryabinin lruvec = mem_cgroup_page_lruvec(page, pgdat); 18110c917313SKonstantin Khlebnikov if (PageLRU(page)) { 1812894bc310SLee Schermerhorn int lru = page_lru(page); 18130c917313SKonstantin Khlebnikov get_page(page); 181462695a84SNick Piggin ClearPageLRU(page); 1815fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 1816fa9add64SHugh Dickins ret = 0; 181762695a84SNick Piggin } 1818f4b7e272SAndrey Ryabinin spin_unlock_irq(&pgdat->lru_lock); 181962695a84SNick Piggin } 182062695a84SNick Piggin return ret; 182162695a84SNick Piggin } 182262695a84SNick Piggin 18235ad333ebSAndy Whitcroft /* 1824d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 1825d37dd5dcSFengguang Wu * then get resheduled. When there are massive number of tasks doing page 1826d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 1827d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 1828d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 182935cd7815SRik van Riel */ 1830599d0c95SMel Gorman static int too_many_isolated(struct pglist_data *pgdat, int file, 183135cd7815SRik van Riel struct scan_control *sc) 183235cd7815SRik van Riel { 183335cd7815SRik van Riel unsigned long inactive, isolated; 183435cd7815SRik van Riel 183535cd7815SRik van Riel if (current_is_kswapd()) 183635cd7815SRik van Riel return 0; 183735cd7815SRik van Riel 183897c9341fSTejun Heo if (!sane_reclaim(sc)) 183935cd7815SRik van Riel return 0; 184035cd7815SRik van Riel 184135cd7815SRik van Riel if (file) { 1842599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_FILE); 1843599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_FILE); 184435cd7815SRik van Riel } else { 1845599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_ANON); 1846599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_ANON); 184735cd7815SRik van Riel } 184835cd7815SRik van Riel 18493cf23841SFengguang Wu /* 18503cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 18513cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 18523cf23841SFengguang Wu * deadlock. 18533cf23841SFengguang Wu */ 1854d0164adcSMel Gorman if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) 18553cf23841SFengguang Wu inactive >>= 3; 18563cf23841SFengguang Wu 185735cd7815SRik van Riel return isolated > inactive; 185835cd7815SRik van Riel } 185935cd7815SRik van Riel 1860a222f341SKirill Tkhai /* 1861a222f341SKirill Tkhai * This moves pages from @list to corresponding LRU list. 1862a222f341SKirill Tkhai * 1863a222f341SKirill Tkhai * We move them the other way if the page is referenced by one or more 1864a222f341SKirill Tkhai * processes, from rmap. 1865a222f341SKirill Tkhai * 1866a222f341SKirill Tkhai * If the pages are mostly unmapped, the processing is fast and it is 1867a222f341SKirill Tkhai * appropriate to hold zone_lru_lock across the whole operation. But if 1868a222f341SKirill Tkhai * the pages are mapped, the processing is slow (page_referenced()) so we 1869a222f341SKirill Tkhai * should drop zone_lru_lock around each page. It's impossible to balance 1870a222f341SKirill Tkhai * this, so instead we remove the pages from the LRU while processing them. 1871a222f341SKirill Tkhai * It is safe to rely on PG_active against the non-LRU pages in here because 1872a222f341SKirill Tkhai * nobody will play with that bit on a non-LRU page. 1873a222f341SKirill Tkhai * 1874a222f341SKirill Tkhai * The downside is that we have to touch page->_refcount against each page. 1875a222f341SKirill Tkhai * But we had to alter page->flags anyway. 1876a222f341SKirill Tkhai * 1877a222f341SKirill Tkhai * Returns the number of pages moved to the given lruvec. 1878a222f341SKirill Tkhai */ 1879a222f341SKirill Tkhai 1880a222f341SKirill Tkhai static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec, 1881a222f341SKirill Tkhai struct list_head *list) 188266635629SMel Gorman { 1883599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 1884a222f341SKirill Tkhai int nr_pages, nr_moved = 0; 18853f79768fSHugh Dickins LIST_HEAD(pages_to_free); 1886a222f341SKirill Tkhai struct page *page; 1887a222f341SKirill Tkhai enum lru_list lru; 188866635629SMel Gorman 1889a222f341SKirill Tkhai while (!list_empty(list)) { 1890a222f341SKirill Tkhai page = lru_to_page(list); 1891309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 189239b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 1893a222f341SKirill Tkhai list_del(&page->lru); 1894599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 189566635629SMel Gorman putback_lru_page(page); 1896599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 189766635629SMel Gorman continue; 189866635629SMel Gorman } 1899599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 1900fa9add64SHugh Dickins 19017a608572SLinus Torvalds SetPageLRU(page); 190266635629SMel Gorman lru = page_lru(page); 1903a222f341SKirill Tkhai 1904a222f341SKirill Tkhai nr_pages = hpage_nr_pages(page); 1905a222f341SKirill Tkhai update_lru_size(lruvec, lru, page_zonenum(page), nr_pages); 1906a222f341SKirill Tkhai list_move(&page->lru, &lruvec->lists[lru]); 1907fa9add64SHugh Dickins 19082bcf8879SHugh Dickins if (put_page_testzero(page)) { 19092bcf8879SHugh Dickins __ClearPageLRU(page); 19102bcf8879SHugh Dickins __ClearPageActive(page); 1911fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 19122bcf8879SHugh Dickins 19132bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 1914599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 19152bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 1916599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 19172bcf8879SHugh Dickins } else 19182bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 1919a222f341SKirill Tkhai } else { 1920a222f341SKirill Tkhai nr_moved += nr_pages; 192166635629SMel Gorman } 192266635629SMel Gorman } 192366635629SMel Gorman 19243f79768fSHugh Dickins /* 19253f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 19263f79768fSHugh Dickins */ 1927a222f341SKirill Tkhai list_splice(&pages_to_free, list); 1928a222f341SKirill Tkhai 1929a222f341SKirill Tkhai return nr_moved; 193066635629SMel Gorman } 193166635629SMel Gorman 193266635629SMel Gorman /* 1933399ba0b9SNeilBrown * If a kernel thread (such as nfsd for loop-back mounts) services 1934399ba0b9SNeilBrown * a backing device by writing to the page cache it sets PF_LESS_THROTTLE. 1935399ba0b9SNeilBrown * In that case we should only throttle if the backing device it is 1936399ba0b9SNeilBrown * writing to is congested. In other cases it is safe to throttle. 1937399ba0b9SNeilBrown */ 1938399ba0b9SNeilBrown static int current_may_throttle(void) 1939399ba0b9SNeilBrown { 1940399ba0b9SNeilBrown return !(current->flags & PF_LESS_THROTTLE) || 1941399ba0b9SNeilBrown current->backing_dev_info == NULL || 1942399ba0b9SNeilBrown bdi_write_congested(current->backing_dev_info); 1943399ba0b9SNeilBrown } 1944399ba0b9SNeilBrown 1945399ba0b9SNeilBrown /* 1946b2e18757SMel Gorman * shrink_inactive_list() is a helper for shrink_node(). It returns the number 19471742f19fSAndrew Morton * of reclaimed pages 19481da177e4SLinus Torvalds */ 194966635629SMel Gorman static noinline_for_stack unsigned long 19501a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 19519e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 19521da177e4SLinus Torvalds { 19531da177e4SLinus Torvalds LIST_HEAD(page_list); 1954e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 195505ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 1956e247dbceSKOSAKI Motohiro unsigned long nr_taken; 1957060f005fSKirill Tkhai struct reclaim_stat stat; 19583cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 1959f46b7912SKirill Tkhai enum vm_event_item item; 1960599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 19611a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 1962db73ee0dSMichal Hocko bool stalled = false; 196378dc583dSKOSAKI Motohiro 1964599d0c95SMel Gorman while (unlikely(too_many_isolated(pgdat, file, sc))) { 1965db73ee0dSMichal Hocko if (stalled) 1966db73ee0dSMichal Hocko return 0; 1967db73ee0dSMichal Hocko 1968db73ee0dSMichal Hocko /* wait a bit for the reclaimer. */ 1969db73ee0dSMichal Hocko msleep(100); 1970db73ee0dSMichal Hocko stalled = true; 197135cd7815SRik van Riel 197235cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 197335cd7815SRik van Riel if (fatal_signal_pending(current)) 197435cd7815SRik van Riel return SWAP_CLUSTER_MAX; 197535cd7815SRik van Riel } 197635cd7815SRik van Riel 19771da177e4SLinus Torvalds lru_add_drain(); 1978f80c0673SMinchan Kim 1979599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 19801da177e4SLinus Torvalds 19815dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 1982a9e7c39fSKirill Tkhai &nr_scanned, sc, lru); 198395d918fcSKonstantin Khlebnikov 1984599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 19859d5e6a9fSHugh Dickins reclaim_stat->recent_scanned[file] += nr_taken; 198695d918fcSKonstantin Khlebnikov 1987f46b7912SKirill Tkhai item = current_is_kswapd() ? PGSCAN_KSWAPD : PGSCAN_DIRECT; 19882262185cSRoman Gushchin if (global_reclaim(sc)) 1989f46b7912SKirill Tkhai __count_vm_events(item, nr_scanned); 1990f46b7912SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned); 1991599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1992d563c050SHillf Danton 1993d563c050SHillf Danton if (nr_taken == 0) 199466635629SMel Gorman return 0; 1995b35ea17bSKOSAKI Motohiro 1996a128ca71SShaohua Li nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, 0, 19973c710c1aSMichal Hocko &stat, false); 1998c661b078SAndy Whitcroft 1999599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 20003f79768fSHugh Dickins 2001f46b7912SKirill Tkhai item = current_is_kswapd() ? PGSTEAL_KSWAPD : PGSTEAL_DIRECT; 20022262185cSRoman Gushchin if (global_reclaim(sc)) 2003f46b7912SKirill Tkhai __count_vm_events(item, nr_reclaimed); 2004f46b7912SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed); 2005b17f18afSKirill Tkhai reclaim_stat->recent_rotated[0] += stat.nr_activate[0]; 2006b17f18afSKirill Tkhai reclaim_stat->recent_rotated[1] += stat.nr_activate[1]; 2007a74609faSNick Piggin 2008a222f341SKirill Tkhai move_pages_to_lru(lruvec, &page_list); 20093f79768fSHugh Dickins 2010599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 20113f79768fSHugh Dickins 2012599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 20133f79768fSHugh Dickins 2014747db954SJohannes Weiner mem_cgroup_uncharge_list(&page_list); 20152d4894b5SMel Gorman free_unref_page_list(&page_list); 2016e11da5b4SMel Gorman 201792df3a72SMel Gorman /* 20181c610d5fSAndrey Ryabinin * If dirty pages are scanned that are not queued for IO, it 20191c610d5fSAndrey Ryabinin * implies that flushers are not doing their job. This can 20201c610d5fSAndrey Ryabinin * happen when memory pressure pushes dirty pages to the end of 20211c610d5fSAndrey Ryabinin * the LRU before the dirty limits are breached and the dirty 20221c610d5fSAndrey Ryabinin * data has expired. It can also happen when the proportion of 20231c610d5fSAndrey Ryabinin * dirty pages grows not through writes but through memory 20241c610d5fSAndrey Ryabinin * pressure reclaiming all the clean cache. And in some cases, 20251c610d5fSAndrey Ryabinin * the flushers simply cannot keep up with the allocation 20261c610d5fSAndrey Ryabinin * rate. Nudge the flusher threads in case they are asleep. 20271c610d5fSAndrey Ryabinin */ 20281c610d5fSAndrey Ryabinin if (stat.nr_unqueued_dirty == nr_taken) 20291c610d5fSAndrey Ryabinin wakeup_flusher_threads(WB_REASON_VMSCAN); 20301c610d5fSAndrey Ryabinin 2031d108c772SAndrey Ryabinin sc->nr.dirty += stat.nr_dirty; 2032d108c772SAndrey Ryabinin sc->nr.congested += stat.nr_congested; 2033d108c772SAndrey Ryabinin sc->nr.unqueued_dirty += stat.nr_unqueued_dirty; 2034d108c772SAndrey Ryabinin sc->nr.writeback += stat.nr_writeback; 2035d108c772SAndrey Ryabinin sc->nr.immediate += stat.nr_immediate; 2036d108c772SAndrey Ryabinin sc->nr.taken += nr_taken; 2037d108c772SAndrey Ryabinin if (file) 2038d108c772SAndrey Ryabinin sc->nr.file_taken += nr_taken; 20398e950282SMel Gorman 2040599d0c95SMel Gorman trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, 2041d51d1e64SSteven Rostedt nr_scanned, nr_reclaimed, &stat, sc->priority, file); 204205ff5137SAndrew Morton return nr_reclaimed; 20431da177e4SLinus Torvalds } 20441da177e4SLinus Torvalds 2045f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 20461a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 2047f16015fbSJohannes Weiner struct scan_control *sc, 20489e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 20491cfb419bSKAMEZAWA Hiroyuki { 205044c241f1SKOSAKI Motohiro unsigned long nr_taken; 2051f626012dSHugh Dickins unsigned long nr_scanned; 20526fe6b7e3SWu Fengguang unsigned long vm_flags; 20531cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 20548cab4754SWu Fengguang LIST_HEAD(l_active); 2055b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 20561cfb419bSKAMEZAWA Hiroyuki struct page *page; 20571a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 20589d998b4fSMichal Hocko unsigned nr_deactivate, nr_activate; 20599d998b4fSMichal Hocko unsigned nr_rotated = 0; 20603cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 2061599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 20621cfb419bSKAMEZAWA Hiroyuki 20631da177e4SLinus Torvalds lru_add_drain(); 2064f80c0673SMinchan Kim 2065599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 2066925b7673SJohannes Weiner 20675dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 2068a9e7c39fSKirill Tkhai &nr_scanned, sc, lru); 206989b5fae5SJohannes Weiner 2070599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 2071b7c46d15SJohannes Weiner reclaim_stat->recent_scanned[file] += nr_taken; 20721cfb419bSKAMEZAWA Hiroyuki 2073599d0c95SMel Gorman __count_vm_events(PGREFILL, nr_scanned); 20742fa2690cSYafang Shao __count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned); 20759d5e6a9fSHugh Dickins 2076599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 20771da177e4SLinus Torvalds 20781da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 20791da177e4SLinus Torvalds cond_resched(); 20801da177e4SLinus Torvalds page = lru_to_page(&l_hold); 20811da177e4SLinus Torvalds list_del(&page->lru); 20827e9cd484SRik van Riel 208339b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 2084894bc310SLee Schermerhorn putback_lru_page(page); 2085894bc310SLee Schermerhorn continue; 2086894bc310SLee Schermerhorn } 2087894bc310SLee Schermerhorn 2088cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 2089cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 2090cc715d99SMel Gorman if (page_has_private(page)) 2091cc715d99SMel Gorman try_to_release_page(page, 0); 2092cc715d99SMel Gorman unlock_page(page); 2093cc715d99SMel Gorman } 2094cc715d99SMel Gorman } 2095cc715d99SMel Gorman 2096c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 2097c3ac9a8aSJohannes Weiner &vm_flags)) { 20989992af10SRik van Riel nr_rotated += hpage_nr_pages(page); 20998cab4754SWu Fengguang /* 21008cab4754SWu Fengguang * Identify referenced, file-backed active pages and 21018cab4754SWu Fengguang * give them one more trip around the active list. So 21028cab4754SWu Fengguang * that executable code get better chances to stay in 21038cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 21048cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 21058cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 21068cab4754SWu Fengguang * so we ignore them here. 21078cab4754SWu Fengguang */ 210841e20983SWu Fengguang if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { 21098cab4754SWu Fengguang list_add(&page->lru, &l_active); 21108cab4754SWu Fengguang continue; 21118cab4754SWu Fengguang } 21128cab4754SWu Fengguang } 21137e9cd484SRik van Riel 21145205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 21151899ad18SJohannes Weiner SetPageWorkingset(page); 21161da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 21171da177e4SLinus Torvalds } 21181da177e4SLinus Torvalds 2119b555749aSAndrew Morton /* 21208cab4754SWu Fengguang * Move pages back to the lru list. 2121b555749aSAndrew Morton */ 2122599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 21234f98a2feSRik van Riel /* 21248cab4754SWu Fengguang * Count referenced pages from currently used mappings as rotated, 21258cab4754SWu Fengguang * even though only some of them are actually re-activated. This 21268cab4754SWu Fengguang * helps balance scan pressure between file and anonymous pages in 21277c0db9e9SJerome Marchand * get_scan_count. 2128556adecbSRik van Riel */ 2129b7c46d15SJohannes Weiner reclaim_stat->recent_rotated[file] += nr_rotated; 2130556adecbSRik van Riel 2131a222f341SKirill Tkhai nr_activate = move_pages_to_lru(lruvec, &l_active); 2132a222f341SKirill Tkhai nr_deactivate = move_pages_to_lru(lruvec, &l_inactive); 2133f372d89eSKirill Tkhai /* Keep all free pages in l_active list */ 2134f372d89eSKirill Tkhai list_splice(&l_inactive, &l_active); 21359851ac13SKirill Tkhai 21369851ac13SKirill Tkhai __count_vm_events(PGDEACTIVATE, nr_deactivate); 21379851ac13SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate); 21389851ac13SKirill Tkhai 2139599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 2140599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 21412bcf8879SHugh Dickins 2142f372d89eSKirill Tkhai mem_cgroup_uncharge_list(&l_active); 2143f372d89eSKirill Tkhai free_unref_page_list(&l_active); 21449d998b4fSMichal Hocko trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate, 21459d998b4fSMichal Hocko nr_deactivate, nr_rotated, sc->priority, file); 21461da177e4SLinus Torvalds } 21471da177e4SLinus Torvalds 2148*1a4e58ccSMinchan Kim unsigned long reclaim_pages(struct list_head *page_list) 2149*1a4e58ccSMinchan Kim { 2150*1a4e58ccSMinchan Kim int nid = -1; 2151*1a4e58ccSMinchan Kim unsigned long nr_reclaimed = 0; 2152*1a4e58ccSMinchan Kim LIST_HEAD(node_page_list); 2153*1a4e58ccSMinchan Kim struct reclaim_stat dummy_stat; 2154*1a4e58ccSMinchan Kim struct page *page; 2155*1a4e58ccSMinchan Kim struct scan_control sc = { 2156*1a4e58ccSMinchan Kim .gfp_mask = GFP_KERNEL, 2157*1a4e58ccSMinchan Kim .priority = DEF_PRIORITY, 2158*1a4e58ccSMinchan Kim .may_writepage = 1, 2159*1a4e58ccSMinchan Kim .may_unmap = 1, 2160*1a4e58ccSMinchan Kim .may_swap = 1, 2161*1a4e58ccSMinchan Kim }; 2162*1a4e58ccSMinchan Kim 2163*1a4e58ccSMinchan Kim while (!list_empty(page_list)) { 2164*1a4e58ccSMinchan Kim page = lru_to_page(page_list); 2165*1a4e58ccSMinchan Kim if (nid == -1) { 2166*1a4e58ccSMinchan Kim nid = page_to_nid(page); 2167*1a4e58ccSMinchan Kim INIT_LIST_HEAD(&node_page_list); 2168*1a4e58ccSMinchan Kim } 2169*1a4e58ccSMinchan Kim 2170*1a4e58ccSMinchan Kim if (nid == page_to_nid(page)) { 2171*1a4e58ccSMinchan Kim ClearPageActive(page); 2172*1a4e58ccSMinchan Kim list_move(&page->lru, &node_page_list); 2173*1a4e58ccSMinchan Kim continue; 2174*1a4e58ccSMinchan Kim } 2175*1a4e58ccSMinchan Kim 2176*1a4e58ccSMinchan Kim nr_reclaimed += shrink_page_list(&node_page_list, 2177*1a4e58ccSMinchan Kim NODE_DATA(nid), 2178*1a4e58ccSMinchan Kim &sc, 0, 2179*1a4e58ccSMinchan Kim &dummy_stat, false); 2180*1a4e58ccSMinchan Kim while (!list_empty(&node_page_list)) { 2181*1a4e58ccSMinchan Kim page = lru_to_page(&node_page_list); 2182*1a4e58ccSMinchan Kim list_del(&page->lru); 2183*1a4e58ccSMinchan Kim putback_lru_page(page); 2184*1a4e58ccSMinchan Kim } 2185*1a4e58ccSMinchan Kim 2186*1a4e58ccSMinchan Kim nid = -1; 2187*1a4e58ccSMinchan Kim } 2188*1a4e58ccSMinchan Kim 2189*1a4e58ccSMinchan Kim if (!list_empty(&node_page_list)) { 2190*1a4e58ccSMinchan Kim nr_reclaimed += shrink_page_list(&node_page_list, 2191*1a4e58ccSMinchan Kim NODE_DATA(nid), 2192*1a4e58ccSMinchan Kim &sc, 0, 2193*1a4e58ccSMinchan Kim &dummy_stat, false); 2194*1a4e58ccSMinchan Kim while (!list_empty(&node_page_list)) { 2195*1a4e58ccSMinchan Kim page = lru_to_page(&node_page_list); 2196*1a4e58ccSMinchan Kim list_del(&page->lru); 2197*1a4e58ccSMinchan Kim putback_lru_page(page); 2198*1a4e58ccSMinchan Kim } 2199*1a4e58ccSMinchan Kim } 2200*1a4e58ccSMinchan Kim 2201*1a4e58ccSMinchan Kim return nr_reclaimed; 2202*1a4e58ccSMinchan Kim } 2203*1a4e58ccSMinchan Kim 220459dc76b0SRik van Riel /* 220559dc76b0SRik van Riel * The inactive anon list should be small enough that the VM never has 220659dc76b0SRik van Riel * to do too much work. 220714797e23SKOSAKI Motohiro * 220859dc76b0SRik van Riel * The inactive file list should be small enough to leave most memory 220959dc76b0SRik van Riel * to the established workingset on the scan-resistant active list, 221059dc76b0SRik van Riel * but large enough to avoid thrashing the aggregate readahead window. 221159dc76b0SRik van Riel * 221259dc76b0SRik van Riel * Both inactive lists should also be large enough that each inactive 221359dc76b0SRik van Riel * page has a chance to be referenced again before it is reclaimed. 221459dc76b0SRik van Riel * 22152a2e4885SJohannes Weiner * If that fails and refaulting is observed, the inactive list grows. 22162a2e4885SJohannes Weiner * 221759dc76b0SRik van Riel * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages 22183a50d14dSAndrey Ryabinin * on this LRU, maintained by the pageout code. An inactive_ratio 221959dc76b0SRik van Riel * of 3 means 3:1 or 25% of the pages are kept on the inactive list. 222059dc76b0SRik van Riel * 222159dc76b0SRik van Riel * total target max 222259dc76b0SRik van Riel * memory ratio inactive 222359dc76b0SRik van Riel * ------------------------------------- 222459dc76b0SRik van Riel * 10MB 1 5MB 222559dc76b0SRik van Riel * 100MB 1 50MB 222659dc76b0SRik van Riel * 1GB 3 250MB 222759dc76b0SRik van Riel * 10GB 10 0.9GB 222859dc76b0SRik van Riel * 100GB 31 3GB 222959dc76b0SRik van Riel * 1TB 101 10GB 223059dc76b0SRik van Riel * 10TB 320 32GB 223114797e23SKOSAKI Motohiro */ 2232f8d1a311SMel Gorman static bool inactive_list_is_low(struct lruvec *lruvec, bool file, 22332c012a4aSKuo-Hsin Yang struct scan_control *sc, bool trace) 223414797e23SKOSAKI Motohiro { 2235fd538803SMichal Hocko enum lru_list active_lru = file * LRU_FILE + LRU_ACTIVE; 22362a2e4885SJohannes Weiner struct pglist_data *pgdat = lruvec_pgdat(lruvec); 22372a2e4885SJohannes Weiner enum lru_list inactive_lru = file * LRU_FILE; 22382a2e4885SJohannes Weiner unsigned long inactive, active; 22392a2e4885SJohannes Weiner unsigned long inactive_ratio; 22402a2e4885SJohannes Weiner unsigned long refaults; 224159dc76b0SRik van Riel unsigned long gb; 224259dc76b0SRik van Riel 224374e3f3c3SMinchan Kim /* 224474e3f3c3SMinchan Kim * If we don't have swap space, anonymous page deactivation 224574e3f3c3SMinchan Kim * is pointless. 224674e3f3c3SMinchan Kim */ 224759dc76b0SRik van Riel if (!file && !total_swap_pages) 224842e2e457SYaowei Bai return false; 224974e3f3c3SMinchan Kim 2250fd538803SMichal Hocko inactive = lruvec_lru_size(lruvec, inactive_lru, sc->reclaim_idx); 2251fd538803SMichal Hocko active = lruvec_lru_size(lruvec, active_lru, sc->reclaim_idx); 2252f8d1a311SMel Gorman 22532a2e4885SJohannes Weiner /* 22542a2e4885SJohannes Weiner * When refaults are being observed, it means a new workingset 22552a2e4885SJohannes Weiner * is being established. Disable active list protection to get 22562a2e4885SJohannes Weiner * rid of the stale workingset quickly. 22572a2e4885SJohannes Weiner */ 2258205b20ccSJohannes Weiner refaults = lruvec_page_state_local(lruvec, WORKINGSET_ACTIVATE); 22592c012a4aSKuo-Hsin Yang if (file && lruvec->refaults != refaults) { 22602a2e4885SJohannes Weiner inactive_ratio = 0; 22612a2e4885SJohannes Weiner } else { 226259dc76b0SRik van Riel gb = (inactive + active) >> (30 - PAGE_SHIFT); 226359dc76b0SRik van Riel if (gb) 226459dc76b0SRik van Riel inactive_ratio = int_sqrt(10 * gb); 2265b39415b2SRik van Riel else 226659dc76b0SRik van Riel inactive_ratio = 1; 22672a2e4885SJohannes Weiner } 226859dc76b0SRik van Riel 22692c012a4aSKuo-Hsin Yang if (trace) 22702a2e4885SJohannes Weiner trace_mm_vmscan_inactive_list_is_low(pgdat->node_id, sc->reclaim_idx, 2271fd538803SMichal Hocko lruvec_lru_size(lruvec, inactive_lru, MAX_NR_ZONES), inactive, 2272fd538803SMichal Hocko lruvec_lru_size(lruvec, active_lru, MAX_NR_ZONES), active, 2273fd538803SMichal Hocko inactive_ratio, file); 2274fd538803SMichal Hocko 227559dc76b0SRik van Riel return inactive * inactive_ratio < active; 2276b39415b2SRik van Riel } 2277b39415b2SRik van Riel 22784f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 22793b991208SJohannes Weiner struct lruvec *lruvec, struct scan_control *sc) 2280b69408e8SChristoph Lameter { 2281b39415b2SRik van Riel if (is_active_lru(lru)) { 22823b991208SJohannes Weiner if (inactive_list_is_low(lruvec, is_file_lru(lru), sc, true)) 22831a93be0eSKonstantin Khlebnikov shrink_active_list(nr_to_scan, lruvec, sc, lru); 2284556adecbSRik van Riel return 0; 2285556adecbSRik van Riel } 2286556adecbSRik van Riel 22871a93be0eSKonstantin Khlebnikov return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 2288b69408e8SChristoph Lameter } 2289b69408e8SChristoph Lameter 22909a265114SJohannes Weiner enum scan_balance { 22919a265114SJohannes Weiner SCAN_EQUAL, 22929a265114SJohannes Weiner SCAN_FRACT, 22939a265114SJohannes Weiner SCAN_ANON, 22949a265114SJohannes Weiner SCAN_FILE, 22959a265114SJohannes Weiner }; 22969a265114SJohannes Weiner 22971da177e4SLinus Torvalds /* 22984f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 22994f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 23004f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 23014f98a2feSRik van Riel * onto the active list instead of evict. 23024f98a2feSRik van Riel * 2303be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 2304be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 23054f98a2feSRik van Riel */ 230633377678SVladimir Davydov static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, 23076b4f7799SJohannes Weiner struct scan_control *sc, unsigned long *nr, 23086b4f7799SJohannes Weiner unsigned long *lru_pages) 23094f98a2feSRik van Riel { 231033377678SVladimir Davydov int swappiness = mem_cgroup_swappiness(memcg); 231190126375SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 23129a265114SJohannes Weiner u64 fraction[2]; 23139a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 2314599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 23159a265114SJohannes Weiner unsigned long anon_prio, file_prio; 23169a265114SJohannes Weiner enum scan_balance scan_balance; 23170bf1457fSJohannes Weiner unsigned long anon, file; 23189a265114SJohannes Weiner unsigned long ap, fp; 23199a265114SJohannes Weiner enum lru_list lru; 232076a33fc3SShaohua Li 232176a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 2322d8b38438SVladimir Davydov if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) { 23239a265114SJohannes Weiner scan_balance = SCAN_FILE; 232476a33fc3SShaohua Li goto out; 232576a33fc3SShaohua Li } 23264f98a2feSRik van Riel 232710316b31SJohannes Weiner /* 232810316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 232910316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 233010316b31SJohannes Weiner * disable swapping for individual groups completely when 233110316b31SJohannes Weiner * using the memory controller's swap limit feature would be 233210316b31SJohannes Weiner * too expensive. 233310316b31SJohannes Weiner */ 233402695175SJohannes Weiner if (!global_reclaim(sc) && !swappiness) { 23359a265114SJohannes Weiner scan_balance = SCAN_FILE; 233610316b31SJohannes Weiner goto out; 233710316b31SJohannes Weiner } 233810316b31SJohannes Weiner 233910316b31SJohannes Weiner /* 234010316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 234110316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 234210316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 234310316b31SJohannes Weiner */ 234402695175SJohannes Weiner if (!sc->priority && swappiness) { 23459a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 234610316b31SJohannes Weiner goto out; 234710316b31SJohannes Weiner } 234810316b31SJohannes Weiner 234911d16c25SJohannes Weiner /* 235062376251SJohannes Weiner * Prevent the reclaimer from falling into the cache trap: as 235162376251SJohannes Weiner * cache pages start out inactive, every cache fault will tip 235262376251SJohannes Weiner * the scan balance towards the file LRU. And as the file LRU 235362376251SJohannes Weiner * shrinks, so does the window for rotation from references. 235462376251SJohannes Weiner * This means we have a runaway feedback loop where a tiny 235562376251SJohannes Weiner * thrashing file LRU becomes infinitely more attractive than 235662376251SJohannes Weiner * anon pages. Try to detect this based on file LRU size. 235762376251SJohannes Weiner */ 235862376251SJohannes Weiner if (global_reclaim(sc)) { 2359599d0c95SMel Gorman unsigned long pgdatfile; 2360599d0c95SMel Gorman unsigned long pgdatfree; 2361599d0c95SMel Gorman int z; 2362599d0c95SMel Gorman unsigned long total_high_wmark = 0; 236362376251SJohannes Weiner 2364599d0c95SMel Gorman pgdatfree = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); 2365599d0c95SMel Gorman pgdatfile = node_page_state(pgdat, NR_ACTIVE_FILE) + 2366599d0c95SMel Gorman node_page_state(pgdat, NR_INACTIVE_FILE); 23672ab051e1SJerome Marchand 2368599d0c95SMel Gorman for (z = 0; z < MAX_NR_ZONES; z++) { 2369599d0c95SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 23706aa303deSMel Gorman if (!managed_zone(zone)) 2371599d0c95SMel Gorman continue; 2372599d0c95SMel Gorman 2373599d0c95SMel Gorman total_high_wmark += high_wmark_pages(zone); 2374599d0c95SMel Gorman } 2375599d0c95SMel Gorman 2376599d0c95SMel Gorman if (unlikely(pgdatfile + pgdatfree <= total_high_wmark)) { 237706226226SDavid Rientjes /* 237806226226SDavid Rientjes * Force SCAN_ANON if there are enough inactive 237906226226SDavid Rientjes * anonymous pages on the LRU in eligible zones. 238006226226SDavid Rientjes * Otherwise, the small LRU gets thrashed. 238106226226SDavid Rientjes */ 23823b991208SJohannes Weiner if (!inactive_list_is_low(lruvec, false, sc, false) && 238306226226SDavid Rientjes lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, sc->reclaim_idx) 238406226226SDavid Rientjes >> sc->priority) { 238562376251SJohannes Weiner scan_balance = SCAN_ANON; 238662376251SJohannes Weiner goto out; 238762376251SJohannes Weiner } 238862376251SJohannes Weiner } 238906226226SDavid Rientjes } 239062376251SJohannes Weiner 239162376251SJohannes Weiner /* 2392316bda0eSVladimir Davydov * If there is enough inactive page cache, i.e. if the size of the 2393316bda0eSVladimir Davydov * inactive list is greater than that of the active list *and* the 2394316bda0eSVladimir Davydov * inactive list actually has some pages to scan on this priority, we 2395316bda0eSVladimir Davydov * do not reclaim anything from the anonymous working set right now. 2396316bda0eSVladimir Davydov * Without the second condition we could end up never scanning an 2397316bda0eSVladimir Davydov * lruvec even if it has plenty of old anonymous pages unless the 2398316bda0eSVladimir Davydov * system is under heavy pressure. 2399e9868505SRik van Riel */ 24003b991208SJohannes Weiner if (!inactive_list_is_low(lruvec, true, sc, false) && 240171ab6cfeSMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, sc->reclaim_idx) >> sc->priority) { 24029a265114SJohannes Weiner scan_balance = SCAN_FILE; 2403e9868505SRik van Riel goto out; 24044f98a2feSRik van Riel } 24054f98a2feSRik van Riel 24069a265114SJohannes Weiner scan_balance = SCAN_FRACT; 24079a265114SJohannes Weiner 24084f98a2feSRik van Riel /* 240958c37f6eSKOSAKI Motohiro * With swappiness at 100, anonymous and file have the same priority. 241058c37f6eSKOSAKI Motohiro * This scanning priority is essentially the inverse of IO cost. 241158c37f6eSKOSAKI Motohiro */ 241202695175SJohannes Weiner anon_prio = swappiness; 241375b00af7SHugh Dickins file_prio = 200 - anon_prio; 241458c37f6eSKOSAKI Motohiro 241558c37f6eSKOSAKI Motohiro /* 24164f98a2feSRik van Riel * OK, so we have swap space and a fair amount of page cache 24174f98a2feSRik van Riel * pages. We use the recently rotated / recently scanned 24184f98a2feSRik van Riel * ratios to determine how valuable each cache is. 24194f98a2feSRik van Riel * 24204f98a2feSRik van Riel * Because workloads change over time (and to avoid overflow) 24214f98a2feSRik van Riel * we keep these statistics as a floating average, which ends 24224f98a2feSRik van Riel * up weighing recent references more than old ones. 24234f98a2feSRik van Riel * 24244f98a2feSRik van Riel * anon in [0], file in [1] 24254f98a2feSRik van Riel */ 24262ab051e1SJerome Marchand 2427fd538803SMichal Hocko anon = lruvec_lru_size(lruvec, LRU_ACTIVE_ANON, MAX_NR_ZONES) + 2428fd538803SMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, MAX_NR_ZONES); 2429fd538803SMichal Hocko file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES) + 2430fd538803SMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, MAX_NR_ZONES); 24312ab051e1SJerome Marchand 2432599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 243358c37f6eSKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { 24346e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[0] /= 2; 24356e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[0] /= 2; 24364f98a2feSRik van Riel } 24374f98a2feSRik van Riel 24386e901571SKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { 24396e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[1] /= 2; 24406e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[1] /= 2; 24414f98a2feSRik van Riel } 24424f98a2feSRik van Riel 24434f98a2feSRik van Riel /* 244400d8089cSRik van Riel * The amount of pressure on anon vs file pages is inversely 244500d8089cSRik van Riel * proportional to the fraction of recently scanned pages on 244600d8089cSRik van Riel * each list that were recently referenced and in active use. 24474f98a2feSRik van Riel */ 2448fe35004fSSatoru Moriya ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1); 24496e901571SKOSAKI Motohiro ap /= reclaim_stat->recent_rotated[0] + 1; 24504f98a2feSRik van Riel 2451fe35004fSSatoru Moriya fp = file_prio * (reclaim_stat->recent_scanned[1] + 1); 24526e901571SKOSAKI Motohiro fp /= reclaim_stat->recent_rotated[1] + 1; 2453599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 24544f98a2feSRik van Riel 245576a33fc3SShaohua Li fraction[0] = ap; 245676a33fc3SShaohua Li fraction[1] = fp; 245776a33fc3SShaohua Li denominator = ap + fp + 1; 245876a33fc3SShaohua Li out: 24596b4f7799SJohannes Weiner *lru_pages = 0; 24604111304dSHugh Dickins for_each_evictable_lru(lru) { 24614111304dSHugh Dickins int file = is_file_lru(lru); 2462d778df51SJohannes Weiner unsigned long size; 246376a33fc3SShaohua Li unsigned long scan; 246476a33fc3SShaohua Li 246571ab6cfeSMichal Hocko size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); 2466d778df51SJohannes Weiner scan = size >> sc->priority; 2467688035f7SJohannes Weiner /* 2468688035f7SJohannes Weiner * If the cgroup's already been deleted, make sure to 2469688035f7SJohannes Weiner * scrape out the remaining cache. 2470688035f7SJohannes Weiner */ 2471688035f7SJohannes Weiner if (!scan && !mem_cgroup_online(memcg)) 2472d778df51SJohannes Weiner scan = min(size, SWAP_CLUSTER_MAX); 24739a265114SJohannes Weiner 24749a265114SJohannes Weiner switch (scan_balance) { 24759a265114SJohannes Weiner case SCAN_EQUAL: 24769a265114SJohannes Weiner /* Scan lists relative to size */ 24779a265114SJohannes Weiner break; 24789a265114SJohannes Weiner case SCAN_FRACT: 24799a265114SJohannes Weiner /* 24809a265114SJohannes Weiner * Scan types proportional to swappiness and 24819a265114SJohannes Weiner * their relative recent reclaim efficiency. 248268600f62SRoman Gushchin * Make sure we don't miss the last page 248368600f62SRoman Gushchin * because of a round-off error. 24849a265114SJohannes Weiner */ 248568600f62SRoman Gushchin scan = DIV64_U64_ROUND_UP(scan * fraction[file], 24866f04f48dSSuleiman Souhlal denominator); 24879a265114SJohannes Weiner break; 24889a265114SJohannes Weiner case SCAN_FILE: 24899a265114SJohannes Weiner case SCAN_ANON: 24909a265114SJohannes Weiner /* Scan one type exclusively */ 24916b4f7799SJohannes Weiner if ((scan_balance == SCAN_FILE) != file) { 24926b4f7799SJohannes Weiner size = 0; 24939a265114SJohannes Weiner scan = 0; 24946b4f7799SJohannes Weiner } 24959a265114SJohannes Weiner break; 24969a265114SJohannes Weiner default: 24979a265114SJohannes Weiner /* Look ma, no brain */ 24989a265114SJohannes Weiner BUG(); 24999a265114SJohannes Weiner } 25006b4f7799SJohannes Weiner 25016b4f7799SJohannes Weiner *lru_pages += size; 25024111304dSHugh Dickins nr[lru] = scan; 250376a33fc3SShaohua Li } 25046e08a369SWu Fengguang } 25054f98a2feSRik van Riel 25069b4f98cdSJohannes Weiner /* 2507a9dd0a83SMel Gorman * This is a basic per-node page freer. Used by both kswapd and direct reclaim. 25089b4f98cdSJohannes Weiner */ 2509a9dd0a83SMel Gorman static void shrink_node_memcg(struct pglist_data *pgdat, struct mem_cgroup *memcg, 25106b4f7799SJohannes Weiner struct scan_control *sc, unsigned long *lru_pages) 25119b4f98cdSJohannes Weiner { 2512ef8f2327SMel Gorman struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg); 25139b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 2514e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 25159b4f98cdSJohannes Weiner unsigned long nr_to_scan; 25169b4f98cdSJohannes Weiner enum lru_list lru; 25179b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 25189b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 25199b4f98cdSJohannes Weiner struct blk_plug plug; 25201a501907SMel Gorman bool scan_adjusted; 25219b4f98cdSJohannes Weiner 252233377678SVladimir Davydov get_scan_count(lruvec, memcg, sc, nr, lru_pages); 25239b4f98cdSJohannes Weiner 2524e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 2525e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 2526e82e0561SMel Gorman 25271a501907SMel Gorman /* 25281a501907SMel Gorman * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal 25291a501907SMel Gorman * event that can occur when there is little memory pressure e.g. 25301a501907SMel Gorman * multiple streaming readers/writers. Hence, we do not abort scanning 25311a501907SMel Gorman * when the requested number of pages are reclaimed when scanning at 25321a501907SMel Gorman * DEF_PRIORITY on the assumption that the fact we are direct 25331a501907SMel Gorman * reclaiming implies that kswapd is not keeping up and it is best to 25341a501907SMel Gorman * do a batch of work at once. For memcg reclaim one check is made to 25351a501907SMel Gorman * abort proportional reclaim if either the file or anon lru has already 25361a501907SMel Gorman * dropped to zero at the first pass. 25371a501907SMel Gorman */ 25381a501907SMel Gorman scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() && 25391a501907SMel Gorman sc->priority == DEF_PRIORITY); 25401a501907SMel Gorman 25419b4f98cdSJohannes Weiner blk_start_plug(&plug); 25429b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 25439b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 2544e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 2545e82e0561SMel Gorman unsigned long nr_scanned; 2546e82e0561SMel Gorman 25479b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 25489b4f98cdSJohannes Weiner if (nr[lru]) { 25499b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 25509b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 25519b4f98cdSJohannes Weiner 25529b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 25533b991208SJohannes Weiner lruvec, sc); 25549b4f98cdSJohannes Weiner } 25559b4f98cdSJohannes Weiner } 2556e82e0561SMel Gorman 2557bd041733SMichal Hocko cond_resched(); 2558bd041733SMichal Hocko 2559e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 2560e82e0561SMel Gorman continue; 2561e82e0561SMel Gorman 25629b4f98cdSJohannes Weiner /* 2563e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 25641a501907SMel Gorman * requested. Ensure that the anon and file LRUs are scanned 2565e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 2566e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 2567e82e0561SMel Gorman * proportional to the original scan target. 2568e82e0561SMel Gorman */ 2569e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 2570e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 2571e82e0561SMel Gorman 25721a501907SMel Gorman /* 25731a501907SMel Gorman * It's just vindictive to attack the larger once the smaller 25741a501907SMel Gorman * has gone to zero. And given the way we stop scanning the 25751a501907SMel Gorman * smaller below, this makes sure that we only make one nudge 25761a501907SMel Gorman * towards proportionality once we've got nr_to_reclaim. 25771a501907SMel Gorman */ 25781a501907SMel Gorman if (!nr_file || !nr_anon) 25791a501907SMel Gorman break; 25801a501907SMel Gorman 2581e82e0561SMel Gorman if (nr_file > nr_anon) { 2582e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 2583e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 2584e82e0561SMel Gorman lru = LRU_BASE; 2585e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 2586e82e0561SMel Gorman } else { 2587e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 2588e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 2589e82e0561SMel Gorman lru = LRU_FILE; 2590e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 2591e82e0561SMel Gorman } 2592e82e0561SMel Gorman 2593e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 2594e82e0561SMel Gorman nr[lru] = 0; 2595e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 2596e82e0561SMel Gorman 2597e82e0561SMel Gorman /* 2598e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 2599e82e0561SMel Gorman * scan target and the percentage scanning already complete 2600e82e0561SMel Gorman */ 2601e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 2602e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2603e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2604e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2605e82e0561SMel Gorman 2606e82e0561SMel Gorman lru += LRU_ACTIVE; 2607e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2608e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2609e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2610e82e0561SMel Gorman 2611e82e0561SMel Gorman scan_adjusted = true; 26129b4f98cdSJohannes Weiner } 26139b4f98cdSJohannes Weiner blk_finish_plug(&plug); 26149b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 26159b4f98cdSJohannes Weiner 26169b4f98cdSJohannes Weiner /* 26179b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 26189b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 26199b4f98cdSJohannes Weiner */ 26203b991208SJohannes Weiner if (inactive_list_is_low(lruvec, false, sc, true)) 26219b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 26229b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 26239b4f98cdSJohannes Weiner } 26249b4f98cdSJohannes Weiner 262523b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 26269e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 262723b9da55SMel Gorman { 2628d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 262923b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 26309e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 263123b9da55SMel Gorman return true; 263223b9da55SMel Gorman 263323b9da55SMel Gorman return false; 263423b9da55SMel Gorman } 263523b9da55SMel Gorman 26364f98a2feSRik van Riel /* 263723b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 263823b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 263923b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 264023b9da55SMel Gorman * calls try_to_compact_zone() that it will have enough free pages to succeed. 264123b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 26423e7d3449SMel Gorman */ 2643a9dd0a83SMel Gorman static inline bool should_continue_reclaim(struct pglist_data *pgdat, 26443e7d3449SMel Gorman unsigned long nr_reclaimed, 26453e7d3449SMel Gorman struct scan_control *sc) 26463e7d3449SMel Gorman { 26473e7d3449SMel Gorman unsigned long pages_for_compaction; 26483e7d3449SMel Gorman unsigned long inactive_lru_pages; 2649a9dd0a83SMel Gorman int z; 26503e7d3449SMel Gorman 26513e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 26529e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 26533e7d3449SMel Gorman return false; 26543e7d3449SMel Gorman 26553e7d3449SMel Gorman /* 26565ee04716SVlastimil Babka * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX 26575ee04716SVlastimil Babka * number of pages that were scanned. This will return to the caller 26585ee04716SVlastimil Babka * with the risk reclaim/compaction and the resulting allocation attempt 26595ee04716SVlastimil Babka * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL 26605ee04716SVlastimil Babka * allocations through requiring that the full LRU list has been scanned 26615ee04716SVlastimil Babka * first, by assuming that zero delta of sc->nr_scanned means full LRU 26625ee04716SVlastimil Babka * scan, but that approximation was wrong, and there were corner cases 26635ee04716SVlastimil Babka * where always a non-zero amount of pages were scanned. 26642876592fSMel Gorman */ 26652876592fSMel Gorman if (!nr_reclaimed) 26662876592fSMel Gorman return false; 26673e7d3449SMel Gorman 26683e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 2669a9dd0a83SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 2670a9dd0a83SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 26716aa303deSMel Gorman if (!managed_zone(zone)) 2672a9dd0a83SMel Gorman continue; 2673a9dd0a83SMel Gorman 2674a9dd0a83SMel Gorman switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) { 2675cf378319SVlastimil Babka case COMPACT_SUCCESS: 26763e7d3449SMel Gorman case COMPACT_CONTINUE: 26773e7d3449SMel Gorman return false; 26783e7d3449SMel Gorman default: 2679a9dd0a83SMel Gorman /* check next zone */ 2680a9dd0a83SMel Gorman ; 26813e7d3449SMel Gorman } 26823e7d3449SMel Gorman } 26831c6c1597SHillf Danton 26841c6c1597SHillf Danton /* 26851c6c1597SHillf Danton * If we have not reclaimed enough pages for compaction and the 26861c6c1597SHillf Danton * inactive lists are large enough, continue reclaiming 26871c6c1597SHillf Danton */ 26881c6c1597SHillf Danton pages_for_compaction = compact_gap(sc->order); 26891c6c1597SHillf Danton inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); 26901c6c1597SHillf Danton if (get_nr_swap_pages() > 0) 26911c6c1597SHillf Danton inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); 26921c6c1597SHillf Danton 26935ee04716SVlastimil Babka return inactive_lru_pages > pages_for_compaction; 2694a9dd0a83SMel Gorman } 26953e7d3449SMel Gorman 2696e3c1ac58SAndrey Ryabinin static bool pgdat_memcg_congested(pg_data_t *pgdat, struct mem_cgroup *memcg) 2697e3c1ac58SAndrey Ryabinin { 2698e3c1ac58SAndrey Ryabinin return test_bit(PGDAT_CONGESTED, &pgdat->flags) || 2699e3c1ac58SAndrey Ryabinin (memcg && memcg_congested(pgdat, memcg)); 2700e3c1ac58SAndrey Ryabinin } 2701e3c1ac58SAndrey Ryabinin 2702970a39a3SMel Gorman static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc) 2703f16015fbSJohannes Weiner { 2704cb731d6cSVladimir Davydov struct reclaim_state *reclaim_state = current->reclaim_state; 27059b4f98cdSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 27062344d7e4SJohannes Weiner bool reclaimable = false; 27079b4f98cdSJohannes Weiner 27089b4f98cdSJohannes Weiner do { 27095660048cSJohannes Weiner struct mem_cgroup *root = sc->target_mem_cgroup; 2710a9dd0a83SMel Gorman unsigned long node_lru_pages = 0; 2711694fbc0fSAndrew Morton struct mem_cgroup *memcg; 27125660048cSJohannes Weiner 2713d108c772SAndrey Ryabinin memset(&sc->nr, 0, sizeof(sc->nr)); 2714d108c772SAndrey Ryabinin 27159b4f98cdSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 27169b4f98cdSJohannes Weiner nr_scanned = sc->nr_scanned; 27179b4f98cdSJohannes Weiner 27181ba6fc9aSJohannes Weiner memcg = mem_cgroup_iter(root, NULL, NULL); 2719694fbc0fSAndrew Morton do { 27206b4f7799SJohannes Weiner unsigned long lru_pages; 27218e8ae645SJohannes Weiner unsigned long reclaimed; 2722cb731d6cSVladimir Davydov unsigned long scanned; 27239b4f98cdSJohannes Weiner 2724bf8d5d52SRoman Gushchin switch (mem_cgroup_protected(root, memcg)) { 2725bf8d5d52SRoman Gushchin case MEMCG_PROT_MIN: 2726bf8d5d52SRoman Gushchin /* 2727bf8d5d52SRoman Gushchin * Hard protection. 2728bf8d5d52SRoman Gushchin * If there is no reclaimable memory, OOM. 2729bf8d5d52SRoman Gushchin */ 2730bf8d5d52SRoman Gushchin continue; 2731bf8d5d52SRoman Gushchin case MEMCG_PROT_LOW: 2732bf8d5d52SRoman Gushchin /* 2733bf8d5d52SRoman Gushchin * Soft protection. 2734bf8d5d52SRoman Gushchin * Respect the protection only as long as 2735bf8d5d52SRoman Gushchin * there is an unprotected supply 2736bf8d5d52SRoman Gushchin * of reclaimable memory from other cgroups. 2737bf8d5d52SRoman Gushchin */ 2738d6622f63SYisheng Xie if (!sc->memcg_low_reclaim) { 2739d6622f63SYisheng Xie sc->memcg_low_skipped = 1; 2740241994edSJohannes Weiner continue; 2741d6622f63SYisheng Xie } 2742e27be240SJohannes Weiner memcg_memory_event(memcg, MEMCG_LOW); 2743bf8d5d52SRoman Gushchin break; 2744bf8d5d52SRoman Gushchin case MEMCG_PROT_NONE: 2745bf8d5d52SRoman Gushchin break; 2746241994edSJohannes Weiner } 2747241994edSJohannes Weiner 27488e8ae645SJohannes Weiner reclaimed = sc->nr_reclaimed; 2749cb731d6cSVladimir Davydov scanned = sc->nr_scanned; 2750a9dd0a83SMel Gorman shrink_node_memcg(pgdat, memcg, sc, &lru_pages); 2751a9dd0a83SMel Gorman node_lru_pages += lru_pages; 2752f9be23d6SKonstantin Khlebnikov 275328360f39SMel Gorman shrink_slab(sc->gfp_mask, pgdat->node_id, memcg, 275428360f39SMel Gorman sc->priority); 2755cb731d6cSVladimir Davydov 27568e8ae645SJohannes Weiner /* Record the group's reclaim efficiency */ 27578e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, memcg, false, 27588e8ae645SJohannes Weiner sc->nr_scanned - scanned, 27598e8ae645SJohannes Weiner sc->nr_reclaimed - reclaimed); 27608e8ae645SJohannes Weiner 27611ba6fc9aSJohannes Weiner } while ((memcg = mem_cgroup_iter(root, memcg, NULL))); 276270ddf637SAnton Vorontsov 27636b4f7799SJohannes Weiner if (reclaim_state) { 2764cb731d6cSVladimir Davydov sc->nr_reclaimed += reclaim_state->reclaimed_slab; 27656b4f7799SJohannes Weiner reclaim_state->reclaimed_slab = 0; 27666b4f7799SJohannes Weiner } 27676b4f7799SJohannes Weiner 27688e8ae645SJohannes Weiner /* Record the subtree's reclaim efficiency */ 27698e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, 277070ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 277170ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 277270ddf637SAnton Vorontsov 27732344d7e4SJohannes Weiner if (sc->nr_reclaimed - nr_reclaimed) 27742344d7e4SJohannes Weiner reclaimable = true; 27752344d7e4SJohannes Weiner 2776e3c1ac58SAndrey Ryabinin if (current_is_kswapd()) { 2777d108c772SAndrey Ryabinin /* 2778e3c1ac58SAndrey Ryabinin * If reclaim is isolating dirty pages under writeback, 2779e3c1ac58SAndrey Ryabinin * it implies that the long-lived page allocation rate 2780e3c1ac58SAndrey Ryabinin * is exceeding the page laundering rate. Either the 2781e3c1ac58SAndrey Ryabinin * global limits are not being effective at throttling 2782e3c1ac58SAndrey Ryabinin * processes due to the page distribution throughout 2783e3c1ac58SAndrey Ryabinin * zones or there is heavy usage of a slow backing 2784e3c1ac58SAndrey Ryabinin * device. The only option is to throttle from reclaim 2785e3c1ac58SAndrey Ryabinin * context which is not ideal as there is no guarantee 2786d108c772SAndrey Ryabinin * the dirtying process is throttled in the same way 2787d108c772SAndrey Ryabinin * balance_dirty_pages() manages. 2788d108c772SAndrey Ryabinin * 2789e3c1ac58SAndrey Ryabinin * Once a node is flagged PGDAT_WRITEBACK, kswapd will 2790e3c1ac58SAndrey Ryabinin * count the number of pages under pages flagged for 2791e3c1ac58SAndrey Ryabinin * immediate reclaim and stall if any are encountered 2792e3c1ac58SAndrey Ryabinin * in the nr_immediate check below. 2793d108c772SAndrey Ryabinin */ 2794d108c772SAndrey Ryabinin if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken) 2795d108c772SAndrey Ryabinin set_bit(PGDAT_WRITEBACK, &pgdat->flags); 2796d108c772SAndrey Ryabinin 2797d108c772SAndrey Ryabinin /* 2798d108c772SAndrey Ryabinin * Tag a node as congested if all the dirty pages 2799d108c772SAndrey Ryabinin * scanned were backed by a congested BDI and 2800d108c772SAndrey Ryabinin * wait_iff_congested will stall. 2801d108c772SAndrey Ryabinin */ 2802d108c772SAndrey Ryabinin if (sc->nr.dirty && sc->nr.dirty == sc->nr.congested) 2803d108c772SAndrey Ryabinin set_bit(PGDAT_CONGESTED, &pgdat->flags); 2804d108c772SAndrey Ryabinin 2805d108c772SAndrey Ryabinin /* Allow kswapd to start writing pages during reclaim.*/ 2806d108c772SAndrey Ryabinin if (sc->nr.unqueued_dirty == sc->nr.file_taken) 2807d108c772SAndrey Ryabinin set_bit(PGDAT_DIRTY, &pgdat->flags); 2808d108c772SAndrey Ryabinin 2809d108c772SAndrey Ryabinin /* 2810d108c772SAndrey Ryabinin * If kswapd scans pages marked marked for immediate 2811d108c772SAndrey Ryabinin * reclaim and under writeback (nr_immediate), it 2812d108c772SAndrey Ryabinin * implies that pages are cycling through the LRU 2813d108c772SAndrey Ryabinin * faster than they are written so also forcibly stall. 2814d108c772SAndrey Ryabinin */ 2815d108c772SAndrey Ryabinin if (sc->nr.immediate) 2816d108c772SAndrey Ryabinin congestion_wait(BLK_RW_ASYNC, HZ/10); 2817d108c772SAndrey Ryabinin } 2818d108c772SAndrey Ryabinin 2819d108c772SAndrey Ryabinin /* 2820e3c1ac58SAndrey Ryabinin * Legacy memcg will stall in page writeback so avoid forcibly 2821e3c1ac58SAndrey Ryabinin * stalling in wait_iff_congested(). 2822e3c1ac58SAndrey Ryabinin */ 2823e3c1ac58SAndrey Ryabinin if (!global_reclaim(sc) && sane_reclaim(sc) && 2824e3c1ac58SAndrey Ryabinin sc->nr.dirty && sc->nr.dirty == sc->nr.congested) 2825e3c1ac58SAndrey Ryabinin set_memcg_congestion(pgdat, root, true); 2826e3c1ac58SAndrey Ryabinin 2827e3c1ac58SAndrey Ryabinin /* 2828d108c772SAndrey Ryabinin * Stall direct reclaim for IO completions if underlying BDIs 2829d108c772SAndrey Ryabinin * and node is congested. Allow kswapd to continue until it 2830d108c772SAndrey Ryabinin * starts encountering unqueued dirty pages or cycling through 2831d108c772SAndrey Ryabinin * the LRU too quickly. 2832d108c772SAndrey Ryabinin */ 2833d108c772SAndrey Ryabinin if (!sc->hibernation_mode && !current_is_kswapd() && 2834e3c1ac58SAndrey Ryabinin current_may_throttle() && pgdat_memcg_congested(pgdat, root)) 2835e3c1ac58SAndrey Ryabinin wait_iff_congested(BLK_RW_ASYNC, HZ/10); 2836d108c772SAndrey Ryabinin 2837a9dd0a83SMel Gorman } while (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, 28385ee04716SVlastimil Babka sc)); 28392344d7e4SJohannes Weiner 2840c73322d0SJohannes Weiner /* 2841c73322d0SJohannes Weiner * Kswapd gives up on balancing particular nodes after too 2842c73322d0SJohannes Weiner * many failures to reclaim anything from them and goes to 2843c73322d0SJohannes Weiner * sleep. On reclaim progress, reset the failure counter. A 2844c73322d0SJohannes Weiner * successful direct reclaim run will revive a dormant kswapd. 2845c73322d0SJohannes Weiner */ 2846c73322d0SJohannes Weiner if (reclaimable) 2847c73322d0SJohannes Weiner pgdat->kswapd_failures = 0; 2848c73322d0SJohannes Weiner 28492344d7e4SJohannes Weiner return reclaimable; 2850f16015fbSJohannes Weiner } 2851f16015fbSJohannes Weiner 285253853e2dSVlastimil Babka /* 2853fdd4c614SVlastimil Babka * Returns true if compaction should go ahead for a costly-order request, or 2854fdd4c614SVlastimil Babka * the allocation would already succeed without compaction. Return false if we 2855fdd4c614SVlastimil Babka * should reclaim first. 285653853e2dSVlastimil Babka */ 28574f588331SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 2858fe4b1b24SMel Gorman { 285931483b6aSMel Gorman unsigned long watermark; 2860fdd4c614SVlastimil Babka enum compact_result suitable; 2861fe4b1b24SMel Gorman 2862fdd4c614SVlastimil Babka suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx); 2863fdd4c614SVlastimil Babka if (suitable == COMPACT_SUCCESS) 2864fdd4c614SVlastimil Babka /* Allocation should succeed already. Don't reclaim. */ 2865fdd4c614SVlastimil Babka return true; 2866fdd4c614SVlastimil Babka if (suitable == COMPACT_SKIPPED) 2867fdd4c614SVlastimil Babka /* Compaction cannot yet proceed. Do reclaim. */ 2868fe4b1b24SMel Gorman return false; 2869fe4b1b24SMel Gorman 2870fdd4c614SVlastimil Babka /* 2871fdd4c614SVlastimil Babka * Compaction is already possible, but it takes time to run and there 2872fdd4c614SVlastimil Babka * are potentially other callers using the pages just freed. So proceed 2873fdd4c614SVlastimil Babka * with reclaim to make a buffer of free pages available to give 2874fdd4c614SVlastimil Babka * compaction a reasonable chance of completing and allocating the page. 2875fdd4c614SVlastimil Babka * Note that we won't actually reclaim the whole buffer in one attempt 2876fdd4c614SVlastimil Babka * as the target watermark in should_continue_reclaim() is lower. But if 2877fdd4c614SVlastimil Babka * we are already above the high+gap watermark, don't reclaim at all. 2878fdd4c614SVlastimil Babka */ 2879fdd4c614SVlastimil Babka watermark = high_wmark_pages(zone) + compact_gap(sc->order); 2880fdd4c614SVlastimil Babka 2881fdd4c614SVlastimil Babka return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); 2882fe4b1b24SMel Gorman } 2883fe4b1b24SMel Gorman 28841da177e4SLinus Torvalds /* 28851da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 28861da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 28871da177e4SLinus Torvalds * request. 28881da177e4SLinus Torvalds * 28891da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 28901da177e4SLinus Torvalds * scan then give up on it. 28911da177e4SLinus Torvalds */ 28920a0337e0SMichal Hocko static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 28931da177e4SLinus Torvalds { 2894dd1a239fSMel Gorman struct zoneref *z; 289554a6eb5cSMel Gorman struct zone *zone; 28960608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 28970608f43dSAndrew Morton unsigned long nr_soft_scanned; 2898619d0d76SWeijie Yang gfp_t orig_mask; 289979dafcdcSMel Gorman pg_data_t *last_pgdat = NULL; 29001cfb419bSKAMEZAWA Hiroyuki 2901cc715d99SMel Gorman /* 2902cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 2903cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 2904cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 2905cc715d99SMel Gorman */ 2906619d0d76SWeijie Yang orig_mask = sc->gfp_mask; 2907b2e18757SMel Gorman if (buffer_heads_over_limit) { 2908cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 29094f588331SMel Gorman sc->reclaim_idx = gfp_zone(sc->gfp_mask); 2910b2e18757SMel Gorman } 2911cc715d99SMel Gorman 2912d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 2913b2e18757SMel Gorman sc->reclaim_idx, sc->nodemask) { 2914b2e18757SMel Gorman /* 29151cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 29161cfb419bSKAMEZAWA Hiroyuki * to global LRU. 29171cfb419bSKAMEZAWA Hiroyuki */ 291889b5fae5SJohannes Weiner if (global_reclaim(sc)) { 2919344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, 2920344736f2SVladimir Davydov GFP_KERNEL | __GFP_HARDWALL)) 29211da177e4SLinus Torvalds continue; 292265ec02cbSVladimir Davydov 2923e0887c19SRik van Riel /* 2924e0c23279SMel Gorman * If we already have plenty of memory free for 2925e0c23279SMel Gorman * compaction in this zone, don't free any more. 2926e0c23279SMel Gorman * Even though compaction is invoked for any 2927e0c23279SMel Gorman * non-zero order, only frequent costly order 2928e0c23279SMel Gorman * reclamation is disruptive enough to become a 2929c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 2930c7cfa37bSCopot Alexandru * page allocations. 2931e0887c19SRik van Riel */ 29320b06496aSJohannes Weiner if (IS_ENABLED(CONFIG_COMPACTION) && 29330b06496aSJohannes Weiner sc->order > PAGE_ALLOC_COSTLY_ORDER && 29344f588331SMel Gorman compaction_ready(zone, sc)) { 29350b06496aSJohannes Weiner sc->compaction_ready = true; 2936e0887c19SRik van Riel continue; 2937e0887c19SRik van Riel } 29380b06496aSJohannes Weiner 29390608f43dSAndrew Morton /* 294079dafcdcSMel Gorman * Shrink each node in the zonelist once. If the 294179dafcdcSMel Gorman * zonelist is ordered by zone (not the default) then a 294279dafcdcSMel Gorman * node may be shrunk multiple times but in that case 294379dafcdcSMel Gorman * the user prefers lower zones being preserved. 294479dafcdcSMel Gorman */ 294579dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 294679dafcdcSMel Gorman continue; 294779dafcdcSMel Gorman 294879dafcdcSMel Gorman /* 29490608f43dSAndrew Morton * This steals pages from memory cgroups over softlimit 29500608f43dSAndrew Morton * and returns the number of reclaimed pages and 29510608f43dSAndrew Morton * scanned pages. This works for global memory pressure 29520608f43dSAndrew Morton * and balancing, not for a memcg's limit. 29530608f43dSAndrew Morton */ 29540608f43dSAndrew Morton nr_soft_scanned = 0; 2955ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, 29560608f43dSAndrew Morton sc->order, sc->gfp_mask, 29570608f43dSAndrew Morton &nr_soft_scanned); 29580608f43dSAndrew Morton sc->nr_reclaimed += nr_soft_reclaimed; 29590608f43dSAndrew Morton sc->nr_scanned += nr_soft_scanned; 2960ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 2961ac34a1a3SKAMEZAWA Hiroyuki } 2962d149e3b2SYing Han 296379dafcdcSMel Gorman /* See comment about same check for global reclaim above */ 296479dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 296579dafcdcSMel Gorman continue; 296679dafcdcSMel Gorman last_pgdat = zone->zone_pgdat; 2967970a39a3SMel Gorman shrink_node(zone->zone_pgdat, sc); 29681da177e4SLinus Torvalds } 2969e0c23279SMel Gorman 297065ec02cbSVladimir Davydov /* 2971619d0d76SWeijie Yang * Restore to original mask to avoid the impact on the caller if we 2972619d0d76SWeijie Yang * promoted it to __GFP_HIGHMEM. 2973619d0d76SWeijie Yang */ 2974619d0d76SWeijie Yang sc->gfp_mask = orig_mask; 29751da177e4SLinus Torvalds } 29761da177e4SLinus Torvalds 29772a2e4885SJohannes Weiner static void snapshot_refaults(struct mem_cgroup *root_memcg, pg_data_t *pgdat) 29782a2e4885SJohannes Weiner { 29792a2e4885SJohannes Weiner struct mem_cgroup *memcg; 29802a2e4885SJohannes Weiner 29812a2e4885SJohannes Weiner memcg = mem_cgroup_iter(root_memcg, NULL, NULL); 29822a2e4885SJohannes Weiner do { 29832a2e4885SJohannes Weiner unsigned long refaults; 29842a2e4885SJohannes Weiner struct lruvec *lruvec; 29852a2e4885SJohannes Weiner 29862a2e4885SJohannes Weiner lruvec = mem_cgroup_lruvec(pgdat, memcg); 2987205b20ccSJohannes Weiner refaults = lruvec_page_state_local(lruvec, WORKINGSET_ACTIVATE); 29882a2e4885SJohannes Weiner lruvec->refaults = refaults; 29892a2e4885SJohannes Weiner } while ((memcg = mem_cgroup_iter(root_memcg, memcg, NULL))); 29902a2e4885SJohannes Weiner } 29912a2e4885SJohannes Weiner 29921da177e4SLinus Torvalds /* 29931da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 29941da177e4SLinus Torvalds * 29951da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 29961da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 29971da177e4SLinus Torvalds * 29981da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 29991da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 30005b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 30015b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 30025b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 30035b0830cbSJens Axboe * work, and the allocation attempt will fail. 3004a41f24eaSNishanth Aravamudan * 3005a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 3006a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 30071da177e4SLinus Torvalds */ 3008dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 30093115cd91SVladimir Davydov struct scan_control *sc) 30101da177e4SLinus Torvalds { 3011241994edSJohannes Weiner int initial_priority = sc->priority; 30122a2e4885SJohannes Weiner pg_data_t *last_pgdat; 30132a2e4885SJohannes Weiner struct zoneref *z; 30142a2e4885SJohannes Weiner struct zone *zone; 3015241994edSJohannes Weiner retry: 3016873b4771SKeika Kobayashi delayacct_freepages_start(); 3017873b4771SKeika Kobayashi 301889b5fae5SJohannes Weiner if (global_reclaim(sc)) 30197cc30fcfSMel Gorman __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); 30201da177e4SLinus Torvalds 30219e3b2f8cSKonstantin Khlebnikov do { 302270ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 302370ddf637SAnton Vorontsov sc->priority); 302466e1707bSBalbir Singh sc->nr_scanned = 0; 30250a0337e0SMichal Hocko shrink_zones(zonelist, sc); 3026e0c23279SMel Gorman 3027bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 30280b06496aSJohannes Weiner break; 30290b06496aSJohannes Weiner 30300b06496aSJohannes Weiner if (sc->compaction_ready) 30310b06496aSJohannes Weiner break; 30321da177e4SLinus Torvalds 30331da177e4SLinus Torvalds /* 30340e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 30350e50ce3bSMinchan Kim * writepage even in laptop mode. 30360e50ce3bSMinchan Kim */ 30370e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 30380e50ce3bSMinchan Kim sc->may_writepage = 1; 30390b06496aSJohannes Weiner } while (--sc->priority >= 0); 3040bb21c7ceSKOSAKI Motohiro 30412a2e4885SJohannes Weiner last_pgdat = NULL; 30422a2e4885SJohannes Weiner for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx, 30432a2e4885SJohannes Weiner sc->nodemask) { 30442a2e4885SJohannes Weiner if (zone->zone_pgdat == last_pgdat) 30452a2e4885SJohannes Weiner continue; 30462a2e4885SJohannes Weiner last_pgdat = zone->zone_pgdat; 30472a2e4885SJohannes Weiner snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat); 3048e3c1ac58SAndrey Ryabinin set_memcg_congestion(last_pgdat, sc->target_mem_cgroup, false); 30492a2e4885SJohannes Weiner } 30502a2e4885SJohannes Weiner 3051873b4771SKeika Kobayashi delayacct_freepages_end(); 3052873b4771SKeika Kobayashi 3053bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 3054bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 3055bb21c7ceSKOSAKI Motohiro 30560cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 30570b06496aSJohannes Weiner if (sc->compaction_ready) 30587335084dSMel Gorman return 1; 30597335084dSMel Gorman 3060241994edSJohannes Weiner /* Untapped cgroup reserves? Don't OOM, retry. */ 3061d6622f63SYisheng Xie if (sc->memcg_low_skipped) { 3062241994edSJohannes Weiner sc->priority = initial_priority; 3063d6622f63SYisheng Xie sc->memcg_low_reclaim = 1; 3064d6622f63SYisheng Xie sc->memcg_low_skipped = 0; 3065241994edSJohannes Weiner goto retry; 3066241994edSJohannes Weiner } 3067241994edSJohannes Weiner 3068bb21c7ceSKOSAKI Motohiro return 0; 30691da177e4SLinus Torvalds } 30701da177e4SLinus Torvalds 3071c73322d0SJohannes Weiner static bool allow_direct_reclaim(pg_data_t *pgdat) 30725515061dSMel Gorman { 30735515061dSMel Gorman struct zone *zone; 30745515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 30755515061dSMel Gorman unsigned long free_pages = 0; 30765515061dSMel Gorman int i; 30775515061dSMel Gorman bool wmark_ok; 30785515061dSMel Gorman 3079c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3080c73322d0SJohannes Weiner return true; 3081c73322d0SJohannes Weiner 30825515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 30835515061dSMel Gorman zone = &pgdat->node_zones[i]; 3084d450abd8SJohannes Weiner if (!managed_zone(zone)) 3085d450abd8SJohannes Weiner continue; 3086d450abd8SJohannes Weiner 3087d450abd8SJohannes Weiner if (!zone_reclaimable_pages(zone)) 3088675becceSMel Gorman continue; 3089675becceSMel Gorman 30905515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 30915515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 30925515061dSMel Gorman } 30935515061dSMel Gorman 3094675becceSMel Gorman /* If there are no reserves (unexpected config) then do not throttle */ 3095675becceSMel Gorman if (!pfmemalloc_reserve) 3096675becceSMel Gorman return true; 3097675becceSMel Gorman 30985515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 30995515061dSMel Gorman 31005515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 31015515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 310238087d9bSMel Gorman pgdat->kswapd_classzone_idx = min(pgdat->kswapd_classzone_idx, 31035515061dSMel Gorman (enum zone_type)ZONE_NORMAL); 31045515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 31055515061dSMel Gorman } 31065515061dSMel Gorman 31075515061dSMel Gorman return wmark_ok; 31085515061dSMel Gorman } 31095515061dSMel Gorman 31105515061dSMel Gorman /* 31115515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 31125515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 31135515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 311450694c28SMel Gorman * when the low watermark is reached. 311550694c28SMel Gorman * 311650694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 311750694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 31185515061dSMel Gorman */ 311950694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 31205515061dSMel Gorman nodemask_t *nodemask) 31215515061dSMel Gorman { 3122675becceSMel Gorman struct zoneref *z; 31235515061dSMel Gorman struct zone *zone; 3124675becceSMel Gorman pg_data_t *pgdat = NULL; 31255515061dSMel Gorman 31265515061dSMel Gorman /* 31275515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 31285515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 31295515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 31305515061dSMel Gorman * committing a transaction where throttling it could forcing other 31315515061dSMel Gorman * processes to block on log_wait_commit(). 31325515061dSMel Gorman */ 31335515061dSMel Gorman if (current->flags & PF_KTHREAD) 313450694c28SMel Gorman goto out; 313550694c28SMel Gorman 313650694c28SMel Gorman /* 313750694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 313850694c28SMel Gorman * It should return quickly so it can exit and free its memory 313950694c28SMel Gorman */ 314050694c28SMel Gorman if (fatal_signal_pending(current)) 314150694c28SMel Gorman goto out; 31425515061dSMel Gorman 3143675becceSMel Gorman /* 3144675becceSMel Gorman * Check if the pfmemalloc reserves are ok by finding the first node 3145675becceSMel Gorman * with a usable ZONE_NORMAL or lower zone. The expectation is that 3146675becceSMel Gorman * GFP_KERNEL will be required for allocating network buffers when 3147675becceSMel Gorman * swapping over the network so ZONE_HIGHMEM is unusable. 3148675becceSMel Gorman * 3149675becceSMel Gorman * Throttling is based on the first usable node and throttled processes 3150675becceSMel Gorman * wait on a queue until kswapd makes progress and wakes them. There 3151675becceSMel Gorman * is an affinity then between processes waking up and where reclaim 3152675becceSMel Gorman * progress has been made assuming the process wakes on the same node. 3153675becceSMel Gorman * More importantly, processes running on remote nodes will not compete 3154675becceSMel Gorman * for remote pfmemalloc reserves and processes on different nodes 3155675becceSMel Gorman * should make reasonable progress. 3156675becceSMel Gorman */ 3157675becceSMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 315817636faaSMichael S. Tsirkin gfp_zone(gfp_mask), nodemask) { 3159675becceSMel Gorman if (zone_idx(zone) > ZONE_NORMAL) 3160675becceSMel Gorman continue; 3161675becceSMel Gorman 3162675becceSMel Gorman /* Throttle based on the first usable node */ 31635515061dSMel Gorman pgdat = zone->zone_pgdat; 3164c73322d0SJohannes Weiner if (allow_direct_reclaim(pgdat)) 316550694c28SMel Gorman goto out; 3166675becceSMel Gorman break; 3167675becceSMel Gorman } 3168675becceSMel Gorman 3169675becceSMel Gorman /* If no zone was usable by the allocation flags then do not throttle */ 3170675becceSMel Gorman if (!pgdat) 3171675becceSMel Gorman goto out; 31725515061dSMel Gorman 317368243e76SMel Gorman /* Account for the throttling */ 317468243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 317568243e76SMel Gorman 31765515061dSMel Gorman /* 31775515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 31785515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 31795515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 31805515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 31815515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 31825515061dSMel Gorman * second before continuing. 31835515061dSMel Gorman */ 31845515061dSMel Gorman if (!(gfp_mask & __GFP_FS)) { 31855515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 3186c73322d0SJohannes Weiner allow_direct_reclaim(pgdat), HZ); 318750694c28SMel Gorman 318850694c28SMel Gorman goto check_pending; 31895515061dSMel Gorman } 31905515061dSMel Gorman 31915515061dSMel Gorman /* Throttle until kswapd wakes the process */ 31925515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 3193c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)); 319450694c28SMel Gorman 319550694c28SMel Gorman check_pending: 319650694c28SMel Gorman if (fatal_signal_pending(current)) 319750694c28SMel Gorman return true; 319850694c28SMel Gorman 319950694c28SMel Gorman out: 320050694c28SMel Gorman return false; 32015515061dSMel Gorman } 32025515061dSMel Gorman 3203dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 3204327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 320566e1707bSBalbir Singh { 320633906bc5SMel Gorman unsigned long nr_reclaimed; 320766e1707bSBalbir Singh struct scan_control sc = { 320822fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3209f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 3210b2e18757SMel Gorman .reclaim_idx = gfp_zone(gfp_mask), 3211ee814fe2SJohannes Weiner .order = order, 3212ee814fe2SJohannes Weiner .nodemask = nodemask, 3213ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 3214ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3215a6dc60f8SJohannes Weiner .may_unmap = 1, 32162e2e4259SKOSAKI Motohiro .may_swap = 1, 321766e1707bSBalbir Singh }; 321866e1707bSBalbir Singh 32195515061dSMel Gorman /* 3220bb451fdfSGreg Thelen * scan_control uses s8 fields for order, priority, and reclaim_idx. 3221bb451fdfSGreg Thelen * Confirm they are large enough for max values. 3222bb451fdfSGreg Thelen */ 3223bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_ORDER > S8_MAX); 3224bb451fdfSGreg Thelen BUILD_BUG_ON(DEF_PRIORITY > S8_MAX); 3225bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX); 3226bb451fdfSGreg Thelen 3227bb451fdfSGreg Thelen /* 322850694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 322950694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 323050694c28SMel Gorman * point. 32315515061dSMel Gorman */ 3232f2f43e56SNick Desaulniers if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask)) 32335515061dSMel Gorman return 1; 32345515061dSMel Gorman 32351732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 32363481c37fSYafang Shao trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask); 323733906bc5SMel Gorman 32383115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 323933906bc5SMel Gorman 324033906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 32411732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 324233906bc5SMel Gorman 324333906bc5SMel Gorman return nr_reclaimed; 324466e1707bSBalbir Singh } 324566e1707bSBalbir Singh 3246c255a458SAndrew Morton #ifdef CONFIG_MEMCG 324766e1707bSBalbir Singh 3248d2e5fb92SMichal Hocko /* Only used by soft limit reclaim. Do not reuse for anything else. */ 3249a9dd0a83SMel Gorman unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, 32504e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 3251ef8f2327SMel Gorman pg_data_t *pgdat, 32520ae5e89cSYing Han unsigned long *nr_scanned) 32534e416953SBalbir Singh { 32544e416953SBalbir Singh struct scan_control sc = { 3255b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3256ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 32574e416953SBalbir Singh .may_writepage = !laptop_mode, 32584e416953SBalbir Singh .may_unmap = 1, 3259b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 32604e416953SBalbir Singh .may_swap = !noswap, 32614e416953SBalbir Singh }; 32626b4f7799SJohannes Weiner unsigned long lru_pages; 32630ae5e89cSYing Han 3264d2e5fb92SMichal Hocko WARN_ON_ONCE(!current->reclaim_state); 3265d2e5fb92SMichal Hocko 32664e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 32674e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 3268bdce6d9eSKOSAKI Motohiro 32699e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 32703481c37fSYafang Shao sc.gfp_mask); 3271bdce6d9eSKOSAKI Motohiro 32724e416953SBalbir Singh /* 32734e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 32744e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 3275a9dd0a83SMel Gorman * if we don't reclaim here, the shrink_node from balance_pgdat 32764e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 32774e416953SBalbir Singh * the priority and make it zero. 32784e416953SBalbir Singh */ 3279ef8f2327SMel Gorman shrink_node_memcg(pgdat, memcg, &sc, &lru_pages); 3280bdce6d9eSKOSAKI Motohiro 3281bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 3282bdce6d9eSKOSAKI Motohiro 32830ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 32840308f7cfSYafang Shao 32854e416953SBalbir Singh return sc.nr_reclaimed; 32864e416953SBalbir Singh } 32874e416953SBalbir Singh 328872835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 3289b70a2a21SJohannes Weiner unsigned long nr_pages, 32908c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 3291b70a2a21SJohannes Weiner bool may_swap) 329266e1707bSBalbir Singh { 32934e416953SBalbir Singh struct zonelist *zonelist; 3294bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 3295eb414681SJohannes Weiner unsigned long pflags; 3296889976dbSYing Han int nid; 3297499118e9SVlastimil Babka unsigned int noreclaim_flag; 329866e1707bSBalbir Singh struct scan_control sc = { 3299b70a2a21SJohannes Weiner .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 33007dea19f9SMichal Hocko .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | 3301ee814fe2SJohannes Weiner (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 3302b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 3303ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 3304ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 330566e1707bSBalbir Singh .may_writepage = !laptop_mode, 3306a6dc60f8SJohannes Weiner .may_unmap = 1, 3307b70a2a21SJohannes Weiner .may_swap = may_swap, 3308a09ed5e0SYing Han }; 330966e1707bSBalbir Singh 33101732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 3311889976dbSYing Han /* 3312889976dbSYing Han * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't 3313889976dbSYing Han * take care of from where we get pages. So the node where we start the 3314889976dbSYing Han * scan does not need to be the current node. 3315889976dbSYing Han */ 331672835c86SJohannes Weiner nid = mem_cgroup_select_victim_node(memcg); 3317889976dbSYing Han 3318c9634cf0SAneesh Kumar K.V zonelist = &NODE_DATA(nid)->node_zonelists[ZONELIST_FALLBACK]; 3319bdce6d9eSKOSAKI Motohiro 33203481c37fSYafang Shao trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask); 3321bdce6d9eSKOSAKI Motohiro 3322eb414681SJohannes Weiner psi_memstall_enter(&pflags); 3323499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 3324eb414681SJohannes Weiner 33253115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3326eb414681SJohannes Weiner 3327499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 3328eb414681SJohannes Weiner psi_memstall_leave(&pflags); 3329bdce6d9eSKOSAKI Motohiro 3330bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 33311732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 3332bdce6d9eSKOSAKI Motohiro 3333bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 333466e1707bSBalbir Singh } 333566e1707bSBalbir Singh #endif 333666e1707bSBalbir Singh 33371d82de61SMel Gorman static void age_active_anon(struct pglist_data *pgdat, 3338ef8f2327SMel Gorman struct scan_control *sc) 3339f16015fbSJohannes Weiner { 3340b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 3341b95a2f2dSJohannes Weiner 3342b95a2f2dSJohannes Weiner if (!total_swap_pages) 3343b95a2f2dSJohannes Weiner return; 3344b95a2f2dSJohannes Weiner 3345b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 3346b95a2f2dSJohannes Weiner do { 3347ef8f2327SMel Gorman struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg); 3348f16015fbSJohannes Weiner 33493b991208SJohannes Weiner if (inactive_list_is_low(lruvec, false, sc, true)) 33501a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 33519e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 3352b95a2f2dSJohannes Weiner 3353b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 3354b95a2f2dSJohannes Weiner } while (memcg); 3355f16015fbSJohannes Weiner } 3356f16015fbSJohannes Weiner 33571c30844dSMel Gorman static bool pgdat_watermark_boosted(pg_data_t *pgdat, int classzone_idx) 33581c30844dSMel Gorman { 33591c30844dSMel Gorman int i; 33601c30844dSMel Gorman struct zone *zone; 33611c30844dSMel Gorman 33621c30844dSMel Gorman /* 33631c30844dSMel Gorman * Check for watermark boosts top-down as the higher zones 33641c30844dSMel Gorman * are more likely to be boosted. Both watermarks and boosts 33651c30844dSMel Gorman * should not be checked at the time time as reclaim would 33661c30844dSMel Gorman * start prematurely when there is no boosting and a lower 33671c30844dSMel Gorman * zone is balanced. 33681c30844dSMel Gorman */ 33691c30844dSMel Gorman for (i = classzone_idx; i >= 0; i--) { 33701c30844dSMel Gorman zone = pgdat->node_zones + i; 33711c30844dSMel Gorman if (!managed_zone(zone)) 33721c30844dSMel Gorman continue; 33731c30844dSMel Gorman 33741c30844dSMel Gorman if (zone->watermark_boost) 33751c30844dSMel Gorman return true; 33761c30844dSMel Gorman } 33771c30844dSMel Gorman 33781c30844dSMel Gorman return false; 33791c30844dSMel Gorman } 33801c30844dSMel Gorman 3381e716f2ebSMel Gorman /* 3382e716f2ebSMel Gorman * Returns true if there is an eligible zone balanced for the request order 3383e716f2ebSMel Gorman * and classzone_idx 3384e716f2ebSMel Gorman */ 3385e716f2ebSMel Gorman static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx) 338660cefed4SJohannes Weiner { 3387e716f2ebSMel Gorman int i; 3388e716f2ebSMel Gorman unsigned long mark = -1; 3389e716f2ebSMel Gorman struct zone *zone; 339060cefed4SJohannes Weiner 33911c30844dSMel Gorman /* 33921c30844dSMel Gorman * Check watermarks bottom-up as lower zones are more likely to 33931c30844dSMel Gorman * meet watermarks. 33941c30844dSMel Gorman */ 3395e716f2ebSMel Gorman for (i = 0; i <= classzone_idx; i++) { 3396e716f2ebSMel Gorman zone = pgdat->node_zones + i; 33976256c6b4SMel Gorman 3398e716f2ebSMel Gorman if (!managed_zone(zone)) 3399e716f2ebSMel Gorman continue; 3400e716f2ebSMel Gorman 3401e716f2ebSMel Gorman mark = high_wmark_pages(zone); 3402e716f2ebSMel Gorman if (zone_watermark_ok_safe(zone, order, mark, classzone_idx)) 34036256c6b4SMel Gorman return true; 340460cefed4SJohannes Weiner } 340560cefed4SJohannes Weiner 3406e716f2ebSMel Gorman /* 3407e716f2ebSMel Gorman * If a node has no populated zone within classzone_idx, it does not 3408e716f2ebSMel Gorman * need balancing by definition. This can happen if a zone-restricted 3409e716f2ebSMel Gorman * allocation tries to wake a remote kswapd. 3410e716f2ebSMel Gorman */ 3411e716f2ebSMel Gorman if (mark == -1) 3412e716f2ebSMel Gorman return true; 3413e716f2ebSMel Gorman 3414e716f2ebSMel Gorman return false; 3415e716f2ebSMel Gorman } 3416e716f2ebSMel Gorman 3417631b6e08SMel Gorman /* Clear pgdat state for congested, dirty or under writeback. */ 3418631b6e08SMel Gorman static void clear_pgdat_congested(pg_data_t *pgdat) 3419631b6e08SMel Gorman { 3420631b6e08SMel Gorman clear_bit(PGDAT_CONGESTED, &pgdat->flags); 3421631b6e08SMel Gorman clear_bit(PGDAT_DIRTY, &pgdat->flags); 3422631b6e08SMel Gorman clear_bit(PGDAT_WRITEBACK, &pgdat->flags); 3423631b6e08SMel Gorman } 3424631b6e08SMel Gorman 34251741c877SMel Gorman /* 34265515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 34275515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 34285515061dSMel Gorman * 34295515061dSMel Gorman * Returns true if kswapd is ready to sleep 34305515061dSMel Gorman */ 3431d9f21d42SMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, int classzone_idx) 3432f50de2d3SMel Gorman { 34335515061dSMel Gorman /* 34349e5e3661SVlastimil Babka * The throttled processes are normally woken up in balance_pgdat() as 3435c73322d0SJohannes Weiner * soon as allow_direct_reclaim() is true. But there is a potential 34369e5e3661SVlastimil Babka * race between when kswapd checks the watermarks and a process gets 34379e5e3661SVlastimil Babka * throttled. There is also a potential race if processes get 34389e5e3661SVlastimil Babka * throttled, kswapd wakes, a large process exits thereby balancing the 34399e5e3661SVlastimil Babka * zones, which causes kswapd to exit balance_pgdat() before reaching 34409e5e3661SVlastimil Babka * the wake up checks. If kswapd is going to sleep, no process should 34419e5e3661SVlastimil Babka * be sleeping on pfmemalloc_wait, so wake them now if necessary. If 34429e5e3661SVlastimil Babka * the wake up is premature, processes will wake kswapd and get 34439e5e3661SVlastimil Babka * throttled again. The difference from wake ups in balance_pgdat() is 34449e5e3661SVlastimil Babka * that here we are under prepare_to_wait(). 34455515061dSMel Gorman */ 34469e5e3661SVlastimil Babka if (waitqueue_active(&pgdat->pfmemalloc_wait)) 34479e5e3661SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 3448f50de2d3SMel Gorman 3449c73322d0SJohannes Weiner /* Hopeless node, leave it to direct reclaim */ 3450c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3451c73322d0SJohannes Weiner return true; 3452c73322d0SJohannes Weiner 3453e716f2ebSMel Gorman if (pgdat_balanced(pgdat, order, classzone_idx)) { 3454631b6e08SMel Gorman clear_pgdat_congested(pgdat); 3455333b0a45SShantanu Goel return true; 34561d82de61SMel Gorman } 34571d82de61SMel Gorman 3458333b0a45SShantanu Goel return false; 3459f50de2d3SMel Gorman } 3460f50de2d3SMel Gorman 34611da177e4SLinus Torvalds /* 34621d82de61SMel Gorman * kswapd shrinks a node of pages that are at or below the highest usable 34631d82de61SMel Gorman * zone that is currently unbalanced. 3464b8e83b94SMel Gorman * 3465b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 3466283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 3467283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 346875485363SMel Gorman */ 34691d82de61SMel Gorman static bool kswapd_shrink_node(pg_data_t *pgdat, 3470accf6242SVlastimil Babka struct scan_control *sc) 347175485363SMel Gorman { 34721d82de61SMel Gorman struct zone *zone; 34731d82de61SMel Gorman int z; 347475485363SMel Gorman 34751d82de61SMel Gorman /* Reclaim a number of pages proportional to the number of zones */ 34761d82de61SMel Gorman sc->nr_to_reclaim = 0; 3477970a39a3SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 34781d82de61SMel Gorman zone = pgdat->node_zones + z; 34796aa303deSMel Gorman if (!managed_zone(zone)) 34801d82de61SMel Gorman continue; 34817c954f6dSMel Gorman 34821d82de61SMel Gorman sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); 34837c954f6dSMel Gorman } 34847c954f6dSMel Gorman 34851d82de61SMel Gorman /* 34861d82de61SMel Gorman * Historically care was taken to put equal pressure on all zones but 34871d82de61SMel Gorman * now pressure is applied based on node LRU order. 34881d82de61SMel Gorman */ 3489970a39a3SMel Gorman shrink_node(pgdat, sc); 34901d82de61SMel Gorman 34911d82de61SMel Gorman /* 34921d82de61SMel Gorman * Fragmentation may mean that the system cannot be rebalanced for 34931d82de61SMel Gorman * high-order allocations. If twice the allocation size has been 34941d82de61SMel Gorman * reclaimed then recheck watermarks only at order-0 to prevent 34951d82de61SMel Gorman * excessive reclaim. Assume that a process requested a high-order 34961d82de61SMel Gorman * can direct reclaim/compact. 34971d82de61SMel Gorman */ 34989861a62cSVlastimil Babka if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order)) 34991d82de61SMel Gorman sc->order = 0; 35001d82de61SMel Gorman 3501b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 350275485363SMel Gorman } 350375485363SMel Gorman 350475485363SMel Gorman /* 35051d82de61SMel Gorman * For kswapd, balance_pgdat() will reclaim pages across a node from zones 35061d82de61SMel Gorman * that are eligible for use by the caller until at least one zone is 35071d82de61SMel Gorman * balanced. 35081da177e4SLinus Torvalds * 35091d82de61SMel Gorman * Returns the order kswapd finished reclaiming at. 35101da177e4SLinus Torvalds * 35111da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 351241858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 35138bb4e7a2SWei Yang * found to have free_pages <= high_wmark_pages(zone), any page in that zone 35141d82de61SMel Gorman * or lower is eligible for reclaim until at least one usable zone is 35151d82de61SMel Gorman * balanced. 35161da177e4SLinus Torvalds */ 3517accf6242SVlastimil Babka static int balance_pgdat(pg_data_t *pgdat, int order, int classzone_idx) 35181da177e4SLinus Torvalds { 35191da177e4SLinus Torvalds int i; 35200608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 35210608f43dSAndrew Morton unsigned long nr_soft_scanned; 3522eb414681SJohannes Weiner unsigned long pflags; 35231c30844dSMel Gorman unsigned long nr_boost_reclaim; 35241c30844dSMel Gorman unsigned long zone_boosts[MAX_NR_ZONES] = { 0, }; 35251c30844dSMel Gorman bool boosted; 35261d82de61SMel Gorman struct zone *zone; 3527179e9639SAndrew Morton struct scan_control sc = { 3528179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 3529ee814fe2SJohannes Weiner .order = order, 3530a6dc60f8SJohannes Weiner .may_unmap = 1, 3531179e9639SAndrew Morton }; 353293781325SOmar Sandoval 35331732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 3534eb414681SJohannes Weiner psi_memstall_enter(&pflags); 353593781325SOmar Sandoval __fs_reclaim_acquire(); 353693781325SOmar Sandoval 3537f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 35381da177e4SLinus Torvalds 35391c30844dSMel Gorman /* 35401c30844dSMel Gorman * Account for the reclaim boost. Note that the zone boost is left in 35411c30844dSMel Gorman * place so that parallel allocations that are near the watermark will 35421c30844dSMel Gorman * stall or direct reclaim until kswapd is finished. 35431c30844dSMel Gorman */ 35441c30844dSMel Gorman nr_boost_reclaim = 0; 35451c30844dSMel Gorman for (i = 0; i <= classzone_idx; i++) { 35461c30844dSMel Gorman zone = pgdat->node_zones + i; 35471c30844dSMel Gorman if (!managed_zone(zone)) 35481c30844dSMel Gorman continue; 35491c30844dSMel Gorman 35501c30844dSMel Gorman nr_boost_reclaim += zone->watermark_boost; 35511c30844dSMel Gorman zone_boosts[i] = zone->watermark_boost; 35521c30844dSMel Gorman } 35531c30844dSMel Gorman boosted = nr_boost_reclaim; 35541c30844dSMel Gorman 35551c30844dSMel Gorman restart: 35561c30844dSMel Gorman sc.priority = DEF_PRIORITY; 35579e3b2f8cSKonstantin Khlebnikov do { 3558c73322d0SJohannes Weiner unsigned long nr_reclaimed = sc.nr_reclaimed; 3559b8e83b94SMel Gorman bool raise_priority = true; 35601c30844dSMel Gorman bool balanced; 356193781325SOmar Sandoval bool ret; 3562b8e83b94SMel Gorman 356384c7a777SMel Gorman sc.reclaim_idx = classzone_idx; 35641da177e4SLinus Torvalds 356586c79f6bSMel Gorman /* 356684c7a777SMel Gorman * If the number of buffer_heads exceeds the maximum allowed 356784c7a777SMel Gorman * then consider reclaiming from all zones. This has a dual 356884c7a777SMel Gorman * purpose -- on 64-bit systems it is expected that 356984c7a777SMel Gorman * buffer_heads are stripped during active rotation. On 32-bit 357084c7a777SMel Gorman * systems, highmem pages can pin lowmem memory and shrinking 357184c7a777SMel Gorman * buffers can relieve lowmem pressure. Reclaim may still not 357284c7a777SMel Gorman * go ahead if all eligible zones for the original allocation 357384c7a777SMel Gorman * request are balanced to avoid excessive reclaim from kswapd. 357486c79f6bSMel Gorman */ 357586c79f6bSMel Gorman if (buffer_heads_over_limit) { 357686c79f6bSMel Gorman for (i = MAX_NR_ZONES - 1; i >= 0; i--) { 357786c79f6bSMel Gorman zone = pgdat->node_zones + i; 35786aa303deSMel Gorman if (!managed_zone(zone)) 357986c79f6bSMel Gorman continue; 358086c79f6bSMel Gorman 3581970a39a3SMel Gorman sc.reclaim_idx = i; 358286c79f6bSMel Gorman break; 358386c79f6bSMel Gorman } 358486c79f6bSMel Gorman } 358586c79f6bSMel Gorman 358686c79f6bSMel Gorman /* 35871c30844dSMel Gorman * If the pgdat is imbalanced then ignore boosting and preserve 35881c30844dSMel Gorman * the watermarks for a later time and restart. Note that the 35891c30844dSMel Gorman * zone watermarks will be still reset at the end of balancing 35901c30844dSMel Gorman * on the grounds that the normal reclaim should be enough to 35911c30844dSMel Gorman * re-evaluate if boosting is required when kswapd next wakes. 359286c79f6bSMel Gorman */ 35931c30844dSMel Gorman balanced = pgdat_balanced(pgdat, sc.order, classzone_idx); 35941c30844dSMel Gorman if (!balanced && nr_boost_reclaim) { 35951c30844dSMel Gorman nr_boost_reclaim = 0; 35961c30844dSMel Gorman goto restart; 35971c30844dSMel Gorman } 35981c30844dSMel Gorman 35991c30844dSMel Gorman /* 36001c30844dSMel Gorman * If boosting is not active then only reclaim if there are no 36011c30844dSMel Gorman * eligible zones. Note that sc.reclaim_idx is not used as 36021c30844dSMel Gorman * buffer_heads_over_limit may have adjusted it. 36031c30844dSMel Gorman */ 36041c30844dSMel Gorman if (!nr_boost_reclaim && balanced) 36051da177e4SLinus Torvalds goto out; 3606e1dbeda6SAndrew Morton 36071c30844dSMel Gorman /* Limit the priority of boosting to avoid reclaim writeback */ 36081c30844dSMel Gorman if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2) 36091c30844dSMel Gorman raise_priority = false; 36101c30844dSMel Gorman 36111c30844dSMel Gorman /* 36121c30844dSMel Gorman * Do not writeback or swap pages for boosted reclaim. The 36131c30844dSMel Gorman * intent is to relieve pressure not issue sub-optimal IO 36141c30844dSMel Gorman * from reclaim context. If no pages are reclaimed, the 36151c30844dSMel Gorman * reclaim will be aborted. 36161c30844dSMel Gorman */ 36171c30844dSMel Gorman sc.may_writepage = !laptop_mode && !nr_boost_reclaim; 36181c30844dSMel Gorman sc.may_swap = !nr_boost_reclaim; 36191c30844dSMel Gorman 36201da177e4SLinus Torvalds /* 36211d82de61SMel Gorman * Do some background aging of the anon list, to give 36221d82de61SMel Gorman * pages a chance to be referenced before reclaiming. All 36231d82de61SMel Gorman * pages are rotated regardless of classzone as this is 36241d82de61SMel Gorman * about consistent aging. 36251d82de61SMel Gorman */ 3626ef8f2327SMel Gorman age_active_anon(pgdat, &sc); 36271d82de61SMel Gorman 36281d82de61SMel Gorman /* 3629b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 3630b7ea3c41SMel Gorman * even in laptop mode. 3631b7ea3c41SMel Gorman */ 3632047d72c3SJohannes Weiner if (sc.priority < DEF_PRIORITY - 2) 3633b7ea3c41SMel Gorman sc.may_writepage = 1; 3634b7ea3c41SMel Gorman 36351d82de61SMel Gorman /* Call soft limit reclaim before calling shrink_node. */ 36361da177e4SLinus Torvalds sc.nr_scanned = 0; 36370608f43dSAndrew Morton nr_soft_scanned = 0; 3638ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, 36391d82de61SMel Gorman sc.gfp_mask, &nr_soft_scanned); 36400608f43dSAndrew Morton sc.nr_reclaimed += nr_soft_reclaimed; 36410608f43dSAndrew Morton 364232a4330dSRik van Riel /* 36431d82de61SMel Gorman * There should be no need to raise the scanning priority if 36441d82de61SMel Gorman * enough pages are already being scanned that that high 36451d82de61SMel Gorman * watermark would be met at 100% efficiency. 364632a4330dSRik van Riel */ 3647970a39a3SMel Gorman if (kswapd_shrink_node(pgdat, &sc)) 3648b8e83b94SMel Gorman raise_priority = false; 3649d7868daeSMel Gorman 36505515061dSMel Gorman /* 36515515061dSMel Gorman * If the low watermark is met there is no need for processes 36525515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 36535515061dSMel Gorman * able to safely make forward progress. Wake them 36545515061dSMel Gorman */ 36555515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 3656c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)) 3657cfc51155SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 36585515061dSMel Gorman 3659b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 366093781325SOmar Sandoval __fs_reclaim_release(); 366193781325SOmar Sandoval ret = try_to_freeze(); 366293781325SOmar Sandoval __fs_reclaim_acquire(); 366393781325SOmar Sandoval if (ret || kthread_should_stop()) 3664b8e83b94SMel Gorman break; 3665b8e83b94SMel Gorman 3666b8e83b94SMel Gorman /* 3667b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 3668b8e83b94SMel Gorman * progress in reclaiming pages 3669b8e83b94SMel Gorman */ 3670c73322d0SJohannes Weiner nr_reclaimed = sc.nr_reclaimed - nr_reclaimed; 36711c30844dSMel Gorman nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed); 36721c30844dSMel Gorman 36731c30844dSMel Gorman /* 36741c30844dSMel Gorman * If reclaim made no progress for a boost, stop reclaim as 36751c30844dSMel Gorman * IO cannot be queued and it could be an infinite loop in 36761c30844dSMel Gorman * extreme circumstances. 36771c30844dSMel Gorman */ 36781c30844dSMel Gorman if (nr_boost_reclaim && !nr_reclaimed) 36791c30844dSMel Gorman break; 36801c30844dSMel Gorman 3681c73322d0SJohannes Weiner if (raise_priority || !nr_reclaimed) 3682b8e83b94SMel Gorman sc.priority--; 36831d82de61SMel Gorman } while (sc.priority >= 1); 36841da177e4SLinus Torvalds 3685c73322d0SJohannes Weiner if (!sc.nr_reclaimed) 3686c73322d0SJohannes Weiner pgdat->kswapd_failures++; 3687c73322d0SJohannes Weiner 3688b8e83b94SMel Gorman out: 36891c30844dSMel Gorman /* If reclaim was boosted, account for the reclaim done in this pass */ 36901c30844dSMel Gorman if (boosted) { 36911c30844dSMel Gorman unsigned long flags; 36921c30844dSMel Gorman 36931c30844dSMel Gorman for (i = 0; i <= classzone_idx; i++) { 36941c30844dSMel Gorman if (!zone_boosts[i]) 36951c30844dSMel Gorman continue; 36961c30844dSMel Gorman 36971c30844dSMel Gorman /* Increments are under the zone lock */ 36981c30844dSMel Gorman zone = pgdat->node_zones + i; 36991c30844dSMel Gorman spin_lock_irqsave(&zone->lock, flags); 37001c30844dSMel Gorman zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]); 37011c30844dSMel Gorman spin_unlock_irqrestore(&zone->lock, flags); 37021c30844dSMel Gorman } 37031c30844dSMel Gorman 37041c30844dSMel Gorman /* 37051c30844dSMel Gorman * As there is now likely space, wakeup kcompact to defragment 37061c30844dSMel Gorman * pageblocks. 37071c30844dSMel Gorman */ 37081c30844dSMel Gorman wakeup_kcompactd(pgdat, pageblock_order, classzone_idx); 37091c30844dSMel Gorman } 37101c30844dSMel Gorman 37112a2e4885SJohannes Weiner snapshot_refaults(NULL, pgdat); 371293781325SOmar Sandoval __fs_reclaim_release(); 3713eb414681SJohannes Weiner psi_memstall_leave(&pflags); 37141732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 3715e5ca8071SYafang Shao 37160abdee2bSMel Gorman /* 37171d82de61SMel Gorman * Return the order kswapd stopped reclaiming at as 37181d82de61SMel Gorman * prepare_kswapd_sleep() takes it into account. If another caller 37191d82de61SMel Gorman * entered the allocator slow path while kswapd was awake, order will 37201d82de61SMel Gorman * remain at the higher level. 37210abdee2bSMel Gorman */ 37221d82de61SMel Gorman return sc.order; 37231da177e4SLinus Torvalds } 37241da177e4SLinus Torvalds 3725e716f2ebSMel Gorman /* 3726dffcac2cSShakeel Butt * The pgdat->kswapd_classzone_idx is used to pass the highest zone index to be 3727dffcac2cSShakeel Butt * reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is not 3728dffcac2cSShakeel Butt * a valid index then either kswapd runs for first time or kswapd couldn't sleep 3729dffcac2cSShakeel Butt * after previous reclaim attempt (node is still unbalanced). In that case 3730dffcac2cSShakeel Butt * return the zone index of the previous kswapd reclaim cycle. 3731e716f2ebSMel Gorman */ 3732e716f2ebSMel Gorman static enum zone_type kswapd_classzone_idx(pg_data_t *pgdat, 3733dffcac2cSShakeel Butt enum zone_type prev_classzone_idx) 3734e716f2ebSMel Gorman { 3735e716f2ebSMel Gorman if (pgdat->kswapd_classzone_idx == MAX_NR_ZONES) 3736dffcac2cSShakeel Butt return prev_classzone_idx; 3737dffcac2cSShakeel Butt return pgdat->kswapd_classzone_idx; 3738e716f2ebSMel Gorman } 3739e716f2ebSMel Gorman 374038087d9bSMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, 374138087d9bSMel Gorman unsigned int classzone_idx) 3742f0bc0a60SKOSAKI Motohiro { 3743f0bc0a60SKOSAKI Motohiro long remaining = 0; 3744f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 3745f0bc0a60SKOSAKI Motohiro 3746f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 3747f0bc0a60SKOSAKI Motohiro return; 3748f0bc0a60SKOSAKI Motohiro 3749f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3750f0bc0a60SKOSAKI Motohiro 3751333b0a45SShantanu Goel /* 3752333b0a45SShantanu Goel * Try to sleep for a short interval. Note that kcompactd will only be 3753333b0a45SShantanu Goel * woken if it is possible to sleep for a short interval. This is 3754333b0a45SShantanu Goel * deliberate on the assumption that if reclaim cannot keep an 3755333b0a45SShantanu Goel * eligible zone balanced that it's also unlikely that compaction will 3756333b0a45SShantanu Goel * succeed. 3757333b0a45SShantanu Goel */ 3758d9f21d42SMel Gorman if (prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) { 3759fd901c95SVlastimil Babka /* 3760fd901c95SVlastimil Babka * Compaction records what page blocks it recently failed to 3761fd901c95SVlastimil Babka * isolate pages from and skips them in the future scanning. 3762fd901c95SVlastimil Babka * When kswapd is going to sleep, it is reasonable to assume 3763fd901c95SVlastimil Babka * that pages and compaction may succeed so reset the cache. 3764fd901c95SVlastimil Babka */ 3765fd901c95SVlastimil Babka reset_isolation_suitable(pgdat); 3766fd901c95SVlastimil Babka 3767fd901c95SVlastimil Babka /* 3768fd901c95SVlastimil Babka * We have freed the memory, now we should compact it to make 3769fd901c95SVlastimil Babka * allocation of the requested order possible. 3770fd901c95SVlastimil Babka */ 377138087d9bSMel Gorman wakeup_kcompactd(pgdat, alloc_order, classzone_idx); 3772fd901c95SVlastimil Babka 3773f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 377438087d9bSMel Gorman 377538087d9bSMel Gorman /* 377638087d9bSMel Gorman * If woken prematurely then reset kswapd_classzone_idx and 377738087d9bSMel Gorman * order. The values will either be from a wakeup request or 377838087d9bSMel Gorman * the previous request that slept prematurely. 377938087d9bSMel Gorman */ 378038087d9bSMel Gorman if (remaining) { 3781e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx); 378238087d9bSMel Gorman pgdat->kswapd_order = max(pgdat->kswapd_order, reclaim_order); 378338087d9bSMel Gorman } 378438087d9bSMel Gorman 3785f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3786f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3787f0bc0a60SKOSAKI Motohiro } 3788f0bc0a60SKOSAKI Motohiro 3789f0bc0a60SKOSAKI Motohiro /* 3790f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 3791f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 3792f0bc0a60SKOSAKI Motohiro */ 3793d9f21d42SMel Gorman if (!remaining && 3794d9f21d42SMel Gorman prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) { 3795f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 3796f0bc0a60SKOSAKI Motohiro 3797f0bc0a60SKOSAKI Motohiro /* 3798f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 3799f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 3800f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 3801f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 3802f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 3803f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 3804f0bc0a60SKOSAKI Motohiro */ 3805f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 38061c7e7f6cSAaditya Kumar 38071c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 3808f0bc0a60SKOSAKI Motohiro schedule(); 38091c7e7f6cSAaditya Kumar 3810f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 3811f0bc0a60SKOSAKI Motohiro } else { 3812f0bc0a60SKOSAKI Motohiro if (remaining) 3813f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 3814f0bc0a60SKOSAKI Motohiro else 3815f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 3816f0bc0a60SKOSAKI Motohiro } 3817f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3818f0bc0a60SKOSAKI Motohiro } 3819f0bc0a60SKOSAKI Motohiro 38201da177e4SLinus Torvalds /* 38211da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 38221da177e4SLinus Torvalds * from the init process. 38231da177e4SLinus Torvalds * 38241da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 38251da177e4SLinus Torvalds * free memory available even if there is no other activity 38261da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 38271da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 38281da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 38291da177e4SLinus Torvalds * 38301da177e4SLinus Torvalds * If there are applications that are active memory-allocators 38311da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 38321da177e4SLinus Torvalds */ 38331da177e4SLinus Torvalds static int kswapd(void *p) 38341da177e4SLinus Torvalds { 3835e716f2ebSMel Gorman unsigned int alloc_order, reclaim_order; 3836e716f2ebSMel Gorman unsigned int classzone_idx = MAX_NR_ZONES - 1; 38371da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 38381da177e4SLinus Torvalds struct task_struct *tsk = current; 3839a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 38401da177e4SLinus Torvalds 3841174596a0SRusty Russell if (!cpumask_empty(cpumask)) 3842c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 38431da177e4SLinus Torvalds 38441da177e4SLinus Torvalds /* 38451da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 38461da177e4SLinus Torvalds * and that if we need more memory we should get access to it 38471da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 38481da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 38491da177e4SLinus Torvalds * 38501da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 38511da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 38521da177e4SLinus Torvalds * page out something else, and this flag essentially protects 38531da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 38541da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 38551da177e4SLinus Torvalds */ 3856930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 385783144186SRafael J. Wysocki set_freezable(); 38581da177e4SLinus Torvalds 3859e716f2ebSMel Gorman pgdat->kswapd_order = 0; 3860e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = MAX_NR_ZONES; 38611da177e4SLinus Torvalds for ( ; ; ) { 38626f6313d4SJeff Liu bool ret; 38633e1d1d28SChristoph Lameter 3864e716f2ebSMel Gorman alloc_order = reclaim_order = pgdat->kswapd_order; 3865e716f2ebSMel Gorman classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx); 3866e716f2ebSMel Gorman 386738087d9bSMel Gorman kswapd_try_sleep: 386838087d9bSMel Gorman kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, 386938087d9bSMel Gorman classzone_idx); 3870215ddd66SMel Gorman 387138087d9bSMel Gorman /* Read the new order and classzone_idx */ 387238087d9bSMel Gorman alloc_order = reclaim_order = pgdat->kswapd_order; 3873dffcac2cSShakeel Butt classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx); 387438087d9bSMel Gorman pgdat->kswapd_order = 0; 3875e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = MAX_NR_ZONES; 38761da177e4SLinus Torvalds 38778fe23e05SDavid Rientjes ret = try_to_freeze(); 38788fe23e05SDavid Rientjes if (kthread_should_stop()) 38798fe23e05SDavid Rientjes break; 38808fe23e05SDavid Rientjes 38818fe23e05SDavid Rientjes /* 38828fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 38838fe23e05SDavid Rientjes * after returning from the refrigerator 3884b1296cc4SRafael J. Wysocki */ 388538087d9bSMel Gorman if (ret) 388638087d9bSMel Gorman continue; 38871d82de61SMel Gorman 388838087d9bSMel Gorman /* 388938087d9bSMel Gorman * Reclaim begins at the requested order but if a high-order 389038087d9bSMel Gorman * reclaim fails then kswapd falls back to reclaiming for 389138087d9bSMel Gorman * order-0. If that happens, kswapd will consider sleeping 389238087d9bSMel Gorman * for the order it finished reclaiming at (reclaim_order) 389338087d9bSMel Gorman * but kcompactd is woken to compact for the original 389438087d9bSMel Gorman * request (alloc_order). 389538087d9bSMel Gorman */ 3896e5146b12SMel Gorman trace_mm_vmscan_kswapd_wake(pgdat->node_id, classzone_idx, 3897e5146b12SMel Gorman alloc_order); 389838087d9bSMel Gorman reclaim_order = balance_pgdat(pgdat, alloc_order, classzone_idx); 389938087d9bSMel Gorman if (reclaim_order < alloc_order) 390038087d9bSMel Gorman goto kswapd_try_sleep; 390133906bc5SMel Gorman } 3902b0a8cc58STakamori Yamaguchi 390371abdc15SJohannes Weiner tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); 390471abdc15SJohannes Weiner 39051da177e4SLinus Torvalds return 0; 39061da177e4SLinus Torvalds } 39071da177e4SLinus Torvalds 39081da177e4SLinus Torvalds /* 39095ecd9d40SDavid Rientjes * A zone is low on free memory or too fragmented for high-order memory. If 39105ecd9d40SDavid Rientjes * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's 39115ecd9d40SDavid Rientjes * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim 39125ecd9d40SDavid Rientjes * has failed or is not needed, still wake up kcompactd if only compaction is 39135ecd9d40SDavid Rientjes * needed. 39141da177e4SLinus Torvalds */ 39155ecd9d40SDavid Rientjes void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, 39165ecd9d40SDavid Rientjes enum zone_type classzone_idx) 39171da177e4SLinus Torvalds { 39181da177e4SLinus Torvalds pg_data_t *pgdat; 39191da177e4SLinus Torvalds 39206aa303deSMel Gorman if (!managed_zone(zone)) 39211da177e4SLinus Torvalds return; 39221da177e4SLinus Torvalds 39235ecd9d40SDavid Rientjes if (!cpuset_zone_allowed(zone, gfp_flags)) 39241da177e4SLinus Torvalds return; 392588f5acf8SMel Gorman pgdat = zone->zone_pgdat; 3926dffcac2cSShakeel Butt 3927dffcac2cSShakeel Butt if (pgdat->kswapd_classzone_idx == MAX_NR_ZONES) 3928dffcac2cSShakeel Butt pgdat->kswapd_classzone_idx = classzone_idx; 3929dffcac2cSShakeel Butt else 3930dffcac2cSShakeel Butt pgdat->kswapd_classzone_idx = max(pgdat->kswapd_classzone_idx, 3931e716f2ebSMel Gorman classzone_idx); 393238087d9bSMel Gorman pgdat->kswapd_order = max(pgdat->kswapd_order, order); 39338d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 39341da177e4SLinus Torvalds return; 3935e1a55637SMel Gorman 39365ecd9d40SDavid Rientjes /* Hopeless node, leave it to direct reclaim if possible */ 39375ecd9d40SDavid Rientjes if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES || 39381c30844dSMel Gorman (pgdat_balanced(pgdat, order, classzone_idx) && 39391c30844dSMel Gorman !pgdat_watermark_boosted(pgdat, classzone_idx))) { 39405ecd9d40SDavid Rientjes /* 39415ecd9d40SDavid Rientjes * There may be plenty of free memory available, but it's too 39425ecd9d40SDavid Rientjes * fragmented for high-order allocations. Wake up kcompactd 39435ecd9d40SDavid Rientjes * and rely on compaction_suitable() to determine if it's 39445ecd9d40SDavid Rientjes * needed. If it fails, it will defer subsequent attempts to 39455ecd9d40SDavid Rientjes * ratelimit its work. 39465ecd9d40SDavid Rientjes */ 39475ecd9d40SDavid Rientjes if (!(gfp_flags & __GFP_DIRECT_RECLAIM)) 39485ecd9d40SDavid Rientjes wakeup_kcompactd(pgdat, order, classzone_idx); 3949c73322d0SJohannes Weiner return; 39505ecd9d40SDavid Rientjes } 3951c73322d0SJohannes Weiner 39525ecd9d40SDavid Rientjes trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, classzone_idx, order, 39535ecd9d40SDavid Rientjes gfp_flags); 39548d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 39551da177e4SLinus Torvalds } 39561da177e4SLinus Torvalds 3957c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 39581da177e4SLinus Torvalds /* 39597b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 3960d6277db4SRafael J. Wysocki * freed pages. 3961d6277db4SRafael J. Wysocki * 3962d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 3963d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 3964d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 39651da177e4SLinus Torvalds */ 39667b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 39671da177e4SLinus Torvalds { 3968d6277db4SRafael J. Wysocki struct scan_control sc = { 39697b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 3970ee814fe2SJohannes Weiner .gfp_mask = GFP_HIGHUSER_MOVABLE, 3971b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 39729e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 3973ee814fe2SJohannes Weiner .may_writepage = 1, 3974ee814fe2SJohannes Weiner .may_unmap = 1, 3975ee814fe2SJohannes Weiner .may_swap = 1, 3976ee814fe2SJohannes Weiner .hibernation_mode = 1, 39771da177e4SLinus Torvalds }; 39787b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 39797b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 3980499118e9SVlastimil Babka unsigned int noreclaim_flag; 39811da177e4SLinus Torvalds 3982d92a8cfcSPeter Zijlstra fs_reclaim_acquire(sc.gfp_mask); 398393781325SOmar Sandoval noreclaim_flag = memalloc_noreclaim_save(); 39841732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 3985d6277db4SRafael J. Wysocki 39863115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3987d6277db4SRafael J. Wysocki 39881732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 3989499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 399093781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 3991d6277db4SRafael J. Wysocki 39927b51755cSKOSAKI Motohiro return nr_reclaimed; 39931da177e4SLinus Torvalds } 3994c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 39951da177e4SLinus Torvalds 39961da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 39971da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 39981da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 39991da177e4SLinus Torvalds restore their cpu bindings. */ 4000517bbed9SSebastian Andrzej Siewior static int kswapd_cpu_online(unsigned int cpu) 40011da177e4SLinus Torvalds { 400258c0a4a7SYasunori Goto int nid; 40031da177e4SLinus Torvalds 400448fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) { 4005c5f59f08SMike Travis pg_data_t *pgdat = NODE_DATA(nid); 4006a70f7302SRusty Russell const struct cpumask *mask; 4007a70f7302SRusty Russell 4008a70f7302SRusty Russell mask = cpumask_of_node(pgdat->node_id); 4009c5f59f08SMike Travis 40103e597945SRusty Russell if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 40111da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 4012c5f59f08SMike Travis set_cpus_allowed_ptr(pgdat->kswapd, mask); 40131da177e4SLinus Torvalds } 4014517bbed9SSebastian Andrzej Siewior return 0; 40151da177e4SLinus Torvalds } 40161da177e4SLinus Torvalds 40173218ae14SYasunori Goto /* 40183218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 40193218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 40203218ae14SYasunori Goto */ 40213218ae14SYasunori Goto int kswapd_run(int nid) 40223218ae14SYasunori Goto { 40233218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 40243218ae14SYasunori Goto int ret = 0; 40253218ae14SYasunori Goto 40263218ae14SYasunori Goto if (pgdat->kswapd) 40273218ae14SYasunori Goto return 0; 40283218ae14SYasunori Goto 40293218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 40303218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 40313218ae14SYasunori Goto /* failure at boot is fatal */ 4032c6202adfSThomas Gleixner BUG_ON(system_state < SYSTEM_RUNNING); 4033d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 4034d5dc0ad9SGavin Shan ret = PTR_ERR(pgdat->kswapd); 4035d72515b8SXishi Qiu pgdat->kswapd = NULL; 40363218ae14SYasunori Goto } 40373218ae14SYasunori Goto return ret; 40383218ae14SYasunori Goto } 40393218ae14SYasunori Goto 40408fe23e05SDavid Rientjes /* 4041d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 4042bfc8c901SVladimir Davydov * hold mem_hotplug_begin/end(). 40438fe23e05SDavid Rientjes */ 40448fe23e05SDavid Rientjes void kswapd_stop(int nid) 40458fe23e05SDavid Rientjes { 40468fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 40478fe23e05SDavid Rientjes 4048d8adde17SJiang Liu if (kswapd) { 40498fe23e05SDavid Rientjes kthread_stop(kswapd); 4050d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 4051d8adde17SJiang Liu } 40528fe23e05SDavid Rientjes } 40538fe23e05SDavid Rientjes 40541da177e4SLinus Torvalds static int __init kswapd_init(void) 40551da177e4SLinus Torvalds { 4056517bbed9SSebastian Andrzej Siewior int nid, ret; 405769e05944SAndrew Morton 40581da177e4SLinus Torvalds swap_setup(); 405948fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 40603218ae14SYasunori Goto kswapd_run(nid); 4061517bbed9SSebastian Andrzej Siewior ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, 4062517bbed9SSebastian Andrzej Siewior "mm/vmscan:online", kswapd_cpu_online, 4063517bbed9SSebastian Andrzej Siewior NULL); 4064517bbed9SSebastian Andrzej Siewior WARN_ON(ret < 0); 40651da177e4SLinus Torvalds return 0; 40661da177e4SLinus Torvalds } 40671da177e4SLinus Torvalds 40681da177e4SLinus Torvalds module_init(kswapd_init) 40699eeff239SChristoph Lameter 40709eeff239SChristoph Lameter #ifdef CONFIG_NUMA 40719eeff239SChristoph Lameter /* 4072a5f5f91dSMel Gorman * Node reclaim mode 40739eeff239SChristoph Lameter * 4074a5f5f91dSMel Gorman * If non-zero call node_reclaim when the number of free pages falls below 40759eeff239SChristoph Lameter * the watermarks. 40769eeff239SChristoph Lameter */ 4077a5f5f91dSMel Gorman int node_reclaim_mode __read_mostly; 40789eeff239SChristoph Lameter 40791b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 40807d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ 40811b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 408295bbc0c7SZhihui Zhang #define RECLAIM_UNMAP (1<<2) /* Unmap pages during reclaim */ 40831b2ffb78SChristoph Lameter 40849eeff239SChristoph Lameter /* 4085a5f5f91dSMel Gorman * Priority for NODE_RECLAIM. This determines the fraction of pages 4086a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 4087a92f7126SChristoph Lameter * a zone. 4088a92f7126SChristoph Lameter */ 4089a5f5f91dSMel Gorman #define NODE_RECLAIM_PRIORITY 4 4090a92f7126SChristoph Lameter 40919eeff239SChristoph Lameter /* 4092a5f5f91dSMel Gorman * Percentage of pages in a zone that must be unmapped for node_reclaim to 40939614634fSChristoph Lameter * occur. 40949614634fSChristoph Lameter */ 40959614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 40969614634fSChristoph Lameter 40979614634fSChristoph Lameter /* 40980ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 40990ff38490SChristoph Lameter * slab reclaim needs to occur. 41000ff38490SChristoph Lameter */ 41010ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 41020ff38490SChristoph Lameter 410311fb9989SMel Gorman static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) 410490afa5deSMel Gorman { 410511fb9989SMel Gorman unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); 410611fb9989SMel Gorman unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + 410711fb9989SMel Gorman node_page_state(pgdat, NR_ACTIVE_FILE); 410890afa5deSMel Gorman 410990afa5deSMel Gorman /* 411090afa5deSMel Gorman * It's possible for there to be more file mapped pages than 411190afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 411290afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 411390afa5deSMel Gorman */ 411490afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 411590afa5deSMel Gorman } 411690afa5deSMel Gorman 411790afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 4118a5f5f91dSMel Gorman static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) 411990afa5deSMel Gorman { 4120d031a157SAlexandru Moise unsigned long nr_pagecache_reclaimable; 4121d031a157SAlexandru Moise unsigned long delta = 0; 412290afa5deSMel Gorman 412390afa5deSMel Gorman /* 412495bbc0c7SZhihui Zhang * If RECLAIM_UNMAP is set, then all file pages are considered 412590afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 412611fb9989SMel Gorman * pages like swapcache and node_unmapped_file_pages() provides 412790afa5deSMel Gorman * a better estimate 412890afa5deSMel Gorman */ 4129a5f5f91dSMel Gorman if (node_reclaim_mode & RECLAIM_UNMAP) 4130a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); 413190afa5deSMel Gorman else 4132a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); 413390afa5deSMel Gorman 413490afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 4135a5f5f91dSMel Gorman if (!(node_reclaim_mode & RECLAIM_WRITE)) 4136a5f5f91dSMel Gorman delta += node_page_state(pgdat, NR_FILE_DIRTY); 413790afa5deSMel Gorman 413890afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 413990afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 414090afa5deSMel Gorman delta = nr_pagecache_reclaimable; 414190afa5deSMel Gorman 414290afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 414390afa5deSMel Gorman } 414490afa5deSMel Gorman 41450ff38490SChristoph Lameter /* 4146a5f5f91dSMel Gorman * Try to free up some pages from this node through reclaim. 41479eeff239SChristoph Lameter */ 4148a5f5f91dSMel Gorman static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 41499eeff239SChristoph Lameter { 41507fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 415169e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 41529eeff239SChristoph Lameter struct task_struct *p = current; 4153499118e9SVlastimil Babka unsigned int noreclaim_flag; 4154179e9639SAndrew Morton struct scan_control sc = { 415562b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 4156f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 4157bd2f6199SJohannes Weiner .order = order, 4158a5f5f91dSMel Gorman .priority = NODE_RECLAIM_PRIORITY, 4159a5f5f91dSMel Gorman .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), 4160a5f5f91dSMel Gorman .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), 4161ee814fe2SJohannes Weiner .may_swap = 1, 4162f2f43e56SNick Desaulniers .reclaim_idx = gfp_zone(gfp_mask), 4163179e9639SAndrew Morton }; 41649eeff239SChristoph Lameter 4165132bb8cfSYafang Shao trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order, 4166132bb8cfSYafang Shao sc.gfp_mask); 4167132bb8cfSYafang Shao 41689eeff239SChristoph Lameter cond_resched(); 416993781325SOmar Sandoval fs_reclaim_acquire(sc.gfp_mask); 4170d4f7796eSChristoph Lameter /* 417195bbc0c7SZhihui Zhang * We need to be able to allocate from the reserves for RECLAIM_UNMAP 4172d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 417395bbc0c7SZhihui Zhang * and RECLAIM_UNMAP. 4174d4f7796eSChristoph Lameter */ 4175499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 4176499118e9SVlastimil Babka p->flags |= PF_SWAPWRITE; 41771732d2b0SAndrew Morton set_task_reclaim_state(p, &sc.reclaim_state); 4178c84db23cSChristoph Lameter 4179a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) { 4180a92f7126SChristoph Lameter /* 4181894befecSAndrey Ryabinin * Free memory by calling shrink node with increasing 41820ff38490SChristoph Lameter * priorities until we have enough memory freed. 4183a92f7126SChristoph Lameter */ 4184a92f7126SChristoph Lameter do { 4185970a39a3SMel Gorman shrink_node(pgdat, &sc); 41869e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 41870ff38490SChristoph Lameter } 4188a92f7126SChristoph Lameter 41891732d2b0SAndrew Morton set_task_reclaim_state(p, NULL); 4190499118e9SVlastimil Babka current->flags &= ~PF_SWAPWRITE; 4191499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 419293781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 4193132bb8cfSYafang Shao 4194132bb8cfSYafang Shao trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed); 4195132bb8cfSYafang Shao 4196a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 41979eeff239SChristoph Lameter } 4198179e9639SAndrew Morton 4199a5f5f91dSMel Gorman int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 4200179e9639SAndrew Morton { 4201d773ed6bSDavid Rientjes int ret; 4202179e9639SAndrew Morton 4203179e9639SAndrew Morton /* 4204a5f5f91dSMel Gorman * Node reclaim reclaims unmapped file backed pages and 42050ff38490SChristoph Lameter * slab pages if we are over the defined limits. 420634aa1330SChristoph Lameter * 42079614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 42089614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 4209a5f5f91dSMel Gorman * thrown out if the node is overallocated. So we do not reclaim 4210a5f5f91dSMel Gorman * if less than a specified percentage of the node is used by 42119614634fSChristoph Lameter * unmapped file backed pages. 4212179e9639SAndrew Morton */ 4213a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && 4214385386cfSJohannes Weiner node_page_state(pgdat, NR_SLAB_RECLAIMABLE) <= pgdat->min_slab_pages) 4215a5f5f91dSMel Gorman return NODE_RECLAIM_FULL; 4216179e9639SAndrew Morton 4217179e9639SAndrew Morton /* 4218d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 4219179e9639SAndrew Morton */ 4220d0164adcSMel Gorman if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) 4221a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4222179e9639SAndrew Morton 4223179e9639SAndrew Morton /* 4224a5f5f91dSMel Gorman * Only run node reclaim on the local node or on nodes that do not 4225179e9639SAndrew Morton * have associated processors. This will favor the local processor 4226179e9639SAndrew Morton * over remote processors and spread off node memory allocations 4227179e9639SAndrew Morton * as wide as possible. 4228179e9639SAndrew Morton */ 4229a5f5f91dSMel Gorman if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) 4230a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4231d773ed6bSDavid Rientjes 4232a5f5f91dSMel Gorman if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) 4233a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4234fa5e084eSMel Gorman 4235a5f5f91dSMel Gorman ret = __node_reclaim(pgdat, gfp_mask, order); 4236a5f5f91dSMel Gorman clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); 4237d773ed6bSDavid Rientjes 423824cf7251SMel Gorman if (!ret) 423924cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 424024cf7251SMel Gorman 4241d773ed6bSDavid Rientjes return ret; 4242179e9639SAndrew Morton } 42439eeff239SChristoph Lameter #endif 4244894bc310SLee Schermerhorn 4245894bc310SLee Schermerhorn /* 4246894bc310SLee Schermerhorn * page_evictable - test whether a page is evictable 4247894bc310SLee Schermerhorn * @page: the page to test 4248894bc310SLee Schermerhorn * 4249894bc310SLee Schermerhorn * Test whether page is evictable--i.e., should be placed on active/inactive 425039b5f29aSHugh Dickins * lists vs unevictable list. 4251894bc310SLee Schermerhorn * 4252894bc310SLee Schermerhorn * Reasons page might not be evictable: 4253ba9ddf49SLee Schermerhorn * (1) page's mapping marked unevictable 4254b291f000SNick Piggin * (2) page is part of an mlocked VMA 4255ba9ddf49SLee Schermerhorn * 4256894bc310SLee Schermerhorn */ 425739b5f29aSHugh Dickins int page_evictable(struct page *page) 4258894bc310SLee Schermerhorn { 4259e92bb4ddSHuang Ying int ret; 4260e92bb4ddSHuang Ying 4261e92bb4ddSHuang Ying /* Prevent address_space of inode and swap cache from being freed */ 4262e92bb4ddSHuang Ying rcu_read_lock(); 4263e92bb4ddSHuang Ying ret = !mapping_unevictable(page_mapping(page)) && !PageMlocked(page); 4264e92bb4ddSHuang Ying rcu_read_unlock(); 4265e92bb4ddSHuang Ying return ret; 4266894bc310SLee Schermerhorn } 426789e004eaSLee Schermerhorn 426889e004eaSLee Schermerhorn /** 426964e3d12fSKuo-Hsin Yang * check_move_unevictable_pages - check pages for evictability and move to 427064e3d12fSKuo-Hsin Yang * appropriate zone lru list 427164e3d12fSKuo-Hsin Yang * @pvec: pagevec with lru pages to check 427289e004eaSLee Schermerhorn * 427364e3d12fSKuo-Hsin Yang * Checks pages for evictability, if an evictable page is in the unevictable 427464e3d12fSKuo-Hsin Yang * lru list, moves it to the appropriate evictable lru list. This function 427564e3d12fSKuo-Hsin Yang * should be only used for lru pages. 427689e004eaSLee Schermerhorn */ 427764e3d12fSKuo-Hsin Yang void check_move_unevictable_pages(struct pagevec *pvec) 427889e004eaSLee Schermerhorn { 4279925b7673SJohannes Weiner struct lruvec *lruvec; 4280785b99feSMel Gorman struct pglist_data *pgdat = NULL; 428124513264SHugh Dickins int pgscanned = 0; 428224513264SHugh Dickins int pgrescued = 0; 428389e004eaSLee Schermerhorn int i; 428489e004eaSLee Schermerhorn 428564e3d12fSKuo-Hsin Yang for (i = 0; i < pvec->nr; i++) { 428664e3d12fSKuo-Hsin Yang struct page *page = pvec->pages[i]; 4287785b99feSMel Gorman struct pglist_data *pagepgdat = page_pgdat(page); 428889e004eaSLee Schermerhorn 428924513264SHugh Dickins pgscanned++; 4290785b99feSMel Gorman if (pagepgdat != pgdat) { 4291785b99feSMel Gorman if (pgdat) 4292785b99feSMel Gorman spin_unlock_irq(&pgdat->lru_lock); 4293785b99feSMel Gorman pgdat = pagepgdat; 4294785b99feSMel Gorman spin_lock_irq(&pgdat->lru_lock); 429589e004eaSLee Schermerhorn } 4296785b99feSMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 429789e004eaSLee Schermerhorn 429824513264SHugh Dickins if (!PageLRU(page) || !PageUnevictable(page)) 429924513264SHugh Dickins continue; 430089e004eaSLee Schermerhorn 430139b5f29aSHugh Dickins if (page_evictable(page)) { 430224513264SHugh Dickins enum lru_list lru = page_lru_base_type(page); 430324513264SHugh Dickins 4304309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 430524513264SHugh Dickins ClearPageUnevictable(page); 4306fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE); 4307fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 430824513264SHugh Dickins pgrescued++; 430989e004eaSLee Schermerhorn } 431089e004eaSLee Schermerhorn } 431124513264SHugh Dickins 4312785b99feSMel Gorman if (pgdat) { 431324513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 431424513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 4315785b99feSMel Gorman spin_unlock_irq(&pgdat->lru_lock); 431624513264SHugh Dickins } 431785046579SHugh Dickins } 431864e3d12fSKuo-Hsin Yang EXPORT_SYMBOL_GPL(check_move_unevictable_pages); 4319